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