1From 02b6b6977d80af4b9b806054fadb5a06cedf011d Mon Sep 17 00:00:00 2001
2From: Etienne Cordonnier <ecordonnier@snap.com>
3Date: Tue, 14 Mar 2023 11:33:50 +0100
4Subject: [PATCH] Update usage of usbdevfs_urb to match new kernel UAPI
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9Linux kernel API has been changed by commit 94dfc73e7cf4 ("treewide: uapi: Replace zero-length arrays with flexible-array members")
10where zero-length array iso_frame_desc in struct usbdevfs_urb was replaced with a proper flexible-array member.
11
12Current USB API usage causes a compilation error at Linux 6.0:
13
14In file included from /home/mae/.cache/kiss/proc/121205/build/android-tools/vendor/adb/client/usb_linux.cpp:28:
15/usr/include/linux/usbdevice_fs.h:134:41: error: flexible array member ‘usbdevfs_urb::iso_frame_desc’ not at end of ‘struct usb_handle’
16  134 |         struct usbdevfs_iso_packet_desc iso_frame_desc[];
17      |                                         ^~~~~~~~~~~~~~
18/home/mae/.cache/kiss/proc/121205/build/android-tools/vendor/adb/client/usb_linux.cpp:76:18: note: next member ‘usbdevfs_urb usb_handle::urb_out’ declared here
19   76 |     usbdevfs_urb urb_out;
20      |                  ^~~~~~~
21/home/mae/.cache/kiss/proc/121205/build/android-tools/vendor/adb/client/usb_linux.cpp:61:8: note: in the definition of ‘struct usb_handle’
22   61 | struct usb_handle {
23      |        ^~~~~~~~~~
24
25Fix it by using pointers to a struct with flexible-array members.
26Current fix works both with the old and the new API.
27
28See https://github.com/nmeum/android-tools/issues/74 for more context.
29
30Tested: built on Linux against kernel 5.19 and 6.0; 'adb shell' over USB
31cable
32Acked-by: Gustavo A. R. Silva gustavoars@kernel.org
33Change-Id: I7f0f7b35d9a3ab980d3520b541b60c7857a6b101
34Signed-off-by: Anatol Pomozov <anatol.pomozov@gmail.com>
35
36[Backported on version 10]
37Signed-off-by: Etienne Cordonnier <ecordonnier@snap.com>
38
39---
40Upstream-Status: Pending
41
42 system/core/adb/client/usb_linux.cpp | 24 ++++++++++++++----------
43 1 file changed, 14 insertions(+), 10 deletions(-)
44
45diff --git a/system/core/adb/client/usb_linux.cpp b/system/core/adb/client/usb_linux.cpp
46index 343e7b59..6a4479f3 100644
47--- a/system/core/adb/client/usb_linux.cpp
48+++ b/system/core/adb/client/usb_linux.cpp
49@@ -71,8 +71,8 @@ struct usb_handle : public ::usb_handle {
50     unsigned zero_mask;
51     unsigned writeable = 1;
52
53-    usbdevfs_urb urb_in;
54-    usbdevfs_urb urb_out;
55+    usbdevfs_urb *urb_in;
56+    usbdevfs_urb *urb_out;
57
58     bool urb_in_busy = false;
59     bool urb_out_busy = false;
60@@ -305,7 +305,7 @@ static int usb_bulk_write(usb_handle* h, const void* data, int len) {
61     std::unique_lock<std::mutex> lock(h->mutex);
62     D("++ usb_bulk_write ++");
63
64-    usbdevfs_urb* urb = &h->urb_out;
65+    usbdevfs_urb* urb = h->urb_out;
66     memset(urb, 0, sizeof(*urb));
67     urb->type = USBDEVFS_URB_TYPE_BULK;
68     urb->endpoint = h->ep_out;
69@@ -344,7 +344,7 @@ static int usb_bulk_read(usb_handle* h, void* data, int len) {
70     std::unique_lock<std::mutex> lock(h->mutex);
71     D("++ usb_bulk_read ++");
72
73-    usbdevfs_urb* urb = &h->urb_in;
74+    usbdevfs_urb* urb = h->urb_in;
75     memset(urb, 0, sizeof(*urb));
76     urb->type = USBDEVFS_URB_TYPE_BULK;
77     urb->endpoint = h->ep_in;
78@@ -389,7 +389,7 @@ static int usb_bulk_read(usb_handle* h, void* data, int len) {
79         }
80         D("[ urb @%p status = %d, actual = %d ]", out, out->status, out->actual_length);
81
82-        if (out == &h->urb_in) {
83+        if (out == h->urb_in) {
84             D("[ reap urb - IN complete ]");
85             h->urb_in_busy = false;
86             if (urb->status != 0) {
87@@ -398,7 +398,7 @@ static int usb_bulk_read(usb_handle* h, void* data, int len) {
88             }
89             return urb->actual_length;
90         }
91-        if (out == &h->urb_out) {
92+        if (out == h->urb_out) {
93             D("[ reap urb - OUT compelete ]");
94             h->urb_out_busy = false;
95             h->cv.notify_all();
96@@ -502,10 +502,10 @@ void usb_kick(usb_handle* h) {
97             ** but this ensures that a reader blocked on REAPURB
98             ** will get unblocked
99             */
100-            ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in);
101-            ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out);
102-            h->urb_in.status = -ENODEV;
103-            h->urb_out.status = -ENODEV;
104+            ioctl(h->fd, USBDEVFS_DISCARDURB, h->urb_in);
105+            ioctl(h->fd, USBDEVFS_DISCARDURB, h->urb_out);
106+            h->urb_in->status = -ENODEV;
107+            h->urb_out->status = -ENODEV;
108             h->urb_in_busy = false;
109             h->urb_out_busy = false;
110             h->cv.notify_all();
111@@ -521,6 +521,8 @@ int usb_close(usb_handle* h) {
112
113     D("-- usb close %p (fd = %d) --", h, h->fd);
114
115+    delete h->urb_in;
116+    delete h->urb_out;
117     delete h;
118
119     return 0;
120@@ -556,6 +558,8 @@ static void register_device(const char* dev_name, const char* dev_path, unsigned
121     usb->ep_out = ep_out;
122     usb->zero_mask = zero_mask;
123     usb->max_packet_size = max_packet_size;
124+    usb->urb_in = new usbdevfs_urb;
125+    usb->urb_out = new usbdevfs_urb;
126
127     // Initialize mark so we don't get garbage collected after the device scan.
128     usb->mark = true;
129