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