ubser.c revision 190172
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/usb/serial/ubser.c 190172 2009-03-20 18:56:27Z thompsa $");
74
75/*
76 * BWCT serial adapter driver
77 */
78
79#include <dev/usb/usb.h>
80#include <dev/usb/usb_mfunc.h>
81#include <dev/usb/usb_error.h>
82#include <dev/usb/usb_cdc.h>
83#include <dev/usb/usb_defs.h>
84
85#define	USB_DEBUG_VAR ubser_debug
86
87#include <dev/usb/usb_core.h>
88#include <dev/usb/usb_debug.h>
89#include <dev/usb/usb_process.h>
90#include <dev/usb/usb_request.h>
91#include <dev/usb/usb_lookup.h>
92#include <dev/usb/usb_util.h>
93#include <dev/usb/usb_busdma.h>
94#include <dev/usb/usb_device.h>
95
96#include <dev/usb/serial/usb_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_N_TRANSFER,
117};
118
119struct ubser_softc {
120	struct usb2_com_super_softc sc_super_ucom;
121	struct usb2_com_softc sc_ucom[UBSER_UNIT_MAX];
122
123	struct usb2_xfer *sc_xfer[UBSER_N_TRANSFER];
124	struct usb2_device *sc_udev;
125	struct mtx sc_mtx;
126
127	uint16_t sc_tx_size;
128
129	uint8_t	sc_numser;
130	uint8_t	sc_iface_no;
131	uint8_t	sc_iface_index;
132	uint8_t	sc_curr_tx_unit;
133	uint8_t	sc_name[16];
134};
135
136/* prototypes */
137
138static device_probe_t ubser_probe;
139static device_attach_t ubser_attach;
140static device_detach_t ubser_detach;
141
142static usb2_callback_t ubser_write_callback;
143static usb2_callback_t ubser_read_callback;
144
145static int	ubser_pre_param(struct usb2_com_softc *, struct termios *);
146static void	ubser_cfg_set_break(struct usb2_com_softc *, uint8_t);
147static void	ubser_cfg_get_status(struct usb2_com_softc *, uint8_t *,
148		    uint8_t *);
149static void	ubser_start_read(struct usb2_com_softc *);
150static void	ubser_stop_read(struct usb2_com_softc *);
151static void	ubser_start_write(struct usb2_com_softc *);
152static void	ubser_stop_write(struct usb2_com_softc *);
153
154static const struct usb2_config ubser_config[UBSER_N_TRANSFER] = {
155
156	[UBSER_BULK_DT_WR] = {
157		.type = UE_BULK,
158		.endpoint = UE_ADDR_ANY,
159		.direction = UE_DIR_OUT,
160		.mh.bufsize = 0,	/* use wMaxPacketSize */
161		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
162		.mh.callback = &ubser_write_callback,
163	},
164
165	[UBSER_BULK_DT_RD] = {
166		.type = UE_BULK,
167		.endpoint = UE_ADDR_ANY,
168		.direction = UE_DIR_IN,
169		.mh.bufsize = 0,	/* use wMaxPacketSize */
170		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
171		.mh.callback = &ubser_read_callback,
172	},
173};
174
175static const struct usb2_com_callback ubser_callback = {
176	.usb2_com_cfg_set_break = &ubser_cfg_set_break,
177	.usb2_com_cfg_get_status = &ubser_cfg_get_status,
178	.usb2_com_pre_param = &ubser_pre_param,
179	.usb2_com_start_read = &ubser_start_read,
180	.usb2_com_stop_read = &ubser_stop_read,
181	.usb2_com_start_write = &ubser_start_write,
182	.usb2_com_stop_write = &ubser_stop_write,
183};
184
185static device_method_t ubser_methods[] = {
186	DEVMETHOD(device_probe, ubser_probe),
187	DEVMETHOD(device_attach, ubser_attach),
188	DEVMETHOD(device_detach, ubser_detach),
189	{0, 0}
190};
191
192static devclass_t ubser_devclass;
193
194static driver_t ubser_driver = {
195	.name = "ubser",
196	.methods = ubser_methods,
197	.size = sizeof(struct ubser_softc),
198};
199
200DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, NULL, 0);
201MODULE_DEPEND(ubser, ucom, 1, 1, 1);
202MODULE_DEPEND(ubser, usb, 1, 1, 1);
203
204static int
205ubser_probe(device_t dev)
206{
207	struct usb2_attach_arg *uaa = device_get_ivars(dev);
208
209	if (uaa->usb2_mode != USB_MODE_HOST) {
210		return (ENXIO);
211	}
212	/* check if this is a BWCT vendor specific ubser interface */
213	if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) &&
214	    (uaa->info.bInterfaceClass == 0xff) &&
215	    (uaa->info.bInterfaceSubClass == 0x00))
216		return (0);
217
218	return (ENXIO);
219}
220
221static int
222ubser_attach(device_t dev)
223{
224	struct usb2_attach_arg *uaa = device_get_ivars(dev);
225	struct ubser_softc *sc = device_get_softc(dev);
226	struct usb2_device_request req;
227	uint8_t n;
228	int error;
229
230	device_set_usb2_desc(dev);
231	mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF);
232
233	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
234	    device_get_nameunit(dev));
235
236	sc->sc_iface_no = uaa->info.bIfaceNum;
237	sc->sc_iface_index = uaa->info.bIfaceIndex;
238	sc->sc_udev = uaa->device;
239
240	/* get number of serials */
241	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
242	req.bRequest = VENDOR_GET_NUMSER;
243	USETW(req.wValue, 0);
244	req.wIndex[0] = sc->sc_iface_no;
245	req.wIndex[1] = 0;
246	USETW(req.wLength, 1);
247	error = usb2_do_request_flags(uaa->device, NULL,
248	    &req, &sc->sc_numser,
249	    0, NULL, USB_DEFAULT_TIMEOUT);
250
251	if (error || (sc->sc_numser == 0)) {
252		device_printf(dev, "failed to get number "
253		    "of serial ports: %s\n",
254		    usb2_errstr(error));
255		goto detach;
256	}
257	if (sc->sc_numser > UBSER_UNIT_MAX)
258		sc->sc_numser = UBSER_UNIT_MAX;
259
260	device_printf(dev, "found %i serials\n", sc->sc_numser);
261
262	error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
263	    sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx);
264	if (error) {
265		goto detach;
266	}
267	sc->sc_tx_size = sc->sc_xfer[UBSER_BULK_DT_WR]->max_data_length;
268
269	if (sc->sc_tx_size == 0) {
270		DPRINTFN(0, "invalid tx_size!\n");
271		goto detach;
272	}
273	/* initialize port numbers */
274
275	for (n = 0; n < sc->sc_numser; n++) {
276		sc->sc_ucom[n].sc_portno = n;
277	}
278
279	error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom,
280	    sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx);
281	if (error) {
282		goto detach;
283	}
284
285	mtx_lock(&sc->sc_mtx);
286	usb2_transfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]);
287	usb2_transfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]);
288	usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
289	mtx_unlock(&sc->sc_mtx);
290
291	return (0);			/* success */
292
293detach:
294	ubser_detach(dev);
295	return (ENXIO);			/* failure */
296}
297
298static int
299ubser_detach(device_t dev)
300{
301	struct ubser_softc *sc = device_get_softc(dev);
302
303	DPRINTF("\n");
304
305	usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser);
306	usb2_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
307	mtx_destroy(&sc->sc_mtx);
308
309	return (0);
310}
311
312static int
313ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t)
314{
315	DPRINTF("\n");
316
317	/*
318	 * The firmware on our devices can only do 8n1@9600bps
319	 * without handshake.
320	 * We refuse to accept other configurations.
321	 */
322
323	/* ensure 9600bps */
324	switch (t->c_ospeed) {
325	case 9600:
326		break;
327	default:
328		return (EINVAL);
329	}
330
331	/* 2 stop bits not possible */
332	if (t->c_cflag & CSTOPB)
333		return (EINVAL);
334
335	/* XXX parity handling not possible with current firmware */
336	if (t->c_cflag & PARENB)
337		return (EINVAL);
338
339	/* we can only do 8 data bits */
340	switch (t->c_cflag & CSIZE) {
341	case CS8:
342		break;
343	default:
344		return (EINVAL);
345	}
346
347	/* we can't do any kind of hardware handshaking */
348	if ((t->c_cflag &
349	    (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
350		return (EINVAL);
351
352	/*
353	 * XXX xon/xoff not supported by the firmware!
354	 * This is handled within FreeBSD only and may overflow buffers
355	 * because of delayed reaction due to device buffering.
356	 */
357
358	return (0);
359}
360
361static __inline void
362ubser_inc_tx_unit(struct ubser_softc *sc)
363{
364	sc->sc_curr_tx_unit++;
365	if (sc->sc_curr_tx_unit >= sc->sc_numser) {
366		sc->sc_curr_tx_unit = 0;
367	}
368}
369
370static void
371ubser_write_callback(struct usb2_xfer *xfer)
372{
373	struct ubser_softc *sc = xfer->priv_sc;
374	uint8_t buf[1];
375	uint8_t first_unit = sc->sc_curr_tx_unit;
376	uint32_t actlen;
377
378	switch (USB_GET_STATE(xfer)) {
379	case USB_ST_SETUP:
380	case USB_ST_TRANSFERRED:
381tr_setup:
382		do {
383			if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
384			    xfer->frbuffers, 1, sc->sc_tx_size - 1,
385			    &actlen)) {
386
387				buf[0] = sc->sc_curr_tx_unit;
388
389				usb2_copy_in(xfer->frbuffers, 0, buf, 1);
390
391				xfer->frlengths[0] = actlen + 1;
392				usb2_start_hardware(xfer);
393
394				ubser_inc_tx_unit(sc);	/* round robin */
395
396				break;
397			}
398			ubser_inc_tx_unit(sc);
399
400		} while (sc->sc_curr_tx_unit != first_unit);
401
402		return;
403
404	default:			/* Error */
405		if (xfer->error != USB_ERR_CANCELLED) {
406			/* try to clear stall first */
407			xfer->flags.stall_pipe = 1;
408			goto tr_setup;
409		}
410		return;
411
412	}
413}
414
415static void
416ubser_read_callback(struct usb2_xfer *xfer)
417{
418	struct ubser_softc *sc = xfer->priv_sc;
419	uint8_t buf[1];
420
421	switch (USB_GET_STATE(xfer)) {
422	case USB_ST_TRANSFERRED:
423		if (xfer->actlen < 1) {
424			DPRINTF("invalid actlen=0!\n");
425			goto tr_setup;
426		}
427		usb2_copy_out(xfer->frbuffers, 0, buf, 1);
428
429		if (buf[0] >= sc->sc_numser) {
430			DPRINTF("invalid serial number!\n");
431			goto tr_setup;
432		}
433		usb2_com_put_data(sc->sc_ucom + buf[0],
434		    xfer->frbuffers, 1, xfer->actlen - 1);
435
436	case USB_ST_SETUP:
437tr_setup:
438		xfer->frlengths[0] = xfer->max_data_length;
439		usb2_start_hardware(xfer);
440		return;
441
442	default:			/* Error */
443		if (xfer->error != USB_ERR_CANCELLED) {
444			/* try to clear stall first */
445			xfer->flags.stall_pipe = 1;
446			goto tr_setup;
447		}
448		return;
449
450	}
451}
452
453static void
454ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
455{
456	struct ubser_softc *sc = ucom->sc_parent;
457	uint8_t x = ucom->sc_portno;
458	struct usb2_device_request req;
459	usb2_error_t err;
460
461	if (onoff) {
462
463		req.bmRequestType = UT_READ_VENDOR_INTERFACE;
464		req.bRequest = VENDOR_SET_BREAK;
465		req.wValue[0] = x;
466		req.wValue[1] = 0;
467		req.wIndex[0] = sc->sc_iface_no;
468		req.wIndex[1] = 0;
469		USETW(req.wLength, 0);
470
471		err = usb2_com_cfg_do_request(sc->sc_udev, ucom,
472		    &req, NULL, 0, 1000);
473		if (err) {
474			DPRINTFN(0, "send break failed, error=%s\n",
475			    usb2_errstr(err));
476		}
477	}
478}
479
480static void
481ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
482{
483	/* fake status bits */
484	*lsr = 0;
485	*msr = SER_DCD;
486}
487
488static void
489ubser_start_read(struct usb2_com_softc *ucom)
490{
491	struct ubser_softc *sc = ucom->sc_parent;
492
493	usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
494}
495
496static void
497ubser_stop_read(struct usb2_com_softc *ucom)
498{
499	struct ubser_softc *sc = ucom->sc_parent;
500
501	usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
502}
503
504static void
505ubser_start_write(struct usb2_com_softc *ucom)
506{
507	struct ubser_softc *sc = ucom->sc_parent;
508
509	usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
510}
511
512static void
513ubser_stop_write(struct usb2_com_softc *ucom)
514{
515	struct ubser_softc *sc = ucom->sc_parent;
516
517	usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
518}
519