jack.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Jack abstraction layer
  4. *
  5. * Copyright 2008 Wolfson Microelectronics
  6. */
  7. #include <linux/input.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/ctype.h>
  11. #include <linux/mm.h>
  12. #include <linux/debugfs.h>
  13. #include <sound/jack.h>
  14. #include <sound/core.h>
  15. #include <sound/control.h>
  16. struct snd_jack_kctl {
  17. struct snd_kcontrol *kctl;
  18. struct list_head list; /* list of controls belong to the same jack */
  19. unsigned int mask_bits; /* only masked status bits are reported via kctl */
  20. struct snd_jack *jack; /* pointer to struct snd_jack */
  21. bool sw_inject_enable; /* allow to inject plug event via debugfs */
  22. #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
  23. struct dentry *jack_debugfs_root; /* jack_kctl debugfs root */
  24. #endif
  25. };
  26. #ifdef CONFIG_SND_JACK_INPUT_DEV
  27. static const int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
  28. SW_HEADPHONE_INSERT,
  29. SW_MICROPHONE_INSERT,
  30. SW_LINEOUT_INSERT,
  31. SW_JACK_PHYSICAL_INSERT,
  32. SW_VIDEOOUT_INSERT,
  33. SW_LINEIN_INSERT,
  34. };
  35. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  36. static void snd_jack_remove_debugfs(struct snd_jack *jack);
  37. static int snd_jack_dev_disconnect(struct snd_device *device)
  38. {
  39. struct snd_jack *jack = device->device_data;
  40. snd_jack_remove_debugfs(jack);
  41. #ifdef CONFIG_SND_JACK_INPUT_DEV
  42. guard(mutex)(&jack->input_dev_lock);
  43. if (!jack->input_dev)
  44. return 0;
  45. /* If the input device is registered with the input subsystem
  46. * then we need to use a different deallocator. */
  47. if (jack->registered)
  48. input_unregister_device(jack->input_dev);
  49. else
  50. input_free_device(jack->input_dev);
  51. jack->input_dev = NULL;
  52. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  53. return 0;
  54. }
  55. static int snd_jack_dev_free(struct snd_device *device)
  56. {
  57. struct snd_jack *jack = device->device_data;
  58. struct snd_card *card = device->card;
  59. struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
  60. list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
  61. list_del_init(&jack_kctl->list);
  62. snd_ctl_remove(card, jack_kctl->kctl);
  63. }
  64. if (jack->private_free)
  65. jack->private_free(jack);
  66. snd_jack_dev_disconnect(device);
  67. kfree(jack->id);
  68. kfree(jack);
  69. return 0;
  70. }
  71. #ifdef CONFIG_SND_JACK_INPUT_DEV
  72. static int snd_jack_dev_register(struct snd_device *device)
  73. {
  74. struct snd_jack *jack = device->device_data;
  75. struct snd_card *card = device->card;
  76. int err, i;
  77. snprintf(jack->name, sizeof(jack->name), "%s %s",
  78. card->shortname, jack->id);
  79. guard(mutex)(&jack->input_dev_lock);
  80. if (!jack->input_dev)
  81. return 0;
  82. jack->input_dev->name = jack->name;
  83. /* Default to the sound card device. */
  84. if (!jack->input_dev->dev.parent)
  85. jack->input_dev->dev.parent = snd_card_get_device_link(card);
  86. /* Add capabilities for any keys that are enabled */
  87. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  88. int testbit = SND_JACK_BTN_0 >> i;
  89. if (!(jack->type & testbit))
  90. continue;
  91. if (!jack->key[i])
  92. jack->key[i] = BTN_0 + i;
  93. input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
  94. }
  95. err = input_register_device(jack->input_dev);
  96. if (err == 0)
  97. jack->registered = 1;
  98. return err;
  99. }
  100. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  101. #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
  102. static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status)
  103. {
  104. struct snd_jack *jack;
  105. #ifdef CONFIG_SND_JACK_INPUT_DEV
  106. int i;
  107. #endif
  108. if (!jack_kctl)
  109. return;
  110. jack = jack_kctl->jack;
  111. if (jack_kctl->sw_inject_enable)
  112. snd_kctl_jack_report(jack->card, jack_kctl->kctl,
  113. status & jack_kctl->mask_bits);
  114. #ifdef CONFIG_SND_JACK_INPUT_DEV
  115. if (!jack->input_dev)
  116. return;
  117. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  118. int testbit = ((SND_JACK_BTN_0 >> i) & jack_kctl->mask_bits);
  119. if (jack->type & testbit)
  120. input_report_key(jack->input_dev, jack->key[i],
  121. status & testbit);
  122. }
  123. for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
  124. int testbit = ((1 << i) & jack_kctl->mask_bits);
  125. if (jack->type & testbit)
  126. input_report_switch(jack->input_dev,
  127. jack_switch_types[i],
  128. status & testbit);
  129. }
  130. input_sync(jack->input_dev);
  131. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  132. }
  133. static ssize_t sw_inject_enable_read(struct file *file,
  134. char __user *to, size_t count, loff_t *ppos)
  135. {
  136. struct snd_jack_kctl *jack_kctl = file->private_data;
  137. int len, ret;
  138. char buf[128];
  139. len = scnprintf(buf, sizeof(buf), "%s: %s\t\t%s: %i\n", "Jack", jack_kctl->kctl->id.name,
  140. "Inject Enabled", jack_kctl->sw_inject_enable);
  141. ret = simple_read_from_buffer(to, count, ppos, buf, len);
  142. return ret;
  143. }
  144. static ssize_t sw_inject_enable_write(struct file *file,
  145. const char __user *from, size_t count, loff_t *ppos)
  146. {
  147. struct snd_jack_kctl *jack_kctl = file->private_data;
  148. int ret, err;
  149. unsigned long enable;
  150. char buf[8] = { 0 };
  151. ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
  152. err = kstrtoul(buf, 0, &enable);
  153. if (err)
  154. return err;
  155. if (jack_kctl->sw_inject_enable == (!!enable))
  156. return ret;
  157. jack_kctl->sw_inject_enable = !!enable;
  158. if (!jack_kctl->sw_inject_enable)
  159. snd_jack_report(jack_kctl->jack, jack_kctl->jack->hw_status_cache);
  160. return ret;
  161. }
  162. static ssize_t jackin_inject_write(struct file *file,
  163. const char __user *from, size_t count, loff_t *ppos)
  164. {
  165. struct snd_jack_kctl *jack_kctl = file->private_data;
  166. int ret, err;
  167. unsigned long enable;
  168. char buf[8] = { 0 };
  169. if (!jack_kctl->sw_inject_enable)
  170. return -EINVAL;
  171. ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
  172. err = kstrtoul(buf, 0, &enable);
  173. if (err)
  174. return err;
  175. snd_jack_inject_report(jack_kctl, !!enable ? jack_kctl->mask_bits : 0);
  176. return ret;
  177. }
  178. static ssize_t jack_kctl_id_read(struct file *file,
  179. char __user *to, size_t count, loff_t *ppos)
  180. {
  181. struct snd_jack_kctl *jack_kctl = file->private_data;
  182. char buf[64];
  183. int len, ret;
  184. len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->id.name);
  185. ret = simple_read_from_buffer(to, count, ppos, buf, len);
  186. return ret;
  187. }
  188. /* the bit definition is aligned with snd_jack_types in jack.h */
  189. static const char * const jack_events_name[] = {
  190. "HEADPHONE(0x0001)", "MICROPHONE(0x0002)", "LINEOUT(0x0004)",
  191. "MECHANICAL(0x0008)", "VIDEOOUT(0x0010)", "LINEIN(0x0020)",
  192. "", "", "", "BTN_5(0x0200)", "BTN_4(0x0400)", "BTN_3(0x0800)",
  193. "BTN_2(0x1000)", "BTN_1(0x2000)", "BTN_0(0x4000)", "",
  194. };
  195. /* the recommended buffer size is 256 */
  196. static int parse_mask_bits(unsigned int mask_bits, char *buf, size_t buf_size)
  197. {
  198. int i;
  199. scnprintf(buf, buf_size, "0x%04x", mask_bits);
  200. for (i = 0; i < ARRAY_SIZE(jack_events_name); i++)
  201. if (mask_bits & (1 << i)) {
  202. strlcat(buf, " ", buf_size);
  203. strlcat(buf, jack_events_name[i], buf_size);
  204. }
  205. strlcat(buf, "\n", buf_size);
  206. return strlen(buf);
  207. }
  208. static ssize_t jack_kctl_mask_bits_read(struct file *file,
  209. char __user *to, size_t count, loff_t *ppos)
  210. {
  211. struct snd_jack_kctl *jack_kctl = file->private_data;
  212. char buf[256];
  213. int len, ret;
  214. len = parse_mask_bits(jack_kctl->mask_bits, buf, sizeof(buf));
  215. ret = simple_read_from_buffer(to, count, ppos, buf, len);
  216. return ret;
  217. }
  218. static ssize_t jack_kctl_status_read(struct file *file,
  219. char __user *to, size_t count, loff_t *ppos)
  220. {
  221. struct snd_jack_kctl *jack_kctl = file->private_data;
  222. char buf[16];
  223. int len, ret;
  224. len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->private_value ?
  225. "Plugged" : "Unplugged");
  226. ret = simple_read_from_buffer(to, count, ppos, buf, len);
  227. return ret;
  228. }
  229. #ifdef CONFIG_SND_JACK_INPUT_DEV
  230. static ssize_t jack_type_read(struct file *file,
  231. char __user *to, size_t count, loff_t *ppos)
  232. {
  233. struct snd_jack_kctl *jack_kctl = file->private_data;
  234. char buf[256];
  235. int len, ret;
  236. len = parse_mask_bits(jack_kctl->jack->type, buf, sizeof(buf));
  237. ret = simple_read_from_buffer(to, count, ppos, buf, len);
  238. return ret;
  239. }
  240. static const struct file_operations jack_type_fops = {
  241. .open = simple_open,
  242. .read = jack_type_read,
  243. .llseek = default_llseek,
  244. };
  245. #endif
  246. static const struct file_operations sw_inject_enable_fops = {
  247. .open = simple_open,
  248. .read = sw_inject_enable_read,
  249. .write = sw_inject_enable_write,
  250. .llseek = default_llseek,
  251. };
  252. static const struct file_operations jackin_inject_fops = {
  253. .open = simple_open,
  254. .write = jackin_inject_write,
  255. .llseek = default_llseek,
  256. };
  257. static const struct file_operations jack_kctl_id_fops = {
  258. .open = simple_open,
  259. .read = jack_kctl_id_read,
  260. .llseek = default_llseek,
  261. };
  262. static const struct file_operations jack_kctl_mask_bits_fops = {
  263. .open = simple_open,
  264. .read = jack_kctl_mask_bits_read,
  265. .llseek = default_llseek,
  266. };
  267. static const struct file_operations jack_kctl_status_fops = {
  268. .open = simple_open,
  269. .read = jack_kctl_status_read,
  270. .llseek = default_llseek,
  271. };
  272. static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
  273. struct snd_jack_kctl *jack_kctl)
  274. {
  275. char *tname;
  276. int i;
  277. /* Don't create injection interface for Phantom jacks */
  278. if (strstr(jack_kctl->kctl->id.name, "Phantom"))
  279. return 0;
  280. tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL);
  281. if (!tname)
  282. return -ENOMEM;
  283. /* replace the chars which are not suitable for folder's name with _ */
  284. for (i = 0; tname[i]; i++)
  285. if (!isalnum(tname[i]))
  286. tname[i] = '_';
  287. jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root);
  288. kfree(tname);
  289. debugfs_create_file("sw_inject_enable", 0644, jack_kctl->jack_debugfs_root, jack_kctl,
  290. &sw_inject_enable_fops);
  291. debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl,
  292. &jackin_inject_fops);
  293. debugfs_create_file("kctl_id", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
  294. &jack_kctl_id_fops);
  295. debugfs_create_file("mask_bits", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
  296. &jack_kctl_mask_bits_fops);
  297. debugfs_create_file("status", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
  298. &jack_kctl_status_fops);
  299. #ifdef CONFIG_SND_JACK_INPUT_DEV
  300. debugfs_create_file("type", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
  301. &jack_type_fops);
  302. #endif
  303. return 0;
  304. }
  305. static void snd_jack_remove_debugfs(struct snd_jack *jack)
  306. {
  307. struct snd_jack_kctl *jack_kctl;
  308. list_for_each_entry(jack_kctl, &jack->kctl_list, list) {
  309. debugfs_remove(jack_kctl->jack_debugfs_root);
  310. jack_kctl->jack_debugfs_root = NULL;
  311. }
  312. }
  313. #else /* CONFIG_SND_JACK_INJECTION_DEBUG */
  314. static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
  315. struct snd_jack_kctl *jack_kctl)
  316. {
  317. return 0;
  318. }
  319. static void snd_jack_remove_debugfs(struct snd_jack *jack)
  320. {
  321. }
  322. #endif /* CONFIG_SND_JACK_INJECTION_DEBUG */
  323. static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
  324. {
  325. struct snd_jack_kctl *jack_kctl;
  326. jack_kctl = kctl->private_data;
  327. if (jack_kctl) {
  328. list_del(&jack_kctl->list);
  329. kfree(jack_kctl);
  330. }
  331. }
  332. static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
  333. {
  334. jack_kctl->jack = jack;
  335. list_add_tail(&jack_kctl->list, &jack->kctl_list);
  336. snd_jack_debugfs_add_inject_node(jack, jack_kctl);
  337. }
  338. static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
  339. {
  340. struct snd_kcontrol *kctl;
  341. struct snd_jack_kctl *jack_kctl;
  342. int err;
  343. kctl = snd_kctl_jack_new(name, card);
  344. if (!kctl)
  345. return NULL;
  346. err = snd_ctl_add(card, kctl);
  347. if (err < 0)
  348. return NULL;
  349. jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
  350. if (!jack_kctl)
  351. goto error;
  352. jack_kctl->kctl = kctl;
  353. jack_kctl->mask_bits = mask;
  354. kctl->private_data = jack_kctl;
  355. kctl->private_free = snd_jack_kctl_private_free;
  356. return jack_kctl;
  357. error:
  358. snd_ctl_free_one(kctl);
  359. return NULL;
  360. }
  361. /**
  362. * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
  363. * @jack: the jack instance which the kctl will attaching to
  364. * @name: the name for the snd_kcontrol object
  365. * @mask: a bitmask of enum snd_jack_type values that can be detected
  366. * by this snd_jack_kctl object.
  367. *
  368. * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
  369. *
  370. * Return: Zero if successful, or a negative error code on failure.
  371. */
  372. int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
  373. {
  374. struct snd_jack_kctl *jack_kctl;
  375. jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
  376. if (!jack_kctl)
  377. return -ENOMEM;
  378. snd_jack_kctl_add(jack, jack_kctl);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(snd_jack_add_new_kctl);
  382. /**
  383. * snd_jack_new - Create a new jack
  384. * @card: the card instance
  385. * @id: an identifying string for this jack
  386. * @type: a bitmask of enum snd_jack_type values that can be detected by
  387. * this jack
  388. * @jjack: Used to provide the allocated jack object to the caller.
  389. * @initial_kctl: if true, create a kcontrol and add it to the jack list.
  390. * @phantom_jack: Don't create a input device for phantom jacks.
  391. *
  392. * Creates a new jack object.
  393. *
  394. * Return: Zero if successful, or a negative error code on failure.
  395. * On success @jjack will be initialised.
  396. */
  397. int snd_jack_new(struct snd_card *card, const char *id, int type,
  398. struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
  399. {
  400. struct snd_jack *jack;
  401. struct snd_jack_kctl *jack_kctl = NULL;
  402. int err;
  403. static const struct snd_device_ops ops = {
  404. .dev_free = snd_jack_dev_free,
  405. #ifdef CONFIG_SND_JACK_INPUT_DEV
  406. .dev_register = snd_jack_dev_register,
  407. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  408. .dev_disconnect = snd_jack_dev_disconnect,
  409. };
  410. if (initial_kctl) {
  411. jack_kctl = snd_jack_kctl_new(card, id, type);
  412. if (!jack_kctl)
  413. return -ENOMEM;
  414. }
  415. jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
  416. if (jack == NULL)
  417. return -ENOMEM;
  418. jack->id = kstrdup(id, GFP_KERNEL);
  419. if (jack->id == NULL) {
  420. kfree(jack);
  421. return -ENOMEM;
  422. }
  423. #ifdef CONFIG_SND_JACK_INPUT_DEV
  424. mutex_init(&jack->input_dev_lock);
  425. /* don't create input device for phantom jack */
  426. if (!phantom_jack) {
  427. int i;
  428. jack->input_dev = input_allocate_device();
  429. if (jack->input_dev == NULL) {
  430. err = -ENOMEM;
  431. goto fail_input;
  432. }
  433. jack->input_dev->phys = "ALSA";
  434. jack->type = type;
  435. for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
  436. if (type & (1 << i))
  437. input_set_capability(jack->input_dev, EV_SW,
  438. jack_switch_types[i]);
  439. }
  440. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  441. err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
  442. if (err < 0)
  443. goto fail_input;
  444. jack->card = card;
  445. INIT_LIST_HEAD(&jack->kctl_list);
  446. if (initial_kctl)
  447. snd_jack_kctl_add(jack, jack_kctl);
  448. *jjack = jack;
  449. return 0;
  450. fail_input:
  451. #ifdef CONFIG_SND_JACK_INPUT_DEV
  452. input_free_device(jack->input_dev);
  453. #endif
  454. kfree(jack->id);
  455. kfree(jack);
  456. return err;
  457. }
  458. EXPORT_SYMBOL(snd_jack_new);
  459. #ifdef CONFIG_SND_JACK_INPUT_DEV
  460. /**
  461. * snd_jack_set_parent - Set the parent device for a jack
  462. *
  463. * @jack: The jack to configure
  464. * @parent: The device to set as parent for the jack.
  465. *
  466. * Set the parent for the jack devices in the device tree. This
  467. * function is only valid prior to registration of the jack. If no
  468. * parent is configured then the parent device will be the sound card.
  469. */
  470. void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
  471. {
  472. WARN_ON(jack->registered);
  473. guard(mutex)(&jack->input_dev_lock);
  474. if (jack->input_dev)
  475. jack->input_dev->dev.parent = parent;
  476. }
  477. EXPORT_SYMBOL(snd_jack_set_parent);
  478. /**
  479. * snd_jack_set_key - Set a key mapping on a jack
  480. *
  481. * @jack: The jack to configure
  482. * @type: Jack report type for this key
  483. * @keytype: Input layer key type to be reported
  484. *
  485. * Map a SND_JACK_BTN_* button type to an input layer key, allowing
  486. * reporting of keys on accessories via the jack abstraction. If no
  487. * mapping is provided but keys are enabled in the jack type then
  488. * BTN_n numeric buttons will be reported.
  489. *
  490. * If jacks are not reporting via the input API this call will have no
  491. * effect.
  492. *
  493. * Note that this is intended to be use by simple devices with small
  494. * numbers of keys that can be reported. It is also possible to
  495. * access the input device directly - devices with complex input
  496. * capabilities on accessories should consider doing this rather than
  497. * using this abstraction.
  498. *
  499. * This function may only be called prior to registration of the jack.
  500. *
  501. * Return: Zero if successful, or a negative error code on failure.
  502. */
  503. int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
  504. int keytype)
  505. {
  506. int key = fls(SND_JACK_BTN_0) - fls(type);
  507. WARN_ON(jack->registered);
  508. if (!keytype || key >= ARRAY_SIZE(jack->key))
  509. return -EINVAL;
  510. jack->type |= type;
  511. jack->key[key] = keytype;
  512. return 0;
  513. }
  514. EXPORT_SYMBOL(snd_jack_set_key);
  515. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  516. /**
  517. * snd_jack_report - Report the current status of a jack
  518. * Note: This function uses mutexes and should be called from a
  519. * context which can sleep (such as a workqueue).
  520. *
  521. * @jack: The jack to report status for
  522. * @status: The current status of the jack
  523. */
  524. void snd_jack_report(struct snd_jack *jack, int status)
  525. {
  526. struct snd_jack_kctl *jack_kctl;
  527. unsigned int mask_bits = 0;
  528. #ifdef CONFIG_SND_JACK_INPUT_DEV
  529. struct input_dev *idev;
  530. int i;
  531. #endif
  532. if (!jack)
  533. return;
  534. jack->hw_status_cache = status;
  535. list_for_each_entry(jack_kctl, &jack->kctl_list, list)
  536. if (jack_kctl->sw_inject_enable)
  537. mask_bits |= jack_kctl->mask_bits;
  538. else
  539. snd_kctl_jack_report(jack->card, jack_kctl->kctl,
  540. status & jack_kctl->mask_bits);
  541. #ifdef CONFIG_SND_JACK_INPUT_DEV
  542. idev = input_get_device(jack->input_dev);
  543. if (!idev)
  544. return;
  545. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  546. int testbit = ((SND_JACK_BTN_0 >> i) & ~mask_bits);
  547. if (jack->type & testbit)
  548. input_report_key(idev, jack->key[i],
  549. status & testbit);
  550. }
  551. for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
  552. int testbit = ((1 << i) & ~mask_bits);
  553. if (jack->type & testbit)
  554. input_report_switch(idev,
  555. jack_switch_types[i],
  556. status & testbit);
  557. }
  558. input_sync(idev);
  559. input_put_device(idev);
  560. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  561. }
  562. EXPORT_SYMBOL(snd_jack_report);