cobalt-alsa-main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * ALSA interface to cobalt PCM capture streams
  3. *
  4. * Copyright 2014-2015 Cisco Systems, Inc. and/or its affiliates.
  5. * All rights reserved.
  6. *
  7. * This program is free software; you may redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  15. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. * SOFTWARE.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/device.h>
  25. #include <linux/spinlock.h>
  26. #include <media/v4l2-device.h>
  27. #include <sound/core.h>
  28. #include <sound/initval.h>
  29. #include "cobalt-driver.h"
  30. #include "cobalt-alsa.h"
  31. #include "cobalt-alsa-pcm.h"
  32. static void snd_cobalt_card_free(struct snd_cobalt_card *cobsc)
  33. {
  34. if (cobsc == NULL)
  35. return;
  36. cobsc->s->alsa = NULL;
  37. kfree(cobsc);
  38. }
  39. static void snd_cobalt_card_private_free(struct snd_card *sc)
  40. {
  41. if (sc == NULL)
  42. return;
  43. snd_cobalt_card_free(sc->private_data);
  44. sc->private_data = NULL;
  45. sc->private_free = NULL;
  46. }
  47. static int snd_cobalt_card_create(struct cobalt_stream *s,
  48. struct snd_card *sc,
  49. struct snd_cobalt_card **cobsc)
  50. {
  51. *cobsc = kzalloc(sizeof(struct snd_cobalt_card), GFP_KERNEL);
  52. if (*cobsc == NULL)
  53. return -ENOMEM;
  54. (*cobsc)->s = s;
  55. (*cobsc)->sc = sc;
  56. sc->private_data = *cobsc;
  57. sc->private_free = snd_cobalt_card_private_free;
  58. return 0;
  59. }
  60. static int snd_cobalt_card_set_names(struct snd_cobalt_card *cobsc)
  61. {
  62. struct cobalt_stream *s = cobsc->s;
  63. struct cobalt *cobalt = s->cobalt;
  64. struct snd_card *sc = cobsc->sc;
  65. /* sc->driver is used by alsa-lib's configurator: simple, unique */
  66. strlcpy(sc->driver, "cobalt", sizeof(sc->driver));
  67. /* sc->shortname is a symlink in /proc/asound: COBALT-M -> cardN */
  68. snprintf(sc->shortname, sizeof(sc->shortname), "cobalt-%d-%d",
  69. cobalt->instance, s->video_channel);
  70. /* sc->longname is read from /proc/asound/cards */
  71. snprintf(sc->longname, sizeof(sc->longname),
  72. "Cobalt %d HDMI %d",
  73. cobalt->instance, s->video_channel);
  74. return 0;
  75. }
  76. int cobalt_alsa_init(struct cobalt_stream *s)
  77. {
  78. struct cobalt *cobalt = s->cobalt;
  79. struct snd_card *sc = NULL;
  80. struct snd_cobalt_card *cobsc;
  81. int ret;
  82. /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
  83. /* (1) Check and increment the device index */
  84. /* This is a no-op for us. We'll use the cobalt->instance */
  85. /* (2) Create a card instance */
  86. ret = snd_card_new(&cobalt->pci_dev->dev, SNDRV_DEFAULT_IDX1,
  87. SNDRV_DEFAULT_STR1, THIS_MODULE, 0, &sc);
  88. if (ret) {
  89. cobalt_err("snd_card_new() failed with err %d\n", ret);
  90. goto err_exit;
  91. }
  92. /* (3) Create a main component */
  93. ret = snd_cobalt_card_create(s, sc, &cobsc);
  94. if (ret) {
  95. cobalt_err("snd_cobalt_card_create() failed with err %d\n",
  96. ret);
  97. goto err_exit_free;
  98. }
  99. /* (4) Set the driver ID and name strings */
  100. snd_cobalt_card_set_names(cobsc);
  101. ret = snd_cobalt_pcm_create(cobsc);
  102. if (ret) {
  103. cobalt_err("snd_cobalt_pcm_create() failed with err %d\n",
  104. ret);
  105. goto err_exit_free;
  106. }
  107. /* FIXME - proc files */
  108. /* (7) Set the driver data and return 0 */
  109. /* We do this out of normal order for PCI drivers to avoid races */
  110. s->alsa = cobsc;
  111. /* (6) Register the card instance */
  112. ret = snd_card_register(sc);
  113. if (ret) {
  114. s->alsa = NULL;
  115. cobalt_err("snd_card_register() failed with err %d\n", ret);
  116. goto err_exit_free;
  117. }
  118. return 0;
  119. err_exit_free:
  120. if (sc != NULL)
  121. snd_card_free(sc);
  122. kfree(cobsc);
  123. err_exit:
  124. return ret;
  125. }
  126. void cobalt_alsa_exit(struct cobalt_stream *s)
  127. {
  128. struct snd_cobalt_card *cobsc = s->alsa;
  129. if (cobsc)
  130. snd_card_free(cobsc->sc);
  131. s->alsa = NULL;
  132. }