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