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