-
Notifications
You must be signed in to change notification settings - Fork 0
Automatically detach ftdi_sio driver #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+125
−19
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright lowRISC contributors. | ||
| // Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include "libusb.hh" | ||
|
|
||
| #include <libudev.h> | ||
| #include <libusb-1.0/libusb.h> | ||
|
|
||
| #include <iostream> | ||
|
|
||
| namespace ftdi { | ||
|
|
||
| enum { kFtdiVid = 0x0403 }; | ||
|
|
||
| // Use libusb to temporarily detach ftdi_sio from every interface of | ||
| // every FTDI USB device. The kernel driver is re-attached automatically when | ||
| // the D2XX / libft4222 handle is closed. | ||
| void detach_ftdi_sio(uint16_t pid) { | ||
| libusb_context* ctx = nullptr; | ||
| if (libusb_init(&ctx) != LIBUSB_SUCCESS) { | ||
| std::cerr << "libusb_init failed\n"; | ||
| return; | ||
| } | ||
|
|
||
| libusb_device** devs = nullptr; | ||
| ssize_t cnt = libusb_get_device_list(ctx, &devs); | ||
| if (cnt < 0) { | ||
| libusb_exit(ctx); | ||
| return; | ||
| } | ||
|
|
||
| for (ssize_t i = 0; i < cnt; i++) { | ||
| libusb_device_descriptor desc{}; | ||
| if (libusb_get_device_descriptor(devs[i], &desc) != LIBUSB_SUCCESS) continue; | ||
| if (desc.idVendor != kFtdiVid || desc.idProduct != pid) continue; | ||
|
|
||
| libusb_device_handle* handle = nullptr; | ||
| if (libusb_open(devs[i], &handle) != LIBUSB_SUCCESS) continue; | ||
|
|
||
| libusb_config_descriptor* config = nullptr; | ||
| if (libusb_get_active_config_descriptor(devs[i], &config) == LIBUSB_SUCCESS) { | ||
| for (uint8_t j = 0; j < config->bNumInterfaces; j++) { | ||
| if (libusb_kernel_driver_active(handle, j) != 1) continue; | ||
| int ret = libusb_detach_kernel_driver(handle, j); | ||
| if (ret != LIBUSB_SUCCESS) { | ||
| std::cerr << "Failed to detach kernel driver on interface " << (int)j << ": " | ||
| << libusb_error_name(ret) << "\n"; | ||
| } | ||
| } | ||
| libusb_free_config_descriptor(config); | ||
| } | ||
|
|
||
| libusb_close(handle); | ||
| } | ||
|
|
||
| libusb_free_device_list(devs, 1); | ||
| libusb_exit(ctx); | ||
| } | ||
|
|
||
| } // namespace ftdi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright lowRISC contributors. | ||
| // Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <format> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace ftdi { | ||
|
|
||
| // Detach ftdi_sio (or any kernel driver) from all FTDI USB interfaces | ||
| // using libusb. This allows the D2XX library to claim the device afterwards. | ||
| // Requires write access to the USB device files in the udev rules. | ||
| void detach_ftdi_sio(uint16_t pid); | ||
|
|
||
| } // namespace ftdi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to default to the FT2232H/D, but should this default pid be behind a named constant?