2 * $NetBSD: uplcom.c,v 1.21 2001/11/13 06:24:56 lukem Exp $
3 * $FreeBSD: src/sys/dev/usb/uplcom.c,v 1.39 2006/09/07 00:06:42 imp 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 devices devices driven by Prolific PL-2303
70 * (known also as PL-2303H), PL-2303X and PL-2303HX USB-to-RS232 bridge chip.
71 * The devices are sold under many different brand names.
73 * Datasheets are available at Prolific www site at http://www.prolific.com.tw
74 * The datasheets don't contain full programming information for the chip.
76 * PL-2303HX has the same features as PL-2303X (at least from the point of
77 * view of device driver) but is pin-to-pin compatible with PL-2303.
79 * There are several differences between PL-2303 and PL-2303(H)X.
80 * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
81 * different command for controlling CRTSCTS and needs special
82 * sequence of commands for initialization which aren't also
83 * documented in the datasheet.
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/malloc.h>
91 #include <sys/fcntl.h>
95 #include <sys/select.h>
98 #include <sys/sysctl.h>
99 #include <sys/taskqueue.h>
101 #include <bus/usb/usb.h>
102 #include <bus/usb/usbcdc.h>
104 #include <bus/usb/usbdi.h>
105 #include <bus/usb/usbdi_util.h>
106 #include <bus/usb/usb_quirks.h>
108 #include "../ucom/ucomvar.h"
110 SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
112 static int uplcomdebug = 0;
113 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
114 &uplcomdebug, 0, "uplcom debug level");
116 #define DPRINTFN(n, x) do { \
117 if (uplcomdebug > (n)) \
121 #define DPRINTFN(n, x)
123 #define DPRINTF(x) DPRINTFN(0, x)
125 #define UPLCOM_MODVER 1 /* module version */
127 #define UPLCOM_CONFIG_INDEX 0
128 #define UPLCOM_IFACE_INDEX 0
129 #define UPLCOM_SECOND_IFACE_INDEX 1
131 #ifndef UPLCOM_INTR_INTERVAL
132 #define UPLCOM_INTR_INTERVAL 100 /* ms */
135 #define UPLCOM_SET_REQUEST 0x01
136 #define UPLCOM_SET_CRTSCTS 0x41
137 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
138 #define RSAQ_STATUS_CTS 0x80
139 #define RSAQ_STATUS_DSR 0x02
140 #define RSAQ_STATUS_DCD 0x01
142 #define TYPE_PL2303 0
143 #define TYPE_PL2303X 1
145 struct uplcom_softc {
146 struct ucom_softc sc_ucom;
148 int sc_iface_number; /* interface number */
150 usbd_interface_handle sc_intr_iface; /* interrupt interface */
151 int sc_intr_number; /* interrupt number */
152 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
153 u_char *sc_intr_buf; /* interrupt buffer */
156 usb_cdc_line_state_t sc_line_state; /* current line state */
157 u_char sc_dtr; /* current DTR state */
158 u_char sc_rts; /* current RTS state */
161 u_char sc_lsr; /* Local status register */
162 u_char sc_msr; /* uplcom status register */
164 int sc_chiptype; /* Type of chip */
170 * These are the maximum number of bytes transferred per frame.
171 * The output buffer size cannot be increased due to the size encoding.
173 #define UPLCOMIBUFSIZE 256
174 #define UPLCOMOBUFSIZE 256
176 static usbd_status uplcom_reset(struct uplcom_softc *);
177 static usbd_status uplcom_set_line_coding(struct uplcom_softc *,
178 usb_cdc_line_state_t *);
179 static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
180 static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
182 static void uplcom_set(void *, int, int, int);
183 static void uplcom_dtr(struct uplcom_softc *, int);
184 static void uplcom_rts(struct uplcom_softc *, int);
185 static void uplcom_break(struct uplcom_softc *, int);
186 static void uplcom_set_line_state(struct uplcom_softc *);
187 static void uplcom_get_status(void *, int, u_char *, u_char *);
189 static int uplcom_ioctl(void *, int, u_long, caddr_t, int, struct thread *);
191 static int uplcom_param(void *, int, struct termios *);
192 static int uplcom_open(void *, int);
193 static void uplcom_close(void *, int);
194 static void uplcom_notify(void *, int);
196 struct ucom_callback uplcom_callback = {
200 NULL, /* uplcom_ioctl, TODO */
207 static const struct usb_devno uplcom_devs[] = {
208 { USB_DEVICE(0x0413, 0x2101) }, /* Leadtek 9531 GPS */
209 { USB_DEVICE(0x0421, 0x1234) }, /* Nokia CA-42 USB data cable clones*/
210 { USB_DEVICE(0x04bb, 0x0a03) }, /* I/O DATA USB-RSAQ USB to serial */
211 { USB_DEVICE(0x04bb, 0x0a0e) }, /* I/O DATA USB-RSAQ5 USB to serial */
212 { USB_DEVICE(0x04bf, 0x0115) }, /* TDK USB-PDC adapter UPA9664 */
213 { USB_DEVICE(0x04bf, 0x0117) }, /* TDK USB-PHS adapter UHA6400 */
214 { USB_DEVICE(0x04da, 0x3900) }, /* Panasonic 50" touch panel */
215 { USB_DEVICE(0x04e8, 0x8001) }, /* Samsung I330 smartphone cradle */
216 { USB_DEVICE(0x050d, 0x0257) }, /* Belkin F5U257 USB to serial */
217 { USB_DEVICE(0x0547, 0x2008) }, /* Anchor serial */
218 { USB_DEVICE(0x0557, 0x2008) }, /* Aten UC232A USB to serial */
219 { USB_DEVICE(0x056e, 0x5003) }, /* ELECOM UC-SGT USB to serial */
220 { USB_DEVICE(0x056e, 0x5004) }, /* ELECOM UC-SGT USB to serial */
221 { USB_DEVICE(0x0584, 0xb000) }, /* RATOC REX-USB60 USB to serial */
222 { USB_DEVICE(0x058f, 0x9720) }, /* Alcor AU9720 USB to serial */
223 { USB_DEVICE(0x067b, 0x04bb) }, /* I/O DATA USB-RSAQ2 USB to serial */
224 { USB_DEVICE(0x067b, 0x1234) }, /* Unbranded DCU-11 clone */
225 { USB_DEVICE(0x067b, 0x2303) }, /* IOGEAR/ATEN UC-232A, ST Lab
227 { USB_DEVICE(0x067b, 0xaaa0) }, /* Microsoft OEM Pharos 360 GPS */
228 { USB_DEVICE(0x067b, 0xaaa2) }, /* I/O DATA USB-RSAQ3 USB to serial */
229 { USB_DEVICE(0x0731, 0x0528) }, /* Sony-Ericsson DCU-10/DCU-11 */
230 { USB_DEVICE(0x0731, 0x2003) }, /* Susteen Datapilot Universal-2 Phone
232 { USB_DEVICE(0x0745, 0x0001) }, /* Syntech CPT-8001C barcode scanner
234 { USB_DEVICE(0x079b, 0x0027) }, /* Sagem USB data cables */
235 { USB_DEVICE(0x0833, 0x012e) }, /* SOURCENEXT KeikaiDenwa 8 with
237 { USB_DEVICE(0x0833, 0x039f) }, /* SOURCENEXT KeikaiDenwa 8 */
238 { USB_DEVICE(0x0b41, 0x0011) }, /* HAL Corporation Crossam2+USB */
239 { USB_DEVICE(0x0b8c, 0x2303) }, /* Smart Technologies USB to serial */
240 { USB_DEVICE(0x0df7, 0x0620) }, /* Mobile Action MA-620 IrDA */
241 { USB_DEVICE(0x0e55, 0x110b) }, /* Speed Dragon MS3303H */
242 { USB_DEVICE(0x0eba, 0x1080) }, /* Itegno GSM/GPRS modem */
243 { USB_DEVICE(0x0eba, 0x2080) }, /* Itegno CDMA 1x card */
244 { USB_DEVICE(0x10b5, 0xac70) }, /* PLX CA-42 USB data cable clone */
245 { USB_DEVICE(0x11f5, 0x0001) }, /* Siemens SX1 cellphone */
246 { USB_DEVICE(0x11f5, 0x0003) }, /* Siemens x65 series cellphones */
247 { USB_DEVICE(0x11f5, 0x0004) }, /* Siemens x75 series cellphones */
248 { USB_DEVICE(0x11f6, 0x2001) }, /* Willcom W-SIM */
249 { USB_DEVICE(0x11f7, 0x02df) }, /* Alcatel One Touch 535/735 phones */
250 { USB_DEVICE(0x1453, 0x4026) }, /* RADIOSHACK USB cable */
251 { USB_DEVICE(0x2478, 0x2008) }, /* Tripp-Lite U209-000-R */
252 { USB_DEVICE(0x6189, 0x2068) }, /* Sitecom USB to serial cable */
255 static device_probe_t uplcom_match;
256 static device_attach_t uplcom_attach;
257 static device_detach_t uplcom_detach;
259 static device_method_t uplcom_methods[] = {
260 /* Device interface */
261 DEVMETHOD(device_probe, uplcom_match),
262 DEVMETHOD(device_attach, uplcom_attach),
263 DEVMETHOD(device_detach, uplcom_detach),
267 static driver_t uplcom_driver = {
270 sizeof (struct uplcom_softc)
273 DRIVER_MODULE(uplcom, uhub, uplcom_driver, ucom_devclass, usbd_driver_load, NULL);
274 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
275 MODULE_DEPEND(uplcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
276 MODULE_VERSION(uplcom, UPLCOM_MODVER);
278 static int uplcominterval = UPLCOM_INTR_INTERVAL;
281 sysctl_hw_usb_uplcom_interval(SYSCTL_HANDLER_ARGS)
285 val = uplcominterval;
286 err = sysctl_handle_int(oidp, &val, 0, req);
287 if (err != 0 || req->newptr == NULL)
289 if (0 < val && val <= 1000)
290 uplcominterval = val;
297 SYSCTL_PROC(_hw_usb_uplcom, OID_AUTO, interval, CTLTYPE_INT | CTLFLAG_RW,
298 0, sizeof(int), sysctl_hw_usb_uplcom_interval,
299 "I", "uplcom interrupt pipe interval");
302 uplcom_match(device_t self)
304 struct usb_attach_arg *uaa = device_get_ivars(self);
306 if (uaa->iface != NULL)
307 return (UMATCH_NONE);
309 return (usb_lookup(uplcom_devs, uaa->vendor, uaa->product) != NULL ?
310 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
314 uplcom_attach(device_t self)
316 struct uplcom_softc *sc = device_get_softc(self);
317 struct usb_attach_arg *uaa = device_get_ivars(self);
318 usbd_device_handle dev = uaa->device;
319 usb_device_descriptor_t *dd;
320 struct ucom_softc *ucom;
321 usb_config_descriptor_t *cdesc;
322 usb_interface_descriptor_t *id;
323 usb_endpoint_descriptor_t *ed;
328 bzero(sc, sizeof (struct uplcom_softc));
332 ucom->sc_iface = uaa->iface;
334 DPRINTF(("uplcom attach: sc = %p\n", sc));
336 dd = usbd_get_device_descriptor(uaa->device);
342 * Determine chip type with algorithm sequence taken from the
343 * Linux driver as I'm not aware of any better method. Device
344 * release number in chip could be (and in fact is in many cases)
345 * replaced by the contents of external EEPROM etc.
347 else if (dd->bDeviceClass == 0x02)
348 sc->sc_chiptype = TYPE_PL2303;
349 else if (dd->bMaxPacketSize == 0x40)
350 sc->sc_chiptype = TYPE_PL2303X;
352 sc->sc_chiptype = TYPE_PL2303;
355 /* print the chip type */
356 if (sc->sc_chiptype == TYPE_PL2303X) {
357 DPRINTF(("uplcom_attach: chiptype 2303X\n"));
359 DPRINTF(("uplcom_attach: chiptype 2303\n"));
363 /* initialize endpoints */
364 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
365 sc->sc_intr_number = -1;
366 sc->sc_intr_pipe = NULL;
368 /* Move the device into the configured state. */
369 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
371 device_printf(ucom->sc_dev, "failed to set configuration: %s\n",
377 /* get the config descriptor */
378 cdesc = usbd_get_config_descriptor(ucom->sc_udev);
381 device_printf(ucom->sc_dev, "failed to get configuration "
387 /* get the (first/common) interface */
388 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
391 device_printf(ucom->sc_dev, "failed to get interface: %s\n",
397 /* Find the interrupt endpoints */
399 id = usbd_get_interface_descriptor(ucom->sc_iface);
400 sc->sc_iface_number = id->bInterfaceNumber;
402 for (i = 0; i < id->bNumEndpoints; i++) {
403 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
405 device_printf(ucom->sc_dev, "no endpoint descriptor "
411 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
412 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
413 sc->sc_intr_number = ed->bEndpointAddress;
414 sc->sc_isize = UGETW(ed->wMaxPacketSize);
418 if (sc->sc_intr_number == -1) {
419 device_printf(ucom->sc_dev, "could not find interrupt in\n");
424 /* keep interface for interrupt */
425 sc->sc_intr_iface = ucom->sc_iface;
428 * USB-RSAQ1 has two interface
430 * USB-RSAQ1 | USB-RSAQ2
431 * -----------------+-----------------
432 * Interface 0 |Interface 0
433 * Interrupt(0x81) | Interrupt(0x81)
434 * -----------------+ BulkIN(0x02)
435 * Interface 1 | BulkOUT(0x83)
439 if (cdesc->bNumInterface == 2) {
440 err = usbd_device2interface_handle(dev,
441 UPLCOM_SECOND_IFACE_INDEX,
444 device_printf(ucom->sc_dev, "failed to get second "
445 "interface: %s\n", usbd_errstr(err));
451 /* Find the bulk{in,out} endpoints */
453 id = usbd_get_interface_descriptor(ucom->sc_iface);
454 sc->sc_iface_number = id->bInterfaceNumber;
456 for (i = 0; i < id->bNumEndpoints; i++) {
457 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
459 device_printf(ucom->sc_dev, "no endpoint descriptor "
465 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
466 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
467 ucom->sc_bulkin_no = ed->bEndpointAddress;
468 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
469 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
470 ucom->sc_bulkout_no = ed->bEndpointAddress;
474 if (ucom->sc_bulkin_no == -1) {
475 device_printf(ucom->sc_dev, "could not find data bulk in\n");
480 if (ucom->sc_bulkout_no == -1) {
481 device_printf(ucom->sc_dev, "could not find data bulk out\n");
486 sc->sc_dtr = sc->sc_rts = -1;
487 ucom->sc_parent = sc;
488 ucom->sc_portno = UCOM_UNK_PORTNO;
489 /* bulkin, bulkout set above */
490 ucom->sc_ibufsize = UPLCOMIBUFSIZE;
491 ucom->sc_obufsize = UPLCOMOBUFSIZE;
492 ucom->sc_ibufsizepad = UPLCOMIBUFSIZE;
493 ucom->sc_opkthdrlen = 0;
494 ucom->sc_callback = &uplcom_callback;
496 err = uplcom_reset(sc);
499 device_printf(ucom->sc_dev, "reset failed: %s\n",
505 DPRINTF(("uplcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
506 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
508 TASK_INIT(&sc->sc_task, 0, uplcom_notify, sc);
509 ucom_attach(&sc->sc_ucom);
518 uplcom_detach(device_t self)
520 struct uplcom_softc *sc = device_get_softc(self);
523 DPRINTF(("uplcom_detach: sc = %p\n", sc));
525 if (sc->sc_intr_pipe != NULL) {
526 usbd_abort_pipe(sc->sc_intr_pipe);
527 usbd_close_pipe(sc->sc_intr_pipe);
528 kfree(sc->sc_intr_buf, M_USBDEV);
529 sc->sc_intr_pipe = NULL;
532 sc->sc_ucom.sc_dying = 1;
534 rv = ucom_detach(&sc->sc_ucom);
540 uplcom_reset(struct uplcom_softc *sc)
542 usb_device_request_t req;
545 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
546 req.bRequest = UPLCOM_SET_REQUEST;
547 USETW(req.wValue, 0);
548 USETW(req.wIndex, sc->sc_iface_number);
549 USETW(req.wLength, 0);
551 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
553 device_printf(sc->sc_ucom.sc_dev, "uplcom_reset: %s\n",
561 struct pl2303x_init {
569 static const struct pl2303x_init pl2303x[] = {
570 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
571 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
572 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
573 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
574 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
575 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
576 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
577 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
578 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
579 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
580 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
582 #define N_PL2302X_INIT NELEM(pl2303x)
585 uplcom_pl2303x_init(struct uplcom_softc *sc)
587 usb_device_request_t req;
591 for (i = 0; i < N_PL2302X_INIT; i++) {
592 req.bmRequestType = pl2303x[i].req_type;
593 req.bRequest = pl2303x[i].request;
594 USETW(req.wValue, pl2303x[i].value);
595 USETW(req.wIndex, pl2303x[i].index);
596 USETW(req.wLength, pl2303x[i].length);
598 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
600 device_printf(sc->sc_ucom.sc_dev,
601 "uplcom_pl2303x_init: %d: %s\n", i,
611 uplcom_set_line_state(struct uplcom_softc *sc)
613 usb_device_request_t req;
617 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
618 (sc->sc_rts ? UCDC_LINE_RTS : 0);
619 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
620 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
621 USETW(req.wValue, ls);
622 USETW(req.wIndex, sc->sc_iface_number);
623 USETW(req.wLength, 0);
625 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
627 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_line_status: "
628 "%s\n", usbd_errstr(err));
632 uplcom_set(void *addr, int portno, int reg, int onoff)
634 struct uplcom_softc *sc = addr;
638 uplcom_dtr(sc, onoff);
641 uplcom_rts(sc, onoff);
644 uplcom_break(sc, onoff);
652 uplcom_dtr(struct uplcom_softc *sc, int onoff)
654 DPRINTF(("uplcom_dtr: onoff = %d\n", onoff));
656 if (sc->sc_dtr == onoff)
660 uplcom_set_line_state(sc);
664 uplcom_rts(struct uplcom_softc *sc, int onoff)
666 DPRINTF(("uplcom_rts: onoff = %d\n", onoff));
668 if (sc->sc_rts == onoff)
672 uplcom_set_line_state(sc);
676 uplcom_break(struct uplcom_softc *sc, int onoff)
678 usb_device_request_t req;
681 DPRINTF(("uplcom_break: onoff = %d\n", onoff));
683 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
684 req.bRequest = UCDC_SEND_BREAK;
685 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
686 USETW(req.wIndex, sc->sc_iface_number);
687 USETW(req.wLength, 0);
689 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
691 device_printf(sc->sc_ucom.sc_dev, "uplcom_break: %s\n",
696 uplcom_set_crtscts(struct uplcom_softc *sc)
698 usb_device_request_t req;
701 DPRINTF(("uplcom_set_crtscts: on\n"));
703 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
704 req.bRequest = UPLCOM_SET_REQUEST;
705 USETW(req.wValue, 0);
706 if (sc->sc_chiptype == TYPE_PL2303X)
707 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
709 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
710 USETW(req.wLength, 0);
712 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
714 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_crtscts: %s\n",
719 return (USBD_NORMAL_COMPLETION);
723 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
725 usb_device_request_t req;
729 "uplcom_set_line_coding: rate = %d, fmt = %d, parity = %d bits = %d\n",
730 UGETDW(state->dwDTERate), state->bCharFormat,
731 state->bParityType, state->bDataBits));
733 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
734 DPRINTF(("uplcom_set_line_coding: already set\n"));
735 return (USBD_NORMAL_COMPLETION);
738 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
739 req.bRequest = UCDC_SET_LINE_CODING;
740 USETW(req.wValue, 0);
741 USETW(req.wIndex, sc->sc_iface_number);
742 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
744 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, state);
746 device_printf(sc->sc_ucom.sc_dev, "uplcom_set_line_coding: "
747 "%s\n", usbd_errstr(err));
751 sc->sc_line_state = *state;
753 return (USBD_NORMAL_COMPLETION);
756 static const int uplcom_rates[] = {
757 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
758 19200, 28800, 38400, 57600, 115200,
760 * Higher speeds are probably possible. PL2303X supports up to
761 * 6Mb and can set any rate
763 230400, 460800, 614400, 921600, 1228800
765 #define N_UPLCOM_RATES NELEM(uplcom_rates)
768 uplcom_param(void *addr, int portno, struct termios *t)
770 struct uplcom_softc *sc = addr;
772 usb_cdc_line_state_t ls;
775 DPRINTF(("uplcom_param: sc = %p\n", sc));
777 /* Check requested baud rate */
778 for (i = 0; i < N_UPLCOM_RATES; i++)
779 if (uplcom_rates[i] == t->c_ospeed)
781 if (i == N_UPLCOM_RATES) {
782 DPRINTF(("uplcom_param: bad baud rate (%d)\n", t->c_ospeed));
786 USETDW(ls.dwDTERate, t->c_ospeed);
787 if (ISSET(t->c_cflag, CSTOPB))
788 ls.bCharFormat = UCDC_STOP_BIT_2;
790 ls.bCharFormat = UCDC_STOP_BIT_1;
791 if (ISSET(t->c_cflag, PARENB)) {
792 if (ISSET(t->c_cflag, PARODD))
793 ls.bParityType = UCDC_PARITY_ODD;
795 ls.bParityType = UCDC_PARITY_EVEN;
797 ls.bParityType = UCDC_PARITY_NONE;
798 switch (ISSET(t->c_cflag, CSIZE)) {
813 err = uplcom_set_line_coding(sc, &ls);
817 if (ISSET(t->c_cflag, CRTSCTS)) {
818 err = uplcom_set_crtscts(sc);
827 uplcom_open(void *addr, int portno)
829 struct uplcom_softc *sc = addr;
832 if (sc->sc_ucom.sc_dying)
835 DPRINTF(("uplcom_open: sc = %p\n", sc));
837 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
838 sc->sc_status = 0; /* clear status bit */
839 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
840 err = usbd_open_pipe_intr(sc->sc_intr_iface,
850 device_printf(sc->sc_ucom.sc_dev, "cannot open "
851 "interrupt pipe (addr %d)\n",
857 if (sc->sc_chiptype == TYPE_PL2303X)
858 return (uplcom_pl2303x_init(sc));
864 uplcom_close(void *addr, int portno)
866 struct uplcom_softc *sc = addr;
869 if (sc->sc_ucom.sc_dying)
872 DPRINTF(("uplcom_close: close\n"));
874 if (sc->sc_intr_pipe != NULL) {
875 err = usbd_abort_pipe(sc->sc_intr_pipe);
877 device_printf(sc->sc_ucom.sc_dev, "abort interrupt "
878 "pipe failed: %s\n", usbd_errstr(err));
879 err = usbd_close_pipe(sc->sc_intr_pipe);
881 device_printf(sc->sc_ucom.sc_dev, "close interrupt "
882 "pipe failed: %s\n", usbd_errstr(err));
883 kfree(sc->sc_intr_buf, M_USBDEV);
884 sc->sc_intr_pipe = NULL;
889 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
891 struct uplcom_softc *sc = priv;
892 u_char *buf = sc->sc_intr_buf;
895 if (sc->sc_ucom.sc_dying)
898 if (status != USBD_NORMAL_COMPLETION) {
899 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
902 DPRINTF(("%s: uplcom_intr: abnormal status: %s\n",
903 device_get_nameunit(sc->sc_ucom.sc_dev),
904 usbd_errstr(status)));
905 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
909 DPRINTF(("%s: uplcom status = %02x\n",
910 device_get_nameunit(sc->sc_ucom.sc_dev), buf[8]));
912 sc->sc_lsr = sc->sc_msr = 0;
914 if (ISSET(pstatus, RSAQ_STATUS_CTS))
915 sc->sc_msr |= UMSR_CTS;
917 sc->sc_msr &= ~UMSR_CTS;
918 if (ISSET(pstatus, RSAQ_STATUS_DSR))
919 sc->sc_msr |= UMSR_DSR;
921 sc->sc_msr &= ~UMSR_DSR;
922 if (ISSET(pstatus, RSAQ_STATUS_DCD))
923 sc->sc_msr |= UMSR_DCD;
925 sc->sc_msr &= ~UMSR_DCD;
927 /* Deferred notifying to the ucom layer */
928 taskqueue_enqueue(taskqueue_swi, &sc->sc_task);
932 uplcom_notify(void *arg, int count)
934 struct uplcom_softc *sc;
936 sc = (struct uplcom_softc *)arg;
937 if (sc->sc_ucom.sc_dying)
939 ucom_status_change(&sc->sc_ucom);
943 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
945 struct uplcom_softc *sc = addr;
947 DPRINTF(("uplcom_get_status:\n"));
957 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
960 struct uplcom_softc *sc = addr;
963 if (sc->sc_ucom.sc_dying)
966 DPRINTF(("uplcom_ioctl: cmd = 0x%08lx\n", cmd));
972 case USB_GET_CM_OVER_DATA:
973 case USB_SET_CM_OVER_DATA:
977 DPRINTF(("uplcom_ioctl: unknown\n"));