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