Home | History | Annotate | Line # | Download | only in usb
uvscom.c revision 1.19.14.1
      1 /*	$NetBSD: uvscom.c,v 1.19.14.1 2007/06/18 14:17:37 itohy 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.19.14.1 2007/06/18 14:17:37 itohy 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 A4 */
    223 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4 },
    224 	/* SUNTAC U-Cable type D2 */
    225 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L },
    226 	/* SUNTAC U-Cable type P1 */
    227 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1 },
    228 	/* SUNTAC Slipper U  */
    229 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U },
    230 	/* SUNTAC Ir-Trinity */
    231 	{ USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U },
    232 };
    233 #define uvscom_lookup(v, p) usb_lookup(uvscom_devs, v, p)
    234 
    235 USB_DECLARE_DRIVER(uvscom);
    236 
    237 #ifdef __FreeBSD__
    238 Static device_probe_t uvscom_match;
    239 Static device_attach_t uvscom_attach;
    240 Static device_detach_t uvscom_detach;
    241 
    242 Static device_method_t uvscom_methods[] = {
    243 	/* Device interface */
    244 	DEVMETHOD(device_probe, uvscom_match),
    245 	DEVMETHOD(device_attach, uvscom_attach),
    246 	DEVMETHOD(device_detach, uvscom_detach),
    247 	{ 0, 0 }
    248 };
    249 
    250 Static driver_t uvscom_driver = {
    251 	"usio",
    252 	uvscom_methods,
    253 	sizeof (struct uvscom_softc)
    254 };
    255 
    256 DRIVER_MODULE(uvscom, uhub, uvscom_driver, ucom_devclass, usbd_driver_load, 0);
    257 MODULE_DEPEND(uvscom, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
    258 MODULE_VERSION(uvscom, UVSCOM_MODVER);
    259 #endif
    260 
    261 USB_MATCH(uvscom)
    262 {
    263 	USB_MATCH_START(uvscom, uaa);
    264 
    265 #ifndef USB_USE_IFATTACH
    266 	if (uaa->iface != NULL)
    267 		return (UMATCH_NONE);
    268 #endif /* USB_USE_IFATTACH */
    269 
    270 	return (uvscom_lookup(uaa->vendor, uaa->product) != NULL ?
    271 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    272 }
    273 
    274 USB_ATTACH(uvscom)
    275 {
    276 	USB_ATTACH_START(uvscom, sc, uaa);
    277 	usbd_device_handle dev = uaa->device;
    278 	usb_config_descriptor_t *cdesc;
    279 	usb_interface_descriptor_t *id;
    280 	usb_endpoint_descriptor_t *ed;
    281 	char *devinfop;
    282 	const char *devname = USBDEVNAME(sc->sc_dev);
    283 	usbd_status err;
    284 	int i;
    285 	struct ucom_attach_args uca;
    286 
    287 	devinfop = usbd_devinfo_alloc(dev, 0);
    288         USB_ATTACH_SETUP;
    289         printf("%s: %s\n", devname, devinfop);
    290 	usbd_devinfo_free(devinfop);
    291 
    292         sc->sc_udev = dev;
    293 
    294 	DPRINTF(("uvscom attach: sc = %p\n", sc));
    295 
    296 	/* initialize endpoints */
    297 	uca.bulkin = uca.bulkout = -1;
    298 	sc->sc_intr_number = -1;
    299 	sc->sc_intr_pipe = NULL;
    300 
    301 	/* Move the device into the configured state. */
    302 	err = usbd_set_config_index(dev, UVSCOM_CONFIG_INDEX, 1);
    303 	if (err) {
    304 		printf("%s: failed to set configuration, err=%s\n",
    305 			devname, usbd_errstr(err));
    306 		sc->sc_dying = 1;
    307 		USB_ATTACH_ERROR_RETURN;
    308 	}
    309 
    310 	/* get the config descriptor */
    311 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    312 
    313 	if (cdesc == NULL) {
    314 		printf("%s: failed to get configuration descriptor\n",
    315 			USBDEVNAME(sc->sc_dev));
    316 		sc->sc_dying = 1;
    317 		USB_ATTACH_ERROR_RETURN;
    318 	}
    319 
    320 	/* get the common interface */
    321 	err = usbd_device2interface_handle(dev, UVSCOM_IFACE_INDEX,
    322 					   &sc->sc_iface);
    323 	if (err) {
    324 		printf("%s: failed to get interface, err=%s\n",
    325 			devname, usbd_errstr(err));
    326 		sc->sc_dying = 1;
    327 		USB_ATTACH_ERROR_RETURN;
    328 	}
    329 
    330 	id = usbd_get_interface_descriptor(sc->sc_iface);
    331 	sc->sc_iface_number = id->bInterfaceNumber;
    332 
    333 	/* Find endpoints */
    334 	for (i = 0; i < id->bNumEndpoints; i++) {
    335 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    336 		if (ed == NULL) {
    337 			printf("%s: no endpoint descriptor for %d\n",
    338 				USBDEVNAME(sc->sc_dev), i);
    339 			sc->sc_dying = 1;
    340 			USB_ATTACH_ERROR_RETURN;
    341 		}
    342 
    343 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    344 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    345 			uca.bulkin = ed->bEndpointAddress;
    346 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    347 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    348 			uca.bulkout = ed->bEndpointAddress;
    349 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    350 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    351 			sc->sc_intr_number = ed->bEndpointAddress;
    352 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    353 		}
    354 	}
    355 
    356 	if (uca.bulkin == -1) {
    357 		printf("%s: Could not find data bulk in\n",
    358 			USBDEVNAME(sc->sc_dev));
    359 		sc->sc_dying = 1;
    360 		USB_ATTACH_ERROR_RETURN;
    361 	}
    362 	if (uca.bulkout == -1) {
    363 		printf("%s: Could not find data bulk out\n",
    364 			USBDEVNAME(sc->sc_dev));
    365 		sc->sc_dying = 1;
    366 		USB_ATTACH_ERROR_RETURN;
    367 	}
    368 	if (sc->sc_intr_number == -1) {
    369 		printf("%s: Could not find interrupt in\n",
    370 			USBDEVNAME(sc->sc_dev));
    371 		sc->sc_dying = 1;
    372 		USB_ATTACH_ERROR_RETURN;
    373 	}
    374 
    375 	sc->sc_dtr = sc->sc_rts = 0;
    376 	sc->sc_lcr = UVSCOM_LINE_INIT;
    377 
    378 	uca.portno = UCOM_UNK_PORTNO;
    379 	/* bulkin, bulkout set above */
    380 	uca.ibufsize = UVSCOMIBUFSIZE;
    381 	uca.obufsize = UVSCOMOBUFSIZE;
    382 	uca.ibufsizepad = UVSCOMIBUFSIZE;
    383 	uca.opkthdrlen = 0;
    384 	uca.device = dev;
    385 	uca.iface = sc->sc_iface;
    386 	uca.methods = &uvscom_methods;
    387 	uca.arg = sc;
    388 	uca.info = NULL;
    389 
    390 	err = uvscom_reset(sc);
    391 
    392 	if (err) {
    393 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    394 			usbd_errstr(err));
    395 		sc->sc_dying = 1;
    396 		USB_ATTACH_ERROR_RETURN;
    397 	}
    398 
    399 	DPRINTF(("uvscom: in = 0x%x out = 0x%x intr = 0x%x\n",
    400 		 uca.bulkin, uca.bulkout, sc->sc_intr_number));
    401 
    402 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    403 			   USBDEV(sc->sc_dev));
    404 
    405 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
    406 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    407 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    408 					    ucomprint, ucomsubmatch);
    409 
    410 	USB_ATTACH_SUCCESS_RETURN;
    411 }
    412 
    413 USB_DETACH(uvscom)
    414 {
    415 	USB_DETACH_START(uvscom, sc);
    416 	int rv = 0;
    417 
    418 	DPRINTF(("uvscom_detach: sc = %p\n", sc));
    419 
    420 	sc->sc_dying = 1;
    421 
    422 	if (sc->sc_intr_pipe != NULL) {
    423 		usbd_abort_pipe(sc->sc_intr_pipe);
    424 		usbd_close_pipe(sc->sc_intr_pipe);
    425 		free(sc->sc_intr_buf, M_USBDEV);
    426 		sc->sc_intr_pipe = NULL;
    427 	}
    428 
    429 	sc->sc_dying = 1;
    430 	if (sc->sc_subdev != NULL) {
    431 		rv = config_detach(sc->sc_subdev, flags);
    432 		sc->sc_subdev = NULL;
    433 	}
    434 
    435 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    436 			   USBDEV(sc->sc_dev));
    437 
    438 	return (rv);
    439 }
    440 
    441 int
    442 uvscom_activate(device_ptr_t self, enum devact act)
    443 {
    444 	struct uvscom_softc *sc = (struct uvscom_softc *)self;
    445 	int rv = 0;
    446 
    447 	switch (act) {
    448 	case DVACT_ACTIVATE:
    449 		return (EOPNOTSUPP);
    450 
    451 	case DVACT_DEACTIVATE:
    452 		if (sc->sc_subdev != NULL)
    453 			rv = config_deactivate(sc->sc_subdev);
    454 		sc->sc_dying = 1;
    455 		break;
    456 	}
    457 	return (rv);
    458 }
    459 
    460 Static usbd_status
    461 uvscom_readstat(struct uvscom_softc *sc)
    462 {
    463 	usb_device_request_t req;
    464 	usbd_status err;
    465 	uint16_t r;
    466 
    467 	DPRINTF(("%s: send readstat\n", USBDEVNAME(sc->sc_dev)));
    468 
    469 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    470 	req.bRequest = UVSCOM_READ_STATUS;
    471 	USETW(req.wValue, 0);
    472 	USETW(req.wIndex, 0);
    473 	USETW(req.wLength, 2);
    474 
    475 	err = usbd_do_request(sc->sc_udev, &req, &r);
    476 	if (err) {
    477 		printf("%s: uvscom_readstat: %s\n",
    478 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    479 		return (err);
    480 	}
    481 
    482 	DPRINTF(("%s: uvscom_readstat: r = %d\n",
    483 		 USBDEVNAME(sc->sc_dev), r));
    484 
    485 	return (USBD_NORMAL_COMPLETION);
    486 }
    487 
    488 Static usbd_status
    489 uvscom_shutdown(struct uvscom_softc *sc)
    490 {
    491 	usb_device_request_t req;
    492 	usbd_status err;
    493 
    494 	DPRINTF(("%s: send shutdown\n", USBDEVNAME(sc->sc_dev)));
    495 
    496 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    497 	req.bRequest = UVSCOM_SHUTDOWN;
    498 	USETW(req.wValue, 0);
    499 	USETW(req.wIndex, 0);
    500 	USETW(req.wLength, 0);
    501 
    502 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    503 	if (err) {
    504 		printf("%s: uvscom_shutdown: %s\n",
    505 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    506 		return (err);
    507 	}
    508 
    509 	return (USBD_NORMAL_COMPLETION);
    510 }
    511 
    512 Static usbd_status
    513 uvscom_reset(struct uvscom_softc *sc)
    514 {
    515 	DPRINTF(("%s: uvscom_reset\n", USBDEVNAME(sc->sc_dev)));
    516 
    517 	return (USBD_NORMAL_COMPLETION);
    518 }
    519 
    520 Static usbd_status
    521 uvscom_set_crtscts(struct uvscom_softc *sc)
    522 {
    523 	DPRINTF(("%s: uvscom_set_crtscts\n", USBDEVNAME(sc->sc_dev)));
    524 
    525 	return (USBD_NORMAL_COMPLETION);
    526 }
    527 
    528 Static usbd_status
    529 uvscom_set_line(struct uvscom_softc *sc, uint16_t line)
    530 {
    531 	usb_device_request_t req;
    532 	usbd_status err;
    533 
    534 	DPRINTF(("%s: uvscom_set_line: %04x\n",
    535 		 USBDEVNAME(sc->sc_dev), line));
    536 
    537 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    538 	req.bRequest = UVSCOM_LINE_CTL;
    539 	USETW(req.wValue, line);
    540 	USETW(req.wIndex, 0);
    541 	USETW(req.wLength, 0);
    542 
    543 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    544 	if (err) {
    545 		printf("%s: uvscom_set_line: %s\n",
    546 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    547 		return (err);
    548 	}
    549 
    550 	return (USBD_NORMAL_COMPLETION);
    551 }
    552 
    553 Static usbd_status
    554 uvscom_set_line_coding(struct uvscom_softc *sc, uint16_t lsp, uint16_t ls)
    555 {
    556 	usb_device_request_t req;
    557 	usbd_status err;
    558 
    559 	DPRINTF(("%s: uvscom_set_line_coding: %02x %02x\n",
    560 		 USBDEVNAME(sc->sc_dev), lsp, ls));
    561 
    562 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    563 	req.bRequest = UVSCOM_SET_SPEED;
    564 	USETW(req.wValue, lsp);
    565 	USETW(req.wIndex, 0);
    566 	USETW(req.wLength, 0);
    567 
    568 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    569 	if (err) {
    570 		printf("%s: uvscom_set_line_coding: %s\n",
    571 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    572 		return (err);
    573 	}
    574 
    575 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    576 	req.bRequest = UVSCOM_SET_PARAM;
    577 	USETW(req.wValue, ls);
    578 	USETW(req.wIndex, 0);
    579 	USETW(req.wLength, 0);
    580 
    581 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    582 	if (err) {
    583 		printf("%s: uvscom_set_line_coding: %s\n",
    584 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    585 		return (err);
    586 	}
    587 
    588 	return (USBD_NORMAL_COMPLETION);
    589 }
    590 
    591 Static void
    592 uvscom_dtr(struct uvscom_softc *sc, int onoff)
    593 {
    594 	DPRINTF(("%s: uvscom_dtr: onoff = %d\n",
    595 		 USBDEVNAME(sc->sc_dev), onoff));
    596 
    597 	if (sc->sc_dtr == onoff)
    598 		return;			/* no change */
    599 
    600 	sc->sc_dtr = onoff;
    601 
    602 	if (onoff)
    603 		SET(sc->sc_lcr, UVSCOM_DTR);
    604 	else
    605 		CLR(sc->sc_lcr, UVSCOM_DTR);
    606 
    607 	uvscom_set_line(sc, sc->sc_lcr);
    608 }
    609 
    610 Static void
    611 uvscom_rts(struct uvscom_softc *sc, int onoff)
    612 {
    613 	DPRINTF(("%s: uvscom_rts: onoff = %d\n",
    614 		 USBDEVNAME(sc->sc_dev), onoff));
    615 
    616 	if (sc->sc_rts == onoff)
    617 		return;			/* no change */
    618 
    619 	sc->sc_rts = onoff;
    620 
    621 	if (onoff)
    622 		SET(sc->sc_lcr, UVSCOM_RTS);
    623 	else
    624 		CLR(sc->sc_lcr, UVSCOM_RTS);
    625 
    626 	uvscom_set_line(sc, sc->sc_lcr);
    627 }
    628 
    629 Static void
    630 uvscom_break(struct uvscom_softc *sc, int onoff)
    631 {
    632 	DPRINTF(("%s: uvscom_break: onoff = %d\n",
    633 		 USBDEVNAME(sc->sc_dev), onoff));
    634 
    635 	if (onoff)
    636 		uvscom_set_line(sc, SET(sc->sc_lcr, UVSCOM_BREAK));
    637 }
    638 
    639 Static void
    640 uvscom_set(void *addr, int portno, int reg, int onoff)
    641 {
    642 	struct uvscom_softc *sc = addr;
    643 
    644 	switch (reg) {
    645 	case UCOM_SET_DTR:
    646 		uvscom_dtr(sc, onoff);
    647 		break;
    648 	case UCOM_SET_RTS:
    649 		uvscom_rts(sc, onoff);
    650 		break;
    651 	case UCOM_SET_BREAK:
    652 		uvscom_break(sc, onoff);
    653 		break;
    654 	default:
    655 		break;
    656 	}
    657 }
    658 
    659 Static int
    660 uvscom_param(void *addr, int portno, struct termios *t)
    661 {
    662 	struct uvscom_softc *sc = addr;
    663 	usbd_status err;
    664 	uint16_t lsp;
    665 	uint16_t ls;
    666 
    667 	DPRINTF(("%s: uvscom_param: sc = %p\n",
    668 		 USBDEVNAME(sc->sc_dev), sc));
    669 
    670 	ls = 0;
    671 
    672 	switch (t->c_ospeed) {
    673 	case B150:
    674 		lsp = UVSCOM_SPEED_150BPS;
    675 		break;
    676 	case B300:
    677 		lsp = UVSCOM_SPEED_300BPS;
    678 		break;
    679 	case B600:
    680 		lsp = UVSCOM_SPEED_600BPS;
    681 		break;
    682 	case B1200:
    683 		lsp = UVSCOM_SPEED_1200BPS;
    684 		break;
    685 	case B2400:
    686 		lsp = UVSCOM_SPEED_2400BPS;
    687 		break;
    688 	case B4800:
    689 		lsp = UVSCOM_SPEED_4800BPS;
    690 		break;
    691 	case B9600:
    692 		lsp = UVSCOM_SPEED_9600BPS;
    693 		break;
    694 	case B19200:
    695 		lsp = UVSCOM_SPEED_19200BPS;
    696 		break;
    697 	case B38400:
    698 		lsp = UVSCOM_SPEED_38400BPS;
    699 		break;
    700 	case B57600:
    701 		lsp = UVSCOM_SPEED_57600BPS;
    702 		break;
    703 	case B115200:
    704 		lsp = UVSCOM_SPEED_115200BPS;
    705 		break;
    706 	default:
    707 		return (EIO);
    708 	}
    709 
    710 	if (ISSET(t->c_cflag, CSTOPB))
    711 		SET(ls, UVSCOM_STOP_BIT_2);
    712 	else
    713 		SET(ls, UVSCOM_STOP_BIT_1);
    714 
    715 	if (ISSET(t->c_cflag, PARENB)) {
    716 		if (ISSET(t->c_cflag, PARODD))
    717 			SET(ls, UVSCOM_PARITY_ODD);
    718 		else
    719 			SET(ls, UVSCOM_PARITY_EVEN);
    720 	} else
    721 		SET(ls, UVSCOM_PARITY_NONE);
    722 
    723 	switch (ISSET(t->c_cflag, CSIZE)) {
    724 	case CS5:
    725 		SET(ls, UVSCOM_DATA_BIT_5);
    726 		break;
    727 	case CS6:
    728 		SET(ls, UVSCOM_DATA_BIT_6);
    729 		break;
    730 	case CS7:
    731 		SET(ls, UVSCOM_DATA_BIT_7);
    732 		break;
    733 	case CS8:
    734 		SET(ls, UVSCOM_DATA_BIT_8);
    735 		break;
    736 	default:
    737 		return (EIO);
    738 	}
    739 
    740 	err = uvscom_set_line_coding(sc, lsp, ls);
    741 	if (err)
    742 		return (EIO);
    743 
    744 	if (ISSET(t->c_cflag, CRTSCTS)) {
    745 		err = uvscom_set_crtscts(sc);
    746 		if (err)
    747 			return (EIO);
    748 	}
    749 
    750 	return (0);
    751 }
    752 
    753 Static int
    754 uvscom_open(void *addr, int portno)
    755 {
    756 	struct uvscom_softc *sc = addr;
    757 	int err;
    758 	int i;
    759 
    760 	if (sc->sc_dying)
    761 		return (EIO);
    762 
    763 	DPRINTF(("uvscom_open: sc = %p\n", sc));
    764 
    765 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    766 		DPRINTF(("uvscom_open: open interrupt pipe.\n"));
    767 
    768 		sc->sc_usr = 0;		/* clear unit status */
    769 
    770 		err = uvscom_readstat(sc);
    771 		if (err) {
    772 			DPRINTF(("%s: uvscom_open: readstat faild\n",
    773 				 USBDEVNAME(sc->sc_dev)));
    774 			return (EIO);
    775 		}
    776 
    777 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    778 		err = usbd_open_pipe_intr(sc->sc_iface,
    779 					  sc->sc_intr_number,
    780 					  USBD_SHORT_XFER_OK,
    781 					  &sc->sc_intr_pipe,
    782 					  sc,
    783 					  sc->sc_intr_buf,
    784 					  sc->sc_isize,
    785 					  uvscom_intr,
    786 					  UVSCOM_INTR_INTERVAL);
    787 		if (err) {
    788 			printf("%s: cannot open interrupt pipe (addr %d)\n",
    789 				 USBDEVNAME(sc->sc_dev),
    790 				 sc->sc_intr_number);
    791 			return (EIO);
    792 		}
    793 	} else {
    794 		DPRINTF(("uvscom_open: did not open interrupt pipe.\n"));
    795 	}
    796 
    797 	if ((sc->sc_usr & UVSCOM_USTAT_MASK) == 0) {
    798 		/* unit is not ready */
    799 
    800 		for (i = UVSCOM_UNIT_WAIT; i > 0; --i) {
    801 			tsleep(&err, TTIPRI, "uvsop", hz);	/* XXX */
    802 			if (ISSET(sc->sc_usr, UVSCOM_USTAT_MASK))
    803 				break;
    804 		}
    805 		if (i == 0) {
    806 			DPRINTF(("%s: unit is not ready\n",
    807 				 USBDEVNAME(sc->sc_dev)));
    808 			return (EIO);
    809 		}
    810 
    811 		/* check PC card was inserted */
    812 		if (ISSET(sc->sc_usr, UVSCOM_NOCARD)) {
    813 			DPRINTF(("%s: no card\n",
    814 				 USBDEVNAME(sc->sc_dev)));
    815 			return (EIO);
    816 		}
    817 	}
    818 
    819 	return (0);
    820 }
    821 
    822 Static void
    823 uvscom_close(void *addr, int portno)
    824 {
    825 	struct uvscom_softc *sc = addr;
    826 	int err;
    827 
    828 	if (sc->sc_dying)
    829 		return;
    830 
    831 	DPRINTF(("uvscom_close: close\n"));
    832 
    833 	uvscom_shutdown(sc);
    834 
    835 	if (sc->sc_intr_pipe != NULL) {
    836 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    837 		if (err)
    838 			printf("%s: abort interrupt pipe failed: %s\n",
    839 				USBDEVNAME(sc->sc_dev),
    840 					   usbd_errstr(err));
    841 		err = usbd_close_pipe(sc->sc_intr_pipe);
    842 		if (err)
    843 			printf("%s: close interrupt pipe failed: %s\n",
    844 				USBDEVNAME(sc->sc_dev),
    845 					   usbd_errstr(err));
    846 		free(sc->sc_intr_buf, M_USBDEV);
    847 		sc->sc_intr_pipe = NULL;
    848 	}
    849 }
    850 
    851 Static void
    852 uvscom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
    853     usbd_status status)
    854 {
    855 	struct uvscom_softc *sc = priv;
    856 	u_char *buf = sc->sc_intr_buf;
    857 	u_char pstatus;
    858 
    859 	if (sc->sc_dying)
    860 		return;
    861 
    862 	if (status != USBD_NORMAL_COMPLETION) {
    863 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    864 			return;
    865 
    866 		printf("%s: uvscom_intr: abnormal status: %s\n",
    867 			USBDEVNAME(sc->sc_dev),
    868 			usbd_errstr(status));
    869 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    870 		return;
    871 	}
    872 
    873 	DPRINTFN(2, ("%s: uvscom status = %02x %02x\n",
    874 		 USBDEVNAME(sc->sc_dev), buf[0], buf[1]));
    875 
    876 	sc->sc_lsr = sc->sc_msr = 0;
    877 	sc->sc_usr = buf[1];
    878 
    879 	pstatus = buf[0];
    880 	if (ISSET(pstatus, UVSCOM_TXRDY))
    881 		SET(sc->sc_lsr, ULSR_TXRDY);
    882 	if (ISSET(pstatus, UVSCOM_RXRDY))
    883 		SET(sc->sc_lsr, ULSR_RXRDY);
    884 
    885 	pstatus = buf[1];
    886 	if (ISSET(pstatus, UVSCOM_CTS))
    887 		SET(sc->sc_msr, UMSR_CTS);
    888 	if (ISSET(pstatus, UVSCOM_DSR))
    889 		SET(sc->sc_msr, UMSR_DSR);
    890 	if (ISSET(pstatus, UVSCOM_DCD))
    891 		SET(sc->sc_msr, UMSR_DCD);
    892 
    893 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
    894 }
    895 
    896 Static void
    897 uvscom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    898 {
    899 	struct uvscom_softc *sc = addr;
    900 
    901 	if (lsr != NULL)
    902 		*lsr = sc->sc_lsr;
    903 	if (msr != NULL)
    904 		*msr = sc->sc_msr;
    905 }
    906 
    907