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