2009-06-22 17:13:00 +03:00
#pypp 0
2009-06-24 01:47:13 +03:00
// Iris: micro-kernel for a capability-based operating system.
// boot-programs/devices.hhp: interfaces for core devices.
// Copyright 2009 Bas Wijnen <wijnen@debian.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-06-22 17:13:00 +03:00
#ifndef __IRIS_DEVICES_HH
#define __IRIS_DEVICES_HH
2009-08-18 00:11:15 +03:00
#include "iris.hh"
2009-07-27 21:03:58 +03:00
2009-08-18 00:11:15 +03:00
// List interface.
template <typename _T> //
2009-09-12 23:48:47 +03:00
struct List : public Kernel::Caps:
List (Kernel::Caps c = Kernel::Cap ()) : Kernel::Caps (c):
List <_T> create (unsigned size, Kernel::Memory mem = Kernel::my_memory):
return List <_T> (mem.create_caps (size))
void set (unsigned idx, _T value):
return Kernel::Caps::set (idx, value)
_T get (unsigned idx):
return _T (Kernel::Caps::get (idx))
2009-07-27 21:03:58 +03:00
2009-09-12 23:48:47 +03:00
/// A block of data with a size and content. Any character can be stored in it (including '\0').
2009-09-06 12:04:09 +03:00
struct String : public Kernel::Cap:
String (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
2009-09-12 23:48:47 +03:00
enum request:
GET_SIZE = 0x2001
GET_CHARS
GET_PAGE
2009-10-04 20:47:20 +03:00
ID
2009-09-12 23:48:47 +03:00
/// Get the size of the string.
Kernel::Num get_size ():
return call (CAP_MASTER_DIRECT | GET_SIZE)
/// Get exactly 16 characters. The index must be word-aligned.
char *get_chars (Kernel::Num idx, char buffer[16]):
call (CAP_MASTER_DIRECT | GET_CHARS, idx)
unsigned *b = (unsigned *)buffer
b[0] = Kernel::recv.data[0].l
b[1] = Kernel::recv.data[0].h
b[2] = Kernel::recv.data[1].l
b[3] = Kernel::recv.data[1].h
return buffer
/// Get a page from the string. This need not be implemented for strings smaller than PAGE_SIZE. The index must be page-aligned.
Cap get_page (Kernel::Num idx, Kernel::Page ret = Kernel::my_memory.create_page ()):
ocall (ret, CAP_MASTER_DIRECT | GET_PAGE, idx)
return ret
/// A writable String.
struct WString : public String:
WString (Kernel::Cap c = Kernel::Cap ()) : String (c):
enum request:
2009-10-04 20:47:20 +03:00
TRUNCATE = String::ID
2009-09-12 23:48:47 +03:00
SET_CHARS
SET_PAGE
2009-10-04 20:47:20 +03:00
ID
2009-09-12 23:48:47 +03:00
/// Set the size of the string. Strings may have a limit to this setting.
void truncate (Kernel::Num size):
call (CAP_MASTER_DIRECT | TRUNCATE, size)
/// Set exactly 4 characters. The index must be word-aligned.
void set_chars (Kernel::Num idx, char buffer[4]):
call (Kernel::Num (CAP_MASTER_DIRECT | SET_CHARS, *(unsigned *)buffer), idx)
/// Overwrite a page from the string. This need not be implemented for strings smaller than PAGE_SIZE. The index must be page-aligned. The caller may lose the frame in the transaction.
void set_page (Kernel::Num idx, Kernel::Page page):
ocall (page, CAP_MASTER_DIRECT | SET_PAGE, idx)
2009-07-27 21:03:58 +03:00
2009-10-04 20:47:20 +03:00
// Every process which wants to be switchable through a terminal must implement this interface.
struct Device : public Kernel::Cap:
Device (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
enum request:
2009-10-31 10:32:23 +02:00
CREATE_USER = WString::ID
2009-10-04 20:47:20 +03:00
DESTROY_USER
UNUSE
USE
ID
// Create a new user for this device. It will not be the active user.
// The provided storage must allow object creation; no other actions may be used by the terminal.
Kernel::Cap create_user (Kernel::Memory storage):
iocall (storage, CAP_MASTER_DIRECT | CREATE_USER)
return Kernel::get_arg ()
2009-12-11 10:43:42 +02:00
// Destroy a user. It is made inactive if it was active.
2009-10-04 20:47:20 +03:00
void destroy_user (Kernel::Cap user):
ocall (user, CAP_MASTER_DIRECT | DESTROY_USER)
// Make user inactive.
void unuse (Kernel::Cap user):
ocall (user, CAP_MASTER_DIRECT | UNUSE)
2009-12-11 10:43:42 +02:00
// Make user active. It makes the previous active user inactive.
2009-10-04 20:47:20 +03:00
void use (Kernel::Cap user):
ocall (user, CAP_MASTER_DIRECT | USE)
2010-01-14 19:14:37 +02:00
// Convenience function for threads implementing a device.
2010-01-16 17:13:54 +02:00
static void host (unsigned id, unsigned ¤t_user, Kernel::Cap &reply, Kernel::Cap &arg):
2010-01-14 19:14:37 +02:00
static unsigned last_user
switch Kernel::recv.data[0].l:
case Device::CREATE_USER:
// Increment last_user; skip 0.
// FIXME: if this really wraps, it is possible that two users share their id.
if !++last_user:
++last_user
Kernel::Cap c = Kernel::my_receiver.create_capability (Kernel::Num (last_user, id))
reply.invoke (0, 0, c.copy ())
Kernel::free_cap (c)
Kernel::free_cap (reply)
2010-01-16 17:13:54 +02:00
Kernel::free_cap (arg)
2010-01-14 19:14:37 +02:00
break
case Device::DESTROY_USER:
reply.invoke ()
Kernel::free_cap (reply)
2010-01-16 17:13:54 +02:00
Kernel::free_cap (arg)
2010-01-14 19:14:37 +02:00
break
case Device::USE:
current_user = Kernel::my_receiver.get_protected (arg).l
reply.invoke ()
Kernel::free_cap (reply)
Kernel::free_cap (arg)
break
case Device::UNUSE:
Kernel::Num p = Kernel::my_receiver.get_protected (arg)
if p.h == id && current_user == p.l:
current_user = 0
reply.invoke ()
Kernel::free_cap (reply)
Kernel::free_cap (arg)
break
2009-10-04 20:47:20 +03:00
2009-10-31 10:32:23 +02:00
// Interface for talking to the parent process.
struct Parent : public Kernel::Cap:
Parent (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
enum request:
GET_DEVICE = Device::ID
PROVIDE_DEVICE
GET_MEMORY
PROVIDE_MEMORY
2010-01-18 07:45:52 +02:00
INIT_DONE
2009-10-31 10:32:23 +02:00
EXIT
ID
// Get a device handle.
template <typename _T> _T get_device (unsigned num = 0):
icall (Kernel::Num (CAP_MASTER_DIRECT | GET_DEVICE, num), _T::ID)
return Kernel::get_arg ()
// Provide a device handle.
template <typename _T> void provide_device (Device dev, unsigned num = 0):
ocall (dev, Kernel::Num (CAP_MASTER_DIRECT | PROVIDE_DEVICE, num), _T::ID)
// Get memory paid for by another thread, which cannot be inspected or changed by that thread. The memory can be inspected and changed by the user (owning both threads). The call will fail when the threads are not owned by the same user.
Kernel::Memory get_memory (Kernel::Cap target):
iocall (target, CAP_MASTER_DIRECT | GET_MEMORY)
return Kernel::get_arg ()
// Get a handle that another thread can use to call get_memory on. The actual limit on the created memory is floor(limit, thread address space limit).
Kernel::Cap provide_memory (unsigned limit):
icall (CAP_MASTER_DIRECT | PROVIDE_MEMORY, limit)
return Kernel::get_arg ()
2010-01-18 07:45:52 +02:00
// Signal the parent that the initialisation phase is over.
void init_done ():
invoke (CAP_MASTER_DIRECT | INIT_DONE)
2009-10-31 10:32:23 +02:00
// Exit the program. The parent does not reply, but kills the process.
void exit (Kernel::Num code):
call (CAP_MASTER_DIRECT | EXIT, code)
2009-08-18 00:11:15 +03:00
// Keyboard interface.
2009-09-06 12:04:09 +03:00
struct Keyboard : public Kernel::Cap:
Keyboard (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
2009-08-18 00:11:15 +03:00
enum request:
2009-10-31 10:32:23 +02:00
SET_CB = Parent::ID
2009-10-10 02:31:10 +03:00
GET_NUM_KEYS
2009-08-18 00:11:15 +03:00
GET_KEYS
2009-10-04 20:47:20 +03:00
ID
2009-08-18 00:11:15 +03:00
// At event: the callback is called with a keycode. One bit defines if it's a press or release event.
enum constant:
RELEASE = 1 << 31
2009-10-04 20:47:20 +03:00
// Set the event callback. Currently pressed keys emit a key press event to the new callback immediately.
2009-09-06 12:04:09 +03:00
void set_cb (Kernel::Cap cb):
2009-08-24 22:02:35 +03:00
ocall (cb, CAP_MASTER_DIRECT | SET_CB)
2009-12-11 10:43:42 +02:00
// Get the number of keys on the keyboard.
2009-10-10 02:31:10 +03:00
unsigned get_num_keys ():
return call (CAP_MASTER_DIRECT | GET_NUM_KEYS).l
2009-12-11 10:43:42 +02:00
// Get the keycodes for the keys. The reply sends 4 key codes (32 bit each).
2009-10-10 02:31:10 +03:00
void get_keys (unsigned first):
call (CAP_MASTER_DIRECT | GET_KEYS, first)
2009-07-27 21:03:58 +03:00
2009-10-04 20:47:20 +03:00
// Buzzer interface.
struct Buzzer : public Kernel::Cap:
Buzzer (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
enum request:
BEEP = Keyboard::ID
STOP
ID
2009-12-11 10:43:42 +02:00
// Emit a beep of specified frequency, time and volume. Volume may not be supported. If an other beep is in progress, it is aborted.
2009-10-04 20:47:20 +03:00
void beep (unsigned freq, unsigned ms, unsigned volume, Kernel::Cap cb = Kernel::Cap ()):
ocall (cb, Kernel::Num (CAP_MASTER_DIRECT | BEEP, volume), Kernel::Num (freq, ms))
// Abort current beep, if any.
void stop ():
call (CAP_MASTER_DIRECT | STOP)
2009-07-27 21:03:58 +03:00
// Display interface.
2009-09-06 12:04:09 +03:00
struct Display : public Kernel::Cap:
Display (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
2009-08-18 00:11:15 +03:00
enum request:
2009-10-04 20:47:20 +03:00
EOF_CB = Buzzer::ID
2009-08-18 00:11:15 +03:00
CREATE_FB
USE_FB
GET_INFO
2009-12-26 15:17:06 +02:00
SET_STATE
2009-10-04 20:47:20 +03:00
ID
2009-08-18 00:11:15 +03:00
// Register an end-of-frame callback.
// At end of frame, the callback is invoked and forgotten. It must be reregistered to keep a stream of events.
2009-09-06 12:04:09 +03:00
void set_eof_cb (Kernel::Cap cb):
2009-08-24 22:02:35 +03:00
ocall (cb, CAP_MASTER_DIRECT | EOF_CB)
2009-08-18 00:11:15 +03:00
// Create a framebuffer for the display. When not in use, it can be freed by the user.
// The pages must be cappages holding Page capabilities. They are filled by the display.
// The passed numbers must be 0 or match a mode that the device can use.
// The returned number is the physical address of the framebuffer. It can be used with display_use_framebuffer.
2009-09-06 12:04:09 +03:00
unsigned create_framebuffer (unsigned w = 0, unsigned h = 0, unsigned mode = 0):
return icall (Kernel::Num (CAP_MASTER_DIRECT | CREATE_FB, 0), Kernel::Num ((w << 16) | h, mode)).l
2009-08-18 00:11:15 +03:00
// Use a framebuffer. The address must have been returned from display_create_framebuffer.
// w, h and mode must match the values given at creation time.
// unuse_cb is called the next time this operation is requested for this display.
2009-09-06 12:04:09 +03:00
void use_framebuffer (unsigned addr, Kernel::Cap unuse_cb = Kernel::Cap (), unsigned w = 0, unsigned h = 0, unsigned mode = 0):
ocall (unuse_cb, Kernel::Num (CAP_MASTER_DIRECT | USE_FB, addr), Kernel::Num ((w << 16) | h, mode))
2009-08-18 00:11:15 +03:00
// Get information about the display.
void get_info ():
// TODO: Interface is to be designed.
2009-07-27 21:03:58 +03:00
2009-12-26 15:17:06 +02:00
// Numeric setting, such as a display backlight.
struct Setting : public Kernel::Cap:
Setting (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
enum request:
SET = Display::ID
GET_RANGE
ID
// Set a new value.
void set (unsigned value):
call (CAP_MASTER_DIRECT | SET, value)
// Get the maximum value for this setting. Using a higher value with SET gives undefined results.
unsigned get_range ():
return call (CAP_MASTER_DIRECT | GET_RANGE).l
2009-07-27 21:03:58 +03:00
// File system interface.
2009-12-11 10:43:42 +02:00
// filesystem-related interfaces: directory, stream, seekable.
// Normal files implement stream and/or seekable. Directories implement directory.
// Seekable is not a class, it is identical to [W]String.
2009-07-27 21:03:58 +03:00
// Directory interface.
2009-10-31 10:32:23 +02:00
struct Directory : public Kernel::Cap:
Directory (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
2009-08-18 00:11:15 +03:00
enum request:
2009-12-26 15:17:06 +02:00
GET_SIZE = Setting::ID
2009-08-18 00:11:15 +03:00
GET_NAME
2009-12-11 10:43:42 +02:00
GET_FILE_RO
2009-08-18 00:11:15 +03:00
GET_FILE_INFO
2009-10-31 10:32:23 +02:00
LOCK_RO
UNLOCK_RO
2009-10-04 20:47:20 +03:00
ID
2009-08-18 00:11:15 +03:00
// Get the number of entries in this directory.
2009-09-06 12:04:09 +03:00
Kernel::Num get_size ():
2009-08-18 00:11:15 +03:00
return call (CAP_MASTER_DIRECT | GET_SIZE)
2009-12-28 08:53:29 +02:00
// Get the filename.
2009-09-06 12:04:09 +03:00
String get_name (Kernel::Num idx):
icall (CAP_MASTER_DIRECT | GET_NAME, idx)
return Kernel::get_arg ()
2009-08-18 00:11:15 +03:00
// Get the file.
2009-12-11 10:43:42 +02:00
Kernel::Cap get_file_ro (Kernel::Num idx):
icall (CAP_MASTER_DIRECT | GET_FILE_RO, idx)
2009-09-06 12:04:09 +03:00
return Kernel::get_arg ()
2009-12-11 10:43:42 +02:00
// Get file info. TODO: define possible types.
2009-09-06 12:04:09 +03:00
Kernel::Num get_file_info (Kernel::Num idx, unsigned type):
return icall (Kernel::Num (CAP_MASTER_DIRECT | GET_FILE_INFO, type), idx)
2009-12-11 10:43:42 +02:00
// Lock the directory for reading. Multiple read locks can exist simultaneously, but no write lock can be present.
void lock_ro ():
call (CAP_MASTER_DIRECT | LOCK_RO)
// Release a read-only lock. Write operations can only be done when the directory is locked.
void unlock_ro ():
call (CAP_MASTER_DIRECT | UNLOCK_RO)
struct WDirectory : public Directory:
WDirectory (Kernel::Cap c = Kernel::Cap ()) : Directory (c):
enum request:
GET_FILE = Directory::ID
CREATE_FILE
DELETE_FILE
LOCK
UNLOCK
ID
// Get the file.
Kernel::Cap get_file (Kernel::Num idx):
icall (CAP_MASTER_DIRECT | GET_FILE, idx)
return Kernel::get_arg ()
2009-08-18 00:11:15 +03:00
// Create a new file. After this, any index may map to a different file.
2009-10-31 10:32:23 +02:00
Kernel::Cap create_file (String name):
2009-09-06 12:04:09 +03:00
icall (CAP_MASTER_DIRECT | CREATE_FILE)
return Kernel::get_arg ()
2009-08-18 00:11:15 +03:00
// Delete a file. After this, any index may map to a different file.
2009-09-06 12:04:09 +03:00
void delete_file (Kernel::Num idx):
2009-08-24 22:02:35 +03:00
call (CAP_MASTER_DIRECT | DELETE_FILE, idx)
2009-10-31 10:32:23 +02:00
// Lock the directory. Write operations can only be done when the directory is locked.
void lock ():
call (CAP_MASTER_DIRECT | LOCK)
// Unlock the directory. Write operations can only be done when the directory is locked.
void unlock ():
2009-12-26 15:17:06 +02:00
call (CAP_MASTER_DIRECT | UNLOCK)
2009-07-27 21:03:58 +03:00
2010-01-14 19:14:37 +02:00
// A filesystem turns a String into a Directory.
struct Filesystem : public Device:
Filesystem (Kernel::Cap c = Kernel::Cap ()) : Device (c):
enum request:
USE_DEVICE = WDirectory::ID
USE_DEVICE_RO
ID
WDirectory use_device (WString dev):
ocall (dev, CAP_MASTER_DIRECT | USE_DEVICE)
return Kernel::get_arg ()
Directory use_device_ro (String dev):
ocall (dev, CAP_MASTER_DIRECT | USE_DEVICE_RO)
return Kernel::get_arg ()
2009-07-27 21:03:58 +03:00
// Stream interface.
2009-10-31 10:32:23 +02:00
struct Stream : public Kernel::Cap:
Stream (Kernel::Cap c = Kernel::Cap ()) : Kernel::Cap (c):
2009-08-18 00:11:15 +03:00
enum request:
2010-01-14 19:14:37 +02:00
READ = Filesystem::ID
2009-08-18 00:11:15 +03:00
WRITE
2009-10-04 20:47:20 +03:00
ID
2009-08-18 00:11:15 +03:00
// Try to read size bytes. Returns the number of bytes successfully read.
2009-12-11 10:43:42 +02:00
Kernel::Num read (Kernel::Num size, bool block):
return icall (Kernel::Num (CAP_MASTER_DIRECT | READ, block), size)
2009-08-18 00:11:15 +03:00
// Try to write size bytes. Returns the number of bytes successfully written.
2009-09-06 12:04:09 +03:00
Kernel::Num write (String s, Kernel::Num size):
2009-08-24 22:02:35 +03:00
return ocall (s, CAP_MASTER_DIRECT | WRITE, size)
2009-07-27 21:03:58 +03:00
// Block device interface.
2009-12-11 10:43:42 +02:00
struct Block_device : public WString:
Block_device (Kernel::Cap c = Kernel::Cap ()) : WString (c):
2009-08-18 00:11:15 +03:00
// TODO: to be designed.
2009-07-27 21:03:58 +03:00
// TODO.
// Sound interface.
// Usb interfaces (port, device).
// Pointer interface. (Only movement; buttons follow keyboard interface.)
// Network interface.
// Camera interface.
2009-06-22 17:13:00 +03:00
#endif