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