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