xref: /linux/samples/rust/rust_driver_usb.rs (revision a3f09f8e47c4262510c979b384d6f85d376d91f5)
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     MODULE_USB_TABLE,
23     <SampleDriver as usb::Driver>::IdInfo,
24     [(usb::DeviceId::from_id(0x1234, 0x5678), ()),]
25 );
26 
27 impl usb::Driver for SampleDriver {
28     type IdInfo = ();
29     type Data<'bound> = Self;
30     const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
31 
32     fn probe<'bound>(
33         intf: &'bound usb::Interface<Core<'_>>,
34         _id: &usb::DeviceId,
35         _info: &'bound Self::IdInfo,
36     ) -> impl PinInit<Self, Error> + 'bound {
37         let dev: &device::Device<Core<'_>> = intf.as_ref();
38         dev_info!(dev, "Rust USB driver sample probed\n");
39 
40         Ok(Self { _intf: intf.into() })
41     }
42 
43     fn disconnect<'bound>(intf: &'bound usb::Interface<Core<'_>>, _data: Pin<&Self>) {
44         let dev: &device::Device<Core<'_>> = intf.as_ref();
45         dev_info!(dev, "Rust USB driver sample disconnected\n");
46     }
47 }
48 
49 kernel::module_usb_driver! {
50     type: SampleDriver,
51     name: "rust_driver_usb",
52     authors: ["Daniel Almeida"],
53     description: "Rust USB driver sample",
54     license: "GPL v2",
55 }
56