uslsa.c revision 1.3 1 /* $NetBSD: uslsa.c,v 1.3 2007/06/13 17:14:25 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jonathan A. Kollasch <jakllsch (at) kollasch.net>. Craig Shelley's Linux
9 * driver provided invaluable documentation.
10 *
11 * This code is derived from ugensa.c, software contributed to
12 * The NetBSD Foundation by Roland C. Dowdeswell <elric (at) netbsd.org>.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: uslsa.c,v 1.3 2007/06/13 17:14:25 nathanw Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/device.h>
50 #include <sys/conf.h>
51 #include <sys/tty.h>
52
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbhid.h>
55
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 #include <dev/usb/usbdevs.h>
59
60 #include <dev/usb/ucomvar.h>
61
62 #ifdef DEBUG
63 #define USLSA_DEBUG
64 #endif
65
66 #ifdef USLSA_DEBUG
67 #define DPRINTF(x) if (uslsadebug) printf x
68 #define DPRINTFN(n,x) if (uslsadebug>(n)) printf x
69 int uslsadebug = 0;
70 #else
71 #define DPRINTF(x)
72 #define DPRINTFN(n,x)
73 #endif
74
75 #define USLSA_REQUEST_SET 0x41
76 #define USLSA_REQUEST_GET 0xc1
77
78 #define USLSA_REQ_SET_STATE 0x00
79
80 #define USLSA_REQ_SET_BPS 0x01
81 #define USLSA_REQ_GET_BPS 0x02
82
83 #define USLSA_REQ_SET_DPS 0x03
84 #define USLSA_REQ_GET_DPS 0x04
85
86 #define USLSA_REQ_SET_BREAK 0x05
87 #define USLSA_REQ_GET_BREAK 0x06
88
89 #define USLSA_REQ_SET_FLOW 0x07
90 #define USLSA_REQ_GET_FLOW 0x08
91
92 #define USLSA_REQ_SET_MODEM 0x13
93 #define USLSA_REQ_GET_MODEM 0x14
94
95 #define USLSA_REQ_SET_MISC 0x19
96 #define USLSA_REQ_GET_MISC 0x20
97
98 #define USLSA_STATE_DISABLE 0x0000
99 #define USLSA_STATE_ENABLE 0x0001
100
101 #define USLSA_BPS(b) (3686400/b)
102
103 #define USLSA_DPS_DATA_MASK 0x0f00
104 #define USLSA_DPS_DATA_FIVE 0x0500
105 #define USLSA_DPS_DATA_SIX 0x0600
106 #define USLSA_DPS_DATA_SEVEN 0x0700
107 #define USLSA_DPS_DATA_EIGHT 0x0800
108 #define USLSA_DPS_DATA_NINE 0x0900
109
110 #define USLSA_DPS_PARITY_MASK 0x00f0
111 #define USLSA_DPS_PARITY_SPACE 0x0040
112 #define USLSA_DPS_PARITY_MARK 0x0030
113 #define USLSA_DPS_PARITY_EVEN 0x0020
114 #define USLSA_DPS_PARITY_ODD 0x0010
115 #define USLSA_DPS_PARITY_NONE 0x0000
116
117 #define USLSA_DPS_STOP_MASK 0x000f
118 #define USLSA_DPS_STOP_TWO 0x0002
119 #define USLSA_DPS_STOP_ONE_FIVE 0x0001
120 #define USLSA_DPS_STOP_ONE 0x0000
121
122 #define USLSA_BREAK_DISABLE 0x0001
123 #define USLSA_BREAK_ENABLE 0x0000
124
125 #define USLSA_FLOW_SET_RTS 0x0200
126 #define USLSA_FLOW_SET_DTR 0x0100
127 #define USLSA_FLOW_MSR_MASK 0x00f0
128 #define USLSA_FLOW_MSR_DCD 0x0080
129 #define USLSA_FLOW_MSR_RI 0x0040
130 #define USLSA_FLOW_MSR_DSR 0x0020
131 #define USLSA_FLOW_MSR_CTS 0x0010
132 #define USLSA_FLOW_RTS 0x0002
133 #define USLSA_FLOW_DTR 0x0001
134
135 struct uslsa_softc {
136 USBBASEDEVICE sc_dev; /* base device */
137 usbd_device_handle sc_udev; /* device */
138 usbd_interface_handle sc_iface; /* interface */
139
140 device_ptr_t sc_subdev; /* ucom device */
141
142 u_char sc_dying; /* disconnecting */
143
144 u_char sc_lsr; /* local status register */
145 u_char sc_msr; /* uslsa status register */
146 };
147
148 static void uslsa_get_status(void *sc, int, u_char *, u_char *);
149 static void uslsa_set(void *, int, int, int);
150 static int uslsa_param(void *, int, struct termios *);
151 static int uslsa_open(void *, int);
152 static void uslsa_close(void *, int);
153
154 static int uslsa_request_set(struct uslsa_softc *, uint8_t, uint16_t);
155 static void uslsa_set_flow(struct uslsa_softc *, tcflag_t, tcflag_t);
156
157 struct ucom_methods uslsa_methods = {
158 uslsa_get_status,
159 uslsa_set,
160 uslsa_param,
161 NULL,
162 uslsa_open,
163 uslsa_close,
164 NULL,
165 NULL,
166 };
167
168 #define USLSA_CONFIG_INDEX 0
169 #define USLSA_IFACE_INDEX 0
170 #define USLSA_BUFSIZE 256
171
172 static const struct usb_devno uslsa_devs[] = {
173 { USB_VENDOR_BALTECH, USB_PRODUCT_BALTECH_CARDREADER },
174 { USB_VENDOR_DYNASTREAM, USB_PRODUCT_DYNASTREAM_ANTDEVBOARD },
175 { USB_VENDOR_JABLOTRON, USB_PRODUCT_JABLOTRON_PC60B },
176 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_ARGUSISP },
177 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CRUMB128 },
178 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DEGREECONT },
179 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_DESKTOPMOBILE },
180 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_IPLINK1220 },
181 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_HARP },
182 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_JTAG },
183 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_LIPOWSKY_LIN },
184 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_POLOLU },
185 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_1 },
186 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP210X_2 },
187 { USB_VENDOR_SILABS, USB_PRODUCT_SILABS_SUNNTO },
188 { USB_VENDOR_SILABS2, USB_PRODUCT_SILABS2_DCU11CLONE },
189 { USB_VENDOR_USI, USB_PRODUCT_USI_MC60 }
190 };
191 #define uslsa_lookup(v, p) usb_lookup(uslsa_devs, v, p)
192
193 USB_DECLARE_DRIVER(uslsa);
194
195 USB_MATCH(uslsa)
196 {
197 USB_MATCH_START(uslsa, uaa);
198
199 return (uslsa_lookup(uaa->vendor, uaa->product) != NULL ?
200 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
201 }
202
203 USB_ATTACH(uslsa)
204 {
205 USB_ATTACH_START(uslsa, sc, uaa);
206 usbd_device_handle dev = uaa->device;
207 usbd_interface_handle iface;
208 usb_interface_descriptor_t *id;
209 usb_endpoint_descriptor_t *ed;
210 char *devinfop;
211 char *devname;
212 usbd_status err;
213 struct ucom_attach_args uca;
214 int i;
215
216 devname = USBDEVNAME(sc->sc_dev);
217
218 DPRINTFN(10, ("\nuslsa_attach: sc=%p\n", sc));
219
220 /* Move the device into the configured state. */
221 err = usbd_set_config_index(dev, USLSA_CONFIG_INDEX, 1);
222 if (err) {
223 printf("\n%s: failed to set configuration, err=%s\n",
224 devname, usbd_errstr(err));
225 goto bad;
226 }
227
228 err = usbd_device2interface_handle(dev, USLSA_IFACE_INDEX, &iface);
229 if (err) {
230 printf("\n%s: failed to get interface, err=%s\n",
231 devname, usbd_errstr(err));
232 goto bad;
233 }
234
235 devinfop = usbd_devinfo_alloc(dev, 0);
236 USB_ATTACH_SETUP;
237 printf("%s: %s\n", devname, devinfop);
238 usbd_devinfo_free(devinfop);
239
240 id = usbd_get_interface_descriptor(iface);
241
242 sc->sc_udev = dev;
243 sc->sc_iface = iface;
244
245 uca.info = "Silicon Labs CP210x";
246 uca.portno = UCOM_UNK_PORTNO;
247 uca.ibufsize = USLSA_BUFSIZE;
248 uca.obufsize = USLSA_BUFSIZE;
249 uca.ibufsizepad = USLSA_BUFSIZE;
250 uca.opkthdrlen = 0;
251 uca.device = dev;
252 uca.iface = iface;
253 uca.methods = &uslsa_methods;
254 uca.arg = sc;
255
256 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
257 USBDEV(sc->sc_dev));
258
259 uca.bulkin = uca.bulkout = -1;
260 for (i = 0; i < id->bNumEndpoints; i++) {
261 int addr, dir, attr;
262
263 ed = usbd_interface2endpoint_descriptor(iface, i);
264 if (ed == NULL) {
265 printf("%s: could not read endpoint descriptor"
266 ": %s\n", devname, usbd_errstr(err));
267 goto bad;
268 }
269 addr = ed->bEndpointAddress;
270 dir = UE_GET_DIR(ed->bEndpointAddress);
271 attr = ed->bmAttributes & UE_XFERTYPE;
272 if (dir == UE_DIR_IN && attr == UE_BULK)
273 uca.bulkin = addr;
274 else if (dir == UE_DIR_OUT && attr == UE_BULK)
275 uca.bulkout = addr;
276 else
277 printf("%s: unexpected endpoint\n", devname);
278 }
279 if (uca.bulkin == -1) {
280 printf("%s: Could not find data bulk in\n",
281 USBDEVNAME(sc->sc_dev));
282 goto bad;
283 }
284 if (uca.bulkout == -1) {
285 printf("%s: Could not find data bulk out\n",
286 USBDEVNAME(sc->sc_dev));
287 goto bad;
288 }
289
290 DPRINTF(("uslsa: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
291 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
292 ucomprint, ucomsubmatch);
293
294 USB_ATTACH_SUCCESS_RETURN;
295
296 bad:
297 DPRINTF(("uslsa_attach: ATTACH ERROR\n"));
298 sc->sc_dying = 1;
299 USB_ATTACH_ERROR_RETURN;
300 }
301
302 int
303 uslsa_activate(device_ptr_t self, enum devact act)
304 {
305 struct uslsa_softc *sc = (struct uslsa_softc *) self;
306 int rv = 0;
307
308 switch (act) {
309 case DVACT_ACTIVATE:
310 return (EOPNOTSUPP);
311 break;
312
313 case DVACT_DEACTIVATE:
314 sc->sc_dying = 1;
315 if (sc->sc_subdev)
316 rv = config_deactivate(sc->sc_subdev);
317 break;
318 }
319 return (rv);
320 }
321
322 USB_DETACH(uslsa)
323 {
324 USB_DETACH_START(uslsa, sc);
325 int rv = 0;
326
327 DPRINTF(("uslsa_detach: sc=%p flags=%d\n", sc, flags));
328
329 sc->sc_dying = 1;
330
331 if (sc->sc_subdev != NULL)
332 rv = config_detach(sc->sc_subdev, flags);
333
334 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
335 USBDEV(sc->sc_dev));
336
337 return (rv);
338 }
339
340 static void
341 uslsa_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
342 {
343 struct uslsa_softc *sc;
344 usb_device_request_t req;
345 usbd_status err;
346 int actlen;
347 uint16_t flowreg;
348
349 sc = vsc;
350
351 DPRINTF(("uslsa_get_status:\n"));
352
353 req.bmRequestType = USLSA_REQUEST_GET;
354 req.bRequest = USLSA_REQ_GET_FLOW;
355 USETW(req.wValue, 0);
356 USETW(req.wIndex, 0);
357 USETW(req.wLength, sizeof(flowreg));
358
359 err = usbd_do_request_flags(sc->sc_udev, &req, &flowreg,
360 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
361 if (err)
362 printf("%s: uslsa_get_status: %s\n",
363 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
364
365 DPRINTF(("uslsa_get_status: flowreg=0x%x\n", flowreg));
366
367 sc->sc_msr = (u_char)(USLSA_FLOW_MSR_MASK & flowreg);
368
369 if (lsr != NULL)
370 *lsr = sc->sc_lsr;
371 if (msr != NULL)
372 *msr = sc->sc_msr;
373 }
374
375 static void
376 uslsa_set(void *vsc, int portno, int reg, int onoff)
377 {
378 struct uslsa_softc *sc;
379
380 sc = vsc;
381
382 DPRINTF(("uslsa_set: sc=%p, port=%d reg=%d onoff=%d\n", sc, portno,
383 reg, onoff));
384
385 switch (reg) {
386 case UCOM_SET_DTR:
387 if (uslsa_request_set(sc, USLSA_REQ_SET_FLOW,
388 (onoff ? (USLSA_FLOW_DTR | USLSA_FLOW_SET_DTR) :
389 USLSA_FLOW_SET_DTR)))
390 printf("%s: uslsa_set_dtr failed\n",
391 USBDEVNAME(sc->sc_dev));
392 break;
393 case UCOM_SET_RTS:
394 if (uslsa_request_set(sc, USLSA_REQ_SET_FLOW,
395 (onoff ? (USLSA_FLOW_RTS | USLSA_FLOW_SET_RTS) :
396 USLSA_FLOW_SET_RTS)))
397 printf("%s: uslsa_set_rts failed\n",
398 USBDEVNAME(sc->sc_dev));
399 break;
400 case UCOM_SET_BREAK:
401 if (uslsa_request_set(sc, USLSA_REQ_SET_BREAK,
402 (onoff ? USLSA_BREAK_ENABLE :
403 USLSA_BREAK_DISABLE)))
404 printf("%s: uslsa_set_break failed\n",
405 USBDEVNAME(sc->sc_dev));
406 break;
407 default:
408 break;
409 }
410 }
411
412 static int
413 uslsa_param(void *vsc, int portno, struct termios * t)
414 {
415 struct uslsa_softc *sc;
416 uint16_t data;
417
418 sc = vsc;
419
420 DPRINTF(("uslsa_param: sc=%p\n", sc));
421
422 switch (t->c_ospeed) {
423 case B600:
424 case B1200:
425 case B2400:
426 case B4800:
427 case B9600:
428 case B19200:
429 case B38400:
430 case B57600:
431 case B115200:
432 case B230400:
433 case B460800:
434 case B921600:
435 data = USLSA_BPS(t->c_ospeed);
436 break;
437 default:
438 printf("%s: uslsa_param: unsupported data rate, "
439 "forcing default of 115200bps\n",
440 USBDEVNAME(sc->sc_dev));
441 data = USLSA_BPS(B115200);
442 };
443
444 if (uslsa_request_set(sc, USLSA_REQ_SET_BPS, data))
445 printf("%s: uslsa_param: setting data rate failed\n",
446 USBDEVNAME(sc->sc_dev));
447
448 data = 0;
449
450 if (ISSET(t->c_cflag, CSTOPB))
451 data |= USLSA_DPS_STOP_TWO;
452 else
453 data |= USLSA_DPS_STOP_ONE;
454
455 if (ISSET(t->c_cflag, PARENB)) {
456 if (ISSET(t->c_cflag, PARODD))
457 data |= USLSA_DPS_PARITY_ODD;
458 else
459 data |= USLSA_DPS_PARITY_EVEN;
460 } else
461 data |= USLSA_DPS_PARITY_NONE;
462
463 switch (ISSET(t->c_cflag, CSIZE)) {
464 case CS5:
465 data |= USLSA_DPS_DATA_FIVE;
466 break;
467 case CS6:
468 data |= USLSA_DPS_DATA_SIX;
469 break;
470 case CS7:
471 data |= USLSA_DPS_DATA_SEVEN;
472 break;
473 case CS8:
474 data |= USLSA_DPS_DATA_EIGHT;
475 break;
476 }
477
478 DPRINTF(("uslsa_param: setting DPS register to 0x%x\n", data));
479 if (uslsa_request_set(sc, USLSA_REQ_SET_DPS, data))
480 printf("%s: setting DPS register failed: invalid argument\n",
481 USBDEVNAME(sc->sc_dev));
482
483 uslsa_set_flow(sc, t->c_cflag, t->c_iflag);
484
485 return 0;
486 }
487
488
489 static int
490 uslsa_open(void *vsc, int portno)
491 {
492 struct uslsa_softc *sc;
493
494 sc = vsc;
495
496 DPRINTF(("uslsa_open: sc=%p\n", sc));
497
498 if (sc->sc_dying)
499 return (EIO);
500
501 if (uslsa_request_set(sc, USLSA_REQ_SET_STATE, USLSA_STATE_ENABLE))
502 return (EIO);
503
504 return 0;
505 }
506
507 void
508 uslsa_close(void *vsc, int portno)
509 {
510 struct uslsa_softc *sc;
511
512 sc = vsc;
513
514 if (sc->sc_dying)
515 return;
516
517 DPRINTF(("uslsa_close: sc=%p\n", sc));
518
519 if (uslsa_request_set(sc, USLSA_REQ_SET_STATE,
520 USLSA_STATE_DISABLE))
521 printf("%s: disable-on-close failed\n",
522 USBDEVNAME(sc->sc_dev));
523 }
524
525 /*
526 * uslsa_request_set(), wrapper for doing sets with usbd_do_request()
527 */
528 static int
529 uslsa_request_set(struct uslsa_softc * sc, uint8_t request, uint16_t value)
530 {
531 usb_device_request_t req;
532 usbd_status err;
533
534 req.bmRequestType = USLSA_REQUEST_SET;
535 req.bRequest = request;
536 USETW(req.wValue, value);
537 USETW(req.wIndex, 0);
538 USETW(req.wLength, 0);
539
540 err = usbd_do_request(sc->sc_udev, &req, 0);
541 if (err)
542 printf("%s: uslsa_request: %s\n",
543 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
544 return err;
545 }
546
547 /*
548 * uslsa_set_flow() does some magic to set up hardware flow control
549 */
550 static void
551 uslsa_set_flow(struct uslsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
552 {
553 uint8_t mysterydata[16];
554 usb_device_request_t req;
555 usbd_status err;
556
557 DPRINTF(("uslsa_set_flow: cflag = 0x%x, iflag = 0x%x\n",
558 cflag, iflag));
559
560 req.bmRequestType = USLSA_REQUEST_GET;
561 req.bRequest = USLSA_REQ_GET_MODEM;
562 USETW(req.wValue, 0);
563 USETW(req.wIndex, 0);
564 USETW(req.wLength, 16);
565
566 err = usbd_do_request(sc->sc_udev, &req, mysterydata);
567 if (err)
568 printf("%s: uslsa_set_flow: %s\n",
569 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
570
571 if (ISSET(cflag, CRTSCTS)) {
572 mysterydata[0] &= ~0x7b;
573 mysterydata[0] |= 0x09;
574 mysterydata[4] = 0x80;
575 } else {
576 mysterydata[0] &= ~0x7b;
577 mysterydata[0] |= 0x01;
578 mysterydata[4] = 0x40;
579 }
580
581 req.bmRequestType = USLSA_REQUEST_SET;
582 req.bRequest = USLSA_REQ_SET_MODEM;
583 USETW(req.wValue, 0);
584 USETW(req.wIndex, 0);
585 USETW(req.wLength, 16);
586
587 err = usbd_do_request(sc->sc_udev, &req, mysterydata);
588 if (err)
589 printf("%s: uslsa_set_flow: %s\n",
590 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
591 }
592