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
//! USB system call interface
//!
//! This capsule provides a system call interface to the USB controller.
//!
//! ## Instantiation
//!
//! The `UsbSyscallDriver` must be created by passing a reference to something
//! that implements `hil::usb::Client` (that is, something that is connected to
//! the USBC), as well as a `Grant` for managing application requests.  For
//! example:
//!
//! ```rust
//! // Configure the USB controller
//! let usb_client = static_init!(
//!     capsules::usbc_client::Client<'static, sam4l::usbc::Usbc<'static>>,
//!     capsules::usbc_client::Client::new(&sam4l::usbc::USBC));
//! sam4l::usbc::USBC.set_client(usb_client);
//!
//! // Configure the USB userspace driver
//! let usb_driver = static_init!(
//!     capsules::usb_user::UsbSyscallDriver<'static,
//!         capsules::usbc_client::Client<'static, sam4l::usbc::Usbc<'static>>>,
//!     capsules::usb_user::UsbSyscallDriver::new(
//!         usb_client, kernel::Grant::create()));
//! ```

use core::cell::Cell;
use kernel::{AppId, Callback, Driver, Grant, ReturnCode};
use kernel::hil;

/// Syscall number
pub const DRIVER_NUM: usize = 0x20005;

#[derive(Default)]
pub struct App {
    callback: Option<Callback>,
    awaiting: Option<Request>,
}

pub struct UsbSyscallDriver<'a, C: hil::usb::Client + 'a> {
    usbc_client: &'a C,
    apps: Grant<App>,
    serving_app: Cell<Option<AppId>>,
}

impl<'a, C> UsbSyscallDriver<'a, C>
where
    C: hil::usb::Client,
{
    pub fn new(usbc_client: &'a C, apps: Grant<App>) -> Self {
        UsbSyscallDriver {
            usbc_client: usbc_client,
            apps: apps,
            serving_app: Cell::new(None),
        }
    }

    fn serve_waiting_apps(&self) {
        if self.serving_app.get().is_some() {
            // An operation on the USBC client is in progress
            return;
        }

        // Find a waiting app and start its requested computation
        let mut found = false;
        for app in self.apps.iter() {
            app.enter(|app, _| {
                if let Some(request) = app.awaiting {
                    found = true;
                    match request {
                        Request::EnableAndAttach => {
                            // Enable and attach (synchronously)
                            self.usbc_client.enable();
                            self.usbc_client.attach();

                            // Schedule a callback immediately
                            if let Some(mut callback) = app.callback {
                                callback.schedule(From::from(ReturnCode::SUCCESS), 0, 0);
                            }
                            app.awaiting = None;
                        }
                    }
                }
            });
            if found {
                break;
            }
        }

        if !found {
            // No userspace requests pending at this time
        }
    }
}

#[derive(Copy, Clone)]
enum Request {
    EnableAndAttach,
}

impl<'a, C> Driver for UsbSyscallDriver<'a, C>
where
    C: hil::usb::Client,
{
    fn subscribe(&self, subscribe_num: usize, callback: Callback) -> ReturnCode {
        match subscribe_num {
            // Set callback for result
            0 => self.apps
                .enter(callback.app_id(), |app, _| {
                    app.callback = Some(callback);
                    ReturnCode::SUCCESS
                })
                .unwrap_or_else(|err| err.into()),
            _ => ReturnCode::ENOSUPPORT,
        }
    }

    fn command(&self, command_num: usize, _arg: usize, _: usize, appid: AppId) -> ReturnCode {
        match command_num {
            // This driver is present
            0 => ReturnCode::SUCCESS,

            // Enable USB controller, attach to bus, and service default control endpoint
            1 => {
                let result = self.apps
                    .enter(appid, |app, _| {
                        if app.awaiting.is_some() {
                            // Each app may make only one request at a time
                            ReturnCode::EBUSY
                        } else {
                            if app.callback.is_some() {
                                app.awaiting = Some(Request::EnableAndAttach);
                                ReturnCode::SUCCESS
                            } else {
                                ReturnCode::EINVAL
                            }
                        }
                    })
                    .unwrap_or_else(|err| err.into());

                if result == ReturnCode::SUCCESS {
                    self.serve_waiting_apps();
                }
                result
            }

            _ => ReturnCode::ENOSUPPORT,
        }
    }
}