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.
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>
26 #include <sys/ioccom.h>
27 #include <sys/fcntl.h>
31 #include <sys/selinfo.h>
32 #include <sys/sysctl.h>
34 #include <dev/usb/usb.h>
35 #include <dev/usb/usbdi.h>
36 #include <dev/usb/usbdi_util.h>
39 #include <dev/usb/ucomvar.h>
42 #define DPRINTFN(n, x) do { \
43 if (uarkdebug > (n)) \
48 #define DPRINTFN(n, x)
50 #define DPRINTF(x) DPRINTFN(0, x)
53 #define UARK_CONFIG_NO 0
54 #define UARK_IFACE_NO 0
56 #define UARK_SET_DATA_BITS(x) (x - 5)
58 #define UARK_PARITY_NONE 0x00
59 #define UARK_PARITY_ODD 0x08
60 #define UARK_PARITY_EVEN 0x18
62 #define UARK_STOP_BITS_1 0x00
63 #define UARK_STOP_BITS_2 0x04
65 #define UARK_BAUD_REF 3000000
67 #define UARK_WRITE 0x40
68 #define UARK_READ 0xc0
70 #define UARK_REQUEST 0xfe
72 #define UARK_CONFIG_INDEX 0
73 #define UARK_IFACE_INDEX 0
76 struct ucom_softc sc_ucom;
77 usbd_interface_handle sc_iface;
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);
89 struct ucom_callback uark_callback = {
100 static const struct uark_product {
103 } uark_products[] = {
104 { USB_VENDOR_ARKMICRO, USB_PRODUCT_ARKMICRO_ARK3116 },
110 USB_MATCH_START(uark, uaa);
113 if (uaa->iface != NULL)
114 return (UMATCH_NONE);
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);
123 return (UMATCH_NONE);
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;
137 struct ucom_softc *ucom = &sc->sc_ucom;
139 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
141 bzero(ucom, sizeof(struct ucom_softc));
145 devname = device_get_nameunit(ucom->sc_dev);
147 if (uaa->iface == NULL) {
148 /* Move the device into the configured state. */
149 error = usbd_set_config_index(dev, UARK_CONFIG_INDEX, 1);
151 printf("\n%s: failed to set configuration, err=%s\n",
152 devname, usbd_errstr(error));
156 usbd_device2interface_handle(dev, UARK_IFACE_INDEX, &iface);
158 printf("\n%s: failed to get interface, err=%s\n",
159 devname, usbd_errstr(error));
165 usbd_devinfo(dev, 0, devinfo);
166 printf("%s: %s\n", devname, devinfo);
168 id = usbd_get_interface_descriptor(iface);
169 ucom->sc_iface = iface;
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);
175 printf("%s: could not read endpoint descriptor\n",
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;
186 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
187 printf("%s: missing endpoint\n", devname);
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;
196 ucom->sc_callback = &uark_callback;
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);
202 USB_ATTACH_SUCCESS_RETURN;
205 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
207 free(devinfo, M_USBDEV);
209 USB_ATTACH_ERROR_RETURN;
214 USB_DETACH_START(uark, sc);
217 DPRINTF(("uark_detach: sc=%p\n", sc));
218 sc->sc_ucom.sc_dying = 1;
219 rv = ucom_detach(&sc->sc_ucom);
225 uark_set(void *vsc, int portno, int reg, int onoff)
227 struct uark_softc *sc = vsc;
231 uark_break(sc, portno, onoff);
243 uark_param(void *vsc, int portno, struct termios *t)
245 struct uark_softc *sc = (struct uark_softc *)vsc;
248 switch (t->c_ospeed) {
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);
269 if (ISSET(t->c_cflag, CSTOPB))
270 data = UARK_STOP_BITS_2;
272 data = UARK_STOP_BITS_1;
274 if (ISSET(t->c_cflag, PARENB)) {
275 if (ISSET(t->c_cflag, PARODD))
276 data |= UARK_PARITY_ODD;
278 data |= UARK_PARITY_EVEN;
280 data |= UARK_PARITY_NONE;
282 switch (ISSET(t->c_cflag, CSIZE)) {
284 data |= UARK_SET_DATA_BITS(5);
287 data |= UARK_SET_DATA_BITS(6);
290 data |= UARK_SET_DATA_BITS(7);
293 data |= UARK_SET_DATA_BITS(8);
296 uark_cmd(sc, 3, 0x00);
297 uark_cmd(sc, 3, data);
303 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
305 struct uark_softc *sc = vsc;
314 uark_break(void *vsc, int portno, int onoff)
317 struct uark_softc *sc = vsc;
319 printf("%s: break %s!\n", device_get_nameunit(sc->sc_dev),
320 onoff ? "on" : "off");
324 uark_cmd(sc, 4, 0x01);
326 uark_cmd(sc, 4, 0x00);
331 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
333 usb_device_request_t req;
335 struct ucom_softc *ucom = &sc->sc_ucom;
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);
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),
359 static driver_t uark_driver = {
362 sizeof (struct uark_softc)
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);