summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/debug_menu.c48
-rw-r--r--firmware/SOURCES1
-rw-r--r--firmware/export/usb.h1
-rw-r--r--firmware/export/usb_core.h1
-rw-r--r--firmware/usbstack/usb_core.c27
-rw-r--r--firmware/usbstack/usb_hid.c288
-rw-r--r--firmware/usbstack/usb_hid.h38
7 files changed, 391 insertions, 13 deletions
diff --git a/apps/debug_menu.c b/apps/debug_menu.c
index 2912129a1a..5fa666796b 100644
--- a/apps/debug_menu.c
+++ b/apps/debug_menu.c
@@ -2583,15 +2583,36 @@ static bool dbg_scrollwheel(void)
2583} 2583}
2584#endif 2584#endif
2585 2585
2586#if defined(HAVE_USBSTACK) && defined(ROCKBOX_HAS_LOGF) && defined(USB_SERIAL) 2586#if defined (HAVE_USBSTACK)
2587static bool logf_usb_serial(void) 2587static bool toggle_usb_core_driver(int driver, char *msg)
2588{ 2588{
2589 bool serial_enabled = !usb_core_driver_enabled(USB_DRIVER_SERIAL); 2589 bool enabled = !usb_core_driver_enabled(driver);
2590 usb_core_enable_driver(USB_DRIVER_SERIAL,serial_enabled); 2590
2591 splashf(HZ, "USB logf %s", 2591 usb_core_enable_driver(driver,enabled);
2592 serial_enabled?"enabled":"disabled"); 2592 splashf(HZ, "%s %s", msg, enabled?"enabled":"disabled");
2593
2593 return false; 2594 return false;
2594} 2595}
2596#if 0 && defined(USB_STORAGE)
2597static bool toggle_usb_mass_storage(void)
2598{
2599 return toggle_usb_core_driver(USB_DRIVER_MASS_STORAGE,"USB Mass Storage");
2600}
2601#endif
2602
2603#if defined(ROCKBOX_HAS_LOGF) && defined(USB_SERIAL)
2604static bool toggle_usb_serial(void)
2605{
2606 return toggle_usb_core_driver(USB_DRIVER_SERIAL,"USB Serial");
2607}
2608#endif
2609
2610#if defined(USB_HID)
2611static bool toggle_usb_hid(void)
2612{
2613 return toggle_usb_core_driver(USB_DRIVER_HID, "USB HID");
2614}
2615#endif
2595#endif 2616#endif
2596 2617
2597#if CONFIG_USBOTG == USBOTG_ISP1583 2618#if CONFIG_USBOTG == USBOTG_ISP1583
@@ -2728,17 +2749,22 @@ static const struct the_menu_item menuitems[] = {
2728 {"logf", logfdisplay }, 2749 {"logf", logfdisplay },
2729 {"logfdump", logfdump }, 2750 {"logfdump", logfdump },
2730#endif 2751#endif
2731#if defined(HAVE_USBSTACK) && defined(ROCKBOX_HAS_LOGF) && defined(USB_SERIAL) 2752#if defined(HAVE_USBSTACK)
2732 {"logf over usb",logf_usb_serial }, 2753#if 0 && defined(USB_STORAGE)
2754 {"USB Mass-Storage driver", toggle_usb_mass_storage },
2755#endif
2756#if defined(ROCKBOX_HAS_LOGF) && defined(USB_SERIAL)
2757 {"USB Serial driver (logf)", toggle_usb_serial },
2733#endif 2758#endif
2734#if 0 && defined(HAVE_USBSTACK) && defined(USB_STORAGE) 2759#if defined(USB_HID)
2735 {"reconnect usb storage",usb_reconnect}, 2760 {"USB HID driver", toggle_usb_hid },
2736#endif 2761#endif
2762#endif /* HAVE_USBSTACK */
2737#ifdef CPU_BOOST_LOGGING 2763#ifdef CPU_BOOST_LOGGING
2738 {"cpu_boost log",cpu_boost_log}, 2764 {"cpu_boost log",cpu_boost_log},
2739#endif 2765#endif
2740#if (defined(HAVE_WHEEL_ACCELERATION) && (CONFIG_KEYPAD==IPOD_4G_PAD) && !defined(SIMULATOR)) 2766#if (defined(HAVE_WHEEL_ACCELERATION) && (CONFIG_KEYPAD==IPOD_4G_PAD) && !defined(SIMULATOR))
2741 {"Debug scrollwheel", dbg_scrollwheel}, 2767 {"Debug scrollwheel", dbg_scrollwheel },
2742#endif 2768#endif
2743 }; 2769 };
2744static int menu_action_callback(int btn, struct gui_synclist *lists) 2770static int menu_action_callback(int btn, struct gui_synclist *lists)
diff --git a/firmware/SOURCES b/firmware/SOURCES
index adbcb08c95..d96dc8c8a9 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -260,6 +260,7 @@ usbstack/usb_core.c
260usbstack/usb_storage.c 260usbstack/usb_storage.c
261usbstack/usb_serial.c 261usbstack/usb_serial.c
262usbstack/usb_charging_only.c 262usbstack/usb_charging_only.c
263usbstack/usb_hid.c
263#if CONFIG_USBOTG == USBOTG_ARC 264#if CONFIG_USBOTG == USBOTG_ARC
264target/arm/usb-drv-arc.c 265target/arm/usb-drv-arc.c
265#elif CONFIG_USBOTG == USBOTG_ISP1583 266#elif CONFIG_USBOTG == USBOTG_ISP1583
diff --git a/firmware/export/usb.h b/firmware/export/usb.h
index 2b3452db25..e1bacfffc7 100644
--- a/firmware/export/usb.h
+++ b/firmware/export/usb.h
@@ -93,6 +93,7 @@ enum {
93 USB_DRIVER_MASS_STORAGE, 93 USB_DRIVER_MASS_STORAGE,
94 USB_DRIVER_SERIAL, 94 USB_DRIVER_SERIAL,
95 USB_DRIVER_CHARGING_ONLY, 95 USB_DRIVER_CHARGING_ONLY,
96 USB_DRIVER_HID,
96 USB_NUM_DRIVERS 97 USB_NUM_DRIVERS
97}; 98};
98 99
diff --git a/firmware/export/usb_core.h b/firmware/export/usb_core.h
index aac84756f5..abf090ed9d 100644
--- a/firmware/export/usb_core.h
+++ b/firmware/export/usb_core.h
@@ -26,6 +26,7 @@
26//#define USB_SERIAL 26//#define USB_SERIAL
27#define USB_STORAGE 27#define USB_STORAGE
28#define USB_CHARGING_ONLY 28#define USB_CHARGING_ONLY
29//#define USB_HID
29#else /* BOOTLOADER */ 30#else /* BOOTLOADER */
30#define USB_CHARGING_ONLY 31#define USB_CHARGING_ONLY
31#endif /* BOOTLOADER */ 32#endif /* BOOTLOADER */
diff --git a/firmware/usbstack/usb_core.c b/firmware/usbstack/usb_core.c
index 8c235723ad..838529b484 100644
--- a/firmware/usbstack/usb_core.c
+++ b/firmware/usbstack/usb_core.c
@@ -43,6 +43,10 @@
43#include "usb_charging_only.h" 43#include "usb_charging_only.h"
44#endif 44#endif
45 45
46#if defined(USB_HID)
47#include "usb_hid.h"
48#endif
49
46/* TODO: Move target-specific stuff somewhere else (serial number reading) */ 50/* TODO: Move target-specific stuff somewhere else (serial number reading) */
47 51
48#ifdef HAVE_AS3514 52#ifdef HAVE_AS3514
@@ -166,8 +170,8 @@ static enum { DEFAULT, ADDRESS, CONFIGURED } usb_state;
166 170
167static int usb_core_num_interfaces; 171static int usb_core_num_interfaces;
168 172
169typedef void (*completion_handler_t)(int ep,int dir, int status, int length); 173typedef void (*completion_handler_t)(int ep,int dir,int status,int length);
170typedef bool (*control_handler_t)(struct usb_ctrlrequest* req, unsigned char* dest); 174typedef bool (*control_handler_t)(struct usb_ctrlrequest* req,unsigned char* dest);
171 175
172static struct 176static struct
173{ 177{
@@ -234,6 +238,25 @@ static struct usb_class_driver drivers[USB_NUM_DRIVERS] =
234 .notify_hotswap = NULL, 238 .notify_hotswap = NULL,
235#endif 239#endif
236 }, 240 },
241#ifdef USB_HID
242 [USB_DRIVER_HID] = {
243 .enabled = false,
244 .needs_exclusive_storage = false,
245 .first_interface = 0,
246 .last_interface = 0,
247 .request_endpoints = usb_hid_request_endpoints,
248 .set_first_interface = usb_hid_set_first_interface,
249 .get_config_descriptor = usb_hid_get_config_descriptor,
250 .init_connection = usb_hid_init_connection,
251 .init = usb_hid_init,
252 .disconnect = usb_hid_disconnect,
253 .transfer_complete = usb_hid_transfer_complete,
254 .control_request = usb_hid_control_request,
255#ifdef HAVE_HOTSWAP
256 .notify_hotswap = NULL,
257#endif
258 },
259#endif
237#endif 260#endif
238}; 261};
239 262
diff --git a/firmware/usbstack/usb_hid.c b/firmware/usbstack/usb_hid.c
new file mode 100644
index 0000000000..c3cd5d9a04
--- /dev/null
+++ b/firmware/usbstack/usb_hid.c
@@ -0,0 +1,288 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2009 by Tomer Shalev
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "string.h"
22#include "system.h"
23#include "usb_core.h"
24#include "usb_drv.h"
25#include "kernel.h"
26#include "usb_hid.h"
27#include "usb_class_driver.h"
28#define LOGF_ENABLE
29#include "logf.h"
30
31#ifdef USB_HID
32
33#define CONCAT(low, high) ((high << 8) | low)
34#define SIZE_VALUE 0x01
35/* HID main items (HID1_11.pdf, page 38) */
36#define INPUT (0x80 | SIZE_VALUE)
37#define OUTPUT (0x90 | SIZE_VALUE)
38#define FEATURE (0xb0 | SIZE_VALUE)
39#define COLLECTION (0xa0 | SIZE_VALUE)
40#define COLLECTION_APPLICATION CONCAT(COLLECTION, 0x01)
41#define END_COLLECTION 0xc0
42/* HID global items (HID1_11.pdf, page 45) */
43#define USAGE_PAGE (0x04 | SIZE_VALUE)
44#define LOGICAL_MINIMUM (0x14 | SIZE_VALUE)
45#define LOGICAL_MAXIMUM (0x24 | SIZE_VALUE)
46#define PHYSICAL_MINIMUM (0x34 | SIZE_VALUE)
47#define PHYSICAL_MAXIMUM (0x44 | SIZE_VALUE)
48#define UNIT_EXPONENT (0x54 | SIZE_VALUE)
49#define UNIT (0x64 | SIZE_VALUE)
50#define REPORT_SIZE (0x74 | SIZE_VALUE)
51#define REPORT_ID (0x84 | SIZE_VALUE)
52#define REPORT_COUNT (0x94 | SIZE_VALUE)
53#define PUSH (0xa4 | SIZE_VALUE)
54#define POP (0xb4 | SIZE_VALUE)
55/* Hut1_12.pdf, Table 1, page 14 */
56#define USAGE_PAGE_CONSUMER CONCAT(USAGE_PAGE, 0x0c)
57/* Hut1_12.pdf, Table 17, page 77 */
58#define CONSUMER_USAGE 0x09
59#define CONSUMER_USAGE_CONTROL CONCAT(CONSUMER_USAGE, 0x01)
60#define CONSUMER_USAGE_MUTE CONCAT(CONSUMER_USAGE, 0xe2)
61#define CONSUMER_USAGE_VOLUME_INCREMENT CONCAT(CONSUMER_USAGE, 0xe9)
62#define CONSUMER_USAGE_VOLUME_DECREMENT CONCAT(CONSUMER_USAGE, 0xea)
63/* Hut1_12.pdf, Table 4, page 20 */
64#define CONSUMER_CONTROL CONCAT(COLLECTION_APPLICATION, 0x01)
65
66#define USB_DT_HID 0x21
67#define USB_DT_REPORT 0x22
68#define USB_DT_PHYSICAL_DESCRIPTOR 0x23
69
70/* serial interface */
71static struct usb_interface_descriptor __attribute__((aligned(2)))
72 interface_descriptor =
73{
74 .bLength = sizeof(struct usb_interface_descriptor),
75 .bDescriptorType = USB_DT_INTERFACE,
76 .bInterfaceNumber = 0,
77 .bAlternateSetting = 0,
78 .bNumEndpoints = 1,
79 .bInterfaceClass = USB_CLASS_HID,
80 .bInterfaceSubClass = 0,
81 .bInterfaceProtocol = 0,
82 .iInterface = 0
83};
84
85/* USB_DT_HID: Endpoint descriptor */
86struct usb_hid_descriptor {
87 uint8_t bLength;
88 uint8_t bDescriptorType;
89 uint16_t wBcdHID;
90 uint8_t bCountryCode;
91 uint8_t bNumDescriptors;
92 uint8_t bDescriptorType0;
93 uint16_t wDescriptorLength0;
94} __attribute__ ((packed));
95
96/* USB_DT_REPORT: Endpoint descriptor */
97static struct usb_hid_descriptor __attribute__((aligned(2))) hid_descriptor =
98{
99 .bLength = sizeof(struct usb_hid_descriptor),
100 .bDescriptorType = USB_DT_HID,
101 .wBcdHID = 0x0100,
102 .bCountryCode = 0,
103 .bNumDescriptors = 1,
104 .bDescriptorType0 = 0x22,
105 .wDescriptorLength0 = 0
106};
107
108static struct usb_endpoint_descriptor __attribute__((aligned(2))) endpoint_descriptor =
109{
110 .bLength = sizeof(struct usb_endpoint_descriptor),
111 .bDescriptorType = USB_DT_ENDPOINT,
112 .bEndpointAddress = 0,
113 .bmAttributes = USB_ENDPOINT_XFER_INT,
114 .wMaxPacketSize = 0,
115 .bInterval = 0
116};
117
118/* USB_DT_REPORT: Endpoint descriptor */
119struct usb_report_descriptor {
120 uint16_t wUsagePage;
121 uint16_t wUsage;
122 uint16_t wCollection;
123 uint16_t wCollectionItems[12];
124 uint8_t wEndCollection;
125} __attribute__ ((packed));
126
127static struct usb_report_descriptor __attribute__((aligned(2))) report_descriptor =
128{
129 .wUsagePage = USAGE_PAGE_CONSUMER,
130 .wUsage = CONSUMER_USAGE_CONTROL,
131 .wCollection = COLLECTION_APPLICATION,
132 .wCollectionItems = {
133 CONCAT(LOGICAL_MINIMUM, 0x0),
134 CONCAT(LOGICAL_MAXIMUM, 0x1),
135 USAGE_PAGE_CONSUMER,
136 CONSUMER_USAGE_MUTE,
137 CONSUMER_USAGE_VOLUME_INCREMENT,
138 CONSUMER_USAGE_VOLUME_DECREMENT,
139 CONCAT(REPORT_COUNT, 0x3),
140 CONCAT(REPORT_SIZE, 0x1),
141 CONCAT(INPUT, 0x42),
142 CONCAT(REPORT_COUNT, 0x5),
143 CONCAT(REPORT_SIZE, 0x1),
144 CONCAT(INPUT, 0x01)
145 },
146 .wEndCollection = END_COLLECTION
147};
148
149static int ep_in;
150static int usb_interface;
151
152int usb_hid_request_endpoints(struct usb_class_driver *drv)
153{
154 ep_in = usb_core_request_endpoint(USB_DIR_IN, drv);
155 if (ep_in < 0)
156 return -1;
157
158 return 0;
159}
160
161int usb_hid_set_first_interface(int interface)
162{
163 usb_interface = interface;
164
165 return interface + 1;
166}
167
168
169int usb_hid_get_config_descriptor(unsigned char *dest,int max_packet_size)
170{
171 unsigned char *orig_dest = dest;
172
173 logf("hid: config desc.");
174 interface_descriptor.bInterfaceNumber = usb_interface;
175 PACK_DESCRIPTOR(dest, interface_descriptor);
176
177 hid_descriptor.wDescriptorLength0 = sizeof(report_descriptor);
178 PACK_DESCRIPTOR(dest, hid_descriptor);
179
180 /* Ignore max_packet_size and set to 1 bytes long packet size */
181 (void)max_packet_size;
182 endpoint_descriptor.wMaxPacketSize = 1;
183 endpoint_descriptor.bInterval = 8;
184
185 endpoint_descriptor.bEndpointAddress = ep_in;
186 PACK_DESCRIPTOR(dest, endpoint_descriptor);
187
188 return (dest - orig_dest);
189}
190
191void usb_hid_init_connection(void)
192{
193 logf("hid: init connection");
194}
195
196/* called by usb_code_init() */
197void usb_hid_init(void)
198{
199 logf("hid: init");
200}
201
202void usb_hid_disconnect(void)
203{
204 logf("hid: disconnect");
205}
206
207/* called by usb_core_transfer_complete() */
208void usb_hid_transfer_complete(int ep,int dir, int status, int length)
209{
210 (void)ep;
211 (void)dir;
212 (void)status;
213 (void)length;
214
215 logf("hid: transfer complete. ep %d, dir %d, status %d ,length %d",
216 ep, dir, status, length);
217}
218
219/* HID-only class specific requests */
220#define USB_HID_GET_REPORT 0x01
221#define USB_HID_GET_IDLE 0x02
222#define USB_HID_GET_PROTOCOL 0x03
223#define USB_HID_SET_REPORT 0x09
224#define USB_HID_SET_IDLE 0x0a
225#define USB_HID_SET_PROTOCOL 0x0b
226
227/* called by usb_core_control_request() */
228bool usb_hid_control_request(struct usb_ctrlrequest* req, unsigned char* dest)
229{
230 bool handled = false;
231
232 switch(req->bRequestType & USB_TYPE_MASK) {
233 case USB_TYPE_STANDARD: {
234 switch(req->wValue>>8) { /* type */
235 case USB_DT_REPORT: {
236 logf("hid: report");
237 if (dest == NULL) {
238 logf("dest is NULL!");
239 }
240 if (dest) {
241 unsigned char *orig_dest = dest;
242 PACK_DESCRIPTOR(dest, report_descriptor);
243 if(usb_drv_send(EP_CONTROL, orig_dest, dest - orig_dest))
244 break;
245 usb_core_ack_control(req);
246
247 }
248 handled = true;
249 break;
250 }
251 default:
252 logf("hid: unsup. std. req");
253 break;
254 }
255 break;
256 }
257
258 case USB_TYPE_CLASS: {
259 switch (req->bRequest) {
260 case USB_HID_SET_IDLE:
261 logf("hid: set idle");
262 usb_core_ack_control(req);
263 handled = true;
264 break;
265 default:
266 //logf("hid: unsup. cls. req");
267 logf("%d: unsup. cls. req", req->bRequest);
268 break;
269 }
270 break;
271 }
272
273 case USB_TYPE_VENDOR:
274 logf("hid: unsup. ven. req");
275 break;
276 }
277 return handled;
278}
279
280void usb_hid_send(unsigned char *data, int length)
281{
282 (void)data;
283 (void)(length);
284
285 logf("hid: send %d bytes: \"%s\"", length, data);
286}
287
288#endif /*USB_HID*/
diff --git a/firmware/usbstack/usb_hid.h b/firmware/usbstack/usb_hid.h
new file mode 100644
index 0000000000..ff2a33dcbf
--- /dev/null
+++ b/firmware/usbstack/usb_hid.h
@@ -0,0 +1,38 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2009 by Tomer Shalev
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef USB_HID_H
22#define USB_HID_H
23
24#include "usb_ch9.h"
25
26int usb_hid_request_endpoints(struct usb_class_driver *drv);
27int usb_hid_set_first_interface(int interface);
28int usb_hid_get_config_descriptor(unsigned char *dest, int max_packet_size);
29void usb_hid_init_connection(void);
30void usb_hid_init(void);
31void usb_hid_disconnect(void);
32void usb_hid_transfer_complete(int ep, int dir, int status, int length);
33bool usb_hid_control_request(struct usb_ctrlrequest* req, unsigned char* dest);
34
35void usb_hid_send(unsigned char *data,int length);
36
37#endif
38