uslsa.c revision 1.17 1 /* $NetBSD: uslsa.c,v 1.17 2012/01/14 21:06:01 jakllsch 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.17 2012/01/14 21:06:01 jakllsch 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 void uslsa_props(struct uslsa_softc *);
111
112 static const struct ucom_methods uslsa_methods = {
113 uslsa_get_status,
114 uslsa_set,
115 uslsa_param,
116 uslsa_ioctl,
117 uslsa_open,
118 uslsa_close,
119 NULL,
120 NULL,
121 };
122
123 #define USLSA_CONFIG_INDEX 0
124 #define USLSA_IFACE_INDEX 0
125 #define USLSA_BUFSIZE 256
126
127 static const struct usb_devno uslsa_devs[] = {
128 { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER },
129 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
130 { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B },
131 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP },
132 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 },
133 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREECONT },
134 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DESKTOPMOBILE },
135 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_IPLINK1220 },
136 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP },
137 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
138 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN },
139 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU },
140 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_1 },
141 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 },
142 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUNNTO },
143 { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE },
144 { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 }
145 };
146
147 static int uslsa_match(device_t, cfdata_t, void *);
148 static void uslsa_attach(device_t, device_t, void *);
149 static void uslsa_childdet(device_t, device_t);
150 static int uslsa_detach(device_t, int);
151 static int uslsa_activate(device_t, enum devact);
152
153 CFATTACH_DECL2_NEW(uslsa, sizeof(struct uslsa_softc), uslsa_match,
154 uslsa_attach, uslsa_detach, uslsa_activate, NULL, uslsa_childdet);
155
156 static int
157 uslsa_match(device_t parent, cfdata_t match, void *aux)
158 {
159 const struct usbif_attach_arg *uaa;
160
161 uaa = aux;
162
163 if (usb_lookup(uslsa_devs, uaa->vendor, uaa->product) != NULL) {
164 return UMATCH_VENDOR_PRODUCT;
165 } else {
166 return UMATCH_NONE;
167 }
168 }
169
170 static void
171 uslsa_attach(device_t parent, device_t self, void *aux)
172 {
173 struct uslsa_softc *sc;
174 const struct usbif_attach_arg *uaa;
175 const usb_interface_descriptor_t *id;
176 const usb_endpoint_descriptor_t *ed;
177 char *devinfop;
178 struct ucom_attach_args uca;
179 int i;
180
181 sc = device_private(self);
182 uaa = aux;
183
184 sc->sc_dev = self;
185 sc->sc_udev = uaa->device;
186 sc->sc_iface = uaa->iface;
187
188 aprint_naive("\n");
189 aprint_normal("\n");
190
191 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
192 aprint_normal_dev(self, "%s\n", devinfop);
193 usbd_devinfo_free(devinfop);
194
195 id = usbd_get_interface_descriptor(sc->sc_iface);
196
197 sc->sc_ifnum = id->bInterfaceNumber;
198
199 uca.info = "Silicon Labs CP210x";
200 uca.portno = UCOM_UNK_PORTNO;
201 uca.ibufsize = USLSA_BUFSIZE;
202 uca.obufsize = USLSA_BUFSIZE;
203 uca.ibufsizepad = USLSA_BUFSIZE;
204 uca.opkthdrlen = 0;
205 uca.device = sc->sc_udev;
206 uca.iface = sc->sc_iface;
207 uca.methods = &uslsa_methods;
208 uca.arg = sc;
209
210 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
211 sc->sc_dev);
212
213 uca.bulkin = uca.bulkout = -1;
214 for (i = 0; i < id->bNumEndpoints; i++) {
215 int addr, dir, attr;
216
217 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
218 if (ed == NULL) {
219 aprint_error_dev(self,
220 "could not read endpoint descriptor\n");
221 sc->sc_dying = true;
222 return;
223 }
224 addr = ed->bEndpointAddress;
225 dir = UE_GET_DIR(ed->bEndpointAddress);
226 attr = ed->bmAttributes & UE_XFERTYPE;
227 if (dir == UE_DIR_IN && attr == UE_BULK) {
228 uca.bulkin = addr;
229 } else if (dir == UE_DIR_OUT && attr == UE_BULK) {
230 uca.bulkout = addr;
231 } else {
232 aprint_error_dev(self, "unexpected endpoint\n");
233 }
234 }
235 aprint_debug_dev(sc->sc_dev, "EPs: in=%#x out=%#x\n",
236 uca.bulkin, uca.bulkout);
237 if ((uca.bulkin == -1) || (uca.bulkout == -1)) {
238 aprint_error_dev(self, "could not find endpoints\n");
239 sc->sc_dying = true;
240 return;
241 }
242
243 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
244 ucomprint, ucomsubmatch);
245
246 return;
247 }
248
249 static int
250 uslsa_activate(device_t self, enum devact act)
251 {
252 struct uslsa_softc *sc = device_private(self);
253
254 switch (act) {
255 case DVACT_DEACTIVATE:
256 sc->sc_dying = true;
257 return 0;
258 default:
259 return EOPNOTSUPP;
260 }
261 }
262
263 static void
264 uslsa_childdet(device_t self, device_t child)
265 {
266 struct uslsa_softc *sc = device_private(self);
267
268 KASSERT(sc->sc_subdev == child);
269 sc->sc_subdev = NULL;
270 }
271
272 static int
273 uslsa_detach(device_t self, int flags)
274 {
275 struct uslsa_softc *sc = device_private(self);
276 int rv = 0;
277
278 DPRINTF((self, "%s(%p, %#x)\n", __func__, self, flags));
279
280 sc->sc_dying = true;
281
282 if (sc->sc_subdev != NULL) {
283 rv = config_detach(sc->sc_subdev, flags);
284 }
285
286 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
287 sc->sc_dev);
288
289 return (rv);
290 }
291
292 static int
293 uslsa_usbd_errno(usbd_status status)
294 {
295 switch (status) {
296 case USBD_NORMAL_COMPLETION:
297 return 0;
298 case USBD_STALLED:
299 return EINVAL;
300 default:
301 return EIO;
302 }
303 }
304
305 static void
306 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
307 {
308 struct uslsa_softc *sc;
309 usb_device_request_t req;
310 usbd_status status;
311 uint8_t mdmsts;
312
313 sc = vsc;
314
315 DPRINTF((sc->sc_dev, "%s(%p, %d, ....)\n", __func__, vsc, portno));
316
317 if (sc->sc_dying) {
318 return;
319 }
320
321 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
322 req.bRequest = SLSA_R_GET_MDMSTS;
323 USETW(req.wValue, 0);
324 USETW(req.wIndex, sc->sc_ifnum);
325 USETW(req.wLength, SLSA_RL_GET_MDMSTS);
326
327 status = usbd_do_request(sc->sc_udev, &req, &mdmsts);
328 if (status != USBD_NORMAL_COMPLETION) {
329 device_printf(sc->sc_dev, "%s: GET_MDMSTS %s\n",
330 __func__, usbd_errstr(status));
331 return;
332 }
333
334 DPRINTF((sc->sc_dev, "%s: GET_MDMSTS %#x\n", __func__, mdmsts));
335
336 if (lsr != NULL) {
337 *lsr = 0;
338 }
339
340 if (msr != NULL) {
341 *msr = 0;
342 *msr |= ISSET(mdmsts, SLSA_MDMSTS_CTS) ? UMSR_CTS : 0;
343 *msr |= ISSET(mdmsts, SLSA_MDMSTS_DSR) ? UMSR_DSR : 0;
344 *msr |= ISSET(mdmsts, SLSA_MDMSTS_RI) ? UMSR_RI : 0;
345 *msr |= ISSET(mdmsts, SLSA_MDMSTS_DCD) ? UMSR_DCD : 0;
346 }
347 }
348
349 static void
350 uslsa_set(void *vsc, int portno, int reg, int onoff)
351 {
352 struct uslsa_softc *sc;
353
354 sc = vsc;
355
356 DPRINTF((sc->sc_dev, "%s(%p, %d, %d, %d)\n", __func__, vsc, portno, reg, onoff));
357
358 if (sc->sc_dying) {
359 return;
360 }
361
362 switch (reg) {
363 case UCOM_SET_DTR:
364 if (uslsa_request_set(sc, SLSA_R_SET_MHS,
365 SLSA_RV_SET_MHS_DTR_MASK |
366 (onoff ? SLSA_RV_SET_MHS_DTR : 0))) {
367 device_printf(sc->sc_dev, "SET_MHS/DTR failed\n");
368 }
369 break;
370 case UCOM_SET_RTS:
371 if (uslsa_request_set(sc, SLSA_R_SET_MHS,
372 SLSA_RV_SET_MHS_RTS_MASK |
373 (onoff ? SLSA_RV_SET_MHS_RTS : 0))) {
374 device_printf(sc->sc_dev, "SET_MHS/RTS failed\n");
375 }
376 break;
377 case UCOM_SET_BREAK:
378 if (uslsa_request_set(sc, SLSA_R_SET_BREAK,
379 (onoff ? SLSA_RV_SET_BREAK_ENABLE :
380 SLSA_RV_SET_BREAK_DISABLE))) {
381 device_printf(sc->sc_dev, "SET_BREAK failed\n");
382 }
383 break;
384 default:
385 break;
386 }
387 }
388
389 static int
390 uslsa_param(void *vsc, int portno, struct termios *t)
391 {
392 struct uslsa_softc *sc;
393 int ret;
394 uint16_t value;
395
396 sc = vsc;
397
398 DPRINTF((sc->sc_dev, "%s(%p, %d, %p)\n", __func__, vsc, portno, t));
399
400 if (sc->sc_dying) {
401 return EIO;
402 }
403
404 value = SLSA_RV_BAUDDIV(t->c_ospeed);
405
406 if ((ret = uslsa_request_set(sc, SLSA_R_SET_BAUDDIV, value)) != 0) {
407 device_printf(sc->sc_dev, "%s: SET_BAUDDIV failed\n",
408 __func__);
409 return ret;
410 }
411
412 value = 0;
413
414 if (ISSET(t->c_cflag, CSTOPB)) {
415 value |= SLSA_RV_LINE_CTL_STOP_2;
416 } else {
417 value |= SLSA_RV_LINE_CTL_STOP_1;
418 }
419
420 if (ISSET(t->c_cflag, PARENB)) {
421 if (ISSET(t->c_cflag, PARODD)) {
422 value |= SLSA_RV_LINE_CTL_PARITY_ODD;
423 } else {
424 value |= SLSA_RV_LINE_CTL_PARITY_EVEN;
425 }
426 } else {
427 value |= SLSA_RV_LINE_CTL_PARITY_NONE;
428 }
429
430 switch (ISSET(t->c_cflag, CSIZE)) {
431 case CS5:
432 value |= SLSA_RV_LINE_CTL_LEN_5;
433 break;
434 case CS6:
435 value |= SLSA_RV_LINE_CTL_LEN_6;
436 break;
437 case CS7:
438 value |= SLSA_RV_LINE_CTL_LEN_7;
439 break;
440 case CS8:
441 value |= SLSA_RV_LINE_CTL_LEN_8;
442 break;
443 }
444
445 DPRINTF((sc->sc_dev, "%s: setting LINE_CTL to 0x%x\n",
446 __func__, value));
447 if ((ret = uslsa_request_set(sc, SLSA_R_SET_LINE_CTL, value)) != 0) {
448 device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
449 return ret;
450 }
451
452 if ((ret = uslsa_set_flow(sc, t->c_cflag, t->c_iflag)) != 0) {
453 device_printf(sc->sc_dev, "SET_LINE_CTL failed\n");
454 }
455
456 return ret;
457 }
458
459 static int
460 uslsa_ioctl(void *vsc, int portno, u_long cmd, void *data, int flag, proc_t *p)
461 {
462 struct uslsa_softc *sc;
463
464 sc = vsc;
465
466 switch (cmd) {
467 case TIOCMGET:
468 ucom_status_change(device_private(sc->sc_subdev));
469 return EPASSTHROUGH;
470 default:
471 return EPASSTHROUGH;
472 }
473
474 return 0;
475 }
476
477 static int
478 uslsa_open(void *vsc, int portno)
479 {
480 struct uslsa_softc *sc;
481
482 sc = vsc;
483
484 DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
485
486 if (sc->sc_dying) {
487 return EIO;
488 }
489
490 return uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
491 SLSA_RV_IFC_ENABLE_ENABLE);
492 }
493
494 static void
495 uslsa_close(void *vsc, int portno)
496 {
497 struct uslsa_softc *sc;
498
499 sc = vsc;
500
501 DPRINTF((sc->sc_dev, "%s(%p, %d)\n", __func__, vsc, portno));
502
503 if (sc->sc_dying) {
504 return;
505 }
506
507 (void)uslsa_request_set(sc, SLSA_R_IFC_ENABLE,
508 SLSA_RV_IFC_ENABLE_DISABLE);
509 }
510
511 static int
512 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
513 {
514 usb_device_request_t req;
515 usbd_status status;
516
517 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
518 req.bRequest = request;
519 USETW(req.wValue, value);
520 USETW(req.wIndex, sc->sc_ifnum);
521 USETW(req.wLength, 0);
522
523 status = usbd_do_request(sc->sc_udev, &req, NULL);
524
525 return uslsa_usbd_errno(status);
526 }
527
528 static int
529 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
530 {
531 struct slsa_fcs fcs;
532 usb_device_request_t req;
533 uint32_t ulControlHandshake;
534 uint32_t ulFlowReplace;
535 usbd_status status;
536
537 DPRINTF((sc->sc_dev, "%s(%p, %#x, %#x)\n", __func__, sc, cflag, iflag));
538
539 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
540 req.bRequest = SLSA_R_GET_FLOW;
541 USETW(req.wValue, 0);
542 USETW(req.wIndex, sc->sc_ifnum);
543 USETW(req.wLength, SLSA_RL_GET_FLOW);
544
545 status = usbd_do_request(sc->sc_udev, &req, &fcs);
546 if (status != USBD_NORMAL_COMPLETION) {
547 device_printf(sc->sc_dev, "%s: GET_FLOW %s\n",
548 __func__, usbd_errstr(status));
549 return uslsa_usbd_errno(status);
550 }
551
552 ulControlHandshake = le32toh(fcs.ulControlHandshake);
553 ulFlowReplace = le32toh(fcs.ulFlowReplace);
554
555 if (ISSET(cflag, CRTSCTS)) {
556 ulControlHandshake =
557 SERIAL_CTS_HANDSHAKE | __SHIFTIN(1, SERIAL_DTR_MASK);
558 ulFlowReplace = __SHIFTIN(2, SERIAL_RTS_MASK);
559 } else {
560 ulControlHandshake = __SHIFTIN(1, SERIAL_DTR_MASK);
561 ulFlowReplace = __SHIFTIN(1, SERIAL_RTS_MASK);
562 }
563
564 fcs.ulControlHandshake = htole32(ulControlHandshake);
565 fcs.ulFlowReplace = htole32(ulFlowReplace);
566
567 req.bmRequestType = UT_WRITE_VENDOR_INTERFACE;
568 req.bRequest = SLSA_R_SET_FLOW;
569 USETW(req.wValue, 0);
570 USETW(req.wIndex, sc->sc_ifnum);
571 USETW(req.wLength, SLSA_RL_SET_FLOW);
572
573 status = usbd_do_request(sc->sc_udev, &req, &fcs);
574
575 return uslsa_usbd_errno(status);
576 }
577