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