Skip to content

Commit 456e7dc

Browse files
Merge pull request #82 from FrameworkComputer/tablet-mode
Add command to toggle tablet mode
2 parents d58a758 + 7eb8d79 commit 456e7dc

File tree

6 files changed

+74
-2
lines changed

6 files changed

+74
-2
lines changed

framework_lib/src/chromium_ec/command.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum EcCommands {
3333
PwmSetFanDuty = 0x0024,
3434
PwmSetDuty = 0x0025,
3535
PwmGetDuty = 0x0026,
36+
SetTabletMode = 0x0031,
3637
GpioGet = 0x93,
3738
I2cPassthrough = 0x9e,
3839
ConsoleSnapshot = 0x97,

framework_lib/src/chromium_ec/commands.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,24 @@ impl EcRequest<EcResponsePwmGetDuty> for EcRequestPwmGetDuty {
225225
}
226226
}
227227

228+
pub enum TabletModeOverride {
229+
Default = 0,
230+
ForceTablet = 1,
231+
ForceClamshell = 2,
232+
}
233+
234+
#[repr(C, packed)]
235+
pub struct EcRequestSetTabletMode {
236+
/// See TabletModeOverride
237+
pub mode: u8,
238+
}
239+
240+
impl EcRequest<()> for EcRequestSetTabletMode {
241+
fn command_id() -> EcCommands {
242+
EcCommands::SetTabletMode
243+
}
244+
}
245+
228246
#[repr(C, packed)]
229247
pub struct EcRequestGpioGetV0 {
230248
pub name: [u8; 32],

framework_lib/src/chromium_ec/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,6 @@ impl CrosEc {
387387
}
388388

389389
/// Check the current brightness of the keyboard backlight
390-
///
391390
pub fn get_keyboard_backlight(&self) -> EcResult<u8> {
392391
let kblight = EcRequestPwmGetDuty {
393392
pwm_type: PwmType::KbLight as u8,
@@ -398,6 +397,13 @@ impl CrosEc {
398397
Ok((kblight.duty / (PWM_MAX_DUTY / 100)) as u8)
399398
}
400399

400+
/// Set tablet mode
401+
pub fn set_tablet_mode(&self, mode: TabletModeOverride) {
402+
let mode = mode as u8;
403+
let res = EcRequestSetTabletMode { mode }.send_command(self);
404+
print_err(res);
405+
}
406+
401407
/// Overwrite RO and RW regions of EC flash
402408
/// MEC/Legacy EC
403409
/// | Start | End | Size | Region |

framework_lib/src/commandline/clap_std.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use clap::Parser;
66
use crate::chromium_ec::CrosEcDriverType;
77
use crate::commandline::{
88
Cli, ConsoleArg, FpBrightnessArg, HardwareDeviceType, InputDeckModeArg, RebootEcArg,
9+
TabletModeArg,
910
};
1011

1112
/// Swiss army knife for Framework laptops
@@ -144,6 +145,11 @@ struct ClapCli {
144145
#[arg(long)]
145146
kblight: Option<Option<u8>>,
146147

148+
/// Set tablet mode override
149+
#[clap(value_enum)]
150+
#[arg(long)]
151+
tablet_mode: Option<TabletModeArg>,
152+
147153
/// Get EC console, choose whether recent or to follow the output
148154
#[clap(value_enum)]
149155
#[arg(long)]
@@ -263,6 +269,7 @@ pub fn parse(args: &[String]) -> Cli {
263269
get_gpio: args.get_gpio,
264270
fp_brightness: args.fp_brightness,
265271
kblight: args.kblight,
272+
tablet_mode: args.tablet_mode,
266273
console: args.console,
267274
reboot_ec: args.reboot_ec,
268275
hash: args.hash.map(|x| x.into_os_string().into_string().unwrap()),

framework_lib/src/commandline/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::chromium_ec;
3535
use crate::chromium_ec::commands::DeckStateMode;
3636
use crate::chromium_ec::commands::FpLedBrightnessLevel;
3737
use crate::chromium_ec::commands::RebootEcCmd;
38+
use crate::chromium_ec::commands::TabletModeOverride;
3839
use crate::chromium_ec::EcResponseStatus;
3940
use crate::chromium_ec::{print_err, EcFlashType};
4041
use crate::chromium_ec::{EcError, EcResult};
@@ -61,6 +62,14 @@ use crate::chromium_ec::{CrosEc, CrosEcDriverType, HardwareDeviceType};
6162
#[cfg(feature = "uefi")]
6263
use core::prelude::rust_2021::derive;
6364

65+
#[cfg_attr(not(feature = "uefi"), derive(clap::ValueEnum))]
66+
#[derive(Clone, Debug, PartialEq)]
67+
pub enum TabletModeArg {
68+
Auto,
69+
Tablet,
70+
Laptop,
71+
}
72+
6473
#[cfg_attr(not(feature = "uefi"), derive(clap::ValueEnum))]
6574
#[derive(Clone, Debug, PartialEq)]
6675
pub enum ConsoleArg {
@@ -152,6 +161,7 @@ pub struct Cli {
152161
pub get_gpio: Option<String>,
153162
pub fp_brightness: Option<Option<FpBrightnessArg>>,
154163
pub kblight: Option<Option<u8>>,
164+
pub tablet_mode: Option<TabletModeArg>,
155165
pub console: Option<ConsoleArg>,
156166
pub reboot_ec: Option<RebootEcArg>,
157167
pub hash: Option<String>,
@@ -743,6 +753,13 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
743753
} else {
744754
println!("Unable to tell");
745755
}
756+
} else if let Some(tablet_arg) = &args.tablet_mode {
757+
let mode = match tablet_arg {
758+
TabletModeArg::Auto => TabletModeOverride::Default,
759+
TabletModeArg::Tablet => TabletModeOverride::ForceTablet,
760+
TabletModeArg::Laptop => TabletModeOverride::ForceClamshell,
761+
};
762+
ec.set_tablet_mode(mode);
746763
} else if let Some(console_arg) = &args.console {
747764
match console_arg {
748765
ConsoleArg::Follow => {

framework_lib/src/commandline/uefi.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use uefi::Identify;
1212
use crate::chromium_ec::{CrosEcDriverType, HardwareDeviceType};
1313
use crate::commandline::Cli;
1414

15-
use super::{ConsoleArg, FpBrightnessArg, InputDeckModeArg, RebootEcArg};
15+
use super::{ConsoleArg, FpBrightnessArg, InputDeckModeArg, RebootEcArg, TabletModeArg};
1616

1717
/// Get commandline arguments from UEFI environment
1818
pub fn get_args(boot_services: &BootServices) -> Vec<String> {
@@ -86,6 +86,7 @@ pub fn parse(args: &[String]) -> Cli {
8686
get_gpio: None,
8787
fp_brightness: None,
8888
kblight: None,
89+
tablet_mode: None,
8990
console: None,
9091
reboot_ec: None,
9192
hash: None,
@@ -214,6 +215,28 @@ pub fn parse(args: &[String]) -> Cli {
214215
Some(None)
215216
};
216217
found_an_option = true;
218+
} else if arg == "--tablet-mode" {
219+
cli.tablet_mode = if args.len() > i + 1 {
220+
let tablet_mode_arg = &args[i + 1];
221+
if tablet_mode_arg == "auto" {
222+
Some(TabletModeArg::Auto)
223+
} else if tablet_mode_arg == "tablet" {
224+
Some(TabletModeArg::Tablet)
225+
} else if tablet_mode_arg == "laptop" {
226+
Some(TabletModeArg::Laptop)
227+
} else {
228+
println!(
229+
"Need to provide a value for --tablet-mode: '{}'. {}",
230+
args[i + 1],
231+
"Must be one of: `auto`, `tablet` or `laptop`",
232+
);
233+
None
234+
}
235+
} else {
236+
println!("Need to provide a value for --tablet-mode. One of: `auto`, `tablet` or `laptop`");
237+
None
238+
};
239+
found_an_option = true;
217240
} else if arg == "--fp-brightness" {
218241
cli.fp_brightness = if args.len() > i + 1 {
219242
let fp_brightness_arg = &args[i + 1];

0 commit comments

Comments
 (0)