hda-codec.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. *
  4. * written by Gerd Hoffmann <kraxel@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 or
  9. * (at your option) version 3 of the License.
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/pci/pci.h"
  21. #include "hw/qdev-properties.h"
  22. #include "intel-hda.h"
  23. #include "migration/vmstate.h"
  24. #include "qemu/host-utils.h"
  25. #include "qemu/module.h"
  26. #include "intel-hda-defs.h"
  27. #include "audio/audio.h"
  28. #include "trace.h"
  29. #include "qom/object.h"
  30. /* -------------------------------------------------------------------------- */
  31. typedef struct desc_param {
  32. uint32_t id;
  33. uint32_t val;
  34. } desc_param;
  35. typedef struct desc_node {
  36. uint32_t nid;
  37. const char *name;
  38. const desc_param *params;
  39. uint32_t nparams;
  40. uint32_t config;
  41. uint32_t pinctl;
  42. uint32_t *conn;
  43. uint32_t stindex;
  44. } desc_node;
  45. typedef struct desc_codec {
  46. const char *name;
  47. uint32_t iid;
  48. const desc_node *nodes;
  49. uint32_t nnodes;
  50. } desc_codec;
  51. static const desc_param* hda_codec_find_param(const desc_node *node, uint32_t id)
  52. {
  53. int i;
  54. for (i = 0; i < node->nparams; i++) {
  55. if (node->params[i].id == id) {
  56. return &node->params[i];
  57. }
  58. }
  59. return NULL;
  60. }
  61. static const desc_node* hda_codec_find_node(const desc_codec *codec, uint32_t nid)
  62. {
  63. int i;
  64. for (i = 0; i < codec->nnodes; i++) {
  65. if (codec->nodes[i].nid == nid) {
  66. return &codec->nodes[i];
  67. }
  68. }
  69. return NULL;
  70. }
  71. static void hda_codec_parse_fmt(uint32_t format, struct audsettings *as)
  72. {
  73. if (format & AC_FMT_TYPE_NON_PCM) {
  74. return;
  75. }
  76. as->freq = (format & AC_FMT_BASE_44K) ? 44100 : 48000;
  77. switch ((format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT) {
  78. case 1: as->freq *= 2; break;
  79. case 2: as->freq *= 3; break;
  80. case 3: as->freq *= 4; break;
  81. }
  82. switch ((format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT) {
  83. case 1: as->freq /= 2; break;
  84. case 2: as->freq /= 3; break;
  85. case 3: as->freq /= 4; break;
  86. case 4: as->freq /= 5; break;
  87. case 5: as->freq /= 6; break;
  88. case 6: as->freq /= 7; break;
  89. case 7: as->freq /= 8; break;
  90. }
  91. switch (format & AC_FMT_BITS_MASK) {
  92. case AC_FMT_BITS_8: as->fmt = AUDIO_FORMAT_S8; break;
  93. case AC_FMT_BITS_16: as->fmt = AUDIO_FORMAT_S16; break;
  94. case AC_FMT_BITS_32: as->fmt = AUDIO_FORMAT_S32; break;
  95. }
  96. as->nchannels = ((format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT) + 1;
  97. }
  98. /* -------------------------------------------------------------------------- */
  99. /*
  100. * HDA codec descriptions
  101. */
  102. /* some defines */
  103. #define QEMU_HDA_ID_VENDOR 0x1af4
  104. #define QEMU_HDA_PCM_FORMATS (AC_SUPPCM_BITS_16 | \
  105. 0x1fc /* 16 -> 96 kHz */)
  106. #define QEMU_HDA_AMP_NONE (0)
  107. #define QEMU_HDA_AMP_STEPS 0x4a
  108. #define PARAM mixemu
  109. #define HDA_MIXER
  110. #include "hda-codec-common.h"
  111. #define PARAM nomixemu
  112. #include "hda-codec-common.h"
  113. #define HDA_TIMER_TICKS (SCALE_MS)
  114. #define B_SIZE sizeof(st->buf)
  115. #define B_MASK (sizeof(st->buf) - 1)
  116. /* -------------------------------------------------------------------------- */
  117. static const char *fmt2name[] = {
  118. [ AUDIO_FORMAT_U8 ] = "PCM-U8",
  119. [ AUDIO_FORMAT_S8 ] = "PCM-S8",
  120. [ AUDIO_FORMAT_U16 ] = "PCM-U16",
  121. [ AUDIO_FORMAT_S16 ] = "PCM-S16",
  122. [ AUDIO_FORMAT_U32 ] = "PCM-U32",
  123. [ AUDIO_FORMAT_S32 ] = "PCM-S32",
  124. };
  125. #define TYPE_HDA_AUDIO "hda-audio"
  126. OBJECT_DECLARE_SIMPLE_TYPE(HDAAudioState, HDA_AUDIO)
  127. typedef struct HDAAudioStream HDAAudioStream;
  128. struct HDAAudioStream {
  129. HDAAudioState *state;
  130. const desc_node *node;
  131. bool output, running;
  132. uint32_t stream;
  133. uint32_t channel;
  134. uint32_t format;
  135. uint32_t gain_left, gain_right;
  136. bool mute_left, mute_right;
  137. struct audsettings as;
  138. union {
  139. SWVoiceIn *in;
  140. SWVoiceOut *out;
  141. } voice;
  142. uint8_t compat_buf[HDA_BUFFER_SIZE];
  143. uint32_t compat_bpos;
  144. uint8_t buf[8192]; /* size must be power of two */
  145. int64_t rpos;
  146. int64_t wpos;
  147. QEMUTimer *buft;
  148. int64_t buft_start;
  149. };
  150. struct HDAAudioState {
  151. HDACodecDevice hda;
  152. const char *name;
  153. QEMUSoundCard card;
  154. const desc_codec *desc;
  155. HDAAudioStream st[4];
  156. bool running_compat[16];
  157. bool running_real[2 * 16];
  158. /* properties */
  159. uint32_t debug;
  160. bool mixer;
  161. bool use_timer;
  162. };
  163. static inline uint32_t hda_bytes_per_second(HDAAudioStream *st)
  164. {
  165. return 2 * (uint32_t)st->as.nchannels * (uint32_t)st->as.freq;
  166. }
  167. static inline void hda_timer_sync_adjust(HDAAudioStream *st, int64_t target_pos)
  168. {
  169. int64_t limit = B_SIZE / 8;
  170. int64_t corr = 0;
  171. if (target_pos > limit) {
  172. corr = HDA_TIMER_TICKS;
  173. }
  174. if (target_pos < -limit) {
  175. corr = -HDA_TIMER_TICKS;
  176. }
  177. if (target_pos < -(2 * limit)) {
  178. corr = -(4 * HDA_TIMER_TICKS);
  179. }
  180. if (corr == 0) {
  181. return;
  182. }
  183. trace_hda_audio_adjust(st->node->name, target_pos);
  184. st->buft_start += corr;
  185. }
  186. static void hda_audio_input_timer(void *opaque)
  187. {
  188. HDAAudioStream *st = opaque;
  189. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  190. int64_t uptime = now - st->buft_start;
  191. int64_t wpos = st->wpos;
  192. int64_t rpos = st->rpos;
  193. int64_t wanted_rpos;
  194. if (uptime <= 0) {
  195. /* wanted_rpos <= 0 */
  196. goto out_timer;
  197. }
  198. wanted_rpos = muldiv64(uptime, hda_bytes_per_second(st),
  199. NANOSECONDS_PER_SECOND);
  200. wanted_rpos &= -4; /* IMPORTANT! clip to frames */
  201. if (wanted_rpos <= rpos) {
  202. /* we already transmitted the data */
  203. goto out_timer;
  204. }
  205. int64_t to_transfer = MIN(wpos - rpos, wanted_rpos - rpos);
  206. while (to_transfer) {
  207. uint32_t start = (rpos & B_MASK);
  208. uint32_t chunk = MIN(B_SIZE - start, to_transfer);
  209. int rc = hda_codec_xfer(
  210. &st->state->hda, st->stream, false, st->buf + start, chunk);
  211. if (!rc) {
  212. break;
  213. }
  214. rpos += chunk;
  215. to_transfer -= chunk;
  216. st->rpos += chunk;
  217. }
  218. out_timer:
  219. if (st->running) {
  220. timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS);
  221. }
  222. }
  223. static void hda_audio_input_cb(void *opaque, int avail)
  224. {
  225. HDAAudioStream *st = opaque;
  226. int64_t wpos = st->wpos;
  227. int64_t rpos = st->rpos;
  228. int64_t to_transfer = MIN(B_SIZE - (wpos - rpos), avail);
  229. while (to_transfer) {
  230. uint32_t start = (uint32_t) (wpos & B_MASK);
  231. uint32_t chunk = (uint32_t) MIN(B_SIZE - start, to_transfer);
  232. uint32_t read = AUD_read(st->voice.in, st->buf + start, chunk);
  233. wpos += read;
  234. to_transfer -= read;
  235. st->wpos += read;
  236. if (chunk != read) {
  237. break;
  238. }
  239. }
  240. hda_timer_sync_adjust(st, -((wpos - rpos) - (B_SIZE >> 1)));
  241. }
  242. static void hda_audio_output_timer(void *opaque)
  243. {
  244. HDAAudioStream *st = opaque;
  245. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  246. int64_t uptime = now - st->buft_start;
  247. int64_t wpos = st->wpos;
  248. int64_t rpos = st->rpos;
  249. int64_t wanted_wpos;
  250. if (uptime <= 0) {
  251. /* wanted_wpos <= 0 */
  252. goto out_timer;
  253. }
  254. wanted_wpos = muldiv64(uptime, hda_bytes_per_second(st),
  255. NANOSECONDS_PER_SECOND);
  256. wanted_wpos &= -4; /* IMPORTANT! clip to frames */
  257. if (wanted_wpos <= wpos) {
  258. /* we already received the data */
  259. goto out_timer;
  260. }
  261. int64_t to_transfer = MIN(B_SIZE - (wpos - rpos), wanted_wpos - wpos);
  262. while (to_transfer) {
  263. uint32_t start = (wpos & B_MASK);
  264. uint32_t chunk = MIN(B_SIZE - start, to_transfer);
  265. int rc = hda_codec_xfer(
  266. &st->state->hda, st->stream, true, st->buf + start, chunk);
  267. if (!rc) {
  268. break;
  269. }
  270. wpos += chunk;
  271. to_transfer -= chunk;
  272. st->wpos += chunk;
  273. }
  274. out_timer:
  275. if (st->running) {
  276. timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS);
  277. }
  278. }
  279. static void hda_audio_output_cb(void *opaque, int avail)
  280. {
  281. HDAAudioStream *st = opaque;
  282. int64_t wpos = st->wpos;
  283. int64_t rpos = st->rpos;
  284. int64_t to_transfer = MIN(wpos - rpos, avail);
  285. if (wpos - rpos == B_SIZE) {
  286. /* drop buffer, reset timer adjust */
  287. st->rpos = 0;
  288. st->wpos = 0;
  289. st->buft_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  290. trace_hda_audio_overrun(st->node->name);
  291. return;
  292. }
  293. while (to_transfer) {
  294. uint32_t start = (uint32_t) (rpos & B_MASK);
  295. uint32_t chunk = (uint32_t) MIN(B_SIZE - start, to_transfer);
  296. uint32_t written = AUD_write(st->voice.out, st->buf + start, chunk);
  297. rpos += written;
  298. to_transfer -= written;
  299. st->rpos += written;
  300. if (chunk != written) {
  301. break;
  302. }
  303. }
  304. hda_timer_sync_adjust(st, (wpos - rpos) - (B_SIZE >> 1));
  305. }
  306. static void hda_audio_compat_input_cb(void *opaque, int avail)
  307. {
  308. HDAAudioStream *st = opaque;
  309. int recv = 0;
  310. int len;
  311. bool rc;
  312. while (avail - recv >= sizeof(st->compat_buf)) {
  313. if (st->compat_bpos != sizeof(st->compat_buf)) {
  314. len = AUD_read(st->voice.in, st->compat_buf + st->compat_bpos,
  315. sizeof(st->compat_buf) - st->compat_bpos);
  316. st->compat_bpos += len;
  317. recv += len;
  318. if (st->compat_bpos != sizeof(st->compat_buf)) {
  319. break;
  320. }
  321. }
  322. rc = hda_codec_xfer(&st->state->hda, st->stream, false,
  323. st->compat_buf, sizeof(st->compat_buf));
  324. if (!rc) {
  325. break;
  326. }
  327. st->compat_bpos = 0;
  328. }
  329. }
  330. static void hda_audio_compat_output_cb(void *opaque, int avail)
  331. {
  332. HDAAudioStream *st = opaque;
  333. int sent = 0;
  334. int len;
  335. bool rc;
  336. while (avail - sent >= sizeof(st->compat_buf)) {
  337. if (st->compat_bpos == sizeof(st->compat_buf)) {
  338. rc = hda_codec_xfer(&st->state->hda, st->stream, true,
  339. st->compat_buf, sizeof(st->compat_buf));
  340. if (!rc) {
  341. break;
  342. }
  343. st->compat_bpos = 0;
  344. }
  345. len = AUD_write(st->voice.out, st->compat_buf + st->compat_bpos,
  346. sizeof(st->compat_buf) - st->compat_bpos);
  347. st->compat_bpos += len;
  348. sent += len;
  349. if (st->compat_bpos != sizeof(st->compat_buf)) {
  350. break;
  351. }
  352. }
  353. }
  354. static void hda_audio_set_running(HDAAudioStream *st, bool running)
  355. {
  356. if (st->node == NULL) {
  357. return;
  358. }
  359. if (st->running == running) {
  360. return;
  361. }
  362. st->running = running;
  363. trace_hda_audio_running(st->node->name, st->stream, st->running);
  364. if (st->state->use_timer) {
  365. if (running) {
  366. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  367. st->rpos = 0;
  368. st->wpos = 0;
  369. st->buft_start = now;
  370. timer_mod_anticipate_ns(st->buft, now + HDA_TIMER_TICKS);
  371. } else {
  372. timer_del(st->buft);
  373. }
  374. }
  375. if (st->output) {
  376. AUD_set_active_out(st->voice.out, st->running);
  377. } else {
  378. AUD_set_active_in(st->voice.in, st->running);
  379. }
  380. }
  381. static void hda_audio_set_amp(HDAAudioStream *st)
  382. {
  383. bool muted;
  384. uint32_t left, right;
  385. if (st->node == NULL) {
  386. return;
  387. }
  388. muted = st->mute_left && st->mute_right;
  389. left = st->mute_left ? 0 : st->gain_left;
  390. right = st->mute_right ? 0 : st->gain_right;
  391. left = left * 255 / QEMU_HDA_AMP_STEPS;
  392. right = right * 255 / QEMU_HDA_AMP_STEPS;
  393. if (!st->state->mixer) {
  394. return;
  395. }
  396. if (st->output) {
  397. AUD_set_volume_out(st->voice.out, muted, left, right);
  398. } else {
  399. AUD_set_volume_in(st->voice.in, muted, left, right);
  400. }
  401. }
  402. static void hda_audio_setup(HDAAudioStream *st)
  403. {
  404. bool use_timer = st->state->use_timer;
  405. audio_callback_fn cb;
  406. if (st->node == NULL) {
  407. return;
  408. }
  409. trace_hda_audio_format(st->node->name, st->as.nchannels,
  410. fmt2name[st->as.fmt], st->as.freq);
  411. if (st->output) {
  412. if (use_timer) {
  413. cb = hda_audio_output_cb;
  414. timer_del(st->buft);
  415. } else {
  416. cb = hda_audio_compat_output_cb;
  417. }
  418. st->voice.out = AUD_open_out(&st->state->card, st->voice.out,
  419. st->node->name, st, cb, &st->as);
  420. } else {
  421. if (use_timer) {
  422. cb = hda_audio_input_cb;
  423. timer_del(st->buft);
  424. } else {
  425. cb = hda_audio_compat_input_cb;
  426. }
  427. st->voice.in = AUD_open_in(&st->state->card, st->voice.in,
  428. st->node->name, st, cb, &st->as);
  429. }
  430. }
  431. static void hda_audio_command(HDACodecDevice *hda, uint32_t nid, uint32_t data)
  432. {
  433. HDAAudioState *a = HDA_AUDIO(hda);
  434. HDAAudioStream *st;
  435. const desc_node *node = NULL;
  436. const desc_param *param;
  437. uint32_t verb, payload, response, count, shift;
  438. if ((data & 0x70000) == 0x70000) {
  439. /* 12/8 id/payload */
  440. verb = (data >> 8) & 0xfff;
  441. payload = data & 0x00ff;
  442. } else {
  443. /* 4/16 id/payload */
  444. verb = (data >> 8) & 0xf00;
  445. payload = data & 0xffff;
  446. }
  447. node = hda_codec_find_node(a->desc, nid);
  448. if (node == NULL) {
  449. goto fail;
  450. }
  451. dprint(a, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n",
  452. __func__, nid, node->name, verb, payload);
  453. switch (verb) {
  454. /* all nodes */
  455. case AC_VERB_PARAMETERS:
  456. param = hda_codec_find_param(node, payload);
  457. if (param == NULL) {
  458. goto fail;
  459. }
  460. hda_codec_response(hda, true, param->val);
  461. break;
  462. case AC_VERB_GET_SUBSYSTEM_ID:
  463. hda_codec_response(hda, true, a->desc->iid);
  464. break;
  465. /* all functions */
  466. case AC_VERB_GET_CONNECT_LIST:
  467. param = hda_codec_find_param(node, AC_PAR_CONNLIST_LEN);
  468. count = param ? param->val : 0;
  469. response = 0;
  470. shift = 0;
  471. while (payload < count && shift < 32) {
  472. response |= node->conn[payload] << shift;
  473. payload++;
  474. shift += 8;
  475. }
  476. hda_codec_response(hda, true, response);
  477. break;
  478. /* pin widget */
  479. case AC_VERB_GET_CONFIG_DEFAULT:
  480. hda_codec_response(hda, true, node->config);
  481. break;
  482. case AC_VERB_GET_PIN_WIDGET_CONTROL:
  483. hda_codec_response(hda, true, node->pinctl);
  484. break;
  485. case AC_VERB_SET_PIN_WIDGET_CONTROL:
  486. if (node->pinctl != payload) {
  487. dprint(a, 1, "unhandled pin control bit\n");
  488. }
  489. hda_codec_response(hda, true, 0);
  490. break;
  491. /* audio in/out widget */
  492. case AC_VERB_SET_CHANNEL_STREAMID:
  493. st = a->st + node->stindex;
  494. if (st->node == NULL) {
  495. goto fail;
  496. }
  497. hda_audio_set_running(st, false);
  498. st->stream = (payload >> 4) & 0x0f;
  499. st->channel = payload & 0x0f;
  500. dprint(a, 2, "%s: stream %d, channel %d\n",
  501. st->node->name, st->stream, st->channel);
  502. hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
  503. hda_codec_response(hda, true, 0);
  504. break;
  505. case AC_VERB_GET_CONV:
  506. st = a->st + node->stindex;
  507. if (st->node == NULL) {
  508. goto fail;
  509. }
  510. response = st->stream << 4 | st->channel;
  511. hda_codec_response(hda, true, response);
  512. break;
  513. case AC_VERB_SET_STREAM_FORMAT:
  514. st = a->st + node->stindex;
  515. if (st->node == NULL) {
  516. goto fail;
  517. }
  518. st->format = payload;
  519. hda_codec_parse_fmt(st->format, &st->as);
  520. hda_audio_setup(st);
  521. hda_codec_response(hda, true, 0);
  522. break;
  523. case AC_VERB_GET_STREAM_FORMAT:
  524. st = a->st + node->stindex;
  525. if (st->node == NULL) {
  526. goto fail;
  527. }
  528. hda_codec_response(hda, true, st->format);
  529. break;
  530. case AC_VERB_GET_AMP_GAIN_MUTE:
  531. st = a->st + node->stindex;
  532. if (st->node == NULL) {
  533. goto fail;
  534. }
  535. if (payload & AC_AMP_GET_LEFT) {
  536. response = st->gain_left | (st->mute_left ? AC_AMP_MUTE : 0);
  537. } else {
  538. response = st->gain_right | (st->mute_right ? AC_AMP_MUTE : 0);
  539. }
  540. hda_codec_response(hda, true, response);
  541. break;
  542. case AC_VERB_SET_AMP_GAIN_MUTE:
  543. st = a->st + node->stindex;
  544. if (st->node == NULL) {
  545. goto fail;
  546. }
  547. dprint(a, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n",
  548. st->node->name,
  549. (payload & AC_AMP_SET_OUTPUT) ? "o" : "-",
  550. (payload & AC_AMP_SET_INPUT) ? "i" : "-",
  551. (payload & AC_AMP_SET_LEFT) ? "l" : "-",
  552. (payload & AC_AMP_SET_RIGHT) ? "r" : "-",
  553. (payload & AC_AMP_SET_INDEX) >> AC_AMP_SET_INDEX_SHIFT,
  554. (payload & AC_AMP_GAIN),
  555. (payload & AC_AMP_MUTE) ? "muted" : "");
  556. if (payload & AC_AMP_SET_LEFT) {
  557. st->gain_left = payload & AC_AMP_GAIN;
  558. st->mute_left = payload & AC_AMP_MUTE;
  559. }
  560. if (payload & AC_AMP_SET_RIGHT) {
  561. st->gain_right = payload & AC_AMP_GAIN;
  562. st->mute_right = payload & AC_AMP_MUTE;
  563. }
  564. hda_audio_set_amp(st);
  565. hda_codec_response(hda, true, 0);
  566. break;
  567. /* not supported */
  568. case AC_VERB_SET_POWER_STATE:
  569. case AC_VERB_GET_POWER_STATE:
  570. case AC_VERB_GET_SDI_SELECT:
  571. hda_codec_response(hda, true, 0);
  572. break;
  573. default:
  574. goto fail;
  575. }
  576. return;
  577. fail:
  578. dprint(a, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n",
  579. __func__, nid, node ? node->name : "?", verb, payload);
  580. hda_codec_response(hda, true, 0);
  581. }
  582. static void hda_audio_stream(HDACodecDevice *hda, uint32_t stnr, bool running, bool output)
  583. {
  584. HDAAudioState *a = HDA_AUDIO(hda);
  585. int s;
  586. a->running_compat[stnr] = running;
  587. a->running_real[output * 16 + stnr] = running;
  588. for (s = 0; s < ARRAY_SIZE(a->st); s++) {
  589. if (a->st[s].node == NULL) {
  590. continue;
  591. }
  592. if (a->st[s].output != output) {
  593. continue;
  594. }
  595. if (a->st[s].stream != stnr) {
  596. continue;
  597. }
  598. hda_audio_set_running(&a->st[s], running);
  599. }
  600. }
  601. static void hda_audio_init(HDACodecDevice *hda,
  602. const struct desc_codec *desc,
  603. Error **errp)
  604. {
  605. HDAAudioState *a = HDA_AUDIO(hda);
  606. HDAAudioStream *st;
  607. const desc_node *node;
  608. const desc_param *param;
  609. uint32_t i, type;
  610. if (!AUD_register_card("hda", &a->card, errp)) {
  611. return;
  612. }
  613. a->desc = desc;
  614. a->name = object_get_typename(OBJECT(a));
  615. dprint(a, 1, "%s: cad %d\n", __func__, a->hda.cad);
  616. for (i = 0; i < a->desc->nnodes; i++) {
  617. node = a->desc->nodes + i;
  618. param = hda_codec_find_param(node, AC_PAR_AUDIO_WIDGET_CAP);
  619. if (param == NULL) {
  620. continue;
  621. }
  622. type = (param->val & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  623. switch (type) {
  624. case AC_WID_AUD_OUT:
  625. case AC_WID_AUD_IN:
  626. assert(node->stindex < ARRAY_SIZE(a->st));
  627. st = a->st + node->stindex;
  628. st->state = a;
  629. st->node = node;
  630. if (type == AC_WID_AUD_OUT) {
  631. /* unmute output by default */
  632. st->gain_left = QEMU_HDA_AMP_STEPS;
  633. st->gain_right = QEMU_HDA_AMP_STEPS;
  634. st->compat_bpos = sizeof(st->compat_buf);
  635. st->output = true;
  636. st->buft = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  637. hda_audio_output_timer, st);
  638. } else {
  639. st->output = false;
  640. st->buft = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  641. hda_audio_input_timer, st);
  642. }
  643. st->format = AC_FMT_TYPE_PCM | AC_FMT_BITS_16 |
  644. (1 << AC_FMT_CHAN_SHIFT);
  645. hda_codec_parse_fmt(st->format, &st->as);
  646. hda_audio_setup(st);
  647. break;
  648. }
  649. }
  650. }
  651. static void hda_audio_exit(HDACodecDevice *hda)
  652. {
  653. HDAAudioState *a = HDA_AUDIO(hda);
  654. HDAAudioStream *st;
  655. int i;
  656. dprint(a, 1, "%s\n", __func__);
  657. for (i = 0; i < ARRAY_SIZE(a->st); i++) {
  658. st = a->st + i;
  659. if (st->node == NULL) {
  660. continue;
  661. }
  662. timer_free(st->buft);
  663. if (st->output) {
  664. AUD_close_out(&a->card, st->voice.out);
  665. } else {
  666. AUD_close_in(&a->card, st->voice.in);
  667. }
  668. }
  669. AUD_remove_card(&a->card);
  670. }
  671. static int hda_audio_post_load(void *opaque, int version)
  672. {
  673. HDAAudioState *a = opaque;
  674. HDAAudioStream *st;
  675. int i;
  676. dprint(a, 1, "%s\n", __func__);
  677. if (version == 1) {
  678. /* assume running_compat[] is for output streams */
  679. for (i = 0; i < ARRAY_SIZE(a->running_compat); i++)
  680. a->running_real[16 + i] = a->running_compat[i];
  681. }
  682. for (i = 0; i < ARRAY_SIZE(a->st); i++) {
  683. st = a->st + i;
  684. if (st->node == NULL)
  685. continue;
  686. hda_codec_parse_fmt(st->format, &st->as);
  687. hda_audio_setup(st);
  688. hda_audio_set_amp(st);
  689. hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
  690. }
  691. return 0;
  692. }
  693. static void hda_audio_reset(DeviceState *dev)
  694. {
  695. HDAAudioState *a = HDA_AUDIO(dev);
  696. HDAAudioStream *st;
  697. int i;
  698. dprint(a, 1, "%s\n", __func__);
  699. for (i = 0; i < ARRAY_SIZE(a->st); i++) {
  700. st = a->st + i;
  701. if (st->node != NULL) {
  702. hda_audio_set_running(st, false);
  703. }
  704. }
  705. }
  706. static bool vmstate_hda_audio_stream_buf_needed(void *opaque)
  707. {
  708. HDAAudioStream *st = opaque;
  709. return st->state && st->state->use_timer;
  710. }
  711. static const VMStateDescription vmstate_hda_audio_stream_buf = {
  712. .name = "hda-audio-stream/buffer",
  713. .version_id = 1,
  714. .needed = vmstate_hda_audio_stream_buf_needed,
  715. .fields = (const VMStateField[]) {
  716. VMSTATE_BUFFER(buf, HDAAudioStream),
  717. VMSTATE_INT64(rpos, HDAAudioStream),
  718. VMSTATE_INT64(wpos, HDAAudioStream),
  719. VMSTATE_TIMER_PTR(buft, HDAAudioStream),
  720. VMSTATE_INT64(buft_start, HDAAudioStream),
  721. VMSTATE_END_OF_LIST()
  722. }
  723. };
  724. static const VMStateDescription vmstate_hda_audio_stream = {
  725. .name = "hda-audio-stream",
  726. .version_id = 1,
  727. .fields = (const VMStateField[]) {
  728. VMSTATE_UINT32(stream, HDAAudioStream),
  729. VMSTATE_UINT32(channel, HDAAudioStream),
  730. VMSTATE_UINT32(format, HDAAudioStream),
  731. VMSTATE_UINT32(gain_left, HDAAudioStream),
  732. VMSTATE_UINT32(gain_right, HDAAudioStream),
  733. VMSTATE_BOOL(mute_left, HDAAudioStream),
  734. VMSTATE_BOOL(mute_right, HDAAudioStream),
  735. VMSTATE_UINT32(compat_bpos, HDAAudioStream),
  736. VMSTATE_BUFFER(compat_buf, HDAAudioStream),
  737. VMSTATE_END_OF_LIST()
  738. },
  739. .subsections = (const VMStateDescription * const []) {
  740. &vmstate_hda_audio_stream_buf,
  741. NULL
  742. }
  743. };
  744. static const VMStateDescription vmstate_hda_audio = {
  745. .name = "hda-audio",
  746. .version_id = 2,
  747. .post_load = hda_audio_post_load,
  748. .fields = (const VMStateField[]) {
  749. VMSTATE_STRUCT_ARRAY(st, HDAAudioState, 4, 0,
  750. vmstate_hda_audio_stream,
  751. HDAAudioStream),
  752. VMSTATE_BOOL_ARRAY(running_compat, HDAAudioState, 16),
  753. VMSTATE_BOOL_ARRAY_V(running_real, HDAAudioState, 2 * 16, 2),
  754. VMSTATE_END_OF_LIST()
  755. }
  756. };
  757. static const Property hda_audio_properties[] = {
  758. DEFINE_AUDIO_PROPERTIES(HDAAudioState, card),
  759. DEFINE_PROP_UINT32("debug", HDAAudioState, debug, 0),
  760. DEFINE_PROP_BOOL("mixer", HDAAudioState, mixer, true),
  761. DEFINE_PROP_BOOL("use-timer", HDAAudioState, use_timer, true),
  762. };
  763. static void hda_audio_init_output(HDACodecDevice *hda, Error **errp)
  764. {
  765. HDAAudioState *a = HDA_AUDIO(hda);
  766. const struct desc_codec *desc = &output_mixemu;
  767. if (!a->mixer) {
  768. desc = &output_nomixemu;
  769. }
  770. hda_audio_init(hda, desc, errp);
  771. }
  772. static void hda_audio_init_duplex(HDACodecDevice *hda, Error **errp)
  773. {
  774. HDAAudioState *a = HDA_AUDIO(hda);
  775. const struct desc_codec *desc = &duplex_mixemu;
  776. if (!a->mixer) {
  777. desc = &duplex_nomixemu;
  778. }
  779. hda_audio_init(hda, desc, errp);
  780. }
  781. static void hda_audio_init_micro(HDACodecDevice *hda, Error **errp)
  782. {
  783. HDAAudioState *a = HDA_AUDIO(hda);
  784. const struct desc_codec *desc = &micro_mixemu;
  785. if (!a->mixer) {
  786. desc = &micro_nomixemu;
  787. }
  788. hda_audio_init(hda, desc, errp);
  789. }
  790. static void hda_audio_base_class_init(ObjectClass *klass, void *data)
  791. {
  792. DeviceClass *dc = DEVICE_CLASS(klass);
  793. HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
  794. k->exit = hda_audio_exit;
  795. k->command = hda_audio_command;
  796. k->stream = hda_audio_stream;
  797. set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
  798. device_class_set_legacy_reset(dc, hda_audio_reset);
  799. dc->vmsd = &vmstate_hda_audio;
  800. device_class_set_props(dc, hda_audio_properties);
  801. }
  802. static const TypeInfo hda_audio_info = {
  803. .name = TYPE_HDA_AUDIO,
  804. .parent = TYPE_HDA_CODEC_DEVICE,
  805. .instance_size = sizeof(HDAAudioState),
  806. .class_init = hda_audio_base_class_init,
  807. .abstract = true,
  808. };
  809. static void hda_audio_output_class_init(ObjectClass *klass, void *data)
  810. {
  811. DeviceClass *dc = DEVICE_CLASS(klass);
  812. HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
  813. k->init = hda_audio_init_output;
  814. dc->desc = "HDA Audio Codec, output-only (line-out)";
  815. }
  816. static const TypeInfo hda_audio_output_info = {
  817. .name = "hda-output",
  818. .parent = TYPE_HDA_AUDIO,
  819. .class_init = hda_audio_output_class_init,
  820. };
  821. static void hda_audio_duplex_class_init(ObjectClass *klass, void *data)
  822. {
  823. DeviceClass *dc = DEVICE_CLASS(klass);
  824. HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
  825. k->init = hda_audio_init_duplex;
  826. dc->desc = "HDA Audio Codec, duplex (line-out, line-in)";
  827. }
  828. static const TypeInfo hda_audio_duplex_info = {
  829. .name = "hda-duplex",
  830. .parent = TYPE_HDA_AUDIO,
  831. .class_init = hda_audio_duplex_class_init,
  832. };
  833. static void hda_audio_micro_class_init(ObjectClass *klass, void *data)
  834. {
  835. DeviceClass *dc = DEVICE_CLASS(klass);
  836. HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
  837. k->init = hda_audio_init_micro;
  838. dc->desc = "HDA Audio Codec, duplex (speaker, microphone)";
  839. }
  840. static const TypeInfo hda_audio_micro_info = {
  841. .name = "hda-micro",
  842. .parent = TYPE_HDA_AUDIO,
  843. .class_init = hda_audio_micro_class_init,
  844. };
  845. static void hda_audio_register_types(void)
  846. {
  847. type_register_static(&hda_audio_info);
  848. type_register_static(&hda_audio_output_info);
  849. type_register_static(&hda_audio_duplex_info);
  850. type_register_static(&hda_audio_micro_info);
  851. }
  852. type_init(hda_audio_register_types)