]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/uark.c
This commit was generated by cvs2svn to compensate for changes in r165182,
[FreeBSD/FreeBSD.git] / sys / dev / usb / 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 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
23 #include <sys/malloc.h>
24 #include <sys/module.h>
25 #include <sys/bus.h>
26 #include <sys/ioccom.h>
27 #include <sys/fcntl.h>
28 #include <sys/conf.h>
29 #include <sys/tty.h>
30 #include <sys/file.h>
31 #include <sys/selinfo.h>
32 #include <sys/sysctl.h>
33
34 #include <dev/usb/usb.h>
35 #include <dev/usb/usbdi.h>
36 #include <dev/usb/usbdi_util.h>
37 #include "usbdevs.h"
38
39 #include <dev/usb/ucomvar.h>
40
41 #ifdef UARK_DEBUG
42 #define DPRINTFN(n, x)  do {    \
43         if (uarkdebug > (n))    \
44                 logprintf x;    \
45 } while (0)
46 int     uarkebug = 0;
47 #else
48 #define DPRINTFN(n, x)
49 #endif
50 #define DPRINTF(x) DPRINTFN(0, x)
51
52 #define UARKBUFSZ               256
53 #define UARK_CONFIG_NO          0
54 #define UARK_IFACE_NO           0
55
56 #define UARK_SET_DATA_BITS(x)   (x - 5)
57
58 #define UARK_PARITY_NONE        0x00
59 #define UARK_PARITY_ODD         0x08
60 #define UARK_PARITY_EVEN        0x18
61
62 #define UARK_STOP_BITS_1        0x00
63 #define UARK_STOP_BITS_2        0x04
64
65 #define UARK_BAUD_REF           3000000
66
67 #define UARK_WRITE              0x40
68 #define UARK_READ               0xc0
69
70 #define UARK_REQUEST            0xfe
71
72 #define UARK_CONFIG_INDEX       0
73 #define UARK_IFACE_INDEX        0
74
75 struct uark_softc {
76         struct ucom_softc       sc_ucom;
77         usbd_interface_handle   sc_iface;
78
79         u_char                  sc_msr;
80         u_char                  sc_lsr;
81 };
82
83 static void     uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
84 static void     uark_set(void *, int, int, int);
85 static int      uark_param(void *, int, struct termios *);
86 static void     uark_break(void *, int, int);
87 static int      uark_cmd(struct uark_softc *, uint16_t, uint16_t);
88
89 struct ucom_callback uark_callback = {
90         uark_get_status,
91         uark_set,
92         uark_param,
93         NULL,
94         NULL,
95         NULL,
96         NULL,
97         NULL,
98 };
99
100 static const struct uark_product {
101         uint16_t        vendor;
102         uint16_t        product;
103 } uark_products[] = {
104         { USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116 },
105         { 0, 0 }
106 };
107
108 USB_MATCH(uark)
109 {
110         USB_MATCH_START(uark, uaa);
111         int i;
112
113         if (uaa->iface != NULL)
114                 return (UMATCH_NONE);
115
116         for (i = 0; uark_products[i].vendor != 0; i++) {
117                 if (uark_products[i].vendor == uaa->vendor &&
118                     uark_products[i].product == uaa->product) {
119                         return (UMATCH_VENDOR_PRODUCT);
120                 }
121         }
122
123         return (UMATCH_NONE);
124 }
125
126 USB_ATTACH(uark)
127 {
128         USB_ATTACH_START(uark, sc, uaa);
129         usbd_device_handle dev = uaa->device;
130         usbd_interface_handle iface;
131         usb_interface_descriptor_t *id;
132         usb_endpoint_descriptor_t *ed;
133         usbd_status error;
134         char *devinfo;
135         const char *devname;
136         int i;
137         struct ucom_softc *ucom = &sc->sc_ucom;
138
139         devinfo = malloc(1024, M_USBDEV, M_WAITOK);
140
141         bzero(ucom, sizeof(struct ucom_softc));
142         ucom->sc_dev = self;
143         ucom->sc_udev = dev;
144
145         devname = device_get_nameunit(ucom->sc_dev);
146
147         if (uaa->iface == NULL) {
148                 /* Move the device into the configured state. */
149                 error = usbd_set_config_index(dev, UARK_CONFIG_INDEX, 1);
150                 if (error) {
151                         printf("\n%s: failed to set configuration, err=%s\n",
152                                devname, usbd_errstr(error));
153                         goto bad;
154                 }
155                 error =
156                     usbd_device2interface_handle(dev, UARK_IFACE_INDEX, &iface);
157                 if (error) {
158                         printf("\n%s: failed to get interface, err=%s\n",
159                                devname, usbd_errstr(error));
160                         goto bad;
161                 }
162         } else
163                 iface = uaa->iface;
164
165         usbd_devinfo(dev, 0, devinfo);
166         printf("%s: %s\n", devname, devinfo);
167
168         id = usbd_get_interface_descriptor(iface);
169         ucom->sc_iface = iface;
170
171         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
172         for (i = 0; i < id->bNumEndpoints; i++) {
173                 ed = usbd_interface2endpoint_descriptor(iface, i);
174                 if (ed == NULL) {
175                         printf("%s: could not read endpoint descriptor\n",
176                             devname);
177                         goto bad;
178                 }
179                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
180                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
181                         ucom->sc_bulkin_no = ed->bEndpointAddress;
182                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
183                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
184                         ucom->sc_bulkout_no = ed->bEndpointAddress;
185         }
186         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
187                 printf("%s: missing endpoint\n", devname);
188                 goto bad;
189         }
190         ucom->sc_parent = sc;
191         ucom->sc_ibufsize = UARKBUFSZ;
192         ucom->sc_obufsize = UARKBUFSZ;
193         ucom->sc_ibufsizepad = UARKBUFSZ;
194         ucom->sc_opkthdrlen = 0;
195
196         ucom->sc_callback = &uark_callback;
197
198         DPRINTF(("uark: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
199         ucom_attach(&sc->sc_ucom);
200         free(devinfo, M_USBDEV);
201
202         USB_ATTACH_SUCCESS_RETURN;
203
204 bad:
205         DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
206         ucom->sc_dying = 1;
207         free(devinfo, M_USBDEV);
208
209         USB_ATTACH_ERROR_RETURN;
210 }
211
212 USB_DETACH(uark)
213 {
214         USB_DETACH_START(uark, sc);
215         int rv = 0;
216
217         DPRINTF(("uark_detach: sc=%p\n", sc));
218         sc->sc_ucom.sc_dying = 1;
219         rv = ucom_detach(&sc->sc_ucom);
220
221         return (rv);
222 }
223
224 static void
225 uark_set(void *vsc, int portno, int reg, int onoff)
226 {
227         struct uark_softc *sc = vsc;
228
229         switch (reg) {
230         case UCOM_SET_BREAK:
231                 uark_break(sc, portno, onoff);
232                 return;
233                 /* NOTREACHED */
234         case UCOM_SET_DTR:
235         case UCOM_SET_RTS:
236         default:
237                 return;
238                 /* NOTREACHED */
239         }
240 }
241
242 static int
243 uark_param(void *vsc, int portno, struct termios *t)
244 {
245         struct uark_softc *sc = (struct uark_softc *)vsc;
246         int data;
247
248         switch (t->c_ospeed) {
249         case 300:
250         case 600:
251         case 1200:
252         case 1800:
253         case 2400:
254         case 4800:
255         case 9600:
256         case 19200:
257         case 38400:
258         case 57600:
259         case 115200:
260                 uark_cmd(sc, 3, 0x83);
261                 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
262                 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
263                 uark_cmd(sc, 3, 0x03);
264                 break;
265         default:
266                 return (EINVAL);
267                 /* NOTREACHED */
268         }
269         if (ISSET(t->c_cflag, CSTOPB))
270                 data = UARK_STOP_BITS_2;
271         else
272                 data = UARK_STOP_BITS_1;
273
274         if (ISSET(t->c_cflag, PARENB)) {
275                 if (ISSET(t->c_cflag, PARODD))
276                         data |= UARK_PARITY_ODD;
277                 else
278                         data |= UARK_PARITY_EVEN;
279         } else
280                 data |= UARK_PARITY_NONE;
281
282         switch (ISSET(t->c_cflag, CSIZE)) {
283         case CS5:
284                 data |= UARK_SET_DATA_BITS(5);
285                 break;
286         case CS6:
287                 data |= UARK_SET_DATA_BITS(6);
288                 break;
289         case CS7:
290                 data |= UARK_SET_DATA_BITS(7);
291                 break;
292         case CS8:
293                 data |= UARK_SET_DATA_BITS(8);
294                 break;
295         }
296         uark_cmd(sc, 3, 0x00);
297         uark_cmd(sc, 3, data);
298
299         return (0);
300 }
301
302 void
303 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
304 {
305         struct uark_softc *sc = vsc;
306
307         if (msr != NULL)
308                 *msr = sc->sc_msr;
309         if (lsr != NULL)
310                 *lsr = sc->sc_lsr;
311 }
312
313 void
314 uark_break(void *vsc, int portno, int onoff)
315 {
316 #ifdef UARK_DEBUG
317         struct uark_softc *sc = vsc;
318
319         printf("%s: break %s!\n", device_get_nameunit(sc->sc_dev),
320             onoff ? "on" : "off");
321
322         if (onoff)
323                 /* break on */
324                 uark_cmd(sc, 4, 0x01);
325         else
326                 uark_cmd(sc, 4, 0x00);
327 #endif
328 }
329
330 int
331 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
332 {
333         usb_device_request_t req;
334         usbd_status err;
335         struct ucom_softc *ucom = &sc->sc_ucom;
336
337         req.bmRequestType = UARK_WRITE;
338         req.bRequest = UARK_REQUEST;
339         USETW(req.wValue, value);
340         USETW(req.wIndex, index);
341         USETW(req.wLength, 0);
342         err = usbd_do_request(ucom->sc_udev, &req, NULL);
343
344         if (err)
345                 return (EIO);
346
347         return (0);
348 }
349
350 static device_method_t uark_methods[] = {
351         /* Device interface */
352         DEVMETHOD(device_probe, uark_match),
353         DEVMETHOD(device_attach, uark_attach),
354         DEVMETHOD(device_detach, uark_detach),
355
356         { 0, 0 }
357 };
358
359 static driver_t uark_driver = {
360         "ucom",
361         uark_methods,
362         sizeof (struct uark_softc)
363 };
364
365 DRIVER_MODULE(uark, uhub, uark_driver, ucom_devclass, usbd_driver_load, 0);
366 MODULE_DEPEND(uark, usb, 1, 1, 1);
367 MODULE_DEPEND(uark, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);