Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT"
[dependencies]
config = "0.13.3"
hidapi = "1.4.1-3"
rusb = "0.9.4"
serde = "1.0.171"
serde_derive = "1.0.174"
serde_json = "1.0.103"
114 changes: 114 additions & 0 deletions src/devices/control_transfer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use rusb::{Context, DeviceHandle, UsbContext};
use std::{thread, time::Duration};

const VENDOR_REQUEST_TYPE: u8 = 0x40;
const VENDOR_REQUEST: u8 = 128;
const TIMEOUT_MS: u64 = 1000;
const INTER_CMD_DELAY_MS: u64 = 100;

pub struct VendorDevice {
handle: DeviceHandle<Context>,
}

impl VendorDevice {
pub fn open(vid: u16, pid: u16, serial: &str) -> Result<Self, rusb::Error> {
let context = Context::new()?;

for device in context.devices()?.iter() {
let device_desc = device.device_descriptor()?;

if device_desc.vendor_id() == vid && device_desc.product_id() == pid {
let handle = device.open()?;

if let Ok(sn) = handle.read_serial_number_string_ascii(&device_desc) {
if sn == serial {
handle.set_auto_detach_kernel_driver(true).ok();

if let Err(e) = handle.claim_interface(0) {
eprintln!("Failed to claim interface 0: {:?}", e);
return Err(e);
}

return Ok(VendorDevice { handle });
}
}
}
}

Err(rusb::Error::NoDevice)
}

pub fn setup_channel(&self, channel_id: u8) -> Result<(), rusb::Error> {
let channel_mask: u16 = 0x10 << channel_id;

let mut payload = [0u8; 16];
payload[2] = (channel_mask & 0xFF) as u8;
payload[3] = ((channel_mask >> 8) & 0xFF) as u8;
payload[15] = 0x01;

self.handle.write_control(
VENDOR_REQUEST_TYPE,
VENDOR_REQUEST,
0,
0xe020,
&payload,
Duration::from_millis(TIMEOUT_MS)
)?;

Ok(())
}

pub fn set_channel_speed(&self, channel_id: u8, rpm: u16) -> Result<(), rusb::Error> {
let windex: u16 = 0xd8a0 + (channel_id as u16 * 2);

let payload: [u8; 2] = [
(rpm & 0xFF) as u8,
((rpm >> 8) & 0xFF) as u8
];

self.handle.write_control(
VENDOR_REQUEST_TYPE,
VENDOR_REQUEST,
0,
windex,
&payload,
Duration::from_millis(TIMEOUT_MS)
)?;

Ok(())
}

pub fn commit_channel(&self, channel_id: u8) -> Result<(), rusb::Error> {
let windex: u16 = 0xd890 + channel_id as u16;
let payload: [u8; 1] = [0x01];

self.handle.write_control(
VENDOR_REQUEST_TYPE,
VENDOR_REQUEST,
0,
windex,
&payload,
Duration::from_millis(TIMEOUT_MS)
)?;

Ok(())
}

pub fn set_speed_with_delays(&self, channel_id: u8, rpm: u16) -> Result<(), rusb::Error> {
self.setup_channel(channel_id)?;
thread::sleep(Duration::from_millis(INTER_CMD_DELAY_MS));

self.set_channel_speed(channel_id, rpm)?;
thread::sleep(Duration::from_millis(INTER_CMD_DELAY_MS));

self.commit_channel(channel_id)?;

Ok(())
}
}

impl Drop for VendorDevice {
fn drop(&mut self) {
let _ = self.handle.release_interface(0);
}
}
159 changes: 118 additions & 41 deletions src/devices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use serde_derive::{Deserialize, Serialize};
use hidapi::{self, HidDevice};
use std::{thread, time};

mod control_transfer;

#[derive(Serialize, Deserialize, Clone)]
pub struct Configs {
pub configs: Vec<Config>
Expand All @@ -23,6 +25,36 @@ pub struct Channel {
const VENDOR_IDS: [u16; 1] = [ 0x0cf2 ];
const PRODUCT_IDS: [u16; 7] = [ 0x7750, 0xa100, 0xa101, 0xa102, 0xa103, 0xa104, 0xa105 ];

const V1_2_PRODUCT_IDS: [u16; 2] = [ 0x7750, 0xa100 ];

enum TransportMode {
HID,
VendorControl,
}

fn percent_to_rpm(pct: u8, product_id: u16) -> u16 {
let pct = pct.min(100) as u32;

let (min_rpm, max_rpm) = match product_id {
0xa100 => (500u32, 1500u32),
0x7750 => (800u32, 1900u32),
0xa101 => (800u32, 1900u32),
0xa102 => (200u32, 2100u32),
0xa103 | 0xa105 | 0xa104 => (250u32, 2000u32),
_ => (800u32, 1900u32),
};

(min_rpm + ((max_rpm - min_rpm) * pct / 100)) as u16
}

fn detect_transport_mode(product_id: u16) -> TransportMode {
if V1_2_PRODUCT_IDS.contains(&product_id) {
TransportMode::VendorControl
} else {
TransportMode::HID
}
}

pub fn run(mut existing_configs: Configs) -> Configs {

let mut default_channels: Vec<Channel> = Vec::new();
Expand All @@ -40,7 +72,6 @@ pub fn run(mut existing_configs: Configs) -> Configs {
};

for hiddevice in api.device_list() {

if VENDOR_IDS.contains(&hiddevice.vendor_id()) && PRODUCT_IDS.contains(&hiddevice.product_id()) {

let serial_number: &str = match hiddevice.serial_number() {
Expand Down Expand Up @@ -96,56 +127,102 @@ pub fn run(mut existing_configs: Configs) -> Configs {
thread::sleep(time::Duration::from_millis(200));


for x in 0..channels.len() {

// Disable Sync to fan header
let mut channel_byte = 0x10 << x;

if channels[x].mode == "PWM" {
channel_byte = channel_byte | 0x1 << x;
let transport_mode = detect_transport_mode(hiddevice.product_id());

match transport_mode {
TransportMode::VendorControl => {
apply_vendor_control_settings(
hiddevice.vendor_id(),
hiddevice.product_id(),
serial_number,
&channels,
&hid,
&hiddevice
);
}
TransportMode::HID => {
println!("Using HID mode for device");
apply_hid_settings(&hid, &hiddevice, &channels);
}
}
}
}
return existing_configs;
}

let _ = match &hiddevice.product_id() {
0xa100|0x7750 => hid.write(&[224, 16, 49, channel_byte]), // SL
0xa101 => hid.write(&[224, 16, 66, channel_byte]), // AL
0xa102 => hid.write(&[224, 16, 98, channel_byte]), // SLI
0xa103|0xa105 => hid.write(&[224, 16, 98, channel_byte]), // SLv2
0xa104 => hid.write(&[224, 16, 98, channel_byte]), // ALv2
_ => hid.write(&[224, 16, 49, channel_byte]), // SL
};
fn apply_vendor_control_settings(
vendor_id: u16,
product_id: u16,
serial_number: &str,
channels: &Vec<Channel>,
hid: &HidDevice,
hiddevice: &hidapi::DeviceInfo
) {
println!("Using VendorControl mode for device");

if let Ok(vendor_device) = control_transfer::VendorDevice::open(
vendor_id,
product_id,
serial_number
) {
for x in 0..channels.len() {
if channels[x].mode == "Manual" {
let speed_pct = channels[x].speed.min(100) as u8;
let rpm = percent_to_rpm(speed_pct, product_id);

if let Err(e) = vendor_device.set_speed_with_delays(x as u8, rpm) {
eprintln!("Failed to set channel {} speed: {:?}", x, e);
} else {
println!("Set channel {} to {}% ({} RPM)", x, speed_pct, rpm);
}
}
}
} else {
eprintln!("Failed to open device via VendorControl, falling back to HID");
apply_hid_settings(hid, hiddevice, channels);
}
}

// Avoid Race Condition
thread::sleep(time::Duration::from_millis(200));
fn apply_hid_settings(hid: &HidDevice, hiddevice: &hidapi::DeviceInfo, channels: &Vec<Channel>) {
for x in 0..channels.len() {
let mut channel_byte = 0x10 << x;

// Set Channel Speed
if channels[x].mode == "Manual" {
if channels[x].mode == "PWM" {
channel_byte = channel_byte | 0x1 << x;
}

let mut speed = channels[x].speed as f64;
if speed > 100.0 { speed = 100.0 }
let _ = match &hiddevice.product_id() {
0xa100|0x7750 => hid.write(&[224, 16, 49, channel_byte]), // SL
0xa101 => hid.write(&[224, 16, 66, channel_byte]), // AL
0xa102 => hid.write(&[224, 16, 98, channel_byte]), // SLI
0xa103|0xa105 => hid.write(&[224, 16, 98, channel_byte]), // SLv2
0xa104 => hid.write(&[224, 16, 98, channel_byte]), // ALv2
_ => hid.write(&[224, 16, 49, channel_byte]), // SL
};

let speed_800_1900: u8 = ((800.0 + (11.0 * speed)) as usize / 19).try_into().unwrap();
let speed_250_2000: u8 = ((250.0 + (17.5 * speed)) as usize / 20).try_into().unwrap();
let speed_200_2100: u8 = ((200.0 + (19.0 * speed)) as usize / 21).try_into().unwrap();
// Avoid Race Condition
thread::sleep(time::Duration::from_millis(200));

let _ = match &hiddevice.product_id() {
0xa100|0x7750 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_800_1900]), // SL
0xa101 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_800_1900]), // AL
0xa102 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_200_2100]), // SLI
0xa103|0xa105 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_250_2000]), // SLv2
0xa104 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_250_2000]), // ALv2
_ => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_800_1900]), // SL
};
// Set Channel Speed
if channels[x].mode == "Manual" {
let mut speed = channels[x].speed as f64;
if speed > 100.0 { speed = 100.0 }

// Avoid Race Condition
thread::sleep(time::Duration::from_millis(100));
let speed_800_1900: u8 = ((800.0 + (11.0 * speed)) as usize / 19).try_into().unwrap();
let speed_250_2000: u8 = ((250.0 + (17.5 * speed)) as usize / 20).try_into().unwrap();
let speed_200_2100: u8 = ((200.0 + (19.0 * speed)) as usize / 21).try_into().unwrap();

}
}
let _ = match &hiddevice.product_id() {
0xa100|0x7750 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_800_1900]), // SL
0xa101 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_800_1900]), // AL
0xa102 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_200_2100]), // SLI
0xa103|0xa105 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_250_2000]), // SLv2
0xa104 => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_250_2000]), // ALv2
_ => hid.write(&[224, (x+32).try_into().unwrap(), 0, speed_800_1900]), // SL
};

// Avoid Race Condition
thread::sleep(time::Duration::from_millis(100));
}

}

return existing_configs;

}