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