uplcom.c revision 1.82 1 /* $NetBSD: uplcom.c,v 1.82 2019/05/05 03:17:54 mrg 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 * General information: http://www.prolific.com.tw/fr_pl2303.htm
33 * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.82 2019/05/05 03:17:54 mrg Exp $");
38
39 #ifdef _KERNEL_OPT
40 #include "opt_usb.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/kmem.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/device.h>
54 #include <sys/poll.h>
55
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbcdc.h>
58
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
63
64 #include <dev/usb/ucomvar.h>
65
66 #ifdef UPLCOM_DEBUG
67 #define DPRINTFN(n, x) if (uplcomdebug > (n)) printf x
68 int uplcomdebug = 0;
69 #else
70 #define DPRINTFN(n, x)
71 #endif
72 #define DPRINTF(x) DPRINTFN(0, x)
73
74 #define UPLCOM_CONFIG_INDEX 0
75 #define UPLCOM_IFACE_INDEX 0
76 #define UPLCOM_SECOND_IFACE_INDEX 1
77
78 #define UPLCOM_SET_REQUEST 0x01
79 #define UPLCOM_SET_CRTSCTS_0 0x41
80 #define UPLCOM_SET_CRTSCTS_HX 0x61
81
82 #define UPLCOM_N_SERIAL_CTS 0x80
83
84 enum pl2303_type {
85 UPLCOM_TYPE_0, /* we use this for all non-HX variants */
86 UPLCOM_TYPE_HX,
87 };
88
89 struct uplcom_softc {
90 device_t sc_dev; /* base device */
91 struct usbd_device * sc_udev; /* USB device */
92 struct usbd_interface * sc_iface; /* interface */
93 int sc_iface_number; /* interface number */
94
95 struct usbd_interface * sc_intr_iface; /* interrupt interface */
96 int sc_intr_number; /* interrupt number */
97 struct usbd_pipe * sc_intr_pipe; /* interrupt pipe */
98 u_char *sc_intr_buf; /* interrupt buffer */
99 int sc_isize;
100
101 usb_cdc_line_state_t sc_line_state; /* current line state */
102 int sc_dtr; /* current DTR state */
103 int sc_rts; /* current RTS state */
104
105 device_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; /* uplcom status register */
111
112 enum pl2303_type sc_type; /* PL2303 chip type */
113 };
114
115 /*
116 * These are the maximum number of bytes transferred per frame.
117 * The output buffer size cannot be increased due to the size encoding.
118 */
119 #define UPLCOMIBUFSIZE 256
120 #define UPLCOMOBUFSIZE 256
121
122 Static usbd_status uplcom_reset(struct uplcom_softc *);
123 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *,
124 usb_cdc_line_state_t *);
125 Static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
126 Static void uplcom_intr(struct usbd_xfer *, void *, usbd_status);
127
128 Static void uplcom_set(void *, int, int, int);
129 Static void uplcom_dtr(struct uplcom_softc *, int);
130 Static void uplcom_rts(struct uplcom_softc *, int);
131 Static void uplcom_break(struct uplcom_softc *, int);
132 Static void uplcom_set_line_state(struct uplcom_softc *);
133 Static void uplcom_get_status(void *, int, u_char *, u_char *);
134 #if TODO
135 Static int uplcom_ioctl(void *, int, u_long, void *, int, proc_t *);
136 #endif
137 Static int uplcom_param(void *, int, struct termios *);
138 Static int uplcom_open(void *, int);
139 Static void uplcom_close(void *, int);
140 Static usbd_status uplcom_vendor_control_write(struct usbd_device *, uint16_t, uint16_t);
141
142 struct ucom_methods uplcom_methods = {
143 .ucom_get_status = uplcom_get_status,
144 .ucom_set = uplcom_set,
145 .ucom_param = uplcom_param,
146 .ucom_ioctl = NULL, /* TODO */
147 .ucom_open = uplcom_open,
148 .ucom_close = uplcom_close,
149 };
150
151 static const struct usb_devno uplcom_devs[] = {
152 /* I/O DATA USB-RSAQ2 */
153 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
154 /* I/O DATA USB-RSAQ3 */
155 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
156 /* I/O DATA USB-RSAQ */
157 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
158 /* I/O DATA USB-RSAQ5 */
159 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
160 /* PLANEX USB-RS232 URS-03 */
161 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
162 /* various */
163 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
164 /* SMART Technologies USB to serial */
165 { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
166 /* IOGEAR/ATENTRIPPLITE */
167 { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
168 /* ELECOM UC-SGT */
169 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
170 /* ELECOM UC-SGT0 */
171 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
172 /* Panasonic 50" Touch Panel */
173 { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
174 /* RATOC REX-USB60 */
175 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
176 /* TDK USB-PHS Adapter UHA6400 */
177 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
178 /* TDK USB-PDC Adapter UPA9664 */
179 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
180 /* Sony Ericsson USB Cable */
181 { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
182 /* SOURCENEXT KeikaiDenwa 8 */
183 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
184 /* SOURCENEXT KeikaiDenwa 8 with charger */
185 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
186 /* HAL Corporation Crossam2+USB */
187 { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
188 /* Sitecom USB to serial cable */
189 { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
190 /* Pharos USB GPS - Microsoft version */
191 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
192 /* Willcom WS002IN (DD) */
193 { USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
194 /* COREGA CG-USBRS232R */
195 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGUSBRS232R },
196 /* Sharp CE-175TU (USB to Zaurus option port 15 adapter) */
197 { USB_VENDOR_SHARP, USB_PRODUCT_SHARP_CE175TU },
198 };
199 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
200
201 int uplcom_match(device_t, cfdata_t, void *);
202 void uplcom_attach(device_t, device_t, void *);
203 void uplcom_childdet(device_t, device_t);
204 int uplcom_detach(device_t, int);
205 int uplcom_activate(device_t, enum devact);
206
207 CFATTACH_DECL2_NEW(uplcom, sizeof(struct uplcom_softc), uplcom_match,
208 uplcom_attach, uplcom_detach, uplcom_activate, NULL, uplcom_childdet);
209
210 int
211 uplcom_match(device_t parent, cfdata_t match, void *aux)
212 {
213 struct usb_attach_arg *uaa = aux;
214
215 return uplcom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
216 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
217 }
218
219 void
220 uplcom_attach(device_t parent, device_t self, void *aux)
221 {
222 struct uplcom_softc *sc = device_private(self);
223 struct usb_attach_arg *uaa = aux;
224 struct usbd_device *dev = uaa->uaa_device;
225 usb_device_descriptor_t *ddesc;
226 usb_config_descriptor_t *cdesc;
227 usb_interface_descriptor_t *id;
228 usb_endpoint_descriptor_t *ed;
229 char *devinfop;
230 const char *devname = device_xname(self);
231 usbd_status err;
232 int i;
233 struct ucom_attach_args ucaa;
234
235 sc->sc_dev = self;
236
237 aprint_naive("\n");
238 aprint_normal("\n");
239
240 devinfop = usbd_devinfo_alloc(dev, 0);
241 aprint_normal_dev(self, "%s\n", devinfop);
242 usbd_devinfo_free(devinfop);
243
244 sc->sc_udev = dev;
245
246 DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
247
248 /* initialize endpoints */
249 ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
250 sc->sc_intr_number = -1;
251 sc->sc_intr_pipe = NULL;
252
253 /* Move the device into the configured state. */
254 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
255 if (err) {
256 aprint_error("\n%s: failed to set configuration, err=%s\n",
257 devname, usbd_errstr(err));
258 sc->sc_dying = 1;
259 return;
260 }
261
262 /* determine chip type */
263 ddesc = usbd_get_device_descriptor(dev);
264 if (ddesc->bDeviceClass != UDCLASS_COMM &&
265 ddesc->bMaxPacketSize == 0x40)
266 sc->sc_type = UPLCOM_TYPE_HX;
267
268 #ifdef UPLCOM_DEBUG
269 /* print the chip type */
270 if (sc->sc_type == UPLCOM_TYPE_HX) {
271 DPRINTF(("uplcom_attach: chiptype HX\n"));
272 } else {
273 DPRINTF(("uplcom_attach: chiptype 0\n"));
274 }
275 #endif
276
277 /* Move the device into the configured state. */
278 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
279 if (err) {
280 aprint_error_dev(self, "failed to set configuration: %s\n",
281 usbd_errstr(err));
282 sc->sc_dying = 1;
283 return;
284 }
285
286 /* get the config descriptor */
287 cdesc = usbd_get_config_descriptor(sc->sc_udev);
288
289 if (cdesc == NULL) {
290 aprint_error_dev(self,
291 "failed to get configuration descriptor\n");
292 sc->sc_dying = 1;
293 return;
294 }
295
296 /* get the (first/common) interface */
297 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
298 &sc->sc_iface);
299 if (err) {
300 aprint_error("\n%s: failed to get interface, err=%s\n",
301 devname, usbd_errstr(err));
302 sc->sc_dying = 1;
303 return;
304 }
305
306 /* Find the interrupt endpoints */
307
308 id = usbd_get_interface_descriptor(sc->sc_iface);
309 sc->sc_iface_number = id->bInterfaceNumber;
310
311 for (i = 0; i < id->bNumEndpoints; i++) {
312 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
313 if (ed == NULL) {
314 aprint_error_dev(self,
315 "no endpoint descriptor for %d\n", i);
316 sc->sc_dying = 1;
317 return;
318 }
319
320 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
321 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
322 sc->sc_intr_number = ed->bEndpointAddress;
323 sc->sc_isize = UGETW(ed->wMaxPacketSize);
324 }
325 }
326
327 if (sc->sc_intr_number== -1) {
328 aprint_error_dev(self, "Could not find interrupt in\n");
329 sc->sc_dying = 1;
330 return;
331 }
332
333 /* keep interface for interrupt */
334 sc->sc_intr_iface = sc->sc_iface;
335
336 /*
337 * USB-RSAQ1 has two interface
338 *
339 * USB-RSAQ1 | USB-RSAQ2
340 * -----------------+-----------------
341 * Interface 0 |Interface 0
342 * Interrupt(0x81) | Interrupt(0x81)
343 * -----------------+ BulkIN(0x02)
344 * Interface 1 | BulkOUT(0x83)
345 * BulkIN(0x02) |
346 * BulkOUT(0x83) |
347 */
348 if (cdesc->bNumInterface == 2) {
349 err = usbd_device2interface_handle(dev,
350 UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
351 if (err) {
352 aprint_error("\n%s: failed to get second interface, "
353 "err=%s\n", devname, usbd_errstr(err));
354 sc->sc_dying = 1;
355 return;
356 }
357 }
358
359 /* Find the bulk{in,out} endpoints */
360
361 id = usbd_get_interface_descriptor(sc->sc_iface);
362 sc->sc_iface_number = id->bInterfaceNumber;
363
364 for (i = 0; i < id->bNumEndpoints; i++) {
365 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
366 if (ed == NULL) {
367 aprint_error_dev(self,
368 "no endpoint descriptor for %d\n", i);
369 sc->sc_dying = 1;
370 return;
371 }
372
373 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
374 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
375 ucaa.ucaa_bulkin = ed->bEndpointAddress;
376 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
377 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
378 ucaa.ucaa_bulkout = ed->bEndpointAddress;
379 }
380 }
381
382 if (ucaa.ucaa_bulkin == -1) {
383 aprint_error_dev(self, "Could not find data bulk in\n");
384 sc->sc_dying = 1;
385 return;
386 }
387
388 if (ucaa.ucaa_bulkout == -1) {
389 aprint_error_dev(self, "Could not find data bulk out\n");
390 sc->sc_dying = 1;
391 return;
392 }
393
394 sc->sc_dtr = sc->sc_rts = -1;
395 ucaa.ucaa_portno = UCOM_UNK_PORTNO;
396 /* ucaa_bulkin, ucaa_bulkout set above */
397 ucaa.ucaa_ibufsize = UPLCOMIBUFSIZE;
398 ucaa.ucaa_obufsize = UPLCOMOBUFSIZE;
399 ucaa.ucaa_ibufsizepad = UPLCOMIBUFSIZE;
400 ucaa.ucaa_opkthdrlen = 0;
401 ucaa.ucaa_device = dev;
402 ucaa.ucaa_iface = sc->sc_iface;
403 ucaa.ucaa_methods = &uplcom_methods;
404 ucaa.ucaa_arg = sc;
405 ucaa.ucaa_info = NULL;
406
407 err = uplcom_reset(sc);
408
409 if (err) {
410 aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err));
411 sc->sc_dying = 1;
412 return;
413 }
414
415 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
416
417 DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
418 ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number ));
419 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
420 ucomprint, ucomsubmatch);
421
422 if (!pmf_device_register(self, NULL, NULL))
423 aprint_error_dev(self, "couldn't establish power handler\n");
424
425 return;
426 }
427
428 void
429 uplcom_childdet(device_t self, device_t child)
430 {
431 struct uplcom_softc *sc = device_private(self);
432
433 KASSERT(sc->sc_subdev == child);
434 sc->sc_subdev = NULL;
435 }
436
437 int
438 uplcom_detach(device_t self, int flags)
439 {
440 struct uplcom_softc *sc = device_private(self);
441 int rv = 0;
442
443 DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
444
445 if (sc->sc_intr_pipe != NULL) {
446 usbd_abort_pipe(sc->sc_intr_pipe);
447 usbd_close_pipe(sc->sc_intr_pipe);
448 kmem_free(sc->sc_intr_buf, sc->sc_isize);
449 sc->sc_intr_pipe = NULL;
450 }
451
452 sc->sc_dying = 1;
453 if (sc->sc_subdev != NULL)
454 rv = config_detach(sc->sc_subdev, flags);
455
456 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
457
458 if (rv == 0)
459 pmf_device_deregister(self);
460
461 return rv;
462 }
463
464 int
465 uplcom_activate(device_t self, enum devact act)
466 {
467 struct uplcom_softc *sc = device_private(self);
468
469 switch (act) {
470 case DVACT_DEACTIVATE:
471 sc->sc_dying = 1;
472 return 0;
473 default:
474 return EOPNOTSUPP;
475 }
476 }
477
478 usbd_status
479 uplcom_reset(struct uplcom_softc *sc)
480 {
481 usb_device_request_t req;
482 usbd_status err;
483
484 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
485 req.bRequest = UPLCOM_SET_REQUEST;
486 USETW(req.wValue, 0);
487 USETW(req.wIndex, sc->sc_iface_number);
488 USETW(req.wLength, 0);
489
490 err = usbd_do_request(sc->sc_udev, &req, 0);
491 if (err)
492 return EIO;
493
494 return 0;
495 }
496
497 struct pl2303x_init {
498 uint8_t req_type;
499 uint8_t request;
500 uint16_t value;
501 uint16_t index;
502 };
503
504 static const struct pl2303x_init pl2303x[] = {
505 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 },
506 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0 },
507 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 },
508 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0 },
509 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 },
510 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1 },
511 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0 },
512 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0 },
513 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1 },
514 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0 },
515 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44 }
516 };
517 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
518
519 static usbd_status
520 uplcom_pl2303x_init(struct uplcom_softc *sc)
521 {
522 usb_device_request_t req;
523 usbd_status err;
524 int i;
525
526 for (i = 0; i < N_PL2302X_INIT; i++) {
527 char buf[1];
528 void *b;
529
530 req.bmRequestType = pl2303x[i].req_type;
531 req.bRequest = pl2303x[i].request;
532 USETW(req.wValue, pl2303x[i].value);
533 USETW(req.wIndex, pl2303x[i].index);
534 if (UT_GET_DIR(req.bmRequestType) == UT_READ) {
535 b = buf;
536 USETW(req.wLength, sizeof(buf));
537 } else {
538 b = NULL;
539 USETW(req.wLength, 0);
540 }
541
542 err = usbd_do_request(sc->sc_udev, &req, b);
543 if (err) {
544 aprint_error_dev(sc->sc_dev,
545 "uplcom_pl2303x_init failed: %s\n",
546 usbd_errstr(err));
547 return EIO;
548 }
549 }
550
551 return 0;
552 }
553
554 void
555 uplcom_set_line_state(struct uplcom_softc *sc)
556 {
557 usb_device_request_t req;
558 int ls;
559
560 /* make sure we have initialized state for sc_dtr and sc_rts */
561 if (sc->sc_dtr == -1)
562 sc->sc_dtr = 0;
563 if (sc->sc_rts == -1)
564 sc->sc_rts = 0;
565
566 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
567 (sc->sc_rts ? UCDC_LINE_RTS : 0);
568
569 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
570 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
571 USETW(req.wValue, ls);
572 USETW(req.wIndex, sc->sc_iface_number);
573 USETW(req.wLength, 0);
574
575 (void)usbd_do_request(sc->sc_udev, &req, 0);
576 }
577
578 void
579 uplcom_set(void *addr, int portno, int reg, int onoff)
580 {
581 struct uplcom_softc *sc = addr;
582
583 switch (reg) {
584 case UCOM_SET_DTR:
585 uplcom_dtr(sc, onoff);
586 break;
587 case UCOM_SET_RTS:
588 uplcom_rts(sc, onoff);
589 break;
590 case UCOM_SET_BREAK:
591 uplcom_break(sc, onoff);
592 break;
593 default:
594 break;
595 }
596 }
597
598 void
599 uplcom_dtr(struct uplcom_softc *sc, int onoff)
600 {
601
602 DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
603
604 if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
605 return;
606
607 sc->sc_dtr = !!onoff;
608
609 uplcom_set_line_state(sc);
610 }
611
612 void
613 uplcom_rts(struct uplcom_softc *sc, int onoff)
614 {
615 DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
616
617 if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
618 return;
619
620 sc->sc_rts = !!onoff;
621
622 uplcom_set_line_state(sc);
623 }
624
625 void
626 uplcom_break(struct uplcom_softc *sc, int onoff)
627 {
628 usb_device_request_t req;
629
630 DPRINTF(("uplcom_break: onoff=%d\n", onoff));
631
632 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
633 req.bRequest = UCDC_SEND_BREAK;
634 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
635 USETW(req.wIndex, sc->sc_iface_number);
636 USETW(req.wLength, 0);
637
638 (void)usbd_do_request(sc->sc_udev, &req, 0);
639 }
640
641 usbd_status
642 uplcom_set_crtscts(struct uplcom_softc *sc)
643 {
644 usb_device_request_t req;
645 usbd_status err;
646
647 DPRINTF(("uplcom_set_crtscts: on\n"));
648
649 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
650 req.bRequest = UPLCOM_SET_REQUEST;
651 USETW(req.wValue, 0);
652 if (sc->sc_type == UPLCOM_TYPE_HX)
653 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
654 else
655 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
656 USETW(req.wLength, 0);
657
658 err = usbd_do_request(sc->sc_udev, &req, 0);
659 if (err) {
660 DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
661 usbd_errstr(err)));
662 return err;
663 }
664
665 return USBD_NORMAL_COMPLETION;
666 }
667
668 usbd_status
669 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
670 {
671 usb_device_request_t req;
672 usbd_status err;
673
674 DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
675 UGETDW(state->dwDTERate), state->bCharFormat,
676 state->bParityType, state->bDataBits));
677
678 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
679 DPRINTF(("uplcom_set_line_coding: already set\n"));
680 return USBD_NORMAL_COMPLETION;
681 }
682
683 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
684 req.bRequest = UCDC_SET_LINE_CODING;
685 USETW(req.wValue, 0);
686 USETW(req.wIndex, sc->sc_iface_number);
687 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
688
689 err = usbd_do_request(sc->sc_udev, &req, state);
690 if (err) {
691 DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
692 usbd_errstr(err)));
693 return err;
694 }
695
696 sc->sc_line_state = *state;
697
698 return USBD_NORMAL_COMPLETION;
699 }
700
701 int
702 uplcom_param(void *addr, int portno, struct termios *t)
703 {
704 struct uplcom_softc *sc = addr;
705 usbd_status err;
706 usb_cdc_line_state_t ls;
707
708 DPRINTF(("uplcom_param: sc=%p\n", sc));
709
710 USETDW(ls.dwDTERate, t->c_ospeed);
711 if (ISSET(t->c_cflag, CSTOPB))
712 ls.bCharFormat = UCDC_STOP_BIT_2;
713 else
714 ls.bCharFormat = UCDC_STOP_BIT_1;
715 if (ISSET(t->c_cflag, PARENB)) {
716 if (ISSET(t->c_cflag, PARODD))
717 ls.bParityType = UCDC_PARITY_ODD;
718 else
719 ls.bParityType = UCDC_PARITY_EVEN;
720 } else
721 ls.bParityType = UCDC_PARITY_NONE;
722 switch (ISSET(t->c_cflag, CSIZE)) {
723 case CS5:
724 ls.bDataBits = 5;
725 break;
726 case CS6:
727 ls.bDataBits = 6;
728 break;
729 case CS7:
730 ls.bDataBits = 7;
731 break;
732 case CS8:
733 ls.bDataBits = 8;
734 break;
735 }
736
737 err = uplcom_set_line_coding(sc, &ls);
738 if (err) {
739 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
740 return EIO;
741 }
742
743 if (ISSET(t->c_cflag, CRTSCTS))
744 uplcom_set_crtscts(sc);
745
746 if (sc->sc_rts == -1 || sc->sc_dtr == -1)
747 uplcom_set_line_state(sc);
748
749 if (err) {
750 DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
751 return EIO;
752 }
753
754 return 0;
755 }
756
757 Static usbd_status
758 uplcom_vendor_control_write(struct usbd_device *dev, uint16_t value,
759 uint16_t index)
760 {
761 usb_device_request_t req;
762 usbd_status err;
763
764 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
765 req.bRequest = UPLCOM_SET_REQUEST;
766 USETW(req.wValue, value);
767 USETW(req.wIndex, index);
768 USETW(req.wLength, 0);
769
770 err = usbd_do_request(dev, &req, NULL);
771
772 if (err) {
773 DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
774 usbd_errstr(err), err));
775 }
776
777 return err;
778 }
779
780 int
781 uplcom_open(void *addr, int portno)
782 {
783 struct uplcom_softc *sc = addr;
784 usbd_status err;
785
786 if (sc->sc_dying)
787 return EIO;
788
789 DPRINTF(("uplcom_open: sc=%p\n", sc));
790
791 /* Some unknown device frobbing. */
792 if (sc->sc_type == UPLCOM_TYPE_HX)
793 uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
794 else
795 uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
796
797 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
798 sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
799 err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
800 USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
801 sc->sc_intr_buf, sc->sc_isize,
802 uplcom_intr, USBD_DEFAULT_INTERVAL);
803 if (err) {
804 DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
805 device_xname(sc->sc_dev), sc->sc_intr_number));
806 return EIO;
807 }
808 }
809
810 if (sc->sc_type == UPLCOM_TYPE_HX)
811 return uplcom_pl2303x_init(sc);
812
813 return 0;
814 }
815
816 void
817 uplcom_close(void *addr, int portno)
818 {
819 struct uplcom_softc *sc = addr;
820 int err;
821
822 if (sc->sc_dying)
823 return;
824
825 DPRINTF(("uplcom_close: close\n"));
826
827 if (sc->sc_intr_pipe != NULL) {
828 err = usbd_abort_pipe(sc->sc_intr_pipe);
829 if (err)
830 printf("%s: abort interrupt pipe failed: %s\n",
831 device_xname(sc->sc_dev), usbd_errstr(err));
832 err = usbd_close_pipe(sc->sc_intr_pipe);
833 if (err)
834 printf("%s: close interrupt pipe failed: %s\n",
835 device_xname(sc->sc_dev), usbd_errstr(err));
836 kmem_free(sc->sc_intr_buf, sc->sc_isize);
837 sc->sc_intr_pipe = NULL;
838 }
839 }
840
841 void
842 uplcom_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
843 {
844 struct uplcom_softc *sc = priv;
845 u_char *buf = sc->sc_intr_buf;
846 u_char pstatus;
847
848 if (sc->sc_dying)
849 return;
850
851 if (status != USBD_NORMAL_COMPLETION) {
852 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
853 return;
854
855 DPRINTF(("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
856 usbd_errstr(status)));
857 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
858 return;
859 }
860
861 DPRINTF(("%s: uplcom status = %02x\n", device_xname(sc->sc_dev),
862 buf[8]));
863
864 sc->sc_lsr = sc->sc_msr = 0;
865 pstatus = buf[8];
866 if (ISSET(pstatus, UPLCOM_N_SERIAL_CTS))
867 sc->sc_msr |= UMSR_CTS;
868 if (ISSET(pstatus, UCDC_N_SERIAL_RI))
869 sc->sc_msr |= UMSR_RI;
870 if (ISSET(pstatus, UCDC_N_SERIAL_DSR))
871 sc->sc_msr |= UMSR_DSR;
872 if (ISSET(pstatus, UCDC_N_SERIAL_DCD))
873 sc->sc_msr |= UMSR_DCD;
874 ucom_status_change(device_private(sc->sc_subdev));
875 }
876
877 void
878 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
879 {
880 struct uplcom_softc *sc = addr;
881
882 DPRINTF(("uplcom_get_status:\n"));
883
884 *lsr = sc->sc_lsr;
885 *msr = sc->sc_msr;
886 }
887
888 #if TODO
889 int
890 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
891 proc_t *p)
892 {
893 struct uplcom_softc *sc = addr;
894 int error = 0;
895
896 if (sc->sc_dying)
897 return EIO;
898
899 DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
900
901 switch (cmd) {
902 case TIOCNOTTY:
903 case TIOCMGET:
904 case TIOCMSET:
905 case USB_GET_CM_OVER_DATA:
906 case USB_SET_CM_OVER_DATA:
907 break;
908
909 default:
910 DPRINTF(("uplcom_ioctl: unknown\n"));
911 error = ENOTTY;
912 break;
913 }
914
915 return error;
916 }
917 #endif
918