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