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