1 /* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */
7 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 2001 The NetBSD Foundation, Inc.
34 * All rights reserved.
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Ichiro FUKUHARA (ichiro@ichiro.org).
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the NetBSD
50 * Foundation, Inc. and its contributors.
51 * 4. Neither the name of The NetBSD Foundation nor the names of its
52 * contributors may be used to endorse or promote products derived
53 * from this software without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
69 * This driver supports several USB-to-RS232 serial adapters driven by
70 * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
71 * bridge chip. The adapters are sold under many different brand
74 * Datasheets are available at Prolific www site at
75 * http://www.prolific.com.tw. The datasheets don't contain full
76 * programming information for the chip.
78 * PL-2303HX is probably programmed the same as PL-2303X.
80 * There are several differences between PL-2303 and PL-2303(H)X.
81 * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
82 * different command for controlling CRTSCTS and needs special
83 * sequence of commands for initialization which aren't also
84 * documented in the datasheet.
87 #include <sys/stdint.h>
88 #include <sys/stddef.h>
89 #include <sys/param.h>
90 #include <sys/queue.h>
91 #include <sys/types.h>
92 #include <sys/systm.h>
93 #include <sys/kernel.h>
95 #include <sys/linker_set.h>
96 #include <sys/module.h>
98 #include <sys/mutex.h>
99 #include <sys/condvar.h>
100 #include <sys/sysctl.h>
102 #include <sys/unistd.h>
103 #include <sys/callout.h>
104 #include <sys/malloc.h>
105 #include <sys/priv.h>
107 #include <dev/usb/usb.h>
108 #include <dev/usb/usbdi.h>
109 #include <dev/usb/usbdi_util.h>
110 #include <dev/usb/usb_cdc.h>
113 #define USB_DEBUG_VAR uplcom_debug
114 #include <dev/usb/usb_debug.h>
115 #include <dev/usb/usb_process.h>
117 #include <dev/usb/serial/usb_serial.h>
120 static int uplcom_debug = 0;
122 SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
123 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
124 &uplcom_debug, 0, "Debug level");
127 #define UPLCOM_MODVER 1 /* module version */
129 #define UPLCOM_CONFIG_INDEX 0
130 #define UPLCOM_IFACE_INDEX 0
131 #define UPLCOM_SECOND_IFACE_INDEX 1
133 #ifndef UPLCOM_INTR_INTERVAL
134 #define UPLCOM_INTR_INTERVAL 0 /* default */
137 #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */
139 #define UPLCOM_SET_REQUEST 0x01
140 #define UPLCOM_SET_CRTSCTS 0x41
141 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
142 #define RSAQ_STATUS_CTS 0x80
143 #define RSAQ_STATUS_DSR 0x02
144 #define RSAQ_STATUS_DCD 0x01
146 #define TYPE_PL2303 0
147 #define TYPE_PL2303X 1
156 struct uplcom_softc {
157 struct ucom_super_softc sc_super_ucom;
158 struct ucom_softc sc_ucom;
160 struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
161 struct usb_device *sc_udev;
166 uint8_t sc_lsr; /* local status register */
167 uint8_t sc_msr; /* uplcom status register */
168 uint8_t sc_chiptype; /* type of chip */
169 uint8_t sc_ctrl_iface_no;
170 uint8_t sc_data_iface_no;
171 uint8_t sc_iface_index[2];
176 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
177 static int uplcom_pl2303x_init(struct usb_device *);
178 static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
179 static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
180 static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
181 static int uplcom_pre_param(struct ucom_softc *, struct termios *);
182 static void uplcom_cfg_param(struct ucom_softc *, struct termios *);
183 static void uplcom_start_read(struct ucom_softc *);
184 static void uplcom_stop_read(struct ucom_softc *);
185 static void uplcom_start_write(struct ucom_softc *);
186 static void uplcom_stop_write(struct ucom_softc *);
187 static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
189 static void uplcom_poll(struct ucom_softc *ucom);
191 static device_probe_t uplcom_probe;
192 static device_attach_t uplcom_attach;
193 static device_detach_t uplcom_detach;
195 static usb_callback_t uplcom_intr_callback;
196 static usb_callback_t uplcom_write_callback;
197 static usb_callback_t uplcom_read_callback;
199 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
201 [UPLCOM_BULK_DT_WR] = {
203 .endpoint = UE_ADDR_ANY,
204 .direction = UE_DIR_OUT,
205 .bufsize = UPLCOM_BULK_BUF_SIZE,
206 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
207 .callback = &uplcom_write_callback,
211 [UPLCOM_BULK_DT_RD] = {
213 .endpoint = UE_ADDR_ANY,
214 .direction = UE_DIR_IN,
215 .bufsize = UPLCOM_BULK_BUF_SIZE,
216 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
217 .callback = &uplcom_read_callback,
221 [UPLCOM_INTR_DT_RD] = {
222 .type = UE_INTERRUPT,
223 .endpoint = UE_ADDR_ANY,
224 .direction = UE_DIR_IN,
225 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
226 .bufsize = 0, /* use wMaxPacketSize */
227 .callback = &uplcom_intr_callback,
232 static struct ucom_callback uplcom_callback = {
233 .ucom_cfg_get_status = &uplcom_cfg_get_status,
234 .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
235 .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
236 .ucom_cfg_set_break = &uplcom_cfg_set_break,
237 .ucom_cfg_param = &uplcom_cfg_param,
238 .ucom_pre_param = &uplcom_pre_param,
239 .ucom_start_read = &uplcom_start_read,
240 .ucom_stop_read = &uplcom_stop_read,
241 .ucom_start_write = &uplcom_start_write,
242 .ucom_stop_write = &uplcom_stop_write,
243 .ucom_poll = &uplcom_poll,
246 #define UPLCOM_DEV(v,p,rl,rh,t) \
247 { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p), \
248 USB_DEV_BCD_GTEQ(rl), USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(TYPE_##t) }
250 static const struct usb_device_id uplcom_devs[] = {
252 UPLCOM_DEV(BELKIN, F5U257, 0, 0xFFFF, PL2303X),
253 /* I/O DATA USB-RSAQ */
254 UPLCOM_DEV(IODATA, USBRSAQ, 0, 0xFFFF, PL2303),
255 /* I/O DATA USB-RSAQ2 */
256 UPLCOM_DEV(PROLIFIC, RSAQ2, 0, 0xFFFF, PL2303),
257 /* I/O DATA USB-RSAQ3 */
258 UPLCOM_DEV(PROLIFIC, RSAQ3, 0, 0xFFFF, PL2303X),
259 /* PLANEX USB-RS232 URS-03 */
260 UPLCOM_DEV(ATEN, UC232A, 0, 0xFFFF, PL2303),
262 UPLCOM_DEV(PROLIFIC, PL2303, 0x0400, 0xFFFF, PL2303X),
263 /* ST Lab USB-SERIAL-4 */
264 UPLCOM_DEV(PROLIFIC, PL2303, 0x0300, 0x03FF, PL2303X),
265 /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
266 UPLCOM_DEV(PROLIFIC, PL2303, 0, 0x02FF, PL2303),
267 /* TDK USB-PHS Adapter UHA6400 */
268 UPLCOM_DEV(TDK, UHA6400, 0, 0xFFFF, PL2303),
269 /* RATOC REX-USB60 */
270 UPLCOM_DEV(RATOC, REXUSB60, 0, 0xFFFF, PL2303),
272 UPLCOM_DEV(ELECOM, UCSGT, 0, 0xFFFF, PL2303),
273 UPLCOM_DEV(ELECOM, UCSGT0, 0, 0xFFFF, PL2303),
274 /* Sagem USB-Serial Controller */
275 UPLCOM_DEV(SAGEM, USBSERIAL, 0, 0xFFFF, PL2303X),
276 /* Sony Ericsson USB Cable */
277 UPLCOM_DEV(SONYERICSSON, DCU10, 0, 0xFFFF, PL2303),
278 /* SOURCENEXT KeikaiDenwa 8 */
279 UPLCOM_DEV(SOURCENEXT, KEIKAI8, 0, 0xFFFF, PL2303),
280 /* SOURCENEXT KeikaiDenwa 8 with charger */
281 UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG, 0, 0, PL2303),
282 /* HAL Corporation Crossam2+USB */
283 UPLCOM_DEV(HAL, IMR001, 0, 0xFFFF, PL2303),
284 /* Sitecom USB to Serial */
285 UPLCOM_DEV(SITECOM, SERIAL, 0, 0xFFFF, PL2303),
286 /* Tripp-Lite U209-000-R */
287 UPLCOM_DEV(TRIPPLITE, U209, 0, 0xFFFF, PL2303X),
288 UPLCOM_DEV(RADIOSHACK, USBCABLE, 0, 0xFFFF, PL2303),
289 /* Prolific Pharos */
290 UPLCOM_DEV(PROLIFIC, PHAROS, 0, 0xFFFF, PL2303),
292 UPLCOM_DEV(PROLIFIC2, WSIM, 0, 0xFFFF, PL2303X),
293 /* Mobile Action MA-620 Infrared Adapter */
294 UPLCOM_DEV(MOBILEACTION, MA620, 0, 0xFFFF, PL2303X),
295 /* Corega CG-USBRS232R */
296 UPLCOM_DEV(COREGA, CGUSBRS232R, 0, 0xFFFF, PL2303X),
300 static device_method_t uplcom_methods[] = {
301 DEVMETHOD(device_probe, uplcom_probe),
302 DEVMETHOD(device_attach, uplcom_attach),
303 DEVMETHOD(device_detach, uplcom_detach),
307 static devclass_t uplcom_devclass;
309 static driver_t uplcom_driver = {
311 .methods = uplcom_methods,
312 .size = sizeof(struct uplcom_softc),
315 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
316 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
317 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
318 MODULE_VERSION(uplcom, UPLCOM_MODVER);
321 uplcom_probe(device_t dev)
323 struct usb_attach_arg *uaa = device_get_ivars(dev);
327 if (uaa->usb_mode != USB_MODE_HOST) {
330 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
333 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
336 return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
340 uplcom_attach(device_t dev)
342 struct usb_attach_arg *uaa = device_get_ivars(dev);
343 struct uplcom_softc *sc = device_get_softc(dev);
344 struct usb_interface *iface;
345 struct usb_interface_descriptor *id;
350 device_set_usb_desc(dev);
351 mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
353 DPRINTF("sc = %p\n", sc);
355 sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa);
356 sc->sc_udev = uaa->device;
358 DPRINTF("chiptype: %s\n",
359 (sc->sc_chiptype == TYPE_PL2303X) ?
363 * USB-RSAQ1 has two interface
365 * USB-RSAQ1 | USB-RSAQ2
366 * -----------------+-----------------
367 * Interface 0 |Interface 0
368 * Interrupt(0x81) | Interrupt(0x81)
369 * -----------------+ BulkIN(0x02)
370 * Interface 1 | BulkOUT(0x83)
375 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
376 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
378 iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
380 id = usbd_get_interface_descriptor(iface);
382 device_printf(dev, "no interface descriptor (2)\n");
385 sc->sc_data_iface_no = id->bInterfaceNumber;
386 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
387 usbd_set_parent_iface(uaa->device,
388 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
390 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
391 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
394 error = usbd_transfer_setup(uaa->device,
395 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
396 UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
398 DPRINTF("one or more missing USB endpoints, "
399 "error=%s\n", usbd_errstr(error));
402 error = uplcom_reset(sc, uaa->device);
404 device_printf(dev, "reset failed, error=%s\n",
408 /* clear stall at first run */
409 mtx_lock(&sc->sc_mtx);
410 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
411 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
412 mtx_unlock(&sc->sc_mtx);
414 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
415 &uplcom_callback, &sc->sc_mtx);
420 * do the initialization during attach so that the system does not
423 if (sc->sc_chiptype == TYPE_PL2303X) {
424 if (uplcom_pl2303x_init(uaa->device)) {
425 device_printf(dev, "init failed\n");
437 uplcom_detach(device_t dev)
439 struct uplcom_softc *sc = device_get_softc(dev);
441 DPRINTF("sc=%p\n", sc);
443 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
444 usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
445 mtx_destroy(&sc->sc_mtx);
451 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
453 struct usb_device_request req;
455 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
456 req.bRequest = UPLCOM_SET_REQUEST;
457 USETW(req.wValue, 0);
458 req.wIndex[0] = sc->sc_data_iface_no;
460 USETW(req.wLength, 0);
462 return (usbd_do_request(udev, NULL, &req, NULL));
465 struct pl2303x_init {
473 static const struct pl2303x_init pl2303x[] = {
474 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
475 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0},
476 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
477 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
478 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
479 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0},
480 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
481 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
482 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0},
483 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0},
484 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0},
485 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0},
486 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0},
489 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
492 uplcom_pl2303x_init(struct usb_device *udev)
494 struct usb_device_request req;
499 for (i = 0; i != N_PL2302X_INIT; i++) {
500 req.bmRequestType = pl2303x[i].req_type;
501 req.bRequest = pl2303x[i].request;
502 USETW(req.wValue, pl2303x[i].value);
503 USETW(req.wIndex, pl2303x[i].index);
504 USETW(req.wLength, pl2303x[i].length);
506 err = usbd_do_request(udev, NULL, &req, buf);
508 DPRINTF("error=%s\n", usbd_errstr(err));
516 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
518 struct uplcom_softc *sc = ucom->sc_parent;
519 struct usb_device_request req;
521 DPRINTF("onoff = %d\n", onoff);
524 sc->sc_line |= UCDC_LINE_DTR;
526 sc->sc_line &= ~UCDC_LINE_DTR;
528 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
529 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
530 USETW(req.wValue, sc->sc_line);
531 req.wIndex[0] = sc->sc_data_iface_no;
533 USETW(req.wLength, 0);
535 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
536 &req, NULL, 0, 1000);
540 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
542 struct uplcom_softc *sc = ucom->sc_parent;
543 struct usb_device_request req;
545 DPRINTF("onoff = %d\n", onoff);
548 sc->sc_line |= UCDC_LINE_RTS;
550 sc->sc_line &= ~UCDC_LINE_RTS;
552 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
553 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
554 USETW(req.wValue, sc->sc_line);
555 req.wIndex[0] = sc->sc_data_iface_no;
557 USETW(req.wLength, 0);
559 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
560 &req, NULL, 0, 1000);
564 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
566 struct uplcom_softc *sc = ucom->sc_parent;
567 struct usb_device_request req;
570 DPRINTF("onoff = %d\n", onoff);
572 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
574 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
575 req.bRequest = UCDC_SEND_BREAK;
576 USETW(req.wValue, temp);
577 req.wIndex[0] = sc->sc_data_iface_no;
579 USETW(req.wLength, 0);
581 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
582 &req, NULL, 0, 1000);
585 static const int32_t uplcom_rates[] = {
586 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
587 19200, 28800, 38400, 57600, 115200,
589 * Higher speeds are probably possible. PL2303X supports up to
590 * 6Mb and can set any rate
592 230400, 460800, 614400, 921600, 1228800
595 #define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
598 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
604 /* check requested baud rate */
608 if (i != N_UPLCOM_RATES) {
609 if (uplcom_rates[i] == t->c_ospeed) {
613 DPRINTF("invalid baud rate (%d)\n", t->c_ospeed);
622 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
624 struct uplcom_softc *sc = ucom->sc_parent;
625 struct usb_cdc_line_state ls;
626 struct usb_device_request req;
628 DPRINTF("sc = %p\n", sc);
630 bzero(&ls, sizeof(ls));
632 USETDW(ls.dwDTERate, t->c_ospeed);
634 if (t->c_cflag & CSTOPB) {
635 ls.bCharFormat = UCDC_STOP_BIT_2;
637 ls.bCharFormat = UCDC_STOP_BIT_1;
640 if (t->c_cflag & PARENB) {
641 if (t->c_cflag & PARODD) {
642 ls.bParityType = UCDC_PARITY_ODD;
644 ls.bParityType = UCDC_PARITY_EVEN;
647 ls.bParityType = UCDC_PARITY_NONE;
650 switch (t->c_cflag & CSIZE) {
665 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
666 UGETDW(ls.dwDTERate), ls.bCharFormat,
667 ls.bParityType, ls.bDataBits);
669 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
670 req.bRequest = UCDC_SET_LINE_CODING;
671 USETW(req.wValue, 0);
672 req.wIndex[0] = sc->sc_data_iface_no;
674 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
676 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
679 if (t->c_cflag & CRTSCTS) {
681 DPRINTF("crtscts = on\n");
683 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
684 req.bRequest = UPLCOM_SET_REQUEST;
685 USETW(req.wValue, 0);
686 if (sc->sc_chiptype == TYPE_PL2303X)
687 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
689 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
690 USETW(req.wLength, 0);
692 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
693 &req, NULL, 0, 1000);
695 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
696 req.bRequest = UPLCOM_SET_REQUEST;
697 USETW(req.wValue, 0);
698 USETW(req.wIndex, 0);
699 USETW(req.wLength, 0);
700 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
701 &req, NULL, 0, 1000);
706 uplcom_start_read(struct ucom_softc *ucom)
708 struct uplcom_softc *sc = ucom->sc_parent;
710 /* start interrupt endpoint */
711 usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
713 /* start read endpoint */
714 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
718 uplcom_stop_read(struct ucom_softc *ucom)
720 struct uplcom_softc *sc = ucom->sc_parent;
722 /* stop interrupt endpoint */
723 usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
725 /* stop read endpoint */
726 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
730 uplcom_start_write(struct ucom_softc *ucom)
732 struct uplcom_softc *sc = ucom->sc_parent;
734 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
738 uplcom_stop_write(struct ucom_softc *ucom)
740 struct uplcom_softc *sc = ucom->sc_parent;
742 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
746 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
748 struct uplcom_softc *sc = ucom->sc_parent;
757 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
759 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
760 struct usb_page_cache *pc;
764 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
766 switch (USB_GET_STATE(xfer)) {
767 case USB_ST_TRANSFERRED:
769 DPRINTF("actlen = %u\n", actlen);
773 pc = usbd_xfer_get_frame(xfer, 0);
774 usbd_copy_out(pc, 0, buf, sizeof(buf));
776 DPRINTF("status = 0x%02x\n", buf[8]);
781 if (buf[8] & RSAQ_STATUS_CTS) {
782 sc->sc_msr |= SER_CTS;
784 if (buf[8] & RSAQ_STATUS_DSR) {
785 sc->sc_msr |= SER_DSR;
787 if (buf[8] & RSAQ_STATUS_DCD) {
788 sc->sc_msr |= SER_DCD;
790 ucom_status_change(&sc->sc_ucom);
794 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
795 usbd_transfer_submit(xfer);
799 if (error != USB_ERR_CANCELLED) {
800 /* try to clear stall first */
801 usbd_xfer_set_stall(xfer);
809 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
811 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
812 struct usb_page_cache *pc;
815 switch (USB_GET_STATE(xfer)) {
817 case USB_ST_TRANSFERRED:
819 pc = usbd_xfer_get_frame(xfer, 0);
820 if (ucom_get_data(&sc->sc_ucom, pc, 0,
821 UPLCOM_BULK_BUF_SIZE, &actlen)) {
823 DPRINTF("actlen = %d\n", actlen);
825 usbd_xfer_set_frame_len(xfer, 0, actlen);
826 usbd_transfer_submit(xfer);
831 if (error != USB_ERR_CANCELLED) {
832 /* try to clear stall first */
833 usbd_xfer_set_stall(xfer);
841 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
843 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
844 struct usb_page_cache *pc;
847 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
849 switch (USB_GET_STATE(xfer)) {
850 case USB_ST_TRANSFERRED:
851 pc = usbd_xfer_get_frame(xfer, 0);
852 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
856 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
857 usbd_transfer_submit(xfer);
861 if (error != USB_ERR_CANCELLED) {
862 /* try to clear stall first */
863 usbd_xfer_set_stall(xfer);
871 uplcom_poll(struct ucom_softc *ucom)
873 struct uplcom_softc *sc = ucom->sc_parent;
874 usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);