Android 耳机驱动知识

https://yunzhi.github.io/headset_knowledge

 

工作以后接手的第一个驱动就是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,小米,魅族,索尼

ctia headset

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

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

omtp headset

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

headset jack type

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

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

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

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

headset detect priciple

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

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

流程待分析

hook按键的处理

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

一般的,耳机按键对应的按键映射:key 231 CALLkey 122 ENDCALL WAKEkey 166 MEDIA_STOPkey 163 HEADSETHOOKkey 164 MEDIA_PLAY_PAUSEkey 165 MEDIA_PREVIOUSkey 114 VOLUME_DOWNkey 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会把这个数字定义成这样的常量:

 
  1. public class KeyEvent extends InputEvent implements Parcelable {
  2. ...
  3. public static final int KEYCODE_CALL = 5;
  4. /** Key code constant: End Call key. */
  5. public static final int KEYCODE_ENDCALL = 6;
  6. ...
  7. /** Key code constant: Play/Pause media key. */
  8. public static final int KEYCODE_MEDIA_PLAY_PAUSE= 85;
  9. /** Key code constant: Stop media key. */
  10. public static final int KEYCODE_MEDIA_STOP = 86;
  11. ...
  12. }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值