jack.c 6.1 KB

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