Home | History | Annotate | Line # | Download | only in usb
uvscom.c revision 1.10
      1 /*	$NetBSD: uvscom.c,v 1.10 2003/07/14 15:47:30 lukem Exp $	*/
      2 /*-
      3  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama (at) jp.FreeBSD.org>.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  *
     27  * $FreeBSD: src/sys/dev/usb/uvscom.c,v 1.1 2002/03/18 18:23:39 joe Exp $
     28  */
     29 
     30 /*
     31  * uvscom: SUNTAC Slipper U VS-10U driver.
     32  * Slipper U is a PC card to USB converter for data communication card
     33  * adapter.  It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in,
     34  * P-in m@ater and various data communication card adapters.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: uvscom.c,v 1.10 2003/07/14 15:47:30 lukem Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/malloc.h>
     44 #include <sys/fcntl.h>
     45 #include <sys/conf.h>
     46 #include <sys/tty.h>
     47 #include <sys/file.h>
     48 #if defined(__FreeBSD__)
     49 #include <sys/bus.h>
     50 #include <sys/ioccom.h>
     51 #if __FreeBSD_version >= 500014
     52 #include <sys/selinfo.h>
     53 #else
     54 #include <sys/select.h>
     55 #endif
     56 #else
     57 #include <sys/ioctl.h>
     58 #include <sys/device.h>
     59 #endif
     60 #include <sys/proc.h>
     61 #include <sys/vnode.h>
     62 #include <sys/poll.h>
     63 
     64 #include <dev/usb/usb.h>
     65 #include <dev/usb/usbcdc.h>
     66 
     67 #include <dev/usb/usbdi.h>
     68 #include <dev/usb/usbdi_util.h>
     69 #include <dev/usb/usbdevs.h>
     70 #include <dev/usb/usb_quirks.h>
     71 
     72 #include <dev/usb/ucomvar.h>
     73 
     74 #ifdef UVSCOM_DEBUG
     75 static int	uvscomdebug = 1;
     76 
     77 #if defined(__FreeBSD__)
     78 #include <sys/sysctl.h>
     79 
     80 SYSCTL_DECL(_debug_usb);
     81 SYSCTL_INT(_debug_usb, OID_AUTO, uvscom, CTLFLAG_RW,
     82 	   &uvscomdebug, 0, "uvscom debug level");
     83 
     84 #endif
     85 
     86 #define DPRINTFN(n, x)  do { \
     87 				if (uvscomdebug > (n)) \
     88 					logprintf x; \
     89 			} while (0)
     90 #else
     91 #define DPRINTFN(n, x)
     92 #endif
     93 #define DPRINTF(x) DPRINTFN(0, x)
     94 
     95 #if defined(__FreeBSD__)
     96 #define UVSCOM_MODVER		1	/* module version */
     97 #endif
     98 
     99 #define	UVSCOM_CONFIG_INDEX	0
    100 #define	UVSCOM_IFACE_INDEX	0
    101 
    102 #define UVSCOM_INTR_INTERVAL	100	/* mS */
    103 
    104 #define UVSCOM_UNIT_WAIT	5
    105 
    106 /* Request */
    107 #define UVSCOM_SET_SPEED	0x10
    108 #define UVSCOM_LINE_CTL		0x11
    109 #define UVSCOM_SET_PARAM	0x12
    110 #define UVSCOM_READ_STATUS	0xd0
    111 #define UVSCOM_SHUTDOWN		0xe0
    112 
    113 /* UVSCOM_SET_SPEED parameters */
    114 #define UVSCOM_SPEED_150BPS	0x00
    115 #define UVSCOM_SPEED_300BPS	0x01
    116 #define UVSCOM_SPEED_600BPS	0x02
    117 #define UVSCOM_SPEED_1200BPS	0x03
    118 #define UVSCOM_SPEED_2400BPS	0x04
    119 #define UVSCOM_SPEED_4800BPS	0x05
    120 #define UVSCOM_SPEED_9600BPS	0x06
    121 #define UVSCOM_SPEED_19200BPS	0x07
    122 #define UVSCOM_SPEED_38400BPS	0x08
    123 #define UVSCOM_SPEED_57600BPS	0x09
    124 #define UVSCOM_SPEED_115200BPS	0x0a
    125 
    126 /* UVSCOM_LINE_CTL parameters */
    127 #define UVSCOM_BREAK		0x40
    128 #define UVSCOM_RTS		0x02
    129 #define UVSCOM_DTR		0x01
    130 #define UVSCOM_LINE_INIT	0x08
    131 
    132 /* UVSCOM_SET_PARAM parameters */
    133 #define UVSCOM_DATA_MASK	0x03
    134 #define UVSCOM_DATA_BIT_8	0x03
    135 #define UVSCOM_DATA_BIT_7	0x02
    136 #define UVSCOM_DATA_BIT_6	0x01
    137 #define UVSCOM_DATA_BIT_5	0x00
    138 
    139 #define UVSCOM_STOP_MASK	0x04
    140 #define UVSCOM_STOP_BIT_2	0x04
    141 #define UVSCOM_STOP_BIT_1	0x00
    142 
    143 #define UVSCOM_PARITY_MASK	0x18
    144 #define UVSCOM_PARITY_EVEN	0x18
    145 #if 0
    146 #define UVSCOM_PARITY_UNK	0x10
    147 #endif
    148 #define UVSCOM_PARITY_ODD	0x08
    149 #define UVSCOM_PARITY_NONE	0x00
    150 
    151 /* Status bits */
    152 #define UVSCOM_TXRDY		0x04
    153 #define UVSCOM_RXRDY		0x01
    154 
    155 #define UVSCOM_DCD		0x08
    156 #define UVSCOM_NOCARD		0x04
    157 #define UVSCOM_DSR		0x02
    158 #define UVSCOM_CTS		0x01
    159 #define UVSCOM_USTAT_MASK	(UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
    160 
    161 struct	uvscom_softc {
    162 	USBBASEDEVICE		sc_dev;		/* base device */
    163 	usbd_device_handle	sc_udev;	/* USB device */
    164 	usbd_interface_handle	sc_iface;	/* interface */
    165 	int			sc_iface_number;/* interface number */
    166 
    167 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
    168 	int			sc_intr_number;	/* interrupt number */
    169 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
    170 	u_char			*sc_intr_buf;	/* interrupt buffer */
    171 	int			sc_isize;
    172 
    173 	u_char			sc_dtr;		/* current DTR state */
    174 	u_char			sc_rts;		/* current RTS state */
    175 
    176 	u_char			sc_lsr;		/* Local status register */
    177 	u_char			sc_msr;		/* uvscom status register */
    178 
    179 	uint16_t		sc_lcr;		/* Line control */
    180 	u_char			sc_usr;		/* unit status */
    181 
    182 	device_ptr_t		sc_subdev;	/* ucom device */
    183 	u_char			sc_dying;	/* disconnecting */
    184 };
    185 
    186 /*
    187  * These are the maximum number of bytes transferred per frame.
    188  * The output buffer size cannot be increased due to the size encoding.
    189  */
    190 #define UVSCOMIBUFSIZE 512
    191 #define UVSCOMOBUFSIZE 64
    192 
    193 Static	usbd_status uvscom_readstat(struct uvscom_softc *);
    194 Static	usbd_status uvscom_shutdown(struct uvscom_softc *);
    195 Static	usbd_status uvscom_reset(struct uvscom_softc *);
    196 Static	usbd_status uvscom_set_line_coding(struct uvscom_softc *,
    197 					   uint16_t, uint16_t);
    198 Static	usbd_status uvscom_set_line(struct uvscom_softc *, uint16_t);
    199 Static	usbd_status uvscom_set_crtscts(struct uvscom_softc *);
    200 Static	void uvscom_get_status(void *, int, u_char *, u_char *);
    201 Static	void uvscom_dtr(struct uvscom_softc *, int);
    202 Static	void uvscom_rts(struct uvscom_softc *, int);
    203 Static	void uvscom_break(struct uvscom_softc *, int);
    204 
    205 Static	void uvscom_set(void *, int, int, int);
    206 Static	void uvscom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    207 Static	int  uvscom_param(void *, int, struct termios *);
    208 Static	int  uvscom_open(void *, int);
    209 Static	void uvscom_close(void *, int);
    210 
    211 struct ucom_methods uvscom_methods = {
    212 	uvscom_get_status,
    213 	uvscom_set,
    214 	uvscom_param,
    215 	NULL, /* uvscom_ioctl, TODO */
    216 	uvscom_open,
    217 	uvscom_close,
    218 	NULL,
    219 	NULL
    220 };
    221 
    222 static const struct usb_devno uvscom_devs [] = {
    223 	/* SUNTAC U-Cable type D2 */
    224 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L },
    225 	/* SUNTAC U-Cable type P1 */
    226 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1 },
    227 	/* SUNTAC Slipper U  */
    228 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U },
    229 	/* SUNTAC Ir-Trinity */
    230 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U },
    231 };
    232 #define uvscom_lookup(v, p) usb_lookup(uvscom_devs, v, p)
    233 
    234 USB_DECLARE_DRIVER(uvscom);
    235 
    236 #ifdef __FreeBSD__
    237 Static device_probe_t uvscom_match;
    238 Static device_attach_t uvscom_attach;
    239 Static device_detach_t uvscom_detach;
    240 
    241 Static device_method_t uvscom_methods[] = {
    242 	/* Device interface */
    243 	DEVMETHOD(device_probe, uvscom_match),
    244 	DEVMETHOD(device_attach, uvscom_attach),
    245 	DEVMETHOD(device_detach, uvscom_detach),
    246 	{ 0, 0 }
    247 };
    248 
    249 Static driver_t uvscom_driver = {
    250 	"usio",
    251 	uvscom_methods,
    252 	sizeof (struct uvscom_softc)
    253 };
    254 
    255 DRIVER_MODULE(uvscom, uhub, uvscom_driver, ucom_devclass, usbd_driver_load, 0);
    256 MODULE_DEPEND(uvscom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
    257 MODULE_VERSION(uvscom, UVSCOM_MODVER);
    258 #endif
    259 
    260 USB_MATCH(uvscom)
    261 {
    262 	USB_MATCH_START(uvscom, uaa);
    263 
    264 	if (uaa->iface != NULL)
    265 		return (UMATCH_NONE);
    266 
    267 	return (uvscom_lookup(uaa->vendor, uaa->product) != NULL ?
    268 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    269 }
    270 
    271 USB_ATTACH(uvscom)
    272 {
    273 	USB_ATTACH_START(uvscom, sc, uaa);
    274 	usbd_device_handle dev = uaa->device;
    275 	usb_config_descriptor_t *cdesc;
    276 	usb_interface_descriptor_t *id;
    277 	usb_endpoint_descriptor_t *ed;
    278 	char devinfo[1024];
    279 	const char *devname = USBDEVNAME(sc->sc_dev);
    280 	usbd_status err;
    281 	int i;
    282 	struct ucom_attach_args uca;
    283 
    284         usbd_devinfo(dev, 0, devinfo);
    285         USB_ATTACH_SETUP;
    286         printf("%s: %s\n", devname, devinfo);
    287 
    288         sc->sc_udev = dev;
    289 
    290 	DPRINTF(("uvscom attach: sc = %p\n", sc));
    291 
    292 	/* initialize endpoints */
    293 	uca.bulkin = uca.bulkout = -1;
    294 	sc->sc_intr_number = -1;
    295 	sc->sc_intr_pipe = NULL;
    296 
    297 	/* Move the device into the configured state. */
    298 	err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
    299 	if (err) {
    300 		printf("%s: failed to set configuration, err=%s\n",
    301 			devname, usbd_errstr(err));
    302 		sc->sc_dying = 1;
    303 		USB_ATTACH_ERROR_RETURN;
    304 	}
    305 
    306 	/* get the config descriptor */
    307 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    308 
    309 	if (cdesc == NULL) {
    310 		printf("%s: failed to get configuration descriptor\n",
    311 			USBDEVNAME(sc->sc_dev));
    312 		sc->sc_dying = 1;
    313 		USB_ATTACH_ERROR_RETURN;
    314 	}
    315 
    316 	/* get the common interface */
    317 	err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
    318 					   &sc->sc_iface);
    319 	if (err) {
    320 		printf("%s: failed to get interface, err=%s\n",
    321 			devname, usbd_errstr(err));
    322 		sc->sc_dying = 1;
    323 		USB_ATTACH_ERROR_RETURN;
    324 	}
    325 
    326 	id = usbd_get_interface_descriptor(sc->sc_iface);
    327 	sc->sc_iface_number = id->bInterfaceNumber;
    328 
    329 	/* Find endpoints */
    330 	for (i = 0; i < id->bNumEndpoints; i++) {
    331 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    332 		if (ed == NULL) {
    333 			printf("%s: no endpoint descriptor for %d\n",
    334 				USBDEVNAME(sc->sc_dev), i);
    335 			sc->sc_dying = 1;
    336 			USB_ATTACH_ERROR_RETURN;
    337 		}
    338 
    339 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    340 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    341 			uca.bulkin = ed->bEndpointAddress;
    342 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    343 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    344 			uca.bulkout = ed->bEndpointAddress;
    345 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    346 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    347 			sc->sc_intr_number = ed->bEndpointAddress;
    348 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    349 		}
    350 	}
    351 
    352 	if (uca.bulkin == -1) {
    353 		printf("%s: Could not find data bulk in\n",
    354 			USBDEVNAME(sc->sc_dev));
    355 		sc->sc_dying = 1;
    356 		USB_ATTACH_ERROR_RETURN;
    357 	}
    358 	if (uca.bulkout == -1) {
    359 		printf("%s: Could not find data bulk out\n",
    360 			USBDEVNAME(sc->sc_dev));
    361 		sc->sc_dying = 1;
    362 		USB_ATTACH_ERROR_RETURN;
    363 	}
    364 	if (sc->sc_intr_number == -1) {
    365 		printf("%s: Could not find interrupt in\n",
    366 			USBDEVNAME(sc->sc_dev));
    367 		sc->sc_dying = 1;
    368 		USB_ATTACH_ERROR_RETURN;
    369 	}
    370 
    371 	sc->sc_dtr = sc->sc_rts = 0;
    372 	sc->sc_lcr = UVSCOM_LINE_INIT;
    373 
    374 	uca.portno = UCOM_UNK_PORTNO;
    375 	/* bulkin, bulkout set above */
    376 	uca.ibufsize = UVSCOMIBUFSIZE;
    377 	uca.obufsize = UVSCOMOBUFSIZE;
    378 	uca.ibufsizepad = UVSCOMIBUFSIZE;
    379 	uca.opkthdrlen = 0;
    380 	uca.device = dev;
    381 	uca.iface = sc->sc_iface;
    382 	uca.methods = &uvscom_methods;
    383 	uca.arg = sc;
    384 	uca.info = NULL;
    385 
    386 	err = uvscom_reset(sc);
    387 
    388 	if (err) {
    389 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    390 			usbd_errstr(err));
    391 		sc->sc_dying = 1;
    392 		USB_ATTACH_ERROR_RETURN;
    393 	}
    394 
    395 	DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
    396 		 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
    397 
    398 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    399 			   USBDEV(sc->sc_dev));
    400 
    401 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
    402 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    403 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    404 
    405 	USB_ATTACH_SUCCESS_RETURN;
    406 }
    407 
    408 USB_DETACH(uvscom)
    409 {
    410 	USB_DETACH_START(uvscom, sc);
    411 	int rv = 0;
    412 
    413 	DPRINTF(("uvscom_detach: sc = %p\n", sc));
    414 
    415 	sc->sc_dying = 1;
    416 
    417 	if (sc->sc_intr_pipe != NULL) {
    418 		usbd_abort_pipe(sc->sc_intr_pipe);
    419 		usbd_close_pipe(sc->sc_intr_pipe);
    420 		free(sc->sc_intr_buf, M_USBDEV);
    421 		sc->sc_intr_pipe = NULL;
    422 	}
    423 
    424 	sc->sc_dying = 1;
    425 	if (sc->sc_subdev != NULL) {
    426 		rv = config_detach(sc->sc_subdev, flags);
    427 		sc->sc_subdev = NULL;
    428 	}
    429 
    430 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    431 			   USBDEV(sc->sc_dev));
    432 
    433 	return (rv);
    434 }
    435 
    436 int
    437 uvscom_activate(device_ptr_t self, enum devact act)
    438 {
    439 	struct uvscom_softc *sc = (struct uvscom_softc *)self;
    440 	int rv = 0;
    441 
    442 	switch (act) {
    443 	case DVACT_ACTIVATE:
    444 		return (EOPNOTSUPP);
    445 
    446 	case DVACT_DEACTIVATE:
    447 		if (sc->sc_subdev != NULL)
    448 			rv = config_deactivate(sc->sc_subdev);
    449 		sc->sc_dying = 1;
    450 		break;
    451 	}
    452 	return (rv);
    453 }
    454 
    455 Static usbd_status
    456 uvscom_readstat(struct uvscom_softc *sc)
    457 {
    458 	usb_device_request_t req;
    459 	usbd_status err;
    460 	uint16_t r;
    461 
    462 	DPRINTF(("%s: send readstat\n", USBDEVNAME(sc->sc_dev)));
    463 
    464 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    465 	req.bRequest = UVSCOM_READ_STATUS;
    466 	USETW(req.wValue, 0);
    467 	USETW(req.wIndex, 0);
    468 	USETW(req.wLength, 2);
    469 
    470 	err = usbd_do_request(sc->sc_udev, &req, &r);
    471 	if (err) {
    472 		printf("%s: uvscom_readstat: %s\n",
    473 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    474 		return (err);
    475 	}
    476 
    477 	DPRINTF(("%s: uvscom_readstat: r = %d\n",
    478 		 USBDEVNAME(sc->sc_dev), r));
    479 
    480 	return (USBD_NORMAL_COMPLETION);
    481 }
    482 
    483 Static usbd_status
    484 uvscom_shutdown(struct uvscom_softc *sc)
    485 {
    486 	usb_device_request_t req;
    487 	usbd_status err;
    488 
    489 	DPRINTF(("%s: send shutdown\n", USBDEVNAME(sc->sc_dev)));
    490 
    491 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    492 	req.bRequest = UVSCOM_SHUTDOWN;
    493 	USETW(req.wValue, 0);
    494 	USETW(req.wIndex, 0);
    495 	USETW(req.wLength, 0);
    496 
    497 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    498 	if (err) {
    499 		printf("%s: uvscom_shutdown: %s\n",
    500 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    501 		return (err);
    502 	}
    503 
    504 	return (USBD_NORMAL_COMPLETION);
    505 }
    506 
    507 Static usbd_status
    508 uvscom_reset(struct uvscom_softc *sc)
    509 {
    510 	DPRINTF(("%s: uvscom_reset\n", USBDEVNAME(sc->sc_dev)));
    511 
    512 	return (USBD_NORMAL_COMPLETION);
    513 }
    514 
    515 Static usbd_status
    516 uvscom_set_crtscts(struct uvscom_softc *sc)
    517 {
    518 	DPRINTF(("%s: uvscom_set_crtscts\n", USBDEVNAME(sc->sc_dev)));
    519 
    520 	return (USBD_NORMAL_COMPLETION);
    521 }
    522 
    523 Static usbd_status
    524 uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
    525 {
    526 	usb_device_request_t req;
    527 	usbd_status err;
    528 
    529 	DPRINTF(("%s: uvscom_set_line: %04x\n",
    530 		 USBDEVNAME(sc->sc_dev), line));
    531 
    532 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    533 	req.bRequest = UVSCOM_LINE_CTL;
    534 	USETW(req.wValue, line);
    535 	USETW(req.wIndex, 0);
    536 	USETW(req.wLength, 0);
    537 
    538 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    539 	if (err) {
    540 		printf("%s: uvscom_set_line: %s\n",
    541 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    542 		return (err);
    543 	}
    544 
    545 	return (USBD_NORMAL_COMPLETION);
    546 }
    547 
    548 Static usbd_status
    549 uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
    550 {
    551 	usb_device_request_t req;
    552 	usbd_status err;
    553 
    554 	DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
    555 		 USBDEVNAME(sc->sc_dev), lsp, ls));
    556 
    557 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    558 	req.bRequest = UVSCOM_SET_SPEED;
    559 	USETW(req.wValue, lsp);
    560 	USETW(req.wIndex, 0);
    561 	USETW(req.wLength, 0);
    562 
    563 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    564 	if (err) {
    565 		printf("%s: uvscom_set_line_coding: %s\n",
    566 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    567 		return (err);
    568 	}
    569 
    570 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    571 	req.bRequest = UVSCOM_SET_PARAM;
    572 	USETW(req.wValue, ls);
    573 	USETW(req.wIndex, 0);
    574 	USETW(req.wLength, 0);
    575 
    576 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    577 	if (err) {
    578 		printf("%s: uvscom_set_line_coding: %s\n",
    579 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    580 		return (err);
    581 	}
    582 
    583 	return (USBD_NORMAL_COMPLETION);
    584 }
    585 
    586 Static void
    587 uvscom_dtr(struct uvscom_softc *sc, int onoff)
    588 {
    589 	DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
    590 		 USBDEVNAME(sc->sc_dev), onoff));
    591 
    592 	if (sc->sc_dtr == onoff)
    593 		return;			/* no change */
    594 
    595 	sc->sc_dtr = onoff;
    596 
    597 	if (onoff)
    598 		SET(sc->sc_lcr, UVSCOM_DTR);
    599 	else
    600 		CLR(sc->sc_lcr, UVSCOM_DTR);
    601 
    602 	uvscom_set_line(sc, sc->sc_lcr);
    603 }
    604 
    605 Static void
    606 uvscom_rts(struct uvscom_softc *sc, int onoff)
    607 {
    608 	DPRINTF(("%s: uvscom_rts: onoff = %d\n",
    609 		 USBDEVNAME(sc->sc_dev), onoff));
    610 
    611 	if (sc->sc_rts == onoff)
    612 		return;			/* no change */
    613 
    614 	sc->sc_rts = onoff;
    615 
    616 	if (onoff)
    617 		SET(sc->sc_lcr, UVSCOM_RTS);
    618 	else
    619 		CLR(sc->sc_lcr, UVSCOM_RTS);
    620 
    621 	uvscom_set_line(sc, sc->sc_lcr);
    622 }
    623 
    624 Static void
    625 uvscom_break(struct uvscom_softc *sc, int onoff)
    626 {
    627 	DPRINTF(("%s: uvscom_break: onoff = %d\n",
    628 		 USBDEVNAME(sc->sc_dev), onoff));
    629 
    630 	if (onoff)
    631 		uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
    632 }
    633 
    634 Static void
    635 uvscom_set(void *addr, int portno, int reg, int onoff)
    636 {
    637 	struct uvscom_softc *sc = addr;
    638 
    639 	switch (reg) {
    640 	case UCOM_SET_DTR:
    641 		uvscom_dtr(sc, onoff);
    642 		break;
    643 	case UCOM_SET_RTS:
    644 		uvscom_rts(sc, onoff);
    645 		break;
    646 	case UCOM_SET_BREAK:
    647 		uvscom_break(sc, onoff);
    648 		break;
    649 	default:
    650 		break;
    651 	}
    652 }
    653 
    654 Static int
    655 uvscom_param(void *addr, int portno, struct termios *t)
    656 {
    657 	struct uvscom_softc *sc = addr;
    658 	usbd_status err;
    659 	uint16_t lsp;
    660 	uint16_t ls;
    661 
    662 	DPRINTF(("%s: uvscom_param: sc = %p\n",
    663 		 USBDEVNAME(sc->sc_dev), sc));
    664 
    665 	ls = 0;
    666 
    667 	switch (t->c_ospeed) {
    668 	case B150:
    669 		lsp = UVSCOM_SPEED_150BPS;
    670 		break;
    671 	case B300:
    672 		lsp = UVSCOM_SPEED_300BPS;
    673 		break;
    674 	case B600:
    675 		lsp = UVSCOM_SPEED_600BPS;
    676 		break;
    677 	case B1200:
    678 		lsp = UVSCOM_SPEED_1200BPS;
    679 		break;
    680 	case B2400:
    681 		lsp = UVSCOM_SPEED_2400BPS;
    682 		break;
    683 	case B4800:
    684 		lsp = UVSCOM_SPEED_4800BPS;
    685 		break;
    686 	case B9600:
    687 		lsp = UVSCOM_SPEED_9600BPS;
    688 		break;
    689 	case B19200:
    690 		lsp = UVSCOM_SPEED_19200BPS;
    691 		break;
    692 	case B38400:
    693 		lsp = UVSCOM_SPEED_38400BPS;
    694 		break;
    695 	case B57600:
    696 		lsp = UVSCOM_SPEED_57600BPS;
    697 		break;
    698 	case B115200:
    699 		lsp = UVSCOM_SPEED_115200BPS;
    700 		break;
    701 	default:
    702 		return (EIO);
    703 	}
    704 
    705 	if (ISSET(t->c_cflag, CSTOPB))
    706 		SET(ls, UVSCOM_STOP_BIT_2);
    707 	else
    708 		SET(ls, UVSCOM_STOP_BIT_1);
    709 
    710 	if (ISSET(t->c_cflag, PARENB)) {
    711 		if (ISSET(t->c_cflag, PARODD))
    712 			SET(ls, UVSCOM_PARITY_ODD);
    713 		else
    714 			SET(ls, UVSCOM_PARITY_EVEN);
    715 	} else
    716 		SET(ls, UVSCOM_PARITY_NONE);
    717 
    718 	switch (ISSET(t->c_cflag, CSIZE)) {
    719 	case CS5:
    720 		SET(ls, UVSCOM_DATA_BIT_5);
    721 		break;
    722 	case CS6:
    723 		SET(ls, UVSCOM_DATA_BIT_6);
    724 		break;
    725 	case CS7:
    726 		SET(ls, UVSCOM_DATA_BIT_7);
    727 		break;
    728 	case CS8:
    729 		SET(ls, UVSCOM_DATA_BIT_8);
    730 		break;
    731 	default:
    732 		return (EIO);
    733 	}
    734 
    735 	err = uvscom_set_line_coding(sc, lsp, ls);
    736 	if (err)
    737 		return (EIO);
    738 
    739 	if (ISSET(t->c_cflag, CRTSCTS)) {
    740 		err = uvscom_set_crtscts(sc);
    741 		if (err)
    742 			return (EIO);
    743 	}
    744 
    745 	return (0);
    746 }
    747 
    748 Static int
    749 uvscom_open(void *addr, int portno)
    750 {
    751 	struct uvscom_softc *sc = addr;
    752 	int err;
    753 	int i;
    754 
    755 	if (sc->sc_dying)
    756 		return (EIO);
    757 
    758 	DPRINTF(("uvscom_open: sc = %p\n", sc));
    759 
    760 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    761 		DPRINTF(("uvscom_open: open interrupt pipe.\n"));
    762 
    763 		sc->sc_usr = 0;		/* clear unit status */
    764 
    765 		err = uvscom_readstat(sc);
    766 		if (err) {
    767 			DPRINTF(("%s: uvscom_open: readstat faild\n",
    768 				 USBDEVNAME(sc->sc_dev)));
    769 			return (EIO);
    770 		}
    771 
    772 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    773 		err = usbd_open_pipe_intr(sc->sc_iface,
    774 					  sc->sc_intr_number,
    775 					  USBD_SHORT_XFER_OK,
    776 					  &sc->sc_intr_pipe,
    777 					  sc,
    778 					  sc->sc_intr_buf,
    779 					  sc->sc_isize,
    780 					  uvscom_intr,
    781 					  UVSCOM_INTR_INTERVAL);
    782 		if (err) {
    783 			printf("%s: cannot open interrupt pipe (addr %d)\n",
    784 				 USBDEVNAME(sc->sc_dev),
    785 				 sc->sc_intr_number);
    786 			return (EIO);
    787 		}
    788 	} else {
    789 		DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
    790 	}
    791 
    792 	if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
    793 		/* unit is not ready */
    794 
    795 		for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
    796 			tsleep(&err, TTIPRI, "uvsop", hz);	/* XXX */
    797 			if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
    798 				break;
    799 		}
    800 		if (i == 0) {
    801 			DPRINTF(("%s: unit is not ready\n",
    802 				 USBDEVNAME(sc->sc_dev)));
    803 			return (EIO);
    804 		}
    805 
    806 		/* check PC card was inserted */
    807 		if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
    808 			DPRINTF(("%s: no card\n",
    809 				 USBDEVNAME(sc->sc_dev)));
    810 			return (EIO);
    811 		}
    812 	}
    813 
    814 	return (0);
    815 }
    816 
    817 Static void
    818 uvscom_close(void *addr, int portno)
    819 {
    820 	struct uvscom_softc *sc = addr;
    821 	int err;
    822 
    823 	if (sc->sc_dying)
    824 		return;
    825 
    826 	DPRINTF(("uvscom_close: close\n"));
    827 
    828 	uvscom_shutdown(sc);
    829 
    830 	if (sc->sc_intr_pipe != NULL) {
    831 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    832 		if (err)
    833 			printf("%s: abort interrupt pipe failed: %s\n",
    834 				USBDEVNAME(sc->sc_dev),
    835 					   usbd_errstr(err));
    836 		err = usbd_close_pipe(sc->sc_intr_pipe);
    837 		if (err)
    838 			printf("%s: close interrupt pipe failed: %s\n",
    839 				USBDEVNAME(sc->sc_dev),
    840 					   usbd_errstr(err));
    841 		free(sc->sc_intr_buf, M_USBDEV);
    842 		sc->sc_intr_pipe = NULL;
    843 	}
    844 }
    845 
    846 Static void
    847 uvscom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    848 {
    849 	struct uvscom_softc *sc = priv;
    850 	u_char *buf = sc->sc_intr_buf;
    851 	u_char pstatus;
    852 
    853 	if (sc->sc_dying)
    854 		return;
    855 
    856 	if (status != USBD_NORMAL_COMPLETION) {
    857 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    858 			return;
    859 
    860 		printf("%s: uvscom_intr: abnormal status: %s\n",
    861 			USBDEVNAME(sc->sc_dev),
    862 			usbd_errstr(status));
    863 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    864 		return;
    865 	}
    866 
    867 	DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
    868 		 USBDEVNAME(sc->sc_dev), buf[0], buf[1]));
    869 
    870 	sc->sc_lsr = sc->sc_msr = 0;
    871 	sc->sc_usr = buf[1];
    872 
    873 	pstatus = buf[0];
    874 	if (ISSET(pstatus, UVSCOM_TXRDY))
    875 		SET(sc->sc_lsr, ULSR_TXRDY);
    876 	if (ISSET(pstatus, UVSCOM_RXRDY))
    877 		SET(sc->sc_lsr, ULSR_RXRDY);
    878 
    879 	pstatus = buf[1];
    880 	if (ISSET(pstatus, UVSCOM_CTS))
    881 		SET(sc->sc_msr, UMSR_CTS);
    882 	if (ISSET(pstatus, UVSCOM_DSR))
    883 		SET(sc->sc_msr, UMSR_DSR);
    884 	if (ISSET(pstatus, UVSCOM_DCD))
    885 		SET(sc->sc_msr, UMSR_DCD);
    886 
    887 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
    888 }
    889 
    890 Static void
    891 uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    892 {
    893 	struct uvscom_softc *sc = addr;
    894 
    895 	if (lsr != NULL)
    896 		*lsr = sc->sc_lsr;
    897 	if (msr != NULL)
    898 		*msr = sc->sc_msr;
    899 }
    900 
    901