diff options
| author | Yuval Adam <_@yuv.al> | 2018-07-26 23:35:53 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2018-07-26 23:35:53 +0300 |
| commit | 0435cc777042f4800a7cdeb8dc33bff3ecafda10 (patch) | |
| tree | ba27da743e38d17e509702e3915ef7725928f78b /src | |
| parent | dd597a9208675f7d8c9de9dfc3110014d6105ce1 (diff) | |
Successfully perform a dummy write
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 51 |
1 files changed, 48 insertions, 3 deletions
@@ -1,5 +1,13 @@ extern crate libusb; +use std::time::Duration; +use libusb::{Direction, RequestType, Recipient}; + +const BLOCK_USBB: u16 = 1; +const ADDR_USB_SYSCTL: u16 = 0x2000; + +const INTERFACE_ID: u8 = 0; +const CTRL_TIMEOUT: Duration = Duration::from_millis(300); const KNOWN_DEVICES: [(u16, u16, &str); 2] = [ (0x0bda, 0x2832, "Generic RTL2832U"), (0x0bda, 0x2838, "Generic RTL2832U OEM") @@ -7,7 +15,9 @@ const KNOWN_DEVICES: [(u16, u16, &str); 2] = [ pub struct RtlSdr<'a> { ctx: &'a libusb::Context, - dev: Option<libusb::DeviceHandle<'a>> + dev: Option<libusb::DeviceHandle<'a>>, + iface_id: u8, + iface: Option<libusb::Interface<'a>> } impl<'a> RtlSdr<'a> { @@ -15,7 +25,9 @@ impl<'a> RtlSdr<'a> { pub fn new(ctx: &'a libusb::Context) -> RtlSdr<'a> { RtlSdr { ctx, - dev: None + dev: None, + iface_id: INTERFACE_ID, + iface: None } } @@ -23,6 +35,20 @@ impl<'a> RtlSdr<'a> { self.dev = self.find_device(); } + fn write_reg(&self, handle: &libusb::DeviceHandle, block: u16, addr: u16, val: u8, len: u8) -> usize { + let vendor_out = libusb::request_type(Direction::Out, RequestType::Vendor, Recipient::Device); + let mut data: [u8; 2] = [0, 0]; + let index: u16 = (block << 8) | 0x10; + + data[0] = val; + data[1] = val; + + match handle.write_control(vendor_out, 0, addr, index, &data, CTRL_TIMEOUT) { + Ok(n) => n, + Err(_) => 0 + } + } + pub fn find_device(&self) -> Option<libusb::DeviceHandle<'a>> { for mut dev in self.ctx.devices().unwrap().iter() { let desc = dev.device_descriptor().unwrap(); @@ -31,7 +57,26 @@ impl<'a> RtlSdr<'a> { for kd in KNOWN_DEVICES.iter() { if kd.0 == vid && kd.1 == pid { - return Some(dev.open().unwrap()) + let mut handle = dev.open().unwrap(); + + let has_kernel_driver = match handle.kernel_driver_active(self.iface_id) { + Ok(true) => { + handle.detach_kernel_driver(self.iface_id).ok(); + true + }, + _ => false + }; + + let iface = handle.claim_interface(self.iface_id).unwrap(); + + let res = self.write_reg(&handle, BLOCK_USBB, ADDR_USB_SYSCTL, 0x09, 1); + println!("Got {}", res); + + if has_kernel_driver { + handle.attach_kernel_driver(self.iface_id).ok(); + } + + return Some(handle) } } } |
