jack.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <sound/jack.h>
  11. #include <sound/core.h>
  12. #include <sound/control.h>
  13. struct snd_jack_kctl {
  14. struct snd_kcontrol *kctl;
  15. struct list_head list; /* list of controls belong to the same jack */
  16. unsigned int mask_bits; /* only masked status bits are reported via kctl */
  17. };
  18. #ifdef CONFIG_SND_JACK_INPUT_DEV
  19. static const int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
  20. SW_HEADPHONE_INSERT,
  21. SW_MICROPHONE_INSERT,
  22. SW_LINEOUT_INSERT,
  23. SW_JACK_PHYSICAL_INSERT,
  24. SW_VIDEOOUT_INSERT,
  25. SW_LINEIN_INSERT,
  26. };
  27. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  28. static int snd_jack_dev_disconnect(struct snd_device *device)
  29. {
  30. #ifdef CONFIG_SND_JACK_INPUT_DEV
  31. struct snd_jack *jack = device->device_data;
  32. if (!jack->input_dev)
  33. return 0;
  34. /* If the input device is registered with the input subsystem
  35. * then we need to use a different deallocator. */
  36. if (jack->registered)
  37. input_unregister_device(jack->input_dev);
  38. else
  39. input_free_device(jack->input_dev);
  40. jack->input_dev = NULL;
  41. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  42. return 0;
  43. }
  44. static int snd_jack_dev_free(struct snd_device *device)
  45. {
  46. struct snd_jack *jack = device->device_data;
  47. struct snd_card *card = device->card;
  48. struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
  49. down_write(&card->controls_rwsem);
  50. list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
  51. list_del_init(&jack_kctl->list);
  52. snd_ctl_remove(card, jack_kctl->kctl);
  53. }
  54. up_write(&card->controls_rwsem);
  55. if (jack->private_free)
  56. jack->private_free(jack);
  57. snd_jack_dev_disconnect(device);
  58. kfree(jack->id);
  59. kfree(jack);
  60. return 0;
  61. }
  62. #ifdef CONFIG_SND_JACK_INPUT_DEV
  63. static int snd_jack_dev_register(struct snd_device *device)
  64. {
  65. struct snd_jack *jack = device->device_data;
  66. struct snd_card *card = device->card;
  67. int err, i;
  68. snprintf(jack->name, sizeof(jack->name), "%s %s",
  69. card->shortname, jack->id);
  70. if (!jack->input_dev)
  71. return 0;
  72. jack->input_dev->name = jack->name;
  73. /* Default to the sound card device. */
  74. if (!jack->input_dev->dev.parent)
  75. jack->input_dev->dev.parent = snd_card_get_device_link(card);
  76. /* Add capabilities for any keys that are enabled */
  77. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  78. int testbit = SND_JACK_BTN_0 >> i;
  79. if (!(jack->type & testbit))
  80. continue;
  81. if (!jack->key[i])
  82. jack->key[i] = BTN_0 + i;
  83. input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
  84. }
  85. err = input_register_device(jack->input_dev);
  86. if (err == 0)
  87. jack->registered = 1;
  88. return err;
  89. }
  90. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  91. static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
  92. {
  93. struct snd_jack_kctl *jack_kctl;
  94. jack_kctl = kctl->private_data;
  95. if (jack_kctl) {
  96. list_del(&jack_kctl->list);
  97. kfree(jack_kctl);
  98. }
  99. }
  100. static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
  101. {
  102. list_add_tail(&jack_kctl->list, &jack->kctl_list);
  103. }
  104. static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
  105. {
  106. struct snd_kcontrol *kctl;
  107. struct snd_jack_kctl *jack_kctl;
  108. int err;
  109. kctl = snd_kctl_jack_new(name, card);
  110. if (!kctl)
  111. return NULL;
  112. err = snd_ctl_add(card, kctl);
  113. if (err < 0)
  114. return NULL;
  115. jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
  116. if (!jack_kctl)
  117. goto error;
  118. jack_kctl->kctl = kctl;
  119. jack_kctl->mask_bits = mask;
  120. kctl->private_data = jack_kctl;
  121. kctl->private_free = snd_jack_kctl_private_free;
  122. return jack_kctl;
  123. error:
  124. snd_ctl_free_one(kctl);
  125. return NULL;
  126. }
  127. /**
  128. * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
  129. * @jack: the jack instance which the kctl will attaching to
  130. * @name: the name for the snd_kcontrol object
  131. * @mask: a bitmask of enum snd_jack_type values that can be detected
  132. * by this snd_jack_kctl object.
  133. *
  134. * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
  135. *
  136. * Return: Zero if successful, or a negative error code on failure.
  137. */
  138. int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
  139. {
  140. struct snd_jack_kctl *jack_kctl;
  141. jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
  142. if (!jack_kctl)
  143. return -ENOMEM;
  144. snd_jack_kctl_add(jack, jack_kctl);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(snd_jack_add_new_kctl);
  148. /**
  149. * snd_jack_new - Create a new jack
  150. * @card: the card instance
  151. * @id: an identifying string for this jack
  152. * @type: a bitmask of enum snd_jack_type values that can be detected by
  153. * this jack
  154. * @jjack: Used to provide the allocated jack object to the caller.
  155. * @initial_kctl: if true, create a kcontrol and add it to the jack list.
  156. * @phantom_jack: Don't create a input device for phantom jacks.
  157. *
  158. * Creates a new jack object.
  159. *
  160. * Return: Zero if successful, or a negative error code on failure.
  161. * On success @jjack will be initialised.
  162. */
  163. int snd_jack_new(struct snd_card *card, const char *id, int type,
  164. struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
  165. {
  166. struct snd_jack *jack;
  167. struct snd_jack_kctl *jack_kctl = NULL;
  168. int err;
  169. static const struct snd_device_ops ops = {
  170. .dev_free = snd_jack_dev_free,
  171. #ifdef CONFIG_SND_JACK_INPUT_DEV
  172. .dev_register = snd_jack_dev_register,
  173. .dev_disconnect = snd_jack_dev_disconnect,
  174. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  175. };
  176. if (initial_kctl) {
  177. jack_kctl = snd_jack_kctl_new(card, id, type);
  178. if (!jack_kctl)
  179. return -ENOMEM;
  180. }
  181. jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
  182. if (jack == NULL)
  183. return -ENOMEM;
  184. jack->id = kstrdup(id, GFP_KERNEL);
  185. if (jack->id == NULL) {
  186. kfree(jack);
  187. return -ENOMEM;
  188. }
  189. /* don't creat input device for phantom jack */
  190. if (!phantom_jack) {
  191. #ifdef CONFIG_SND_JACK_INPUT_DEV
  192. int i;
  193. jack->input_dev = input_allocate_device();
  194. if (jack->input_dev == NULL) {
  195. err = -ENOMEM;
  196. goto fail_input;
  197. }
  198. jack->input_dev->phys = "ALSA";
  199. jack->type = type;
  200. for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
  201. if (type & (1 << i))
  202. input_set_capability(jack->input_dev, EV_SW,
  203. jack_switch_types[i]);
  204. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  205. }
  206. err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
  207. if (err < 0)
  208. goto fail_input;
  209. jack->card = card;
  210. INIT_LIST_HEAD(&jack->kctl_list);
  211. if (initial_kctl)
  212. snd_jack_kctl_add(jack, jack_kctl);
  213. *jjack = jack;
  214. return 0;
  215. fail_input:
  216. #ifdef CONFIG_SND_JACK_INPUT_DEV
  217. input_free_device(jack->input_dev);
  218. #endif
  219. kfree(jack->id);
  220. kfree(jack);
  221. return err;
  222. }
  223. EXPORT_SYMBOL(snd_jack_new);
  224. #ifdef CONFIG_SND_JACK_INPUT_DEV
  225. /**
  226. * snd_jack_set_parent - Set the parent device for a jack
  227. *
  228. * @jack: The jack to configure
  229. * @parent: The device to set as parent for the jack.
  230. *
  231. * Set the parent for the jack devices in the device tree. This
  232. * function is only valid prior to registration of the jack. If no
  233. * parent is configured then the parent device will be the sound card.
  234. */
  235. void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
  236. {
  237. WARN_ON(jack->registered);
  238. if (!jack->input_dev)
  239. return;
  240. jack->input_dev->dev.parent = parent;
  241. }
  242. EXPORT_SYMBOL(snd_jack_set_parent);
  243. /**
  244. * snd_jack_set_key - Set a key mapping on a jack
  245. *
  246. * @jack: The jack to configure
  247. * @type: Jack report type for this key
  248. * @keytype: Input layer key type to be reported
  249. *
  250. * Map a SND_JACK_BTN_* button type to an input layer key, allowing
  251. * reporting of keys on accessories via the jack abstraction. If no
  252. * mapping is provided but keys are enabled in the jack type then
  253. * BTN_n numeric buttons will be reported.
  254. *
  255. * If jacks are not reporting via the input API this call will have no
  256. * effect.
  257. *
  258. * Note that this is intended to be use by simple devices with small
  259. * numbers of keys that can be reported. It is also possible to
  260. * access the input device directly - devices with complex input
  261. * capabilities on accessories should consider doing this rather than
  262. * using this abstraction.
  263. *
  264. * This function may only be called prior to registration of the jack.
  265. *
  266. * Return: Zero if successful, or a negative error code on failure.
  267. */
  268. int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
  269. int keytype)
  270. {
  271. int key = fls(SND_JACK_BTN_0) - fls(type);
  272. WARN_ON(jack->registered);
  273. if (!keytype || key >= ARRAY_SIZE(jack->key))
  274. return -EINVAL;
  275. jack->type |= type;
  276. jack->key[key] = keytype;
  277. return 0;
  278. }
  279. EXPORT_SYMBOL(snd_jack_set_key);
  280. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  281. /**
  282. * snd_jack_report - Report the current status of a jack
  283. *
  284. * @jack: The jack to report status for
  285. * @status: The current status of the jack
  286. */
  287. void snd_jack_report(struct snd_jack *jack, int status)
  288. {
  289. struct snd_jack_kctl *jack_kctl;
  290. #ifdef CONFIG_SND_JACK_INPUT_DEV
  291. int i;
  292. #endif
  293. if (!jack)
  294. return;
  295. list_for_each_entry(jack_kctl, &jack->kctl_list, list)
  296. snd_kctl_jack_report(jack->card, jack_kctl->kctl,
  297. status & jack_kctl->mask_bits);
  298. #ifdef CONFIG_SND_JACK_INPUT_DEV
  299. if (!jack->input_dev)
  300. return;
  301. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  302. int testbit = SND_JACK_BTN_0 >> i;
  303. if (jack->type & testbit)
  304. input_report_key(jack->input_dev, jack->key[i],
  305. status & testbit);
  306. }
  307. for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
  308. int testbit = 1 << i;
  309. if (jack->type & testbit)
  310. input_report_switch(jack->input_dev,
  311. jack_switch_types[i],
  312. status & testbit);
  313. }
  314. input_sync(jack->input_dev);
  315. #endif /* CONFIG_SND_JACK_INPUT_DEV */
  316. }
  317. EXPORT_SYMBOL(snd_jack_report);