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