]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/storage/urio.c
MFV r339226 (peter): Record merge of serf-1.3.9.
[FreeBSD/FreeBSD.git] / sys / dev / usb / storage / urio.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Iwasa Kazmi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson.
29  * This code includes software developed by the NetBSD Foundation, Inc. and
30  * its contributors.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36
37 /*
38  * 2000/3/24  added NetBSD/OpenBSD support (from Alex Nemirovsky)
39  * 2000/3/07  use two bulk-pipe handles for read and write (Dirk)
40  * 2000/3/06  change major number(143), and copyright header
41  *            some fix for 4.0 (Dirk)
42  * 2000/3/05  codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
43  * 2000/3/01  remove retry code from urioioctl()
44  *            change method of bulk transfer (no interrupt)
45  * 2000/2/28  small fixes for new rio_usb.h
46  * 2000/2/24  first version.
47  */
48
49 #include <sys/stdint.h>
50 #include <sys/stddef.h>
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/bus.h>
57 #include <sys/module.h>
58 #include <sys/lock.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/sx.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
66 #include <sys/priv.h>
67 #include <sys/conf.h>
68 #include <sys/fcntl.h>
69
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbdi.h>
72 #include "usbdevs.h"
73
74 #include <dev/usb/usb_ioctl.h>
75 #include <dev/usb/usb_generic.h>
76
77 #define USB_DEBUG_VAR urio_debug
78 #include <dev/usb/usb_debug.h>
79
80 #include <dev/usb/storage/rio500_usb.h>
81
82 #ifdef USB_DEBUG
83 static int urio_debug = 0;
84
85 static SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
86 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RWTUN,
87     &urio_debug, 0, "urio debug level");
88 #endif
89
90 #define URIO_T_WR     0
91 #define URIO_T_RD     1
92 #define URIO_T_WR_CS  2
93 #define URIO_T_RD_CS  3
94 #define URIO_T_MAX    4
95
96 #define URIO_BSIZE      (1<<12)         /* bytes */
97 #define URIO_IFQ_MAXLEN      2          /* units */
98
99 struct urio_softc {
100         struct usb_fifo_sc sc_fifo;
101         struct mtx sc_mtx;
102
103         struct usb_device *sc_udev;
104         struct usb_xfer *sc_xfer[URIO_T_MAX];
105
106         uint8_t sc_flags;
107 #define URIO_FLAG_READ_STALL    0x01    /* read transfer stalled */
108 #define URIO_FLAG_WRITE_STALL   0x02    /* write transfer stalled */
109
110         uint8_t sc_name[16];
111 };
112
113 /* prototypes */
114
115 static device_probe_t urio_probe;
116 static device_attach_t urio_attach;
117 static device_detach_t urio_detach;
118
119 static usb_callback_t urio_write_callback;
120 static usb_callback_t urio_write_clear_stall_callback;
121 static usb_callback_t urio_read_callback;
122 static usb_callback_t urio_read_clear_stall_callback;
123
124 static usb_fifo_close_t urio_close;
125 static usb_fifo_cmd_t urio_start_read;
126 static usb_fifo_cmd_t urio_start_write;
127 static usb_fifo_cmd_t urio_stop_read;
128 static usb_fifo_cmd_t urio_stop_write;
129 static usb_fifo_ioctl_t urio_ioctl;
130 static usb_fifo_open_t urio_open;
131
132 static struct usb_fifo_methods urio_fifo_methods = {
133         .f_close = &urio_close,
134         .f_ioctl = &urio_ioctl,
135         .f_open = &urio_open,
136         .f_start_read = &urio_start_read,
137         .f_start_write = &urio_start_write,
138         .f_stop_read = &urio_stop_read,
139         .f_stop_write = &urio_stop_write,
140         .basename[0] = "urio",
141 };
142
143 static const struct usb_config urio_config[URIO_T_MAX] = {
144         [URIO_T_WR] = {
145                 .type = UE_BULK,
146                 .endpoint = UE_ADDR_ANY,
147                 .direction = UE_DIR_OUT,
148                 .bufsize = URIO_BSIZE,
149                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.proxy_buffer = 1,},
150                 .callback = &urio_write_callback,
151         },
152
153         [URIO_T_RD] = {
154                 .type = UE_BULK,
155                 .endpoint = UE_ADDR_ANY,
156                 .direction = UE_DIR_IN,
157                 .bufsize = URIO_BSIZE,
158                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1,},
159                 .callback = &urio_read_callback,
160         },
161
162         [URIO_T_WR_CS] = {
163                 .type = UE_CONTROL,
164                 .endpoint = 0x00,       /* Control pipe */
165                 .direction = UE_DIR_ANY,
166                 .bufsize = sizeof(struct usb_device_request),
167                 .callback = &urio_write_clear_stall_callback,
168                 .timeout = 1000,        /* 1 second */
169                 .interval = 50, /* 50ms */
170         },
171
172         [URIO_T_RD_CS] = {
173                 .type = UE_CONTROL,
174                 .endpoint = 0x00,       /* Control pipe */
175                 .direction = UE_DIR_ANY,
176                 .bufsize = sizeof(struct usb_device_request),
177                 .callback = &urio_read_clear_stall_callback,
178                 .timeout = 1000,        /* 1 second */
179                 .interval = 50, /* 50ms */
180         },
181 };
182
183 static devclass_t urio_devclass;
184
185 static device_method_t urio_methods[] = {
186         /* Device interface */
187         DEVMETHOD(device_probe, urio_probe),
188         DEVMETHOD(device_attach, urio_attach),
189         DEVMETHOD(device_detach, urio_detach),
190
191         DEVMETHOD_END
192 };
193
194 static driver_t urio_driver = {
195         .name = "urio",
196         .methods = urio_methods,
197         .size = sizeof(struct urio_softc),
198 };
199
200 static const STRUCT_USB_HOST_ID urio_devs[] = {
201         {USB_VPI(USB_VENDOR_DIAMOND, USB_PRODUCT_DIAMOND_RIO500USB, 0)},
202         {USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO600USB, 0)},
203         {USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO800USB, 0)},
204 };
205
206 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, NULL, 0);
207 MODULE_DEPEND(urio, usb, 1, 1, 1);
208 MODULE_VERSION(urio, 1);
209 USB_PNP_HOST_INFO(urio_devs);
210
211 static int
212 urio_probe(device_t dev)
213 {
214         struct usb_attach_arg *uaa = device_get_ivars(dev);
215
216         if (uaa->usb_mode != USB_MODE_HOST)
217                 return (ENXIO);
218         if (uaa->info.bConfigIndex != 0)
219                 return (ENXIO);
220         if (uaa->info.bIfaceIndex != 0)
221                 return (ENXIO);
222
223         return (usbd_lookup_id_by_uaa(urio_devs, sizeof(urio_devs), uaa));
224 }
225
226 static int
227 urio_attach(device_t dev)
228 {
229         struct usb_attach_arg *uaa = device_get_ivars(dev);
230         struct urio_softc *sc = device_get_softc(dev);
231         int error;
232
233         device_set_usb_desc(dev);
234
235         sc->sc_udev = uaa->device;
236
237         mtx_init(&sc->sc_mtx, "urio lock", NULL, MTX_DEF | MTX_RECURSE);
238
239         snprintf(sc->sc_name, sizeof(sc->sc_name),
240             "%s", device_get_nameunit(dev));
241
242         error = usbd_transfer_setup(uaa->device,
243             &uaa->info.bIfaceIndex, sc->sc_xfer,
244             urio_config, URIO_T_MAX, sc, &sc->sc_mtx);
245
246         if (error) {
247                 DPRINTF("error=%s\n", usbd_errstr(error));
248                 goto detach;
249         }
250
251         error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
252             &urio_fifo_methods, &sc->sc_fifo,
253             device_get_unit(dev), -1, uaa->info.bIfaceIndex,
254             UID_ROOT, GID_OPERATOR, 0644);
255         if (error) {
256                 goto detach;
257         }
258         return (0);                     /* success */
259
260 detach:
261         urio_detach(dev);
262         return (ENOMEM);                /* failure */
263 }
264
265 static void
266 urio_write_callback(struct usb_xfer *xfer, usb_error_t error)
267 {
268         struct urio_softc *sc = usbd_xfer_softc(xfer);
269         struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX];
270         struct usb_page_cache *pc;
271         uint32_t actlen;
272
273         switch (USB_GET_STATE(xfer)) {
274         case USB_ST_TRANSFERRED:
275         case USB_ST_SETUP:
276                 if (sc->sc_flags & URIO_FLAG_WRITE_STALL) {
277                         usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]);
278                         return;
279                 }
280                 pc = usbd_xfer_get_frame(xfer, 0);
281                 if (usb_fifo_get_data(f, pc, 0,
282                     usbd_xfer_max_len(xfer), &actlen, 0)) {
283
284                         usbd_xfer_set_frame_len(xfer, 0, actlen);
285                         usbd_transfer_submit(xfer);
286                 }
287                 return;
288
289         default:                        /* Error */
290                 if (error != USB_ERR_CANCELLED) {
291                         /* try to clear stall first */
292                         sc->sc_flags |= URIO_FLAG_WRITE_STALL;
293                         usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]);
294                 }
295                 return;
296         }
297 }
298
299 static void
300 urio_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
301 {
302         struct urio_softc *sc = usbd_xfer_softc(xfer);
303         struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_WR];
304
305         if (usbd_clear_stall_callback(xfer, xfer_other)) {
306                 DPRINTF("stall cleared\n");
307                 sc->sc_flags &= ~URIO_FLAG_WRITE_STALL;
308                 usbd_transfer_start(xfer_other);
309         }
310 }
311
312 static void
313 urio_read_callback(struct usb_xfer *xfer, usb_error_t error)
314 {
315         struct urio_softc *sc = usbd_xfer_softc(xfer);
316         struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_RX];
317         struct usb_page_cache *pc;
318         int actlen;
319
320         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
321
322         switch (USB_GET_STATE(xfer)) {
323         case USB_ST_TRANSFERRED:
324                 pc = usbd_xfer_get_frame(xfer, 0);
325                 usb_fifo_put_data(f, pc, 0, actlen, 1);
326
327         case USB_ST_SETUP:
328                 if (sc->sc_flags & URIO_FLAG_READ_STALL) {
329                         usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]);
330                         return;
331                 }
332                 if (usb_fifo_put_bytes_max(f) != 0) {
333                         usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
334                         usbd_transfer_submit(xfer);
335                 }
336                 return;
337
338         default:                        /* Error */
339                 if (error != USB_ERR_CANCELLED) {
340                         /* try to clear stall first */
341                         sc->sc_flags |= URIO_FLAG_READ_STALL;
342                         usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]);
343                 }
344                 return;
345         }
346 }
347
348 static void
349 urio_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
350 {
351         struct urio_softc *sc = usbd_xfer_softc(xfer);
352         struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_RD];
353
354         if (usbd_clear_stall_callback(xfer, xfer_other)) {
355                 DPRINTF("stall cleared\n");
356                 sc->sc_flags &= ~URIO_FLAG_READ_STALL;
357                 usbd_transfer_start(xfer_other);
358         }
359 }
360
361 static void
362 urio_start_read(struct usb_fifo *fifo)
363 {
364         struct urio_softc *sc = usb_fifo_softc(fifo);
365
366         usbd_transfer_start(sc->sc_xfer[URIO_T_RD]);
367 }
368
369 static void
370 urio_stop_read(struct usb_fifo *fifo)
371 {
372         struct urio_softc *sc = usb_fifo_softc(fifo);
373
374         usbd_transfer_stop(sc->sc_xfer[URIO_T_RD_CS]);
375         usbd_transfer_stop(sc->sc_xfer[URIO_T_RD]);
376 }
377
378 static void
379 urio_start_write(struct usb_fifo *fifo)
380 {
381         struct urio_softc *sc = usb_fifo_softc(fifo);
382
383         usbd_transfer_start(sc->sc_xfer[URIO_T_WR]);
384 }
385
386 static void
387 urio_stop_write(struct usb_fifo *fifo)
388 {
389         struct urio_softc *sc = usb_fifo_softc(fifo);
390
391         usbd_transfer_stop(sc->sc_xfer[URIO_T_WR_CS]);
392         usbd_transfer_stop(sc->sc_xfer[URIO_T_WR]);
393 }
394
395 static int
396 urio_open(struct usb_fifo *fifo, int fflags)
397 {
398         struct urio_softc *sc = usb_fifo_softc(fifo);
399
400         if (fflags & FREAD) {
401                 /* clear stall first */
402                 mtx_lock(&sc->sc_mtx);
403                 sc->sc_flags |= URIO_FLAG_READ_STALL;
404                 mtx_unlock(&sc->sc_mtx);
405
406                 if (usb_fifo_alloc_buffer(fifo,
407                     usbd_xfer_max_len(sc->sc_xfer[URIO_T_RD]),
408                     URIO_IFQ_MAXLEN)) {
409                         return (ENOMEM);
410                 }
411         }
412         if (fflags & FWRITE) {
413                 /* clear stall first */
414                 sc->sc_flags |= URIO_FLAG_WRITE_STALL;
415
416                 if (usb_fifo_alloc_buffer(fifo,
417                     usbd_xfer_max_len(sc->sc_xfer[URIO_T_WR]),
418                     URIO_IFQ_MAXLEN)) {
419                         return (ENOMEM);
420                 }
421         }
422         return (0);                     /* success */
423 }
424
425 static void
426 urio_close(struct usb_fifo *fifo, int fflags)
427 {
428         if (fflags & (FREAD | FWRITE)) {
429                 usb_fifo_free_buffer(fifo);
430         }
431 }
432
433 static int
434 urio_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
435     int fflags)
436 {
437         struct usb_ctl_request ur;
438         struct RioCommand *rio_cmd;
439         int error;
440
441         switch (cmd) {
442         case RIO_RECV_COMMAND:
443                 if (!(fflags & FWRITE)) {
444                         error = EPERM;
445                         goto done;
446                 }
447                 memset(&ur, 0, sizeof(ur));
448                 rio_cmd = addr;
449                 ur.ucr_request.bmRequestType =
450                     rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
451                 break;
452
453         case RIO_SEND_COMMAND:
454                 if (!(fflags & FWRITE)) {
455                         error = EPERM;
456                         goto done;
457                 }
458                 memset(&ur, 0, sizeof(ur));
459                 rio_cmd = addr;
460                 ur.ucr_request.bmRequestType =
461                     rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
462                 break;
463
464         default:
465                 error = EINVAL;
466                 goto done;
467         }
468
469         DPRINTFN(2, "Sending command\n");
470
471         /* Send rio control message */
472         ur.ucr_request.bRequest = rio_cmd->request;
473         USETW(ur.ucr_request.wValue, rio_cmd->value);
474         USETW(ur.ucr_request.wIndex, rio_cmd->index);
475         USETW(ur.ucr_request.wLength, rio_cmd->length);
476         ur.ucr_data = rio_cmd->buffer;
477
478         /* reuse generic USB code */
479         error = ugen_do_request(fifo, &ur);
480
481 done:
482         return (error);
483 }
484
485 static int
486 urio_detach(device_t dev)
487 {
488         struct urio_softc *sc = device_get_softc(dev);
489
490         DPRINTF("\n");
491
492         usb_fifo_detach(&sc->sc_fifo);
493
494         usbd_transfer_unsetup(sc->sc_xfer, URIO_T_MAX);
495
496         mtx_destroy(&sc->sc_mtx);
497
498         return (0);
499 }