jack.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
  27. SW_HEADPHONE_INSERT,
  28. SW_MICROPHONE_INSERT,
  29. SW_LINEOUT_INSERT,
  30. SW_JACK_PHYSICAL_INSERT,
  31. SW_VIDEOOUT_INSERT,
  32. SW_LINEIN_INSERT,
  33. };
  34. static int snd_jack_dev_disconnect(struct snd_device *device)
  35. {
  36. struct snd_jack *jack = device->device_data;
  37. if (!jack->input_dev)
  38. return 0;
  39. /* If the input device is registered with the input subsystem
  40. * then we need to use a different deallocator. */
  41. if (jack->registered)
  42. input_unregister_device(jack->input_dev);
  43. else
  44. input_free_device(jack->input_dev);
  45. jack->input_dev = NULL;
  46. return 0;
  47. }
  48. static int snd_jack_dev_free(struct snd_device *device)
  49. {
  50. struct snd_jack *jack = device->device_data;
  51. if (jack->private_free)
  52. jack->private_free(jack);
  53. snd_jack_dev_disconnect(device);
  54. kfree(jack->id);
  55. kfree(jack);
  56. return 0;
  57. }
  58. static int snd_jack_dev_register(struct snd_device *device)
  59. {
  60. struct snd_jack *jack = device->device_data;
  61. struct snd_card *card = device->card;
  62. int err, i;
  63. snprintf(jack->name, sizeof(jack->name), "%s %s",
  64. card->shortname, jack->id);
  65. jack->input_dev->name = jack->name;
  66. /* Default to the sound card device. */
  67. if (!jack->input_dev->dev.parent)
  68. jack->input_dev->dev.parent = snd_card_get_device_link(card);
  69. /* Add capabilities for any keys that are enabled */
  70. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  71. int testbit = SND_JACK_BTN_0 >> i;
  72. if (!(jack->type & testbit))
  73. continue;
  74. if (!jack->key[i])
  75. jack->key[i] = BTN_0 + i;
  76. input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
  77. }
  78. err = input_register_device(jack->input_dev);
  79. if (err == 0)
  80. jack->registered = 1;
  81. return err;
  82. }
  83. /**
  84. * snd_jack_new - Create a new jack
  85. * @card: the card instance
  86. * @id: an identifying string for this jack
  87. * @type: a bitmask of enum snd_jack_type values that can be detected by
  88. * this jack
  89. * @jjack: Used to provide the allocated jack object to the caller.
  90. *
  91. * Creates a new jack object.
  92. *
  93. * Return: Zero if successful, or a negative error code on failure.
  94. * On success @jjack will be initialised.
  95. */
  96. int snd_jack_new(struct snd_card *card, const char *id, int type,
  97. struct snd_jack **jjack)
  98. {
  99. struct snd_jack *jack;
  100. int err;
  101. int i;
  102. static struct snd_device_ops ops = {
  103. .dev_free = snd_jack_dev_free,
  104. .dev_register = snd_jack_dev_register,
  105. .dev_disconnect = snd_jack_dev_disconnect,
  106. };
  107. jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
  108. if (jack == NULL)
  109. return -ENOMEM;
  110. jack->id = kstrdup(id, GFP_KERNEL);
  111. jack->input_dev = input_allocate_device();
  112. if (jack->input_dev == NULL) {
  113. err = -ENOMEM;
  114. goto fail_input;
  115. }
  116. jack->input_dev->phys = "ALSA";
  117. jack->type = type;
  118. for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
  119. if (type & (1 << i))
  120. input_set_capability(jack->input_dev, EV_SW,
  121. jack_switch_types[i]);
  122. err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
  123. if (err < 0)
  124. goto fail_input;
  125. *jjack = jack;
  126. return 0;
  127. fail_input:
  128. input_free_device(jack->input_dev);
  129. kfree(jack->id);
  130. kfree(jack);
  131. return err;
  132. }
  133. EXPORT_SYMBOL(snd_jack_new);
  134. /**
  135. * snd_jack_set_parent - Set the parent device for a jack
  136. *
  137. * @jack: The jack to configure
  138. * @parent: The device to set as parent for the jack.
  139. *
  140. * Set the parent for the jack devices in the device tree. This
  141. * function is only valid prior to registration of the jack. If no
  142. * parent is configured then the parent device will be the sound card.
  143. */
  144. void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
  145. {
  146. WARN_ON(jack->registered);
  147. jack->input_dev->dev.parent = parent;
  148. }
  149. EXPORT_SYMBOL(snd_jack_set_parent);
  150. /**
  151. * snd_jack_set_key - Set a key mapping on a jack
  152. *
  153. * @jack: The jack to configure
  154. * @type: Jack report type for this key
  155. * @keytype: Input layer key type to be reported
  156. *
  157. * Map a SND_JACK_BTN_ button type to an input layer key, allowing
  158. * reporting of keys on accessories via the jack abstraction. If no
  159. * mapping is provided but keys are enabled in the jack type then
  160. * BTN_n numeric buttons will be reported.
  161. *
  162. * If jacks are not reporting via the input API this call will have no
  163. * effect.
  164. *
  165. * Note that this is intended to be use by simple devices with small
  166. * numbers of keys that can be reported. It is also possible to
  167. * access the input device directly - devices with complex input
  168. * capabilities on accessories should consider doing this rather than
  169. * using this abstraction.
  170. *
  171. * This function may only be called prior to registration of the jack.
  172. *
  173. * Return: Zero if successful, or a negative error code on failure.
  174. */
  175. int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
  176. int keytype)
  177. {
  178. int key = fls(SND_JACK_BTN_0) - fls(type);
  179. WARN_ON(jack->registered);
  180. if (!keytype || key >= ARRAY_SIZE(jack->key))
  181. return -EINVAL;
  182. jack->type |= type;
  183. jack->key[key] = keytype;
  184. return 0;
  185. }
  186. EXPORT_SYMBOL(snd_jack_set_key);
  187. /**
  188. * snd_jack_report - Report the current status of a jack
  189. *
  190. * @jack: The jack to report status for
  191. * @status: The current status of the jack
  192. */
  193. void snd_jack_report(struct snd_jack *jack, int status)
  194. {
  195. int i;
  196. if (!jack)
  197. return;
  198. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  199. int testbit = SND_JACK_BTN_0 >> i;
  200. if (jack->type & testbit)
  201. input_report_key(jack->input_dev, jack->key[i],
  202. status & testbit);
  203. }
  204. for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
  205. int testbit = 1 << i;
  206. if (jack->type & testbit)
  207. input_report_switch(jack->input_dev,
  208. jack_switch_types[i],
  209. status & testbit);
  210. }
  211. input_sync(jack->input_dev);
  212. }
  213. EXPORT_SYMBOL(snd_jack_report);
  214. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  215. MODULE_DESCRIPTION("Jack detection support for ALSA");
  216. MODULE_LICENSE("GPL");