Add uark(4) driver which supports Arkmicro Technologies ARK3116 chip found
[dragonfly.git] / sys / dev / usbmisc / uark / uark.c
blob1ceea551afe69343f7c4ab4e00e89a1b945b6931
1 /* $DragonFly: src/sys/dev/usbmisc/uark/uark.c,v 1.1 2007/08/14 22:28:11 hasso Exp $ */
2 /* $OpenBSD: uark.c,v 1.9 2007/06/13 06:25:03 mbalmer Exp $ */
4 /*
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
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.
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.
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>
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>
35 #include <dev/usbmisc/ucom/ucomvar.h>
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)
45 #define UARKBUFSZ 256
46 #define UARK_CONFIG_NO 0
47 #define UARK_IFACE_NO 0
49 #define UARK_SET_DATA_BITS(x) (x - 5)
51 #define UARK_PARITY_NONE 0x00
52 #define UARK_PARITY_ODD 0x08
53 #define UARK_PARITY_EVEN 0x18
55 #define UARK_STOP_BITS_1 0x00
56 #define UARK_STOP_BITS_2 0x04
58 #define UARK_BAUD_REF 3000000
60 #define UARK_WRITE 0x40
61 #define UARK_READ 0xc0
63 #define UARK_REQUEST 0xfe
65 struct uark_softc {
66 struct ucom_softc sc_ucom;
67 u_char sc_msr;
68 u_char sc_lsr;
71 void uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
72 void uark_set(void *, int, int, int);
73 int uark_param(void *, int, struct termios *);
74 int uark_open(void *sc, int);
75 void uark_break(void *, int, int);
76 int uark_cmd(struct uark_softc *, uint16_t, uint16_t);
78 struct ucom_callback uark_callback = {
79 uark_get_status,
80 uark_set,
81 uark_param,
82 NULL,
83 NULL,
84 NULL,
85 NULL,
86 NULL,
89 static const struct usb_devno uark_devs[] = {
90 { USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116 }
93 static device_probe_t uark_match;
94 static device_attach_t uark_attach;
95 static device_detach_t uark_detach;
97 static device_method_t uark_methods[] = {
98 /* Device interface */
99 DEVMETHOD(device_probe, uark_match),
100 DEVMETHOD(device_attach, uark_attach),
101 DEVMETHOD(device_detach, uark_detach),
102 { 0, 0 }
105 static driver_t uark_driver = {
106 "ucom",
107 uark_methods,
108 sizeof (struct uark_softc)
111 DRIVER_MODULE(uark, uhub, uark_driver, ucom_devclass, usbd_driver_load, 0);
112 MODULE_DEPEND(uark, usb, 1, 1, 1);
113 MODULE_DEPEND(uark, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
114 MODULE_VERSION(uark, 1);
116 static int
117 uark_match(device_t self)
119 struct usb_attach_arg *uaa = device_get_ivars(self);
121 if (uaa->iface != NULL)
122 return UMATCH_NONE;
124 return (usb_lookup(uark_devs, uaa->vendor, uaa->product) != NULL) ?
125 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
128 static int
129 uark_attach(device_t self)
131 struct uark_softc *sc = device_get_softc(self);
132 struct usb_attach_arg *uaa = device_get_ivars(self);
133 struct ucom_softc *ucom;
134 usb_interface_descriptor_t *id;
135 usb_endpoint_descriptor_t *ed;
136 usbd_status error;
137 char *devinfo;
138 const char *devname;
139 int i;
141 devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
142 ucom = &sc->sc_ucom;
144 bzero(sc, sizeof (struct uark_softc));
146 usbd_devinfo(uaa->device, 0, devinfo);
147 ucom->sc_dev = self;
148 device_set_desc_copy(self, devinfo);
150 ucom->sc_udev = uaa->device;
151 ucom->sc_iface = uaa->iface;
153 devname = device_get_nameunit(ucom->sc_dev);
154 kprintf("%s: %s\n", devname, devinfo);
155 kfree(devinfo, M_USBDEV);
157 if (usbd_set_config_index(ucom->sc_udev, UARK_CONFIG_NO, 1) != 0) {
158 kprintf("%s: could not set configuration no\n", devname);
159 goto error;
162 /* get the first interface handle */
163 error = usbd_device2interface_handle(ucom->sc_udev, UARK_IFACE_NO,
164 &ucom->sc_iface);
165 if (error != 0) {
166 kprintf("%s: could not get interface handle\n", devname);
167 goto error;
170 id = usbd_get_interface_descriptor(ucom->sc_iface);
172 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
173 for (i = 0; i < id->bNumEndpoints; i++) {
174 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
175 if (ed == NULL) {
176 kprintf("%s: no endpoint descriptor found for %d\n",
177 devname, i);
178 goto error;
181 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
182 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
183 ucom->sc_bulkin_no = ed->bEndpointAddress;
184 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
185 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
186 ucom->sc_bulkout_no = ed->bEndpointAddress;
189 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
190 kprintf("%s: missing endpoint\n", devname);
191 goto error;
194 ucom->sc_parent = sc;
195 ucom->sc_portno = UCOM_UNK_PORTNO;
196 ucom->sc_ibufsize = UARKBUFSZ;
197 ucom->sc_obufsize = UARKBUFSZ;
198 ucom->sc_ibufsizepad = UARKBUFSZ;
199 ucom->sc_opkthdrlen = 0;
200 ucom->sc_callback = &uark_callback;
202 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
203 ucom->sc_dev);
205 DPRINTF(("uark: in = 0x%x, out = 0x%x, intr = 0x%x\n",
206 ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
208 ucom_attach(&sc->sc_ucom);
210 return 0;
212 error:
213 ucom->sc_dying = 1;
214 return ENXIO;
217 static int
218 uark_detach(device_t self)
220 struct uark_softc *sc = device_get_softc(self);
221 int rv = 0;
223 DPRINTF(("uark_detach: sc=%p\n", sc));
224 sc->sc_ucom.sc_dying = 1;
225 rv = ucom_detach(&sc->sc_ucom);
226 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
227 sc->sc_ucom.sc_dev);
229 return (rv);
232 #if 0 /* not yet */
234 uark_activate(struct device *self, enum devact act)
236 struct uark_softc *sc = (struct uark_softc *)self;
237 int rv = 0;
239 switch (act) {
240 case DVACT_ACTIVATE:
241 break;
243 case DVACT_DEACTIVATE:
244 if (sc->sc_subdev != NULL)
245 rv = config_deactivate(sc->sc_subdev);
246 sc->sc_dying = 1;
247 break;
249 return (rv);
251 #endif
253 void
254 uark_set(void *vsc, int portno, int reg, int onoff)
256 struct uark_softc *sc = vsc;
258 switch (reg) {
259 case UCOM_SET_BREAK:
260 uark_break(sc, portno, onoff);
261 return;
262 case UCOM_SET_DTR:
263 case UCOM_SET_RTS:
264 default:
265 return;
270 uark_param(void *vsc, int portno, struct termios *t)
272 struct uark_softc *sc = (struct uark_softc *)vsc;
273 int data;
275 switch (t->c_ospeed) {
276 case 300:
277 case 600:
278 case 1200:
279 case 1800:
280 case 2400:
281 case 4800:
282 case 9600:
283 case 19200:
284 case 38400:
285 case 57600:
286 case 115200:
287 uark_cmd(sc, 3, 0x83);
288 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
289 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
290 uark_cmd(sc, 3, 0x03);
291 break;
292 default:
293 return (EINVAL);
296 if (ISSET(t->c_cflag, CSTOPB))
297 data = UARK_STOP_BITS_2;
298 else
299 data = UARK_STOP_BITS_1;
301 if (ISSET(t->c_cflag, PARENB)) {
302 if (ISSET(t->c_cflag, PARODD))
303 data |= UARK_PARITY_ODD;
304 else
305 data |= UARK_PARITY_EVEN;
306 } else
307 data |= UARK_PARITY_NONE;
309 switch (ISSET(t->c_cflag, CSIZE)) {
310 case CS5:
311 data |= UARK_SET_DATA_BITS(5);
312 break;
313 case CS6:
314 data |= UARK_SET_DATA_BITS(6);
315 break;
316 case CS7:
317 data |= UARK_SET_DATA_BITS(7);
318 break;
319 case CS8:
320 data |= UARK_SET_DATA_BITS(8);
321 break;
324 uark_cmd(sc, 3, 0x00);
325 uark_cmd(sc, 3, data);
327 #if 0
328 /* XXX flow control */
329 if (ISSET(t->c_cflag, CRTSCTS))
330 /* rts/cts flow ctl */
331 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
332 /* xon/xoff flow ctl */
333 } else {
334 /* disable flow ctl */
336 #endif
338 return (0);
341 void
342 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
344 struct uark_softc *sc = vsc;
346 if (msr != NULL)
347 *msr = sc->sc_msr;
348 if (lsr != NULL)
349 *lsr = sc->sc_lsr;
352 void
353 uark_break(void *vsc, int portno, int onoff)
355 #ifdef UARK_DEBUG
356 struct uark_softc *sc = vsc;
358 kprintf("%s: break %s!\n", device_get_nameunit(sc->sc_ucom->sc_dev),
359 onoff ? "on" : "off");
361 if (onoff)
362 /* break on */
363 uark_cmd(sc, 4, 0x01);
364 else
365 uark_cmd(sc, 4, 0x00);
366 #endif
370 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
372 usb_device_request_t req;
373 usbd_status err;
375 req.bmRequestType = UARK_WRITE;
376 req.bRequest = UARK_REQUEST;
377 USETW(req.wValue, value);
378 USETW(req.wIndex, index);
379 USETW(req.wLength, 0);
380 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, NULL);
382 if (err)
383 return (EIO);
385 return (0);