1 /** $MirOS: src/sys/dev/usb/uslcom.c,v 1.2 2007/08/10 00:35:57 tg Exp $ */
2 /* $OpenBSD: uslcom.c,v 1.13 2007/06/14 10:11:16 mbalmer Exp $ */
3
4 /*
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6 *
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.
10 *
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.
18 */
19
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
27 #include <dev/usb/usb.h>
28 #include <dev/usb/usbdi.h>
29 #include <dev/usb/usbdi_util.h>
30 #include <dev/usb/usbdevs.h>
31
32 #include <dev/usb/usbdevs.h>
33 #include <dev/usb/ucomvar.h>
34
35 #ifdef USLCOM_DEBUG
36 #define DPRINTFN(n, x) do { if (uslcomdebug > (n)) printf 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
58 #define USLCOM_UART_DISABLE 0x00
59 #define USLCOM_UART_ENABLE 0x01
60
61 #define USLCOM_CTRL_DTR_ON 0x0001
62 #define USLCOM_CTRL_DTR_SET 0x0100
63 #define USLCOM_CTRL_RTS_ON 0x0002
64 #define USLCOM_CTRL_RTS_SET 0x0200
65 #define USLCOM_CTRL_CTS 0x0010
66 #define USLCOM_CTRL_DSR 0x0020
67 #define USLCOM_CTRL_DCD 0x0080
68
69
70 #define USLCOM_BAUD_REF 0x384000
71
72 #define USLCOM_STOP_BITS_1 0x00
73 #define USLCOM_STOP_BITS_2 0x02
74
75 #define USLCOM_PARITY_NONE 0x00
76 #define USLCOM_PARITY_ODD 0x10
77 #define USLCOM_PARITY_EVEN 0x20
78
79 #define USLCOM_BREAK_OFF 0x00
80 #define USLCOM_BREAK_ON 0x01
81
82
83 struct uslcom_softc {
84 struct device sc_dev;
85 usbd_device_handle sc_udev;
86 usbd_interface_handle sc_iface;
87 struct device *sc_subdev;
88
89 u_char sc_msr;
90 u_char sc_lsr;
91
92 u_char sc_dying;
93 };
94
95 void uslcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
96 void uslcom_set(void *, int, int, int);
97 int uslcom_param(void *, int, struct termios *);
98 int uslcom_open(void *sc, int portno);
99 void uslcom_close(void *, int);
100 void uslcom_break(void *sc, int portno, int onoff);
101
102 struct ucom_methods uslcom_methods = {
103 uslcom_get_status,
104 uslcom_set,
105 uslcom_param,
106 NULL,
107 uslcom_open,
108 uslcom_close,
109 NULL,
110 NULL,
111 };
112
113 static const struct usb_devno uslcom_devs[] = {
114 { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER },
115 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
116 { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B },
117 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP },
118 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 },
119 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREECONT },
120 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DESKTOPMOBILE },
121 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_IPLINK1220 },
122 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP },
123 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
124 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN },
125 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU },
126 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_1 },
127 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 },
128 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUNNTO },
129 { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE },
130 { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 }
131 };
132
133 int uslcom_match(struct device *, void *, void *);
134 void uslcom_attach(struct device *, struct device *, void *);
135 int uslcom_detach(struct device *, int);
136 int uslcom_activate(struct device *, enum devact);
137
138 struct cfdriver uslcom_cd = {
139 NULL, "uslcom", DV_DULL
140 };
141
142 const struct cfattach uslcom_ca = {
143 sizeof(struct uslcom_softc),
144 uslcom_match,
145 uslcom_attach,
146 uslcom_detach,
147 uslcom_activate,
148 };
149
150 int
uslcom_match(struct device * parent,void * match,void * aux)151 uslcom_match(struct device *parent, void *match, void *aux)
152 {
153 struct usb_attach_arg *uaa = aux;
154
155 if (uaa->iface != NULL)
156 return UMATCH_NONE;
157
158 return (usb_lookup(uslcom_devs, uaa->vendor, uaa->product) != NULL) ?
159 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
160 }
161
162 void
uslcom_attach(struct device * parent,struct device * self,void * aux)163 uslcom_attach(struct device *parent, struct device *self, void *aux)
164 {
165 struct uslcom_softc *sc = (struct uslcom_softc *)self;
166 struct usb_attach_arg *uaa = aux;
167 struct ucom_attach_args uca;
168 usb_interface_descriptor_t *id;
169 usb_endpoint_descriptor_t *ed;
170 usbd_status error;
171 // char *devinfop;
172 char devinfop[1024];
173 int i;
174
175 bzero(&uca, sizeof(uca));
176 sc->sc_udev = uaa->device;
177 // devinfop = usbd_devinfo_alloc(uaa->device, 0);
178 usbd_devinfo(uaa->device, 0, devinfop, sizeof (devinfop));
179 printf("\n%s: %s\n", sc->sc_dev.dv_xname, devinfop);
180 // usbd_devinfo_free(devinfop);
181
182 if (usbd_set_config_index(sc->sc_udev, USLCOM_CONFIG_NO, 1) != 0) {
183 printf("%s: could not set configuration no\n",
184 sc->sc_dev.dv_xname);
185 sc->sc_dying = 1;
186 return;
187 }
188
189 /* get the first interface handle */
190 error = usbd_device2interface_handle(sc->sc_udev, USLCOM_IFACE_NO,
191 &sc->sc_iface);
192 if (error != 0) {
193 printf("%s: could not get interface handle\n",
194 sc->sc_dev.dv_xname);
195 sc->sc_dying = 1;
196 return;
197 }
198
199 id = usbd_get_interface_descriptor(sc->sc_iface);
200
201 uca.bulkin = uca.bulkout = -1;
202 for (i = 0; i < id->bNumEndpoints; i++) {
203 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
204 if (ed == NULL) {
205 printf("%s: no endpoint descriptor found for %d\n",
206 sc->sc_dev.dv_xname, i);
207 sc->sc_dying = 1;
208 return;
209 }
210
211 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
212 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
213 uca.bulkin = ed->bEndpointAddress;
214 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
215 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
216 uca.bulkout = ed->bEndpointAddress;
217 }
218
219 if (uca.bulkin == -1 || uca.bulkout == -1) {
220 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
221 sc->sc_dying = 1;
222 return;
223 }
224
225 uca.ibufsize = USLCOMBUFSZ;
226 uca.obufsize = USLCOMBUFSZ;
227 uca.ibufsizepad = USLCOMBUFSZ;
228 uca.opkthdrlen = 0;
229 uca.device = sc->sc_udev;
230 uca.iface = sc->sc_iface;
231 uca.methods = &uslcom_methods;
232 uca.arg = sc;
233 uca.info = NULL;
234
235 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
236 &sc->sc_dev);
237
238 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
239 }
240
241 int
uslcom_detach(struct device * self,int flags)242 uslcom_detach(struct device *self, int flags)
243 {
244 struct uslcom_softc *sc = (struct uslcom_softc *)self;
245 int rv = 0;
246
247 sc->sc_dying = 1;
248 if (sc->sc_subdev != NULL) {
249 rv = config_detach(sc->sc_subdev, flags);
250 sc->sc_subdev = NULL;
251 }
252
253 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
254 &sc->sc_dev);
255
256 return (rv);
257 }
258
259 int
uslcom_activate(struct device * self,enum devact act)260 uslcom_activate(struct device *self, enum devact act)
261 {
262 struct uslcom_softc *sc = (struct uslcom_softc *)self;
263 int rv = 0;
264
265 switch (act) {
266 case DVACT_ACTIVATE:
267 break;
268
269 case DVACT_DEACTIVATE:
270 if (sc->sc_subdev != NULL)
271 rv = config_deactivate(sc->sc_subdev);
272 sc->sc_dying = 1;
273 break;
274 }
275 return (rv);
276 }
277
278 int
uslcom_open(void * vsc,int portno)279 uslcom_open(void *vsc, int portno)
280 {
281 struct uslcom_softc *sc = vsc;
282 usb_device_request_t req;
283 usbd_status err;
284
285 if (sc->sc_dying)
286 return (EIO);
287
288 req.bmRequestType = USLCOM_WRITE;
289 req.bRequest = USLCOM_UART;
290 USETW(req.wValue, USLCOM_UART_ENABLE);
291 USETW(req.wIndex, portno);
292 USETW(req.wLength, 0);
293 err = usbd_do_request(sc->sc_udev, &req, NULL);
294 if (err)
295 return (EIO);
296
297 return (0);
298 }
299
300 void
uslcom_close(void * vsc,int portno)301 uslcom_close(void *vsc, int portno)
302 {
303 struct uslcom_softc *sc = vsc;
304 usb_device_request_t req;
305
306 if (sc->sc_dying)
307 return;
308
309 req.bmRequestType = USLCOM_WRITE;
310 req.bRequest = USLCOM_UART;
311 USETW(req.wValue, USLCOM_UART_DISABLE);
312 USETW(req.wIndex, portno);
313 USETW(req.wLength, 0);
314 usbd_do_request(sc->sc_udev, &req, NULL);
315 }
316
317 void
uslcom_set(void * vsc,int portno,int reg,int onoff)318 uslcom_set(void *vsc, int portno, int reg, int onoff)
319 {
320 struct uslcom_softc *sc = vsc;
321 usb_device_request_t req;
322 int ctl;
323
324 switch (reg) {
325 case UCOM_SET_DTR:
326 ctl = onoff ? USLCOM_CTRL_DTR_ON : 0;
327 ctl |= USLCOM_CTRL_DTR_SET;
328 break;
329 case UCOM_SET_RTS:
330 ctl = onoff ? USLCOM_CTRL_RTS_ON : 0;
331 ctl |= USLCOM_CTRL_RTS_SET;
332 break;
333 case UCOM_SET_BREAK:
334 uslcom_break(sc, portno, onoff);
335 return;
336 default:
337 return;
338 }
339 req.bmRequestType = USLCOM_WRITE;
340 req.bRequest = USLCOM_CTRL;
341 USETW(req.wValue, ctl);
342 USETW(req.wIndex, portno);
343 USETW(req.wLength, 0);
344 usbd_do_request(sc->sc_udev, &req, NULL);
345 }
346
347 int
uslcom_param(void * vsc,int portno,struct termios * t)348 uslcom_param(void *vsc, int portno, struct termios *t)
349 {
350 struct uslcom_softc *sc = (struct uslcom_softc *)vsc;
351 usbd_status err;
352 usb_device_request_t req;
353 int data;
354
355 switch (t->c_ospeed) {
356 case 600:
357 case 1200:
358 case 1800:
359 case 2400:
360 case 4800:
361 case 9600:
362 case 19200:
363 case 38400:
364 case 57600:
365 case 115200:
366 case 460800:
367 case 921600:
368 req.bmRequestType = USLCOM_WRITE;
369 req.bRequest = USLCOM_BAUD_RATE;
370 USETW(req.wValue, USLCOM_BAUD_REF / t->c_ospeed);
371 USETW(req.wIndex, portno);
372 USETW(req.wLength, 0);
373 err = usbd_do_request(sc->sc_udev, &req, NULL);
374 if (err)
375 return (EIO);
376 break;
377 default:
378 return (EINVAL);
379 }
380
381 if (ISSET(t->c_cflag, CSTOPB))
382 data = USLCOM_STOP_BITS_2;
383 else
384 data = USLCOM_STOP_BITS_1;
385 if (ISSET(t->c_cflag, PARENB)) {
386 if (ISSET(t->c_cflag, PARODD))
387 data |= USLCOM_PARITY_ODD;
388 else
389 data |= USLCOM_PARITY_EVEN;
390 } else
391 data |= USLCOM_PARITY_NONE;
392 switch (ISSET(t->c_cflag, CSIZE)) {
393 case CS5:
394 data |= USLCOM_SET_DATA_BITS(5);
395 break;
396 case CS6:
397 data |= USLCOM_SET_DATA_BITS(6);
398 break;
399 case CS7:
400 data |= USLCOM_SET_DATA_BITS(7);
401 break;
402 case CS8:
403 data |= USLCOM_SET_DATA_BITS(8);
404 break;
405 }
406
407 req.bmRequestType = USLCOM_WRITE;
408 req.bRequest = USLCOM_DATA;
409 USETW(req.wValue, data);
410 USETW(req.wIndex, portno);
411 USETW(req.wLength, 0);
412 err = usbd_do_request(sc->sc_udev, &req, NULL);
413 if (err)
414 return (EIO);
415
416 #if 0
417 /* XXX flow control */
418 if (ISSET(t->c_cflag, CRTSCTS))
419 /* rts/cts flow ctl */
420 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
421 /* xon/xoff flow ctl */
422 } else {
423 /* disable flow ctl */
424 }
425 #endif
426
427 return (0);
428 }
429
430 void
uslcom_get_status(void * vsc,int portno,u_char * lsr,u_char * msr)431 uslcom_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
432 {
433 struct uslcom_softc *sc = vsc;
434
435 if (msr != NULL)
436 *msr = sc->sc_msr;
437 if (lsr != NULL)
438 *lsr = sc->sc_lsr;
439 }
440
441 void
uslcom_break(void * vsc,int portno,int onoff)442 uslcom_break(void *vsc, int portno, int onoff)
443 {
444 struct uslcom_softc *sc = vsc;
445 usb_device_request_t req;
446 int brk = onoff ? USLCOM_BREAK_ON : USLCOM_BREAK_OFF;
447
448 req.bmRequestType = USLCOM_WRITE;
449 req.bRequest = USLCOM_BREAK;
450 USETW(req.wValue, brk);
451 USETW(req.wIndex, portno);
452 USETW(req.wLength, 0);
453 usbd_do_request(sc->sc_udev, &req, NULL);
454 }
455