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