Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.52
      1 /*	$NetBSD: uplcom.c,v 1.52 2007/01/31 10:11:50 msaitoh Exp $	*/
      2 /*
      3  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * General information: http://www.prolific.com.tw/fr_pl2303.htm
     40  * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.52 2007/01/31 10:11:50 msaitoh Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/malloc.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/conf.h>
     52 #include <sys/tty.h>
     53 #include <sys/file.h>
     54 #include <sys/select.h>
     55 #include <sys/proc.h>
     56 #include <sys/device.h>
     57 #include <sys/poll.h>
     58 
     59 #include <dev/usb/usb.h>
     60 #include <dev/usb/usbcdc.h>
     61 
     62 #include <dev/usb/usbdi.h>
     63 #include <dev/usb/usbdi_util.h>
     64 #include <dev/usb/usbdevs.h>
     65 #include <dev/usb/usb_quirks.h>
     66 
     67 #include <dev/usb/ucomvar.h>
     68 
     69 #ifdef UPLCOM_DEBUG
     70 #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
     71 int	uplcomdebug = 0;
     72 #else
     73 #define DPRINTFN(n, x)
     74 #endif
     75 #define DPRINTF(x) DPRINTFN(0, x)
     76 
     77 #define	UPLCOM_CONFIG_INDEX	0
     78 #define	UPLCOM_IFACE_INDEX	0
     79 #define	UPLCOM_SECOND_IFACE_INDEX	1
     80 
     81 #define	UPLCOM_SET_REQUEST	0x01
     82 #define	UPLCOM_SET_CRTSCTS_0	0x41
     83 #define	UPLCOM_SET_CRTSCTS_HX	0x61
     84 #define RSAQ_STATUS_DSR		0x02
     85 #define RSAQ_STATUS_DCD		0x01
     86 
     87 enum  pl2303_type {
     88 	UPLCOM_TYPE_0,
     89 	UPLCOM_TYPE_HX,
     90 };
     91 
     92 struct	uplcom_softc {
     93 	USBBASEDEVICE		sc_dev;		/* base device */
     94 	usbd_device_handle	sc_udev;	/* USB device */
     95 	usbd_interface_handle	sc_iface;	/* interface */
     96 	int			sc_iface_number;	/* interface number */
     97 
     98 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
     99 	int			sc_intr_number;	/* interrupt number */
    100 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
    101 	u_char			*sc_intr_buf;	/* interrupt buffer */
    102 	int			sc_isize;
    103 
    104 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
    105 	int			sc_dtr;		/* current DTR state */
    106 	int			sc_rts;		/* current RTS state */
    107 
    108 	device_ptr_t		sc_subdev;	/* ucom device */
    109 
    110 	u_char			sc_dying;	/* disconnecting */
    111 
    112 	u_char			sc_lsr;		/* Local status register */
    113 	u_char			sc_msr;		/* uplcom status register */
    114 
    115 	enum pl2303_type	sc_type;	/* PL2303 chip type */
    116 };
    117 
    118 /*
    119  * These are the maximum number of bytes transferred per frame.
    120  * The output buffer size cannot be increased due to the size encoding.
    121  */
    122 #define UPLCOMIBUFSIZE 256
    123 #define UPLCOMOBUFSIZE 256
    124 
    125 Static	usbd_status uplcom_reset(struct uplcom_softc *);
    126 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
    127 					   usb_cdc_line_state_t *state);
    128 Static	usbd_status uplcom_set_crtscts(struct uplcom_softc *);
    129 Static	void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    130 
    131 Static	void uplcom_set(void *, int, int, int);
    132 Static	void uplcom_dtr(struct uplcom_softc *, int);
    133 Static	void uplcom_rts(struct uplcom_softc *, int);
    134 Static	void uplcom_break(struct uplcom_softc *, int);
    135 Static	void uplcom_set_line_state(struct uplcom_softc *);
    136 Static	void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
    137 #if TODO
    138 Static	int  uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
    139 #endif
    140 Static	int  uplcom_param(void *, int, struct termios *);
    141 Static	int  uplcom_open(void *, int);
    142 Static	void uplcom_close(void *, int);
    143 Static usbd_status uplcom_vendor_control_write(usbd_device_handle, u_int16_t, u_int16_t);
    144 
    145 struct	ucom_methods uplcom_methods = {
    146 	uplcom_get_status,
    147 	uplcom_set,
    148 	uplcom_param,
    149 	NULL, /* uplcom_ioctl, TODO */
    150 	uplcom_open,
    151 	uplcom_close,
    152 	NULL,
    153 	NULL,
    154 };
    155 
    156 static const struct usb_devno uplcom_devs[] = {
    157 	/* I/O DATA USB-RSAQ2 */
    158 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
    159 	/* I/O DATA USB-RSAQ3 */
    160 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
    161 	/* I/O DATA USB-RSAQ */
    162 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
    163 	/* PLANEX USB-RS232 URS-03 */
    164 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
    165 	/* IOGEAR/ATEN UC-232A */
    166 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    167 	/* IOGEAR/ATENTRIPPLITE */
    168 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
    169 	/* ELECOM UC-SGT */
    170 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
    171 	/* ELECOM UC-SGT0 */
    172 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
    173 	/* Panasonic 50" Touch Panel */
    174 	{ USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
    175 	/* RATOC REX-USB60 */
    176 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
    177 	/* TDK USB-PHS Adapter UHA6400 */
    178 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
    179 	/* TDK USB-PDC Adapter UPA9664 */
    180 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
    181 	/* Sony Ericsson USB Cable */
    182 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
    183 	/* SOURCENEXT KeikaiDenwa 8 */
    184 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
    185 	/* SOURCENEXT KeikaiDenwa 8 with charger */
    186 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
    187 	/* HAL Corporation Crossam2+USB */
    188 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
    189 	/* Sitecom USB to serial cable */
    190 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
    191 	/* Pharos USB GPS - Microsoft version */
    192 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
    193 	/* Willcom WS002IN (DD) */
    194 	{ USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303X },
    195 };
    196 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
    197 
    198 static const struct {
    199 	uint16_t		vendor;
    200 	uint16_t		product;
    201 	enum pl2303_type	chiptype;
    202 } uplcom_devs_ext[] = {
    203 	/* I/O DATA USB-RSAQ3 */
    204 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, UPLCOM_TYPE_HX },
    205 	{ USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303X, UPLCOM_TYPE_HX },
    206 	{0, 0, 0}
    207 };
    208 
    209 
    210 USB_DECLARE_DRIVER(uplcom);
    211 
    212 USB_MATCH(uplcom)
    213 {
    214 	USB_MATCH_START(uplcom, uaa);
    215 
    216 	if (uaa->iface != NULL)
    217 		return (UMATCH_NONE);
    218 
    219 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
    220 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    221 }
    222 
    223 USB_ATTACH(uplcom)
    224 {
    225 	USB_ATTACH_START(uplcom, sc, uaa);
    226 	usbd_device_handle dev = uaa->device;
    227 	usb_device_descriptor_t *ddesc;
    228 	usb_config_descriptor_t *cdesc;
    229 	usb_interface_descriptor_t *id;
    230 	usb_endpoint_descriptor_t *ed;
    231 	char *devinfop;
    232 	char *devname = USBDEVNAME(sc->sc_dev);
    233 	usbd_status err;
    234 	int i;
    235 	struct ucom_attach_args uca;
    236 
    237 	devinfop = usbd_devinfo_alloc(dev, 0);
    238 	USB_ATTACH_SETUP;
    239 	printf("%s: %s\n", devname, devinfop);
    240 	usbd_devinfo_free(devinfop);
    241 
    242         sc->sc_udev = dev;
    243 
    244 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
    245 
    246 	/* initialize endpoints */
    247 	uca.bulkin = uca.bulkout = -1;
    248 	sc->sc_intr_number = -1;
    249 	sc->sc_intr_pipe = NULL;
    250 
    251 	/* Move the device into the configured state. */
    252 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    253 	if (err) {
    254 		printf("\n%s: failed to set configuration, err=%s\n",
    255 			devname, usbd_errstr(err));
    256 		sc->sc_dying = 1;
    257 		USB_ATTACH_ERROR_RETURN;
    258 	}
    259 
    260 	/* get the device descriptor */
    261 	ddesc = usbd_get_device_descriptor(sc->sc_udev);
    262 	if (ddesc == NULL) {
    263 		printf("%s: failed to get device descriptor\n",
    264 		    USBDEVNAME(sc->sc_dev));
    265 		sc->sc_dying = 1;
    266 		USB_ATTACH_ERROR_RETURN;
    267 	}
    268 
    269 	/* determine chip type */
    270 	for (i = 0; uplcom_devs_ext[i].vendor != 0; i++) {
    271 		if (uplcom_devs_ext[i].vendor == uaa->vendor &&
    272 		    uplcom_devs_ext[i].product == uaa->product) {
    273 			sc->sc_type = uplcom_devs_ext[i].chiptype;
    274 			goto chiptype_determined;
    275 		}
    276 	}
    277 	/*
    278 	 * NOTE: The Linux driver distinguishes between UPLCOM_TYPE_0
    279 	 * and UPLCOM_TYPE_1 type chips by testing other fields in the
    280 	 * device descriptor.  As far as the uplcom driver is
    281 	 * concerned, both types are identical.
    282 	 * The bcdDevice field should also distinguish these versions,
    283 	 * but who knows.
    284 	 */
    285 	if (UGETW(ddesc->bcdDevice) == 0x0300)
    286 		sc->sc_type = UPLCOM_TYPE_HX;
    287 	else
    288 		sc->sc_type = UPLCOM_TYPE_0;
    289 chiptype_determined:
    290 
    291 	/* get the config descriptor */
    292 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    293 
    294 	if (cdesc == NULL) {
    295 		printf("%s: failed to get configuration descriptor\n",
    296 			USBDEVNAME(sc->sc_dev));
    297 		sc->sc_dying = 1;
    298 		USB_ATTACH_ERROR_RETURN;
    299 	}
    300 
    301 	/* get the (first/common) interface */
    302 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
    303 							&sc->sc_iface);
    304 	if (err) {
    305 		printf("\n%s: failed to get interface, err=%s\n",
    306 			devname, usbd_errstr(err));
    307 		sc->sc_dying = 1;
    308 		USB_ATTACH_ERROR_RETURN;
    309 	}
    310 
    311 	/* Find the interrupt endpoints */
    312 
    313 	id = usbd_get_interface_descriptor(sc->sc_iface);
    314 	sc->sc_iface_number = id->bInterfaceNumber;
    315 
    316 	for (i = 0; i < id->bNumEndpoints; i++) {
    317 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    318 		if (ed == NULL) {
    319 			printf("%s: no endpoint descriptor for %d\n",
    320 				USBDEVNAME(sc->sc_dev), i);
    321 			sc->sc_dying = 1;
    322 			USB_ATTACH_ERROR_RETURN;
    323 		}
    324 
    325 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    326 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    327 			sc->sc_intr_number = ed->bEndpointAddress;
    328 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    329 		}
    330 	}
    331 
    332 	if (sc->sc_intr_number== -1) {
    333 		printf("%s: Could not find interrupt in\n",
    334 			USBDEVNAME(sc->sc_dev));
    335 		sc->sc_dying = 1;
    336 		USB_ATTACH_ERROR_RETURN;
    337 	}
    338 
    339 	/* keep interface for interrupt */
    340 	sc->sc_intr_iface = sc->sc_iface;
    341 
    342 	/*
    343 	 * USB-RSAQ1 has two interface
    344 	 *
    345 	 *  USB-RSAQ1       | USB-RSAQ2
    346  	 * -----------------+-----------------
    347 	 * Interface 0      |Interface 0
    348 	 *  Interrupt(0x81) | Interrupt(0x81)
    349 	 * -----------------+ BulkIN(0x02)
    350 	 * Interface 1	    | BulkOUT(0x83)
    351 	 *   BulkIN(0x02)   |
    352 	 *   BulkOUT(0x83)  |
    353 	 */
    354 	if (cdesc->bNumInterface == 2) {
    355 		err = usbd_device2interface_handle(dev,
    356 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
    357 		if (err) {
    358 			printf("\n%s: failed to get second interface, err=%s\n",
    359 							devname, usbd_errstr(err));
    360 			sc->sc_dying = 1;
    361 			USB_ATTACH_ERROR_RETURN;
    362 		}
    363 	}
    364 
    365 	/* Find the bulk{in,out} endpoints */
    366 
    367 	id = usbd_get_interface_descriptor(sc->sc_iface);
    368 	sc->sc_iface_number = id->bInterfaceNumber;
    369 
    370 	for (i = 0; i < id->bNumEndpoints; i++) {
    371 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    372 		if (ed == NULL) {
    373 			printf("%s: no endpoint descriptor for %d\n",
    374 				USBDEVNAME(sc->sc_dev), i);
    375 			sc->sc_dying = 1;
    376 			USB_ATTACH_ERROR_RETURN;
    377 		}
    378 
    379 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    380 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    381 			uca.bulkin = ed->bEndpointAddress;
    382 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    383 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    384 			uca.bulkout = ed->bEndpointAddress;
    385 		}
    386 	}
    387 
    388 	if (uca.bulkin == -1) {
    389 		printf("%s: Could not find data bulk in\n",
    390 			USBDEVNAME(sc->sc_dev));
    391 		sc->sc_dying = 1;
    392 		USB_ATTACH_ERROR_RETURN;
    393 	}
    394 
    395 	if (uca.bulkout == -1) {
    396 		printf("%s: Could not find data bulk out\n",
    397 			USBDEVNAME(sc->sc_dev));
    398 		sc->sc_dying = 1;
    399 		USB_ATTACH_ERROR_RETURN;
    400 	}
    401 
    402 	sc->sc_dtr = sc->sc_rts = -1;
    403 	uca.portno = UCOM_UNK_PORTNO;
    404 	/* bulkin, bulkout set above */
    405 	uca.ibufsize = UPLCOMIBUFSIZE;
    406 	uca.obufsize = UPLCOMOBUFSIZE;
    407 	uca.ibufsizepad = UPLCOMIBUFSIZE;
    408 	uca.opkthdrlen = 0;
    409 	uca.device = dev;
    410 	uca.iface = sc->sc_iface;
    411 	uca.methods = &uplcom_methods;
    412 	uca.arg = sc;
    413 	uca.info = NULL;
    414 
    415 	err = uplcom_reset(sc);
    416 
    417 	if (err) {
    418 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    419 			usbd_errstr(err));
    420 		sc->sc_dying = 1;
    421 		USB_ATTACH_ERROR_RETURN;
    422 	}
    423 
    424 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    425 			   USBDEV(sc->sc_dev));
    426 
    427 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
    428 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    429 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    430 					    ucomprint, ucomsubmatch);
    431 
    432 	USB_ATTACH_SUCCESS_RETURN;
    433 }
    434 
    435 USB_DETACH(uplcom)
    436 {
    437 	USB_DETACH_START(uplcom, sc);
    438 	int rv = 0;
    439 
    440 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
    441 
    442         if (sc->sc_intr_pipe != NULL) {
    443                 usbd_abort_pipe(sc->sc_intr_pipe);
    444                 usbd_close_pipe(sc->sc_intr_pipe);
    445 		free(sc->sc_intr_buf, M_USBDEV);
    446                 sc->sc_intr_pipe = NULL;
    447         }
    448 
    449 	sc->sc_dying = 1;
    450 	if (sc->sc_subdev != NULL) {
    451 		rv = config_detach(sc->sc_subdev, flags);
    452 		sc->sc_subdev = NULL;
    453 	}
    454 
    455 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    456 			   USBDEV(sc->sc_dev));
    457 
    458 	return (rv);
    459 }
    460 
    461 int
    462 uplcom_activate(device_ptr_t self, enum devact act)
    463 {
    464 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    465 	int rv = 0;
    466 
    467 	switch (act) {
    468 	case DVACT_ACTIVATE:
    469 		return (EOPNOTSUPP);
    470 
    471 	case DVACT_DEACTIVATE:
    472 		if (sc->sc_subdev != NULL)
    473 			rv = config_deactivate(sc->sc_subdev);
    474 		sc->sc_dying = 1;
    475 		break;
    476 	}
    477 	return (rv);
    478 }
    479 
    480 usbd_status
    481 uplcom_reset(struct uplcom_softc *sc)
    482 {
    483         usb_device_request_t req;
    484 	usbd_status err;
    485 
    486         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    487         req.bRequest = UPLCOM_SET_REQUEST;
    488         USETW(req.wValue, 0);
    489         USETW(req.wIndex, sc->sc_iface_number);
    490         USETW(req.wLength, 0);
    491 
    492         err = usbd_do_request(sc->sc_udev, &req, 0);
    493 	if (err)
    494 		return (EIO);
    495 
    496 	return (0);
    497 }
    498 
    499 void
    500 uplcom_set_line_state(struct uplcom_softc *sc)
    501 {
    502 	usb_device_request_t req;
    503 	int ls;
    504 
    505 	/* make sure we have initialized state for sc_dtr and sc_rts */
    506 	if (sc->sc_dtr == -1)
    507 		sc->sc_dtr = 0;
    508 	if (sc->sc_rts == -1)
    509 		sc->sc_rts = 0;
    510 
    511 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    512 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    513 
    514 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    515 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    516 	USETW(req.wValue, ls);
    517 	USETW(req.wIndex, sc->sc_iface_number);
    518 	USETW(req.wLength, 0);
    519 
    520 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    521 }
    522 
    523 void
    524 uplcom_set(void *addr, int portno, int reg, int onoff)
    525 {
    526 	struct uplcom_softc *sc = addr;
    527 
    528 	switch (reg) {
    529 	case UCOM_SET_DTR:
    530 		uplcom_dtr(sc, onoff);
    531 		break;
    532 	case UCOM_SET_RTS:
    533 		uplcom_rts(sc, onoff);
    534 		break;
    535 	case UCOM_SET_BREAK:
    536 		uplcom_break(sc, onoff);
    537 		break;
    538 	default:
    539 		break;
    540 	}
    541 }
    542 
    543 void
    544 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    545 {
    546 
    547 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
    548 
    549 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
    550 		return;
    551 
    552 	sc->sc_dtr = !!onoff;
    553 
    554 	uplcom_set_line_state(sc);
    555 }
    556 
    557 void
    558 uplcom_rts(struct uplcom_softc *sc, int onoff)
    559 {
    560 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    561 
    562 	if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
    563 		return;
    564 
    565 	sc->sc_rts = !!onoff;
    566 
    567 	uplcom_set_line_state(sc);
    568 }
    569 
    570 void
    571 uplcom_break(struct uplcom_softc *sc, int onoff)
    572 {
    573 	usb_device_request_t req;
    574 
    575 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    576 
    577 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    578 	req.bRequest = UCDC_SEND_BREAK;
    579 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    580 	USETW(req.wIndex, sc->sc_iface_number);
    581 	USETW(req.wLength, 0);
    582 
    583 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    584 }
    585 
    586 usbd_status
    587 uplcom_set_crtscts(struct uplcom_softc *sc)
    588 {
    589 	usb_device_request_t req;
    590 	usbd_status err;
    591 
    592 	DPRINTF(("uplcom_set_crtscts: on\n"));
    593 
    594 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    595 	req.bRequest = UPLCOM_SET_REQUEST;
    596 	USETW(req.wValue, 0);
    597 	if (sc->sc_type == UPLCOM_TYPE_HX)
    598 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
    599 	else
    600 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
    601 	USETW(req.wLength, 0);
    602 
    603 	err = usbd_do_request(sc->sc_udev, &req, 0);
    604 	if (err) {
    605 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
    606 			usbd_errstr(err)));
    607 		return (err);
    608 	}
    609 
    610 	return (USBD_NORMAL_COMPLETION);
    611 }
    612 
    613 usbd_status
    614 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    615 {
    616 	usb_device_request_t req;
    617 	usbd_status err;
    618 
    619 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    620 		UGETDW(state->dwDTERate), state->bCharFormat,
    621 		state->bParityType, state->bDataBits));
    622 
    623 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    624 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    625 		return (USBD_NORMAL_COMPLETION);
    626 	}
    627 
    628 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    629 	req.bRequest = UCDC_SET_LINE_CODING;
    630 	USETW(req.wValue, 0);
    631 	USETW(req.wIndex, sc->sc_iface_number);
    632 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    633 
    634 	err = usbd_do_request(sc->sc_udev, &req, state);
    635 	if (err) {
    636 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    637 			usbd_errstr(err)));
    638 		return (err);
    639 	}
    640 
    641 	sc->sc_line_state = *state;
    642 
    643 	return (USBD_NORMAL_COMPLETION);
    644 }
    645 
    646 int
    647 uplcom_param(void *addr, int portno, struct termios *t)
    648 {
    649 	struct uplcom_softc *sc = addr;
    650 	usbd_status err;
    651 	usb_cdc_line_state_t ls;
    652 
    653 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    654 
    655 	USETDW(ls.dwDTERate, t->c_ospeed);
    656 	if (ISSET(t->c_cflag, CSTOPB))
    657 		ls.bCharFormat = UCDC_STOP_BIT_2;
    658 	else
    659 		ls.bCharFormat = UCDC_STOP_BIT_1;
    660 	if (ISSET(t->c_cflag, PARENB)) {
    661 		if (ISSET(t->c_cflag, PARODD))
    662 			ls.bParityType = UCDC_PARITY_ODD;
    663 		else
    664 			ls.bParityType = UCDC_PARITY_EVEN;
    665 	} else
    666 		ls.bParityType = UCDC_PARITY_NONE;
    667 	switch (ISSET(t->c_cflag, CSIZE)) {
    668 	case CS5:
    669 		ls.bDataBits = 5;
    670 		break;
    671 	case CS6:
    672 		ls.bDataBits = 6;
    673 		break;
    674 	case CS7:
    675 		ls.bDataBits = 7;
    676 		break;
    677 	case CS8:
    678 		ls.bDataBits = 8;
    679 		break;
    680 	}
    681 
    682 	err = uplcom_set_line_coding(sc, &ls);
    683 	if (err) {
    684 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    685 		return (EIO);
    686 	}
    687 
    688 	if (ISSET(t->c_cflag, CRTSCTS))
    689 		uplcom_set_crtscts(sc);
    690 
    691 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
    692 		uplcom_set_line_state(sc);
    693 
    694 	if (err) {
    695 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    696 		return (EIO);
    697 	}
    698 
    699 	return (0);
    700 }
    701 
    702 Static usbd_status
    703 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
    704 {
    705 	usb_device_request_t req;
    706 	usbd_status err;
    707 
    708 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    709 	req.bRequest = UPLCOM_SET_REQUEST;
    710 	USETW(req.wValue, value);
    711 	USETW(req.wIndex, index);
    712 	USETW(req.wLength, 0);
    713 
    714 	err = usbd_do_request(dev, &req, NULL);
    715 
    716 	if (err) {
    717 		DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
    718 			    usbd_errstr(err), err));
    719 	}
    720 
    721 	return err;
    722 }
    723 
    724 int
    725 uplcom_open(void *addr, int portno)
    726 {
    727 	struct uplcom_softc *sc = addr;
    728 	usbd_status err;
    729 
    730 	if (sc->sc_dying)
    731 		return (EIO);
    732 
    733 	DPRINTF(("uplcom_open: sc=%p\n", sc));
    734 
    735 	/* Some unknown device frobbing. */
    736 	if (sc->sc_type == UPLCOM_TYPE_HX)
    737 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
    738 	else
    739 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
    740 
    741 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    742 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    743 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
    744 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    745 			sc->sc_intr_buf, sc->sc_isize,
    746 			uplcom_intr, USBD_DEFAULT_INTERVAL);
    747 		if (err) {
    748 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    749 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
    750 					return (EIO);
    751 		}
    752 	}
    753 
    754 	return (0);
    755 }
    756 
    757 void
    758 uplcom_close(void *addr, int portno)
    759 {
    760 	struct uplcom_softc *sc = addr;
    761 	int err;
    762 
    763 	if (sc->sc_dying)
    764 		return;
    765 
    766 	DPRINTF(("uplcom_close: close\n"));
    767 
    768 	if (sc->sc_intr_pipe != NULL) {
    769 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    770 		if (err)
    771 			printf("%s: abort interrupt pipe failed: %s\n",
    772 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    773 		err = usbd_close_pipe(sc->sc_intr_pipe);
    774 		if (err)
    775 			printf("%s: close interrupt pipe failed: %s\n",
    776 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    777 		free(sc->sc_intr_buf, M_USBDEV);
    778 		sc->sc_intr_pipe = NULL;
    779 	}
    780 }
    781 
    782 void
    783 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
    784     usbd_status status)
    785 {
    786 	struct uplcom_softc *sc = priv;
    787 	u_char *buf = sc->sc_intr_buf;
    788 	u_char pstatus;
    789 
    790 	if (sc->sc_dying)
    791 		return;
    792 
    793 	if (status != USBD_NORMAL_COMPLETION) {
    794 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    795 			return;
    796 
    797 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    798 			usbd_errstr(status)));
    799 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    800 		return;
    801 	}
    802 
    803 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
    804 
    805 	sc->sc_lsr = sc->sc_msr = 0;
    806 	pstatus = buf[8];
    807 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
    808 		sc->sc_msr |= UMSR_DSR;
    809 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
    810 		sc->sc_msr |= UMSR_DCD;
    811 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
    812 }
    813 
    814 void
    815 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    816 {
    817 	struct uplcom_softc *sc = addr;
    818 
    819 	DPRINTF(("uplcom_get_status:\n"));
    820 
    821 	if (lsr != NULL)
    822 		*lsr = sc->sc_lsr;
    823 	if (msr != NULL)
    824 		*msr = sc->sc_msr;
    825 }
    826 
    827 #if TODO
    828 int
    829 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
    830 	     usb_proc_ptr p)
    831 {
    832 	struct uplcom_softc *sc = addr;
    833 	int error = 0;
    834 
    835 	if (sc->sc_dying)
    836 		return (EIO);
    837 
    838 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
    839 
    840 	switch (cmd) {
    841 	case TIOCNOTTY:
    842 	case TIOCMGET:
    843 	case TIOCMSET:
    844 	case USB_GET_CM_OVER_DATA:
    845 	case USB_SET_CM_OVER_DATA:
    846 		break;
    847 
    848 	default:
    849 		DPRINTF(("uplcom_ioctl: unknown\n"));
    850 		error = ENOTTY;
    851 		break;
    852 	}
    853 
    854 	return (error);
    855 }
    856 #endif
    857