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