Nuke usbdevs and references to it.
[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.25 2007/11/05 19:09:44 hasso 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 #include <sys/module.h>
53 #include <sys/bus.h>
54 #include <sys/ioccom.h>
55 #include <sys/fcntl.h>
56 #include <sys/filio.h>
57 #include <sys/conf.h>
58 #include <sys/uio.h>
59 #include <sys/tty.h>
60 #include <sys/file.h>
61 #include <sys/select.h>
62 #include <sys/poll.h>
63 #include <sys/sysctl.h>
64 #include <sys/proc.h>
65 #include <sys/thread2.h>
66
67 #include <bus/usb/usb.h>
68 #include <bus/usb/usbdi.h>
69 #include <bus/usb/usbdi_util.h>
70
71 #include <bus/usb/rio500_usb.h>
72
73 #ifdef USB_DEBUG
74 #define DPRINTF(x)      if (uriodebug) kprintf x
75 #define DPRINTFN(n,x)   if (uriodebug>(n)) kprintf x
76 int     uriodebug = 0;
77 SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
78 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RW,
79            &uriodebug, 0, "urio debug level");
80 #else
81 #define DPRINTF(x)
82 #define DPRINTFN(n,x)
83 #endif
84
85 /* difference of usbd interface */
86 #define USBDI 1
87
88 #define RIO_OUT 0
89 #define RIO_IN  1
90 #define RIO_NODIR  2
91
92 d_open_t  urioopen;
93 d_close_t urioclose;
94 d_read_t  urioread;
95 d_write_t uriowrite;
96 d_ioctl_t urioioctl;
97
98 #define URIO_CDEV_MAJOR 143
99
100 static struct dev_ops urio_ops = {
101         { "urio", URIO_CDEV_MAJOR, 0 },
102         .d_open =       urioopen,
103         .d_close =      urioclose,
104         .d_read =       urioread,
105         .d_write =      uriowrite,
106         .d_ioctl =      urioioctl,
107 };
108 #define RIO_UE_GET_DIR(p) ((UE_GET_DIR(p) == UE_DIR_IN) ? RIO_IN :\
109                           ((UE_GET_DIR(p) == UE_DIR_OUT) ? RIO_OUT :\
110                                                            RIO_NODIR))
111
112 #define URIO_BBSIZE     1024
113
114 struct urio_softc {
115         device_t sc_dev;
116         usbd_device_handle sc_udev;
117         usbd_interface_handle sc_iface;
118
119         int sc_opened;
120         usbd_pipe_handle sc_pipeh_in;
121         usbd_pipe_handle sc_pipeh_out;
122         int sc_epaddr[2];
123
124         int sc_refcnt;
125 };
126
127 #define URIOUNIT(n) (minor(n))
128
129 #define RIO_RW_TIMEOUT 4000     /* ms */
130
131 static device_probe_t urio_match;
132 static device_attach_t urio_attach;
133 static device_detach_t urio_detach;
134
135 static devclass_t urio_devclass;
136
137 static kobj_method_t urio_methods[] = {
138         DEVMETHOD(device_probe, urio_match),
139         DEVMETHOD(device_attach, urio_attach),
140         DEVMETHOD(device_detach, urio_detach),
141         {0,0}
142 };
143
144 static driver_t urio_driver = {
145         "urio",
146         urio_methods,
147         sizeof(struct urio_softc)
148 };
149
150 static const struct usb_devno urio_devs[] = {
151         { USB_DEVICE(0x045a, 0x5001) }, /* Diamond Multimedia Rio 600 */
152         { USB_DEVICE(0x045a, 0x5002) }, /* Diamond Multimedia Rio 800 */
153         { USB_DEVICE(0x0841, 0x0001) }, /* Diamond Multimedia Rio 500 */
154 };
155
156 MODULE_DEPEND(urio, usb, 1, 1, 1);
157
158 static int
159 urio_match(device_t self)
160 {
161         struct usb_attach_arg *uaa = device_get_ivars(self);
162
163         DPRINTFN(10,("urio_match\n"));
164         if (uaa->iface == NULL)
165                 return (UMATCH_NONE);
166
167         return (usb_lookup(urio_devs, uaa->vendor, uaa->product) != NULL ?
168                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
169 }
170
171 static int
172 urio_attach(device_t self)
173 {
174         struct urio_softc *sc = device_get_softc(self);
175         struct usb_attach_arg *uaa = device_get_ivars(self);
176         char devinfo[1024];
177         usbd_device_handle udev;
178         usbd_interface_handle iface;
179         u_int8_t epcount;
180         usbd_status r;
181         char * ermsg = "<none>";
182         int i;
183
184         DPRINTFN(10,("urio_attach: sc=%p\n", sc));
185         usbd_devinfo(uaa->device, 0, devinfo);
186         sc->sc_dev = self;
187         device_set_desc_copy(self, devinfo);
188         kprintf("%s: %s\n", device_get_nameunit(sc->sc_dev), devinfo);
189
190         sc->sc_udev = udev = uaa->device;
191
192         if ((!uaa->device) || (!uaa->iface)) {
193                 ermsg = "device or iface";
194                 goto nobulk;
195         }
196         sc->sc_iface = iface = uaa->iface;
197         sc->sc_opened = 0;
198         sc->sc_pipeh_in = 0;
199         sc->sc_pipeh_out = 0;
200         sc->sc_refcnt = 0;
201
202         r = usbd_endpoint_count(iface, &epcount);
203         if (r != USBD_NORMAL_COMPLETION) {
204                 ermsg = "endpoints";
205                 goto nobulk;
206         }
207
208         sc->sc_epaddr[RIO_OUT] = 0xff;
209         sc->sc_epaddr[RIO_IN] = 0x00;
210
211         for (i = 0; i < epcount; i++) {
212                 usb_endpoint_descriptor_t *edesc =
213                         usbd_interface2endpoint_descriptor(iface, i);
214                 int d;
215
216                 if (!edesc) {
217                         ermsg = "interface endpoint";
218                         goto nobulk;
219                 }
220
221                 d = RIO_UE_GET_DIR(edesc->bEndpointAddress);
222                 if (d != RIO_NODIR)
223                         sc->sc_epaddr[d] = edesc->bEndpointAddress;
224         }
225         if ( sc->sc_epaddr[RIO_OUT] == 0xff ||
226              sc->sc_epaddr[RIO_IN] == 0x00) {
227                 ermsg = "Rio I&O";
228                 goto nobulk;
229         }
230
231         dev_ops_add(&urio_ops, -1, device_get_unit(self));
232         make_dev(&urio_ops, device_get_unit(self),
233                         UID_ROOT, GID_OPERATOR,
234                         0644, "urio%d", device_get_unit(self));
235
236         DPRINTFN(10, ("urio_attach: %p\n", sc->sc_udev));
237
238         return 0;
239
240  nobulk:
241         kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
242         return ENXIO;
243 }
244
245
246 int
247 urioopen(struct dev_open_args *ap)
248 {
249         cdev_t dev = ap->a_head.a_dev;
250 #if (USBDI >= 1)
251         struct urio_softc * sc;
252 #endif
253         int unit = URIOUNIT(dev);
254         sc = devclass_get_softc(urio_devclass, unit);
255         if (sc == NULL)
256                 return (ENXIO);
257
258         DPRINTFN(5, ("urioopen: flag=%d, mode=%d, unit=%d\n",
259                      ap->a_oflags, ap->a_devtype, unit));
260
261         if (sc->sc_opened)
262                 return EBUSY;
263
264         if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
265                 return EACCES;
266
267         sc->sc_opened = 1;
268         sc->sc_pipeh_in = 0;
269         sc->sc_pipeh_out = 0;
270         if (usbd_open_pipe(sc->sc_iface,
271                 sc->sc_epaddr[RIO_IN], 0, &sc->sc_pipeh_in)
272                         != USBD_NORMAL_COMPLETION)
273         {
274                         sc->sc_pipeh_in = 0;
275                         return EIO;
276         };
277         if (usbd_open_pipe(sc->sc_iface,
278                 sc->sc_epaddr[RIO_OUT], 0, &sc->sc_pipeh_out)
279                         != USBD_NORMAL_COMPLETION)
280         {
281                         usbd_close_pipe(sc->sc_pipeh_in);
282                         sc->sc_pipeh_in = 0;
283                         sc->sc_pipeh_out = 0;
284                         return EIO;
285         };
286         return 0;
287 }
288
289 int
290 urioclose(struct dev_close_args *ap)
291 {
292         cdev_t dev = ap->a_head.a_dev;
293 #if (USBDI >= 1)
294         struct urio_softc * sc;
295 #endif
296         int unit = URIOUNIT(dev);
297         sc = devclass_get_softc(urio_devclass, unit);
298
299         DPRINTFN(5, ("urioclose: flag=%d, mode=%d, unit=%d\n",
300                 ap->a_fflag, ap->a_devtype, unit));
301         if (sc->sc_pipeh_in)
302                 usbd_close_pipe(sc->sc_pipeh_in);
303
304         if (sc->sc_pipeh_out)
305                 usbd_close_pipe(sc->sc_pipeh_out);
306
307         sc->sc_pipeh_in = 0;
308         sc->sc_pipeh_out = 0;
309         sc->sc_opened = 0;
310         sc->sc_refcnt = 0;
311         return 0;
312 }
313
314 int
315 urioread(struct dev_read_args *ap)
316 {
317         cdev_t dev = ap->a_head.a_dev;
318         struct uio *uio = ap->a_uio;
319 #if (USBDI >= 1)
320         struct urio_softc * sc;
321         usbd_xfer_handle reqh;
322 #else
323         usbd_request_handle reqh;
324         usbd_private_handle r_priv;
325         void *r_buff;
326         usbd_status r_status;
327 #endif
328         int unit = URIOUNIT(dev);
329         usbd_status r;
330         char buf[URIO_BBSIZE];
331         u_int32_t n, tn;
332         int error = 0;
333
334         sc = devclass_get_softc(urio_devclass, unit);
335
336         DPRINTFN(5, ("urioread: %d\n", unit));
337         if (!sc->sc_opened)
338                 return EIO;
339
340 #if (USBDI >= 1)
341         sc->sc_refcnt++;
342         reqh = usbd_alloc_xfer(sc->sc_udev);
343 #else
344         reqh = usbd_alloc_request();
345 #endif
346         if (reqh == 0)
347                 return ENOMEM;
348         while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
349                 DPRINTFN(1, ("urioread: start transfer %d bytes\n", n));
350                 tn = n;
351 #if (USBDI >= 1)
352                 usbd_setup_xfer(reqh, sc->sc_pipeh_in, 0, buf, tn,
353                                        0, RIO_RW_TIMEOUT, 0);
354 #else
355                 r = usbd_setup_request(reqh, sc->sc_pipeh_in, 0, buf, tn,
356                                        0, RIO_RW_TIMEOUT, 0);
357                 if (r != USBD_NORMAL_COMPLETION) {
358                         error = EIO;
359                         break;
360                 }
361 #endif
362                 r = usbd_sync_transfer(reqh);
363                 if (r != USBD_NORMAL_COMPLETION) {
364                         DPRINTFN(1, ("urioread: error=%d\n", r));
365                         usbd_clear_endpoint_stall(sc->sc_pipeh_in);
366                         tn = 0;
367                         error = EIO;
368                         break;
369                 }
370 #if (USBDI >= 1)
371                 usbd_get_xfer_status(reqh, 0, 0, &tn, 0);
372 #else
373                 usbd_get_request_status(reqh, &r_priv, &r_buff, &tn, &r_status);
374 #endif
375
376                 DPRINTFN(1, ("urioread: got %d bytes\n", tn));
377                 error = uiomove(buf, tn, uio);
378                 if (error || tn < n)
379                         break;
380         }
381 #if (USBDI >= 1)
382         usbd_free_xfer(reqh);
383 #else
384         usbd_free_request(reqh);
385 #endif
386
387         return error;
388 }
389
390 int
391 uriowrite(struct dev_write_args *ap)
392 {
393         cdev_t dev = ap->a_head.a_dev;
394         struct uio *uio = ap->a_uio;
395 #if (USBDI >= 1)
396         struct urio_softc * sc;
397         usbd_xfer_handle reqh;
398 #else
399         usbd_request_handle reqh;
400 #endif
401         int unit = URIOUNIT(dev);
402         usbd_status r;
403         char buf[URIO_BBSIZE];
404         u_int32_t n;
405         int error = 0;
406
407         sc = devclass_get_softc(urio_devclass, unit);
408
409         DPRINTFN(5, ("uriowrite: %d\n", unit));
410         if (!sc->sc_opened)
411                 return EIO;
412
413 #if (USBDI >= 1)
414         sc->sc_refcnt++;
415         reqh = usbd_alloc_xfer(sc->sc_udev);
416 #else
417         reqh = usbd_alloc_request();
418 #endif
419         if (reqh == 0)
420                 return EIO;
421         while ((n = min(URIO_BBSIZE, uio->uio_resid)) != 0) {
422                 error = uiomove(buf, n, uio);
423                 if (error)
424                         break;
425                 DPRINTFN(1, ("uriowrite: transfer %d bytes\n", n));
426 #if (USBDI >= 1)
427                 usbd_setup_xfer(reqh, sc->sc_pipeh_out, 0, buf, n,
428                                        0, RIO_RW_TIMEOUT, 0);
429 #else
430                 r = usbd_setup_request(reqh, sc->sc_pipeh_out, 0, buf, n,
431                                        0, RIO_RW_TIMEOUT, 0);
432                 if (r != USBD_NORMAL_COMPLETION) {
433                         error = EIO;
434                         break;
435                 }
436 #endif
437                 r = usbd_sync_transfer(reqh);
438                 if (r != USBD_NORMAL_COMPLETION) {
439                         DPRINTFN(1, ("uriowrite: error=%d\n", r));
440                         usbd_clear_endpoint_stall(sc->sc_pipeh_out);
441                         error = EIO;
442                         break;
443                 }
444 #if (USBDI >= 1)
445                 usbd_get_xfer_status(reqh, 0, 0, 0, 0);
446 #endif
447         }
448
449 #if (USBDI >= 1)
450         usbd_free_xfer(reqh);
451 #else
452         usbd_free_request(reqh);
453 #endif
454
455         return error;
456 }
457
458
459 int
460 urioioctl(struct dev_ioctl_args *ap)
461 {
462         cdev_t dev = ap->a_head.a_dev;
463 #if (USBDI >= 1)
464         struct urio_softc * sc;
465 #endif
466         int unit = URIOUNIT(dev);
467         struct RioCommand *rio_cmd;
468         int requesttype, len;
469         struct iovec iov;
470         struct uio uio;
471         usb_device_request_t req;
472         int req_flags = 0, req_actlen = 0;
473         void *ptr = 0;
474         int error = 0;
475         usbd_status r;
476
477         sc = devclass_get_softc(urio_devclass, unit);
478
479         switch (ap->a_cmd) {
480         case RIO_RECV_COMMAND:
481                 if (!(ap->a_fflag & FWRITE))
482                         return EPERM;
483                 rio_cmd = (struct RioCommand *)ap->a_data;
484                 if (rio_cmd == NULL)
485                         return EINVAL;
486                 len = rio_cmd->length;
487
488                 requesttype = rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
489                 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
490                         requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
491                 break;
492
493         case RIO_SEND_COMMAND:
494                 if (!(ap->a_fflag & FWRITE))
495                         return EPERM;
496                 rio_cmd = (struct RioCommand *)ap->a_data;
497                 if (rio_cmd == NULL)
498                         return EINVAL;
499                 len = rio_cmd->length;
500
501                 requesttype = rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
502                 DPRINTFN(1,("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x\n",
503                         requesttype, rio_cmd->request, rio_cmd->value, rio_cmd->index, len));
504                 break;
505
506         default:
507                 return EINVAL;
508                 break;
509         }
510
511         /* Send rio control message */
512         req.bmRequestType = requesttype;
513         req.bRequest = rio_cmd->request;
514         USETW(req.wValue, rio_cmd->value);
515         USETW(req.wIndex, rio_cmd->index);
516         USETW(req.wLength, len);
517
518         if (len < 0 || len > 32767)
519                 return EINVAL;
520         if (len != 0) {
521                 iov.iov_base = (caddr_t)rio_cmd->buffer;
522                 iov.iov_len = len;
523                 uio.uio_iov = &iov;
524                 uio.uio_iovcnt = 1;
525                 uio.uio_resid = len;
526                 uio.uio_offset = 0;
527                 uio.uio_segflg = UIO_USERSPACE;
528                 uio.uio_rw =
529                         req.bmRequestType & UT_READ ?
530                         UIO_READ : UIO_WRITE;
531                 uio.uio_td = curthread;
532                 ptr = kmalloc(len, M_TEMP, M_WAITOK);
533                 if (uio.uio_rw == UIO_WRITE) {
534                         error = uiomove(ptr, len, &uio);
535                         if (error)
536                                 goto ret;
537                 }
538         }
539
540         r = usbd_do_request_flags(sc->sc_udev, &req,
541                                   ptr, req_flags, &req_actlen,
542                                   USBD_DEFAULT_TIMEOUT);
543         if (r == USBD_NORMAL_COMPLETION) {
544                 error = 0;
545                 if (len != 0) {
546                         if (uio.uio_rw == UIO_READ) {
547                                 error = uiomove(ptr, len, &uio);
548                         }
549                 }
550         } else {
551                 error = EIO;
552         }
553
554 ret:
555         if (ptr)
556                 kfree(ptr, M_TEMP);
557         return error;
558 }
559
560 static int
561 urio_detach(device_t self)
562 {
563         DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
564         dev_ops_remove(&urio_ops, -1, device_get_unit(self));
565         /* XXX not implemented yet */
566         device_set_desc(self, NULL);
567         return 0;
568 }
569
570 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, usbd_driver_load, 0);
571