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