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