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