summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/1394/adapters/hci1394_ioctl.c
blob: 8a529680fb49effcaf0c0a888f424ade13dacd58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * hci1394_ioctl.c
 *   Test ioctl's to support test/debug of the 1394 HW. hci1394_ioctl_enum_t is
 *   passed in cmd and a pointer to the appropriate structure (i.e.
 *   hci1394_ioctl_wrreg_t) is passed in arg.
 */

#include <sys/conf.h>
#include <sys/modctl.h>
#include <sys/mkdev.h>
#include <sys/cred.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>

#include <sys/1394/h1394.h>
#include <sys/1394/adapters/hci1394.h>
#include <sys/1394/adapters/hci1394_extern.h>
#include <sys/1394/adapters/hci1394_ioctl.h>


/* HCI1394_IOCTL_READ_SELFID for 32-bit apps in 64-bit kernel */
typedef struct hci1394_ioctl_readselfid32_s {
	uint32_t buf;
	uint_t count;
} hci1394_ioctl_readselfid32_t;


static int hci1394_ioctl_wrreg(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_rdreg(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_wrvreg(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_rdvreg(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_selfid_cnt(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_busgen_cnt(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_wrphy(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_rdphy(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_hbainfo(hci1394_state_t *soft_state, void *arg,
    int mode);
static int hci1394_ioctl_read_selfid(hci1394_state_t *soft_state, void *arg,
    int mode);
#ifdef	_MULTI_DATAMODEL
static int hci1394_ioctl_read_selfid32(hci1394_state_t *soft_state,
    hci1394_ioctl_readselfid32_t *read_selfid, int mode);
#endif


/* ARGSUSED */
int
hci1394_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
    int *rvalp)
{
	hci1394_state_t *soft_state;
	int instance;
	int status;

	instance = getminor(dev);
	if (instance == -1) {
		return (EBADF);
	}

	soft_state = ddi_get_soft_state(hci1394_statep, instance);
	if (soft_state == NULL) {
		return (EBADF);
	}

	status = 0;

	switch (cmd) {
	case HCI1394_IOCTL_WRITE_REG:
		status = hci1394_ioctl_wrreg(soft_state, (void *)arg, mode);
		break;
	case HCI1394_IOCTL_READ_REG:
		status = hci1394_ioctl_rdreg(soft_state, (void *)arg, mode);
		break;
	case HCI1394_IOCTL_READ_VREG:
		status = hci1394_ioctl_rdvreg(soft_state, (void *)arg, mode);
		break;
	case HCI1394_IOCTL_WRITE_VREG:
		status = hci1394_ioctl_wrvreg(soft_state, (void *)arg, mode);
		break;
	case HCI1394_IOCTL_RESET_BUS:
		status = hci1394_ohci_bus_reset(soft_state->ohci);
		break;
	case HCI1394_IOCTL_SELFID_CNT:
		status = hci1394_ioctl_selfid_cnt(soft_state, (void *)arg,
		    mode);
		break;
	case HCI1394_IOCTL_BUSGEN_CNT:
		status = hci1394_ioctl_busgen_cnt(soft_state, (void *)arg,
		    mode);
		break;
	case HCI1394_IOCTL_READ_SELFID:
		status = hci1394_ioctl_read_selfid(soft_state, (void *)arg,
		    mode);
		break;
	case HCI1394_IOCTL_READ_PHY:
		status = hci1394_ioctl_rdphy(soft_state, (void *)arg, mode);
		break;
	case HCI1394_IOCTL_WRITE_PHY:
		status = hci1394_ioctl_wrphy(soft_state, (void *)arg, mode);
		break;
	case HCI1394_IOCTL_HBA_INFO:
		status = hci1394_ioctl_hbainfo(soft_state, (void *)arg, mode);
		break;
	default:
		/*
		 * if we don't know what the ioctl is, forward it on to the
		 * services layer.  The services layer will handle the devctl
		 * ioctl's along with any services layer private ioctls that
		 * it has defined.
		 */
		status = h1394_ioctl(soft_state->drvinfo.di_sl_private, cmd,
		    arg, mode, credp, rvalp);
		break;
	}

	return (status);
}


static int
hci1394_ioctl_wrreg(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_wrreg_t wrreg;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	status = ddi_copyin(arg, &wrreg, sizeof (hci1394_ioctl_wrreg_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	hci1394_ohci_reg_write(soft_state->ohci, wrreg.addr, wrreg.data);

	return (0);
}


static int
hci1394_ioctl_rdreg(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_rdreg_t rdreg;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	status = ddi_copyin(arg, &rdreg, sizeof (hci1394_ioctl_rdreg_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	hci1394_ohci_reg_read(soft_state->ohci, rdreg.addr, &rdreg.data);

	status = ddi_copyout(&rdreg, arg, sizeof (hci1394_ioctl_rdreg_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	return (0);
}


static int
hci1394_ioctl_wrvreg(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_wrvreg_t wrvreg;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	status = ddi_copyin(arg, &wrvreg, sizeof (hci1394_ioctl_wrvreg_t),
	    mode);
	if (status != 0) {
		return (EFAULT);
	}

	status = hci1394_vendor_reg_write(soft_state->vendor,
	    wrvreg.regset, wrvreg.addr, wrvreg.data);
	if (status != DDI_SUCCESS) {
		return (EINVAL);
	}

	return (0);
}


static int
hci1394_ioctl_rdvreg(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_rdvreg_t rdvreg;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	status = ddi_copyin(arg, &rdvreg, sizeof (hci1394_ioctl_rdvreg_t),
	    mode);
	if (status != 0) {
		return (EFAULT);
	}

	status = hci1394_vendor_reg_read(soft_state->vendor,
	    rdvreg.regset, rdvreg.addr, &rdvreg.data);
	if (status != DDI_SUCCESS) {
		return (EINVAL);
	}

	status = ddi_copyout(&rdvreg, arg, sizeof (hci1394_ioctl_rdvreg_t),
	    mode);
	if (status != 0) {
		return (EFAULT);
	}

	return (0);
}


static int
hci1394_ioctl_selfid_cnt(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_selfid_cnt_t selfid_cnt;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	selfid_cnt.count = soft_state->drvinfo.di_stats.st_selfid_count;

	status = ddi_copyout(&selfid_cnt, arg,
	    sizeof (hci1394_ioctl_selfid_cnt_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	return (0);
}


static int
hci1394_ioctl_busgen_cnt(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_busgen_cnt_t busgen_cnt;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	busgen_cnt.count = hci1394_ohci_current_busgen(soft_state->ohci);

	status = ddi_copyout(&busgen_cnt, arg,
	    sizeof (hci1394_ioctl_busgen_cnt_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	return (0);
}


static int
hci1394_ioctl_wrphy(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_wrphy_t wrphy;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	status = ddi_copyin(arg, &wrphy, sizeof (hci1394_ioctl_wrphy_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	status = hci1394_ohci_phy_write(soft_state->ohci, wrphy.addr,
	    wrphy.data);
	if (status != DDI_SUCCESS) {
		return (EINVAL);
	}

	return (0);
}


static int
hci1394_ioctl_rdphy(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_rdphy_t rdphy;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	status = ddi_copyin(arg, &rdphy, sizeof (hci1394_ioctl_rdphy_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	status = hci1394_ohci_phy_read(soft_state->ohci, rdphy.addr,
	    &rdphy.data);
	if (status != DDI_SUCCESS) {
		return (EINVAL);
	}

	status = ddi_copyout(&rdphy, arg, sizeof (hci1394_ioctl_rdphy_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	return (0);
}


static int
hci1394_ioctl_hbainfo(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_hbainfo_t hbainfo;
	int status;


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

	hbainfo.pci_vendor_id = soft_state->vendor_info.vendor_id;
	hbainfo.pci_device_id = soft_state->vendor_info.device_id;
	hbainfo.pci_revision_id = soft_state->vendor_info.revision_id;
	hbainfo.ohci_version = soft_state->vendor_info.ohci_version;
	hbainfo.ohci_vendor_id = soft_state->vendor_info.ohci_vendor_id;
	hbainfo.ohci_vregset_cnt = soft_state->vendor_info.vendor_reg_count;

	status = ddi_copyout(&hbainfo, arg, sizeof (hci1394_ioctl_hbainfo_t),
	    mode);
	if (status != 0) {
		return (EFAULT);
	}

	return (0);
}


static int
hci1394_ioctl_read_selfid(hci1394_state_t *soft_state, void *arg, int mode)
{
	hci1394_ioctl_read_selfid_t read_selfid;
	int status;
	uint_t offset;
	uint32_t data;
#ifdef	_MULTI_DATAMODEL
	hci1394_ioctl_readselfid32_t read_selfid32;
#endif


	ASSERT(soft_state != NULL);
	ASSERT(arg != NULL);

#ifdef	_MULTI_DATAMODEL
	switch (ddi_model_convert_from(mode & FMODELS)) {

		/* 32-bit app in 64-bit kernel */
	case DDI_MODEL_ILP32:
		/* copy in the 32-bit version of the args */
		status = ddi_copyin(arg, &read_selfid32,
		    sizeof (hci1394_ioctl_readselfid32_t), mode);
		if (status != 0) {
			return (EFAULT);
		}

		/*
		 * Use a special function to process the 32-bit user address
		 * pointer embedded in the structure we pass in arg.
		 */
		status = hci1394_ioctl_read_selfid32(soft_state,
		    &read_selfid32, mode);
		return (status);
	default:
		break;
	}
#endif

	/*
	 * if we got here, we either are a 64-bit app in a 64-bit kernel or a
	 * 32-bit app in a 32-bit kernel
	 */

	/* copy in the args. We don't need to do any special conversions */
	status = ddi_copyin(arg, &read_selfid,
	    sizeof (hci1394_ioctl_read_selfid_t), mode);
	if (status != 0) {
		return (EFAULT);
	}

	/*
	 * make sure we are not trying to copy more data than the selfid buffer
	 * can hold.  count is in quadlets and max_selfid_size is in bytes.
	 */
	if ((read_selfid.count * 4) > OHCI_MAX_SELFID_SIZE) {
		return (EINVAL);
	}

	/*
	 * copy the selfid buffer one word at a time into the user buffer. The
	 * combination between having to do ddi_get32's (for endian reasons)
	 * and a ddi_copyout() make it easier to do it one word at a time.
	 */
	for (offset = 0; offset < read_selfid.count; offset++) {
		/* read word from selfid buffer */
		hci1394_ohci_selfid_read(soft_state->ohci, offset, &data);

		/* copy the selfid word into the user buffer */
		status = ddi_copyout(&data, &read_selfid.buf[offset], 4, mode);
		if (status != 0) {
			return (EFAULT);
		}
	}

	return (0);
}


#ifdef	_MULTI_DATAMODEL
static int
hci1394_ioctl_read_selfid32(hci1394_state_t *soft_state,
    hci1394_ioctl_readselfid32_t *read_selfid, int mode)
{
	int status;
	uint_t offset;
	uint32_t data;


	ASSERT(soft_state != NULL);
	ASSERT(read_selfid != NULL);

	/*
	 * make sure we are not trying to copy more data than the selfid buffer
	 * can hold.  count is in quadlets and max_selfid_size is in bytes.
	 */
	if ((read_selfid->count * 4) > OHCI_MAX_SELFID_SIZE) {
		return (EINVAL);
	}

	/*
	 * copy the selfid buffer one word at a time into the user buffer. The
	 * combination between having to do ddi_get32's (for endian reasons) and
	 * a ddi_copyout() make it easier to do it one word at a time.
	 */
	for (offset = 0; offset < read_selfid->count; offset++) {
		/* read word from selfid buffer */
		hci1394_ohci_selfid_read(soft_state->ohci, offset, &data);
		/* copy the selfid word into the user buffer */
		status = ddi_copyout(&data,
		    (void *)(uintptr_t)(read_selfid->buf + (offset * 4)),
		    4, mode);
		if (status != 0) {
			return (EFAULT);
		}
	}

	return (0);
}
#endif