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