jack.c 6.2 KB

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