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