| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | use crate::{
|
| 7 | acpi,
|
| 8 | container_of,
|
| 9 | device,
|
| 10 | device_id::{
|
| 11 | RawDeviceId,
|
| 12 | RawDeviceIdIndex, |
| 13 | },
|
| 14 | devres::Devres,
|
| 15 | driver,
|
| 16 | error::*,
|
| 17 | of,
|
| 18 | prelude::*,
|
| 19 | sync::aref::{
|
| 20 | ARef,
|
| 21 | AlwaysRefCounted, |
| 22 | },
|
| 23 | types::Opaque, |
| 24 | };
|
| 25 |
|
| 26 | use core::{
|
| 27 | marker::PhantomData,
|
| 28 | mem::offset_of,
|
| 29 | ptr::{
|
| 30 | from_ref,
|
| 31 | NonNull, |
| 32 | }, |
| 33 | };
|
| 34 |
|
| 35 | |
| 36 | #[repr(transparent)]
|
| 37 | #[derive(Clone, Copy)]
|
| 38 | pub struct DeviceId(bindings::i2c_device_id);
|
| 39 |
|
| 40 | impl DeviceId {
|
| 41 | const I2C_NAME_SIZE: usize = 20;
|
| 42 |
|
| 43 | |
| 44 | #[inline(always)]
|
| 45 | pub const fn new(id: &'static CStr) -> Self {
|
| 46 | let src = id.to_bytes_with_nul();
|
| 47 | build_assert!(src.len() <= Self::I2C_NAME_SIZE, "ID exceeds 20 bytes");
|
| 48 | let mut i2c: bindings::i2c_device_id = pin_init::zeroed();
|
| 49 | let mut i = 0;
|
| 50 | while i < src.len() {
|
| 51 | i2c.name[i] = src[i];
|
| 52 | i += 1;
|
| 53 | }
|
| 54 |
|
| 55 | Self(i2c)
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | |
| 60 | |
| 61 | unsafe impl RawDeviceId for DeviceId {
|
| 62 | type RawType = bindings::i2c_device_id;
|
| 63 | }
|
| 64 |
|
| 65 | |
| 66 | unsafe impl RawDeviceIdIndex for DeviceId {
|
| 67 | const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::i2c_device_id, driver_data);
|
| 68 | }
|
| 69 |
|
| 70 | |
| 71 | pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
|
| 72 |
|
| 73 | |
| 74 | #[macro_export]
|
| 75 | macro_rules! i2c_device_table {
|
| 76 | ($($tt:tt)*) => {
|
| 77 | $crate::module_device_table!("i2c", $crate::i2c::DeviceId, $($tt)*);
|
| 78 | };
|
| 79 | }
|
| 80 |
|
| 81 | |
| 82 | pub struct Adapter<T: Driver>(T);
|
| 83 |
|
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
|
| 90 | type DriverType = bindings::i2c_driver;
|
| 91 | type DriverData<'bound> = T::Data<'bound>;
|
| 92 | const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
|
| 93 | }
|
| 94 |
|
| 95 | |
| 96 | |
| 97 | unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
|
| 98 | unsafe fn register(
|
| 99 | idrv: &Opaque<Self::DriverType>,
|
| 100 | name: &'static CStr,
|
| 101 | module: &'static ThisModule,
|
| 102 | ) -> Result {
|
| 103 | build_assert!(
|
| 104 | T::ACPI_ID_TABLE.is_some() || T::OF_ID_TABLE.is_some() || T::I2C_ID_TABLE.is_some(),
|
| 105 | "At least one of ACPI/OF/Legacy tables must be present when registering an i2c driver"
|
| 106 | );
|
| 107 |
|
| 108 | let i2c_table = match T::I2C_ID_TABLE {
|
| 109 | Some(table) => table.as_ptr(),
|
| 110 | None => core::ptr::null(),
|
| 111 | };
|
| 112 |
|
| 113 | let of_table = match T::OF_ID_TABLE {
|
| 114 | Some(table) => table.as_ptr(),
|
| 115 | None => core::ptr::null(),
|
| 116 | };
|
| 117 |
|
| 118 | let acpi_table = match T::ACPI_ID_TABLE {
|
| 119 | Some(table) => table.as_ptr(),
|
| 120 | None => core::ptr::null(),
|
| 121 | };
|
| 122 |
|
| 123 | |
| 124 | unsafe {
|
| 125 | (*idrv.get()).driver.name = name.as_char_ptr();
|
| 126 | (*idrv.get()).probe = Some(Self::probe_callback);
|
| 127 | (*idrv.get()).remove = Some(Self::remove_callback);
|
| 128 | (*idrv.get()).shutdown = Some(Self::shutdown_callback);
|
| 129 | (*idrv.get()).id_table = i2c_table;
|
| 130 | (*idrv.get()).driver.of_match_table = of_table;
|
| 131 | (*idrv.get()).driver.acpi_match_table = acpi_table;
|
| 132 | }
|
| 133 |
|
| 134 | |
| 135 | to_result(unsafe { bindings::i2c_register_driver(module.as_ptr(), idrv.get()) })
|
| 136 | }
|
| 137 |
|
| 138 | unsafe fn unregister(idrv: &Opaque<Self::DriverType>) {
|
| 139 | |
| 140 | unsafe { bindings::i2c_del_driver(idrv.get()) }
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | impl<T: Driver> Adapter<T> {
|
| 145 | extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_int {
|
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
|
| 151 |
|
| 152 | let info = Self::i2c_id_info(idev).or_else(|| {
|
| 153 | |
| 154 | unsafe { <Self as driver::Adapter>::id_info(idev.as_ref()) }
|
| 155 | });
|
| 156 |
|
| 157 | from_result(|| {
|
| 158 | let data = T::probe(idev, info);
|
| 159 |
|
| 160 | idev.as_ref().set_drvdata(data)?;
|
| 161 | Ok(0)
|
| 162 | })
|
| 163 | }
|
| 164 |
|
| 165 | extern "C" fn remove_callback(idev: *mut bindings::i2c_client) {
|
| 166 | |
| 167 | let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
|
| 168 |
|
| 169 | |
| 170 | |
| 171 | |
| 172 | let data = unsafe { idev.as_ref().drvdata_borrow::<T::Data<'_>>() };
|
| 173 |
|
| 174 | T::unbind(idev, data);
|
| 175 | }
|
| 176 |
|
| 177 | extern "C" fn shutdown_callback(idev: *mut bindings::i2c_client) {
|
| 178 | |
| 179 | let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
|
| 180 |
|
| 181 | |
| 182 | |
| 183 | |
| 184 | let data = unsafe { idev.as_ref().drvdata_borrow::<T::Data<'_>>() };
|
| 185 |
|
| 186 | T::shutdown(idev, data);
|
| 187 | }
|
| 188 |
|
| 189 | |
| 190 | fn i2c_id_table() -> Option<IdTable<<Self as driver::Adapter>::IdInfo>> {
|
| 191 | T::I2C_ID_TABLE
|
| 192 | }
|
| 193 |
|
| 194 | |
| 195 | |
| 196 | |
| 197 | fn i2c_id_info(dev: &I2cClient) -> Option<&'static <Self as driver::Adapter>::IdInfo> {
|
| 198 | let table = Self::i2c_id_table()?;
|
| 199 |
|
| 200 | |
| 201 | |
| 202 | |
| 203 | let raw_id = unsafe { bindings::i2c_match_id(table.as_ptr(), dev.as_raw()) };
|
| 204 |
|
| 205 | if raw_id.is_null() {
|
| 206 | return None;
|
| 207 | }
|
| 208 |
|
| 209 | |
| 210 | |
| 211 | let id = unsafe { &*raw_id.cast::<DeviceId>() };
|
| 212 |
|
| 213 | |
| 214 | Some(unsafe { id.info_unchecked::<T::IdInfo>() })
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | impl<T: Driver> driver::Adapter for Adapter<T> {
|
| 219 | type IdInfo = T::IdInfo;
|
| 220 |
|
| 221 | fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {
|
| 222 | T::OF_ID_TABLE
|
| 223 | }
|
| 224 |
|
| 225 | fn acpi_id_table() -> Option<acpi::IdTable<Self::IdInfo>> {
|
| 226 | T::ACPI_ID_TABLE
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | #[macro_export]
|
| 244 | macro_rules! module_i2c_driver {
|
| 245 | ($($f:tt)*) => {
|
| 246 | $crate::module_driver!(<T>, $crate::i2c::Adapter<T>, { $($f)* });
|
| 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 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | pub trait Driver {
|
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | type IdInfo: 'static;
|
| 314 |
|
| 315 | |
| 316 | type Data<'bound>: Send + 'bound;
|
| 317 |
|
| 318 | |
| 319 | const I2C_ID_TABLE: Option<IdTable<Self::IdInfo>> = None;
|
| 320 |
|
| 321 | |
| 322 | const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
|
| 323 |
|
| 324 | |
| 325 | const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = None;
|
| 326 |
|
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | fn probe<'bound>(
|
| 332 | dev: &'bound I2cClient<device::Core<'_>>,
|
| 333 | id_info: Option<&'bound Self::IdInfo>,
|
| 334 | ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
|
| 335 |
|
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | fn shutdown<'bound>(dev: &'bound I2cClient<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
|
| 348 | let _ = (dev, this);
|
| 349 | }
|
| 350 |
|
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | fn unbind<'bound>(dev: &'bound I2cClient<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
|
| 362 | let _ = (dev, this);
|
| 363 | }
|
| 364 | }
|
| 365 |
|
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | #[repr(transparent)]
|
| 377 | pub struct I2cAdapter<Ctx: device::DeviceContext = device::Normal>(
|
| 378 | Opaque<bindings::i2c_adapter>,
|
| 379 | PhantomData<Ctx>,
|
| 380 | );
|
| 381 |
|
| 382 | impl<Ctx: device::DeviceContext> I2cAdapter<Ctx> {
|
| 383 | fn as_raw(&self) -> *mut bindings::i2c_adapter {
|
| 384 | self.0.get()
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 | impl I2cAdapter {
|
| 389 | |
| 390 | #[inline]
|
| 391 | pub fn index(&self) -> i32 {
|
| 392 | |
| 393 | unsafe { (*self.as_raw()).nr }
|
| 394 | }
|
| 395 |
|
| 396 | |
| 397 | #[inline]
|
| 398 | pub fn get(index: i32) -> Result<ARef<Self>> {
|
| 399 | |
| 400 | |
| 401 | |
| 402 | let adapter = NonNull::new(unsafe { bindings::i2c_get_adapter(index) }).ok_or(ENODEV)?;
|
| 403 |
|
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | Ok(unsafe { ARef::from_raw(adapter.cast::<I2cAdapter<device::Normal>>()) })
|
| 409 | }
|
| 410 | }
|
| 411 |
|
| 412 | |
| 413 | |
| 414 | kernel::impl_device_context_deref!(unsafe { I2cAdapter });
|
| 415 | kernel::impl_device_context_into_aref!(I2cAdapter);
|
| 416 |
|
| 417 | |
| 418 | unsafe impl AlwaysRefCounted for I2cAdapter {
|
| 419 | #[inline]
|
| 420 | fn inc_ref(&self) {
|
| 421 | |
| 422 | unsafe { bindings::i2c_get_adapter(self.index()) };
|
| 423 | }
|
| 424 |
|
| 425 | #[inline]
|
| 426 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 427 | |
| 428 | unsafe { bindings::i2c_put_adapter(obj.as_ref().as_raw()) }
|
| 429 | }
|
| 430 | }
|
| 431 |
|
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | #[repr(transparent)]
|
| 437 | pub struct I2cBoardInfo(bindings::i2c_board_info);
|
| 438 |
|
| 439 | impl I2cBoardInfo {
|
| 440 | const I2C_TYPE_SIZE: usize = 20;
|
| 441 | |
| 442 | #[inline(always)]
|
| 443 | pub const fn new(type_: &'static CStr, addr: u16) -> Self {
|
| 444 | let src = type_.to_bytes_with_nul();
|
| 445 | build_assert!(src.len() <= Self::I2C_TYPE_SIZE, "Type exceeds 20 bytes");
|
| 446 | let mut i2c_board_info: bindings::i2c_board_info = pin_init::zeroed();
|
| 447 | let mut i: usize = 0;
|
| 448 | while i < src.len() {
|
| 449 | i2c_board_info.type_[i] = src[i];
|
| 450 | i += 1;
|
| 451 | }
|
| 452 |
|
| 453 | i2c_board_info.addr = addr;
|
| 454 | Self(i2c_board_info)
|
| 455 | }
|
| 456 |
|
| 457 | fn as_raw(&self) -> *const bindings::i2c_board_info {
|
| 458 | from_ref(&self.0)
|
| 459 | }
|
| 460 | }
|
| 461 |
|
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | #[repr(transparent)]
|
| 473 | pub struct I2cClient<Ctx: device::DeviceContext = device::Normal>(
|
| 474 | Opaque<bindings::i2c_client>,
|
| 475 | PhantomData<Ctx>,
|
| 476 | );
|
| 477 |
|
| 478 | impl<Ctx: device::DeviceContext> I2cClient<Ctx> {
|
| 479 | fn as_raw(&self) -> *mut bindings::i2c_client {
|
| 480 | self.0.get()
|
| 481 | }
|
| 482 | }
|
| 483 |
|
| 484 | |
| 485 | |
| 486 | unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for I2cClient<Ctx> {
|
| 487 | const OFFSET: usize = offset_of!(bindings::i2c_client, dev);
|
| 488 | }
|
| 489 |
|
| 490 | |
| 491 | |
| 492 | kernel::impl_device_context_deref!(unsafe { I2cClient });
|
| 493 | kernel::impl_device_context_into_aref!(I2cClient);
|
| 494 |
|
| 495 | |
| 496 | unsafe impl AlwaysRefCounted for I2cClient {
|
| 497 | fn inc_ref(&self) {
|
| 498 | |
| 499 | unsafe { bindings::get_device(self.as_ref().as_raw()) };
|
| 500 | }
|
| 501 |
|
| 502 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 503 | |
| 504 | unsafe { bindings::put_device(&raw mut (*obj.as_ref().as_raw()).dev) }
|
| 505 | }
|
| 506 | }
|
| 507 |
|
| 508 | impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for I2cClient<Ctx> {
|
| 509 | fn as_ref(&self) -> &device::Device<Ctx> {
|
| 510 | let raw = self.as_raw();
|
| 511 | |
| 512 | |
| 513 | let dev = unsafe { &raw mut (*raw).dev };
|
| 514 |
|
| 515 | |
| 516 | unsafe { device::Device::from_raw(dev) }
|
| 517 | }
|
| 518 | }
|
| 519 |
|
| 520 | impl<Ctx: device::DeviceContext> TryFrom<&device::Device<Ctx>> for &I2cClient<Ctx> {
|
| 521 | type Error = kernel::error::Error;
|
| 522 |
|
| 523 | fn try_from(dev: &device::Device<Ctx>) -> Result<Self, Self::Error> {
|
| 524 | |
| 525 | |
| 526 | if unsafe { bindings::i2c_verify_client(dev.as_raw()).is_null() } {
|
| 527 | return Err(EINVAL);
|
| 528 | }
|
| 529 |
|
| 530 | |
| 531 | |
| 532 | |
| 533 | let idev = unsafe { container_of!(dev.as_raw(), bindings::i2c_client, dev) };
|
| 534 |
|
| 535 | |
| 536 | Ok(unsafe { &*idev.cast() })
|
| 537 | }
|
| 538 | }
|
| 539 |
|
| 540 | |
| 541 | unsafe impl Send for I2cClient {}
|
| 542 |
|
| 543 | |
| 544 | |
| 545 | unsafe impl Sync for I2cClient {}
|
| 546 |
|
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | #[repr(transparent)]
|
| 557 | pub struct Registration(NonNull<bindings::i2c_client>);
|
| 558 |
|
| 559 | impl Registration {
|
| 560 | |
| 561 | pub fn new<'a>(
|
| 562 | i2c_adapter: &I2cAdapter,
|
| 563 | i2c_board_info: &I2cBoardInfo,
|
| 564 | parent_dev: &'a device::Device<device::Bound>,
|
| 565 | ) -> impl PinInit<Devres<Self>, Error> + 'a {
|
| 566 | Devres::new(parent_dev, Self::try_new(i2c_adapter, i2c_board_info))
|
| 567 | }
|
| 568 |
|
| 569 | fn try_new(i2c_adapter: &I2cAdapter, i2c_board_info: &I2cBoardInfo) -> Result<Self> {
|
| 570 | |
| 571 | |
| 572 | |
| 573 | let raw_dev = from_err_ptr(unsafe {
|
| 574 | bindings::i2c_new_client_device(i2c_adapter.as_raw(), i2c_board_info.as_raw())
|
| 575 | })?;
|
| 576 |
|
| 577 | let dev_ptr = NonNull::new(raw_dev).ok_or(ENODEV)?;
|
| 578 |
|
| 579 | Ok(Self(dev_ptr))
|
| 580 | }
|
| 581 | }
|
| 582 |
|
| 583 | impl Drop for Registration {
|
| 584 | fn drop(&mut self) {
|
| 585 | |
| 586 | |
| 587 | unsafe { bindings::i2c_unregister_device(self.0.as_ptr()) }
|
| 588 | }
|
| 589 | }
|
| 590 |
|
| 591 | |
| 592 | unsafe impl Send for Registration {}
|
| 593 |
|
| 594 | |
| 595 | |
| 596 | unsafe impl Sync for Registration {}
|