Merge from vendor branch LESS:
[dragonfly.git] / sys / dev / usbmisc / urio / 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 /*
32  * $FreeBSD: src/sys/dev/usb/urio.c,v 1.28 2003/08/25 22:01:06 joe Exp $
33  * $DragonFly: src/sys/dev/usbmisc/urio/urio.c,v 1.15 2006/12/22 23:26:26 swildner Exp $
34  */
35
36 /*
37  * 2000/3/24  added NetBSD/OpenBSD support (from Alex Nemirovsky)
38  * 2000/3/07  use two bulk-pipe handles for read and write (Dirk)
39  * 2000/3/06  change major number(143), and copyright header
40  *            some fix for 4.0 (Dirk)
41  * 2000/3/05  codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
42  * 2000/3/01  remove retry code from urioioctl()
43  *            change method of bulk transfer (no interrupt)
44  * 2000/2/28  small fixes for new rio_usb.h
45  * 2000/2/24  first version.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #if defined(__NetBSD__)
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #elif defined(__FreeBSD__) || defined(__DragonFly__)
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <sys/ioccom.h>
59 #endif
60 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/conf.h>
63 #include <sys/uio.h>
64 #include <sys/tty.h>
65 #include <sys/file.h>
66 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
67 #include <sys/selinfo.h>
68 #else
69 #include <sys/select.h>
70 #endif
71 #include <sys/vnode.h>
72 #include <sys/poll.h>
73 #include <sys/sysctl.h>
74 #include <sys/proc.h>
75 #include <sys/thread2.h>
76
77 #include <bus/usb/usb.h>
78 #include <bus/usb/usbdi.h>
79 #include <bus/usb/usbdi_util.h>
80
81 #include <bus/usb/usbdevs.h>
82 #include <bus/usb/rio500_usb.h>
83
84 #ifdef USB_DEBUG
85 #define DPRINTF(x)      if (uriodebug) logprintf x
86 #define DPRINTFN(n,x)   if (uriodebug>(n)) logprintf x
87 int     uriodebug = 0;
88 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
89 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
90            &uriodebug, 0, "urio debug level");
91 #else
92 #define DPRINTF(x)
93 #define DPRINTFN(n,x)
94 #endif
95
96 /* difference of usbd interface */
97 #define USBDI 1
98
99 #define RIO_OUT 0
100 #define RIO_IN  1
101 #define RIO_NODIR  2
102
103 #if defined(__NetBSD__)
104 int urioopen(cdev_t, int, int, struct proc *);
105 int urioclose(cdev_t, int, int, struct proc *p);
106 int urioread(cdev_t, struct uio *uio, int);
107 int uriowrite(cdev_t, struct uio *uio, int);
108 int urioioctl(cdev_t, u_long, caddr_t, int, struct proc *);
109
110 cdev_decl(urio);
111 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
112                           ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
113                                                            RIO_NODIR))
114 #elif defined(__FreeBSD__) || defined(__DragonFly__)
115 d_open_t  urioopen;
116 d_close_t urioclose;
117 d_read_t  urioread;
118 d_write_t uriowrite;
119 d_ioctl_t urioioctl;
120
121 #define URIO_CDEV_MAJOR 143
122
123 Static struct dev_ops urio_ops = {
124         { "urio", URIO_CDEV_MAJOR, 0 },
125         .d_open =       urioopen,
126         .d_close =      urioclose,
127         .d_read =       urioread,
128         .d_write =      uriowrite,
129         .d_ioctl =      urioioctl,
130 };
131 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
132                           ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
133                                                            RIO_NODIR))
134 #endif  /*defined(__FreeBSD__)*/
135
136 #define URIO_BBSIZE     1024
137
138 struct urio_softc {
139         USBBASEDEVICE sc_dev;
140         usbd_device_handle sc_udev;
141         usbd_interface_handle sc_iface;
142
143         int sc_opened;
144         usbd_pipe_handle sc_pipeh_in;
145         usbd_pipe_handle sc_pipeh_out;
146         int sc_epaddr[2];
147
148         int sc_refcnt;
149 #if defined(__NetBSD__) || defined(__OpenBSD__)
150         u_char sc_dying;
151 #endif
152 };
153
154 #define URIOUNIT(n) (minor(n))
155
156 #define RIO_RW_TIMEOUT 4000     /* ms */
157
158 USB_DECLARE_DRIVER(urio);
159
160 USB_MATCH(urio)
161 {
162         USB_MATCH_START(urio, uaa);
163         usb_device_descriptor_t *dd;
164
165         DPRINTFN(10,("urio_match\n"));
166         if (!uaa->iface)
167                 return UMATCH_NONE;
168
169         dd = usbd_get_device_descriptor(uaa->device);
170
171         if (dd &&
172             ((UGETW(dd->idVendor) == USB_VENDOR_DIAMOND &&
173             UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND_RIO500USB) ||
174             (UGETW(dd->idVendor) == USB_VENDOR_DIAMOND2 &&
175               (UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO600USB ||
176               UGETW(dd->idProduct) == USB_PRODUCT_DIAMOND2_RIO800USB))))
177                 return UMATCH_VENDOR_PRODUCT;
178         else
179                 return UMATCH_NONE;
180 }
181
182 USB_ATTACH(urio)
183 {
184         USB_ATTACH_START(urio, sc, uaa);
185         char devinfo[1024];
186         usbd_device_handle udev;
187         usbd_interface_handle iface;
188         u_int8_t epcount;
189 #if defined(__NetBSD__) || defined(__OpenBSD__)
190         u_int8_t niface;
191 #endif
192         usbd_status r;
193         char * ermsg = "<none>";
194         int i;
195
196         DPRINTFN(10,("urio_attach: sc=%p\n", sc));
197         usbd_devinfo(uaa->device, 0, devinfo);
198         USB_ATTACH_SETUP;
199         kprintf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
200
201         sc->sc_udev = udev = uaa->device;
202
203 #if defined(__FreeBSD__) || defined(__DragonFly__)
204         if ((!uaa->device) || (!uaa->iface)) {
205                 ermsg = "device or iface";
206                 goto nobulk;
207         }
208         sc->sc_iface = iface = uaa->iface;
209 #elif defined(__NetBSD__) || defined(__OpenBSD__)
210         if (!udev) {
211                 ermsg = "device";
212                 goto nobulk;
213         }
214         r = usbd_interface_count(udev, &niface);
215         if (r) {
216                 ermsg = "iface";
217                 goto nobulk;
218         }
219         r = usbd_device2interface_handle(udev, 0, &iface);
220         if (r) {
221                 ermsg = "iface";
222                 goto nobulk;
223         }
224         sc->sc_iface = iface;
225 #endif
226         sc->sc_opened = 0;
227         sc->sc_pipeh_in = 0;
228         sc->sc_pipeh_out = 0;
229         sc->sc_refcnt = 0;
230
231         r = usbd_endpoint_count(iface, &epcount);
232         if (r != USBD_NORMAL_COMPLETION) {
233                 ermsg = "endpoints";
234                 goto nobulk;
235         }
236
237         sc->sc_epaddr[RIO_OUT] = 0xff;
238         sc->sc_epaddr[RIO_IN] = 0x00;
239
240         for (i = 0; i < epcount; i++) {
241                 usb_endpoint_descriptor_t *edesc =
242                         usbd_interface2endpoint_descriptor(iface, i);
243                 int d;
244
245                 if (!edesc) {
246                         ermsg = "interface endpoint";
247                         goto nobulk;
248                 }
249
250                 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
251                 if (d != RIO_NODIR)
252                         sc->sc_epaddr[d] = edesc->bEndpointAddress;
253         }
254         if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
255              sc->sc_epaddr[RIO_IN] == 0x00) {
256                 ermsg = "Rio I&O";
257                 goto nobulk;
258         }
259
260 #if defined(__FreeBSD__) || defined(__DragonFly__)
261         dev_ops_add(&urio_ops, -1, device_get_unit(self));
262         make_dev(&urio_ops, device_get_unit(self),
263                         UID_ROOT, GID_OPERATOR,
264                         0644, "urio%d", device_get_unit(self));
265 #elif defined(__NetBSD__) || defined(__OpenBSD__)
266         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
267                            USBDEV(sc->sc_dev));
268 #endif
269
270         DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
271
272         USB_ATTACH_SUCCESS_RETURN;
273
274  nobulk:
275         kprintf("%s: could not find %s\n", USBDEVNAME(sc->sc_dev),ermsg);
276         USB_ATTACH_ERROR_RETURN;
277 }
278
279
280 int
281 urioopen(struct dev_open_args *ap)
282 {
283         cdev_t dev = ap->a_head.a_dev;
284 #if (USBDI >= 1)
285         struct urio_softc * sc;
286 #endif
287         int unit = URIOUNIT(dev);
288         USB_GET_SC_OPEN(urio, unit, sc);
289
290         DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
291                      ap->a_oflags, ap->a_devtype, unit));
292
293         if (sc->sc_opened)
294                 return EBUSY;
295
296         if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
297                 return EACCES;
298
299         sc->sc_opened = 1;
300         sc->sc_pipeh_in = 0;
301         sc->sc_pipeh_out = 0;
302         if (usbd_open_pipe(sc->sc_iface,
303                 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
304                         != USBD_NORMAL_COMPLETION)
305         {
306                         sc->sc_pipeh_in = 0;
307                         return EIO;
308         };
309         if (usbd_open_pipe(sc->sc_iface,
310                 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
311                         != USBD_NORMAL_COMPLETION)
312         {
313                         usbd_close_pipe(sc->sc_pipeh_in);
314                         sc->sc_pipeh_in = 0;
315                         sc->sc_pipeh_out = 0;
316                         return EIO;
317         };
318         return 0;
319 }
320
321 int
322 urioclose(struct dev_close_args *ap)
323 {
324         cdev_t dev = ap->a_head.a_dev;
325 #if (USBDI >= 1)
326         struct urio_softc * sc;
327 #endif
328         int unit = URIOUNIT(dev);
329         USB_GET_SC(urio, unit, sc);
330
331         DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n",
332                 ap->a_fflag, ap->a_devtype, unit));
333         if (sc->sc_pipeh_in)
334                 usbd_close_pipe(sc->sc_pipeh_in);
335
336         if (sc->sc_pipeh_out)
337                 usbd_close_pipe(sc->sc_pipeh_out);
338
339         sc->sc_pipeh_in = 0;
340         sc->sc_pipeh_out = 0;
341         sc->sc_opened = 0;
342         sc->sc_refcnt = 0;
343         return 0;
344 }
345
346 int
347 urioread(struct dev_read_args *ap)
348 {
349         cdev_t dev = ap->a_head.a_dev;
350         struct uio *uio = ap->a_uio;
351 #if (USBDI >= 1)
352         struct urio_softc * sc;
353         usbd_xfer_handle reqh;
354 #else
355         usbd_request_handle reqh;
356         usbd_private_handle r_priv;
357         void *r_buff;
358         usbd_status r_status;
359 #endif
360         int unit = URIOUNIT(dev);
361         usbd_status r;
362         char buf[URIO_BBSIZE];
363         u_int32_t n, tn;
364         int error = 0;
365
366         USB_GET_SC(urio, unit, sc);
367
368         DPRINTFN(5, ("urioread: %d\n", unit));
369         if (!sc->sc_opened)
370                 return EIO;
371
372 #if (USBDI >= 1)
373         sc->sc_refcnt++;
374         reqh = usbd_alloc_xfer(sc->sc_udev);
375 #else
376         reqh = usbd_alloc_request();
377 #endif
378         if (reqh == 0)
379                 return ENOMEM;
380         while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
381                 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
382                 tn = n;
383 #if (USBDI >= 1)
384                 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
385                                        0, RIO_RW_TIMEOUT, 0);
386 #else
387                 r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
388                                        0, RIO_RW_TIMEOUT, 0);
389                 if (r != USBD_NORMAL_COMPLETION) {
390                         error = EIO;
391                         break;
392                 }
393 #endif
394                 r = usbd_sync_transfer(reqh);
395                 if (r != USBD_NORMAL_COMPLETION) {
396                         DPRINTFN(1, ("urioread: error=%d\n", r));
397                         usbd_clear_endpoint_stall(sc->sc_pipeh_in);
398                         tn = 0;
399                         error = EIO;
400                         break;
401                 }
402 #if (USBDI >= 1)
403                 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
404 #else
405                 usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
406 #endif
407
408                 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
409                 error = uiomove(buf, tn, uio);
410                 if (error || tn < n)
411                         break;
412         }
413 #if (USBDI >= 1)
414         usbd_free_xfer(reqh);
415 #else
416         usbd_free_request(reqh);
417 #endif
418
419         return error;
420 }
421
422 int
423 uriowrite(struct dev_write_args *ap)
424 {
425         cdev_t dev = ap->a_head.a_dev;
426         struct uio *uio = ap->a_uio;
427 #if (USBDI >= 1)
428         struct urio_softc * sc;
429         usbd_xfer_handle reqh;
430 #else
431         usbd_request_handle reqh;
432 #endif
433         int unit = URIOUNIT(dev);
434         usbd_status r;
435         char buf[URIO_BBSIZE];
436         u_int32_t n;
437         int error = 0;
438
439         USB_GET_SC(urio, unit, sc);
440
441         DPRINTFN(5, ("uriowrite: %d\n", unit));
442         if (!sc->sc_opened)
443                 return EIO;
444
445 #if (USBDI >= 1)
446         sc->sc_refcnt++;
447         reqh = usbd_alloc_xfer(sc->sc_udev);
448 #else
449         reqh = usbd_alloc_request();
450 #endif
451         if (reqh == 0)
452                 return EIO;
453         while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
454                 error = uiomove(buf, n, uio);
455                 if (error)
456                         break;
457                 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
458 #if (USBDI >= 1)
459                 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
460                                        0, RIO_RW_TIMEOUT, 0);
461 #else
462                 r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
463                                        0, RIO_RW_TIMEOUT, 0);
464                 if (r != USBD_NORMAL_COMPLETION) {
465                         error = EIO;
466                         break;
467                 }
468 #endif
469                 r = usbd_sync_transfer(reqh);
470                 if (r != USBD_NORMAL_COMPLETION) {
471                         DPRINTFN(1, ("uriowrite: error=%d\n", r));
472                         usbd_clear_endpoint_stall(sc->sc_pipeh_out);
473                         error = EIO;
474                         break;
475                 }
476 #if (USBDI >= 1)
477                 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
478 #endif
479         }
480
481 #if (USBDI >= 1)
482         usbd_free_xfer(reqh);
483 #else
484         usbd_free_request(reqh);
485 #endif
486
487         return error;
488 }
489
490
491 int
492 urioioctl(struct dev_ioctl_args *ap)
493 {
494         cdev_t dev = ap->a_head.a_dev;
495 #if (USBDI >= 1)
496         struct urio_softc * sc;
497 #endif
498         int unit = URIOUNIT(dev);
499         struct RioCommand *rio_cmd;
500         int requesttype, len;
501         struct iovec iov;
502         struct uio uio;
503         usb_device_request_t req;
504         int req_flags = 0, req_actlen = 0;
505         void *ptr = 0;
506         int error = 0;
507         usbd_status r;
508
509         USB_GET_SC(urio, unit, sc);
510
511         switch (ap->a_cmd) {
512         case RIO_RECV_COMMAND:
513                 if (!(ap->a_fflag & FWRITE))
514                         return EPERM;
515                 rio_cmd = (struct RioCommand *)ap->a_data;
516                 if (rio_cmd == NULL)
517                         return EINVAL;
518                 len = rio_cmd->length;
519
520                 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
521                 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
522                         requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
523                 break;
524
525         case RIO_SEND_COMMAND:
526                 if (!(ap->a_fflag & FWRITE))
527                         return EPERM;
528                 rio_cmd = (struct RioCommand *)ap->a_data;
529                 if (rio_cmd == NULL)
530                         return EINVAL;
531                 len = rio_cmd->length;
532
533                 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
534                 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
535                         requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
536                 break;
537
538         default:
539                 return EINVAL;
540                 break;
541         }
542
543         /* Send rio control message */
544         req.bmRequestType = requesttype;
545         req.bRequest = rio_cmd->request;
546         USETW(req.wValue, rio_cmd->value);
547         USETW(req.wIndex, rio_cmd->index);
548         USETW(req.wLength, len);
549
550         if (len < 0 || len > 32767)
551                 return EINVAL;
552         if (len != 0) {
553                 iov.iov_base = (caddr_t)rio_cmd->buffer;
554                 iov.iov_len = len;
555                 uio.uio_iov = &iov;
556                 uio.uio_iovcnt = 1;
557                 uio.uio_resid = len;
558                 uio.uio_offset = 0;
559                 uio.uio_segflg = UIO_USERSPACE;
560                 uio.uio_rw =
561                         req.bmRequestType & UT_READ ?
562                         UIO_READ : UIO_WRITE;
563                 uio.uio_td = curthread;
564                 ptr = kmalloc(len, M_TEMP, M_WAITOK);
565                 if (uio.uio_rw == UIO_WRITE) {
566                         error = uiomove(ptr, len, &uio);
567                         if (error)
568                                 goto ret;
569                 }
570         }
571
572         r = usbd_do_request_flags(sc->sc_udev, &req,
573                                   ptr, req_flags, &req_actlen,
574                                   USBD_DEFAULT_TIMEOUT);
575         if (r == USBD_NORMAL_COMPLETION) {
576                 error = 0;
577                 if (len != 0) {
578                         if (uio.uio_rw == UIO_READ) {
579                                 error = uiomove(ptr, len, &uio);
580                         }
581                 }
582         } else {
583                 error = EIO;
584         }
585
586 ret:
587         if (ptr)
588                 kfree(ptr, M_TEMP);
589         return error;
590 }
591
592
593 #if defined(__NetBSD__) || defined(__OpenBSD__)
594 int
595 urio_activate(device_ptr_t self, enum devact act)
596 {
597         struct urio_softc *sc = (struct urio_softc *)self;
598
599         switch (act) {
600         case DVACT_ACTIVATE:
601                 return (EOPNOTSUPP);
602                 break;
603
604         case DVACT_DEACTIVATE:
605                 sc->sc_dying = 1;
606                 break;
607         }
608         return (0);
609 }
610
611 USB_DETACH(urio)
612 {
613         USB_DETACH_START(urio, sc);
614         struct urio_endpoint *sce;
615         int i, dir;
616         int s;
617 #if defined(__NetBSD__) || defined(__OpenBSD__)
618         int maj, mn;
619
620         DPRINTF(("urio_detach: sc=%p flags=%d\n", sc, flags));
621 #elif defined(__FreeBSD__) || defined(__DragonFly__)
622         DPRINTF(("urio_detach: sc=%p\n", sc));
623 #endif
624
625         sc->sc_dying = 1;
626         /* Abort all pipes.  Causes processes waiting for transfer to wake. */
627 #if 0
628         for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
629                 for (dir = OUT; dir <= IN; dir++) {
630                         sce = &sc->sc_endpoints[i][dir];
631                         if (sce && sce->pipeh)
632                                 usbd_abort_pipe(sce->pipeh);
633                 }
634         }
635
636         crit_enter();
637         if (--sc->sc_refcnt >= 0) {
638                 /* Wake everyone */
639                 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
640                         wakeup(&sc->sc_endpoints[i][IN]);
641                 /* Wait for processes to go away. */
642                 usb_detach_wait(USBDEV(sc->sc_dev));
643         }
644         crit_exit();
645 #else
646         if (sc->sc_pipeh_in)
647                 usbd_abort_pipe(sc->sc_pipeh_in);
648
649         if (sc->sc_pipeh_out)
650                 usbd_abort_pipe(sc->sc_pipeh_out);
651
652         crit_enter();
653         if (--sc->sc_refcnt >= 0) {
654                 /* Wait for processes to go away. */
655                 usb_detach_wait(USBDEV(sc->sc_dev));
656         }
657         crit_exit();
658 #endif
659
660 #if defined(__NetBSD__) || defined(__OpenBSD__)
661         /* locate the major number */
662         for (maj = 0; maj < nchrdev; maj++)
663                 if (cdevsw[maj].d_open == urioopen)
664                         break;
665
666         /* Nuke the vnodes for any open instances (calls close). */
667         mn = self->dv_unit * USB_MAX_ENDPOINTS;
668         vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
669 #endif
670
671         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
672                            USBDEV(sc->sc_dev));
673
674         return (0);
675 }
676 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
677
678 #if defined(__FreeBSD__) || defined(__DragonFly__)
679 Static int
680 urio_detach(device_t self)
681 {
682         DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
683         dev_ops_remove(&urio_ops, -1, device_get_unit(self));
684         /* XXX not implemented yet */
685         device_set_desc(self, NULL);
686         return 0;
687 }
688
689 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);
690 #endif