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