Home | History | Annotate | Line # | Download | only in usb
umct.c revision 1.32.24.13
      1 /*	$NetBSD: umct.c,v 1.32.24.13 2016/05/30 06:54:17 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.13 2016/05/30 06:54:17 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 	if (sc->sc_inendpt != -1) {
    289 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_inendpt,
    290 		    USBD_SHORT_XFER_OK, &sc->sc_inpipe, sc, sc->sc_inbuf,
    291 		    sc->sc_isize, umct_rxintr, USBD_DEFAULT_INTERVAL);
    292 		if (err) {
    293 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    294 			    device_xname(sc->sc_dev), sc->sc_inendpt));
    295 			return EIO;
    296 		}
    297 	}
    298 	sc->sc_dtr = sc->sc_rts = 0;
    299 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    300 	/* ucaa_bulkin, ucaa_bulkout set above */
    301 	ucaa.ucaa_ibufsize = UMCTIBUFSIZE;
    302 	if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232)
    303 		ucaa.ucaa_obufsize = 16; /* device is broken */
    304 	else
    305 		ucaa.ucaa_obufsize = UMCTOBUFSIZE;
    306 	ucaa.ucaa_ibufsizepad = UMCTIBUFSIZE;
    307 	ucaa.ucaa_opkthdrlen = 0;
    308 	ucaa.ucaa_device = dev;
    309 	ucaa.ucaa_iface = sc->sc_iface;
    310 	ucaa.ucaa_methods = &umct_methods;
    311 	ucaa.ucaa_arg = sc;
    312 	ucaa.ucaa_info = NULL;
    313 
    314 	umct_init(sc);
    315 
    316 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    317 			   sc->sc_dev);
    318 
    319 	DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n",
    320 	    ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number));
    321 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    322 					    ucomprint, ucomsubmatch);
    323 
    324 	return;
    325 }
    326 
    327 void
    328 umct_childdet(device_t self, device_t child)
    329 {
    330 	struct umct_softc *sc = device_private(self);
    331 
    332 	KASSERT(sc->sc_subdev == child);
    333 	sc->sc_subdev = NULL;
    334 }
    335 
    336 int
    337 umct_detach(device_t self, int flags)
    338 {
    339 	struct umct_softc *sc = device_private(self);
    340 	int rv = 0;
    341 
    342 	DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags));
    343 
    344 	if (sc->sc_intr_pipe != NULL) {
    345 		usbd_abort_pipe(sc->sc_intr_pipe);
    346 		usbd_close_pipe(sc->sc_intr_pipe);
    347 		kmem_free(sc->sc_intr_buf, sc->sc_isize);
    348 		sc->sc_intr_pipe = NULL;
    349 	}
    350 
    351 	sc->sc_dying = 1;
    352 	if (sc->sc_subdev != NULL)
    353 		rv = config_detach(sc->sc_subdev, flags);
    354 
    355 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    356 			   sc->sc_dev);
    357 
    358 	return rv;
    359 }
    360 
    361 int
    362 umct_activate(device_t self, enum devact act)
    363 {
    364 	struct umct_softc *sc = device_private(self);
    365 
    366 	switch (act) {
    367 	case DVACT_DEACTIVATE:
    368 		sc->sc_dying = 1;
    369 		return 0;
    370 	default:
    371 		return EOPNOTSUPP;
    372 	}
    373 }
    374 
    375 void
    376 umct_set_line_state(struct umct_softc *sc)
    377 {
    378 	usb_device_request_t req;
    379 	uByte ls;
    380 
    381 	ls = (sc->sc_dtr ? MCR_DTR : 0) |
    382 	     (sc->sc_rts ? MCR_RTS : 0);
    383 
    384 	DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n",
    385 			sc->sc_dtr, sc->sc_rts, ls));
    386 
    387 	req.bmRequestType = UMCT_SET_REQUEST;
    388 	req.bRequest = REQ_SET_MCR;
    389 	USETW(req.wValue, 0);
    390 	USETW(req.wIndex, sc->sc_iface_number);
    391 	USETW(req.wLength, LENGTH_SET_MCR);
    392 
    393 	(void)usbd_do_request(sc->sc_udev, &req, &ls);
    394 }
    395 
    396 void
    397 umct_set(void *addr, int portno, int reg, int onoff)
    398 {
    399 	struct umct_softc *sc = addr;
    400 
    401 	switch (reg) {
    402 	case UCOM_SET_DTR:
    403 		umct_dtr(sc, onoff);
    404 		break;
    405 	case UCOM_SET_RTS:
    406 		umct_rts(sc, onoff);
    407 		break;
    408 	case UCOM_SET_BREAK:
    409 		umct_break(sc, onoff);
    410 		break;
    411 	default:
    412 		break;
    413 	}
    414 }
    415 
    416 void
    417 umct_dtr(struct umct_softc *sc, int onoff)
    418 {
    419 
    420 	DPRINTF(("umct_dtr: onoff=%d\n", onoff));
    421 
    422 	if (sc->sc_dtr == onoff)
    423 		return;
    424 	sc->sc_dtr = onoff;
    425 
    426 	umct_set_line_state(sc);
    427 }
    428 
    429 void
    430 umct_rts(struct umct_softc *sc, int onoff)
    431 {
    432 	DPRINTF(("umct_rts: onoff=%d\n", onoff));
    433 
    434 	if (sc->sc_rts == onoff)
    435 		return;
    436 	sc->sc_rts = onoff;
    437 
    438 	umct_set_line_state(sc);
    439 }
    440 
    441 void
    442 umct_break(struct umct_softc *sc, int onoff)
    443 {
    444 	DPRINTF(("umct_break: onoff=%d\n", onoff));
    445 
    446 	umct_set_lcr(sc, onoff ? sc->last_lcr | LCR_SET_BREAK :
    447 		     sc->last_lcr);
    448 }
    449 
    450 void
    451 umct_set_lcr(struct umct_softc *sc, u_int data)
    452 {
    453 	usb_device_request_t req;
    454 	uByte adata;
    455 
    456 	adata = data;
    457 	req.bmRequestType = UMCT_SET_REQUEST;
    458 	req.bRequest = REQ_SET_LCR;
    459 	USETW(req.wValue, 0);
    460 	USETW(req.wIndex, sc->sc_iface_number);
    461 	USETW(req.wLength, LENGTH_SET_LCR);
    462 
    463 	(void)usbd_do_request(sc->sc_udev, &req, &adata); /* XXX should check */
    464 }
    465 
    466 void
    467 umct_set_baudrate(struct umct_softc *sc, u_int rate)
    468 {
    469 	usb_device_request_t req;
    470 	uDWord arate;
    471 	u_int val;
    472 
    473 	if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232 ||
    474 	    sc->sc_product == USB_PRODUCT_BELKIN_F5U109) {
    475 		switch (rate) {
    476 		case    300: val = 0x01; break;
    477 		case    600: val = 0x02; break;
    478 		case   1200: val = 0x03; break;
    479 		case   2400: val = 0x04; break;
    480 		case   4800: val = 0x06; break;
    481 		case   9600: val = 0x08; break;
    482 		case  19200: val = 0x09; break;
    483 		case  38400: val = 0x0a; break;
    484 		case  57600: val = 0x0b; break;
    485 		case 115200: val = 0x0c; break;
    486 		default:     val = -1; break;
    487 		}
    488 	} else {
    489 		val = UMCT_BAUD_RATE(rate);
    490 	}
    491 	USETDW(arate, val);
    492 
    493 	req.bmRequestType = UMCT_SET_REQUEST;
    494 	req.bRequest = REQ_SET_BAUD_RATE;
    495 	USETW(req.wValue, 0);
    496 	USETW(req.wIndex, sc->sc_iface_number);
    497 	USETW(req.wLength, LENGTH_BAUD_RATE);
    498 
    499 	(void)usbd_do_request(sc->sc_udev, &req, arate); /* XXX should check */
    500 }
    501 
    502 void
    503 umct_init(struct umct_softc *sc)
    504 {
    505 	umct_set_baudrate(sc, 9600);
    506 	umct_set_lcr(sc, LCR_DATA_BITS_8 | LCR_PARITY_NONE | LCR_STOP_BITS_1);
    507 }
    508 
    509 int
    510 umct_param(void *addr, int portno, struct termios *t)
    511 {
    512 	struct umct_softc *sc = addr;
    513 	u_int data = 0;
    514 
    515 	DPRINTF(("umct_param: sc=%p\n", sc));
    516 
    517 	DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed));
    518 
    519 	if (ISSET(t->c_cflag, CSTOPB))
    520 		data |= LCR_STOP_BITS_2;
    521 	else
    522 		data |= LCR_STOP_BITS_1;
    523 	if (ISSET(t->c_cflag, PARENB)) {
    524 		if (ISSET(t->c_cflag, PARODD))
    525 			data |= LCR_PARITY_ODD;
    526 		else
    527 			data |= LCR_PARITY_EVEN;
    528 	} else
    529 		data |= LCR_PARITY_NONE;
    530 	switch (ISSET(t->c_cflag, CSIZE)) {
    531 	case CS5:
    532 		data |= LCR_DATA_BITS_5;
    533 		break;
    534 	case CS6:
    535 		data |= LCR_DATA_BITS_6;
    536 		break;
    537 	case CS7:
    538 		data |= LCR_DATA_BITS_7;
    539 		break;
    540 	case CS8:
    541 		data |= LCR_DATA_BITS_8;
    542 		break;
    543 	}
    544 
    545 	umct_set_baudrate(sc, t->c_ospeed);
    546 
    547 	sc->last_lcr = data;
    548 	umct_set_lcr(sc, data);
    549 
    550 	return 0;
    551 }
    552 
    553 int
    554 umct_open(void *addr, int portno)
    555 {
    556 	struct umct_softc *sc = addr;
    557 	int err, lcr_data;
    558 
    559 	if (sc->sc_dying)
    560 		return EIO;
    561 
    562 	DPRINTF(("umct_open: sc=%p\n", sc));
    563 
    564 	/* initialize LCR */
    565 	lcr_data = LCR_DATA_BITS_8 | LCR_PARITY_NONE |
    566 	    LCR_STOP_BITS_1;
    567 	umct_set_lcr(sc, lcr_data);
    568 
    569 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    570 		sc->sc_status = 0; /* clear status bit */
    571 		sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
    572 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
    573 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    574 			sc->sc_intr_buf, sc->sc_isize,
    575 			umct_intr, USBD_DEFAULT_INTERVAL);
    576 		if (err) {
    577 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    578 				device_xname(sc->sc_dev), sc->sc_intr_number));
    579 					return EIO;
    580 		}
    581 	}
    582 
    583 	return 0;
    584 }
    585 
    586 void
    587 umct_close(void *addr, int portno)
    588 {
    589 	struct umct_softc *sc = addr;
    590 	int err;
    591 
    592 	if (sc->sc_dying)
    593 		return;
    594 
    595 	DPRINTF(("umct_close: close\n"));
    596 
    597 	if (sc->sc_intr_pipe != NULL) {
    598 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    599 		if (err)
    600 			printf("%s: abort interrupt pipe failed: %s\n",
    601 				device_xname(sc->sc_dev), usbd_errstr(err));
    602 		err = usbd_close_pipe(sc->sc_intr_pipe);
    603 		if (err)
    604 			printf("%s: close interrupt pipe failed: %s\n",
    605 				device_xname(sc->sc_dev), usbd_errstr(err));
    606 		kmem_free(sc->sc_intr_buf, sc->sc_isize);
    607 		sc->sc_intr_pipe = NULL;
    608 	}
    609 }
    610 
    611 void
    612 umct_rxintr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    613 {
    614 	struct umct_softc *sc = priv;
    615 
    616 	if (sc->sc_dying)
    617 		return;
    618 
    619 	ucomreadcb(xfer, device_private(sc->sc_subdev), status);
    620 }
    621 
    622 void
    623 umct_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    624 {
    625 	struct umct_softc *sc = priv;
    626 	u_char *tbuf = sc->sc_intr_buf;
    627 	u_char mstatus;
    628 
    629 	if (sc->sc_dying)
    630 		return;
    631 
    632 	if (status != USBD_NORMAL_COMPLETION) {
    633 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    634 			return;
    635 
    636 		DPRINTF(("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
    637 			usbd_errstr(status)));
    638 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    639 		return;
    640 	}
    641 
    642 	DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n",
    643 		 device_xname(sc->sc_dev), tbuf[0],tbuf[1]));
    644 
    645 	sc->sc_lsr = sc->sc_msr = 0;
    646 	mstatus = tbuf[0];
    647 	if (ISSET(mstatus, MSR_DSR))
    648 		sc->sc_msr |= UMSR_DSR;
    649 	if (ISSET(mstatus, MSR_DCD))
    650 		sc->sc_msr |= UMSR_DCD;
    651 	ucom_status_change(device_private(sc->sc_subdev));
    652 }
    653 
    654 void
    655 umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    656 {
    657 	struct umct_softc *sc = addr;
    658 
    659 	DPRINTF(("umct_get_status:\n"));
    660 
    661 	if (lsr != NULL)
    662 		*lsr = sc->sc_lsr;
    663 	if (msr != NULL)
    664 		*msr = sc->sc_msr;
    665 }
    666