most_snd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sound.c - Sound component for Mostcore
  4. *
  5. * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <sound/core.h>
  14. #include <sound/pcm.h>
  15. #include <sound/pcm_params.h>
  16. #include <linux/sched.h>
  17. #include <linux/kthread.h>
  18. #include <linux/most.h>
  19. #define DRIVER_NAME "sound"
  20. #define STRING_SIZE 80
  21. static struct most_component comp;
  22. /**
  23. * struct channel - private structure to keep channel specific data
  24. * @substream: stores the substream structure
  25. * @pcm_hardware: low-level hardware description
  26. * @iface: interface for which the channel belongs to
  27. * @cfg: channel configuration
  28. * @card: registered sound card
  29. * @list: list for private use
  30. * @id: channel index
  31. * @period_pos: current period position (ring buffer)
  32. * @buffer_pos: current buffer position (ring buffer)
  33. * @is_stream_running: identifies whether a stream is running or not
  34. * @opened: set when the stream is opened
  35. * @playback_task: playback thread
  36. * @playback_waitq: waitq used by playback thread
  37. * @copy_fn: copy function for PCM-specific format and width
  38. */
  39. struct channel {
  40. struct snd_pcm_substream *substream;
  41. struct snd_pcm_hardware pcm_hardware;
  42. struct most_interface *iface;
  43. struct most_channel_config *cfg;
  44. struct snd_card *card;
  45. struct list_head list;
  46. int id;
  47. unsigned int period_pos;
  48. unsigned int buffer_pos;
  49. bool is_stream_running;
  50. struct task_struct *playback_task;
  51. wait_queue_head_t playback_waitq;
  52. void (*copy_fn)(void *alsa, void *most, unsigned int bytes);
  53. };
  54. struct sound_adapter {
  55. struct list_head dev_list;
  56. struct most_interface *iface;
  57. struct snd_card *card;
  58. struct list_head list;
  59. bool registered;
  60. int pcm_dev_idx;
  61. };
  62. static struct list_head adpt_list;
  63. #define MOST_PCM_INFO (SNDRV_PCM_INFO_MMAP | \
  64. SNDRV_PCM_INFO_MMAP_VALID | \
  65. SNDRV_PCM_INFO_BATCH | \
  66. SNDRV_PCM_INFO_INTERLEAVED | \
  67. SNDRV_PCM_INFO_BLOCK_TRANSFER)
  68. static void swap_copy16(u16 *dest, const u16 *source, unsigned int bytes)
  69. {
  70. unsigned int i = 0;
  71. while (i < (bytes / 2)) {
  72. dest[i] = swab16(source[i]);
  73. i++;
  74. }
  75. }
  76. static void swap_copy24(u8 *dest, const u8 *source, unsigned int bytes)
  77. {
  78. unsigned int i = 0;
  79. if (bytes < 2)
  80. return;
  81. while (i < bytes - 2) {
  82. dest[i] = source[i + 2];
  83. dest[i + 1] = source[i + 1];
  84. dest[i + 2] = source[i];
  85. i += 3;
  86. }
  87. }
  88. static void swap_copy32(u32 *dest, const u32 *source, unsigned int bytes)
  89. {
  90. unsigned int i = 0;
  91. while (i < bytes / 4) {
  92. dest[i] = swab32(source[i]);
  93. i++;
  94. }
  95. }
  96. static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes)
  97. {
  98. memcpy(most, alsa, bytes);
  99. }
  100. static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes)
  101. {
  102. swap_copy16(most, alsa, bytes);
  103. }
  104. static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes)
  105. {
  106. swap_copy24(most, alsa, bytes);
  107. }
  108. static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes)
  109. {
  110. swap_copy32(most, alsa, bytes);
  111. }
  112. static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes)
  113. {
  114. memcpy(alsa, most, bytes);
  115. }
  116. static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes)
  117. {
  118. swap_copy16(alsa, most, bytes);
  119. }
  120. static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes)
  121. {
  122. swap_copy24(alsa, most, bytes);
  123. }
  124. static void most_to_alsa_copy32(void *alsa, void *most, unsigned int bytes)
  125. {
  126. swap_copy32(alsa, most, bytes);
  127. }
  128. /**
  129. * get_channel - get pointer to channel
  130. * @iface: interface structure
  131. * @channel_id: channel ID
  132. *
  133. * This traverses the channel list and returns the channel matching the
  134. * ID and interface.
  135. *
  136. * Returns pointer to channel on success or NULL otherwise.
  137. */
  138. static struct channel *get_channel(struct most_interface *iface,
  139. int channel_id)
  140. {
  141. struct sound_adapter *adpt = iface->priv;
  142. struct channel *channel;
  143. list_for_each_entry(channel, &adpt->dev_list, list) {
  144. if ((channel->iface == iface) && (channel->id == channel_id))
  145. return channel;
  146. }
  147. return NULL;
  148. }
  149. /**
  150. * copy_data - implements data copying function
  151. * @channel: channel
  152. * @mbo: MBO from core
  153. *
  154. * Copy data from/to ring buffer to/from MBO and update the buffer position
  155. */
  156. static bool copy_data(struct channel *channel, struct mbo *mbo)
  157. {
  158. struct snd_pcm_runtime *const runtime = channel->substream->runtime;
  159. unsigned int const frame_bytes = channel->cfg->subbuffer_size;
  160. unsigned int const buffer_size = runtime->buffer_size;
  161. unsigned int frames;
  162. unsigned int fr0;
  163. if (channel->cfg->direction & MOST_CH_RX)
  164. frames = mbo->processed_length / frame_bytes;
  165. else
  166. frames = mbo->buffer_length / frame_bytes;
  167. fr0 = min(buffer_size - channel->buffer_pos, frames);
  168. channel->copy_fn(runtime->dma_area + channel->buffer_pos * frame_bytes,
  169. mbo->virt_address,
  170. fr0 * frame_bytes);
  171. if (frames > fr0) {
  172. /* wrap around at end of ring buffer */
  173. channel->copy_fn(runtime->dma_area,
  174. mbo->virt_address + fr0 * frame_bytes,
  175. (frames - fr0) * frame_bytes);
  176. }
  177. channel->buffer_pos += frames;
  178. if (channel->buffer_pos >= buffer_size)
  179. channel->buffer_pos -= buffer_size;
  180. channel->period_pos += frames;
  181. if (channel->period_pos >= runtime->period_size) {
  182. channel->period_pos -= runtime->period_size;
  183. return true;
  184. }
  185. return false;
  186. }
  187. /**
  188. * playback_thread - function implements the playback thread
  189. * @data: private data
  190. *
  191. * Thread which does the playback functionality in a loop. It waits for a free
  192. * MBO from mostcore for a particular channel and copy the data from ring buffer
  193. * to MBO. Submit the MBO back to mostcore, after copying the data.
  194. *
  195. * Returns 0 on success or error code otherwise.
  196. */
  197. static int playback_thread(void *data)
  198. {
  199. struct channel *const channel = data;
  200. while (!kthread_should_stop()) {
  201. struct mbo *mbo = NULL;
  202. bool period_elapsed = false;
  203. wait_event_interruptible(
  204. channel->playback_waitq,
  205. kthread_should_stop() ||
  206. (channel->is_stream_running &&
  207. (mbo = most_get_mbo(channel->iface, channel->id,
  208. &comp))));
  209. if (!mbo)
  210. continue;
  211. if (channel->is_stream_running)
  212. period_elapsed = copy_data(channel, mbo);
  213. else
  214. memset(mbo->virt_address, 0, mbo->buffer_length);
  215. most_submit_mbo(mbo);
  216. if (period_elapsed)
  217. snd_pcm_period_elapsed(channel->substream);
  218. }
  219. return 0;
  220. }
  221. /**
  222. * pcm_open - implements open callback function for PCM middle layer
  223. * @substream: pointer to ALSA PCM substream
  224. *
  225. * This is called when a PCM substream is opened. At least, the function should
  226. * initialize the runtime->hw record.
  227. *
  228. * Returns 0 on success or error code otherwise.
  229. */
  230. static int pcm_open(struct snd_pcm_substream *substream)
  231. {
  232. struct channel *channel = substream->private_data;
  233. struct snd_pcm_runtime *runtime = substream->runtime;
  234. struct most_channel_config *cfg = channel->cfg;
  235. int ret;
  236. channel->substream = substream;
  237. if (cfg->direction == MOST_CH_TX) {
  238. channel->playback_task = kthread_run(playback_thread, channel,
  239. "most_audio_playback");
  240. if (IS_ERR(channel->playback_task)) {
  241. pr_err("Couldn't start thread\n");
  242. return PTR_ERR(channel->playback_task);
  243. }
  244. }
  245. ret = most_start_channel(channel->iface, channel->id, &comp);
  246. if (ret) {
  247. pr_err("most_start_channel() failed!\n");
  248. if (cfg->direction == MOST_CH_TX)
  249. kthread_stop(channel->playback_task);
  250. return ret;
  251. }
  252. runtime->hw = channel->pcm_hardware;
  253. return 0;
  254. }
  255. /**
  256. * pcm_close - implements close callback function for PCM middle layer
  257. * @substream: sub-stream pointer
  258. *
  259. * Obviously, this is called when a PCM substream is closed. Any private
  260. * instance for a PCM substream allocated in the open callback will be
  261. * released here.
  262. *
  263. * Returns 0 on success or error code otherwise.
  264. */
  265. static int pcm_close(struct snd_pcm_substream *substream)
  266. {
  267. struct channel *channel = substream->private_data;
  268. if (channel->cfg->direction == MOST_CH_TX)
  269. kthread_stop(channel->playback_task);
  270. most_stop_channel(channel->iface, channel->id, &comp);
  271. return 0;
  272. }
  273. /**
  274. * pcm_prepare - implements prepare callback function for PCM middle layer
  275. * @substream: substream pointer
  276. *
  277. * This callback is called when the PCM is "prepared". Format rate, sample rate,
  278. * etc., can be set here. This callback can be called many times at each setup.
  279. *
  280. * Returns 0 on success or error code otherwise.
  281. */
  282. static int pcm_prepare(struct snd_pcm_substream *substream)
  283. {
  284. struct channel *channel = substream->private_data;
  285. struct snd_pcm_runtime *runtime = substream->runtime;
  286. struct most_channel_config *cfg = channel->cfg;
  287. int width = snd_pcm_format_physical_width(runtime->format);
  288. channel->copy_fn = NULL;
  289. if (cfg->direction == MOST_CH_TX) {
  290. if (snd_pcm_format_big_endian(runtime->format) || width == 8)
  291. channel->copy_fn = alsa_to_most_memcpy;
  292. else if (width == 16)
  293. channel->copy_fn = alsa_to_most_copy16;
  294. else if (width == 24)
  295. channel->copy_fn = alsa_to_most_copy24;
  296. else if (width == 32)
  297. channel->copy_fn = alsa_to_most_copy32;
  298. } else {
  299. if (snd_pcm_format_big_endian(runtime->format) || width == 8)
  300. channel->copy_fn = most_to_alsa_memcpy;
  301. else if (width == 16)
  302. channel->copy_fn = most_to_alsa_copy16;
  303. else if (width == 24)
  304. channel->copy_fn = most_to_alsa_copy24;
  305. else if (width == 32)
  306. channel->copy_fn = most_to_alsa_copy32;
  307. }
  308. if (!channel->copy_fn)
  309. return -EINVAL;
  310. channel->period_pos = 0;
  311. channel->buffer_pos = 0;
  312. return 0;
  313. }
  314. /**
  315. * pcm_trigger - implements trigger callback function for PCM middle layer
  316. * @substream: substream pointer
  317. * @cmd: action to perform
  318. *
  319. * This is called when the PCM is started, stopped or paused. The action will be
  320. * specified in the second argument, SNDRV_PCM_TRIGGER_XXX
  321. *
  322. * Returns 0 on success or error code otherwise.
  323. */
  324. static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  325. {
  326. struct channel *channel = substream->private_data;
  327. switch (cmd) {
  328. case SNDRV_PCM_TRIGGER_START:
  329. channel->is_stream_running = true;
  330. wake_up_interruptible(&channel->playback_waitq);
  331. return 0;
  332. case SNDRV_PCM_TRIGGER_STOP:
  333. channel->is_stream_running = false;
  334. return 0;
  335. default:
  336. return -EINVAL;
  337. }
  338. return 0;
  339. }
  340. /**
  341. * pcm_pointer - implements pointer callback function for PCM middle layer
  342. * @substream: substream pointer
  343. *
  344. * This callback is called when the PCM middle layer inquires the current
  345. * hardware position on the buffer. The position must be returned in frames,
  346. * ranging from 0 to buffer_size-1.
  347. */
  348. static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
  349. {
  350. struct channel *channel = substream->private_data;
  351. return channel->buffer_pos;
  352. }
  353. /*
  354. * Initialization of struct snd_pcm_ops
  355. */
  356. static const struct snd_pcm_ops pcm_ops = {
  357. .open = pcm_open,
  358. .close = pcm_close,
  359. .prepare = pcm_prepare,
  360. .trigger = pcm_trigger,
  361. .pointer = pcm_pointer,
  362. };
  363. static int split_arg_list(char *buf, u16 *ch_num, char **sample_res)
  364. {
  365. char *num;
  366. int ret;
  367. num = strsep(&buf, "x");
  368. if (!num)
  369. goto err;
  370. ret = kstrtou16(num, 0, ch_num);
  371. if (ret)
  372. goto err;
  373. *sample_res = strsep(&buf, ".\n");
  374. if (!*sample_res)
  375. goto err;
  376. return 0;
  377. err:
  378. pr_err("Bad PCM format\n");
  379. return -EINVAL;
  380. }
  381. static const struct sample_resolution_info {
  382. const char *sample_res;
  383. int bytes;
  384. u64 formats;
  385. } sinfo[] = {
  386. { "8", 1, SNDRV_PCM_FMTBIT_S8 },
  387. { "16", 2, SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE },
  388. { "24", 3, SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE },
  389. { "32", 4, SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE },
  390. };
  391. static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw,
  392. u16 ch_num, char *sample_res,
  393. struct most_channel_config *cfg)
  394. {
  395. int i;
  396. for (i = 0; i < ARRAY_SIZE(sinfo); i++) {
  397. if (!strcmp(sample_res, sinfo[i].sample_res))
  398. goto found;
  399. }
  400. pr_err("Unsupported PCM format\n");
  401. return -EINVAL;
  402. found:
  403. if (!ch_num) {
  404. pr_err("Bad number of channels\n");
  405. return -EINVAL;
  406. }
  407. if (cfg->subbuffer_size != ch_num * sinfo[i].bytes) {
  408. pr_err("Audio resolution doesn't fit subbuffer size\n");
  409. return -EINVAL;
  410. }
  411. pcm_hw->info = MOST_PCM_INFO;
  412. pcm_hw->rates = SNDRV_PCM_RATE_48000;
  413. pcm_hw->rate_min = 48000;
  414. pcm_hw->rate_max = 48000;
  415. pcm_hw->buffer_bytes_max = cfg->num_buffers * cfg->buffer_size;
  416. pcm_hw->period_bytes_min = cfg->buffer_size;
  417. pcm_hw->period_bytes_max = cfg->buffer_size;
  418. pcm_hw->periods_min = 1;
  419. pcm_hw->periods_max = cfg->num_buffers;
  420. pcm_hw->channels_min = ch_num;
  421. pcm_hw->channels_max = ch_num;
  422. pcm_hw->formats = sinfo[i].formats;
  423. return 0;
  424. }
  425. static void release_adapter(struct sound_adapter *adpt)
  426. {
  427. struct channel *channel, *tmp;
  428. list_for_each_entry_safe(channel, tmp, &adpt->dev_list, list) {
  429. list_del(&channel->list);
  430. kfree(channel);
  431. }
  432. if (adpt->card)
  433. snd_card_free(adpt->card);
  434. list_del(&adpt->list);
  435. kfree(adpt);
  436. }
  437. /**
  438. * audio_probe_channel - probe function of the driver module
  439. * @iface: pointer to interface instance
  440. * @channel_id: channel index/ID
  441. * @cfg: pointer to actual channel configuration
  442. * @device_name: name of the device to be created in /dev
  443. * @arg_list: string that provides the desired audio resolution
  444. *
  445. * Creates sound card, pcm device, sets pcm ops and registers sound card.
  446. *
  447. * Returns 0 on success or error code otherwise.
  448. */
  449. static int audio_probe_channel(struct most_interface *iface, int channel_id,
  450. struct most_channel_config *cfg,
  451. char *device_name, char *arg_list)
  452. {
  453. struct channel *channel;
  454. struct sound_adapter *adpt;
  455. struct snd_pcm *pcm;
  456. int playback_count = 0;
  457. int capture_count = 0;
  458. int ret;
  459. int direction;
  460. u16 ch_num;
  461. char *sample_res;
  462. char arg_list_cpy[STRING_SIZE];
  463. if (cfg->data_type != MOST_CH_SYNC) {
  464. pr_err("Incompatible channel type\n");
  465. return -EINVAL;
  466. }
  467. strscpy(arg_list_cpy, arg_list, STRING_SIZE);
  468. ret = split_arg_list(arg_list_cpy, &ch_num, &sample_res);
  469. if (ret < 0)
  470. return ret;
  471. list_for_each_entry(adpt, &adpt_list, list) {
  472. if (adpt->iface != iface)
  473. continue;
  474. if (adpt->registered)
  475. return -ENOSPC;
  476. adpt->pcm_dev_idx++;
  477. goto skip_adpt_alloc;
  478. }
  479. adpt = kzalloc(sizeof(*adpt), GFP_KERNEL);
  480. if (!adpt)
  481. return -ENOMEM;
  482. adpt->iface = iface;
  483. INIT_LIST_HEAD(&adpt->dev_list);
  484. iface->priv = adpt;
  485. list_add_tail(&adpt->list, &adpt_list);
  486. ret = snd_card_new(iface->driver_dev, -1, "INIC", THIS_MODULE,
  487. sizeof(*channel), &adpt->card);
  488. if (ret < 0)
  489. goto err_free_adpt;
  490. snprintf(adpt->card->driver, sizeof(adpt->card->driver),
  491. "%s", DRIVER_NAME);
  492. snprintf(adpt->card->shortname, sizeof(adpt->card->shortname),
  493. "Microchip INIC");
  494. snprintf(adpt->card->longname, sizeof(adpt->card->longname),
  495. "%s at %s", adpt->card->shortname, iface->description);
  496. skip_adpt_alloc:
  497. if (get_channel(iface, channel_id)) {
  498. pr_err("channel (%s:%d) is already linked\n",
  499. iface->description, channel_id);
  500. return -EEXIST;
  501. }
  502. if (cfg->direction == MOST_CH_TX) {
  503. playback_count = 1;
  504. direction = SNDRV_PCM_STREAM_PLAYBACK;
  505. } else {
  506. capture_count = 1;
  507. direction = SNDRV_PCM_STREAM_CAPTURE;
  508. }
  509. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  510. if (!channel) {
  511. ret = -ENOMEM;
  512. goto err_free_adpt;
  513. }
  514. channel->card = adpt->card;
  515. channel->cfg = cfg;
  516. channel->iface = iface;
  517. channel->id = channel_id;
  518. init_waitqueue_head(&channel->playback_waitq);
  519. list_add_tail(&channel->list, &adpt->dev_list);
  520. ret = audio_set_hw_params(&channel->pcm_hardware, ch_num, sample_res,
  521. cfg);
  522. if (ret)
  523. goto err_free_adpt;
  524. ret = snd_pcm_new(adpt->card, device_name, adpt->pcm_dev_idx,
  525. playback_count, capture_count, &pcm);
  526. if (ret < 0)
  527. goto err_free_adpt;
  528. pcm->private_data = channel;
  529. strscpy(pcm->name, device_name, sizeof(pcm->name));
  530. snd_pcm_set_ops(pcm, direction, &pcm_ops);
  531. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
  532. return 0;
  533. err_free_adpt:
  534. release_adapter(adpt);
  535. return ret;
  536. }
  537. static int audio_create_sound_card(void)
  538. {
  539. int ret;
  540. struct sound_adapter *adpt;
  541. list_for_each_entry(adpt, &adpt_list, list) {
  542. if (!adpt->registered)
  543. goto adpt_alloc;
  544. }
  545. return -ENODEV;
  546. adpt_alloc:
  547. ret = snd_card_register(adpt->card);
  548. if (ret < 0) {
  549. release_adapter(adpt);
  550. return ret;
  551. }
  552. adpt->registered = true;
  553. return 0;
  554. }
  555. /**
  556. * audio_disconnect_channel - function to disconnect a channel
  557. * @iface: pointer to interface instance
  558. * @channel_id: channel index
  559. *
  560. * This frees allocated memory and removes the sound card from ALSA
  561. *
  562. * Returns 0 on success or error code otherwise.
  563. */
  564. static int audio_disconnect_channel(struct most_interface *iface,
  565. int channel_id)
  566. {
  567. struct channel *channel;
  568. struct sound_adapter *adpt = iface->priv;
  569. channel = get_channel(iface, channel_id);
  570. if (!channel)
  571. return -EINVAL;
  572. list_del(&channel->list);
  573. kfree(channel);
  574. if (list_empty(&adpt->dev_list))
  575. release_adapter(adpt);
  576. return 0;
  577. }
  578. /**
  579. * audio_rx_completion - completion handler for rx channels
  580. * @mbo: pointer to buffer object that has completed
  581. *
  582. * This searches for the channel this MBO belongs to and copy the data from MBO
  583. * to ring buffer
  584. *
  585. * Returns 0 on success or error code otherwise.
  586. */
  587. static int audio_rx_completion(struct mbo *mbo)
  588. {
  589. struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
  590. bool period_elapsed = false;
  591. if (!channel)
  592. return -EINVAL;
  593. if (channel->is_stream_running)
  594. period_elapsed = copy_data(channel, mbo);
  595. most_put_mbo(mbo);
  596. if (period_elapsed)
  597. snd_pcm_period_elapsed(channel->substream);
  598. return 0;
  599. }
  600. /**
  601. * audio_tx_completion - completion handler for tx channels
  602. * @iface: pointer to interface instance
  603. * @channel_id: channel index/ID
  604. *
  605. * This searches the channel that belongs to this combination of interface
  606. * pointer and channel ID and wakes a process sitting in the wait queue of
  607. * this channel.
  608. *
  609. * Returns 0 on success or error code otherwise.
  610. */
  611. static int audio_tx_completion(struct most_interface *iface, int channel_id)
  612. {
  613. struct channel *channel = get_channel(iface, channel_id);
  614. if (!channel)
  615. return -EINVAL;
  616. wake_up_interruptible(&channel->playback_waitq);
  617. return 0;
  618. }
  619. /*
  620. * Initialization of the struct most_component
  621. */
  622. static struct most_component comp = {
  623. .mod = THIS_MODULE,
  624. .name = DRIVER_NAME,
  625. .probe_channel = audio_probe_channel,
  626. .disconnect_channel = audio_disconnect_channel,
  627. .rx_completion = audio_rx_completion,
  628. .tx_completion = audio_tx_completion,
  629. .cfg_complete = audio_create_sound_card,
  630. };
  631. static int __init audio_init(void)
  632. {
  633. int ret;
  634. INIT_LIST_HEAD(&adpt_list);
  635. ret = most_register_component(&comp);
  636. if (ret) {
  637. pr_err("Failed to register %s\n", comp.name);
  638. return ret;
  639. }
  640. ret = most_register_configfs_subsys(&comp);
  641. if (ret) {
  642. pr_err("Failed to register %s configfs subsys\n", comp.name);
  643. most_deregister_component(&comp);
  644. }
  645. return ret;
  646. }
  647. static void __exit audio_exit(void)
  648. {
  649. most_deregister_configfs_subsys(&comp);
  650. most_deregister_component(&comp);
  651. }
  652. module_init(audio_init);
  653. module_exit(audio_exit);
  654. MODULE_LICENSE("GPL");
  655. MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
  656. MODULE_DESCRIPTION("Sound Component Module for Mostcore");