2 * Copyright (c) 2014, 2017 Kevin Lo
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
50 #include <sys/fcntl.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbhid.h>
57 #define USB_DEBUG_VAR usb_debug
58 #include <dev/usb/usb_debug.h>
60 #include <dev/usb/uled_ioctl.h>
63 struct usb_fifo_sc sc_fifo;
66 struct usb_device *sc_udev;
67 struct uled_color sc_color;
70 #define ULED_ENABLED 0x01
73 #define ULED_FLAG_BLINK1 0x0001
76 /* Initial commands. */
77 static uint8_t blink1[] = { 0x1, 'v', 0, 0, 0, 0, 0, 0 };
78 static uint8_t dl100b[] = { 0x1f, 0x2, 0, 0x5f, 0, 0, 0x1a, 0x3 };
81 static device_probe_t uled_probe;
82 static device_attach_t uled_attach;
83 static device_detach_t uled_detach;
85 static usb_fifo_open_t uled_open;
86 static usb_fifo_close_t uled_close;
87 static usb_fifo_ioctl_t uled_ioctl;
89 static struct usb_fifo_methods uled_fifo_methods = {
91 .f_close = &uled_close,
92 .f_ioctl = &uled_ioctl,
93 .basename[0] = "uled",
96 static usb_error_t uled_ctrl_msg(struct uled_softc *, uint8_t, uint8_t,
97 uint16_t, uint16_t, void *, uint16_t);
98 static int uled_enable(struct uled_softc *);
100 static devclass_t uled_devclass;
102 static device_method_t uled_methods[] = {
103 DEVMETHOD(device_probe, uled_probe),
104 DEVMETHOD(device_attach, uled_attach),
105 DEVMETHOD(device_detach, uled_detach),
110 static driver_t uled_driver = {
112 .methods = uled_methods,
113 .size = sizeof(struct uled_softc),
116 static const STRUCT_USB_HOST_ID uled_devs[] = {
117 #define ULED_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
118 ULED_DEV(DREAMLINK, DL100B, 0),
119 ULED_DEV(THINGM, BLINK1, ULED_FLAG_BLINK1),
123 DRIVER_MODULE(uled, uhub, uled_driver, uled_devclass, NULL, NULL);
124 MODULE_DEPEND(uled, usb, 1, 1, 1);
125 MODULE_VERSION(uled, 1);
126 USB_PNP_HOST_INFO(uled_devs);
129 uled_probe(device_t dev)
131 struct usb_attach_arg *uaa;
133 uaa = device_get_ivars(dev);
134 if (uaa->usb_mode != USB_MODE_HOST)
136 if (uaa->info.bInterfaceClass != UICLASS_HID)
139 return (usbd_lookup_id_by_uaa(uled_devs, sizeof(uled_devs), uaa));
143 uled_attach(device_t dev)
145 struct usb_attach_arg *uaa;
146 struct uled_softc *sc;
150 uaa = device_get_ivars(dev);
151 sc = device_get_softc(dev);
152 unit = device_get_unit(dev);
153 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
155 device_set_usb_desc(dev);
156 mtx_init(&sc->sc_mtx, "uled lock", NULL, MTX_DEF | MTX_RECURSE);
158 sc->sc_udev = uaa->device;
160 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
161 &uled_fifo_methods, &sc->sc_fifo, unit, -1,
162 uaa->info.bIfaceIndex, UID_ROOT, GID_OPERATOR, 0644);
166 sc->sc_color.red = 0;
167 sc->sc_color.green = 0;
168 sc->sc_color.blue = 0;
178 uled_detach(device_t dev)
180 struct uled_softc *sc;
182 sc = device_get_softc(dev);
183 usb_fifo_detach(&sc->sc_fifo);
184 mtx_destroy(&sc->sc_mtx);
189 uled_ctrl_msg(struct uled_softc *sc, uint8_t rt, uint8_t reqno,
190 uint16_t value, uint16_t index, void *buf, uint16_t buflen)
192 struct usb_device_request req;
194 req.bmRequestType = rt;
195 req.bRequest = reqno;
196 USETW(req.wValue, value);
197 USETW(req.wIndex, index);
198 USETW(req.wLength, buflen);
200 return (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, &req, buf,
205 uled_enable(struct uled_softc *sc)
210 cmdbuf = (sc->sc_flags & ULED_FLAG_BLINK1) ? blink1 : dl100b;
212 sc->sc_state |= ULED_ENABLED;
213 mtx_lock(&sc->sc_mtx);
214 error = uled_ctrl_msg(sc, UT_WRITE_CLASS_INTERFACE, UR_SET_REPORT,
215 0x200, 0, cmdbuf, sizeof(cmdbuf));
216 mtx_unlock(&sc->sc_mtx);
221 uled_open(struct usb_fifo *fifo, int fflags)
223 if (fflags & FREAD) {
224 struct uled_softc *sc;
227 sc = usb_fifo_softc(fifo);
228 if (sc->sc_state & ULED_ENABLED)
230 if ((rc = uled_enable(sc)) != 0)
237 uled_close(struct usb_fifo *fifo, int fflags)
239 if (fflags & FREAD) {
240 struct uled_softc *sc;
242 sc = usb_fifo_softc(fifo);
243 sc->sc_state &= ~ULED_ENABLED;
248 uled_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
250 struct uled_softc *sc;
251 struct uled_color color;
254 sc = usb_fifo_softc(fifo);
257 mtx_lock(&sc->sc_mtx);
261 *(struct uled_color *)addr = sc->sc_color;
264 color = *(struct uled_color *)addr;
267 sc->sc_color.red = color.red;
268 sc->sc_color.green = color.green;
269 sc->sc_color.blue = color.blue;
271 if (sc->sc_flags & ULED_FLAG_BLINK1) {
275 buf[3] = color.green;
277 buf[5] = buf[6] = buf[7] = 0;
280 buf[1] = color.green;
282 buf[3] = buf[4] = buf[5] = 0;
286 error = uled_ctrl_msg(sc, UT_WRITE_CLASS_INTERFACE,
287 UR_SET_REPORT, 0x200, 0, buf, sizeof(buf));
294 mtx_unlock(&sc->sc_mtx);