1 /* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */
7 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
9 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2001 The NetBSD Foundation, Inc.
36 * All rights reserved.
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Ichiro FUKUHARA (ichiro@ichiro.org).
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
64 * This driver supports several USB-to-RS232 serial adapters driven by
65 * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
66 * bridge chip. The adapters are sold under many different brand
69 * Datasheets are available at Prolific www site at
70 * http://www.prolific.com.tw. The datasheets don't contain full
71 * programming information for the chip.
73 * PL-2303HX is probably programmed the same as PL-2303X.
75 * There are several differences between PL-2303 and PL-2303(H)X.
76 * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
77 * different command for controlling CRTSCTS and needs special
78 * sequence of commands for initialization which aren't also
79 * documented in the datasheet.
82 #include <sys/stdint.h>
83 #include <sys/stddef.h>
84 #include <sys/param.h>
85 #include <sys/queue.h>
86 #include <sys/types.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
90 #include <sys/module.h>
92 #include <sys/mutex.h>
93 #include <sys/condvar.h>
94 #include <sys/sysctl.h>
96 #include <sys/unistd.h>
97 #include <sys/callout.h>
98 #include <sys/malloc.h>
101 #include <dev/usb/usb.h>
102 #include <dev/usb/usbdi.h>
103 #include <dev/usb/usbdi_util.h>
104 #include <dev/usb/usb_cdc.h>
107 #define USB_DEBUG_VAR uplcom_debug
108 #include <dev/usb/usb_debug.h>
109 #include <dev/usb/usb_process.h>
111 #include <dev/usb/serial/usb_serial.h>
114 static int uplcom_debug = 0;
116 static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
117 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
118 &uplcom_debug, 0, "Debug level");
121 #define UPLCOM_MODVER 1 /* module version */
123 #define UPLCOM_CONFIG_INDEX 0
124 #define UPLCOM_IFACE_INDEX 0
125 #define UPLCOM_SECOND_IFACE_INDEX 1
127 #ifndef UPLCOM_INTR_INTERVAL
128 #define UPLCOM_INTR_INTERVAL 0 /* default */
131 #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */
133 #define UPLCOM_SET_REQUEST 0x01
134 #define UPLCOM_SET_CRTSCTS 0x41
135 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
136 #define RSAQ_STATUS_CTS 0x80
137 #define RSAQ_STATUS_OVERRUN_ERROR 0x40
138 #define RSAQ_STATUS_PARITY_ERROR 0x20
139 #define RSAQ_STATUS_FRAME_ERROR 0x10
140 #define RSAQ_STATUS_RING 0x08
141 #define RSAQ_STATUS_BREAK_ERROR 0x04
142 #define RSAQ_STATUS_DSR 0x02
143 #define RSAQ_STATUS_DCD 0x01
145 #define TYPE_PL2303 0
146 #define TYPE_PL2303HX 1
147 #define TYPE_PL2303HXD 2
149 #define UPLCOM_STATE_INDEX 8
158 struct uplcom_softc {
159 struct ucom_super_softc sc_super_ucom;
160 struct ucom_softc sc_ucom;
162 struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
163 struct usb_device *sc_udev;
168 uint8_t sc_lsr; /* local status register */
169 uint8_t sc_msr; /* uplcom status register */
170 uint8_t sc_chiptype; /* type of chip */
171 uint8_t sc_ctrl_iface_no;
172 uint8_t sc_data_iface_no;
173 uint8_t sc_iface_index[2];
178 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
179 static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
180 uint16_t, uint16_t, uint16_t);
181 static int uplcom_pl2303_init(struct usb_device *, uint8_t);
182 static void uplcom_free(struct ucom_softc *);
183 static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
184 static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
185 static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
186 static int uplcom_pre_param(struct ucom_softc *, struct termios *);
187 static void uplcom_cfg_param(struct ucom_softc *, struct termios *);
188 static void uplcom_start_read(struct ucom_softc *);
189 static void uplcom_stop_read(struct ucom_softc *);
190 static void uplcom_start_write(struct ucom_softc *);
191 static void uplcom_stop_write(struct ucom_softc *);
192 static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
194 static void uplcom_poll(struct ucom_softc *ucom);
196 static device_probe_t uplcom_probe;
197 static device_attach_t uplcom_attach;
198 static device_detach_t uplcom_detach;
199 static void uplcom_free_softc(struct uplcom_softc *);
201 static usb_callback_t uplcom_intr_callback;
202 static usb_callback_t uplcom_write_callback;
203 static usb_callback_t uplcom_read_callback;
205 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
207 [UPLCOM_BULK_DT_WR] = {
209 .endpoint = UE_ADDR_ANY,
210 .direction = UE_DIR_OUT,
211 .bufsize = UPLCOM_BULK_BUF_SIZE,
212 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
213 .callback = &uplcom_write_callback,
217 [UPLCOM_BULK_DT_RD] = {
219 .endpoint = UE_ADDR_ANY,
220 .direction = UE_DIR_IN,
221 .bufsize = UPLCOM_BULK_BUF_SIZE,
222 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
223 .callback = &uplcom_read_callback,
227 [UPLCOM_INTR_DT_RD] = {
228 .type = UE_INTERRUPT,
229 .endpoint = UE_ADDR_ANY,
230 .direction = UE_DIR_IN,
231 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
232 .bufsize = 0, /* use wMaxPacketSize */
233 .callback = &uplcom_intr_callback,
238 static struct ucom_callback uplcom_callback = {
239 .ucom_cfg_get_status = &uplcom_cfg_get_status,
240 .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
241 .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
242 .ucom_cfg_set_break = &uplcom_cfg_set_break,
243 .ucom_cfg_param = &uplcom_cfg_param,
244 .ucom_pre_param = &uplcom_pre_param,
245 .ucom_start_read = &uplcom_start_read,
246 .ucom_stop_read = &uplcom_stop_read,
247 .ucom_start_write = &uplcom_start_write,
248 .ucom_stop_write = &uplcom_stop_write,
249 .ucom_poll = &uplcom_poll,
250 .ucom_free = &uplcom_free,
253 #define UPLCOM_DEV(v,p) \
254 { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
256 static const STRUCT_USB_HOST_ID uplcom_devs[] = {
257 UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */
258 UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */
259 UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */
260 UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */
261 UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */
262 UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */
263 UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */
264 UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */
265 UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */
266 UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */
267 UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */
268 UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */
269 UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */
270 UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */
271 UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */
272 UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */
273 UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */
274 UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */
275 UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */
276 UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */
277 UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */
278 UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */
279 UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */
280 UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */
281 UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */
282 UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */
283 UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */
284 UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */
285 UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */
286 UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */
287 UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */
288 UPLCOM_DEV(PROLIFIC, MOTOROLA), /* Motorola cable */
289 UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */
290 UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */
291 UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */
292 UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */
293 UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */
294 UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */
295 UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */
296 UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */
297 UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */
298 UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */
299 UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */
300 UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */
301 UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */
302 UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */
303 UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */
304 UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */
305 UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */
306 UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */
307 UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */
308 UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */
309 UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */
310 UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */
311 UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */
312 UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */
313 UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */
314 UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */
315 UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */
316 UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */
320 static device_method_t uplcom_methods[] = {
321 DEVMETHOD(device_probe, uplcom_probe),
322 DEVMETHOD(device_attach, uplcom_attach),
323 DEVMETHOD(device_detach, uplcom_detach),
327 static devclass_t uplcom_devclass;
329 static driver_t uplcom_driver = {
331 .methods = uplcom_methods,
332 .size = sizeof(struct uplcom_softc),
335 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
336 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
337 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
338 MODULE_VERSION(uplcom, UPLCOM_MODVER);
339 USB_PNP_HOST_INFO(uplcom_devs);
342 uplcom_probe(device_t dev)
344 struct usb_attach_arg *uaa = device_get_ivars(dev);
348 if (uaa->usb_mode != USB_MODE_HOST) {
351 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
354 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
357 return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
361 uplcom_attach(device_t dev)
363 struct usb_attach_arg *uaa = device_get_ivars(dev);
364 struct uplcom_softc *sc = device_get_softc(dev);
365 struct usb_interface *iface;
366 struct usb_interface_descriptor *id;
367 struct usb_device_descriptor *dd;
372 device_set_usb_desc(dev);
373 mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
374 ucom_ref(&sc->sc_super_ucom);
376 DPRINTF("sc = %p\n", sc);
378 sc->sc_udev = uaa->device;
380 dd = usbd_get_device_descriptor(sc->sc_udev);
382 switch (UGETW(dd->bcdDevice)) {
384 sc->sc_chiptype = TYPE_PL2303HX;
385 /* or TA, that is HX with external crystal */
388 sc->sc_chiptype = TYPE_PL2303HXD;
389 /* or EA, that is HXD with ESD protection */
390 /* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
393 sc->sc_chiptype = TYPE_PL2303HXD;
394 /* in fact it's TB, that is HXD with external crystal */
397 /* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
398 only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
399 /* Determine the chip type. This algorithm is taken from Linux. */
400 if (dd->bDeviceClass == 0x02)
401 sc->sc_chiptype = TYPE_PL2303;
402 else if (dd->bMaxPacketSize == 0x40)
403 sc->sc_chiptype = TYPE_PL2303HX;
405 sc->sc_chiptype = TYPE_PL2303;
409 switch (sc->sc_chiptype) {
411 DPRINTF("chiptype: 2303\n");
414 DPRINTF("chiptype: 2303HX/TA\n");
417 DPRINTF("chiptype: 2303HXD/TB/RA/EA\n");
420 DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype);
425 * USB-RSAQ1 has two interface
427 * USB-RSAQ1 | USB-RSAQ2
428 * -----------------+-----------------
429 * Interface 0 |Interface 0
430 * Interrupt(0x81) | Interrupt(0x81)
431 * -----------------+ BulkIN(0x02)
432 * Interface 1 | BulkOUT(0x83)
437 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
438 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
440 iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
442 id = usbd_get_interface_descriptor(iface);
444 device_printf(dev, "no interface descriptor (2)\n");
447 sc->sc_data_iface_no = id->bInterfaceNumber;
448 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
449 usbd_set_parent_iface(uaa->device,
450 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
452 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
453 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
456 error = usbd_transfer_setup(uaa->device,
457 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
458 UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
460 DPRINTF("one or more missing USB endpoints, "
461 "error=%s\n", usbd_errstr(error));
464 error = uplcom_reset(sc, uaa->device);
466 device_printf(dev, "reset failed, error=%s\n",
471 if (sc->sc_chiptype == TYPE_PL2303) {
472 /* HX variants seem to lock up after a clear stall request. */
473 mtx_lock(&sc->sc_mtx);
474 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
475 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
476 mtx_unlock(&sc->sc_mtx);
478 /* reset upstream data pipes */
479 if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
480 UPLCOM_SET_REQUEST, 8, 0, 0) ||
481 uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
482 UPLCOM_SET_REQUEST, 9, 0, 0)) {
487 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
488 &uplcom_callback, &sc->sc_mtx);
493 * do the initialization during attach so that the system does not
496 if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
497 device_printf(dev, "init failed\n");
500 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
510 uplcom_detach(device_t dev)
512 struct uplcom_softc *sc = device_get_softc(dev);
514 DPRINTF("sc=%p\n", sc);
516 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
517 usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
519 device_claim_softc(dev);
521 uplcom_free_softc(sc);
526 UCOM_UNLOAD_DRAIN(uplcom);
529 uplcom_free_softc(struct uplcom_softc *sc)
531 if (ucom_unref(&sc->sc_super_ucom)) {
532 mtx_destroy(&sc->sc_mtx);
533 device_free_softc(sc);
538 uplcom_free(struct ucom_softc *ucom)
540 uplcom_free_softc(ucom->sc_parent);
544 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
546 struct usb_device_request req;
548 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
549 req.bRequest = UPLCOM_SET_REQUEST;
550 USETW(req.wValue, 0);
551 req.wIndex[0] = sc->sc_data_iface_no;
553 USETW(req.wLength, 0);
555 return (usbd_do_request(udev, NULL, &req, NULL));
559 uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
560 uint16_t value, uint16_t index, uint16_t length)
562 struct usb_device_request req;
566 req.bmRequestType = req_type;
567 req.bRequest = request;
568 USETW(req.wValue, value);
569 USETW(req.wIndex, index);
570 USETW(req.wLength, length);
572 err = usbd_do_request(udev, NULL, &req, buf);
574 DPRINTF("error=%s\n", usbd_errstr(err));
581 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
585 if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
586 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
587 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
588 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
589 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
590 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
591 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
592 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
593 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
594 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
597 if (chiptype != TYPE_PL2303)
598 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
600 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
608 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
610 struct uplcom_softc *sc = ucom->sc_parent;
611 struct usb_device_request req;
613 DPRINTF("onoff = %d\n", onoff);
616 sc->sc_line |= UCDC_LINE_DTR;
618 sc->sc_line &= ~UCDC_LINE_DTR;
620 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
621 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
622 USETW(req.wValue, sc->sc_line);
623 req.wIndex[0] = sc->sc_data_iface_no;
625 USETW(req.wLength, 0);
627 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
628 &req, NULL, 0, 1000);
632 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
634 struct uplcom_softc *sc = ucom->sc_parent;
635 struct usb_device_request req;
637 DPRINTF("onoff = %d\n", onoff);
640 sc->sc_line |= UCDC_LINE_RTS;
642 sc->sc_line &= ~UCDC_LINE_RTS;
644 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
645 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
646 USETW(req.wValue, sc->sc_line);
647 req.wIndex[0] = sc->sc_data_iface_no;
649 USETW(req.wLength, 0);
651 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
652 &req, NULL, 0, 1000);
656 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
658 struct uplcom_softc *sc = ucom->sc_parent;
659 struct usb_device_request req;
662 DPRINTF("onoff = %d\n", onoff);
664 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
666 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
667 req.bRequest = UCDC_SEND_BREAK;
668 USETW(req.wValue, temp);
669 req.wIndex[0] = sc->sc_data_iface_no;
671 USETW(req.wLength, 0);
673 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
674 &req, NULL, 0, 1000);
678 * NOTE: These baud rates are officially supported, they can be written
679 * directly into dwDTERate register.
681 * Free baudrate setting is not supported by the base PL2303, and on
682 * other models it requires writing a divisor value to dwDTERate instead
683 * of the raw baudrate. The formula for divisor calculation is not published
684 * by the vendor, so it is speculative, though the official product homepage
685 * refers to the Linux module source as a reference implementation.
687 static const uint32_t uplcom_rates[] = {
689 * Basic 'standard' speed rates, supported by all models
690 * NOTE: 900 and 56000 actually works as well
692 75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
693 19200, 28800, 38400, 56000, 57600, 115200,
695 * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA
696 * NOTE: regardless of the spec, 256000 does not work
698 128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400,
699 806400, 921600, 1228800, 2457600, 3000000, 6000000,
701 * Advanced speed rates up to 12, supported by HXD/TB/EA/RA
706 #define N_UPLCOM_RATES nitems(uplcom_rates)
709 uplcom_baud_supported(unsigned int speed)
712 for (i = 0; i < N_UPLCOM_RATES; i++) {
713 if (uplcom_rates[i] == speed)
720 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
722 struct uplcom_softc *sc = ucom->sc_parent;
727 * Check requested baud rate.
729 * The PL2303 can only set specific baud rates, up to 1228800 baud.
730 * The PL2303HX can set any baud rate up to 6Mb.
731 * The PL2303HX rev. D can set any baud rate up to 12Mb.
735 /* accept raw divisor data, if someone wants to do the math in user domain */
736 if (t->c_ospeed & 0x80000000)
738 switch (sc->sc_chiptype) {
740 if (t->c_ospeed <= 12000000)
744 if (t->c_ospeed <= 6000000)
748 if (uplcom_baud_supported(t->c_ospeed))
753 DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
758 uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned int baud)
760 unsigned int baseline, mantissa, exponent;
762 /* Determine the baud rate divisor. This algorithm is taken from Linux. */
764 * Apparently the formula is:
765 * baudrate = baseline / (mantissa * 4^exponent)
767 * mantissa = buf[8:0]
768 * exponent = buf[11:9]
772 baseline = 383385600;
773 mantissa = baseline / baud;
777 while (mantissa >= 512) {
779 mantissa >>= 2; /* divide by 4 */
782 /* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */
790 buf[1] = exponent << 1 | mantissa >> 8;
791 buf[0] = mantissa & 0xff;
793 /* Calculate and return the exact baud rate. */
794 baud = (baseline / mantissa) >> (exponent << 1);
795 DPRINTF("real baud rate will be %u\n", baud);
800 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
802 struct uplcom_softc *sc = ucom->sc_parent;
803 struct usb_cdc_line_state ls;
804 struct usb_device_request req;
806 DPRINTF("sc = %p\n", sc);
808 memset(&ls, 0, sizeof(ls));
811 * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud.
813 if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed))
814 USETDW(ls.dwDTERate, t->c_ospeed);
816 t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed);
818 if (t->c_cflag & CSTOPB) {
819 if ((t->c_cflag & CSIZE) == CS5) {
821 * NOTE: Comply with "real" UARTs / RS232:
822 * use 1.5 instead of 2 stop bits with 5 data bits
824 ls.bCharFormat = UCDC_STOP_BIT_1_5;
826 ls.bCharFormat = UCDC_STOP_BIT_2;
829 ls.bCharFormat = UCDC_STOP_BIT_1;
832 if (t->c_cflag & PARENB) {
833 if (t->c_cflag & PARODD) {
834 ls.bParityType = UCDC_PARITY_ODD;
836 ls.bParityType = UCDC_PARITY_EVEN;
839 ls.bParityType = UCDC_PARITY_NONE;
842 switch (t->c_cflag & CSIZE) {
857 DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n",
858 UGETDW(ls.dwDTERate), ls.bCharFormat,
859 ls.bParityType, ls.bDataBits);
861 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
862 req.bRequest = UCDC_SET_LINE_CODING;
863 USETW(req.wValue, 0);
864 req.wIndex[0] = sc->sc_data_iface_no;
866 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
868 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
871 if (t->c_cflag & CRTSCTS) {
873 DPRINTF("crtscts = on\n");
875 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
876 req.bRequest = UPLCOM_SET_REQUEST;
877 USETW(req.wValue, 0);
878 if (sc->sc_chiptype != TYPE_PL2303)
879 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
881 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
882 USETW(req.wLength, 0);
884 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
885 &req, NULL, 0, 1000);
887 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
888 req.bRequest = UPLCOM_SET_REQUEST;
889 USETW(req.wValue, 0);
890 USETW(req.wIndex, 0);
891 USETW(req.wLength, 0);
892 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
893 &req, NULL, 0, 1000);
898 uplcom_start_read(struct ucom_softc *ucom)
900 struct uplcom_softc *sc = ucom->sc_parent;
902 /* start interrupt endpoint */
903 usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
905 /* start read endpoint */
906 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
910 uplcom_stop_read(struct ucom_softc *ucom)
912 struct uplcom_softc *sc = ucom->sc_parent;
914 /* stop interrupt endpoint */
915 usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
917 /* stop read endpoint */
918 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
922 uplcom_start_write(struct ucom_softc *ucom)
924 struct uplcom_softc *sc = ucom->sc_parent;
926 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
930 uplcom_stop_write(struct ucom_softc *ucom)
932 struct uplcom_softc *sc = ucom->sc_parent;
934 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
938 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
940 struct uplcom_softc *sc = ucom->sc_parent;
949 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
951 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
952 struct usb_page_cache *pc;
956 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
958 switch (USB_GET_STATE(xfer)) {
959 case USB_ST_TRANSFERRED:
961 DPRINTF("actlen = %u\n", actlen);
965 pc = usbd_xfer_get_frame(xfer, 0);
966 usbd_copy_out(pc, 0, buf, sizeof(buf));
968 DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]);
973 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) {
974 sc->sc_msr |= SER_CTS;
976 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) {
977 sc->sc_lsr |= ULSR_OE;
979 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) {
980 sc->sc_lsr |= ULSR_PE;
982 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) {
983 sc->sc_lsr |= ULSR_FE;
985 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) {
986 sc->sc_msr |= SER_RI;
988 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) {
989 sc->sc_lsr |= ULSR_BI;
991 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) {
992 sc->sc_msr |= SER_DSR;
994 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) {
995 sc->sc_msr |= SER_DCD;
997 ucom_status_change(&sc->sc_ucom);
1001 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1002 usbd_transfer_submit(xfer);
1005 default: /* Error */
1006 if (error != USB_ERR_CANCELLED) {
1007 /* try to clear stall first */
1008 usbd_xfer_set_stall(xfer);
1016 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
1018 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1019 struct usb_page_cache *pc;
1022 switch (USB_GET_STATE(xfer)) {
1024 case USB_ST_TRANSFERRED:
1026 pc = usbd_xfer_get_frame(xfer, 0);
1027 if (ucom_get_data(&sc->sc_ucom, pc, 0,
1028 UPLCOM_BULK_BUF_SIZE, &actlen)) {
1030 DPRINTF("actlen = %d\n", actlen);
1032 usbd_xfer_set_frame_len(xfer, 0, actlen);
1033 usbd_transfer_submit(xfer);
1037 default: /* Error */
1038 if (error != USB_ERR_CANCELLED) {
1039 /* try to clear stall first */
1040 usbd_xfer_set_stall(xfer);
1048 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
1050 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1051 struct usb_page_cache *pc;
1054 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1056 switch (USB_GET_STATE(xfer)) {
1057 case USB_ST_TRANSFERRED:
1058 pc = usbd_xfer_get_frame(xfer, 0);
1059 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
1063 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1064 usbd_transfer_submit(xfer);
1067 default: /* Error */
1068 if (error != USB_ERR_CANCELLED) {
1069 /* try to clear stall first */
1070 usbd_xfer_set_stall(xfer);
1078 uplcom_poll(struct ucom_softc *ucom)
1080 struct uplcom_softc *sc = ucom->sc_parent;
1081 usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);