1 /* $OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $ */
7 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
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.
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.
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>
30 #include <sys/linker_set.h>
31 #include <sys/module.h>
33 #include <sys/mutex.h>
34 #include <sys/condvar.h>
35 #include <sys/sysctl.h>
37 #include <sys/unistd.h>
38 #include <sys/callout.h>
39 #include <sys/malloc.h>
42 #include <dev/usb/usb.h>
43 #include <dev/usb/usbdi.h>
44 #include <dev/usb/usbdi_util.h>
47 #define USB_DEBUG_VAR uslcom_debug
48 #include <dev/usb/usb_debug.h>
49 #include <dev/usb/usb_process.h>
51 #include <dev/usb/serial/usb_serial.h>
54 static int uslcom_debug = 0;
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");
61 #define USLCOM_BULK_BUF_SIZE 1024
62 #define USLCOM_CONFIG_INDEX 0
63 #define USLCOM_IFACE_INDEX 0
65 #define USLCOM_SET_DATA_BITS(x) ((x) << 8)
67 #define USLCOM_WRITE 0x41
68 #define USLCOM_READ 0xc1
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
76 #define USLCOM_UART_DISABLE 0x00
77 #define USLCOM_UART_ENABLE 0x01
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
87 #define USLCOM_BAUD_REF 0x384000
89 #define USLCOM_STOP_BITS_1 0x00
90 #define USLCOM_STOP_BITS_2 0x02
92 #define USLCOM_PARITY_NONE 0x00
93 #define USLCOM_PARITY_ODD 0x10
94 #define USLCOM_PARITY_EVEN 0x20
96 #define USLCOM_PORT_NO 0xFFFF /* XXX think this should be 0 --hps */
98 #define USLCOM_BREAK_OFF 0x00
99 #define USLCOM_BREAK_ON 0x01
107 struct uslcom_softc {
108 struct ucom_super_softc sc_super_ucom;
109 struct ucom_softc sc_ucom;
111 struct usb_xfer *sc_xfer[USLCOM_N_TRANSFER];
112 struct usb_device *sc_udev;
119 static device_probe_t uslcom_probe;
120 static device_attach_t uslcom_attach;
121 static device_detach_t uslcom_detach;
123 static usb_callback_t uslcom_write_callback;
124 static usb_callback_t uslcom_read_callback;
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 *);
139 static const struct usb_config uslcom_config[USLCOM_N_TRANSFER] = {
141 [USLCOM_BULK_DT_WR] = {
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,
150 [USLCOM_BULK_DT_RD] = {
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,
160 static 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,
175 static 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) },
196 static device_method_t uslcom_methods[] = {
197 DEVMETHOD(device_probe, uslcom_probe),
198 DEVMETHOD(device_attach, uslcom_attach),
199 DEVMETHOD(device_detach, uslcom_detach),
203 static devclass_t uslcom_devclass;
205 static driver_t uslcom_driver = {
207 .methods = uslcom_methods,
208 .size = sizeof(struct uslcom_softc),
211 DRIVER_MODULE(uslcom, uhub, uslcom_driver, uslcom_devclass, NULL, 0);
212 MODULE_DEPEND(uslcom, ucom, 1, 1, 1);
213 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
214 MODULE_VERSION(uslcom, 1);
217 uslcom_probe(device_t dev)
219 struct usb_attach_arg *uaa = device_get_ivars(dev);
223 if (uaa->usb_mode != USB_MODE_HOST) {
226 if (uaa->info.bConfigIndex != USLCOM_CONFIG_INDEX) {
229 if (uaa->info.bIfaceIndex != USLCOM_IFACE_INDEX) {
232 return (usbd_lookup_id_by_uaa(uslcom_devs, sizeof(uslcom_devs), uaa));
236 uslcom_attach(device_t dev)
238 struct usb_attach_arg *uaa = device_get_ivars(dev);
239 struct uslcom_softc *sc = device_get_softc(dev);
244 device_set_usb_desc(dev);
245 mtx_init(&sc->sc_mtx, "uslcom", NULL, MTX_DEF);
247 sc->sc_udev = uaa->device;
249 error = usbd_transfer_setup(uaa->device,
250 &uaa->info.bIfaceIndex, sc->sc_xfer, uslcom_config,
251 USLCOM_N_TRANSFER, sc, &sc->sc_mtx);
253 DPRINTF("one or more missing USB endpoints, "
254 "error=%s\n", usbd_errstr(error));
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);
263 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
264 &uslcom_callback, &sc->sc_mtx);
276 uslcom_detach(device_t dev)
278 struct uslcom_softc *sc = device_get_softc(dev);
280 DPRINTF("sc=%p\n", sc);
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);
290 uslcom_open(struct ucom_softc *ucom)
292 struct uslcom_softc *sc = ucom->sc_parent;
293 struct usb_device_request req;
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);
301 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
302 &req, NULL, 0, 1000)) {
303 DPRINTF("UART enable failed (ignored)\n");
308 uslcom_close(struct ucom_softc *ucom)
310 struct uslcom_softc *sc = ucom->sc_parent;
311 struct usb_device_request req;
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);
319 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
320 &req, NULL, 0, 1000)) {
321 DPRINTF("UART disable failed (ignored)\n");
326 uslcom_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
328 struct uslcom_softc *sc = ucom->sc_parent;
329 struct usb_device_request req;
332 DPRINTF("onoff = %d\n", onoff);
334 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
335 ctl |= USLCOM_CTRL_DTR_SET;
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);
343 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
344 &req, NULL, 0, 1000)) {
345 DPRINTF("Setting DTR failed (ignored)\n");
350 uslcom_set_rts(struct ucom_softc *ucom, uint8_t onoff)
352 struct uslcom_softc *sc = ucom->sc_parent;
353 struct usb_device_request req;
356 DPRINTF("onoff = %d\n", onoff);
358 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
359 ctl |= USLCOM_CTRL_RTS_SET;
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);
367 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
368 &req, NULL, 0, 1000)) {
369 DPRINTF("Setting DTR failed (ignored)\n");
374 uslcom_pre_param(struct ucom_softc *ucom, struct termios *t)
376 if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
382 uslcom_param(struct ucom_softc *ucom, struct termios *t)
384 struct uslcom_softc *sc = ucom->sc_parent;
385 struct usb_device_request req;
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);
396 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
397 &req, NULL, 0, 1000)) {
398 DPRINTF("Set baudrate failed (ignored)\n");
401 if (t->c_cflag & CSTOPB)
402 data = USLCOM_STOP_BITS_2;
404 data = USLCOM_STOP_BITS_1;
405 if (t->c_cflag & PARENB) {
406 if (t->c_cflag & PARODD)
407 data |= USLCOM_PARITY_ODD;
409 data |= USLCOM_PARITY_EVEN;
411 data |= USLCOM_PARITY_NONE;
412 switch (t->c_cflag & CSIZE) {
414 data |= USLCOM_SET_DATA_BITS(5);
417 data |= USLCOM_SET_DATA_BITS(6);
420 data |= USLCOM_SET_DATA_BITS(7);
423 data |= USLCOM_SET_DATA_BITS(8);
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);
433 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
434 &req, NULL, 0, 1000)) {
435 DPRINTF("Set format failed (ignored)\n");
441 uslcom_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
443 struct uslcom_softc *sc = ucom->sc_parent;
452 uslcom_set_break(struct ucom_softc *ucom, uint8_t onoff)
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;
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);
464 if (ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
465 &req, NULL, 0, 1000)) {
466 DPRINTF("Set BREAK failed (ignored)\n");
471 uslcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
473 struct uslcom_softc *sc = usbd_xfer_softc(xfer);
474 struct usb_page_cache *pc;
477 switch (USB_GET_STATE(xfer)) {
479 case USB_ST_TRANSFERRED:
481 pc = usbd_xfer_get_frame(xfer, 0);
482 if (ucom_get_data(&sc->sc_ucom, pc, 0,
483 USLCOM_BULK_BUF_SIZE, &actlen)) {
485 DPRINTF("actlen = %d\n", actlen);
487 usbd_xfer_set_frame_len(xfer, 0, actlen);
488 usbd_transfer_submit(xfer);
493 if (error != USB_ERR_CANCELLED) {
494 /* try to clear stall first */
495 usbd_xfer_set_stall(xfer);
503 uslcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
505 struct uslcom_softc *sc = usbd_xfer_softc(xfer);
506 struct usb_page_cache *pc;
509 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
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);
518 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
519 usbd_transfer_submit(xfer);
523 if (error != USB_ERR_CANCELLED) {
524 /* try to clear stall first */
525 usbd_xfer_set_stall(xfer);
533 uslcom_start_read(struct ucom_softc *ucom)
535 struct uslcom_softc *sc = ucom->sc_parent;
537 /* start read endpoint */
538 usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_RD]);
542 uslcom_stop_read(struct ucom_softc *ucom)
544 struct uslcom_softc *sc = ucom->sc_parent;
546 /* stop read endpoint */
547 usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_RD]);
551 uslcom_start_write(struct ucom_softc *ucom)
553 struct uslcom_softc *sc = ucom->sc_parent;
555 usbd_transfer_start(sc->sc_xfer[USLCOM_BULK_DT_WR]);
559 uslcom_stop_write(struct ucom_softc *ucom)
561 struct uslcom_softc *sc = ucom->sc_parent;
563 usbd_transfer_stop(sc->sc_xfer[USLCOM_BULK_DT_WR]);