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