xref: /freebsd/sys/dev/usb/serial/uark.c (revision d27ba3088424e53eabc0b0186ed122ec43119501)
1 /*	$OpenBSD: uark.c,v 1.1 2006/08/14 08:30:22 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * $FreeBSD$
19  */
20 
21 /*
22  * NOTE: all function names beginning like "uark_cfg_" can only
23  * be called from within the config thread function !
24  */
25 
26 #include <sys/stdint.h>
27 #include <sys/stddef.h>
28 #include <sys/param.h>
29 #include <sys/queue.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/module.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/sx.h>
40 #include <sys/unistd.h>
41 #include <sys/callout.h>
42 #include <sys/malloc.h>
43 #include <sys/priv.h>
44 
45 #include <dev/usb/usb.h>
46 #include <dev/usb/usbdi.h>
47 #include <dev/usb/usbdi_util.h>
48 #include <dev/usb/usbhid.h>
49 #include "usbdevs.h"
50 
51 #define	USB_DEBUG_VAR usb_debug
52 #include <dev/usb/usb_debug.h>
53 #include <dev/usb/usb_process.h>
54 
55 #include <dev/usb/serial/usb_serial.h>
56 
57 #define	UARK_BUF_SIZE		1024	/* bytes */
58 
59 #define	UARK_SET_DATA_BITS(x)	((x) - 5)
60 
61 #define	UARK_PARITY_NONE	0x00
62 #define	UARK_PARITY_ODD		0x08
63 #define	UARK_PARITY_EVEN	0x18
64 
65 #define	UARK_STOP_BITS_1	0x00
66 #define	UARK_STOP_BITS_2	0x04
67 
68 #define	UARK_BAUD_REF		3000000
69 
70 #define	UARK_WRITE		0x40
71 #define	UARK_READ		0xc0
72 
73 #define	UARK_REQUEST		0xfe
74 
75 #define	UARK_CONFIG_INDEX	0
76 #define	UARK_IFACE_INDEX	0
77 
78 enum {
79 	UARK_BULK_DT_WR,
80 	UARK_BULK_DT_RD,
81 	UARK_N_TRANSFER,
82 };
83 
84 struct uark_softc {
85 	struct ucom_super_softc sc_super_ucom;
86 	struct ucom_softc sc_ucom;
87 
88 	struct usb_xfer *sc_xfer[UARK_N_TRANSFER];
89 	struct usb_device *sc_udev;
90 	struct mtx sc_mtx;
91 
92 	uint8_t	sc_msr;
93 	uint8_t	sc_lsr;
94 };
95 
96 /* prototypes */
97 
98 static device_probe_t uark_probe;
99 static device_attach_t uark_attach;
100 static device_detach_t uark_detach;
101 static void uark_free_softc(struct uark_softc *);
102 
103 static usb_callback_t uark_bulk_write_callback;
104 static usb_callback_t uark_bulk_read_callback;
105 
106 static void	uark_free(struct ucom_softc *);
107 static void	uark_start_read(struct ucom_softc *);
108 static void	uark_stop_read(struct ucom_softc *);
109 static void	uark_start_write(struct ucom_softc *);
110 static void	uark_stop_write(struct ucom_softc *);
111 static int	uark_pre_param(struct ucom_softc *, struct termios *);
112 static void	uark_cfg_param(struct ucom_softc *, struct termios *);
113 static void	uark_cfg_get_status(struct ucom_softc *, uint8_t *,
114 		    uint8_t *);
115 static void	uark_cfg_set_break(struct ucom_softc *, uint8_t);
116 static void	uark_cfg_write(struct uark_softc *, uint16_t, uint16_t);
117 static void	uark_poll(struct ucom_softc *ucom);
118 
119 static const struct usb_config
120 	uark_xfer_config[UARK_N_TRANSFER] = {
121 	[UARK_BULK_DT_WR] = {
122 		.type = UE_BULK,
123 		.endpoint = UE_ADDR_ANY,
124 		.direction = UE_DIR_OUT,
125 		.bufsize = UARK_BUF_SIZE,
126 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
127 		.callback = &uark_bulk_write_callback,
128 	},
129 
130 	[UARK_BULK_DT_RD] = {
131 		.type = UE_BULK,
132 		.endpoint = UE_ADDR_ANY,
133 		.direction = UE_DIR_IN,
134 		.bufsize = UARK_BUF_SIZE,
135 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
136 		.callback = &uark_bulk_read_callback,
137 	},
138 };
139 
140 static const struct ucom_callback uark_callback = {
141 	.ucom_cfg_get_status = &uark_cfg_get_status,
142 	.ucom_cfg_set_break = &uark_cfg_set_break,
143 	.ucom_cfg_param = &uark_cfg_param,
144 	.ucom_pre_param = &uark_pre_param,
145 	.ucom_start_read = &uark_start_read,
146 	.ucom_stop_read = &uark_stop_read,
147 	.ucom_start_write = &uark_start_write,
148 	.ucom_stop_write = &uark_stop_write,
149 	.ucom_poll = &uark_poll,
150 	.ucom_free = &uark_free,
151 };
152 
153 static device_method_t uark_methods[] = {
154 	/* Device methods */
155 	DEVMETHOD(device_probe, uark_probe),
156 	DEVMETHOD(device_attach, uark_attach),
157 	DEVMETHOD(device_detach, uark_detach),
158 	DEVMETHOD_END
159 };
160 
161 static devclass_t uark_devclass;
162 
163 static driver_t uark_driver = {
164 	.name = "uark",
165 	.methods = uark_methods,
166 	.size = sizeof(struct uark_softc),
167 };
168 
169 static const STRUCT_USB_HOST_ID uark_devs[] = {
170 	{USB_VPI(USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116, 0)},
171 };
172 
173 DRIVER_MODULE(uark, uhub, uark_driver, uark_devclass, NULL, 0);
174 MODULE_DEPEND(uark, ucom, 1, 1, 1);
175 MODULE_DEPEND(uark, usb, 1, 1, 1);
176 MODULE_VERSION(uark, 1);
177 USB_PNP_HOST_INFO(uark_devs);
178 
179 static int
180 uark_probe(device_t dev)
181 {
182 	struct usb_attach_arg *uaa = device_get_ivars(dev);
183 
184 	if (uaa->usb_mode != USB_MODE_HOST) {
185 		return (ENXIO);
186 	}
187 	if (uaa->info.bConfigIndex != 0) {
188 		return (ENXIO);
189 	}
190 	if (uaa->info.bIfaceIndex != UARK_IFACE_INDEX) {
191 		return (ENXIO);
192 	}
193 	return (usbd_lookup_id_by_uaa(uark_devs, sizeof(uark_devs), uaa));
194 }
195 
196 static int
197 uark_attach(device_t dev)
198 {
199 	struct usb_attach_arg *uaa = device_get_ivars(dev);
200 	struct uark_softc *sc = device_get_softc(dev);
201 	int32_t error;
202 	uint8_t iface_index;
203 
204 	device_set_usb_desc(dev);
205 	mtx_init(&sc->sc_mtx, "uark", NULL, MTX_DEF);
206 	ucom_ref(&sc->sc_super_ucom);
207 
208 	sc->sc_udev = uaa->device;
209 
210 	iface_index = UARK_IFACE_INDEX;
211 	error = usbd_transfer_setup
212 	    (uaa->device, &iface_index, sc->sc_xfer,
213 	    uark_xfer_config, UARK_N_TRANSFER, sc, &sc->sc_mtx);
214 
215 	if (error) {
216 		device_printf(dev, "allocating control USB "
217 		    "transfers failed\n");
218 		goto detach;
219 	}
220 	/* clear stall at first run */
221 	mtx_lock(&sc->sc_mtx);
222 	usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_WR]);
223 	usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_RD]);
224 	mtx_unlock(&sc->sc_mtx);
225 
226 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
227 	    &uark_callback, &sc->sc_mtx);
228 	if (error) {
229 		DPRINTF("ucom_attach failed\n");
230 		goto detach;
231 	}
232 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
233 
234 	return (0);			/* success */
235 
236 detach:
237 	uark_detach(dev);
238 	return (ENXIO);			/* failure */
239 }
240 
241 static int
242 uark_detach(device_t dev)
243 {
244 	struct uark_softc *sc = device_get_softc(dev);
245 
246 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
247 	usbd_transfer_unsetup(sc->sc_xfer, UARK_N_TRANSFER);
248 
249 	device_claim_softc(dev);
250 
251 	uark_free_softc(sc);
252 
253 	return (0);
254 }
255 
256 UCOM_UNLOAD_DRAIN(uark);
257 
258 static void
259 uark_free_softc(struct uark_softc *sc)
260 {
261 	if (ucom_unref(&sc->sc_super_ucom)) {
262 		mtx_destroy(&sc->sc_mtx);
263 		device_free_softc(sc);
264 	}
265 }
266 
267 static void
268 uark_free(struct ucom_softc *ucom)
269 {
270 	uark_free_softc(ucom->sc_parent);
271 }
272 
273 static void
274 uark_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
275 {
276 	struct uark_softc *sc = usbd_xfer_softc(xfer);
277 	struct usb_page_cache *pc;
278 	uint32_t actlen;
279 
280 	switch (USB_GET_STATE(xfer)) {
281 	case USB_ST_SETUP:
282 	case USB_ST_TRANSFERRED:
283 tr_setup:
284 		pc = usbd_xfer_get_frame(xfer, 0);
285 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
286 		    UARK_BUF_SIZE, &actlen)) {
287 			usbd_xfer_set_frame_len(xfer, 0, actlen);
288 			usbd_transfer_submit(xfer);
289 		}
290 		return;
291 
292 	default:			/* Error */
293 		if (error != USB_ERR_CANCELLED) {
294 			/* try to clear stall first */
295 			usbd_xfer_set_stall(xfer);
296 			goto tr_setup;
297 		}
298 		return;
299 	}
300 }
301 
302 static void
303 uark_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
304 {
305 	struct uark_softc *sc = usbd_xfer_softc(xfer);
306 	struct usb_page_cache *pc;
307 	int actlen;
308 
309 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
310 
311 	switch (USB_GET_STATE(xfer)) {
312 	case USB_ST_TRANSFERRED:
313 		pc = usbd_xfer_get_frame(xfer, 0);
314 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
315 
316 	case USB_ST_SETUP:
317 tr_setup:
318 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
319 		usbd_transfer_submit(xfer);
320 		return;
321 
322 	default:			/* Error */
323 		if (error != USB_ERR_CANCELLED) {
324 			/* try to clear stall first */
325 			usbd_xfer_set_stall(xfer);
326 			goto tr_setup;
327 		}
328 		return;
329 	}
330 }
331 
332 static void
333 uark_start_read(struct ucom_softc *ucom)
334 {
335 	struct uark_softc *sc = ucom->sc_parent;
336 
337 	usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_RD]);
338 }
339 
340 static void
341 uark_stop_read(struct ucom_softc *ucom)
342 {
343 	struct uark_softc *sc = ucom->sc_parent;
344 
345 	usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_RD]);
346 }
347 
348 static void
349 uark_start_write(struct ucom_softc *ucom)
350 {
351 	struct uark_softc *sc = ucom->sc_parent;
352 
353 	usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_WR]);
354 }
355 
356 static void
357 uark_stop_write(struct ucom_softc *ucom)
358 {
359 	struct uark_softc *sc = ucom->sc_parent;
360 
361 	usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_WR]);
362 }
363 
364 static int
365 uark_pre_param(struct ucom_softc *ucom, struct termios *t)
366 {
367 	if ((t->c_ospeed < 300) || (t->c_ospeed > 115200))
368 		return (EINVAL);
369 	return (0);
370 }
371 
372 static void
373 uark_cfg_param(struct ucom_softc *ucom, struct termios *t)
374 {
375 	struct uark_softc *sc = ucom->sc_parent;
376 	uint32_t speed = t->c_ospeed;
377 	uint16_t data;
378 
379 	/*
380 	 * NOTE: When reverse computing the baud rate from the "data" all
381 	 * allowed baud rates are within 3% of the initial baud rate.
382 	 */
383 	data = (UARK_BAUD_REF + (speed / 2)) / speed;
384 
385 	uark_cfg_write(sc, 3, 0x83);
386 	uark_cfg_write(sc, 0, data & 0xFF);
387 	uark_cfg_write(sc, 1, data >> 8);
388 	uark_cfg_write(sc, 3, 0x03);
389 
390 	if (t->c_cflag & CSTOPB)
391 		data = UARK_STOP_BITS_2;
392 	else
393 		data = UARK_STOP_BITS_1;
394 
395 	if (t->c_cflag & PARENB) {
396 		if (t->c_cflag & PARODD)
397 			data |= UARK_PARITY_ODD;
398 		else
399 			data |= UARK_PARITY_EVEN;
400 	} else
401 		data |= UARK_PARITY_NONE;
402 
403 	switch (t->c_cflag & CSIZE) {
404 	case CS5:
405 		data |= UARK_SET_DATA_BITS(5);
406 		break;
407 	case CS6:
408 		data |= UARK_SET_DATA_BITS(6);
409 		break;
410 	case CS7:
411 		data |= UARK_SET_DATA_BITS(7);
412 		break;
413 	default:
414 	case CS8:
415 		data |= UARK_SET_DATA_BITS(8);
416 		break;
417 	}
418 	uark_cfg_write(sc, 3, 0x00);
419 	uark_cfg_write(sc, 3, data);
420 }
421 
422 static void
423 uark_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
424 {
425 	struct uark_softc *sc = ucom->sc_parent;
426 
427 	/* XXX Note: sc_lsr is always zero */
428 	*lsr = sc->sc_lsr;
429 	*msr = sc->sc_msr;
430 }
431 
432 static void
433 uark_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
434 {
435 	struct uark_softc *sc = ucom->sc_parent;
436 
437 	DPRINTF("onoff=%d\n", onoff);
438 
439 	uark_cfg_write(sc, 4, onoff ? 0x01 : 0x00);
440 }
441 
442 static void
443 uark_cfg_write(struct uark_softc *sc, uint16_t index, uint16_t value)
444 {
445 	struct usb_device_request req;
446 	usb_error_t err;
447 
448 	req.bmRequestType = UARK_WRITE;
449 	req.bRequest = UARK_REQUEST;
450 	USETW(req.wValue, value);
451 	USETW(req.wIndex, index);
452 	USETW(req.wLength, 0);
453 
454 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
455 	    &req, NULL, 0, 1000);
456 	if (err) {
457 		DPRINTFN(0, "device request failed, err=%s "
458 		    "(ignored)\n", usbd_errstr(err));
459 	}
460 }
461 
462 static void
463 uark_poll(struct ucom_softc *ucom)
464 {
465 	struct uark_softc *sc = ucom->sc_parent;
466 	usbd_transfer_poll(sc->sc_xfer, UARK_N_TRANSFER);
467 }
468