jack.c 18 KB

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