source: mainline/uspace/drv/bus/usb/usbhid/blink1/blink1.c@ ad22fa4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ad22fa4 was 1e94e09, checked in by Martin Decky <martin@…>, 11 years ago

implement driver support for blink(1) USB LED devices
add simple LED device DDF interface

  • Property mode set to 100644
File size: 4.5 KB
Line 
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 <str_error.h>
38#include <usb/debug.h>
39#include <ops/led_dev.h>
40#include <usb/hid/request.h>
41#include "blink1.h"
42
43const char *HID_BLINK1_FUN_NAME = "blink1";
44const char *HID_BLINK1_CATEGORY = "led";
45
46#define BLINK1_REPORT_ID 0x0001
47
48#define BLINK1_COMMAND_SET 0x006e
49
50typedef struct {
51 uint8_t id;
52 uint8_t command;
53 uint8_t arg0;
54 uint8_t arg1;
55 uint8_t arg2;
56 uint8_t arg3;
57 uint8_t arg4;
58 uint8_t arg5;
59} blink1_report_t;
60
61static int usb_blink1_color_set(ddf_fun_t *fun, pixel_t pixel)
62{
63 usb_blink1_t *blink1_dev = (usb_blink1_t *) ddf_fun_data_get(fun);
64 if (blink1_dev == NULL) {
65 usb_log_debug("Missing parameters.\n");
66 return EINVAL;
67 }
68
69 blink1_report_t report;
70
71 report.id = BLINK1_REPORT_ID;
72 report.command = BLINK1_COMMAND_SET;
73 report.arg0 = RED(pixel);
74 report.arg1 = GREEN(pixel);
75 report.arg2 = BLUE(pixel);
76 report.arg3 = 0;
77 report.arg4 = 0;
78 report.arg5 = 0;
79
80 return usbhid_req_set_report(&blink1_dev->hid_dev->usb_dev->ctrl_pipe,
81 blink1_dev->hid_dev->usb_dev->interface_no, USB_HID_REPORT_TYPE_FEATURE,
82 (uint8_t *) &report, sizeof(report));
83}
84
85static led_dev_ops_t usb_blink1_iface = {
86 .color_set = usb_blink1_color_set
87};
88
89static ddf_dev_ops_t blink1_ops = {
90 .interfaces[LED_DEV_IFACE] = &usb_blink1_iface
91};
92
93int usb_blink1_init(usb_hid_dev_t *hid_dev, void **data)
94{
95 if (hid_dev == NULL) {
96 usb_log_error("Failed to init blink(1) structure: no structure "
97 "given.\n");
98 return EINVAL;
99 }
100
101 /* Create the exposed function. */
102 ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
103 HID_BLINK1_FUN_NAME);
104 if (fun == NULL) {
105 usb_log_error("Could not create DDF function node `%s'.\n",
106 HID_BLINK1_FUN_NAME);
107 return ENOMEM;
108 }
109
110 usb_blink1_t *blink1_dev = (usb_blink1_t *)
111 ddf_fun_data_alloc(fun, sizeof(usb_blink1_t));
112 if (blink1_dev == NULL) {
113 usb_log_error("Error while creating USB/HID blink(1) device "
114 "structure.\n");
115 ddf_fun_destroy(fun);
116 return ENOMEM;
117 }
118
119 ddf_fun_set_ops(fun, &blink1_ops);
120
121 int rc = ddf_fun_bind(fun);
122 if (rc != EOK) {
123 usb_log_error("Could not bind DDF function `%s': %s.\n",
124 ddf_fun_get_name(fun), str_error(rc));
125 ddf_fun_destroy(fun);
126 return rc;
127 }
128
129 rc = ddf_fun_add_to_category(fun, HID_BLINK1_CATEGORY);
130 if (rc != EOK) {
131 usb_log_error("Could not add DDF function to category %s: %s.\n",
132 HID_BLINK1_CATEGORY, str_error(rc));
133
134 rc = ddf_fun_unbind(fun);
135 if (rc != EOK) {
136 usb_log_error("Could not unbind function `%s', it "
137 "will not be destroyed.\n", ddf_fun_get_name(fun));
138 return rc;
139 }
140
141 ddf_fun_destroy(fun);
142 return rc;
143 }
144
145 blink1_dev->fun = fun;
146 blink1_dev->hid_dev = hid_dev;
147 *data = blink1_dev;
148
149 return EOK;
150}
151
152void usb_blink1_deinit(usb_hid_dev_t *hid_dev, void *data)
153{
154 if (data == NULL)
155 return;
156
157 usb_blink1_t *blink1_dev = (usb_blink1_t *) data;
158
159 int rc = ddf_fun_unbind(blink1_dev->fun);
160 if (rc != EOK) {
161 usb_log_error("Could not unbind function `%s', it "
162 "will not be destroyed.\n", ddf_fun_get_name(blink1_dev->fun));
163 return;
164 }
165
166 ddf_fun_destroy(blink1_dev->fun);
167}
168
169/**
170 * @}
171 */
Note: See TracBrowser for help on using the repository browser.