uslcom.c revision 196219
1/*	$OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $	*/
2
3#include <sys/cdefs.h>
4__FBSDID("$FreeBSD: head/sys/dev/usb/serial/uslcom.c 196219 2009-08-14 20:03:53Z jhb $");
5
6/*
7 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#include <sys/stdint.h>
23#include <sys/stddef.h>
24#include <sys/param.h>
25#include <sys/queue.h>
26#include <sys/types.h>
27#include <sys/systm.h>
28#include <sys/kernel.h>
29#include <sys/bus.h>
30#include <sys/linker_set.h>
31#include <sys/module.h>
32#include <sys/lock.h>
33#include <sys/mutex.h>
34#include <sys/condvar.h>
35#include <sys/sysctl.h>
36#include <sys/sx.h>
37#include <sys/unistd.h>
38#include <sys/callout.h>
39#include <sys/malloc.h>
40#include <sys/priv.h>
41
42#include <dev/usb/usb.h>
43#include <dev/usb/usbdi.h>
44#include <dev/usb/usbdi_util.h>
45#include "usbdevs.h"
46
47#define	USB_DEBUG_VAR uslcom_debug
48#include <dev/usb/usb_debug.h>
49#include <dev/usb/usb_process.h>
50
51#include <dev/usb/serial/usb_serial.h>
52
53#if USB_DEBUG
54static int uslcom_debug = 0;
55
56SYSCTL_NODE(_hw_usb, OID_AUTO, uslcom, CTLFLAG_RW, 0, "USB uslcom");
57SYSCTL_INT(_hw_usb_uslcom, OID_AUTO, debug, CTLFLAG_RW,
58    &uslcom_debug, 0, "Debug level");
59#endif
60
61#define	USLCOM_BULK_BUF_SIZE		1024
62#define	USLCOM_CONFIG_INDEX	0
63#define	USLCOM_IFACE_INDEX	0
64
65#define	USLCOM_SET_DATA_BITS(x)	((x) << 8)
66
67#define	USLCOM_WRITE		0x41
68#define	USLCOM_READ		0xc1
69
70#define	USLCOM_UART		0x00
71#define	USLCOM_BAUD_RATE	0x01
72#define	USLCOM_DATA		0x03
73#define	USLCOM_BREAK		0x05
74#define	USLCOM_CTRL		0x07
75
76#define	USLCOM_UART_DISABLE	0x00
77#define	USLCOM_UART_ENABLE	0x01
78
79#define	USLCOM_CTRL_DTR_ON	0x0001
80#define	USLCOM_CTRL_DTR_SET	0x0100
81#define	USLCOM_CTRL_RTS_ON	0x0002
82#define	USLCOM_CTRL_RTS_SET	0x0200
83#define	USLCOM_CTRL_CTS		0x0010
84#define	USLCOM_CTRL_DSR		0x0020
85#define	USLCOM_CTRL_DCD		0x0080
86
87#define	USLCOM_BAUD_REF		0x384000
88
89#define	USLCOM_STOP_BITS_1	0x00
90#define	USLCOM_STOP_BITS_2	0x02
91
92#define	USLCOM_PARITY_NONE	0x00
93#define	USLCOM_PARITY_ODD	0x10
94#define	USLCOM_PARITY_EVEN	0x20
95
96#define	USLCOM_PORT_NO		0xFFFF /* XXX think this should be 0 --hps */
97
98#define	USLCOM_BREAK_OFF	0x00
99#define	USLCOM_BREAK_ON		0x01
100
101enum {
102	USLCOM_BULK_DT_WR,
103	USLCOM_BULK_DT_RD,
104	USLCOM_N_TRANSFER,
105};
106
107struct uslcom_softc {
108	struct ucom_super_softc sc_super_ucom;
109	struct ucom_softc sc_ucom;
110
111	struct usb_xfer *sc_xfer[USLCOM_N_TRANSFER];
112	struct usb_device *sc_udev;
113	struct mtx sc_mtx;
114
115	uint8_t		 sc_msr;
116	uint8_t		 sc_lsr;
117};
118
119static device_probe_t uslcom_probe;
120static device_attach_t uslcom_attach;
121static device_detach_t uslcom_detach;
122
123static usb_callback_t uslcom_write_callback;
124static usb_callback_t uslcom_read_callback;
125
126static void uslcom_open(struct ucom_softc *);
127static void uslcom_close(struct ucom_softc *);
128static void uslcom_set_dtr(struct ucom_softc *, uint8_t);
129static void uslcom_set_rts(struct ucom_softc *, uint8_t);
130static void uslcom_set_break(struct ucom_softc *, uint8_t);
131static int uslcom_pre_param(struct ucom_softc *, struct termios *);
132static void uslcom_param(struct ucom_softc *, struct termios *);
133static void uslcom_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
134static void uslcom_start_read(struct ucom_softc *);
135static void uslcom_stop_read(struct ucom_softc *);
136static void uslcom_start_write(struct ucom_softc *);
137static void uslcom_stop_write(struct ucom_softc *);
138
139static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = {
140
141	[USLCOM_BULK_DT_WR] = {
142		.type = UE_BULK,
143		.endpoint = UE_ADDR_ANY,
144		.direction = UE_DIR_OUT,
145		.bufsize = USLCOM_BULK_BUF_SIZE,
146		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
147		.callback = &uslcom_write_callback,
148	},
149
150	[USLCOM_BULK_DT_RD] = {
151		.type = UE_BULK,
152		.endpoint = UE_ADDR_ANY,
153		.direction = UE_DIR_IN,
154		.bufsize = USLCOM_BULK_BUF_SIZE,
155		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
156		.callback = &uslcom_read_callback,
157	},
158};
159
160static struct ucom_callback uslcom_callback = {
161	.ucom_cfg_open = &uslcom_open,
162	.ucom_cfg_close = &uslcom_close,
163	.ucom_cfg_get_status = &uslcom_get_status,
164	.ucom_cfg_set_dtr = &uslcom_set_dtr,
165	.ucom_cfg_set_rts = &uslcom_set_rts,
166	.ucom_cfg_set_break = &uslcom_set_break,
167	.ucom_cfg_param = &uslcom_param,
168	.ucom_pre_param = &uslcom_pre_param,
169	.ucom_start_read = &uslcom_start_read,
170	.ucom_stop_read = &uslcom_stop_read,
171	.ucom_start_write = &uslcom_start_write,
172	.ucom_stop_write = &uslcom_stop_write,
173};
174
175static const struct usb_device_id uslcom_devs[] = {
176    { USB_VPI(USB_VENDOR_BALTECH,	USB_PRODUCT_BALTECH_CARDREADER, 0) },
177    { USB_VPI(USB_VENDOR_DYNASTREAM,	USB_PRODUCT_DYNASTREAM_ANTDEVBOARD, 0) },
178    { USB_VPI(USB_VENDOR_JABLOTRON,	USB_PRODUCT_JABLOTRON_PC60B, 0) },
179    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_ARGUSISP, 0) },
180    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_CRUMB128, 0) },
181    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_DEGREE, 0) },
182    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_BURNSIDE, 0) },
183    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_HELICOM, 0) },
184    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_LIPOWSKY_HARP, 0) },
185    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_LIPOWSKY_JTAG, 0) },
186    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_LIPOWSKY_LIN, 0) },
187    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_POLOLU, 0) },
188    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_CP2102, 0) },
189    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_CP210X_2, 0) },
190    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_SUUNTO, 0) },
191    { USB_VPI(USB_VENDOR_SILABS,	USB_PRODUCT_SILABS_TRAQMATE, 0) },
192    { USB_VPI(USB_VENDOR_SILABS2,	USB_PRODUCT_SILABS2_DCU11CLONE, 0) },
193    { USB_VPI(USB_VENDOR_USI,		USB_PRODUCT_USI_MC60, 0) },
194};
195
196static device_method_t uslcom_methods[] = {
197	DEVMETHOD(device_probe, uslcom_probe),
198	DEVMETHOD(device_attach, uslcom_attach),
199	DEVMETHOD(device_detach, uslcom_detach),
200	{0, 0}
201};
202
203static devclass_t uslcom_devclass;
204
205static driver_t uslcom_driver = {
206	.name = "uslcom",
207	.methods = uslcom_methods,
208	.size = sizeof(struct uslcom_softc),
209};
210
211DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
212MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
213MODULE_DEPEND(uslcom, usb, 1, 1, 1);
214MODULE_VERSION(uslcom, 1);
215
216static int
217uslcom_probe(device_t dev)
218{
219	struct usb_attach_arg *uaa = device_get_ivars(dev);
220
221	DPRINTFN(11, "\n");
222
223	if (uaa->usb_mode != USB_MODE_HOST) {
224		return (ENXIO);
225	}
226	if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
227		return (ENXIO);
228	}
229	if (uaa->info.bIfaceIndex != USLCOM_IFACE_INDEX) {
230		return (ENXIO);
231	}
232	return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
233}
234
235static int
236uslcom_attach(device_t dev)
237{
238	struct usb_attach_arg *uaa = device_get_ivars(dev);
239	struct uslcom_softc *sc = device_get_softc(dev);
240	int error;
241
242	DPRINTFN(11, "\n");
243
244	device_set_usb_desc(dev);
245	mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
246
247	sc->sc_udev = uaa->device;
248
249	error = usbd_transfer_setup(uaa->device,
250	    &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
251	    USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
252	if (error) {
253		DPRINTF("one or more missing USB endpoints, "
254		    "error=%s\n", usbd_errstr(error));
255		goto detach;
256	}
257	/* clear stall at first run */
258	mtx_lock(&sc->sc_mtx);
259	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
260	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
261	mtx_unlock(&sc->sc_mtx);
262
263	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
264	    &uslcom_callback, &sc->sc_mtx);
265	if (error) {
266		goto detach;
267	}
268	return (0);
269
270detach:
271	uslcom_detach(dev);
272	return (ENXIO);
273}
274
275static int
276uslcom_detach(device_t dev)
277{
278	struct uslcom_softc *sc = device_get_softc(dev);
279
280	DPRINTF("sc=%p\n", sc);
281
282	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
283	usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
284	mtx_destroy(&sc->sc_mtx);
285
286	return (0);
287}
288
289static void
290uslcom_open(struct ucom_softc *ucom)
291{
292	struct uslcom_softc *sc = ucom->sc_parent;
293	struct usb_device_request req;
294
295	req.bmRequestType = USLCOM_WRITE;
296	req.bRequest = USLCOM_UART;
297	USETW(req.wValue, USLCOM_UART_ENABLE);
298	USETW(req.wIndex, USLCOM_PORT_NO);
299	USETW(req.wLength, 0);
300
301        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
302	    &req, NULL, 0, 1000)) {
303		DPRINTF("UART enable failed (ignored)\n");
304	}
305}
306
307static void
308uslcom_close(struct ucom_softc *ucom)
309{
310	struct uslcom_softc *sc = ucom->sc_parent;
311	struct usb_device_request req;
312
313	req.bmRequestType = USLCOM_WRITE;
314	req.bRequest = USLCOM_UART;
315	USETW(req.wValue, USLCOM_UART_DISABLE);
316	USETW(req.wIndex, USLCOM_PORT_NO);
317	USETW(req.wLength, 0);
318
319        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
320	    &req, NULL, 0, 1000)) {
321		DPRINTF("UART disable failed (ignored)\n");
322	}
323}
324
325static void
326uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
327{
328        struct uslcom_softc *sc = ucom->sc_parent;
329	struct usb_device_request req;
330	uint16_t ctl;
331
332        DPRINTF("onoff = %d\n", onoff);
333
334	ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
335	ctl |= USLCOM_CTRL_DTR_SET;
336
337	req.bmRequestType = USLCOM_WRITE;
338	req.bRequest = USLCOM_CTRL;
339	USETW(req.wValue, ctl);
340	USETW(req.wIndex, USLCOM_PORT_NO);
341	USETW(req.wLength, 0);
342
343        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
344	    &req, NULL, 0, 1000)) {
345		DPRINTF("Setting DTR failed (ignored)\n");
346	}
347}
348
349static void
350uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
351{
352        struct uslcom_softc *sc = ucom->sc_parent;
353	struct usb_device_request req;
354	uint16_t ctl;
355
356        DPRINTF("onoff = %d\n", onoff);
357
358	ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
359	ctl |= USLCOM_CTRL_RTS_SET;
360
361	req.bmRequestType = USLCOM_WRITE;
362	req.bRequest = USLCOM_CTRL;
363	USETW(req.wValue, ctl);
364	USETW(req.wIndex, USLCOM_PORT_NO);
365	USETW(req.wLength, 0);
366
367        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
368	    &req, NULL, 0, 1000)) {
369		DPRINTF("Setting DTR failed (ignored)\n");
370	}
371}
372
373static int
374uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
375{
376	if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
377		return (EINVAL);
378	return (0);
379}
380
381static void
382uslcom_param(struct ucom_softc *ucom, struct termios *t)
383{
384	struct uslcom_softc *sc = ucom->sc_parent;
385	struct usb_device_request req;
386	uint16_t data;
387
388	DPRINTF("\n");
389
390	req.bmRequestType = USLCOM_WRITE;
391	req.bRequest = USLCOM_BAUD_RATE;
392	USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
393	USETW(req.wIndex, USLCOM_PORT_NO);
394	USETW(req.wLength, 0);
395
396        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
397	    &req, NULL, 0, 1000)) {
398		DPRINTF("Set baudrate failed (ignored)\n");
399	}
400
401	if (t->c_cflag & CSTOPB)
402		data = USLCOM_STOP_BITS_2;
403	else
404		data = USLCOM_STOP_BITS_1;
405	if (t->c_cflag & PARENB) {
406		if (t->c_cflag & PARODD)
407			data |= USLCOM_PARITY_ODD;
408		else
409			data |= USLCOM_PARITY_EVEN;
410	} else
411		data |= USLCOM_PARITY_NONE;
412	switch (t->c_cflag & CSIZE) {
413	case CS5:
414		data |= USLCOM_SET_DATA_BITS(5);
415		break;
416	case CS6:
417		data |= USLCOM_SET_DATA_BITS(6);
418		break;
419	case CS7:
420		data |= USLCOM_SET_DATA_BITS(7);
421		break;
422	case CS8:
423		data |= USLCOM_SET_DATA_BITS(8);
424		break;
425	}
426
427	req.bmRequestType = USLCOM_WRITE;
428	req.bRequest = USLCOM_DATA;
429	USETW(req.wValue, data);
430	USETW(req.wIndex, USLCOM_PORT_NO);
431	USETW(req.wLength, 0);
432
433        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
434	    &req, NULL, 0, 1000)) {
435		DPRINTF("Set format failed (ignored)\n");
436	}
437	return;
438}
439
440static void
441uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
442{
443	struct uslcom_softc *sc = ucom->sc_parent;
444
445	DPRINTF("\n");
446
447	*lsr = sc->sc_lsr;
448	*msr = sc->sc_msr;
449}
450
451static void
452uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
453{
454        struct uslcom_softc *sc = ucom->sc_parent;
455	struct usb_device_request req;
456	uint16_t brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
457
458	req.bmRequestType = USLCOM_WRITE;
459	req.bRequest = USLCOM_BREAK;
460	USETW(req.wValue, brk);
461	USETW(req.wIndex, USLCOM_PORT_NO);
462	USETW(req.wLength, 0);
463
464        if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
465	    &req, NULL, 0, 1000)) {
466		DPRINTF("Set BREAK failed (ignored)\n");
467	}
468}
469
470static void
471uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
472{
473	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
474	struct usb_page_cache *pc;
475	uint32_t actlen;
476
477	switch (USB_GET_STATE(xfer)) {
478	case USB_ST_SETUP:
479	case USB_ST_TRANSFERRED:
480tr_setup:
481		pc = usbd_xfer_get_frame(xfer, 0);
482		if (ucom_get_data(&sc->sc_ucom, pc, 0,
483		    USLCOM_BULK_BUF_SIZE, &actlen)) {
484
485			DPRINTF("actlen = %d\n", actlen);
486
487			usbd_xfer_set_frame_len(xfer, 0, actlen);
488			usbd_transfer_submit(xfer);
489		}
490		return;
491
492	default:			/* Error */
493		if (error != USB_ERR_CANCELLED) {
494			/* try to clear stall first */
495			usbd_xfer_set_stall(xfer);
496			goto tr_setup;
497		}
498		return;
499	}
500}
501
502static void
503uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
504{
505	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
506	struct usb_page_cache *pc;
507	int actlen;
508
509	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
510
511	switch (USB_GET_STATE(xfer)) {
512	case USB_ST_TRANSFERRED:
513		pc = usbd_xfer_get_frame(xfer, 0);
514		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
515
516	case USB_ST_SETUP:
517tr_setup:
518		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
519		usbd_transfer_submit(xfer);
520		return;
521
522	default:			/* Error */
523		if (error != USB_ERR_CANCELLED) {
524			/* try to clear stall first */
525			usbd_xfer_set_stall(xfer);
526			goto tr_setup;
527		}
528		return;
529	}
530}
531
532static void
533uslcom_start_read(struct ucom_softc *ucom)
534{
535	struct uslcom_softc *sc = ucom->sc_parent;
536
537	/* start read endpoint */
538	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
539}
540
541static void
542uslcom_stop_read(struct ucom_softc *ucom)
543{
544	struct uslcom_softc *sc = ucom->sc_parent;
545
546	/* stop read endpoint */
547	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
548}
549
550static void
551uslcom_start_write(struct ucom_softc *ucom)
552{
553	struct uslcom_softc *sc = ucom->sc_parent;
554
555	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
556}
557
558static void
559uslcom_stop_write(struct ucom_softc *ucom)
560{
561	struct uslcom_softc *sc = ucom->sc_parent;
562
563	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
564}
565