1 /* $OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $ */
4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/kernel.h>
24 #include <sys/device.h>
25 #include <sys/types.h>
27 #include <sys/module.h>
29 #include <bus/usb/usb.h>
30 #include <bus/usb/usbdi.h>
31 #include <bus/usb/usbdi_util.h>
33 #include <dev/usbmisc/ucom/ucomvar.h>
36 #define DPRINTFN(n, x) do { if (uslcomdebug > (n)) kprintf x; } while (0)
39 #define DPRINTFN(n, x)
41 #define DPRINTF(x) DPRINTFN(0, x)
43 #define USLCOMBUFSZ 256
44 #define USLCOM_CONFIG_NO 0
45 #define USLCOM_IFACE_NO 0
47 #define USLCOM_SET_DATA_BITS(x) (x << 8)
49 #define USLCOM_WRITE 0x41
50 #define USLCOM_READ 0xc1
52 #define USLCOM_UART 0x00
53 #define USLCOM_BAUD_RATE 0x01
54 #define USLCOM_DATA 0x03
55 #define USLCOM_BREAK 0x05
56 #define USLCOM_CTRL 0x07
57 #define USLCOM_MODEM 0x13
59 #define USLCOM_UART_DISABLE 0x00
60 #define USLCOM_UART_ENABLE 0x01
62 #define USLCOM_CTRL_DTR_ON 0x0001
63 #define USLCOM_CTRL_DTR_SET 0x0100
64 #define USLCOM_CTRL_RTS_ON 0x0002
65 #define USLCOM_CTRL_RTS_SET 0x0200
66 #define USLCOM_CTRL_CTS 0x0010
67 #define USLCOM_CTRL_DSR 0x0020
68 #define USLCOM_CTRL_DCD 0x0080
71 #define USLCOM_BAUD_REF 0x384000
73 #define USLCOM_STOP_BITS_1 0x00
74 #define USLCOM_STOP_BITS_2 0x02
76 #define USLCOM_PARITY_NONE 0x00
77 #define USLCOM_PARITY_ODD 0x10
78 #define USLCOM_PARITY_EVEN 0x20
80 #define USLCOM_BREAK_OFF 0x00
81 #define USLCOM_BREAK_ON 0x01
85 struct ucom_softc sc_ucom;
90 static void uslcom_get_status(void *, int portno, u_char *lsr,
92 static void uslcom_set(void *, int, int, int);
93 static int uslcom_param(void *, int, struct termios *);
94 static int uslcom_open(void *sc, int portno);
95 static void uslcom_close(void *, int);
96 static void uslcom_break(void *sc, int portno, int onoff);
97 static void uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag,
100 struct ucom_callback uslcom_callback = {
111 static const struct usb_devno uslcom_devs[] = {
112 { USB_DEVICE(0x0fcf, 0x1003) }, /* ANT development board */
113 { USB_DEVICE(0x10a6, 0xaa26) }, /* Noname DCU-11 clone */
114 { USB_DEVICE(0x10ab, 0x10c5) }, /* USI MC60 */
115 { USB_DEVICE(0x10b5, 0xac70) }, /* PLX CA-42 */
116 { USB_DEVICE(0x10c4, 0x803b) }, /* Pololu Serial */
117 { USB_DEVICE(0x10c4, 0x8053) }, /* Enfora EDG1228 */
118 { USB_DEVICE(0x10c4, 0x8066) }, /* Argussoft In-System Programmer */
119 { USB_DEVICE(0x10c4, 0x807a) }, /* Crumb128 board */
120 { USB_DEVICE(0x10c4, 0x80ca) }, /* Degree Controls */
121 { USB_DEVICE(0x10c4, 0x80dd) }, /* Tracient RFID */
122 { USB_DEVICE(0x10c4, 0x80ed) }, /* Track Systems Traqmate */
123 { USB_DEVICE(0x10c4, 0x80f6) }, /* Suunto sports */
124 { USB_DEVICE(0x10c4, 0x813d) }, /* Burnside Desktop mobile */
125 { USB_DEVICE(0x10c4, 0x814a) }, /* West Mountain Radio RIGblaster */
126 { USB_DEVICE(0x10c4, 0x814b) }, /* West Mountain Radio RIGtalk */
127 { USB_DEVICE(0x10c4, 0x815e) }, /* IP-Link 1220 */
128 { USB_DEVICE(0x10c4, 0x81c8) }, /* Lipowsky Baby-JTAG */
129 { USB_DEVICE(0x10c4, 0x81e2) }, /* Lipowsky Baby-LIN */
130 { USB_DEVICE(0x10c4, 0x8218) }, /* Lipowsky HARP-1 */
131 { USB_DEVICE(0x10c4, 0xea60) }, /* Silicon Labs CP210x */
132 { USB_DEVICE(0x10c4, 0xea61) }, /* Silicon Labs CP210x */
133 { USB_DEVICE(0x13ad, 0x9999) }, /* Baltech card reader */
134 { USB_DEVICE(0x16d6, 0x0001) }, /* Jablotron PC-60B */
137 static device_probe_t uslcom_match;
138 static device_attach_t uslcom_attach;
139 static device_detach_t uslcom_detach;
141 static device_method_t uslcom_methods[] = {
142 /* Device interface */
143 DEVMETHOD(device_probe, uslcom_match),
144 DEVMETHOD(device_attach, uslcom_attach),
145 DEVMETHOD(device_detach, uslcom_detach),
149 static driver_t uslcom_driver = {
152 sizeof (struct uslcom_softc)
155 DRIVER_MODULE(uslcom, uhub, uslcom_driver, ucom_devclass, usbd_driver_load, NULL);
156 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
157 MODULE_DEPEND(uslcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
158 MODULE_VERSION(uslcom, 1);
161 uslcom_match(device_t self)
163 struct usb_attach_arg *uaa = device_get_ivars(self);
165 if (uaa->iface != NULL)
168 return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
169 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
173 uslcom_attach(device_t self)
175 struct uslcom_softc *sc = device_get_softc(self);
176 struct usb_attach_arg *uaa = device_get_ivars(self);
177 struct ucom_softc *ucom;
178 usb_interface_descriptor_t *id;
179 usb_endpoint_descriptor_t *ed;
185 bzero(sc, sizeof (struct uslcom_softc));
188 ucom->sc_udev = uaa->device;
189 ucom->sc_iface = uaa->iface;
191 if (usbd_set_config_index(ucom->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
192 device_printf(ucom->sc_dev, "could not set configuration no\n");
196 /* get the first interface handle */
197 error = usbd_device2interface_handle(ucom->sc_udev, USLCOM_IFACE_NO,
200 device_printf(ucom->sc_dev, "could not get interface handle\n");
204 id = usbd_get_interface_descriptor(ucom->sc_iface);
206 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
207 for (i = 0; i < id->bNumEndpoints; i++) {
208 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
210 device_printf(ucom->sc_dev, "no endpoint descriptor "
211 "found for %d\n", i);
215 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
216 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
217 ucom->sc_bulkin_no = ed->bEndpointAddress;
218 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
219 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
220 ucom->sc_bulkout_no = ed->bEndpointAddress;
223 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
224 device_printf(ucom->sc_dev, "missing endpoint\n");
228 ucom->sc_parent = sc;
229 ucom->sc_portno = UCOM_UNK_PORTNO;
230 ucom->sc_ibufsize = USLCOMBUFSZ;
231 ucom->sc_obufsize = USLCOMBUFSZ;
232 ucom->sc_ibufsizepad = USLCOMBUFSZ;
233 ucom->sc_opkthdrlen = 0;
234 ucom->sc_callback = &uslcom_callback;
236 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
239 DPRINTF(("uslcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
240 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
242 ucom_attach(&sc->sc_ucom);
251 uslcom_detach(device_t self)
253 struct uslcom_softc *sc = device_get_softc(self);
256 DPRINTF(("uslcom_detach: sc=%p\n", sc));
257 sc->sc_ucom.sc_dying = 1;
258 rv = ucom_detach(&sc->sc_ucom);
259 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
267 uslcom_activate(struct device *self, enum devact act)
269 struct uslcom_softc *sc = (struct uslcom_softc *)self;
276 case DVACT_DEACTIVATE:
277 if (sc->sc_subdev != NULL)
278 rv = config_deactivate(sc->sc_subdev);
287 uslcom_open(void *vsc, int portno)
289 struct uslcom_softc *sc = vsc;
290 usb_device_request_t req;
293 if (sc->sc_ucom.sc_dying)
296 req.bmRequestType = USLCOM_WRITE;
297 req.bRequest = USLCOM_UART;
298 USETW(req.wValue, USLCOM_UART_ENABLE);
299 USETW(req.wIndex, portno);
300 USETW(req.wLength, 0);
301 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
309 uslcom_close(void *vsc, int portno)
311 struct uslcom_softc *sc = vsc;
312 usb_device_request_t req;
314 if (sc->sc_ucom.sc_dying)
317 req.bmRequestType = USLCOM_WRITE;
318 req.bRequest = USLCOM_UART;
319 USETW(req.wValue, USLCOM_UART_DISABLE);
320 USETW(req.wIndex, portno);
321 USETW(req.wLength, 0);
322 usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
326 uslcom_set(void *vsc, int portno, int reg, int onoff)
328 struct uslcom_softc *sc = vsc;
329 usb_device_request_t req;
334 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
335 ctl |= USLCOM_CTRL_DTR_SET;
338 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
339 ctl |= USLCOM_CTRL_RTS_SET;
342 uslcom_break(sc, portno, onoff);
347 req.bmRequestType = USLCOM_WRITE;
348 req.bRequest = USLCOM_CTRL;
349 USETW(req.wValue, ctl);
350 USETW(req.wIndex, portno);
351 USETW(req.wLength, 0);
352 usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
356 uslcom_param(void *vsc, int portno, struct termios *t)
358 struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
360 usb_device_request_t req;
363 if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
366 req.bmRequestType = USLCOM_WRITE;
367 req.bRequest = USLCOM_BAUD_RATE;
368 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
369 USETW(req.wIndex, portno);
370 USETW(req.wLength, 0);
371 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
375 if (ISSET(t->c_cflag, CSTOPB))
376 data = USLCOM_STOP_BITS_2;
378 data = USLCOM_STOP_BITS_1;
379 if (ISSET(t->c_cflag, PARENB)) {
380 if (ISSET(t->c_cflag, PARODD))
381 data |= USLCOM_PARITY_ODD;
383 data |= USLCOM_PARITY_EVEN;
385 data |= USLCOM_PARITY_NONE;
386 switch (ISSET(t->c_cflag, CSIZE)) {
388 data |= USLCOM_SET_DATA_BITS(5);
391 data |= USLCOM_SET_DATA_BITS(6);
394 data |= USLCOM_SET_DATA_BITS(7);
397 data |= USLCOM_SET_DATA_BITS(8);
401 req.bmRequestType = USLCOM_WRITE;
402 req.bRequest = USLCOM_DATA;
403 USETW(req.wValue, data);
404 USETW(req.wIndex, portno);
405 USETW(req.wLength, 0);
406 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
410 uslcom_set_flow_ctrl(sc, t->c_cflag, t->c_iflag);
416 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
418 struct uslcom_softc *sc = vsc;
427 uslcom_break(void *vsc, int portno, int onoff)
429 struct uslcom_softc *sc = vsc;
430 usb_device_request_t req;
431 int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
433 req.bmRequestType = USLCOM_WRITE;
434 req.bRequest = USLCOM_BREAK;
435 USETW(req.wValue, brk);
436 USETW(req.wIndex, portno);
437 USETW(req.wLength, 0);
438 usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
442 uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag, tcflag_t iflag)
444 uint8_t modemdata[16];
445 usb_device_request_t req;
448 req.bmRequestType = USLCOM_READ;
449 req.bRequest = USLCOM_MODEM;
450 USETW(req.wValue, 0);
451 USETW(req.wIndex, 0);
452 USETW(req.wLength, 16);
454 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
456 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
459 if (ISSET(cflag, CRTSCTS)) {
460 modemdata[0] &= ~0x7b;
461 modemdata[0] |= 0x09;
464 modemdata[0] &= ~0x7b;
465 modemdata[0] |= 0x01;
469 req.bmRequestType = USLCOM_WRITE;
470 req.bRequest = USLCOM_MODEM;
471 USETW(req.wValue, 0);
472 USETW(req.wIndex, 0);
473 USETW(req.wLength, 16);
475 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
477 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",