Skip to content

Commit

Permalink
Cleanup xhci, add basic IRQ event functions, cleanup e1000d and rtl8168d
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Aug 6, 2017
1 parent 0231e44 commit 14d8165
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 57 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions e1000d/src/main.rs
Expand Up @@ -29,7 +29,7 @@ fn main() {
let irq_str = args.next().expect("e1000d: no irq provided");
let irq = irq_str.parse::<u8>().expect("e1000d: failed to parse irq");

print!("{}", format!(" + E1000 {} on: {:X}, IRQ: {}\n", name, bar, irq));
print!("{}", format!(" + E1000 {} on: {:X} IRQ: {}\n", name, bar, irq));

// Daemonize
if unsafe { syscall::clone(0).unwrap() } == 0 {
Expand Down Expand Up @@ -100,7 +100,7 @@ fn main() {
}

Ok(None)
}).expect("e1000d: failed to catch events on IRQ file");
}).expect("e1000d: failed to catch events on scheme file");

for event_count in event_queue.trigger_all(0).expect("e1000d: failed to trigger events") {
socket.borrow_mut().write(&Packet {
Expand Down
2 changes: 1 addition & 1 deletion rtl8168d/src/main.rs
Expand Up @@ -104,7 +104,7 @@ fn main() {
}

Ok(None)
}).expect("rtl8168d: failed to catch events on IRQ file");
}).expect("rtl8168d: failed to catch events on scheme file");

for event_count in event_queue.trigger_all(0).expect("rtl8168d: failed to trigger events") {
socket.borrow_mut().write(&Packet {
Expand Down
1 change: 1 addition & 0 deletions xhcid/Cargo.toml
Expand Up @@ -6,4 +6,5 @@ version = "0.1.0"
bitflags = "0.7"
plain = "0.2"
spin = "0.4"
redox_event = "0.1"
redox_syscall = "0.1"
92 changes: 82 additions & 10 deletions xhcid/src/main.rs
@@ -1,9 +1,19 @@
#[macro_use]
extern crate bitflags;
extern crate event;
extern crate plain;
extern crate syscall;

use event::EventQueue;
use std::cell::RefCell;
use std::env;
use std::fs::File;
use std::io::{Result, Read, Write};
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::sync::Arc;
use syscall::data::Packet;
use syscall::error::EWOULDBLOCK;
use syscall::scheme::SchemeMut;

use xhci::Xhci;

Expand All @@ -19,23 +29,85 @@ fn main() {
let bar_str = args.next().expect("xhcid: no address provided");
let bar = usize::from_str_radix(&bar_str, 16).expect("xhcid: failed to parse address");

print!("{}", format!(" + XHCI {} on: {:X}\n", name, bar));
let irq_str = args.next().expect("xhcid: no IRQ provided");
let irq = irq_str.parse::<u8>().expect("xhcid: failed to parse irq");

print!("{}", format!(" + XHCI {} on: {:X} IRQ: {}\n", name, bar, irq));

// Daemonize
if unsafe { syscall::clone(0).unwrap() } == 0 {
let socket_fd = syscall::open(":usb", syscall::O_RDWR | syscall::O_CREAT | syscall::O_NONBLOCK).expect("xhcid: failed to create usb scheme");
let socket = Arc::new(RefCell::new(unsafe { File::from_raw_fd(socket_fd) }));

let mut irq_file = File::open(format!("irq:{}", irq)).expect("xhcid: failed to open IRQ file");

let address = unsafe { syscall::physmap(bar, 65536, syscall::MAP_WRITE).expect("xhcid: failed to map address") };
{
let mut hci = Xhci::new(address).expect("xhcid: failed to allocate device");

hci.probe().expect("xhcid: failed to probe");

let mut event_queue = EventQueue::<()>::new().expect("xhcid: failed to create event queue");

let todo = Arc::new(RefCell::new(Vec::<Packet>::new()));

//let device_irq = device.clone();
let socket_irq = socket.clone();
let todo_irq = todo.clone();
event_queue.add(irq_file.as_raw_fd(), move |_count: usize| -> Result<Option<()>> {
/*
let mut irq = [0; 8];
irq_file.read(&mut irq)?;
match Xhci::new(address) {
Ok(mut xhci) => {
if let Err(err) = xhci.probe() {
println!("xhcid: probe error: {}", err);
let isr = unsafe { device_irq.borrow_mut().irq() };
if isr != 0 {
irq_file.write(&mut irq)?;
let mut todo = todo_irq.borrow_mut();
let mut i = 0;
while i < todo.len() {
let a = todo[i].a;
device_irq.borrow_mut().handle(&mut todo[i]);
if todo[i].a == (-EWOULDBLOCK) as usize {
todo[i].a = a;
i += 1;
} else {
socket_irq.borrow_mut().write(&mut todo[i])?;
todo.remove(i);
}
}
}
},
Err(err) => {
println!("xhcid: open error: {}", err);
}
}
*/
Ok(None)
}).expect("xhcid: failed to catch events on IRQ file");

let socket_fd = socket.borrow().as_raw_fd();
let socket_packet = socket.clone();
event_queue.add(socket_fd, move |_count: usize| -> Result<Option<()>> {
/*
loop {
let mut packet = Packet::default();
if socket_packet.borrow_mut().read(&mut packet)? == 0 {
break;
}
let a = packet.a;
device.borrow_mut().handle(&mut packet);
if packet.a == (-EWOULDBLOCK) as usize {
packet.a = a;
todo.borrow_mut().push(packet);
} else {
socket_packet.borrow_mut().write(&mut packet)?;
}
}
*/
Ok(None)
}).expect("xhcid: failed to catch events on scheme file");

event_queue.trigger_all(0).expect("xhcid: failed to trigger events");

event_queue.run().expect("xhcid: failed to handle events");
}
unsafe { let _ = syscall::physunmap(address); }
}
}
8 changes: 4 additions & 4 deletions xhcid/src/xhci/device.rs → xhcid/src/xhci/context.rs
Expand Up @@ -35,13 +35,13 @@ pub struct InputContext {
pub device: DeviceContext,
}

pub struct DeviceList {
pub struct DeviceContextList {
pub dcbaa: Dma<[u64; 256]>,
pub contexts: Vec<Dma<DeviceContext>>,
}

impl DeviceList {
pub fn new(max_slots: u8) -> Result<DeviceList> {
impl DeviceContextList {
pub fn new(max_slots: u8) -> Result<DeviceContextList> {
let mut dcbaa = Dma::<[u64; 256]>::zeroed()?;
let mut contexts = vec![];

Expand All @@ -53,7 +53,7 @@ impl DeviceList {
contexts.push(context);
}

Ok(DeviceList {
Ok(DeviceContextList {
dcbaa: dcbaa,
contexts: contexts
})
Expand Down
92 changes: 52 additions & 40 deletions xhcid/src/xhci/mod.rs
Expand Up @@ -6,7 +6,7 @@ use usb;

mod capability;
mod command;
mod device;
mod context;
mod doorbell;
mod event;
mod operational;
Expand All @@ -16,7 +16,7 @@ mod trb;

use self::capability::CapabilityRegs;
use self::command::CommandRing;
use self::device::DeviceList;
use self::context::{DeviceContextList, InputContext};
use self::doorbell::Doorbell;
use self::operational::OperationalRegs;
use self::port::Port;
Expand Down Expand Up @@ -57,6 +57,26 @@ impl<'a> Device<'a> {
event.reserved(false);
}

fn get_device(&mut self) -> Result<usb::DeviceDescriptor> {
let mut desc = Dma::<usb::DeviceDescriptor>::zeroed()?;
self.get_desc(
usb::DescriptorKind::Device,
0,
&mut desc
);
Ok(*desc)
}

fn get_config(&mut self, config: u8) -> Result<(usb::ConfigDescriptor, [u8; 4087])> {
let mut desc = Dma::<(usb::ConfigDescriptor, [u8; 4087])>::zeroed()?;
self.get_desc(
usb::DescriptorKind::Configuration,
config,
&mut desc
);
Ok(*desc)
}

fn get_string(&mut self, index: u8) -> Result<String> {
let mut sdesc = Dma::<(u8, u8, [u16; 127])>::zeroed()?;
self.get_desc(
Expand All @@ -80,7 +100,7 @@ pub struct Xhci {
ports: &'static mut [Port],
dbs: &'static mut [Doorbell],
run: &'static mut RuntimeRegs,
devices: DeviceList,
dev_ctx: DeviceContextList,
cmd: CommandRing,
}

Expand Down Expand Up @@ -146,7 +166,7 @@ impl Xhci {
ports: ports,
dbs: dbs,
run: run,
devices: DeviceList::new(max_slots)?,
dev_ctx: DeviceContextList::new(max_slots)?,
cmd: CommandRing::new()?,
};

Expand All @@ -162,7 +182,7 @@ impl Xhci {
println!(" - Enabled Slots: {}", self.op.config.read() & 0xFF);

// Set device context address array pointer
let dcbaap = self.devices.dcbaap();
let dcbaap = self.dev_ctx.dcbaap();
println!(" - Write DCBAAP: {:X}", dcbaap);
self.op.dcbaap.write(dcbaap as u64);

Expand All @@ -173,16 +193,19 @@ impl Xhci {

// Set event ring segment table registers
println!(" - Interrupter 0: {:X}", self.run.ints.as_ptr() as usize);
println!(" - Write ERSTZ");
self.run.ints[0].erstsz.write(1);
{
let erstz = 1;
println!(" - Write ERSTZ: {}", erstz);
self.run.ints[0].erstsz.write(erstz);

let erdp = self.cmd.events.trbs.physical();
println!(" - Write ERDP: {:X}", erdp);
self.run.ints[0].erdp.write(erdp as u64);
let erdp = self.cmd.events.trbs.physical();
println!(" - Write ERDP: {:X}", erdp);
self.run.ints[0].erdp.write(erdp as u64);

let erstba = self.cmd.events.ste.physical();
println!(" - Write ERSTBA: {:X}", erstba);
self.run.ints[0].erstba.write(erstba as u64);
let erstba = self.cmd.events.ste.physical();
println!(" - Write ERSTBA: {:X}", erstba);
self.run.ints[0].erstba.write(erstba as u64);
}

// Set run/stop to 1
println!(" - Start");
Expand Down Expand Up @@ -234,12 +257,12 @@ impl Xhci {
println!(" - Slot {}", slot);

let mut trbs = Dma::<[trb::Trb; 256]>::zeroed()?;
let mut trb_i = 0;
let mut input = Dma::<device::InputContext>::zeroed()?;

let mut input = Dma::<InputContext>::zeroed()?;
{
input.add_context.write(1 << 1 | 1);

input.device.slot.a.write(1 << 27);
input.device.slot.a.write((1 << 27) | (speed << 20));
input.device.slot.b.write(((i as u32 + 1) & 0xFF) << 16);

input.device.endpoints[0].b.write(4096 << 16 | 4 << 3 | 3 << 1);
Expand All @@ -264,20 +287,15 @@ impl Xhci {

let mut dev = Device {
trbs: trbs,
trb_i: trb_i,
trb_i: 0,
cmd: &mut self.cmd,
db: &mut self.dbs[slot as usize],
};

println!(" - Get descriptor");

let mut ddesc = Dma::<usb::DeviceDescriptor>::zeroed()?;
dev.get_desc(
usb::DescriptorKind::Device,
0,
&mut ddesc
);
println!(" {:?}", *ddesc);
let ddesc = dev.get_device()?;
println!(" {:?}", ddesc);

if ddesc.manufacturer_str > 0 {
println!(" Manufacturer: {}", dev.get_string(ddesc.manufacturer_str)?);
Expand All @@ -292,26 +310,20 @@ impl Xhci {
}

for config in 0..ddesc.configurations {
let mut cdesc = Dma::<(usb::ConfigDescriptor, [u8; 4087])>::zeroed()?;
dev.get_desc(
usb::DescriptorKind::Configuration,
config,
&mut cdesc
);
println!(" {}: {:?}", config, cdesc.0);

if cdesc.0.configuration_str > 0 {
println!(" Name: {}", dev.get_string(cdesc.0.configuration_str)?);
let (cdesc, data) = dev.get_config(config)?;
println!(" {}: {:?}", config, cdesc);

if cdesc.configuration_str > 0 {
println!(" Name: {}", dev.get_string(cdesc.configuration_str)?);
}

if cdesc.0.total_length as usize > mem::size_of::<usb::ConfigDescriptor>() {
let len = cdesc.0.total_length as usize - mem::size_of::<usb::ConfigDescriptor>();
let data = &cdesc.1[..len];
if cdesc.total_length as usize > mem::size_of::<usb::ConfigDescriptor>() {
let len = cdesc.total_length as usize - mem::size_of::<usb::ConfigDescriptor>();

let mut i = 0;
for interface in 0..cdesc.0.interfaces {
for interface in 0..cdesc.interfaces {
let mut idesc = usb::InterfaceDescriptor::default();
if i < data.len() && idesc.copy_from_bytes(&data[i..]).is_ok() {
if i < len && i < data.len() && idesc.copy_from_bytes(&data[i..len]).is_ok() {
i += mem::size_of_val(&idesc);
println!(" {}: {:?}", interface, idesc);

Expand All @@ -321,7 +333,7 @@ impl Xhci {

for endpoint in 0..idesc.endpoints {
let mut edesc = usb::EndpointDescriptor::default();
if i < data.len() && edesc.copy_from_bytes(&data[i..]).is_ok() {
if i < len && i < data.len() && edesc.copy_from_bytes(&data[i..len]).is_ok() {
i += mem::size_of_val(&edesc);
println!(" {}: {:?}", endpoint, edesc);
}
Expand Down

0 comments on commit 14d8165

Please sign in to comment.