hda-codec.c 27 KB

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