Home | History | Annotate | Line # | Download | only in usb
umct.c revision 1.29.4.1
      1  1.29.4.1     rmind /*	$NetBSD: umct.c,v 1.29.4.1 2011/03/05 20:54:15 rmind 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.29.4.1     rmind __KERNEL_RCSID(0, "$NetBSD: umct.c,v 1.29.4.1 2011/03/05 20:54:15 rmind 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/malloc.h>
     44       1.1    ichiro #include <sys/ioctl.h>
     45       1.1    ichiro #include <sys/conf.h>
     46       1.1    ichiro #include <sys/tty.h>
     47       1.1    ichiro #include <sys/file.h>
     48       1.1    ichiro #include <sys/select.h>
     49       1.1    ichiro #include <sys/proc.h>
     50       1.1    ichiro #include <sys/vnode.h>
     51       1.1    ichiro #include <sys/device.h>
     52       1.1    ichiro #include <sys/poll.h>
     53       1.1    ichiro 
     54       1.1    ichiro #include <dev/usb/usb.h>
     55       1.1    ichiro #include <dev/usb/usbcdc.h>
     56       1.1    ichiro 
     57       1.1    ichiro #include <dev/usb/usbdi.h>
     58       1.1    ichiro #include <dev/usb/usbdi_util.h>
     59       1.1    ichiro #include <dev/usb/usbdevs.h>
     60       1.1    ichiro #include <dev/usb/usb_quirks.h>
     61       1.1    ichiro 
     62       1.1    ichiro #include <dev/usb/ucomvar.h>
     63       1.1    ichiro #include <dev/usb/umct.h>
     64       1.1    ichiro 
     65       1.1    ichiro #ifdef UMCT_DEBUG
     66  1.29.4.1     rmind #define DPRINTFN(n, x)  if (umctdebug > (n)) printf x
     67       1.1    ichiro int	umctdebug = 0;
     68       1.1    ichiro #else
     69       1.1    ichiro #define DPRINTFN(n, x)
     70       1.1    ichiro #endif
     71       1.1    ichiro #define DPRINTF(x) DPRINTFN(0, x)
     72       1.1    ichiro 
     73       1.1    ichiro #define	UMCT_CONFIG_INDEX	0
     74       1.1    ichiro #define	UMCT_IFACE_INDEX	0
     75       1.1    ichiro 
     76       1.1    ichiro struct	umct_softc {
     77  1.29.4.1     rmind 	device_t		sc_dev;		/* base device */
     78       1.1    ichiro 	usbd_device_handle	sc_udev;	/* USB device */
     79       1.1    ichiro 	usbd_interface_handle	sc_iface;	/* interface */
     80       1.1    ichiro 	int			sc_iface_number;	/* interface number */
     81       1.5  augustss 	u_int16_t		sc_product;
     82       1.1    ichiro 
     83       1.1    ichiro 	int			sc_intr_number;	/* interrupt number */
     84       1.1    ichiro 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
     85       1.1    ichiro 	u_char			*sc_intr_buf;	/* interrupt buffer */
     86       1.1    ichiro 	int			sc_isize;
     87       1.1    ichiro 
     88       1.1    ichiro 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
     89       1.1    ichiro 	u_char			sc_dtr;		/* current DTR state */
     90       1.1    ichiro 	u_char			sc_rts;		/* current RTS state */
     91       1.1    ichiro 	u_char			sc_break;	/* set break */
     92       1.1    ichiro 
     93       1.1    ichiro 	u_char			sc_status;
     94       1.1    ichiro 
     95      1.23    dyoung 	device_t		sc_subdev;	/* ucom device */
     96       1.1    ichiro 
     97       1.1    ichiro 	u_char			sc_dying;	/* disconnecting */
     98       1.1    ichiro 
     99       1.1    ichiro 	u_char			sc_lsr;		/* Local status register */
    100       1.1    ichiro 	u_char			sc_msr;		/* umct status register */
    101       1.7    ichiro 
    102       1.7    ichiro 	u_int			last_lcr;	/* keep lcr register */
    103       1.1    ichiro };
    104       1.1    ichiro 
    105       1.1    ichiro /*
    106       1.1    ichiro  * These are the maximum number of bytes transferred per frame.
    107       1.1    ichiro  * The output buffer size cannot be increased due to the size encoding.
    108       1.1    ichiro  */
    109       1.1    ichiro #define UMCTIBUFSIZE 256
    110       1.1    ichiro #define UMCTOBUFSIZE 256
    111       1.1    ichiro 
    112       1.1    ichiro Static	void umct_init(struct umct_softc *);
    113       1.5  augustss Static	void umct_set_baudrate(struct umct_softc *, u_int);
    114       1.5  augustss Static	void umct_set_lcr(struct umct_softc *, u_int);
    115       1.1    ichiro Static	void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    116       1.1    ichiro 
    117       1.1    ichiro Static	void umct_set(void *, int, int, int);
    118       1.1    ichiro Static	void umct_dtr(struct umct_softc *, int);
    119       1.1    ichiro Static	void umct_rts(struct umct_softc *, int);
    120       1.1    ichiro Static	void umct_break(struct umct_softc *, int);
    121       1.1    ichiro Static	void umct_set_line_state(struct umct_softc *);
    122       1.1    ichiro Static	void umct_get_status(void *, int portno, u_char *lsr, u_char *msr);
    123       1.1    ichiro Static	int  umct_param(void *, int, struct termios *);
    124       1.1    ichiro Static	int  umct_open(void *, int);
    125       1.1    ichiro Static	void umct_close(void *, int);
    126       1.1    ichiro 
    127       1.1    ichiro struct	ucom_methods umct_methods = {
    128       1.1    ichiro 	umct_get_status,
    129       1.1    ichiro 	umct_set,
    130       1.1    ichiro 	umct_param,
    131       1.1    ichiro 	NULL,
    132       1.1    ichiro 	umct_open,
    133       1.1    ichiro 	umct_close,
    134       1.1    ichiro 	NULL,
    135       1.1    ichiro 	NULL,
    136       1.1    ichiro };
    137       1.1    ichiro 
    138       1.4  augustss static const struct usb_devno umct_devs[] = {
    139       1.1    ichiro 	/* MCT USB-232 Interface Products */
    140       1.1    ichiro 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
    141       1.1    ichiro 	/* Sitecom USB-232 Products */
    142       1.1    ichiro 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
    143       1.1    ichiro 	/* D-Link DU-H3SP USB BAY Hub Products */
    144       1.1    ichiro 	{ USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
    145      1.11  augustss 	/* BELKIN F5U109 */
    146      1.11  augustss 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109 },
    147       1.1    ichiro };
    148       1.4  augustss #define umct_lookup(v, p) usb_lookup(umct_devs, v, p)
    149       1.1    ichiro 
    150      1.27    cegger int umct_match(device_t, cfdata_t, void *);
    151      1.23    dyoung void umct_attach(device_t, device_t, void *);
    152      1.23    dyoung void umct_childdet(device_t, device_t);
    153      1.23    dyoung int umct_detach(device_t, int);
    154      1.23    dyoung int umct_activate(device_t, enum devact);
    155      1.23    dyoung extern struct cfdriver umct_cd;
    156      1.26      cube CFATTACH_DECL2_NEW(umct, sizeof(struct umct_softc), umct_match,
    157      1.23    dyoung     umct_attach, umct_detach, umct_activate, NULL, umct_childdet);
    158       1.1    ichiro 
    159  1.29.4.1     rmind int
    160  1.29.4.1     rmind umct_match(device_t parent, cfdata_t match, void *aux)
    161       1.1    ichiro {
    162  1.29.4.1     rmind 	struct usb_attach_arg *uaa = aux;
    163       1.1    ichiro 
    164       1.4  augustss 	return (umct_lookup(uaa->vendor, uaa->product) != NULL ?
    165       1.4  augustss 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    166       1.1    ichiro }
    167       1.1    ichiro 
    168  1.29.4.1     rmind void
    169  1.29.4.1     rmind umct_attach(device_t parent, device_t self, void *aux)
    170       1.1    ichiro {
    171  1.29.4.1     rmind 	struct umct_softc *sc = device_private(self);
    172  1.29.4.1     rmind 	struct usb_attach_arg *uaa = aux;
    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.16  augustss 	char *devinfop;
    179       1.1    ichiro 	usbd_status err;
    180      1.14    rumble 	int i;
    181       1.1    ichiro 	struct ucom_attach_args uca;
    182       1.1    ichiro 
    183      1.26      cube 	sc->sc_dev = self;
    184      1.26      cube 
    185      1.28    plunky 	aprint_naive("\n");
    186      1.28    plunky 	aprint_normal("\n");
    187      1.28    plunky 
    188      1.16  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    189      1.26      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    190      1.16  augustss 	usbd_devinfo_free(devinfop);
    191       1.1    ichiro 
    192       1.1    ichiro         sc->sc_udev = dev;
    193       1.5  augustss 	sc->sc_product = uaa->product;
    194       1.1    ichiro 
    195       1.1    ichiro 	DPRINTF(("\n\numct attach: sc=%p\n", sc));
    196       1.1    ichiro 
    197       1.8  augustss 	/* initialize endpoints */
    198       1.1    ichiro 	uca.bulkin = uca.bulkout = -1;
    199       1.1    ichiro 	sc->sc_intr_number = -1;
    200       1.1    ichiro 	sc->sc_intr_pipe = NULL;
    201       1.1    ichiro 
    202       1.1    ichiro 	/* Move the device into the configured state. */
    203       1.1    ichiro 	err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
    204       1.1    ichiro 	if (err) {
    205      1.26      cube 		aprint_error_dev(self, "failed to set configuration, err=%s\n",
    206      1.26      cube 		    usbd_errstr(err));
    207       1.1    ichiro 		sc->sc_dying = 1;
    208  1.29.4.1     rmind 		return;
    209       1.1    ichiro 	}
    210       1.1    ichiro 
    211       1.1    ichiro 	/* get the config descriptor */
    212       1.1    ichiro 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    213       1.1    ichiro 
    214       1.1    ichiro 	if (cdesc == NULL) {
    215      1.26      cube 		aprint_error_dev(self,
    216      1.26      cube 		    "failed to get configuration descriptor\n");
    217       1.1    ichiro 		sc->sc_dying = 1;
    218  1.29.4.1     rmind 		return;
    219       1.1    ichiro 	}
    220       1.1    ichiro 
    221       1.1    ichiro 	/* get the interface */
    222       1.8  augustss 	err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
    223       1.1    ichiro 							&sc->sc_iface);
    224       1.1    ichiro 	if (err) {
    225      1.26      cube 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    226      1.26      cube 		    usbd_errstr(err));
    227       1.1    ichiro 		sc->sc_dying = 1;
    228  1.29.4.1     rmind 		return;
    229       1.1    ichiro 	}
    230       1.1    ichiro 
    231       1.1    ichiro 	/* Find the bulk{in,out} and interrupt endpoints */
    232       1.1    ichiro 
    233       1.1    ichiro 	id = usbd_get_interface_descriptor(sc->sc_iface);
    234       1.1    ichiro 	sc->sc_iface_number = id->bInterfaceNumber;
    235       1.1    ichiro 
    236       1.1    ichiro 	for (i = 0; i < id->bNumEndpoints; i++) {
    237       1.1    ichiro 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    238       1.1    ichiro 		if (ed == NULL) {
    239      1.26      cube 			aprint_error_dev(self,
    240      1.26      cube 			    "no endpoint descriptor for %d\n", i);
    241       1.1    ichiro 			sc->sc_dying = 1;
    242  1.29.4.1     rmind 			return;
    243       1.1    ichiro 		}
    244       1.1    ichiro 
    245      1.14    rumble 		/*
    246      1.14    rumble 		 * The Bulkin endpoint is marked as an interrupt. Since
    247      1.14    rumble 		 * we can't rely on the endpoint descriptor order, we'll
    248      1.14    rumble 		 * check the wMaxPacketSize field to differentiate.
    249      1.14    rumble 		 */
    250       1.1    ichiro 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    251       1.1    ichiro 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
    252      1.14    rumble 		    UGETW(ed->wMaxPacketSize) != 0x2) {
    253       1.1    ichiro 			uca.bulkin = ed->bEndpointAddress;
    254       1.1    ichiro 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    255       1.1    ichiro 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    256       1.1    ichiro 			uca.bulkout = ed->bEndpointAddress;
    257       1.1    ichiro 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    258       1.1    ichiro 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    259       1.1    ichiro 			sc->sc_intr_number = ed->bEndpointAddress;
    260       1.1    ichiro 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    261       1.1    ichiro 		}
    262       1.1    ichiro 	}
    263       1.1    ichiro 
    264       1.1    ichiro 	if (uca.bulkin == -1) {
    265      1.26      cube 		aprint_error_dev(self, "Could not find data bulk in\n");
    266       1.1    ichiro 		sc->sc_dying = 1;
    267  1.29.4.1     rmind 		return;
    268       1.1    ichiro 	}
    269       1.1    ichiro 
    270       1.1    ichiro 	if (uca.bulkout == -1) {
    271      1.26      cube 		aprint_error_dev(self, "Could not find data bulk out\n");
    272       1.1    ichiro 		sc->sc_dying = 1;
    273  1.29.4.1     rmind 		return;
    274       1.1    ichiro 	}
    275       1.1    ichiro 
    276       1.1    ichiro 	if (sc->sc_intr_number== -1) {
    277      1.26      cube 		aprint_error_dev(self, "Could not find interrupt in\n");
    278       1.1    ichiro 		sc->sc_dying = 1;
    279  1.29.4.1     rmind 		return;
    280       1.1    ichiro 	}
    281       1.1    ichiro 
    282       1.1    ichiro 	sc->sc_dtr = sc->sc_rts = 0;
    283       1.1    ichiro 	uca.portno = UCOM_UNK_PORTNO;
    284       1.1    ichiro 	/* bulkin, bulkout set above */
    285       1.1    ichiro 	uca.ibufsize = UMCTIBUFSIZE;
    286       1.5  augustss 	if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232)
    287       1.5  augustss 		uca.obufsize = 16; /* device is broken */
    288       1.5  augustss 	else
    289       1.5  augustss 		uca.obufsize = UMCTOBUFSIZE;
    290       1.1    ichiro 	uca.ibufsizepad = UMCTIBUFSIZE;
    291       1.1    ichiro 	uca.opkthdrlen = 0;
    292       1.1    ichiro 	uca.device = dev;
    293       1.1    ichiro 	uca.iface = sc->sc_iface;
    294       1.1    ichiro 	uca.methods = &umct_methods;
    295       1.1    ichiro 	uca.arg = sc;
    296       1.1    ichiro 	uca.info = NULL;
    297       1.1    ichiro 
    298       1.1    ichiro 	umct_init(sc);
    299       1.1    ichiro 
    300       1.1    ichiro 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    301  1.29.4.1     rmind 			   sc->sc_dev);
    302       1.1    ichiro 
    303       1.1    ichiro 	DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n",
    304       1.1    ichiro 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    305      1.15  drochner 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    306      1.15  drochner 					    ucomprint, ucomsubmatch);
    307       1.1    ichiro 
    308  1.29.4.1     rmind 	return;
    309       1.1    ichiro }
    310       1.1    ichiro 
    311      1.23    dyoung void
    312      1.23    dyoung umct_childdet(device_t self, device_t child)
    313      1.23    dyoung {
    314      1.23    dyoung 	struct umct_softc *sc = device_private(self);
    315      1.23    dyoung 
    316      1.23    dyoung 	KASSERT(sc->sc_subdev == child);
    317      1.23    dyoung 	sc->sc_subdev = NULL;
    318      1.23    dyoung }
    319      1.23    dyoung 
    320  1.29.4.1     rmind int
    321  1.29.4.1     rmind umct_detach(device_t self, int flags)
    322       1.1    ichiro {
    323  1.29.4.1     rmind 	struct umct_softc *sc = device_private(self);
    324       1.1    ichiro 	int rv = 0;
    325       1.1    ichiro 
    326       1.1    ichiro 	DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags));
    327       1.1    ichiro 
    328       1.1    ichiro         if (sc->sc_intr_pipe != NULL) {
    329       1.1    ichiro                 usbd_abort_pipe(sc->sc_intr_pipe);
    330       1.1    ichiro                 usbd_close_pipe(sc->sc_intr_pipe);
    331       1.1    ichiro 		free(sc->sc_intr_buf, M_USBDEV);
    332       1.1    ichiro                 sc->sc_intr_pipe = NULL;
    333       1.1    ichiro         }
    334       1.1    ichiro 
    335       1.1    ichiro 	sc->sc_dying = 1;
    336      1.23    dyoung 	if (sc->sc_subdev != NULL)
    337       1.1    ichiro 		rv = config_detach(sc->sc_subdev, flags);
    338       1.1    ichiro 
    339       1.1    ichiro 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    340  1.29.4.1     rmind 			   sc->sc_dev);
    341       1.1    ichiro 
    342       1.1    ichiro 	return (rv);
    343       1.1    ichiro }
    344       1.1    ichiro 
    345       1.1    ichiro int
    346      1.23    dyoung umct_activate(device_t self, enum devact act)
    347       1.1    ichiro {
    348      1.23    dyoung 	struct umct_softc *sc = device_private(self);
    349       1.1    ichiro 
    350       1.1    ichiro 	switch (act) {
    351       1.1    ichiro 	case DVACT_DEACTIVATE:
    352       1.1    ichiro 		sc->sc_dying = 1;
    353      1.29    dyoung 		return 0;
    354      1.29    dyoung 	default:
    355      1.29    dyoung 		return EOPNOTSUPP;
    356       1.1    ichiro 	}
    357       1.1    ichiro }
    358       1.1    ichiro 
    359       1.1    ichiro void
    360       1.1    ichiro umct_set_line_state(struct umct_softc *sc)
    361       1.1    ichiro {
    362       1.1    ichiro 	usb_device_request_t req;
    363       1.5  augustss 	uByte ls;
    364       1.1    ichiro 
    365       1.1    ichiro 	ls = (sc->sc_dtr ? MCR_DTR : 0) |
    366       1.5  augustss 	     (sc->sc_rts ? MCR_RTS : 0);
    367       1.1    ichiro 
    368       1.1    ichiro 	DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n",
    369       1.1    ichiro 			sc->sc_dtr, sc->sc_rts, ls));
    370       1.1    ichiro 
    371       1.1    ichiro 	req.bmRequestType = UMCT_SET_REQUEST;
    372       1.1    ichiro 	req.bRequest = REQ_SET_MCR;
    373       1.1    ichiro 	USETW(req.wValue, 0);
    374       1.1    ichiro 	USETW(req.wIndex, sc->sc_iface_number);
    375       1.1    ichiro 	USETW(req.wLength, LENGTH_SET_MCR);
    376       1.1    ichiro 
    377       1.1    ichiro 	(void)usbd_do_request(sc->sc_udev, &req, &ls);
    378       1.1    ichiro }
    379       1.1    ichiro 
    380       1.1    ichiro void
    381      1.20  christos umct_set(void *addr, int portno, int reg, int onoff)
    382       1.1    ichiro {
    383       1.1    ichiro 	struct umct_softc *sc = addr;
    384       1.1    ichiro 
    385       1.1    ichiro 	switch (reg) {
    386       1.1    ichiro 	case UCOM_SET_DTR:
    387       1.1    ichiro 		umct_dtr(sc, onoff);
    388       1.1    ichiro 		break;
    389       1.1    ichiro 	case UCOM_SET_RTS:
    390       1.1    ichiro 		umct_rts(sc, onoff);
    391       1.1    ichiro 		break;
    392       1.1    ichiro 	case UCOM_SET_BREAK:
    393       1.1    ichiro 		umct_break(sc, onoff);
    394       1.1    ichiro 		break;
    395       1.1    ichiro 	default:
    396       1.1    ichiro 		break;
    397       1.1    ichiro 	}
    398       1.1    ichiro }
    399       1.1    ichiro 
    400       1.1    ichiro void
    401       1.1    ichiro umct_dtr(struct umct_softc *sc, int onoff)
    402       1.1    ichiro {
    403       1.1    ichiro 
    404       1.1    ichiro 	DPRINTF(("umct_dtr: onoff=%d\n", onoff));
    405       1.1    ichiro 
    406       1.1    ichiro 	if (sc->sc_dtr == onoff)
    407       1.1    ichiro 		return;
    408       1.1    ichiro 	sc->sc_dtr = onoff;
    409       1.1    ichiro 
    410       1.1    ichiro 	umct_set_line_state(sc);
    411       1.1    ichiro }
    412       1.1    ichiro 
    413       1.1    ichiro void
    414       1.1    ichiro umct_rts(struct umct_softc *sc, int onoff)
    415       1.1    ichiro {
    416       1.1    ichiro 	DPRINTF(("umct_rts: onoff=%d\n", onoff));
    417       1.1    ichiro 
    418       1.1    ichiro 	if (sc->sc_rts == onoff)
    419       1.1    ichiro 		return;
    420       1.1    ichiro 	sc->sc_rts = onoff;
    421       1.1    ichiro 
    422       1.1    ichiro 	umct_set_line_state(sc);
    423       1.1    ichiro }
    424       1.1    ichiro 
    425       1.1    ichiro void
    426       1.1    ichiro umct_break(struct umct_softc *sc, int onoff)
    427       1.1    ichiro {
    428       1.1    ichiro 	DPRINTF(("umct_break: onoff=%d\n", onoff));
    429       1.1    ichiro 
    430       1.7    ichiro 	umct_set_lcr(sc, onoff ? sc->last_lcr | LCR_SET_BREAK :
    431       1.7    ichiro 		     sc->last_lcr);
    432       1.1    ichiro }
    433       1.1    ichiro 
    434       1.1    ichiro void
    435       1.5  augustss umct_set_lcr(struct umct_softc *sc, u_int data)
    436       1.1    ichiro {
    437       1.1    ichiro 	usb_device_request_t req;
    438       1.5  augustss 	uByte adata;
    439       1.1    ichiro 
    440       1.5  augustss 	adata = data;
    441       1.1    ichiro 	req.bmRequestType = UMCT_SET_REQUEST;
    442       1.1    ichiro 	req.bRequest = REQ_SET_LCR;
    443       1.1    ichiro 	USETW(req.wValue, 0);
    444       1.1    ichiro 	USETW(req.wIndex, sc->sc_iface_number);
    445       1.1    ichiro 	USETW(req.wLength, LENGTH_SET_LCR);
    446       1.1    ichiro 
    447       1.5  augustss 	(void)usbd_do_request(sc->sc_udev, &req, &adata); /* XXX should check */
    448       1.1    ichiro }
    449       1.1    ichiro 
    450       1.1    ichiro void
    451       1.5  augustss umct_set_baudrate(struct umct_softc *sc, u_int rate)
    452       1.1    ichiro {
    453       1.1    ichiro         usb_device_request_t req;
    454       1.5  augustss 	uDWord arate;
    455       1.5  augustss 	u_int val;
    456       1.5  augustss 
    457      1.11  augustss 	if (sc->sc_product == USB_PRODUCT_MCT_SITECOM_USB232 ||
    458      1.11  augustss 	    sc->sc_product == USB_PRODUCT_BELKIN_F5U109) {
    459       1.5  augustss 		switch (rate) {
    460       1.5  augustss 		case    300: val = 0x01; break;
    461       1.5  augustss 		case    600: val = 0x02; break;
    462       1.5  augustss 		case   1200: val = 0x03; break;
    463       1.5  augustss 		case   2400: val = 0x04; break;
    464       1.5  augustss 		case   4800: val = 0x06; break;
    465       1.5  augustss 		case   9600: val = 0x08; break;
    466       1.5  augustss 		case  19200: val = 0x09; break;
    467       1.5  augustss 		case  38400: val = 0x0a; break;
    468       1.5  augustss 		case  57600: val = 0x0b; break;
    469       1.5  augustss 		case 115200: val = 0x0c; break;
    470       1.5  augustss 		default:     val = -1; break;
    471       1.5  augustss 		}
    472       1.5  augustss 	} else {
    473       1.5  augustss 		val = UMCT_BAUD_RATE(rate);
    474       1.5  augustss 	}
    475       1.5  augustss 	USETDW(arate, val);
    476       1.1    ichiro 
    477       1.1    ichiro         req.bmRequestType = UMCT_SET_REQUEST;
    478       1.1    ichiro         req.bRequest = REQ_SET_BAUD_RATE;
    479       1.1    ichiro         USETW(req.wValue, 0);
    480       1.1    ichiro         USETW(req.wIndex, sc->sc_iface_number);
    481       1.1    ichiro         USETW(req.wLength, LENGTH_BAUD_RATE);
    482       1.1    ichiro 
    483       1.5  augustss         (void)usbd_do_request(sc->sc_udev, &req, arate); /* XXX should check */
    484       1.1    ichiro }
    485       1.1    ichiro 
    486       1.1    ichiro void
    487       1.1    ichiro umct_init(struct umct_softc *sc)
    488       1.1    ichiro {
    489       1.5  augustss 	umct_set_baudrate(sc, 9600);
    490       1.5  augustss 	umct_set_lcr(sc, LCR_DATA_BITS_8 | LCR_PARITY_NONE | LCR_STOP_BITS_1);
    491       1.1    ichiro }
    492       1.1    ichiro 
    493       1.1    ichiro int
    494      1.20  christos umct_param(void *addr, int portno, struct termios *t)
    495       1.1    ichiro {
    496       1.1    ichiro 	struct umct_softc *sc = addr;
    497       1.5  augustss 	u_int data = 0;
    498       1.1    ichiro 
    499       1.1    ichiro 	DPRINTF(("umct_param: sc=%p\n", sc));
    500       1.1    ichiro 
    501       1.1    ichiro 	DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed));
    502       1.1    ichiro 
    503       1.1    ichiro 	if (ISSET(t->c_cflag, CSTOPB))
    504       1.1    ichiro 		data |= LCR_STOP_BITS_2;
    505       1.1    ichiro 	else
    506       1.1    ichiro 		data |= LCR_STOP_BITS_1;
    507       1.1    ichiro 	if (ISSET(t->c_cflag, PARENB)) {
    508       1.1    ichiro 		if (ISSET(t->c_cflag, PARODD))
    509       1.1    ichiro 			data |= LCR_PARITY_ODD;
    510       1.1    ichiro 		else
    511       1.1    ichiro 			data |= LCR_PARITY_EVEN;
    512       1.1    ichiro 	} else
    513       1.1    ichiro 		data |= LCR_PARITY_NONE;
    514       1.1    ichiro 	switch (ISSET(t->c_cflag, CSIZE)) {
    515       1.1    ichiro 	case CS5:
    516       1.1    ichiro 		data |= LCR_DATA_BITS_5;
    517       1.1    ichiro 		break;
    518       1.1    ichiro 	case CS6:
    519       1.1    ichiro 		data |= LCR_DATA_BITS_6;
    520       1.1    ichiro 		break;
    521       1.1    ichiro 	case CS7:
    522       1.1    ichiro 		data |= LCR_DATA_BITS_7;
    523       1.1    ichiro 		break;
    524       1.1    ichiro 	case CS8:
    525       1.1    ichiro 		data |= LCR_DATA_BITS_8;
    526       1.1    ichiro 		break;
    527       1.1    ichiro 	}
    528       1.1    ichiro 
    529       1.5  augustss 	umct_set_baudrate(sc, t->c_ospeed);
    530       1.1    ichiro 
    531       1.7    ichiro 	sc->last_lcr = data;
    532       1.5  augustss 	umct_set_lcr(sc, data);
    533       1.1    ichiro 
    534       1.1    ichiro 	return (0);
    535       1.1    ichiro }
    536       1.1    ichiro 
    537       1.1    ichiro int
    538      1.20  christos umct_open(void *addr, int portno)
    539       1.1    ichiro {
    540       1.1    ichiro 	struct umct_softc *sc = addr;
    541       1.1    ichiro 	int err, lcr_data;
    542       1.8  augustss 
    543       1.1    ichiro 	if (sc->sc_dying)
    544       1.1    ichiro 		return (EIO);
    545       1.1    ichiro 
    546       1.1    ichiro 	DPRINTF(("umct_open: sc=%p\n", sc));
    547       1.1    ichiro 
    548       1.1    ichiro 	/* initialize LCR */
    549       1.5  augustss         lcr_data = LCR_DATA_BITS_8 | LCR_PARITY_NONE |
    550       1.5  augustss 	    LCR_STOP_BITS_1;
    551       1.8  augustss         umct_set_lcr(sc, lcr_data);
    552       1.1    ichiro 
    553       1.1    ichiro 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    554       1.1    ichiro 		sc->sc_status = 0; /* clear status bit */
    555       1.1    ichiro 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    556       1.1    ichiro 		err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
    557       1.1    ichiro 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    558       1.1    ichiro 			sc->sc_intr_buf, sc->sc_isize,
    559       1.1    ichiro 			umct_intr, USBD_DEFAULT_INTERVAL);
    560       1.1    ichiro 		if (err) {
    561       1.1    ichiro 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    562  1.29.4.1     rmind 				device_xname(sc->sc_dev), sc->sc_intr_number));
    563       1.1    ichiro 					return (EIO);
    564       1.1    ichiro 		}
    565       1.1    ichiro 	}
    566       1.1    ichiro 
    567       1.1    ichiro 	return (0);
    568       1.1    ichiro }
    569       1.1    ichiro 
    570       1.1    ichiro void
    571      1.20  christos umct_close(void *addr, int portno)
    572       1.1    ichiro {
    573       1.1    ichiro 	struct umct_softc *sc = addr;
    574       1.1    ichiro 	int err;
    575       1.1    ichiro 
    576       1.1    ichiro 	if (sc->sc_dying)
    577       1.1    ichiro 		return;
    578       1.1    ichiro 
    579       1.1    ichiro 	DPRINTF(("umct_close: close\n"));
    580       1.1    ichiro 
    581       1.1    ichiro 	if (sc->sc_intr_pipe != NULL) {
    582       1.1    ichiro 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    583       1.1    ichiro 		if (err)
    584       1.1    ichiro 			printf("%s: abort interrupt pipe failed: %s\n",
    585  1.29.4.1     rmind 				device_xname(sc->sc_dev), usbd_errstr(err));
    586       1.1    ichiro 		err = usbd_close_pipe(sc->sc_intr_pipe);
    587       1.1    ichiro 		if (err)
    588       1.1    ichiro 			printf("%s: close interrupt pipe failed: %s\n",
    589  1.29.4.1     rmind 				device_xname(sc->sc_dev), usbd_errstr(err));
    590       1.1    ichiro 		free(sc->sc_intr_buf, M_USBDEV);
    591       1.1    ichiro 		sc->sc_intr_pipe = NULL;
    592       1.1    ichiro 	}
    593       1.1    ichiro }
    594       1.1    ichiro 
    595       1.1    ichiro void
    596      1.20  christos umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
    597      1.19  christos     usbd_status status)
    598       1.1    ichiro {
    599       1.1    ichiro 	struct umct_softc *sc = priv;
    600      1.17  christos 	u_char *tbuf = sc->sc_intr_buf;
    601      1.10    simonb 	u_char mstatus;
    602       1.1    ichiro 
    603       1.1    ichiro 	if (sc->sc_dying)
    604       1.1    ichiro 		return;
    605       1.1    ichiro 
    606       1.1    ichiro 	if (status != USBD_NORMAL_COMPLETION) {
    607       1.1    ichiro 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    608       1.1    ichiro 			return;
    609       1.1    ichiro 
    610  1.29.4.1     rmind 		DPRINTF(("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
    611       1.1    ichiro 			usbd_errstr(status)));
    612       1.1    ichiro 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    613       1.1    ichiro 		return;
    614       1.1    ichiro 	}
    615       1.1    ichiro 
    616       1.2  augustss 	DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n",
    617  1.29.4.1     rmind 		 device_xname(sc->sc_dev), tbuf[0],tbuf[1]));
    618       1.1    ichiro 
    619       1.2  augustss 	sc->sc_lsr = sc->sc_msr = 0;
    620      1.17  christos 	mstatus = tbuf[0];
    621       1.2  augustss 	if (ISSET(mstatus, MSR_DSR))
    622       1.2  augustss 		sc->sc_msr |= UMSR_DSR;
    623       1.2  augustss 	if (ISSET(mstatus, MSR_DCD))
    624       1.2  augustss 		sc->sc_msr |= UMSR_DCD;
    625      1.26      cube 	ucom_status_change(device_private(sc->sc_subdev));
    626       1.1    ichiro }
    627       1.1    ichiro 
    628       1.1    ichiro void
    629      1.20  christos umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    630       1.1    ichiro {
    631       1.1    ichiro 	struct umct_softc *sc = addr;
    632       1.1    ichiro 
    633       1.1    ichiro 	DPRINTF(("umct_get_status:\n"));
    634       1.1    ichiro 
    635       1.1    ichiro 	if (lsr != NULL)
    636       1.1    ichiro 		*lsr = sc->sc_lsr;
    637       1.1    ichiro 	if (msr != NULL)
    638       1.1    ichiro 		*msr = sc->sc_msr;
    639       1.1    ichiro }
    640