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