1 /* $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $ */
4 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 2001 The NetBSD Foundation, Inc.
31 * All rights reserved.
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Ichiro FUKUHARA (ichiro@ichiro.org).
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the NetBSD
47 * Foundation, Inc. and its contributors.
48 * 4. Neither the name of The NetBSD Foundation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
66 * This driver supports several USB-to-RS232 serial adapters driven by
67 * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
68 * bridge chip. The adapters are sold under many different brand
71 * Datasheets are available at Prolific www site at
72 * http://www.prolific.com.tw. The datasheets don't contain full
73 * programming information for the chip.
75 * PL-2303HX is probably programmed the same as PL-2303X.
77 * There are several differences between PL-2303 and PL-2303(H)X.
78 * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
79 * different command for controlling CRTSCTS and needs special
80 * sequence of commands for initialization which aren't also
81 * documented in the datasheet.
84 #include <sys/stdint.h>
85 #include <sys/param.h>
86 #include <sys/queue.h>
87 #include <sys/types.h>
88 #include <sys/systm.h>
89 #include <sys/kernel.h>
91 #include <sys/module.h>
93 #include <sys/condvar.h>
94 #include <sys/sysctl.h>
95 #include <sys/unistd.h>
96 #include <sys/callout.h>
97 #include <sys/malloc.h>
100 #include <bus/u4b/usb.h>
101 #include <bus/u4b/usbdi.h>
102 #include <bus/u4b/usbdi_util.h>
103 #include <bus/u4b/usb_cdc.h>
104 #include <bus/u4b/usbdevs.h>
106 #define USB_DEBUG_VAR uplcom_debug
107 #include <bus/u4b/usb_debug.h>
108 #include <bus/u4b/usb_process.h>
110 #include <bus/u4b/serial/usb_serial.h>
113 static int uplcom_debug = 0;
115 static SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
116 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
117 &uplcom_debug, 0, "Debug level");
120 #define UPLCOM_MODVER 1 /* module version */
122 #define UPLCOM_CONFIG_INDEX 0
123 #define UPLCOM_IFACE_INDEX 0
124 #define UPLCOM_SECOND_IFACE_INDEX 1
126 #ifndef UPLCOM_INTR_INTERVAL
127 #define UPLCOM_INTR_INTERVAL 0 /* default */
130 #define UPLCOM_BULK_BUF_SIZE 1024 /* bytes */
132 #define UPLCOM_SET_REQUEST 0x01
133 #define UPLCOM_SET_CRTSCTS 0x41
134 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
135 #define RSAQ_STATUS_CTS 0x80
136 #define RSAQ_STATUS_DSR 0x02
137 #define RSAQ_STATUS_DCD 0x01
139 #define TYPE_PL2303 0
140 #define TYPE_PL2303HX 1
149 struct uplcom_softc {
150 struct ucom_super_softc sc_super_ucom;
151 struct ucom_softc sc_ucom;
153 struct usb_xfer *sc_xfer[UPLCOM_N_TRANSFER];
154 struct usb_device *sc_udev;
159 uint8_t sc_lsr; /* local status register */
160 uint8_t sc_msr; /* uplcom status register */
161 uint8_t sc_chiptype; /* type of chip */
162 uint8_t sc_ctrl_iface_no;
163 uint8_t sc_data_iface_no;
164 uint8_t sc_iface_index[2];
169 static usb_error_t uplcom_reset(struct uplcom_softc *, struct usb_device *);
170 static usb_error_t uplcom_pl2303_do(struct usb_device *, int8_t, uint8_t,
171 uint16_t, uint16_t, uint16_t);
172 static int uplcom_pl2303_init(struct usb_device *, uint8_t);
173 static void uplcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
174 static void uplcom_cfg_set_rts(struct ucom_softc *, uint8_t);
175 static void uplcom_cfg_set_break(struct ucom_softc *, uint8_t);
176 static int uplcom_pre_param(struct ucom_softc *, struct termios *);
177 static void uplcom_cfg_param(struct ucom_softc *, struct termios *);
178 static void uplcom_start_read(struct ucom_softc *);
179 static void uplcom_stop_read(struct ucom_softc *);
180 static void uplcom_start_write(struct ucom_softc *);
181 static void uplcom_stop_write(struct ucom_softc *);
182 static void uplcom_cfg_get_status(struct ucom_softc *, uint8_t *,
184 static void uplcom_poll(struct ucom_softc *ucom);
186 static device_probe_t uplcom_probe;
187 static device_attach_t uplcom_attach;
188 static device_detach_t uplcom_detach;
190 static usb_callback_t uplcom_intr_callback;
191 static usb_callback_t uplcom_write_callback;
192 static usb_callback_t uplcom_read_callback;
194 static const struct usb_config uplcom_config_data[UPLCOM_N_TRANSFER] = {
196 [UPLCOM_BULK_DT_WR] = {
198 .endpoint = UE_ADDR_ANY,
199 .direction = UE_DIR_OUT,
200 .bufsize = UPLCOM_BULK_BUF_SIZE,
201 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
202 .callback = &uplcom_write_callback,
206 [UPLCOM_BULK_DT_RD] = {
208 .endpoint = UE_ADDR_ANY,
209 .direction = UE_DIR_IN,
210 .bufsize = UPLCOM_BULK_BUF_SIZE,
211 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
212 .callback = &uplcom_read_callback,
216 [UPLCOM_INTR_DT_RD] = {
217 .type = UE_INTERRUPT,
218 .endpoint = UE_ADDR_ANY,
219 .direction = UE_DIR_IN,
220 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
221 .bufsize = 0, /* use wMaxPacketSize */
222 .callback = &uplcom_intr_callback,
227 static struct ucom_callback uplcom_callback = {
228 .ucom_cfg_get_status = &uplcom_cfg_get_status,
229 .ucom_cfg_set_dtr = &uplcom_cfg_set_dtr,
230 .ucom_cfg_set_rts = &uplcom_cfg_set_rts,
231 .ucom_cfg_set_break = &uplcom_cfg_set_break,
232 .ucom_cfg_param = &uplcom_cfg_param,
233 .ucom_pre_param = &uplcom_pre_param,
234 .ucom_start_read = &uplcom_start_read,
235 .ucom_stop_read = &uplcom_stop_read,
236 .ucom_start_write = &uplcom_start_write,
237 .ucom_stop_write = &uplcom_stop_write,
238 .ucom_poll = &uplcom_poll,
241 #define UPLCOM_DEV(v,p) \
242 { USB_VENDOR(USB_VENDOR_##v), USB_PRODUCT(USB_PRODUCT_##v##_##p) }
244 static const STRUCT_USB_HOST_ID uplcom_devs[] = {
245 UPLCOM_DEV(ACERP, S81), /* BenQ S81 phone */
246 UPLCOM_DEV(ADLINK, ND6530), /* ADLINK ND-6530 USB-Serial */
247 UPLCOM_DEV(ALCATEL, OT535), /* Alcatel One Touch 535/735 */
248 UPLCOM_DEV(ALCOR, AU9720), /* Alcor AU9720 USB 2.0-RS232 */
249 UPLCOM_DEV(ANCHOR, SERIAL), /* Anchor Serial adapter */
250 UPLCOM_DEV(ATEN, UC232A), /* PLANEX USB-RS232 URS-03 */
251 UPLCOM_DEV(BELKIN, F5U257), /* Belkin F5U257 USB to Serial */
252 UPLCOM_DEV(COREGA, CGUSBRS232R), /* Corega CG-USBRS232R */
253 UPLCOM_DEV(EPSON, CRESSI_EDY), /* Cressi Edy diving computer */
254 UPLCOM_DEV(EPSON, N2ITION3), /* Zeagle N2iTion3 diving computer */
255 UPLCOM_DEV(ELECOM, UCSGT), /* ELECOM UC-SGT Serial Adapter */
256 UPLCOM_DEV(ELECOM, UCSGT0), /* ELECOM UC-SGT Serial Adapter */
257 UPLCOM_DEV(HAL, IMR001), /* HAL Corporation Crossam2+USB */
258 UPLCOM_DEV(HP, LD220), /* HP LD220 POS Display */
259 UPLCOM_DEV(IODATA, USBRSAQ), /* I/O DATA USB-RSAQ */
260 UPLCOM_DEV(IODATA, USBRSAQ5), /* I/O DATA USB-RSAQ5 */
261 UPLCOM_DEV(ITEGNO, WM1080A), /* iTegno WM1080A GSM/GFPRS modem */
262 UPLCOM_DEV(ITEGNO, WM2080A), /* iTegno WM2080A CDMA modem */
263 UPLCOM_DEV(LEADTEK, 9531), /* Leadtek 9531 GPS */
264 UPLCOM_DEV(MICROSOFT, 700WX), /* Microsoft Palm 700WX */
265 UPLCOM_DEV(MOBILEACTION, MA620), /* Mobile Action MA-620 Infrared Adapter */
266 UPLCOM_DEV(NETINDEX, WS002IN), /* Willcom W-S002IN */
267 UPLCOM_DEV(NOKIA2, CA42), /* Nokia CA-42 cable */
268 UPLCOM_DEV(OTI, DKU5), /* OTI DKU-5 cable */
269 UPLCOM_DEV(PANASONIC, TYTP50P6S), /* Panasonic TY-TP50P6-S flat screen */
270 UPLCOM_DEV(PLX, CA42), /* PLX CA-42 clone cable */
271 UPLCOM_DEV(PROLIFIC, ALLTRONIX_GPRS), /* Alltronix ACM003U00 modem */
272 UPLCOM_DEV(PROLIFIC, ALDIGA_AL11U), /* AlDiga AL-11U modem */
273 UPLCOM_DEV(PROLIFIC, DCU11), /* DCU-11 Phone Cable */
274 UPLCOM_DEV(PROLIFIC, HCR331), /* HCR331 Card Reader */
275 UPLCOM_DEV(PROLIFIC, MICROMAX_610U), /* Micromax 610U modem */
276 UPLCOM_DEV(PROLIFIC, PHAROS), /* Prolific Pharos */
277 UPLCOM_DEV(PROLIFIC, PL2303), /* Generic adapter */
278 UPLCOM_DEV(PROLIFIC, RSAQ2), /* I/O DATA USB-RSAQ2 */
279 UPLCOM_DEV(PROLIFIC, RSAQ3), /* I/O DATA USB-RSAQ3 */
280 UPLCOM_DEV(PROLIFIC, UIC_MSR206), /* UIC MSR206 Card Reader */
281 UPLCOM_DEV(PROLIFIC2, PL2303), /* Prolific adapter */
282 UPLCOM_DEV(RADIOSHACK, USBCABLE), /* Radio Shack USB Adapter */
283 UPLCOM_DEV(RATOC, REXUSB60), /* RATOC REX-USB60 */
284 UPLCOM_DEV(SAGEM, USBSERIAL), /* Sagem USB-Serial Controller */
285 UPLCOM_DEV(SAMSUNG, I330), /* Samsung I330 phone cradle */
286 UPLCOM_DEV(SANWA, KB_USB2), /* Sanwa KB-USB2 Multimeter cable */
287 UPLCOM_DEV(SIEMENS3, EF81), /* Siemens EF81 */
288 UPLCOM_DEV(SIEMENS3, SX1), /* Siemens SX1 */
289 UPLCOM_DEV(SIEMENS3, X65), /* Siemens X65 */
290 UPLCOM_DEV(SIEMENS3, X75), /* Siemens X75 */
291 UPLCOM_DEV(SITECOM, SERIAL), /* Sitecom USB to Serial */
292 UPLCOM_DEV(SMART, PL2303), /* SMART Technologies USB to Serial */
293 UPLCOM_DEV(SONY, QN3), /* Sony QN3 phone cable */
294 UPLCOM_DEV(SONYERICSSON, DATAPILOT), /* Sony Ericsson Datapilot */
295 UPLCOM_DEV(SONYERICSSON, DCU10), /* Sony Ericsson DCU-10 Cable */
296 UPLCOM_DEV(SOURCENEXT, KEIKAI8), /* SOURCENEXT KeikaiDenwa 8 */
297 UPLCOM_DEV(SOURCENEXT, KEIKAI8_CHG), /* SOURCENEXT KeikaiDenwa 8 with charger */
298 UPLCOM_DEV(SPEEDDRAGON, MS3303H), /* Speed Dragon USB-Serial */
299 UPLCOM_DEV(SYNTECH, CPT8001C), /* Syntech CPT-8001C Barcode scanner */
300 UPLCOM_DEV(TDK, UHA6400), /* TDK USB-PHS Adapter UHA6400 */
301 UPLCOM_DEV(TDK, UPA9664), /* TDK USB-PHS Adapter UPA9664 */
302 UPLCOM_DEV(TRIPPLITE, U209), /* Tripp-Lite U209-000-R USB to Serial */
303 UPLCOM_DEV(YCCABLE, PL2303), /* YC Cable USB-Serial */
307 static device_method_t uplcom_methods[] = {
308 DEVMETHOD(device_probe, uplcom_probe),
309 DEVMETHOD(device_attach, uplcom_attach),
310 DEVMETHOD(device_detach, uplcom_detach),
314 static devclass_t uplcom_devclass;
316 static driver_t uplcom_driver = {
318 .methods = uplcom_methods,
319 .size = sizeof(struct uplcom_softc),
322 DRIVER_MODULE(uplcom, uhub, uplcom_driver, uplcom_devclass, NULL, 0);
323 MODULE_DEPEND(uplcom, ucom, 1, 1, 1);
324 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
325 MODULE_VERSION(uplcom, UPLCOM_MODVER);
328 uplcom_probe(device_t dev)
330 struct usb_attach_arg *uaa = device_get_ivars(dev);
334 if (uaa->usb_mode != USB_MODE_HOST) {
337 if (uaa->info.bConfigIndex != UPLCOM_CONFIG_INDEX) {
340 if (uaa->info.bIfaceIndex != UPLCOM_IFACE_INDEX) {
343 return (usbd_lookup_id_by_uaa(uplcom_devs, sizeof(uplcom_devs), uaa));
347 uplcom_attach(device_t dev)
349 struct usb_attach_arg *uaa = device_get_ivars(dev);
350 struct uplcom_softc *sc = device_get_softc(dev);
351 struct usb_interface *iface;
352 struct usb_interface_descriptor *id;
353 struct usb_device_descriptor *dd;
358 device_set_usb_desc(dev);
359 lockinit(&sc->sc_lock, "uplcom", 0, LK_CANRECURSE);
361 DPRINTF("sc = %p\n", sc);
363 sc->sc_udev = uaa->device;
365 /* Determine the chip type. This algorithm is taken from Linux. */
366 dd = usbd_get_device_descriptor(sc->sc_udev);
367 if (dd->bDeviceClass == 0x02)
368 sc->sc_chiptype = TYPE_PL2303;
369 else if (dd->bMaxPacketSize == 0x40)
370 sc->sc_chiptype = TYPE_PL2303HX;
372 sc->sc_chiptype = TYPE_PL2303;
374 DPRINTF("chiptype: %s\n",
375 (sc->sc_chiptype == TYPE_PL2303HX) ?
379 * USB-RSAQ1 has two interface
381 * USB-RSAQ1 | USB-RSAQ2
382 * -----------------+-----------------
383 * Interface 0 |Interface 0
384 * Interrupt(0x81) | Interrupt(0x81)
385 * -----------------+ BulkIN(0x02)
386 * Interface 1 | BulkOUT(0x83)
391 sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
392 sc->sc_iface_index[1] = UPLCOM_IFACE_INDEX;
394 iface = usbd_get_iface(uaa->device, UPLCOM_SECOND_IFACE_INDEX);
396 id = usbd_get_interface_descriptor(iface);
398 device_printf(dev, "no interface descriptor (2)\n");
401 sc->sc_data_iface_no = id->bInterfaceNumber;
402 sc->sc_iface_index[0] = UPLCOM_SECOND_IFACE_INDEX;
403 usbd_set_parent_iface(uaa->device,
404 UPLCOM_SECOND_IFACE_INDEX, uaa->info.bIfaceIndex);
406 sc->sc_data_iface_no = sc->sc_ctrl_iface_no;
407 sc->sc_iface_index[0] = UPLCOM_IFACE_INDEX;
410 error = usbd_transfer_setup(uaa->device,
411 sc->sc_iface_index, sc->sc_xfer, uplcom_config_data,
412 UPLCOM_N_TRANSFER, sc, &sc->sc_lock);
414 DPRINTF("one or more missing USB endpoints, "
415 "error=%s\n", usbd_errstr(error));
418 error = uplcom_reset(sc, uaa->device);
420 device_printf(dev, "reset failed, error=%s\n",
424 /* clear stall at first run */
425 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
426 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
427 usbd_xfer_set_stall(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
428 lockmgr(&sc->sc_lock, LK_RELEASE);
430 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
431 &uplcom_callback, &sc->sc_lock);
436 * do the initialization during attach so that the system does not
439 if (uplcom_pl2303_init(uaa->device, sc->sc_chiptype)) {
440 device_printf(dev, "init failed\n");
443 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
453 uplcom_detach(device_t dev)
455 struct uplcom_softc *sc = device_get_softc(dev);
457 DPRINTF("sc=%p\n", sc);
459 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
460 usbd_transfer_unsetup(sc->sc_xfer, UPLCOM_N_TRANSFER);
461 lockuninit(&sc->sc_lock);
467 uplcom_reset(struct uplcom_softc *sc, struct usb_device *udev)
469 struct usb_device_request req;
471 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
472 req.bRequest = UPLCOM_SET_REQUEST;
473 USETW(req.wValue, 0);
474 req.wIndex[0] = sc->sc_data_iface_no;
476 USETW(req.wLength, 0);
478 return (usbd_do_request(udev, NULL, &req, NULL));
482 uplcom_pl2303_do(struct usb_device *udev, int8_t req_type, uint8_t request,
483 uint16_t value, uint16_t index, uint16_t length)
485 struct usb_device_request req;
489 req.bmRequestType = req_type;
490 req.bRequest = request;
491 USETW(req.wValue, value);
492 USETW(req.wIndex, index);
493 USETW(req.wLength, length);
495 err = usbd_do_request(udev, NULL, &req, buf);
497 DPRINTF("error=%s\n", usbd_errstr(err));
504 uplcom_pl2303_init(struct usb_device *udev, uint8_t chiptype)
508 if (uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
509 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0)
510 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
511 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
512 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
513 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0)
514 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 1)
515 || uplcom_pl2303_do(udev, UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 1)
516 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0)
517 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0))
520 if (chiptype == TYPE_PL2303HX)
521 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0);
523 err = uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x24, 0);
527 if (uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 8, 0, 0)
528 || uplcom_pl2303_do(udev, UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 9, 0, 0))
534 uplcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
536 struct uplcom_softc *sc = ucom->sc_parent;
537 struct usb_device_request req;
539 DPRINTF("onoff = %d\n", onoff);
542 sc->sc_line |= UCDC_LINE_DTR;
544 sc->sc_line &= ~UCDC_LINE_DTR;
546 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
547 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
548 USETW(req.wValue, sc->sc_line);
549 req.wIndex[0] = sc->sc_data_iface_no;
551 USETW(req.wLength, 0);
553 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
554 &req, NULL, 0, 1000);
558 uplcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
560 struct uplcom_softc *sc = ucom->sc_parent;
561 struct usb_device_request req;
563 DPRINTF("onoff = %d\n", onoff);
566 sc->sc_line |= UCDC_LINE_RTS;
568 sc->sc_line &= ~UCDC_LINE_RTS;
570 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
571 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
572 USETW(req.wValue, sc->sc_line);
573 req.wIndex[0] = sc->sc_data_iface_no;
575 USETW(req.wLength, 0);
577 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
578 &req, NULL, 0, 1000);
582 uplcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
584 struct uplcom_softc *sc = ucom->sc_parent;
585 struct usb_device_request req;
588 DPRINTF("onoff = %d\n", onoff);
590 temp = (onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
592 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
593 req.bRequest = UCDC_SEND_BREAK;
594 USETW(req.wValue, temp);
595 req.wIndex[0] = sc->sc_data_iface_no;
597 USETW(req.wLength, 0);
599 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
600 &req, NULL, 0, 1000);
603 static const int32_t uplcom_rates[] = {
604 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
605 19200, 28800, 38400, 57600, 115200,
607 * Higher speeds are probably possible. PL2303X supports up to
608 * 6Mb and can set any rate
610 230400, 460800, 614400, 921600, 1228800
613 #define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
616 uplcom_pre_param(struct ucom_softc *ucom, struct termios *t)
618 struct uplcom_softc *sc = ucom->sc_parent;
624 * Check requested baud rate.
626 * The PL2303 can only set specific baud rates, up to 1228800 baud.
627 * The PL2303X can set any baud rate up to 6Mb.
628 * The PL2303HX rev. D can set any baud rate up to 12Mb.
630 * XXX: We currently cannot identify the PL2303HX rev. D, so treat
631 * it the same as the PL2303X.
633 if (sc->sc_chiptype != TYPE_PL2303HX) {
634 for (i = 0; i < N_UPLCOM_RATES; i++) {
635 if (uplcom_rates[i] == t->c_ospeed)
639 if (t->c_ospeed <= 6000000)
643 DPRINTF("uplcom_param: bad baud rate (%d)\n", t->c_ospeed);
648 uplcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
650 struct uplcom_softc *sc = ucom->sc_parent;
651 struct usb_cdc_line_state ls;
652 struct usb_device_request req;
654 DPRINTF("sc = %p\n", sc);
656 memset(&ls, 0, sizeof(ls));
658 USETDW(ls.dwDTERate, t->c_ospeed);
660 if (t->c_cflag & CSTOPB) {
661 ls.bCharFormat = UCDC_STOP_BIT_2;
663 ls.bCharFormat = UCDC_STOP_BIT_1;
666 if (t->c_cflag & PARENB) {
667 if (t->c_cflag & PARODD) {
668 ls.bParityType = UCDC_PARITY_ODD;
670 ls.bParityType = UCDC_PARITY_EVEN;
673 ls.bParityType = UCDC_PARITY_NONE;
676 switch (t->c_cflag & CSIZE) {
691 DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
692 UGETDW(ls.dwDTERate), ls.bCharFormat,
693 ls.bParityType, ls.bDataBits);
695 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
696 req.bRequest = UCDC_SET_LINE_CODING;
697 USETW(req.wValue, 0);
698 req.wIndex[0] = sc->sc_data_iface_no;
700 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
702 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
705 if (t->c_cflag & CRTSCTS) {
707 DPRINTF("crtscts = on\n");
709 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
710 req.bRequest = UPLCOM_SET_REQUEST;
711 USETW(req.wValue, 0);
712 if (sc->sc_chiptype == TYPE_PL2303HX)
713 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
715 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
716 USETW(req.wLength, 0);
718 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
719 &req, NULL, 0, 1000);
721 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
722 req.bRequest = UPLCOM_SET_REQUEST;
723 USETW(req.wValue, 0);
724 USETW(req.wIndex, 0);
725 USETW(req.wLength, 0);
726 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
727 &req, NULL, 0, 1000);
732 uplcom_start_read(struct ucom_softc *ucom)
734 struct uplcom_softc *sc = ucom->sc_parent;
736 /* start interrupt endpoint */
737 usbd_transfer_start(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
739 /* start read endpoint */
740 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
744 uplcom_stop_read(struct ucom_softc *ucom)
746 struct uplcom_softc *sc = ucom->sc_parent;
748 /* stop interrupt endpoint */
749 usbd_transfer_stop(sc->sc_xfer[UPLCOM_INTR_DT_RD]);
751 /* stop read endpoint */
752 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_RD]);
756 uplcom_start_write(struct ucom_softc *ucom)
758 struct uplcom_softc *sc = ucom->sc_parent;
760 usbd_transfer_start(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
764 uplcom_stop_write(struct ucom_softc *ucom)
766 struct uplcom_softc *sc = ucom->sc_parent;
768 usbd_transfer_stop(sc->sc_xfer[UPLCOM_BULK_DT_WR]);
772 uplcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
774 struct uplcom_softc *sc = ucom->sc_parent;
783 uplcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
785 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
786 struct usb_page_cache *pc;
790 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
792 switch (USB_GET_STATE(xfer)) {
793 case USB_ST_TRANSFERRED:
795 DPRINTF("actlen = %u\n", actlen);
799 pc = usbd_xfer_get_frame(xfer, 0);
800 usbd_copy_out(pc, 0, buf, sizeof(buf));
802 DPRINTF("status = 0x%02x\n", buf[8]);
807 if (buf[8] & RSAQ_STATUS_CTS) {
808 sc->sc_msr |= SER_CTS;
810 if (buf[8] & RSAQ_STATUS_DSR) {
811 sc->sc_msr |= SER_DSR;
813 if (buf[8] & RSAQ_STATUS_DCD) {
814 sc->sc_msr |= SER_DCD;
816 ucom_status_change(&sc->sc_ucom);
820 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
821 usbd_transfer_submit(xfer);
825 if (error != USB_ERR_CANCELLED) {
826 /* try to clear stall first */
827 usbd_xfer_set_stall(xfer);
835 uplcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
837 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
838 struct usb_page_cache *pc;
841 switch (USB_GET_STATE(xfer)) {
843 case USB_ST_TRANSFERRED:
845 pc = usbd_xfer_get_frame(xfer, 0);
846 if (ucom_get_data(&sc->sc_ucom, pc, 0,
847 UPLCOM_BULK_BUF_SIZE, &actlen)) {
849 DPRINTF("actlen = %d\n", actlen);
851 usbd_xfer_set_frame_len(xfer, 0, actlen);
852 usbd_transfer_submit(xfer);
857 if (error != USB_ERR_CANCELLED) {
858 /* try to clear stall first */
859 usbd_xfer_set_stall(xfer);
867 uplcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
869 struct uplcom_softc *sc = usbd_xfer_softc(xfer);
870 struct usb_page_cache *pc;
873 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
875 switch (USB_GET_STATE(xfer)) {
876 case USB_ST_TRANSFERRED:
877 pc = usbd_xfer_get_frame(xfer, 0);
878 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
882 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
883 usbd_transfer_submit(xfer);
887 if (error != USB_ERR_CANCELLED) {
888 /* try to clear stall first */
889 usbd_xfer_set_stall(xfer);
897 uplcom_poll(struct ucom_softc *ucom)
899 struct uplcom_softc *sc = ucom->sc_parent;
900 usbd_transfer_poll(sc->sc_xfer, UPLCOM_N_TRANSFER);