xref: /linux/samples/rust/rust_driver_usb.rs (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
1 // SPDX-License-Identifier: GPL-2.0
2 // SPDX-FileCopyrightText: Copyright (C) 2025 Collabora Ltd.
3 
4 //! Rust USB driver sample.
5 
6 use kernel::{
7     device::{
8         self,
9         Core, //
10     },
11     prelude::*,
12     sync::aref::ARef,
13     usb, //
14 };
15 
16 struct SampleDriver {
17     _intf: ARef<usb::Interface>,
18 }
19 
20 kernel::usb_device_table!(
21     USB_TABLE,
22     <SampleDriver as usb::Driver>::IdInfo,
23     [(usb::DeviceId::from_id(0x1234, 0x5678), ()),]
24 );
25 
26 impl usb::Driver for SampleDriver {
27     type IdInfo = ();
28     type Data<'bound> = Self;
29     const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
30 
31     fn probe<'bound>(
32         intf: &'bound usb::Interface<Core<'_>>,
33         _id: &usb::DeviceId,
34         _info: Option<&'bound Self::IdInfo>,
35     ) -> impl PinInit<Self, Error> + 'bound {
36         let dev: &device::Device<Core<'_>> = intf.as_ref();
37         dev_info!(dev, "Rust USB driver sample probed\n");
38 
39         Ok(Self { _intf: intf.into() })
40     }
41 
42     fn disconnect<'bound>(intf: &'bound usb::Interface<Core<'_>>, _data: Pin<&Self>) {
43         let dev: &device::Device<Core<'_>> = intf.as_ref();
44         dev_info!(dev, "Rust USB driver sample disconnected\n");
45     }
46 }
47 
48 kernel::module_usb_driver! {
49     type: SampleDriver,
50     name: "rust_driver_usb",
51     authors: ["Daniel Almeida"],
52     description: "Rust USB driver sample",
53     license: "GPL v2",
54 }
55