1 /* $OpenBSD: uark.c,v 1.1 2006/08/14 08:30:22 jsg Exp $ */
4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
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.
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.
22 * NOTE: all function names beginning like "uark_cfg_" can only
23 * be called from within the config thread function !
27 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usbdi_util.h>
49 #include <dev/usb/usbhid.h>
52 #define USB_DEBUG_VAR usb_debug
53 #include <dev/usb/usb_debug.h>
54 #include <dev/usb/usb_process.h>
56 #include <dev/usb/serial/usb_serial.h>
58 #define UARK_BUF_SIZE 1024 /* bytes */
60 #define UARK_SET_DATA_BITS(x) ((x) - 5)
62 #define UARK_PARITY_NONE 0x00
63 #define UARK_PARITY_ODD 0x08
64 #define UARK_PARITY_EVEN 0x18
66 #define UARK_STOP_BITS_1 0x00
67 #define UARK_STOP_BITS_2 0x04
69 #define UARK_BAUD_REF 3000000
71 #define UARK_WRITE 0x40
72 #define UARK_READ 0xc0
74 #define UARK_REQUEST 0xfe
76 #define UARK_CONFIG_INDEX 0
77 #define UARK_IFACE_INDEX 0
86 struct ucom_super_softc sc_super_ucom;
87 struct ucom_softc sc_ucom;
89 struct usb_xfer *sc_xfer[UARK_N_TRANSFER];
90 struct usb_device *sc_udev;
99 static device_probe_t uark_probe;
100 static device_attach_t uark_attach;
101 static device_detach_t uark_detach;
102 static void uark_free_softc(struct uark_softc *);
104 static usb_callback_t uark_bulk_write_callback;
105 static usb_callback_t uark_bulk_read_callback;
107 static void uark_free(struct ucom_softc *);
108 static void uark_start_read(struct ucom_softc *);
109 static void uark_stop_read(struct ucom_softc *);
110 static void uark_start_write(struct ucom_softc *);
111 static void uark_stop_write(struct ucom_softc *);
112 static int uark_pre_param(struct ucom_softc *, struct termios *);
113 static void uark_cfg_param(struct ucom_softc *, struct termios *);
114 static void uark_cfg_get_status(struct ucom_softc *, uint8_t *,
116 static void uark_cfg_set_break(struct ucom_softc *, uint8_t);
117 static void uark_cfg_write(struct uark_softc *, uint16_t, uint16_t);
118 static void uark_poll(struct ucom_softc *ucom);
120 static const struct usb_config
121 uark_xfer_config[UARK_N_TRANSFER] = {
123 [UARK_BULK_DT_WR] = {
125 .endpoint = UE_ADDR_ANY,
126 .direction = UE_DIR_OUT,
127 .bufsize = UARK_BUF_SIZE,
128 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
129 .callback = &uark_bulk_write_callback,
132 [UARK_BULK_DT_RD] = {
134 .endpoint = UE_ADDR_ANY,
135 .direction = UE_DIR_IN,
136 .bufsize = UARK_BUF_SIZE,
137 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
138 .callback = &uark_bulk_read_callback,
142 static const struct ucom_callback uark_callback = {
143 .ucom_cfg_get_status = &uark_cfg_get_status,
144 .ucom_cfg_set_break = &uark_cfg_set_break,
145 .ucom_cfg_param = &uark_cfg_param,
146 .ucom_pre_param = &uark_pre_param,
147 .ucom_start_read = &uark_start_read,
148 .ucom_stop_read = &uark_stop_read,
149 .ucom_start_write = &uark_start_write,
150 .ucom_stop_write = &uark_stop_write,
151 .ucom_poll = &uark_poll,
152 .ucom_free = &uark_free,
155 static device_method_t uark_methods[] = {
157 DEVMETHOD(device_probe, uark_probe),
158 DEVMETHOD(device_attach, uark_attach),
159 DEVMETHOD(device_detach, uark_detach),
163 static devclass_t uark_devclass;
165 static driver_t uark_driver = {
167 .methods = uark_methods,
168 .size = sizeof(struct uark_softc),
171 DRIVER_MODULE(uark, uhub, uark_driver, uark_devclass, NULL, 0);
172 MODULE_DEPEND(uark, ucom, 1, 1, 1);
173 MODULE_DEPEND(uark, usb, 1, 1, 1);
174 MODULE_VERSION(uark, 1);
176 static const STRUCT_USB_HOST_ID uark_devs[] = {
177 {USB_VPI(USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116, 0)},
181 uark_probe(device_t dev)
183 struct usb_attach_arg *uaa = device_get_ivars(dev);
185 if (uaa->usb_mode != USB_MODE_HOST) {
188 if (uaa->info.bConfigIndex != 0) {
191 if (uaa->info.bIfaceIndex != UARK_IFACE_INDEX) {
194 return (usbd_lookup_id_by_uaa(uark_devs, sizeof(uark_devs), uaa));
198 uark_attach(device_t dev)
200 struct usb_attach_arg *uaa = device_get_ivars(dev);
201 struct uark_softc *sc = device_get_softc(dev);
205 device_set_usb_desc(dev);
206 mtx_init(&sc->sc_mtx, "uark", NULL, MTX_DEF);
207 ucom_ref(&sc->sc_super_ucom);
209 sc->sc_udev = uaa->device;
211 iface_index = UARK_IFACE_INDEX;
212 error = usbd_transfer_setup
213 (uaa->device, &iface_index, sc->sc_xfer,
214 uark_xfer_config, UARK_N_TRANSFER, sc, &sc->sc_mtx);
217 device_printf(dev, "allocating control USB "
218 "transfers failed\n");
221 /* clear stall at first run */
222 mtx_lock(&sc->sc_mtx);
223 usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_WR]);
224 usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_RD]);
225 mtx_unlock(&sc->sc_mtx);
227 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
228 &uark_callback, &sc->sc_mtx);
230 DPRINTF("ucom_attach failed\n");
233 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
235 return (0); /* success */
239 return (ENXIO); /* failure */
243 uark_detach(device_t dev)
245 struct uark_softc *sc = device_get_softc(dev);
247 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
248 usbd_transfer_unsetup(sc->sc_xfer, UARK_N_TRANSFER);
250 device_claim_softc(dev);
257 UCOM_UNLOAD_DRAIN(uark);
260 uark_free_softc(struct uark_softc *sc)
262 if (ucom_unref(&sc->sc_super_ucom)) {
263 mtx_destroy(&sc->sc_mtx);
264 device_free_softc(sc);
269 uark_free(struct ucom_softc *ucom)
271 uark_free_softc(ucom->sc_parent);
275 uark_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
277 struct uark_softc *sc = usbd_xfer_softc(xfer);
278 struct usb_page_cache *pc;
281 switch (USB_GET_STATE(xfer)) {
283 case USB_ST_TRANSFERRED:
285 pc = usbd_xfer_get_frame(xfer, 0);
286 if (ucom_get_data(&sc->sc_ucom, pc, 0,
287 UARK_BUF_SIZE, &actlen)) {
288 usbd_xfer_set_frame_len(xfer, 0, actlen);
289 usbd_transfer_submit(xfer);
294 if (error != USB_ERR_CANCELLED) {
295 /* try to clear stall first */
296 usbd_xfer_set_stall(xfer);
305 uark_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
307 struct uark_softc *sc = usbd_xfer_softc(xfer);
308 struct usb_page_cache *pc;
311 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
313 switch (USB_GET_STATE(xfer)) {
314 case USB_ST_TRANSFERRED:
315 pc = usbd_xfer_get_frame(xfer, 0);
316 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
320 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
321 usbd_transfer_submit(xfer);
325 if (error != USB_ERR_CANCELLED) {
326 /* try to clear stall first */
327 usbd_xfer_set_stall(xfer);
335 uark_start_read(struct ucom_softc *ucom)
337 struct uark_softc *sc = ucom->sc_parent;
339 usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_RD]);
343 uark_stop_read(struct ucom_softc *ucom)
345 struct uark_softc *sc = ucom->sc_parent;
347 usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_RD]);
351 uark_start_write(struct ucom_softc *ucom)
353 struct uark_softc *sc = ucom->sc_parent;
355 usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_WR]);
359 uark_stop_write(struct ucom_softc *ucom)
361 struct uark_softc *sc = ucom->sc_parent;
363 usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_WR]);
367 uark_pre_param(struct ucom_softc *ucom, struct termios *t)
369 if ((t->c_ospeed < 300) || (t->c_ospeed > 115200))
375 uark_cfg_param(struct ucom_softc *ucom, struct termios *t)
377 struct uark_softc *sc = ucom->sc_parent;
378 uint32_t speed = t->c_ospeed;
382 * NOTE: When reverse computing the baud rate from the "data" all
383 * allowed baud rates are within 3% of the initial baud rate.
385 data = (UARK_BAUD_REF + (speed / 2)) / speed;
387 uark_cfg_write(sc, 3, 0x83);
388 uark_cfg_write(sc, 0, data & 0xFF);
389 uark_cfg_write(sc, 1, data >> 8);
390 uark_cfg_write(sc, 3, 0x03);
392 if (t->c_cflag & CSTOPB)
393 data = UARK_STOP_BITS_2;
395 data = UARK_STOP_BITS_1;
397 if (t->c_cflag & PARENB) {
398 if (t->c_cflag & PARODD)
399 data |= UARK_PARITY_ODD;
401 data |= UARK_PARITY_EVEN;
403 data |= UARK_PARITY_NONE;
405 switch (t->c_cflag & CSIZE) {
407 data |= UARK_SET_DATA_BITS(5);
410 data |= UARK_SET_DATA_BITS(6);
413 data |= UARK_SET_DATA_BITS(7);
417 data |= UARK_SET_DATA_BITS(8);
420 uark_cfg_write(sc, 3, 0x00);
421 uark_cfg_write(sc, 3, data);
425 uark_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
427 struct uark_softc *sc = ucom->sc_parent;
434 uark_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
436 struct uark_softc *sc = ucom->sc_parent;
438 DPRINTF("onoff=%d\n", onoff);
440 uark_cfg_write(sc, 4, onoff ? 0x01 : 0x00);
444 uark_cfg_write(struct uark_softc *sc, uint16_t index, uint16_t value)
446 struct usb_device_request req;
449 req.bmRequestType = UARK_WRITE;
450 req.bRequest = UARK_REQUEST;
451 USETW(req.wValue, value);
452 USETW(req.wIndex, index);
453 USETW(req.wLength, 0);
455 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
456 &req, NULL, 0, 1000);
458 DPRINTFN(0, "device request failed, err=%s "
459 "(ignored)\n", usbd_errstr(err));
464 uark_poll(struct ucom_softc *ucom)
466 struct uark_softc *sc = ucom->sc_parent;
467 usbd_transfer_poll(sc->sc_xfer, UARK_N_TRANSFER);