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