]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/dev/usb/uark.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / 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         ucom->sc_dev = self;
142         ucom->sc_udev = dev;
143
144         devname = device_get_nameunit(ucom->sc_dev);
145
146         if (uaa->iface == NULL) {
147                 /* Move the device into the configured state. */
148                 error = usbd_set_config_index(dev, UARK_CONFIG_INDEX, 1);
149                 if (error) {
150                         printf("\n%s: failed to set configuration, err=%s\n",
151                                devname, usbd_errstr(error));
152                         goto bad;
153                 }
154                 error =
155                     usbd_device2interface_handle(dev, UARK_IFACE_INDEX, &iface);
156                 if (error) {
157                         printf("\n%s: failed to get interface, err=%s\n",
158                                devname, usbd_errstr(error));
159                         goto bad;
160                 }
161         } else
162                 iface = uaa->iface;
163
164         usbd_devinfo(dev, 0, devinfo);
165         printf("%s: %s\n", devname, devinfo);
166
167         id = usbd_get_interface_descriptor(iface);
168         ucom->sc_iface = iface;
169
170         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
171         for (i = 0; i < id->bNumEndpoints; i++) {
172                 ed = usbd_interface2endpoint_descriptor(iface, i);
173                 if (ed == NULL) {
174                         printf("%s: could not read endpoint descriptor\n",
175                             devname);
176                         goto bad;
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         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
186                 printf("%s: missing endpoint\n", devname);
187                 goto bad;
188         }
189         ucom->sc_parent = sc;
190         ucom->sc_ibufsize = UARKBUFSZ;
191         ucom->sc_obufsize = UARKBUFSZ;
192         ucom->sc_ibufsizepad = UARKBUFSZ;
193         ucom->sc_opkthdrlen = 0;
194
195         ucom->sc_callback = &uark_callback;
196
197         DPRINTF(("uark: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
198         ucom_attach(&sc->sc_ucom);
199         free(devinfo, M_USBDEV);
200
201         USB_ATTACH_SUCCESS_RETURN;
202
203 bad:
204         DPRINTF(("uark_attach: ATTACH ERROR\n"));
205         ucom->sc_dying = 1;
206         free(devinfo, M_USBDEV);
207
208         USB_ATTACH_ERROR_RETURN;
209 }
210
211 USB_DETACH(uark)
212 {
213         USB_DETACH_START(uark, sc);
214         int rv = 0;
215
216         DPRINTF(("uark_detach: sc=%p\n", sc));
217         sc->sc_ucom.sc_dying = 1;
218         rv = ucom_detach(&sc->sc_ucom);
219
220         return (rv);
221 }
222
223 static void
224 uark_set(void *vsc, int portno, int reg, int onoff)
225 {
226         struct uark_softc *sc = vsc;
227
228         switch (reg) {
229         case UCOM_SET_BREAK:
230                 uark_break(sc, portno, onoff);
231                 return;
232                 /* NOTREACHED */
233         case UCOM_SET_DTR:
234         case UCOM_SET_RTS:
235         default:
236                 return;
237                 /* NOTREACHED */
238         }
239 }
240
241 static int
242 uark_param(void *vsc, int portno, struct termios *t)
243 {
244         struct uark_softc *sc = (struct uark_softc *)vsc;
245         int data;
246
247         switch (t->c_ospeed) {
248         case 300:
249         case 600:
250         case 1200:
251         case 1800:
252         case 2400:
253         case 4800:
254         case 9600:
255         case 19200:
256         case 38400:
257         case 57600:
258         case 115200:
259                 uark_cmd(sc, 3, 0x83);
260                 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
261                 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
262                 uark_cmd(sc, 3, 0x03);
263                 break;
264         default:
265                 return (EINVAL);
266                 /* NOTREACHED */
267         }
268         if (ISSET(t->c_cflag, CSTOPB))
269                 data = UARK_STOP_BITS_2;
270         else
271                 data = UARK_STOP_BITS_1;
272
273         if (ISSET(t->c_cflag, PARENB)) {
274                 if (ISSET(t->c_cflag, PARODD))
275                         data |= UARK_PARITY_ODD;
276                 else
277                         data |= UARK_PARITY_EVEN;
278         } else
279                 data |= UARK_PARITY_NONE;
280
281         switch (ISSET(t->c_cflag, CSIZE)) {
282         case CS5:
283                 data |= UARK_SET_DATA_BITS(5);
284                 break;
285         case CS6:
286                 data |= UARK_SET_DATA_BITS(6);
287                 break;
288         case CS7:
289                 data |= UARK_SET_DATA_BITS(7);
290                 break;
291         case CS8:
292                 data |= UARK_SET_DATA_BITS(8);
293                 break;
294         }
295         uark_cmd(sc, 3, 0x00);
296         uark_cmd(sc, 3, data);
297
298         return (0);
299 }
300
301 void
302 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
303 {
304         struct uark_softc *sc = vsc;
305
306         if (msr != NULL)
307                 *msr = sc->sc_msr;
308         if (lsr != NULL)
309                 *lsr = sc->sc_lsr;
310 }
311
312 void
313 uark_break(void *vsc, int portno, int onoff)
314 {
315 #ifdef UARK_DEBUG
316         struct uark_softc *sc = vsc;
317
318         printf("%s: break %s!\n", device_get_nameunit(sc->sc_dev),
319             onoff ? "on" : "off");
320
321         if (onoff)
322                 /* break on */
323                 uark_cmd(sc, 4, 0x01);
324         else
325                 uark_cmd(sc, 4, 0x00);
326 #endif
327 }
328
329 int
330 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
331 {
332         usb_device_request_t req;
333         usbd_status err;
334         struct ucom_softc *ucom = &sc->sc_ucom;
335
336         req.bmRequestType = UARK_WRITE;
337         req.bRequest = UARK_REQUEST;
338         USETW(req.wValue, value);
339         USETW(req.wIndex, index);
340         USETW(req.wLength, 0);
341         err = usbd_do_request(ucom->sc_udev, &req, NULL);
342
343         if (err)
344                 return (EIO);
345
346         return (0);
347 }
348
349 static device_method_t uark_methods[] = {
350         /* Device interface */
351         DEVMETHOD(device_probe, uark_match),
352         DEVMETHOD(device_attach, uark_attach),
353         DEVMETHOD(device_detach, uark_detach),
354
355         { 0, 0 }
356 };
357
358 static driver_t uark_driver = {
359         "ucom",
360         uark_methods,
361         sizeof (struct uark_softc)
362 };
363
364 DRIVER_MODULE(uark, uhub, uark_driver, ucom_devclass, usbd_driver_load, 0);
365 MODULE_DEPEND(uark, usb, 1, 1, 1);
366 MODULE_DEPEND(uark, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);