ubser.c revision 212122
1258945Sroberto/*-
2258945Sroberto * Copyright (c) 2004 Bernd Walter <ticso@FreeBSD.org>
3258945Sroberto *
4258945Sroberto * $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $
5258945Sroberto * $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $
6258945Sroberto * $Author: ticso $
7258945Sroberto * $Rev: 1127 $
8258945Sroberto */
9258945Sroberto
10258945Sroberto/*-
11258945Sroberto * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
12258945Sroberto * All rights reserved.
13258945Sroberto *
14258945Sroberto * Redistribution and use in source and binary forms, with or without
15258945Sroberto * modification, are permitted provided that the following conditions
16258945Sroberto * are met:
17258945Sroberto * 1. Redistributions of source code must retain the above copyright
18258945Sroberto *    notice, this list of conditions and the following disclaimer.
19258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright
20258945Sroberto *    notice, this list of conditions and the following disclaimer in the
21258945Sroberto *    documentation and/or other materials provided with the distribution.
22258945Sroberto *
23258945Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24258945Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25258945Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26258945Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27258945Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28258945Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29258945Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30258945Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31258945Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32258945Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33258945Sroberto * SUCH DAMAGE.
34258945Sroberto */
35258945Sroberto
36258945Sroberto/*-
37258945Sroberto * Copyright (c) 2000 The NetBSD Foundation, Inc.
38258945Sroberto * All rights reserved.
39258945Sroberto *
40258945Sroberto * This code is derived from software contributed to The NetBSD Foundation
41258945Sroberto * by Lennart Augustsson (lennart@augustsson.net).
42258945Sroberto *
43258945Sroberto * Redistribution and use in source and binary forms, with or without
44258945Sroberto * modification, are permitted provided that the following conditions
45258945Sroberto * are met:
46258945Sroberto * 1. Redistributions of source code must retain the above copyright
47258945Sroberto *    notice, this list of conditions and the following disclaimer.
48258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright
49258945Sroberto *    notice, this list of conditions and the following disclaimer in the
50258945Sroberto *    documentation and/or other materials provided with the distribution.
51258945Sroberto * 3. All advertising materials mentioning features or use of this software
52258945Sroberto *    must display the following acknowledgement:
53258945Sroberto *        This product includes software developed by the NetBSD
54258945Sroberto *        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 212122 2010-09-01 23:47:53Z thompsa $");
74
75/*
76 * BWCT serial adapter driver
77 */
78
79#include <sys/stdint.h>
80#include <sys/stddef.h>
81#include <sys/param.h>
82#include <sys/queue.h>
83#include <sys/types.h>
84#include <sys/systm.h>
85#include <sys/kernel.h>
86#include <sys/bus.h>
87#include <sys/linker_set.h>
88#include <sys/module.h>
89#include <sys/lock.h>
90#include <sys/mutex.h>
91#include <sys/condvar.h>
92#include <sys/sysctl.h>
93#include <sys/sx.h>
94#include <sys/unistd.h>
95#include <sys/callout.h>
96#include <sys/malloc.h>
97#include <sys/priv.h>
98
99#include <dev/usb/usb.h>
100#include <dev/usb/usbdi.h>
101#include <dev/usb/usbdi_util.h>
102#include "usbdevs.h"
103
104#define	USB_DEBUG_VAR ubser_debug
105#include <dev/usb/usb_debug.h>
106#include <dev/usb/usb_process.h>
107#include <dev/usb/usb_device.h>
108
109#include <dev/usb/serial/usb_serial.h>
110
111#define	UBSER_UNIT_MAX	32
112
113/* Vendor Interface Requests */
114#define	VENDOR_GET_NUMSER		0x01
115#define	VENDOR_SET_BREAK		0x02
116#define	VENDOR_CLEAR_BREAK		0x03
117
118#ifdef USB_DEBUG
119static int ubser_debug = 0;
120
121SYSCTL_NODE(_hw_usb, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
122SYSCTL_INT(_hw_usb_ubser, OID_AUTO, debug, CTLFLAG_RW,
123    &ubser_debug, 0, "ubser debug level");
124#endif
125
126enum {
127	UBSER_BULK_DT_WR,
128	UBSER_BULK_DT_RD,
129	UBSER_N_TRANSFER,
130};
131
132struct ubser_softc {
133	struct ucom_super_softc sc_super_ucom;
134	struct ucom_softc sc_ucom[UBSER_UNIT_MAX];
135
136	struct usb_xfer *sc_xfer[UBSER_N_TRANSFER];
137	struct usb_device *sc_udev;
138	struct mtx sc_mtx;
139
140	uint16_t sc_tx_size;
141
142	uint8_t	sc_numser;
143	uint8_t	sc_iface_no;
144	uint8_t	sc_iface_index;
145	uint8_t	sc_curr_tx_unit;
146	uint8_t	sc_name[16];
147};
148
149/* prototypes */
150
151static device_probe_t ubser_probe;
152static device_attach_t ubser_attach;
153static device_detach_t ubser_detach;
154
155static usb_callback_t ubser_write_callback;
156static usb_callback_t ubser_read_callback;
157
158static int	ubser_pre_param(struct ucom_softc *, struct termios *);
159static void	ubser_cfg_set_break(struct ucom_softc *, uint8_t);
160static void	ubser_cfg_get_status(struct ucom_softc *, uint8_t *,
161		    uint8_t *);
162static void	ubser_start_read(struct ucom_softc *);
163static void	ubser_stop_read(struct ucom_softc *);
164static void	ubser_start_write(struct ucom_softc *);
165static void	ubser_stop_write(struct ucom_softc *);
166static void	ubser_poll(struct ucom_softc *ucom);
167
168static const struct usb_config ubser_config[UBSER_N_TRANSFER] = {
169
170	[UBSER_BULK_DT_WR] = {
171		.type = UE_BULK,
172		.endpoint = UE_ADDR_ANY,
173		.direction = UE_DIR_OUT,
174		.bufsize = 0,	/* use wMaxPacketSize */
175		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
176		.callback = &ubser_write_callback,
177	},
178
179	[UBSER_BULK_DT_RD] = {
180		.type = UE_BULK,
181		.endpoint = UE_ADDR_ANY,
182		.direction = UE_DIR_IN,
183		.bufsize = 0,	/* use wMaxPacketSize */
184		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
185		.callback = &ubser_read_callback,
186	},
187};
188
189static const struct ucom_callback ubser_callback = {
190	.ucom_cfg_set_break = &ubser_cfg_set_break,
191	.ucom_cfg_get_status = &ubser_cfg_get_status,
192	.ucom_pre_param = &ubser_pre_param,
193	.ucom_start_read = &ubser_start_read,
194	.ucom_stop_read = &ubser_stop_read,
195	.ucom_start_write = &ubser_start_write,
196	.ucom_stop_write = &ubser_stop_write,
197	.ucom_poll = &ubser_poll,
198};
199
200static device_method_t ubser_methods[] = {
201	DEVMETHOD(device_probe, ubser_probe),
202	DEVMETHOD(device_attach, ubser_attach),
203	DEVMETHOD(device_detach, ubser_detach),
204	{0, 0}
205};
206
207static devclass_t ubser_devclass;
208
209static driver_t ubser_driver = {
210	.name = "ubser",
211	.methods = ubser_methods,
212	.size = sizeof(struct ubser_softc),
213};
214
215DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, NULL, 0);
216MODULE_DEPEND(ubser, ucom, 1, 1, 1);
217MODULE_DEPEND(ubser, usb, 1, 1, 1);
218MODULE_VERSION(ubser, 1);
219
220static int
221ubser_probe(device_t dev)
222{
223	struct usb_attach_arg *uaa = device_get_ivars(dev);
224
225	if (uaa->usb_mode != USB_MODE_HOST) {
226		return (ENXIO);
227	}
228	/* check if this is a BWCT vendor specific ubser interface */
229	if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) &&
230	    (uaa->info.bInterfaceClass == 0xff) &&
231	    (uaa->info.bInterfaceSubClass == 0x00))
232		return (0);
233
234	return (ENXIO);
235}
236
237static int
238ubser_attach(device_t dev)
239{
240	struct usb_attach_arg *uaa = device_get_ivars(dev);
241	struct ubser_softc *sc = device_get_softc(dev);
242	struct usb_device_request req;
243	uint8_t n;
244	int error;
245
246	device_set_usb_desc(dev);
247	mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF);
248
249	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
250	    device_get_nameunit(dev));
251
252	sc->sc_iface_no = uaa->info.bIfaceNum;
253	sc->sc_iface_index = uaa->info.bIfaceIndex;
254	sc->sc_udev = uaa->device;
255
256	/* get number of serials */
257	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
258	req.bRequest = VENDOR_GET_NUMSER;
259	USETW(req.wValue, 0);
260	req.wIndex[0] = sc->sc_iface_no;
261	req.wIndex[1] = 0;
262	USETW(req.wLength, 1);
263	error = usbd_do_request_flags(uaa->device, NULL,
264	    &req, &sc->sc_numser,
265	    0, NULL, USB_DEFAULT_TIMEOUT);
266
267	if (error || (sc->sc_numser == 0)) {
268		device_printf(dev, "failed to get number "
269		    "of serial ports: %s\n",
270		    usbd_errstr(error));
271		goto detach;
272	}
273	if (sc->sc_numser > UBSER_UNIT_MAX)
274		sc->sc_numser = UBSER_UNIT_MAX;
275
276	device_printf(dev, "found %i serials\n", sc->sc_numser);
277
278	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
279	    sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx);
280	if (error) {
281		goto detach;
282	}
283	sc->sc_tx_size = usbd_xfer_max_len(sc->sc_xfer[UBSER_BULK_DT_WR]);
284
285	if (sc->sc_tx_size == 0) {
286		DPRINTFN(0, "invalid tx_size\n");
287		goto detach;
288	}
289	/* initialize port numbers */
290
291	for (n = 0; n < sc->sc_numser; n++) {
292		sc->sc_ucom[n].sc_portno = n;
293	}
294
295	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
296	    sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx);
297	if (error) {
298		goto detach;
299	}
300
301	mtx_lock(&sc->sc_mtx);
302	usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]);
303	usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]);
304	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
305	mtx_unlock(&sc->sc_mtx);
306
307	return (0);			/* success */
308
309detach:
310	ubser_detach(dev);
311	return (ENXIO);			/* failure */
312}
313
314static int
315ubser_detach(device_t dev)
316{
317	struct ubser_softc *sc = device_get_softc(dev);
318
319	DPRINTF("\n");
320
321	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser);
322	usbd_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
323	mtx_destroy(&sc->sc_mtx);
324
325	return (0);
326}
327
328static int
329ubser_pre_param(struct ucom_softc *ucom, struct termios *t)
330{
331	DPRINTF("\n");
332
333	/*
334	 * The firmware on our devices can only do 8n1@9600bps
335	 * without handshake.
336	 * We refuse to accept other configurations.
337	 */
338
339	/* ensure 9600bps */
340	switch (t->c_ospeed) {
341	case 9600:
342		break;
343	default:
344		return (EINVAL);
345	}
346
347	/* 2 stop bits not possible */
348	if (t->c_cflag & CSTOPB)
349		return (EINVAL);
350
351	/* XXX parity handling not possible with current firmware */
352	if (t->c_cflag & PARENB)
353		return (EINVAL);
354
355	/* we can only do 8 data bits */
356	switch (t->c_cflag & CSIZE) {
357	case CS8:
358		break;
359	default:
360		return (EINVAL);
361	}
362
363	/* we can't do any kind of hardware handshaking */
364	if ((t->c_cflag &
365	    (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
366		return (EINVAL);
367
368	/*
369	 * XXX xon/xoff not supported by the firmware!
370	 * This is handled within FreeBSD only and may overflow buffers
371	 * because of delayed reaction due to device buffering.
372	 */
373
374	return (0);
375}
376
377static __inline void
378ubser_inc_tx_unit(struct ubser_softc *sc)
379{
380	sc->sc_curr_tx_unit++;
381	if (sc->sc_curr_tx_unit >= sc->sc_numser) {
382		sc->sc_curr_tx_unit = 0;
383	}
384}
385
386static void
387ubser_write_callback(struct usb_xfer *xfer, usb_error_t error)
388{
389	struct ubser_softc *sc = usbd_xfer_softc(xfer);
390	struct usb_page_cache *pc;
391	uint8_t buf[1];
392	uint8_t first_unit = sc->sc_curr_tx_unit;
393	uint32_t actlen;
394
395	switch (USB_GET_STATE(xfer)) {
396	case USB_ST_SETUP:
397	case USB_ST_TRANSFERRED:
398tr_setup:
399		pc = usbd_xfer_get_frame(xfer, 0);
400		do {
401			if (ucom_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
402			    pc, 1, sc->sc_tx_size - 1,
403			    &actlen)) {
404
405				buf[0] = sc->sc_curr_tx_unit;
406
407				usbd_copy_in(pc, 0, buf, 1);
408
409				usbd_xfer_set_frame_len(xfer, 0, actlen + 1);
410				usbd_transfer_submit(xfer);
411
412				ubser_inc_tx_unit(sc);	/* round robin */
413
414				break;
415			}
416			ubser_inc_tx_unit(sc);
417
418		} while (sc->sc_curr_tx_unit != first_unit);
419
420		return;
421
422	default:			/* Error */
423		if (error != USB_ERR_CANCELLED) {
424			/* try to clear stall first */
425			usbd_xfer_set_stall(xfer);
426			goto tr_setup;
427		}
428		return;
429
430	}
431}
432
433static void
434ubser_read_callback(struct usb_xfer *xfer, usb_error_t error)
435{
436	struct ubser_softc *sc = usbd_xfer_softc(xfer);
437	struct usb_page_cache *pc;
438	uint8_t buf[1];
439	int actlen;
440
441	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
442
443	switch (USB_GET_STATE(xfer)) {
444	case USB_ST_TRANSFERRED:
445		if (actlen < 1) {
446			DPRINTF("invalid actlen=0!\n");
447			goto tr_setup;
448		}
449		pc = usbd_xfer_get_frame(xfer, 0);
450		usbd_copy_out(pc, 0, buf, 1);
451
452		if (buf[0] >= sc->sc_numser) {
453			DPRINTF("invalid serial number!\n");
454			goto tr_setup;
455		}
456		ucom_put_data(sc->sc_ucom + buf[0], pc, 1, actlen - 1);
457
458	case USB_ST_SETUP:
459tr_setup:
460		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
461		usbd_transfer_submit(xfer);
462		return;
463
464	default:			/* Error */
465		if (error != USB_ERR_CANCELLED) {
466			/* try to clear stall first */
467			usbd_xfer_set_stall(xfer);
468			goto tr_setup;
469		}
470		return;
471
472	}
473}
474
475static void
476ubser_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
477{
478	struct ubser_softc *sc = ucom->sc_parent;
479	uint8_t x = ucom->sc_portno;
480	struct usb_device_request req;
481	usb_error_t err;
482
483	if (onoff) {
484
485		req.bmRequestType = UT_READ_VENDOR_INTERFACE;
486		req.bRequest = VENDOR_SET_BREAK;
487		req.wValue[0] = x;
488		req.wValue[1] = 0;
489		req.wIndex[0] = sc->sc_iface_no;
490		req.wIndex[1] = 0;
491		USETW(req.wLength, 0);
492
493		err = ucom_cfg_do_request(sc->sc_udev, ucom,
494		    &req, NULL, 0, 1000);
495		if (err) {
496			DPRINTFN(0, "send break failed, error=%s\n",
497			    usbd_errstr(err));
498		}
499	}
500}
501
502static void
503ubser_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
504{
505	/* fake status bits */
506	*lsr = 0;
507	*msr = SER_DCD;
508}
509
510static void
511ubser_start_read(struct ucom_softc *ucom)
512{
513	struct ubser_softc *sc = ucom->sc_parent;
514
515	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
516}
517
518static void
519ubser_stop_read(struct ucom_softc *ucom)
520{
521	struct ubser_softc *sc = ucom->sc_parent;
522
523	usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
524}
525
526static void
527ubser_start_write(struct ucom_softc *ucom)
528{
529	struct ubser_softc *sc = ucom->sc_parent;
530
531	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
532}
533
534static void
535ubser_stop_write(struct ucom_softc *ucom)
536{
537	struct ubser_softc *sc = ucom->sc_parent;
538
539	usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
540}
541
542static void
543ubser_poll(struct ucom_softc *ucom)
544{
545	struct ubser_softc *sc = ucom->sc_parent;
546	usbd_transfer_poll(sc->sc_xfer, UBSER_N_TRANSFER);
547}
548