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