- Add support for 230400 baud rate.
[dragonfly.git] / sys / dev / usbmisc / uslcom / uslcom.c
bloba7066ee8ae35bca95b944317f49382f0327a13c7
1 /* $DragonFly: src/sys/dev/usbmisc/uslcom/uslcom.c,v 1.9 2007/11/06 20:25:26 hasso Exp $ */
2 /* $OpenBSD: uslcom.c,v 1.12 2007/06/13 06:25:03 mbalmer Exp $ */
4 /*
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>
23 #include <sys/conf.h>
24 #include <sys/tty.h>
25 #include <sys/device.h>
26 #include <sys/types.h>
27 #include <sys/bus.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>
36 #ifdef USLCOM_DEBUG
37 #define DPRINTFN(n, x) do { if (uslcomdebug > (n)) kprintf x; } while (0)
38 int uslcomdebug = 0;
39 #else
40 #define DPRINTFN(n, x)
41 #endif
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
85 struct uslcom_softc {
86 struct ucom_softc sc_ucom;
87 u_char sc_msr;
88 u_char sc_lsr;
91 static void uslcom_get_status(void *, int portno, u_char *lsr,
92 u_char *msr);
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,
99 tcflag_t iflag);
101 struct ucom_callback uslcom_callback = {
102 uslcom_get_status,
103 uslcom_set,
104 uslcom_param,
105 NULL,
106 uslcom_open,
107 uslcom_close,
108 NULL,
109 NULL,
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),
147 { 0, 0 }
150 static driver_t uslcom_driver = {
151 "ucom",
152 uslcom_methods,
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);
161 static int
162 uslcom_match(device_t self)
164 struct usb_attach_arg *uaa = device_get_ivars(self);
166 if (uaa->iface != NULL)
167 return UMATCH_NONE;
169 return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
170 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
173 static int
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;
181 usbd_status error;
182 int i;
184 ucom = &sc->sc_ucom;
186 bzero(sc, sizeof (struct uslcom_softc));
188 ucom->sc_dev = self;
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");
194 goto error;
197 /* get the first interface handle */
198 error = usbd_device2interface_handle(ucom->sc_udev, USLCOM_IFACE_NO,
199 &ucom->sc_iface);
200 if (error != 0) {
201 device_printf(ucom->sc_dev, "could not get interface handle\n");
202 goto error;
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);
210 if (ed == NULL) {
211 device_printf(ucom->sc_dev, "no endpoint descriptor "
212 "found for %d\n", i);
213 goto error;
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");
226 goto error;
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,
238 ucom->sc_dev);
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);
244 return 0;
246 error:
247 ucom->sc_dying = 1;
248 return ENXIO;
251 static int
252 uslcom_detach(device_t self)
254 struct uslcom_softc *sc = device_get_softc(self);
255 int rv = 0;
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,
261 sc->sc_ucom.sc_dev);
263 return (rv);
266 #if 0 /* not yet */
268 uslcom_activate(struct device *self, enum devact act)
270 struct uslcom_softc *sc = (struct uslcom_softc *)self;
271 int rv = 0;
273 switch (act) {
274 case DVACT_ACTIVATE:
275 break;
277 case DVACT_DEACTIVATE:
278 if (sc->sc_subdev != NULL)
279 rv = config_deactivate(sc->sc_subdev);
280 sc->sc_dying = 1;
281 break;
283 return (rv);
285 #endif
287 static int
288 uslcom_open(void *vsc, int portno)
290 struct uslcom_softc *sc = vsc;
291 usb_device_request_t req;
292 usbd_status err;
294 if (sc->sc_ucom.sc_dying)
295 return (EIO);
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);
303 if (err)
304 return (EIO);
306 return (0);
309 static void
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)
316 return;
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);
326 static void
327 uslcom_set(void *vsc, int portno, int reg, int onoff)
329 struct uslcom_softc *sc = vsc;
330 usb_device_request_t req;
331 int ctl;
333 switch (reg) {
334 case UCOM_SET_DTR:
335 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
336 ctl |= USLCOM_CTRL_DTR_SET;
337 break;
338 case UCOM_SET_RTS:
339 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
340 ctl |= USLCOM_CTRL_RTS_SET;
341 break;
342 case UCOM_SET_BREAK:
343 uslcom_break(sc, portno, onoff);
344 return;
345 default:
346 return;
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);
356 static int
357 uslcom_param(void *vsc, int portno, struct termios *t)
359 struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
360 usbd_status err;
361 usb_device_request_t req;
362 int data;
364 switch (t->c_ospeed) {
365 case 600:
366 case 1200:
367 case 1800:
368 case 2400:
369 case 4800:
370 case 9600:
371 case 19200:
372 case 38400:
373 case 57600:
374 case 115200:
375 case 230400:
376 case 460800:
377 case 921600:
378 req.bmRequestType = USLCOM_WRITE;
379 req.bRequest = USLCOM_BAUD_RATE;
380 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
381 USETW(req.wIndex, portno);
382 USETW(req.wLength, 0);
383 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
384 if (err)
385 return (EIO);
386 break;
387 default:
388 return (EINVAL);
391 if (ISSET(t->c_cflag, CSTOPB))
392 data = USLCOM_STOP_BITS_2;
393 else
394 data = USLCOM_STOP_BITS_1;
395 if (ISSET(t->c_cflag, PARENB)) {
396 if (ISSET(t->c_cflag, PARODD))
397 data |= USLCOM_PARITY_ODD;
398 else
399 data |= USLCOM_PARITY_EVEN;
400 } else
401 data |= USLCOM_PARITY_NONE;
402 switch (ISSET(t->c_cflag, CSIZE)) {
403 case CS5:
404 data |= USLCOM_SET_DATA_BITS(5);
405 break;
406 case CS6:
407 data |= USLCOM_SET_DATA_BITS(6);
408 break;
409 case CS7:
410 data |= USLCOM_SET_DATA_BITS(7);
411 break;
412 case CS8:
413 data |= USLCOM_SET_DATA_BITS(8);
414 break;
417 req.bmRequestType = USLCOM_WRITE;
418 req.bRequest = USLCOM_DATA;
419 USETW(req.wValue, data);
420 USETW(req.wIndex, portno);
421 USETW(req.wLength, 0);
422 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
423 if (err)
424 return (EIO);
426 uslcom_set_flow_ctrl(sc, t->c_cflag, t->c_iflag);
428 return (0);
431 static void
432 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
434 struct uslcom_softc *sc = vsc;
436 if (msr != NULL)
437 *msr = sc->sc_msr;
438 if (lsr != NULL)
439 *lsr = sc->sc_lsr;
442 static void
443 uslcom_break(void *vsc, int portno, int onoff)
445 struct uslcom_softc *sc = vsc;
446 usb_device_request_t req;
447 int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
449 req.bmRequestType = USLCOM_WRITE;
450 req.bRequest = USLCOM_BREAK;
451 USETW(req.wValue, brk);
452 USETW(req.wIndex, portno);
453 USETW(req.wLength, 0);
454 usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
457 static void
458 uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag, tcflag_t iflag)
460 uint8_t modemdata[16];
461 usb_device_request_t req;
462 usbd_status err;
464 req.bmRequestType = USLCOM_READ;
465 req.bRequest = USLCOM_MODEM;
466 USETW(req.wValue, 0);
467 USETW(req.wIndex, 0);
468 USETW(req.wLength, 16);
470 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
471 if (err)
472 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
473 usbd_errstr(err));
475 if (ISSET(cflag, CRTSCTS)) {
476 modemdata[0] &= ~0x7b;
477 modemdata[0] |= 0x09;
478 modemdata[4] = 0x80;
479 } else {
480 modemdata[0] &= ~0x7b;
481 modemdata[0] |= 0x01;
482 modemdata[4] = 0x40;
485 req.bmRequestType = USLCOM_WRITE;
486 req.bRequest = USLCOM_MODEM;
487 USETW(req.wValue, 0);
488 USETW(req.wIndex, 0);
489 USETW(req.wLength, 16);
491 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
492 if (err)
493 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
494 usbd_errstr(err));