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 | CTLFLAG_MPSAFE, 0,
118 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RWTUN,
119 &uplcom_debug, 0, "Debug level");
122 #define UPLCOM_MODVER 1 /* module version */
124 #define UPLCOM_CONFIG_INDEX 0
125 #define UPLCOM_IFACE_INDEX 0
126 #define UPLCOM_SECOND_IFACE_INDEX 1
128 #ifndef UPLCOM_INTR_INTERVAL
129 #define UPLCOM_INTR_INTERVAL 0 /* default */
132 #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */
134 #define UPLCOM_SET_REQUEST 0x01
135 #define UPLCOM_SET_CRTSCTS 0x41
136 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
137 #define RSAQ_STATUS_CTS 0x80
138 #define RSAQ_STATUS_OVERRUN_ERROR 0x40
139 #define RSAQ_STATUS_PARITY_ERROR 0x20
140 #define RSAQ_STATUS_FRAME_ERROR 0x10
141 #define RSAQ_STATUS_RING 0x08
142 #define RSAQ_STATUS_BREAK_ERROR 0x04
143 #define RSAQ_STATUS_DSR 0x02
144 #define RSAQ_STATUS_DCD 0x01
146 #define TYPE_PL2303 0
147 #define TYPE_PL2303HX 1
148 #define TYPE_PL2303HXD 2
150 #define UPLCOM_STATE_INDEX 8
159 struct uplcom_softc {
160 struct ucom_super_softc sc_super_ucom;
161 struct ucom_softc sc_ucom;
163 struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
164 struct usb_device *sc_udev;
169 uint8_t sc_lsr; /* local status register */
170 uint8_t sc_msr; /* uplcom status register */
171 uint8_t sc_chiptype; /* type of chip */
172 uint8_t sc_ctrl_iface_no;
173 uint8_t sc_data_iface_no;
174 uint8_t sc_iface_index[2];
179 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
180 static usb_error_t uplcom_pl2303_do(struct usb_device *, uint8_t, uint8_t,
181 uint16_t, uint16_t, uint16_t);
182 static int uplcom_pl2303_init(struct usb_device *, uint8_t);
183 static void uplcom_free(struct ucom_softc *);
184 static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
185 static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
186 static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
187 static int uplcom_pre_param(struct ucom_softc *, struct termios *);
188 static void uplcom_cfg_param(struct ucom_softc *, struct termios *);
189 static void uplcom_start_read(struct ucom_softc *);
190 static void uplcom_stop_read(struct ucom_softc *);
191 static void uplcom_start_write(struct ucom_softc *);
192 static void uplcom_stop_write(struct ucom_softc *);
193 static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
195 static void uplcom_poll(struct ucom_softc *ucom);
197 static device_probe_t uplcom_probe;
198 static device_attach_t uplcom_attach;
199 static device_detach_t uplcom_detach;
200 static void uplcom_free_softc(struct uplcom_softc *);
202 static usb_callback_t uplcom_intr_callback;
203 static usb_callback_t uplcom_write_callback;
204 static usb_callback_t uplcom_read_callback;
206 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
208 [UPLCOM_BULK_DT_WR] = {
210 .endpoint = UE_ADDR_ANY,
211 .direction = UE_DIR_OUT,
212 .bufsize = UPLCOM_BULK_BUF_SIZE,
213 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
214 .callback = &uplcom_write_callback,
218 [UPLCOM_BULK_DT_RD] = {
220 .endpoint = UE_ADDR_ANY,
221 .direction = UE_DIR_IN,
222 .bufsize = UPLCOM_BULK_BUF_SIZE,
223 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
224 .callback = &uplcom_read_callback,
228 [UPLCOM_INTR_DT_RD] = {
229 .type = UE_INTERRUPT,
230 .endpoint = UE_ADDR_ANY,
231 .direction = UE_DIR_IN,
232 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
233 .bufsize = 0, /* use wMaxPacketSize */
234 .callback = &uplcom_intr_callback,
239 static struct ucom_callback uplcom_callback = {
240 .ucom_cfg_get_status = &uplcom_cfg_get_status,
241 .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
242 .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
243 .ucom_cfg_set_break = &uplcom_cfg_set_break,
244 .ucom_cfg_param = &uplcom_cfg_param,
245 .ucom_pre_param = &uplcom_pre_param,
246 .ucom_start_read = &uplcom_start_read,
247 .ucom_stop_read = &uplcom_stop_read,
248 .ucom_start_write = &uplcom_start_write,
249 .ucom_stop_write = &uplcom_stop_write,
250 .ucom_poll = &uplcom_poll,
251 .ucom_free = &uplcom_free,
254 #define UPLCOM_DEV(v,p) \
255 { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
257 static const STRUCT_USB_HOST_ID uplcom_devs[] = {
258 UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */
259 UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */
260 UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */
261 UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */
262 UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */
263 UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */
264 UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */
265 UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */
266 UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */
267 UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */
268 UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */
269 UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */
270 UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */
271 UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */
272 UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */
273 UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */
274 UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */
275 UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */
276 UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */
277 UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */
278 UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */
279 UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */
280 UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */
281 UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */
282 UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */
283 UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */
284 UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */
285 UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */
286 UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */
287 UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */
288 UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */
289 UPLCOM_DEV(PROLIFIC, MOTOROLA), /* Motorola cable */
290 UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */
291 UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */
292 UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */
293 UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */
294 UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */
295 UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */
296 UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */
297 UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */
298 UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */
299 UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */
300 UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */
301 UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */
302 UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */
303 UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */
304 UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */
305 UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */
306 UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */
307 UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */
308 UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */
309 UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */
310 UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */
311 UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */
312 UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */
313 UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */
314 UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */
315 UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */
316 UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */
317 UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */
321 static device_method_t uplcom_methods[] = {
322 DEVMETHOD(device_probe, uplcom_probe),
323 DEVMETHOD(device_attach, uplcom_attach),
324 DEVMETHOD(device_detach, uplcom_detach),
328 static devclass_t uplcom_devclass;
330 static driver_t uplcom_driver = {
332 .methods = uplcom_methods,
333 .size = sizeof(struct uplcom_softc),
336 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
337 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
338 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
339 MODULE_VERSION(uplcom, UPLCOM_MODVER);
340 USB_PNP_HOST_INFO(uplcom_devs);
343 uplcom_probe(device_t dev)
345 struct usb_attach_arg *uaa = device_get_ivars(dev);
349 if (uaa->usb_mode != USB_MODE_HOST) {
352 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
355 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
358 return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
362 uplcom_attach(device_t dev)
364 struct usb_attach_arg *uaa = device_get_ivars(dev);
365 struct uplcom_softc *sc = device_get_softc(dev);
366 struct usb_interface *iface;
367 struct usb_interface_descriptor *id;
368 struct usb_device_descriptor *dd;
373 device_set_usb_desc(dev);
374 mtx_init(&sc->sc_mtx, "uplcom", NULL, MTX_DEF);
375 ucom_ref(&sc->sc_super_ucom);
377 DPRINTF("sc = %p\n", sc);
379 sc->sc_udev = uaa->device;
381 dd = usbd_get_device_descriptor(sc->sc_udev);
383 switch (UGETW(dd->bcdDevice)) {
385 sc->sc_chiptype = TYPE_PL2303HX;
386 /* or TA, that is HX with external crystal */
389 sc->sc_chiptype = TYPE_PL2303HXD;
390 /* or EA, that is HXD with ESD protection */
391 /* or RA, that has internal voltage level converter that works only up to 1Mbaud (!) */
394 sc->sc_chiptype = TYPE_PL2303HXD;
395 /* in fact it's TB, that is HXD with external crystal */
398 /* NOTE: I have no info about the bcdDevice for the base PL2303 (up to 1.2Mbaud,
399 only fixed rates) and for PL2303SA (8-pin chip, up to 115200 baud */
400 /* Determine the chip type. This algorithm is taken from Linux. */
401 if (dd->bDeviceClass == 0x02)
402 sc->sc_chiptype = TYPE_PL2303;
403 else if (dd->bMaxPacketSize == 0x40)
404 sc->sc_chiptype = TYPE_PL2303HX;
406 sc->sc_chiptype = TYPE_PL2303;
410 switch (sc->sc_chiptype) {
412 DPRINTF("chiptype: 2303\n");
415 DPRINTF("chiptype: 2303HX/TA\n");
418 DPRINTF("chiptype: 2303HXD/TB/RA/EA\n");
421 DPRINTF("chiptype: unknown %d\n", sc->sc_chiptype);
426 * USB-RSAQ1 has two interface
428 * USB-RSAQ1 | USB-RSAQ2
429 * -----------------+-----------------
430 * Interface 0 |Interface 0
431 * Interrupt(0x81) | Interrupt(0x81)
432 * -----------------+ BulkIN(0x02)
433 * Interface 1 | BulkOUT(0x83)
438 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
439 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
441 iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
443 id = usbd_get_interface_descriptor(iface);
445 device_printf(dev, "no interface descriptor (2)\n");
448 sc->sc_data_iface_no = id->bInterfaceNumber;
449 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
450 usbd_set_parent_iface(uaa->device,
451 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
453 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
454 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
457 error = usbd_transfer_setup(uaa->device,
458 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
459 UPLCOM_N_TRANSFER, sc, &sc->sc_mtx);
461 DPRINTF("one or more missing USB endpoints, "
462 "error=%s\n", usbd_errstr(error));
465 error = uplcom_reset(sc, uaa->device);
467 device_printf(dev, "reset failed, error=%s\n",
472 if (sc->sc_chiptype == TYPE_PL2303) {
473 /* HX variants seem to lock up after a clear stall request. */
474 mtx_lock(&sc->sc_mtx);
475 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
476 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
477 mtx_unlock(&sc->sc_mtx);
479 /* reset upstream data pipes */
480 if (uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
481 UPLCOM_SET_REQUEST, 8, 0, 0) ||
482 uplcom_pl2303_do(sc->sc_udev, UT_WRITE_VENDOR_DEVICE,
483 UPLCOM_SET_REQUEST, 9, 0, 0)) {
488 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
489 &uplcom_callback, &sc->sc_mtx);
494 * do the initialization during attach so that the system does not
497 if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
498 device_printf(dev, "init failed\n");
501 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
511 uplcom_detach(device_t dev)
513 struct uplcom_softc *sc = device_get_softc(dev);
515 DPRINTF("sc=%p\n", sc);
517 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
518 usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
520 device_claim_softc(dev);
522 uplcom_free_softc(sc);
527 UCOM_UNLOAD_DRAIN(uplcom);
530 uplcom_free_softc(struct uplcom_softc *sc)
532 if (ucom_unref(&sc->sc_super_ucom)) {
533 mtx_destroy(&sc->sc_mtx);
534 device_free_softc(sc);
539 uplcom_free(struct ucom_softc *ucom)
541 uplcom_free_softc(ucom->sc_parent);
545 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
547 struct usb_device_request req;
549 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
550 req.bRequest = UPLCOM_SET_REQUEST;
551 USETW(req.wValue, 0);
552 req.wIndex[0] = sc->sc_data_iface_no;
554 USETW(req.wLength, 0);
556 return (usbd_do_request(udev, NULL, &req, NULL));
560 uplcom_pl2303_do(struct usb_device *udev, uint8_t req_type, uint8_t request,
561 uint16_t value, uint16_t index, uint16_t length)
563 struct usb_device_request req;
567 req.bmRequestType = req_type;
568 req.bRequest = request;
569 USETW(req.wValue, value);
570 USETW(req.wIndex, index);
571 USETW(req.wLength, length);
573 err = usbd_do_request(udev, NULL, &req, buf);
575 DPRINTF("error=%s\n", usbd_errstr(err));
582 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
586 if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
587 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
588 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
589 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
590 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
591 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
592 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
593 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
594 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
595 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
598 if (chiptype != TYPE_PL2303)
599 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
601 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
609 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
611 struct uplcom_softc *sc = ucom->sc_parent;
612 struct usb_device_request req;
614 DPRINTF("onoff = %d\n", onoff);
617 sc->sc_line |= UCDC_LINE_DTR;
619 sc->sc_line &= ~UCDC_LINE_DTR;
621 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
622 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
623 USETW(req.wValue, sc->sc_line);
624 req.wIndex[0] = sc->sc_data_iface_no;
626 USETW(req.wLength, 0);
628 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
629 &req, NULL, 0, 1000);
633 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
635 struct uplcom_softc *sc = ucom->sc_parent;
636 struct usb_device_request req;
638 DPRINTF("onoff = %d\n", onoff);
641 sc->sc_line |= UCDC_LINE_RTS;
643 sc->sc_line &= ~UCDC_LINE_RTS;
645 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
646 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
647 USETW(req.wValue, sc->sc_line);
648 req.wIndex[0] = sc->sc_data_iface_no;
650 USETW(req.wLength, 0);
652 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
653 &req, NULL, 0, 1000);
657 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
659 struct uplcom_softc *sc = ucom->sc_parent;
660 struct usb_device_request req;
663 DPRINTF("onoff = %d\n", onoff);
665 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
667 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
668 req.bRequest = UCDC_SEND_BREAK;
669 USETW(req.wValue, temp);
670 req.wIndex[0] = sc->sc_data_iface_no;
672 USETW(req.wLength, 0);
674 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
675 &req, NULL, 0, 1000);
679 * NOTE: These baud rates are officially supported, they can be written
680 * directly into dwDTERate register.
682 * Free baudrate setting is not supported by the base PL2303, and on
683 * other models it requires writing a divisor value to dwDTERate instead
684 * of the raw baudrate. The formula for divisor calculation is not published
685 * by the vendor, so it is speculative, though the official product homepage
686 * refers to the Linux module source as a reference implementation.
688 static const uint32_t uplcom_rates[] = {
690 * Basic 'standard' speed rates, supported by all models
691 * NOTE: 900 and 56000 actually works as well
693 75, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
694 19200, 28800, 38400, 56000, 57600, 115200,
696 * Advanced speed rates up to 6Mbs, supported by HX/TA and HXD/TB/EA/RA
697 * NOTE: regardless of the spec, 256000 does not work
699 128000, 134400, 161280, 201600, 230400, 268800, 403200, 460800, 614400,
700 806400, 921600, 1228800, 2457600, 3000000, 6000000,
702 * Advanced speed rates up to 12, supported by HXD/TB/EA/RA
707 #define N_UPLCOM_RATES nitems(uplcom_rates)
710 uplcom_baud_supported(unsigned int speed)
713 for (i = 0; i < N_UPLCOM_RATES; i++) {
714 if (uplcom_rates[i] == speed)
721 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
723 struct uplcom_softc *sc = ucom->sc_parent;
728 * Check requested baud rate.
730 * The PL2303 can only set specific baud rates, up to 1228800 baud.
731 * The PL2303HX can set any baud rate up to 6Mb.
732 * The PL2303HX rev. D can set any baud rate up to 12Mb.
736 /* accept raw divisor data, if someone wants to do the math in user domain */
737 if (t->c_ospeed & 0x80000000)
739 switch (sc->sc_chiptype) {
741 if (t->c_ospeed <= 12000000)
745 if (t->c_ospeed <= 6000000)
749 if (uplcom_baud_supported(t->c_ospeed))
754 DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
759 uplcom_encode_baud_rate_divisor(uint8_t *buf, unsigned int baud)
761 unsigned int baseline, mantissa, exponent;
763 /* Determine the baud rate divisor. This algorithm is taken from Linux. */
765 * Apparently the formula is:
766 * baudrate = baseline / (mantissa * 4^exponent)
768 * mantissa = buf[8:0]
769 * exponent = buf[11:9]
773 baseline = 383385600;
774 mantissa = baseline / baud;
778 while (mantissa >= 512) {
780 mantissa >>= 2; /* divide by 4 */
783 /* Exponent is maxed. Trim mantissa and leave. This gives approx. 45.8 baud */
791 buf[1] = exponent << 1 | mantissa >> 8;
792 buf[0] = mantissa & 0xff;
794 /* Calculate and return the exact baud rate. */
795 baud = (baseline / mantissa) >> (exponent << 1);
796 DPRINTF("real baud rate will be %u\n", baud);
801 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
803 struct uplcom_softc *sc = ucom->sc_parent;
804 struct usb_cdc_line_state ls;
805 struct usb_device_request req;
807 DPRINTF("sc = %p\n", sc);
809 memset(&ls, 0, sizeof(ls));
812 * NOTE: If unsupported baud rates are set directly, the PL2303* uses 9600 baud.
814 if ((t->c_ospeed & 0x80000000) || uplcom_baud_supported(t->c_ospeed))
815 USETDW(ls.dwDTERate, t->c_ospeed);
817 t->c_ospeed = uplcom_encode_baud_rate_divisor((uint8_t*)&ls.dwDTERate, t->c_ospeed);
819 if (t->c_cflag & CSTOPB) {
820 if ((t->c_cflag & CSIZE) == CS5) {
822 * NOTE: Comply with "real" UARTs / RS232:
823 * use 1.5 instead of 2 stop bits with 5 data bits
825 ls.bCharFormat = UCDC_STOP_BIT_1_5;
827 ls.bCharFormat = UCDC_STOP_BIT_2;
830 ls.bCharFormat = UCDC_STOP_BIT_1;
833 if (t->c_cflag & PARENB) {
834 if (t->c_cflag & PARODD) {
835 ls.bParityType = UCDC_PARITY_ODD;
837 ls.bParityType = UCDC_PARITY_EVEN;
840 ls.bParityType = UCDC_PARITY_NONE;
843 switch (t->c_cflag & CSIZE) {
858 DPRINTF("rate=0x%08x fmt=%d parity=%d bits=%d\n",
859 UGETDW(ls.dwDTERate), ls.bCharFormat,
860 ls.bParityType, ls.bDataBits);
862 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
863 req.bRequest = UCDC_SET_LINE_CODING;
864 USETW(req.wValue, 0);
865 req.wIndex[0] = sc->sc_data_iface_no;
867 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
869 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
872 if (t->c_cflag & CRTSCTS) {
874 DPRINTF("crtscts = on\n");
876 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
877 req.bRequest = UPLCOM_SET_REQUEST;
878 USETW(req.wValue, 0);
879 if (sc->sc_chiptype != TYPE_PL2303)
880 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
882 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
883 USETW(req.wLength, 0);
885 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
886 &req, NULL, 0, 1000);
888 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
889 req.bRequest = UPLCOM_SET_REQUEST;
890 USETW(req.wValue, 0);
891 USETW(req.wIndex, 0);
892 USETW(req.wLength, 0);
893 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
894 &req, NULL, 0, 1000);
899 uplcom_start_read(struct ucom_softc *ucom)
901 struct uplcom_softc *sc = ucom->sc_parent;
903 /* start interrupt endpoint */
904 usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
906 /* start read endpoint */
907 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
911 uplcom_stop_read(struct ucom_softc *ucom)
913 struct uplcom_softc *sc = ucom->sc_parent;
915 /* stop interrupt endpoint */
916 usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
918 /* stop read endpoint */
919 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
923 uplcom_start_write(struct ucom_softc *ucom)
925 struct uplcom_softc *sc = ucom->sc_parent;
927 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
931 uplcom_stop_write(struct ucom_softc *ucom)
933 struct uplcom_softc *sc = ucom->sc_parent;
935 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
939 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
941 struct uplcom_softc *sc = ucom->sc_parent;
950 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
952 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
953 struct usb_page_cache *pc;
957 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
959 switch (USB_GET_STATE(xfer)) {
960 case USB_ST_TRANSFERRED:
962 DPRINTF("actlen = %u\n", actlen);
966 pc = usbd_xfer_get_frame(xfer, 0);
967 usbd_copy_out(pc, 0, buf, sizeof(buf));
969 DPRINTF("status = 0x%02x\n", buf[UPLCOM_STATE_INDEX]);
974 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_CTS) {
975 sc->sc_msr |= SER_CTS;
977 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_OVERRUN_ERROR) {
978 sc->sc_lsr |= ULSR_OE;
980 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_PARITY_ERROR) {
981 sc->sc_lsr |= ULSR_PE;
983 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_FRAME_ERROR) {
984 sc->sc_lsr |= ULSR_FE;
986 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_RING) {
987 sc->sc_msr |= SER_RI;
989 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_BREAK_ERROR) {
990 sc->sc_lsr |= ULSR_BI;
992 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DSR) {
993 sc->sc_msr |= SER_DSR;
995 if (buf[UPLCOM_STATE_INDEX] & RSAQ_STATUS_DCD) {
996 sc->sc_msr |= SER_DCD;
998 ucom_status_change(&sc->sc_ucom);
1002 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1003 usbd_transfer_submit(xfer);
1006 default: /* Error */
1007 if (error != USB_ERR_CANCELLED) {
1008 /* try to clear stall first */
1009 usbd_xfer_set_stall(xfer);
1017 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
1019 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1020 struct usb_page_cache *pc;
1023 switch (USB_GET_STATE(xfer)) {
1025 case USB_ST_TRANSFERRED:
1027 pc = usbd_xfer_get_frame(xfer, 0);
1028 if (ucom_get_data(&sc->sc_ucom, pc, 0,
1029 UPLCOM_BULK_BUF_SIZE, &actlen)) {
1031 DPRINTF("actlen = %d\n", actlen);
1033 usbd_xfer_set_frame_len(xfer, 0, actlen);
1034 usbd_transfer_submit(xfer);
1038 default: /* Error */
1039 if (error != USB_ERR_CANCELLED) {
1040 /* try to clear stall first */
1041 usbd_xfer_set_stall(xfer);
1049 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
1051 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
1052 struct usb_page_cache *pc;
1055 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1057 switch (USB_GET_STATE(xfer)) {
1058 case USB_ST_TRANSFERRED:
1059 pc = usbd_xfer_get_frame(xfer, 0);
1060 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
1064 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1065 usbd_transfer_submit(xfer);
1068 default: /* Error */
1069 if (error != USB_ERR_CANCELLED) {
1070 /* try to clear stall first */
1071 usbd_xfer_set_stall(xfer);
1079 uplcom_poll(struct ucom_softc *ucom)
1081 struct uplcom_softc *sc = ucom->sc_parent;
1082 usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);