1 | /*
|
---|
2 | * Copyright (c) 2014 Martin Decky
|
---|
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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup drvusbhid
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * USB blink(1) subdriver.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <str_error.h>
|
---|
39 | #include <usb/debug.h>
|
---|
40 | #include <ops/led_dev.h>
|
---|
41 | #include <usb/hid/request.h>
|
---|
42 | #include "blink1.h"
|
---|
43 |
|
---|
44 | const char *HID_BLINK1_FUN_NAME = "blink1";
|
---|
45 | const char *HID_BLINK1_CATEGORY = "led";
|
---|
46 |
|
---|
47 | #define BLINK1_REPORT_ID 0x0001
|
---|
48 |
|
---|
49 | #define BLINK1_COMMAND_SET 0x006e
|
---|
50 |
|
---|
51 | typedef struct {
|
---|
52 | uint8_t id;
|
---|
53 | uint8_t command;
|
---|
54 | uint8_t arg0;
|
---|
55 | uint8_t arg1;
|
---|
56 | uint8_t arg2;
|
---|
57 | uint8_t arg3;
|
---|
58 | uint8_t arg4;
|
---|
59 | uint8_t arg5;
|
---|
60 | } blink1_report_t;
|
---|
61 |
|
---|
62 | static int usb_blink1_color_set(ddf_fun_t *fun, pixel_t pixel)
|
---|
63 | {
|
---|
64 | usb_blink1_t *blink1_dev = (usb_blink1_t *) ddf_fun_data_get(fun);
|
---|
65 | if (blink1_dev == NULL) {
|
---|
66 | usb_log_debug("Missing parameters.\n");
|
---|
67 | return EINVAL;
|
---|
68 | }
|
---|
69 |
|
---|
70 | blink1_report_t report;
|
---|
71 |
|
---|
72 | report.id = BLINK1_REPORT_ID;
|
---|
73 | report.command = BLINK1_COMMAND_SET;
|
---|
74 | report.arg0 = RED(pixel);
|
---|
75 | report.arg1 = GREEN(pixel);
|
---|
76 | report.arg2 = BLUE(pixel);
|
---|
77 | report.arg3 = 0;
|
---|
78 | report.arg4 = 0;
|
---|
79 | report.arg5 = 0;
|
---|
80 |
|
---|
81 | return usbhid_req_set_report(
|
---|
82 | usb_device_get_default_pipe(blink1_dev->hid_dev->usb_dev),
|
---|
83 | usb_device_get_iface_number(blink1_dev->hid_dev->usb_dev),
|
---|
84 | USB_HID_REPORT_TYPE_FEATURE, (uint8_t *) &report, sizeof(report));
|
---|
85 | }
|
---|
86 |
|
---|
87 | static led_dev_ops_t usb_blink1_iface = {
|
---|
88 | .color_set = usb_blink1_color_set
|
---|
89 | };
|
---|
90 |
|
---|
91 | static ddf_dev_ops_t blink1_ops = {
|
---|
92 | .interfaces[LED_DEV_IFACE] = &usb_blink1_iface
|
---|
93 | };
|
---|
94 |
|
---|
95 | int usb_blink1_init(usb_hid_dev_t *hid_dev, void **data)
|
---|
96 | {
|
---|
97 | if (hid_dev == NULL) {
|
---|
98 | usb_log_error("Failed to init blink(1) structure: no structure "
|
---|
99 | "given.\n");
|
---|
100 | return EINVAL;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* Create the exposed function. */
|
---|
104 | ddf_fun_t *fun = usb_device_ddf_fun_create(hid_dev->usb_dev,
|
---|
105 | fun_exposed, HID_BLINK1_FUN_NAME);
|
---|
106 | if (fun == NULL) {
|
---|
107 | usb_log_error("Could not create DDF function node `%s'.\n",
|
---|
108 | HID_BLINK1_FUN_NAME);
|
---|
109 | return ENOMEM;
|
---|
110 | }
|
---|
111 |
|
---|
112 | usb_blink1_t *blink1_dev = (usb_blink1_t *)
|
---|
113 | ddf_fun_data_alloc(fun, sizeof(usb_blink1_t));
|
---|
114 | if (blink1_dev == NULL) {
|
---|
115 | usb_log_error("Error while creating USB/HID blink(1) device "
|
---|
116 | "structure.\n");
|
---|
117 | ddf_fun_destroy(fun);
|
---|
118 | return ENOMEM;
|
---|
119 | }
|
---|
120 |
|
---|
121 | ddf_fun_set_ops(fun, &blink1_ops);
|
---|
122 |
|
---|
123 | int rc = ddf_fun_bind(fun);
|
---|
124 | if (rc != EOK) {
|
---|
125 | usb_log_error("Could not bind DDF function `%s': %s.\n",
|
---|
126 | ddf_fun_get_name(fun), str_error(rc));
|
---|
127 | ddf_fun_destroy(fun);
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 | rc = ddf_fun_add_to_category(fun, HID_BLINK1_CATEGORY);
|
---|
132 | if (rc != EOK) {
|
---|
133 | usb_log_error("Could not add DDF function to category %s: %s.\n",
|
---|
134 | HID_BLINK1_CATEGORY, str_error(rc));
|
---|
135 |
|
---|
136 | rc = ddf_fun_unbind(fun);
|
---|
137 | if (rc != EOK) {
|
---|
138 | usb_log_error("Could not unbind function `%s', it "
|
---|
139 | "will not be destroyed.\n", ddf_fun_get_name(fun));
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 | ddf_fun_destroy(fun);
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 | blink1_dev->fun = fun;
|
---|
148 | blink1_dev->hid_dev = hid_dev;
|
---|
149 | *data = blink1_dev;
|
---|
150 |
|
---|
151 | return EOK;
|
---|
152 | }
|
---|
153 |
|
---|
154 | void usb_blink1_deinit(usb_hid_dev_t *hid_dev, void *data)
|
---|
155 | {
|
---|
156 | if (data == NULL)
|
---|
157 | return;
|
---|
158 |
|
---|
159 | usb_blink1_t *blink1_dev = (usb_blink1_t *) data;
|
---|
160 |
|
---|
161 | int rc = ddf_fun_unbind(blink1_dev->fun);
|
---|
162 | if (rc != EOK) {
|
---|
163 | usb_log_error("Could not unbind function `%s', it "
|
---|
164 | "will not be destroyed.\n", ddf_fun_get_name(blink1_dev->fun));
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | ddf_fun_destroy(blink1_dev->fun);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * @}
|
---|
173 | */
|
---|