Home | History | Annotate | Line # | Download | only in usb
umct.c revision 1.2.2.6
      1  1.2.2.6  nathanw /*	$NetBSD: umct.c,v 1.2.2.6 2002/10/18 02:44:36 nathanw Exp $	*/
      2  1.2.2.2  nathanw /*
      3  1.2.2.2  nathanw  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      4  1.2.2.2  nathanw  * All rights reserved.
      5  1.2.2.2  nathanw  *
      6  1.2.2.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      7  1.2.2.2  nathanw  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
      8  1.2.2.2  nathanw  *
      9  1.2.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
     10  1.2.2.2  nathanw  * modification, are permitted provided that the following conditions
     11  1.2.2.2  nathanw  * are met:
     12  1.2.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     13  1.2.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     14  1.2.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.2.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     16  1.2.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     17  1.2.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     18  1.2.2.2  nathanw  *    must display the following acknowledgement:
     19  1.2.2.2  nathanw  *        This product includes software developed by the NetBSD
     20  1.2.2.2  nathanw  *        Foundation, Inc. and its contributors.
     21  1.2.2.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  1.2.2.2  nathanw  *    contributors may be used to endorse or promote products derived
     23  1.2.2.2  nathanw  *    from this software without specific prior written permission.
     24  1.2.2.2  nathanw  *
     25  1.2.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  1.2.2.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.2.2.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.2.2.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  1.2.2.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.2.2.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.2.2.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.2.2.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.2.2.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.2.2.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.2.2.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     36  1.2.2.2  nathanw  */
     37  1.2.2.2  nathanw 
     38  1.2.2.2  nathanw /*
     39  1.2.2.4  nathanw  * MCT USB-RS232 Interface Controller
     40  1.2.2.2  nathanw  * http://www.mct.com.tw/p_u232.html
     41  1.2.2.2  nathanw  * http://www.dlink.com/products/usb/dsbs25
     42  1.2.2.2  nathanw  */
     43  1.2.2.2  nathanw 
     44  1.2.2.3  nathanw #include <sys/cdefs.h>
     45  1.2.2.6  nathanw __KERNEL_RCSID(0, "$NetBSD: umct.c,v 1.2.2.6 2002/10/18 02:44:36 nathanw Exp $");
     46  1.2.2.3  nathanw 
     47  1.2.2.2  nathanw #include <sys/param.h>
     48  1.2.2.2  nathanw #include <sys/systm.h>
     49  1.2.2.2  nathanw #include <sys/kernel.h>
     50  1.2.2.2  nathanw #include <sys/malloc.h>
     51  1.2.2.2  nathanw #include <sys/ioctl.h>
     52  1.2.2.2  nathanw #include <sys/conf.h>
     53  1.2.2.2  nathanw #include <sys/tty.h>
     54  1.2.2.2  nathanw #include <sys/file.h>
     55  1.2.2.2  nathanw #include <sys/select.h>
     56  1.2.2.2  nathanw #include <sys/proc.h>
     57  1.2.2.2  nathanw #include <sys/vnode.h>
     58  1.2.2.2  nathanw #include <sys/device.h>
     59  1.2.2.2  nathanw #include <sys/poll.h>
     60  1.2.2.2  nathanw 
     61  1.2.2.2  nathanw #include <dev/usb/usb.h>
     62  1.2.2.2  nathanw #include <dev/usb/usbcdc.h>
     63  1.2.2.2  nathanw 
     64  1.2.2.2  nathanw #include <dev/usb/usbdi.h>
     65  1.2.2.2  nathanw #include <dev/usb/usbdi_util.h>
     66  1.2.2.2  nathanw #include <dev/usb/usbdevs.h>
     67  1.2.2.2  nathanw #include <dev/usb/usb_quirks.h>
     68  1.2.2.2  nathanw 
     69  1.2.2.2  nathanw #include <dev/usb/usbdevs.h>
     70  1.2.2.2  nathanw #include <dev/usb/ucomvar.h>
     71  1.2.2.2  nathanw 
     72  1.2.2.2  nathanw #include <dev/usb/umct.h>
     73  1.2.2.2  nathanw 
     74  1.2.2.2  nathanw #ifdef UMCT_DEBUG
     75  1.2.2.2  nathanw #define DPRINTFN(n, x)  if (umctdebug > (n)) logprintf x
     76  1.2.2.2  nathanw int	umctdebug = 0;
     77  1.2.2.2  nathanw #else
     78  1.2.2.2  nathanw #define DPRINTFN(n, x)
     79  1.2.2.2  nathanw #endif
     80  1.2.2.2  nathanw #define DPRINTF(x) DPRINTFN(0, x)
     81  1.2.2.2  nathanw 
     82  1.2.2.2  nathanw #define	UMCT_CONFIG_INDEX	0
     83  1.2.2.2  nathanw #define	UMCT_IFACE_INDEX	0
     84  1.2.2.2  nathanw 
     85  1.2.2.2  nathanw struct	umct_softc {
     86  1.2.2.2  nathanw 	USBBASEDEVICE		sc_dev;		/* base device */
     87  1.2.2.2  nathanw 	usbd_device_handle	sc_udev;	/* USB device */
     88  1.2.2.2  nathanw 	usbd_interface_handle	sc_iface;	/* interface */
     89  1.2.2.2  nathanw 	int			sc_iface_number;	/* interface number */
     90  1.2.2.4  nathanw 	u_int16_t		sc_product;
     91  1.2.2.2  nathanw 
     92  1.2.2.2  nathanw 	int			sc_intr_number;	/* interrupt number */
     93  1.2.2.2  nathanw 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
     94  1.2.2.2  nathanw 	u_char			*sc_intr_buf;	/* interrupt buffer */
     95  1.2.2.2  nathanw 	int			sc_isize;
     96  1.2.2.2  nathanw 
     97  1.2.2.2  nathanw 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
     98  1.2.2.2  nathanw 	u_char			sc_dtr;		/* current DTR state */
     99  1.2.2.2  nathanw 	u_char			sc_rts;		/* current RTS state */
    100  1.2.2.2  nathanw 	u_char			sc_break;	/* set break */
    101  1.2.2.2  nathanw 
    102  1.2.2.2  nathanw 	u_char			sc_status;
    103  1.2.2.2  nathanw 
    104  1.2.2.2  nathanw 	device_ptr_t		sc_subdev;	/* ucom device */
    105  1.2.2.2  nathanw 
    106  1.2.2.2  nathanw 	u_char			sc_dying;	/* disconnecting */
    107  1.2.2.2  nathanw 
    108  1.2.2.2  nathanw 	u_char			sc_lsr;		/* Local status register */
    109  1.2.2.2  nathanw 	u_char			sc_msr;		/* umct status register */
    110  1.2.2.4  nathanw 
    111  1.2.2.4  nathanw 	u_int			last_lcr;	/* keep lcr register */
    112  1.2.2.2  nathanw };
    113  1.2.2.2  nathanw 
    114  1.2.2.2  nathanw /*
    115  1.2.2.2  nathanw  * These are the maximum number of bytes transferred per frame.
    116  1.2.2.2  nathanw  * The output buffer size cannot be increased due to the size encoding.
    117  1.2.2.2  nathanw  */
    118  1.2.2.2  nathanw #define UMCTIBUFSIZE 256
    119  1.2.2.2  nathanw #define UMCTOBUFSIZE 256
    120  1.2.2.2  nathanw 
    121  1.2.2.2  nathanw Static	void umct_init(struct umct_softc *);
    122  1.2.2.4  nathanw Static	void umct_set_baudrate(struct umct_softc *, u_int);
    123  1.2.2.4  nathanw Static	void umct_set_lcr(struct umct_softc *, u_int);
    124  1.2.2.2  nathanw Static	void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    125  1.2.2.2  nathanw 
    126  1.2.2.2  nathanw Static	void umct_set(void *, int, int, int);
    127  1.2.2.2  nathanw Static	void umct_dtr(struct umct_softc *, int);
    128  1.2.2.2  nathanw Static	void umct_rts(struct umct_softc *, int);
    129  1.2.2.2  nathanw Static	void umct_break(struct umct_softc *, int);
    130  1.2.2.2  nathanw Static	void umct_set_line_state(struct umct_softc *);
    131  1.2.2.2  nathanw Static	void umct_get_status(void *, int portno, u_char *lsr, u_char *msr);
    132  1.2.2.2  nathanw Static	int  umct_param(void *, int, struct termios *);
    133  1.2.2.2  nathanw Static	int  umct_open(void *, int);
    134  1.2.2.2  nathanw Static	void umct_close(void *, int);
    135  1.2.2.2  nathanw 
    136  1.2.2.2  nathanw struct	ucom_methods umct_methods = {
    137  1.2.2.2  nathanw 	umct_get_status,
    138  1.2.2.2  nathanw 	umct_set,
    139  1.2.2.2  nathanw 	umct_param,
    140  1.2.2.2  nathanw 	NULL,
    141  1.2.2.2  nathanw 	umct_open,
    142  1.2.2.2  nathanw 	umct_close,
    143  1.2.2.2  nathanw 	NULL,
    144  1.2.2.2  nathanw 	NULL,
    145  1.2.2.2  nathanw };
    146  1.2.2.2  nathanw 
    147  1.2.2.4  nathanw static const struct usb_devno umct_devs[] = {
    148  1.2.2.2  nathanw 	/* MCT USB-232 Interface Products */
    149  1.2.2.2  nathanw 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
    150  1.2.2.2  nathanw 	/* Sitecom USB-232 Products */
    151  1.2.2.2  nathanw 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
    152  1.2.2.2  nathanw 	/* D-Link DU-H3SP USB BAY Hub Products */
    153  1.2.2.2  nathanw 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
    154  1.2.2.2  nathanw };
    155  1.2.2.4  nathanw #define umct_lookup(v, p) usb_lookup(umct_devs, v, p)
    156  1.2.2.2  nathanw 
    157  1.2.2.2  nathanw USB_DECLARE_DRIVER(umct);
    158  1.2.2.2  nathanw 
    159  1.2.2.2  nathanw USB_MATCH(umct)
    160  1.2.2.2  nathanw {
    161  1.2.2.2  nathanw 	USB_MATCH_START(umct, uaa);
    162  1.2.2.2  nathanw 
    163  1.2.2.2  nathanw 	if (uaa->iface != NULL)
    164  1.2.2.2  nathanw 		return (UMATCH_NONE);
    165  1.2.2.2  nathanw 
    166  1.2.2.4  nathanw 	return (umct_lookup(uaa->vendor, uaa->product) != NULL ?
    167  1.2.2.4  nathanw 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    168  1.2.2.2  nathanw }
    169  1.2.2.2  nathanw 
    170  1.2.2.2  nathanw USB_ATTACH(umct)
    171  1.2.2.2  nathanw {
    172  1.2.2.2  nathanw 	USB_ATTACH_START(umct, sc, uaa);
    173  1.2.2.2  nathanw 	usbd_device_handle dev = uaa->device;
    174  1.2.2.2  nathanw 	usb_config_descriptor_t *cdesc;
    175  1.2.2.2  nathanw 	usb_interface_descriptor_t *id;
    176  1.2.2.2  nathanw 	usb_endpoint_descriptor_t *ed;
    177  1.2.2.5  nathanw 
    178  1.2.2.2  nathanw 	char devinfo[1024];
    179  1.2.2.2  nathanw 	char *devname = USBDEVNAME(sc->sc_dev);
    180  1.2.2.2  nathanw 	usbd_status err;
    181  1.2.2.4  nathanw 	int i, found;
    182  1.2.2.2  nathanw 	struct ucom_attach_args uca;
    183  1.2.2.2  nathanw 
    184  1.2.2.2  nathanw         usbd_devinfo(dev, 0, devinfo);
    185  1.2.2.2  nathanw         USB_ATTACH_SETUP;
    186  1.2.2.2  nathanw         printf("%s: %s\n", devname, devinfo);
    187  1.2.2.2  nathanw 
    188  1.2.2.2  nathanw         sc->sc_udev = dev;
    189  1.2.2.4  nathanw 	sc->sc_product = uaa->product;
    190  1.2.2.2  nathanw 
    191  1.2.2.2  nathanw 	DPRINTF(("\n\numct attach: sc=%p\n", sc));
    192  1.2.2.2  nathanw 
    193  1.2.2.5  nathanw 	/* initialize endpoints */
    194  1.2.2.2  nathanw 	uca.bulkin = uca.bulkout = -1;
    195  1.2.2.2  nathanw 	sc->sc_intr_number = -1;
    196  1.2.2.2  nathanw 	sc->sc_intr_pipe = NULL;
    197  1.2.2.2  nathanw 
    198  1.2.2.2  nathanw 	/* Move the device into the configured state. */
    199  1.2.2.2  nathanw 	err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
    200  1.2.2.2  nathanw 	if (err) {
    201  1.2.2.2  nathanw 		printf("\n%s: failed to set configuration, err=%s\n",
    202  1.2.2.2  nathanw 			devname, usbd_errstr(err));
    203  1.2.2.2  nathanw 		sc->sc_dying = 1;
    204  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    205  1.2.2.2  nathanw 	}
    206  1.2.2.2  nathanw 
    207  1.2.2.2  nathanw 	/* get the config descriptor */
    208  1.2.2.2  nathanw 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    209  1.2.2.2  nathanw 
    210  1.2.2.2  nathanw 	if (cdesc == NULL) {
    211  1.2.2.2  nathanw 		printf("%s: failed to get configuration descriptor\n",
    212  1.2.2.2  nathanw 			USBDEVNAME(sc->sc_dev));
    213  1.2.2.2  nathanw 		sc->sc_dying = 1;
    214  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    215  1.2.2.2  nathanw 	}
    216  1.2.2.2  nathanw 
    217  1.2.2.2  nathanw 	/* get the interface */
    218  1.2.2.5  nathanw 	err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
    219  1.2.2.2  nathanw 							&sc->sc_iface);
    220  1.2.2.2  nathanw 	if (err) {
    221  1.2.2.2  nathanw 		printf("\n%s: failed to get interface, err=%s\n",
    222  1.2.2.2  nathanw 			devname, usbd_errstr(err));
    223  1.2.2.2  nathanw 		sc->sc_dying = 1;
    224  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    225  1.2.2.2  nathanw 	}
    226  1.2.2.2  nathanw 
    227  1.2.2.2  nathanw 	/* Find the bulk{in,out} and interrupt endpoints */
    228  1.2.2.2  nathanw 
    229  1.2.2.2  nathanw 	id = usbd_get_interface_descriptor(sc->sc_iface);
    230  1.2.2.2  nathanw 	sc->sc_iface_number = id->bInterfaceNumber;
    231  1.2.2.2  nathanw 	found = 0;
    232  1.2.2.2  nathanw 
    233  1.2.2.2  nathanw 	for (i = 0; i < id->bNumEndpoints; i++) {
    234  1.2.2.2  nathanw 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    235  1.2.2.2  nathanw 		if (ed == NULL) {
    236  1.2.2.2  nathanw 			printf("%s: no endpoint descriptor for %d\n",
    237  1.2.2.2  nathanw 				USBDEVNAME(sc->sc_dev), i);
    238  1.2.2.2  nathanw 			sc->sc_dying = 1;
    239  1.2.2.2  nathanw 			USB_ATTACH_ERROR_RETURN;
    240  1.2.2.2  nathanw 		}
    241  1.2.2.2  nathanw 
    242  1.2.2.2  nathanw 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    243  1.2.2.2  nathanw 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
    244  1.2.2.2  nathanw 		    found == 0) {
    245  1.2.2.2  nathanw 			uca.bulkin = ed->bEndpointAddress;
    246  1.2.2.2  nathanw 			found = 1;
    247  1.2.2.2  nathanw 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    248  1.2.2.2  nathanw 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    249  1.2.2.2  nathanw 			uca.bulkout = ed->bEndpointAddress;
    250  1.2.2.2  nathanw 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    251  1.2.2.2  nathanw 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    252  1.2.2.2  nathanw 			sc->sc_intr_number = ed->bEndpointAddress;
    253  1.2.2.2  nathanw 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    254  1.2.2.2  nathanw 		}
    255  1.2.2.2  nathanw 	}
    256  1.2.2.2  nathanw 
    257  1.2.2.2  nathanw 	if (uca.bulkin == -1) {
    258  1.2.2.2  nathanw 		printf("%s: Could not find data bulk in\n",
    259  1.2.2.2  nathanw 			USBDEVNAME(sc->sc_dev));
    260  1.2.2.2  nathanw 		sc->sc_dying = 1;
    261  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    262  1.2.2.2  nathanw 	}
    263  1.2.2.2  nathanw 
    264  1.2.2.2  nathanw 	if (uca.bulkout == -1) {
    265  1.2.2.2  nathanw 		printf("%s: Could not find data bulk out\n",
    266  1.2.2.2  nathanw 			USBDEVNAME(sc->sc_dev));
    267  1.2.2.2  nathanw 		sc->sc_dying = 1;
    268  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    269  1.2.2.2  nathanw 	}
    270  1.2.2.2  nathanw 
    271  1.2.2.2  nathanw 	if (sc->sc_intr_number== -1) {
    272  1.2.2.2  nathanw 		printf("%s: Could not find interrupt in\n",
    273  1.2.2.2  nathanw 			USBDEVNAME(sc->sc_dev));
    274  1.2.2.2  nathanw 		sc->sc_dying = 1;
    275  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    276  1.2.2.2  nathanw 	}
    277  1.2.2.2  nathanw 
    278  1.2.2.2  nathanw 	sc->sc_dtr = sc->sc_rts = 0;
    279  1.2.2.2  nathanw 	uca.portno = UCOM_UNK_PORTNO;
    280  1.2.2.2  nathanw 	/* bulkin, bulkout set above */
    281  1.2.2.2  nathanw 	uca.ibufsize = UMCTIBUFSIZE;
    282  1.2.2.4  nathanw 	if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232)
    283  1.2.2.4  nathanw 		uca.obufsize = 16; /* device is broken */
    284  1.2.2.4  nathanw 	else
    285  1.2.2.4  nathanw 		uca.obufsize = UMCTOBUFSIZE;
    286  1.2.2.2  nathanw 	uca.ibufsizepad = UMCTIBUFSIZE;
    287  1.2.2.2  nathanw 	uca.opkthdrlen = 0;
    288  1.2.2.2  nathanw 	uca.device = dev;
    289  1.2.2.2  nathanw 	uca.iface = sc->sc_iface;
    290  1.2.2.2  nathanw 	uca.methods = &umct_methods;
    291  1.2.2.2  nathanw 	uca.arg = sc;
    292  1.2.2.2  nathanw 	uca.info = NULL;
    293  1.2.2.2  nathanw 
    294  1.2.2.2  nathanw 	umct_init(sc);
    295  1.2.2.2  nathanw 
    296  1.2.2.2  nathanw 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    297  1.2.2.2  nathanw 			   USBDEV(sc->sc_dev));
    298  1.2.2.2  nathanw 
    299  1.2.2.2  nathanw 	DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n",
    300  1.2.2.2  nathanw 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    301  1.2.2.2  nathanw 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    302  1.2.2.2  nathanw 
    303  1.2.2.2  nathanw 	USB_ATTACH_SUCCESS_RETURN;
    304  1.2.2.2  nathanw }
    305  1.2.2.2  nathanw 
    306  1.2.2.2  nathanw USB_DETACH(umct)
    307  1.2.2.2  nathanw {
    308  1.2.2.2  nathanw 	USB_DETACH_START(umct, sc);
    309  1.2.2.2  nathanw 	int rv = 0;
    310  1.2.2.2  nathanw 
    311  1.2.2.2  nathanw 	DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags));
    312  1.2.2.2  nathanw 
    313  1.2.2.2  nathanw         if (sc->sc_intr_pipe != NULL) {
    314  1.2.2.2  nathanw                 usbd_abort_pipe(sc->sc_intr_pipe);
    315  1.2.2.2  nathanw                 usbd_close_pipe(sc->sc_intr_pipe);
    316  1.2.2.2  nathanw 		free(sc->sc_intr_buf, M_USBDEV);
    317  1.2.2.2  nathanw                 sc->sc_intr_pipe = NULL;
    318  1.2.2.2  nathanw         }
    319  1.2.2.2  nathanw 
    320  1.2.2.2  nathanw 	sc->sc_dying = 1;
    321  1.2.2.2  nathanw 	if (sc->sc_subdev != NULL) {
    322  1.2.2.2  nathanw 		rv = config_detach(sc->sc_subdev, flags);
    323  1.2.2.2  nathanw 		sc->sc_subdev = NULL;
    324  1.2.2.2  nathanw 	}
    325  1.2.2.2  nathanw 
    326  1.2.2.2  nathanw 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    327  1.2.2.2  nathanw 			   USBDEV(sc->sc_dev));
    328  1.2.2.2  nathanw 
    329  1.2.2.2  nathanw 	return (rv);
    330  1.2.2.2  nathanw }
    331  1.2.2.2  nathanw 
    332  1.2.2.2  nathanw int
    333  1.2.2.2  nathanw umct_activate(device_ptr_t self, enum devact act)
    334  1.2.2.2  nathanw {
    335  1.2.2.2  nathanw 	struct umct_softc *sc = (struct umct_softc *)self;
    336  1.2.2.2  nathanw 	int rv = 0;
    337  1.2.2.2  nathanw 
    338  1.2.2.2  nathanw 	switch (act) {
    339  1.2.2.2  nathanw 	case DVACT_ACTIVATE:
    340  1.2.2.2  nathanw 		return (EOPNOTSUPP);
    341  1.2.2.2  nathanw 
    342  1.2.2.2  nathanw 	case DVACT_DEACTIVATE:
    343  1.2.2.2  nathanw 		if (sc->sc_subdev != NULL)
    344  1.2.2.2  nathanw 			rv = config_deactivate(sc->sc_subdev);
    345  1.2.2.2  nathanw 		sc->sc_dying = 1;
    346  1.2.2.2  nathanw 		break;
    347  1.2.2.2  nathanw 	}
    348  1.2.2.2  nathanw 	return (rv);
    349  1.2.2.2  nathanw }
    350  1.2.2.2  nathanw 
    351  1.2.2.2  nathanw void
    352  1.2.2.2  nathanw umct_set_line_state(struct umct_softc *sc)
    353  1.2.2.2  nathanw {
    354  1.2.2.2  nathanw 	usb_device_request_t req;
    355  1.2.2.4  nathanw 	uByte ls;
    356  1.2.2.2  nathanw 
    357  1.2.2.2  nathanw 	ls = (sc->sc_dtr ? MCR_DTR : 0) |
    358  1.2.2.4  nathanw 	     (sc->sc_rts ? MCR_RTS : 0);
    359  1.2.2.2  nathanw 
    360  1.2.2.2  nathanw 	DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n",
    361  1.2.2.2  nathanw 			sc->sc_dtr, sc->sc_rts, ls));
    362  1.2.2.2  nathanw 
    363  1.2.2.2  nathanw 	req.bmRequestType = UMCT_SET_REQUEST;
    364  1.2.2.2  nathanw 	req.bRequest = REQ_SET_MCR;
    365  1.2.2.2  nathanw 	USETW(req.wValue, 0);
    366  1.2.2.2  nathanw 	USETW(req.wIndex, sc->sc_iface_number);
    367  1.2.2.2  nathanw 	USETW(req.wLength, LENGTH_SET_MCR);
    368  1.2.2.2  nathanw 
    369  1.2.2.2  nathanw 	(void)usbd_do_request(sc->sc_udev, &req, &ls);
    370  1.2.2.2  nathanw }
    371  1.2.2.2  nathanw 
    372  1.2.2.2  nathanw void
    373  1.2.2.2  nathanw umct_set(void *addr, int portno, int reg, int onoff)
    374  1.2.2.2  nathanw {
    375  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    376  1.2.2.2  nathanw 
    377  1.2.2.2  nathanw 	switch (reg) {
    378  1.2.2.2  nathanw 	case UCOM_SET_DTR:
    379  1.2.2.2  nathanw 		umct_dtr(sc, onoff);
    380  1.2.2.2  nathanw 		break;
    381  1.2.2.2  nathanw 	case UCOM_SET_RTS:
    382  1.2.2.2  nathanw 		umct_rts(sc, onoff);
    383  1.2.2.2  nathanw 		break;
    384  1.2.2.2  nathanw 	case UCOM_SET_BREAK:
    385  1.2.2.2  nathanw 		umct_break(sc, onoff);
    386  1.2.2.2  nathanw 		break;
    387  1.2.2.2  nathanw 	default:
    388  1.2.2.2  nathanw 		break;
    389  1.2.2.2  nathanw 	}
    390  1.2.2.2  nathanw }
    391  1.2.2.2  nathanw 
    392  1.2.2.2  nathanw void
    393  1.2.2.2  nathanw umct_dtr(struct umct_softc *sc, int onoff)
    394  1.2.2.2  nathanw {
    395  1.2.2.2  nathanw 
    396  1.2.2.2  nathanw 	DPRINTF(("umct_dtr: onoff=%d\n", onoff));
    397  1.2.2.2  nathanw 
    398  1.2.2.2  nathanw 	if (sc->sc_dtr == onoff)
    399  1.2.2.2  nathanw 		return;
    400  1.2.2.2  nathanw 	sc->sc_dtr = onoff;
    401  1.2.2.2  nathanw 
    402  1.2.2.2  nathanw 	umct_set_line_state(sc);
    403  1.2.2.2  nathanw }
    404  1.2.2.2  nathanw 
    405  1.2.2.2  nathanw void
    406  1.2.2.2  nathanw umct_rts(struct umct_softc *sc, int onoff)
    407  1.2.2.2  nathanw {
    408  1.2.2.2  nathanw 	DPRINTF(("umct_rts: onoff=%d\n", onoff));
    409  1.2.2.2  nathanw 
    410  1.2.2.2  nathanw 	if (sc->sc_rts == onoff)
    411  1.2.2.2  nathanw 		return;
    412  1.2.2.2  nathanw 	sc->sc_rts = onoff;
    413  1.2.2.2  nathanw 
    414  1.2.2.2  nathanw 	umct_set_line_state(sc);
    415  1.2.2.2  nathanw }
    416  1.2.2.2  nathanw 
    417  1.2.2.2  nathanw void
    418  1.2.2.2  nathanw umct_break(struct umct_softc *sc, int onoff)
    419  1.2.2.2  nathanw {
    420  1.2.2.2  nathanw 	DPRINTF(("umct_break: onoff=%d\n", onoff));
    421  1.2.2.2  nathanw 
    422  1.2.2.4  nathanw 	umct_set_lcr(sc, onoff ? sc->last_lcr | LCR_SET_BREAK :
    423  1.2.2.4  nathanw 		     sc->last_lcr);
    424  1.2.2.2  nathanw }
    425  1.2.2.2  nathanw 
    426  1.2.2.2  nathanw void
    427  1.2.2.4  nathanw umct_set_lcr(struct umct_softc *sc, u_int data)
    428  1.2.2.2  nathanw {
    429  1.2.2.2  nathanw 	usb_device_request_t req;
    430  1.2.2.4  nathanw 	uByte adata;
    431  1.2.2.2  nathanw 
    432  1.2.2.4  nathanw 	adata = data;
    433  1.2.2.2  nathanw 	req.bmRequestType = UMCT_SET_REQUEST;
    434  1.2.2.2  nathanw 	req.bRequest = REQ_SET_LCR;
    435  1.2.2.2  nathanw 	USETW(req.wValue, 0);
    436  1.2.2.2  nathanw 	USETW(req.wIndex, sc->sc_iface_number);
    437  1.2.2.2  nathanw 	USETW(req.wLength, LENGTH_SET_LCR);
    438  1.2.2.2  nathanw 
    439  1.2.2.4  nathanw 	(void)usbd_do_request(sc->sc_udev, &req, &adata); /* XXX should check */
    440  1.2.2.2  nathanw }
    441  1.2.2.2  nathanw 
    442  1.2.2.2  nathanw void
    443  1.2.2.4  nathanw umct_set_baudrate(struct umct_softc *sc, u_int rate)
    444  1.2.2.2  nathanw {
    445  1.2.2.2  nathanw         usb_device_request_t req;
    446  1.2.2.4  nathanw 	uDWord arate;
    447  1.2.2.4  nathanw 	u_int val;
    448  1.2.2.4  nathanw 
    449  1.2.2.4  nathanw 	if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232) {
    450  1.2.2.4  nathanw 		switch (rate) {
    451  1.2.2.4  nathanw 		case    300: val = 0x01; break;
    452  1.2.2.4  nathanw 		case    600: val = 0x02; break;
    453  1.2.2.4  nathanw 		case   1200: val = 0x03; break;
    454  1.2.2.4  nathanw 		case   2400: val = 0x04; break;
    455  1.2.2.4  nathanw 		case   4800: val = 0x06; break;
    456  1.2.2.4  nathanw 		case   9600: val = 0x08; break;
    457  1.2.2.4  nathanw 		case  19200: val = 0x09; break;
    458  1.2.2.4  nathanw 		case  38400: val = 0x0a; break;
    459  1.2.2.4  nathanw 		case  57600: val = 0x0b; break;
    460  1.2.2.4  nathanw 		case 115200: val = 0x0c; break;
    461  1.2.2.4  nathanw 		default:     val = -1; break;
    462  1.2.2.4  nathanw 		}
    463  1.2.2.4  nathanw 	} else {
    464  1.2.2.4  nathanw 		val = UMCT_BAUD_RATE(rate);
    465  1.2.2.4  nathanw 	}
    466  1.2.2.4  nathanw 	USETDW(arate, val);
    467  1.2.2.2  nathanw 
    468  1.2.2.2  nathanw         req.bmRequestType = UMCT_SET_REQUEST;
    469  1.2.2.2  nathanw         req.bRequest = REQ_SET_BAUD_RATE;
    470  1.2.2.2  nathanw         USETW(req.wValue, 0);
    471  1.2.2.2  nathanw         USETW(req.wIndex, sc->sc_iface_number);
    472  1.2.2.2  nathanw         USETW(req.wLength, LENGTH_BAUD_RATE);
    473  1.2.2.2  nathanw 
    474  1.2.2.4  nathanw         (void)usbd_do_request(sc->sc_udev, &req, arate); /* XXX should check */
    475  1.2.2.2  nathanw }
    476  1.2.2.2  nathanw 
    477  1.2.2.2  nathanw void
    478  1.2.2.2  nathanw umct_init(struct umct_softc *sc)
    479  1.2.2.2  nathanw {
    480  1.2.2.4  nathanw 	umct_set_baudrate(sc, 9600);
    481  1.2.2.4  nathanw 	umct_set_lcr(sc, LCR_DATA_BITS_8 | LCR_PARITY_NONE | LCR_STOP_BITS_1);
    482  1.2.2.2  nathanw }
    483  1.2.2.2  nathanw 
    484  1.2.2.2  nathanw int
    485  1.2.2.2  nathanw umct_param(void *addr, int portno, struct termios *t)
    486  1.2.2.2  nathanw {
    487  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    488  1.2.2.4  nathanw 	u_int data = 0;
    489  1.2.2.2  nathanw 
    490  1.2.2.2  nathanw 	DPRINTF(("umct_param: sc=%p\n", sc));
    491  1.2.2.2  nathanw 
    492  1.2.2.2  nathanw 	DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed));
    493  1.2.2.2  nathanw 
    494  1.2.2.2  nathanw 	if (ISSET(t->c_cflag, CSTOPB))
    495  1.2.2.2  nathanw 		data |= LCR_STOP_BITS_2;
    496  1.2.2.2  nathanw 	else
    497  1.2.2.2  nathanw 		data |= LCR_STOP_BITS_1;
    498  1.2.2.2  nathanw 	if (ISSET(t->c_cflag, PARENB)) {
    499  1.2.2.2  nathanw 		if (ISSET(t->c_cflag, PARODD))
    500  1.2.2.2  nathanw 			data |= LCR_PARITY_ODD;
    501  1.2.2.2  nathanw 		else
    502  1.2.2.2  nathanw 			data |= LCR_PARITY_EVEN;
    503  1.2.2.2  nathanw 	} else
    504  1.2.2.2  nathanw 		data |= LCR_PARITY_NONE;
    505  1.2.2.2  nathanw 	switch (ISSET(t->c_cflag, CSIZE)) {
    506  1.2.2.2  nathanw 	case CS5:
    507  1.2.2.2  nathanw 		data |= LCR_DATA_BITS_5;
    508  1.2.2.2  nathanw 		break;
    509  1.2.2.2  nathanw 	case CS6:
    510  1.2.2.2  nathanw 		data |= LCR_DATA_BITS_6;
    511  1.2.2.2  nathanw 		break;
    512  1.2.2.2  nathanw 	case CS7:
    513  1.2.2.2  nathanw 		data |= LCR_DATA_BITS_7;
    514  1.2.2.2  nathanw 		break;
    515  1.2.2.2  nathanw 	case CS8:
    516  1.2.2.2  nathanw 		data |= LCR_DATA_BITS_8;
    517  1.2.2.2  nathanw 		break;
    518  1.2.2.2  nathanw 	}
    519  1.2.2.2  nathanw 
    520  1.2.2.4  nathanw 	umct_set_baudrate(sc, t->c_ospeed);
    521  1.2.2.2  nathanw 
    522  1.2.2.4  nathanw 	sc->last_lcr = data;
    523  1.2.2.4  nathanw 	umct_set_lcr(sc, data);
    524  1.2.2.2  nathanw 
    525  1.2.2.2  nathanw 	return (0);
    526  1.2.2.2  nathanw }
    527  1.2.2.2  nathanw 
    528  1.2.2.2  nathanw int
    529  1.2.2.2  nathanw umct_open(void *addr, int portno)
    530  1.2.2.2  nathanw {
    531  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    532  1.2.2.2  nathanw 	int err, lcr_data;
    533  1.2.2.5  nathanw 
    534  1.2.2.2  nathanw 	if (sc->sc_dying)
    535  1.2.2.2  nathanw 		return (EIO);
    536  1.2.2.2  nathanw 
    537  1.2.2.2  nathanw 	DPRINTF(("umct_open: sc=%p\n", sc));
    538  1.2.2.2  nathanw 
    539  1.2.2.2  nathanw 	/* initialize LCR */
    540  1.2.2.4  nathanw         lcr_data = LCR_DATA_BITS_8 | LCR_PARITY_NONE |
    541  1.2.2.4  nathanw 	    LCR_STOP_BITS_1;
    542  1.2.2.5  nathanw         umct_set_lcr(sc, lcr_data);
    543  1.2.2.2  nathanw 
    544  1.2.2.2  nathanw 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    545  1.2.2.2  nathanw 		sc->sc_status = 0; /* clear status bit */
    546  1.2.2.2  nathanw 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    547  1.2.2.2  nathanw 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
    548  1.2.2.2  nathanw 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    549  1.2.2.2  nathanw 			sc->sc_intr_buf, sc->sc_isize,
    550  1.2.2.2  nathanw 			umct_intr, USBD_DEFAULT_INTERVAL);
    551  1.2.2.2  nathanw 		if (err) {
    552  1.2.2.2  nathanw 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    553  1.2.2.2  nathanw 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
    554  1.2.2.2  nathanw 					return (EIO);
    555  1.2.2.2  nathanw 		}
    556  1.2.2.2  nathanw 	}
    557  1.2.2.2  nathanw 
    558  1.2.2.2  nathanw 	return (0);
    559  1.2.2.2  nathanw }
    560  1.2.2.2  nathanw 
    561  1.2.2.2  nathanw void
    562  1.2.2.5  nathanw umct_close(void *addr, int portno)
    563  1.2.2.2  nathanw {
    564  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    565  1.2.2.2  nathanw 	int err;
    566  1.2.2.2  nathanw 
    567  1.2.2.2  nathanw 	if (sc->sc_dying)
    568  1.2.2.2  nathanw 		return;
    569  1.2.2.2  nathanw 
    570  1.2.2.2  nathanw 	DPRINTF(("umct_close: close\n"));
    571  1.2.2.2  nathanw 
    572  1.2.2.2  nathanw 	if (sc->sc_intr_pipe != NULL) {
    573  1.2.2.2  nathanw 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    574  1.2.2.2  nathanw 		if (err)
    575  1.2.2.2  nathanw 			printf("%s: abort interrupt pipe failed: %s\n",
    576  1.2.2.2  nathanw 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    577  1.2.2.2  nathanw 		err = usbd_close_pipe(sc->sc_intr_pipe);
    578  1.2.2.2  nathanw 		if (err)
    579  1.2.2.2  nathanw 			printf("%s: close interrupt pipe failed: %s\n",
    580  1.2.2.2  nathanw 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    581  1.2.2.2  nathanw 		free(sc->sc_intr_buf, M_USBDEV);
    582  1.2.2.2  nathanw 		sc->sc_intr_pipe = NULL;
    583  1.2.2.2  nathanw 	}
    584  1.2.2.2  nathanw }
    585  1.2.2.2  nathanw 
    586  1.2.2.2  nathanw void
    587  1.2.2.2  nathanw umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    588  1.2.2.2  nathanw {
    589  1.2.2.2  nathanw 	struct umct_softc *sc = priv;
    590  1.2.2.2  nathanw 	u_char *buf = sc->sc_intr_buf;
    591  1.2.2.5  nathanw 	u_char mstatus, lstatus;
    592  1.2.2.2  nathanw 
    593  1.2.2.2  nathanw 	if (sc->sc_dying)
    594  1.2.2.2  nathanw 		return;
    595  1.2.2.2  nathanw 
    596  1.2.2.2  nathanw 	if (status != USBD_NORMAL_COMPLETION) {
    597  1.2.2.2  nathanw 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    598  1.2.2.2  nathanw 			return;
    599  1.2.2.2  nathanw 
    600  1.2.2.2  nathanw 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    601  1.2.2.2  nathanw 			usbd_errstr(status)));
    602  1.2.2.2  nathanw 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    603  1.2.2.2  nathanw 		return;
    604  1.2.2.2  nathanw 	}
    605  1.2.2.2  nathanw 
    606  1.2.2.2  nathanw 	DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n",
    607  1.2.2.2  nathanw 		 USBDEVNAME(sc->sc_dev), buf[0],buf[1]));
    608  1.2.2.2  nathanw 
    609  1.2.2.2  nathanw 	sc->sc_lsr = sc->sc_msr = 0;
    610  1.2.2.2  nathanw 	mstatus = buf[0];
    611  1.2.2.2  nathanw 	lstatus = buf[1];
    612  1.2.2.2  nathanw 	if (ISSET(mstatus, MSR_DSR))
    613  1.2.2.2  nathanw 		sc->sc_msr |= UMSR_DSR;
    614  1.2.2.2  nathanw 	if (ISSET(mstatus, MSR_DCD))
    615  1.2.2.2  nathanw 		sc->sc_msr |= UMSR_DCD;
    616  1.2.2.2  nathanw 	ucom_status_change((struct ucom_softc *)sc->sc_subdev);
    617  1.2.2.2  nathanw }
    618  1.2.2.2  nathanw 
    619  1.2.2.2  nathanw void
    620  1.2.2.2  nathanw umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    621  1.2.2.2  nathanw {
    622  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    623  1.2.2.2  nathanw 
    624  1.2.2.2  nathanw 	DPRINTF(("umct_get_status:\n"));
    625  1.2.2.2  nathanw 
    626  1.2.2.2  nathanw 	if (lsr != NULL)
    627  1.2.2.2  nathanw 		*lsr = sc->sc_lsr;
    628  1.2.2.2  nathanw 	if (msr != NULL)
    629  1.2.2.2  nathanw 		*msr = sc->sc_msr;
    630  1.2.2.2  nathanw }
    631