xref: /freebsd/sys/dev/usb/misc/uled.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2014 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 
62 struct 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 
73 /* prototypes */
74 
75 static device_probe_t	uled_probe;
76 static device_attach_t	uled_attach;
77 static device_detach_t	uled_detach;
78 
79 static usb_fifo_open_t	uled_open;
80 static usb_fifo_close_t	uled_close;
81 static usb_fifo_ioctl_t	uled_ioctl;
82 
83 static struct usb_fifo_methods uled_fifo_methods = {
84 	.f_open = &uled_open,
85 	.f_close = &uled_close,
86 	.f_ioctl = &uled_ioctl,
87 	.basename[0] = "uled",
88 };
89 
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 *);
93 
94 static devclass_t uled_devclass;
95 
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),
100 
101 	DEVMETHOD_END
102 };
103 
104 static driver_t uled_driver = {
105 	.name = "uled",
106 	.methods = uled_methods,
107 	.size = sizeof(struct uled_softc),
108 };
109 
110 static const STRUCT_USB_HOST_ID uled_devs[] = {
111 	{USB_VPI(USB_VENDOR_DREAMLINK, USB_PRODUCT_DREAMLINK_DL100B, 0)},
112 };
113 
114 DRIVER_MODULE(uled, uhub, uled_driver, uled_devclass, NULL, NULL);
115 MODULE_DEPEND(uled, usb, 1, 1, 1);
116 MODULE_VERSION(uled, 1);
117 USB_PNP_HOST_INFO(uled_devs);
118 
119 static int
120 uled_probe(device_t dev)
121 {
122 	struct usb_attach_arg *uaa;
123 
124 	uaa = device_get_ivars(dev);
125 	if (uaa->usb_mode != USB_MODE_HOST)
126 		return (ENXIO);
127 	if (uaa->info.bInterfaceClass != UICLASS_HID)
128 		return (ENXIO);
129 
130 	return (usbd_lookup_id_by_uaa(uled_devs, sizeof(uled_devs), uaa));
131 }
132 
133 static int
134 uled_attach(device_t dev)
135 {
136 	struct usb_attach_arg *uaa;
137 	struct uled_softc *sc;
138 	int unit;
139 	usb_error_t error;
140 
141 	uaa = device_get_ivars(dev);
142 	sc = device_get_softc(dev);
143 	unit = device_get_unit(dev);
144 
145 	device_set_usb_desc(dev);
146 	mtx_init(&sc->sc_mtx, "uled lock", NULL, MTX_DEF | MTX_RECURSE);
147 
148 	sc->sc_udev = uaa->device;
149 
150 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
151 	    &uled_fifo_methods, &sc->sc_fifo, unit, -1,
152 	    uaa->info.bIfaceIndex, UID_ROOT, GID_OPERATOR, 0644);
153 	if (error != 0)
154 		goto detach;
155 
156 	sc->sc_color.red = 0;
157 	sc->sc_color.green = 0;
158 	sc->sc_color.blue = 0;
159 
160 	return (0);
161 
162 detach:
163 	uled_detach(dev);
164 	return (ENOMEM);
165 }
166 
167 static int
168 uled_detach(device_t dev)
169 {
170 	struct uled_softc *sc;
171 
172 	sc = device_get_softc(dev);
173 	usb_fifo_detach(&sc->sc_fifo);
174 	mtx_destroy(&sc->sc_mtx);
175 	return (0);
176 }
177 
178 static usb_error_t
179 uled_ctrl_msg(struct uled_softc *sc, uint8_t rt, uint8_t reqno,
180     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
181 {
182 	struct usb_device_request req;
183 
184 	req.bmRequestType = rt;
185 	req.bRequest = reqno;
186 	USETW(req.wValue, value);
187 	USETW(req.wIndex, index);
188 	USETW(req.wLength, buflen);
189 
190 	return (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, &req, buf,
191 	    0, NULL, 2000));
192 }
193 
194 static int
195 uled_enable(struct uled_softc *sc)
196 {
197 	static uint8_t cmdbuf[] = { 0x1f, 0x02, 0x00, 0x5f, 0x00, 0x00, 0x1a,
198 	    0x03 };
199 	int error;
200 
201 	sc->sc_state |= ULED_ENABLED;
202 	mtx_lock(&sc->sc_mtx);
203 	error = uled_ctrl_msg(sc, UT_WRITE_CLASS_INTERFACE, UR_SET_REPORT,
204 	    0x200, 0, cmdbuf, sizeof(cmdbuf));
205 	mtx_unlock(&sc->sc_mtx);
206 	return (error);
207 }
208 
209 static int
210 uled_open(struct usb_fifo *fifo, int fflags)
211 {
212 	if (fflags & FREAD) {
213 		struct uled_softc *sc;
214 		int rc;
215 
216 		sc = usb_fifo_softc(fifo);
217 		if (sc->sc_state & ULED_ENABLED)
218 			return (EBUSY);
219 		if ((rc = uled_enable(sc)) != 0)
220 			return (rc);
221 	}
222 	return (0);
223 }
224 
225 static void
226 uled_close(struct usb_fifo *fifo, int fflags)
227 {
228 	if (fflags & FREAD) {
229 		struct uled_softc *sc;
230 
231 		sc = usb_fifo_softc(fifo);
232 		sc->sc_state &= ~ULED_ENABLED;
233 	}
234 }
235 
236 static int
237 uled_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
238 {
239 	struct uled_softc *sc;
240 	struct uled_color color;
241 	int error;
242 
243 	sc = usb_fifo_softc(fifo);
244 	error = 0;
245 
246 	mtx_lock(&sc->sc_mtx);
247 
248 	switch(cmd) {
249 	case ULED_GET_COLOR:
250 		*(struct uled_color *)addr = sc->sc_color;
251 		break;
252 	case ULED_SET_COLOR:
253 		color = *(struct uled_color *)addr;
254 		uint8_t buf[8];
255 
256 		sc->sc_color.red = color.red;
257 		sc->sc_color.green = color.green;
258 		sc->sc_color.blue = color.blue;
259 
260 		buf[0] = color.red;
261 		buf[1] = color.green;
262 		buf[2] = color.blue;
263 		buf[3] = buf[4] = buf[5] = 0;
264 		buf[6] = 0x1a;
265 		buf[7] = 0x05;
266 		error = uled_ctrl_msg(sc, UT_WRITE_CLASS_INTERFACE,
267 		    UR_SET_REPORT, 0x200, 0, buf, sizeof(buf));
268 		break;
269 	default:
270 		error = ENOTTY;
271 		break;
272 	}
273 
274 	mtx_unlock(&sc->sc_mtx);
275 	return (error);
276 }
277