• 嵌入式Android系统耳机驱动基本知识


         开发项目的时候,接手的第一个驱动就是android平台下耳机的插拔检测和按键检测​。这部分涉及的硬件知识比较简单,但是软件上对中断的处理,软件检测的鲁棒性,都有比较高的要求,涉及到驱动开发中经常使用的中断申请,工作队列,tasklet,竟态和同步,linux input子系统,android 键值映射等知识。

    1.耳机的通用接口为一个裸露的圆柱体,从头端到线侧的直径依次增大,并通过橡胶环进行绝缘而设计,这样方便无论从哪个角度都可以插入。在耳机座上,通过弹片和耳机头的金属环触而形成电路导通。

    2.市面上流通的耳机从接口大小上分3.5mm和2.5mm两种,主要适配不同尺寸的插口,比较常见的是3.5mm。从接口电气特性上分三段式和四段式,四段式在三段耳机的基础上增加了mic端——耳机内部的一个声电转化装置。

    • 三段式耳机接口从头部到线一侧的定义式左声道,右声道,GND。
    • 四段式耳机分美标(CTIA)和欧标(国内要求为欧标,OMTP-Open Mobile Terminal Platform开放移动终端平台),主要区别在于耳机线侧最后两端的定义,美标为左声道(L),右声道(R),GND(G),MIC(M),括号内为缩写,下面为清晰主要采用缩写,国标为L,R,M,G。

    3.从耳机识别的角度来讲,耳机上的电声转化装置(左声道听音器和右声道听音器)可以认为是一个16欧或者32欧的电阻,电阻值根据耳机厂商的设计而不同,一般的标准为16欧或者32欧,但有些比较好的耳机这个内阻值比较大;mic端可以认为是一个大电阻(通常为1k欧)和一个开关(多按键耳机可以认为好多个开关串上不同组值得电阻)。

    耳机标准-美标 (CTIA,通常称为美标)

    从插入端到线分别是: 左声道,右声道,GND,MIC。耳机上德绝缘橡胶环一般是白色的 代表品牌:iphone,MOTO,小米,魅族,索尼

    耳机接口标准 (OMTP,通常称为欧标)

    从插入端到线分别是: 左声道,右声道,MIC,GND。耳机上德绝缘橡胶环一般是黑色的 代表品牌:诺基亚,三星,HTC

    相应的,耳机座也分为支持欧标设计的耳机座和支持美标设计的耳机座。另外,从耳机座左声道的检测方式来又可以分为 “Nomally-closed type”(常闭型) 和 “Normally-open type”(常开型) 两种。其简易设计如下图

    图中所示的耳机座为美标的。

    • 在常闭型中,不接耳机时,耳机座左声道和检测端HS-DET接触,插入耳机时,HS-DET与HPH-L不导通。

    • 在常开型中,不接耳机时,耳机座左声道和检测端HS-DET不接触,插入耳机时,HS-DET与HPH-L导通。

    下图是一个高通平台下耳机座设计的原理图

    可以看到,该耳机座为常开型,采用了左声道检测的机制——CDC_HS_DET为插入耳机触发硬件中断的的管脚。当没有插入耳机时,由于CDC_HS_DET悬空,而该网络对应的平台端的gpio(输入状态)口为低电平。当插入耳机后,由于耳机左声道内部相当于1个16欧的电阻和GND相接,于是有如下的模拟图:

    正常情况下,CDC_HPH_L会有一点电压存在,通过电阻的分压,于是CDC_HS_DET接收到了高电平,引起了软件中断。软件上通过debounce后,检测到持续的高电平,于是认为有耳机插入。这时候需要判断,插入的是三段还是四段。平台上打开mic_bias,当插入的是三段耳机时,MIC_IN2_P端口被拉低(忽略原理图R3501处的NC,应该是个笔误),于是判断为三段耳机。若为四段耳机,MIC_IN2_P的电平接近于MIC_BIAS,软件判断该处的直流电压之后设置识别了四段耳机。当按键按下时,MIC_IN2_P的电压发生变化,触发了系统中断,之后软件通过采样该处的电压值判断按键阻值而确定按下了哪一个按键。

    一般的,一键耳机按下后电阻值在10欧以下,三键带音量加减的耳机上键的电阻范围在60欧到100欧之间,中键在10欧以下,下键在120欧~200欧之间。

    我接触过四个平台的耳机驱动,mtk、高通、Nividia和spreadtrum。除了高通将检测耳机插拔的事件也申请为input设备外,,其他平台都注册为switch/h2w设备。mtk平台的耳机驱动称为ACCDET+EINT的模式,高通的机制叫做MBHC,都是一套看起来特别麻烦的机制。而展讯的code将耳机驱动作为misc下得一个设备驱动来用,很体现linux “write code do one thing and do it well”的哲理。下面来看看展讯的耳机驱动。

    headset.h

    1. /*
    2. * Copyright (C) 2012 Spreadtrum Communications Inc.
    3. *
    4. * This software is licensed under the terms of the GNU General Public
    5. * License version 2, as published by the Free Software Foundation, and
    6. * may be copied, distributed, and modified under those terms.
    7. *
    8. * This program is distributed in the hope that it will be useful,
    9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11. * GNU General Public License for more details.
    12. */
    13. #ifndef __HEADSET_H__
    14. #define __HEADSET_H__
    15. #include <linux/switch.h>
    16. #include <linux/input.h>
    17. #include <linux/platform_device.h>
    18. enum {
    19. BIT_HEADSET_OUT = 0,
    20. BIT_HEADSET_MIC = (1 << 0),
    21. BIT_HEADSET_NO_MIC = (1 << 1),
    22. };
    23. enum {
    24. HEADSET_BUTTON_DOWN_INVALID = -1,
    25. HEADSET_BUTTON_DOWN_SHORT,
    26. HEADSET_BUTTON_DOWN_LONG,
    27. };
    28. struct _headset_gpio {
    29. int active_low;
    30. int gpio;
    31. int irq;
    32. unsigned int irq_type_active;
    33. unsigned int irq_type_inactive;
    34. int debounce;
    35. int debounce_sw;
    36. int holded;
    37. int active;
    38. int irq_enabled;
    39. const char *desc;
    40. struct _headset *parent;
    41. unsigned int timeout_ms;
    42. struct hrtimer timer;
    43. enum hrtimer_restart (*callback)(int active, struct _headset_gpio *hgp);
    44. };
    45. struct _headset_keycap {
    46. unsigned int type;
    47. unsigned int key;
    48. };
    49. struct _headset_button {
    50. struct _headset_keycap cap[15];
    51. unsigned int (*headset_get_button_code_board_method)(int v);
    52. unsigned int (*headset_map_code2push_code_board_method)(unsigned int code, int push_type);
    53. };
    54. struct _headset {
    55. struct switch_dev sdev;
    56. struct input_dev *input;
    57. struct _headset_gpio detect;
    58. struct _headset_gpio button;
    59. int headphone;
    60. int type;
    61. struct work_struct switch_work;
    62. struct workqueue_struct * switch_workqueue;
    63. };
    64. #ifndef ARRY_SIZE
    65. #define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
    66. #endif
    67. #endif

    headset.c

    1. /*
    2. * Copyright (C) 2012 Spreadtrum Communications Inc.
    3. *
    4. * This software is licensed under the terms of the GNU General Public
    5. * License version 2, as published by the Free Software Foundation, and
    6. * may be copied, distributed, and modified under those terms.
    7. *
    8. * This program is distributed in the hope that it will be useful,
    9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11. * GNU General Public License for more details.
    12. */
    13. #include <linux/interrupt.h>
    14. #include <linux/irq.h>
    15. #include <linux/delay.h>
    16. #include <mach/gpio.h>
    17. #include <linux/headset.h>
    18. #include <mach/board.h>
    19. #ifndef HEADSET_DETECT_GPIO
    20. #define HEADSET_DETECT_GPIO 165
    21. #endif
    22. #ifndef HEADSET_BUTTON_GPIO
    23. #define HEADSET_BUTTON_GPIO 164
    24. #endif
    25. #ifndef HEADSET_DETECT_GPIO_ACTIVE_LOW
    26. #define HEADSET_DETECT_GPIO_ACTIVE_LOW 1
    27. #endif
    28. #ifndef HEADSET_BUTTON_GPIO_ACTIVE_LOW
    29. #define HEADSET_BUTTON_GPIO_ACTIVE_LOW 0
    30. #endif
    31. #ifndef HEADSET_DETECT_GPIO_DEBOUNCE_SW
    32. #define HEADSET_DETECT_GPIO_DEBOUNCE_SW 1000
    33. #endif
    34. #ifndef HEADSET_BUTTON_GPIO_DEBOUNCE_SW
    35. #define HEADSET_BUTTON_GPIO_DEBOUNCE_SW 100
    36. #endif
    37. static enum hrtimer_restart report_headset_button_status(int active, struct _headset_gpio *hgp);
    38. static enum hrtimer_restart report_headset_detect_status(int active, struct _headset_gpio *hgp);
    39. static struct _headset headset = {
    40. .sdev = {
    41. .name = "h2w",
    42. },
    43. .detect = {
    44. .desc = "headset detect",
    45. .active_low = HEADSET_DETECT_GPIO_ACTIVE_LOW,
    46. .gpio = HEADSET_DETECT_GPIO,
    47. .debounce = 0,
    48. .debounce_sw = HEADSET_DETECT_GPIO_DEBOUNCE_SW,
    49. .irq_enabled = 1,
    50. .callback = report_headset_detect_status,
    51. },
    52. .button = {
    53. .desc = "headset button",
    54. .active_low = HEADSET_BUTTON_GPIO_ACTIVE_LOW,
    55. .gpio = HEADSET_BUTTON_GPIO,
    56. .debounce = 0,
    57. .debounce_sw = HEADSET_BUTTON_GPIO_DEBOUNCE_SW,
    58. .irq_enabled = 1,
    59. .callback = report_headset_button_status,
    60. .timeout_ms = 800, /* 800ms for long button down */
    61. },
    62. };
    63. #ifndef headset_gpio_init
    64. #define headset_gpio_init(gpio, desc) \
    65. do { \
    66. gpio_request(gpio, desc); \
    67. gpio_direction_input(gpio); \
    68. } while (0)
    69. #endif
    70. #ifndef headset_gpio_free
    71. #define headset_gpio_free(gpio) \
    72. gpio_free(gpio)
    73. #endif
    74. #ifndef headset_gpio2irq_free
    75. #define headset_gpio2irq_free(irq, args) { }
    76. #endif
    77. #ifndef headset_gpio2irq
    78. #define headset_gpio2irq(gpio) \
    79. gpio_to_irq(gpio)
    80. #endif
    81. #ifndef headset_gpio_set_irq_type
    82. #define headset_gpio_set_irq_type(irq, type) \
    83. irq_set_irq_type(irq, type)
    84. #endif
    85. #ifndef headset_gpio_get_value
    86. #define headset_gpio_get_value(gpio) \
    87. gpio_get_value(gpio)
    88. #endif
    89. #ifndef headset_gpio_debounce
    90. #define headset_gpio_debounce(gpio, ms) \
    91. gpio_set_debounce(gpio, ms)
    92. #endif
    93. #ifndef headset_hook_detect
    94. #define headset_hook_detect(status) { }
    95. #endif
    96. #define HEADSET_DEBOUNCE_ROUND_UP(dw) \
    97. dw = (((dw ? dw : 1) + HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD - 1) / \
    98. HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD) * HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD;
    99. static struct _headset_keycap headset_key_capability[20] = {
    100. { EV_KEY, KEY_MEDIA },
    101. { EV_KEY, KEY_END },
    102. { EV_KEY, KEY_RESERVED },
    103. };
    104. static unsigned int (*headset_get_button_code_board_method)(int v);
    105. static unsigned int (*headset_map_code2push_code_board_method)(unsigned int code, int push_type);
    106. static __devinit int headset_button_probe(struct platform_device *pdev)
    107. {
    108. struct _headset_button *headset_button = platform_get_drvdata(pdev);
    109. headset_get_button_code_board_method = headset_button->headset_get_button_code_board_method;
    110. headset_map_code2push_code_board_method = headset_button->headset_map_code2push_code_board_method;
    111. memcpy(headset_key_capability, headset_button->cap, sizeof headset_button->cap);
    112. return 0;
    113. }
    114. static struct platform_driver headset_button_driver = {
    115. .driver = {
    116. .name = "headset-button",
    117. .owner = THIS_MODULE,
    118. },
    119. .probe = headset_button_probe,
    120. };
    121. static unsigned int headset_get_button_code(int v)
    122. {
    123. unsigned int code;
    124. if (headset_get_button_code_board_method)
    125. code = headset_get_button_code_board_method(v);
    126. else
    127. code = KEY_MEDIA;
    128. return code;
    129. }
    130. static unsigned int headset_map_code2key_type(unsigned int code)
    131. {
    132. unsigned int key_type = EV_KEY;
    133. int i;
    134. for(i = 0; headset_key_capability[i].key != KEY_RESERVED &&
    135. headset_key_capability[i].key != code && i < ARRY_SIZE(headset_key_capability); i++);
    136. if (i < ARRY_SIZE(headset_key_capability) &&
    137. headset_key_capability[i].key == code)
    138. key_type = headset_key_capability[i].type;
    139. else
    140. pr_err("headset not find code [0x%x]'s maping type\n", code);
    141. return key_type;
    142. }
    143. static unsigned int headset_map_code2push_code(unsigned int code, int push_type)
    144. {
    145. if (headset_map_code2push_code_board_method)
    146. return headset_map_code2push_code_board_method(code, push_type);
    147. switch (push_type) {
    148. case HEADSET_BUTTON_DOWN_SHORT:
    149. code = KEY_MEDIA;
    150. break;
    151. case HEADSET_BUTTON_DOWN_LONG:
    152. code = KEY_END;
    153. break;
    154. }
    155. return code;
    156. }
    157. /*tangyao modified on 2013-01-25*/
    158. static void headset_gpio_irq_enable(int enable, struct _headset_gpio *hgp);
    159. #define HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD 50 /* 10 */
    160. static enum hrtimer_restart report_headset_button_status(int active, struct _headset_gpio *hgp)
    161. {
    162. enum hrtimer_restart restart;
    163. static int step = 0;
    164. if (active < 0) {
    165. step = 0;
    166. return HRTIMER_NORESTART;
    167. }
    168. if (active) {
    169. restart = HRTIMER_RESTART;
    170. if (++step > 3)
    171. step = 0;
    172. switch (step) {
    173. case 1:
    174. /*short press report*/
    175. input_event(hgp->parent->input,EV_KEY,KEY_MEDIA, 1);
    176. input_sync(hgp->parent->input);
    177. break;
    178. case 2:
    179. /*long press report,first report short press release,then long press start*/
    180. input_event(hgp->parent->input,EV_KEY,KEY_MEDIA, 0);
    181. input_sync(hgp->parent->input);
    182. input_event(hgp->parent->input,EV_KEY,KEY_END, 1);
    183. input_sync(hgp->parent->input);
    184. break;
    185. default:
    186. pr_info("Are you press too long? step = %d\n",step);
    187. }
    188. } else {
    189. restart = HRTIMER_NORESTART;
    190. if (step == 1){
    191. /*short press release report*/
    192. input_event(hgp->parent->input,EV_KEY,KEY_MEDIA, 0);
    193. input_sync(hgp->parent->input);
    194. }else{
    195. /*long press release report*/
    196. input_event(hgp->parent->input,EV_KEY,KEY_END, 0);
    197. input_sync(hgp->parent->input);
    198. }
    199. step = 0;
    200. }
    201. return restart;
    202. }
    203. static enum hrtimer_restart report_headset_detect_status(int active, struct _headset_gpio *hgp)
    204. {
    205. struct _headset * ht = hgp->parent;
    206. if (active) {
    207. headset_hook_detect(1);
    208. ht->headphone = 0;
    209. /*headphone support,tangyao modified on 2012-01-25*/
    210. ht->headphone = ht->button.active_low ^ headset_gpio_get_value(ht->button.gpio);
    211. if (ht->headphone) {
    212. ht->type = BIT_HEADSET_NO_MIC;
    213. queue_work(ht->switch_workqueue, &ht->switch_work);
    214. pr_info("headphone plug in\n");
    215. } else {
    216. ht->type = BIT_HEADSET_MIC;
    217. queue_work(ht->switch_workqueue, &ht->switch_work);
    218. pr_info("headset plug in\n");
    219. headset_gpio_set_irq_type(ht->button.irq, ht->button.irq_type_active);
    220. headset_gpio_irq_enable(1, &ht->button);
    221. }
    222. } else {
    223. headset_gpio_irq_enable(0, &ht->button);
    224. ht->button.callback(-1, &ht->button);
    225. headset_hook_detect(0);
    226. if (ht->headphone)
    227. pr_info("headphone plug out\n");
    228. else
    229. pr_info("headset plug out\n");
    230. ht->type = BIT_HEADSET_OUT;
    231. queue_work(ht->switch_workqueue, &ht->switch_work);
    232. }
    233. /* use below code only when gpio irq misses state, because of the dithering */
    234. headset_gpio_set_irq_type(hgp->irq, active ? hgp->irq_type_inactive : hgp->irq_type_active);
    235. return HRTIMER_NORESTART;
    236. }
    237. static enum hrtimer_restart headset_gpio_timer_func(struct hrtimer *timer)
    238. {
    239. enum hrtimer_restart restart = HRTIMER_RESTART;
    240. struct _headset_gpio *hgp =
    241. container_of(timer, struct _headset_gpio, timer);
    242. int active = hgp->active_low ^ headset_gpio_get_value(hgp->gpio); /* hgp->active */
    243. int green_ch = (!active && &hgp->parent->detect == hgp);
    244. if (active != hgp->active) {
    245. pr_info("The value %s mismatch [%d:%d] at %dms!\n",
    246. hgp->desc, active, hgp->active, hgp->holded);
    247. hgp->holded = 0;
    248. }
    249. pr_debug("%s : %s %s green_ch[%d], holed=%d, debounce_sw=%d\n", __func__,
    250. hgp->desc, active ? "active" : "inactive", green_ch, hgp->holded, hgp->debounce_sw);
    251. hgp->holded += HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD;
    252. if (hgp->holded >= hgp->debounce_sw || green_ch) {
    253. if (hgp->holded == hgp->debounce_sw || \
    254. hgp->holded == hgp->timeout_ms || \
    255. green_ch) {
    256. pr_debug("call headset gpio handler\n");
    257. restart = hgp->callback(active, hgp);
    258. } else
    259. pr_debug("gpio <%d> has kept active for %d ms\n", hgp->gpio, hgp->holded);
    260. }
    261. if (restart == HRTIMER_RESTART)
    262. hrtimer_forward_now(timer,
    263. ktime_set(HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD / 1000,
    264. (HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD % 1000) * 1000000)); /* repeat timer */
    265. return restart;
    266. }
    267. static irqreturn_t headset_gpio_irq_handler(int irq, void *dev)
    268. {
    269. struct _headset_gpio *hgp = dev;
    270. hrtimer_cancel(&hgp->timer);
    271. hgp->active = hgp->active_low ^ headset_gpio_get_value(hgp->gpio);
    272. headset_gpio_set_irq_type(hgp->irq, hgp->active ? hgp->irq_type_inactive : hgp->irq_type_active);
    273. pr_debug("%s : %s %s\n", __func__, hgp->desc, hgp->active ? "active" : "inactive");
    274. hgp->holded = 0;
    275. hrtimer_start(&hgp->timer,
    276. ktime_set(HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD / 1000,
    277. (HEADSET_GPIO_DEBOUNCE_SW_SAMPLE_PERIOD % 1000) * 1000000),
    278. HRTIMER_MODE_REL);
    279. return IRQ_HANDLED;
    280. }
    281. static void headset_gpio_irq_enable(int enable, struct _headset_gpio *hgp)
    282. {
    283. int action = 0;
    284. if (enable) {
    285. if (!hgp->irq_enabled) {
    286. hrtimer_cancel(&hgp->timer);
    287. hgp->irq_enabled = 1;
    288. action = 1;
    289. hgp->holded = 0;
    290. enable_irq(hgp->irq);
    291. }
    292. } else {
    293. if (hgp->irq_enabled) {
    294. disable_irq(hgp->irq);
    295. hrtimer_cancel(&hgp->timer);
    296. hgp->irq_enabled = 0;
    297. action = 1;
    298. hgp->holded = 0;
    299. }
    300. }
    301. pr_info("%s [ irq=%d ] --- %saction %s\n", __func__, hgp->irq_enabled, action ? "do " : "no ", hgp->desc);
    302. }
    303. static void headset_switch_state(struct work_struct *work)
    304. {
    305. struct _headset *ht;
    306. int type;
    307. ht = container_of(work, struct _headset, switch_work);
    308. type = ht->type;
    309. switch_set_state(&headset.sdev, type);
    310. pr_info("set headset state to %d\n", type);
    311. }
    312. static int __init headset_init(void)
    313. {
    314. int ret, i;
    315. struct _headset *ht = &headset;
    316. ret = switch_dev_register(&ht->sdev);
    317. if (ret < 0) {
    318. pr_err("switch_dev_register failed!\n");
    319. return ret;
    320. }
    321. platform_driver_register(&headset_button_driver);
    322. ht->input = input_allocate_device();
    323. if (ht->input == NULL) {
    324. pr_err("switch_dev_register failed!\n");
    325. goto _switch_dev_register;
    326. }
    327. ht->input->name = "headset-keyboard";
    328. ht->input->id.bustype = BUS_HOST;
    329. ht->input->id.vendor = 0x0001;
    330. ht->input->id.product = 0x0001;
    331. ht->input->id.version = 0x0100;
    332. for(i = 0; headset_key_capability[i].key != KEY_RESERVED; i++) {
    333. __set_bit(headset_key_capability[i].type, ht->input->evbit);
    334. input_set_capability(ht->input, headset_key_capability[i].type, headset_key_capability[i].key);
    335. }
    336. if (input_register_device(ht->input))
    337. goto _switch_dev_register;
    338. headset_gpio_init(ht->detect.gpio, ht->detect.desc);
    339. headset_gpio_init(ht->button.gpio, ht->button.desc);
    340. headset_gpio_debounce(ht->detect.gpio, ht->detect.debounce * 1000);
    341. headset_gpio_debounce(ht->button.gpio, ht->button.debounce * 1000);
    342. hrtimer_init(&ht->button.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    343. ht->button.timer.function = headset_gpio_timer_func;
    344. HEADSET_DEBOUNCE_ROUND_UP(ht->button.debounce_sw);
    345. HEADSET_DEBOUNCE_ROUND_UP(ht->button.timeout_ms);
    346. ht->button.parent = ht;
    347. ht->button.irq = headset_gpio2irq(ht->button.gpio);
    348. ht->button.irq_type_active = ht->button.active_low ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
    349. ht->button.irq_type_inactive = ht->button.active_low ? IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW;
    350. ret = request_irq(ht->button.irq, headset_gpio_irq_handler,
    351. ht->button.irq_type_active, ht->button.desc, &ht->button);
    352. if (ret) {
    353. pr_err("request_irq gpio %d's irq failed!\n", ht->button.gpio);
    354. goto _gpio_request;
    355. }
    356. headset_gpio_irq_enable(0, &ht->button);
    357. hrtimer_init(&ht->detect.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    358. ht->detect.timer.function = headset_gpio_timer_func;
    359. HEADSET_DEBOUNCE_ROUND_UP(ht->detect.debounce_sw);
    360. ht->detect.parent = ht;
    361. ht->detect.irq = headset_gpio2irq(ht->detect.gpio);
    362. ht->detect.irq_type_active = ht->detect.active_low ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
    363. ht->detect.irq_type_inactive = ht->detect.active_low ? IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW;
    364. ret = request_irq(ht->detect.irq, headset_gpio_irq_handler,
    365. ht->detect.irq_type_active, ht->detect.desc, &ht->detect);
    366. if (ret) {
    367. pr_err("request_irq gpio %d's irq failed!\n", ht->detect.gpio);
    368. goto _headset_button_gpio_irq_handler;
    369. }
    370. INIT_WORK(&ht->switch_work, headset_switch_state);
    371. ht->switch_workqueue = create_singlethread_workqueue("headset_switch");
    372. if (ht->switch_workqueue == NULL) {
    373. pr_err("can't create headset switch workqueue\n");
    374. ret = -ENOMEM;
    375. goto _headset_workqueue;
    376. }
    377. return 0;
    378. _headset_workqueue:
    379. destroy_workqueue(ht->switch_workqueue);
    380. _headset_button_gpio_irq_handler:
    381. free_irq(ht->button.irq, &ht->button);
    382. headset_gpio2irq_free(ht->button.irq, &ht->button);
    383. _gpio_request:
    384. headset_gpio_free(ht->detect.gpio);
    385. headset_gpio_free(ht->button.gpio);
    386. input_free_device(ht->input);
    387. _switch_dev_register:
    388. platform_driver_unregister(&headset_button_driver);
    389. switch_dev_unregister(&ht->sdev);
    390. return ret;
    391. }
    392. module_init(headset_init);
    393. static void __exit headset_exit(void)
    394. {
    395. struct _headset *ht = &headset;
    396. destroy_workqueue(ht->switch_workqueue);
    397. headset_gpio_irq_enable(0, &ht->button);
    398. headset_gpio_irq_enable(0, &ht->detect);
    399. free_irq(ht->detect.irq, &ht->detect);
    400. headset_gpio2irq_free(ht->detect.irq, &ht->detect);
    401. free_irq(ht->button.irq, &ht->button);
    402. headset_gpio2irq_free(ht->button.irq, &ht->button);
    403. headset_gpio_free(ht->detect.gpio);
    404. headset_gpio_free(ht->button.gpio);
    405. input_free_device(ht->input);
    406. platform_driver_unregister(&headset_button_driver);
    407. switch_dev_unregister(&ht->sdev);
    408. }
    409. module_exit(headset_exit);
    410. MODULE_DESCRIPTION("headset & button detect driver");
    411. MODULE_AUTHOR("Luther Ge ");
    412. MODULE_LICENSE("GPL");

    分析:

    1. 360-373行,注册input设备。可以看到,一个input设备的注册方法,首先使用input_allocate_device为设备申请相关数据结构,然后初始化该结构的相关成员,如input->name,input->id.vendor, input->id.product, input->id.version(这四个字符串决定了键盘映射文件的名称),然后调用__set_bit设置该input设备支持的事件类型,及调用input_set_capability设置支持的按键值,最后调用input_register_device将输出化完成的数据结构注册到input子系统中。一般的,我们不用去实现他的handle函数,evdev.c就可以完成该目的。

    2. 374-378行,初始化耳机和按键检测时用到的gpio口
    3. 380-394行,申请耳机按键检测的中断处理函数,初始化中断下半段的处理机制。可以看到这里使用了hr_timer这样一个内核中的高精度定时器来实现
    4. 396-417行,申请耳机插拔检测的中断处理函数,初始化中断下半段的处理机制。可以看到这里使用了work_queue这样一个机制来实现。

    可以看到,耳机在中断下半段处理时采用了内核定时器timer来实现。另外,耳机插拔的检测使用了h2w这个class,hook按键上报则采用了input子系统。

    headset插拔识别的框架代码分析

    涉及的相关文件如下 hardware/libhardware_legacy/uevent.c frameworks/base/core/jni/android_os_UEventObserver.cpp frameworks/base/core/java/android/os/UEventObserver.java frameworks/services/java/com/android/server/SystemServer.java frameworks/base/services/java/com/android/server/WiredAccessoryManager.java

    流程待分析

    hook按键的处理

    hook按键通过input子系统上报给Android,在Android手机/system/usr/keylayout/目录下保存着键值映射配置文件。

    一般的,耳机按键对应的按键映射: key 231 CALL key 122 ENDCALL WAKE key 166 MEDIA_STOP key 163 HEADSETHOOK key 164 MEDIA_PLAY_PAUSE key 165 MEDIA_PREVIOUS key 114 VOLUME_DOWN key 115 VOLUME_UP

    这个按键配置文件第三列的字符串在/frameworks/base/include/androidfw/KeycodeLabels.h (android 4.0), frameworks/native/include/input/KeycodeLabels.h(android 4.4), 被定义成:

    1. static const KeycodeLabel KEYCODES[] = {
    2. ...
    3. { "CALL", 5 },
    4. { "ENDCALL", 6 },
    5. ...
    6. { "MEDIA_PLAY_PAUSE", 85 },
    7. { "MEDIA_STOP", 86 },
    8. ...
    9. } 最终/frameworks/base/core/java/android/view/KeyEvent.java会把这个数字定义成这样的常量:
    10. public class KeyEvent extends InputEvent implements Parcelable {
    11. ...
    12. public static final int KEYCODE_CALL = 5;
    13. /** Key code constant: End Call key. */
    14. public static final int KEYCODE_ENDCALL = 6;
    15. ...
    16. /** Key code constant: Play/Pause media key. */
    17. public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
    18. /** Key code constant: Stop media key. */
    19. public static final int KEYCODE_MEDIA_STOP = 86;
    20. ...
    21. }

    手机耳机是手机非常重要的功能之一,耳机的插拔检测和按键检测和相对比较麻烦,日常工作中也容易出现一些新的需求,如新的设备需要通过耳机接口被接入到手机中。因此,研究其驱动和应用层的实现还是很有必要的。

  • 相关阅读:
    1.1 Windows驱动开发:配置驱动开发环境
    用R语言和python进行社交网络中的社区检测
    会员管理系统编程教学编程工具下载
    Opencv——颜色模型+通道分离与合并
    [plugin:vite:css] [sass] Undefined mixin.
    GO学习之切片操作
    CGAL 入门基础
    Git Pull failure 【add/commit】
    4.累积分布函数CDF
    虹科案例|大有可为!虹科AR医疗解决方案应用大盘点
  • 原文地址:https://blog.csdn.net/weixin_41114301/article/details/131072445