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