jack.c 9.9 KB

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