]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/usb/serial/uark.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / usb / serial / uark.c
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
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>
34 #include <sys/bus.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/sx.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
44 #include <sys/priv.h>
45
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>
50 #include "usbdevs.h"
51
52 #define USB_DEBUG_VAR usb_debug
53 #include <dev/usb/usb_debug.h>
54 #include <dev/usb/usb_process.h>
55
56 #include <dev/usb/serial/usb_serial.h>
57
58 #define UARK_BUF_SIZE           1024    /* bytes */
59
60 #define UARK_SET_DATA_BITS(x)   ((x) - 5)
61
62 #define UARK_PARITY_NONE        0x00
63 #define UARK_PARITY_ODD         0x08
64 #define UARK_PARITY_EVEN        0x18
65
66 #define UARK_STOP_BITS_1        0x00
67 #define UARK_STOP_BITS_2        0x04
68
69 #define UARK_BAUD_REF           3000000
70
71 #define UARK_WRITE              0x40
72 #define UARK_READ               0xc0
73
74 #define UARK_REQUEST            0xfe
75
76 #define UARK_CONFIG_INDEX       0
77 #define UARK_IFACE_INDEX        0
78
79 enum {
80         UARK_BULK_DT_WR,
81         UARK_BULK_DT_RD,
82         UARK_N_TRANSFER,
83 };
84
85 struct uark_softc {
86         struct ucom_super_softc sc_super_ucom;
87         struct ucom_softc sc_ucom;
88
89         struct usb_xfer *sc_xfer[UARK_N_TRANSFER];
90         struct usb_device *sc_udev;
91         struct mtx sc_mtx;
92
93         uint8_t sc_msr;
94         uint8_t sc_lsr;
95 };
96
97 /* prototypes */
98
99 static device_probe_t uark_probe;
100 static device_attach_t uark_attach;
101 static device_detach_t uark_detach;
102
103 static usb_callback_t uark_bulk_write_callback;
104 static usb_callback_t uark_bulk_read_callback;
105
106 static void     uark_start_read(struct ucom_softc *);
107 static void     uark_stop_read(struct ucom_softc *);
108 static void     uark_start_write(struct ucom_softc *);
109 static void     uark_stop_write(struct ucom_softc *);
110 static int      uark_pre_param(struct ucom_softc *, struct termios *);
111 static void     uark_cfg_param(struct ucom_softc *, struct termios *);
112 static void     uark_cfg_get_status(struct ucom_softc *, uint8_t *,
113                     uint8_t *);
114 static void     uark_cfg_set_break(struct ucom_softc *, uint8_t);
115 static void     uark_cfg_write(struct uark_softc *, uint16_t, uint16_t);
116 static void     uark_poll(struct ucom_softc *ucom);
117
118 static const struct usb_config
119         uark_xfer_config[UARK_N_TRANSFER] = {
120
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 };
151
152 static device_method_t uark_methods[] = {
153         /* Device methods */
154         DEVMETHOD(device_probe, uark_probe),
155         DEVMETHOD(device_attach, uark_attach),
156         DEVMETHOD(device_detach, uark_detach),
157         {0, 0}
158 };
159
160 static devclass_t uark_devclass;
161
162 static driver_t uark_driver = {
163         .name = "uark",
164         .methods = uark_methods,
165         .size = sizeof(struct uark_softc),
166 };
167
168 DRIVER_MODULE(uark, uhub, uark_driver, uark_devclass, NULL, 0);
169 MODULE_DEPEND(uark, ucom, 1, 1, 1);
170 MODULE_DEPEND(uark, usb, 1, 1, 1);
171 MODULE_VERSION(uark, 1);
172
173 static const STRUCT_USB_HOST_ID uark_devs[] = {
174         {USB_VPI(USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116, 0)},
175 };
176
177 static int
178 uark_probe(device_t dev)
179 {
180         struct usb_attach_arg *uaa = device_get_ivars(dev);
181
182         if (uaa->usb_mode != USB_MODE_HOST) {
183                 return (ENXIO);
184         }
185         if (uaa->info.bConfigIndex != 0) {
186                 return (ENXIO);
187         }
188         if (uaa->info.bIfaceIndex != UARK_IFACE_INDEX) {
189                 return (ENXIO);
190         }
191         return (usbd_lookup_id_by_uaa(uark_devs, sizeof(uark_devs), uaa));
192 }
193
194 static int
195 uark_attach(device_t dev)
196 {
197         struct usb_attach_arg *uaa = device_get_ivars(dev);
198         struct uark_softc *sc = device_get_softc(dev);
199         int32_t error;
200         uint8_t iface_index;
201
202         device_set_usb_desc(dev);
203         mtx_init(&sc->sc_mtx, "uark", NULL, MTX_DEF);
204
205         sc->sc_udev = uaa->device;
206
207         iface_index = UARK_IFACE_INDEX;
208         error = usbd_transfer_setup
209             (uaa->device, &iface_index, sc->sc_xfer,
210             uark_xfer_config, UARK_N_TRANSFER, sc, &sc->sc_mtx);
211
212         if (error) {
213                 device_printf(dev, "allocating control USB "
214                     "transfers failed\n");
215                 goto detach;
216         }
217         /* clear stall at first run */
218         mtx_lock(&sc->sc_mtx);
219         usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_WR]);
220         usbd_xfer_set_stall(sc->sc_xfer[UARK_BULK_DT_RD]);
221         mtx_unlock(&sc->sc_mtx);
222
223         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
224             &uark_callback, &sc->sc_mtx);
225         if (error) {
226                 DPRINTF("ucom_attach failed\n");
227                 goto detach;
228         }
229         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
230
231         return (0);                     /* success */
232
233 detach:
234         uark_detach(dev);
235         return (ENXIO);                 /* failure */
236 }
237
238 static int
239 uark_detach(device_t dev)
240 {
241         struct uark_softc *sc = device_get_softc(dev);
242
243         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
244         usbd_transfer_unsetup(sc->sc_xfer, UARK_N_TRANSFER);
245         mtx_destroy(&sc->sc_mtx);
246
247         return (0);
248 }
249
250 static void
251 uark_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
252 {
253         struct uark_softc *sc = usbd_xfer_softc(xfer);
254         struct usb_page_cache *pc;
255         uint32_t actlen;
256
257         switch (USB_GET_STATE(xfer)) {
258         case USB_ST_SETUP:
259         case USB_ST_TRANSFERRED:
260 tr_setup:
261                 pc = usbd_xfer_get_frame(xfer, 0);
262                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
263                     UARK_BUF_SIZE, &actlen)) {
264                         usbd_xfer_set_frame_len(xfer, 0, actlen);
265                         usbd_transfer_submit(xfer);
266                 }
267                 return;
268
269         default:                        /* Error */
270                 if (error != USB_ERR_CANCELLED) {
271                         /* try to clear stall first */
272                         usbd_xfer_set_stall(xfer);
273                         goto tr_setup;
274                 }
275                 return;
276
277         }
278 }
279
280 static void
281 uark_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
282 {
283         struct uark_softc *sc = usbd_xfer_softc(xfer);
284         struct usb_page_cache *pc;
285         int actlen;
286
287         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
288
289         switch (USB_GET_STATE(xfer)) {
290         case USB_ST_TRANSFERRED:
291                 pc = usbd_xfer_get_frame(xfer, 0);
292                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
293
294         case USB_ST_SETUP:
295 tr_setup:
296                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
297                 usbd_transfer_submit(xfer);
298                 return;
299
300         default:                        /* Error */
301                 if (error != USB_ERR_CANCELLED) {
302                         /* try to clear stall first */
303                         usbd_xfer_set_stall(xfer);
304                         goto tr_setup;
305                 }
306                 return;
307         }
308 }
309
310 static void
311 uark_start_read(struct ucom_softc *ucom)
312 {
313         struct uark_softc *sc = ucom->sc_parent;
314
315         usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_RD]);
316 }
317
318 static void
319 uark_stop_read(struct ucom_softc *ucom)
320 {
321         struct uark_softc *sc = ucom->sc_parent;
322
323         usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_RD]);
324 }
325
326 static void
327 uark_start_write(struct ucom_softc *ucom)
328 {
329         struct uark_softc *sc = ucom->sc_parent;
330
331         usbd_transfer_start(sc->sc_xfer[UARK_BULK_DT_WR]);
332 }
333
334 static void
335 uark_stop_write(struct ucom_softc *ucom)
336 {
337         struct uark_softc *sc = ucom->sc_parent;
338
339         usbd_transfer_stop(sc->sc_xfer[UARK_BULK_DT_WR]);
340 }
341
342 static int
343 uark_pre_param(struct ucom_softc *ucom, struct termios *t)
344 {
345         if ((t->c_ospeed < 300) || (t->c_ospeed > 115200))
346                 return (EINVAL);
347         return (0);
348 }
349
350 static void
351 uark_cfg_param(struct ucom_softc *ucom, struct termios *t)
352 {
353         struct uark_softc *sc = ucom->sc_parent;
354         uint32_t speed = t->c_ospeed;
355         uint16_t data;
356
357         /*
358          * NOTE: When reverse computing the baud rate from the "data" all
359          * allowed baud rates are within 3% of the initial baud rate.
360          */
361         data = (UARK_BAUD_REF + (speed / 2)) / speed;
362
363         uark_cfg_write(sc, 3, 0x83);
364         uark_cfg_write(sc, 0, data & 0xFF);
365         uark_cfg_write(sc, 1, data >> 8);
366         uark_cfg_write(sc, 3, 0x03);
367
368         if (t->c_cflag & CSTOPB)
369                 data = UARK_STOP_BITS_2;
370         else
371                 data = UARK_STOP_BITS_1;
372
373         if (t->c_cflag & PARENB) {
374                 if (t->c_cflag & PARODD)
375                         data |= UARK_PARITY_ODD;
376                 else
377                         data |= UARK_PARITY_EVEN;
378         } else
379                 data |= UARK_PARITY_NONE;
380
381         switch (t->c_cflag & CSIZE) {
382         case CS5:
383                 data |= UARK_SET_DATA_BITS(5);
384                 break;
385         case CS6:
386                 data |= UARK_SET_DATA_BITS(6);
387                 break;
388         case CS7:
389                 data |= UARK_SET_DATA_BITS(7);
390                 break;
391         default:
392         case CS8:
393                 data |= UARK_SET_DATA_BITS(8);
394                 break;
395         }
396         uark_cfg_write(sc, 3, 0x00);
397         uark_cfg_write(sc, 3, data);
398 }
399
400 static void
401 uark_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
402 {
403         struct uark_softc *sc = ucom->sc_parent;
404
405         *lsr = sc->sc_lsr;
406         *msr = sc->sc_msr;
407 }
408
409 static void
410 uark_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
411 {
412         struct uark_softc *sc = ucom->sc_parent;
413
414         DPRINTF("onoff=%d\n", onoff);
415
416         uark_cfg_write(sc, 4, onoff ? 0x01 : 0x00);
417 }
418
419 static void
420 uark_cfg_write(struct uark_softc *sc, uint16_t index, uint16_t value)
421 {
422         struct usb_device_request req;
423         usb_error_t err;
424
425         req.bmRequestType = UARK_WRITE;
426         req.bRequest = UARK_REQUEST;
427         USETW(req.wValue, value);
428         USETW(req.wIndex, index);
429         USETW(req.wLength, 0);
430
431         err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
432             &req, NULL, 0, 1000);
433         if (err) {
434                 DPRINTFN(0, "device request failed, err=%s "
435                     "(ignored)\n", usbd_errstr(err));
436         }
437 }
438
439 static void
440 uark_poll(struct ucom_softc *ucom)
441 {
442         struct uark_softc *sc = ucom->sc_parent;
443         usbd_transfer_poll(sc->sc_xfer, UARK_N_TRANSFER);
444 }