浏览代码

Have drivers own transport rather than taking a reference to it.

There's not much point in the caller keeping it around.
Andrew Walbran 2 年之前
父节点
当前提交
005dd18e11
共有 6 个文件被更改,包括 27 次插入30 次删除
  1. 8 11
      examples/riscv/src/main.rs
  2. 3 3
      src/blk.rs
  3. 4 4
      src/console.rs
  4. 4 4
      src/gpu.rs
  5. 4 4
      src/input.rs
  6. 4 4
      src/net.rs

+ 8 - 11
examples/riscv/src/main.rs

@@ -83,9 +83,8 @@ fn virtio_device(transport: impl Transport) {
     }
 }
 
-fn virtio_blk<T: Transport>(mut transport: T) {
-    let mut blk =
-        VirtIOBlk::<HalImpl, T>::new(&mut transport).expect("failed to create blk driver");
+fn virtio_blk<T: Transport>(transport: T) {
+    let mut blk = VirtIOBlk::<HalImpl, T>::new(transport).expect("failed to create blk driver");
     let mut input = vec![0xffu8; 512];
     let mut output = vec![0; 512];
     for i in 0..32 {
@@ -99,9 +98,8 @@ fn virtio_blk<T: Transport>(mut transport: T) {
     info!("virtio-blk test finished");
 }
 
-fn virtio_gpu<T: Transport>(mut transport: T) {
-    let mut gpu =
-        VirtIOGpu::<HalImpl, T>::new(&mut transport).expect("failed to create gpu driver");
+fn virtio_gpu<T: Transport>(transport: T) {
+    let mut gpu = VirtIOGpu::<HalImpl, T>::new(transport).expect("failed to create gpu driver");
     let fb = gpu.setup_framebuffer().expect("failed to get fb");
     for y in 0..768 {
         for x in 0..1024 {
@@ -115,10 +113,10 @@ fn virtio_gpu<T: Transport>(mut transport: T) {
     info!("virtio-gpu test finished");
 }
 
-fn virtio_input<T: Transport>(mut transport: T) {
+fn virtio_input<T: Transport>(transport: T) {
     //let mut event_buf = [0u64; 32];
     let mut _input =
-        VirtIOInput::<HalImpl, T>::new(&mut transport).expect("failed to create input driver");
+        VirtIOInput::<HalImpl, T>::new(transport).expect("failed to create input driver");
     // loop {
     //     input.ack_interrupt().expect("failed to ack");
     //     info!("mouse: {:?}", input.mouse_xy());
@@ -126,9 +124,8 @@ fn virtio_input<T: Transport>(mut transport: T) {
     // TODO: handle external interrupt
 }
 
-fn virtio_net<T: Transport>(mut transport: T) {
-    let mut net =
-        VirtIONet::<HalImpl, T>::new(&mut transport).expect("failed to create net driver");
+fn virtio_net<T: Transport>(transport: T) {
+    let mut net = VirtIONet::<HalImpl, T>::new(transport).expect("failed to create net driver");
     let mut buf = [0u8; 0x100];
     let len = net.recv(&mut buf).expect("failed to recv");
     info!("recv: {:?}", &buf[..len]);

+ 3 - 3
src/blk.rs

@@ -11,14 +11,14 @@ use volatile::Volatile;
 /// Read and write requests (and other exotic requests) are placed in the queue,
 /// and serviced (probably out of order) by the device except where noted.
 pub struct VirtIOBlk<'a, H: Hal, T: Transport> {
-    transport: &'a mut T,
+    transport: T,
     queue: VirtQueue<'a, H>,
     capacity: usize,
 }
 
 impl<'a, H: Hal, T: Transport> VirtIOBlk<'a, H, T> {
     /// Create a new VirtIO-Blk driver.
-    pub fn new(transport: &'a mut T) -> Result<Self> {
+    pub fn new(mut transport: T) -> Result<Self> {
         transport.begin_init(|features| {
             let features = BlkFeature::from_bits_truncate(features);
             info!("device features: {:?}", features);
@@ -36,7 +36,7 @@ impl<'a, H: Hal, T: Transport> VirtIOBlk<'a, H, T> {
             config.capacity.read() / 2
         );
 
-        let queue = VirtQueue::new(transport, 0, 16)?;
+        let queue = VirtQueue::new(&mut transport, 0, 16)?;
         transport.finish_init();
 
         Ok(VirtIOBlk {

+ 4 - 4
src/console.rs

@@ -13,7 +13,7 @@ const QUEUE_SIZE: u16 = 2;
 /// Virtio console. Only one single port is allowed since ``alloc'' is disabled.
 /// Emergency and cols/rows unimplemented.
 pub struct VirtIOConsole<'a, H: Hal, T: Transport> {
-    transport: &'a mut T,
+    transport: T,
     receiveq: VirtQueue<'a, H>,
     transmitq: VirtQueue<'a, H>,
     queue_buf_dma: DMA<H>,
@@ -24,7 +24,7 @@ pub struct VirtIOConsole<'a, H: Hal, T: Transport> {
 
 impl<'a, H: Hal, T: Transport> VirtIOConsole<'a, H, T> {
     /// Create a new VirtIO-Console driver.
-    pub fn new(transport: &'a mut T) -> Result<Self> {
+    pub fn new(mut transport: T) -> Result<Self> {
         transport.begin_init(|features| {
             let features = Features::from_bits_truncate(features);
             info!("Device features {:?}", features);
@@ -34,8 +34,8 @@ impl<'a, H: Hal, T: Transport> VirtIOConsole<'a, H, T> {
         let config_space = transport.config_space().cast::<Config>();
         let config = unsafe { config_space.as_ref() };
         info!("Config: {:?}", config);
-        let receiveq = VirtQueue::new(transport, QUEUE_RECEIVEQ_PORT_0, QUEUE_SIZE)?;
-        let transmitq = VirtQueue::new(transport, QUEUE_TRANSMITQ_PORT_0, QUEUE_SIZE)?;
+        let receiveq = VirtQueue::new(&mut transport, QUEUE_RECEIVEQ_PORT_0, QUEUE_SIZE)?;
+        let transmitq = VirtQueue::new(&mut transport, QUEUE_TRANSMITQ_PORT_0, QUEUE_SIZE)?;
         let queue_buf_dma = DMA::new(1)?;
         let queue_buf_rx = unsafe { &mut queue_buf_dma.as_buf()[0..] };
         transport.finish_init();

+ 4 - 4
src/gpu.rs

@@ -14,7 +14,7 @@ use volatile::{ReadOnly, Volatile, WriteOnly};
 /// In 2D mode the virtio-gpu device provides support for ARGB Hardware cursors
 /// and multiple scanouts (aka heads).
 pub struct VirtIOGpu<'a, H: Hal, T: Transport> {
-    transport: &'a mut T,
+    transport: T,
     rect: Rect,
     /// DMA area of frame buffer.
     frame_buffer_dma: Option<DMA<H>>,
@@ -34,7 +34,7 @@ pub struct VirtIOGpu<'a, H: Hal, T: Transport> {
 
 impl<'a, H: Hal, T: Transport> VirtIOGpu<'a, H, T> {
     /// Create a new VirtIO-Gpu driver.
-    pub fn new(transport: &'a mut T) -> Result<Self> {
+    pub fn new(mut transport: T) -> Result<Self> {
         transport.begin_init(|features| {
             let features = Features::from_bits_truncate(features);
             info!("Device features {:?}", features);
@@ -47,8 +47,8 @@ impl<'a, H: Hal, T: Transport> VirtIOGpu<'a, H, T> {
         let config = unsafe { config_space.as_ref() };
         info!("Config: {:?}", config);
 
-        let control_queue = VirtQueue::new(transport, QUEUE_TRANSMIT, 2)?;
-        let cursor_queue = VirtQueue::new(transport, QUEUE_CURSOR, 2)?;
+        let control_queue = VirtQueue::new(&mut transport, QUEUE_TRANSMIT, 2)?;
+        let cursor_queue = VirtQueue::new(&mut transport, QUEUE_CURSOR, 2)?;
 
         let queue_buf_dma = DMA::new(2)?;
         let queue_buf_send = unsafe { &mut queue_buf_dma.as_buf()[..PAGE_SIZE] };

+ 4 - 4
src/input.rs

@@ -11,7 +11,7 @@ use volatile::{ReadOnly, WriteOnly};
 /// Device behavior mirrors that of the evdev layer in Linux,
 /// making pass-through implementations on top of evdev easy.
 pub struct VirtIOInput<'a, H: Hal, T: Transport> {
-    transport: &'a mut T,
+    transport: T,
     event_queue: VirtQueue<'a, H>,
     status_queue: VirtQueue<'a, H>,
     event_buf: Box<[InputEvent; 32]>,
@@ -19,7 +19,7 @@ pub struct VirtIOInput<'a, H: Hal, T: Transport> {
 
 impl<'a, H: Hal, T: Transport> VirtIOInput<'a, H, T> {
     /// Create a new VirtIO-Input driver.
-    pub fn new(transport: &'a mut T) -> Result<Self> {
+    pub fn new(mut transport: T) -> Result<Self> {
         let mut event_buf = Box::new([InputEvent::default(); QUEUE_SIZE]);
         transport.begin_init(|features| {
             let features = Feature::from_bits_truncate(features);
@@ -29,8 +29,8 @@ impl<'a, H: Hal, T: Transport> VirtIOInput<'a, H, T> {
             (features & supported_features).bits()
         });
 
-        let mut event_queue = VirtQueue::new(transport, QUEUE_EVENT, QUEUE_SIZE as u16)?;
-        let status_queue = VirtQueue::new(transport, QUEUE_STATUS, QUEUE_SIZE as u16)?;
+        let mut event_queue = VirtQueue::new(&mut transport, QUEUE_EVENT, QUEUE_SIZE as u16)?;
+        let status_queue = VirtQueue::new(&mut transport, QUEUE_STATUS, QUEUE_SIZE as u16)?;
         for (i, event) in event_buf.as_mut().iter_mut().enumerate() {
             let token = event_queue.add(&[], &[event.as_buf_mut()])?;
             assert_eq!(token, i as u16);

+ 4 - 4
src/net.rs

@@ -15,7 +15,7 @@ use volatile::{ReadOnly, Volatile};
 /// outgoing packets are enqueued into another for transmission in that order.
 /// A third command queue is used to control advanced filtering features.
 pub struct VirtIONet<'a, H: Hal, T: Transport> {
-    transport: &'a mut T,
+    transport: T,
     mac: EthernetAddress,
     recv_queue: VirtQueue<'a, H>,
     send_queue: VirtQueue<'a, H>,
@@ -23,7 +23,7 @@ pub struct VirtIONet<'a, H: Hal, T: Transport> {
 
 impl<'a, H: Hal, T: Transport> VirtIONet<'a, H, T> {
     /// Create a new VirtIO-Net driver.
-    pub fn new(transport: &'a mut T) -> Result<Self> {
+    pub fn new(mut transport: T) -> Result<Self> {
         transport.begin_init(|features| {
             let features = Features::from_bits_truncate(features);
             info!("Device features {:?}", features);
@@ -37,8 +37,8 @@ impl<'a, H: Hal, T: Transport> VirtIONet<'a, H, T> {
         debug!("Got MAC={:?}, status={:?}", mac, config.status.read());
 
         let queue_num = 2; // for simplicity
-        let recv_queue = VirtQueue::new(transport, QUEUE_RECEIVE, queue_num)?;
-        let send_queue = VirtQueue::new(transport, QUEUE_TRANSMIT, queue_num)?;
+        let recv_queue = VirtQueue::new(&mut transport, QUEUE_RECEIVE, queue_num)?;
+        let send_queue = VirtQueue::new(&mut transport, QUEUE_TRANSMIT, queue_num)?;
 
         transport.finish_init();