uslsa.c revision 1.12.8.1 1 /* $NetBSD: uslsa.c,v 1.12.8.1 2012/04/17 00:08:10 yamt Exp $ */
2
3 /* from ugensa.c */
4
5 /*
6 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Roland C. Dowdeswell <elric (at) netbsd.org>.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 2007, 2009 Jonathan A. Kollasch.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 *
58 */
59
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.12.8.1 2012/04/17 00:08:10 yamt Exp $");
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/device.h>
67 #include <sys/conf.h>
68 #include <sys/tty.h>
69
70 #include <dev/usb/usb.h>
71
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include <dev/usb/usbdevs.h>
75
76 #include <dev/usb/ucomvar.h>
77
78 #include <dev/usb/uslsareg.h>
79
80 #include <fs/unicode.h>
81
82 #ifdef USLSA_DEBUG
83 #define DPRINTF(x) if (uslsadebug) device_printf x
84 int uslsadebug = 0;
85 #else
86 #define DPRINTF(x)
87 #endif
88
89 struct uslsa_softc {
90 device_t sc_dev; /* base device */
91 device_t sc_subdev; /* ucom device */
92 usbd_device_handle sc_udev; /* usb device */
93 usbd_interface_handle sc_iface; /* interface */
94 uint8_t sc_ifnum; /* interface number */
95 bool sc_dying; /* disconnecting */
96 };
97
98 static void uslsa_get_status(void *sc, int, u_char *, u_char *);
99 static void uslsa_set(void *, int, int, int);
100 static int uslsa_param(void *, int, struct termios *);
101 static int uslsa_ioctl(void *, int, u_long, void *, int, proc_t *);
102
103 static int uslsa_open(void *, int);
104 static void uslsa_close(void *, int);
105
106 static int uslsa_usbd_errno(usbd_status);
107 static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t);
108 static int uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t);
109
110 static const struct ucom_methods uslsa_methods = {
111 uslsa_get_status,
112 uslsa_set,
113 uslsa_param,
114 uslsa_ioctl,
115 uslsa_open,
116 uslsa_close,
117 NULL,
118 NULL,
119 };
120
121 #define USLSA_CONFIG_INDEX 0
122 #define USLSA_IFACE_INDEX 0
123 #define USLSA_BUFSIZE 256
124
125 static const struct usb_devno uslsa_devs[] = {
126 { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER },
127 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
128 { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B },
129 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP },
130 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 },
131 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREECONT },
132 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DESKTOPMOBILE },
133 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_IPLINK1220 },
134 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP },
135 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
136 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN },
137 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU },
138 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_1 },
139 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 },
140 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUNNTO },
141 { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE },
142 { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 }
143 };
144
145 static int uslsa_match(device_t, cfdata_t, void *);
146 static void uslsa_attach(device_t, device_t, void *);
147 static void uslsa_childdet(device_t, device_t);
148 static int uslsa_detach(device_t, int);
149 static int uslsa_activate(device_t, enum devact);
150
151 CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
152 uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet);
153
154 static int
155 uslsa_match(device_t parent, cfdata_t match, void *aux)
156 {
157 const struct usbif_attach_arg *uaa;
158
159 uaa = aux;
160
161 if (usb_lookup(uslsa_devs, uaa->vendor, uaa->product) != NULL) {
162 return UMATCH_VENDOR_PRODUCT;
163 } else {
164 return UMATCH_NONE;
165 }
166 }
167
168 static void
169 uslsa_attach(device_t parent, device_t self, void *aux)
170 {
171 struct uslsa_softc *sc;
172 const struct usbif_attach_arg *uaa;
173 const usb_interface_descriptor_t *id;
174 const usb_endpoint_descriptor_t *ed;
175 char *devinfop;
176 struct ucom_attach_args uca;
177 int i;
178
179 sc = device_private(self);
180 uaa = aux;
181
182 sc->sc_dev = self;
183 sc->sc_udev = uaa->device;
184 sc->sc_iface = uaa->iface;
185
186 aprint_naive("\n");
187 aprint_normal("\n");
188
189 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
190 aprint_normal_dev(self, "%s\n", devinfop);
191 usbd_devinfo_free(devinfop);
192
193 id = usbd_get_interface_descriptor(sc->sc_iface);
194
195 sc->sc_ifnum = id->bInterfaceNumber;
196
197 uca.info = "Silicon Labs CP210x";
198 uca.portno = UCOM_UNK_PORTNO;
199 uca.ibufsize = USLSA_BUFSIZE;
200 uca.obufsize = USLSA_BUFSIZE;
201 uca.ibufsizepad = USLSA_BUFSIZE;
202 uca.opkthdrlen = 0;
203 uca.device = sc->sc_udev;
204 uca.iface = sc->sc_iface;
205 uca.methods = &uslsa_methods;
206 uca.arg = sc;
207
208 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
209 sc->sc_dev);
210
211 uca.bulkin = uca.bulkout = -1;
212 for (i = 0; i < id->bNumEndpoints; i++) {
213 int addr, dir, attr;
214
215 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
216 if (ed == NULL) {
217 aprint_error_dev(self,
218 "could not read endpoint descriptor\n");
219 sc->sc_dying = true;
220 return;
221 }
222 addr = ed->bEndpointAddress;
223 dir = UE_GET_DIR(ed->bEndpointAddress);
224 attr = ed->bmAttributes & UE_XFERTYPE;
225 if (dir == UE_DIR_IN && attr == UE_BULK) {
226 uca.bulkin = addr;
227 } else if (dir == UE_DIR_OUT && attr == UE_BULK) {
228 uca.bulkout = addr;
229 } else {
230 aprint_error_dev(self, "unexpected endpoint\n");
231 }
232 }
233 aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n",
234 uca.bulkin, uca.bulkout);
235 if ((uca.bulkin == -1) || (uca.bulkout == -1)) {
236 aprint_error_dev(self, "could not find endpoints\n");
237 sc->sc_dying = true;
238 return;
239 }
240
241 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
242 ucomprint, ucomsubmatch);
243
244 return;
245 }
246
247 static int
248 uslsa_activate(device_t self, enum devact act)
249 {
250 struct uslsa_softc *sc = device_private(self);
251
252 switch (act) {
253 case DVACT_DEACTIVATE:
254 sc->sc_dying = true;
255 return 0;
256 default:
257 return EOPNOTSUPP;
258 }
259 }
260
261 static void
262 uslsa_childdet(device_t self, device_t child)
263 {
264 struct uslsa_softc *sc = device_private(self);
265
266 KASSERT(sc->sc_subdev == child);
267 sc->sc_subdev = NULL;
268 }
269
270 static int
271 uslsa_detach(device_t self, int flags)
272 {
273 struct uslsa_softc *sc = device_private(self);
274 int rv = 0;
275
276 DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags));
277
278 sc->sc_dying = true;
279
280 if (sc->sc_subdev != NULL) {
281 rv = config_detach(sc->sc_subdev, flags);
282 }
283
284 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
285 sc->sc_dev);
286
287 return (rv);
288 }
289
290 static int
291 uslsa_usbd_errno(usbd_status status)
292 {
293 switch (status) {
294 case USBD_NORMAL_COMPLETION:
295 return 0;
296 case USBD_STALLED:
297 return EINVAL;
298 default:
299 return EIO;
300 }
301 }
302
303 static void
304 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
305 {
306 struct uslsa_softc *sc;
307 usb_device_request_t req;
308 usbd_status status;
309 uint8_t mdmsts;
310
311 sc = vsc;
312
313 DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno));
314
315 if (sc->sc_dying) {
316 return;
317 }
318
319 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
320 req.bRequest = SLSA_R_GET_MDMSTS;
321 USETW(req.wValue, 0);
322 USETW(req.wIndex, sc->sc_ifnum);
323 USETW(req.wLength, SLSA_RL_GET_MDMSTS);
324
325 status = usbd_do_request(sc->sc_udev, &req, &mdmsts);
326 if (status != USBD_NORMAL_COMPLETION) {
327 device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n",
328 __func__, usbd_errstr(status));
329 return;
330 }
331
332 DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts));
333
334 if (lsr != NULL) {
335 *lsr = 0;
336 }
337
338 if (msr != NULL) {
339 *msr = 0;
340 *msr |= ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0;
341 *msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0;
342 *msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0;
343 *msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0;
344 }
345 }
346
347 static void
348 uslsa_set(void *vsc, int portno, int reg, int onoff)
349 {
350 struct uslsa_softc *sc;
351
352 sc = vsc;
353
354 DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno, reg, onoff));
355
356 if (sc->sc_dying) {
357 return;
358 }
359
360 switch (reg) {
361 case UCOM_SET_DTR:
362 if (uslsa_request_set(sc, SLSA_R_SET_MHS,
363 SLSA_RV_SET_MHS_DTR_MASK |
364 (onoff ? SLSA_RV_SET_MHS_DTR : 0))) {
365 device_printf(sc->sc_dev, "SET_MHS/DTR failed\n");
366 }
367 break;
368 case UCOM_SET_RTS:
369 if (uslsa_request_set(sc, SLSA_R_SET_MHS,
370 SLSA_RV_SET_MHS_RTS_MASK |
371 (onoff ? SLSA_RV_SET_MHS_RTS : 0))) {
372 device_printf(sc->sc_dev, "SET_MHS/RTS failed\n");
373 }
374 break;
375 case UCOM_SET_BREAK:
376 if (uslsa_request_set(sc, SLSA_R_SET_BREAK,
377 (onoff ? SLSA_RV_SET_BREAK_ENABLE :
378 SLSA_RV_SET_BREAK_DISABLE))) {
379 device_printf(sc->sc_dev, "SET_BREAK failed\n");
380 }
381 break;
382 default:
383 break;
384 }
385 }
386
387 static int
388 uslsa_param(void *vsc, int portno, struct termios *t)
389 {
390 struct uslsa_softc *sc;
391 int ret;
392 uint16_t value;
393
394 sc = vsc;
395
396 DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t));
397
398 if (sc->sc_dying) {
399 return EIO;
400 }
401
402 value = SLSA_RV_BAUDDIV(t->c_ospeed);
403
404 if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value)) != 0) {
405 device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n",
406 __func__);
407 return ret;
408 }
409
410 value = 0;
411
412 if (ISSET(t->c_cflag, CSTOPB)) {
413 value |= SLSA_RV_LINE_CTL_STOP_2;
414 } else {
415 value |= SLSA_RV_LINE_CTL_STOP_1;
416 }
417
418 if (ISSET(t->c_cflag, PARENB)) {
419 if (ISSET(t->c_cflag, PARODD)) {
420 value |= SLSA_RV_LINE_CTL_PARITY_ODD;
421 } else {
422 value |= SLSA_RV_LINE_CTL_PARITY_EVEN;
423 }
424 } else {
425 value |= SLSA_RV_LINE_CTL_PARITY_NONE;
426 }
427
428 switch (ISSET(t->c_cflag, CSIZE)) {
429 case CS5:
430 value |= SLSA_RV_LINE_CTL_LEN_5;
431 break;
432 case CS6:
433 value |= SLSA_RV_LINE_CTL_LEN_6;
434 break;
435 case CS7:
436 value |= SLSA_RV_LINE_CTL_LEN_7;
437 break;
438 case CS8:
439 value |= SLSA_RV_LINE_CTL_LEN_8;
440 break;
441 }
442
443 DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to 0x%x\n",
444 __func__, value));
445 if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) {
446 device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
447 return ret;
448 }
449
450 if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) {
451 device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
452 }
453
454 return ret;
455 }
456
457 static int
458 uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p)
459 {
460 struct uslsa_softc *sc;
461
462 sc = vsc;
463
464 switch (cmd) {
465 case TIOCMGET:
466 ucom_status_change(device_private(sc->sc_subdev));
467 return EPASSTHROUGH;
468 default:
469 return EPASSTHROUGH;
470 }
471
472 return 0;
473 }
474
475 static int
476 uslsa_open(void *vsc, int portno)
477 {
478 struct uslsa_softc *sc;
479
480 sc = vsc;
481
482 DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
483
484 if (sc->sc_dying) {
485 return EIO;
486 }
487
488 return uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
489 SLSA_RV_IFC_ENABLE_ENABLE);
490 }
491
492 static void
493 uslsa_close(void *vsc, int portno)
494 {
495 struct uslsa_softc *sc;
496
497 sc = vsc;
498
499 DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
500
501 if (sc->sc_dying) {
502 return;
503 }
504
505 (void)uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
506 SLSA_RV_IFC_ENABLE_DISABLE);
507 }
508
509 static int
510 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
511 {
512 usb_device_request_t req;
513 usbd_status status;
514
515 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
516 req.bRequest = request;
517 USETW(req.wValue, value);
518 USETW(req.wIndex, sc->sc_ifnum);
519 USETW(req.wLength, 0);
520
521 status = usbd_do_request(sc->sc_udev, &req, NULL);
522
523 return uslsa_usbd_errno(status);
524 }
525
526 static int
527 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
528 {
529 struct slsa_fcs fcs;
530 usb_device_request_t req;
531 uint32_t ulControlHandshake;
532 uint32_t ulFlowReplace;
533 usbd_status status;
534
535 DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag));
536
537 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
538 req.bRequest = SLSA_R_GET_FLOW;
539 USETW(req.wValue, 0);
540 USETW(req.wIndex, sc->sc_ifnum);
541 USETW(req.wLength, SLSA_RL_GET_FLOW);
542
543 status = usbd_do_request(sc->sc_udev, &req, &fcs);
544 if (status != USBD_NORMAL_COMPLETION) {
545 device_printf(sc->sc_dev, "%s: GET_FLOW %s\n",
546 __func__, usbd_errstr(status));
547 return uslsa_usbd_errno(status);
548 }
549
550 ulControlHandshake = le32toh(fcs.ulControlHandshake);
551 ulFlowReplace = le32toh(fcs.ulFlowReplace);
552
553 if (ISSET(cflag, CRTSCTS)) {
554 ulControlHandshake =
555 SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK);
556 ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK);
557 } else {
558 ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK);
559 ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK);
560 }
561
562 fcs.ulControlHandshake = htole32(ulControlHandshake);
563 fcs.ulFlowReplace = htole32(ulFlowReplace);
564
565 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
566 req.bRequest = SLSA_R_SET_FLOW;
567 USETW(req.wValue, 0);
568 USETW(req.wIndex, sc->sc_ifnum);
569 USETW(req.wLength, SLSA_RL_SET_FLOW);
570
571 status = usbd_do_request(sc->sc_udev, &req, &fcs);
572
573 return uslsa_usbd_errno(status);
574 }
575