1 /* $OpenBSD: uslcom.c,v 1.17 2007/11/24 10:52:12 jsg Exp $ */
2
3 /*
4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
5 *
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.
9 *
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.
17 */
18
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/kernel.h>
22 #include <sys/conf.h>
23 #include <sys/tty.h>
24 #include <sys/device.h>
25 #include <sys/types.h>
26 #include <sys/bus.h>
27 #include <sys/module.h>
28
29 #include <bus/usb/usb.h>
30 #include <bus/usb/usbdi.h>
31 #include <bus/usb/usbdi_util.h>
32
33 #include <dev/usbmisc/ucom/ucomvar.h>
34
35 #ifdef USLCOM_DEBUG
36 #define DPRINTFN(n, x) do { if (uslcomdebug > (n)) kprintf x; } while (0)
37 int uslcomdebug = 0;
38 #else
39 #define DPRINTFN(n, x)
40 #endif
41 #define DPRINTF(x) DPRINTFN(0, x)
42
43 #define USLCOMBUFSZ 256
44 #define USLCOM_CONFIG_NO 0
45 #define USLCOM_IFACE_NO 0
46
47 #define USLCOM_SET_DATA_BITS(x) (x << 8)
48
49 #define USLCOM_WRITE 0x41
50 #define USLCOM_READ 0xc1
51
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
58
59 #define USLCOM_UART_DISABLE 0x00
60 #define USLCOM_UART_ENABLE 0x01
61
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
69
70
71 #define USLCOM_BAUD_REF 0x384000
72
73 #define USLCOM_STOP_BITS_1 0x00
74 #define USLCOM_STOP_BITS_2 0x02
75
76 #define USLCOM_PARITY_NONE 0x00
77 #define USLCOM_PARITY_ODD 0x10
78 #define USLCOM_PARITY_EVEN 0x20
79
80 #define USLCOM_BREAK_OFF 0x00
81 #define USLCOM_BREAK_ON 0x01
82
83
84 struct uslcom_softc {
85 struct ucom_softc sc_ucom;
86 u_char sc_msr;
87 u_char sc_lsr;
88 };
89
90 static void uslcom_get_status(void *, int portno, u_char *lsr,
91 u_char *msr);
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,
98 tcflag_t iflag);
99
100 struct ucom_callback uslcom_callback = {
101 uslcom_get_status,
102 uslcom_set,
103 uslcom_param,
104 NULL,
105 uslcom_open,
106 uslcom_close,
107 NULL,
108 NULL,
109 };
110
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 */
135 };
136
137 static device_probe_t uslcom_match;
138 static device_attach_t uslcom_attach;
139 static device_detach_t uslcom_detach;
140
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),
146 DEVMETHOD_END
147 };
148
149 static driver_t uslcom_driver = {
150 "ucom",
151 uslcom_methods,
152 sizeof (struct uslcom_softc)
153 };
154
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);
159
160 static int
161 uslcom_match(device_t self)
162 {
163 struct usb_attach_arg *uaa = device_get_ivars(self);
164
165 if (uaa->iface != NULL)
166 return UMATCH_NONE;
167
168 return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
169 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
170 }
171
172 static int
173 uslcom_attach(device_t self)
174 {
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;
180 usbd_status error;
181 int i;
182
183 ucom = &sc->sc_ucom;
184
185 bzero(sc, sizeof (struct uslcom_softc));
186
187 ucom->sc_dev = self;
188 ucom->sc_udev = uaa->device;
189 ucom->sc_iface = uaa->iface;
190
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");
193 goto error;
194 }
195
196 /* get the first interface handle */
197 error = usbd_device2interface_handle(ucom->sc_udev, USLCOM_IFACE_NO,
198 &ucom->sc_iface);
199 if (error != 0) {
200 device_printf(ucom->sc_dev, "could not get interface handle\n");
201 goto error;
202 }
203
204 id = usbd_get_interface_descriptor(ucom->sc_iface);
205
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);
209 if (ed == NULL) {
210 device_printf(ucom->sc_dev, "no endpoint descriptor "
211 "found for %d\n", i);
212 goto error;
213 }
214
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;
221 }
222
223 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
224 device_printf(ucom->sc_dev, "missing endpoint\n");
225 goto error;
226 }
227
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;
235
236 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
237 ucom->sc_dev);
238
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));
241
242 ucom_attach(&sc->sc_ucom);
243 return 0;
244
245 error:
246 ucom->sc_dying = 1;
247 return ENXIO;
248 }
249
250 static int
251 uslcom_detach(device_t self)
252 {
253 struct uslcom_softc *sc = device_get_softc(self);
254 int rv = 0;
255
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,
260 sc->sc_ucom.sc_dev);
261
262 return (rv);
263 }
264
265 #if 0 /* not yet */
266 int
267 uslcom_activate(struct device *self, enum devact act)
268 {
269 struct uslcom_softc *sc = (struct uslcom_softc *)self;
270 int rv = 0;
271
272 switch (act) {
273 case DVACT_ACTIVATE:
274 break;
275
276 case DVACT_DEACTIVATE:
277 if (sc->sc_subdev != NULL)
278 rv = config_deactivate(sc->sc_subdev);
279 sc->sc_dying = 1;
280 break;
281 }
282 return (rv);
283 }
284 #endif
285
286 static int
287 uslcom_open(void *vsc, int portno)
288 {
289 struct uslcom_softc *sc = vsc;
290 usb_device_request_t req;
291 usbd_status err;
292
293 if (sc->sc_ucom.sc_dying)
294 return (EIO);
295
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);
302 if (err)
303 return (EIO);
304
305 return (0);
306 }
307
308 static void
309 uslcom_close(void *vsc, int portno)
310 {
311 struct uslcom_softc *sc = vsc;
312 usb_device_request_t req;
313
314 if (sc->sc_ucom.sc_dying)
315 return;
316
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);
323 }
324
325 static void
326 uslcom_set(void *vsc, int portno, int reg, int onoff)
327 {
328 struct uslcom_softc *sc = vsc;
329 usb_device_request_t req;
330 int ctl;
331
332 switch (reg) {
333 case UCOM_SET_DTR:
334 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
335 ctl |= USLCOM_CTRL_DTR_SET;
336 break;
337 case UCOM_SET_RTS:
338 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
339 ctl |= USLCOM_CTRL_RTS_SET;
340 break;
341 case UCOM_SET_BREAK:
342 uslcom_break(sc, portno, onoff);
343 return;
344 default:
345 return;
346 }
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);
353 }
354
355 static int
356 uslcom_param(void *vsc, int portno, struct termios *t)
357 {
358 struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
359 usbd_status err;
360 usb_device_request_t req;
361 int data;
362
363 if (t->c_ospeed <= 0 || t->c_ospeed > 921600)
364 return (EINVAL);
365
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);
372 if (err)
373 return (EIO);
374
375 if (ISSET(t->c_cflag, CSTOPB))
376 data = USLCOM_STOP_BITS_2;
377 else
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;
382 else
383 data |= USLCOM_PARITY_EVEN;
384 } else
385 data |= USLCOM_PARITY_NONE;
386 switch (ISSET(t->c_cflag, CSIZE)) {
387 case CS5:
388 data |= USLCOM_SET_DATA_BITS(5);
389 break;
390 case CS6:
391 data |= USLCOM_SET_DATA_BITS(6);
392 break;
393 case CS7:
394 data |= USLCOM_SET_DATA_BITS(7);
395 break;
396 case CS8:
397 data |= USLCOM_SET_DATA_BITS(8);
398 break;
399 }
400
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);
407 if (err)
408 return (EIO);
409
410 uslcom_set_flow_ctrl(sc, t->c_cflag, t->c_iflag);
411
412 return (0);
413 }
414
415 static void
416 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
417 {
418 struct uslcom_softc *sc = vsc;
419
420 if (msr != NULL)
421 *msr = sc->sc_msr;
422 if (lsr != NULL)
423 *lsr = sc->sc_lsr;
424 }
425
426 static void
427 uslcom_break(void *vsc, int portno, int onoff)
428 {
429 struct uslcom_softc *sc = vsc;
430 usb_device_request_t req;
431 int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
432
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);
439 }
440
441 static void
442 uslcom_set_flow_ctrl(struct uslcom_softc *sc, tcflag_t cflag, tcflag_t iflag)
443 {
444 uint8_t modemdata[16];
445 usb_device_request_t req;
446 usbd_status err;
447
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);
453
454 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
455 if (err)
456 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
457 usbd_errstr(err));
458
459 if (ISSET(cflag, CRTSCTS)) {
460 modemdata[0] &= ~0x7b;
461 modemdata[0] |= 0x09;
462 modemdata[4] = 0x80;
463 } else {
464 modemdata[0] &= ~0x7b;
465 modemdata[0] |= 0x01;
466 modemdata[4] = 0x40;
467 }
468
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);
474
475 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, modemdata);
476 if (err)
477 device_printf(sc->sc_ucom.sc_dev, "uslcom_set_flow: %s\n",
478 usbd_errstr(err));
479 }
480
Cache object: 1268079b9ea8978a6a71ad1328e00922
|