1 /* $DragonFly: src/sys/dev/usbmisc/uslcom/uslcom.c,v 1.10 2007/11/26 10:52:33 hasso Exp $ */
2 /* $OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $ */
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
25 #include <sys/device.h>
26 #include <sys/types.h>
28 #include <sys/module.h>
30 #include <bus/usb/usb.h>
31 #include <bus/usb/usbdi.h>
32 #include <bus/usb/usbdi_util.h>
34 #include <dev/usbmisc/ucom/ucomvar.h>
37 #define DPRINTFN(n, x) do { if (uslcomdebug > (n)) kprintf x; } while (0)
40 #define DPRINTFN(n, x)
42 #define DPRINTF(x) DPRINTFN(0, x)
44 #define USLCOMBUFSZ 256
45 #define USLCOM_CONFIG_NO 0
46 #define USLCOM_IFACE_NO 0
48 #define USLCOM_SET_DATA_BITS(x) (x << 8)
50 #define USLCOM_WRITE 0x41
51 #define USLCOM_READ 0xc1
53 #define USLCOM_UART 0x00
54 #define USLCOM_BAUD_RATE 0x01
55 #define USLCOM_DATA 0x03
56 #define USLCOM_BREAK 0x05
57 #define USLCOM_CTRL 0x07
58 #define USLCOM_MODEM 0x13
60 #define USLCOM_UART_DISABLE 0x00
61 #define USLCOM_UART_ENABLE 0x01
63 #define USLCOM_CTRL_DTR_ON 0x0001
64 #define USLCOM_CTRL_DTR_SET 0x0100
65 #define USLCOM_CTRL_RTS_ON 0x0002
66 #define USLCOM_CTRL_RTS_SET 0x0200
67 #define USLCOM_CTRL_CTS 0x0010
68 #define USLCOM_CTRL_DSR 0x0020
69 #define USLCOM_CTRL_DCD 0x0080
72 #define USLCOM_BAUD_REF 0x384000
74 #define USLCOM_STOP_BITS_1 0x00
75 #define USLCOM_STOP_BITS_2 0x02
77 #define USLCOM_PARITY_NONE 0x00
78 #define USLCOM_PARITY_ODD 0x10
79 #define USLCOM_PARITY_EVEN 0x20
81 #define USLCOM_BREAK_OFF 0x00
82 #define USLCOM_BREAK_ON 0x01
86 struct ucom_softc sc_ucom;
91 static void uslcom_get_status(void *, int portno, u_char *lsr,
93 static void uslcom_set(void *, int, int, int);
94 static int uslcom_param(void *, int, struct termios *);
95 static int uslcom_open(void *sc, int portno);
96 static void uslcom_close(void *, int);
97 static void uslcom_break(void *sc, int portno, int onoff);
98 static void uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag,
101 struct ucom_callback uslcom_callback = {
112 static const struct usb_devno uslcom_devs[] = {
113 { USB_DEVICE(0x0fcf, 0x1003) }, /* ANT development board */
114 { USB_DEVICE(0x10a6, 0xaa26) }, /* Noname DCU-11 clone */
115 { USB_DEVICE(0x10ab, 0x10c5) }, /* USI MC60 */
116 { USB_DEVICE(0x10b5, 0xac70) }, /* PLX CA-42 */
117 { USB_DEVICE(0x10c4, 0x803b) }, /* Pololu Serial */
118 { USB_DEVICE(0x10c4, 0x8053) }, /* Enfora EDG1228 */
119 { USB_DEVICE(0x10c4, 0x8066) }, /* Argussoft In-System Programmer */
120 { USB_DEVICE(0x10c4, 0x807a) }, /* Crumb128 board */
121 { USB_DEVICE(0x10c4, 0x80ca) }, /* Degree Controls */
122 { USB_DEVICE(0x10c4, 0x80dd) }, /* Tracient RFID */
123 { USB_DEVICE(0x10c4, 0x80ed) }, /* Track Systems Traqmate */
124 { USB_DEVICE(0x10c4, 0x80f6) }, /* Suunto sports */
125 { USB_DEVICE(0x10c4, 0x813d) }, /* Burnside Desktop mobile */
126 { USB_DEVICE(0x10c4, 0x814a) }, /* West Mountain Radio RIGblaster */
127 { USB_DEVICE(0x10c4, 0x814b) }, /* West Mountain Radio RIGtalk */
128 { USB_DEVICE(0x10c4, 0x815e) }, /* IP-Link 1220 */
129 { USB_DEVICE(0x10c4, 0x81c8) }, /* Lipowsky Baby-JTAG */
130 { USB_DEVICE(0x10c4, 0x81e2) }, /* Lipowsky Baby-LIN */
131 { USB_DEVICE(0x10c4, 0x8218) }, /* Lipowsky HARP-1 */
132 { USB_DEVICE(0x10c4, 0xea60) }, /* Silicon Labs CP210x */
133 { USB_DEVICE(0x10c4, 0xea61) }, /* Silicon Labs CP210x */
134 { USB_DEVICE(0x13ad, 0x9999) }, /* Baltech card reader */
135 { USB_DEVICE(0x16d6, 0x0001) }, /* Jablotron PC-60B */
138 static device_probe_t uslcom_match;
139 static device_attach_t uslcom_attach;
140 static device_detach_t uslcom_detach;
142 static device_method_t uslcom_methods[] = {
143 /* Device interface */
144 DEVMETHOD(device_probe, uslcom_match),
145 DEVMETHOD(device_attach, uslcom_attach),
146 DEVMETHOD(device_detach, uslcom_detach),
150 static driver_t uslcom_driver = {
153 sizeof (struct uslcom_softc)
156 DRIVER_MODULE(uslcom, uhub, uslcom_driver, ucom_devclass, usbd_driver_load, 0);
157 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
158 MODULE_DEPEND(uslcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
159 MODULE_VERSION(uslcom, 1);
162 uslcom_match(device_t self)
164 struct usb_attach_arg *uaa = device_get_ivars(self);
166 if (uaa->iface != NULL)
169 return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
170 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
174 uslcom_attach(device_t self)
176 struct uslcom_softc *sc = device_get_softc(self);
177 struct usb_attach_arg *uaa = device_get_ivars(self);
178 struct ucom_softc *ucom;
179 usb_interface_descriptor_t *id;
180 usb_endpoint_descriptor_t *ed;
186 bzero(sc, sizeof (struct uslcom_softc));
189 ucom->sc_udev = uaa->device;
190 ucom->sc_iface = uaa->iface;
192 if (usbd_set_config_index(ucom->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
193 device_printf(ucom->sc_dev, "could not set configuration no\n");
197 /* get the first interface handle */
198 error = usbd_device2interface_handle(ucom->sc_udev, USLCOM_IFACE_NO,
201 device_printf(ucom->sc_dev, "could not get interface handle\n");
205 id = usbd_get_interface_descriptor(ucom->sc_iface);
207 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
208 for (i = 0; i < id->bNumEndpoints; i++) {
209 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
211 device_printf(ucom->sc_dev, "no endpoint descriptor "
212 "found for %d\n", i);
216 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
217 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
218 ucom->sc_bulkin_no = ed->bEndpointAddress;
219 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
220 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
221 ucom->sc_bulkout_no = ed->bEndpointAddress;
224 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
225 device_printf(ucom->sc_dev, "missing endpoint\n");
229 ucom->sc_parent = sc;
230 ucom->sc_portno = UCOM_UNK_PORTNO;
231 ucom->sc_ibufsize = USLCOMBUFSZ;
232 ucom->sc_obufsize = USLCOMBUFSZ;
233 ucom->sc_ibufsizepad = USLCOMBUFSZ;
234 ucom->sc_opkthdrlen = 0;
235 ucom->sc_callback = &uslcom_callback;
237 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
240 DPRINTF(("uslcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
241 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
243 ucom_attach(&sc->sc_ucom);
252 uslcom_detach(device_t self)
254 struct uslcom_softc *sc = device_get_softc(self);
257 DPRINTF(("uslcom_detach: sc=%p\n", sc));
258 sc->sc_ucom.sc_dying = 1;
259 rv = ucom_detach(&sc->sc_ucom);
260 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
268 uslcom_activate(struct device *self, enum devact act)
270 struct uslcom_softc *sc = (struct uslcom_softc *)self;
277 case DVACT_DEACTIVATE:
278 if (sc->sc_subdev != NULL)
279 rv = config_deactivate(sc->sc_subdev);
288 uslcom_open(void *vsc, int portno)
290 struct uslcom_softc *sc = vsc;
291 usb_device_request_t req;
294 if (sc->sc_ucom.sc_dying)
297 req.bmRequestType = USLCOM_WRITE;
298 req.bRequest = USLCOM_UART;
299 USETW(req.wValue, USLCOM_UART_ENABLE);
300 USETW(req.wIndex, portno);
301 USETW(req.wLength, 0);
302 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
310 uslcom_close(void *vsc, int portno)
312 struct uslcom_softc *sc = vsc;
313 usb_device_request_t req;
315 if (sc->sc_ucom.sc_dying)
318 req.bmRequestType = USLCOM_WRITE;
319 req.bRequest = USLCOM_UART;
320 USETW(req.wValue, USLCOM_UART_DISABLE);
321 USETW(req.wIndex, portno);
322 USETW(req.wLength, 0);
323 usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
327 uslcom_set(void *vsc, int portno, int reg, int onoff)
329 struct uslcom_softc *sc = vsc;
330 usb_device_request_t req;
335 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
336 ctl |= USLCOM_CTRL_DTR_SET;
339 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
340 ctl |= USLCOM_CTRL_RTS_SET;
343 uslcom_break(sc, portno, onoff);
348 req.bmRequestType = USLCOM_WRITE;
349 req.bRequest = USLCOM_CTRL;
350 USETW(req.wValue, ctl);
351 USETW(req.wIndex, portno);
352 USETW(req.wLength, 0);
353 usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
357 uslcom_param(void *vsc, int portno, struct termios *t)
359 struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
361 usb_device_request_t req;
364 if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
367 req.bmRequestType = USLCOM_WRITE;
368 req.bRequest = USLCOM_BAUD_RATE;
369 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
370 USETW(req.wIndex, portno);
371 USETW(req.wLength, 0);
372 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
376 if (ISSET(t->c_cflag, CSTOPB))
377 data = USLCOM_STOP_BITS_2;
379 data = USLCOM_STOP_BITS_1;
380 if (ISSET(t->c_cflag, PARENB)) {
381 if (ISSET(t->c_cflag, PARODD))
382 data |= USLCOM_PARITY_ODD;
384 data |= USLCOM_PARITY_EVEN;
386 data |= USLCOM_PARITY_NONE;
387 switch (ISSET(t->c_cflag, CSIZE)) {
389 data |= USLCOM_SET_DATA_BITS(5);
392 data |= USLCOM_SET_DATA_BITS(6);
395 data |= USLCOM_SET_DATA_BITS(7);
398 data |= USLCOM_SET_DATA_BITS(8);
402 req.bmRequestType = USLCOM_WRITE;
403 req.bRequest = USLCOM_DATA;
404 USETW(req.wValue, data);
405 USETW(req.wIndex, portno);
406 USETW(req.wLength, 0);
407 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
411 uslcom_set_flow_ctrl(sc, t->c_cflag, t->c_iflag);
417 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
419 struct uslcom_softc *sc = vsc;
428 uslcom_break(void *vsc, int portno, int onoff)
430 struct uslcom_softc *sc = vsc;
431 usb_device_request_t req;
432 int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
434 req.bmRequestType = USLCOM_WRITE;
435 req.bRequest = USLCOM_BREAK;
436 USETW(req.wValue, brk);
437 USETW(req.wIndex, portno);
438 USETW(req.wLength, 0);
439 usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
443 uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag, tcflag_t iflag)
445 uint8_t modemdata[16];
446 usb_device_request_t req;
449 req.bmRequestType = USLCOM_READ;
450 req.bRequest = USLCOM_MODEM;
451 USETW(req.wValue, 0);
452 USETW(req.wIndex, 0);
453 USETW(req.wLength, 16);
455 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
457 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
460 if (ISSET(cflag, CRTSCTS)) {
461 modemdata[0] &= ~0x7b;
462 modemdata[0] |= 0x09;
465 modemdata[0] &= ~0x7b;
466 modemdata[0] |= 0x01;
470 req.bmRequestType = USLCOM_WRITE;
471 req.bRequest = USLCOM_MODEM;
472 USETW(req.wValue, 0);
473 USETW(req.wIndex, 0);
474 USETW(req.wLength, 16);
476 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
478 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",