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