xref: /freebsd/sys/dev/usb/storage/urio.c (revision 5b9c547c)
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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/bus.h>
55 #include <sys/module.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/sx.h>
61 #include <sys/unistd.h>
62 #include <sys/callout.h>
63 #include <sys/malloc.h>
64 #include <sys/priv.h>
65 #include <sys/conf.h>
66 #include <sys/fcntl.h>
67 
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include "usbdevs.h"
71 
72 #include <dev/usb/usb_ioctl.h>
73 #include <dev/usb/usb_generic.h>
74 
75 #define	USB_DEBUG_VAR urio_debug
76 #include <dev/usb/usb_debug.h>
77 
78 #include <dev/usb/storage/rio500_usb.h>
79 
80 #ifdef USB_DEBUG
81 static int urio_debug = 0;
82 
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW, 0, "USB urio");
84 SYSCTL_INT(_hw_usb_urio, OID_AUTO, debug, CTLFLAG_RWTUN,
85     &urio_debug, 0, "urio debug level");
86 #endif
87 
88 #define	URIO_T_WR     0
89 #define	URIO_T_RD     1
90 #define	URIO_T_WR_CS  2
91 #define	URIO_T_RD_CS  3
92 #define	URIO_T_MAX    4
93 
94 #define	URIO_BSIZE	(1<<12)		/* bytes */
95 #define	URIO_IFQ_MAXLEN      2		/* units */
96 
97 struct urio_softc {
98 	struct usb_fifo_sc sc_fifo;
99 	struct mtx sc_mtx;
100 
101 	struct usb_device *sc_udev;
102 	struct usb_xfer *sc_xfer[URIO_T_MAX];
103 
104 	uint8_t	sc_flags;
105 #define	URIO_FLAG_READ_STALL    0x01	/* read transfer stalled */
106 #define	URIO_FLAG_WRITE_STALL   0x02	/* write transfer stalled */
107 
108 	uint8_t	sc_name[16];
109 };
110 
111 /* prototypes */
112 
113 static device_probe_t urio_probe;
114 static device_attach_t urio_attach;
115 static device_detach_t urio_detach;
116 
117 static usb_callback_t urio_write_callback;
118 static usb_callback_t urio_write_clear_stall_callback;
119 static usb_callback_t urio_read_callback;
120 static usb_callback_t urio_read_clear_stall_callback;
121 
122 static usb_fifo_close_t urio_close;
123 static usb_fifo_cmd_t urio_start_read;
124 static usb_fifo_cmd_t urio_start_write;
125 static usb_fifo_cmd_t urio_stop_read;
126 static usb_fifo_cmd_t urio_stop_write;
127 static usb_fifo_ioctl_t urio_ioctl;
128 static usb_fifo_open_t urio_open;
129 
130 static struct usb_fifo_methods urio_fifo_methods = {
131 	.f_close = &urio_close,
132 	.f_ioctl = &urio_ioctl,
133 	.f_open = &urio_open,
134 	.f_start_read = &urio_start_read,
135 	.f_start_write = &urio_start_write,
136 	.f_stop_read = &urio_stop_read,
137 	.f_stop_write = &urio_stop_write,
138 	.basename[0] = "urio",
139 };
140 
141 static const struct usb_config urio_config[URIO_T_MAX] = {
142 	[URIO_T_WR] = {
143 		.type = UE_BULK,
144 		.endpoint = UE_ADDR_ANY,
145 		.direction = UE_DIR_OUT,
146 		.bufsize = URIO_BSIZE,
147 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,.proxy_buffer = 1,},
148 		.callback = &urio_write_callback,
149 	},
150 
151 	[URIO_T_RD] = {
152 		.type = UE_BULK,
153 		.endpoint = UE_ADDR_ANY,
154 		.direction = UE_DIR_IN,
155 		.bufsize = URIO_BSIZE,
156 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1,},
157 		.callback = &urio_read_callback,
158 	},
159 
160 	[URIO_T_WR_CS] = {
161 		.type = UE_CONTROL,
162 		.endpoint = 0x00,	/* Control pipe */
163 		.direction = UE_DIR_ANY,
164 		.bufsize = sizeof(struct usb_device_request),
165 		.callback = &urio_write_clear_stall_callback,
166 		.timeout = 1000,	/* 1 second */
167 		.interval = 50,	/* 50ms */
168 	},
169 
170 	[URIO_T_RD_CS] = {
171 		.type = UE_CONTROL,
172 		.endpoint = 0x00,	/* Control pipe */
173 		.direction = UE_DIR_ANY,
174 		.bufsize = sizeof(struct usb_device_request),
175 		.callback = &urio_read_clear_stall_callback,
176 		.timeout = 1000,	/* 1 second */
177 		.interval = 50,	/* 50ms */
178 	},
179 };
180 
181 static devclass_t urio_devclass;
182 
183 static device_method_t urio_methods[] = {
184 	/* Device interface */
185 	DEVMETHOD(device_probe, urio_probe),
186 	DEVMETHOD(device_attach, urio_attach),
187 	DEVMETHOD(device_detach, urio_detach),
188 
189 	DEVMETHOD_END
190 };
191 
192 static driver_t urio_driver = {
193 	.name = "urio",
194 	.methods = urio_methods,
195 	.size = sizeof(struct urio_softc),
196 };
197 
198 DRIVER_MODULE(urio, uhub, urio_driver, urio_devclass, NULL, 0);
199 MODULE_DEPEND(urio, usb, 1, 1, 1);
200 MODULE_VERSION(urio, 1);
201 
202 static const STRUCT_USB_HOST_ID urio_devs[] = {
203 	{USB_VPI(USB_VENDOR_DIAMOND, USB_PRODUCT_DIAMOND_RIO500USB, 0)},
204 	{USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO600USB, 0)},
205 	{USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO800USB, 0)},
206 };
207 
208 static int
209 urio_probe(device_t dev)
210 {
211 	struct usb_attach_arg *uaa = device_get_ivars(dev);
212 
213 	if (uaa->usb_mode != USB_MODE_HOST)
214 		return (ENXIO);
215 	if (uaa->info.bConfigIndex != 0)
216 		return (ENXIO);
217 	if (uaa->info.bIfaceIndex != 0)
218 		return (ENXIO);
219 
220 	return (usbd_lookup_id_by_uaa(urio_devs, sizeof(urio_devs), uaa));
221 }
222 
223 static int
224 urio_attach(device_t dev)
225 {
226 	struct usb_attach_arg *uaa = device_get_ivars(dev);
227 	struct urio_softc *sc = device_get_softc(dev);
228 	int error;
229 
230 	device_set_usb_desc(dev);
231 
232 	sc->sc_udev = uaa->device;
233 
234 	mtx_init(&sc->sc_mtx, "urio lock", NULL, MTX_DEF | MTX_RECURSE);
235 
236 	snprintf(sc->sc_name, sizeof(sc->sc_name),
237 	    "%s", device_get_nameunit(dev));
238 
239 	error = usbd_transfer_setup(uaa->device,
240 	    &uaa->info.bIfaceIndex, sc->sc_xfer,
241 	    urio_config, URIO_T_MAX, sc, &sc->sc_mtx);
242 
243 	if (error) {
244 		DPRINTF("error=%s\n", usbd_errstr(error));
245 		goto detach;
246 	}
247 
248 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
249 	    &urio_fifo_methods, &sc->sc_fifo,
250 	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
251 	    UID_ROOT, GID_OPERATOR, 0644);
252 	if (error) {
253 		goto detach;
254 	}
255 	return (0);			/* success */
256 
257 detach:
258 	urio_detach(dev);
259 	return (ENOMEM);		/* failure */
260 }
261 
262 static void
263 urio_write_callback(struct usb_xfer *xfer, usb_error_t error)
264 {
265 	struct urio_softc *sc = usbd_xfer_softc(xfer);
266 	struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX];
267 	struct usb_page_cache *pc;
268 	uint32_t actlen;
269 
270 	switch (USB_GET_STATE(xfer)) {
271 	case USB_ST_TRANSFERRED:
272 	case USB_ST_SETUP:
273 		if (sc->sc_flags & URIO_FLAG_WRITE_STALL) {
274 			usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]);
275 			return;
276 		}
277 		pc = usbd_xfer_get_frame(xfer, 0);
278 		if (usb_fifo_get_data(f, pc, 0,
279 		    usbd_xfer_max_len(xfer), &actlen, 0)) {
280 
281 			usbd_xfer_set_frame_len(xfer, 0, actlen);
282 			usbd_transfer_submit(xfer);
283 		}
284 		return;
285 
286 	default:			/* Error */
287 		if (error != USB_ERR_CANCELLED) {
288 			/* try to clear stall first */
289 			sc->sc_flags |= URIO_FLAG_WRITE_STALL;
290 			usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]);
291 		}
292 		return;
293 	}
294 }
295 
296 static void
297 urio_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
298 {
299 	struct urio_softc *sc = usbd_xfer_softc(xfer);
300 	struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_WR];
301 
302 	if (usbd_clear_stall_callback(xfer, xfer_other)) {
303 		DPRINTF("stall cleared\n");
304 		sc->sc_flags &= ~URIO_FLAG_WRITE_STALL;
305 		usbd_transfer_start(xfer_other);
306 	}
307 }
308 
309 static void
310 urio_read_callback(struct usb_xfer *xfer, usb_error_t error)
311 {
312 	struct urio_softc *sc = usbd_xfer_softc(xfer);
313 	struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_RX];
314 	struct usb_page_cache *pc;
315 	int actlen;
316 
317 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
318 
319 	switch (USB_GET_STATE(xfer)) {
320 	case USB_ST_TRANSFERRED:
321 		pc = usbd_xfer_get_frame(xfer, 0);
322 		usb_fifo_put_data(f, pc, 0, actlen, 1);
323 
324 	case USB_ST_SETUP:
325 		if (sc->sc_flags & URIO_FLAG_READ_STALL) {
326 			usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]);
327 			return;
328 		}
329 		if (usb_fifo_put_bytes_max(f) != 0) {
330 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
331 			usbd_transfer_submit(xfer);
332 		}
333 		return;
334 
335 	default:			/* Error */
336 		if (error != USB_ERR_CANCELLED) {
337 			/* try to clear stall first */
338 			sc->sc_flags |= URIO_FLAG_READ_STALL;
339 			usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]);
340 		}
341 		return;
342 	}
343 }
344 
345 static void
346 urio_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
347 {
348 	struct urio_softc *sc = usbd_xfer_softc(xfer);
349 	struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_RD];
350 
351 	if (usbd_clear_stall_callback(xfer, xfer_other)) {
352 		DPRINTF("stall cleared\n");
353 		sc->sc_flags &= ~URIO_FLAG_READ_STALL;
354 		usbd_transfer_start(xfer_other);
355 	}
356 }
357 
358 static void
359 urio_start_read(struct usb_fifo *fifo)
360 {
361 	struct urio_softc *sc = usb_fifo_softc(fifo);
362 
363 	usbd_transfer_start(sc->sc_xfer[URIO_T_RD]);
364 }
365 
366 static void
367 urio_stop_read(struct usb_fifo *fifo)
368 {
369 	struct urio_softc *sc = usb_fifo_softc(fifo);
370 
371 	usbd_transfer_stop(sc->sc_xfer[URIO_T_RD_CS]);
372 	usbd_transfer_stop(sc->sc_xfer[URIO_T_RD]);
373 }
374 
375 static void
376 urio_start_write(struct usb_fifo *fifo)
377 {
378 	struct urio_softc *sc = usb_fifo_softc(fifo);
379 
380 	usbd_transfer_start(sc->sc_xfer[URIO_T_WR]);
381 }
382 
383 static void
384 urio_stop_write(struct usb_fifo *fifo)
385 {
386 	struct urio_softc *sc = usb_fifo_softc(fifo);
387 
388 	usbd_transfer_stop(sc->sc_xfer[URIO_T_WR_CS]);
389 	usbd_transfer_stop(sc->sc_xfer[URIO_T_WR]);
390 }
391 
392 static int
393 urio_open(struct usb_fifo *fifo, int fflags)
394 {
395 	struct urio_softc *sc = usb_fifo_softc(fifo);
396 
397 	if (fflags & FREAD) {
398 		/* clear stall first */
399 		mtx_lock(&sc->sc_mtx);
400 		sc->sc_flags |= URIO_FLAG_READ_STALL;
401 		mtx_unlock(&sc->sc_mtx);
402 
403 		if (usb_fifo_alloc_buffer(fifo,
404 		    usbd_xfer_max_len(sc->sc_xfer[URIO_T_RD]),
405 		    URIO_IFQ_MAXLEN)) {
406 			return (ENOMEM);
407 		}
408 	}
409 	if (fflags & FWRITE) {
410 		/* clear stall first */
411 		sc->sc_flags |= URIO_FLAG_WRITE_STALL;
412 
413 		if (usb_fifo_alloc_buffer(fifo,
414 		    usbd_xfer_max_len(sc->sc_xfer[URIO_T_WR]),
415 		    URIO_IFQ_MAXLEN)) {
416 			return (ENOMEM);
417 		}
418 	}
419 	return (0);			/* success */
420 }
421 
422 static void
423 urio_close(struct usb_fifo *fifo, int fflags)
424 {
425 	if (fflags & (FREAD | FWRITE)) {
426 		usb_fifo_free_buffer(fifo);
427 	}
428 }
429 
430 static int
431 urio_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
432     int fflags)
433 {
434 	struct usb_ctl_request ur;
435 	struct RioCommand *rio_cmd;
436 	int error;
437 
438 	switch (cmd) {
439 	case RIO_RECV_COMMAND:
440 		if (!(fflags & FWRITE)) {
441 			error = EPERM;
442 			goto done;
443 		}
444 		memset(&ur, 0, sizeof(ur));
445 		rio_cmd = addr;
446 		ur.ucr_request.bmRequestType =
447 		    rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
448 		break;
449 
450 	case RIO_SEND_COMMAND:
451 		if (!(fflags & FWRITE)) {
452 			error = EPERM;
453 			goto done;
454 		}
455 		memset(&ur, 0, sizeof(ur));
456 		rio_cmd = addr;
457 		ur.ucr_request.bmRequestType =
458 		    rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
459 		break;
460 
461 	default:
462 		error = EINVAL;
463 		goto done;
464 	}
465 
466 	DPRINTFN(2, "Sending command\n");
467 
468 	/* Send rio control message */
469 	ur.ucr_request.bRequest = rio_cmd->request;
470 	USETW(ur.ucr_request.wValue, rio_cmd->value);
471 	USETW(ur.ucr_request.wIndex, rio_cmd->index);
472 	USETW(ur.ucr_request.wLength, rio_cmd->length);
473 	ur.ucr_data = rio_cmd->buffer;
474 
475 	/* reuse generic USB code */
476 	error = ugen_do_request(fifo, &ur);
477 
478 done:
479 	return (error);
480 }
481 
482 static int
483 urio_detach(device_t dev)
484 {
485 	struct urio_softc *sc = device_get_softc(dev);
486 
487 	DPRINTF("\n");
488 
489 	usb_fifo_detach(&sc->sc_fifo);
490 
491 	usbd_transfer_unsetup(sc->sc_xfer, URIO_T_MAX);
492 
493 	mtx_destroy(&sc->sc_mtx);
494 
495 	return (0);
496 }
497