xref: /freebsd/sys/dev/usb/serial/uslcom.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
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(DYNASTREAM, ANTDEVBOARD),
181     USLCOM_DEV(JABLOTRON, PC60B),
182     USLCOM_DEV(SILABS, ARGUSISP),
183     USLCOM_DEV(SILABS, CRUMB128),
184     USLCOM_DEV(SILABS, DEGREE),
185     USLCOM_DEV(SILABS, BURNSIDE),
186     USLCOM_DEV(SILABS, HELICOM),
187     USLCOM_DEV(SILABS, LIPOWSKY_HARP),
188     USLCOM_DEV(SILABS, LIPOWSKY_JTAG),
189     USLCOM_DEV(SILABS, LIPOWSKY_LIN),
190     USLCOM_DEV(SILABS, POLOLU),
191     USLCOM_DEV(SILABS, CP2102),
192     USLCOM_DEV(SILABS, CP210X_2),
193     USLCOM_DEV(SILABS, SUUNTO),
194     USLCOM_DEV(SILABS, TRAQMATE),
195     USLCOM_DEV(SILABS2, DCU11CLONE),
196     USLCOM_DEV(USI, MC60),
197 #undef USLCOM_DEV
198 };
199 
200 static device_method_t uslcom_methods[] = {
201 	DEVMETHOD(device_probe, uslcom_probe),
202 	DEVMETHOD(device_attach, uslcom_attach),
203 	DEVMETHOD(device_detach, uslcom_detach),
204 	{0, 0}
205 };
206 
207 static devclass_t uslcom_devclass;
208 
209 static driver_t uslcom_driver = {
210 	.name = "uslcom",
211 	.methods = uslcom_methods,
212 	.size = sizeof(struct uslcom_softc),
213 };
214 
215 DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
216 MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
217 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
218 MODULE_VERSION(uslcom, 1);
219 
220 static int
221 uslcom_probe(device_t dev)
222 {
223 	struct usb_attach_arg *uaa = device_get_ivars(dev);
224 
225 	DPRINTFN(11, "\n");
226 
227 	if (uaa->usb_mode != USB_MODE_HOST) {
228 		return (ENXIO);
229 	}
230 	if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
231 		return (ENXIO);
232 	}
233 	if (uaa->info.bIfaceIndex != USLCOM_IFACE_INDEX) {
234 		return (ENXIO);
235 	}
236 	return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
237 }
238 
239 static int
240 uslcom_attach(device_t dev)
241 {
242 	struct usb_attach_arg *uaa = device_get_ivars(dev);
243 	struct uslcom_softc *sc = device_get_softc(dev);
244 	int error;
245 
246 	DPRINTFN(11, "\n");
247 
248 	device_set_usb_desc(dev);
249 	mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
250 
251 	sc->sc_udev = uaa->device;
252 
253 	error = usbd_transfer_setup(uaa->device,
254 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
255 	    USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
256 	if (error) {
257 		DPRINTF("one or more missing USB endpoints, "
258 		    "error=%s\n", usbd_errstr(error));
259 		goto detach;
260 	}
261 	/* clear stall at first run */
262 	mtx_lock(&sc->sc_mtx);
263 	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_WR]);
264 	usbd_xfer_set_stall(sc->sc_xfer[USLCOM_BULK_DT_RD]);
265 	mtx_unlock(&sc->sc_mtx);
266 
267 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
268 	    &uslcom_callback, &sc->sc_mtx);
269 	if (error) {
270 		goto detach;
271 	}
272 	return (0);
273 
274 detach:
275 	uslcom_detach(dev);
276 	return (ENXIO);
277 }
278 
279 static int
280 uslcom_detach(device_t dev)
281 {
282 	struct uslcom_softc *sc = device_get_softc(dev);
283 
284 	DPRINTF("sc=%p\n", sc);
285 
286 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
287 	usbd_transfer_unsetup(sc->sc_xfer, USLCOM_N_TRANSFER);
288 	mtx_destroy(&sc->sc_mtx);
289 
290 	return (0);
291 }
292 
293 static void
294 uslcom_open(struct ucom_softc *ucom)
295 {
296 	struct uslcom_softc *sc = ucom->sc_parent;
297 	struct usb_device_request req;
298 
299 	req.bmRequestType = USLCOM_WRITE;
300 	req.bRequest = USLCOM_UART;
301 	USETW(req.wValue, USLCOM_UART_ENABLE);
302 	USETW(req.wIndex, USLCOM_PORT_NO);
303 	USETW(req.wLength, 0);
304 
305         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
306 	    &req, NULL, 0, 1000)) {
307 		DPRINTF("UART enable failed (ignored)\n");
308 	}
309 }
310 
311 static void
312 uslcom_close(struct ucom_softc *ucom)
313 {
314 	struct uslcom_softc *sc = ucom->sc_parent;
315 	struct usb_device_request req;
316 
317 	req.bmRequestType = USLCOM_WRITE;
318 	req.bRequest = USLCOM_UART;
319 	USETW(req.wValue, USLCOM_UART_DISABLE);
320 	USETW(req.wIndex, USLCOM_PORT_NO);
321 	USETW(req.wLength, 0);
322 
323         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
324 	    &req, NULL, 0, 1000)) {
325 		DPRINTF("UART disable failed (ignored)\n");
326 	}
327 }
328 
329 static void
330 uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
331 {
332         struct uslcom_softc *sc = ucom->sc_parent;
333 	struct usb_device_request req;
334 	uint16_t ctl;
335 
336         DPRINTF("onoff = %d\n", onoff);
337 
338 	ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
339 	ctl |= USLCOM_CTRL_DTR_SET;
340 
341 	req.bmRequestType = USLCOM_WRITE;
342 	req.bRequest = USLCOM_CTRL;
343 	USETW(req.wValue, ctl);
344 	USETW(req.wIndex, USLCOM_PORT_NO);
345 	USETW(req.wLength, 0);
346 
347         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
348 	    &req, NULL, 0, 1000)) {
349 		DPRINTF("Setting DTR failed (ignored)\n");
350 	}
351 }
352 
353 static void
354 uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
355 {
356         struct uslcom_softc *sc = ucom->sc_parent;
357 	struct usb_device_request req;
358 	uint16_t ctl;
359 
360         DPRINTF("onoff = %d\n", onoff);
361 
362 	ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
363 	ctl |= USLCOM_CTRL_RTS_SET;
364 
365 	req.bmRequestType = USLCOM_WRITE;
366 	req.bRequest = USLCOM_CTRL;
367 	USETW(req.wValue, ctl);
368 	USETW(req.wIndex, USLCOM_PORT_NO);
369 	USETW(req.wLength, 0);
370 
371         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
372 	    &req, NULL, 0, 1000)) {
373 		DPRINTF("Setting DTR failed (ignored)\n");
374 	}
375 }
376 
377 static int
378 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
379 {
380 	if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
381 		return (EINVAL);
382 	return (0);
383 }
384 
385 static void
386 uslcom_param(struct ucom_softc *ucom, struct termios *t)
387 {
388 	struct uslcom_softc *sc = ucom->sc_parent;
389 	struct usb_device_request req;
390 	uint16_t data;
391 
392 	DPRINTF("\n");
393 
394 	req.bmRequestType = USLCOM_WRITE;
395 	req.bRequest = USLCOM_BAUD_RATE;
396 	USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
397 	USETW(req.wIndex, USLCOM_PORT_NO);
398 	USETW(req.wLength, 0);
399 
400         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
401 	    &req, NULL, 0, 1000)) {
402 		DPRINTF("Set baudrate failed (ignored)\n");
403 	}
404 
405 	if (t->c_cflag & CSTOPB)
406 		data = USLCOM_STOP_BITS_2;
407 	else
408 		data = USLCOM_STOP_BITS_1;
409 	if (t->c_cflag & PARENB) {
410 		if (t->c_cflag & PARODD)
411 			data |= USLCOM_PARITY_ODD;
412 		else
413 			data |= USLCOM_PARITY_EVEN;
414 	} else
415 		data |= USLCOM_PARITY_NONE;
416 	switch (t->c_cflag & CSIZE) {
417 	case CS5:
418 		data |= USLCOM_SET_DATA_BITS(5);
419 		break;
420 	case CS6:
421 		data |= USLCOM_SET_DATA_BITS(6);
422 		break;
423 	case CS7:
424 		data |= USLCOM_SET_DATA_BITS(7);
425 		break;
426 	case CS8:
427 		data |= USLCOM_SET_DATA_BITS(8);
428 		break;
429 	}
430 
431 	req.bmRequestType = USLCOM_WRITE;
432 	req.bRequest = USLCOM_DATA;
433 	USETW(req.wValue, data);
434 	USETW(req.wIndex, USLCOM_PORT_NO);
435 	USETW(req.wLength, 0);
436 
437         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
438 	    &req, NULL, 0, 1000)) {
439 		DPRINTF("Set format failed (ignored)\n");
440 	}
441 	return;
442 }
443 
444 static void
445 uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
446 {
447 	struct uslcom_softc *sc = ucom->sc_parent;
448 
449 	DPRINTF("\n");
450 
451 	*lsr = sc->sc_lsr;
452 	*msr = sc->sc_msr;
453 }
454 
455 static void
456 uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
457 {
458         struct uslcom_softc *sc = ucom->sc_parent;
459 	struct usb_device_request req;
460 	uint16_t brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
461 
462 	req.bmRequestType = USLCOM_WRITE;
463 	req.bRequest = USLCOM_BREAK;
464 	USETW(req.wValue, brk);
465 	USETW(req.wIndex, USLCOM_PORT_NO);
466 	USETW(req.wLength, 0);
467 
468         if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
469 	    &req, NULL, 0, 1000)) {
470 		DPRINTF("Set BREAK failed (ignored)\n");
471 	}
472 }
473 
474 static void
475 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
476 {
477 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
478 	struct usb_page_cache *pc;
479 	uint32_t actlen;
480 
481 	switch (USB_GET_STATE(xfer)) {
482 	case USB_ST_SETUP:
483 	case USB_ST_TRANSFERRED:
484 tr_setup:
485 		pc = usbd_xfer_get_frame(xfer, 0);
486 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
487 		    USLCOM_BULK_BUF_SIZE, &actlen)) {
488 
489 			DPRINTF("actlen = %d\n", actlen);
490 
491 			usbd_xfer_set_frame_len(xfer, 0, actlen);
492 			usbd_transfer_submit(xfer);
493 		}
494 		return;
495 
496 	default:			/* Error */
497 		if (error != USB_ERR_CANCELLED) {
498 			/* try to clear stall first */
499 			usbd_xfer_set_stall(xfer);
500 			goto tr_setup;
501 		}
502 		return;
503 	}
504 }
505 
506 static void
507 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
508 {
509 	struct uslcom_softc *sc = usbd_xfer_softc(xfer);
510 	struct usb_page_cache *pc;
511 	int actlen;
512 
513 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
514 
515 	switch (USB_GET_STATE(xfer)) {
516 	case USB_ST_TRANSFERRED:
517 		pc = usbd_xfer_get_frame(xfer, 0);
518 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
519 
520 	case USB_ST_SETUP:
521 tr_setup:
522 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
523 		usbd_transfer_submit(xfer);
524 		return;
525 
526 	default:			/* Error */
527 		if (error != USB_ERR_CANCELLED) {
528 			/* try to clear stall first */
529 			usbd_xfer_set_stall(xfer);
530 			goto tr_setup;
531 		}
532 		return;
533 	}
534 }
535 
536 static void
537 uslcom_start_read(struct ucom_softc *ucom)
538 {
539 	struct uslcom_softc *sc = ucom->sc_parent;
540 
541 	/* start read endpoint */
542 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
543 }
544 
545 static void
546 uslcom_stop_read(struct ucom_softc *ucom)
547 {
548 	struct uslcom_softc *sc = ucom->sc_parent;
549 
550 	/* stop read endpoint */
551 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
552 }
553 
554 static void
555 uslcom_start_write(struct ucom_softc *ucom)
556 {
557 	struct uslcom_softc *sc = ucom->sc_parent;
558 
559 	usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
560 }
561 
562 static void
563 uslcom_stop_write(struct ucom_softc *ucom)
564 {
565 	struct uslcom_softc *sc = ucom->sc_parent;
566 
567 	usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);
568 }
569 
570 static void
571 uslcom_poll(struct ucom_softc *ucom)
572 {
573 	struct uslcom_softc *sc = ucom->sc_parent;
574 	usbd_transfer_poll(sc->sc_xfer, USLCOM_N_TRANSFER);
575 }
576