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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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 USB-to-RS232 serial adapters driven by
70 * Prolific PL-2303, PL-2303X and probably PL-2303HX USB-to-RS232
71 * bridge chip. The adapters are sold under many different brand
74 * Datasheets are available at Prolific www site at
75 * http://www.prolific.com.tw. The datasheets don't contain full
76 * programming information for the chip.
78 * PL-2303HX is probably programmed the same as PL-2303X.
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 "opt_uplcom.h"
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/kernel.h>
92 #include <sys/module.h>
93 #include <sys/malloc.h>
95 #include <sys/ioccom.h>
96 #include <sys/fcntl.h>
98 #include <sys/serial.h>
100 #include <sys/file.h>
101 #if __FreeBSD_version >= 500014
102 #include <sys/selinfo.h>
104 #include <sys/select.h>
106 #include <sys/proc.h>
107 #include <sys/poll.h>
108 #include <sys/sysctl.h>
110 #include <sys/taskqueue.h>
112 #include <machine/bus.h>
114 #include <dev/usb/usb.h>
115 #include <dev/usb/usbcdc.h>
117 #include <dev/usb/usbdi.h>
118 #include <dev/usb/usbdivar.h>
119 #include <dev/usb/usbdi_util.h>
121 #include <dev/usb/usb_quirks.h>
123 #include <dev/usb/ucomvar.h>
125 SYSCTL_NODE(_hw_usb, OID_AUTO, uplcom, CTLFLAG_RW, 0, "USB uplcom");
127 static int uplcomdebug = 0;
128 SYSCTL_INT(_hw_usb_uplcom, OID_AUTO, debug, CTLFLAG_RW,
129 &uplcomdebug, 0, "uplcom debug level");
131 #define DPRINTFN(n, x) do { \
132 if (uplcomdebug > (n)) \
136 #define DPRINTFN(n, x)
138 #define DPRINTF(x) DPRINTFN(0, x)
140 #define UPLCOM_MODVER 1 /* module version */
142 #define UPLCOM_CONFIG_INDEX 0
143 #define UPLCOM_IFACE_INDEX 0
144 #define UPLCOM_SECOND_IFACE_INDEX 1
146 #ifndef UPLCOM_INTR_INTERVAL
147 #define UPLCOM_INTR_INTERVAL 100 /* ms */
150 #define UPLCOM_SET_REQUEST 0x01
151 #define UPLCOM_SET_CRTSCTS 0x41
152 #define UPLCOM_SET_CRTSCTS_PL2303X 0x61
153 #define RSAQ_STATUS_CTS 0x80
154 #define RSAQ_STATUS_DSR 0x02
155 #define RSAQ_STATUS_DCD 0x01
157 #define TYPE_PL2303 0
158 #define TYPE_PL2303X 1
160 struct uplcom_softc {
161 struct ucom_softc sc_ucom;
163 int sc_iface_number; /* interface number */
165 usbd_interface_handle sc_intr_iface; /* interrupt interface */
166 int sc_intr_number; /* interrupt number */
167 usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
168 u_char *sc_intr_buf; /* interrupt buffer */
171 usb_cdc_line_state_t sc_line_state; /* current line state */
172 u_char sc_dtr; /* current DTR state */
173 u_char sc_rts; /* current RTS state */
176 u_char sc_lsr; /* Local status register */
177 u_char sc_msr; /* uplcom status register */
179 int sc_chiptype; /* Type of chip */
185 * These are the maximum number of bytes transferred per frame.
186 * The output buffer size cannot be increased due to the size encoding.
188 #define UPLCOMIBUFSIZE 256
189 #define UPLCOMOBUFSIZE 256
191 Static usbd_status uplcom_reset(struct uplcom_softc *);
192 Static usbd_status uplcom_set_line_coding(struct uplcom_softc *,
193 usb_cdc_line_state_t *);
194 Static usbd_status uplcom_set_crtscts(struct uplcom_softc *);
195 Static void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
197 Static void uplcom_set(void *, int, int, int);
198 Static void uplcom_dtr(struct uplcom_softc *, int);
199 Static void uplcom_rts(struct uplcom_softc *, int);
200 Static void uplcom_break(struct uplcom_softc *, int);
201 Static void uplcom_set_line_state(struct uplcom_softc *);
202 Static void uplcom_get_status(void *, int, u_char *, u_char *);
204 Static int uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr);
206 Static int uplcom_param(void *, int, struct termios *);
207 Static int uplcom_open(void *, int);
208 Static void uplcom_close(void *, int);
209 Static void uplcom_notify(void *, int);
211 struct ucom_callback uplcom_callback = {
215 NULL, /* uplcom_ioctl, TODO */
222 static const struct uplcom_product {
225 int32_t release; /* release is a 16bit entity,
226 * if -1 is specified we "don't care"
229 } uplcom_products [] = {
230 /* I/O DATA USB-RSAQ */
231 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ, -1, TYPE_PL2303 },
232 /* I/O DATA USB-RSAQ2 */
233 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2, -1, TYPE_PL2303 },
234 /* PLANEX USB-RS232 URS-03 */
235 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A, -1, TYPE_PL2303 },
236 /* ST Lab USB-SERIAL-4 */
237 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303,
238 0x300, TYPE_PL2303X },
239 /* IOGEAR/ATEN UC-232A (also ST Lab USB-SERIAL-1) */
240 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303, -1, TYPE_PL2303 },
241 /* TDK USB-PHS Adapter UHA6400 */
242 { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400, -1, TYPE_PL2303 },
243 /* RATOC REX-USB60 */
244 { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60, -1, TYPE_PL2303 },
246 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT, -1, TYPE_PL2303 },
247 { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0, -1, TYPE_PL2303 },
248 /* Sony Ericsson USB Cable */
249 { USB_VENDOR_SONYERICSSON, USB_PRODUCT_SONYERICSSON_DCU10,
251 /* SOURCENEXT KeikaiDenwa 8 */
252 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8,
254 /* SOURCENEXT KeikaiDenwa 8 with charger */
255 { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG,
257 /* HAL Corporation Crossam2+USB */
258 { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001, -1, TYPE_PL2303 },
259 /* Sitecom USB to Serial */
260 { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_SERIAL, -1, TYPE_PL2303 },
264 Static device_probe_t uplcom_match;
265 Static device_attach_t uplcom_attach;
266 Static device_detach_t uplcom_detach;
268 Static device_method_t uplcom_methods[] = {
269 /* Device interface */
270 DEVMETHOD(device_probe, uplcom_match),
271 DEVMETHOD(device_attach, uplcom_attach),
272 DEVMETHOD(device_detach, uplcom_detach),
276 Static driver_t uplcom_driver = {
279 sizeof (struct uplcom_softc)
282 DRIVER_MODULE(uplcom, uhub, uplcom_driver, ucom_devclass, usbd_driver_load, 0);
283 MODULE_DEPEND(uplcom, usb, 1, 1, 1);
284 MODULE_DEPEND(uplcom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
285 MODULE_VERSION(uplcom, UPLCOM_MODVER);
287 static int uplcominterval = UPLCOM_INTR_INTERVAL;
290 sysctl_hw_usb_uplcom_interval(SYSCTL_HANDLER_ARGS)
294 val = uplcominterval;
295 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
296 if (err != 0 || req->newptr == NULL)
298 if (0 < val && val <= 1000)
299 uplcominterval = val;
306 SYSCTL_PROC(_hw_usb_uplcom, OID_AUTO, interval, CTLTYPE_INT | CTLFLAG_RW,
307 0, sizeof(int), sysctl_hw_usb_uplcom_interval,
308 "I", "uplcom interrupt pipe interval");
312 USB_MATCH_START(uplcom, uaa);
315 if (uaa->iface != NULL)
316 return (UMATCH_NONE);
318 for (i = 0; uplcom_products[i].vendor != 0; i++) {
319 if (uplcom_products[i].vendor == uaa->vendor &&
320 uplcom_products[i].product == uaa->product &&
321 (uplcom_products[i].release == uaa->release ||
322 uplcom_products[i].release == -1)) {
323 return (UMATCH_VENDOR_PRODUCT);
326 return (UMATCH_NONE);
331 USB_ATTACH_START(uplcom, sc, uaa);
332 usbd_device_handle dev = uaa->device;
333 struct ucom_softc *ucom;
334 usb_config_descriptor_t *cdesc;
335 usb_interface_descriptor_t *id;
336 usb_endpoint_descriptor_t *ed;
342 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
345 bzero(sc, sizeof (struct uplcom_softc));
347 usbd_devinfo(dev, 0, devinfo);
348 /* USB_ATTACH_SETUP; */
350 device_set_desc_copy(self, devinfo);
351 /* USB_ATTACH_SETUP; */
354 ucom->sc_iface = uaa->iface;
356 devname = USBDEVNAME(ucom->sc_dev);
357 printf("%s: %s\n", devname, devinfo);
359 DPRINTF(("uplcom attach: sc = %p\n", sc));
361 /* determine chip type */
362 for (i = 0; uplcom_products[i].vendor != 0; i++) {
363 if (uplcom_products[i].vendor == uaa->vendor &&
364 uplcom_products[i].product == uaa->product &&
365 (uplcom_products[i].release == uaa->release ||
366 uplcom_products[i].release == -1)) {
367 sc->sc_chiptype = uplcom_products[i].chiptype;
373 * check we found the device - attach should have ensured we
374 * don't get here without matching device
376 if (uplcom_products[i].vendor == 0) {
377 printf("%s: didn't match\n", devname);
383 /* print the chip type */
384 if (sc->sc_chiptype == TYPE_PL2303X) {
385 DPRINTF(("uplcom_attach: chiptype 2303X\n"));
387 DPRINTF(("uplcom_attach: chiptype 2303\n"));
391 /* initialize endpoints */
392 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
393 sc->sc_intr_number = -1;
394 sc->sc_intr_pipe = NULL;
396 /* Move the device into the configured state. */
397 err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
399 printf("%s: failed to set configuration: %s\n",
400 devname, usbd_errstr(err));
405 /* get the config descriptor */
406 cdesc = usbd_get_config_descriptor(ucom->sc_udev);
409 printf("%s: failed to get configuration descriptor\n",
410 USBDEVNAME(ucom->sc_dev));
415 /* get the (first/common) interface */
416 err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
419 printf("%s: failed to get interface: %s\n",
420 devname, usbd_errstr(err));
425 /* Find the interrupt endpoints */
427 id = usbd_get_interface_descriptor(ucom->sc_iface);
428 sc->sc_iface_number = id->bInterfaceNumber;
430 for (i = 0; i < id->bNumEndpoints; i++) {
431 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
433 printf("%s: no endpoint descriptor for %d\n",
434 USBDEVNAME(ucom->sc_dev), i);
439 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
440 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
441 sc->sc_intr_number = ed->bEndpointAddress;
442 sc->sc_isize = UGETW(ed->wMaxPacketSize);
446 if (sc->sc_intr_number == -1) {
447 printf("%s: Could not find interrupt in\n",
448 USBDEVNAME(ucom->sc_dev));
453 /* keep interface for interrupt */
454 sc->sc_intr_iface = ucom->sc_iface;
457 * USB-RSAQ1 has two interface
459 * USB-RSAQ1 | USB-RSAQ2
460 * -----------------+-----------------
461 * Interface 0 |Interface 0
462 * Interrupt(0x81) | Interrupt(0x81)
463 * -----------------+ BulkIN(0x02)
464 * Interface 1 | BulkOUT(0x83)
468 if (cdesc->bNumInterface == 2) {
469 err = usbd_device2interface_handle(dev,
470 UPLCOM_SECOND_IFACE_INDEX,
473 printf("%s: failed to get second interface: %s\n",
474 devname, usbd_errstr(err));
480 /* Find the bulk{in,out} endpoints */
482 id = usbd_get_interface_descriptor(ucom->sc_iface);
483 sc->sc_iface_number = id->bInterfaceNumber;
485 for (i = 0; i < id->bNumEndpoints; i++) {
486 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
488 printf("%s: no endpoint descriptor for %d\n",
489 USBDEVNAME(ucom->sc_dev), i);
494 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
495 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
496 ucom->sc_bulkin_no = ed->bEndpointAddress;
497 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
498 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
499 ucom->sc_bulkout_no = ed->bEndpointAddress;
503 if (ucom->sc_bulkin_no == -1) {
504 printf("%s: Could not find data bulk in\n",
505 USBDEVNAME(ucom->sc_dev));
510 if (ucom->sc_bulkout_no == -1) {
511 printf("%s: Could not find data bulk out\n",
512 USBDEVNAME(ucom->sc_dev));
517 sc->sc_dtr = sc->sc_rts = -1;
518 ucom->sc_parent = sc;
519 ucom->sc_portno = UCOM_UNK_PORTNO;
520 /* bulkin, bulkout set above */
521 ucom->sc_ibufsize = UPLCOMIBUFSIZE;
522 ucom->sc_obufsize = UPLCOMOBUFSIZE;
523 ucom->sc_ibufsizepad = UPLCOMIBUFSIZE;
524 ucom->sc_opkthdrlen = 0;
525 ucom->sc_callback = &uplcom_callback;
527 err = uplcom_reset(sc);
530 printf("%s: reset failed: %s\n",
531 USBDEVNAME(ucom->sc_dev), usbd_errstr(err));
536 DPRINTF(("uplcom: in = 0x%x, out = 0x%x, intr = 0x%x\n",
537 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
539 TASK_INIT(&sc->sc_task, 0, uplcom_notify, sc);
540 ucom_attach(&sc->sc_ucom);
542 free(devinfo, M_USBDEV);
543 USB_ATTACH_SUCCESS_RETURN;
546 free(devinfo, M_USBDEV);
547 USB_ATTACH_ERROR_RETURN;
552 USB_DETACH_START(uplcom, sc);
555 DPRINTF(("uplcom_detach: sc = %p\n", sc));
557 if (sc->sc_intr_pipe != NULL) {
558 usbd_abort_pipe(sc->sc_intr_pipe);
559 usbd_close_pipe(sc->sc_intr_pipe);
560 free(sc->sc_intr_buf, M_USBDEV);
561 sc->sc_intr_pipe = NULL;
564 sc->sc_ucom.sc_dying = 1;
566 rv = ucom_detach(&sc->sc_ucom);
572 uplcom_reset(struct uplcom_softc *sc)
574 usb_device_request_t req;
577 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
578 req.bRequest = UPLCOM_SET_REQUEST;
579 USETW(req.wValue, 0);
580 USETW(req.wIndex, sc->sc_iface_number);
581 USETW(req.wLength, 0);
583 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
585 printf("%s: uplcom_reset: %s\n",
586 USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
593 struct pl2303x_init {
601 Static const struct pl2303x_init pl2303x[] = {
602 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
603 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 0, 0 },
604 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
605 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
606 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
607 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404, 1, 0 },
608 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8484, 0, 0 },
609 { UT_READ_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x8383, 0, 0 },
610 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0, 1, 0 },
611 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 1, 0, 0 },
612 { UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 2, 0x44, 0 }
614 #define N_PL2302X_INIT (sizeof(pl2303x)/sizeof(pl2303x[0]))
617 uplcom_pl2303x_init(struct uplcom_softc *sc)
619 usb_device_request_t req;
623 for (i = 0; i < N_PL2302X_INIT; i++) {
624 req.bmRequestType = pl2303x[i].req_type;
625 req.bRequest = pl2303x[i].request;
626 USETW(req.wValue, pl2303x[i].value);
627 USETW(req.wIndex, pl2303x[i].index);
628 USETW(req.wLength, pl2303x[i].length);
630 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
632 printf("%s: uplcom_pl2303x_init: %d: %s\n",
633 USBDEVNAME(sc->sc_ucom.sc_dev), i,
643 uplcom_set_line_state(struct uplcom_softc *sc)
645 usb_device_request_t req;
649 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
650 (sc->sc_rts ? UCDC_LINE_RTS : 0);
651 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
652 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
653 USETW(req.wValue, ls);
654 USETW(req.wIndex, sc->sc_iface_number);
655 USETW(req.wLength, 0);
657 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
659 printf("%s: uplcom_set_line_status: %s\n",
660 USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
664 uplcom_set(void *addr, int portno, int reg, int onoff)
666 struct uplcom_softc *sc = addr;
670 uplcom_dtr(sc, onoff);
673 uplcom_rts(sc, onoff);
676 uplcom_break(sc, onoff);
684 uplcom_dtr(struct uplcom_softc *sc, int onoff)
686 DPRINTF(("uplcom_dtr: onoff = %d\n", onoff));
688 if (sc->sc_dtr == onoff)
692 uplcom_set_line_state(sc);
696 uplcom_rts(struct uplcom_softc *sc, int onoff)
698 DPRINTF(("uplcom_rts: onoff = %d\n", onoff));
700 if (sc->sc_rts == onoff)
704 uplcom_set_line_state(sc);
708 uplcom_break(struct uplcom_softc *sc, int onoff)
710 usb_device_request_t req;
713 DPRINTF(("uplcom_break: onoff = %d\n", onoff));
715 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
716 req.bRequest = UCDC_SEND_BREAK;
717 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
718 USETW(req.wIndex, sc->sc_iface_number);
719 USETW(req.wLength, 0);
721 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
723 printf("%s: uplcom_break: %s\n",
724 USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
728 uplcom_set_crtscts(struct uplcom_softc *sc)
730 usb_device_request_t req;
733 DPRINTF(("uplcom_set_crtscts: on\n"));
735 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
736 req.bRequest = UPLCOM_SET_REQUEST;
737 USETW(req.wValue, 0);
738 if (sc->sc_chiptype == TYPE_PL2303X)
739 USETW(req.wIndex, UPLCOM_SET_CRTSCTS_PL2303X);
741 USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
742 USETW(req.wLength, 0);
744 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
746 printf("%s: uplcom_set_crtscts: %s\n",
747 USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
751 return (USBD_NORMAL_COMPLETION);
755 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
757 usb_device_request_t req;
761 "uplcom_set_line_coding: rate = %d, fmt = %d, parity = %d bits = %d\n",
762 UGETDW(state->dwDTERate), state->bCharFormat,
763 state->bParityType, state->bDataBits));
765 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
766 DPRINTF(("uplcom_set_line_coding: already set\n"));
767 return (USBD_NORMAL_COMPLETION);
770 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
771 req.bRequest = UCDC_SET_LINE_CODING;
772 USETW(req.wValue, 0);
773 USETW(req.wIndex, sc->sc_iface_number);
774 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
776 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, state);
778 printf("%s: uplcom_set_line_coding: %s\n",
779 USBDEVNAME(sc->sc_ucom.sc_dev), usbd_errstr(err));
783 sc->sc_line_state = *state;
785 return (USBD_NORMAL_COMPLETION);
788 Static const int uplcom_rates[] = {
789 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400,
790 19200, 28800, 38400, 57600, 115200,
792 * Higher speeds are probably possible. PL2303X supports up to
793 * 6Mb and can set any rate
795 230400, 460800, 614400, 921600, 1228800
797 #define N_UPLCOM_RATES (sizeof(uplcom_rates)/sizeof(uplcom_rates[0]))
800 uplcom_param(void *addr, int portno, struct termios *t)
802 struct uplcom_softc *sc = addr;
804 usb_cdc_line_state_t ls;
807 DPRINTF(("uplcom_param: sc = %p\n", sc));
809 /* Check requested baud rate */
810 for (i = 0; i < N_UPLCOM_RATES; i++)
811 if (uplcom_rates[i] == t->c_ospeed)
813 if (i == N_UPLCOM_RATES) {
814 DPRINTF(("uplcom_param: bad baud rate (%d)\n", t->c_ospeed));
818 USETDW(ls.dwDTERate, t->c_ospeed);
819 if (ISSET(t->c_cflag, CSTOPB))
820 ls.bCharFormat = UCDC_STOP_BIT_2;
822 ls.bCharFormat = UCDC_STOP_BIT_1;
823 if (ISSET(t->c_cflag, PARENB)) {
824 if (ISSET(t->c_cflag, PARODD))
825 ls.bParityType = UCDC_PARITY_ODD;
827 ls.bParityType = UCDC_PARITY_EVEN;
829 ls.bParityType = UCDC_PARITY_NONE;
830 switch (ISSET(t->c_cflag, CSIZE)) {
845 err = uplcom_set_line_coding(sc, &ls);
849 if (ISSET(t->c_cflag, CRTSCTS)) {
850 err = uplcom_set_crtscts(sc);
859 uplcom_open(void *addr, int portno)
861 struct uplcom_softc *sc = addr;
864 if (sc->sc_ucom.sc_dying)
867 DPRINTF(("uplcom_open: sc = %p\n", sc));
869 if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
870 sc->sc_status = 0; /* clear status bit */
871 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
872 err = usbd_open_pipe_intr(sc->sc_intr_iface,
882 printf("%s: cannot open interrupt pipe (addr %d)\n",
883 USBDEVNAME(sc->sc_ucom.sc_dev),
889 if (sc->sc_chiptype == TYPE_PL2303X)
890 return (uplcom_pl2303x_init(sc));
896 uplcom_close(void *addr, int portno)
898 struct uplcom_softc *sc = addr;
901 if (sc->sc_ucom.sc_dying)
904 DPRINTF(("uplcom_close: close\n"));
906 if (sc->sc_intr_pipe != NULL) {
907 err = usbd_abort_pipe(sc->sc_intr_pipe);
909 printf("%s: abort interrupt pipe failed: %s\n",
910 USBDEVNAME(sc->sc_ucom.sc_dev),
912 err = usbd_close_pipe(sc->sc_intr_pipe);
914 printf("%s: close interrupt pipe failed: %s\n",
915 USBDEVNAME(sc->sc_ucom.sc_dev),
917 free(sc->sc_intr_buf, M_USBDEV);
918 sc->sc_intr_pipe = NULL;
923 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
925 struct uplcom_softc *sc = priv;
926 u_char *buf = sc->sc_intr_buf;
929 if (sc->sc_ucom.sc_dying)
932 if (status != USBD_NORMAL_COMPLETION) {
933 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
936 DPRINTF(("%s: uplcom_intr: abnormal status: %s\n",
937 USBDEVNAME(sc->sc_ucom.sc_dev),
938 usbd_errstr(status)));
939 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
943 DPRINTF(("%s: uplcom status = %02x\n",
944 USBDEVNAME(sc->sc_ucom.sc_dev), buf[8]));
946 sc->sc_lsr = sc->sc_msr = 0;
948 if (ISSET(pstatus, RSAQ_STATUS_CTS))
949 sc->sc_msr |= SER_CTS;
951 sc->sc_msr &= ~SER_CTS;
952 if (ISSET(pstatus, RSAQ_STATUS_DSR))
953 sc->sc_msr |= SER_DSR;
955 sc->sc_msr &= ~SER_DSR;
956 if (ISSET(pstatus, RSAQ_STATUS_DCD))
957 sc->sc_msr |= SER_DCD;
959 sc->sc_msr &= ~SER_DCD;
961 /* Deferred notifying to the ucom layer */
962 taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task);
966 uplcom_notify(void *arg, int count)
968 struct uplcom_softc *sc;
970 sc = (struct uplcom_softc *)arg;
971 if (sc->sc_ucom.sc_dying)
973 ucom_status_change(&sc->sc_ucom);
977 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
979 struct uplcom_softc *sc = addr;
981 DPRINTF(("uplcom_get_status:\n"));
991 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
994 struct uplcom_softc *sc = addr;
997 if (sc->sc_ucom.sc_dying)
1000 DPRINTF(("uplcom_ioctl: cmd = 0x%08lx\n", cmd));
1006 case USB_GET_CM_OVER_DATA:
1007 case USB_SET_CM_OVER_DATA:
1011 DPRINTF(("uplcom_ioctl: unknown\n"));