| 1 | |
| 2 | |
| 3 |
|
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | use crate::{
|
| 9 | bindings,
|
| 10 | device,
|
| 11 | device_id::{
|
| 12 | RawDeviceId,
|
| 13 | RawDeviceIdIndex, |
| 14 | },
|
| 15 | driver,
|
| 16 | error::{
|
| 17 | from_result,
|
| 18 | to_result, |
| 19 | },
|
| 20 | prelude::*,
|
| 21 | sync::aref::AlwaysRefCounted,
|
| 22 | types::Opaque,
|
| 23 | ThisModule, |
| 24 | };
|
| 25 | use core::{
|
| 26 | marker::PhantomData,
|
| 27 | mem::{
|
| 28 | offset_of,
|
| 29 | MaybeUninit, |
| 30 | },
|
| 31 | ptr::NonNull,
|
| 32 | };
|
| 33 |
|
| 34 | |
| 35 | pub struct Adapter<T: Driver>(T);
|
| 36 |
|
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
|
| 43 | type DriverType = bindings::usb_driver;
|
| 44 | type DriverData = T;
|
| 45 | const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
|
| 46 | }
|
| 47 |
|
| 48 | |
| 49 | |
| 50 | unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
|
| 51 | unsafe fn register(
|
| 52 | udrv: &Opaque<Self::DriverType>,
|
| 53 | name: &'static CStr,
|
| 54 | module: &'static ThisModule,
|
| 55 | ) -> Result {
|
| 56 | |
| 57 | unsafe {
|
| 58 | (*udrv.get()).name = name.as_char_ptr();
|
| 59 | (*udrv.get()).probe = Some(Self::probe_callback);
|
| 60 | (*udrv.get()).disconnect = Some(Self::disconnect_callback);
|
| 61 | (*udrv.get()).id_table = T::ID_TABLE.as_ptr();
|
| 62 | }
|
| 63 |
|
| 64 | |
| 65 | to_result(unsafe {
|
| 66 | bindings::usb_register_driver(udrv.get(), module.0, name.as_char_ptr())
|
| 67 | })
|
| 68 | }
|
| 69 |
|
| 70 | unsafe fn unregister(udrv: &Opaque<Self::DriverType>) {
|
| 71 | |
| 72 | unsafe { bindings::usb_deregister(udrv.get()) };
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | impl<T: Driver + 'static> Adapter<T> {
|
| 77 | extern "C" fn probe_callback(
|
| 78 | intf: *mut bindings::usb_interface,
|
| 79 | id: *const bindings::usb_device_id,
|
| 80 | ) -> kernel::ffi::c_int {
|
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal>>() };
|
| 86 |
|
| 87 | from_result(|| {
|
| 88 | |
| 89 | |
| 90 | let id = unsafe { &*id.cast::<DeviceId>() };
|
| 91 |
|
| 92 | let info = T::ID_TABLE.info(id.index());
|
| 93 | let data = T::probe(intf, id, info);
|
| 94 |
|
| 95 | let dev: &device::Device<device::CoreInternal> = intf.as_ref();
|
| 96 | dev.set_drvdata(data)?;
|
| 97 | Ok(0)
|
| 98 | })
|
| 99 | }
|
| 100 |
|
| 101 | extern "C" fn disconnect_callback(intf: *mut bindings::usb_interface) {
|
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal>>() };
|
| 107 |
|
| 108 | let dev: &device::Device<device::CoreInternal> = intf.as_ref();
|
| 109 |
|
| 110 | |
| 111 | |
| 112 | |
| 113 | let data = unsafe { dev.drvdata_borrow::<T>() };
|
| 114 |
|
| 115 | T::disconnect(intf, data);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | |
| 120 | |
| 121 | |
| 122 | #[repr(transparent)]
|
| 123 | #[derive(Clone, Copy)]
|
| 124 | pub struct DeviceId(bindings::usb_device_id);
|
| 125 |
|
| 126 | impl DeviceId {
|
| 127 | |
| 128 | pub const fn from_id(vendor: u16, product: u16) -> Self {
|
| 129 | Self(bindings::usb_device_id {
|
| 130 | match_flags: bindings::USB_DEVICE_ID_MATCH_DEVICE as u16,
|
| 131 | idVendor: vendor,
|
| 132 | idProduct: product,
|
| 133 | |
| 134 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 135 | })
|
| 136 | }
|
| 137 |
|
| 138 | |
| 139 | pub const fn from_device_ver(vendor: u16, product: u16, bcd_lo: u16, bcd_hi: u16) -> Self {
|
| 140 | Self(bindings::usb_device_id {
|
| 141 | match_flags: bindings::USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION as u16,
|
| 142 | idVendor: vendor,
|
| 143 | idProduct: product,
|
| 144 | bcdDevice_lo: bcd_lo,
|
| 145 | bcdDevice_hi: bcd_hi,
|
| 146 | |
| 147 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 148 | })
|
| 149 | }
|
| 150 |
|
| 151 | |
| 152 | pub const fn from_device_info(class: u8, subclass: u8, protocol: u8) -> Self {
|
| 153 | Self(bindings::usb_device_id {
|
| 154 | match_flags: bindings::USB_DEVICE_ID_MATCH_DEV_INFO as u16,
|
| 155 | bDeviceClass: class,
|
| 156 | bDeviceSubClass: subclass,
|
| 157 | bDeviceProtocol: protocol,
|
| 158 | |
| 159 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 160 | })
|
| 161 | }
|
| 162 |
|
| 163 | |
| 164 | pub const fn from_interface_info(class: u8, subclass: u8, protocol: u8) -> Self {
|
| 165 | Self(bindings::usb_device_id {
|
| 166 | match_flags: bindings::USB_DEVICE_ID_MATCH_INT_INFO as u16,
|
| 167 | bInterfaceClass: class,
|
| 168 | bInterfaceSubClass: subclass,
|
| 169 | bInterfaceProtocol: protocol,
|
| 170 | |
| 171 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 172 | })
|
| 173 | }
|
| 174 |
|
| 175 | |
| 176 | pub const fn from_device_interface_class(vendor: u16, product: u16, class: u8) -> Self {
|
| 177 | Self(bindings::usb_device_id {
|
| 178 | match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
|
| 179 | | bindings::USB_DEVICE_ID_MATCH_INT_CLASS) as u16,
|
| 180 | idVendor: vendor,
|
| 181 | idProduct: product,
|
| 182 | bInterfaceClass: class,
|
| 183 | |
| 184 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 185 | })
|
| 186 | }
|
| 187 |
|
| 188 | |
| 189 | pub const fn from_device_interface_protocol(vendor: u16, product: u16, protocol: u8) -> Self {
|
| 190 | Self(bindings::usb_device_id {
|
| 191 | match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
|
| 192 | | bindings::USB_DEVICE_ID_MATCH_INT_PROTOCOL) as u16,
|
| 193 | idVendor: vendor,
|
| 194 | idProduct: product,
|
| 195 | bInterfaceProtocol: protocol,
|
| 196 | |
| 197 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 198 | })
|
| 199 | }
|
| 200 |
|
| 201 | |
| 202 | pub const fn from_device_interface_number(vendor: u16, product: u16, number: u8) -> Self {
|
| 203 | Self(bindings::usb_device_id {
|
| 204 | match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
|
| 205 | | bindings::USB_DEVICE_ID_MATCH_INT_NUMBER) as u16,
|
| 206 | idVendor: vendor,
|
| 207 | idProduct: product,
|
| 208 | bInterfaceNumber: number,
|
| 209 | |
| 210 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 211 | })
|
| 212 | }
|
| 213 |
|
| 214 | |
| 215 | pub const fn from_device_and_interface_info(
|
| 216 | vendor: u16,
|
| 217 | product: u16,
|
| 218 | class: u8,
|
| 219 | subclass: u8,
|
| 220 | protocol: u8,
|
| 221 | ) -> Self {
|
| 222 | Self(bindings::usb_device_id {
|
| 223 | match_flags: (bindings::USB_DEVICE_ID_MATCH_INT_INFO
|
| 224 | | bindings::USB_DEVICE_ID_MATCH_DEVICE) as u16,
|
| 225 | idVendor: vendor,
|
| 226 | idProduct: product,
|
| 227 | bInterfaceClass: class,
|
| 228 | bInterfaceSubClass: subclass,
|
| 229 | bInterfaceProtocol: protocol,
|
| 230 | |
| 231 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 232 | })
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | |
| 237 | |
| 238 | unsafe impl RawDeviceId for DeviceId {
|
| 239 | type RawType = bindings::usb_device_id;
|
| 240 | }
|
| 241 |
|
| 242 | |
| 243 | unsafe impl RawDeviceIdIndex for DeviceId {
|
| 244 | const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::usb_device_id, driver_info);
|
| 245 |
|
| 246 | fn index(&self) -> usize {
|
| 247 | self.0.driver_info
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | |
| 252 | pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
|
| 253 |
|
| 254 | |
| 255 | #[macro_export]
|
| 256 | macro_rules! usb_device_table {
|
| 257 | ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => {
|
| 258 | const $table_name: $crate::device_id::IdArray<
|
| 259 | $crate::usb::DeviceId,
|
| 260 | $id_info_type,
|
| 261 | { $table_data.len() },
|
| 262 | > = $crate::device_id::IdArray::new($table_data);
|
| 263 |
|
| 264 | $crate::module_device_table!("usb", $module_table_name, $table_name);
|
| 265 | };
|
| 266 | }
|
| 267 |
|
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | pub trait Driver {
|
| 304 | |
| 305 | type IdInfo: 'static;
|
| 306 |
|
| 307 | |
| 308 | const ID_TABLE: IdTable<Self::IdInfo>;
|
| 309 |
|
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | fn probe(
|
| 315 | interface: &Interface<device::Core>,
|
| 316 | id: &DeviceId,
|
| 317 | id_info: &Self::IdInfo,
|
| 318 | ) -> impl PinInit<Self, Error>;
|
| 319 |
|
| 320 | |
| 321 | |
| 322 | |
| 323 | fn disconnect(interface: &Interface<device::Core>, data: Pin<&Self>);
|
| 324 | }
|
| 325 |
|
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | #[repr(transparent)]
|
| 339 | pub struct Interface<Ctx: device::DeviceContext = device::Normal>(
|
| 340 | Opaque<bindings::usb_interface>,
|
| 341 | PhantomData<Ctx>,
|
| 342 | );
|
| 343 |
|
| 344 | impl<Ctx: device::DeviceContext> Interface<Ctx> {
|
| 345 | fn as_raw(&self) -> *mut bindings::usb_interface {
|
| 346 | self.0.get()
|
| 347 | }
|
| 348 | }
|
| 349 |
|
| 350 | |
| 351 | |
| 352 | unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Interface<Ctx> {
|
| 353 | const OFFSET: usize = offset_of!(bindings::usb_interface, dev);
|
| 354 | }
|
| 355 |
|
| 356 | |
| 357 | |
| 358 | kernel::impl_device_context_deref!(unsafe { Interface });
|
| 359 | kernel::impl_device_context_into_aref!(Interface);
|
| 360 |
|
| 361 | impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Interface<Ctx> {
|
| 362 | fn as_ref(&self) -> &device::Device<Ctx> {
|
| 363 | |
| 364 | |
| 365 | let dev = unsafe { &raw mut ((*self.as_raw()).dev) };
|
| 366 |
|
| 367 | |
| 368 | unsafe { device::Device::from_raw(dev) }
|
| 369 | }
|
| 370 | }
|
| 371 |
|
| 372 | impl<Ctx: device::DeviceContext> AsRef<Device> for Interface<Ctx> {
|
| 373 | fn as_ref(&self) -> &Device {
|
| 374 | |
| 375 | let usb_dev = unsafe { bindings::interface_to_usbdev(self.as_raw()) };
|
| 376 |
|
| 377 | |
| 378 | |
| 379 | unsafe { &*(usb_dev.cast()) }
|
| 380 | }
|
| 381 | }
|
| 382 |
|
| 383 | |
| 384 | unsafe impl AlwaysRefCounted for Interface {
|
| 385 | fn inc_ref(&self) {
|
| 386 | |
| 387 | |
| 388 | |
| 389 | unsafe { bindings::usb_get_intf(self.as_raw()) };
|
| 390 | }
|
| 391 |
|
| 392 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 393 | |
| 394 | unsafe { bindings::usb_put_intf(obj.cast().as_ptr()) }
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | |
| 399 | unsafe impl Send for Interface {}
|
| 400 |
|
| 401 | |
| 402 | |
| 403 | unsafe impl Sync for Interface {}
|
| 404 |
|
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | #[repr(transparent)]
|
| 418 | struct Device<Ctx: device::DeviceContext = device::Normal>(
|
| 419 | Opaque<bindings::usb_device>,
|
| 420 | PhantomData<Ctx>,
|
| 421 | );
|
| 422 |
|
| 423 | impl<Ctx: device::DeviceContext> Device<Ctx> {
|
| 424 | fn as_raw(&self) -> *mut bindings::usb_device {
|
| 425 | self.0.get()
|
| 426 | }
|
| 427 | }
|
| 428 |
|
| 429 | |
| 430 | |
| 431 | kernel::impl_device_context_deref!(unsafe { Device });
|
| 432 | kernel::impl_device_context_into_aref!(Device);
|
| 433 |
|
| 434 | |
| 435 | unsafe impl AlwaysRefCounted for Device {
|
| 436 | fn inc_ref(&self) {
|
| 437 | |
| 438 | |
| 439 | |
| 440 | unsafe { bindings::usb_get_dev(self.as_raw()) };
|
| 441 | }
|
| 442 |
|
| 443 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 444 | |
| 445 | unsafe { bindings::usb_put_dev(obj.cast().as_ptr()) }
|
| 446 | }
|
| 447 | }
|
| 448 |
|
| 449 | impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
|
| 450 | fn as_ref(&self) -> &device::Device<Ctx> {
|
| 451 | |
| 452 | |
| 453 | let dev = unsafe { &raw mut ((*self.as_raw()).dev) };
|
| 454 |
|
| 455 | |
| 456 | unsafe { device::Device::from_raw(dev) }
|
| 457 | }
|
| 458 | }
|
| 459 |
|
| 460 | |
| 461 | unsafe impl Send for Device {}
|
| 462 |
|
| 463 | |
| 464 | |
| 465 | unsafe impl Sync for Device {}
|
| 466 |
|
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | #[macro_export]
|
| 481 | macro_rules! module_usb_driver {
|
| 482 | ($($f:tt)*) => {
|
| 483 | $crate::module_driver!(<T>, $crate::usb::Adapter<T>, { $($f)* });
|
| 484 | }
|
| 485 | }
|