jack.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <sound/jack.h>
  23. #include <sound/core.h>
  24. static int jack_types[] = {
  25. SW_HEADPHONE_INSERT,
  26. SW_MICROPHONE_INSERT,
  27. SW_LINEOUT_INSERT,
  28. SW_JACK_PHYSICAL_INSERT,
  29. SW_VIDEOOUT_INSERT,
  30. };
  31. static int snd_jack_dev_free(struct snd_device *device)
  32. {
  33. struct snd_jack *jack = device->device_data;
  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. kfree(jack->id);
  41. kfree(jack);
  42. return 0;
  43. }
  44. static int snd_jack_dev_register(struct snd_device *device)
  45. {
  46. struct snd_jack *jack = device->device_data;
  47. struct snd_card *card = device->card;
  48. int err;
  49. snprintf(jack->name, sizeof(jack->name), "%s %s",
  50. card->shortname, jack->id);
  51. jack->input_dev->name = jack->name;
  52. /* Default to the sound card device. */
  53. if (!jack->input_dev->dev.parent)
  54. jack->input_dev->dev.parent = card->dev;
  55. err = input_register_device(jack->input_dev);
  56. if (err == 0)
  57. jack->registered = 1;
  58. return err;
  59. }
  60. /**
  61. * snd_jack_new - Create a new jack
  62. * @card: the card instance
  63. * @id: an identifying string for this jack
  64. * @type: a bitmask of enum snd_jack_type values that can be detected by
  65. * this jack
  66. * @jjack: Used to provide the allocated jack object to the caller.
  67. *
  68. * Creates a new jack object.
  69. *
  70. * Returns zero if successful, or a negative error code on failure.
  71. * On success jjack will be initialised.
  72. */
  73. int snd_jack_new(struct snd_card *card, const char *id, int type,
  74. struct snd_jack **jjack)
  75. {
  76. struct snd_jack *jack;
  77. int err;
  78. int i;
  79. static struct snd_device_ops ops = {
  80. .dev_free = snd_jack_dev_free,
  81. .dev_register = snd_jack_dev_register,
  82. };
  83. jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
  84. if (jack == NULL)
  85. return -ENOMEM;
  86. jack->id = kstrdup(id, GFP_KERNEL);
  87. jack->input_dev = input_allocate_device();
  88. if (jack->input_dev == NULL) {
  89. err = -ENOMEM;
  90. goto fail_input;
  91. }
  92. jack->input_dev->phys = "ALSA";
  93. jack->type = type;
  94. for (i = 0; i < ARRAY_SIZE(jack_types); i++)
  95. if (type & (1 << i))
  96. input_set_capability(jack->input_dev, EV_SW,
  97. jack_types[i]);
  98. err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
  99. if (err < 0)
  100. goto fail_input;
  101. *jjack = jack;
  102. return 0;
  103. fail_input:
  104. input_free_device(jack->input_dev);
  105. kfree(jack);
  106. return err;
  107. }
  108. EXPORT_SYMBOL(snd_jack_new);
  109. /**
  110. * snd_jack_set_parent - Set the parent device for a jack
  111. *
  112. * @jack: The jack to configure
  113. * @parent: The device to set as parent for the jack.
  114. *
  115. * Set the parent for the jack input device in the device tree. This
  116. * function is only valid prior to registration of the jack. If no
  117. * parent is configured then the parent device will be the sound card.
  118. */
  119. void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
  120. {
  121. WARN_ON(jack->registered);
  122. jack->input_dev->dev.parent = parent;
  123. }
  124. EXPORT_SYMBOL(snd_jack_set_parent);
  125. /**
  126. * snd_jack_report - Report the current status of a jack
  127. *
  128. * @jack: The jack to report status for
  129. * @status: The current status of the jack
  130. */
  131. void snd_jack_report(struct snd_jack *jack, int status)
  132. {
  133. int i;
  134. if (!jack)
  135. return;
  136. for (i = 0; i < ARRAY_SIZE(jack_types); i++) {
  137. int testbit = 1 << i;
  138. if (jack->type & testbit)
  139. input_report_switch(jack->input_dev, jack_types[i],
  140. status & testbit);
  141. }
  142. input_sync(jack->input_dev);
  143. }
  144. EXPORT_SYMBOL(snd_jack_report);
  145. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  146. MODULE_DESCRIPTION("Jack detection support for ALSA");
  147. MODULE_LICENSE("GPL");