| 
									
										
										
										
											2013-05-25 16:03:40 +07:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*!
 | 
					
						
							| 
									
										
										
										
											2013-05-28 15:24:27 +07:00
										 |  |  |     @file     hid_device.c | 
					
						
							| 
									
										
										
										
											2013-05-25 16:03:40 +07:00
										 |  |  |     @author   hathach (tinyusb.org) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @section LICENSE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Software License Agreement (BSD License) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Copyright (c) 2013, hathach (tinyusb.org) | 
					
						
							|  |  |  |     All rights reserved. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |     modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |     1. Redistributions of source code must retain the above copyright | 
					
						
							|  |  |  |     notice, this list of conditions and the following disclaimer. | 
					
						
							|  |  |  |     2. Redistributions in binary form must reproduce the above copyright | 
					
						
							|  |  |  |     notice, this list of conditions and the following disclaimer in the | 
					
						
							|  |  |  |     documentation and/or other materials provided with the distribution. | 
					
						
							|  |  |  |     3. Neither the name of the copyright holders nor the | 
					
						
							|  |  |  |     names of its contributors may be used to endorse or promote products | 
					
						
							|  |  |  |     derived from this software without specific prior written permission. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY | 
					
						
							|  |  |  |     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 
					
						
							|  |  |  |     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY | 
					
						
							|  |  |  |     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 
					
						
							|  |  |  |     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
					
						
							|  |  |  |     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 
					
						
							|  |  |  |     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
					
						
							|  |  |  |     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
					
						
							|  |  |  |     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This file is part of the tinyusb stack. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "tusb_option.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  | #if (MODE_DEVICE_SUPPORTED && DEVICE_CLASS_HID)
 | 
					
						
							| 
									
										
										
										
											2013-05-25 16:03:40 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define _TINY_USB_SOURCE_FILE_
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // INCLUDE
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-05-28 15:24:27 +07:00
										 |  |  | #include "common/common.h"
 | 
					
						
							|  |  |  | #include "hid_device.h"
 | 
					
						
							| 
									
										
										
										
											2013-06-01 21:54:08 +07:00
										 |  |  | #include "tusb_descriptors.h"
 | 
					
						
							| 
									
										
										
										
											2013-05-25 16:03:40 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 18:22:40 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MACRO CONSTANT TYPEDEF
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | enum { | 
					
						
							|  |  |  |   HIDD_NUMBER_OF_SUBCLASS = 3 | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-11-13 14:00:39 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 18:22:40 +07:00
										 |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  |   uint16_t report_length; | 
					
						
							| 
									
										
										
										
											2013-10-29 11:27:25 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   endpoint_handle_t ept_handle; | 
					
						
							|  |  |  |   uint8_t interface_number; | 
					
						
							| 
									
										
										
										
											2013-06-14 18:22:40 +07:00
										 |  |  | }hidd_interface_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |   uint8_t const * const p_report_desc; | 
					
						
							|  |  |  |   hidd_interface_t * const p_interface; | 
					
						
							|  |  |  |   void (* const mounted_cb) (uint8_t coreid); | 
					
						
							|  |  |  |   void (* const unmounted_cb) (uint8_t coreid); | 
					
						
							|  |  |  |   void (* const xfer_cb) (uint8_t, tusb_event_t, uint32_t); | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |   uint16_t (* const get_report_cb) (uint8_t, hid_request_report_type_t, void**, uint16_t ); | 
					
						
							|  |  |  |   void (* const set_report_cb) (uint8_t, hid_request_report_type_t, uint8_t[], uint16_t); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  | }hidd_class_driver_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern ATTR_WEAK hidd_interface_t keyboardd_data; | 
					
						
							|  |  |  | extern ATTR_WEAK hidd_interface_t moused_data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static hidd_class_driver_t const hidd_class_driver[HIDD_NUMBER_OF_SUBCLASS] = | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | //    [HID_PROTOCOL_NONE]  = for HID Generic
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if TUSB_CFG_DEVICE_HID_KEYBOARD
 | 
					
						
							|  |  |  |     [HID_PROTOCOL_KEYBOARD] = | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         .p_report_desc = app_tusb_keyboard_desc_report, | 
					
						
							|  |  |  |         .p_interface   = &keyboardd_data, | 
					
						
							|  |  |  |         .mounted_cb    = tusbd_hid_keyboard_mounted_cb, | 
					
						
							|  |  |  |         .unmounted_cb  = tusbd_hid_keyboard_unmounted_cb, | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |         .xfer_cb       = tusbd_hid_keyboard_cb, | 
					
						
							|  |  |  |         .get_report_cb = tusbd_hid_keyboard_get_report_cb, | 
					
						
							|  |  |  |         .set_report_cb = tusbd_hid_keyboard_set_report_cb | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |     }, | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if TUSB_CFG_DEVICE_HID_MOUSE
 | 
					
						
							|  |  |  |     [HID_PROTOCOL_MOUSE] = | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         .p_report_desc = app_tusb_mouse_desc_report, | 
					
						
							|  |  |  |         .p_interface   = &moused_data, | 
					
						
							|  |  |  |         .mounted_cb    = tusbd_hid_mouse_mounted_cb, | 
					
						
							|  |  |  |         .unmounted_cb  = tusbd_hid_mouse_unmounted_cb, | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |         .xfer_cb       = tusbd_hid_mouse_cb, | 
					
						
							|  |  |  |         .get_report_cb = tusbd_hid_mouse_get_report_cb, | 
					
						
							|  |  |  |         .set_report_cb = tusbd_hid_mouse_set_report_cb | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  | #if TUSB_CFG_DEVICE_HID_KEYBOARD || TUSB_CFG_DEVICE_HID_MOUSE
 | 
					
						
							|  |  |  | ATTR_USB_MIN_ALIGNMENT uint8_t m_control_data[ MAX_OF(sizeof(hid_keyboard_report_t), sizeof(hid_mouse_report_t)) ] TUSB_CFG_ATTR_USBRAM; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // KEYBOARD APPLICATION API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-06-14 18:22:40 +07:00
										 |  |  | #if TUSB_CFG_DEVICE_HID_KEYBOARD
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  | STATIC_VAR TUSB_CFG_ATTR_USBRAM hidd_interface_t keyboardd_data; | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool tusbd_hid_keyboard_is_busy(uint8_t coreid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return dcd_pipe_is_busy(keyboardd_data.ept_handle); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tusb_error_t tusbd_hid_keyboard_send(uint8_t coreid, hid_keyboard_report_t const *p_report) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-21 13:54:29 +07:00
										 |  |  |   ASSERT(tusbd_is_configured(coreid), TUSB_ERROR_USBD_DEVICE_NOT_CONFIGURED); | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   hidd_interface_t * p_kbd = &keyboardd_data; // TODO &keyboardd_data[coreid];
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-01 13:53:36 +07:00
										 |  |  |   ASSERT_STATUS( dcd_pipe_xfer(p_kbd->ept_handle, p_report, sizeof(hid_keyboard_report_t), true) ) ; | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-14 18:22:40 +07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MOUSE APPLICATION API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | #if TUSB_CFG_DEVICE_HID_MOUSE
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  | STATIC_VAR TUSB_CFG_ATTR_USBRAM hidd_interface_t moused_data; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | bool tusbd_hid_mouse_is_busy(uint8_t coreid) | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  |   return dcd_pipe_is_busy(moused_data.ept_handle); | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | tusb_error_t tusbd_hid_mouse_send(uint8_t coreid, hid_mouse_report_t const *p_report) | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-21 13:54:29 +07:00
										 |  |  |   ASSERT(tusbd_is_configured(coreid), TUSB_ERROR_USBD_DEVICE_NOT_CONFIGURED); | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  |   hidd_interface_t * p_mouse = &moused_data; // TODO &keyboardd_data[coreid];
 | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-01 13:53:36 +07:00
										 |  |  |   ASSERT_STATUS( dcd_pipe_xfer(p_mouse->ept_handle, p_report, sizeof(hid_mouse_report_t), true) ) ; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-30 12:20:00 +07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // USBD-CLASS API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  | static void interface_clear(hidd_interface_t * p_interface) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if ( p_interface != NULL ) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     memclr_(p_interface, sizeof(hidd_interface_t)); | 
					
						
							|  |  |  |     p_interface->interface_number = INTERFACE_INVALID_NUMBER; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-11 12:48:21 +07:00
										 |  |  | void hidd_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |   for(uint8_t i=0; i<HIDD_NUMBER_OF_SUBCLASS; i++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     interface_clear( hidd_class_driver[i].p_interface ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-11-11 12:48:21 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-21 12:47:55 +07:00
										 |  |  | void hidd_close(uint8_t coreid) | 
					
						
							| 
									
										
										
										
											2013-11-11 12:48:21 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |   for(uint8_t i=0; i<HIDD_NUMBER_OF_SUBCLASS; i++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     interface_clear(hidd_class_driver[i].p_interface); | 
					
						
							|  |  |  |     if ( hidd_class_driver[i].unmounted_cb ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       hidd_class_driver[i].unmounted_cb(coreid); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-11-11 12:48:21 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  | tusb_error_t hidd_control_request_subtask(uint8_t coreid, tusb_control_request_t const * p_request) | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |   uint8_t subclass_idx; | 
					
						
							|  |  |  |   for(subclass_idx=0; subclass_idx<HIDD_NUMBER_OF_SUBCLASS; subclass_idx++) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     hidd_interface_t * const p_interface = hidd_class_driver[subclass_idx].p_interface; | 
					
						
							|  |  |  |     if ( (p_interface != NULL) && (p_request->wIndex == p_interface->interface_number) ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ASSERT(subclass_idx < HIDD_NUMBER_OF_SUBCLASS, TUSB_ERROR_FAILED); | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |   hidd_class_driver_t const * const p_driver = &hidd_class_driver[subclass_idx]; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |   hidd_interface_t* const p_hid = p_driver->p_interface; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 14:13:06 +07:00
										 |  |  |   //------------- STD Request -------------//
 | 
					
						
							|  |  |  |   if (p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_STANDARD) | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  |   { | 
					
						
							|  |  |  |     uint8_t const desc_type  = u16_high_u8(p_request->wValue); | 
					
						
							|  |  |  |     uint8_t const desc_index = u16_low_u8 (p_request->wValue); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-21 12:47:55 +07:00
										 |  |  |     (void) desc_index; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  |     ASSERT ( p_request->bRequest == TUSB_REQUEST_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT, | 
					
						
							|  |  |  |              TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |     dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_driver->p_report_desc, p_hid->report_length, false); | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  |   } | 
					
						
							|  |  |  |   //------------- Class Specific Request -------------//
 | 
					
						
							|  |  |  |   else if (p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_CLASS) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |     OSAL_SUBTASK_BEGIN | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if( (HID_REQUEST_CONTROL_GET_REPORT == p_request->bRequest) && (p_driver->get_report_cb != NULL) ) | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |       // wValue = Report Type | Report ID
 | 
					
						
							|  |  |  |       void* p_buffer = NULL; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |       uint16_t actual_length = p_driver->get_report_cb(coreid, (hid_request_report_type_t) u16_high_u8(p_request->wValue), | 
					
						
							|  |  |  |                                                        &p_buffer, p_request->wLength); | 
					
						
							|  |  |  |       SUBTASK_ASSERT( p_buffer != NULL && actual_length > 0 ); | 
					
						
							| 
									
										
										
										
											2013-11-14 14:12:07 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |       dcd_pipe_control_xfer(coreid, p_request->bmRequestType_bit.direction, p_buffer, actual_length, false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if ( (HID_REQUEST_CONTROL_SET_REPORT == p_request->bRequest) && (p_driver->set_report_cb != NULL) ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       //        return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; // TODO test STALL control out endpoint (with mouse+keyboard)
 | 
					
						
							|  |  |  |       // wValue = Report Type | Report ID
 | 
					
						
							|  |  |  |       tusb_error_t error; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |       dcd_pipe_control_xfer(coreid, p_request->bmRequestType_bit.direction, &m_control_data, p_request->wLength, true); | 
					
						
							| 
									
										
										
										
											2013-11-21 12:47:55 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |       osal_semaphore_wait(usbd_control_xfer_sem_hdl, OSAL_TIMEOUT_NORMAL, &error); // wait for control xfer complete
 | 
					
						
							|  |  |  |       SUBTASK_ASSERT_STATUS(error); | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |       p_driver->set_report_cb(coreid, (hid_request_report_type_t) u16_high_u8(p_request->wValue), | 
					
						
							|  |  |  |                               &m_control_data, p_request->wLength); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (HID_REQUEST_CONTROL_SET_IDLE == p_request->bRequest) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       // uint8_t idle_rate = u16_high_u8(p_request->wValue);
 | 
					
						
							|  |  |  |     }else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | //      HID_REQUEST_CONTROL_GET_IDLE:
 | 
					
						
							|  |  |  | //      HID_REQUEST_CONTROL_GET_PROTOCOL:
 | 
					
						
							|  |  |  | //      HID_REQUEST_CONTROL_SET_PROTOCOL:
 | 
					
						
							|  |  |  |       return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     OSAL_SUBTASK_END | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  |   }else | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  |     return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT; | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   uint8_t const *p_desc = (uint8_t const *) p_interface_desc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- HID descriptor -------------//
 | 
					
						
							|  |  |  |   p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; | 
					
						
							|  |  |  |   tusb_hid_descriptor_hid_t const *p_desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; | 
					
						
							|  |  |  |   ASSERT_INT(HID_DESC_TYPE_HID, p_desc_hid->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //------------- Endpoint Descriptor -------------//
 | 
					
						
							|  |  |  |   p_desc += p_desc[DESCRIPTOR_OFFSET_LENGTH]; | 
					
						
							|  |  |  |   tusb_descriptor_endpoint_t const *p_desc_endpoint = (tusb_descriptor_endpoint_t const *) p_desc; | 
					
						
							|  |  |  |   ASSERT_INT(TUSB_DESC_TYPE_ENDPOINT, p_desc_endpoint->bDescriptorType, TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (p_interface_desc->bInterfaceSubClass == HID_SUBCLASS_BOOT) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     switch(p_interface_desc->bInterfaceProtocol) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       case HID_PROTOCOL_KEYBOARD: | 
					
						
							|  |  |  |       case HID_PROTOCOL_MOUSE: | 
					
						
							| 
									
										
										
										
											2013-10-30 14:13:06 +07:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2013-12-09 11:15:13 +07:00
										 |  |  |         hidd_class_driver_t const * const p_driver = &hidd_class_driver[p_interface_desc->bInterfaceProtocol]; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |         hidd_interface_t * const p_hid = p_driver->p_interface; | 
					
						
							| 
									
										
										
										
											2013-10-30 14:13:06 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ASSERT_PTR(p_hid, TUSB_ERROR_FAILED); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  |         p_hid->ept_handle       = dcd_pipe_open(coreid, p_desc_endpoint, p_interface_desc->bInterfaceClass); | 
					
						
							| 
									
										
										
										
											2013-10-30 14:13:06 +07:00
										 |  |  |         ASSERT( endpointhandle_is_valid(p_hid->ept_handle), TUSB_ERROR_DCD_FAILED); | 
					
						
							| 
									
										
										
										
											2013-11-21 13:54:29 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         p_hid->interface_number = p_interface_desc->bInterfaceNumber; | 
					
						
							|  |  |  |         p_hid->report_length    = p_desc_hid->wReportLength; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |         p_driver->mounted_cb(coreid); | 
					
						
							| 
									
										
										
										
											2013-10-30 14:13:06 +07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       default: // TODO unknown, unsupported protocol --> skip this interface
 | 
					
						
							|  |  |  |         return TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *p_length = sizeof(tusb_descriptor_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + sizeof(tusb_descriptor_endpoint_t); | 
					
						
							|  |  |  |   }else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // open generic
 | 
					
						
							|  |  |  |     *p_length = 0; | 
					
						
							|  |  |  |     return TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-01 13:53:36 +07:00
										 |  |  | tusb_error_t hidd_xfer_cb(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes) | 
					
						
							| 
									
										
										
										
											2013-11-06 19:55:48 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |   for(uint8_t i=0; i<HIDD_NUMBER_OF_SUBCLASS; i++) | 
					
						
							| 
									
										
										
										
											2013-11-06 19:55:48 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:17:51 +07:00
										 |  |  |     hidd_interface_t * const p_interface = hidd_class_driver[i].p_interface; | 
					
						
							|  |  |  |     if ( (p_interface != NULL) && endpointhandle_is_equal(edpt_hdl, p_interface->ept_handle) ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       hidd_class_driver[i].xfer_cb(edpt_hdl.coreid, event, xferred_bytes); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-11-06 19:55:48 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-01 13:53:36 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return TUSB_ERROR_NONE; | 
					
						
							| 
									
										
										
										
											2013-11-06 19:55:48 +07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-29 17:16:41 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-13 14:00:39 +07:00
										 |  |  | #if defined(CAP_DEVICE_ROMDRIVER)
 | 
					
						
							| 
									
										
										
										
											2013-06-04 13:36:18 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-25 16:03:40 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MACRO CONSTANT TYPEDEF
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-06-01 21:54:08 +07:00
										 |  |  | #if TUSB_CFG_DEVICE_HID_KEYBOARD
 | 
					
						
							|  |  |  | TUSB_CFG_ATTR_USBRAM uint8_t hidd_keyboard_buffer[1024]; // TODO memory reduce
 | 
					
						
							| 
									
										
										
										
											2013-10-25 17:28:31 +07:00
										 |  |  | TUSB_CFG_ATTR_USBRAM hid_keyboard_report_t hid_keyboard_report; | 
					
						
							| 
									
										
										
										
											2013-06-01 21:54:08 +07:00
										 |  |  | static volatile bool bKeyChanged = false; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if TUSB_CFG_DEVICE_HID_MOUSE
 | 
					
						
							| 
									
										
										
										
											2013-06-03 14:31:17 +07:00
										 |  |  | TUSB_CFG_ATTR_USBRAM uint8_t hidd_mouse_buffer[1024]; // TODO memory reduce
 | 
					
						
							| 
									
										
										
										
											2013-10-25 17:28:31 +07:00
										 |  |  | TUSB_CFG_ATTR_USBRAM hid_mouse_report_t hid_mouse_report; | 
					
						
							| 
									
										
										
										
											2013-06-01 21:54:08 +07:00
										 |  |  | static volatile bool bMouseChanged = false; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-05-25 16:03:40 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:14 +07:00
										 |  |  | // APPLICATION API
 | 
					
						
							| 
									
										
										
										
											2013-05-25 16:03:40 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-06-01 21:54:08 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-03 15:56:14 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // IMPLEMENTATION
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  | ErrorCode_t HID_GetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t* plength) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*) hHid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ReportID = SetupPacket.wValue.WB.L; */ | 
					
						
							|  |  |  |   if (pSetup->wValue.WB.H == HID_REQUEST_REPORT_INPUT) | 
					
						
							|  |  |  |     return (ERR_USBD_STALL);          /* Not Supported */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (pHidCtrl->protocol) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     #if TUSB_CFG_DEVICE_HID_KEYBOARD
 | 
					
						
							|  |  |  |       case HID_PROTOCOL_KEYBOARD: | 
					
						
							|  |  |  |         *pBuffer = (uint8_t*) &hid_keyboard_report; | 
					
						
							| 
									
										
										
										
											2013-10-25 17:28:31 +07:00
										 |  |  |         *plength = sizeof(hid_keyboard_report_t); | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!bKeyChanged) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           memset(pBuffer, 0, *plength); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         bKeyChanged = false; | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     #if TUSB_CFG_DEVICE_HID_MOUSE
 | 
					
						
							|  |  |  |       case HID_PROTOCOL_MOUSE: | 
					
						
							|  |  |  |         *pBuffer = (uint8_t*) &hid_mouse_report; | 
					
						
							| 
									
										
										
										
											2013-10-25 17:28:31 +07:00
										 |  |  |         *plength = sizeof(hid_mouse_report_t); | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!bMouseChanged) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           memset(pBuffer, 0, *plength); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         bMouseChanged = false; | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (LPC_OK); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorCode_t HID_SetReport( USBD_HANDLE_T hHid, USB_SETUP_PACKET* pSetup, uint8_t** pBuffer, uint16_t length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* we will reuse standard EP0Buf */ | 
					
						
							|  |  |  |   if (length == 0) | 
					
						
							|  |  |  |     return LPC_OK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ReportID = SetupPacket.wValue.WB.L; */ | 
					
						
							|  |  |  |   if (pSetup->wValue.WB.H != HID_REQUEST_REPORT_OUTPUT) | 
					
						
							|  |  |  |     return (ERR_USBD_STALL);          /* Not Supported */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (LPC_OK); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorCode_t HID_EpIn_Hdlr (USBD_HANDLE_T hUsb, void* data, uint32_t event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (USB_EVT_IN == event) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*)data; | 
					
						
							|  |  |  |     switch(pHidCtrl->protocol) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       #if TUSB_CFG_DEVICE_HID_KEYBOARD
 | 
					
						
							|  |  |  |         case HID_PROTOCOL_KEYBOARD: | 
					
						
							|  |  |  |           if (!bKeyChanged) | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2013-10-25 17:28:31 +07:00
										 |  |  |             memset(&hid_keyboard_report, 0, sizeof(hid_keyboard_report_t)); | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2013-10-25 17:28:31 +07:00
										 |  |  |           ROM_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, (uint8_t*) &hid_keyboard_report, sizeof(hid_keyboard_report_t)); | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  |           bKeyChanged = false; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       #if TUSB_CFG_DEVICE_HID_MOUSE
 | 
					
						
							|  |  |  |         case HID_PROTOCOL_MOUSE: | 
					
						
							|  |  |  |           if (!bMouseChanged) | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2013-10-25 17:28:31 +07:00
										 |  |  |             memset(&hid_mouse_report, 0, sizeof(hid_mouse_report_t)); | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2013-10-25 17:28:31 +07:00
										 |  |  |           ROM_API->hw->WriteEP(hUsb, pHidCtrl->epin_adr, (uint8_t*) &hid_mouse_report, sizeof(hid_mouse_report_t)); | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  |           bMouseChanged = false; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return LPC_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorCode_t HID_EpOut_Hdlr (USBD_HANDLE_T hUsb, void* data, uint32_t event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (USB_EVT_OUT == event) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // not used yet
 | 
					
						
							|  |  |  |     // uint8_t outreport[8];
 | 
					
						
							|  |  |  |     // USB_HID_CTRL_T* pHidCtrl = (USB_HID_CTRL_T*)data;
 | 
					
						
							|  |  |  |     // ROM_API->hw->ReadEP(hUsb, pHidCtrl->epout_adr, outreport);
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return LPC_OK; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-27 21:33:06 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-12 14:06:41 +07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-05-29 14:39:14 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-25 16:03:40 +07:00
										 |  |  | #endif
 |