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