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 $
4 * $DragonFly: src/sys/dev/usbmisc/uplcom/uplcom.c,v 1.17 2007/08/02 11:20:29 hasso Exp $
8 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 2001 The NetBSD Foundation, Inc.
35 * All rights reserved.
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Ichiro FUKUHARA (ichiro@ichiro.org).
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
70 * This driver supports several devices devices driven by Prolific PL-2303
71 * (known also as PL-2303H), PL-2303X and PL-2303HX USB-to-RS232 bridge chip.
72 * The devices are sold under many different brand names.
74 * Datasheets are available at Prolific www site at http://www.prolific.com.tw
75 * The datasheets don't contain full programming information for the chip.
77 * PL-2303HX has the same features as PL-2303X (at least from the point of
78 * view of device driver) but is pin-to-pin compatible with PL-2303.
80 * There are several differences between PL-2303 and PL-2303(H)X.
81 * PL-2303(H)X can do higher bitrate in bulk mode, has _probably_
82 * different command for controlling CRTSCTS and needs special
83 * sequence of commands for initialization which aren't also
84 * documented in the datasheet.
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/kernel.h>
90 #include <sys/malloc.h>
92 #include <sys/ioccom.h>
93 #include <sys/fcntl.h>
97 #include <sys/select.h>
99 #include <sys/vnode.h>
100 #include <sys/poll.h>
101 #include <sys/sysctl.h>
102 #include <sys/taskqueue.h>
104 #include <bus/usb/usb.h>
105 #include <bus/usb/usbcdc.h>
107 #include <bus/usb/usbdi.h>
108 #include <bus/usb/usbdi_util.h>
109 #include <bus/usb/usbdevs.h>
110 #include <bus/usb/usb_quirks.h>
112 #include "../ucom/ucomvar.h"
114 SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
116 static int uplcomdebug = 0;
117 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
118 &uplcomdebug, 0, "uplcom debug level");
120 #define DPRINTFN(n, x) do { \
121 if (uplcomdebug > (n)) \
125 #define DPRINTFN(n, x)
127 #define DPRINTF(x) DPRINTFN(0, x)
129 #define UPLCOM_MODVER 1 /* module version */
131 #define UPLCOM_CONFIG_INDEX 0
132 #define UPLCOM_IFACE_INDEX 0
133 #define UPLCOM_SECOND_IFACE_INDEX 1
135 #ifndef UPLCOM_INTR_INTERVAL
136 #define UPLCOM_INTR_INTERVAL 100 /* ms */
139 #define UPLCOM_SET_REQUEST 0x01
140 #define UPLCOM_SET_CRTSCTS 0x41
141 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
142 #define RSAQ_STATUS_CTS 0x80
143 #define RSAQ_STATUS_DSR 0x02
144 #define RSAQ_STATUS_DCD 0x01
146 #define TYPE_PL2303 0
147 #define TYPE_PL2303X 1
149 struct uplcom_softc {
150 struct ucom_softc sc_ucom;
152 int sc_iface_number; /* interface number */
154 usbd_interface_handle sc_intr_iface; /* interrupt interface */
155 int sc_intr_number; /* interrupt number */
156 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
157 u_char *sc_intr_buf; /* interrupt buffer */
160 usb_cdc_line_state_t sc_line_state; /* current line state */
161 u_char sc_dtr; /* current DTR state */
162 u_char sc_rts; /* current RTS state */
165 u_char sc_lsr; /* Local status register */
166 u_char sc_msr; /* uplcom status register */
168 int sc_chiptype; /* Type of chip */
174 * These are the maximum number of bytes transferred per frame.
175 * The output buffer size cannot be increased due to the size encoding.
177 #define UPLCOMIBUFSIZE 256
178 #define UPLCOMOBUFSIZE 256
180 static usbd_status uplcom_reset(struct uplcom_softc *);
181 static usbd_status uplcom_set_line_coding(struct uplcom_softc *,
182 usb_cdc_line_state_t *);
183 static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
184 static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
186 static void uplcom_set(void *, int, int, int);
187 static void uplcom_dtr(struct uplcom_softc *, int);
188 static void uplcom_rts(struct uplcom_softc *, int);
189 static void uplcom_break(struct uplcom_softc *, int);
190 static void uplcom_set_line_state(struct uplcom_softc *);
191 static void uplcom_get_status(void *, int, u_char *, u_char *);
193 static int uplcom_ioctl(void *, int, u_long, caddr_t, int, struct thread *);
195 static int uplcom_param(void *, int, struct termios *);
196 static int uplcom_open(void *, int);
197 static void uplcom_close(void *, int);
198 static void uplcom_notify(void *, int);
200 struct ucom_callback uplcom_callback = {
204 NULL, /* uplcom_ioctl, TODO */
211 static const struct usb_devno uplcom_devs[] = {
212 { USB_VENDOR_RADIOSHACK, USB_PRODUCT_RADIOSHACK_USBCABLE },
214 /* I/O DATA USB-RSAQ */
215 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
216 /* Prolific Pharos */
217 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
218 /* I/O DATA USB-RSAQ2 */
219 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
220 /* I/O DATA USB-RSAQ3 */
221 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
223 { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_WSIM },
224 /* PLANEX USB-RS232 URS-03 */
225 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
226 /* IOGEAR/ATEN UC-232A, ST Lab USB-SERIAL-X etc */
227 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
228 /* TDK USB-PHS Adapter UHA6400 */
229 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
230 /* RATOC REX-USB60 */
231 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
233 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
234 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
235 /* Sony Ericsson USB Cable */
236 { USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10 },
237 /* SOURCENEXT KeikaiDenwa 8 */
238 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
239 /* SOURCENEXT KeikaiDenwa 8 with charger */
240 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
241 /* HAL Corporation Crossam2+USB */
242 { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
243 /* Sitecom USB to Serial */
244 { USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
245 /* Tripp-Lite U209-000-R */
246 { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
250 static device_probe_t uplcom_match;
251 static device_attach_t uplcom_attach;
252 static device_detach_t uplcom_detach;
254 static device_method_t uplcom_methods[] = {
255 /* Device interface */
256 DEVMETHOD(device_probe, uplcom_match),
257 DEVMETHOD(device_attach, uplcom_attach),
258 DEVMETHOD(device_detach, uplcom_detach),
262 static driver_t uplcom_driver = {
265 sizeof (struct uplcom_softc)
268 DRIVER_MODULE(uplcom, uhub, uplcom_driver, ucom_devclass, usbd_driver_load, 0);
269 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
270 MODULE_DEPEND(uplcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
271 MODULE_VERSION(uplcom, UPLCOM_MODVER);
273 static int uplcominterval = UPLCOM_INTR_INTERVAL;
276 sysctl_hw_usb_uplcom_interval(SYSCTL_HANDLER_ARGS)
280 val = uplcominterval;
281 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
282 if (err != 0 || req->newptr == NULL)
284 if (0 < val && val <= 1000)
285 uplcominterval = val;
292 SYSCTL_PROC(_hw_usb_uplcom, OID_AUTO, interval, CTLTYPE_INT | CTLFLAG_RW,
293 0, sizeof(int), sysctl_hw_usb_uplcom_interval,
294 "I", "uplcom interrupt pipe interval");
297 uplcom_match(device_t self)
299 struct usb_attach_arg *uaa = device_get_ivars(self);
301 if (uaa->iface != NULL)
302 return (UMATCH_NONE);
304 return (usb_lookup(uplcom_devs, uaa->vendor, uaa->product) != NULL ?
305 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
309 uplcom_attach(device_t self)
311 struct uplcom_softc *sc = device_get_softc(self);
312 struct usb_attach_arg *uaa = device_get_ivars(self);
313 usbd_device_handle dev = uaa->device;
314 usb_device_descriptor_t *dd;
315 struct ucom_softc *ucom;
316 usb_config_descriptor_t *cdesc;
317 usb_interface_descriptor_t *id;
318 usb_endpoint_descriptor_t *ed;
324 devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
327 bzero(sc, sizeof (struct uplcom_softc));
329 usbd_devinfo(dev, 0, devinfo);
331 device_set_desc_copy(self, devinfo);
334 ucom->sc_iface = uaa->iface;
336 devname = device_get_nameunit(ucom->sc_dev);
337 kprintf("%s: %s\n", devname, devinfo);
339 DPRINTF(("uplcom attach: sc = %p\n", sc));
341 dd = usbd_get_device_descriptor(uaa->device);
347 * Determine chip type with algorithm sequence taken from the
348 * Linux driver as I'm not aware of any better method. Device
349 * release number in chip could be (and in fact is in many cases)
350 * replaced by the contents of external EEPROM etc.
352 else if (dd->bDeviceClass == 0x02)
353 sc->sc_chiptype = TYPE_PL2303;
354 else if (dd->bMaxPacketSize == 0x40)
355 sc->sc_chiptype = TYPE_PL2303X;
357 sc->sc_chiptype = TYPE_PL2303;
360 /* print the chip type */
361 if (sc->sc_chiptype == TYPE_PL2303X) {
362 DPRINTF(("uplcom_attach: chiptype 2303X\n"));
364 DPRINTF(("uplcom_attach: chiptype 2303\n"));
368 /* initialize endpoints */
369 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
370 sc->sc_intr_number = -1;
371 sc->sc_intr_pipe = NULL;
373 /* Move the device into the configured state. */
374 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
376 kprintf("%s: failed to set configuration: %s\n",
377 devname, usbd_errstr(err));
382 /* get the config descriptor */
383 cdesc = usbd_get_config_descriptor(ucom->sc_udev);
386 kprintf("%s: failed to get configuration descriptor\n",
387 device_get_nameunit(ucom->sc_dev));
392 /* get the (first/common) interface */
393 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
396 kprintf("%s: failed to get interface: %s\n",
397 devname, usbd_errstr(err));
402 /* Find the interrupt endpoints */
404 id = usbd_get_interface_descriptor(ucom->sc_iface);
405 sc->sc_iface_number = id->bInterfaceNumber;
407 for (i = 0; i < id->bNumEndpoints; i++) {
408 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
410 kprintf("%s: no endpoint descriptor for %d\n",
411 device_get_nameunit(ucom->sc_dev), i);
416 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
417 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
418 sc->sc_intr_number = ed->bEndpointAddress;
419 sc->sc_isize = UGETW(ed->wMaxPacketSize);
423 if (sc->sc_intr_number == -1) {
424 kprintf("%s: Could not find interrupt in\n",
425 device_get_nameunit(ucom->sc_dev));
430 /* keep interface for interrupt */
431 sc->sc_intr_iface = ucom->sc_iface;
434 * USB-RSAQ1 has two interface
436 * USB-RSAQ1 | USB-RSAQ2
437 * -----------------+-----------------
438 * Interface 0 |Interface 0
439 * Interrupt(0x81) | Interrupt(0x81)
440 * -----------------+ BulkIN(0x02)
441 * Interface 1 | BulkOUT(0x83)
445 if (cdesc->bNumInterface == 2) {
446 err = usbd_device2interface_handle(dev,
447 UPLCOM_SECOND_IFACE_INDEX,
450 kprintf("%s: failed to get second interface: %s\n",
451 devname, usbd_errstr(err));
457 /* Find the bulk{in,out} endpoints */
459 id = usbd_get_interface_descriptor(ucom->sc_iface);
460 sc->sc_iface_number = id->bInterfaceNumber;
462 for (i = 0; i < id->bNumEndpoints; i++) {
463 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
465 kprintf("%s: no endpoint descriptor for %d\n",
466 device_get_nameunit(ucom->sc_dev), i);
471 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
472 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
473 ucom->sc_bulkin_no = ed->bEndpointAddress;
474 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
475 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
476 ucom->sc_bulkout_no = ed->bEndpointAddress;
480 if (ucom->sc_bulkin_no == -1) {
481 kprintf("%s: Could not find data bulk in\n",
482 device_get_nameunit(ucom->sc_dev));
487 if (ucom->sc_bulkout_no == -1) {
488 kprintf("%s: Could not find data bulk out\n",
489 device_get_nameunit(ucom->sc_dev));
494 sc->sc_dtr = sc->sc_rts = -1;
495 ucom->sc_parent = sc;
496 ucom->sc_portno = UCOM_UNK_PORTNO;
497 /* bulkin, bulkout set above */
498 ucom->sc_ibufsize = UPLCOMIBUFSIZE;
499 ucom->sc_obufsize = UPLCOMOBUFSIZE;
500 ucom->sc_ibufsizepad = UPLCOMIBUFSIZE;
501 ucom->sc_opkthdrlen = 0;
502 ucom->sc_callback = &uplcom_callback;
504 err = uplcom_reset(sc);
507 kprintf("%s: reset failed: %s\n",
508 device_get_nameunit(ucom->sc_dev), usbd_errstr(err));
513 DPRINTF(("uplcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
514 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
516 TASK_INIT(&sc->sc_task, 0, uplcom_notify, sc);
517 ucom_attach(&sc->sc_ucom);
519 kfree(devinfo, M_USBDEV);
523 kfree(devinfo, M_USBDEV);
528 uplcom_detach(device_t self)
530 struct uplcom_softc *sc = device_get_softc(self);
533 DPRINTF(("uplcom_detach: sc = %p\n", sc));
535 if (sc->sc_intr_pipe != NULL) {
536 usbd_abort_pipe(sc->sc_intr_pipe);
537 usbd_close_pipe(sc->sc_intr_pipe);
538 kfree(sc->sc_intr_buf, M_USBDEV);
539 sc->sc_intr_pipe = NULL;
542 sc->sc_ucom.sc_dying = 1;
544 rv = ucom_detach(&sc->sc_ucom);
550 uplcom_reset(struct uplcom_softc *sc)
552 usb_device_request_t req;
555 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
556 req.bRequest = UPLCOM_SET_REQUEST;
557 USETW(req.wValue, 0);
558 USETW(req.wIndex, sc->sc_iface_number);
559 USETW(req.wLength, 0);
561 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
563 kprintf("%s: uplcom_reset: %s\n",
564 device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
571 struct pl2303x_init {
579 static const struct pl2303x_init pl2303x[] = {
580 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
581 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
582 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
583 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
584 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
585 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
586 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
587 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
588 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
589 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
590 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
592 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
595 uplcom_pl2303x_init(struct uplcom_softc *sc)
597 usb_device_request_t req;
601 for (i = 0; i < N_PL2302X_INIT; i++) {
602 req.bmRequestType = pl2303x[i].req_type;
603 req.bRequest = pl2303x[i].request;
604 USETW(req.wValue, pl2303x[i].value);
605 USETW(req.wIndex, pl2303x[i].index);
606 USETW(req.wLength, pl2303x[i].length);
608 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
610 kprintf("%s: uplcom_pl2303x_init: %d: %s\n",
611 device_get_nameunit(sc->sc_ucom.sc_dev), i,
621 uplcom_set_line_state(struct uplcom_softc *sc)
623 usb_device_request_t req;
627 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
628 (sc->sc_rts ? UCDC_LINE_RTS : 0);
629 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
630 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
631 USETW(req.wValue, ls);
632 USETW(req.wIndex, sc->sc_iface_number);
633 USETW(req.wLength, 0);
635 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
637 kprintf("%s: uplcom_set_line_status: %s\n",
638 device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
642 uplcom_set(void *addr, int portno, int reg, int onoff)
644 struct uplcom_softc *sc = addr;
648 uplcom_dtr(sc, onoff);
651 uplcom_rts(sc, onoff);
654 uplcom_break(sc, onoff);
662 uplcom_dtr(struct uplcom_softc *sc, int onoff)
664 DPRINTF(("uplcom_dtr: onoff = %d\n", onoff));
666 if (sc->sc_dtr == onoff)
670 uplcom_set_line_state(sc);
674 uplcom_rts(struct uplcom_softc *sc, int onoff)
676 DPRINTF(("uplcom_rts: onoff = %d\n", onoff));
678 if (sc->sc_rts == onoff)
682 uplcom_set_line_state(sc);
686 uplcom_break(struct uplcom_softc *sc, int onoff)
688 usb_device_request_t req;
691 DPRINTF(("uplcom_break: onoff = %d\n", onoff));
693 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
694 req.bRequest = UCDC_SEND_BREAK;
695 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
696 USETW(req.wIndex, sc->sc_iface_number);
697 USETW(req.wLength, 0);
699 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
701 kprintf("%s: uplcom_break: %s\n",
702 device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
706 uplcom_set_crtscts(struct uplcom_softc *sc)
708 usb_device_request_t req;
711 DPRINTF(("uplcom_set_crtscts: on\n"));
713 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
714 req.bRequest = UPLCOM_SET_REQUEST;
715 USETW(req.wValue, 0);
716 if (sc->sc_chiptype == TYPE_PL2303X)
717 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
719 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
720 USETW(req.wLength, 0);
722 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
724 kprintf("%s: uplcom_set_crtscts: %s\n",
725 device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
729 return (USBD_NORMAL_COMPLETION);
733 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
735 usb_device_request_t req;
739 "uplcom_set_line_coding: rate = %d, fmt = %d, parity = %d bits = %d\n",
740 UGETDW(state->dwDTERate), state->bCharFormat,
741 state->bParityType, state->bDataBits));
743 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
744 DPRINTF(("uplcom_set_line_coding: already set\n"));
745 return (USBD_NORMAL_COMPLETION);
748 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
749 req.bRequest = UCDC_SET_LINE_CODING;
750 USETW(req.wValue, 0);
751 USETW(req.wIndex, sc->sc_iface_number);
752 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
754 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, state);
756 kprintf("%s: uplcom_set_line_coding: %s\n",
757 device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
761 sc->sc_line_state = *state;
763 return (USBD_NORMAL_COMPLETION);
766 static const int uplcom_rates[] = {
767 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
768 19200, 28800, 38400, 57600, 115200,
770 * Higher speeds are probably possible. PL2303X supports up to
771 * 6Mb and can set any rate
773 230400, 460800, 614400, 921600, 1228800
775 #define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
778 uplcom_param(void *addr, int portno, struct termios *t)
780 struct uplcom_softc *sc = addr;
782 usb_cdc_line_state_t ls;
785 DPRINTF(("uplcom_param: sc = %p\n", sc));
787 /* Check requested baud rate */
788 for (i = 0; i < N_UPLCOM_RATES; i++)
789 if (uplcom_rates[i] == t->c_ospeed)
791 if (i == N_UPLCOM_RATES) {
792 DPRINTF(("uplcom_param: bad baud rate (%d)\n", t->c_ospeed));
796 USETDW(ls.dwDTERate, t->c_ospeed);
797 if (ISSET(t->c_cflag, CSTOPB))
798 ls.bCharFormat = UCDC_STOP_BIT_2;
800 ls.bCharFormat = UCDC_STOP_BIT_1;
801 if (ISSET(t->c_cflag, PARENB)) {
802 if (ISSET(t->c_cflag, PARODD))
803 ls.bParityType = UCDC_PARITY_ODD;
805 ls.bParityType = UCDC_PARITY_EVEN;
807 ls.bParityType = UCDC_PARITY_NONE;
808 switch (ISSET(t->c_cflag, CSIZE)) {
823 err = uplcom_set_line_coding(sc, &ls);
827 if (ISSET(t->c_cflag, CRTSCTS)) {
828 err = uplcom_set_crtscts(sc);
837 uplcom_open(void *addr, int portno)
839 struct uplcom_softc *sc = addr;
842 if (sc->sc_ucom.sc_dying)
845 DPRINTF(("uplcom_open: sc = %p\n", sc));
847 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
848 sc->sc_status = 0; /* clear status bit */
849 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
850 err = usbd_open_pipe_intr(sc->sc_intr_iface,
860 kprintf("%s: cannot open interrupt pipe (addr %d)\n",
861 device_get_nameunit(sc->sc_ucom.sc_dev),
867 if (sc->sc_chiptype == TYPE_PL2303X)
868 return (uplcom_pl2303x_init(sc));
874 uplcom_close(void *addr, int portno)
876 struct uplcom_softc *sc = addr;
879 if (sc->sc_ucom.sc_dying)
882 DPRINTF(("uplcom_close: close\n"));
884 if (sc->sc_intr_pipe != NULL) {
885 err = usbd_abort_pipe(sc->sc_intr_pipe);
887 kprintf("%s: abort interrupt pipe failed: %s\n",
888 device_get_nameunit(sc->sc_ucom.sc_dev),
890 err = usbd_close_pipe(sc->sc_intr_pipe);
892 kprintf("%s: close interrupt pipe failed: %s\n",
893 device_get_nameunit(sc->sc_ucom.sc_dev),
895 kfree(sc->sc_intr_buf, M_USBDEV);
896 sc->sc_intr_pipe = NULL;
901 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
903 struct uplcom_softc *sc = priv;
904 u_char *buf = sc->sc_intr_buf;
907 if (sc->sc_ucom.sc_dying)
910 if (status != USBD_NORMAL_COMPLETION) {
911 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
914 DPRINTF(("%s: uplcom_intr: abnormal status: %s\n",
915 device_get_nameunit(sc->sc_ucom.sc_dev),
916 usbd_errstr(status)));
917 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
921 DPRINTF(("%s: uplcom status = %02x\n",
922 device_get_nameunit(sc->sc_ucom.sc_dev), buf[8]));
924 sc->sc_lsr = sc->sc_msr = 0;
926 if (ISSET(pstatus, RSAQ_STATUS_CTS))
927 sc->sc_msr |= UMSR_CTS;
929 sc->sc_msr &= ~UMSR_CTS;
930 if (ISSET(pstatus, RSAQ_STATUS_DSR))
931 sc->sc_msr |= UMSR_DSR;
933 sc->sc_msr &= ~UMSR_DSR;
934 if (ISSET(pstatus, RSAQ_STATUS_DCD))
935 sc->sc_msr |= UMSR_DCD;
937 sc->sc_msr &= ~UMSR_DCD;
939 /* Deferred notifying to the ucom layer */
940 taskqueue_enqueue(taskqueue_swi, &sc->sc_task);
944 uplcom_notify(void *arg, int count)
946 struct uplcom_softc *sc;
948 sc = (struct uplcom_softc *)arg;
949 if (sc->sc_ucom.sc_dying)
951 ucom_status_change(&sc->sc_ucom);
955 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
957 struct uplcom_softc *sc = addr;
959 DPRINTF(("uplcom_get_status:\n"));
969 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
972 struct uplcom_softc *sc = addr;
975 if (sc->sc_ucom.sc_dying)
978 DPRINTF(("uplcom_ioctl: cmd = 0x%08lx\n", cmd));
984 case USB_GET_CM_OVER_DATA:
985 case USB_SET_CM_OVER_DATA:
989 DPRINTF(("uplcom_ioctl: unknown\n"));