Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.58
      1 /*	$NetBSD: uplcom.c,v 1.58 2007/08/18 20:23:04 sborrill 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.58 2007/08/18 20:23:04 sborrill 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, void *, 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 	/* I/O DATA USB-RSAQ5 */
    164 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
    165 	/* PLANEX USB-RS232 URS-03 */
    166 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
    167 	/* IOGEAR/ATEN UC-232A */
    168 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    169 	/* SMART Technologies USB to serial */
    170 	{ USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
    171 	/* IOGEAR/ATENTRIPPLITE */
    172 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
    173 	/* ELECOM UC-SGT */
    174 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
    175 	/* ELECOM UC-SGT0 */
    176 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
    177 	/* Panasonic 50" Touch Panel */
    178 	{ USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
    179 	/* RATOC REX-USB60 */
    180 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
    181 	/* TDK USB-PHS Adapter UHA6400 */
    182 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
    183 	/* TDK USB-PDC Adapter UPA9664 */
    184 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
    185 	/* Sony Ericsson USB Cable */
    186 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
    187 	/* SOURCENEXT KeikaiDenwa 8 */
    188 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
    189 	/* SOURCENEXT KeikaiDenwa 8 with charger */
    190 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
    191 	/* HAL Corporation Crossam2+USB */
    192 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
    193 	/* Sitecom USB to serial cable */
    194 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
    195 	/* Pharos USB GPS - Microsoft version */
    196 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
    197 	/* Willcom WS002IN (DD) */
    198 	{ USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
    199 };
    200 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
    201 
    202 static const struct {
    203 	uint16_t		vendor;
    204 	uint16_t		product;
    205 	enum pl2303_type	chiptype;
    206 } uplcom_devs_ext[] = {
    207 	/* I/O DATA USB-RSAQ3 */
    208 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3, UPLCOM_TYPE_HX },
    209 	{ USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN, UPLCOM_TYPE_HX },
    210 	/* I/O DATA USB-RSAQ5 */
    211 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5, UPLCOM_TYPE_HX },
    212 	{0, 0, 0}
    213 };
    214 
    215 
    216 USB_DECLARE_DRIVER(uplcom);
    217 
    218 USB_MATCH(uplcom)
    219 {
    220 	USB_MATCH_START(uplcom, uaa);
    221 
    222 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
    223 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    224 }
    225 
    226 USB_ATTACH(uplcom)
    227 {
    228 	USB_ATTACH_START(uplcom, sc, uaa);
    229 	usbd_device_handle dev = uaa->device;
    230 	usb_config_descriptor_t *cdesc;
    231 	usb_interface_descriptor_t *id;
    232 	usb_endpoint_descriptor_t *ed;
    233 	char *devinfop;
    234 	char *devname = USBDEVNAME(sc->sc_dev);
    235 	usbd_status err;
    236 	int i;
    237 	struct ucom_attach_args uca;
    238 
    239 	devinfop = usbd_devinfo_alloc(dev, 0);
    240 	USB_ATTACH_SETUP;
    241 	printf("%s: %s\n", devname, devinfop);
    242 	usbd_devinfo_free(devinfop);
    243 
    244         sc->sc_udev = dev;
    245 
    246 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
    247 
    248 	/* initialize endpoints */
    249 	uca.bulkin = uca.bulkout = -1;
    250 	sc->sc_intr_number = -1;
    251 	sc->sc_intr_pipe = NULL;
    252 
    253 	/* Move the device into the configured state. */
    254 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    255 	if (err) {
    256 		printf("\n%s: failed to set configuration, err=%s\n",
    257 			devname, usbd_errstr(err));
    258 		sc->sc_dying = 1;
    259 		USB_ATTACH_ERROR_RETURN;
    260 	}
    261 
    262 	/* determine chip type */
    263 	for (i = 0; uplcom_devs_ext[i].vendor != 0; i++) {
    264 		if (uplcom_devs_ext[i].vendor == uaa->vendor &&
    265 		    uplcom_devs_ext[i].product == uaa->product) {
    266 			sc->sc_type = uplcom_devs_ext[i].chiptype;
    267 			goto chiptype_determined;
    268 		}
    269 	}
    270 	/*
    271 	 * NOTE: The Linux driver distinguishes between UPLCOM_TYPE_0
    272 	 * and UPLCOM_TYPE_1 type chips by testing other fields in the
    273 	 * device descriptor.  As far as the uplcom driver is
    274 	 * concerned, both types are identical.
    275 	 * The bcdDevice field should also distinguish these versions,
    276 	 * but who knows.
    277 	 */
    278 	if (uaa->release == 0x0300)
    279 		sc->sc_type = UPLCOM_TYPE_HX;
    280 	else
    281 		sc->sc_type = UPLCOM_TYPE_0;
    282 chiptype_determined:
    283 
    284 	/* get the config descriptor */
    285 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    286 
    287 	if (cdesc == NULL) {
    288 		printf("%s: failed to get configuration descriptor\n",
    289 			USBDEVNAME(sc->sc_dev));
    290 		sc->sc_dying = 1;
    291 		USB_ATTACH_ERROR_RETURN;
    292 	}
    293 
    294 	/* get the (first/common) interface */
    295 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
    296 							&sc->sc_iface);
    297 	if (err) {
    298 		printf("\n%s: failed to get interface, err=%s\n",
    299 			devname, usbd_errstr(err));
    300 		sc->sc_dying = 1;
    301 		USB_ATTACH_ERROR_RETURN;
    302 	}
    303 
    304 	/* Find the interrupt endpoints */
    305 
    306 	id = usbd_get_interface_descriptor(sc->sc_iface);
    307 	sc->sc_iface_number = id->bInterfaceNumber;
    308 
    309 	for (i = 0; i < id->bNumEndpoints; i++) {
    310 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    311 		if (ed == NULL) {
    312 			printf("%s: no endpoint descriptor for %d\n",
    313 				USBDEVNAME(sc->sc_dev), i);
    314 			sc->sc_dying = 1;
    315 			USB_ATTACH_ERROR_RETURN;
    316 		}
    317 
    318 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    319 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    320 			sc->sc_intr_number = ed->bEndpointAddress;
    321 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    322 		}
    323 	}
    324 
    325 	if (sc->sc_intr_number== -1) {
    326 		printf("%s: Could not find interrupt in\n",
    327 			USBDEVNAME(sc->sc_dev));
    328 		sc->sc_dying = 1;
    329 		USB_ATTACH_ERROR_RETURN;
    330 	}
    331 
    332 	/* keep interface for interrupt */
    333 	sc->sc_intr_iface = sc->sc_iface;
    334 
    335 	/*
    336 	 * USB-RSAQ1 has two interface
    337 	 *
    338 	 *  USB-RSAQ1       | USB-RSAQ2
    339  	 * -----------------+-----------------
    340 	 * Interface 0      |Interface 0
    341 	 *  Interrupt(0x81) | Interrupt(0x81)
    342 	 * -----------------+ BulkIN(0x02)
    343 	 * Interface 1	    | BulkOUT(0x83)
    344 	 *   BulkIN(0x02)   |
    345 	 *   BulkOUT(0x83)  |
    346 	 */
    347 	if (cdesc->bNumInterface == 2) {
    348 		err = usbd_device2interface_handle(dev,
    349 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
    350 		if (err) {
    351 			printf("\n%s: failed to get second interface, err=%s\n",
    352 							devname, usbd_errstr(err));
    353 			sc->sc_dying = 1;
    354 			USB_ATTACH_ERROR_RETURN;
    355 		}
    356 	}
    357 
    358 	/* Find the bulk{in,out} endpoints */
    359 
    360 	id = usbd_get_interface_descriptor(sc->sc_iface);
    361 	sc->sc_iface_number = id->bInterfaceNumber;
    362 
    363 	for (i = 0; i < id->bNumEndpoints; i++) {
    364 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    365 		if (ed == NULL) {
    366 			printf("%s: no endpoint descriptor for %d\n",
    367 				USBDEVNAME(sc->sc_dev), i);
    368 			sc->sc_dying = 1;
    369 			USB_ATTACH_ERROR_RETURN;
    370 		}
    371 
    372 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    373 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    374 			uca.bulkin = ed->bEndpointAddress;
    375 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    376 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    377 			uca.bulkout = ed->bEndpointAddress;
    378 		}
    379 	}
    380 
    381 	if (uca.bulkin == -1) {
    382 		printf("%s: Could not find data bulk in\n",
    383 			USBDEVNAME(sc->sc_dev));
    384 		sc->sc_dying = 1;
    385 		USB_ATTACH_ERROR_RETURN;
    386 	}
    387 
    388 	if (uca.bulkout == -1) {
    389 		printf("%s: Could not find data bulk out\n",
    390 			USBDEVNAME(sc->sc_dev));
    391 		sc->sc_dying = 1;
    392 		USB_ATTACH_ERROR_RETURN;
    393 	}
    394 
    395 	sc->sc_dtr = sc->sc_rts = -1;
    396 	uca.portno = UCOM_UNK_PORTNO;
    397 	/* bulkin, bulkout set above */
    398 	uca.ibufsize = UPLCOMIBUFSIZE;
    399 	uca.obufsize = UPLCOMOBUFSIZE;
    400 	uca.ibufsizepad = UPLCOMIBUFSIZE;
    401 	uca.opkthdrlen = 0;
    402 	uca.device = dev;
    403 	uca.iface = sc->sc_iface;
    404 	uca.methods = &uplcom_methods;
    405 	uca.arg = sc;
    406 	uca.info = NULL;
    407 
    408 	err = uplcom_reset(sc);
    409 
    410 	if (err) {
    411 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    412 			usbd_errstr(err));
    413 		sc->sc_dying = 1;
    414 		USB_ATTACH_ERROR_RETURN;
    415 	}
    416 
    417 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    418 			   USBDEV(sc->sc_dev));
    419 
    420 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
    421 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    422 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    423 					    ucomprint, ucomsubmatch);
    424 
    425 	USB_ATTACH_SUCCESS_RETURN;
    426 }
    427 
    428 USB_DETACH(uplcom)
    429 {
    430 	USB_DETACH_START(uplcom, sc);
    431 	int rv = 0;
    432 
    433 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
    434 
    435         if (sc->sc_intr_pipe != NULL) {
    436                 usbd_abort_pipe(sc->sc_intr_pipe);
    437                 usbd_close_pipe(sc->sc_intr_pipe);
    438 		free(sc->sc_intr_buf, M_USBDEV);
    439                 sc->sc_intr_pipe = NULL;
    440         }
    441 
    442 	sc->sc_dying = 1;
    443 	if (sc->sc_subdev != NULL) {
    444 		rv = config_detach(sc->sc_subdev, flags);
    445 		sc->sc_subdev = NULL;
    446 	}
    447 
    448 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    449 			   USBDEV(sc->sc_dev));
    450 
    451 	return (rv);
    452 }
    453 
    454 int
    455 uplcom_activate(device_ptr_t self, enum devact act)
    456 {
    457 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    458 	int rv = 0;
    459 
    460 	switch (act) {
    461 	case DVACT_ACTIVATE:
    462 		return (EOPNOTSUPP);
    463 
    464 	case DVACT_DEACTIVATE:
    465 		if (sc->sc_subdev != NULL)
    466 			rv = config_deactivate(sc->sc_subdev);
    467 		sc->sc_dying = 1;
    468 		break;
    469 	}
    470 	return (rv);
    471 }
    472 
    473 usbd_status
    474 uplcom_reset(struct uplcom_softc *sc)
    475 {
    476         usb_device_request_t req;
    477 	usbd_status err;
    478 
    479         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    480         req.bRequest = UPLCOM_SET_REQUEST;
    481         USETW(req.wValue, 0);
    482         USETW(req.wIndex, sc->sc_iface_number);
    483         USETW(req.wLength, 0);
    484 
    485         err = usbd_do_request(sc->sc_udev, &req, 0);
    486 	if (err)
    487 		return (EIO);
    488 
    489 	return (0);
    490 }
    491 
    492 void
    493 uplcom_set_line_state(struct uplcom_softc *sc)
    494 {
    495 	usb_device_request_t req;
    496 	int ls;
    497 
    498 	/* make sure we have initialized state for sc_dtr and sc_rts */
    499 	if (sc->sc_dtr == -1)
    500 		sc->sc_dtr = 0;
    501 	if (sc->sc_rts == -1)
    502 		sc->sc_rts = 0;
    503 
    504 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    505 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    506 
    507 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    508 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    509 	USETW(req.wValue, ls);
    510 	USETW(req.wIndex, sc->sc_iface_number);
    511 	USETW(req.wLength, 0);
    512 
    513 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    514 }
    515 
    516 void
    517 uplcom_set(void *addr, int portno, int reg, int onoff)
    518 {
    519 	struct uplcom_softc *sc = addr;
    520 
    521 	switch (reg) {
    522 	case UCOM_SET_DTR:
    523 		uplcom_dtr(sc, onoff);
    524 		break;
    525 	case UCOM_SET_RTS:
    526 		uplcom_rts(sc, onoff);
    527 		break;
    528 	case UCOM_SET_BREAK:
    529 		uplcom_break(sc, onoff);
    530 		break;
    531 	default:
    532 		break;
    533 	}
    534 }
    535 
    536 void
    537 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    538 {
    539 
    540 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
    541 
    542 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
    543 		return;
    544 
    545 	sc->sc_dtr = !!onoff;
    546 
    547 	uplcom_set_line_state(sc);
    548 }
    549 
    550 void
    551 uplcom_rts(struct uplcom_softc *sc, int onoff)
    552 {
    553 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    554 
    555 	if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
    556 		return;
    557 
    558 	sc->sc_rts = !!onoff;
    559 
    560 	uplcom_set_line_state(sc);
    561 }
    562 
    563 void
    564 uplcom_break(struct uplcom_softc *sc, int onoff)
    565 {
    566 	usb_device_request_t req;
    567 
    568 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    569 
    570 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    571 	req.bRequest = UCDC_SEND_BREAK;
    572 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    573 	USETW(req.wIndex, sc->sc_iface_number);
    574 	USETW(req.wLength, 0);
    575 
    576 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    577 }
    578 
    579 usbd_status
    580 uplcom_set_crtscts(struct uplcom_softc *sc)
    581 {
    582 	usb_device_request_t req;
    583 	usbd_status err;
    584 
    585 	DPRINTF(("uplcom_set_crtscts: on\n"));
    586 
    587 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    588 	req.bRequest = UPLCOM_SET_REQUEST;
    589 	USETW(req.wValue, 0);
    590 	if (sc->sc_type == UPLCOM_TYPE_HX)
    591 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
    592 	else
    593 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
    594 	USETW(req.wLength, 0);
    595 
    596 	err = usbd_do_request(sc->sc_udev, &req, 0);
    597 	if (err) {
    598 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
    599 			usbd_errstr(err)));
    600 		return (err);
    601 	}
    602 
    603 	return (USBD_NORMAL_COMPLETION);
    604 }
    605 
    606 usbd_status
    607 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    608 {
    609 	usb_device_request_t req;
    610 	usbd_status err;
    611 
    612 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    613 		UGETDW(state->dwDTERate), state->bCharFormat,
    614 		state->bParityType, state->bDataBits));
    615 
    616 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    617 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    618 		return (USBD_NORMAL_COMPLETION);
    619 	}
    620 
    621 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    622 	req.bRequest = UCDC_SET_LINE_CODING;
    623 	USETW(req.wValue, 0);
    624 	USETW(req.wIndex, sc->sc_iface_number);
    625 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    626 
    627 	err = usbd_do_request(sc->sc_udev, &req, state);
    628 	if (err) {
    629 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    630 			usbd_errstr(err)));
    631 		return (err);
    632 	}
    633 
    634 	sc->sc_line_state = *state;
    635 
    636 	return (USBD_NORMAL_COMPLETION);
    637 }
    638 
    639 int
    640 uplcom_param(void *addr, int portno, struct termios *t)
    641 {
    642 	struct uplcom_softc *sc = addr;
    643 	usbd_status err;
    644 	usb_cdc_line_state_t ls;
    645 
    646 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    647 
    648 	USETDW(ls.dwDTERate, t->c_ospeed);
    649 	if (ISSET(t->c_cflag, CSTOPB))
    650 		ls.bCharFormat = UCDC_STOP_BIT_2;
    651 	else
    652 		ls.bCharFormat = UCDC_STOP_BIT_1;
    653 	if (ISSET(t->c_cflag, PARENB)) {
    654 		if (ISSET(t->c_cflag, PARODD))
    655 			ls.bParityType = UCDC_PARITY_ODD;
    656 		else
    657 			ls.bParityType = UCDC_PARITY_EVEN;
    658 	} else
    659 		ls.bParityType = UCDC_PARITY_NONE;
    660 	switch (ISSET(t->c_cflag, CSIZE)) {
    661 	case CS5:
    662 		ls.bDataBits = 5;
    663 		break;
    664 	case CS6:
    665 		ls.bDataBits = 6;
    666 		break;
    667 	case CS7:
    668 		ls.bDataBits = 7;
    669 		break;
    670 	case CS8:
    671 		ls.bDataBits = 8;
    672 		break;
    673 	}
    674 
    675 	err = uplcom_set_line_coding(sc, &ls);
    676 	if (err) {
    677 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    678 		return (EIO);
    679 	}
    680 
    681 	if (ISSET(t->c_cflag, CRTSCTS))
    682 		uplcom_set_crtscts(sc);
    683 
    684 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
    685 		uplcom_set_line_state(sc);
    686 
    687 	if (err) {
    688 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    689 		return (EIO);
    690 	}
    691 
    692 	return (0);
    693 }
    694 
    695 Static usbd_status
    696 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
    697 {
    698 	usb_device_request_t req;
    699 	usbd_status err;
    700 
    701 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    702 	req.bRequest = UPLCOM_SET_REQUEST;
    703 	USETW(req.wValue, value);
    704 	USETW(req.wIndex, index);
    705 	USETW(req.wLength, 0);
    706 
    707 	err = usbd_do_request(dev, &req, NULL);
    708 
    709 	if (err) {
    710 		DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
    711 			    usbd_errstr(err), err));
    712 	}
    713 
    714 	return err;
    715 }
    716 
    717 int
    718 uplcom_open(void *addr, int portno)
    719 {
    720 	struct uplcom_softc *sc = addr;
    721 	usbd_status err;
    722 
    723 	if (sc->sc_dying)
    724 		return (EIO);
    725 
    726 	DPRINTF(("uplcom_open: sc=%p\n", sc));
    727 
    728 	/* Some unknown device frobbing. */
    729 	if (sc->sc_type == UPLCOM_TYPE_HX)
    730 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
    731 	else
    732 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
    733 
    734 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    735 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    736 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
    737 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    738 			sc->sc_intr_buf, sc->sc_isize,
    739 			uplcom_intr, USBD_DEFAULT_INTERVAL);
    740 		if (err) {
    741 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    742 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
    743 					return (EIO);
    744 		}
    745 	}
    746 
    747 	return (0);
    748 }
    749 
    750 void
    751 uplcom_close(void *addr, int portno)
    752 {
    753 	struct uplcom_softc *sc = addr;
    754 	int err;
    755 
    756 	if (sc->sc_dying)
    757 		return;
    758 
    759 	DPRINTF(("uplcom_close: close\n"));
    760 
    761 	if (sc->sc_intr_pipe != NULL) {
    762 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    763 		if (err)
    764 			printf("%s: abort interrupt pipe failed: %s\n",
    765 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    766 		err = usbd_close_pipe(sc->sc_intr_pipe);
    767 		if (err)
    768 			printf("%s: close interrupt pipe failed: %s\n",
    769 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    770 		free(sc->sc_intr_buf, M_USBDEV);
    771 		sc->sc_intr_pipe = NULL;
    772 	}
    773 }
    774 
    775 void
    776 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
    777     usbd_status status)
    778 {
    779 	struct uplcom_softc *sc = priv;
    780 	u_char *buf = sc->sc_intr_buf;
    781 	u_char pstatus;
    782 
    783 	if (sc->sc_dying)
    784 		return;
    785 
    786 	if (status != USBD_NORMAL_COMPLETION) {
    787 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    788 			return;
    789 
    790 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    791 			usbd_errstr(status)));
    792 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    793 		return;
    794 	}
    795 
    796 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
    797 
    798 	sc->sc_lsr = sc->sc_msr = 0;
    799 	pstatus = buf[8];
    800 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
    801 		sc->sc_msr |= UMSR_DSR;
    802 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
    803 		sc->sc_msr |= UMSR_DCD;
    804 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
    805 }
    806 
    807 void
    808 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    809 {
    810 	struct uplcom_softc *sc = addr;
    811 
    812 	DPRINTF(("uplcom_get_status:\n"));
    813 
    814 	if (lsr != NULL)
    815 		*lsr = sc->sc_lsr;
    816 	if (msr != NULL)
    817 		*msr = sc->sc_msr;
    818 }
    819 
    820 #if TODO
    821 int
    822 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
    823 	     usb_proc_ptr p)
    824 {
    825 	struct uplcom_softc *sc = addr;
    826 	int error = 0;
    827 
    828 	if (sc->sc_dying)
    829 		return (EIO);
    830 
    831 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
    832 
    833 	switch (cmd) {
    834 	case TIOCNOTTY:
    835 	case TIOCMGET:
    836 	case TIOCMSET:
    837 	case USB_GET_CM_OVER_DATA:
    838 	case USB_SET_CM_OVER_DATA:
    839 		break;
    840 
    841 	default:
    842 		DPRINTF(("uplcom_ioctl: unknown\n"));
    843 		error = ENOTTY;
    844 		break;
    845 	}
    846 
    847 	return (error);
    848 }
    849 #endif
    850