Home | History | Annotate | Line # | Download | only in usb
uxrcom.c revision 1.1
      1 /*	$NetBSD: uxrcom.c,v 1.1 2020/04/12 01:10:54 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.1 2020/04/12 01:10:54 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 	if (umodem_common_attach(self, sc, uiaa, &ucaa))
    161 		return;
    162 	return;
    163 }
    164 
    165 static int
    166 uxrcom_detach(device_t self, int flags)
    167 {
    168 	struct umodem_softc *sc = device_private(self);
    169 
    170 	pmf_device_deregister(self);
    171 
    172 	return umodem_common_detach(sc, flags);
    173 }
    174 
    175 static void
    176 uxrcom_set(void *addr, int portno, int reg, int onoff)
    177 {
    178 	struct umodem_softc *sc = addr;
    179 	usb_device_request_t req;
    180 	uint16_t index;
    181 	uint8_t value;
    182 
    183 	if (sc->sc_dying)
    184 		return;
    185 
    186 	index = onoff ? XR_GPIO_SET : XR_GPIO_CLEAR;
    187 
    188 	switch (reg) {
    189 	case UCOM_SET_DTR:
    190 		value = XR_GPIO3;
    191 		break;
    192 	case UCOM_SET_RTS:
    193 		value = XR_GPIO5;
    194 		break;
    195 	case UCOM_SET_BREAK:
    196 		uxrcom_break(sc, portno, onoff);
    197 		return;
    198 	default:
    199 		return;
    200 	}
    201 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    202 	req.bRequest = XR_SET_REG;
    203 	USETW(req.wValue, value);
    204 	USETW(req.wIndex, index | XR_UART_BLOCK(sc));
    205 	USETW(req.wLength, 0);
    206 	usbd_do_request(sc->sc_udev, &req, NULL);
    207 }
    208 
    209 static usbd_status
    210 uxrcom_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    211 {
    212 	usb_device_request_t req;
    213 	usbd_status err;
    214 
    215 	DPRINTF(("%s: rate=%d fmt=%d parity=%d bits=%d\n", __func__,
    216 		UGETDW(state->dwDTERate), state->bCharFormat,
    217 		state->bParityType, state->bDataBits));
    218 
    219 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    220 		DPRINTF(("%s: already set\n", __func__));
    221 		return USBD_NORMAL_COMPLETION;
    222 	}
    223 
    224 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    225 	req.bRequest = UCDC_SET_LINE_CODING;
    226 	USETW(req.wValue, 0);
    227 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    228 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    229 
    230 	err = usbd_do_request(sc->sc_udev, &req, state);
    231 	if (err) {
    232 		DPRINTF(("%s: failed, err=%u\n", __func__, err));
    233 		return err;
    234 	}
    235 
    236 	sc->sc_line_state = *state;
    237 
    238 	return USBD_NORMAL_COMPLETION;
    239 }
    240 
    241 static int
    242 uxrcom_param(void *addr, int portno, struct termios *t)
    243 {
    244 	struct umodem_softc *sc = addr;
    245 	usb_device_request_t req;
    246 	usbd_status err;
    247 	usb_cdc_line_state_t ls;
    248 	uint8_t flowctrl;
    249 
    250 	if (sc->sc_dying)
    251 		return EIO;
    252 
    253 	/* slowest supported baud rate is 1200 bps, max is 12 Mbps */
    254 	if (t->c_ospeed < 1200 || t->c_ospeed > 12000000)
    255 		return (EINVAL);
    256 
    257 	USETDW(ls.dwDTERate, t->c_ospeed);
    258 	if (ISSET(t->c_cflag, CSTOPB))
    259 		ls.bCharFormat = UCDC_STOP_BIT_2;
    260 	else
    261 		ls.bCharFormat = UCDC_STOP_BIT_1;
    262 	if (ISSET(t->c_cflag, PARENB)) {
    263 		if (ISSET(t->c_cflag, PARODD))
    264 			ls.bParityType = UCDC_PARITY_ODD;
    265 		else
    266 			ls.bParityType = UCDC_PARITY_EVEN;
    267 	} else
    268 		ls.bParityType = UCDC_PARITY_NONE;
    269 	switch (ISSET(t->c_cflag, CSIZE)) {
    270 	case CS5:
    271 		ls.bDataBits = 5;
    272 		break;
    273 	case CS6:
    274 		ls.bDataBits = 6;
    275 		break;
    276 	case CS7:
    277 		ls.bDataBits = 7;
    278 		break;
    279 	case CS8:
    280 		ls.bDataBits = 8;
    281 		break;
    282 	}
    283 
    284 	err = uxrcom_set_line_coding(sc, &ls);
    285 	if (err) {
    286 		DPRINTF(("%s: err=%u\n", __func__, err));
    287 		return EIO;
    288 	}
    289 
    290 	if (ISSET(t->c_cflag, CRTSCTS)) {
    291 		/*  rts/cts flow ctl */
    292 		flowctrl = XR_FLOW_CONTROL_ON;
    293 	} else {
    294 		/* disable flow ctl */
    295 		flowctrl = XR_FLOW_CONTROL_OFF;
    296 	}
    297 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    298 	req.bRequest = XR_SET_REG;
    299 	USETW(req.wValue, flowctrl);
    300 	USETW(req.wIndex, XR_FLOW_CONTROL | XR_UART_BLOCK(sc));
    301 	USETW(req.wLength, 0);
    302 	usbd_do_request(sc->sc_udev, &req, NULL);
    303 
    304 	return (0);
    305 }
    306 
    307 static void
    308 uxrcom_break(void *addr, int portno, int onoff)
    309 {
    310 	struct umodem_softc *sc = addr;
    311 	usb_device_request_t req;
    312 	uint8_t brk = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
    313 
    314 	DPRINTF(("%s: port=%d onoff=%d\n", __func__, portno, onoff));
    315 
    316 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    317 	req.bRequest = XR_SET_REG;
    318 	USETW(req.wValue, brk);
    319 	USETW(req.wIndex, XR_TX_BREAK | XR_UART_BLOCK(sc));
    320 	USETW(req.wLength, 0);
    321 
    322 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    323 }
    324