Skip to content

Commit 3952611

Browse files
Linux Build Service AccountGerrit - the friendly Code Review server
authored andcommitted
Merge "adbd: Don't close/reopen FFS ep0 on disconnect"
2 parents 0c7dcfd + 901a92f commit 3952611

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

adb/usb_linux_client.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,25 @@ static void init_functionfs(struct usb_handle *h)
313313
{
314314
ssize_t ret;
315315

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+
}
322323

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+
}
328329

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+
}
333335
}
334336

335337
h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
@@ -369,14 +371,14 @@ static void *usb_ffs_open_thread(void *x)
369371
while (1) {
370372
// wait until the USB device needs opening
371373
adb_mutex_lock(&usb->lock);
372-
while (usb->control != -1)
374+
while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1)
373375
adb_cond_wait(&usb->notify, &usb->lock);
374376
adb_mutex_unlock(&usb->lock);
375377

376378
while (1) {
377379
init_functionfs(usb);
378380

379-
if (usb->control >= 0)
381+
if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0)
380382
break;
381383

382384
adb_sleep_ms(1000);
@@ -473,10 +475,13 @@ static void usb_ffs_kick(usb_handle *h)
473475
D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
474476

475477
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.
477482
adb_close(h->bulk_out);
478483
adb_close(h->bulk_in);
479-
h->control = h->bulk_out = h->bulk_in = -1;
484+
h->bulk_out = h->bulk_in = -1;
480485

481486
// notify usb_ffs_open_thread that we are disconnected
482487
adb_cond_signal(&h->notify);

0 commit comments

Comments
 (0)