hid.c
21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
* File : hid.c
* COPYRIGHT (C) 2006 - 2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-03-13 Urey the first version
* 2017-11-16 ZYH Update to common hid
*/
#include <rthw.h>
#include <rtdevice.h>
#include "drivers/usb_common.h"
#include "drivers/usb_device.h"
#include "hid.h"
#ifdef RT_USB_DEVICE_HID
#define HID_INTF_STR_INDEX 7
struct hid_s
{
struct rt_device parent;
struct ufunction *func;
uep_t ep_in;
uep_t ep_out;
int status;
rt_uint8_t protocol;
rt_uint8_t report_buf[MAX_REPORT_SIZE];
struct rt_messagequeue hid_mq;
};
/* CustomHID_ConfigDescriptor */
ALIGN(4)
const rt_uint8_t _report_desc[]=
{
#ifdef RT_USB_DEVICE_HID_KEYBOARD
USAGE_PAGE(1), 0x01,
USAGE(1), 0x06,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_KEYBOARD1,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0xE0,
USAGE_MAXIMUM(1), 0xE7,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x08,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x08,
INPUT(1), 0x01,
REPORT_COUNT(1), 0x05,
REPORT_SIZE(1), 0x01,
USAGE_PAGE(1), 0x08,
USAGE_MINIMUM(1), 0x01,
USAGE_MAXIMUM(1), 0x05,
OUTPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x03,
OUTPUT(1), 0x01,
REPORT_COUNT(1), 0x06,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x65,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0x00,
USAGE_MAXIMUM(1), 0x65,
INPUT(1), 0x00,
END_COLLECTION(0),
#if RT_USB_DEVICE_HID_KEYBOARD_NUMBER>1
/****keyboard2*****/
USAGE_PAGE(1), 0x01,
USAGE(1), 0x06,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_KEYBOARD2,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0xE0,
USAGE_MAXIMUM(1), 0xE7,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x08,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x08,
INPUT(1), 0x01,
REPORT_COUNT(1), 0x06,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x65,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0x00,
USAGE_MAXIMUM(1), 0x65,
INPUT(1), 0x00,
END_COLLECTION(0),
#if RT_USB_DEVICE_HID_KEYBOARD_NUMBER>2
USAGE_PAGE(1), 0x01,
USAGE(1), 0x06,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_KEYBOARD3,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0xE0,
USAGE_MAXIMUM(1), 0xE7,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x08,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x08,
INPUT(1), 0x01,
REPORT_COUNT(1), 0x06,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x65,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0x00,
USAGE_MAXIMUM(1), 0x65,
INPUT(1), 0x00,
END_COLLECTION(0),
#if RT_USB_DEVICE_HID_KEYBOARD_NUMBER>3
USAGE_PAGE(1), 0x01,
USAGE(1), 0x06,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_KEYBOARD4,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0xE0,
USAGE_MAXIMUM(1), 0xE7,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x08,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x08,
INPUT(1), 0x01,
REPORT_COUNT(1), 0x06,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x65,
USAGE_PAGE(1), 0x07,
USAGE_MINIMUM(1), 0x00,
USAGE_MAXIMUM(1), 0x65,
INPUT(1), 0x00,
END_COLLECTION(0),
#endif
#endif
#endif
#endif
// Media Control
#ifdef RT_USB_DEVICE_HID_MEDIA
USAGE_PAGE(1), 0x0C,
USAGE(1), 0x01,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_MEDIA,
USAGE_PAGE(1), 0x0C,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
REPORT_SIZE(1), 0x01,
REPORT_COUNT(1), 0x07,
USAGE(1), 0xB5, // Next Track
USAGE(1), 0xB6, // Previous Track
USAGE(1), 0xB7, // Stop
USAGE(1), 0xCD, // Play / Pause
USAGE(1), 0xE2, // Mute
USAGE(1), 0xE9, // Volume Up
USAGE(1), 0xEA, // Volume Down
INPUT(1), 0x02, // Input (Data, Variable, Absolute)
REPORT_COUNT(1), 0x01,
INPUT(1), 0x01,
END_COLLECTION(0),
#endif
#ifdef RT_USB_DEVICE_HID_GENERAL
USAGE_PAGE(1), 0x8c,
USAGE(1), 0x01,
COLLECTION(1), 0x01,
REPORT_ID(1), HID_REPORT_ID_GENERAL,
REPORT_COUNT(1), RT_USB_DEVICE_HID_GENERAL_IN_REPORT_LENGTH,
USAGE(1), 0x03,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0xFF,
INPUT(1), 0x02,
REPORT_COUNT(1), RT_USB_DEVICE_HID_GENERAL_OUT_REPORT_LENGTH,
USAGE(1), 0x04,
REPORT_SIZE(1), 0x08,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0xFF,
OUTPUT(1), 0x02,
END_COLLECTION(0),
#endif
#ifdef RT_USB_DEVICE_HID_MOUSE
USAGE_PAGE(1), 0x01, // Generic Desktop
USAGE(1), 0x02, // Mouse
COLLECTION(1), 0x01, // Application
USAGE(1), 0x01, // Pointer
COLLECTION(1), 0x00, // Physical
REPORT_ID(1), HID_REPORT_ID_MOUSE,
REPORT_COUNT(1), 0x03,
REPORT_SIZE(1), 0x01,
USAGE_PAGE(1), 0x09, // Buttons
USAGE_MINIMUM(1), 0x1,
USAGE_MAXIMUM(1), 0x3,
LOGICAL_MINIMUM(1), 0x00,
LOGICAL_MAXIMUM(1), 0x01,
INPUT(1), 0x02,
REPORT_COUNT(1), 0x01,
REPORT_SIZE(1), 0x05,
INPUT(1), 0x01,
REPORT_COUNT(1), 0x03,
REPORT_SIZE(1), 0x08,
USAGE_PAGE(1), 0x01,
USAGE(1), 0x30, // X
USAGE(1), 0x31, // Y
USAGE(1), 0x38, // scroll
LOGICAL_MINIMUM(1), 0x81,
LOGICAL_MAXIMUM(1), 0x7f,
INPUT(1), 0x06,
END_COLLECTION(0),
END_COLLECTION(0),
#endif
}; /* CustomHID_ReportDescriptor */
ALIGN(4)
static struct udevice_descriptor _dev_desc =
{
USB_DESC_LENGTH_DEVICE, //bLength;
USB_DESC_TYPE_DEVICE, //type;
USB_BCD_VERSION, //bcdUSB;
0x0, //bDeviceClass;
0x00, //bDeviceSubClass;
0x00, //bDeviceProtocol;
64, //bMaxPacketSize0;
_VENDOR_ID, //idVendor;
_PRODUCT_ID, //idProduct;
USB_BCD_DEVICE, //bcdDevice;
USB_STRING_MANU_INDEX, //iManufacturer;
USB_STRING_PRODUCT_INDEX, //iProduct;
USB_STRING_SERIAL_INDEX, //iSerialNumber;
USB_DYNAMIC, //bNumConfigurations;
};
//FS and HS needed
ALIGN(4)
static struct usb_qualifier_descriptor dev_qualifier =
{
sizeof(dev_qualifier), //bLength
USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
0x0200, //bcdUSB
0x0, //bDeviceClass
0x0, //bDeviceSubClass
0x50, //bDeviceProtocol
64, //bMaxPacketSize0
0x01, //bNumConfigurations
0,
};
/* hid interface descriptor */
ALIGN(4)
const static struct uhid_comm_descriptor _hid_comm_desc =
{
#ifdef RT_USB_DEVICE_COMPOSITE
/* Interface Association Descriptor */
{
USB_DESC_LENGTH_IAD,
USB_DESC_TYPE_IAD,
USB_DYNAMIC,
0x01,
0x03, /* bInterfaceClass: HID */
#if defined(RT_USB_DEVICE_HID_KEYBOARD)||defined(RT_USB_DEVICE_HID_MOUSE)
USB_HID_SUBCLASS_BOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
#else
USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
#endif
#if !defined(RT_USB_DEVICE_HID_KEYBOARD)||!defined(RT_USB_DEVICE_HID_MOUSE)||!defined(RT_USB_DEVICE_HID_MEDIA)
USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#elif !defined(RT_USB_DEVICE_HID_MOUSE)
USB_HID_PROTOCOL_KEYBOARD, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#else
USB_HID_PROTOCOL_MOUSE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#endif
0x00,
},
#endif
/* Interface Descriptor */
{
USB_DESC_LENGTH_INTERFACE,
USB_DESC_TYPE_INTERFACE,
USB_DYNAMIC, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints */
0x03, /* bInterfaceClass: HID */
#if defined(RT_USB_DEVICE_HID_KEYBOARD)||defined(RT_USB_DEVICE_HID_MOUSE)
USB_HID_SUBCLASS_BOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
#else
USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
#endif
#if !defined(RT_USB_DEVICE_HID_KEYBOARD)||!defined(RT_USB_DEVICE_HID_MOUSE)||!defined(RT_USB_DEVICE_HID_MEDIA)
USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#elif !defined(RT_USB_DEVICE_HID_MOUSE)
USB_HID_PROTOCOL_KEYBOARD, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#else
USB_HID_PROTOCOL_MOUSE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
#endif
#ifdef RT_USB_DEVICE_COMPOSITE
HID_INTF_STR_INDEX, /* iInterface: Index of string descriptor */
#else
0,
#endif
},
/* HID Descriptor */
{
HID_DESCRIPTOR_SIZE, /* bLength: HID Descriptor size */
HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
0x0110, /* bcdHID: HID Class Spec release number */
0x00, /* bCountryCode: Hardware target country */
0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
{
{
0x22, /* bDescriptorType */
sizeof(_report_desc), /* wItemLength: Total length of Report descriptor */
},
},
},
/* Endpoint Descriptor IN */
{
USB_DESC_LENGTH_ENDPOINT,
USB_DESC_TYPE_ENDPOINT,
USB_DYNAMIC | USB_DIR_IN,
USB_EP_ATTR_INT,
0x40,
0x0A,
},
/* Endpoint Descriptor OUT */
{
USB_DESC_LENGTH_ENDPOINT,
USB_DESC_TYPE_ENDPOINT,
USB_DYNAMIC | USB_DIR_OUT,
USB_EP_ATTR_INT,
0x40,
0x01,
},
};
ALIGN(4)
const static char* _ustring[] =
{
"Language",
"RT-Thread Team.",
"RTT HID-Device",
"32021919830108",
"Configuration",
"Interface",
};
static void dump_data(rt_uint8_t *data, rt_size_t size)
{
rt_size_t i;
for (i = 0; i < size; i++)
{
rt_kprintf("%02x ", *data++);
if ((i + 1) % 8 == 0)
{
rt_kprintf("\n");
}else if ((i + 1) % 4 == 0){
rt_kprintf(" ");
}
}
}
static void dump_report(struct hid_report * report)
{
rt_kprintf("\nHID Recived:");
rt_kprintf("\nReport ID %02x \n", report->report_id);
dump_data(report->report,report->size);
}
static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
{
struct hid_s *data;
struct hid_report report;
RT_ASSERT(func != RT_NULL);
RT_ASSERT(func->device != RT_NULL);
data = (struct hid_s *) func->user_data;
if(size != 0)
{
rt_memcpy((void *)&report,(void*)data->ep_out->buffer,size);
report.size = size-1;
rt_mq_send(&data->hid_mq,(void *)&report,sizeof(report));
}
data->ep_out->request.buffer = data->ep_out->buffer;
data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
return RT_EOK;
}
static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
{
struct hid_s *data;
RT_ASSERT(func != RT_NULL);
RT_ASSERT(func->device != RT_NULL);
data = (struct hid_s *) func->user_data;
if(data->parent.tx_complete != RT_NULL)
{
data->parent.tx_complete(&data->parent,RT_NULL);
}
return RT_EOK;
}
static rt_err_t _hid_set_report_callback(udevice_t device, rt_size_t size)
{
RT_DEBUG_LOG(RT_DEBUG_USB, ("_hid_set_report_callback\n"));
if(size != 0)
{
}
dcd_ep0_send_status(device->dcd);
return RT_EOK;
}
/**
* This function will handle hid interface bRequest.
*
* @param device the usb device object.
* @param setup the setup bRequest.
*
* @return RT_EOK on successful.
*/
static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
{
RT_ASSERT(func != RT_NULL);
RT_ASSERT(func->device != RT_NULL);
RT_ASSERT(setup != RT_NULL);
struct hid_s *data = (struct hid_s *) func->user_data;
switch (setup->bRequest)
{
case USB_REQ_GET_DESCRIPTOR:
if((setup->wValue >> 8) == USB_DESC_TYPE_REPORT)
{
rt_usbd_ep0_write(func->device, (void *)(&_report_desc[0]), sizeof(_report_desc));
}
else if((setup->wValue >> 8) == USB_DESC_TYPE_HID)
{
rt_usbd_ep0_write(func->device, (void *)(&_hid_comm_desc.hid_desc), sizeof(struct uhid_descriptor));
}
break;
case USB_HID_REQ_GET_REPORT:
if(setup->wLength == 0)
{
rt_usbd_ep0_set_stall(func->device);
break;
}
if((setup->wLength == 0) || (setup->wLength > MAX_REPORT_SIZE))
setup->wLength = MAX_REPORT_SIZE;
rt_usbd_ep0_write(func->device, data->report_buf,setup->wLength);
break;
case USB_HID_REQ_GET_IDLE:
dcd_ep0_send_status(func->device->dcd);
break;
case USB_HID_REQ_GET_PROTOCOL:
rt_usbd_ep0_write(func->device, &data->protocol,1);
break;
case USB_HID_REQ_SET_REPORT:
if((setup->wLength == 0) || (setup->wLength > MAX_REPORT_SIZE))
rt_usbd_ep0_set_stall(func->device);
rt_usbd_ep0_read(func->device, data->report_buf, setup->wLength, _hid_set_report_callback);
break;
case USB_HID_REQ_SET_IDLE:
dcd_ep0_send_status(func->device->dcd);
break;
case USB_HID_REQ_SET_PROTOCOL:
data->protocol = setup->wValue;
dcd_ep0_send_status(func->device->dcd);
break;
}
return RT_EOK;
}
/**
* This function will run cdc function, it will be called on handle set configuration bRequest.
*
* @param func the usb function object.
*
* @return RT_EOK on successful.
*/
static rt_err_t _function_enable(ufunction_t func)
{
struct hid_s *data;
RT_ASSERT(func != RT_NULL);
RT_ASSERT(func->device != RT_NULL);
data = (struct hid_s *) func->user_data;
RT_DEBUG_LOG(RT_DEBUG_USB, ("hid function enable\n"));
//
// _vcom_reset_state(func);
//
if(data->ep_out->buffer == RT_NULL)
{
data->ep_out->buffer = rt_malloc(HID_RX_BUFSIZE);
}
data->ep_out->request.buffer = data->ep_out->buffer;
data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
return RT_EOK;
}
/**
* This function will stop cdc function, it will be called on handle set configuration bRequest.
*
* @param func the usb function object.
*
* @return RT_EOK on successful.
*/
static rt_err_t _function_disable(ufunction_t func)
{
struct hid_s *data;
RT_ASSERT(func != RT_NULL);
RT_ASSERT(func->device != RT_NULL);
data = (struct hid_s *) func->user_data;
RT_DEBUG_LOG(RT_DEBUG_USB, ("hid function disable\n"));
if(data->ep_out->buffer != RT_NULL)
{
rt_free(data->ep_out->buffer);
data->ep_out->buffer = RT_NULL;
}
return RT_EOK;
}
static struct ufunction_ops ops =
{
_function_enable,
_function_disable,
RT_NULL,
};
/**
* This function will configure hid descriptor.
*
* @param comm the communication interface number.
* @param data the data interface number.
*
* @return RT_EOK on successful.
*/
static rt_err_t _hid_descriptor_config(uhid_comm_desc_t hid, rt_uint8_t cintf_nr)
{
#ifdef RT_USB_DEVICE_COMPOSITE
hid->iad_desc.bFirstInterface = cintf_nr;
#endif
return RT_EOK;
}
static rt_size_t _hid_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
struct hid_s *hiddev = (struct hid_s *)dev;
struct hid_report report;
if (hiddev->func->device->state == USB_STATE_CONFIGURED)
{
report.report_id = pos;
rt_memcpy((void *)report.report,(void *)buffer,size);
report.size = size;
hiddev->ep_in->request.buffer = (void *)&report;
hiddev->ep_in->request.size = (size+1) > 64 ? 64 : size+1;
hiddev->ep_in->request.req_type = UIO_REQUEST_WRITE;
rt_usbd_io_request(hiddev->func->device, hiddev->ep_in, &hiddev->ep_in->request);
return size;
}
return 0;
}
RT_WEAK void HID_Report_Received(hid_report_t report)
{
dump_report(report);
}
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t hid_thread_stack[512];
static struct rt_thread hid_thread;
static void hid_thread_entry(void* parameter)
{
struct hid_report report;
struct hid_s *hiddev;
hiddev = (struct hid_s *)parameter;
while(1)
{
if(rt_mq_recv(&hiddev->hid_mq, &report, sizeof(report),RT_WAITING_FOREVER) != RT_EOK )
continue;
HID_Report_Received(&report);
}
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops hid_device_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
_hid_write,
RT_NULL,
};
#endif
static rt_uint8_t hid_mq_pool[(sizeof(struct hid_report)+sizeof(void*))*8];
static void rt_usb_hid_init(struct ufunction *func)
{
struct hid_s *hiddev;
hiddev = (struct hid_s *)func->user_data;
rt_memset(&hiddev->parent, 0, sizeof(hiddev->parent));
#ifdef RT_USING_DEVICE_OPS
hiddev->parent.ops = &hid_device_ops;
#else
hiddev->parent.write = _hid_write;
#endif
hiddev->func = func;
rt_device_register(&hiddev->parent, "hidd", RT_DEVICE_FLAG_RDWR);
rt_mq_init(&hiddev->hid_mq, "hiddmq", hid_mq_pool, sizeof(struct hid_report),
sizeof(hid_mq_pool), RT_IPC_FLAG_FIFO);
rt_thread_init(&hid_thread, "hidd", hid_thread_entry, hiddev,
hid_thread_stack, sizeof(hid_thread_stack), RT_USBD_THREAD_PRIO, 20);
rt_thread_startup(&hid_thread);
}
/**
* This function will create a hid function instance.
*
* @param device the usb device object.
*
* @return RT_EOK on successful.
*/
ufunction_t rt_usbd_function_hid_create(udevice_t device)
{
ufunction_t func;
struct hid_s *data;
uintf_t hid_intf;
ualtsetting_t hid_setting;
uhid_comm_desc_t hid_desc;
/* parameter check */
RT_ASSERT(device != RT_NULL);
/* set usb device string description */
#ifdef RT_USB_DEVICE_COMPOSITE
rt_usbd_device_set_interface_string(device, HID_INTF_STR_INDEX, _ustring[2]);
#else
rt_usbd_device_set_string(device, _ustring);
#endif
/* create a cdc function */
func = rt_usbd_function_new(device, &_dev_desc, &ops);
/* For high speed mode supporting */
rt_usbd_device_set_qualifier(device, &dev_qualifier);
/* allocate memory for cdc vcom data */
data = (struct hid_s*)rt_malloc(sizeof(struct hid_s));
rt_memset(data, 0, sizeof(struct hid_s));
func->user_data = (void*)data;
/* create an interface object */
hid_intf = rt_usbd_interface_new(device, _interface_handler);
/* create an alternate setting object */
hid_setting = rt_usbd_altsetting_new(sizeof(struct uhid_comm_descriptor));
/* config desc in alternate setting */
rt_usbd_altsetting_config_descriptor(hid_setting, &_hid_comm_desc, (rt_off_t)&((uhid_comm_desc_t)0)->intf_desc);
/* configure the hid interface descriptor */
_hid_descriptor_config(hid_setting->desc, hid_intf->intf_num);
/* create endpoint */
hid_desc = (uhid_comm_desc_t)hid_setting->desc;
data->ep_out = rt_usbd_endpoint_new(&hid_desc->ep_out_desc, _ep_out_handler);
data->ep_in = rt_usbd_endpoint_new(&hid_desc->ep_in_desc, _ep_in_handler);
/* add the int out and int in endpoint to the alternate setting */
rt_usbd_altsetting_add_endpoint(hid_setting, data->ep_out);
rt_usbd_altsetting_add_endpoint(hid_setting, data->ep_in);
/* add the alternate setting to the interface, then set default setting */
rt_usbd_interface_add_altsetting(hid_intf, hid_setting);
rt_usbd_set_altsetting(hid_intf, 0);
/* add the interface to the mass storage function */
rt_usbd_function_add_interface(func, hid_intf);
/* initilize hid */
rt_usb_hid_init(func);
return func;
}
struct udclass hid_class =
{
.rt_usbd_function_create = rt_usbd_function_hid_create
};
int rt_usbd_hid_class_register(void)
{
rt_usbd_class_register(&hid_class);
return 0;
}
INIT_PREV_EXPORT(rt_usbd_hid_class_register);
#endif /* RT_USB_DEVICE_HID */