Home | History | Annotate | Line # | Download | only in usb
ubsa.c revision 1.1
      1 /*	$NetBSD: ubsa.c,v 1.1 2002/10/08 13:08:02 augustss Exp $	*/
      2 /*-
      3  * Copyright (c) 2002, Alexander Kabaev <kan.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 /*
     28  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     29  * All rights reserved.
     30  *
     31  * This code is derived from software contributed to The NetBSD Foundation
     32  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
     33  *
     34  * Redistribution and use in source and binary forms, with or without
     35  * modification, are permitted provided that the following conditions
     36  * are met:
     37  * 1. Redistributions of source code must retain the above copyright
     38  *    notice, this list of conditions and the following disclaimer.
     39  * 2. Redistributions in binary form must reproduce the above copyright
     40  *    notice, this list of conditions and the following disclaimer in the
     41  *    documentation and/or other materials provided with the distribution.
     42  * 3. All advertising materials mentioning features or use of this software
     43  *    must display the following acknowledgement:
     44  *        This product includes software developed by the NetBSD
     45  *        Foundation, Inc. and its contributors.
     46  * 4. Neither the name of The NetBSD Foundation nor the names of its
     47  *    contributors may be used to endorse or promote products derived
     48  *    from this software without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     60  * POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/kernel.h>
     68 #include <sys/malloc.h>
     69 #ifdef __FreeBSD__
     70 #include <sys/bus.h>
     71 #endif
     72 #include <sys/ioccom.h>
     73 #include <sys/fcntl.h>
     74 #include <sys/conf.h>
     75 #include <sys/tty.h>
     76 #include <sys/file.h>
     77 #if __FreeBSD_version >= 500014
     78 #include <sys/selinfo.h>
     79 #else
     80 #include <sys/select.h>
     81 #endif
     82 #include <sys/proc.h>
     83 #include <sys/vnode.h>
     84 #include <sys/poll.h>
     85 #include <sys/sysctl.h>
     86 
     87 #include <dev/usb/usb.h>
     88 #include <dev/usb/usbcdc.h>
     89 
     90 #include <dev/usb/usbdi.h>
     91 #include <dev/usb/usbdi_util.h>
     92 #include <dev/usb/usbdevs.h>
     93 #include <dev/usb/usb_quirks.h>
     94 
     95 #include <dev/usb/ucomvar.h>
     96 
     97 #ifdef UBSA_DEBUG
     98 Static int	ubsadebug = 0;
     99 #ifdef __FreeBSD__
    100 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
    101 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
    102 	   &ubsadebug, 0, "ubsa debug level");
    103 #endif
    104 
    105 #define	DPRINTFN(n, x)	do { \
    106 				if (ubsadebug > (n)) \
    107 					logprintf x; \
    108 			} while (0)
    109 #else
    110 #define	DPRINTFN(n, x)
    111 #endif
    112 #define	DPRINTF(x) DPRINTFN(0, x)
    113 
    114 #define	UBSA_MODVER		1	/* module version */
    115 
    116 #define	UBSA_CONFIG_INDEX	1
    117 #define	UBSA_IFACE_INDEX	0
    118 
    119 #define	UBSA_INTR_INTERVAL	100	/* ms */
    120 
    121 #define	UBSA_SET_BAUDRATE  	0x00
    122 #define	UBSA_SET_STOP_BITS	0x01
    123 #define	UBSA_SET_DATA_BITS	0x02
    124 #define	UBSA_SET_PARITY		0x03
    125 #define	UBSA_SET_DTR		0x0A
    126 #define	UBSA_SET_RTS		0x0B
    127 #define	UBSA_SET_BREAK		0x0C
    128 #define	UBSA_SET_FLOW_CTRL	0x10
    129 
    130 #define	UBSA_PARITY_NONE	0x00
    131 #define	UBSA_PARITY_EVEN	0x01
    132 #define	UBSA_PARITY_ODD		0x02
    133 #define	UBSA_PARITY_MARK	0x03
    134 #define	UBSA_PARITY_SPACE	0x04
    135 
    136 #define	UBSA_FLOW_NONE		0x0000
    137 #define	UBSA_FLOW_OCTS		0x0001
    138 #define	UBSA_FLOW_ODSR		0x0002
    139 #define	UBSA_FLOW_IDSR		0x0004
    140 #define	UBSA_FLOW_IDTR		0x0008
    141 #define	UBSA_FLOW_IRTS		0x0010
    142 #define	UBSA_FLOW_ORTS		0x0020
    143 #define	UBSA_FLOW_UNKNOWN	0x0040
    144 #define	UBSA_FLOW_OXON		0x0080
    145 #define	UBSA_FLOW_IXON		0x0100
    146 
    147 /* line status register */
    148 #define	UBSA_LSR_TSRE		0x40	/* Transmitter empty: byte sent */
    149 #define	UBSA_LSR_TXRDY		0x20	/* Transmitter buffer empty */
    150 #define	UBSA_LSR_BI		0x10	/* Break detected */
    151 #define	UBSA_LSR_FE		0x08	/* Framing error: bad stop bit */
    152 #define	UBSA_LSR_PE		0x04	/* Parity error */
    153 #define	UBSA_LSR_OE		0x02	/* Overrun, lost incoming byte */
    154 #define	UBSA_LSR_RXRDY		0x01	/* Byte ready in Receive Buffer */
    155 #define	UBSA_LSR_RCV_MASK	0x1f	/* Mask for incoming data or error */
    156 
    157 /* modem status register */
    158 /* All deltas are from the last read of the MSR. */
    159 #define	UBSA_MSR_DCD		0x80	/* Current Data Carrier Detect */
    160 #define	UBSA_MSR_RI		0x40	/* Current Ring Indicator */
    161 #define	UBSA_MSR_DSR		0x20	/* Current Data Set Ready */
    162 #define	UBSA_MSR_CTS		0x10	/* Current Clear to Send */
    163 #define	UBSA_MSR_DDCD		0x08	/* DCD has changed state */
    164 #define	UBSA_MSR_TERI		0x04	/* RI has toggled low to high */
    165 #define	UBSA_MSR_DDSR		0x02	/* DSR has changed state */
    166 #define	UBSA_MSR_DCTS		0x01	/* CTS has changed state */
    167 
    168 struct	ubsa_softc {
    169 	USBBASEDEVICE		sc_dev;		/* base device */
    170 	usbd_device_handle	sc_udev;	/* USB device */
    171 	usbd_interface_handle	sc_iface;	/* interface */
    172 
    173 	int			sc_iface_number;	/* interface number */
    174 
    175 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
    176 	int			sc_intr_number;	/* interrupt number */
    177 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
    178 	u_char			*sc_intr_buf;	/* interrupt buffer */
    179 	int			sc_isize;
    180 
    181 	u_char			sc_dtr;		/* current DTR state */
    182 	u_char			sc_rts;		/* current RTS state */
    183 
    184 	u_char			sc_lsr;		/* Local status register */
    185 	u_char			sc_msr;		/* ubsa status register */
    186 
    187 	device_ptr_t		sc_subdev;	/* ucom device */
    188 
    189 	u_char			sc_dying;	/* disconnecting */
    190 
    191 };
    192 
    193 Static	void ubsa_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    194 
    195 Static	void ubsa_get_status(void *, int, u_char *, u_char *);
    196 Static	void ubsa_set(void *, int, int, int);
    197 Static	int  ubsa_param(void *, int, struct termios *);
    198 Static	int  ubsa_open(void *, int);
    199 Static	void ubsa_close(void *, int);
    200 
    201 Static  void ubsa_break(struct ubsa_softc *sc, int onoff);
    202 Static	int  ubsa_request(struct ubsa_softc *, u_int8_t, u_int16_t);
    203 Static	void ubsa_dtr(struct ubsa_softc *, int);
    204 Static	void ubsa_rts(struct ubsa_softc *, int);
    205 Static	void ubsa_baudrate(struct ubsa_softc *, speed_t);
    206 Static	void ubsa_parity(struct ubsa_softc *, tcflag_t);
    207 Static	void ubsa_databits(struct ubsa_softc *, tcflag_t);
    208 Static	void ubsa_stopbits(struct ubsa_softc *, tcflag_t);
    209 Static	void ubsa_flow(struct ubsa_softc *, tcflag_t, tcflag_t);
    210 
    211 struct	ucom_methods ubsa_methods = {
    212 	ubsa_get_status,
    213 	ubsa_set,
    214 	ubsa_param,
    215 	NULL,
    216 	ubsa_open,
    217 	ubsa_close,
    218 	NULL,
    219 	NULL
    220 };
    221 
    222 Static const struct usb_devno ubsa_devs[] = {
    223 	/* BELKIN F5U103 */
    224 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
    225 	/* BELKIN F5U120 */
    226 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
    227 	/* GoHubs GO-COM232 */
    228 	{ USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
    229 	/* GoHubs GO-COM232 */
    230 	{ USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
    231 	/* Peracom */
    232 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
    233 };
    234 #define ubsa_lookup(v, p) usb_lookup(ubsa_devs, v, p)
    235 
    236 USB_DECLARE_DRIVER(ubsa);
    237 
    238 USB_MATCH(ubsa)
    239 {
    240 	USB_MATCH_START(ubsa, uaa);
    241 
    242 	if (uaa->iface != NULL)
    243 		return (UMATCH_NONE);
    244 
    245 	return (ubsa_lookup(uaa->vendor, uaa->product) != NULL ?
    246 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    247 }
    248 
    249 USB_ATTACH(ubsa)
    250 {
    251 	USB_ATTACH_START(ubsa, sc, uaa);
    252 	usbd_device_handle dev = uaa->device;
    253 	usb_config_descriptor_t *cdesc;
    254 	usb_interface_descriptor_t *id;
    255 	usb_endpoint_descriptor_t *ed;
    256 	char devinfo[1024];
    257 	const char *devname = USBDEVNAME(sc->sc_dev);
    258 	usbd_status err;
    259 	struct ucom_attach_args uca;
    260 	int i;
    261 
    262         usbd_devinfo(dev, 0, devinfo);
    263         USB_ATTACH_SETUP;
    264         printf("%s: %s\n", devname, devinfo);
    265 
    266         sc->sc_udev = dev;
    267 
    268 	/*
    269 	 * initialize rts, dtr variables to something
    270 	 * different from boolean 0, 1
    271 	 */
    272 	sc->sc_dtr = -1;
    273 	sc->sc_rts = -1;
    274 
    275 	printf("%s: %s\n", devname, devinfo);
    276 
    277 	DPRINTF(("ubsa attach: sc = %p\n", sc));
    278 
    279 	/* initialize endpoints */
    280 	uca.bulkin = uca.bulkout = -1;
    281 	sc->sc_intr_number = -1;
    282 	sc->sc_intr_pipe = NULL;
    283 
    284 	/* Move the device into the configured state. */
    285 	err = usbd_set_config_index(dev, UBSA_CONFIG_INDEX, 1);
    286 	if (err) {
    287 		printf("%s: failed to set configuration: %s\n",
    288 		    devname, usbd_errstr(err));
    289 		sc->sc_dying = 1;
    290 		goto error;
    291 	}
    292 
    293 	/* get the config descriptor */
    294 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    295 
    296 	if (cdesc == NULL) {
    297 		printf("%s: failed to get configuration descriptor\n",
    298 		    devname);
    299 		sc->sc_dying = 1;
    300 		goto error;
    301 	}
    302 
    303 	/* get the first interface */
    304 	err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX,
    305 	    &sc->sc_iface);
    306 	if (err) {
    307 		printf("%s: failed to get interface: %s\n",
    308 			devname, usbd_errstr(err));
    309 		sc->sc_dying = 1;
    310 		goto error;
    311 	}
    312 
    313 	/* Find the endpoints */
    314 
    315 	id = usbd_get_interface_descriptor(sc->sc_iface);
    316 	sc->sc_iface_number = id->bInterfaceNumber;
    317 
    318 	for (i = 0; i < id->bNumEndpoints; i++) {
    319 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    320 		if (ed == NULL) {
    321 			printf("%s: no endpoint descriptor for %d\n",
    322 			    USBDEVNAME(sc->sc_dev), i);
    323 			sc->sc_dying = 1;
    324 			goto error;
    325 		}
    326 
    327 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    328 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    329 			sc->sc_intr_number = ed->bEndpointAddress;
    330 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    331 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    332 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    333 			uca.bulkin = ed->bEndpointAddress;
    334 			uca.ibufsize = UGETW(ed->wMaxPacketSize);
    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 			uca.obufsize = UGETW(ed->wMaxPacketSize);
    339 		}
    340 	}
    341 
    342 	if (sc->sc_intr_number == -1) {
    343 		printf("%s: Could not find interrupt in\n", devname);
    344 		sc->sc_dying = 1;
    345 		goto error;
    346 	}
    347 
    348 	if (uca.bulkin == -1) {
    349 		printf("%s: Could not find data bulk in\n", devname);
    350 		sc->sc_dying = 1;
    351 		goto error;
    352 	}
    353 
    354 	if (uca.bulkout == -1) {
    355 		printf("%s: Could not find data bulk out\n", devname);
    356 		sc->sc_dying = 1;
    357 		goto error;
    358 	}
    359 
    360 	uca.portno = UCOM_UNK_PORTNO;
    361 	/* bulkin, bulkout set above */
    362 	uca.ibufsizepad = uca.ibufsize;
    363 	uca.opkthdrlen = 0;
    364 	uca.device = dev;
    365 	uca.iface = sc->sc_iface;
    366 	uca.methods = &ubsa_methods;
    367 	uca.arg = sc;
    368 	uca.info = NULL;
    369 
    370 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    371 			   USBDEV(sc->sc_dev));
    372 
    373 	DPRINTF(("ubsa: in = 0x%x, out = 0x%x, intr = 0x%x\n",
    374 	    uca.bulkin_no, uca.bulkout_no, sc->sc_intr_number));
    375 
    376 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    377 
    378 	USB_ATTACH_SUCCESS_RETURN;
    379 
    380 error:
    381 	free(devinfo, M_USBDEV);
    382 	USB_ATTACH_ERROR_RETURN;
    383 }
    384 
    385 USB_DETACH(ubsa)
    386 {
    387 	USB_DETACH_START(ubsa, sc);
    388 	int rv;
    389 
    390 
    391 	DPRINTF(("ubsa_detach: sc = %p\n", sc));
    392 
    393 	if (sc->sc_intr_pipe != NULL) {
    394 		usbd_abort_pipe(sc->sc_intr_pipe);
    395 		usbd_close_pipe(sc->sc_intr_pipe);
    396 		free(sc->sc_intr_buf, M_USBDEV);
    397 		sc->sc_intr_pipe = NULL;
    398 	}
    399 
    400 	sc->sc_dying = 1;
    401 	if (sc->sc_subdev != NULL) {
    402 		rv = config_detach(sc->sc_subdev, flags);
    403 		sc->sc_subdev = NULL;
    404 	}
    405 
    406 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    407 			   USBDEV(sc->sc_dev));
    408 
    409 	return (rv);
    410 }
    411 
    412 Static int
    413 ubsa_activate(device_ptr_t self, enum devact act)
    414 {
    415 	struct ubsa_softc *sc = (struct ubsa_softc *)self;
    416 	int rv = 0;
    417 
    418 	switch (act) {
    419 	case DVACT_ACTIVATE:
    420 		return (EOPNOTSUPP);
    421 
    422 	case DVACT_DEACTIVATE:
    423 		if (sc->sc_subdev != NULL)
    424 			rv = config_deactivate(sc->sc_subdev);
    425 		sc->sc_dying = 1;
    426 		break;
    427 	}
    428 	return (rv);
    429 }
    430 
    431 Static int
    432 ubsa_request(struct ubsa_softc *sc, u_int8_t request, u_int16_t value)
    433 {
    434 	usb_device_request_t req;
    435 	usbd_status err;
    436 
    437 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    438 	req.bRequest = request;
    439 	USETW(req.wValue, value);
    440 	USETW(req.wIndex, sc->sc_iface_number);
    441 	USETW(req.wLength, 0);
    442 
    443 	err = usbd_do_request(sc->sc_udev, &req, 0);
    444 	if (err)
    445 		printf("%s: ubsa_request: %s\n",
    446 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    447 	return (err);
    448 }
    449 
    450 Static void
    451 ubsa_dtr(struct ubsa_softc *sc, int onoff)
    452 {
    453 
    454 	DPRINTF(("ubsa_dtr: onoff = %d\n", onoff));
    455 
    456 	if (sc->sc_dtr == onoff)
    457 		return;
    458 	sc->sc_dtr = onoff;
    459 
    460 	ubsa_request(sc, UBSA_SET_DTR, onoff ? 1 : 0);
    461 }
    462 
    463 Static void
    464 ubsa_rts(struct ubsa_softc *sc, int onoff)
    465 {
    466 
    467 	DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
    468 
    469 	if (sc->sc_rts == onoff)
    470 		return;
    471 	sc->sc_rts = onoff;
    472 
    473 	ubsa_request(sc, UBSA_SET_RTS, onoff ? 1 : 0);
    474 }
    475 
    476 Static void
    477 ubsa_break(struct ubsa_softc *sc, int onoff)
    478 {
    479 
    480 	DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
    481 
    482 	ubsa_request(sc, UBSA_SET_BREAK, onoff ? 1 : 0);
    483 }
    484 
    485 Static void
    486 ubsa_set(void *addr, int portno, int reg, int onoff)
    487 {
    488 	struct ubsa_softc *sc;
    489 
    490 	sc = addr;
    491 	switch (reg) {
    492 	case UCOM_SET_DTR:
    493 		ubsa_dtr(sc, onoff);
    494 		break;
    495 	case UCOM_SET_RTS:
    496 		ubsa_rts(sc, onoff);
    497 		break;
    498 	case UCOM_SET_BREAK:
    499 		ubsa_break(sc, onoff);
    500 		break;
    501 	default:
    502 		break;
    503 	}
    504 }
    505 
    506 Static void
    507 ubsa_baudrate(struct ubsa_softc *sc, speed_t speed)
    508 {
    509 	u_int16_t value = 0;
    510 
    511 	DPRINTF(("ubsa_baudrate: speed = %d\n", speed));
    512 
    513 	switch(speed) {
    514 	case B0:
    515 		break;
    516 	case B300:
    517 	case B600:
    518 	case B1200:
    519 	case B2400:
    520 	case B4800:
    521 	case B9600:
    522 	case B19200:
    523 	case B38400:
    524 	case B57600:
    525 	case B115200:
    526 	case B230400:
    527 		value = B230400 / speed;
    528 		break;
    529 	default:
    530 		printf("%s: ubsa_param: unsupported baudrate, "
    531 		    "forcing default of 9600\n",
    532 		    USBDEVNAME(sc->sc_dev));
    533 		value = B230400 / B9600;
    534 		break;
    535 	};
    536 
    537 	if (speed == B0) {
    538 		ubsa_flow(sc, 0, 0);
    539 		ubsa_dtr(sc, 0);
    540 		ubsa_rts(sc, 0);
    541 	} else
    542 		ubsa_request(sc, UBSA_SET_BAUDRATE, value);
    543 }
    544 
    545 Static void
    546 ubsa_parity(struct ubsa_softc *sc, tcflag_t cflag)
    547 {
    548 	int value;
    549 
    550 	DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag));
    551 
    552 	if (cflag & PARENB)
    553 		value = (cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
    554 	else
    555 		value = UBSA_PARITY_NONE;
    556 
    557 	ubsa_request(sc, UBSA_SET_PARITY, value);
    558 }
    559 
    560 Static void
    561 ubsa_databits(struct ubsa_softc *sc, tcflag_t cflag)
    562 {
    563 	int value;
    564 
    565 	DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag));
    566 
    567 	switch (cflag & CSIZE) {
    568 	case CS5: value = 0; break;
    569 	case CS6: value = 1; break;
    570 	case CS7: value = 2; break;
    571 	case CS8: value = 3; break;
    572 	default:
    573 		printf("%s: ubsa_param: unsupported databits requested, "
    574 		    "forcing default of 8\n",
    575 		    USBDEVNAME(sc->sc_dev));
    576 		value = 3;
    577 	}
    578 
    579 	ubsa_request(sc, UBSA_SET_DATA_BITS, value);
    580 }
    581 
    582 Static void
    583 ubsa_stopbits(struct ubsa_softc *sc, tcflag_t cflag)
    584 {
    585 	int value;
    586 
    587 	DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag));
    588 
    589 	value = (cflag & CSTOPB) ? 1 : 0;
    590 
    591 	ubsa_request(sc, UBSA_SET_STOP_BITS, value);
    592 }
    593 
    594 Static void
    595 ubsa_flow(struct ubsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
    596 {
    597 	int value;
    598 
    599 	DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag, iflag));
    600 
    601 	value = 0;
    602 	if (cflag & CRTSCTS)
    603 		value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
    604 	if (iflag & (IXON|IXOFF))
    605 		value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
    606 
    607 	ubsa_request(sc, UBSA_SET_FLOW_CTRL, value);
    608 }
    609 
    610 Static int
    611 ubsa_param(void *addr, int portno, struct termios *ti)
    612 {
    613 	struct ubsa_softc *sc;
    614 
    615 	DPRINTF(("ubsa_param: sc = %p\n", sc));
    616 
    617 	sc = addr;
    618 	ubsa_baudrate(sc, ti->c_ospeed);
    619 	ubsa_parity(sc, ti->c_cflag);
    620 	ubsa_databits(sc, ti->c_cflag);
    621 	ubsa_stopbits(sc, ti->c_cflag);
    622 	ubsa_flow(sc, ti->c_cflag, ti->c_iflag);
    623 
    624 	return (0);
    625 }
    626 
    627 Static int
    628 ubsa_open(void *addr, int portno)
    629 {
    630 	struct ubsa_softc *sc;
    631 	int err;
    632 
    633 	sc = addr;
    634 	if (sc->sc_dying)
    635 		return (ENXIO);
    636 
    637 	DPRINTF(("ubsa_open: sc = %p\n", sc));
    638 
    639 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    640 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    641 		err = usbd_open_pipe_intr(sc->sc_intr_iface,
    642 		    sc->sc_intr_number,
    643 		    USBD_SHORT_XFER_OK,
    644 		    &sc->sc_intr_pipe,
    645 		    sc,
    646 		    sc->sc_intr_buf,
    647 		    sc->sc_isize,
    648 		    ubsa_intr,
    649 		    UBSA_INTR_INTERVAL);
    650 		if (err) {
    651 			printf("%s: cannot open interrupt pipe (addr %d)\n",
    652 			    USBDEVNAME(sc->sc_dev),
    653 			    sc->sc_intr_number);
    654 			return (EIO);
    655 		}
    656 	}
    657 
    658 	return (0);
    659 }
    660 
    661 Static void
    662 ubsa_close(void *addr, int portno)
    663 {
    664 	struct ubsa_softc *sc;
    665 	int err;
    666 
    667 	sc = addr;
    668 	if (sc->sc_dying)
    669 		return;
    670 
    671 	DPRINTF(("ubsa_close: close\n"));
    672 
    673 	if (sc->sc_intr_pipe != NULL) {
    674 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    675 		if (err)
    676 			printf("%s: abort interrupt pipe failed: %s\n",
    677 			    USBDEVNAME(sc->sc_dev),
    678 			    usbd_errstr(err));
    679 		err = usbd_close_pipe(sc->sc_intr_pipe);
    680 		if (err)
    681 			printf("%s: close interrupt pipe failed: %s\n",
    682 			    USBDEVNAME(sc->sc_dev),
    683 			    usbd_errstr(err));
    684 		free(sc->sc_intr_buf, M_USBDEV);
    685 		sc->sc_intr_pipe = NULL;
    686 	}
    687 }
    688 
    689 Static void
    690 ubsa_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    691 {
    692 	struct ubsa_softc *sc;
    693 	u_char *buf;
    694 
    695 	sc = priv;
    696 	buf = sc->sc_intr_buf;
    697 	if (sc->sc_dying)
    698 		return;
    699 
    700 	if (status != USBD_NORMAL_COMPLETION) {
    701 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    702 			return;
    703 
    704 		DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
    705 		    USBDEVNAME(sc->sc_ucom.sc_dev),
    706 		    usbd_errstr(status)));
    707 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    708 		return;
    709 	}
    710 
    711 	/* incidentally, Belkin adapter status bits match UART 16550 bits */
    712 	sc->sc_lsr = buf[2];
    713 	sc->sc_msr = buf[3];
    714 
    715 	DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
    716 	    USBDEVNAME(sc->sc_ucom.sc_dev), sc->sc_lsr, sc->sc_msr));
    717 
    718 	ucom_status_change((struct ucom_softc *)sc->sc_subdev);
    719 }
    720 
    721 Static void
    722 ubsa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    723 {
    724 	struct ubsa_softc *sc;
    725 
    726 	DPRINTF(("ubsa_get_status\n"));
    727 
    728 	sc = addr;
    729 	if (lsr != NULL)
    730 		*lsr = sc->sc_lsr;
    731 	if (msr != NULL)
    732 		*msr = sc->sc_msr;
    733 }
    734