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