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>
23 #include <sys/module.h>
28 #include <dev/usb/usb.h>
29 #include <dev/usb/usbcdc.h>
31 #include <dev/usb/usb.h>
32 #include <dev/usb/usbdi.h>
33 #include <dev/usb/usbdi_util.h>
36 #include <dev/usb/ucomvar.h>
39 #define DPRINTFN(n, x) do { if (uslcomdebug > (n)) printf x; } while (0)
42 #define DPRINTFN(n, x)
44 #define DPRINTF(x) DPRINTFN(0, x)
46 #define USLCOMBUFSZ 256
47 #define USLCOM_CONFIG_NO 0
48 #define USLCOM_IFACE_NO 0
50 #define USLCOM_SET_DATA_BITS(x) (x << 8)
52 #define USLCOM_WRITE 0x41
53 #define USLCOM_READ 0xc1
55 #define USLCOM_UART 0x00
56 #define USLCOM_BAUD_RATE 0x01
57 #define USLCOM_DATA 0x03
58 #define USLCOM_BREAK 0x05
59 #define USLCOM_CTRL 0x07
61 #define USLCOM_UART_DISABLE 0x00
62 #define USLCOM_UART_ENABLE 0x01
64 #define USLCOM_CTRL_DTR_ON 0x0001
65 #define USLCOM_CTRL_DTR_SET 0x0100
66 #define USLCOM_CTRL_RTS_ON 0x0002
67 #define USLCOM_CTRL_RTS_SET 0x0200
68 #define USLCOM_CTRL_CTS 0x0010
69 #define USLCOM_CTRL_DSR 0x0020
70 #define USLCOM_CTRL_DCD 0x0080
73 #define USLCOM_BAUD_REF 0x384000
75 #define USLCOM_STOP_BITS_1 0x00
76 #define USLCOM_STOP_BITS_2 0x02
78 #define USLCOM_PARITY_NONE 0x00
79 #define USLCOM_PARITY_ODD 0x10
80 #define USLCOM_PARITY_EVEN 0x20
82 #define USLCOM_BREAK_OFF 0x00
83 #define USLCOM_BREAK_ON 0x01
87 struct ucom_softc sc_ucom;
89 usbd_device_handle sc_udev;
97 void uslcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
98 void uslcom_set(void *, int, int, int);
99 int uslcom_param(void *, int, struct termios *);
100 int uslcom_open(void *sc, int portno);
101 void uslcom_close(void *, int);
102 void uslcom_break(void *sc, int portno, int onoff);
104 struct ucom_callback uslcom_callback = {
115 static const struct usb_devno uslcom_devs[] = {
116 { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER },
117 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
118 { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B },
119 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP },
120 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 },
121 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREE },
122 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_BURNSIDE },
123 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_HELICOM },
124 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP },
125 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
126 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN },
127 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU },
128 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP2102 },
129 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 },
130 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUUNTO },
131 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_TRAQMATE },
132 { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE },
133 { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 }
136 static device_probe_t uslcom_match;
137 static device_attach_t uslcom_attach;
138 static device_detach_t uslcom_detach;
140 static device_method_t uslcom_methods[] = {
141 /* Device interface */
142 DEVMETHOD(device_probe, uslcom_match),
143 DEVMETHOD(device_attach, uslcom_attach),
144 DEVMETHOD(device_detach, uslcom_detach),
148 static driver_t uslcom_driver = {
151 sizeof (struct uslcom_softc)
154 DRIVER_MODULE(uslcom, uhub, uslcom_driver, ucom_devclass, usbd_driver_load, 0);
155 MODULE_DEPEND(uslcom, usb, 1, 1, 1);
156 MODULE_DEPEND(uslcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
157 MODULE_VERSION(uslcom, 1);
160 uslcom_match(device_t self)
162 struct usb_attach_arg *uaa = device_get_ivars(self);
164 if (uaa->iface != NULL)
167 return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
168 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
172 uslcom_attach(device_t self)
174 struct uslcom_softc *sc = device_get_softc(self);
175 struct usb_attach_arg *uaa = device_get_ivars(self);
176 usbd_device_handle dev = uaa->device;
177 struct ucom_softc* ucom;
178 usb_interface_descriptor_t *id;
179 usb_endpoint_descriptor_t *ed;
186 ucom->sc_iface = uaa->iface;
189 sc->sc_udev = uaa->device;
191 if (usbd_set_config_index(sc->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
192 device_printf(self, "could not set configuration no\n");
197 /* get the first interface handle */
198 error = usbd_device2interface_handle(sc->sc_udev, USLCOM_IFACE_NO,
201 device_printf(self, "could not get interface handle\n");
206 id = usbd_get_interface_descriptor(ucom->sc_iface);
208 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
209 for (i = 0; i < id->bNumEndpoints; i++) {
210 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
212 device_printf(self, "no endpoint descriptor found for %d\n",
218 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
219 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
220 ucom->sc_bulkin_no = ed->bEndpointAddress;
221 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
222 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
223 ucom->sc_bulkout_no = ed->bEndpointAddress;
226 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
227 device_printf(self, "missing endpoint\n");
232 ucom->sc_parent = sc;
233 ucom->sc_portno = UCOM_UNK_PORTNO;
234 /* bulkin, bulkout set above */
235 ucom->sc_ibufsize = USLCOMBUFSZ;
236 ucom->sc_obufsize = USLCOMBUFSZ;
237 ucom->sc_ibufsizepad = USLCOMBUFSZ;
238 ucom->sc_opkthdrlen = 0;
239 ucom->sc_callback = &uslcom_callback;
241 DPRINTF(("uslcom: in = 0x%x, out = 0x%x\n",
242 ucom->sc_bulkin_no, ucom->sc_bulkout_no));
244 ucom_attach(&sc->sc_ucom);
249 uslcom_detach(device_t self)
251 struct uslcom_softc *sc = device_get_softc(self);
254 return ucom_detach(&sc->sc_ucom);
258 uslcom_open(void *vsc, int portno)
260 struct uslcom_softc *sc = vsc;
261 usb_device_request_t req;
267 req.bmRequestType = USLCOM_WRITE;
268 req.bRequest = USLCOM_UART;
269 USETW(req.wValue, USLCOM_UART_ENABLE);
270 USETW(req.wIndex, portno);
271 USETW(req.wLength, 0);
272 err = usbd_do_request(sc->sc_udev, &req, NULL);
280 uslcom_close(void *vsc, int portno)
282 struct uslcom_softc *sc = vsc;
283 usb_device_request_t req;
288 req.bmRequestType = USLCOM_WRITE;
289 req.bRequest = USLCOM_UART;
290 USETW(req.wValue, USLCOM_UART_DISABLE);
291 USETW(req.wIndex, portno);
292 USETW(req.wLength, 0);
293 usbd_do_request(sc->sc_udev, &req, NULL);
297 uslcom_set(void *vsc, int portno, int reg, int onoff)
299 struct uslcom_softc *sc = vsc;
300 usb_device_request_t req;
305 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
306 ctl |= USLCOM_CTRL_DTR_SET;
309 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
310 ctl |= USLCOM_CTRL_RTS_SET;
313 uslcom_break(sc, portno, onoff);
318 req.bmRequestType = USLCOM_WRITE;
319 req.bRequest = USLCOM_CTRL;
320 USETW(req.wValue, ctl);
321 USETW(req.wIndex, portno);
322 USETW(req.wLength, 0);
323 usbd_do_request(sc->sc_udev, &req, NULL);
327 uslcom_param(void *vsc, int portno, struct termios *t)
329 struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
331 usb_device_request_t req;
334 if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
337 req.bmRequestType = USLCOM_WRITE;
338 req.bRequest = USLCOM_BAUD_RATE;
339 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
340 USETW(req.wIndex, portno);
341 USETW(req.wLength, 0);
342 err = usbd_do_request(sc->sc_udev, &req, NULL);
346 if (ISSET(t->c_cflag, CSTOPB))
347 data = USLCOM_STOP_BITS_2;
349 data = USLCOM_STOP_BITS_1;
350 if (ISSET(t->c_cflag, PARENB)) {
351 if (ISSET(t->c_cflag, PARODD))
352 data |= USLCOM_PARITY_ODD;
354 data |= USLCOM_PARITY_EVEN;
356 data |= USLCOM_PARITY_NONE;
357 switch (ISSET(t->c_cflag, CSIZE)) {
359 data |= USLCOM_SET_DATA_BITS(5);
362 data |= USLCOM_SET_DATA_BITS(6);
365 data |= USLCOM_SET_DATA_BITS(7);
368 data |= USLCOM_SET_DATA_BITS(8);
372 req.bmRequestType = USLCOM_WRITE;
373 req.bRequest = USLCOM_DATA;
374 USETW(req.wValue, data);
375 USETW(req.wIndex, portno);
376 USETW(req.wLength, 0);
377 err = usbd_do_request(sc->sc_udev, &req, NULL);
382 /* XXX flow control */
383 if (ISSET(t->c_cflag, CRTSCTS))
384 /* rts/cts flow ctl */
385 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
386 /* xon/xoff flow ctl */
388 /* disable flow ctl */
396 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
398 struct uslcom_softc *sc = vsc;
407 uslcom_break(void *vsc, int portno, int onoff)
409 struct uslcom_softc *sc = vsc;
410 usb_device_request_t req;
411 int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
413 req.bmRequestType = USLCOM_WRITE;
414 req.bRequest = USLCOM_BREAK;
415 USETW(req.wValue, brk);
416 USETW(req.wIndex, portno);
417 USETW(req.wLength, 0);
418 usbd_do_request(sc->sc_udev, &req, NULL);