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