xref: /netbsd-src/sys/dev/usb/uark.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /*	$NetBSD: uark.c,v 1.17 2021/04/24 23:36:59 thorpej 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@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.17 2021/04/24 23:36:59 thorpej 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 	bool			sc_dying;
79 };
80 
81 static void	uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
82 static void	uark_set(void *, int, int, int);
83 static int	uark_param(void *, int, struct termios *);
84 static int	uark_open(void *, int);
85 static void	uark_break(void *, int, int);
86 static int	uark_cmd(struct uark_softc *, uint16_t, uint16_t);
87 
88 static const struct ucom_methods uark_methods = {
89 	.ucom_get_status = uark_get_status,
90 	.ucom_set = uark_set,
91 	.ucom_param = uark_param,
92 	.ucom_open = uark_open,
93 };
94 
95 static const struct usb_devno uark_devs[] = {
96 	{ USB_VENDOR_ARKMICROCHIPS, USB_PRODUCT_ARKMICROCHIPS_USBSERIAL },
97 };
98 
99 static int	uark_match(device_t, cfdata_t, void *);
100 static void	uark_attach(device_t, device_t, void *);
101 static int	uark_detach(device_t, int);
102 
103 CFATTACH_DECL_NEW(uark, sizeof(struct uark_softc), uark_match, uark_attach,
104     uark_detach, NULL);
105 
106 static int
107 uark_match(device_t parent, cfdata_t match, void *aux)
108 {
109 	struct usb_attach_arg *uaa = aux;
110 
111 	return (usb_lookup(uark_devs, uaa->uaa_vendor, uaa->uaa_product)
112 	    != NULL) ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
113 }
114 
115 static void
116 uark_attach(device_t parent, device_t self, void *aux)
117 {
118 	struct uark_softc *sc = device_private(self);
119 	struct usb_attach_arg *uaa = aux;
120 	struct usbd_device *dev = uaa->uaa_device;
121 	char *devinfop;
122 	struct ucom_attach_args ucaa;
123 	usb_interface_descriptor_t *id;
124 	usb_endpoint_descriptor_t *ed;
125 	usbd_status error;
126 	int i;
127 
128 	memset(&ucaa, 0, sizeof(ucaa));
129 	sc->sc_dev = self;
130 
131 	devinfop = usbd_devinfo_alloc(dev, 0);
132 	aprint_naive("\n");
133 	aprint_normal("\n");
134 	aprint_normal_dev(self, "%s\n", devinfop);
135 	usbd_devinfo_free(devinfop);
136 
137 	sc->sc_udev = dev;
138 	sc->sc_dying = false;
139 
140 	if (usbd_set_config_index(sc->sc_udev, UARK_CONFIG_NO, 1) != 0) {
141 		aprint_error_dev(self, "could not set configuration no\n");
142 		sc->sc_dying = true;
143 		return;
144 	}
145 
146 	/* get the first interface handle */
147 	error = usbd_device2interface_handle(sc->sc_udev, UARK_IFACE_NO,
148 	    &sc->sc_iface);
149 	if (error != 0) {
150 		aprint_error_dev(self, "could not get interface handle\n");
151 		sc->sc_dying = true;
152 		return;
153 	}
154 
155 	id = usbd_get_interface_descriptor(sc->sc_iface);
156 
157 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
158 	for (i = 0; i < id->bNumEndpoints; i++) {
159 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
160 		if (ed == NULL) {
161 			aprint_error_dev(self,
162 			    "no endpoint descriptor found for %d\n", i);
163 			sc->sc_dying = true;
164 			return;
165 		}
166 
167 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
168 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
169 			ucaa.ucaa_bulkin = ed->bEndpointAddress;
170 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
171 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
172 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
173 	}
174 
175 	if (ucaa.ucaa_bulkin == -1 || ucaa.ucaa_bulkout == -1) {
176 		aprint_error_dev(self, "missing endpoint\n");
177 		sc->sc_dying = true;
178 		return;
179 	}
180 
181 	ucaa.ucaa_ibufsize = UARKBUFSZ;
182 	ucaa.ucaa_obufsize = UARKBUFSZ;
183 	ucaa.ucaa_ibufsizepad = UARKBUFSZ;
184 	ucaa.ucaa_opkthdrlen = 0;
185 	ucaa.ucaa_device = sc->sc_udev;
186 	ucaa.ucaa_iface = sc->sc_iface;
187 	ucaa.ucaa_methods = &uark_methods;
188 	ucaa.ucaa_arg = sc;
189 	ucaa.ucaa_info = NULL;
190 
191 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
192 
193 	sc->sc_subdev = config_found(self, &ucaa, ucomprint,
194 				     CFARG_SUBMATCH, ucomsubmatch,
195 				     CFARG_EOL);
196 
197 	return;
198 }
199 
200 static int
201 uark_detach(device_t self, int flags)
202 {
203 	struct uark_softc *sc = device_private(self);
204 	int rv = 0;
205 
206 	sc->sc_dying = true;
207 
208 	if (sc->sc_subdev != NULL) {
209 		rv = config_detach(sc->sc_subdev, flags);
210 		sc->sc_subdev = NULL;
211 	}
212 
213 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
214 
215 	return rv;
216 }
217 
218 static void
219 uark_set(void *vsc, int portno, int reg, int onoff)
220 {
221 	struct uark_softc *sc = vsc;
222 
223 	if (sc->sc_dying)
224 		return;
225 
226 	switch (reg) {
227 	case UCOM_SET_BREAK:
228 		uark_break(sc, portno, onoff);
229 		return;
230 	case UCOM_SET_DTR:
231 	case UCOM_SET_RTS:
232 	default:
233 		return;
234 	}
235 }
236 
237 static int
238 uark_param(void *vsc, int portno, struct termios *t)
239 {
240 	struct uark_softc *sc = (struct uark_softc *)vsc;
241 	int data;
242 
243 	if (sc->sc_dying)
244 		return EIO;
245 
246 	switch (t->c_ospeed) {
247 	case 300:
248 	case 600:
249 	case 1200:
250 	case 1800:
251 	case 2400:
252 	case 4800:
253 	case 9600:
254 	case 19200:
255 	case 38400:
256 	case 57600:
257 	case 115200:
258 		uark_cmd(sc, 3, 0x83);
259 		uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
260 		uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
261 		uark_cmd(sc, 3, 0x03);
262 		break;
263 	default:
264 		return EINVAL;
265 	}
266 
267 	if (ISSET(t->c_cflag, CSTOPB))
268 		data = UARK_STOP_BITS_2;
269 	else
270 		data = UARK_STOP_BITS_1;
271 
272 	if (ISSET(t->c_cflag, PARENB)) {
273 		if (ISSET(t->c_cflag, PARODD))
274 			data |= UARK_PARITY_ODD;
275 		else
276 			data |= UARK_PARITY_EVEN;
277 	} else
278 		data |= UARK_PARITY_NONE;
279 
280 	switch (ISSET(t->c_cflag, CSIZE)) {
281 	case CS5:
282 		data |= UARK_SET_DATA_BITS(5);
283 		break;
284 	case CS6:
285 		data |= UARK_SET_DATA_BITS(6);
286 		break;
287 	case CS7:
288 		data |= UARK_SET_DATA_BITS(7);
289 		break;
290 	case CS8:
291 		data |= UARK_SET_DATA_BITS(8);
292 		break;
293 	}
294 
295 	uark_cmd(sc, 3, 0x00);
296 	uark_cmd(sc, 3, data);
297 
298 #if 0
299 	/* XXX flow control */
300 	if (ISSET(t->c_cflag, CRTSCTS)) {
301 		/*  rts/cts flow ctl */
302 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
303 		/*  xon/xoff flow ctl */
304 	} else {
305 		/* disable flow ctl */
306 	}
307 #endif
308 
309 	return 0;
310 }
311 
312 static int
313 uark_open(void *arg, int portno)
314 {
315 	struct uark_softc *sc = arg;
316 
317 	if (sc->sc_dying)
318 		return EIO;
319 
320 	return 0;
321 }
322 
323 static 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 (sc->sc_dying)
329 		return;
330 
331 	*msr = sc->sc_msr;
332 	*lsr = sc->sc_lsr;
333 }
334 
335 static void
336 uark_break(void *vsc, int portno, int onoff)
337 {
338 #if 0
339 	struct uark_softc *sc = vsc;
340 
341 	if (sc->sc_dying)
342 		return;
343 
344 #ifdef UARK_DEBUG
345 	aprint_normal_dev(sc->sc_dev, "break %s!\n", onoff ? "on" : "off");
346 #endif
347 
348 	if (onoff)
349 		/* break on */
350 		uark_cmd(sc, 4, 0x01);
351 	else
352 		uark_cmd(sc, 4, 0x00);
353 #endif
354 }
355 
356 static int
357 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
358 {
359 	usb_device_request_t req;
360 	usbd_status err;
361 
362 	req.bmRequestType = UARK_WRITE;
363 	req.bRequest = UARK_REQUEST;
364 	USETW(req.wValue, value);
365 	USETW(req.wIndex, index);
366 	USETW(req.wLength, 0);
367 	err = usbd_do_request(sc->sc_udev, &req, NULL);
368 
369 	if (err)
370 		return EIO;
371 
372 	return 0;
373 }
374