Merge from vendor branch TCPDUMP:
[dragonfly.git] / sys / dev / usbmisc / uark / uark.c
1 /*      $DragonFly: src/sys/dev/usbmisc/uark/uark.c,v 1.3 2007/08/19 17:16:43 hasso Exp $       */
2 /*      $OpenBSD: uark.c,v 1.9 2007/06/13 06:25:03 mbalmer 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 #include <sys/types.h>
27 #include <sys/bus.h>
28 #include <sys/module.h>
29
30 #include <bus/usb/usb.h>
31 #include <bus/usb/usbdi.h>
32 #include <bus/usb/usbdi_util.h>
33 #include <bus/usb/usbdevs.h>
34
35 #include <dev/usbmisc/ucom/ucomvar.h>
36
37 #ifdef UARK_DEBUG
38 #define DPRINTFN(n, x)  do { if (uarkdebug > (n)) kprintf x; } while (0)
39 int     uarkebug = 0;
40 #else
41 #define DPRINTFN(n, x)
42 #endif
43 #define DPRINTF(x) DPRINTFN(0, x)
44
45 #define UARKBUFSZ               256
46 #define UARK_CONFIG_NO          0
47 #define UARK_IFACE_NO           0
48
49 #define UARK_SET_DATA_BITS(x)   (x - 5)
50
51 #define UARK_PARITY_NONE        0x00
52 #define UARK_PARITY_ODD         0x08
53 #define UARK_PARITY_EVEN        0x18
54
55 #define UARK_STOP_BITS_1        0x00
56 #define UARK_STOP_BITS_2        0x04
57
58 #define UARK_BAUD_REF           3000000
59
60 #define UARK_WRITE              0x40
61 #define UARK_READ               0xc0
62
63 #define UARK_REQUEST            0xfe
64
65 struct uark_softc {
66         struct ucom_softc       sc_ucom;
67         u_char                  sc_msr;
68         u_char                  sc_lsr;
69 };
70
71 static void     uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
72 static void     uark_set(void *, int, int, int);
73 static int      uark_param(void *, int, struct termios *);
74 static void     uark_break(void *, int, int);
75 static int      uark_cmd(struct uark_softc *, uint16_t, uint16_t);
76
77 struct ucom_callback uark_callback = {
78         uark_get_status,
79         uark_set,
80         uark_param,
81         NULL,
82         NULL,
83         NULL,
84         NULL,
85         NULL,
86 };
87
88 static const struct usb_devno uark_devs[] = {
89         { USB_VENDOR_ARKMICRO,          USB_PRODUCT_ARKMICRO_ARK3116 }
90 };
91
92 static device_probe_t uark_match;
93 static device_attach_t uark_attach;
94 static device_detach_t uark_detach;
95
96 static device_method_t uark_methods[] = {
97         /* Device interface */
98         DEVMETHOD(device_probe, uark_match),
99         DEVMETHOD(device_attach, uark_attach),
100         DEVMETHOD(device_detach, uark_detach),
101         { 0, 0 }
102 };
103
104 static driver_t uark_driver = {
105         "ucom",
106         uark_methods,
107         sizeof (struct uark_softc)
108 };
109
110 DRIVER_MODULE(uark, uhub, uark_driver, ucom_devclass, usbd_driver_load, 0);
111 MODULE_DEPEND(uark, usb, 1, 1, 1);
112 MODULE_DEPEND(uark, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
113 MODULE_VERSION(uark, 1);
114
115 static int
116 uark_match(device_t self)
117 {
118         struct usb_attach_arg *uaa = device_get_ivars(self);
119
120         if (uaa->iface != NULL)
121                 return UMATCH_NONE;
122
123         return (usb_lookup(uark_devs, uaa->vendor, uaa->product) != NULL) ?
124             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
125 }
126
127 static int
128 uark_attach(device_t self)
129 {
130         struct uark_softc *sc = device_get_softc(self);
131         struct usb_attach_arg *uaa = device_get_ivars(self);
132         struct ucom_softc *ucom;
133         usb_interface_descriptor_t *id;
134         usb_endpoint_descriptor_t *ed;
135         usbd_status error;
136         char *devinfo;
137         int i;
138
139         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
140         ucom = &sc->sc_ucom;
141
142         bzero(sc, sizeof (struct uark_softc));
143
144         usbd_devinfo(uaa->device, 0, devinfo);
145         ucom->sc_dev = self;
146         device_set_desc_copy(self, devinfo);
147
148         ucom->sc_udev = uaa->device;
149         ucom->sc_iface = uaa->iface;
150
151         device_printf(ucom->sc_dev, "%s\n", devinfo);
152         kfree(devinfo, M_USBDEV);
153
154         if (usbd_set_config_index(ucom->sc_udev, UARK_CONFIG_NO, 1) != 0) {
155                 device_printf(ucom->sc_dev, "could not set configuration no\n");
156                 goto error;
157         }
158
159         /* get the first interface handle */
160         error = usbd_device2interface_handle(ucom->sc_udev, UARK_IFACE_NO,
161             &ucom->sc_iface);
162         if (error != 0) {
163                 device_printf(ucom->sc_dev, "could not get interface handle\n");
164                 goto error;
165         }
166
167         id = usbd_get_interface_descriptor(ucom->sc_iface);
168
169         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
170         for (i = 0; i < id->bNumEndpoints; i++) {
171                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
172                 if (ed == NULL) {
173                         device_printf(ucom->sc_dev, "no endpoint descriptor "
174                                       "found for %d\n", i);
175                         goto error;
176                 }
177
178                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
179                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
180                         ucom->sc_bulkin_no = ed->bEndpointAddress;
181                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
182                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
183                         ucom->sc_bulkout_no = ed->bEndpointAddress;
184         }
185
186         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
187                 device_printf(ucom->sc_dev, "missing endpoint\n");
188                 goto error;
189         }
190
191         ucom->sc_parent = sc;
192         ucom->sc_portno = UCOM_UNK_PORTNO;
193         ucom->sc_ibufsize = UARKBUFSZ;
194         ucom->sc_obufsize = UARKBUFSZ;
195         ucom->sc_ibufsizepad = UARKBUFSZ;
196         ucom->sc_opkthdrlen = 0;
197         ucom->sc_callback = &uark_callback;
198
199         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
200                            ucom->sc_dev);
201         
202         DPRINTF(("uark: in = 0x%x, out = 0x%x, intr = 0x%x\n",
203                  ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
204
205         ucom_attach(&sc->sc_ucom);
206
207         return 0;
208
209 error:
210         ucom->sc_dying = 1;
211         return ENXIO;
212 }
213
214 static int
215 uark_detach(device_t self)
216 {
217         struct uark_softc *sc = device_get_softc(self);
218         int rv = 0;
219
220         DPRINTF(("uark_detach: sc=%p\n", sc));
221         sc->sc_ucom.sc_dying = 1;
222         rv = ucom_detach(&sc->sc_ucom);
223         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
224                            sc->sc_ucom.sc_dev);
225
226         return (rv);
227 }
228
229 #if 0 /* not yet */
230 int
231 uark_activate(struct device *self, enum devact act)
232 {
233         struct uark_softc *sc = (struct uark_softc *)self;
234         int rv = 0;
235
236         switch (act) {
237         case DVACT_ACTIVATE:
238                 break;
239
240         case DVACT_DEACTIVATE:
241                 if (sc->sc_subdev != NULL)
242                         rv = config_deactivate(sc->sc_subdev);
243                 sc->sc_dying = 1;
244                 break;
245         }
246         return (rv);
247 }
248 #endif
249
250 static void
251 uark_set(void *vsc, int portno, int reg, int onoff)
252 {
253         struct uark_softc *sc = vsc;
254
255         switch (reg) {
256         case UCOM_SET_BREAK:
257                 uark_break(sc, portno, onoff);
258                 return;
259         case UCOM_SET_DTR:
260         case UCOM_SET_RTS:
261         default:
262                 return;
263         }
264 }
265
266 static int
267 uark_param(void *vsc, int portno, struct termios *t)
268 {
269         struct uark_softc *sc = (struct uark_softc *)vsc;
270         int data;
271
272         switch (t->c_ospeed) {
273         case 300:
274         case 600:
275         case 1200:
276         case 1800:
277         case 2400:
278         case 4800:
279         case 9600:
280         case 19200:
281         case 38400:
282         case 57600:
283         case 115200:
284                 uark_cmd(sc, 3, 0x83);
285                 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
286                 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
287                 uark_cmd(sc, 3, 0x03);
288                 break;
289         default:
290                 return (EINVAL);
291         }
292
293         if (ISSET(t->c_cflag, CSTOPB))
294                 data = UARK_STOP_BITS_2;
295         else
296                 data = UARK_STOP_BITS_1;
297
298         if (ISSET(t->c_cflag, PARENB)) {
299                 if (ISSET(t->c_cflag, PARODD))
300                         data |= UARK_PARITY_ODD;
301                 else
302                         data |= UARK_PARITY_EVEN;
303         } else
304                 data |= UARK_PARITY_NONE;
305
306         switch (ISSET(t->c_cflag, CSIZE)) {
307         case CS5:
308                 data |= UARK_SET_DATA_BITS(5);
309                 break;
310         case CS6:
311                 data |= UARK_SET_DATA_BITS(6);
312                 break;
313         case CS7:
314                 data |= UARK_SET_DATA_BITS(7);
315                 break;
316         case CS8:
317                 data |= UARK_SET_DATA_BITS(8);
318                 break;
319         }
320
321         uark_cmd(sc, 3, 0x00);
322         uark_cmd(sc, 3, data);
323
324 #if 0
325         /* XXX flow control */
326         if (ISSET(t->c_cflag, CRTSCTS))
327                 /*  rts/cts flow ctl */
328         } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
329                 /*  xon/xoff flow ctl */
330         } else {
331                 /* disable flow ctl */
332         }
333 #endif
334
335         return (0);
336 }
337
338 static void
339 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
340 {
341         struct uark_softc *sc = vsc;
342         
343         if (msr != NULL)
344                 *msr = sc->sc_msr;
345         if (lsr != NULL)
346                 *lsr = sc->sc_lsr;
347 }
348
349 static void
350 uark_break(void *vsc, int portno, int onoff)
351 {
352 #ifdef UARK_DEBUG
353         struct uark_softc *sc = vsc;
354
355         device_printf(sc->sc_ucom.sc_dev, "break %s!\n", onoff ? "on" : "off");
356
357         if (onoff)
358                 /* break on */
359                 uark_cmd(sc, 4, 0x01);
360         else
361                 uark_cmd(sc, 4, 0x00);
362 #endif
363 }
364
365 static int
366 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
367 {
368         usb_device_request_t req;
369         usbd_status err;
370
371         req.bmRequestType = UARK_WRITE;
372         req.bRequest = UARK_REQUEST;
373         USETW(req.wValue, value);
374         USETW(req.wIndex, index);
375         USETW(req.wLength, 0);
376         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
377
378         if (err)
379                 return (EIO);
380
381         return (0);
382 }