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