hda-codec.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. st->buft = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  415. hda_audio_output_timer, st);
  416. } else {
  417. cb = hda_audio_compat_output_cb;
  418. }
  419. st->voice.out = AUD_open_out(&st->state->card, st->voice.out,
  420. st->node->name, st, cb, &st->as);
  421. } else {
  422. if (use_timer) {
  423. cb = hda_audio_input_cb;
  424. st->buft = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  425. hda_audio_input_timer, st);
  426. } else {
  427. cb = hda_audio_compat_input_cb;
  428. }
  429. st->voice.in = AUD_open_in(&st->state->card, st->voice.in,
  430. st->node->name, st, cb, &st->as);
  431. }
  432. }
  433. static void hda_audio_command(HDACodecDevice *hda, uint32_t nid, uint32_t data)
  434. {
  435. HDAAudioState *a = HDA_AUDIO(hda);
  436. HDAAudioStream *st;
  437. const desc_node *node = NULL;
  438. const desc_param *param;
  439. uint32_t verb, payload, response, count, shift;
  440. if ((data & 0x70000) == 0x70000) {
  441. /* 12/8 id/payload */
  442. verb = (data >> 8) & 0xfff;
  443. payload = data & 0x00ff;
  444. } else {
  445. /* 4/16 id/payload */
  446. verb = (data >> 8) & 0xf00;
  447. payload = data & 0xffff;
  448. }
  449. node = hda_codec_find_node(a->desc, nid);
  450. if (node == NULL) {
  451. goto fail;
  452. }
  453. dprint(a, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n",
  454. __func__, nid, node->name, verb, payload);
  455. switch (verb) {
  456. /* all nodes */
  457. case AC_VERB_PARAMETERS:
  458. param = hda_codec_find_param(node, payload);
  459. if (param == NULL) {
  460. goto fail;
  461. }
  462. hda_codec_response(hda, true, param->val);
  463. break;
  464. case AC_VERB_GET_SUBSYSTEM_ID:
  465. hda_codec_response(hda, true, a->desc->iid);
  466. break;
  467. /* all functions */
  468. case AC_VERB_GET_CONNECT_LIST:
  469. param = hda_codec_find_param(node, AC_PAR_CONNLIST_LEN);
  470. count = param ? param->val : 0;
  471. response = 0;
  472. shift = 0;
  473. while (payload < count && shift < 32) {
  474. response |= node->conn[payload] << shift;
  475. payload++;
  476. shift += 8;
  477. }
  478. hda_codec_response(hda, true, response);
  479. break;
  480. /* pin widget */
  481. case AC_VERB_GET_CONFIG_DEFAULT:
  482. hda_codec_response(hda, true, node->config);
  483. break;
  484. case AC_VERB_GET_PIN_WIDGET_CONTROL:
  485. hda_codec_response(hda, true, node->pinctl);
  486. break;
  487. case AC_VERB_SET_PIN_WIDGET_CONTROL:
  488. if (node->pinctl != payload) {
  489. dprint(a, 1, "unhandled pin control bit\n");
  490. }
  491. hda_codec_response(hda, true, 0);
  492. break;
  493. /* audio in/out widget */
  494. case AC_VERB_SET_CHANNEL_STREAMID:
  495. st = a->st + node->stindex;
  496. if (st->node == NULL) {
  497. goto fail;
  498. }
  499. hda_audio_set_running(st, false);
  500. st->stream = (payload >> 4) & 0x0f;
  501. st->channel = payload & 0x0f;
  502. dprint(a, 2, "%s: stream %d, channel %d\n",
  503. st->node->name, st->stream, st->channel);
  504. hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
  505. hda_codec_response(hda, true, 0);
  506. break;
  507. case AC_VERB_GET_CONV:
  508. st = a->st + node->stindex;
  509. if (st->node == NULL) {
  510. goto fail;
  511. }
  512. response = st->stream << 4 | st->channel;
  513. hda_codec_response(hda, true, response);
  514. break;
  515. case AC_VERB_SET_STREAM_FORMAT:
  516. st = a->st + node->stindex;
  517. if (st->node == NULL) {
  518. goto fail;
  519. }
  520. st->format = payload;
  521. hda_codec_parse_fmt(st->format, &st->as);
  522. hda_audio_setup(st);
  523. hda_codec_response(hda, true, 0);
  524. break;
  525. case AC_VERB_GET_STREAM_FORMAT:
  526. st = a->st + node->stindex;
  527. if (st->node == NULL) {
  528. goto fail;
  529. }
  530. hda_codec_response(hda, true, st->format);
  531. break;
  532. case AC_VERB_GET_AMP_GAIN_MUTE:
  533. st = a->st + node->stindex;
  534. if (st->node == NULL) {
  535. goto fail;
  536. }
  537. if (payload & AC_AMP_GET_LEFT) {
  538. response = st->gain_left | (st->mute_left ? AC_AMP_MUTE : 0);
  539. } else {
  540. response = st->gain_right | (st->mute_right ? AC_AMP_MUTE : 0);
  541. }
  542. hda_codec_response(hda, true, response);
  543. break;
  544. case AC_VERB_SET_AMP_GAIN_MUTE:
  545. st = a->st + node->stindex;
  546. if (st->node == NULL) {
  547. goto fail;
  548. }
  549. dprint(a, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n",
  550. st->node->name,
  551. (payload & AC_AMP_SET_OUTPUT) ? "o" : "-",
  552. (payload & AC_AMP_SET_INPUT) ? "i" : "-",
  553. (payload & AC_AMP_SET_LEFT) ? "l" : "-",
  554. (payload & AC_AMP_SET_RIGHT) ? "r" : "-",
  555. (payload & AC_AMP_SET_INDEX) >> AC_AMP_SET_INDEX_SHIFT,
  556. (payload & AC_AMP_GAIN),
  557. (payload & AC_AMP_MUTE) ? "muted" : "");
  558. if (payload & AC_AMP_SET_LEFT) {
  559. st->gain_left = payload & AC_AMP_GAIN;
  560. st->mute_left = payload & AC_AMP_MUTE;
  561. }
  562. if (payload & AC_AMP_SET_RIGHT) {
  563. st->gain_right = payload & AC_AMP_GAIN;
  564. st->mute_right = payload & AC_AMP_MUTE;
  565. }
  566. hda_audio_set_amp(st);
  567. hda_codec_response(hda, true, 0);
  568. break;
  569. /* not supported */
  570. case AC_VERB_SET_POWER_STATE:
  571. case AC_VERB_GET_POWER_STATE:
  572. case AC_VERB_GET_SDI_SELECT:
  573. hda_codec_response(hda, true, 0);
  574. break;
  575. default:
  576. goto fail;
  577. }
  578. return;
  579. fail:
  580. dprint(a, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n",
  581. __func__, nid, node ? node->name : "?", verb, payload);
  582. hda_codec_response(hda, true, 0);
  583. }
  584. static void hda_audio_stream(HDACodecDevice *hda, uint32_t stnr, bool running, bool output)
  585. {
  586. HDAAudioState *a = HDA_AUDIO(hda);
  587. int s;
  588. a->running_compat[stnr] = running;
  589. a->running_real[output * 16 + stnr] = running;
  590. for (s = 0; s < ARRAY_SIZE(a->st); s++) {
  591. if (a->st[s].node == NULL) {
  592. continue;
  593. }
  594. if (a->st[s].output != output) {
  595. continue;
  596. }
  597. if (a->st[s].stream != stnr) {
  598. continue;
  599. }
  600. hda_audio_set_running(&a->st[s], running);
  601. }
  602. }
  603. static void hda_audio_init(HDACodecDevice *hda,
  604. const struct desc_codec *desc,
  605. Error **errp)
  606. {
  607. HDAAudioState *a = HDA_AUDIO(hda);
  608. HDAAudioStream *st;
  609. const desc_node *node;
  610. const desc_param *param;
  611. uint32_t i, type;
  612. if (!AUD_register_card("hda", &a->card, errp)) {
  613. return;
  614. }
  615. a->desc = desc;
  616. a->name = object_get_typename(OBJECT(a));
  617. dprint(a, 1, "%s: cad %d\n", __func__, a->hda.cad);
  618. for (i = 0; i < a->desc->nnodes; i++) {
  619. node = a->desc->nodes + i;
  620. param = hda_codec_find_param(node, AC_PAR_AUDIO_WIDGET_CAP);
  621. if (param == NULL) {
  622. continue;
  623. }
  624. type = (param->val & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  625. switch (type) {
  626. case AC_WID_AUD_OUT:
  627. case AC_WID_AUD_IN:
  628. assert(node->stindex < ARRAY_SIZE(a->st));
  629. st = a->st + node->stindex;
  630. st->state = a;
  631. st->node = node;
  632. if (type == AC_WID_AUD_OUT) {
  633. /* unmute output by default */
  634. st->gain_left = QEMU_HDA_AMP_STEPS;
  635. st->gain_right = QEMU_HDA_AMP_STEPS;
  636. st->compat_bpos = sizeof(st->compat_buf);
  637. st->output = true;
  638. } else {
  639. st->output = false;
  640. }
  641. st->format = AC_FMT_TYPE_PCM | AC_FMT_BITS_16 |
  642. (1 << AC_FMT_CHAN_SHIFT);
  643. hda_codec_parse_fmt(st->format, &st->as);
  644. hda_audio_setup(st);
  645. break;
  646. }
  647. }
  648. }
  649. static void hda_audio_exit(HDACodecDevice *hda)
  650. {
  651. HDAAudioState *a = HDA_AUDIO(hda);
  652. HDAAudioStream *st;
  653. int i;
  654. dprint(a, 1, "%s\n", __func__);
  655. for (i = 0; i < ARRAY_SIZE(a->st); i++) {
  656. st = a->st + i;
  657. if (st->node == NULL) {
  658. continue;
  659. }
  660. if (a->use_timer) {
  661. timer_del(st->buft);
  662. }
  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 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. DEFINE_PROP_END_OF_LIST(),
  763. };
  764. static void hda_audio_init_output(HDACodecDevice *hda, Error **errp)
  765. {
  766. HDAAudioState *a = HDA_AUDIO(hda);
  767. const struct desc_codec *desc = &output_mixemu;
  768. if (!a->mixer) {
  769. desc = &output_nomixemu;
  770. }
  771. hda_audio_init(hda, desc, errp);
  772. }
  773. static void hda_audio_init_duplex(HDACodecDevice *hda, Error **errp)
  774. {
  775. HDAAudioState *a = HDA_AUDIO(hda);
  776. const struct desc_codec *desc = &duplex_mixemu;
  777. if (!a->mixer) {
  778. desc = &duplex_nomixemu;
  779. }
  780. hda_audio_init(hda, desc, errp);
  781. }
  782. static void hda_audio_init_micro(HDACodecDevice *hda, Error **errp)
  783. {
  784. HDAAudioState *a = HDA_AUDIO(hda);
  785. const struct desc_codec *desc = &micro_mixemu;
  786. if (!a->mixer) {
  787. desc = &micro_nomixemu;
  788. }
  789. hda_audio_init(hda, desc, errp);
  790. }
  791. static void hda_audio_base_class_init(ObjectClass *klass, void *data)
  792. {
  793. DeviceClass *dc = DEVICE_CLASS(klass);
  794. HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
  795. k->exit = hda_audio_exit;
  796. k->command = hda_audio_command;
  797. k->stream = hda_audio_stream;
  798. set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
  799. device_class_set_legacy_reset(dc, hda_audio_reset);
  800. dc->vmsd = &vmstate_hda_audio;
  801. device_class_set_props(dc, hda_audio_properties);
  802. }
  803. static const TypeInfo hda_audio_info = {
  804. .name = TYPE_HDA_AUDIO,
  805. .parent = TYPE_HDA_CODEC_DEVICE,
  806. .instance_size = sizeof(HDAAudioState),
  807. .class_init = hda_audio_base_class_init,
  808. .abstract = true,
  809. };
  810. static void hda_audio_output_class_init(ObjectClass *klass, void *data)
  811. {
  812. DeviceClass *dc = DEVICE_CLASS(klass);
  813. HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
  814. k->init = hda_audio_init_output;
  815. dc->desc = "HDA Audio Codec, output-only (line-out)";
  816. }
  817. static const TypeInfo hda_audio_output_info = {
  818. .name = "hda-output",
  819. .parent = TYPE_HDA_AUDIO,
  820. .class_init = hda_audio_output_class_init,
  821. };
  822. static void hda_audio_duplex_class_init(ObjectClass *klass, void *data)
  823. {
  824. DeviceClass *dc = DEVICE_CLASS(klass);
  825. HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
  826. k->init = hda_audio_init_duplex;
  827. dc->desc = "HDA Audio Codec, duplex (line-out, line-in)";
  828. }
  829. static const TypeInfo hda_audio_duplex_info = {
  830. .name = "hda-duplex",
  831. .parent = TYPE_HDA_AUDIO,
  832. .class_init = hda_audio_duplex_class_init,
  833. };
  834. static void hda_audio_micro_class_init(ObjectClass *klass, void *data)
  835. {
  836. DeviceClass *dc = DEVICE_CLASS(klass);
  837. HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
  838. k->init = hda_audio_init_micro;
  839. dc->desc = "HDA Audio Codec, duplex (speaker, microphone)";
  840. }
  841. static const TypeInfo hda_audio_micro_info = {
  842. .name = "hda-micro",
  843. .parent = TYPE_HDA_AUDIO,
  844. .class_init = hda_audio_micro_class_init,
  845. };
  846. static void hda_audio_register_types(void)
  847. {
  848. type_register_static(&hda_audio_info);
  849. type_register_static(&hda_audio_output_info);
  850. type_register_static(&hda_audio_duplex_info);
  851. type_register_static(&hda_audio_micro_info);
  852. }
  853. type_init(hda_audio_register_types)