1/*- 2 * Copyright (c) 2014, 2017 Kevin Lo 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD$"); 30 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> 38#include <sys/bus.h> 39#include <sys/module.h> 40#include <sys/lock.h> 41#include <sys/mutex.h> 42#include <sys/condvar.h> 43#include <sys/sysctl.h> 44#include <sys/sx.h> 45#include <sys/unistd.h> 46#include <sys/callout.h> 47#include <sys/malloc.h> 48#include <sys/priv.h> 49#include <sys/conf.h> 50#include <sys/fcntl.h> 51 52#include <dev/usb/usb.h> 53#include <dev/usb/usbdi.h> 54#include <dev/usb/usbhid.h> 55#include "usbdevs.h" 56 57#define USB_DEBUG_VAR usb_debug 58#include <dev/usb/usb_debug.h> 59 60#include <dev/usb/uled_ioctl.h> 61 62struct uled_softc { 63 struct usb_fifo_sc sc_fifo; 64 struct mtx sc_mtx; 65 66 struct usb_device *sc_udev; 67 struct uled_color sc_color; 68 69 uint8_t sc_state; 70#define ULED_ENABLED 0x01 71 72 int sc_flags; 73#define ULED_FLAG_BLINK1 0x0001 74}; 75 76/* Initial commands. */ 77static uint8_t blink1[] = { 0x1, 'v', 0, 0, 0, 0, 0, 0 }; 78static uint8_t dl100b[] = { 0x1f, 0x2, 0, 0x5f, 0, 0, 0x1a, 0x3 }; 79 80/* Prototypes. */ 81static device_probe_t uled_probe; 82static device_attach_t uled_attach; 83static device_detach_t uled_detach; 84 85static usb_fifo_open_t uled_open; 86static usb_fifo_close_t uled_close; 87static usb_fifo_ioctl_t uled_ioctl; 88 89static struct usb_fifo_methods uled_fifo_methods = { 90 .f_open = &uled_open, 91 .f_close = &uled_close, 92 .f_ioctl = &uled_ioctl, 93 .basename[0] = "uled", 94}; 95 96static usb_error_t uled_ctrl_msg(struct uled_softc *, uint8_t, uint8_t, 97 uint16_t, uint16_t, void *, uint16_t); 98static int uled_enable(struct uled_softc *); 99 100static devclass_t uled_devclass; 101 102static device_method_t uled_methods[] = { 103 DEVMETHOD(device_probe, uled_probe), 104 DEVMETHOD(device_attach, uled_attach), 105 DEVMETHOD(device_detach, uled_detach), 106 107 DEVMETHOD_END 108}; 109 110static driver_t uled_driver = { 111 .name = "uled", 112 .methods = uled_methods, 113 .size = sizeof(struct uled_softc), 114}; 115 116static 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), 120#undef ULED_DEV 121}; 122 123DRIVER_MODULE(uled, uhub, uled_driver, uled_devclass, NULL, NULL); 124MODULE_DEPEND(uled, usb, 1, 1, 1); 125MODULE_VERSION(uled, 1); 126USB_PNP_HOST_INFO(uled_devs); 127 128static int 129uled_probe(device_t dev) 130{ 131 struct usb_attach_arg *uaa; 132 133 uaa = device_get_ivars(dev); 134 if (uaa->usb_mode != USB_MODE_HOST) 135 return (ENXIO); 136 if (uaa->info.bInterfaceClass != UICLASS_HID) 137 return (ENXIO); 138 139 return (usbd_lookup_id_by_uaa(uled_devs, sizeof(uled_devs), uaa)); 140} 141 142static int 143uled_attach(device_t dev) 144{ 145 struct usb_attach_arg *uaa; 146 struct uled_softc *sc; 147 int unit; 148 usb_error_t error; 149 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); 154 155 device_set_usb_desc(dev); 156 mtx_init(&sc->sc_mtx, "uled lock", NULL, MTX_DEF | MTX_RECURSE); 157 158 sc->sc_udev = uaa->device; 159 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); 163 if (error != 0) 164 goto detach; 165 166 sc->sc_color.red = 0; 167 sc->sc_color.green = 0; 168 sc->sc_color.blue = 0; 169 170 return (0); 171 172detach: 173 uled_detach(dev); 174 return (ENOMEM); 175} 176 177static int 178uled_detach(device_t dev) 179{ 180 struct uled_softc *sc; 181 182 sc = device_get_softc(dev); 183 usb_fifo_detach(&sc->sc_fifo); 184 mtx_destroy(&sc->sc_mtx); 185 return (0); 186} 187 188static usb_error_t 189uled_ctrl_msg(struct uled_softc *sc, uint8_t rt, uint8_t reqno, 190 uint16_t value, uint16_t index, void *buf, uint16_t buflen) 191{ 192 struct usb_device_request req; 193 194 req.bmRequestType = rt; 195 req.bRequest = reqno; 196 USETW(req.wValue, value); 197 USETW(req.wIndex, index); 198 USETW(req.wLength, buflen); 199 200 return (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, &req, buf, 201 0, NULL, 2000)); 202} 203 204static int 205uled_enable(struct uled_softc *sc) 206{ 207 uint8_t *cmdbuf; 208 int error; 209 210 cmdbuf = (sc->sc_flags & ULED_FLAG_BLINK1) ? blink1 : dl100b; 211 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); 217 return (error); 218} 219 220static int 221uled_open(struct usb_fifo *fifo, int fflags) 222{ 223 if (fflags & FREAD) { 224 struct uled_softc *sc; 225 int rc; 226 227 sc = usb_fifo_softc(fifo); 228 if (sc->sc_state & ULED_ENABLED) 229 return (EBUSY); 230 if ((rc = uled_enable(sc)) != 0) 231 return (rc); 232 } 233 return (0); 234} 235 236static void 237uled_close(struct usb_fifo *fifo, int fflags) 238{ 239 if (fflags & FREAD) { 240 struct uled_softc *sc; 241 242 sc = usb_fifo_softc(fifo); 243 sc->sc_state &= ~ULED_ENABLED; 244 } 245} 246 247static int 248uled_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 249{ 250 struct uled_softc *sc; 251 struct uled_color color; 252 int error; 253 254 sc = usb_fifo_softc(fifo); 255 error = 0; 256 257 mtx_lock(&sc->sc_mtx); 258 259 switch(cmd) { 260 case ULED_GET_COLOR: 261 *(struct uled_color *)addr = sc->sc_color; 262 break; 263 case ULED_SET_COLOR: 264 color = *(struct uled_color *)addr; 265 uint8_t buf[8]; 266 267 sc->sc_color.red = color.red; 268 sc->sc_color.green = color.green; 269 sc->sc_color.blue = color.blue; 270 271 if (sc->sc_flags & ULED_FLAG_BLINK1) { 272 buf[0] = 0x1; 273 buf[1] = 'n'; 274 buf[2] = color.red; 275 buf[3] = color.green; 276 buf[4] = color.blue; 277 buf[5] = buf[6] = buf[7] = 0; 278 } else { 279 buf[0] = color.red; 280 buf[1] = color.green; 281 buf[2] = color.blue; 282 buf[3] = buf[4] = buf[5] = 0; 283 buf[6] = 0x1a; 284 buf[7] = 0x05; 285 } 286 error = uled_ctrl_msg(sc, UT_WRITE_CLASS_INTERFACE, 287 UR_SET_REPORT, 0x200, 0, buf, sizeof(buf)); 288 break; 289 default: 290 error = ENOTTY; 291 break; 292 } 293 294 mtx_unlock(&sc->sc_mtx); 295 return (error); 296} 297