2
0

hda-codec.c 27 KB

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