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