diff options
author | Nikolai Kondrashov <spbnick@gmail.com> | 2010-07-05 13:10:41 +0000 |
---|---|---|
committer | Nikolai Kondrashov <spbnick@gmail.com> | 2010-07-05 13:10:41 +0000 |
commit | 872079ec9051bb2b86ec19a5e5a290b40be3b89f (patch) | |
tree | 737a2b25b32f682fa6caef32c4c11e63cc8f00be | |
parent | 8d2a9a44388f168da91401beab36bc51cebc5e23 (diff) | |
download | usbhid-dump-872079ec9051bb2b86ec19a5e5a290b40be3b89f.tar.gz |
Added interface claiming/releasing.
-rw-r--r-- | include/hid_dump/iface.h | 21 | ||||
-rw-r--r-- | lib/iface.c | 43 | ||||
-rw-r--r-- | src/hid-dump.c | 14 |
3 files changed, 74 insertions, 4 deletions
diff --git a/include/hid_dump/iface.h b/include/hid_dump/iface.h index 54b3b00..979928a 100644 --- a/include/hid_dump/iface.h +++ b/include/hid_dump/iface.h @@ -43,6 +43,8 @@ struct hid_dump_iface { bool detached; /**< True if the interface was detached from the kernel driver, false otherwise */ + bool claimed; /**< True if the interface was + claimed */ }; extern bool hid_dump_iface_valid(const hid_dump_iface *iface); @@ -89,7 +91,6 @@ extern hid_dump_iface *hid_dump_iface_list_fltr_by_num( hid_dump_iface *list, int number); - /** * Detach all interfaces in a list from their kernel drivers (if any). * @@ -109,6 +110,24 @@ extern enum libusb_error hid_dump_iface_list_detach(hid_dump_iface *list); */ extern enum libusb_error hid_dump_iface_list_attach(hid_dump_iface *list); +/** + * Claim all interfaces in a list. + * + * @param list The list of interfaces to claim. + * + * @return Libusb error code. + */ +extern enum libusb_error hid_dump_iface_list_claim(hid_dump_iface *list); + +/** + * Release all interfaces in a list (if were claimed before). + * + * @param list The list of interfaces to release. + * + * @return Libusb error code. + */ +extern enum libusb_error hid_dump_iface_list_release(hid_dump_iface *list); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/lib/iface.c b/lib/iface.c index 27718a4..54a34db 100644 --- a/lib/iface.c +++ b/lib/iface.c @@ -203,8 +203,49 @@ hid_dump_iface_list_attach(hid_dump_iface *list) if (list->detached) { err = libusb_attach_kernel_driver(list->handle, list->number); - if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_NOT_FOUND) + if (err != LIBUSB_SUCCESS) return err; + list->detached = false; + } + + return LIBUSB_SUCCESS; +} + + +enum libusb_error +hid_dump_iface_list_claim(hid_dump_iface *list) +{ + enum libusb_error err; + + assert(hid_dump_iface_list_valid(list)); + + for (; list != NULL; list = list->next) + { + err = libusb_claim_interface(list->handle, list->number); + if (err != LIBUSB_SUCCESS) + return err; + + list->claimed = true; + } + + return LIBUSB_SUCCESS; +} + + +enum libusb_error +hid_dump_iface_list_release(hid_dump_iface *list) +{ + enum libusb_error err; + + assert(hid_dump_iface_list_valid(list)); + + for (; list != NULL; list = list->next) + if (list->claimed) + { + err = libusb_release_interface(list->handle, list->number); + if (err != LIBUSB_SUCCESS) + return err; + list->claimed = false; } return LIBUSB_SUCCESS; diff --git a/src/hid-dump.c b/src/hid-dump.c index 0b9206f..34ecb9a 100644 --- a/src/hid-dump.c +++ b/src/hid-dump.c @@ -142,8 +142,13 @@ run(bool dump_descriptor, /* Detach interfaces */ err = hid_dump_iface_list_detach(iface_list); if (err != LIBUSB_SUCCESS) - LIBUSB_FAILURE_CLEANUP("detach interface(s) from " + LIBUSB_FAILURE_CLEANUP("detach the interface(s) from " "the kernel drivers"); + + /* Claim interfaces */ + err = hid_dump_iface_list_claim(iface_list); + if (err != LIBUSB_SUCCESS) + LIBUSB_FAILURE_CLEANUP("claim the interface(s)"); #if 0 /* Run with the handle */ result = run_handle(dump_descriptor, dump_stream, handle, if_num); @@ -156,10 +161,15 @@ run(bool dump_descriptor, cleanup: + /* Release the interfaces back */ + err = hid_dump_iface_list_release(iface_list); + if (err != LIBUSB_SUCCESS) + LIBUSB_FAILURE("release the interface(s)"); + /* Attach interfaces back */ err = hid_dump_iface_list_attach(iface_list); if (err != LIBUSB_SUCCESS) - LIBUSB_FAILURE("attach interface(s) to the kernel drivers"); + LIBUSB_FAILURE("attach the interface(s) to the kernel drivers"); /* Free the device */ if (handle != NULL) |