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