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