GNU Linux-libre 4.9.292-gnu1
[releases.git] / drivers / staging / most / aim-sound / sound.c
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
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
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
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
115 static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes)
116 {
117         memcpy(most, alsa, bytes);
118 }
119
120 static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes)
121 {
122         swap_copy16(most, alsa, bytes);
123 }
124
125 static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes)
126 {
127         swap_copy24(most, alsa, bytes);
128 }
129
130 static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes)
131 {
132         swap_copy32(most, alsa, bytes);
133 }
134
135 static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes)
136 {
137         memcpy(alsa, most, bytes);
138 }
139
140 static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes)
141 {
142         swap_copy16(alsa, most, bytes);
143 }
144
145 static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes)
146 {
147         swap_copy24(alsa, most, bytes);
148 }
149
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  */
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  */
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  */
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
240                 wait_event_interruptible(
241                         channel->playback_waitq,
242                         kthread_should_stop() ||
243                         (channel->is_stream_running &&
244                          (mbo = most_get_mbo(channel->iface, channel->id,
245                                              &audio_aim))));
246                 if (!mbo)
247                         continue;
248
249                 if (channel->is_stream_running)
250                         period_elapsed = copy_data(channel, mbo);
251                 else
252                         memset(mbo->virt_address, 0, mbo->buffer_length);
253
254                 most_submit_mbo(mbo);
255                 if (period_elapsed)
256                         snd_pcm_period_elapsed(channel->substream);
257         }
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  */
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
277         channel->substream = substream;
278
279         if (cfg->direction == MOST_CH_TX) {
280                 channel->playback_task = kthread_run(playback_thread, channel,
281                                                      "most_audio_playback");
282                 if (IS_ERR(channel->playback_task)) {
283                         pr_err("Couldn't start thread\n");
284                         return PTR_ERR(channel->playback_task);
285                 }
286         }
287
288         if (most_start_channel(channel->iface, channel->id, &audio_aim)) {
289                 pr_err("most_start_channel() failed!\n");
290                 if (cfg->direction == MOST_CH_TX)
291                         kthread_stop(channel->playback_task);
292                 return -EBUSY;
293         }
294
295         runtime->hw = channel->pcm_hardware;
296         return 0;
297 }
298
299 /**
300  * pcm_close - implements close callback function for PCM middle layer
301  * @substream: sub-stream pointer
302  *
303  * Obviously, this is called when a PCM substream is closed. Any private
304  * instance for a PCM substream allocated in the open callback will be
305  * released here.
306  *
307  * Returns 0 on success or error code otherwise.
308  */
309 static int pcm_close(struct snd_pcm_substream *substream)
310 {
311         struct channel *channel = substream->private_data;
312
313         if (channel->cfg->direction == MOST_CH_TX)
314                 kthread_stop(channel->playback_task);
315         most_stop_channel(channel->iface, channel->id, &audio_aim);
316
317         return 0;
318 }
319
320 /**
321  * pcm_hw_params - implements hw_params callback function for PCM middle layer
322  * @substream: sub-stream pointer
323  * @hw_params: contains the hardware parameters set by the application
324  *
325  * This is called when the hardware parameters is set by the application, that
326  * is, once when the buffer size, the period size, the format, etc. are defined
327  * for the PCM substream. Many hardware setups should be done is this callback,
328  * including the allocation of buffers.
329  *
330  * Returns 0 on success or error code otherwise.
331  */
332 static int pcm_hw_params(struct snd_pcm_substream *substream,
333                          struct snd_pcm_hw_params *hw_params)
334 {
335         struct channel *channel = substream->private_data;
336
337         if ((params_channels(hw_params) > channel->pcm_hardware.channels_max) ||
338             (params_channels(hw_params) < channel->pcm_hardware.channels_min)) {
339                 pr_err("Requested number of channels not supported.\n");
340                 return -EINVAL;
341         }
342         return snd_pcm_lib_alloc_vmalloc_buffer(substream,
343                                                 params_buffer_bytes(hw_params));
344 }
345
346 /**
347  * pcm_hw_free - implements hw_free callback function for PCM middle layer
348  * @substream: substream pointer
349  *
350  * This is called to release the resources allocated via hw_params.
351  * This function will be always called before the close callback is called.
352  *
353  * Returns 0 on success or error code otherwise.
354  */
355 static int pcm_hw_free(struct snd_pcm_substream *substream)
356 {
357         return snd_pcm_lib_free_vmalloc_buffer(substream);
358 }
359
360 /**
361  * pcm_prepare - implements prepare callback function for PCM middle layer
362  * @substream: substream pointer
363  *
364  * This callback is called when the PCM is "prepared". Format rate, sample rate,
365  * etc., can be set here. This callback can be called many times at each setup.
366  *
367  * Returns 0 on success or error code otherwise.
368  */
369 static int pcm_prepare(struct snd_pcm_substream *substream)
370 {
371         struct channel *channel = substream->private_data;
372         struct snd_pcm_runtime *runtime = substream->runtime;
373         struct most_channel_config *cfg = channel->cfg;
374         int width = snd_pcm_format_physical_width(runtime->format);
375
376         channel->copy_fn = NULL;
377
378         if (cfg->direction == MOST_CH_TX) {
379                 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
380                         channel->copy_fn = alsa_to_most_memcpy;
381                 else if (width == 16)
382                         channel->copy_fn = alsa_to_most_copy16;
383                 else if (width == 24)
384                         channel->copy_fn = alsa_to_most_copy24;
385                 else if (width == 32)
386                         channel->copy_fn = alsa_to_most_copy32;
387         } else {
388                 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
389                         channel->copy_fn = most_to_alsa_memcpy;
390                 else if (width == 16)
391                         channel->copy_fn = most_to_alsa_copy16;
392                 else if (width == 24)
393                         channel->copy_fn = most_to_alsa_copy24;
394                 else if (width == 32)
395                         channel->copy_fn = most_to_alsa_copy32;
396         }
397
398         if (!channel->copy_fn) {
399                 pr_err("unsupported format\n");
400                 return -EINVAL;
401         }
402
403         channel->period_pos = 0;
404         channel->buffer_pos = 0;
405
406         return 0;
407 }
408
409 /**
410  * pcm_trigger - implements trigger callback function for PCM middle layer
411  * @substream: substream pointer
412  * @cmd: action to perform
413  *
414  * This is called when the PCM is started, stopped or paused. The action will be
415  * specified in the second argument, SNDRV_PCM_TRIGGER_XXX
416  *
417  * Returns 0 on success or error code otherwise.
418  */
419 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
420 {
421         struct channel *channel = substream->private_data;
422
423         switch (cmd) {
424         case SNDRV_PCM_TRIGGER_START:
425                 channel->is_stream_running = true;
426                 wake_up_interruptible(&channel->playback_waitq);
427                 return 0;
428
429         case SNDRV_PCM_TRIGGER_STOP:
430                 channel->is_stream_running = false;
431                 return 0;
432
433         default:
434                 pr_info("pcm_trigger(), invalid\n");
435                 return -EINVAL;
436         }
437         return 0;
438 }
439
440 /**
441  * pcm_pointer - implements pointer callback function for PCM middle layer
442  * @substream: substream pointer
443  *
444  * This callback is called when the PCM middle layer inquires the current
445  * hardware position on the buffer. The position must be returned in frames,
446  * ranging from 0 to buffer_size-1.
447  */
448 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
449 {
450         struct channel *channel = substream->private_data;
451
452         return channel->buffer_pos;
453 }
454
455 /**
456  * Initialization of struct snd_pcm_ops
457  */
458 static const struct snd_pcm_ops pcm_ops = {
459         .open       = pcm_open,
460         .close      = pcm_close,
461         .ioctl      = snd_pcm_lib_ioctl,
462         .hw_params  = pcm_hw_params,
463         .hw_free    = pcm_hw_free,
464         .prepare    = pcm_prepare,
465         .trigger    = pcm_trigger,
466         .pointer    = pcm_pointer,
467         .page       = snd_pcm_lib_get_vmalloc_page,
468         .mmap       = snd_pcm_lib_mmap_vmalloc,
469 };
470
471 static int split_arg_list(char *buf, char **card_name, char **pcm_format)
472 {
473         *card_name = strsep(&buf, ".");
474         if (!*card_name)
475                 return -EIO;
476         *pcm_format = strsep(&buf, ".\n");
477         if (!*pcm_format)
478                 return -EIO;
479         return 0;
480 }
481
482 static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw,
483                                char *pcm_format,
484                                struct most_channel_config *cfg)
485 {
486         pcm_hw->info = MOST_PCM_INFO;
487         pcm_hw->rates = SNDRV_PCM_RATE_48000;
488         pcm_hw->rate_min = 48000;
489         pcm_hw->rate_max = 48000;
490         pcm_hw->buffer_bytes_max = cfg->num_buffers * cfg->buffer_size;
491         pcm_hw->period_bytes_min = cfg->buffer_size;
492         pcm_hw->period_bytes_max = cfg->buffer_size;
493         pcm_hw->periods_min = 1;
494         pcm_hw->periods_max = cfg->num_buffers;
495
496         if (!strcmp(pcm_format, "1x8")) {
497                 if (cfg->subbuffer_size != 1)
498                         goto error;
499                 pr_info("PCM format is 8-bit mono\n");
500                 pcm_hw->channels_min = 1;
501                 pcm_hw->channels_max = 1;
502                 pcm_hw->formats = SNDRV_PCM_FMTBIT_S8;
503         } else if (!strcmp(pcm_format, "2x16")) {
504                 if (cfg->subbuffer_size != 4)
505                         goto error;
506                 pr_info("PCM format is 16-bit stereo\n");
507                 pcm_hw->channels_min = 2;
508                 pcm_hw->channels_max = 2;
509                 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
510                                   SNDRV_PCM_FMTBIT_S16_BE;
511         } else if (!strcmp(pcm_format, "2x24")) {
512                 if (cfg->subbuffer_size != 6)
513                         goto error;
514                 pr_info("PCM format is 24-bit stereo\n");
515                 pcm_hw->channels_min = 2;
516                 pcm_hw->channels_max = 2;
517                 pcm_hw->formats = SNDRV_PCM_FMTBIT_S24_3LE |
518                                   SNDRV_PCM_FMTBIT_S24_3BE;
519         } else if (!strcmp(pcm_format, "2x32")) {
520                 if (cfg->subbuffer_size != 8)
521                         goto error;
522                 pr_info("PCM format is 32-bit stereo\n");
523                 pcm_hw->channels_min = 2;
524                 pcm_hw->channels_max = 2;
525                 pcm_hw->formats = SNDRV_PCM_FMTBIT_S32_LE |
526                                   SNDRV_PCM_FMTBIT_S32_BE;
527         } else if (!strcmp(pcm_format, "6x16")) {
528                 if (cfg->subbuffer_size != 12)
529                         goto error;
530                 pr_info("PCM format is 16-bit 5.1 multi channel\n");
531                 pcm_hw->channels_min = 6;
532                 pcm_hw->channels_max = 6;
533                 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
534                                   SNDRV_PCM_FMTBIT_S16_BE;
535         } else {
536                 pr_err("PCM format %s not supported\n", pcm_format);
537                 return -EIO;
538         }
539         return 0;
540 error:
541         pr_err("Audio resolution doesn't fit subbuffer size\n");
542         return -EINVAL;
543 }
544
545 /**
546  * audio_probe_channel - probe function of the driver module
547  * @iface: pointer to interface instance
548  * @channel_id: channel index/ID
549  * @cfg: pointer to actual channel configuration
550  * @parent: pointer to kobject (needed for sysfs hook-up)
551  * @arg_list: string that provides the name of the device to be created in /dev
552  *            plus the desired audio resolution
553  *
554  * Creates sound card, pcm device, sets pcm ops and registers sound card.
555  *
556  * Returns 0 on success or error code otherwise.
557  */
558 static int audio_probe_channel(struct most_interface *iface, int channel_id,
559                                struct most_channel_config *cfg,
560                                struct kobject *parent, char *arg_list)
561 {
562         struct channel *channel;
563         struct snd_card *card;
564         struct snd_pcm *pcm;
565         int playback_count = 0;
566         int capture_count = 0;
567         int ret;
568         int direction;
569         char *card_name;
570         char *pcm_format;
571
572         if (!iface)
573                 return -EINVAL;
574
575         if (cfg->data_type != MOST_CH_SYNC) {
576                 pr_err("Incompatible channel type\n");
577                 return -EINVAL;
578         }
579
580         if (get_channel(iface, channel_id)) {
581                 pr_err("channel (%s:%d) is already linked\n",
582                        iface->description, channel_id);
583                 return -EINVAL;
584         }
585
586         if (cfg->direction == MOST_CH_TX) {
587                 playback_count = 1;
588                 direction = SNDRV_PCM_STREAM_PLAYBACK;
589         } else {
590                 capture_count = 1;
591                 direction = SNDRV_PCM_STREAM_CAPTURE;
592         }
593
594         ret = split_arg_list(arg_list, &card_name, &pcm_format);
595         if (ret < 0) {
596                 pr_info("PCM format missing\n");
597                 return ret;
598         }
599
600         ret = snd_card_new(NULL, -1, card_name, THIS_MODULE,
601                            sizeof(*channel), &card);
602         if (ret < 0)
603                 return ret;
604
605         channel = card->private_data;
606         channel->card = card;
607         channel->cfg = cfg;
608         channel->iface = iface;
609         channel->id = channel_id;
610         init_waitqueue_head(&channel->playback_waitq);
611
612         ret = audio_set_hw_params(&channel->pcm_hardware, pcm_format, cfg);
613         if (ret)
614                 goto err_free_card;
615
616         snprintf(card->driver, sizeof(card->driver), "%s", DRIVER_NAME);
617         snprintf(card->shortname, sizeof(card->shortname), "Microchip MOST:%d",
618                  card->number);
619         snprintf(card->longname, sizeof(card->longname), "%s at %s, ch %d",
620                  card->shortname, iface->description, channel_id);
621
622         ret = snd_pcm_new(card, card_name, 0, playback_count,
623                           capture_count, &pcm);
624         if (ret < 0)
625                 goto err_free_card;
626
627         pcm->private_data = channel;
628
629         snd_pcm_set_ops(pcm, direction, &pcm_ops);
630
631         ret = snd_card_register(card);
632         if (ret < 0)
633                 goto err_free_card;
634
635         list_add_tail(&channel->list, &dev_list);
636
637         return 0;
638
639 err_free_card:
640         snd_card_free(card);
641         return ret;
642 }
643
644 /**
645  * audio_disconnect_channel - function to disconnect a channel
646  * @iface: pointer to interface instance
647  * @channel_id: channel index
648  *
649  * This frees allocated memory and removes the sound card from ALSA
650  *
651  * Returns 0 on success or error code otherwise.
652  */
653 static int audio_disconnect_channel(struct most_interface *iface,
654                                     int channel_id)
655 {
656         struct channel *channel;
657
658         channel = get_channel(iface, channel_id);
659         if (!channel) {
660                 pr_err("sound_disconnect_channel(), invalid channel %d\n",
661                        channel_id);
662                 return -EINVAL;
663         }
664
665         list_del(&channel->list);
666         snd_card_free(channel->card);
667
668         return 0;
669 }
670
671 /**
672  * audio_rx_completion - completion handler for rx channels
673  * @mbo: pointer to buffer object that has completed
674  *
675  * This searches for the channel this MBO belongs to and copy the data from MBO
676  * to ring buffer
677  *
678  * Returns 0 on success or error code otherwise.
679  */
680 static int audio_rx_completion(struct mbo *mbo)
681 {
682         struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
683         bool period_elapsed = false;
684
685         if (!channel) {
686                 pr_err("sound_rx_completion(), invalid channel %d\n",
687                        mbo->hdm_channel_id);
688                 return -EINVAL;
689         }
690
691         if (channel->is_stream_running)
692                 period_elapsed = copy_data(channel, mbo);
693
694         most_put_mbo(mbo);
695
696         if (period_elapsed)
697                 snd_pcm_period_elapsed(channel->substream);
698
699         return 0;
700 }
701
702 /**
703  * audio_tx_completion - completion handler for tx channels
704  * @iface: pointer to interface instance
705  * @channel_id: channel index/ID
706  *
707  * This searches the channel that belongs to this combination of interface
708  * pointer and channel ID and wakes a process sitting in the wait queue of
709  * this channel.
710  *
711  * Returns 0 on success or error code otherwise.
712  */
713 static int audio_tx_completion(struct most_interface *iface, int channel_id)
714 {
715         struct channel *channel = get_channel(iface, channel_id);
716
717         if (!channel) {
718                 pr_err("sound_tx_completion(), invalid channel %d\n",
719                        channel_id);
720                 return -EINVAL;
721         }
722
723         wake_up_interruptible(&channel->playback_waitq);
724
725         return 0;
726 }
727
728 /**
729  * Initialization of the struct most_aim
730  */
731 static struct most_aim audio_aim = {
732         .name = DRIVER_NAME,
733         .probe_channel = audio_probe_channel,
734         .disconnect_channel = audio_disconnect_channel,
735         .rx_completion = audio_rx_completion,
736         .tx_completion = audio_tx_completion,
737 };
738
739 static int __init audio_init(void)
740 {
741         pr_info("init()\n");
742
743         INIT_LIST_HEAD(&dev_list);
744
745         return most_register_aim(&audio_aim);
746 }
747
748 static void __exit audio_exit(void)
749 {
750         struct channel *channel, *tmp;
751
752         pr_info("exit()\n");
753
754         list_for_each_entry_safe(channel, tmp, &dev_list, list) {
755                 list_del(&channel->list);
756                 snd_card_free(channel->card);
757         }
758
759         most_deregister_aim(&audio_aim);
760 }
761
762 module_init(audio_init);
763 module_exit(audio_exit);
764
765 MODULE_LICENSE("GPL");
766 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
767 MODULE_DESCRIPTION("Audio Application Interface Module for MostCore");