Home | History | Annotate | Line # | Download | only in usb
      1 /*	$NetBSD: uxrcom.c,v 1.2 2020/07/09 13:43:04 simonb Exp $	*/
      2 /*	$OpenBSD: uxrcom.c,v 1.1 2019/03/27 22:08:51 kettenis Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998, 2020 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10  * Carlstedt Research & Technology and Simon Burge.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Copyright (c) 2006 Jonathan Gray <jsg (at) openbsd.org>
     36  *
     37  * Permission to use, copy, modify, and distribute this software for any
     38  * purpose with or without fee is hereby granted, provided that the above
     39  * copyright notice and this permission notice appear in all copies.
     40  *
     41  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     42  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     43  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     44  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     45  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     46  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     47  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     48  */
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: uxrcom.c,v 1.2 2020/07/09 13:43:04 simonb Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/kernel.h>
     55 #include <sys/tty.h>
     56 #include <sys/device.h>
     57 
     58 #include <dev/usb/usb.h>
     59 #include <dev/usb/usbcdc.h>
     60 #include <dev/usb/usbdi.h>
     61 #include <dev/usb/usbdi_util.h>
     62 #include <dev/usb/usbdevs.h>
     63 #include <dev/usb/usbhist.h>
     64 
     65 #include <dev/usb/usbdevs.h>
     66 #include <dev/usb/ucomvar.h>
     67 #include <dev/usb/umodemvar.h>
     68 
     69 
     70 #define UXRCOMBUFSZ		64
     71 
     72 /* XXX uxrcomreg.h */
     73 #define XR_SET_REG		0
     74 #define XR_GET_REGN		1
     75 
     76 #define XR_FLOW_CONTROL		0x000c
     77 #define  XR_FLOW_CONTROL_ON	1
     78 #define  XR_FLOW_CONTROL_OFF	0
     79 #define XR_TX_BREAK		0x0014
     80 #define  XR_TX_BREAK_ON		1
     81 #define  XR_TX_BREAK_OFF	0
     82 #define XR_GPIO_SET		0x001d
     83 #define XR_GPIO_CLEAR		0x001e
     84 #define  XR_GPIO3		(1 << 3)
     85 #define  XR_GPIO5		(1 << 5)
     86 
     87 /* for XR_SET_REG/XR_GET_REGN specify which uart block to use */
     88 #define	XR_UART_BLOCK(sc)	(((sc)->sc_ctl_iface_no / 2) << NBBY)
     89 
     90 #ifdef UXRCOM_DEBUG
     91 #define	DPRINTF(x)	if (uxrcomdebug) printf x
     92 int uxrcomdebug = 0;
     93 #else
     94 #define	DPRINTF(x)
     95 #endif
     96 
     97 
     98 static void	uxrcom_set(void *, int, int, int);
     99 static int	uxrcom_param(void *, int, struct termios *);
    100 static void	uxrcom_break(void *, int, int);
    101 
    102 static const struct	ucom_methods uxrcom_methods = {
    103 	.ucom_get_status = umodem_get_status,
    104 	.ucom_set = uxrcom_set,
    105 	.ucom_param = uxrcom_param,
    106 	.ucom_ioctl = NULL,	/* TODO */
    107 	.ucom_open = umodem_open,
    108 	.ucom_close = umodem_close,
    109 };
    110 
    111 static const struct usb_devno uxrcom_devs[] = {
    112 	{ USB_VENDOR_EXAR,	USB_PRODUCT_EXAR_XR21V1410 },
    113 	{ USB_VENDOR_EXAR,	USB_PRODUCT_EXAR_XR21V1412 },
    114 	{ USB_VENDOR_EXAR,	USB_PRODUCT_EXAR_XR21V1414 },
    115 };
    116 #define uxrcom_lookup(v, p) usb_lookup(uxrcom_devs, v, p)
    117 
    118 static int uxrcom_match(device_t, cfdata_t, void *);
    119 static void uxrcom_attach(device_t, device_t, void *);
    120 static int uxrcom_detach(device_t, int);
    121 
    122 CFATTACH_DECL_NEW(uxrcom, sizeof(struct umodem_softc), uxrcom_match,
    123     uxrcom_attach, uxrcom_detach, NULL);
    124 
    125 static int
    126 uxrcom_match(device_t parent, cfdata_t match, void *aux)
    127 {
    128 	struct usbif_attach_arg *uiaa = aux;
    129 
    130 	if (uiaa->uiaa_class != UICLASS_CDC ||
    131 	    uiaa->uiaa_subclass != UISUBCLASS_ABSTRACT_CONTROL_MODEL ||
    132 	    !(uiaa->uiaa_proto == UIPROTO_CDC_NOCLASS ||
    133 	      uiaa->uiaa_proto == UIPROTO_CDC_AT))
    134 		return UMATCH_NONE;
    135 
    136 	return uxrcom_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL ?
    137 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    138 }
    139 
    140 static void
    141 uxrcom_attach(device_t parent, device_t self, void *aux)
    142 {
    143 	struct umodem_softc *sc = device_private(self);
    144 	struct usbif_attach_arg *uiaa = aux;
    145 	struct ucom_attach_args ucaa;
    146 
    147 	memset(&ucaa, 0, sizeof(ucaa));
    148 
    149 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    150 	ucaa.ucaa_methods = &uxrcom_methods;
    151 	ucaa.ucaa_info = NULL;
    152 
    153 	ucaa.ucaa_ibufsize = UXRCOMBUFSZ;
    154 	ucaa.ucaa_obufsize = UXRCOMBUFSZ;
    155 	ucaa.ucaa_ibufsizepad = UXRCOMBUFSZ;
    156 
    157 	if (!pmf_device_register(self, NULL, NULL))
    158 		aprint_error_dev(self, "couldn't establish power handler");
    159 
    160 	umodem_common_attach(self, sc, uiaa, &ucaa);
    161 }
    162 
    163 static int
    164 uxrcom_detach(device_t self, int flags)
    165 {
    166 	struct umodem_softc *sc = device_private(self);
    167 
    168 	pmf_device_deregister(self);
    169 
    170 	return umodem_common_detach(sc, flags);
    171 }
    172 
    173 static void
    174 uxrcom_set(void *addr, int portno, int reg, int onoff)
    175 {
    176 	struct umodem_softc *sc = addr;
    177 	usb_device_request_t req;
    178 	uint16_t index;
    179 	uint8_t value;
    180 
    181 	if (sc->sc_dying)
    182 		return;
    183 
    184 	index = onoff ? XR_GPIO_SET : XR_GPIO_CLEAR;
    185 
    186 	switch (reg) {
    187 	case UCOM_SET_DTR:
    188 		value = XR_GPIO3;
    189 		break;
    190 	case UCOM_SET_RTS:
    191 		value = XR_GPIO5;
    192 		break;
    193 	case UCOM_SET_BREAK:
    194 		uxrcom_break(sc, portno, onoff);
    195 		return;
    196 	default:
    197 		return;
    198 	}
    199 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    200 	req.bRequest = XR_SET_REG;
    201 	USETW(req.wValue, value);
    202 	USETW(req.wIndex, index | XR_UART_BLOCK(sc));
    203 	USETW(req.wLength, 0);
    204 	usbd_do_request(sc->sc_udev, &req, NULL);
    205 }
    206 
    207 static usbd_status
    208 uxrcom_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    209 {
    210 	usb_device_request_t req;
    211 	usbd_status err;
    212 
    213 	DPRINTF(("%s: rate=%d fmt=%d parity=%d bits=%d\n", __func__,
    214 		UGETDW(state->dwDTERate), state->bCharFormat,
    215 		state->bParityType, state->bDataBits));
    216 
    217 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    218 		DPRINTF(("%s: already set\n", __func__));
    219 		return USBD_NORMAL_COMPLETION;
    220 	}
    221 
    222 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    223 	req.bRequest = UCDC_SET_LINE_CODING;
    224 	USETW(req.wValue, 0);
    225 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    226 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    227 
    228 	err = usbd_do_request(sc->sc_udev, &req, state);
    229 	if (err) {
    230 		DPRINTF(("%s: failed, err=%u\n", __func__, err));
    231 		return err;
    232 	}
    233 
    234 	sc->sc_line_state = *state;
    235 
    236 	return USBD_NORMAL_COMPLETION;
    237 }
    238 
    239 static int
    240 uxrcom_param(void *addr, int portno, struct termios *t)
    241 {
    242 	struct umodem_softc *sc = addr;
    243 	usb_device_request_t req;
    244 	usbd_status err;
    245 	usb_cdc_line_state_t ls;
    246 	uint8_t flowctrl;
    247 
    248 	if (sc->sc_dying)
    249 		return EIO;
    250 
    251 	/* slowest supported baud rate is 1200 bps, max is 12 Mbps */
    252 	if (t->c_ospeed < 1200 || t->c_ospeed > 12000000)
    253 		return (EINVAL);
    254 
    255 	USETDW(ls.dwDTERate, t->c_ospeed);
    256 	if (ISSET(t->c_cflag, CSTOPB))
    257 		ls.bCharFormat = UCDC_STOP_BIT_2;
    258 	else
    259 		ls.bCharFormat = UCDC_STOP_BIT_1;
    260 	if (ISSET(t->c_cflag, PARENB)) {
    261 		if (ISSET(t->c_cflag, PARODD))
    262 			ls.bParityType = UCDC_PARITY_ODD;
    263 		else
    264 			ls.bParityType = UCDC_PARITY_EVEN;
    265 	} else
    266 		ls.bParityType = UCDC_PARITY_NONE;
    267 	switch (ISSET(t->c_cflag, CSIZE)) {
    268 	case CS5:
    269 		ls.bDataBits = 5;
    270 		break;
    271 	case CS6:
    272 		ls.bDataBits = 6;
    273 		break;
    274 	case CS7:
    275 		ls.bDataBits = 7;
    276 		break;
    277 	case CS8:
    278 		ls.bDataBits = 8;
    279 		break;
    280 	}
    281 
    282 	err = uxrcom_set_line_coding(sc, &ls);
    283 	if (err) {
    284 		DPRINTF(("%s: err=%u\n", __func__, err));
    285 		return EIO;
    286 	}
    287 
    288 	if (ISSET(t->c_cflag, CRTSCTS)) {
    289 		/*  rts/cts flow ctl */
    290 		flowctrl = XR_FLOW_CONTROL_ON;
    291 	} else {
    292 		/* disable flow ctl */
    293 		flowctrl = XR_FLOW_CONTROL_OFF;
    294 	}
    295 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    296 	req.bRequest = XR_SET_REG;
    297 	USETW(req.wValue, flowctrl);
    298 	USETW(req.wIndex, XR_FLOW_CONTROL | XR_UART_BLOCK(sc));
    299 	USETW(req.wLength, 0);
    300 	usbd_do_request(sc->sc_udev, &req, NULL);
    301 
    302 	return (0);
    303 }
    304 
    305 static void
    306 uxrcom_break(void *addr, int portno, int onoff)
    307 {
    308 	struct umodem_softc *sc = addr;
    309 	usb_device_request_t req;
    310 	uint8_t brk = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
    311 
    312 	DPRINTF(("%s: port=%d onoff=%d\n", __func__, portno, onoff));
    313 
    314 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    315 	req.bRequest = XR_SET_REG;
    316 	USETW(req.wValue, brk);
    317 	USETW(req.wIndex, XR_TX_BREAK | XR_UART_BLOCK(sc));
    318 	USETW(req.wLength, 0);
    319 
    320 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    321 }
    322