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