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