1 /*
2 * sound.c - Audio Application Interface Module for Mostcore
3 *
4 * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * This file is licensed under GPLv2.
12 */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/module.h>
17 #include <linux/printk.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <linux/sched.h>
24 #include <linux/kthread.h>
25 #include <mostcore.h>
26
27 #define DRIVER_NAME "sound"
28
29 static struct list_head dev_list;
30 static struct most_aim audio_aim;
31
32 /**
33 * struct channel - private structure to keep channel specific data
34 * @substream: stores the substream structure
35 * @iface: interface for which the channel belongs to
36 * @cfg: channel configuration
37 * @card: registered sound card
38 * @list: list for private use
39 * @id: channel index
40 * @period_pos: current period position (ring buffer)
41 * @buffer_pos: current buffer position (ring buffer)
42 * @is_stream_running: identifies whether a stream is running or not
43 * @opened: set when the stream is opened
44 * @playback_task: playback thread
45 * @playback_waitq: waitq used by playback thread
46 */
47 struct channel {
48 struct snd_pcm_substream *substream;
49 struct snd_pcm_hardware pcm_hardware;
50 struct most_interface *iface;
51 struct most_channel_config *cfg;
52 struct snd_card *card;
53 struct list_head list;
54 int id;
55 unsigned int period_pos;
56 unsigned int buffer_pos;
57 bool is_stream_running;
58
59 struct task_struct *playback_task;
60 wait_queue_head_t playback_waitq;
61
62 void (*copy_fn)(void *alsa, void *most, unsigned int bytes);
63 };
64
65 #define MOST_PCM_INFO (SNDRV_PCM_INFO_MMAP | \
66 SNDRV_PCM_INFO_MMAP_VALID | \
67 SNDRV_PCM_INFO_BATCH | \
68 SNDRV_PCM_INFO_INTERLEAVED | \
69 SNDRV_PCM_INFO_BLOCK_TRANSFER)
70
71 #define swap16(val) ( \
72 (((u16)(val) << 8) & (u16)0xFF00) | \
73 (((u16)(val) >> 8) & (u16)0x00FF))
74
75 #define swap32(val) ( \
76 (((u32)(val) << 24) & (u32)0xFF000000) | \
77 (((u32)(val) << 8) & (u32)0x00FF0000) | \
78 (((u32)(val) >> 8) & (u32)0x0000FF00) | \
79 (((u32)(val) >> 24) & (u32)0x000000FF))
80
swap_copy16(u16 * dest,const u16 * source,unsigned int bytes)81 static void swap_copy16(u16 *dest, const u16 *source, unsigned int bytes)
82 {
83 unsigned int i = 0;
84
85 while (i < (bytes / 2)) {
86 dest[i] = swap16(source[i]);
87 i++;
88 }
89 }
90
swap_copy24(u8 * dest,const u8 * source,unsigned int bytes)91 static void swap_copy24(u8 *dest, const u8 *source, unsigned int bytes)
92 {
93 unsigned int i = 0;
94
95 if (bytes < 2)
96 return;
97 while (i < bytes - 2) {
98 dest[i] = source[i + 2];
99 dest[i + 1] = source[i + 1];
100 dest[i + 2] = source[i];
101 i += 3;
102 }
103 }
104
swap_copy32(u32 * dest,const u32 * source,unsigned int bytes)105 static void swap_copy32(u32 *dest, const u32 *source, unsigned int bytes)
106 {
107 unsigned int i = 0;
108
109 while (i < bytes / 4) {
110 dest[i] = swap32(source[i]);
111 i++;
112 }
113 }
114
alsa_to_most_memcpy(void * alsa,void * most,unsigned int bytes)115 static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes)
116 {
117 memcpy(most, alsa, bytes);
118 }
119
alsa_to_most_copy16(void * alsa,void * most,unsigned int bytes)120 static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes)
121 {
122 swap_copy16(most, alsa, bytes);
123 }
124
alsa_to_most_copy24(void * alsa,void * most,unsigned int bytes)125 static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes)
126 {
127 swap_copy24(most, alsa, bytes);
128 }
129
alsa_to_most_copy32(void * alsa,void * most,unsigned int bytes)130 static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes)
131 {
132 swap_copy32(most, alsa, bytes);
133 }
134
most_to_alsa_memcpy(void * alsa,void * most,unsigned int bytes)135 static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes)
136 {
137 memcpy(alsa, most, bytes);
138 }
139
most_to_alsa_copy16(void * alsa,void * most,unsigned int bytes)140 static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes)
141 {
142 swap_copy16(alsa, most, bytes);
143 }
144
most_to_alsa_copy24(void * alsa,void * most,unsigned int bytes)145 static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes)
146 {
147 swap_copy24(alsa, most, bytes);
148 }
149
most_to_alsa_copy32(void * alsa,void * most,unsigned int bytes)150 static void most_to_alsa_copy32(void *alsa, void *most, unsigned int bytes)
151 {
152 swap_copy32(alsa, most, bytes);
153 }
154
155 /**
156 * get_channel - get pointer to channel
157 * @iface: interface structure
158 * @channel_id: channel ID
159 *
160 * This traverses the channel list and returns the channel matching the
161 * ID and interface.
162 *
163 * Returns pointer to channel on success or NULL otherwise.
164 */
get_channel(struct most_interface * iface,int channel_id)165 static struct channel *get_channel(struct most_interface *iface,
166 int channel_id)
167 {
168 struct channel *channel, *tmp;
169
170 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
171 if ((channel->iface == iface) && (channel->id == channel_id))
172 return channel;
173 }
174
175 return NULL;
176 }
177
178 /**
179 * copy_data - implements data copying function
180 * @channel: channel
181 * @mbo: MBO from core
182 *
183 * Copy data from/to ring buffer to/from MBO and update the buffer position
184 */
copy_data(struct channel * channel,struct mbo * mbo)185 static bool copy_data(struct channel *channel, struct mbo *mbo)
186 {
187 struct snd_pcm_runtime *const runtime = channel->substream->runtime;
188 unsigned int const frame_bytes = channel->cfg->subbuffer_size;
189 unsigned int const buffer_size = runtime->buffer_size;
190 unsigned int frames;
191 unsigned int fr0;
192
193 if (channel->cfg->direction & MOST_CH_RX)
194 frames = mbo->processed_length / frame_bytes;
195 else
196 frames = mbo->buffer_length / frame_bytes;
197 fr0 = min(buffer_size - channel->buffer_pos, frames);
198
199 channel->copy_fn(runtime->dma_area + channel->buffer_pos * frame_bytes,
200 mbo->virt_address,
201 fr0 * frame_bytes);
202
203 if (frames > fr0) {
204 /* wrap around at end of ring buffer */
205 channel->copy_fn(runtime->dma_area,
206 mbo->virt_address + fr0 * frame_bytes,
207 (frames - fr0) * frame_bytes);
208 }
209
210 channel->buffer_pos += frames;
211 if (channel->buffer_pos >= buffer_size)
212 channel->buffer_pos -= buffer_size;
213 channel->period_pos += frames;
214 if (channel->period_pos >= runtime->period_size) {
215 channel->period_pos -= runtime->period_size;
216 return true;
217 }
218
219 return false;
220 }
221
222 /**
223 * playback_thread - function implements the playback thread
224 * @data: private data
225 *
226 * Thread which does the playback functionality in a loop. It waits for a free
227 * MBO from mostcore for a particular channel and copy the data from ring buffer
228 * to MBO. Submit the MBO back to mostcore, after copying the data.
229 *
230 * Returns 0 on success or error code otherwise.
231 */
playback_thread(void * data)232 static int playback_thread(void *data)
233 {
234 struct channel *const channel = data;
235
236 while (!kthread_should_stop()) {
237 struct mbo *mbo = NULL;
238 bool period_elapsed = false;
239 int ret;
240
241 wait_event_interruptible(
242 channel->playback_waitq,
243 kthread_should_stop() ||
244 (channel->is_stream_running &&
245 (mbo = most_get_mbo(channel->iface, channel->id,
246 &audio_aim))));
247 if (!mbo)
248 continue;
249
250 if (channel->is_stream_running)
251 period_elapsed = copy_data(channel, mbo);
252 else
253 memset(mbo->virt_address, 0, mbo->buffer_length);
254
255 ret = most_submit_mbo(mbo);
256 if (ret)
257 channel->is_stream_running = false;
258
259 if (period_elapsed)
260 snd_pcm_period_elapsed(channel->substream);
261 }
262
263 return 0;
264 }
265
266 /**
267 * pcm_open - implements open callback function for PCM middle layer
268 * @substream: pointer to ALSA PCM substream
269 *
270 * This is called when a PCM substream is opened. At least, the function should
271 * initialize the runtime->hw record.
272 *
273 * Returns 0 on success or error code otherwise.
274 */
pcm_open(struct snd_pcm_substream * substream)275 static int pcm_open(struct snd_pcm_substream *substream)
276 {
277 struct channel *channel = substream->private_data;
278 struct snd_pcm_runtime *runtime = substream->runtime;
279 struct most_channel_config *cfg = channel->cfg;
280
281 channel->substream = substream;
282
283 if (cfg->direction == MOST_CH_TX) {
284 channel->playback_task = kthread_run(playback_thread, channel,
285 "most_audio_playback");
286 if (IS_ERR(channel->playback_task)) {
287 pr_err("Couldn't start thread\n");
288 return PTR_ERR(channel->playback_task);
289 }
290 }
291
292 if (most_start_channel(channel->iface, channel->id, &audio_aim)) {
293 pr_err("most_start_channel() failed!\n");
294 if (cfg->direction == MOST_CH_TX)
295 kthread_stop(channel->playback_task);
296 return -EBUSY;
297 }
298
299 runtime->hw = channel->pcm_hardware;
300 return 0;
301 }
302
303 /**
304 * pcm_close - implements close callback function for PCM middle layer
305 * @substream: sub-stream pointer
306 *
307 * Obviously, this is called when a PCM substream is closed. Any private
308 * instance for a PCM substream allocated in the open callback will be
309 * released here.
310 *
311 * Returns 0 on success or error code otherwise.
312 */
pcm_close(struct snd_pcm_substream * substream)313 static int pcm_close(struct snd_pcm_substream *substream)
314 {
315 struct channel *channel = substream->private_data;
316
317 if (channel->cfg->direction == MOST_CH_TX)
318 kthread_stop(channel->playback_task);
319 most_stop_channel(channel->iface, channel->id, &audio_aim);
320
321 return 0;
322 }
323
324 /**
325 * pcm_hw_params - implements hw_params callback function for PCM middle layer
326 * @substream: sub-stream pointer
327 * @hw_params: contains the hardware parameters set by the application
328 *
329 * This is called when the hardware parameters is set by the application, that
330 * is, once when the buffer size, the period size, the format, etc. are defined
331 * for the PCM substream. Many hardware setups should be done is this callback,
332 * including the allocation of buffers.
333 *
334 * Returns 0 on success or error code otherwise.
335 */
pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)336 static int pcm_hw_params(struct snd_pcm_substream *substream,
337 struct snd_pcm_hw_params *hw_params)
338 {
339 struct channel *channel = substream->private_data;
340
341 if ((params_channels(hw_params) > channel->pcm_hardware.channels_max) ||
342 (params_channels(hw_params) < channel->pcm_hardware.channels_min)) {
343 pr_err("Requested number of channels not supported.\n");
344 return -EINVAL;
345 }
346 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
347 params_buffer_bytes(hw_params));
348 }
349
350 /**
351 * pcm_hw_free - implements hw_free callback function for PCM middle layer
352 * @substream: substream pointer
353 *
354 * This is called to release the resources allocated via hw_params.
355 * This function will be always called before the close callback is called.
356 *
357 * Returns 0 on success or error code otherwise.
358 */
pcm_hw_free(struct snd_pcm_substream * substream)359 static int pcm_hw_free(struct snd_pcm_substream *substream)
360 {
361 return snd_pcm_lib_free_vmalloc_buffer(substream);
362 }
363
364 /**
365 * pcm_prepare - implements prepare callback function for PCM middle layer
366 * @substream: substream pointer
367 *
368 * This callback is called when the PCM is "prepared". Format rate, sample rate,
369 * etc., can be set here. This callback can be called many times at each setup.
370 *
371 * Returns 0 on success or error code otherwise.
372 */
pcm_prepare(struct snd_pcm_substream * substream)373 static int pcm_prepare(struct snd_pcm_substream *substream)
374 {
375 struct channel *channel = substream->private_data;
376 struct snd_pcm_runtime *runtime = substream->runtime;
377 struct most_channel_config *cfg = channel->cfg;
378 int width = snd_pcm_format_physical_width(runtime->format);
379
380 channel->copy_fn = NULL;
381
382 if (cfg->direction == MOST_CH_TX) {
383 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
384 channel->copy_fn = alsa_to_most_memcpy;
385 else if (width == 16)
386 channel->copy_fn = alsa_to_most_copy16;
387 else if (width == 24)
388 channel->copy_fn = alsa_to_most_copy24;
389 else if (width == 32)
390 channel->copy_fn = alsa_to_most_copy32;
391 } else {
392 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
393 channel->copy_fn = most_to_alsa_memcpy;
394 else if (width == 16)
395 channel->copy_fn = most_to_alsa_copy16;
396 else if (width == 24)
397 channel->copy_fn = most_to_alsa_copy24;
398 else if (width == 32)
399 channel->copy_fn = most_to_alsa_copy32;
400 }
401
402 if (!channel->copy_fn) {
403 pr_err("unsupported format\n");
404 return -EINVAL;
405 }
406
407 channel->period_pos = 0;
408 channel->buffer_pos = 0;
409
410 return 0;
411 }
412
413 /**
414 * pcm_trigger - implements trigger callback function for PCM middle layer
415 * @substream: substream pointer
416 * @cmd: action to perform
417 *
418 * This is called when the PCM is started, stopped or paused. The action will be
419 * specified in the second argument, SNDRV_PCM_TRIGGER_XXX
420 *
421 * Returns 0 on success or error code otherwise.
422 */
pcm_trigger(struct snd_pcm_substream * substream,int cmd)423 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
424 {
425 struct channel *channel = substream->private_data;
426
427 switch (cmd) {
428 case SNDRV_PCM_TRIGGER_START:
429 channel->is_stream_running = true;
430 wake_up_interruptible(&channel->playback_waitq);
431 return 0;
432
433 case SNDRV_PCM_TRIGGER_STOP:
434 channel->is_stream_running = false;
435 return 0;
436
437 default:
438 pr_info("pcm_trigger(), invalid\n");
439 return -EINVAL;
440 }
441 return 0;
442 }
443
444 /**
445 * pcm_pointer - implements pointer callback function for PCM middle layer
446 * @substream: substream pointer
447 *
448 * This callback is called when the PCM middle layer inquires the current
449 * hardware position on the buffer. The position must be returned in frames,
450 * ranging from 0 to buffer_size-1.
451 */
pcm_pointer(struct snd_pcm_substream * substream)452 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
453 {
454 struct channel *channel = substream->private_data;
455
456 return channel->buffer_pos;
457 }
458
459 /**
460 * Initialization of struct snd_pcm_ops
461 */
462 static struct snd_pcm_ops pcm_ops = {
463 .open = pcm_open,
464 .close = pcm_close,
465 .ioctl = snd_pcm_lib_ioctl,
466 .hw_params = pcm_hw_params,
467 .hw_free = pcm_hw_free,
468 .prepare = pcm_prepare,
469 .trigger = pcm_trigger,
470 .pointer = pcm_pointer,
471 .page = snd_pcm_lib_get_vmalloc_page,
472 .mmap = snd_pcm_lib_mmap_vmalloc,
473 };
474
split_arg_list(char * buf,char ** card_name,char ** pcm_format)475 static int split_arg_list(char *buf, char **card_name, char **pcm_format)
476 {
477 *card_name = strsep(&buf, ".");
478 if (!*card_name)
479 return -EIO;
480 *pcm_format = strsep(&buf, ".\n");
481 if (!*pcm_format)
482 return -EIO;
483 return 0;
484 }
485
audio_set_hw_params(struct snd_pcm_hardware * pcm_hw,char * pcm_format,struct most_channel_config * cfg)486 static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw,
487 char *pcm_format,
488 struct most_channel_config *cfg)
489 {
490 pcm_hw->info = MOST_PCM_INFO;
491 pcm_hw->rates = SNDRV_PCM_RATE_48000;
492 pcm_hw->rate_min = 48000;
493 pcm_hw->rate_max = 48000;
494 pcm_hw->buffer_bytes_max = cfg->num_buffers * cfg->buffer_size;
495 pcm_hw->period_bytes_min = cfg->buffer_size;
496 pcm_hw->period_bytes_max = cfg->buffer_size;
497 pcm_hw->periods_min = 1;
498 pcm_hw->periods_max = cfg->num_buffers;
499
500 if (!strcmp(pcm_format, "1x8")) {
501 if (cfg->subbuffer_size != 1)
502 goto error;
503 pr_info("PCM format is 8-bit mono\n");
504 pcm_hw->channels_min = 1;
505 pcm_hw->channels_max = 1;
506 pcm_hw->formats = SNDRV_PCM_FMTBIT_S8;
507 } else if (!strcmp(pcm_format, "2x16")) {
508 if (cfg->subbuffer_size != 4)
509 goto error;
510 pr_info("PCM format is 16-bit stereo\n");
511 pcm_hw->channels_min = 2;
512 pcm_hw->channels_max = 2;
513 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
514 SNDRV_PCM_FMTBIT_S16_BE;
515 } else if (!strcmp(pcm_format, "2x24")) {
516 if (cfg->subbuffer_size != 6)
517 goto error;
518 pr_info("PCM format is 24-bit stereo\n");
519 pcm_hw->channels_min = 2;
520 pcm_hw->channels_max = 2;
521 pcm_hw->formats = SNDRV_PCM_FMTBIT_S24_3LE |
522 SNDRV_PCM_FMTBIT_S24_3BE;
523 } else if (!strcmp(pcm_format, "2x32")) {
524 if (cfg->subbuffer_size != 8)
525 goto error;
526 pr_info("PCM format is 32-bit stereo\n");
527 pcm_hw->channels_min = 2;
528 pcm_hw->channels_max = 2;
529 pcm_hw->formats = SNDRV_PCM_FMTBIT_S32_LE |
530 SNDRV_PCM_FMTBIT_S32_BE;
531 } else if (!strcmp(pcm_format, "6x16")) {
532 if (cfg->subbuffer_size != 12)
533 goto error;
534 pr_info("PCM format is 16-bit 5.1 multi channel\n");
535 pcm_hw->channels_min = 6;
536 pcm_hw->channels_max = 6;
537 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
538 SNDRV_PCM_FMTBIT_S16_BE;
539 } else {
540 pr_err("PCM format %s not supported\n", pcm_format);
541 return -EIO;
542 }
543 return 0;
544 error:
545 pr_err("Audio resolution doesn't fit subbuffer size\n");
546 return -EINVAL;
547 }
548
549 /**
550 * audio_probe_channel - probe function of the driver module
551 * @iface: pointer to interface instance
552 * @channel_id: channel index/ID
553 * @cfg: pointer to actual channel configuration
554 * @parent: pointer to kobject (needed for sysfs hook-up)
555 * @arg_list: string that provides the name of the device to be created in /dev
556 * plus the desired audio resolution
557 *
558 * Creates sound card, pcm device, sets pcm ops and registers sound card.
559 *
560 * Returns 0 on success or error code otherwise.
561 */
audio_probe_channel(struct most_interface * iface,int channel_id,struct most_channel_config * cfg,struct kobject * parent,char * arg_list)562 static int audio_probe_channel(struct most_interface *iface, int channel_id,
563 struct most_channel_config *cfg,
564 struct kobject *parent, char *arg_list)
565 {
566 struct channel *channel;
567 struct snd_card *card;
568 struct snd_pcm *pcm;
569 int playback_count = 0;
570 int capture_count = 0;
571 int ret;
572 int direction;
573 char *card_name;
574 char *pcm_format;
575
576 if (!iface)
577 return -EINVAL;
578
579 if (cfg->data_type != MOST_CH_SYNC) {
580 pr_err("Incompatible channel type\n");
581 return -EINVAL;
582 }
583
584 if (get_channel(iface, channel_id)) {
585 pr_err("channel (%s:%d) is already linked\n",
586 iface->description, channel_id);
587 return -EINVAL;
588 }
589
590 if (cfg->direction == MOST_CH_TX) {
591 playback_count = 1;
592 direction = SNDRV_PCM_STREAM_PLAYBACK;
593 } else {
594 capture_count = 1;
595 direction = SNDRV_PCM_STREAM_CAPTURE;
596 }
597
598 ret = split_arg_list(arg_list, &card_name, &pcm_format);
599 if (ret < 0) {
600 pr_info("PCM format missing\n");
601 return ret;
602 }
603
604 ret = snd_card_new(NULL, -1, card_name, THIS_MODULE,
605 sizeof(*channel), &card);
606 if (ret < 0)
607 return ret;
608
609 channel = card->private_data;
610 channel->card = card;
611 channel->cfg = cfg;
612 channel->iface = iface;
613 channel->id = channel_id;
614 init_waitqueue_head(&channel->playback_waitq);
615
616 if (audio_set_hw_params(&channel->pcm_hardware, pcm_format, cfg))
617 goto err_free_card;
618
619 snprintf(card->driver, sizeof(card->driver), "%s", DRIVER_NAME);
620 snprintf(card->shortname, sizeof(card->shortname), "Microchip MOST:%d",
621 card->number);
622 snprintf(card->longname, sizeof(card->longname), "%s at %s, ch %d",
623 card->shortname, iface->description, channel_id);
624
625 ret = snd_pcm_new(card, card_name, 0, playback_count,
626 capture_count, &pcm);
627 if (ret < 0)
628 goto err_free_card;
629
630 pcm->private_data = channel;
631
632 snd_pcm_set_ops(pcm, direction, &pcm_ops);
633
634 ret = snd_card_register(card);
635 if (ret < 0)
636 goto err_free_card;
637
638 list_add_tail(&channel->list, &dev_list);
639
640 return 0;
641
642 err_free_card:
643 snd_card_free(card);
644 return ret;
645 }
646
647 /**
648 * audio_disconnect_channel - function to disconnect a channel
649 * @iface: pointer to interface instance
650 * @channel_id: channel index
651 *
652 * This frees allocated memory and removes the sound card from ALSA
653 *
654 * Returns 0 on success or error code otherwise.
655 */
audio_disconnect_channel(struct most_interface * iface,int channel_id)656 static int audio_disconnect_channel(struct most_interface *iface,
657 int channel_id)
658 {
659 struct channel *channel;
660
661 channel = get_channel(iface, channel_id);
662 if (!channel) {
663 pr_err("sound_disconnect_channel(), invalid channel %d\n",
664 channel_id);
665 return -EINVAL;
666 }
667
668 list_del(&channel->list);
669 snd_card_free(channel->card);
670
671 return 0;
672 }
673
674 /**
675 * audio_rx_completion - completion handler for rx channels
676 * @mbo: pointer to buffer object that has completed
677 *
678 * This searches for the channel this MBO belongs to and copy the data from MBO
679 * to ring buffer
680 *
681 * Returns 0 on success or error code otherwise.
682 */
audio_rx_completion(struct mbo * mbo)683 static int audio_rx_completion(struct mbo *mbo)
684 {
685 struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
686 bool period_elapsed = false;
687
688 if (!channel) {
689 pr_err("sound_rx_completion(), invalid channel %d\n",
690 mbo->hdm_channel_id);
691 return -EINVAL;
692 }
693
694 if (channel->is_stream_running)
695 period_elapsed = copy_data(channel, mbo);
696
697 most_put_mbo(mbo);
698
699 if (period_elapsed)
700 snd_pcm_period_elapsed(channel->substream);
701
702 return 0;
703 }
704
705 /**
706 * audio_tx_completion - completion handler for tx channels
707 * @iface: pointer to interface instance
708 * @channel_id: channel index/ID
709 *
710 * This searches the channel that belongs to this combination of interface
711 * pointer and channel ID and wakes a process sitting in the wait queue of
712 * this channel.
713 *
714 * Returns 0 on success or error code otherwise.
715 */
audio_tx_completion(struct most_interface * iface,int channel_id)716 static int audio_tx_completion(struct most_interface *iface, int channel_id)
717 {
718 struct channel *channel = get_channel(iface, channel_id);
719
720 if (!channel) {
721 pr_err("sound_tx_completion(), invalid channel %d\n",
722 channel_id);
723 return -EINVAL;
724 }
725
726 wake_up_interruptible(&channel->playback_waitq);
727
728 return 0;
729 }
730
731 /**
732 * Initialization of the struct most_aim
733 */
734 static struct most_aim audio_aim = {
735 .name = DRIVER_NAME,
736 .probe_channel = audio_probe_channel,
737 .disconnect_channel = audio_disconnect_channel,
738 .rx_completion = audio_rx_completion,
739 .tx_completion = audio_tx_completion,
740 };
741
audio_init(void)742 static int __init audio_init(void)
743 {
744 pr_info("init()\n");
745
746 INIT_LIST_HEAD(&dev_list);
747
748 return most_register_aim(&audio_aim);
749 }
750
audio_exit(void)751 static void __exit audio_exit(void)
752 {
753 struct channel *channel, *tmp;
754
755 pr_info("exit()\n");
756
757 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
758 list_del(&channel->list);
759 snd_card_free(channel->card);
760 }
761
762 most_deregister_aim(&audio_aim);
763 }
764
765 module_init(audio_init);
766 module_exit(audio_exit);
767
768 MODULE_LICENSE("GPL");
769 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
770 MODULE_DESCRIPTION("Audio Application Interface Module for MostCore");
771