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