hdac_bus.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. spin_lock_irq(&bus->reg_lock);
  142. while (bus->unsol_rp != bus->unsol_wp) {
  143. rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  144. bus->unsol_rp = rp;
  145. rp <<= 1;
  146. res = bus->unsol_queue[rp];
  147. caddr = bus->unsol_queue[rp + 1];
  148. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  149. continue;
  150. codec = bus->caddr_tbl[caddr & 0x0f];
  151. if (!codec || !codec->dev.driver)
  152. continue;
  153. spin_unlock_irq(&bus->reg_lock);
  154. drv = drv_to_hdac_driver(codec->dev.driver);
  155. if (drv->unsol_event)
  156. drv->unsol_event(codec, res);
  157. spin_lock_irq(&bus->reg_lock);
  158. }
  159. spin_unlock_irq(&bus->reg_lock);
  160. }
  161. /**
  162. * snd_hdac_bus_add_device - Add a codec to bus
  163. * @bus: HDA core bus
  164. * @codec: HDA core device to add
  165. *
  166. * Adds the given codec to the list in the bus. The caddr_tbl array
  167. * and codec_powered bits are updated, as well.
  168. * Returns zero if success, or a negative error code.
  169. */
  170. int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
  171. {
  172. if (bus->caddr_tbl[codec->addr]) {
  173. dev_err(bus->dev, "address 0x%x is already occupied\n",
  174. codec->addr);
  175. return -EBUSY;
  176. }
  177. list_add_tail(&codec->list, &bus->codec_list);
  178. bus->caddr_tbl[codec->addr] = codec;
  179. set_bit(codec->addr, &bus->codec_powered);
  180. bus->num_codecs++;
  181. return 0;
  182. }
  183. EXPORT_SYMBOL_GPL(snd_hdac_bus_add_device);
  184. /**
  185. * snd_hdac_bus_remove_device - Remove a codec from bus
  186. * @bus: HDA core bus
  187. * @codec: HDA core device to remove
  188. */
  189. void snd_hdac_bus_remove_device(struct hdac_bus *bus,
  190. struct hdac_device *codec)
  191. {
  192. WARN_ON(bus != codec->bus);
  193. if (list_empty(&codec->list))
  194. return;
  195. list_del_init(&codec->list);
  196. bus->caddr_tbl[codec->addr] = NULL;
  197. clear_bit(codec->addr, &bus->codec_powered);
  198. bus->num_codecs--;
  199. flush_work(&bus->unsol_work);
  200. }
  201. EXPORT_SYMBOL_GPL(snd_hdac_bus_remove_device);