ubser.c revision 187259
1/*-
2 * Copyright (c) 2004 Bernd Walter <ticso@freebsd.org>
3 *
4 * $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $
5 * $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $
6 * $Author: ticso $
7 * $Rev: 1127 $
8 */
9
10/*-
11 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*-
37 * Copyright (c) 2000 The NetBSD Foundation, Inc.
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Lennart Augustsson (lennart@augustsson.net).
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 *    must display the following acknowledgement:
53 *        This product includes software developed by the NetBSD
54 *        Foundation, Inc. and its contributors.
55 * 4. Neither the name of The NetBSD Foundation nor the names of its
56 *    contributors may be used to endorse or promote products derived
57 *    from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
70 */
71
72#include <sys/cdefs.h>
73__FBSDID("$FreeBSD: head/sys/dev/usb2/serial/ubser2.c 187259 2009-01-15 02:35:40Z thompsa $");
74
75/*
76 * BWCT serial adapter driver
77 */
78
79#include <dev/usb2/include/usb2_standard.h>
80#include <dev/usb2/include/usb2_mfunc.h>
81#include <dev/usb2/include/usb2_error.h>
82#include <dev/usb2/include/usb2_cdc.h>
83#include <dev/usb2/include/usb2_defs.h>
84
85#define	USB_DEBUG_VAR ubser_debug
86
87#include <dev/usb2/core/usb2_core.h>
88#include <dev/usb2/core/usb2_debug.h>
89#include <dev/usb2/core/usb2_process.h>
90#include <dev/usb2/core/usb2_request.h>
91#include <dev/usb2/core/usb2_lookup.h>
92#include <dev/usb2/core/usb2_util.h>
93#include <dev/usb2/core/usb2_busdma.h>
94#include <dev/usb2/core/usb2_device.h>
95
96#include <dev/usb2/serial/usb2_serial.h>
97
98#define	UBSER_UNIT_MAX	32
99
100/* Vendor Interface Requests */
101#define	VENDOR_GET_NUMSER		0x01
102#define	VENDOR_SET_BREAK		0x02
103#define	VENDOR_CLEAR_BREAK		0x03
104
105#if USB_DEBUG
106static int ubser_debug = 0;
107
108SYSCTL_NODE(_hw_usb2, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
109SYSCTL_INT(_hw_usb2_ubser, OID_AUTO, debug, CTLFLAG_RW,
110    &ubser_debug, 0, "ubser debug level");
111#endif
112
113enum {
114	UBSER_BULK_DT_WR,
115	UBSER_BULK_DT_RD,
116	UBSER_BULK_CS_WR,
117	UBSER_BULK_CS_RD,
118	UBSER_N_TRANSFER = 4,
119};
120
121struct ubser_softc {
122	struct usb2_com_super_softc sc_super_ucom;
123	struct usb2_com_softc sc_ucom[UBSER_UNIT_MAX];
124
125	struct usb2_xfer *sc_xfer[UBSER_N_TRANSFER];
126	struct usb2_device *sc_udev;
127
128	uint16_t sc_tx_size;
129
130	uint8_t	sc_numser;
131	uint8_t	sc_flags;
132#define	UBSER_FLAG_READ_STALL  0x01
133#define	UBSER_FLAG_WRITE_STALL 0x02
134
135	uint8_t	sc_iface_no;
136	uint8_t	sc_iface_index;
137	uint8_t	sc_curr_tx_unit;
138	uint8_t	sc_name[16];
139};
140
141/* prototypes */
142
143static device_probe_t ubser_probe;
144static device_attach_t ubser_attach;
145static device_detach_t ubser_detach;
146
147static usb2_callback_t ubser_write_clear_stall_callback;
148static usb2_callback_t ubser_write_callback;
149static usb2_callback_t ubser_read_clear_stall_callback;
150static usb2_callback_t ubser_read_callback;
151
152static int	ubser_pre_param(struct usb2_com_softc *, struct termios *);
153static void	ubser_cfg_set_break(struct usb2_com_softc *, uint8_t);
154static void	ubser_cfg_get_status(struct usb2_com_softc *, uint8_t *,
155		    uint8_t *);
156static void	ubser_start_read(struct usb2_com_softc *);
157static void	ubser_stop_read(struct usb2_com_softc *);
158static void	ubser_start_write(struct usb2_com_softc *);
159static void	ubser_stop_write(struct usb2_com_softc *);
160
161static const struct usb2_config ubser_config[UBSER_N_TRANSFER] = {
162
163	[UBSER_BULK_DT_WR] = {
164		.type = UE_BULK,
165		.endpoint = UE_ADDR_ANY,
166		.direction = UE_DIR_OUT,
167		.mh.bufsize = 0,	/* use wMaxPacketSize */
168		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
169		.mh.callback = &ubser_write_callback,
170	},
171
172	[UBSER_BULK_DT_RD] = {
173		.type = UE_BULK,
174		.endpoint = UE_ADDR_ANY,
175		.direction = UE_DIR_IN,
176		.mh.bufsize = 0,	/* use wMaxPacketSize */
177		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
178		.mh.callback = &ubser_read_callback,
179	},
180
181	[UBSER_BULK_CS_WR] = {
182		.type = UE_CONTROL,
183		.endpoint = 0x00,	/* Control pipe */
184		.direction = UE_DIR_ANY,
185		.mh.bufsize = sizeof(struct usb2_device_request),
186		.mh.flags = {},
187		.mh.callback = &ubser_write_clear_stall_callback,
188		.mh.timeout = 1000,	/* 1 second */
189		.mh.interval = 50,	/* 50ms */
190	},
191
192	[UBSER_BULK_CS_RD] = {
193		.type = UE_CONTROL,
194		.endpoint = 0x00,	/* Control pipe */
195		.direction = UE_DIR_ANY,
196		.mh.bufsize = sizeof(struct usb2_device_request),
197		.mh.flags = {},
198		.mh.callback = &ubser_read_clear_stall_callback,
199		.mh.timeout = 1000,	/* 1 second */
200		.mh.interval = 50,	/* 50ms */
201	},
202};
203
204static const struct usb2_com_callback ubser_callback = {
205	.usb2_com_cfg_set_break = &ubser_cfg_set_break,
206	.usb2_com_cfg_get_status = &ubser_cfg_get_status,
207	.usb2_com_pre_param = &ubser_pre_param,
208	.usb2_com_start_read = &ubser_start_read,
209	.usb2_com_stop_read = &ubser_stop_read,
210	.usb2_com_start_write = &ubser_start_write,
211	.usb2_com_stop_write = &ubser_stop_write,
212};
213
214static device_method_t ubser_methods[] = {
215	DEVMETHOD(device_probe, ubser_probe),
216	DEVMETHOD(device_attach, ubser_attach),
217	DEVMETHOD(device_detach, ubser_detach),
218	{0, 0}
219};
220
221static devclass_t ubser_devclass;
222
223static driver_t ubser_driver = {
224	.name = "ubser",
225	.methods = ubser_methods,
226	.size = sizeof(struct ubser_softc),
227};
228
229DRIVER_MODULE(ubser, ushub, ubser_driver, ubser_devclass, NULL, 0);
230MODULE_DEPEND(ubser, usb2_serial, 1, 1, 1);
231MODULE_DEPEND(ubser, usb2_core, 1, 1, 1);
232
233static int
234ubser_probe(device_t dev)
235{
236	struct usb2_attach_arg *uaa = device_get_ivars(dev);
237
238	if (uaa->usb2_mode != USB_MODE_HOST) {
239		return (ENXIO);
240	}
241	/* check if this is a BWCT vendor specific ubser interface */
242	if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) &&
243	    (uaa->info.bInterfaceClass == 0xff) &&
244	    (uaa->info.bInterfaceSubClass == 0x00))
245		return (0);
246
247	return (ENXIO);
248}
249
250static int
251ubser_attach(device_t dev)
252{
253	struct usb2_attach_arg *uaa = device_get_ivars(dev);
254	struct ubser_softc *sc = device_get_softc(dev);
255	struct usb2_device_request req;
256	uint8_t n;
257	int error;
258
259	if (sc == NULL) {
260		return (ENOMEM);
261	}
262	device_set_usb2_desc(dev);
263
264	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
265	    device_get_nameunit(dev));
266
267	sc->sc_iface_no = uaa->info.bIfaceNum;
268	sc->sc_iface_index = uaa->info.bIfaceIndex;
269	sc->sc_udev = uaa->device;
270
271	/* get number of serials */
272	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
273	req.bRequest = VENDOR_GET_NUMSER;
274	USETW(req.wValue, 0);
275	req.wIndex[0] = sc->sc_iface_no;
276	req.wIndex[1] = 0;
277	USETW(req.wLength, 1);
278	error = usb2_do_request_flags
279	    (uaa->device, &Giant, &req, &sc->sc_numser,
280	    0, NULL, USB_DEFAULT_TIMEOUT);
281
282	if (error || (sc->sc_numser == 0)) {
283		device_printf(dev, "failed to get number "
284		    "of serial ports: %s\n",
285		    usb2_errstr(error));
286		goto detach;
287	}
288	if (sc->sc_numser > UBSER_UNIT_MAX)
289		sc->sc_numser = UBSER_UNIT_MAX;
290
291	device_printf(dev, "found %i serials\n", sc->sc_numser);
292
293	error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
294	    sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &Giant);
295	if (error) {
296		goto detach;
297	}
298	sc->sc_tx_size = sc->sc_xfer[UBSER_BULK_DT_WR]->max_data_length;
299
300	if (sc->sc_tx_size == 0) {
301		DPRINTFN(0, "invalid tx_size!\n");
302		goto detach;
303	}
304	/* initialize port numbers */
305
306	for (n = 0; n < sc->sc_numser; n++) {
307		sc->sc_ucom[n].sc_portno = n;
308	}
309
310	error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom,
311	    sc->sc_numser, sc, &ubser_callback, &Giant);
312	if (error) {
313		goto detach;
314	}
315	mtx_lock(&Giant);
316
317	sc->sc_flags |= (UBSER_FLAG_READ_STALL |
318	    UBSER_FLAG_WRITE_STALL);
319
320	usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
321
322	mtx_unlock(&Giant);
323
324	return (0);			/* success */
325
326detach:
327	ubser_detach(dev);
328	return (ENXIO);			/* failure */
329}
330
331static int
332ubser_detach(device_t dev)
333{
334	struct ubser_softc *sc = device_get_softc(dev);
335	uint8_t n;
336
337	DPRINTF("\n");
338
339	usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser);
340
341	/*
342	 * need to stop all transfers atomically, hence when clear stall
343	 * completes, it might start other transfers !
344	 */
345	mtx_lock(&Giant);
346	for (n = 0; n < UBSER_N_TRANSFER; n++) {
347		usb2_transfer_stop(sc->sc_xfer[n]);
348	}
349	mtx_unlock(&Giant);
350
351	usb2_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
352
353	return (0);
354}
355
356static int
357ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t)
358{
359	DPRINTF("\n");
360
361	/*
362	 * The firmware on our devices can only do 8n1@9600bps
363	 * without handshake.
364	 * We refuse to accept other configurations.
365	 */
366
367	/* ensure 9600bps */
368	switch (t->c_ospeed) {
369	case 9600:
370		break;
371	default:
372		return (EINVAL);
373	}
374
375	/* 2 stop bits not possible */
376	if (t->c_cflag & CSTOPB)
377		return (EINVAL);
378
379	/* XXX parity handling not possible with current firmware */
380	if (t->c_cflag & PARENB)
381		return (EINVAL);
382
383	/* we can only do 8 data bits */
384	switch (t->c_cflag & CSIZE) {
385	case CS8:
386		break;
387	default:
388		return (EINVAL);
389	}
390
391	/* we can't do any kind of hardware handshaking */
392	if ((t->c_cflag &
393	    (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
394		return (EINVAL);
395
396	/*
397	 * XXX xon/xoff not supported by the firmware!
398	 * This is handled within FreeBSD only and may overflow buffers
399	 * because of delayed reaction due to device buffering.
400	 */
401
402	return (0);
403}
404
405static __inline void
406ubser_inc_tx_unit(struct ubser_softc *sc)
407{
408	sc->sc_curr_tx_unit++;
409	if (sc->sc_curr_tx_unit >= sc->sc_numser) {
410		sc->sc_curr_tx_unit = 0;
411	}
412}
413
414static void
415ubser_write_clear_stall_callback(struct usb2_xfer *xfer)
416{
417	struct ubser_softc *sc = xfer->priv_sc;
418	struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_BULK_DT_WR];
419
420	if (usb2_clear_stall_callback(xfer, xfer_other)) {
421		DPRINTF("stall cleared\n");
422		sc->sc_flags &= ~UBSER_FLAG_WRITE_STALL;
423		usb2_transfer_start(xfer_other);
424	}
425}
426
427static void
428ubser_write_callback(struct usb2_xfer *xfer)
429{
430	struct ubser_softc *sc = xfer->priv_sc;
431	uint8_t buf[1];
432	uint8_t first_unit = sc->sc_curr_tx_unit;
433	uint32_t actlen;
434
435	switch (USB_GET_STATE(xfer)) {
436	case USB_ST_SETUP:
437	case USB_ST_TRANSFERRED:
438		if (sc->sc_flags & UBSER_FLAG_WRITE_STALL) {
439			usb2_transfer_start(sc->sc_xfer[UBSER_BULK_CS_WR]);
440			return;
441		}
442		do {
443			if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
444			    xfer->frbuffers, 1, sc->sc_tx_size - 1,
445			    &actlen)) {
446
447				buf[0] = sc->sc_curr_tx_unit;
448
449				usb2_copy_in(xfer->frbuffers, 0, buf, 1);
450
451				xfer->frlengths[0] = actlen + 1;
452				usb2_start_hardware(xfer);
453
454				ubser_inc_tx_unit(sc);	/* round robin */
455
456				break;
457			}
458			ubser_inc_tx_unit(sc);
459
460		} while (sc->sc_curr_tx_unit != first_unit);
461
462		return;
463
464	default:			/* Error */
465		if (xfer->error != USB_ERR_CANCELLED) {
466			sc->sc_flags |= UBSER_FLAG_WRITE_STALL;
467			usb2_transfer_start(sc->sc_xfer[UBSER_BULK_CS_WR]);
468		}
469		return;
470
471	}
472}
473
474static void
475ubser_read_clear_stall_callback(struct usb2_xfer *xfer)
476{
477	struct ubser_softc *sc = xfer->priv_sc;
478	struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_BULK_DT_RD];
479
480	if (usb2_clear_stall_callback(xfer, xfer_other)) {
481		DPRINTF("stall cleared\n");
482		sc->sc_flags &= ~UBSER_FLAG_READ_STALL;
483		usb2_transfer_start(xfer_other);
484	}
485}
486
487static void
488ubser_read_callback(struct usb2_xfer *xfer)
489{
490	struct ubser_softc *sc = xfer->priv_sc;
491	uint8_t buf[1];
492
493	switch (USB_GET_STATE(xfer)) {
494	case USB_ST_TRANSFERRED:
495		if (xfer->actlen < 1) {
496			DPRINTF("invalid actlen=0!\n");
497			goto tr_setup;
498		}
499		usb2_copy_out(xfer->frbuffers, 0, buf, 1);
500
501		if (buf[0] >= sc->sc_numser) {
502			DPRINTF("invalid serial number!\n");
503			goto tr_setup;
504		}
505		usb2_com_put_data(sc->sc_ucom + buf[0],
506		    xfer->frbuffers, 1, xfer->actlen - 1);
507
508	case USB_ST_SETUP:
509tr_setup:
510		if (sc->sc_flags & UBSER_FLAG_READ_STALL) {
511			usb2_transfer_start(sc->sc_xfer[UBSER_BULK_CS_RD]);
512		} else {
513			xfer->frlengths[0] = xfer->max_data_length;
514			usb2_start_hardware(xfer);
515		}
516		return;
517
518	default:			/* Error */
519		if (xfer->error != USB_ERR_CANCELLED) {
520			sc->sc_flags |= UBSER_FLAG_READ_STALL;
521			usb2_transfer_start(sc->sc_xfer[UBSER_BULK_CS_RD]);
522		}
523		return;
524
525	}
526}
527
528static void
529ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
530{
531	struct ubser_softc *sc = ucom->sc_parent;
532	uint8_t x = ucom->sc_portno;
533	struct usb2_device_request req;
534	usb2_error_t err;
535
536	if (onoff) {
537
538		req.bmRequestType = UT_READ_VENDOR_INTERFACE;
539		req.bRequest = VENDOR_SET_BREAK;
540		req.wValue[0] = x;
541		req.wValue[1] = 0;
542		req.wIndex[0] = sc->sc_iface_no;
543		req.wIndex[1] = 0;
544		USETW(req.wLength, 0);
545
546		err = usb2_do_request_flags
547		    (sc->sc_udev, &Giant, &req, NULL, 0, NULL, 1000);
548
549		if (err) {
550			DPRINTFN(0, "send break failed, error=%s\n",
551			    usb2_errstr(err));
552		}
553	}
554}
555
556static void
557ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
558{
559	/* fake status bits */
560	*lsr = 0;
561	*msr = SER_DCD;
562}
563
564static void
565ubser_start_read(struct usb2_com_softc *ucom)
566{
567	struct ubser_softc *sc = ucom->sc_parent;
568
569	usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
570}
571
572static void
573ubser_stop_read(struct usb2_com_softc *ucom)
574{
575	struct ubser_softc *sc = ucom->sc_parent;
576
577	usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_CS_RD]);
578	usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
579}
580
581static void
582ubser_start_write(struct usb2_com_softc *ucom)
583{
584	struct ubser_softc *sc = ucom->sc_parent;
585
586	usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
587}
588
589static void
590ubser_stop_write(struct usb2_com_softc *ucom)
591{
592	struct ubser_softc *sc = ucom->sc_parent;
593
594	usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_CS_WR]);
595	usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
596}
597