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