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 USB_UPL(v,p,rl,rh,t) \
247 USB_VENDOR(v), USB_PRODUCT(p), USB_DEV_BCD_GTEQ(rl), \
248 USB_DEV_BCD_LTEQ(rh), USB_DRIVER_INFO(t)
250 static const struct usb_device_id uplcom_devs[] = {
252 {USB_UPL(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U257, 0, 0xFFFF, TYPE_PL2303X)},
253 /* I/O DATA USB-RSAQ */
254 {USB_UPL(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ, 0, 0xFFFF, TYPE_PL2303)},
255 /* I/O DATA USB-RSAQ2 */
256 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2, 0, 0xFFFF, TYPE_PL2303)},
257 /* I/O DATA USB-RSAQ3 */
258 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, 0, 0xFFFF, TYPE_PL2303X)},
259 /* PLANEX USB-RS232 URS-03 */
260 {USB_UPL(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A, 0, 0xFFFF, TYPE_PL2303)},
262 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0400, 0xFFFF, TYPE_PL2303X)},
263 /* ST Lab USB-SERIAL-4 */
264 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0x0300, 0x03FF, TYPE_PL2303X)},
265 /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
266 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, 0, 0x02FF, TYPE_PL2303)},
267 /* TDK USB-PHS Adapter UHA6400 */
268 {USB_UPL(USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400, 0, 0xFFFF, TYPE_PL2303)},
269 /* RATOC REX-USB60 */
270 {USB_UPL(USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60, 0, 0xFFFF, TYPE_PL2303)},
272 {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT, 0, 0xFFFF, TYPE_PL2303)},
273 {USB_UPL(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0, 0, 0xFFFF, TYPE_PL2303)},
274 /* Sagem USB-Serial Controller */
275 {USB_UPL(USB_VENDOR_SAGEM, USB_PRODUCT_SAGEM_USBSERIAL, 0, 0xFFFF, TYPE_PL2303X)},
276 /* Sony Ericsson USB Cable */
277 {USB_UPL(USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10, 0, 0xFFFF, TYPE_PL2303)},
278 /* SOURCENEXT KeikaiDenwa 8 */
279 {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8, 0, 0xFFFF, TYPE_PL2303)},
280 /* SOURCENEXT KeikaiDenwa 8 with charger */
281 {USB_UPL(USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG, 0, 0, TYPE_PL2303)},
282 /* HAL Corporation Crossam2+USB */
283 {USB_UPL(USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001, 0, 0xFFFF, TYPE_PL2303)},
284 /* Sitecom USB to Serial */
285 {USB_UPL(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_SERIAL, 0, 0xFFFF, TYPE_PL2303)},
286 /* Tripp-Lite U209-000-R */
287 {USB_UPL(USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209, 0, 0xFFFF, TYPE_PL2303X)},
288 {USB_UPL(USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_USBCABLE, 0, 0xFFFF, TYPE_PL2303)},
289 /* Prolific Pharos */
290 {USB_UPL(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PHAROS, 0, 0xFFFF, TYPE_PL2303)},
292 {USB_UPL(USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_WSIM, 0, 0xFFFF, TYPE_PL2303X)},
293 /* Mobile Action MA-620 Infrared Adapter */
294 {USB_UPL(USB_VENDOR_MOBILEACTION, USB_PRODUCT_MOBILEACTION_MA620, 0, 0xFFFF, TYPE_PL2303X)},
298 static device_method_t uplcom_methods[] = {
299 DEVMETHOD(device_probe, uplcom_probe),
300 DEVMETHOD(device_attach, uplcom_attach),
301 DEVMETHOD(device_detach, uplcom_detach),
305 static devclass_t uplcom_devclass;
307 static driver_t uplcom_driver = {
309 .methods = uplcom_methods,
310 .size = sizeof(struct uplcom_softc),
313 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
314 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
315 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
316 MODULE_VERSION(uplcom, UPLCOM_MODVER);
319 uplcom_probe(device_t dev)
321 struct usb_attach_arg *uaa = device_get_ivars(dev);
325 if (uaa->usb_mode != USB_MODE_HOST) {
328 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
331 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
334 return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
338 uplcom_attach(device_t dev)
340 struct usb_attach_arg *uaa = device_get_ivars(dev);
341 struct uplcom_softc *sc = device_get_softc(dev);
342 struct usb_interface *iface;
343 struct usb_interface_descriptor *id;
348 device_set_usb_desc(dev);
349 mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
351 DPRINTF("sc = %p\n", sc);
353 sc->sc_chiptype = USB_GET_DRIVER_INFO(uaa);
354 sc->sc_udev = uaa->device;
356 DPRINTF("chiptype: %s\n",
357 (sc->sc_chiptype == TYPE_PL2303X) ?
361 * USB-RSAQ1 has two interface
363 * USB-RSAQ1 | USB-RSAQ2
364 * -----------------+-----------------
365 * Interface 0 |Interface 0
366 * Interrupt(0x81) | Interrupt(0x81)
367 * -----------------+ BulkIN(0x02)
368 * Interface 1 | BulkOUT(0x83)
373 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
374 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
376 iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
378 id = usbd_get_interface_descriptor(iface);
380 device_printf(dev, "no interface descriptor (2)\n");
383 sc->sc_data_iface_no = id->bInterfaceNumber;
384 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
385 usbd_set_parent_iface(uaa->device,
386 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
388 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
389 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
392 error = usbd_transfer_setup(uaa->device,
393 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
394 UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
396 DPRINTF("one or more missing USB endpoints, "
397 "error=%s\n", usbd_errstr(error));
400 error = uplcom_reset(sc, uaa->device);
402 device_printf(dev, "reset failed, error=%s\n",
406 /* clear stall at first run */
407 mtx_lock(&sc->sc_mtx);
408 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
409 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
410 mtx_unlock(&sc->sc_mtx);
412 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
413 &uplcom_callback, &sc->sc_mtx);
418 * do the initialization during attach so that the system does not
421 if (sc->sc_chiptype == TYPE_PL2303X) {
422 if (uplcom_pl2303x_init(uaa->device)) {
423 device_printf(dev, "init failed\n");
435 uplcom_detach(device_t dev)
437 struct uplcom_softc *sc = device_get_softc(dev);
439 DPRINTF("sc=%p\n", sc);
441 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
442 usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
443 mtx_destroy(&sc->sc_mtx);
449 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
451 struct usb_device_request req;
453 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
454 req.bRequest = UPLCOM_SET_REQUEST;
455 USETW(req.wValue, 0);
456 req.wIndex[0] = sc->sc_data_iface_no;
458 USETW(req.wLength, 0);
460 return (usbd_do_request(udev, NULL, &req, NULL));
463 struct pl2303x_init {
471 static const struct pl2303x_init pl2303x[] = {
472 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
473 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0},
474 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
475 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
476 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
477 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0},
478 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1},
479 {UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1},
480 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0},
481 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0},
482 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0},
483 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0},
484 {UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0},
487 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
490 uplcom_pl2303x_init(struct usb_device *udev)
492 struct usb_device_request req;
497 for (i = 0; i != N_PL2302X_INIT; i++) {
498 req.bmRequestType = pl2303x[i].req_type;
499 req.bRequest = pl2303x[i].request;
500 USETW(req.wValue, pl2303x[i].value);
501 USETW(req.wIndex, pl2303x[i].index);
502 USETW(req.wLength, pl2303x[i].length);
504 err = usbd_do_request(udev, NULL, &req, buf);
506 DPRINTF("error=%s\n", usbd_errstr(err));
514 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
516 struct uplcom_softc *sc = ucom->sc_parent;
517 struct usb_device_request req;
519 DPRINTF("onoff = %d\n", onoff);
522 sc->sc_line |= UCDC_LINE_DTR;
524 sc->sc_line &= ~UCDC_LINE_DTR;
526 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
527 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
528 USETW(req.wValue, sc->sc_line);
529 req.wIndex[0] = sc->sc_data_iface_no;
531 USETW(req.wLength, 0);
533 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
534 &req, NULL, 0, 1000);
538 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
540 struct uplcom_softc *sc = ucom->sc_parent;
541 struct usb_device_request req;
543 DPRINTF("onoff = %d\n", onoff);
546 sc->sc_line |= UCDC_LINE_RTS;
548 sc->sc_line &= ~UCDC_LINE_RTS;
550 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
551 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
552 USETW(req.wValue, sc->sc_line);
553 req.wIndex[0] = sc->sc_data_iface_no;
555 USETW(req.wLength, 0);
557 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
558 &req, NULL, 0, 1000);
562 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
564 struct uplcom_softc *sc = ucom->sc_parent;
565 struct usb_device_request req;
568 DPRINTF("onoff = %d\n", onoff);
570 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
572 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
573 req.bRequest = UCDC_SEND_BREAK;
574 USETW(req.wValue, temp);
575 req.wIndex[0] = sc->sc_data_iface_no;
577 USETW(req.wLength, 0);
579 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
580 &req, NULL, 0, 1000);
583 static const int32_t uplcom_rates[] = {
584 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
585 19200, 28800, 38400, 57600, 115200,
587 * Higher speeds are probably possible. PL2303X supports up to
588 * 6Mb and can set any rate
590 230400, 460800, 614400, 921600, 1228800
593 #define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
596 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
602 /* check requested baud rate */
606 if (i != N_UPLCOM_RATES) {
607 if (uplcom_rates[i] == t->c_ospeed) {
611 DPRINTF("invalid baud rate (%d)\n", t->c_ospeed);
620 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
622 struct uplcom_softc *sc = ucom->sc_parent;
623 struct usb_cdc_line_state ls;
624 struct usb_device_request req;
626 DPRINTF("sc = %p\n", sc);
628 bzero(&ls, sizeof(ls));
630 USETDW(ls.dwDTERate, t->c_ospeed);
632 if (t->c_cflag & CSTOPB) {
633 ls.bCharFormat = UCDC_STOP_BIT_2;
635 ls.bCharFormat = UCDC_STOP_BIT_1;
638 if (t->c_cflag & PARENB) {
639 if (t->c_cflag & PARODD) {
640 ls.bParityType = UCDC_PARITY_ODD;
642 ls.bParityType = UCDC_PARITY_EVEN;
645 ls.bParityType = UCDC_PARITY_NONE;
648 switch (t->c_cflag & CSIZE) {
663 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
664 UGETDW(ls.dwDTERate), ls.bCharFormat,
665 ls.bParityType, ls.bDataBits);
667 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
668 req.bRequest = UCDC_SET_LINE_CODING;
669 USETW(req.wValue, 0);
670 req.wIndex[0] = sc->sc_data_iface_no;
672 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
674 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
677 if (t->c_cflag & CRTSCTS) {
679 DPRINTF("crtscts = on\n");
681 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
682 req.bRequest = UPLCOM_SET_REQUEST;
683 USETW(req.wValue, 0);
684 if (sc->sc_chiptype == TYPE_PL2303X)
685 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
687 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
688 USETW(req.wLength, 0);
690 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
691 &req, NULL, 0, 1000);
693 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
694 req.bRequest = UPLCOM_SET_REQUEST;
695 USETW(req.wValue, 0);
696 USETW(req.wIndex, 0);
697 USETW(req.wLength, 0);
698 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
699 &req, NULL, 0, 1000);
704 uplcom_start_read(struct ucom_softc *ucom)
706 struct uplcom_softc *sc = ucom->sc_parent;
708 /* start interrupt endpoint */
709 usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
711 /* start read endpoint */
712 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
716 uplcom_stop_read(struct ucom_softc *ucom)
718 struct uplcom_softc *sc = ucom->sc_parent;
720 /* stop interrupt endpoint */
721 usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
723 /* stop read endpoint */
724 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
728 uplcom_start_write(struct ucom_softc *ucom)
730 struct uplcom_softc *sc = ucom->sc_parent;
732 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
736 uplcom_stop_write(struct ucom_softc *ucom)
738 struct uplcom_softc *sc = ucom->sc_parent;
740 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
744 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
746 struct uplcom_softc *sc = ucom->sc_parent;
755 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
757 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
758 struct usb_page_cache *pc;
762 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
764 switch (USB_GET_STATE(xfer)) {
765 case USB_ST_TRANSFERRED:
767 DPRINTF("actlen = %u\n", actlen);
771 pc = usbd_xfer_get_frame(xfer, 0);
772 usbd_copy_out(pc, 0, buf, sizeof(buf));
774 DPRINTF("status = 0x%02x\n", buf[8]);
779 if (buf[8] & RSAQ_STATUS_CTS) {
780 sc->sc_msr |= SER_CTS;
782 if (buf[8] & RSAQ_STATUS_DSR) {
783 sc->sc_msr |= SER_DSR;
785 if (buf[8] & RSAQ_STATUS_DCD) {
786 sc->sc_msr |= SER_DCD;
788 ucom_status_change(&sc->sc_ucom);
792 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
793 usbd_transfer_submit(xfer);
797 if (error != USB_ERR_CANCELLED) {
798 /* try to clear stall first */
799 usbd_xfer_set_stall(xfer);
807 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
809 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
810 struct usb_page_cache *pc;
813 switch (USB_GET_STATE(xfer)) {
815 case USB_ST_TRANSFERRED:
817 pc = usbd_xfer_get_frame(xfer, 0);
818 if (ucom_get_data(&sc->sc_ucom, pc, 0,
819 UPLCOM_BULK_BUF_SIZE, &actlen)) {
821 DPRINTF("actlen = %d\n", actlen);
823 usbd_xfer_set_frame_len(xfer, 0, actlen);
824 usbd_transfer_submit(xfer);
829 if (error != USB_ERR_CANCELLED) {
830 /* try to clear stall first */
831 usbd_xfer_set_stall(xfer);
839 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
841 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
842 struct usb_page_cache *pc;
845 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
847 switch (USB_GET_STATE(xfer)) {
848 case USB_ST_TRANSFERRED:
849 pc = usbd_xfer_get_frame(xfer, 0);
850 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
854 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
855 usbd_transfer_submit(xfer);
859 if (error != USB_ERR_CANCELLED) {
860 /* try to clear stall first */
861 usbd_xfer_set_stall(xfer);
869 uplcom_poll(struct ucom_softc *ucom)
871 struct uplcom_softc *sc = ucom->sc_parent;
872 usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);