Home | History | Annotate | Line # | Download | only in usb
umct.c revision 1.2.2.3
      1  1.2.2.3  nathanw /*	$NetBSD: umct.c,v 1.2.2.3 2001/11/14 19:16:19 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.2  nathanw  * This driver is for the device MCT USB-RS232 Interfact 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.2  nathanw 
     45  1.2.2.3  nathanw #include <sys/cdefs.h>
     46  1.2.2.3  nathanw __KERNEL_RCSID(0, "$NetBSD: umct.c,v 1.2.2.3 2001/11/14 19:16:19 nathanw Exp $");
     47  1.2.2.3  nathanw 
     48  1.2.2.2  nathanw #include <sys/param.h>
     49  1.2.2.2  nathanw #include <sys/systm.h>
     50  1.2.2.2  nathanw #include <sys/kernel.h>
     51  1.2.2.2  nathanw #include <sys/malloc.h>
     52  1.2.2.2  nathanw #include <sys/ioctl.h>
     53  1.2.2.2  nathanw #include <sys/conf.h>
     54  1.2.2.2  nathanw #include <sys/tty.h>
     55  1.2.2.2  nathanw #include <sys/file.h>
     56  1.2.2.2  nathanw #include <sys/select.h>
     57  1.2.2.2  nathanw #include <sys/proc.h>
     58  1.2.2.2  nathanw #include <sys/vnode.h>
     59  1.2.2.2  nathanw #include <sys/device.h>
     60  1.2.2.2  nathanw #include <sys/poll.h>
     61  1.2.2.2  nathanw 
     62  1.2.2.2  nathanw #include <dev/usb/usb.h>
     63  1.2.2.2  nathanw #include <dev/usb/usbcdc.h>
     64  1.2.2.2  nathanw 
     65  1.2.2.2  nathanw #include <dev/usb/usbdi.h>
     66  1.2.2.2  nathanw #include <dev/usb/usbdi_util.h>
     67  1.2.2.2  nathanw #include <dev/usb/usbdevs.h>
     68  1.2.2.2  nathanw #include <dev/usb/usb_quirks.h>
     69  1.2.2.2  nathanw 
     70  1.2.2.2  nathanw #include <dev/usb/usbdevs.h>
     71  1.2.2.2  nathanw #include <dev/usb/ucomvar.h>
     72  1.2.2.2  nathanw 
     73  1.2.2.2  nathanw #include <dev/usb/umct.h>
     74  1.2.2.2  nathanw 
     75  1.2.2.2  nathanw #ifdef UMCT_DEBUG
     76  1.2.2.2  nathanw #define DPRINTFN(n, x)  if (umctdebug > (n)) logprintf x
     77  1.2.2.2  nathanw int	umctdebug = 0;
     78  1.2.2.2  nathanw #else
     79  1.2.2.2  nathanw #define DPRINTFN(n, x)
     80  1.2.2.2  nathanw #endif
     81  1.2.2.2  nathanw #define DPRINTF(x) DPRINTFN(0, x)
     82  1.2.2.2  nathanw 
     83  1.2.2.2  nathanw #define	UMCT_CONFIG_INDEX	0
     84  1.2.2.2  nathanw #define	UMCT_IFACE_INDEX	0
     85  1.2.2.2  nathanw 
     86  1.2.2.2  nathanw struct	umct_softc {
     87  1.2.2.2  nathanw 	USBBASEDEVICE		sc_dev;		/* base device */
     88  1.2.2.2  nathanw 	usbd_device_handle	sc_udev;	/* USB device */
     89  1.2.2.2  nathanw 	usbd_interface_handle	sc_iface;	/* interface */
     90  1.2.2.2  nathanw 	int			sc_iface_number;	/* interface number */
     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.2  nathanw };
    111  1.2.2.2  nathanw 
    112  1.2.2.2  nathanw /*
    113  1.2.2.2  nathanw  * These are the maximum number of bytes transferred per frame.
    114  1.2.2.2  nathanw  * The output buffer size cannot be increased due to the size encoding.
    115  1.2.2.2  nathanw  */
    116  1.2.2.2  nathanw #define UMCTIBUFSIZE 256
    117  1.2.2.2  nathanw #define UMCTOBUFSIZE 256
    118  1.2.2.2  nathanw 
    119  1.2.2.2  nathanw Static	void umct_init(struct umct_softc *);
    120  1.2.2.2  nathanw Static	void umct_set_baudrate(struct umct_softc *, void *);
    121  1.2.2.2  nathanw Static	void umct_set_lcr(struct umct_softc *, void *);
    122  1.2.2.2  nathanw Static	void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    123  1.2.2.2  nathanw 
    124  1.2.2.2  nathanw Static	void umct_set(void *, int, int, int);
    125  1.2.2.2  nathanw Static	void umct_dtr(struct umct_softc *, int);
    126  1.2.2.2  nathanw Static	void umct_rts(struct umct_softc *, int);
    127  1.2.2.2  nathanw Static	void umct_break(struct umct_softc *, int);
    128  1.2.2.2  nathanw Static	void umct_set_line_state(struct umct_softc *);
    129  1.2.2.2  nathanw Static	void umct_get_status(void *, int portno, u_char *lsr, u_char *msr);
    130  1.2.2.2  nathanw Static	int  umct_param(void *, int, struct termios *);
    131  1.2.2.2  nathanw Static	int  umct_open(void *, int);
    132  1.2.2.2  nathanw Static	void umct_close(void *, int);
    133  1.2.2.2  nathanw 
    134  1.2.2.2  nathanw struct	ucom_methods umct_methods = {
    135  1.2.2.2  nathanw 	umct_get_status,
    136  1.2.2.2  nathanw 	umct_set,
    137  1.2.2.2  nathanw 	umct_param,
    138  1.2.2.2  nathanw 	NULL,
    139  1.2.2.2  nathanw 	umct_open,
    140  1.2.2.2  nathanw 	umct_close,
    141  1.2.2.2  nathanw 	NULL,
    142  1.2.2.2  nathanw 	NULL,
    143  1.2.2.2  nathanw };
    144  1.2.2.2  nathanw 
    145  1.2.2.2  nathanw static const struct umct_product {
    146  1.2.2.2  nathanw 	uint16_t	vendor;
    147  1.2.2.2  nathanw 	uint16_t	product;
    148  1.2.2.2  nathanw } umct_products [] = {
    149  1.2.2.2  nathanw 	/* MCT USB-232 Interface Products */
    150  1.2.2.2  nathanw 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
    151  1.2.2.2  nathanw 	/* Sitecom USB-232 Products */
    152  1.2.2.2  nathanw 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
    153  1.2.2.2  nathanw 	/* D-Link DU-H3SP USB BAY Hub Products */
    154  1.2.2.2  nathanw 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
    155  1.2.2.2  nathanw 
    156  1.2.2.2  nathanw 	{ 0, 0 }
    157  1.2.2.2  nathanw };
    158  1.2.2.2  nathanw 
    159  1.2.2.2  nathanw USB_DECLARE_DRIVER(umct);
    160  1.2.2.2  nathanw 
    161  1.2.2.2  nathanw USB_MATCH(umct)
    162  1.2.2.2  nathanw {
    163  1.2.2.2  nathanw 	USB_MATCH_START(umct, uaa);
    164  1.2.2.2  nathanw 	int i;
    165  1.2.2.2  nathanw 
    166  1.2.2.2  nathanw 	if (uaa->iface != NULL)
    167  1.2.2.2  nathanw 		return (UMATCH_NONE);
    168  1.2.2.2  nathanw 
    169  1.2.2.2  nathanw 	for (i = 0; umct_products[i].vendor != 0; i++) {
    170  1.2.2.2  nathanw 		if (umct_products[i].vendor == uaa->vendor &&
    171  1.2.2.2  nathanw  		    umct_products[i].product == uaa->product) {
    172  1.2.2.2  nathanw 			return (UMATCH_VENDOR_PRODUCT);
    173  1.2.2.2  nathanw 		}
    174  1.2.2.2  nathanw 	}
    175  1.2.2.2  nathanw 	return (UMATCH_NONE);
    176  1.2.2.2  nathanw }
    177  1.2.2.2  nathanw 
    178  1.2.2.2  nathanw USB_ATTACH(umct)
    179  1.2.2.2  nathanw {
    180  1.2.2.2  nathanw 	USB_ATTACH_START(umct, sc, uaa);
    181  1.2.2.2  nathanw 	usbd_device_handle dev = uaa->device;
    182  1.2.2.2  nathanw 	usb_config_descriptor_t *cdesc;
    183  1.2.2.2  nathanw 	usb_interface_descriptor_t *id;
    184  1.2.2.2  nathanw 	usb_endpoint_descriptor_t *ed;
    185  1.2.2.2  nathanw 
    186  1.2.2.2  nathanw 	char devinfo[1024];
    187  1.2.2.2  nathanw 	char *devname = USBDEVNAME(sc->sc_dev);
    188  1.2.2.2  nathanw 	usbd_status err;
    189  1.2.2.2  nathanw 	int i,found;
    190  1.2.2.2  nathanw 	struct ucom_attach_args uca;
    191  1.2.2.2  nathanw 
    192  1.2.2.2  nathanw         usbd_devinfo(dev, 0, devinfo);
    193  1.2.2.2  nathanw         USB_ATTACH_SETUP;
    194  1.2.2.2  nathanw         printf("%s: %s\n", devname, devinfo);
    195  1.2.2.2  nathanw 
    196  1.2.2.2  nathanw         sc->sc_udev = dev;
    197  1.2.2.2  nathanw 
    198  1.2.2.2  nathanw 	DPRINTF(("\n\numct attach: sc=%p\n", sc));
    199  1.2.2.2  nathanw 
    200  1.2.2.2  nathanw 	/* initialize endpoints */
    201  1.2.2.2  nathanw 	uca.bulkin = uca.bulkout = -1;
    202  1.2.2.2  nathanw 	sc->sc_intr_number = -1;
    203  1.2.2.2  nathanw 	sc->sc_intr_pipe = NULL;
    204  1.2.2.2  nathanw 
    205  1.2.2.2  nathanw 	/* Move the device into the configured state. */
    206  1.2.2.2  nathanw 	err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
    207  1.2.2.2  nathanw 	if (err) {
    208  1.2.2.2  nathanw 		printf("\n%s: failed to set configuration, err=%s\n",
    209  1.2.2.2  nathanw 			devname, usbd_errstr(err));
    210  1.2.2.2  nathanw 		sc->sc_dying = 1;
    211  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    212  1.2.2.2  nathanw 	}
    213  1.2.2.2  nathanw 
    214  1.2.2.2  nathanw 	/* get the config descriptor */
    215  1.2.2.2  nathanw 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    216  1.2.2.2  nathanw 
    217  1.2.2.2  nathanw 	if (cdesc == NULL) {
    218  1.2.2.2  nathanw 		printf("%s: failed to get configuration descriptor\n",
    219  1.2.2.2  nathanw 			USBDEVNAME(sc->sc_dev));
    220  1.2.2.2  nathanw 		sc->sc_dying = 1;
    221  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    222  1.2.2.2  nathanw 	}
    223  1.2.2.2  nathanw 
    224  1.2.2.2  nathanw 	/* get the interface */
    225  1.2.2.2  nathanw 	err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
    226  1.2.2.2  nathanw 							&sc->sc_iface);
    227  1.2.2.2  nathanw 	if (err) {
    228  1.2.2.2  nathanw 		printf("\n%s: failed to get interface, err=%s\n",
    229  1.2.2.2  nathanw 			devname, usbd_errstr(err));
    230  1.2.2.2  nathanw 		sc->sc_dying = 1;
    231  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    232  1.2.2.2  nathanw 	}
    233  1.2.2.2  nathanw 
    234  1.2.2.2  nathanw 	/* Find the bulk{in,out} and interrupt endpoints */
    235  1.2.2.2  nathanw 
    236  1.2.2.2  nathanw 	id = usbd_get_interface_descriptor(sc->sc_iface);
    237  1.2.2.2  nathanw 	sc->sc_iface_number = id->bInterfaceNumber;
    238  1.2.2.2  nathanw 	found = 0;
    239  1.2.2.2  nathanw 
    240  1.2.2.2  nathanw 	for (i = 0; i < id->bNumEndpoints; i++) {
    241  1.2.2.2  nathanw 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    242  1.2.2.2  nathanw 		if (ed == NULL) {
    243  1.2.2.2  nathanw 			printf("%s: no endpoint descriptor for %d\n",
    244  1.2.2.2  nathanw 				USBDEVNAME(sc->sc_dev), i);
    245  1.2.2.2  nathanw 			sc->sc_dying = 1;
    246  1.2.2.2  nathanw 			USB_ATTACH_ERROR_RETURN;
    247  1.2.2.2  nathanw 		}
    248  1.2.2.2  nathanw 
    249  1.2.2.2  nathanw 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    250  1.2.2.2  nathanw 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
    251  1.2.2.2  nathanw 		    found == 0) {
    252  1.2.2.2  nathanw 			uca.bulkin = ed->bEndpointAddress;
    253  1.2.2.2  nathanw 			found = 1;
    254  1.2.2.2  nathanw 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    255  1.2.2.2  nathanw 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    256  1.2.2.2  nathanw 			uca.bulkout = ed->bEndpointAddress;
    257  1.2.2.2  nathanw 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    258  1.2.2.2  nathanw 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    259  1.2.2.2  nathanw 			sc->sc_intr_number = ed->bEndpointAddress;
    260  1.2.2.2  nathanw 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    261  1.2.2.2  nathanw 		}
    262  1.2.2.2  nathanw 	}
    263  1.2.2.2  nathanw 
    264  1.2.2.2  nathanw 	if (uca.bulkin == -1) {
    265  1.2.2.2  nathanw 		printf("%s: Could not find data bulk in\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 (uca.bulkout == -1) {
    272  1.2.2.2  nathanw 		printf("%s: Could not find data bulk out\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 	if (sc->sc_intr_number== -1) {
    279  1.2.2.2  nathanw 		printf("%s: Could not find interrupt in\n",
    280  1.2.2.2  nathanw 			USBDEVNAME(sc->sc_dev));
    281  1.2.2.2  nathanw 		sc->sc_dying = 1;
    282  1.2.2.2  nathanw 		USB_ATTACH_ERROR_RETURN;
    283  1.2.2.2  nathanw 	}
    284  1.2.2.2  nathanw 
    285  1.2.2.2  nathanw 	sc->sc_dtr = sc->sc_rts = 0;
    286  1.2.2.2  nathanw 	uca.portno = UCOM_UNK_PORTNO;
    287  1.2.2.2  nathanw 	/* bulkin, bulkout set above */
    288  1.2.2.2  nathanw 	uca.ibufsize = UMCTIBUFSIZE;
    289  1.2.2.2  nathanw 	uca.obufsize = UMCTOBUFSIZE;
    290  1.2.2.2  nathanw 	uca.ibufsizepad = UMCTIBUFSIZE;
    291  1.2.2.2  nathanw 	uca.opkthdrlen = 0;
    292  1.2.2.2  nathanw 	uca.device = dev;
    293  1.2.2.2  nathanw 	uca.iface = sc->sc_iface;
    294  1.2.2.2  nathanw 	uca.methods = &umct_methods;
    295  1.2.2.2  nathanw 	uca.arg = sc;
    296  1.2.2.2  nathanw 	uca.info = NULL;
    297  1.2.2.2  nathanw 
    298  1.2.2.2  nathanw 	umct_init(sc);
    299  1.2.2.2  nathanw 
    300  1.2.2.2  nathanw 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    301  1.2.2.2  nathanw 			   USBDEV(sc->sc_dev));
    302  1.2.2.2  nathanw 
    303  1.2.2.2  nathanw 	DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n",
    304  1.2.2.2  nathanw 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    305  1.2.2.2  nathanw 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    306  1.2.2.2  nathanw 
    307  1.2.2.2  nathanw 	USB_ATTACH_SUCCESS_RETURN;
    308  1.2.2.2  nathanw }
    309  1.2.2.2  nathanw 
    310  1.2.2.2  nathanw USB_DETACH(umct)
    311  1.2.2.2  nathanw {
    312  1.2.2.2  nathanw 	USB_DETACH_START(umct, sc);
    313  1.2.2.2  nathanw 	int rv = 0;
    314  1.2.2.2  nathanw 
    315  1.2.2.2  nathanw 	DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags));
    316  1.2.2.2  nathanw 
    317  1.2.2.2  nathanw         if (sc->sc_intr_pipe != NULL) {
    318  1.2.2.2  nathanw                 usbd_abort_pipe(sc->sc_intr_pipe);
    319  1.2.2.2  nathanw                 usbd_close_pipe(sc->sc_intr_pipe);
    320  1.2.2.2  nathanw 		free(sc->sc_intr_buf, M_USBDEV);
    321  1.2.2.2  nathanw                 sc->sc_intr_pipe = NULL;
    322  1.2.2.2  nathanw         }
    323  1.2.2.2  nathanw 
    324  1.2.2.2  nathanw 	sc->sc_dying = 1;
    325  1.2.2.2  nathanw 	if (sc->sc_subdev != NULL) {
    326  1.2.2.2  nathanw 		rv = config_detach(sc->sc_subdev, flags);
    327  1.2.2.2  nathanw 		sc->sc_subdev = NULL;
    328  1.2.2.2  nathanw 	}
    329  1.2.2.2  nathanw 
    330  1.2.2.2  nathanw 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    331  1.2.2.2  nathanw 			   USBDEV(sc->sc_dev));
    332  1.2.2.2  nathanw 
    333  1.2.2.2  nathanw 	return (rv);
    334  1.2.2.2  nathanw }
    335  1.2.2.2  nathanw 
    336  1.2.2.2  nathanw int
    337  1.2.2.2  nathanw umct_activate(device_ptr_t self, enum devact act)
    338  1.2.2.2  nathanw {
    339  1.2.2.2  nathanw 	struct umct_softc *sc = (struct umct_softc *)self;
    340  1.2.2.2  nathanw 	int rv = 0;
    341  1.2.2.2  nathanw 
    342  1.2.2.2  nathanw 	switch (act) {
    343  1.2.2.2  nathanw 	case DVACT_ACTIVATE:
    344  1.2.2.2  nathanw 		return (EOPNOTSUPP);
    345  1.2.2.2  nathanw 		break;
    346  1.2.2.2  nathanw 
    347  1.2.2.2  nathanw 	case DVACT_DEACTIVATE:
    348  1.2.2.2  nathanw 		if (sc->sc_subdev != NULL)
    349  1.2.2.2  nathanw 			rv = config_deactivate(sc->sc_subdev);
    350  1.2.2.2  nathanw 		sc->sc_dying = 1;
    351  1.2.2.2  nathanw 		break;
    352  1.2.2.2  nathanw 	}
    353  1.2.2.2  nathanw 	return (rv);
    354  1.2.2.2  nathanw }
    355  1.2.2.2  nathanw 
    356  1.2.2.2  nathanw void
    357  1.2.2.2  nathanw umct_set_line_state(struct umct_softc *sc)
    358  1.2.2.2  nathanw {
    359  1.2.2.2  nathanw 	usb_device_request_t req;
    360  1.2.2.2  nathanw 	int ls = MCR_NONE;
    361  1.2.2.2  nathanw 
    362  1.2.2.2  nathanw 	ls = (sc->sc_dtr ? MCR_DTR : 0) |
    363  1.2.2.2  nathanw 		(sc->sc_rts ? MCR_RTS : 0);
    364  1.2.2.2  nathanw 
    365  1.2.2.2  nathanw 	DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n",
    366  1.2.2.2  nathanw 			sc->sc_dtr, sc->sc_rts, ls));
    367  1.2.2.2  nathanw 
    368  1.2.2.2  nathanw 	req.bmRequestType = UMCT_SET_REQUEST;
    369  1.2.2.2  nathanw 	req.bRequest = REQ_SET_MCR;
    370  1.2.2.2  nathanw 	USETW(req.wValue, 0);
    371  1.2.2.2  nathanw 	USETW(req.wIndex, sc->sc_iface_number);
    372  1.2.2.2  nathanw 	USETW(req.wLength, LENGTH_SET_MCR);
    373  1.2.2.2  nathanw 
    374  1.2.2.2  nathanw 	(void)usbd_do_request(sc->sc_udev, &req, &ls);
    375  1.2.2.2  nathanw }
    376  1.2.2.2  nathanw 
    377  1.2.2.2  nathanw void
    378  1.2.2.2  nathanw umct_set(void *addr, int portno, int reg, int onoff)
    379  1.2.2.2  nathanw {
    380  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    381  1.2.2.2  nathanw 
    382  1.2.2.2  nathanw 	switch (reg) {
    383  1.2.2.2  nathanw 	case UCOM_SET_DTR:
    384  1.2.2.2  nathanw 		umct_dtr(sc, onoff);
    385  1.2.2.2  nathanw 		break;
    386  1.2.2.2  nathanw 	case UCOM_SET_RTS:
    387  1.2.2.2  nathanw 		umct_rts(sc, onoff);
    388  1.2.2.2  nathanw 		break;
    389  1.2.2.2  nathanw 	case UCOM_SET_BREAK:
    390  1.2.2.2  nathanw 		umct_break(sc, onoff);
    391  1.2.2.2  nathanw 		break;
    392  1.2.2.2  nathanw 	default:
    393  1.2.2.2  nathanw 		break;
    394  1.2.2.2  nathanw 	}
    395  1.2.2.2  nathanw }
    396  1.2.2.2  nathanw 
    397  1.2.2.2  nathanw void
    398  1.2.2.2  nathanw umct_dtr(struct umct_softc *sc, int onoff)
    399  1.2.2.2  nathanw {
    400  1.2.2.2  nathanw 
    401  1.2.2.2  nathanw 	DPRINTF(("umct_dtr: onoff=%d\n", onoff));
    402  1.2.2.2  nathanw 
    403  1.2.2.2  nathanw 	if (sc->sc_dtr == onoff)
    404  1.2.2.2  nathanw 		return;
    405  1.2.2.2  nathanw 	sc->sc_dtr = onoff;
    406  1.2.2.2  nathanw 
    407  1.2.2.2  nathanw 	umct_set_line_state(sc);
    408  1.2.2.2  nathanw }
    409  1.2.2.2  nathanw 
    410  1.2.2.2  nathanw void
    411  1.2.2.2  nathanw umct_rts(struct umct_softc *sc, int onoff)
    412  1.2.2.2  nathanw {
    413  1.2.2.2  nathanw 	DPRINTF(("umct_rts: onoff=%d\n", onoff));
    414  1.2.2.2  nathanw 
    415  1.2.2.2  nathanw 	if (sc->sc_rts == onoff)
    416  1.2.2.2  nathanw 		return;
    417  1.2.2.2  nathanw 	sc->sc_rts = onoff;
    418  1.2.2.2  nathanw 
    419  1.2.2.2  nathanw 	umct_set_line_state(sc);
    420  1.2.2.2  nathanw }
    421  1.2.2.2  nathanw 
    422  1.2.2.2  nathanw void
    423  1.2.2.2  nathanw umct_break(struct umct_softc *sc, int onoff)
    424  1.2.2.2  nathanw {
    425  1.2.2.2  nathanw 	u_char data;
    426  1.2.2.2  nathanw 
    427  1.2.2.2  nathanw 	DPRINTF(("umct_break: onoff=%d\n", onoff));
    428  1.2.2.2  nathanw 
    429  1.2.2.2  nathanw 	data = onoff ? LCR_SET_BREAK : 0;
    430  1.2.2.2  nathanw 
    431  1.2.2.2  nathanw 	umct_set_lcr(sc, &data);
    432  1.2.2.2  nathanw }
    433  1.2.2.2  nathanw 
    434  1.2.2.2  nathanw void
    435  1.2.2.2  nathanw umct_set_lcr(struct umct_softc *sc, void *data)
    436  1.2.2.2  nathanw {
    437  1.2.2.2  nathanw 	usb_device_request_t req;
    438  1.2.2.2  nathanw 
    439  1.2.2.2  nathanw 	req.bmRequestType = UMCT_SET_REQUEST;
    440  1.2.2.2  nathanw 	req.bRequest = REQ_SET_LCR;
    441  1.2.2.2  nathanw 	USETW(req.wValue, 0);
    442  1.2.2.2  nathanw 	USETW(req.wIndex, sc->sc_iface_number);
    443  1.2.2.2  nathanw 	USETW(req.wLength, LENGTH_SET_LCR);
    444  1.2.2.2  nathanw 
    445  1.2.2.2  nathanw 	(void)usbd_do_request(sc->sc_udev, &req, data);
    446  1.2.2.2  nathanw }
    447  1.2.2.2  nathanw 
    448  1.2.2.2  nathanw void
    449  1.2.2.2  nathanw umct_set_baudrate(struct umct_softc *sc, void *rate)
    450  1.2.2.2  nathanw {
    451  1.2.2.2  nathanw         usb_device_request_t req;
    452  1.2.2.2  nathanw 
    453  1.2.2.2  nathanw         req.bmRequestType = UMCT_SET_REQUEST;
    454  1.2.2.2  nathanw         req.bRequest = REQ_SET_BAUD_RATE;
    455  1.2.2.2  nathanw         USETW(req.wValue, 0);
    456  1.2.2.2  nathanw         USETW(req.wIndex, sc->sc_iface_number);
    457  1.2.2.2  nathanw         USETW(req.wLength, LENGTH_BAUD_RATE);
    458  1.2.2.2  nathanw 
    459  1.2.2.2  nathanw         (void)usbd_do_request(sc->sc_udev, &req, rate);
    460  1.2.2.2  nathanw }
    461  1.2.2.2  nathanw 
    462  1.2.2.2  nathanw void
    463  1.2.2.2  nathanw umct_init(struct umct_softc *sc)
    464  1.2.2.2  nathanw {
    465  1.2.2.2  nathanw 	int brate, data;
    466  1.2.2.2  nathanw 
    467  1.2.2.2  nathanw 	brate = UMCT_BAUD_RATE(9600);
    468  1.2.2.2  nathanw 	data |= LCR_DATA_BITS_8 | LCR_PARITY_NONE |
    469  1.2.2.2  nathanw 		LCR_STOP_BITS_1;
    470  1.2.2.2  nathanw 
    471  1.2.2.2  nathanw 	umct_set_baudrate(sc, &brate);
    472  1.2.2.2  nathanw 	umct_set_lcr(sc, &data);
    473  1.2.2.2  nathanw }
    474  1.2.2.2  nathanw 
    475  1.2.2.2  nathanw int
    476  1.2.2.2  nathanw umct_param(void *addr, int portno, struct termios *t)
    477  1.2.2.2  nathanw {
    478  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    479  1.2.2.2  nathanw 	int divisor = 0;
    480  1.2.2.2  nathanw 	u_char data = NULL;
    481  1.2.2.2  nathanw 
    482  1.2.2.2  nathanw 	DPRINTF(("umct_param: sc=%p\n", sc));
    483  1.2.2.2  nathanw 
    484  1.2.2.2  nathanw 	divisor = UMCT_BAUD_RATE(t->c_ospeed);
    485  1.2.2.2  nathanw 	DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed));
    486  1.2.2.2  nathanw 
    487  1.2.2.2  nathanw 	if (ISSET(t->c_cflag, CSTOPB))
    488  1.2.2.2  nathanw 		data |= LCR_STOP_BITS_2;
    489  1.2.2.2  nathanw 	else
    490  1.2.2.2  nathanw 		data |= LCR_STOP_BITS_1;
    491  1.2.2.2  nathanw 	if (ISSET(t->c_cflag, PARENB)) {
    492  1.2.2.2  nathanw 		if (ISSET(t->c_cflag, PARODD))
    493  1.2.2.2  nathanw 			data |= LCR_PARITY_ODD;
    494  1.2.2.2  nathanw 		else
    495  1.2.2.2  nathanw 			data |= LCR_PARITY_EVEN;
    496  1.2.2.2  nathanw 	} else
    497  1.2.2.2  nathanw 		data |= LCR_PARITY_NONE;
    498  1.2.2.2  nathanw 	switch (ISSET(t->c_cflag, CSIZE)) {
    499  1.2.2.2  nathanw 	case CS5:
    500  1.2.2.2  nathanw 		data |= LCR_DATA_BITS_5;
    501  1.2.2.2  nathanw 		break;
    502  1.2.2.2  nathanw 	case CS6:
    503  1.2.2.2  nathanw 		data |= LCR_DATA_BITS_6;
    504  1.2.2.2  nathanw 		break;
    505  1.2.2.2  nathanw 	case CS7:
    506  1.2.2.2  nathanw 		data |= LCR_DATA_BITS_7;
    507  1.2.2.2  nathanw 		break;
    508  1.2.2.2  nathanw 	case CS8:
    509  1.2.2.2  nathanw 		data |= LCR_DATA_BITS_8;
    510  1.2.2.2  nathanw 		break;
    511  1.2.2.2  nathanw 	}
    512  1.2.2.2  nathanw 
    513  1.2.2.2  nathanw 	umct_set_baudrate(sc, &divisor);
    514  1.2.2.2  nathanw 
    515  1.2.2.2  nathanw 	umct_set_lcr(sc, &data);
    516  1.2.2.2  nathanw 
    517  1.2.2.2  nathanw 	return (0);
    518  1.2.2.2  nathanw }
    519  1.2.2.2  nathanw 
    520  1.2.2.2  nathanw int
    521  1.2.2.2  nathanw umct_open(void *addr, int portno)
    522  1.2.2.2  nathanw {
    523  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    524  1.2.2.2  nathanw 	int err, lcr_data;
    525  1.2.2.2  nathanw 
    526  1.2.2.2  nathanw 	if (sc->sc_dying)
    527  1.2.2.2  nathanw 		return (EIO);
    528  1.2.2.2  nathanw 
    529  1.2.2.2  nathanw 	DPRINTF(("umct_open: sc=%p\n", sc));
    530  1.2.2.2  nathanw 
    531  1.2.2.2  nathanw 	/* initialize LCR */
    532  1.2.2.2  nathanw         lcr_data |= LCR_DATA_BITS_8 | LCR_PARITY_NONE |
    533  1.2.2.2  nathanw                 LCR_STOP_BITS_1;
    534  1.2.2.2  nathanw         umct_set_lcr(sc, &lcr_data);
    535  1.2.2.2  nathanw 
    536  1.2.2.2  nathanw 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    537  1.2.2.2  nathanw 		sc->sc_status = 0; /* clear status bit */
    538  1.2.2.2  nathanw 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    539  1.2.2.2  nathanw 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
    540  1.2.2.2  nathanw 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    541  1.2.2.2  nathanw 			sc->sc_intr_buf, sc->sc_isize,
    542  1.2.2.2  nathanw 			umct_intr, USBD_DEFAULT_INTERVAL);
    543  1.2.2.2  nathanw 		if (err) {
    544  1.2.2.2  nathanw 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    545  1.2.2.2  nathanw 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
    546  1.2.2.2  nathanw 					return (EIO);
    547  1.2.2.2  nathanw 		}
    548  1.2.2.2  nathanw 	}
    549  1.2.2.2  nathanw 
    550  1.2.2.2  nathanw 	return (0);
    551  1.2.2.2  nathanw }
    552  1.2.2.2  nathanw 
    553  1.2.2.2  nathanw void
    554  1.2.2.2  nathanw umct_close(void *addr, int portno)
    555  1.2.2.2  nathanw {
    556  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    557  1.2.2.2  nathanw 	int err;
    558  1.2.2.2  nathanw 
    559  1.2.2.2  nathanw 	if (sc->sc_dying)
    560  1.2.2.2  nathanw 		return;
    561  1.2.2.2  nathanw 
    562  1.2.2.2  nathanw 	DPRINTF(("umct_close: close\n"));
    563  1.2.2.2  nathanw 
    564  1.2.2.2  nathanw 	if (sc->sc_intr_pipe != NULL) {
    565  1.2.2.2  nathanw 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    566  1.2.2.2  nathanw 		if (err)
    567  1.2.2.2  nathanw 			printf("%s: abort interrupt pipe failed: %s\n",
    568  1.2.2.2  nathanw 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    569  1.2.2.2  nathanw 		err = usbd_close_pipe(sc->sc_intr_pipe);
    570  1.2.2.2  nathanw 		if (err)
    571  1.2.2.2  nathanw 			printf("%s: close interrupt pipe failed: %s\n",
    572  1.2.2.2  nathanw 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    573  1.2.2.2  nathanw 		free(sc->sc_intr_buf, M_USBDEV);
    574  1.2.2.2  nathanw 		sc->sc_intr_pipe = NULL;
    575  1.2.2.2  nathanw 	}
    576  1.2.2.2  nathanw }
    577  1.2.2.2  nathanw 
    578  1.2.2.2  nathanw void
    579  1.2.2.2  nathanw umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    580  1.2.2.2  nathanw {
    581  1.2.2.2  nathanw 	struct umct_softc *sc = priv;
    582  1.2.2.2  nathanw 	u_char *buf = sc->sc_intr_buf;
    583  1.2.2.2  nathanw 	u_char mstatus, lstatus;
    584  1.2.2.2  nathanw 
    585  1.2.2.2  nathanw 	if (sc->sc_dying)
    586  1.2.2.2  nathanw 		return;
    587  1.2.2.2  nathanw 
    588  1.2.2.2  nathanw 	if (status != USBD_NORMAL_COMPLETION) {
    589  1.2.2.2  nathanw 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    590  1.2.2.2  nathanw 			return;
    591  1.2.2.2  nathanw 
    592  1.2.2.2  nathanw 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    593  1.2.2.2  nathanw 			usbd_errstr(status)));
    594  1.2.2.2  nathanw 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    595  1.2.2.2  nathanw 		return;
    596  1.2.2.2  nathanw 	}
    597  1.2.2.2  nathanw 
    598  1.2.2.2  nathanw 	DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n",
    599  1.2.2.2  nathanw 		 USBDEVNAME(sc->sc_dev), buf[0],buf[1]));
    600  1.2.2.2  nathanw 
    601  1.2.2.2  nathanw 	sc->sc_lsr = sc->sc_msr = 0;
    602  1.2.2.2  nathanw 	mstatus = buf[0];
    603  1.2.2.2  nathanw 	lstatus = buf[1];
    604  1.2.2.2  nathanw 	if (ISSET(mstatus, MSR_DSR))
    605  1.2.2.2  nathanw 		sc->sc_msr |= UMSR_DSR;
    606  1.2.2.2  nathanw 	if (ISSET(mstatus, MSR_DCD))
    607  1.2.2.2  nathanw 		sc->sc_msr |= UMSR_DCD;
    608  1.2.2.2  nathanw 	ucom_status_change((struct ucom_softc *)sc->sc_subdev);
    609  1.2.2.2  nathanw }
    610  1.2.2.2  nathanw 
    611  1.2.2.2  nathanw void
    612  1.2.2.2  nathanw umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    613  1.2.2.2  nathanw {
    614  1.2.2.2  nathanw 	struct umct_softc *sc = addr;
    615  1.2.2.2  nathanw 
    616  1.2.2.2  nathanw 	DPRINTF(("umct_get_status:\n"));
    617  1.2.2.2  nathanw 
    618  1.2.2.2  nathanw 	if (lsr != NULL)
    619  1.2.2.2  nathanw 		*lsr = sc->sc_lsr;
    620  1.2.2.2  nathanw 	if (msr != NULL)
    621  1.2.2.2  nathanw 		*msr = sc->sc_msr;
    622  1.2.2.2  nathanw }
    623