hdac_bus.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * HD-audio core bus driver
  3. */
  4. #include <linux/init.h>
  5. #include <linux/device.h>
  6. #include <linux/module.h>
  7. #include <linux/export.h>
  8. #include <sound/hdaudio.h>
  9. #include "trace.h"
  10. static void process_unsol_events(struct work_struct *work);
  11. static const struct hdac_bus_ops default_ops = {
  12. .command = snd_hdac_bus_send_cmd,
  13. .get_response = snd_hdac_bus_get_response,
  14. };
  15. /**
  16. * snd_hdac_bus_init - initialize a HD-audio bas bus
  17. * @bus: the pointer to bus object
  18. * @ops: bus verb operators
  19. * @io_ops: lowlevel I/O operators
  20. *
  21. * Returns 0 if successful, or a negative error code.
  22. */
  23. int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
  24. const struct hdac_bus_ops *ops,
  25. const struct hdac_io_ops *io_ops)
  26. {
  27. memset(bus, 0, sizeof(*bus));
  28. bus->dev = dev;
  29. if (ops)
  30. bus->ops = ops;
  31. else
  32. bus->ops = &default_ops;
  33. bus->io_ops = io_ops;
  34. INIT_LIST_HEAD(&bus->stream_list);
  35. INIT_LIST_HEAD(&bus->codec_list);
  36. INIT_WORK(&bus->unsol_work, process_unsol_events);
  37. spin_lock_init(&bus->reg_lock);
  38. mutex_init(&bus->cmd_mutex);
  39. bus->irq = -1;
  40. return 0;
  41. }
  42. EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
  43. /**
  44. * snd_hdac_bus_exit - clean up a HD-audio bas bus
  45. * @bus: the pointer to bus object
  46. */
  47. void snd_hdac_bus_exit(struct hdac_bus *bus)
  48. {
  49. WARN_ON(!list_empty(&bus->stream_list));
  50. WARN_ON(!list_empty(&bus->codec_list));
  51. cancel_work_sync(&bus->unsol_work);
  52. }
  53. EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
  54. /**
  55. * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
  56. * @bus: bus object
  57. * @cmd: HD-audio encoded verb
  58. * @res: pointer to store the response, NULL if performing asynchronously
  59. *
  60. * Returns 0 if successful, or a negative error code.
  61. */
  62. int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
  63. unsigned int cmd, unsigned int *res)
  64. {
  65. int err;
  66. mutex_lock(&bus->cmd_mutex);
  67. err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
  68. mutex_unlock(&bus->cmd_mutex);
  69. return err;
  70. }
  71. EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb);
  72. /**
  73. * snd_hdac_bus_exec_verb_unlocked - unlocked version
  74. * @bus: bus object
  75. * @cmd: HD-audio encoded verb
  76. * @res: pointer to store the response, NULL if performing asynchronously
  77. *
  78. * Returns 0 if successful, or a negative error code.
  79. */
  80. int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
  81. unsigned int cmd, unsigned int *res)
  82. {
  83. unsigned int tmp;
  84. int err;
  85. if (cmd == ~0)
  86. return -EINVAL;
  87. if (res)
  88. *res = -1;
  89. else if (bus->sync_write)
  90. res = &tmp;
  91. for (;;) {
  92. trace_hda_send_cmd(bus, cmd);
  93. err = bus->ops->command(bus, cmd);
  94. if (err != -EAGAIN)
  95. break;
  96. /* process pending verbs */
  97. err = bus->ops->get_response(bus, addr, &tmp);
  98. if (err)
  99. break;
  100. }
  101. if (!err && res) {
  102. err = bus->ops->get_response(bus, addr, res);
  103. trace_hda_get_response(bus, addr, *res);
  104. }
  105. return err;
  106. }
  107. EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
  108. /**
  109. * snd_hdac_bus_queue_event - add an unsolicited event to queue
  110. * @bus: the BUS
  111. * @res: unsolicited event (lower 32bit of RIRB entry)
  112. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  113. *
  114. * Adds the given event to the queue. The events are processed in
  115. * the workqueue asynchronously. Call this function in the interrupt
  116. * hanlder when RIRB receives an unsolicited event.
  117. */
  118. void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
  119. {
  120. unsigned int wp;
  121. if (!bus)
  122. return;
  123. trace_hda_unsol_event(bus, res, res_ex);
  124. wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  125. bus->unsol_wp = wp;
  126. wp <<= 1;
  127. bus->unsol_queue[wp] = res;
  128. bus->unsol_queue[wp + 1] = res_ex;
  129. schedule_work(&bus->unsol_work);
  130. }
  131. EXPORT_SYMBOL_GPL(snd_hdac_bus_queue_event);
  132. /*
  133. * process queued unsolicited events
  134. */
  135. static void process_unsol_events(struct work_struct *work)
  136. {
  137. struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
  138. struct hdac_device *codec;
  139. struct hdac_driver *drv;
  140. unsigned int rp, caddr, res;
  141. while (bus->unsol_rp != bus->unsol_wp) {
  142. rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  143. bus->unsol_rp = rp;
  144. rp <<= 1;
  145. res = bus->unsol_queue[rp];
  146. caddr = bus->unsol_queue[rp + 1];
  147. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  148. continue;
  149. codec = bus->caddr_tbl[caddr & 0x0f];
  150. if (!codec || !codec->dev.driver)
  151. continue;
  152. drv = drv_to_hdac_driver(codec->dev.driver);
  153. if (drv->unsol_event)
  154. drv->unsol_event(codec, res);
  155. }
  156. }
  157. /**
  158. * snd_hdac_bus_add_device - Add a codec to bus
  159. * @bus: HDA core bus
  160. * @codec: HDA core device to add
  161. *
  162. * Adds the given codec to the list in the bus. The caddr_tbl array
  163. * and codec_powered bits are updated, as well.
  164. * Returns zero if success, or a negative error code.
  165. */
  166. int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
  167. {
  168. if (bus->caddr_tbl[codec->addr]) {
  169. dev_err(bus->dev, "address 0x%x is already occupied\n",
  170. codec->addr);
  171. return -EBUSY;
  172. }
  173. list_add_tail(&codec->list, &bus->codec_list);
  174. bus->caddr_tbl[codec->addr] = codec;
  175. set_bit(codec->addr, &bus->codec_powered);
  176. bus->num_codecs++;
  177. return 0;
  178. }
  179. EXPORT_SYMBOL_GPL(snd_hdac_bus_add_device);
  180. /**
  181. * snd_hdac_bus_remove_device - Remove a codec from bus
  182. * @bus: HDA core bus
  183. * @codec: HDA core device to remove
  184. */
  185. void snd_hdac_bus_remove_device(struct hdac_bus *bus,
  186. struct hdac_device *codec)
  187. {
  188. WARN_ON(bus != codec->bus);
  189. if (list_empty(&codec->list))
  190. return;
  191. list_del_init(&codec->list);
  192. bus->caddr_tbl[codec->addr] = NULL;
  193. clear_bit(codec->addr, &bus->codec_powered);
  194. bus->num_codecs--;
  195. }
  196. EXPORT_SYMBOL_GPL(snd_hdac_bus_remove_device);