ubser.c revision 239180
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 239180 2012-08-10 15:29:41Z hselasky $");
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/module.h>
88#include <sys/lock.h>
89#include <sys/mutex.h>
90#include <sys/condvar.h>
91#include <sys/sysctl.h>
92#include <sys/sx.h>
93#include <sys/unistd.h>
94#include <sys/callout.h>
95#include <sys/malloc.h>
96#include <sys/priv.h>
97
98#include <dev/usb/usb.h>
99#include <dev/usb/usbdi.h>
100#include <dev/usb/usbdi_util.h>
101#include "usbdevs.h"
102
103#define	USB_DEBUG_VAR ubser_debug
104#include <dev/usb/usb_debug.h>
105#include <dev/usb/usb_process.h>
106
107#include <dev/usb/serial/usb_serial.h>
108
109#define	UBSER_UNIT_MAX	32
110
111/* Vendor Interface Requests */
112#define	VENDOR_GET_NUMSER		0x01
113#define	VENDOR_SET_BREAK		0x02
114#define	VENDOR_CLEAR_BREAK		0x03
115
116#ifdef USB_DEBUG
117static int ubser_debug = 0;
118
119static SYSCTL_NODE(_hw_usb, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
120SYSCTL_INT(_hw_usb_ubser, OID_AUTO, debug, CTLFLAG_RW,
121    &ubser_debug, 0, "ubser debug level");
122#endif
123
124enum {
125	UBSER_BULK_DT_WR,
126	UBSER_BULK_DT_RD,
127	UBSER_N_TRANSFER,
128};
129
130struct ubser_softc {
131	struct ucom_super_softc sc_super_ucom;
132	struct ucom_softc sc_ucom[UBSER_UNIT_MAX];
133
134	struct usb_xfer *sc_xfer[UBSER_N_TRANSFER];
135	struct usb_device *sc_udev;
136	struct mtx sc_mtx;
137
138	uint16_t sc_tx_size;
139
140	uint8_t	sc_numser;
141	uint8_t	sc_iface_no;
142	uint8_t	sc_iface_index;
143	uint8_t	sc_curr_tx_unit;
144	uint8_t	sc_name[16];
145};
146
147/* prototypes */
148
149static device_probe_t ubser_probe;
150static device_attach_t ubser_attach;
151static device_detach_t ubser_detach;
152static device_free_softc_t ubser_free_softc;
153
154static usb_callback_t ubser_write_callback;
155static usb_callback_t ubser_read_callback;
156
157static void	ubser_free(struct ucom_softc *);
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	.ucom_free = &ubser_free,
199};
200
201static device_method_t ubser_methods[] = {
202	DEVMETHOD(device_probe, ubser_probe),
203	DEVMETHOD(device_attach, ubser_attach),
204	DEVMETHOD(device_detach, ubser_detach),
205	DEVMETHOD(device_free_softc, ubser_free_softc),
206	DEVMETHOD_END
207};
208
209static devclass_t ubser_devclass;
210
211static driver_t ubser_driver = {
212	.name = "ubser",
213	.methods = ubser_methods,
214	.size = sizeof(struct ubser_softc),
215};
216
217DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, NULL, 0);
218MODULE_DEPEND(ubser, ucom, 1, 1, 1);
219MODULE_DEPEND(ubser, usb, 1, 1, 1);
220MODULE_VERSION(ubser, 1);
221
222static int
223ubser_probe(device_t dev)
224{
225	struct usb_attach_arg *uaa = device_get_ivars(dev);
226
227	if (uaa->usb_mode != USB_MODE_HOST) {
228		return (ENXIO);
229	}
230	/* check if this is a BWCT vendor specific ubser interface */
231	if ((strcmp(usb_get_manufacturer(uaa->device), "BWCT") == 0) &&
232	    (uaa->info.bInterfaceClass == 0xff) &&
233	    (uaa->info.bInterfaceSubClass == 0x00))
234		return (0);
235
236	return (ENXIO);
237}
238
239static int
240ubser_attach(device_t dev)
241{
242	struct usb_attach_arg *uaa = device_get_ivars(dev);
243	struct ubser_softc *sc = device_get_softc(dev);
244	struct usb_device_request req;
245	uint8_t n;
246	int error;
247
248	device_set_usb_desc(dev);
249	mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF);
250	ucom_ref(&sc->sc_super_ucom);
251
252	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
253	    device_get_nameunit(dev));
254
255	sc->sc_iface_no = uaa->info.bIfaceNum;
256	sc->sc_iface_index = uaa->info.bIfaceIndex;
257	sc->sc_udev = uaa->device;
258
259	/* get number of serials */
260	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
261	req.bRequest = VENDOR_GET_NUMSER;
262	USETW(req.wValue, 0);
263	req.wIndex[0] = sc->sc_iface_no;
264	req.wIndex[1] = 0;
265	USETW(req.wLength, 1);
266	error = usbd_do_request_flags(uaa->device, NULL,
267	    &req, &sc->sc_numser,
268	    0, NULL, USB_DEFAULT_TIMEOUT);
269
270	if (error || (sc->sc_numser == 0)) {
271		device_printf(dev, "failed to get number "
272		    "of serial ports: %s\n",
273		    usbd_errstr(error));
274		goto detach;
275	}
276	if (sc->sc_numser > UBSER_UNIT_MAX)
277		sc->sc_numser = UBSER_UNIT_MAX;
278
279	device_printf(dev, "found %i serials\n", sc->sc_numser);
280
281	error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
282	    sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx);
283	if (error) {
284		goto detach;
285	}
286	sc->sc_tx_size = usbd_xfer_max_len(sc->sc_xfer[UBSER_BULK_DT_WR]);
287
288	if (sc->sc_tx_size == 0) {
289		DPRINTFN(0, "invalid tx_size\n");
290		goto detach;
291	}
292	/* initialize port numbers */
293
294	for (n = 0; n < sc->sc_numser; n++) {
295		sc->sc_ucom[n].sc_portno = n;
296	}
297
298	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
299	    sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx);
300	if (error) {
301		goto detach;
302	}
303	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
304
305	mtx_lock(&sc->sc_mtx);
306	usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]);
307	usbd_xfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]);
308	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
309	mtx_unlock(&sc->sc_mtx);
310
311	return (0);			/* success */
312
313detach:
314	ubser_detach(dev);
315	return (ENXIO);			/* failure */
316}
317
318static int
319ubser_detach(device_t dev)
320{
321	struct ubser_softc *sc = device_get_softc(dev);
322
323	DPRINTF("\n");
324
325	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
326	usbd_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
327
328	return (0);
329}
330
331UCOM_UNLOAD_DRAIN(ubser);
332
333static void
334ubser_free_softc(device_t dev, void *arg)
335{
336	struct ubser_softc *sc = arg;
337
338	if (ucom_unref(&sc->sc_super_ucom)) {
339		if (mtx_initialized(&sc->sc_mtx))
340			mtx_destroy(&sc->sc_mtx);
341		device_free_softc(dev, sc);
342	}
343}
344
345static void
346ubser_free(struct ucom_softc *ucom)
347{
348	ubser_free_softc(NULL, ucom->sc_parent);
349}
350
351static int
352ubser_pre_param(struct ucom_softc *ucom, struct termios *t)
353{
354	DPRINTF("\n");
355
356	/*
357	 * The firmware on our devices can only do 8n1@9600bps
358	 * without handshake.
359	 * We refuse to accept other configurations.
360	 */
361
362	/* ensure 9600bps */
363	switch (t->c_ospeed) {
364	case 9600:
365		break;
366	default:
367		return (EINVAL);
368	}
369
370	/* 2 stop bits not possible */
371	if (t->c_cflag & CSTOPB)
372		return (EINVAL);
373
374	/* XXX parity handling not possible with current firmware */
375	if (t->c_cflag & PARENB)
376		return (EINVAL);
377
378	/* we can only do 8 data bits */
379	switch (t->c_cflag & CSIZE) {
380	case CS8:
381		break;
382	default:
383		return (EINVAL);
384	}
385
386	/* we can't do any kind of hardware handshaking */
387	if ((t->c_cflag &
388	    (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
389		return (EINVAL);
390
391	/*
392	 * XXX xon/xoff not supported by the firmware!
393	 * This is handled within FreeBSD only and may overflow buffers
394	 * because of delayed reaction due to device buffering.
395	 */
396
397	return (0);
398}
399
400static __inline void
401ubser_inc_tx_unit(struct ubser_softc *sc)
402{
403	sc->sc_curr_tx_unit++;
404	if (sc->sc_curr_tx_unit >= sc->sc_numser) {
405		sc->sc_curr_tx_unit = 0;
406	}
407}
408
409static void
410ubser_write_callback(struct usb_xfer *xfer, usb_error_t error)
411{
412	struct ubser_softc *sc = usbd_xfer_softc(xfer);
413	struct usb_page_cache *pc;
414	uint8_t buf[1];
415	uint8_t first_unit = sc->sc_curr_tx_unit;
416	uint32_t actlen;
417
418	switch (USB_GET_STATE(xfer)) {
419	case USB_ST_SETUP:
420	case USB_ST_TRANSFERRED:
421tr_setup:
422		pc = usbd_xfer_get_frame(xfer, 0);
423		do {
424			if (ucom_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
425			    pc, 1, sc->sc_tx_size - 1,
426			    &actlen)) {
427
428				buf[0] = sc->sc_curr_tx_unit;
429
430				usbd_copy_in(pc, 0, buf, 1);
431
432				usbd_xfer_set_frame_len(xfer, 0, actlen + 1);
433				usbd_transfer_submit(xfer);
434
435				ubser_inc_tx_unit(sc);	/* round robin */
436
437				break;
438			}
439			ubser_inc_tx_unit(sc);
440
441		} while (sc->sc_curr_tx_unit != first_unit);
442
443		return;
444
445	default:			/* Error */
446		if (error != USB_ERR_CANCELLED) {
447			/* try to clear stall first */
448			usbd_xfer_set_stall(xfer);
449			goto tr_setup;
450		}
451		return;
452
453	}
454}
455
456static void
457ubser_read_callback(struct usb_xfer *xfer, usb_error_t error)
458{
459	struct ubser_softc *sc = usbd_xfer_softc(xfer);
460	struct usb_page_cache *pc;
461	uint8_t buf[1];
462	int actlen;
463
464	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
465
466	switch (USB_GET_STATE(xfer)) {
467	case USB_ST_TRANSFERRED:
468		if (actlen < 1) {
469			DPRINTF("invalid actlen=0!\n");
470			goto tr_setup;
471		}
472		pc = usbd_xfer_get_frame(xfer, 0);
473		usbd_copy_out(pc, 0, buf, 1);
474
475		if (buf[0] >= sc->sc_numser) {
476			DPRINTF("invalid serial number!\n");
477			goto tr_setup;
478		}
479		ucom_put_data(sc->sc_ucom + buf[0], pc, 1, actlen - 1);
480
481	case USB_ST_SETUP:
482tr_setup:
483		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
484		usbd_transfer_submit(xfer);
485		return;
486
487	default:			/* Error */
488		if (error != USB_ERR_CANCELLED) {
489			/* try to clear stall first */
490			usbd_xfer_set_stall(xfer);
491			goto tr_setup;
492		}
493		return;
494
495	}
496}
497
498static void
499ubser_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
500{
501	struct ubser_softc *sc = ucom->sc_parent;
502	uint8_t x = ucom->sc_portno;
503	struct usb_device_request req;
504	usb_error_t err;
505
506	if (onoff) {
507
508		req.bmRequestType = UT_READ_VENDOR_INTERFACE;
509		req.bRequest = VENDOR_SET_BREAK;
510		req.wValue[0] = x;
511		req.wValue[1] = 0;
512		req.wIndex[0] = sc->sc_iface_no;
513		req.wIndex[1] = 0;
514		USETW(req.wLength, 0);
515
516		err = ucom_cfg_do_request(sc->sc_udev, ucom,
517		    &req, NULL, 0, 1000);
518		if (err) {
519			DPRINTFN(0, "send break failed, error=%s\n",
520			    usbd_errstr(err));
521		}
522	}
523}
524
525static void
526ubser_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
527{
528	/* fake status bits */
529	*lsr = 0;
530	*msr = SER_DCD;
531}
532
533static void
534ubser_start_read(struct ucom_softc *ucom)
535{
536	struct ubser_softc *sc = ucom->sc_parent;
537
538	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
539}
540
541static void
542ubser_stop_read(struct ucom_softc *ucom)
543{
544	struct ubser_softc *sc = ucom->sc_parent;
545
546	usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
547}
548
549static void
550ubser_start_write(struct ucom_softc *ucom)
551{
552	struct ubser_softc *sc = ucom->sc_parent;
553
554	usbd_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
555}
556
557static void
558ubser_stop_write(struct ucom_softc *ucom)
559{
560	struct ubser_softc *sc = ucom->sc_parent;
561
562	usbd_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
563}
564
565static void
566ubser_poll(struct ucom_softc *ucom)
567{
568	struct ubser_softc *sc = ucom->sc_parent;
569	usbd_transfer_poll(sc->sc_xfer, UBSER_N_TRANSFER);
570}
571