1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/*
35 * 2000/3/24  added NetBSD/OpenBSD support (from Alex Nemirovsky)
36 * 2000/3/07  use two bulk-pipe handles for read and write (Dirk)
37 * 2000/3/06  change major number(143), and copyright header
38 *            some fix for 4.0 (Dirk)
39 * 2000/3/05  codes for FreeBSD 4.x - CURRENT (Thanks to Dirk-Willem van Gulik)
40 * 2000/3/01  remove retry code from urioioctl()
41 *            change method of bulk transfer (no interrupt)
42 * 2000/2/28  small fixes for new rio_usb.h
43 * 2000/2/24  first version.
44 */
45
46#include <sys/stdint.h>
47#include <sys/stddef.h>
48#include <sys/param.h>
49#include <sys/queue.h>
50#include <sys/types.h>
51#include <sys/systm.h>
52#include <sys/kernel.h>
53#include <sys/bus.h>
54#include <sys/module.h>
55#include <sys/lock.h>
56#include <sys/mutex.h>
57#include <sys/condvar.h>
58#include <sys/sysctl.h>
59#include <sys/sx.h>
60#include <sys/unistd.h>
61#include <sys/callout.h>
62#include <sys/malloc.h>
63#include <sys/priv.h>
64#include <sys/conf.h>
65#include <sys/fcntl.h>
66
67#include <dev/usb/usb.h>
68#include <dev/usb/usbdi.h>
69#include "usbdevs.h"
70
71#include <dev/usb/usb_ioctl.h>
72#include <dev/usb/usb_generic.h>
73
74#define	USB_DEBUG_VAR urio_debug
75#include <dev/usb/usb_debug.h>
76
77#include <dev/usb/storage/rio500_usb.h>
78
79#ifdef USB_DEBUG
80static int urio_debug = 0;
81
82static SYSCTL_NODE(_hw_usb, OID_AUTO, urio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
83    "USB urio");
84SYSCTL_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
97struct 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
113static device_probe_t urio_probe;
114static device_attach_t urio_attach;
115static device_detach_t urio_detach;
116
117static usb_callback_t urio_write_callback;
118static usb_callback_t urio_write_clear_stall_callback;
119static usb_callback_t urio_read_callback;
120static usb_callback_t urio_read_clear_stall_callback;
121
122static usb_fifo_close_t urio_close;
123static usb_fifo_cmd_t urio_start_read;
124static usb_fifo_cmd_t urio_start_write;
125static usb_fifo_cmd_t urio_stop_read;
126static usb_fifo_cmd_t urio_stop_write;
127static usb_fifo_ioctl_t urio_ioctl;
128static usb_fifo_open_t urio_open;
129
130static 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
141static 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
181static device_method_t urio_methods[] = {
182	/* Device interface */
183	DEVMETHOD(device_probe, urio_probe),
184	DEVMETHOD(device_attach, urio_attach),
185	DEVMETHOD(device_detach, urio_detach),
186
187	DEVMETHOD_END
188};
189
190static driver_t urio_driver = {
191	.name = "urio",
192	.methods = urio_methods,
193	.size = sizeof(struct urio_softc),
194};
195
196static const STRUCT_USB_HOST_ID urio_devs[] = {
197	{USB_VPI(USB_VENDOR_DIAMOND, USB_PRODUCT_DIAMOND_RIO500USB, 0)},
198	{USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO600USB, 0)},
199	{USB_VPI(USB_VENDOR_DIAMOND2, USB_PRODUCT_DIAMOND2_RIO800USB, 0)},
200};
201
202DRIVER_MODULE(urio, uhub, urio_driver, NULL, NULL);
203MODULE_DEPEND(urio, usb, 1, 1, 1);
204MODULE_VERSION(urio, 1);
205USB_PNP_HOST_INFO(urio_devs);
206
207static int
208urio_probe(device_t dev)
209{
210	struct usb_attach_arg *uaa = device_get_ivars(dev);
211
212	if (uaa->usb_mode != USB_MODE_HOST)
213		return (ENXIO);
214	if (uaa->info.bConfigIndex != 0)
215		return (ENXIO);
216	if (uaa->info.bIfaceIndex != 0)
217		return (ENXIO);
218
219	return (usbd_lookup_id_by_uaa(urio_devs, sizeof(urio_devs), uaa));
220}
221
222static int
223urio_attach(device_t dev)
224{
225	struct usb_attach_arg *uaa = device_get_ivars(dev);
226	struct urio_softc *sc = device_get_softc(dev);
227	int error;
228
229	device_set_usb_desc(dev);
230
231	sc->sc_udev = uaa->device;
232
233	mtx_init(&sc->sc_mtx, "urio lock", NULL, MTX_DEF | MTX_RECURSE);
234
235	snprintf(sc->sc_name, sizeof(sc->sc_name),
236	    "%s", device_get_nameunit(dev));
237
238	error = usbd_transfer_setup(uaa->device,
239	    &uaa->info.bIfaceIndex, sc->sc_xfer,
240	    urio_config, URIO_T_MAX, sc, &sc->sc_mtx);
241
242	if (error) {
243		DPRINTF("error=%s\n", usbd_errstr(error));
244		goto detach;
245	}
246
247	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
248	    &urio_fifo_methods, &sc->sc_fifo,
249	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
250	    UID_ROOT, GID_OPERATOR, 0644);
251	if (error) {
252		goto detach;
253	}
254	return (0);			/* success */
255
256detach:
257	urio_detach(dev);
258	return (ENOMEM);		/* failure */
259}
260
261static void
262urio_write_callback(struct usb_xfer *xfer, usb_error_t error)
263{
264	struct urio_softc *sc = usbd_xfer_softc(xfer);
265	struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_TX];
266	struct usb_page_cache *pc;
267	uint32_t actlen;
268
269	switch (USB_GET_STATE(xfer)) {
270	case USB_ST_TRANSFERRED:
271	case USB_ST_SETUP:
272		if (sc->sc_flags & URIO_FLAG_WRITE_STALL) {
273			usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]);
274			return;
275		}
276		pc = usbd_xfer_get_frame(xfer, 0);
277		if (usb_fifo_get_data(f, pc, 0,
278		    usbd_xfer_max_len(xfer), &actlen, 0)) {
279			usbd_xfer_set_frame_len(xfer, 0, actlen);
280			usbd_transfer_submit(xfer);
281		}
282		return;
283
284	default:			/* Error */
285		if (error != USB_ERR_CANCELLED) {
286			/* try to clear stall first */
287			sc->sc_flags |= URIO_FLAG_WRITE_STALL;
288			usbd_transfer_start(sc->sc_xfer[URIO_T_WR_CS]);
289		}
290		return;
291	}
292}
293
294static void
295urio_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
296{
297	struct urio_softc *sc = usbd_xfer_softc(xfer);
298	struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_WR];
299
300	if (usbd_clear_stall_callback(xfer, xfer_other)) {
301		DPRINTF("stall cleared\n");
302		sc->sc_flags &= ~URIO_FLAG_WRITE_STALL;
303		usbd_transfer_start(xfer_other);
304	}
305}
306
307static void
308urio_read_callback(struct usb_xfer *xfer, usb_error_t error)
309{
310	struct urio_softc *sc = usbd_xfer_softc(xfer);
311	struct usb_fifo *f = sc->sc_fifo.fp[USB_FIFO_RX];
312	struct usb_page_cache *pc;
313	int actlen;
314
315	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
316
317	switch (USB_GET_STATE(xfer)) {
318	case USB_ST_TRANSFERRED:
319		pc = usbd_xfer_get_frame(xfer, 0);
320		usb_fifo_put_data(f, pc, 0, actlen, 1);
321
322	case USB_ST_SETUP:
323		if (sc->sc_flags & URIO_FLAG_READ_STALL) {
324			usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]);
325			return;
326		}
327		if (usb_fifo_put_bytes_max(f) != 0) {
328			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
329			usbd_transfer_submit(xfer);
330		}
331		return;
332
333	default:			/* Error */
334		if (error != USB_ERR_CANCELLED) {
335			/* try to clear stall first */
336			sc->sc_flags |= URIO_FLAG_READ_STALL;
337			usbd_transfer_start(sc->sc_xfer[URIO_T_RD_CS]);
338		}
339		return;
340	}
341}
342
343static void
344urio_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
345{
346	struct urio_softc *sc = usbd_xfer_softc(xfer);
347	struct usb_xfer *xfer_other = sc->sc_xfer[URIO_T_RD];
348
349	if (usbd_clear_stall_callback(xfer, xfer_other)) {
350		DPRINTF("stall cleared\n");
351		sc->sc_flags &= ~URIO_FLAG_READ_STALL;
352		usbd_transfer_start(xfer_other);
353	}
354}
355
356static void
357urio_start_read(struct usb_fifo *fifo)
358{
359	struct urio_softc *sc = usb_fifo_softc(fifo);
360
361	usbd_transfer_start(sc->sc_xfer[URIO_T_RD]);
362}
363
364static void
365urio_stop_read(struct usb_fifo *fifo)
366{
367	struct urio_softc *sc = usb_fifo_softc(fifo);
368
369	usbd_transfer_stop(sc->sc_xfer[URIO_T_RD_CS]);
370	usbd_transfer_stop(sc->sc_xfer[URIO_T_RD]);
371}
372
373static void
374urio_start_write(struct usb_fifo *fifo)
375{
376	struct urio_softc *sc = usb_fifo_softc(fifo);
377
378	usbd_transfer_start(sc->sc_xfer[URIO_T_WR]);
379}
380
381static void
382urio_stop_write(struct usb_fifo *fifo)
383{
384	struct urio_softc *sc = usb_fifo_softc(fifo);
385
386	usbd_transfer_stop(sc->sc_xfer[URIO_T_WR_CS]);
387	usbd_transfer_stop(sc->sc_xfer[URIO_T_WR]);
388}
389
390static int
391urio_open(struct usb_fifo *fifo, int fflags)
392{
393	struct urio_softc *sc = usb_fifo_softc(fifo);
394
395	if (fflags & FREAD) {
396		/* clear stall first */
397		mtx_lock(&sc->sc_mtx);
398		sc->sc_flags |= URIO_FLAG_READ_STALL;
399		mtx_unlock(&sc->sc_mtx);
400
401		if (usb_fifo_alloc_buffer(fifo,
402		    usbd_xfer_max_len(sc->sc_xfer[URIO_T_RD]),
403		    URIO_IFQ_MAXLEN)) {
404			return (ENOMEM);
405		}
406	}
407	if (fflags & FWRITE) {
408		/* clear stall first */
409		sc->sc_flags |= URIO_FLAG_WRITE_STALL;
410
411		if (usb_fifo_alloc_buffer(fifo,
412		    usbd_xfer_max_len(sc->sc_xfer[URIO_T_WR]),
413		    URIO_IFQ_MAXLEN)) {
414			return (ENOMEM);
415		}
416	}
417	return (0);			/* success */
418}
419
420static void
421urio_close(struct usb_fifo *fifo, int fflags)
422{
423	if (fflags & (FREAD | FWRITE)) {
424		usb_fifo_free_buffer(fifo);
425	}
426}
427
428static int
429urio_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
430    int fflags)
431{
432	struct usb_ctl_request ur;
433	struct RioCommand *rio_cmd;
434	int error;
435
436	switch (cmd) {
437	case RIO_RECV_COMMAND:
438		if (!(fflags & FWRITE)) {
439			error = EPERM;
440			goto done;
441		}
442		memset(&ur, 0, sizeof(ur));
443		rio_cmd = addr;
444		ur.ucr_request.bmRequestType =
445		    rio_cmd->requesttype | UT_READ_VENDOR_DEVICE;
446		break;
447
448	case RIO_SEND_COMMAND:
449		if (!(fflags & FWRITE)) {
450			error = EPERM;
451			goto done;
452		}
453		memset(&ur, 0, sizeof(ur));
454		rio_cmd = addr;
455		ur.ucr_request.bmRequestType =
456		    rio_cmd->requesttype | UT_WRITE_VENDOR_DEVICE;
457		break;
458
459	default:
460		error = EINVAL;
461		goto done;
462	}
463
464	DPRINTFN(2, "Sending command\n");
465
466	/* Send rio control message */
467	ur.ucr_request.bRequest = rio_cmd->request;
468	USETW(ur.ucr_request.wValue, rio_cmd->value);
469	USETW(ur.ucr_request.wIndex, rio_cmd->index);
470	USETW(ur.ucr_request.wLength, rio_cmd->length);
471	ur.ucr_data = rio_cmd->buffer;
472
473	/* reuse generic USB code */
474	error = ugen_do_request(fifo, &ur);
475
476done:
477	return (error);
478}
479
480static int
481urio_detach(device_t dev)
482{
483	struct urio_softc *sc = device_get_softc(dev);
484
485	DPRINTF("\n");
486
487	usb_fifo_detach(&sc->sc_fifo);
488
489	usbd_transfer_unsetup(sc->sc_xfer, URIO_T_MAX);
490
491	mtx_destroy(&sc->sc_mtx);
492
493	return (0);
494}
495