@@ -313,23 +313,25 @@ static void init_functionfs(struct usb_handle *h)
313
313
{
314
314
ssize_t ret ;
315
315
316
- D ("OPENING %s\n" , USB_FFS_ADB_EP0 );
317
- h -> control = adb_open (USB_FFS_ADB_EP0 , O_RDWR );
318
- if (h -> control < 0 ) {
319
- D ("[ %s: cannot open control endpoint: errno=%d]\n" , USB_FFS_ADB_EP0 , errno );
320
- goto err ;
321
- }
316
+ if (h -> control < 0 ) { // might have already done this before
317
+ D ("OPENING %s\n" , USB_FFS_ADB_EP0 );
318
+ h -> control = adb_open (USB_FFS_ADB_EP0 , O_RDWR );
319
+ if (h -> control < 0 ) {
320
+ D ("[ %s: cannot open control endpoint: errno=%d]\n" , USB_FFS_ADB_EP0 , errno );
321
+ goto err ;
322
+ }
322
323
323
- ret = adb_write (h -> control , & descriptors , sizeof (descriptors ));
324
- if (ret < 0 ) {
325
- D ("[ %s: write descriptors failed: errno=%d ]\n" , USB_FFS_ADB_EP0 , errno );
326
- goto err ;
327
- }
324
+ ret = adb_write (h -> control , & descriptors , sizeof (descriptors ));
325
+ if (ret < 0 ) {
326
+ D ("[ %s: write descriptors failed: errno=%d ]\n" , USB_FFS_ADB_EP0 , errno );
327
+ goto err ;
328
+ }
328
329
329
- ret = adb_write (h -> control , & strings , sizeof (strings ));
330
- if (ret < 0 ) {
331
- D ("[ %s: writing strings failed: errno=%d]\n" , USB_FFS_ADB_EP0 , errno );
332
- goto err ;
330
+ ret = adb_write (h -> control , & strings , sizeof (strings ));
331
+ if (ret < 0 ) {
332
+ D ("[ %s: writing strings failed: errno=%d]\n" , USB_FFS_ADB_EP0 , errno );
333
+ goto err ;
334
+ }
333
335
}
334
336
335
337
h -> bulk_out = adb_open (USB_FFS_ADB_OUT , O_RDWR );
@@ -369,14 +371,14 @@ static void *usb_ffs_open_thread(void *x)
369
371
while (1 ) {
370
372
// wait until the USB device needs opening
371
373
adb_mutex_lock (& usb -> lock );
372
- while (usb -> control != -1 )
374
+ while (usb -> control != -1 && usb -> bulk_in != -1 && usb -> bulk_out != -1 )
373
375
adb_cond_wait (& usb -> notify , & usb -> lock );
374
376
adb_mutex_unlock (& usb -> lock );
375
377
376
378
while (1 ) {
377
379
init_functionfs (usb );
378
380
379
- if (usb -> control >= 0 )
381
+ if (usb -> control >= 0 && usb -> bulk_in >= 0 && usb -> bulk_out >= 0 )
380
382
break ;
381
383
382
384
adb_sleep_ms (1000 );
@@ -473,10 +475,13 @@ static void usb_ffs_kick(usb_handle *h)
473
475
D ("[ kick: sink (fd=%d) clear halt failed (%d) ]" , h -> bulk_out , errno );
474
476
475
477
adb_mutex_lock (& h -> lock );
476
- adb_close (h -> control );
478
+
479
+ // don't close ep0 here, since we may not need to reinitialize it with
480
+ // the same descriptors again. if however ep1/ep2 fail to re-open in
481
+ // init_functionfs, only then would we close and open ep0 again.
477
482
adb_close (h -> bulk_out );
478
483
adb_close (h -> bulk_in );
479
- h -> control = h -> bulk_out = h -> bulk_in = -1 ;
484
+ h -> bulk_out = h -> bulk_in = -1 ;
480
485
481
486
// notify usb_ffs_open_thread that we are disconnected
482
487
adb_cond_signal (& h -> notify );
0 commit comments