Home | History | Annotate | Line # | Download | only in usb
uark.c revision 1.1.2.2
      1  1.1.2.2  rmind /*	$NetBSD: uark.c,v 1.1.2.2 2010/05/30 05:17:45 rmind Exp $	*/
      2  1.1.2.2  rmind /*	$OpenBSD: uark.c,v 1.13 2009/10/13 19:33:17 pirofti Exp $	*/
      3  1.1.2.2  rmind 
      4  1.1.2.2  rmind /*
      5  1.1.2.2  rmind  * Copyright (c) 2006 Jonathan Gray <jsg (at) openbsd.org>
      6  1.1.2.2  rmind  *
      7  1.1.2.2  rmind  * Permission to use, copy, modify, and distribute this software for any
      8  1.1.2.2  rmind  * purpose with or without fee is hereby granted, provided that the above
      9  1.1.2.2  rmind  * copyright notice and this permission notice appear in all copies.
     10  1.1.2.2  rmind  *
     11  1.1.2.2  rmind  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  1.1.2.2  rmind  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1.2.2  rmind  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  1.1.2.2  rmind  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1.2.2  rmind  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1.2.2  rmind  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  1.1.2.2  rmind  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1.2.2  rmind  */
     19  1.1.2.2  rmind 
     20  1.1.2.2  rmind #include <sys/param.h>
     21  1.1.2.2  rmind #include <sys/systm.h>
     22  1.1.2.2  rmind #include <sys/kernel.h>
     23  1.1.2.2  rmind #include <sys/conf.h>
     24  1.1.2.2  rmind #include <sys/tty.h>
     25  1.1.2.2  rmind #include <sys/device.h>
     26  1.1.2.2  rmind 
     27  1.1.2.2  rmind #include <dev/usb/usb.h>
     28  1.1.2.2  rmind #include <dev/usb/usbdi.h>
     29  1.1.2.2  rmind #include <dev/usb/usbdi_util.h>
     30  1.1.2.2  rmind #include <dev/usb/usbdevs.h>
     31  1.1.2.2  rmind 
     32  1.1.2.2  rmind #include <dev/usb/usbdevs.h>
     33  1.1.2.2  rmind #include <dev/usb/ucomvar.h>
     34  1.1.2.2  rmind 
     35  1.1.2.2  rmind #ifdef UARK_DEBUG
     36  1.1.2.2  rmind #define DPRINTFN(n, x)  do { if (uarkdebug > (n)) printf x; } while (0)
     37  1.1.2.2  rmind int	uarkebug = 0;
     38  1.1.2.2  rmind #else
     39  1.1.2.2  rmind #define DPRINTFN(n, x)
     40  1.1.2.2  rmind #endif
     41  1.1.2.2  rmind #define DPRINTF(x) DPRINTFN(0, x)
     42  1.1.2.2  rmind 
     43  1.1.2.2  rmind #define UARKBUFSZ		256
     44  1.1.2.2  rmind #define UARK_CONFIG_NO	0
     45  1.1.2.2  rmind #define UARK_IFACE_NO		0
     46  1.1.2.2  rmind 
     47  1.1.2.2  rmind #define UARK_SET_DATA_BITS(x)	(x - 5)
     48  1.1.2.2  rmind 
     49  1.1.2.2  rmind #define UARK_PARITY_NONE	0x00
     50  1.1.2.2  rmind #define UARK_PARITY_ODD		0x08
     51  1.1.2.2  rmind #define UARK_PARITY_EVEN	0x18
     52  1.1.2.2  rmind 
     53  1.1.2.2  rmind #define UARK_STOP_BITS_1	0x00
     54  1.1.2.2  rmind #define UARK_STOP_BITS_2	0x04
     55  1.1.2.2  rmind 
     56  1.1.2.2  rmind #define UARK_BAUD_REF		3000000
     57  1.1.2.2  rmind 
     58  1.1.2.2  rmind #define UARK_WRITE		0x40
     59  1.1.2.2  rmind #define UARK_READ		0xc0
     60  1.1.2.2  rmind 
     61  1.1.2.2  rmind #define UARK_REQUEST		0xfe
     62  1.1.2.2  rmind 
     63  1.1.2.2  rmind struct uark_softc {
     64  1.1.2.2  rmind 	struct device		*sc_dev;
     65  1.1.2.2  rmind 	usbd_device_handle	 sc_udev;
     66  1.1.2.2  rmind 	usbd_interface_handle	 sc_iface;
     67  1.1.2.2  rmind 	struct device 		*sc_subdev;
     68  1.1.2.2  rmind 
     69  1.1.2.2  rmind 	u_char			 sc_msr;
     70  1.1.2.2  rmind 	u_char			 sc_lsr;
     71  1.1.2.2  rmind 
     72  1.1.2.2  rmind 	u_char			 sc_dying;
     73  1.1.2.2  rmind };
     74  1.1.2.2  rmind 
     75  1.1.2.2  rmind void	uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
     76  1.1.2.2  rmind void	uark_set(void *, int, int, int);
     77  1.1.2.2  rmind int	uark_param(void *, int, struct termios *);
     78  1.1.2.2  rmind void	uark_break(void *, int, int);
     79  1.1.2.2  rmind int	uark_cmd(struct uark_softc *, uint16_t, uint16_t);
     80  1.1.2.2  rmind 
     81  1.1.2.2  rmind struct ucom_methods uark_methods = {
     82  1.1.2.2  rmind 	uark_get_status,
     83  1.1.2.2  rmind 	uark_set,
     84  1.1.2.2  rmind 	uark_param,
     85  1.1.2.2  rmind 	NULL,
     86  1.1.2.2  rmind 	NULL,
     87  1.1.2.2  rmind 	NULL,
     88  1.1.2.2  rmind 	NULL,
     89  1.1.2.2  rmind 	NULL,
     90  1.1.2.2  rmind };
     91  1.1.2.2  rmind 
     92  1.1.2.2  rmind static const struct usb_devno uark_devs[] = {
     93  1.1.2.2  rmind 	{ USB_VENDOR_ARKMICROCHIPS, USB_PRODUCT_ARKMICROCHIPS_USBSERIAL },
     94  1.1.2.2  rmind };
     95  1.1.2.2  rmind 
     96  1.1.2.2  rmind USB_DECLARE_DRIVER(uark);
     97  1.1.2.2  rmind 
     98  1.1.2.2  rmind USB_MATCH(uark)
     99  1.1.2.2  rmind {
    100  1.1.2.2  rmind 	USB_MATCH_START(uarkcom, uaa);
    101  1.1.2.2  rmind 
    102  1.1.2.2  rmind 	return (usb_lookup(uark_devs, uaa->vendor, uaa->product) != NULL) ?
    103  1.1.2.2  rmind 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    104  1.1.2.2  rmind }
    105  1.1.2.2  rmind 
    106  1.1.2.2  rmind USB_ATTACH(uark)
    107  1.1.2.2  rmind {
    108  1.1.2.2  rmind 	USB_ATTACH_START(uark, sc, uaa);
    109  1.1.2.2  rmind 	usbd_device_handle dev = uaa->device;
    110  1.1.2.2  rmind 	char *devinfop;
    111  1.1.2.2  rmind 	struct ucom_attach_args uca;
    112  1.1.2.2  rmind 	usb_interface_descriptor_t *id;
    113  1.1.2.2  rmind 	usb_endpoint_descriptor_t *ed;
    114  1.1.2.2  rmind 	usbd_status error;
    115  1.1.2.2  rmind 	int i;
    116  1.1.2.2  rmind 
    117  1.1.2.2  rmind 	memset(&uca, 0, sizeof(uca));
    118  1.1.2.2  rmind 	sc->sc_dev = self;
    119  1.1.2.2  rmind 
    120  1.1.2.2  rmind 	devinfop = usbd_devinfo_alloc(dev, 0);
    121  1.1.2.2  rmind 	USB_ATTACH_SETUP;
    122  1.1.2.2  rmind 	aprint_normal_dev(self, "%s\n", devinfop);
    123  1.1.2.2  rmind 	usbd_devinfo_free(devinfop);
    124  1.1.2.2  rmind 
    125  1.1.2.2  rmind 	sc->sc_udev = dev;
    126  1.1.2.2  rmind 
    127  1.1.2.2  rmind 	if (usbd_set_config_index(sc->sc_udev, UARK_CONFIG_NO, 1) != 0) {
    128  1.1.2.2  rmind 		aprint_error_dev(self, "could not set configuration no\n");
    129  1.1.2.2  rmind 		sc->sc_dying = 1;
    130  1.1.2.2  rmind 		return;
    131  1.1.2.2  rmind 	}
    132  1.1.2.2  rmind 
    133  1.1.2.2  rmind 	/* get the first interface handle */
    134  1.1.2.2  rmind 	error = usbd_device2interface_handle(sc->sc_udev, UARK_IFACE_NO,
    135  1.1.2.2  rmind 	    &sc->sc_iface);
    136  1.1.2.2  rmind 	if (error != 0) {
    137  1.1.2.2  rmind 		aprint_error_dev(self, "could not get interface handle\n");
    138  1.1.2.2  rmind 		sc->sc_dying = 1;
    139  1.1.2.2  rmind 		return;
    140  1.1.2.2  rmind 	}
    141  1.1.2.2  rmind 
    142  1.1.2.2  rmind 	id = usbd_get_interface_descriptor(sc->sc_iface);
    143  1.1.2.2  rmind 
    144  1.1.2.2  rmind 	uca.bulkin = uca.bulkout = -1;
    145  1.1.2.2  rmind 	for (i = 0; i < id->bNumEndpoints; i++) {
    146  1.1.2.2  rmind 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    147  1.1.2.2  rmind 		if (ed == NULL) {
    148  1.1.2.2  rmind 			aprint_error_dev(self, "no endpoint descriptor found for %d\n",
    149  1.1.2.2  rmind 			    i);
    150  1.1.2.2  rmind 			sc->sc_dying = 1;
    151  1.1.2.2  rmind 			return;
    152  1.1.2.2  rmind 		}
    153  1.1.2.2  rmind 
    154  1.1.2.2  rmind 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    155  1.1.2.2  rmind 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
    156  1.1.2.2  rmind 			uca.bulkin = ed->bEndpointAddress;
    157  1.1.2.2  rmind 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    158  1.1.2.2  rmind 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
    159  1.1.2.2  rmind 			uca.bulkout = ed->bEndpointAddress;
    160  1.1.2.2  rmind 	}
    161  1.1.2.2  rmind 
    162  1.1.2.2  rmind 	if (uca.bulkin == -1 || uca.bulkout == -1) {
    163  1.1.2.2  rmind 		aprint_error_dev(self, "missing endpoint\n");
    164  1.1.2.2  rmind 		sc->sc_dying = 1;
    165  1.1.2.2  rmind 		return;
    166  1.1.2.2  rmind 	}
    167  1.1.2.2  rmind 
    168  1.1.2.2  rmind 	uca.ibufsize = UARKBUFSZ;
    169  1.1.2.2  rmind 	uca.obufsize = UARKBUFSZ;
    170  1.1.2.2  rmind 	uca.ibufsizepad = UARKBUFSZ;
    171  1.1.2.2  rmind 	uca.opkthdrlen = 0;
    172  1.1.2.2  rmind 	uca.device = sc->sc_udev;
    173  1.1.2.2  rmind 	uca.iface = sc->sc_iface;
    174  1.1.2.2  rmind 	uca.methods = &uark_methods;
    175  1.1.2.2  rmind 	uca.arg = sc;
    176  1.1.2.2  rmind 	uca.info = NULL;
    177  1.1.2.2  rmind 
    178  1.1.2.2  rmind 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    179  1.1.2.2  rmind 	    sc->sc_dev);
    180  1.1.2.2  rmind 
    181  1.1.2.2  rmind 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    182  1.1.2.2  rmind 					    ucomprint, ucomsubmatch);
    183  1.1.2.2  rmind 
    184  1.1.2.2  rmind 	USB_ATTACH_SUCCESS_RETURN;
    185  1.1.2.2  rmind }
    186  1.1.2.2  rmind 
    187  1.1.2.2  rmind int
    188  1.1.2.2  rmind uark_detach(struct device *self, int flags)
    189  1.1.2.2  rmind {
    190  1.1.2.2  rmind 	struct uark_softc *sc = device_private(self);
    191  1.1.2.2  rmind 	int rv = 0;
    192  1.1.2.2  rmind 
    193  1.1.2.2  rmind 	sc->sc_dying = 1;
    194  1.1.2.2  rmind 	if (sc->sc_subdev != NULL) {
    195  1.1.2.2  rmind 		rv = config_detach(sc->sc_subdev, flags);
    196  1.1.2.2  rmind 		sc->sc_subdev = NULL;
    197  1.1.2.2  rmind 	}
    198  1.1.2.2  rmind 
    199  1.1.2.2  rmind 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    200  1.1.2.2  rmind 			   sc->sc_dev);
    201  1.1.2.2  rmind 
    202  1.1.2.2  rmind 	return rv;
    203  1.1.2.2  rmind }
    204  1.1.2.2  rmind 
    205  1.1.2.2  rmind int
    206  1.1.2.2  rmind uark_activate(struct device* self, enum devact act)
    207  1.1.2.2  rmind {
    208  1.1.2.2  rmind 	struct uark_softc *sc = device_private(self);
    209  1.1.2.2  rmind 	int rv = 0;
    210  1.1.2.2  rmind 
    211  1.1.2.2  rmind 	switch (act) {
    212  1.1.2.2  rmind 	case DVACT_DEACTIVATE:
    213  1.1.2.2  rmind 		if (sc->sc_subdev != NULL)
    214  1.1.2.2  rmind 			rv = config_deactivate(sc->sc_subdev);
    215  1.1.2.2  rmind 		sc->sc_dying = 1;
    216  1.1.2.2  rmind 		break;
    217  1.1.2.2  rmind 	}
    218  1.1.2.2  rmind 	return (rv);
    219  1.1.2.2  rmind }
    220  1.1.2.2  rmind 
    221  1.1.2.2  rmind void
    222  1.1.2.2  rmind uark_set(void *vsc, int portno, int reg, int onoff)
    223  1.1.2.2  rmind {
    224  1.1.2.2  rmind 	struct uark_softc *sc = vsc;
    225  1.1.2.2  rmind 
    226  1.1.2.2  rmind 	switch (reg) {
    227  1.1.2.2  rmind 	case UCOM_SET_BREAK:
    228  1.1.2.2  rmind 		uark_break(sc, portno, onoff);
    229  1.1.2.2  rmind 		return;
    230  1.1.2.2  rmind 	case UCOM_SET_DTR:
    231  1.1.2.2  rmind 	case UCOM_SET_RTS:
    232  1.1.2.2  rmind 	default:
    233  1.1.2.2  rmind 		return;
    234  1.1.2.2  rmind 	}
    235  1.1.2.2  rmind }
    236  1.1.2.2  rmind 
    237  1.1.2.2  rmind int
    238  1.1.2.2  rmind uark_param(void *vsc, int portno, struct termios *t)
    239  1.1.2.2  rmind {
    240  1.1.2.2  rmind 	struct uark_softc *sc = (struct uark_softc *)vsc;
    241  1.1.2.2  rmind 	int data;
    242  1.1.2.2  rmind 
    243  1.1.2.2  rmind 	switch (t->c_ospeed) {
    244  1.1.2.2  rmind 	case 300:
    245  1.1.2.2  rmind 	case 600:
    246  1.1.2.2  rmind 	case 1200:
    247  1.1.2.2  rmind 	case 1800:
    248  1.1.2.2  rmind 	case 2400:
    249  1.1.2.2  rmind 	case 4800:
    250  1.1.2.2  rmind 	case 9600:
    251  1.1.2.2  rmind 	case 19200:
    252  1.1.2.2  rmind 	case 38400:
    253  1.1.2.2  rmind 	case 57600:
    254  1.1.2.2  rmind 	case 115200:
    255  1.1.2.2  rmind 		uark_cmd(sc, 3, 0x83);
    256  1.1.2.2  rmind 		uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
    257  1.1.2.2  rmind 		uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
    258  1.1.2.2  rmind 		uark_cmd(sc, 3, 0x03);
    259  1.1.2.2  rmind 		break;
    260  1.1.2.2  rmind 	default:
    261  1.1.2.2  rmind 		return (EINVAL);
    262  1.1.2.2  rmind 	}
    263  1.1.2.2  rmind 
    264  1.1.2.2  rmind 	if (ISSET(t->c_cflag, CSTOPB))
    265  1.1.2.2  rmind 		data = UARK_STOP_BITS_2;
    266  1.1.2.2  rmind 	else
    267  1.1.2.2  rmind 		data = UARK_STOP_BITS_1;
    268  1.1.2.2  rmind 
    269  1.1.2.2  rmind 	if (ISSET(t->c_cflag, PARENB)) {
    270  1.1.2.2  rmind 		if (ISSET(t->c_cflag, PARODD))
    271  1.1.2.2  rmind 			data |= UARK_PARITY_ODD;
    272  1.1.2.2  rmind 		else
    273  1.1.2.2  rmind 			data |= UARK_PARITY_EVEN;
    274  1.1.2.2  rmind 	} else
    275  1.1.2.2  rmind 		data |= UARK_PARITY_NONE;
    276  1.1.2.2  rmind 
    277  1.1.2.2  rmind 	switch (ISSET(t->c_cflag, CSIZE)) {
    278  1.1.2.2  rmind 	case CS5:
    279  1.1.2.2  rmind 		data |= UARK_SET_DATA_BITS(5);
    280  1.1.2.2  rmind 		break;
    281  1.1.2.2  rmind 	case CS6:
    282  1.1.2.2  rmind 		data |= UARK_SET_DATA_BITS(6);
    283  1.1.2.2  rmind 		break;
    284  1.1.2.2  rmind 	case CS7:
    285  1.1.2.2  rmind 		data |= UARK_SET_DATA_BITS(7);
    286  1.1.2.2  rmind 		break;
    287  1.1.2.2  rmind 	case CS8:
    288  1.1.2.2  rmind 		data |= UARK_SET_DATA_BITS(8);
    289  1.1.2.2  rmind 		break;
    290  1.1.2.2  rmind 	}
    291  1.1.2.2  rmind 
    292  1.1.2.2  rmind 	uark_cmd(sc, 3, 0x00);
    293  1.1.2.2  rmind 	uark_cmd(sc, 3, data);
    294  1.1.2.2  rmind 
    295  1.1.2.2  rmind #if 0
    296  1.1.2.2  rmind 	/* XXX flow control */
    297  1.1.2.2  rmind 	if (ISSET(t->c_cflag, CRTSCTS))
    298  1.1.2.2  rmind 		/*  rts/cts flow ctl */
    299  1.1.2.2  rmind 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
    300  1.1.2.2  rmind 		/*  xon/xoff flow ctl */
    301  1.1.2.2  rmind 	} else {
    302  1.1.2.2  rmind 		/* disable flow ctl */
    303  1.1.2.2  rmind 	}
    304  1.1.2.2  rmind #endif
    305  1.1.2.2  rmind 
    306  1.1.2.2  rmind 	return (0);
    307  1.1.2.2  rmind }
    308  1.1.2.2  rmind 
    309  1.1.2.2  rmind void
    310  1.1.2.2  rmind uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
    311  1.1.2.2  rmind {
    312  1.1.2.2  rmind 	struct uark_softc *sc = vsc;
    313  1.1.2.2  rmind 
    314  1.1.2.2  rmind 	if (msr != NULL)
    315  1.1.2.2  rmind 		*msr = sc->sc_msr;
    316  1.1.2.2  rmind 	if (lsr != NULL)
    317  1.1.2.2  rmind 		*lsr = sc->sc_lsr;
    318  1.1.2.2  rmind }
    319  1.1.2.2  rmind 
    320  1.1.2.2  rmind void
    321  1.1.2.2  rmind uark_break(void *vsc, int portno, int onoff)
    322  1.1.2.2  rmind {
    323  1.1.2.2  rmind #if 0
    324  1.1.2.2  rmind 	struct uark_softc *sc = vsc;
    325  1.1.2.2  rmind 
    326  1.1.2.2  rmind #ifdef UARK_DEBUG
    327  1.1.2.2  rmind 	aprint_normal_dev(sc->sc_dev, "break %s!\n", onoff ? "on" : "off");
    328  1.1.2.2  rmind #endif
    329  1.1.2.2  rmind 
    330  1.1.2.2  rmind 	if (onoff)
    331  1.1.2.2  rmind 		/* break on */
    332  1.1.2.2  rmind 		uark_cmd(sc, 4, 0x01);
    333  1.1.2.2  rmind 	else
    334  1.1.2.2  rmind 		uark_cmd(sc, 4, 0x00);
    335  1.1.2.2  rmind #endif
    336  1.1.2.2  rmind }
    337  1.1.2.2  rmind 
    338  1.1.2.2  rmind int
    339  1.1.2.2  rmind uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
    340  1.1.2.2  rmind {
    341  1.1.2.2  rmind 	usb_device_request_t req;
    342  1.1.2.2  rmind 	usbd_status err;
    343  1.1.2.2  rmind 
    344  1.1.2.2  rmind 	req.bmRequestType = UARK_WRITE;
    345  1.1.2.2  rmind 	req.bRequest = UARK_REQUEST;
    346  1.1.2.2  rmind 	USETW(req.wValue, value);
    347  1.1.2.2  rmind 	USETW(req.wIndex, index);
    348  1.1.2.2  rmind 	USETW(req.wLength, 0);
    349  1.1.2.2  rmind 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    350  1.1.2.2  rmind 
    351  1.1.2.2  rmind 	if (err)
    352  1.1.2.2  rmind 		return (EIO);
    353  1.1.2.2  rmind 
    354  1.1.2.2  rmind 	return (0);
    355  1.1.2.2  rmind }
    356