@@ -23,6 +23,9 @@ struct uhid_device {
23
23
int dev_id ; /* uniq (random) number to identify the device */
24
24
int uhid_fd ;
25
25
int hid_id ; /* HID device id in the system */
26
+ __u16 bus ;
27
+ __u32 vid ;
28
+ __u32 pid ;
26
29
pthread_t tid ; /* thread for reading uhid events */
27
30
};
28
31
@@ -129,7 +132,9 @@ static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uh
129
132
}
130
133
}
131
134
132
- static int uhid_create (struct __test_metadata * _metadata , int fd , int rand_nb )
135
+ static int uhid_create (struct __test_metadata * _metadata , int fd , int rand_nb ,
136
+ __u16 bus , __u32 vid , __u32 pid , __u8 * rdesc ,
137
+ size_t rdesc_size )
133
138
{
134
139
struct uhid_event ev ;
135
140
char buf [25 ];
@@ -140,10 +145,10 @@ static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb)
140
145
ev .type = UHID_CREATE ;
141
146
strcpy ((char * )ev .u .create .name , buf );
142
147
ev .u .create .rd_data = rdesc ;
143
- ev .u .create .rd_size = sizeof ( rdesc ) ;
144
- ev .u .create .bus = BUS_USB ;
145
- ev .u .create .vendor = 0x0001 ;
146
- ev .u .create .product = 0x0a37 ;
148
+ ev .u .create .rd_size = rdesc_size ;
149
+ ev .u .create .bus = bus ;
150
+ ev .u .create .vendor = vid ;
151
+ ev .u .create .product = pid ;
147
152
ev .u .create .version = 0 ;
148
153
ev .u .create .country = 0 ;
149
154
@@ -305,15 +310,17 @@ static int uhid_send_event(struct __test_metadata *_metadata, struct uhid_device
305
310
return uhid_write (_metadata , hid -> uhid_fd , & ev );
306
311
}
307
312
308
- static bool match_sysfs_device (int dev_id , const char * workdir , struct dirent * dir )
313
+ static bool match_sysfs_device (struct uhid_device * hid , const char * workdir , struct dirent * dir )
309
314
{
310
- const char * target = "0003:0001:0A37.* " ;
315
+ char target [ 20 ] = "" ;
311
316
char phys [512 ];
312
317
char uevent [1024 ];
313
318
char temp [512 ];
314
319
int fd , nread ;
315
320
bool found = false;
316
321
322
+ snprintf (target , sizeof (target ), "%04X:%04X:%04X.*" , hid -> bus , hid -> vid , hid -> pid );
323
+
317
324
if (fnmatch (target , dir -> d_name , 0 ))
318
325
return false;
319
326
@@ -324,7 +331,7 @@ static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *d
324
331
if (fd < 0 )
325
332
return false;
326
333
327
- sprintf (phys , "PHYS=%d" , dev_id );
334
+ sprintf (phys , "PHYS=%d" , hid -> dev_id );
328
335
329
336
nread = read (fd , temp , ARRAY_SIZE (temp ));
330
337
if (nread > 0 && (strstr (temp , phys )) != NULL )
@@ -335,7 +342,7 @@ static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *d
335
342
return found ;
336
343
}
337
344
338
- static int get_hid_id (int dev_id )
345
+ static int get_hid_id (struct uhid_device * hid )
339
346
{
340
347
const char * workdir = "/sys/devices/virtual/misc/uhid" ;
341
348
const char * str_id ;
@@ -350,10 +357,10 @@ static int get_hid_id(int dev_id)
350
357
d = opendir (workdir );
351
358
if (d ) {
352
359
while ((dir = readdir (d )) != NULL ) {
353
- if (!match_sysfs_device (dev_id , workdir , dir ))
360
+ if (!match_sysfs_device (hid , workdir , dir ))
354
361
continue ;
355
362
356
- str_id = dir -> d_name + sizeof ("0003:0001:0A37 ." );
363
+ str_id = dir -> d_name + sizeof ("0000:0000:0000 ." );
357
364
found = (int )strtol (str_id , NULL , 16 );
358
365
359
366
break ;
@@ -367,7 +374,7 @@ static int get_hid_id(int dev_id)
367
374
return found ;
368
375
}
369
376
370
- static int get_hidraw (int dev_id )
377
+ static int get_hidraw (struct uhid_device * hid )
371
378
{
372
379
const char * workdir = "/sys/devices/virtual/misc/uhid" ;
373
380
char sysfs [1024 ];
@@ -384,7 +391,7 @@ static int get_hidraw(int dev_id)
384
391
continue ;
385
392
386
393
while ((dir = readdir (d )) != NULL ) {
387
- if (!match_sysfs_device (dev_id , workdir , dir ))
394
+ if (!match_sysfs_device (hid , workdir , dir ))
388
395
continue ;
389
396
390
397
sprintf (sysfs , "%s/%s/hidraw" , workdir , dir -> d_name );
@@ -416,7 +423,7 @@ static int open_hidraw(struct uhid_device *hid)
416
423
int hidraw_number ;
417
424
char hidraw_path [64 ] = { 0 };
418
425
419
- hidraw_number = get_hidraw (hid -> dev_id );
426
+ hidraw_number = get_hidraw (hid );
420
427
if (hidraw_number < 0 )
421
428
return hidraw_number ;
422
429
@@ -425,7 +432,8 @@ static int open_hidraw(struct uhid_device *hid)
425
432
return open (hidraw_path , O_RDWR | O_NONBLOCK );
426
433
}
427
434
428
- static int setup_uhid (struct __test_metadata * _metadata , struct uhid_device * hid )
435
+ static int setup_uhid (struct __test_metadata * _metadata , struct uhid_device * hid ,
436
+ __u16 bus , __u32 vid , __u32 pid , const __u8 * rdesc , size_t rdesc_size )
429
437
{
430
438
const char * path = "/dev/uhid" ;
431
439
time_t t ;
@@ -435,19 +443,23 @@ static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid
435
443
srand ((unsigned int )time (& t ));
436
444
437
445
hid -> dev_id = rand () % 1024 ;
446
+ hid -> bus = bus ;
447
+ hid -> vid = vid ;
448
+ hid -> pid = pid ;
438
449
439
450
hid -> uhid_fd = open (path , O_RDWR | O_CLOEXEC );
440
451
ASSERT_GE (hid -> uhid_fd , 0 ) TH_LOG ("open uhid-cdev failed; %d" , hid -> uhid_fd );
441
452
442
- ret = uhid_create (_metadata , hid -> uhid_fd , hid -> dev_id );
453
+ ret = uhid_create (_metadata , hid -> uhid_fd , hid -> dev_id , bus , vid , pid ,
454
+ (__u8 * )rdesc , rdesc_size );
443
455
ASSERT_EQ (0 , ret ) {
444
456
TH_LOG ("create uhid device failed: %d" , ret );
445
457
close (hid -> uhid_fd );
446
458
return ret ;
447
459
}
448
460
449
461
/* locate the uevent file of the created device */
450
- hid -> hid_id = get_hid_id (hid -> dev_id );
462
+ hid -> hid_id = get_hid_id (hid );
451
463
ASSERT_GT (hid -> hid_id , 0 )
452
464
TH_LOG ("Could not locate uhid device id: %d" , hid -> hid_id );
453
465
0 commit comments