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