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);
144 devname = device_get_nameunit(ucom->sc_dev);
146 if (uaa->iface == NULL) {
147 /* Move the device into the configured state. */
148 error = usbd_set_config_index(dev, UARK_CONFIG_INDEX, 1);
150 printf("\n%s: failed to set configuration, err=%s\n",
151 devname, usbd_errstr(error));
155 usbd_device2interface_handle(dev, UARK_IFACE_INDEX, &iface);
157 printf("\n%s: failed to get interface, err=%s\n",
158 devname, usbd_errstr(error));
164 usbd_devinfo(dev, 0, devinfo);
165 printf("%s: %s\n", devname, devinfo);
167 id = usbd_get_interface_descriptor(iface);
168 ucom->sc_iface = iface;
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);
174 printf("%s: could not read endpoint descriptor\n",
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;
185 if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
186 printf("%s: missing endpoint\n", devname);
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;
195 ucom->sc_callback = &uark_callback;
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);
201 USB_ATTACH_SUCCESS_RETURN;
204 DPRINTF(("uark_attach: ATTACH ERROR\n"));
206 free(devinfo, M_USBDEV);
208 USB_ATTACH_ERROR_RETURN;
213 USB_DETACH_START(uark, sc);
216 DPRINTF(("uark_detach: sc=%p\n", sc));
217 sc->sc_ucom.sc_dying = 1;
218 rv = ucom_detach(&sc->sc_ucom);
224 uark_set(void *vsc, int portno, int reg, int onoff)
226 struct uark_softc *sc = vsc;
230 uark_break(sc, portno, onoff);
242 uark_param(void *vsc, int portno, struct termios *t)
244 struct uark_softc *sc = (struct uark_softc *)vsc;
247 switch (t->c_ospeed) {
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);
268 if (ISSET(t->c_cflag, CSTOPB))
269 data = UARK_STOP_BITS_2;
271 data = UARK_STOP_BITS_1;
273 if (ISSET(t->c_cflag, PARENB)) {
274 if (ISSET(t->c_cflag, PARODD))
275 data |= UARK_PARITY_ODD;
277 data |= UARK_PARITY_EVEN;
279 data |= UARK_PARITY_NONE;
281 switch (ISSET(t->c_cflag, CSIZE)) {
283 data |= UARK_SET_DATA_BITS(5);
286 data |= UARK_SET_DATA_BITS(6);
289 data |= UARK_SET_DATA_BITS(7);
292 data |= UARK_SET_DATA_BITS(8);
295 uark_cmd(sc, 3, 0x00);
296 uark_cmd(sc, 3, data);
302 uark_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
304 struct uark_softc *sc = vsc;
313 uark_break(void *vsc, int portno, int onoff)
316 struct uark_softc *sc = vsc;
318 printf("%s: break %s!\n", device_get_nameunit(sc->sc_dev),
319 onoff ? "on" : "off");
323 uark_cmd(sc, 4, 0x01);
325 uark_cmd(sc, 4, 0x00);
330 uark_cmd(struct uark_softc *sc, uint16_t index, uint16_t value)
332 usb_device_request_t req;
334 struct ucom_softc *ucom = &sc->sc_ucom;
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);
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),
358 static driver_t uark_driver = {
361 sizeof (struct uark_softc)
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);