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