1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.
//! Provides userspace applications with a driver interface to asynchronous GPIO
//! pins.
//!
//! Async GPIO pins are pins that exist on something like a GPIO extender or a
//! radio that has controllable GPIOs.
//!
//! Usage
//! -----
//!
//! ```rust,ignore
//! # use kernel::static_init;
//!
//! // Generate a list of ports to group into one userspace driver.
//! let async_gpio_ports = static_init!(
//! [&'static capsules::mcp230xx::MCP230xx; 1],
//! [mcp23008]);
//!
//! let gpio_async = static_init!(
//! capsules::gpio_async::GPIOAsync<'static, capsules::mcp230xx::MCP230xx<'static>>,
//! capsules::gpio_async::GPIOAsync::new(async_gpio_ports));
//!
//! // Setup the clients correctly.
//! for port in async_gpio_ports.iter() {
//! port.set_client(gpio_async);
//! }
//! ```
use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil;
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::utilities::cells::OptionalCell;
use kernel::{ErrorCode, ProcessId};
/// Syscall driver number.
use capsules_core::driver;
pub const DRIVER_NUM: usize = driver::NUM::GpioAsync as usize;
pub struct GPIOAsync<'a, Port: hil::gpio_async::Port> {
ports: &'a [&'a Port],
grants: Grant<App, UpcallCount<2>, AllowRoCount<0>, AllowRwCount<0>>,
/// **Transient** ownership of the partially virtualized peripheral.
///
/// Current GPIO HIL semantics notify *all* processes of interrupts
/// to any pin with interrupts configured (and it's left to higher
/// layers to filter activity based on which pin generated their
/// interrupt). For Async GPIO, this is awkward to virtualize, as
/// there is no owning process of a pin for activity interrupts, but
/// there is for configuration result interrupts. Also, the underlying
/// hardware likely can't handle multiple concurrent configurations
/// from multiple apps. Hence, this variable, which tracks a configuration
/// while it is in flight and notifies the correct process that their
/// configuration has succeeded. In the rare case where two apps attempt
/// concurrent configuration requests, the later app will receive `EBUSY`.
/// A retry loop should be sufficient for most apps to handle this rare
/// case.
configuring_process: OptionalCell<ProcessId>,
}
#[derive(Default)]
pub struct App {}
impl<'a, Port: hil::gpio_async::Port> GPIOAsync<'a, Port> {
pub fn new(
ports: &'a [&'a Port],
grants: Grant<App, UpcallCount<2>, AllowRoCount<0>, AllowRwCount<0>>,
) -> GPIOAsync<'a, Port> {
GPIOAsync {
ports,
grants,
configuring_process: OptionalCell::empty(),
}
}
fn configure_input_pin(&self, port: usize, pin: usize, config: usize) -> Result<(), ErrorCode> {
let ports = self.ports;
let mode = match config {
0 => hil::gpio::FloatingState::PullNone,
1 => hil::gpio::FloatingState::PullUp,
2 => hil::gpio::FloatingState::PullDown,
_ => return Err(ErrorCode::INVAL),
};
ports[port].make_input(pin, mode)
}
fn configure_interrupt(&self, port: usize, pin: usize, config: usize) -> Result<(), ErrorCode> {
let ports = self.ports;
let mode = match config {
0 => hil::gpio::InterruptEdge::EitherEdge,
1 => hil::gpio::InterruptEdge::RisingEdge,
2 => hil::gpio::InterruptEdge::FallingEdge,
_ => return Err(ErrorCode::INVAL),
};
ports[port].enable_interrupt(pin, mode)
}
}
impl<Port: hil::gpio_async::Port> hil::gpio_async::Client for GPIOAsync<'_, Port> {
fn fired(&self, pin: usize, identifier: usize) {
// schedule callback with the pin number and value for all apps
self.grants.each(|_, _app, upcalls| {
upcalls.schedule_upcall(1, (identifier, pin, 0)).ok();
});
}
fn done(&self, value: usize) {
// alert currently configuring app
self.configuring_process.map(|pid| {
let _ = self.grants.enter(pid, |_app, upcalls| {
upcalls.schedule_upcall(0, (0, value, 0)).ok();
});
});
// then clear currently configuring app
self.configuring_process.clear();
}
}
impl<Port: hil::gpio_async::Port> SyscallDriver for GPIOAsync<'_, Port> {
// Setup callbacks for gpio_async events.
//
// ### `subscribe_num`
//
// - `0`: Setup a callback for when **split-phase operations complete**.
// This callback gets called from the gpio_async `done()` event and
// signals the end of operations like asserting a GPIO pin or configuring
// an interrupt pin. The callback will be called with two valid
// arguments. The first is the callback type, which is currently 0 for
// all `done()` events. The second is a value, which is only useful for
// operations which should return something, like a GPIO read.
// - `1`: Setup a callback for when a **GPIO interrupt** occurs. This
// callback will be called with two arguments, the first being the port
// number of the interrupting pin, and the second being the pin number.
/// Configure and read GPIO pins.
///
/// `pin` is the index of the pin.
///
/// `data` is a 32 bit value packed with the lowest 16 bits as the port
/// number, and the remaining upper bits as a command-specific value.
///
/// ### `command_num`
///
/// - `0`: Driver existence check.
/// - `1`: Set a pin as an output.
/// - `2`: Set a pin high by setting it to 1.
/// - `3`: Clear a pin by setting it to 0.
/// - `4`: Toggle a pin.
/// - `5`: Set a pin as an input and configure its pull-up or pull-down
/// state. The command-specific field should be set to 0 for a pull-up, 1
/// for a pull-down, or 2 for neither.
/// - `6`: Read a GPIO pin state, and have its value returned in the done()
/// callback.
/// - `7`: Enable an interrupt on a GPIO pin. The command-specific data
/// should be 0 for an either-edge interrupt, 1 for a rising edge
/// interrupt, and 2 for a falling edge interrupt.
/// - `8`: Disable an interrupt on a pin.
/// - `9`: Disable a GPIO pin.
/// - `10`: Get number of GPIO ports supported.
fn command(
&self,
command_number: usize,
pin: usize,
data: usize,
process_id: ProcessId,
) -> CommandReturn {
let port = data & 0xFFFF;
let other = (data >> 16) & 0xFFFF;
let ports = self.ports;
// Driver existence check.
if command_number == 0 {
CommandReturn::success();
}
// Special case command 10; everything else results in a process-owned,
// split-phase call.
if command_number == 10 {
// How many ports
return CommandReturn::success_u32(ports.len() as u32);
}
// On any command other than 0, we check for ports length.
if port >= ports.len() {
return CommandReturn::failure(ErrorCode::INVAL);
}
// On any command other than 0, we check if another command is in flight
if self.configuring_process.is_some() {
return CommandReturn::failure(ErrorCode::BUSY);
};
let res = match command_number {
// enable output
1 => ports[port].make_output(pin),
// set pin
2 => ports[port].set(pin),
// clear pin
3 => ports[port].clear(pin),
// toggle pin
4 => ports[port].toggle(pin),
// enable and configure input
5 => self.configure_input_pin(port, pin, other & 0xFF),
// read input
6 => ports[port].read(pin),
// enable interrupt on pin
7 => self.configure_interrupt(port, pin, other & 0xFF),
// disable interrupt on pin
8 => ports[port].disable_interrupt(pin),
// disable pin
9 => ports[port].disable(pin),
// default
_ => return CommandReturn::failure(ErrorCode::NOSUPPORT),
};
// If any async command kicked off, note that the peripheral is busy
// and which process to return the command result to
if res.is_ok() {
self.configuring_process.set(process_id);
}
res.into()
}
fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
self.grants.enter(processid, |_, _| {})
}
}