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