jack.c 4.6 KB

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