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