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