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