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