umct.c revision 1.32.24.3 1 /* $NetBSD: umct.c,v 1.32.24.3 2014/12/03 14:18:07 skrll Exp $ */
2 /*
3 * Copyright (c) 2001 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * MCT USB-RS232 Interface Controller
33 * http://www.mct.com.tw/prod/rs232.html
34 * http://www.dlink.com/products/usb/dsbs25
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: umct.c,v 1.32.24.3 2014/12/03 14:18:07 skrll Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/ioctl.h>
45 #include <sys/conf.h>
46 #include <sys/tty.h>
47 #include <sys/file.h>
48 #include <sys/select.h>
49 #include <sys/proc.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/poll.h>
53
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbcdc.h>
56
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbdevs.h>
60 #include <dev/usb/usb_quirks.h>
61
62 #include <dev/usb/ucomvar.h>
63 #include <dev/usb/umct.h>
64
65 #ifdef UMCT_DEBUG
66 #define DPRINTFN(n, x) if (umctdebug > (n)) printf x
67 int umctdebug = 0;
68 #else
69 #define DPRINTFN(n, x)
70 #endif
71 #define DPRINTF(x) DPRINTFN(0, x)
72
73 #define UMCT_CONFIG_INDEX 0
74 #define UMCT_IFACE_INDEX 0
75
76 struct umct_softc {
77 device_t sc_dev; /* base device */
78 usbd_device_handle sc_udev; /* USB device */
79 usbd_interface_handle sc_iface; /* interface */
80 int sc_iface_number; /* interface number */
81 uint16_t sc_product;
82
83 int sc_intr_number; /* interrupt number */
84 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
85 u_char *sc_intr_buf; /* interrupt buffer */
86 int sc_isize;
87
88 usb_cdc_line_state_t sc_line_state; /* current line state */
89 u_char sc_dtr; /* current DTR state */
90 u_char sc_rts; /* current RTS state */
91 u_char sc_break; /* set break */
92
93 u_char sc_status;
94
95 device_t sc_subdev; /* ucom device */
96
97 u_char sc_dying; /* disconnecting */
98
99 u_char sc_lsr; /* Local status register */
100 u_char sc_msr; /* umct status register */
101
102 u_int last_lcr; /* keep lcr register */
103 };
104
105 /*
106 * These are the maximum number of bytes transferred per frame.
107 * The output buffer size cannot be increased due to the size encoding.
108 */
109 #define UMCTIBUFSIZE 256
110 #define UMCTOBUFSIZE 256
111
112 Static void umct_init(struct umct_softc *);
113 Static void umct_set_baudrate(struct umct_softc *, u_int);
114 Static void umct_set_lcr(struct umct_softc *, u_int);
115 Static void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
116
117 Static void umct_set(void *, int, int, int);
118 Static void umct_dtr(struct umct_softc *, int);
119 Static void umct_rts(struct umct_softc *, int);
120 Static void umct_break(struct umct_softc *, int);
121 Static void umct_set_line_state(struct umct_softc *);
122 Static void umct_get_status(void *, int portno, u_char *lsr, u_char *msr);
123 Static int umct_param(void *, int, struct termios *);
124 Static int umct_open(void *, int);
125 Static void umct_close(void *, int);
126
127 struct ucom_methods umct_methods = {
128 umct_get_status,
129 umct_set,
130 umct_param,
131 NULL,
132 umct_open,
133 umct_close,
134 NULL,
135 NULL,
136 };
137
138 static const struct usb_devno umct_devs[] = {
139 /* MCT USB-232 Interface Products */
140 { USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
141 /* Sitecom USB-232 Products */
142 { USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
143 /* D-Link DU-H3SP USB BAY Hub Products */
144 { USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
145 /* BELKIN F5U109 */
146 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109 },
147 };
148 #define umct_lookup(v, p) usb_lookup(umct_devs, v, p)
149
150 int umct_match(device_t, cfdata_t, void *);
151 void umct_attach(device_t, device_t, void *);
152 void umct_childdet(device_t, device_t);
153 int umct_detach(device_t, int);
154 int umct_activate(device_t, enum devact);
155 extern struct cfdriver umct_cd;
156 CFATTACH_DECL2_NEW(umct, sizeof(struct umct_softc), umct_match,
157 umct_attach, umct_detach, umct_activate, NULL, umct_childdet);
158
159 int
160 umct_match(device_t parent, cfdata_t match, void *aux)
161 {
162 struct usb_attach_arg *uaa = aux;
163
164 return (umct_lookup(uaa->vendor, uaa->product) != NULL ?
165 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
166 }
167
168 void
169 umct_attach(device_t parent, device_t self, void *aux)
170 {
171 struct umct_softc *sc = device_private(self);
172 struct usb_attach_arg *uaa = aux;
173 usbd_device_handle dev = uaa->device;
174 usb_config_descriptor_t *cdesc;
175 usb_interface_descriptor_t *id;
176 usb_endpoint_descriptor_t *ed;
177
178 char *devinfop;
179 usbd_status err;
180 int i;
181 struct ucom_attach_args uca;
182
183 sc->sc_dev = self;
184
185 aprint_naive("\n");
186 aprint_normal("\n");
187
188 devinfop = usbd_devinfo_alloc(dev, 0);
189 aprint_normal_dev(self, "%s\n", devinfop);
190 usbd_devinfo_free(devinfop);
191
192 sc->sc_udev = dev;
193 sc->sc_product = uaa->product;
194
195 DPRINTF(("\n\numct attach: sc=%p\n", sc));
196
197 /* initialize endpoints */
198 uca.bulkin = uca.bulkout = -1;
199 sc->sc_intr_number = -1;
200 sc->sc_intr_pipe = NULL;
201
202 /* Move the device into the configured state. */
203 err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
204 if (err) {
205 aprint_error_dev(self, "failed to set configuration, err=%s\n",
206 usbd_errstr(err));
207 sc->sc_dying = 1;
208 return;
209 }
210
211 /* get the config descriptor */
212 cdesc = usbd_get_config_descriptor(sc->sc_udev);
213
214 if (cdesc == NULL) {
215 aprint_error_dev(self,
216 "failed to get configuration descriptor\n");
217 sc->sc_dying = 1;
218 return;
219 }
220
221 /* get the interface */
222 err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
223 &sc->sc_iface);
224 if (err) {
225 aprint_error_dev(self, "failed to get interface, err=%s\n",
226 usbd_errstr(err));
227 sc->sc_dying = 1;
228 return;
229 }
230
231 /* Find the bulk{in,out} and interrupt endpoints */
232
233 id = usbd_get_interface_descriptor(sc->sc_iface);
234 sc->sc_iface_number = id->bInterfaceNumber;
235
236 for (i = 0; i < id->bNumEndpoints; i++) {
237 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
238 if (ed == NULL) {
239 aprint_error_dev(self,
240 "no endpoint descriptor for %d\n", i);
241 sc->sc_dying = 1;
242 return;
243 }
244
245 /*
246 * The Bulkin endpoint is marked as an interrupt. Since
247 * we can't rely on the endpoint descriptor order, we'll
248 * check the wMaxPacketSize field to differentiate.
249 */
250 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
251 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
252 UGETW(ed->wMaxPacketSize) != 0x2) {
253 uca.bulkin = ed->bEndpointAddress;
254 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
255 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
256 uca.bulkout = ed->bEndpointAddress;
257 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
258 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
259 sc->sc_intr_number = ed->bEndpointAddress;
260 sc->sc_isize = UGETW(ed->wMaxPacketSize);
261 }
262 }
263
264 if (uca.bulkin == -1) {
265 aprint_error_dev(self, "Could not find data bulk in\n");
266 sc->sc_dying = 1;
267 return;
268 }
269
270 if (uca.bulkout == -1) {
271 aprint_error_dev(self, "Could not find data bulk out\n");
272 sc->sc_dying = 1;
273 return;
274 }
275
276 if (sc->sc_intr_number== -1) {
277 aprint_error_dev(self, "Could not find interrupt in\n");
278 sc->sc_dying = 1;
279 return;
280 }
281
282 sc->sc_dtr = sc->sc_rts = 0;
283 uca.portno = UCOM_UNK_PORTNO;
284 /* bulkin, bulkout set above */
285 uca.ibufsize = UMCTIBUFSIZE;
286 if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232)
287 uca.obufsize = 16; /* device is broken */
288 else
289 uca.obufsize = UMCTOBUFSIZE;
290 uca.ibufsizepad = UMCTIBUFSIZE;
291 uca.opkthdrlen = 0;
292 uca.device = dev;
293 uca.iface = sc->sc_iface;
294 uca.methods = &umct_methods;
295 uca.arg = sc;
296 uca.info = NULL;
297
298 umct_init(sc);
299
300 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
301 sc->sc_dev);
302
303 DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n",
304 uca.bulkin, uca.bulkout, sc->sc_intr_number ));
305 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
306 ucomprint, ucomsubmatch);
307
308 return;
309 }
310
311 void
312 umct_childdet(device_t self, device_t child)
313 {
314 struct umct_softc *sc = device_private(self);
315
316 KASSERT(sc->sc_subdev == child);
317 sc->sc_subdev = NULL;
318 }
319
320 int
321 umct_detach(device_t self, int flags)
322 {
323 struct umct_softc *sc = device_private(self);
324 int rv = 0;
325
326 DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags));
327
328 if (sc->sc_intr_pipe != NULL) {
329 usbd_abort_pipe(sc->sc_intr_pipe);
330 usbd_close_pipe(sc->sc_intr_pipe);
331 kmem_free(sc->sc_intr_buf, sc->sc_isize);
332 sc->sc_intr_pipe = NULL;
333 }
334
335 sc->sc_dying = 1;
336 if (sc->sc_subdev != NULL)
337 rv = config_detach(sc->sc_subdev, flags);
338
339 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
340 sc->sc_dev);
341
342 return (rv);
343 }
344
345 int
346 umct_activate(device_t self, enum devact act)
347 {
348 struct umct_softc *sc = device_private(self);
349
350 switch (act) {
351 case DVACT_DEACTIVATE:
352 sc->sc_dying = 1;
353 return 0;
354 default:
355 return EOPNOTSUPP;
356 }
357 }
358
359 void
360 umct_set_line_state(struct umct_softc *sc)
361 {
362 usb_device_request_t req;
363 uByte ls;
364
365 ls = (sc->sc_dtr ? MCR_DTR : 0) |
366 (sc->sc_rts ? MCR_RTS : 0);
367
368 DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n",
369 sc->sc_dtr, sc->sc_rts, ls));
370
371 req.bmRequestType = UMCT_SET_REQUEST;
372 req.bRequest = REQ_SET_MCR;
373 USETW(req.wValue, 0);
374 USETW(req.wIndex, sc->sc_iface_number);
375 USETW(req.wLength, LENGTH_SET_MCR);
376
377 (void)usbd_do_request(sc->sc_udev, &req, &ls);
378 }
379
380 void
381 umct_set(void *addr, int portno, int reg, int onoff)
382 {
383 struct umct_softc *sc = addr;
384
385 switch (reg) {
386 case UCOM_SET_DTR:
387 umct_dtr(sc, onoff);
388 break;
389 case UCOM_SET_RTS:
390 umct_rts(sc, onoff);
391 break;
392 case UCOM_SET_BREAK:
393 umct_break(sc, onoff);
394 break;
395 default:
396 break;
397 }
398 }
399
400 void
401 umct_dtr(struct umct_softc *sc, int onoff)
402 {
403
404 DPRINTF(("umct_dtr: onoff=%d\n", onoff));
405
406 if (sc->sc_dtr == onoff)
407 return;
408 sc->sc_dtr = onoff;
409
410 umct_set_line_state(sc);
411 }
412
413 void
414 umct_rts(struct umct_softc *sc, int onoff)
415 {
416 DPRINTF(("umct_rts: onoff=%d\n", onoff));
417
418 if (sc->sc_rts == onoff)
419 return;
420 sc->sc_rts = onoff;
421
422 umct_set_line_state(sc);
423 }
424
425 void
426 umct_break(struct umct_softc *sc, int onoff)
427 {
428 DPRINTF(("umct_break: onoff=%d\n", onoff));
429
430 umct_set_lcr(sc, onoff ? sc->last_lcr | LCR_SET_BREAK :
431 sc->last_lcr);
432 }
433
434 void
435 umct_set_lcr(struct umct_softc *sc, u_int data)
436 {
437 usb_device_request_t req;
438 uByte adata;
439
440 adata = data;
441 req.bmRequestType = UMCT_SET_REQUEST;
442 req.bRequest = REQ_SET_LCR;
443 USETW(req.wValue, 0);
444 USETW(req.wIndex, sc->sc_iface_number);
445 USETW(req.wLength, LENGTH_SET_LCR);
446
447 (void)usbd_do_request(sc->sc_udev, &req, &adata); /* XXX should check */
448 }
449
450 void
451 umct_set_baudrate(struct umct_softc *sc, u_int rate)
452 {
453 usb_device_request_t req;
454 uDWord arate;
455 u_int val;
456
457 if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232 ||
458 sc->sc_product == USB_PRODUCT_BELKIN_F5U109) {
459 switch (rate) {
460 case 300: val = 0x01; break;
461 case 600: val = 0x02; break;
462 case 1200: val = 0x03; break;
463 case 2400: val = 0x04; break;
464 case 4800: val = 0x06; break;
465 case 9600: val = 0x08; break;
466 case 19200: val = 0x09; break;
467 case 38400: val = 0x0a; break;
468 case 57600: val = 0x0b; break;
469 case 115200: val = 0x0c; break;
470 default: val = -1; break;
471 }
472 } else {
473 val = UMCT_BAUD_RATE(rate);
474 }
475 USETDW(arate, val);
476
477 req.bmRequestType = UMCT_SET_REQUEST;
478 req.bRequest = REQ_SET_BAUD_RATE;
479 USETW(req.wValue, 0);
480 USETW(req.wIndex, sc->sc_iface_number);
481 USETW(req.wLength, LENGTH_BAUD_RATE);
482
483 (void)usbd_do_request(sc->sc_udev, &req, arate); /* XXX should check */
484 }
485
486 void
487 umct_init(struct umct_softc *sc)
488 {
489 umct_set_baudrate(sc, 9600);
490 umct_set_lcr(sc, LCR_DATA_BITS_8 | LCR_PARITY_NONE | LCR_STOP_BITS_1);
491 }
492
493 int
494 umct_param(void *addr, int portno, struct termios *t)
495 {
496 struct umct_softc *sc = addr;
497 u_int data = 0;
498
499 DPRINTF(("umct_param: sc=%p\n", sc));
500
501 DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed));
502
503 if (ISSET(t->c_cflag, CSTOPB))
504 data |= LCR_STOP_BITS_2;
505 else
506 data |= LCR_STOP_BITS_1;
507 if (ISSET(t->c_cflag, PARENB)) {
508 if (ISSET(t->c_cflag, PARODD))
509 data |= LCR_PARITY_ODD;
510 else
511 data |= LCR_PARITY_EVEN;
512 } else
513 data |= LCR_PARITY_NONE;
514 switch (ISSET(t->c_cflag, CSIZE)) {
515 case CS5:
516 data |= LCR_DATA_BITS_5;
517 break;
518 case CS6:
519 data |= LCR_DATA_BITS_6;
520 break;
521 case CS7:
522 data |= LCR_DATA_BITS_7;
523 break;
524 case CS8:
525 data |= LCR_DATA_BITS_8;
526 break;
527 }
528
529 umct_set_baudrate(sc, t->c_ospeed);
530
531 sc->last_lcr = data;
532 umct_set_lcr(sc, data);
533
534 return (0);
535 }
536
537 int
538 umct_open(void *addr, int portno)
539 {
540 struct umct_softc *sc = addr;
541 int err, lcr_data;
542
543 if (sc->sc_dying)
544 return (EIO);
545
546 DPRINTF(("umct_open: sc=%p\n", sc));
547
548 /* initialize LCR */
549 lcr_data = LCR_DATA_BITS_8 | LCR_PARITY_NONE |
550 LCR_STOP_BITS_1;
551 umct_set_lcr(sc, lcr_data);
552
553 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
554 sc->sc_status = 0; /* clear status bit */
555 sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
556 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
557 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
558 sc->sc_intr_buf, sc->sc_isize,
559 umct_intr, USBD_DEFAULT_INTERVAL);
560 if (err) {
561 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
562 device_xname(sc->sc_dev), sc->sc_intr_number));
563 return (EIO);
564 }
565 }
566
567 return (0);
568 }
569
570 void
571 umct_close(void *addr, int portno)
572 {
573 struct umct_softc *sc = addr;
574 int err;
575
576 if (sc->sc_dying)
577 return;
578
579 DPRINTF(("umct_close: close\n"));
580
581 if (sc->sc_intr_pipe != NULL) {
582 err = usbd_abort_pipe(sc->sc_intr_pipe);
583 if (err)
584 printf("%s: abort interrupt pipe failed: %s\n",
585 device_xname(sc->sc_dev), usbd_errstr(err));
586 err = usbd_close_pipe(sc->sc_intr_pipe);
587 if (err)
588 printf("%s: close interrupt pipe failed: %s\n",
589 device_xname(sc->sc_dev), usbd_errstr(err));
590 kmem_free(sc->sc_intr_buf, sc->sc_isize);
591 sc->sc_intr_pipe = NULL;
592 }
593 }
594
595 void
596 umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
597 usbd_status status)
598 {
599 struct umct_softc *sc = priv;
600 u_char *tbuf = sc->sc_intr_buf;
601 u_char mstatus;
602
603 if (sc->sc_dying)
604 return;
605
606 if (status != USBD_NORMAL_COMPLETION) {
607 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
608 return;
609
610 DPRINTF(("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
611 usbd_errstr(status)));
612 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
613 return;
614 }
615
616 DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n",
617 device_xname(sc->sc_dev), tbuf[0],tbuf[1]));
618
619 sc->sc_lsr = sc->sc_msr = 0;
620 mstatus = tbuf[0];
621 if (ISSET(mstatus, MSR_DSR))
622 sc->sc_msr |= UMSR_DSR;
623 if (ISSET(mstatus, MSR_DCD))
624 sc->sc_msr |= UMSR_DCD;
625 ucom_status_change(device_private(sc->sc_subdev));
626 }
627
628 void
629 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
630 {
631 struct umct_softc *sc = addr;
632
633 DPRINTF(("umct_get_status:\n"));
634
635 if (lsr != NULL)
636 *lsr = sc->sc_lsr;
637 if (msr != NULL)
638 *msr = sc->sc_msr;
639 }
640