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