cobalt-alsa-main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALSA interface to cobalt PCM capture streams
  4. *
  5. * Copyright 2014-2015 Cisco Systems, Inc. and/or its affiliates.
  6. * All rights reserved.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/device.h>
  13. #include <linux/spinlock.h>
  14. #include <media/v4l2-device.h>
  15. #include <sound/core.h>
  16. #include <sound/initval.h>
  17. #include "cobalt-driver.h"
  18. #include "cobalt-alsa.h"
  19. #include "cobalt-alsa-pcm.h"
  20. static void snd_cobalt_card_free(struct snd_cobalt_card *cobsc)
  21. {
  22. if (cobsc == NULL)
  23. return;
  24. cobsc->s->alsa = NULL;
  25. kfree(cobsc);
  26. }
  27. static void snd_cobalt_card_private_free(struct snd_card *sc)
  28. {
  29. if (sc == NULL)
  30. return;
  31. snd_cobalt_card_free(sc->private_data);
  32. sc->private_data = NULL;
  33. sc->private_free = NULL;
  34. }
  35. static int snd_cobalt_card_create(struct cobalt_stream *s,
  36. struct snd_card *sc,
  37. struct snd_cobalt_card **cobsc)
  38. {
  39. *cobsc = kzalloc(sizeof(struct snd_cobalt_card), GFP_KERNEL);
  40. if (*cobsc == NULL)
  41. return -ENOMEM;
  42. (*cobsc)->s = s;
  43. (*cobsc)->sc = sc;
  44. sc->private_data = *cobsc;
  45. sc->private_free = snd_cobalt_card_private_free;
  46. return 0;
  47. }
  48. static int snd_cobalt_card_set_names(struct snd_cobalt_card *cobsc)
  49. {
  50. struct cobalt_stream *s = cobsc->s;
  51. struct cobalt *cobalt = s->cobalt;
  52. struct snd_card *sc = cobsc->sc;
  53. /* sc->driver is used by alsa-lib's configurator: simple, unique */
  54. strlcpy(sc->driver, "cobalt", sizeof(sc->driver));
  55. /* sc->shortname is a symlink in /proc/asound: COBALT-M -> cardN */
  56. snprintf(sc->shortname, sizeof(sc->shortname), "cobalt-%d-%d",
  57. cobalt->instance, s->video_channel);
  58. /* sc->longname is read from /proc/asound/cards */
  59. snprintf(sc->longname, sizeof(sc->longname),
  60. "Cobalt %d HDMI %d",
  61. cobalt->instance, s->video_channel);
  62. return 0;
  63. }
  64. int cobalt_alsa_init(struct cobalt_stream *s)
  65. {
  66. struct cobalt *cobalt = s->cobalt;
  67. struct snd_card *sc = NULL;
  68. struct snd_cobalt_card *cobsc;
  69. int ret;
  70. /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
  71. /* (1) Check and increment the device index */
  72. /* This is a no-op for us. We'll use the cobalt->instance */
  73. /* (2) Create a card instance */
  74. ret = snd_card_new(&cobalt->pci_dev->dev, SNDRV_DEFAULT_IDX1,
  75. SNDRV_DEFAULT_STR1, THIS_MODULE, 0, &sc);
  76. if (ret) {
  77. cobalt_err("snd_card_new() failed with err %d\n", ret);
  78. goto err_exit;
  79. }
  80. /* (3) Create a main component */
  81. ret = snd_cobalt_card_create(s, sc, &cobsc);
  82. if (ret) {
  83. cobalt_err("snd_cobalt_card_create() failed with err %d\n",
  84. ret);
  85. goto err_exit_free;
  86. }
  87. /* (4) Set the driver ID and name strings */
  88. snd_cobalt_card_set_names(cobsc);
  89. ret = snd_cobalt_pcm_create(cobsc);
  90. if (ret) {
  91. cobalt_err("snd_cobalt_pcm_create() failed with err %d\n",
  92. ret);
  93. goto err_exit_free;
  94. }
  95. /* FIXME - proc files */
  96. /* (7) Set the driver data and return 0 */
  97. /* We do this out of normal order for PCI drivers to avoid races */
  98. s->alsa = cobsc;
  99. /* (6) Register the card instance */
  100. ret = snd_card_register(sc);
  101. if (ret) {
  102. s->alsa = NULL;
  103. cobalt_err("snd_card_register() failed with err %d\n", ret);
  104. goto err_exit_free;
  105. }
  106. return 0;
  107. err_exit_free:
  108. if (sc != NULL)
  109. snd_card_free(sc);
  110. kfree(cobsc);
  111. err_exit:
  112. return ret;
  113. }
  114. void cobalt_alsa_exit(struct cobalt_stream *s)
  115. {
  116. struct snd_cobalt_card *cobsc = s->alsa;
  117. if (cobsc)
  118. snd_card_free(cobsc->sc);
  119. s->alsa = NULL;
  120. }