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