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