2 * Copyright (c) 2014 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
75 static device_probe_t uled_probe;
76 static device_attach_t uled_attach;
77 static device_detach_t uled_detach;
79 static usb_fifo_open_t uled_open;
80 static usb_fifo_close_t uled_close;
81 static usb_fifo_ioctl_t uled_ioctl;
83 static struct usb_fifo_methods uled_fifo_methods = {
85 .f_close = &uled_close,
86 .f_ioctl = &uled_ioctl,
87 .basename[0] = "uled",
90 static usb_error_t uled_ctrl_msg(struct uled_softc *, uint8_t, uint8_t,
91 uint16_t, uint16_t, void *buf, uint16_t);
92 static int uled_enable(struct uled_softc *);
94 static devclass_t uled_devclass;
96 static device_method_t uled_methods[] = {
97 DEVMETHOD(device_probe, uled_probe),
98 DEVMETHOD(device_attach, uled_attach),
99 DEVMETHOD(device_detach, uled_detach),
104 static driver_t uled_driver = {
106 .methods = uled_methods,
107 .size = sizeof(struct uled_softc),
110 DRIVER_MODULE(uled, uhub, uled_driver, uled_devclass, NULL, NULL);
111 MODULE_DEPEND(uled, usb, 1, 1, 1);
112 MODULE_VERSION(uled, 1);
114 static const STRUCT_USB_HOST_ID uled_devs[] = {
115 {USB_VPI(USB_VENDOR_DREAMLINK, USB_PRODUCT_DREAMLINK_DL100B, 0)},
119 uled_probe(device_t dev)
121 struct usb_attach_arg *uaa;
123 uaa = device_get_ivars(dev);
124 if (uaa->usb_mode != USB_MODE_HOST)
126 if (uaa->info.bInterfaceClass != UICLASS_HID)
129 return (usbd_lookup_id_by_uaa(uled_devs, sizeof(uled_devs), uaa));
133 uled_attach(device_t dev)
135 struct usb_attach_arg *uaa;
136 struct uled_softc *sc;
140 uaa = device_get_ivars(dev);
141 sc = device_get_softc(dev);
142 unit = device_get_unit(dev);
144 device_set_usb_desc(dev);
145 mtx_init(&sc->sc_mtx, "uled lock", NULL, MTX_DEF | MTX_RECURSE);
147 sc->sc_udev = uaa->device;
149 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
150 &uled_fifo_methods, &sc->sc_fifo, unit, -1,
151 uaa->info.bIfaceIndex, UID_ROOT, GID_OPERATOR, 0644);
155 sc->sc_color.red = 0;
156 sc->sc_color.green = 0;
157 sc->sc_color.blue = 0;
167 uled_detach(device_t dev)
169 struct uled_softc *sc;
171 sc = device_get_softc(dev);
172 usb_fifo_detach(&sc->sc_fifo);
173 mtx_destroy(&sc->sc_mtx);
178 uled_ctrl_msg(struct uled_softc *sc, uint8_t rt, uint8_t reqno,
179 uint16_t value, uint16_t index, void *buf, uint16_t buflen)
181 struct usb_device_request req;
183 req.bmRequestType = rt;
184 req.bRequest = reqno;
185 USETW(req.wValue, value);
186 USETW(req.wIndex, index);
187 USETW(req.wLength, buflen);
189 return (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, &req, buf,
194 uled_enable(struct uled_softc *sc)
196 static uint8_t cmdbuf[] = { 0x1f, 0x02, 0x00, 0x5f, 0x00, 0x00, 0x1a,
200 sc->sc_state |= ULED_ENABLED;
201 mtx_lock(&sc->sc_mtx);
202 error = uled_ctrl_msg(sc, UT_WRITE_CLASS_INTERFACE, UR_SET_REPORT,
203 0x200, 0, cmdbuf, sizeof(cmdbuf));
204 mtx_unlock(&sc->sc_mtx);
209 uled_open(struct usb_fifo *fifo, int fflags)
211 if (fflags & FREAD) {
212 struct uled_softc *sc;
215 sc = usb_fifo_softc(fifo);
216 if (sc->sc_state & ULED_ENABLED)
218 if ((rc = uled_enable(sc)) != 0)
225 uled_close(struct usb_fifo *fifo, int fflags)
227 if (fflags & FREAD) {
228 struct uled_softc *sc;
230 sc = usb_fifo_softc(fifo);
231 sc->sc_state &= ~ULED_ENABLED;
236 uled_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
238 struct uled_softc *sc;
239 struct uled_color color;
242 sc = usb_fifo_softc(fifo);
245 mtx_lock(&sc->sc_mtx);
249 *(struct uled_color *)addr = sc->sc_color;
252 color = *(struct uled_color *)addr;
255 sc->sc_color.red = color.red;
256 sc->sc_color.green = color.green;
257 sc->sc_color.blue = color.blue;
260 buf[1] = color.green;
262 buf[3] = buf[4] = buf[5] = 0;
265 error = uled_ctrl_msg(sc, UT_WRITE_CLASS_INTERFACE,
266 UR_SET_REPORT, 0x200, 0, buf, sizeof(buf));
273 mtx_unlock(&sc->sc_mtx);