1 /* $OpenBSD: uark.c,v 1.1 2006/08/14 08:30:22 jsg Exp $ */
4 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
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.
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.
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/kernel.h>
24 #include <sys/malloc.h>
25 #include <sys/module.h>
27 #include <sys/ioccom.h>
28 #include <sys/fcntl.h>
32 #include <sys/selinfo.h>
33 #include <sys/sysctl.h>
35 #include <dev/usb/usb.h>
36 #include <dev/usb/usbdi.h>
37 #include <dev/usb/usbdi_util.h>
40 #include <dev/usb/ucomvar.h>
43 #define DPRINTFN(n, x) do { \
44 if (uarkdebug > (n)) \
49 #define DPRINTFN(n, x)
51 #define DPRINTF(x) DPRINTFN(0, x)
54 #define UARK_CONFIG_NO 0
55 #define UARK_IFACE_NO 0
57 #define UARK_SET_DATA_BITS(x) (x - 5)
59 #define UARK_PARITY_NONE 0x00
60 #define UARK_PARITY_ODD 0x08
61 #define UARK_PARITY_EVEN 0x18
63 #define UARK_STOP_BITS_1 0x00
64 #define UARK_STOP_BITS_2 0x04
66 #define UARK_BAUD_REF 3000000
68 #define UARK_WRITE 0x40
69 #define UARK_READ 0xc0
71 #define UARK_REQUEST 0xfe
73 #define UARK_CONFIG_INDEX 0
74 #define UARK_IFACE_INDEX 0
77 struct ucom_softc sc_ucom;
78 usbd_interface_handle sc_iface;
84 static void uark_get_status(void *, int portno, u_char *lsr, u_char *msr);
85 static void uark_set(void *, int, int, int);
86 static int uark_param(void *, int, struct termios *);
87 static void uark_break(void *, int, int);
88 static int uark_cmd(struct uark_softc *, uint16_t, uint16_t);
90 struct ucom_callback uark_callback = {
101 static const struct usb_devno uark_devs[] = {
102 { USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116 }
106 uark_match(device_t self)
108 struct usb_attach_arg *uaa = device_get_ivars(self);
110 if (uaa->iface != NULL)
111 return (UMATCH_NONE);
113 return (usb_lookup(uark_devs, uaa->vendor, uaa->product) != NULL) ?
114 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
118 uark_attach(device_t self)
120 struct uark_softc *sc = device_get_softc(self);
121 struct usb_attach_arg *uaa = device_get_ivars(self);
122 usbd_device_handle dev = uaa->device;
123 usbd_interface_handle iface;
124 usb_interface_descriptor_t *id;
125 usb_endpoint_descriptor_t *ed;
128 struct ucom_softc *ucom = &sc->sc_ucom;
133 if (uaa->iface == NULL) {
134 /* Move the device into the configured state. */
135 error = usbd_set_config_index(dev, UARK_CONFIG_INDEX, 1);
137 device_printf(ucom->sc_dev,
138 "failed to set configuration, err=%s\n",
143 usbd_device2interface_handle(dev, UARK_IFACE_INDEX, &iface);
145 device_printf(ucom->sc_dev,
146 "failed to get interface, err=%s\n",
153 id = usbd_get_interface_descriptor(iface);
154 ucom->sc_iface = iface;
156 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
157 for (i = 0; i < id->bNumEndpoints; i++) {
158 ed = usbd_interface2endpoint_descriptor(iface, i);
160 device_printf(ucom->sc_dev,
161 "could not read endpoint descriptor\n");
164 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
165 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
166 ucom->sc_bulkin_no = ed->bEndpointAddress;
167 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
168 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
169 ucom->sc_bulkout_no = ed->bEndpointAddress;
171 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
172 device_printf(ucom->sc_dev, "missing endpoint\n");
175 ucom->sc_parent = sc;
176 ucom->sc_ibufsize = UARKBUFSZ;
177 ucom->sc_obufsize = UARKBUFSZ;
178 ucom->sc_ibufsizepad = UARKBUFSZ;
179 ucom->sc_opkthdrlen = 0;
181 ucom->sc_callback = &uark_callback;
183 DPRINTF(("uark: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
184 ucom_attach(&sc->sc_ucom);
188 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
194 uark_detach(device_t self)
196 struct uark_softc *sc = device_get_softc(self);
199 DPRINTF(("uark_detach: sc=%p\n", sc));
200 sc->sc_ucom.sc_dying = 1;
201 rv = ucom_detach(&sc->sc_ucom);
207 uark_set(void *vsc, int portno, int reg, int onoff)
209 struct uark_softc *sc = vsc;
213 uark_break(sc, portno, onoff);
225 uark_param(void *vsc, int portno, struct termios *t)
227 struct uark_softc *sc = (struct uark_softc *)vsc;
230 switch (t->c_ospeed) {
242 uark_cmd(sc, 3, 0x83);
243 uark_cmd(sc, 0, (UARK_BAUD_REF / t->c_ospeed) & 0xFF);
244 uark_cmd(sc, 1, (UARK_BAUD_REF / t->c_ospeed) >> 8);
245 uark_cmd(sc, 3, 0x03);
251 if (ISSET(t->c_cflag, CSTOPB))
252 data = UARK_STOP_BITS_2;
254 data = UARK_STOP_BITS_1;
256 if (ISSET(t->c_cflag, PARENB)) {
257 if (ISSET(t->c_cflag, PARODD))
258 data |= UARK_PARITY_ODD;
260 data |= UARK_PARITY_EVEN;
262 data |= UARK_PARITY_NONE;
264 switch (ISSET(t->c_cflag, CSIZE)) {
266 data |= UARK_SET_DATA_BITS(5);
269 data |= UARK_SET_DATA_BITS(6);
272 data |= UARK_SET_DATA_BITS(7);
275 data |= UARK_SET_DATA_BITS(8);
278 uark_cmd(sc, 3, 0x00);
279 uark_cmd(sc, 3, data);
285 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
287 struct uark_softc *sc = vsc;
296 uark_break(void *vsc, int portno, int onoff)
299 struct uark_softc *sc = vsc;
301 device_printf(sc->sc_dev, "%s: break %s!\n", onoff ? "on" : "off");
304 uark_cmd(sc, 4, 0x01);
306 uark_cmd(sc, 4, 0x00);
311 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
313 usb_device_request_t req;
315 struct ucom_softc *ucom = &sc->sc_ucom;
317 req.bmRequestType = UARK_WRITE;
318 req.bRequest = UARK_REQUEST;
319 USETW(req.wValue, value);
320 USETW(req.wIndex, index);
321 USETW(req.wLength, 0);
322 err = usbd_do_request(ucom->sc_udev, &req, NULL);
330 static device_method_t uark_methods[] = {
331 /* Device interface */
332 DEVMETHOD(device_probe, uark_match),
333 DEVMETHOD(device_attach, uark_attach),
334 DEVMETHOD(device_detach, uark_detach),
339 static driver_t uark_driver = {
342 sizeof (struct uark_softc)
345 DRIVER_MODULE(uark, uhub, uark_driver, ucom_devclass, usbd_driver_load, 0);
346 MODULE_DEPEND(uark, usb, 1, 1, 1);
347 MODULE_DEPEND(uark, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);