17
17
#include <linux/poll.h>
18
18
#include <linux/uaccess.h>
19
19
#include <linux/wait.h>
20
+ #include <linux/delay.h>
20
21
#include <linux/sched.h>
21
22
#include <linux/usb/g_hid.h>
23
+ #include "f_hid.h"
22
24
23
25
static int major , minors ;
24
26
static struct class * hidg_class ;
@@ -60,6 +62,43 @@ struct f_hidg {
60
62
struct usb_ep * out_ep ;
61
63
};
62
64
65
+ /* Hacky device list to fix f_hidg_write being called after device destroyed.
66
+ It covers only most common race conditions, there will be rare crashes anyway. */
67
+ enum { HACKY_DEVICE_LIST_SIZE = 4 };
68
+ static struct f_hidg * hacky_device_list [HACKY_DEVICE_LIST_SIZE ];
69
+ static void hacky_device_list_add (struct f_hidg * hidg )
70
+ {
71
+ int i ;
72
+ for (i = 0 ; i < HACKY_DEVICE_LIST_SIZE ; i ++ ) {
73
+ if (!hacky_device_list [i ]) {
74
+ hacky_device_list [i ] = hidg ;
75
+ return ;
76
+ }
77
+ }
78
+ pr_err ("%s: too many devices, not adding device %p\n" , __func__ , hidg );
79
+ }
80
+ static void hacky_device_list_remove (struct f_hidg * hidg )
81
+ {
82
+ int i ;
83
+ for (i = 0 ; i < HACKY_DEVICE_LIST_SIZE ; i ++ ) {
84
+ if (hacky_device_list [i ] == hidg ) {
85
+ hacky_device_list [i ] = NULL ;
86
+ return ;
87
+ }
88
+ }
89
+ pr_err ("%s: cannot find device %p\n" , __func__ , hidg );
90
+ }
91
+ static int hacky_device_list_check (struct f_hidg * hidg )
92
+ {
93
+ int i ;
94
+ for (i = 0 ; i < HACKY_DEVICE_LIST_SIZE ; i ++ ) {
95
+ if (hacky_device_list [i ] == hidg ) {
96
+ return 0 ;
97
+ }
98
+ }
99
+ return 1 ;
100
+ }
101
+
63
102
static inline struct f_hidg * func_to_hidg (struct usb_function * f )
64
103
{
65
104
return container_of (f , struct f_hidg , func );
@@ -176,6 +215,11 @@ static ssize_t f_hidg_read(struct file *file, char __user *buffer,
176
215
if (!access_ok (VERIFY_WRITE , buffer , count ))
177
216
return - EFAULT ;
178
217
218
+ if (hacky_device_list_check (hidg )) {
219
+ pr_err ("%s: trying to read from device %p that was destroyed\n" , __func__ , hidg );
220
+ return - EIO ;
221
+ }
222
+
179
223
spin_lock_irqsave (& hidg -> spinlock , flags );
180
224
181
225
#define READ_COND (!list_empty(&hidg->completed_out_req))
@@ -246,6 +290,11 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
246
290
if (!access_ok (VERIFY_READ , buffer , count ))
247
291
return - EFAULT ;
248
292
293
+ if (hacky_device_list_check (hidg )) {
294
+ pr_err ("%s: trying to write to device %p that was destroyed\n" , __func__ , hidg );
295
+ return - EIO ;
296
+ }
297
+
249
298
mutex_lock (& hidg -> lock );
250
299
251
300
#define WRITE_COND (!hidg->write_pending)
@@ -260,6 +309,11 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
260
309
hidg -> write_queue , WRITE_COND ))
261
310
return - ERESTARTSYS ;
262
311
312
+ if (hacky_device_list_check (hidg )) {
313
+ pr_err ("%s: trying to write to device %p that was destroyed\n" , __func__ , hidg );
314
+ return - EIO ;
315
+ }
316
+
263
317
mutex_lock (& hidg -> lock );
264
318
}
265
319
@@ -300,7 +354,18 @@ static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
300
354
struct f_hidg * hidg = file -> private_data ;
301
355
unsigned int ret = 0 ;
302
356
357
+ if (hacky_device_list_check (hidg )) {
358
+ pr_err ("%s: trying to poll device %p that was destroyed\n" , __func__ , hidg );
359
+ return - EIO ;
360
+ }
361
+
303
362
poll_wait (file , & hidg -> read_queue , wait );
363
+
364
+ if (hacky_device_list_check (hidg )) {
365
+ pr_err ("%s: trying to poll device %p that was destroyed\n" , __func__ , hidg );
366
+ return - EIO ;
367
+ }
368
+
304
369
poll_wait (file , & hidg -> write_queue , wait );
305
370
306
371
if (WRITE_COND )
@@ -399,7 +464,12 @@ static int hidg_setup(struct usb_function *f,
399
464
case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE ) << 8
400
465
| HID_REQ_GET_PROTOCOL ):
401
466
VDBG (cdev , "get_protocol\n" );
402
- goto stall ;
467
+ length = min_t (unsigned , length , 1 );
468
+ if (hidg -> bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT )
469
+ ((u8 * ) req -> buf )[0 ] = 0 ; /* Boot protocol */
470
+ else
471
+ ((u8 * ) req -> buf )[0 ] = 1 ; /* Report protocol */
472
+ goto respond ;
403
473
break ;
404
474
405
475
case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE ) << 8
@@ -411,6 +481,14 @@ static int hidg_setup(struct usb_function *f,
411
481
case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE ) << 8
412
482
| HID_REQ_SET_PROTOCOL ):
413
483
VDBG (cdev , "set_protocol\n" );
484
+ length = 0 ;
485
+ if (hidg -> bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT ) {
486
+ if (value == 0 ) /* Boot protocol */
487
+ goto respond ;
488
+ } else {
489
+ if (value == 1 ) /* Report protocol */
490
+ goto respond ;
491
+ }
414
492
goto stall ;
415
493
break ;
416
494
@@ -560,13 +638,15 @@ const struct file_operations f_hidg_fops = {
560
638
.llseek = noop_llseek ,
561
639
};
562
640
563
- static int __init hidg_bind (struct usb_configuration * c , struct usb_function * f )
641
+ static int hidg_bind (struct usb_configuration * c , struct usb_function * f )
564
642
{
565
643
struct usb_ep * ep ;
566
644
struct f_hidg * hidg = func_to_hidg (f );
567
645
int status ;
568
646
dev_t dev ;
569
647
648
+ pr_info ("%s: creating device %p\n" , __func__ , hidg );
649
+
570
650
/* allocate instance-specific interface IDs, and patch descriptors */
571
651
status = usb_interface_id (c , f );
572
652
if (status < 0 )
@@ -632,6 +712,7 @@ static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
632
712
goto fail ;
633
713
634
714
device_create (hidg_class , NULL , dev , NULL , "%s%d" , "hidg" , hidg -> minor );
715
+ hacky_device_list_add (hidg );
635
716
636
717
return 0 ;
637
718
@@ -651,12 +732,21 @@ static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
651
732
{
652
733
struct f_hidg * hidg = func_to_hidg (f );
653
734
735
+ pr_info ("%s: destroying device %p\n" , __func__ , hidg );
736
+ /* This does not cover all race conditions, only most common one */
737
+ mutex_lock (& hidg -> lock );
738
+ hacky_device_list_remove (hidg );
739
+ mutex_unlock (& hidg -> lock );
740
+
654
741
device_destroy (hidg_class , MKDEV (major , hidg -> minor ));
655
742
cdev_del (& hidg -> cdev );
656
743
657
744
/* disable/free request and end point */
658
745
usb_ep_disable (hidg -> in_ep );
659
- usb_ep_dequeue (hidg -> in_ep , hidg -> req );
746
+ /* TODO: calling this function crash kernel,
747
+ not calling this funct ion crash kernel inside f_hidg_write */
748
+ /* usb_ep_dequeue(hidg->in_ep, hidg->req); */
749
+
660
750
kfree (hidg -> req -> buf );
661
751
usb_ep_free_request (hidg -> in_ep , hidg -> req );
662
752
@@ -689,7 +779,7 @@ static struct usb_gadget_strings *ct_func_strings[] = {
689
779
/*-------------------------------------------------------------------------*/
690
780
/* usb_configuration */
691
781
692
- int __init hidg_bind_config (struct usb_configuration * c ,
782
+ int hidg_bind_config (struct usb_configuration * c ,
693
783
struct hidg_func_descriptor * fdesc , int index )
694
784
{
695
785
struct f_hidg * hidg ;
@@ -743,7 +833,7 @@ int __init hidg_bind_config(struct usb_configuration *c,
743
833
return status ;
744
834
}
745
835
746
- int __init ghid_setup (struct usb_gadget * g , int count )
836
+ int ghid_setup (struct usb_gadget * g , int count )
747
837
{
748
838
int status ;
749
839
dev_t dev ;
0 commit comments