FreeBSD kernel usb device Code
uled.c
Go to the documentation of this file.
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 {
64 struct mtx sc_mtx;
65
68
69 uint8_t sc_state;
70#define ULED_ENABLED 0x01
71
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
88
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
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);
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;
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
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:
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);
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
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);
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 }
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}
struct @109 error
uint32_t value
u_int index
device_t dev
uint64_t * addr
uint8_t green
Definition: uled_ioctl.h:36
uint8_t red
Definition: uled_ioctl.h:35
uint8_t blue
Definition: uled_ioctl.h:37
int sc_flags
Definition: uled.c:72
struct mtx sc_mtx
Definition: uled.c:64
struct usb_fifo_sc sc_fifo
Definition: uled.c:63
struct uled_color sc_color
Definition: uled.c:67
struct usb_device * sc_udev
Definition: uled.c:66
uint8_t sc_state
Definition: uled.c:69
enum usb_hc_mode usb_mode
Definition: usbdi.h:432
struct usbd_lookup_info info
Definition: usbdi.h:426
struct usb_device * device
Definition: usbdi.h:430
usb_fifo_open_t * f_open
Definition: usbdi.h:535
uint8_t bIfaceIndex
Definition: usbdi.h:417
uint8_t bInterfaceClass
Definition: usbdi.h:414
static device_attach_t uled_attach
Definition: uled.c:82
MODULE_DEPEND(uled, usb, 1, 1, 1)
static int uled_enable(struct uled_softc *)
Definition: uled.c:205
#define ULED_FLAG_BLINK1
Definition: uled.c:73
static device_method_t uled_methods[]
Definition: uled.c:102
static device_probe_t uled_probe
Definition: uled.c:81
DRIVER_MODULE(uled, uhub, uled_driver, uled_devclass, NULL, NULL)
static uint8_t blink1[]
Definition: uled.c:77
USB_PNP_HOST_INFO(uled_devs)
static usb_fifo_open_t uled_open
Definition: uled.c:85
#define ULED_ENABLED
Definition: uled.c:70
__FBSDID("$FreeBSD$")
static usb_fifo_close_t uled_close
Definition: uled.c:86
static usb_fifo_ioctl_t uled_ioctl
Definition: uled.c:87
static devclass_t uled_devclass
Definition: uled.c:100
MODULE_VERSION(uled, 1)
static struct usb_fifo_methods uled_fifo_methods
Definition: uled.c:89
static driver_t uled_driver
Definition: uled.c:110
static device_detach_t uled_detach
Definition: uled.c:83
#define ULED_DEV(v, p, i)
static usb_error_t uled_ctrl_msg(struct uled_softc *, uint8_t, uint8_t, uint16_t, uint16_t, void *, uint16_t)
Definition: uled.c:189
static const STRUCT_USB_HOST_ID uled_devs[]
Definition: uled.c:116
static uint8_t dl100b[]
Definition: uled.c:78
#define ULED_GET_COLOR
Definition: uled_ioctl.h:40
#define ULED_SET_COLOR
Definition: uled_ioctl.h:41
#define UICLASS_HID
Definition: usb.h:453
@ USB_MODE_HOST
Definition: usb.h:778
#define UT_WRITE_CLASS_INTERFACE
Definition: usb.h:177
#define USETW(w, v)
Definition: usb_endian.h:77
const void * req
Definition: usb_if.m:51
INTERFACE usb
Definition: usb_if.m:35
int usbd_lookup_id_by_uaa(const struct usb_device_id *id, usb_size_t sizeof_id, struct usb_attach_arg *uaa)
Definition: usb_lookup.c:143
usb_error_t usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx, struct usb_device_request *req, void *data, uint16_t flags, uint16_t *actlen, usb_timeout_t timeout)
Definition: usb_request.c:405
void device_set_usb_desc(device_t dev)
Definition: usb_util.c:73
int() usb_fifo_ioctl_t(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
Definition: usbdi.h:101
void * usb_fifo_softc(struct usb_fifo *fifo)
void() usb_fifo_close_t(struct usb_fifo *fifo, int fflags)
Definition: usbdi.h:100
void usb_fifo_detach(struct usb_fifo_sc *f_sc)
int usb_fifo_attach(struct usb_device *udev, void *priv_sc, struct mtx *priv_mtx, struct usb_fifo_methods *pm, struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit, uint8_t iface_index, uid_t uid, gid_t gid, int mode)
usb_error_t
Definition: usbdi.h:45
int() usb_fifo_open_t(struct usb_fifo *fifo, int fflags)
Definition: usbdi.h:99
#define STRUCT_USB_HOST_ID
Definition: usbdi.h:258
#define USB_GET_DRIVER_INFO(did)
Definition: usbdi.h:400
#define UR_SET_REPORT
Definition: usbhid.h:46