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