xref: /freebsd/sys/dev/usb/serial/uslcom.c (revision 1670a1c2a47d10ecccd001970b859caf93cd3b6e)
1 /*	$OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $	*/
2 
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
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 #ifdef USB_DEBUG
54 static int uslcom_debug = 0;
55 
56 SYSCTL_NODE(_hw_usb, OID_AUTO, uslcom, CTLFLAG_RW, 0, "USB uslcom");
57 SYSCTL_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 
101 enum {
102 	USLCOM_BULK_DT_WR,
103 	USLCOM_BULK_DT_RD,
104 	USLCOM_N_TRANSFER,
105 };
106 
107 struct 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 
119 static device_probe_t uslcom_probe;
120 static device_attach_t uslcom_attach;
121 static device_detach_t uslcom_detach;
122 
123 static usb_callback_t uslcom_write_callback;
124 static usb_callback_t uslcom_read_callback;
125 
126 static void uslcom_open(struct ucom_softc *);
127 static void uslcom_close(struct ucom_softc *);
128 static void uslcom_set_dtr(struct ucom_softc *, uint8_t);
129 static void uslcom_set_rts(struct ucom_softc *, uint8_t);
130 static void uslcom_set_break(struct ucom_softc *, uint8_t);
131 static int uslcom_pre_param(struct ucom_softc *, struct termios *);
132 static void uslcom_param(struct ucom_softc *, struct termios *);
133 static void uslcom_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
134 static void uslcom_start_read(struct ucom_softc *);
135 static void uslcom_stop_read(struct ucom_softc *);
136 static void uslcom_start_write(struct ucom_softc *);
137 static void uslcom_stop_write(struct ucom_softc *);
138 static void uslcom_poll(struct ucom_softc *ucom);
139 
140 static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = {
141 
142 	[USLCOM_BULK_DT_WR] = {
143 		.type = UE_BULK,
144 		.endpoint = UE_ADDR_ANY,
145 		.direction = UE_DIR_OUT,
146 		.bufsize = USLCOM_BULK_BUF_SIZE,
147 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
148 		.callback = &uslcom_write_callback,
149 	},
150 
151 	[USLCOM_BULK_DT_RD] = {
152 		.type = UE_BULK,
153 		.endpoint = UE_ADDR_ANY,
154 		.direction = UE_DIR_IN,
155 		.bufsize = USLCOM_BULK_BUF_SIZE,
156 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
157 		.callback = &uslcom_read_callback,
158 	},
159 };
160 
161 static struct ucom_callback uslcom_callback = {
162 	.ucom_cfg_open = &uslcom_open,
163 	.ucom_cfg_close = &uslcom_close,
164 	.ucom_cfg_get_status = &uslcom_get_status,
165 	.ucom_cfg_set_dtr = &uslcom_set_dtr,
166 	.ucom_cfg_set_rts = &uslcom_set_rts,
167 	.ucom_cfg_set_break = &uslcom_set_break,
168 	.ucom_cfg_param = &uslcom_param,
169 	.ucom_pre_param = &uslcom_pre_param,
170 	.ucom_start_read = &uslcom_start_read,
171 	.ucom_stop_read = &uslcom_stop_read,
172 	.ucom_start_write = &uslcom_start_write,
173 	.ucom_stop_write = &uslcom_stop_write,
174 	.ucom_poll = &uslcom_poll,
175 };
176 
177 static const struct usb_device_id uslcom_devs[] = {
178 #define	USLCOM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
179     USLCOM_DEV(BALTECH, CARDREADER),
180     USLCOM_DEV(CLIPSAL, 5500PCU),
181     USLCOM_DEV(DYNASTREAM, ANTDEVBOARD),
182     USLCOM_DEV(DYNASTREAM, ANT2USB),
183     USLCOM_DEV(GEMALTO, PROXPU),
184     USLCOM_DEV(JABLOTRON, PC60B),
185     USLCOM_DEV(SILABS, AEROCOMM),
186     USLCOM_DEV(SILABS, ARGUSISP),
187     USLCOM_DEV(SILABS, BSM7DUSB),
188     USLCOM_DEV(SILABS, BURNSIDE),
189     USLCOM_DEV(SILABS, CP2102),
190     USLCOM_DEV(SILABS, CP210X_2),
191     USLCOM_DEV(SILABS, CRUMB128),
192     USLCOM_DEV(SILABS, DEGREE),
193     USLCOM_DEV(SILABS, HELICOM),
194     USLCOM_DEV(SILABS, LIPOWSKY_HARP),
195     USLCOM_DEV(SILABS, LIPOWSKY_JTAG),
196     USLCOM_DEV(SILABS, LIPOWSKY_LIN),
197     USLCOM_DEV(SILABS, MC35PU),
198     USLCOM_DEV(SILABS, POLOLU),
199     USLCOM_DEV(SILABS, RIGBLASTER),
200     USLCOM_DEV(SILABS, RIGTALK),
201     USLCOM_DEV(SILABS, SUUNTO),
202     USLCOM_DEV(SILABS, TRACIENT),
203     USLCOM_DEV(SILABS, TRAQMATE),
204     USLCOM_DEV(SILABS, USBCOUNT50),
205     USLCOM_DEV(SILABS, USBPULSE100),
206     USLCOM_DEV(SILABS, USBWAVE12),
207     USLCOM_DEV(SILABS2, DCU11CLONE),
208     USLCOM_DEV(SILABS3, GPRS_MODEM),
209     USLCOM_DEV(USI, MC60),
210 #undef USLCOM_DEV
211 };
212 
213 static device_method_t uslcom_methods[] = {
214 	DEVMETHOD(device_probe, uslcom_probe),
215 	DEVMETHOD(device_attach, uslcom_attach),
216 	DEVMETHOD(device_detach, uslcom_detach),
217 	{0, 0}
218 };
219 
220 static devclass_t uslcom_devclass;
221 
222 static driver_t uslcom_driver = {
223 	.name = "uslcom",
224 	.methods = uslcom_methods,
225 	.size = sizeof(struct uslcom_softc),
226 };
227 
228 DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
229 MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
230 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
231 MODULE_VERSION(uslcom, 1);
232 
233 static int
234 uslcom_probe(device_t dev)
235 {
236 	struct usb_attach_arg *uaa = device_get_ivars(dev);
237 
238 	DPRINTFN(11, "\n");
239 
240 	if (uaa->usb_mode != USB_MODE_HOST) {
241 		return (ENXIO);
242 	}
243 	if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
244 		return (ENXIO);
245 	}
246 	if (uaa->info.bIfaceIndex != USLCOM_IFACE_INDEX) {
247 		return (ENXIO);
248 	}
249 	return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
250 }
251 
252 static int
253 uslcom_attach(device_t dev)
254 {
255 	struct usb_attach_arg *uaa = device_get_ivars(dev);
256 	struct uslcom_softc *sc = device_get_softc(dev);
257 	int error;
258 
259 	DPRINTFN(11, "\n");
260 
261 	device_set_usb_desc(dev);
262 	mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
263 
264 	sc->sc_udev = uaa->device;
265 
266 	error = usbd_transfer_setup(uaa->device,
267 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
268 	    USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
269 	if (error) {
270 		DPRINTF("one or more missing USB endpoints, "
271 		    "error=%s\n", usbd_errstr(error));
272 		goto detach;
273 	}
274 	/* clear stall at first run */
275 	mtx_lock(&sc->sc_mtx);
276 	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
277 	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
278 	mtx_unlock(&sc->sc_mtx);
279 
280 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
281 	    &uslcom_callback, &sc->sc_mtx);
282 	if (error) {
283 		goto detach;
284 	}
285 	return (0);
286 
287 detach:
288 	uslcom_detach(dev);
289 	return (ENXIO);
290 }
291 
292 static int
293 uslcom_detach(device_t dev)
294 {
295 	struct uslcom_softc *sc = device_get_softc(dev);
296 
297 	DPRINTF("sc=%p\n", sc);
298 
299 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
300 	usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
301 	mtx_destroy(&sc->sc_mtx);
302 
303 	return (0);
304 }
305 
306 static void
307 uslcom_open(struct ucom_softc *ucom)
308 {
309 	struct uslcom_softc *sc = ucom->sc_parent;
310 	struct usb_device_request req;
311 
312 	req.bmRequestType = USLCOM_WRITE;
313 	req.bRequest = USLCOM_UART;
314 	USETW(req.wValue, USLCOM_UART_ENABLE);
315 	USETW(req.wIndex, USLCOM_PORT_NO);
316 	USETW(req.wLength, 0);
317 
318         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
319 	    &req, NULL, 0, 1000)) {
320 		DPRINTF("UART enable failed (ignored)\n");
321 	}
322 }
323 
324 static void
325 uslcom_close(struct ucom_softc *ucom)
326 {
327 	struct uslcom_softc *sc = ucom->sc_parent;
328 	struct usb_device_request req;
329 
330 	req.bmRequestType = USLCOM_WRITE;
331 	req.bRequest = USLCOM_UART;
332 	USETW(req.wValue, USLCOM_UART_DISABLE);
333 	USETW(req.wIndex, USLCOM_PORT_NO);
334 	USETW(req.wLength, 0);
335 
336         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
337 	    &req, NULL, 0, 1000)) {
338 		DPRINTF("UART disable failed (ignored)\n");
339 	}
340 }
341 
342 static void
343 uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
344 {
345         struct uslcom_softc *sc = ucom->sc_parent;
346 	struct usb_device_request req;
347 	uint16_t ctl;
348 
349         DPRINTF("onoff = %d\n", onoff);
350 
351 	ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
352 	ctl |= USLCOM_CTRL_DTR_SET;
353 
354 	req.bmRequestType = USLCOM_WRITE;
355 	req.bRequest = USLCOM_CTRL;
356 	USETW(req.wValue, ctl);
357 	USETW(req.wIndex, USLCOM_PORT_NO);
358 	USETW(req.wLength, 0);
359 
360         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
361 	    &req, NULL, 0, 1000)) {
362 		DPRINTF("Setting DTR failed (ignored)\n");
363 	}
364 }
365 
366 static void
367 uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
368 {
369         struct uslcom_softc *sc = ucom->sc_parent;
370 	struct usb_device_request req;
371 	uint16_t ctl;
372 
373         DPRINTF("onoff = %d\n", onoff);
374 
375 	ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
376 	ctl |= USLCOM_CTRL_RTS_SET;
377 
378 	req.bmRequestType = USLCOM_WRITE;
379 	req.bRequest = USLCOM_CTRL;
380 	USETW(req.wValue, ctl);
381 	USETW(req.wIndex, USLCOM_PORT_NO);
382 	USETW(req.wLength, 0);
383 
384         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
385 	    &req, NULL, 0, 1000)) {
386 		DPRINTF("Setting DTR failed (ignored)\n");
387 	}
388 }
389 
390 static int
391 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
392 {
393 	if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
394 		return (EINVAL);
395 	return (0);
396 }
397 
398 static void
399 uslcom_param(struct ucom_softc *ucom, struct termios *t)
400 {
401 	struct uslcom_softc *sc = ucom->sc_parent;
402 	struct usb_device_request req;
403 	uint16_t data;
404 
405 	DPRINTF("\n");
406 
407 	req.bmRequestType = USLCOM_WRITE;
408 	req.bRequest = USLCOM_BAUD_RATE;
409 	USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
410 	USETW(req.wIndex, USLCOM_PORT_NO);
411 	USETW(req.wLength, 0);
412 
413         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
414 	    &req, NULL, 0, 1000)) {
415 		DPRINTF("Set baudrate failed (ignored)\n");
416 	}
417 
418 	if (t->c_cflag & CSTOPB)
419 		data = USLCOM_STOP_BITS_2;
420 	else
421 		data = USLCOM_STOP_BITS_1;
422 	if (t->c_cflag & PARENB) {
423 		if (t->c_cflag & PARODD)
424 			data |= USLCOM_PARITY_ODD;
425 		else
426 			data |= USLCOM_PARITY_EVEN;
427 	} else
428 		data |= USLCOM_PARITY_NONE;
429 	switch (t->c_cflag & CSIZE) {
430 	case CS5:
431 		data |= USLCOM_SET_DATA_BITS(5);
432 		break;
433 	case CS6:
434 		data |= USLCOM_SET_DATA_BITS(6);
435 		break;
436 	case CS7:
437 		data |= USLCOM_SET_DATA_BITS(7);
438 		break;
439 	case CS8:
440 		data |= USLCOM_SET_DATA_BITS(8);
441 		break;
442 	}
443 
444 	req.bmRequestType = USLCOM_WRITE;
445 	req.bRequest = USLCOM_DATA;
446 	USETW(req.wValue, data);
447 	USETW(req.wIndex, USLCOM_PORT_NO);
448 	USETW(req.wLength, 0);
449 
450         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
451 	    &req, NULL, 0, 1000)) {
452 		DPRINTF("Set format failed (ignored)\n");
453 	}
454 	return;
455 }
456 
457 static void
458 uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
459 {
460 	struct uslcom_softc *sc = ucom->sc_parent;
461 
462 	DPRINTF("\n");
463 
464 	*lsr = sc->sc_lsr;
465 	*msr = sc->sc_msr;
466 }
467 
468 static void
469 uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
470 {
471         struct uslcom_softc *sc = ucom->sc_parent;
472 	struct usb_device_request req;
473 	uint16_t brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
474 
475 	req.bmRequestType = USLCOM_WRITE;
476 	req.bRequest = USLCOM_BREAK;
477 	USETW(req.wValue, brk);
478 	USETW(req.wIndex, USLCOM_PORT_NO);
479 	USETW(req.wLength, 0);
480 
481         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
482 	    &req, NULL, 0, 1000)) {
483 		DPRINTF("Set BREAK failed (ignored)\n");
484 	}
485 }
486 
487 static void
488 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
489 {
490 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
491 	struct usb_page_cache *pc;
492 	uint32_t actlen;
493 
494 	switch (USB_GET_STATE(xfer)) {
495 	case USB_ST_SETUP:
496 	case USB_ST_TRANSFERRED:
497 tr_setup:
498 		pc = usbd_xfer_get_frame(xfer, 0);
499 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
500 		    USLCOM_BULK_BUF_SIZE, &actlen)) {
501 
502 			DPRINTF("actlen = %d\n", actlen);
503 
504 			usbd_xfer_set_frame_len(xfer, 0, actlen);
505 			usbd_transfer_submit(xfer);
506 		}
507 		return;
508 
509 	default:			/* Error */
510 		if (error != USB_ERR_CANCELLED) {
511 			/* try to clear stall first */
512 			usbd_xfer_set_stall(xfer);
513 			goto tr_setup;
514 		}
515 		return;
516 	}
517 }
518 
519 static void
520 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
521 {
522 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
523 	struct usb_page_cache *pc;
524 	int actlen;
525 
526 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
527 
528 	switch (USB_GET_STATE(xfer)) {
529 	case USB_ST_TRANSFERRED:
530 		pc = usbd_xfer_get_frame(xfer, 0);
531 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
532 
533 	case USB_ST_SETUP:
534 tr_setup:
535 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
536 		usbd_transfer_submit(xfer);
537 		return;
538 
539 	default:			/* Error */
540 		if (error != USB_ERR_CANCELLED) {
541 			/* try to clear stall first */
542 			usbd_xfer_set_stall(xfer);
543 			goto tr_setup;
544 		}
545 		return;
546 	}
547 }
548 
549 static void
550 uslcom_start_read(struct ucom_softc *ucom)
551 {
552 	struct uslcom_softc *sc = ucom->sc_parent;
553 
554 	/* start read endpoint */
555 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
556 }
557 
558 static void
559 uslcom_stop_read(struct ucom_softc *ucom)
560 {
561 	struct uslcom_softc *sc = ucom->sc_parent;
562 
563 	/* stop read endpoint */
564 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
565 }
566 
567 static void
568 uslcom_start_write(struct ucom_softc *ucom)
569 {
570 	struct uslcom_softc *sc = ucom->sc_parent;
571 
572 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
573 }
574 
575 static void
576 uslcom_stop_write(struct ucom_softc *ucom)
577 {
578 	struct uslcom_softc *sc = ucom->sc_parent;
579 
580 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
581 }
582 
583 static void
584 uslcom_poll(struct ucom_softc *ucom)
585 {
586 	struct uslcom_softc *sc = ucom->sc_parent;
587 	usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
588 }
589