Home | History | Annotate | Line # | Download | only in usb
umct.c revision 1.32.24.16
      1 /*	$NetBSD: umct.c,v 1.32.24.16 2016/06/09 05:06:20 skrll 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  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * MCT USB-RS232 Interface Controller
     33  * http://www.mct.com.tw/prod/rs232.html
     34  * http://www.dlink.com/products/usb/dsbs25
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: umct.c,v 1.32.24.16 2016/06/09 05:06:20 skrll Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/conf.h>
     45 #include <sys/tty.h>
     46 #include <sys/file.h>
     47 #include <sys/select.h>
     48 #include <sys/proc.h>
     49 #include <sys/vnode.h>
     50 #include <sys/device.h>
     51 #include <sys/poll.h>
     52 
     53 #include <dev/usb/usb.h>
     54 #include <dev/usb/usbcdc.h>
     55 
     56 #include <dev/usb/usbdi.h>
     57 #include <dev/usb/usbdi_util.h>
     58 #include <dev/usb/usbdevs.h>
     59 #include <dev/usb/usb_quirks.h>
     60 
     61 #include <dev/usb/ucomvar.h>
     62 #include <dev/usb/umct.h>
     63 
     64 #ifdef UMCT_DEBUG
     65 #define DPRINTFN(n, x)  if (umctdebug > (n)) printf x
     66 int	umctdebug = 0;
     67 #else
     68 #define DPRINTFN(n, x)
     69 #endif
     70 #define DPRINTF(x) DPRINTFN(0, x)
     71 
     72 #define	UMCT_CONFIG_INDEX	0
     73 #define	UMCT_IFACE_INDEX	0
     74 
     75 struct	umct_softc {
     76 	device_t		sc_dev;		/* base device */
     77 	struct usbd_device	*sc_udev;	/* USB device */
     78 	struct usbd_interface	*sc_iface;	/* interface */
     79 	int			sc_iface_number;	/* interface number */
     80 	uint16_t		sc_product;
     81 
     82 	int			sc_inendpt;	/* data in endpoint */
     83 	struct usbd_pipe	*sc_inpipe;	/* data in pipe */
     84 	u_char			*sc_inbuf;	/* interrupt buffer */
     85 
     86 	int			sc_intr_number;	/* interrupt number */
     87 	struct usbd_pipe	*sc_intr_pipe;	/* interrupt pipe */
     88 	u_char			*sc_intr_buf;	/* interrupt buffer */
     89 	int			sc_isize;
     90 
     91 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
     92 	u_char			sc_dtr;		/* current DTR state */
     93 	u_char			sc_rts;		/* current RTS state */
     94 	u_char			sc_break;	/* set break */
     95 
     96 	u_char			sc_status;
     97 
     98 	device_t		sc_subdev;	/* ucom device */
     99 
    100 	u_char			sc_dying;	/* disconnecting */
    101 
    102 	u_char			sc_lsr;		/* Local status register */
    103 	u_char			sc_msr;		/* umct status register */
    104 
    105 	u_int			last_lcr;	/* keep lcr register */
    106 };
    107 
    108 /*
    109  * These are the maximum number of bytes transferred per frame.
    110  * The output buffer size cannot be increased due to the size encoding.
    111  */
    112 #define UMCTIBUFSIZE 256
    113 #define UMCTOBUFSIZE 256
    114 
    115 Static	void umct_init(struct umct_softc *);
    116 Static	void umct_set_baudrate(struct umct_softc *, u_int);
    117 Static	void umct_set_lcr(struct umct_softc *, u_int);
    118 Static	void umct_intr(struct usbd_xfer *, void *, usbd_status);
    119 
    120 Static	void umct_rxintr(struct usbd_xfer *, void *, usbd_status);
    121 
    122 Static	void umct_set(void *, int, int, int);
    123 Static	void umct_dtr(struct umct_softc *, int);
    124 Static	void umct_rts(struct umct_softc *, int);
    125 Static	void umct_break(struct umct_softc *, int);
    126 Static	void umct_set_line_state(struct umct_softc *);
    127 Static	void umct_get_status(void *, int, u_char *, u_char *);
    128 Static	int  umct_param(void *, int, struct termios *);
    129 Static	int  umct_open(void *, int);
    130 Static	void umct_close(void *, int);
    131 
    132 struct	ucom_methods umct_methods = {
    133 	.ucom_get_status = umct_get_status,
    134 	.ucom_set = umct_set,
    135 	.ucom_param = umct_param,
    136 	.ucom_ioctl = NULL,
    137 	.ucom_open = umct_open,
    138 	.ucom_close = umct_close,
    139 	.ucom_read = NULL,
    140 	.ucom_write = NULL,
    141 };
    142 
    143 static const struct usb_devno umct_devs[] = {
    144 	/* MCT USB-232 Interface Products */
    145 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
    146 	/* Sitecom USB-232 Products */
    147 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
    148 	/* D-Link DU-H3SP USB BAY Hub Products */
    149 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
    150 	/* BELKIN F5U109 */
    151 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109 },
    152 };
    153 #define umct_lookup(v, p) usb_lookup(umct_devs, v, p)
    154 
    155 int umct_match(device_t, cfdata_t, void *);
    156 void umct_attach(device_t, device_t, void *);
    157 void umct_childdet(device_t, device_t);
    158 int umct_detach(device_t, int);
    159 int umct_activate(device_t, enum devact);
    160 extern struct cfdriver umct_cd;
    161 CFATTACH_DECL2_NEW(umct, sizeof(struct umct_softc), umct_match,
    162     umct_attach, umct_detach, umct_activate, NULL, umct_childdet);
    163 
    164 int
    165 umct_match(device_t parent, cfdata_t match, void *aux)
    166 {
    167 	struct usb_attach_arg *uaa = aux;
    168 
    169 	return umct_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    170 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    171 }
    172 
    173 void
    174 umct_attach(device_t parent, device_t self, void *aux)
    175 {
    176 	struct umct_softc *sc = device_private(self);
    177 	struct usb_attach_arg *uaa = aux;
    178 	struct usbd_device *dev = uaa->uaa_device;
    179 	usb_config_descriptor_t *cdesc;
    180 	usb_interface_descriptor_t *id;
    181 	usb_endpoint_descriptor_t *ed;
    182 
    183 	char *devinfop;
    184 	usbd_status err;
    185 	int i;
    186 	struct ucom_attach_args ucaa;
    187 
    188 	sc->sc_dev = self;
    189 
    190 	aprint_naive("\n");
    191 	aprint_normal("\n");
    192 
    193 	devinfop = usbd_devinfo_alloc(dev, 0);
    194 	aprint_normal_dev(self, "%s\n", devinfop);
    195 	usbd_devinfo_free(devinfop);
    196 
    197 	sc->sc_udev = dev;
    198 	sc->sc_product = uaa->uaa_product;
    199 
    200 	DPRINTF(("\n\numct attach: sc=%p\n", sc));
    201 
    202 	/* initialize endpoints */
    203 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    204 	sc->sc_intr_number = -1;
    205 	sc->sc_intr_pipe = NULL;
    206 
    207 	/* Move the device into the configured state. */
    208 	err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
    209 	if (err) {
    210 		aprint_error_dev(self, "failed to set configuration, err=%s\n",
    211 		    usbd_errstr(err));
    212 		sc->sc_dying = 1;
    213 		return;
    214 	}
    215 
    216 	/* get the config descriptor */
    217 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    218 
    219 	if (cdesc == NULL) {
    220 		aprint_error_dev(self,
    221 		    "failed to get configuration descriptor\n");
    222 		sc->sc_dying = 1;
    223 		return;
    224 	}
    225 
    226 	/* get the interface */
    227 	err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
    228 							&sc->sc_iface);
    229 	if (err) {
    230 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    231 		    usbd_errstr(err));
    232 		sc->sc_dying = 1;
    233 		return;
    234 	}
    235 
    236 	/* Find the bulk{in,out} and interrupt endpoints */
    237 
    238 	id = usbd_get_interface_descriptor(sc->sc_iface);
    239 	sc->sc_iface_number = id->bInterfaceNumber;
    240 
    241 	for (i = 0; i < id->bNumEndpoints; i++) {
    242 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    243 		if (ed == NULL) {
    244 			aprint_error_dev(self,
    245 			    "no endpoint descriptor for %d\n", i);
    246 			sc->sc_dying = 1;
    247 			return;
    248 		}
    249 
    250 		/*
    251 		 * Input is done via an interrupt endpoint, so we handle
    252 		 * the pipe and pass RX characters up to ucom.  Since
    253 		 * we can't rely on the endpoint descriptor order, we'll
    254 		 * check the wMaxPacketSize field to differentiate.
    255 		 */
    256 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    257 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
    258 		    UGETW(ed->wMaxPacketSize) != 0x2) {
    259 			sc->sc_inendpt = ed->bEndpointAddress;
    260 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    261 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    262 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
    263 		} else 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_inendpt == -1) {
    271 		aprint_error_dev(self, "Could not find data in\n");
    272 		sc->sc_dying = 1;
    273 		return;
    274 	}
    275 
    276 	if (ucaa.ucaa_bulkout == -1) {
    277 		aprint_error_dev(self, "Could not find data bulk out\n");
    278 		sc->sc_dying = 1;
    279 		return;
    280 	}
    281 
    282 	if (sc->sc_intr_number == -1) {
    283 		aprint_error_dev(self, "Could not find interrupt in\n");
    284 		sc->sc_dying = 1;
    285 		return;
    286 	}
    287 
    288 	sc->sc_dtr = sc->sc_rts = 0;
    289 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    290 	/* ucaa_bulkin, ucaa_bulkout set above */
    291 	ucaa.ucaa_ibufsize = UMCTIBUFSIZE;
    292 	if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232)
    293 		ucaa.ucaa_obufsize = 16; /* device is broken */
    294 	else
    295 		ucaa.ucaa_obufsize = UMCTOBUFSIZE;
    296 	ucaa.ucaa_ibufsizepad = UMCTIBUFSIZE;
    297 	ucaa.ucaa_opkthdrlen = 0;
    298 	ucaa.ucaa_device = dev;
    299 	ucaa.ucaa_iface = sc->sc_iface;
    300 	ucaa.ucaa_methods = &umct_methods;
    301 	ucaa.ucaa_arg = sc;
    302 	ucaa.ucaa_info = NULL;
    303 
    304 	umct_init(sc);
    305 
    306 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    307 			   sc->sc_dev);
    308 
    309 	DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n",
    310 	    ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number));
    311 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    312 					    ucomprint, ucomsubmatch);
    313 
    314 	return;
    315 }
    316 
    317 void
    318 umct_childdet(device_t self, device_t child)
    319 {
    320 	struct umct_softc *sc = device_private(self);
    321 
    322 	KASSERT(sc->sc_subdev == child);
    323 	sc->sc_subdev = NULL;
    324 }
    325 
    326 int
    327 umct_detach(device_t self, int flags)
    328 {
    329 	struct umct_softc *sc = device_private(self);
    330 	int rv = 0;
    331 
    332 	DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags));
    333 
    334 	if (sc->sc_intr_pipe != NULL) {
    335 		usbd_abort_pipe(sc->sc_intr_pipe);
    336 		usbd_close_pipe(sc->sc_intr_pipe);
    337 		kmem_free(sc->sc_intr_buf, sc->sc_isize);
    338 		sc->sc_intr_pipe = NULL;
    339 	}
    340 
    341 	sc->sc_dying = 1;
    342 	if (sc->sc_subdev != NULL)
    343 		rv = config_detach(sc->sc_subdev, flags);
    344 
    345 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    346 			   sc->sc_dev);
    347 
    348 	return rv;
    349 }
    350 
    351 int
    352 umct_activate(device_t self, enum devact act)
    353 {
    354 	struct umct_softc *sc = device_private(self);
    355 
    356 	switch (act) {
    357 	case DVACT_DEACTIVATE:
    358 		sc->sc_dying = 1;
    359 		return 0;
    360 	default:
    361 		return EOPNOTSUPP;
    362 	}
    363 }
    364 
    365 void
    366 umct_set_line_state(struct umct_softc *sc)
    367 {
    368 	usb_device_request_t req;
    369 	uByte ls;
    370 
    371 	ls = (sc->sc_dtr ? MCR_DTR : 0) |
    372 	     (sc->sc_rts ? MCR_RTS : 0);
    373 
    374 	DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n",
    375 			sc->sc_dtr, sc->sc_rts, ls));
    376 
    377 	req.bmRequestType = UMCT_SET_REQUEST;
    378 	req.bRequest = REQ_SET_MCR;
    379 	USETW(req.wValue, 0);
    380 	USETW(req.wIndex, sc->sc_iface_number);
    381 	USETW(req.wLength, LENGTH_SET_MCR);
    382 
    383 	(void)usbd_do_request(sc->sc_udev, &req, &ls);
    384 }
    385 
    386 void
    387 umct_set(void *addr, int portno, int reg, int onoff)
    388 {
    389 	struct umct_softc *sc = addr;
    390 
    391 	switch (reg) {
    392 	case UCOM_SET_DTR:
    393 		umct_dtr(sc, onoff);
    394 		break;
    395 	case UCOM_SET_RTS:
    396 		umct_rts(sc, onoff);
    397 		break;
    398 	case UCOM_SET_BREAK:
    399 		umct_break(sc, onoff);
    400 		break;
    401 	default:
    402 		break;
    403 	}
    404 }
    405 
    406 void
    407 umct_dtr(struct umct_softc *sc, int onoff)
    408 {
    409 
    410 	DPRINTF(("umct_dtr: onoff=%d\n", onoff));
    411 
    412 	if (sc->sc_dtr == onoff)
    413 		return;
    414 	sc->sc_dtr = onoff;
    415 
    416 	umct_set_line_state(sc);
    417 }
    418 
    419 void
    420 umct_rts(struct umct_softc *sc, int onoff)
    421 {
    422 	DPRINTF(("umct_rts: onoff=%d\n", onoff));
    423 
    424 	if (sc->sc_rts == onoff)
    425 		return;
    426 	sc->sc_rts = onoff;
    427 
    428 	umct_set_line_state(sc);
    429 }
    430 
    431 void
    432 umct_break(struct umct_softc *sc, int onoff)
    433 {
    434 	DPRINTF(("umct_break: onoff=%d\n", onoff));
    435 
    436 	umct_set_lcr(sc, onoff ? sc->last_lcr | LCR_SET_BREAK :
    437 		     sc->last_lcr);
    438 }
    439 
    440 void
    441 umct_set_lcr(struct umct_softc *sc, u_int data)
    442 {
    443 	usb_device_request_t req;
    444 	uByte adata;
    445 
    446 	adata = data;
    447 	req.bmRequestType = UMCT_SET_REQUEST;
    448 	req.bRequest = REQ_SET_LCR;
    449 	USETW(req.wValue, 0);
    450 	USETW(req.wIndex, sc->sc_iface_number);
    451 	USETW(req.wLength, LENGTH_SET_LCR);
    452 
    453 	(void)usbd_do_request(sc->sc_udev, &req, &adata); /* XXX should check */
    454 }
    455 
    456 void
    457 umct_set_baudrate(struct umct_softc *sc, u_int rate)
    458 {
    459 	usb_device_request_t req;
    460 	uDWord arate;
    461 	u_int val;
    462 
    463 	if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232 ||
    464 	    sc->sc_product == USB_PRODUCT_BELKIN_F5U109) {
    465 		switch (rate) {
    466 		case    300: val = 0x01; break;
    467 		case    600: val = 0x02; break;
    468 		case   1200: val = 0x03; break;
    469 		case   2400: val = 0x04; break;
    470 		case   4800: val = 0x06; break;
    471 		case   9600: val = 0x08; break;
    472 		case  19200: val = 0x09; break;
    473 		case  38400: val = 0x0a; break;
    474 		case  57600: val = 0x0b; break;
    475 		case 115200: val = 0x0c; break;
    476 		default:     val = -1; break;
    477 		}
    478 	} else {
    479 		val = UMCT_BAUD_RATE(rate);
    480 	}
    481 	USETDW(arate, val);
    482 
    483 	req.bmRequestType = UMCT_SET_REQUEST;
    484 	req.bRequest = REQ_SET_BAUD_RATE;
    485 	USETW(req.wValue, 0);
    486 	USETW(req.wIndex, sc->sc_iface_number);
    487 	USETW(req.wLength, LENGTH_BAUD_RATE);
    488 
    489 	(void)usbd_do_request(sc->sc_udev, &req, arate); /* XXX should check */
    490 }
    491 
    492 void
    493 umct_init(struct umct_softc *sc)
    494 {
    495 	umct_set_baudrate(sc, 9600);
    496 	umct_set_lcr(sc, LCR_DATA_BITS_8 | LCR_PARITY_NONE | LCR_STOP_BITS_1);
    497 }
    498 
    499 int
    500 umct_param(void *addr, int portno, struct termios *t)
    501 {
    502 	struct umct_softc *sc = addr;
    503 	u_int data = 0;
    504 
    505 	DPRINTF(("umct_param: sc=%p\n", sc));
    506 
    507 	DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed));
    508 
    509 	if (ISSET(t->c_cflag, CSTOPB))
    510 		data |= LCR_STOP_BITS_2;
    511 	else
    512 		data |= LCR_STOP_BITS_1;
    513 	if (ISSET(t->c_cflag, PARENB)) {
    514 		if (ISSET(t->c_cflag, PARODD))
    515 			data |= LCR_PARITY_ODD;
    516 		else
    517 			data |= LCR_PARITY_EVEN;
    518 	} else
    519 		data |= LCR_PARITY_NONE;
    520 	switch (ISSET(t->c_cflag, CSIZE)) {
    521 	case CS5:
    522 		data |= LCR_DATA_BITS_5;
    523 		break;
    524 	case CS6:
    525 		data |= LCR_DATA_BITS_6;
    526 		break;
    527 	case CS7:
    528 		data |= LCR_DATA_BITS_7;
    529 		break;
    530 	case CS8:
    531 		data |= LCR_DATA_BITS_8;
    532 		break;
    533 	}
    534 
    535 	umct_set_baudrate(sc, t->c_ospeed);
    536 
    537 	sc->last_lcr = data;
    538 	umct_set_lcr(sc, data);
    539 
    540 	return 0;
    541 }
    542 
    543 int
    544 umct_open(void *addr, int portno)
    545 {
    546 	struct umct_softc *sc = addr;
    547 	int err, lcr_data;
    548 
    549 	if (sc->sc_dying)
    550 		return EIO;
    551 
    552 	DPRINTF(("umct_open: sc=%p\n", sc));
    553 
    554 	/* initialize LCR */
    555 	lcr_data = LCR_DATA_BITS_8 | LCR_PARITY_NONE |
    556 	    LCR_STOP_BITS_1;
    557 	umct_set_lcr(sc, lcr_data);
    558 
    559 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    560 		sc->sc_status = 0; /* clear status bit */
    561 		sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
    562 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
    563 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    564 			sc->sc_intr_buf, sc->sc_isize,
    565 			umct_intr, USBD_DEFAULT_INTERVAL);
    566 		if (err) {
    567 			DPRINTF(("%s: cannot open interrupt pipe (%d)\n",
    568 			    device_xname(sc->sc_dev), sc->sc_intr_number));
    569 			return EIO;
    570 		}
    571 	}
    572 	if (sc->sc_inendpt != -1 && sc->sc_inpipe == NULL) {
    573 		sc->sc_inbuf = kmem_alloc(UMCTIBUFSIZE, KM_SLEEP);
    574 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_inendpt,
    575 		    USBD_SHORT_XFER_OK, &sc->sc_inpipe, sc, sc->sc_inbuf,
    576 		    UMCTIBUFSIZE, umct_rxintr, USBD_DEFAULT_INTERVAL);
    577 		if (err) {
    578 			DPRINTF(("%s: cannot open rx interrupt pipe (%d)\n",
    579 			    device_xname(sc->sc_dev), sc->sc_inendpt));
    580 			return EIO;
    581 		}
    582 	}
    583 
    584 	return 0;
    585 }
    586 
    587 void
    588 umct_close(void *addr, int portno)
    589 {
    590 	struct umct_softc *sc = addr;
    591 	int err;
    592 
    593 	if (sc->sc_dying)
    594 		return;
    595 
    596 	DPRINTF(("umct_close: close\n"));
    597 
    598 	if (sc->sc_intr_pipe != NULL) {
    599 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    600 		if (err)
    601 			printf("%s: abort interrupt pipe failed: %s\n",
    602 				device_xname(sc->sc_dev), usbd_errstr(err));
    603 		err = usbd_close_pipe(sc->sc_intr_pipe);
    604 		if (err)
    605 			printf("%s: close interrupt pipe failed: %s\n",
    606 				device_xname(sc->sc_dev), usbd_errstr(err));
    607 		kmem_free(sc->sc_intr_buf, sc->sc_isize);
    608 		sc->sc_intr_pipe = NULL;
    609 	}
    610 	if (sc->sc_inpipe != NULL) {
    611 		err = usbd_abort_pipe(sc->sc_inpipe);
    612 		if (err)
    613 			printf("%s: abort rx interrupt pipe failed: %s\n",
    614 				device_xname(sc->sc_dev), usbd_errstr(err));
    615 		err = usbd_close_pipe(sc->sc_inpipe);
    616 		if (err)
    617 			printf("%s: close rx interrupt pipe failed: %s\n",
    618 				device_xname(sc->sc_dev), usbd_errstr(err));
    619 		kmem_free(sc->sc_inbuf, UMCTIBUFSIZE);
    620 		sc->sc_inpipe = NULL;
    621 	}
    622 }
    623 
    624 void
    625 umct_rxintr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    626 {
    627 	struct umct_softc *sc = priv;
    628 
    629 	if (sc->sc_dying)
    630 		return;
    631 
    632 	ucomreadcb(xfer, device_private(sc->sc_subdev), status);
    633 }
    634 
    635 void
    636 umct_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    637 {
    638 	struct umct_softc *sc = priv;
    639 	u_char *tbuf = sc->sc_intr_buf;
    640 	u_char mstatus;
    641 
    642 	if (sc->sc_dying)
    643 		return;
    644 
    645 	if (status != USBD_NORMAL_COMPLETION) {
    646 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    647 			return;
    648 
    649 		DPRINTF(("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
    650 			usbd_errstr(status)));
    651 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    652 		return;
    653 	}
    654 
    655 	DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n",
    656 		 device_xname(sc->sc_dev), tbuf[0],tbuf[1]));
    657 
    658 	sc->sc_lsr = sc->sc_msr = 0;
    659 	mstatus = tbuf[0];
    660 	if (ISSET(mstatus, MSR_DSR))
    661 		sc->sc_msr |= UMSR_DSR;
    662 	if (ISSET(mstatus, MSR_DCD))
    663 		sc->sc_msr |= UMSR_DCD;
    664 	ucom_status_change(device_private(sc->sc_subdev));
    665 }
    666 
    667 void
    668 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    669 {
    670 	struct umct_softc *sc = addr;
    671 
    672 	DPRINTF(("umct_get_status:\n"));
    673 
    674 	if (lsr != NULL)
    675 		*lsr = sc->sc_lsr;
    676 	if (msr != NULL)
    677 		*msr = sc->sc_msr;
    678 }
    679