porting-android-switch-class 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Staging/Android Switch Class Porting Guide
  2. (linux/drivers/staging/android/switch)
  3. (c) Copyright 2012 Samsung Electronics
  4. AUTHORS
  5. MyungJoo Ham <myungjoo.ham@samsung.com>
  6. /*****************************************************************
  7. * CHAPTER 1. *
  8. * PORTING SWITCH CLASS DEVICE DRIVERS *
  9. *****************************************************************/
  10. ****** STEP 1. Basic Functionality
  11. No extcon extended feature, but switch features only.
  12. - struct switch_dev (fed to switch_dev_register/unregister)
  13. @name: no change
  14. @dev: no change
  15. @index: drop (not used in switch device driver side anyway)
  16. @state: no change
  17. If you have used @state with magic numbers, keep it
  18. at this step.
  19. @print_name: no change but type change (switch_dev->extcon_dev)
  20. @print_state: no change but type change (switch_dev->extcon_dev)
  21. - switch_dev_register(sdev, dev)
  22. => extcon_dev_register(edev, dev)
  23. : no change but type change (sdev->edev)
  24. - switch_dev_unregister(sdev)
  25. => extcon_dev_unregister(edev)
  26. : no change but type change (sdev->edev)
  27. - switch_get_state(sdev)
  28. => extcon_get_state(edev)
  29. : no change but type change (sdev->edev) and (return: int->u32)
  30. - switch_set_state(sdev, state)
  31. => extcon_set_state(edev, state)
  32. : no change but type change (sdev->edev) and (state: int->u32)
  33. With this changes, the ex-switch extcon class device works as it once
  34. worked as switch class device. However, it will now have additional
  35. interfaces (both ABI and in-kernel API) and different ABI locations.
  36. However, if CONFIG_ANDROID is enabled without CONFIG_ANDROID_SWITCH,
  37. /sys/class/switch/* will be symbolically linked to /sys/class/extcon/
  38. so that they are still compatible with legacy userspace processes.
  39. ****** STEP 2. Multistate (no more magic numbers in state value)
  40. Extcon's extended features for switch device drivers with
  41. complex features usually required magic numbers in state
  42. value of switch_dev. With extcon, such magic numbers that
  43. support multiple cables (
  44. 1. Define cable names at edev->supported_cable.
  45. 2. (Recommended) remove print_state callback.
  46. 3. Use extcon_get_cable_state_(edev, index) or
  47. extcon_get_cable_state(edev, cable_name) instead of
  48. extcon_get_state(edev) if you intend to get a state of a specific
  49. cable. Same for set_state. This way, you can remove the usage of
  50. magic numbers in state value.
  51. 4. Use extcon_update_state() if you are updating specific bits of
  52. the state value.
  53. Example: a switch device driver w/ magic numbers for two cables.
  54. "0x00": no cables connected.
  55. "0x01": cable 1 connected
  56. "0x02": cable 2 connected
  57. "0x03": cable 1 and 2 connected
  58. 1. edev->supported_cable = {"1", "2", NULL};
  59. 2. edev->print_state = NULL;
  60. 3. extcon_get_cable_state_(edev, 0) shows cable 1's state.
  61. extcon_get_cable_state(edev, "1") shows cable 1's state.
  62. extcon_set_cable_state_(edev, 1) sets cable 2's state.
  63. extcon_set_cable_state(edev, "2") sets cable 2's state
  64. 4. extcon_update_state(edev, 0x01, 0) sets the least bit's 0.
  65. ****** STEP 3. Notify other device drivers
  66. You can notify others of the cable attach/detach events with
  67. notifier chains.
  68. At the side of other device drivers (the extcon device itself
  69. does not need to get notified of its own events), there are two
  70. methods to register notifier_block for cable events:
  71. (a) for a specific cable or (b) for every cable.
  72. (a) extcon_register_interest(obj, extcon_name, cable_name, nb)
  73. Example: want to get news of "MAX8997_MUIC"'s "USB" cable
  74. obj = kzalloc(sizeof(struct extcon_specific_cable_nb),
  75. GFP_KERNEL);
  76. nb->notifier_call = the_callback_to_handle_usb;
  77. extcon_register_intereset(obj, "MAX8997_MUIC", "USB", nb);
  78. (b) extcon_register_notifier(edev, nb)
  79. Call nb for any changes in edev.
  80. Please note that in order to properly behave with method (a),
  81. the extcon device driver should support multistate feature (STEP 2).
  82. ****** STEP 4. Inter-cable relation (mutually exclusive)
  83. You can provide inter-cable mutually exclusiveness information
  84. for an extcon device. When cables A and B are declared to be mutually
  85. exclusive, the two cables cannot be in ATTACHED state simulteneously.
  86. /*****************************************************************
  87. * CHAPTER 2. *
  88. * PORTING USERSPACE w/ SWITCH CLASS DEVICE SUPPORT *
  89. *****************************************************************/
  90. ****** ABI Location
  91. If "CONFIG_ANDROID" is enabled and "CONFIG_ANDROID_SWITCH" is
  92. disabled, /sys/class/switch/* are created as symbolic links to
  93. /sys/class/extcon/*. Because CONFIG_ANDROID_SWITCH creates
  94. /sys/class/switch directory, we disable symboling linking if
  95. CONFIG_ANDROID_SWITCH is enabled.
  96. The two files of switch class, name and state, are provided with
  97. extcon, too. When the multistate support (STEP 2 of CHAPTER 1.) is
  98. not enabled or print_state callback is supplied, the output of
  99. state ABI is same with switch class.