FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/urio.c
1 /*-
2 * Copyright (c) 2000 Iwasa Kazmi
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson.
27 * This code includes software developed by the NetBSD Foundation, Inc. and
28 * its contributors.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: releng/7.3/sys/dev/usb/urio.c 170969 2007-06-21 14:42:34Z imp $");
33
34
35 /*
36 * 2000/3/24 added NetBSD/OpenBSD support (from Alex Nemirovsky)
37 * 2000/3/07 use two bulk-pipe handles for read and write (Dirk)
38 * 2000/3/06 change major number(143), and copyright header
39 * some fix for 4.0 (Dirk)
40 * 2000/3/05 codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
41 * 2000/3/01 remove retry code from urioioctl()
42 * change method of bulk transfer (no interrupt)
43 * 2000/2/28 small fixes for new rio_usb.h
44 * 2000/2/24 first version.
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/bus.h>
53 #include <sys/ioccom.h>
54 #include <sys/fcntl.h>
55 #include <sys/filio.h>
56 #include <sys/conf.h>
57 #include <sys/uio.h>
58 #include <sys/tty.h>
59 #include <sys/file.h>
60 #include <sys/selinfo.h>
61 #include <sys/poll.h>
62 #include <sys/sysctl.h>
63 #include <sys/uio.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68
69 #include "usbdevs.h"
70 #include <dev/usb/rio500_usb.h>
71
72 #ifdef USB_DEBUG
73 #define DPRINTF(x) if (uriodebug) printf x
74 #define DPRINTFN(n,x) if (uriodebug>(n)) printf x
75 int uriodebug = 0;
76 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
77 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
78 &uriodebug, 0, "urio debug level");
79 #else
80 #define DPRINTF(x)
81 #define DPRINTFN(n,x)
82 #endif
83
84 /* difference of usbd interface */
85 #define USBDI 1
86
87 #define RIO_OUT 0
88 #define RIO_IN 1
89 #define RIO_NODIR 2
90
91 d_open_t urioopen;
92 d_close_t urioclose;
93 d_read_t urioread;
94 d_write_t uriowrite;
95 d_ioctl_t urioioctl;
96
97
98 static struct cdevsw urio_cdevsw = {
99 .d_version = D_VERSION,
100 .d_flags = D_NEEDGIANT,
101 .d_open = urioopen,
102 .d_close = urioclose,
103 .d_read = urioread,
104 .d_write = uriowrite,
105 .d_ioctl = urioioctl,
106 .d_name = "urio",
107 };
108 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
109 ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
110 RIO_NODIR))
111
112 #define URIO_BBSIZE 1024
113
114 struct urio_softc {
115 device_t sc_dev;
116 usbd_device_handle sc_udev;
117 usbd_interface_handle sc_iface;
118
119 int sc_opened;
120 usbd_pipe_handle sc_pipeh_in;
121 usbd_pipe_handle sc_pipeh_out;
122 int sc_epaddr[2];
123
124 int sc_refcnt;
125 struct cdev *sc_dev_t;
126 u_char sc_dying;
127 };
128
129 #define URIOUNIT(n) (minor(n))
130
131 #define RIO_RW_TIMEOUT 4000 /* ms */
132
133 static device_probe_t urio_match;
134 static device_attach_t urio_attach;
135 static device_detach_t urio_detach;
136
137 static device_method_t urio_methods[] = {
138 /* Device interface */
139 DEVMETHOD(device_probe, urio_match),
140 DEVMETHOD(device_attach, urio_attach),
141 DEVMETHOD(device_detach, urio_detach),
142
143 { 0, 0 }
144 };
145
146 static driver_t urio_driver = {
147 "urio",
148 urio_methods,
149 sizeof(struct urio_softc)
150 };
151
152 static devclass_t urio_devclass;
153
154 static int
155 urio_match(device_t self)
156 {
157 struct usb_attach_arg *uaa = device_get_ivars(self);
158 usb_device_descriptor_t *dd;
159
160 DPRINTFN(10,("urio_match\n"));
161 if (!uaa->iface)
162 return UMATCH_NONE;
163
164 dd = usbd_get_device_descriptor(uaa->device);
165
166 if (dd &&
167 ((UGETW(dd->idVendor) == USB_VENDOR_DIAMOND &&
168 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND_RIO500USB) ||
169 (UGETW(dd->idVendor) == USB_VENDOR_DIAMOND2 &&
170 (UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO600USB ||
171 UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO800USB))))
172 return UMATCH_VENDOR_PRODUCT;
173 else
174 return UMATCH_NONE;
175 }
176
177 static int
178 urio_attach(device_t self)
179 {
180 struct urio_softc *sc = device_get_softc(self);
181 struct usb_attach_arg *uaa = device_get_ivars(self);
182 usbd_device_handle udev;
183 usbd_interface_handle iface;
184 u_int8_t epcount;
185 usbd_status r;
186 char * ermsg = "<none>";
187 int i;
188
189 DPRINTFN(10,("urio_attach: sc=%p\n", sc));
190 sc->sc_dev = self;
191 sc->sc_udev = udev = uaa->device;
192
193 if ((!uaa->device) || (!uaa->iface)) {
194 ermsg = "device or iface";
195 goto nobulk;
196 }
197 sc->sc_iface = iface = uaa->iface;
198 sc->sc_opened = 0;
199 sc->sc_pipeh_in = 0;
200 sc->sc_pipeh_out = 0;
201 sc->sc_refcnt = 0;
202
203 r = usbd_endpoint_count(iface, &epcount);
204 if (r != USBD_NORMAL_COMPLETION) {
205 ermsg = "endpoints";
206 goto nobulk;
207 }
208
209 sc->sc_epaddr[RIO_OUT] = 0xff;
210 sc->sc_epaddr[RIO_IN] = 0x00;
211
212 for (i = 0; i < epcount; i++) {
213 usb_endpoint_descriptor_t *edesc =
214 usbd_interface2endpoint_descriptor(iface, i);
215 int d;
216
217 if (!edesc) {
218 ermsg = "interface endpoint";
219 goto nobulk;
220 }
221
222 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
223 if (d != RIO_NODIR)
224 sc->sc_epaddr[d] = edesc->bEndpointAddress;
225 }
226 if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
227 sc->sc_epaddr[RIO_IN] == 0x00) {
228 ermsg = "Rio I&O";
229 goto nobulk;
230 }
231
232 sc->sc_dev_t = make_dev(&urio_cdevsw, device_get_unit(self),
233 UID_ROOT, GID_OPERATOR,
234 0644, "urio%d", device_get_unit(self));
235 DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
236
237 return 0;
238
239 nobulk:
240 printf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
241 return ENXIO;
242 }
243
244
245 int
246 urioopen(struct cdev *dev, int flag, int mode, struct thread *p)
247 {
248 struct urio_softc * sc;
249 int unit = URIOUNIT(dev);
250 sc = devclass_get_softc(urio_devclass, unit);
251 if (sc == NULL)
252 return (ENXIO);
253
254 DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
255 flag, mode, unit));
256
257 if (sc->sc_opened)
258 return EBUSY;
259
260 if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
261 return EACCES;
262
263 sc->sc_opened = 1;
264 sc->sc_pipeh_in = 0;
265 sc->sc_pipeh_out = 0;
266 if (usbd_open_pipe(sc->sc_iface,
267 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
268 != USBD_NORMAL_COMPLETION)
269 {
270 sc->sc_pipeh_in = 0;
271 return EIO;
272 };
273 if (usbd_open_pipe(sc->sc_iface,
274 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
275 != USBD_NORMAL_COMPLETION)
276 {
277 usbd_close_pipe(sc->sc_pipeh_in);
278 sc->sc_pipeh_in = 0;
279 sc->sc_pipeh_out = 0;
280 return EIO;
281 };
282 return 0;
283 }
284
285 int
286 urioclose(struct cdev *dev, int flag, int mode, struct thread *p)
287 {
288 struct urio_softc * sc;
289 int unit = URIOUNIT(dev);
290 sc = devclass_get_softc(urio_devclass, unit);
291
292 DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n", flag, mode, unit));
293 if (sc->sc_pipeh_in)
294 usbd_close_pipe(sc->sc_pipeh_in);
295
296 if (sc->sc_pipeh_out)
297 usbd_close_pipe(sc->sc_pipeh_out);
298
299 sc->sc_pipeh_in = 0;
300 sc->sc_pipeh_out = 0;
301 sc->sc_opened = 0;
302 sc->sc_refcnt = 0;
303 return 0;
304 }
305
306 int
307 urioread(struct cdev *dev, struct uio *uio, int flag)
308 {
309 struct urio_softc * sc;
310 usbd_xfer_handle reqh;
311 int unit = URIOUNIT(dev);
312 usbd_status r;
313 char buf[URIO_BBSIZE];
314 u_int32_t n, tn;
315 int error = 0;
316
317 sc = devclass_get_softc(urio_devclass, unit);
318
319 DPRINTFN(5, ("urioread: %d\n", unit));
320 if (!sc->sc_opened)
321 return EIO;
322
323 sc->sc_refcnt++;
324 reqh = usbd_alloc_xfer(sc->sc_udev);
325 if (reqh == 0)
326 return ENOMEM;
327 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
328 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
329 tn = n;
330 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
331 0, RIO_RW_TIMEOUT, 0);
332 r = usbd_sync_transfer(reqh);
333 if (r != USBD_NORMAL_COMPLETION) {
334 DPRINTFN(1, ("urioread: error=%d\n", r));
335 usbd_clear_endpoint_stall(sc->sc_pipeh_in);
336 tn = 0;
337 error = EIO;
338 break;
339 }
340 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
341
342 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
343 error = uiomove(buf, tn, uio);
344 if (error || tn < n)
345 break;
346 }
347 usbd_free_xfer(reqh);
348 return error;
349 }
350
351 int
352 uriowrite(struct cdev *dev, struct uio *uio, int flag)
353 {
354 struct urio_softc * sc;
355 usbd_xfer_handle reqh;
356 int unit = URIOUNIT(dev);
357 usbd_status r;
358 char buf[URIO_BBSIZE];
359 u_int32_t n;
360 int error = 0;
361
362 sc = devclass_get_softc(urio_devclass, unit);
363 DPRINTFN(5, ("uriowrite: %d\n", unit));
364 if (!sc->sc_opened)
365 return EIO;
366
367 sc->sc_refcnt++;
368 reqh = usbd_alloc_xfer(sc->sc_udev);
369 if (reqh == 0)
370 return EIO;
371 while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
372 error = uiomove(buf, n, uio);
373 if (error)
374 break;
375 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
376 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
377 0, RIO_RW_TIMEOUT, 0);
378 r = usbd_sync_transfer(reqh);
379 if (r != USBD_NORMAL_COMPLETION) {
380 DPRINTFN(1, ("uriowrite: error=%d\n", r));
381 usbd_clear_endpoint_stall(sc->sc_pipeh_out);
382 error = EIO;
383 break;
384 }
385 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
386 }
387
388 usbd_free_xfer(reqh);
389 return error;
390 }
391
392
393 int
394 urioioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *p)
395 {
396 struct urio_softc * sc;
397 int unit = URIOUNIT(dev);
398 struct RioCommand *rio_cmd;
399 int requesttype, len;
400 struct iovec iov;
401 struct uio uio;
402 usb_device_request_t req;
403 int req_flags = 0, req_actlen = 0;
404 void *ptr = 0;
405 int error = 0;
406 usbd_status r;
407
408 sc = devclass_get_softc(urio_devclass, unit);
409 switch (cmd) {
410 case RIO_RECV_COMMAND:
411 if (!(flag & FWRITE))
412 return EPERM;
413 rio_cmd = (struct RioCommand *)addr;
414 if (rio_cmd == NULL)
415 return EINVAL;
416 len = rio_cmd->length;
417
418 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
419 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
420 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
421 break;
422
423 case RIO_SEND_COMMAND:
424 if (!(flag & FWRITE))
425 return EPERM;
426 rio_cmd = (struct RioCommand *)addr;
427 if (rio_cmd == NULL)
428 return EINVAL;
429 len = rio_cmd->length;
430
431 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
432 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
433 requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
434 break;
435
436 default:
437 return EINVAL;
438 break;
439 }
440
441 /* Send rio control message */
442 req.bmRequestType = requesttype;
443 req.bRequest = rio_cmd->request;
444 USETW(req.wValue, rio_cmd->value);
445 USETW(req.wIndex, rio_cmd->index);
446 USETW(req.wLength, len);
447
448 if (len < 0 || len > 32767)
449 return EINVAL;
450 if (len != 0) {
451 iov.iov_base = (caddr_t)rio_cmd->buffer;
452 iov.iov_len = len;
453 uio.uio_iov = &iov;
454 uio.uio_iovcnt = 1;
455 uio.uio_resid = len;
456 uio.uio_offset = 0;
457 uio.uio_segflg = UIO_USERSPACE;
458 uio.uio_rw =
459 req.bmRequestType & UT_READ ?
460 UIO_READ : UIO_WRITE;
461 uio.uio_td = p;
462 ptr = malloc(len, M_TEMP, M_WAITOK);
463 if (uio.uio_rw == UIO_WRITE) {
464 error = uiomove(ptr, len, &uio);
465 if (error)
466 goto ret;
467 }
468 }
469
470 r = usbd_do_request_flags(sc->sc_udev, &req,
471 ptr, req_flags, &req_actlen,
472 USBD_DEFAULT_TIMEOUT);
473 if (r == USBD_NORMAL_COMPLETION) {
474 error = 0;
475 if (len != 0) {
476 if (uio.uio_rw == UIO_READ) {
477 error = uiomove(ptr, len, &uio);
478 }
479 }
480 } else {
481 error = EIO;
482 }
483
484 ret:
485 if (ptr)
486 free(ptr, M_TEMP);
487 return error;
488 }
489
490 static int
491 urio_detach(device_t self)
492 {
493 struct urio_softc *sc = device_get_softc(self);
494 int s;
495
496 DPRINTF(("urio_detach: sc=%p\n", sc));
497 sc->sc_dying = 1;
498 if (sc->sc_pipeh_in)
499 usbd_abort_pipe(sc->sc_pipeh_in);
500
501 if (sc->sc_pipeh_out)
502 usbd_abort_pipe(sc->sc_pipeh_out);
503
504 s = splusb();
505 if (--sc->sc_refcnt >= 0) {
506 /* Wait for processes to go away. */
507 usb_detach_wait(sc->sc_dev);
508 }
509 splx(s);
510
511 destroy_dev(sc->sc_dev_t);
512 /* XXX not implemented yet */
513 device_set_desc(self, NULL);
514 return 0;
515 }
516
517 MODULE_DEPEND(uscanner, usb, 1, 1, 1);
518 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);
Cache object: 1d014c0f23fd82c7d272f5c0782cc8c7
|