2 * intel_sst_stream.c - Intel SST Driver for audio engine
4 * Copyright (C) 2008-10 Intel Corp
5 * Authors: Vinod Koul <vinod.koul@intel.com>
6 * Harsha Priya <priya.harsha@intel.com>
7 * Dharageswari R <dharageswari.r@intel.com>
8 * KP Jeeja <jeeja.kp@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 * This file contains the stream operations of SST driver
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/pci.h>
32 #include <linux/firmware.h>
33 #include <linux/sched.h>
34 #include <linux/delay.h>
35 #include "intel_sst_ioctl.h"
36 #include "intel_sst.h"
37 #include "intel_sst_fw_ipc.h"
38 #include "intel_sst_common.h"
41 * sst_check_device_type - Check the medfield device type
43 * @device: Device to be checked
44 * @num_ch: Number of channels queried
45 * @pcm_slot: slot to be enabled for this device
47 * This checks the deivce against the map and calculates pcm_slot value
49 int sst_check_device_type(u32 device, u32 num_chan, u32 *pcm_slot)
51 if (device >= MAX_NUM_STREAMS_MFLD) {
52 pr_debug("device type invalid %d\n", device);
55 if (sst_drv_ctx->streams[device].status == STREAM_UN_INIT) {
56 if (device == SND_SST_DEVICE_VIBRA && num_chan == 1)
58 else if (device == SND_SST_DEVICE_HAPTIC && num_chan == 1)
60 else if (device == SND_SST_DEVICE_IHF && num_chan == 1)
62 else if (device == SND_SST_DEVICE_IHF && num_chan == 2)
64 else if (device == SND_SST_DEVICE_HEADSET && num_chan == 1)
66 else if (device == SND_SST_DEVICE_HEADSET && num_chan == 2)
68 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 1)
70 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 2)
72 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 3)
74 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 4)
76 else if (device == SND_SST_DEVICE_CAPTURE && num_chan > 4)
79 pr_debug("No condition satisfied.. ret err\n");
83 pr_debug("this stream state is not uni-init, is %d\n",
84 sst_drv_ctx->streams[device].status);
87 pr_debug("returning slot %x\n", *pcm_slot);
91 * get_mrst_stream_id - gets a new stream id for use
93 * This functions searches the current streams and allocated an empty stream
94 * lock stream_lock required to be held before calling this
96 static unsigned int get_mrst_stream_id(void)
100 for (i = 1; i <= MAX_NUM_STREAMS_MRST; i++) {
101 if (sst_drv_ctx->streams[i].status == STREAM_UN_INIT)
104 pr_debug("Didn't find empty stream for mrst\n");
109 * sst_alloc_stream - Send msg for a new stream ID
111 * @params: stream params
112 * @stream_ops: operation of stream PB/capture
113 * @codec: codec for stream
114 * @device: device stream to be allocated for
116 * This function is called by any function which wants to start
117 * a new stream. This also check if a stream exists which is idle
118 * it initializes idle stream id to this request
120 int sst_alloc_stream(char *params, unsigned int stream_ops,
121 u8 codec, unsigned int device)
123 struct ipc_post *msg = NULL;
124 struct snd_sst_alloc_params alloc_param;
125 unsigned int pcm_slot = 0, num_ch;
127 struct snd_sst_stream_params *sparams;
128 struct stream_info *str_info;
130 pr_debug("SST DBG:entering sst_alloc_stream\n");
131 pr_debug("SST DBG:%d %d %d\n", stream_ops, codec, device);
134 sparams = (struct snd_sst_stream_params *)params;
135 num_ch = sparams->uc.pcm_params.num_chan;
136 /*check the device type*/
137 if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID) {
138 if (sst_check_device_type(device, num_ch, &pcm_slot))
140 mutex_lock(&sst_drv_ctx->stream_lock);
142 mutex_unlock(&sst_drv_ctx->stream_lock);
143 pr_debug("SST_DBG: slot %x\n", pcm_slot);
145 mutex_lock(&sst_drv_ctx->stream_lock);
146 str_id = get_mrst_stream_id();
147 mutex_unlock(&sst_drv_ctx->stream_lock);
151 /*allocate device type context*/
152 sst_init_stream(&sst_drv_ctx->streams[str_id], codec,
153 str_id, stream_ops, pcm_slot, device);
154 /* send msg to FW to allocate a stream */
155 if (sst_create_large_msg(&msg))
158 sst_fill_header(&msg->header, IPC_IA_ALLOC_STREAM, 1, str_id);
159 msg->header.part.data = sizeof(alloc_param) + sizeof(u32);
160 alloc_param.str_type.codec_type = codec;
161 alloc_param.str_type.str_type = SST_STREAM_TYPE_MUSIC;
162 alloc_param.str_type.operation = stream_ops;
163 alloc_param.str_type.protected_str = 0; /* non drm */
164 alloc_param.str_type.time_slots = pcm_slot;
165 alloc_param.str_type.result = alloc_param.str_type.reserved = 0;
166 memcpy(&alloc_param.stream_params, params,
167 sizeof(struct snd_sst_stream_params));
169 memcpy(msg->mailbox_data, &msg->header, sizeof(u32));
170 memcpy(msg->mailbox_data + sizeof(u32), &alloc_param,
171 sizeof(alloc_param));
172 str_info = &sst_drv_ctx->streams[str_id];
173 str_info->ctrl_blk.condition = false;
174 str_info->ctrl_blk.ret_code = 0;
175 str_info->ctrl_blk.on = true;
176 spin_lock(&sst_drv_ctx->list_spin_lock);
177 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
178 spin_unlock(&sst_drv_ctx->list_spin_lock);
179 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
180 pr_debug("SST DBG:alloc stream done\n");
186 * sst_alloc_stream_response - process alloc reply
188 * @str_id: stream id for which the stream has been allocated
189 * @resp the stream response from firware
191 * This function is called by firmware as a response to stream allcoation
194 int sst_alloc_stream_response(unsigned int str_id,
195 struct snd_sst_alloc_response *resp)
198 struct stream_info *str_info;
199 struct snd_sst_lib_download *lib_dnld;
201 pr_debug("SST DEBUG: stream number given = %d\n", str_id);
202 str_info = &sst_drv_ctx->streams[str_id];
203 if (resp->str_type.result == SST_LIB_ERR_LIB_DNLD_REQUIRED) {
204 lib_dnld = kzalloc(sizeof(*lib_dnld), GFP_KERNEL);
205 memcpy(lib_dnld, &resp->lib_dnld, sizeof(*lib_dnld));
208 if (str_info->ctrl_blk.on == true) {
209 str_info->ctrl_blk.on = false;
210 str_info->ctrl_blk.data = lib_dnld;
211 str_info->ctrl_blk.condition = true;
212 str_info->ctrl_blk.ret_code = resp->str_type.result;
213 pr_debug("SST DEBUG: sst_alloc_stream_response: waking up.\n");
214 wake_up(&sst_drv_ctx->wait_queue);
221 * sst_get_fw_info - Send msg to query for firmware configurations
222 * @info: out param that holds the firmare configurations
224 * This function is called when the firmware configurations are queiried for
226 int sst_get_fw_info(struct snd_sst_fw_info *info)
229 struct ipc_post *msg = NULL;
231 pr_debug("SST DBG:sst_get_fw_info called\n");
233 if (sst_create_short_msg(&msg)) {
234 pr_err("SST ERR: message creation failed\n");
238 sst_fill_header(&msg->header, IPC_IA_GET_FW_INFO, 0, 0);
239 sst_drv_ctx->fw_info_blk.condition = false;
240 sst_drv_ctx->fw_info_blk.ret_code = 0;
241 sst_drv_ctx->fw_info_blk.on = true;
242 sst_drv_ctx->fw_info_blk.data = info;
243 spin_lock(&sst_drv_ctx->list_spin_lock);
244 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
245 spin_unlock(&sst_drv_ctx->list_spin_lock);
246 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
247 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
248 &sst_drv_ctx->fw_info_blk, SST_BLOCK_TIMEOUT);
250 pr_err("SST ERR: error in fw_info = %d\n", retval);
258 * sst_pause_stream - Send msg for a pausing stream
261 * This function is called by any function which wants to pause
262 * an already running stream.
264 int sst_start_stream(int str_id)
267 struct ipc_post *msg = NULL;
268 struct stream_info *str_info;
270 pr_debug("sst_start_stream for %d\n", str_id);
271 retval = sst_validate_strid(str_id);
274 str_info = &sst_drv_ctx->streams[str_id];
275 if (str_info->status != STREAM_INIT)
277 if (sst_create_short_msg(&msg))
280 sst_fill_header(&msg->header, IPC_IA_START_STREAM, 0, str_id);
281 spin_lock(&sst_drv_ctx->list_spin_lock);
282 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
283 spin_unlock(&sst_drv_ctx->list_spin_lock);
284 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
289 * sst_pause_stream - Send msg for a pausing stream
292 * This function is called by any function which wants to pause
293 * an already running stream.
295 int sst_pause_stream(int str_id)
298 struct ipc_post *msg = NULL;
299 struct stream_info *str_info;
301 pr_debug("SST DBG:sst_pause_stream for %d\n", str_id);
302 retval = sst_validate_strid(str_id);
305 str_info = &sst_drv_ctx->streams[str_id];
306 if (str_info->status == STREAM_PAUSED)
308 if (str_info->status == STREAM_RUNNING ||
309 str_info->status == STREAM_INIT) {
310 if (str_info->prev == STREAM_UN_INIT)
312 if (str_info->ctrl_blk.on == true) {
313 pr_err("SST ERR: control path is in use\n");
316 if (sst_create_short_msg(&msg))
319 sst_fill_header(&msg->header, IPC_IA_PAUSE_STREAM, 0, str_id);
320 str_info->ctrl_blk.condition = false;
321 str_info->ctrl_blk.ret_code = 0;
322 str_info->ctrl_blk.on = true;
323 spin_lock(&sst_drv_ctx->list_spin_lock);
324 list_add_tail(&msg->node,
325 &sst_drv_ctx->ipc_dispatch_list);
326 spin_unlock(&sst_drv_ctx->list_spin_lock);
327 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
328 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
329 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
331 str_info->prev = str_info->status;
332 str_info->status = STREAM_PAUSED;
333 } else if (retval == SST_ERR_INVALID_STREAM_ID) {
335 mutex_lock(&sst_drv_ctx->stream_lock);
336 sst_clean_stream(str_info);
337 mutex_unlock(&sst_drv_ctx->stream_lock);
341 pr_err("SST ERR: BADQRC for stream\n");
348 * sst_resume_stream - Send msg for resuming stream
351 * This function is called by any function which wants to resume
352 * an already paused stream.
354 int sst_resume_stream(int str_id)
357 struct ipc_post *msg = NULL;
358 struct stream_info *str_info;
360 pr_debug("SST DBG:sst_resume_stream for %d\n", str_id);
361 retval = sst_validate_strid(str_id);
364 str_info = &sst_drv_ctx->streams[str_id];
365 if (str_info->status == STREAM_RUNNING)
367 if (str_info->status == STREAM_PAUSED) {
368 if (str_info->ctrl_blk.on == true) {
369 pr_err("SST ERR: control path in use\n");
372 if (sst_create_short_msg(&msg)) {
373 pr_err("SST ERR: mem allocation failed\n");
376 sst_fill_header(&msg->header, IPC_IA_RESUME_STREAM, 0, str_id);
377 str_info->ctrl_blk.condition = false;
378 str_info->ctrl_blk.ret_code = 0;
379 str_info->ctrl_blk.on = true;
380 spin_lock(&sst_drv_ctx->list_spin_lock);
381 list_add_tail(&msg->node,
382 &sst_drv_ctx->ipc_dispatch_list);
383 spin_unlock(&sst_drv_ctx->list_spin_lock);
384 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
385 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
386 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
388 if (str_info->prev == STREAM_RUNNING)
389 str_info->status = STREAM_RUNNING;
391 str_info->status = STREAM_INIT;
392 str_info->prev = STREAM_PAUSED;
393 } else if (retval == -SST_ERR_INVALID_STREAM_ID) {
395 mutex_lock(&sst_drv_ctx->stream_lock);
396 sst_clean_stream(str_info);
397 mutex_unlock(&sst_drv_ctx->stream_lock);
401 pr_err("SST ERR: BADQRC for stream\n");
409 * sst_drop_stream - Send msg for stopping stream
412 * This function is called by any function which wants to stop
415 int sst_drop_stream(int str_id)
418 struct ipc_post *msg = NULL;
419 struct sst_stream_bufs *bufs = NULL, *_bufs;
420 struct stream_info *str_info;
422 pr_debug("SST DBG:sst_drop_stream for %d\n", str_id);
423 retval = sst_validate_strid(str_id);
426 str_info = &sst_drv_ctx->streams[str_id];
428 if (str_info->status != STREAM_UN_INIT &&
429 str_info->status != STREAM_DECODE) {
430 if (str_info->ctrl_blk.on == true) {
431 pr_err("SST ERR: control path in use\n");
434 if (sst_create_short_msg(&msg)) {
435 pr_err("SST ERR: mem allocation failed\n");
438 sst_fill_header(&msg->header, IPC_IA_DROP_STREAM, 0, str_id);
439 str_info->ctrl_blk.condition = false;
440 str_info->ctrl_blk.ret_code = 0;
441 str_info->ctrl_blk.on = true;
442 spin_lock(&sst_drv_ctx->list_spin_lock);
443 list_add_tail(&msg->node,
444 &sst_drv_ctx->ipc_dispatch_list);
445 spin_unlock(&sst_drv_ctx->list_spin_lock);
446 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
447 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
448 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
450 pr_debug("SST DBG:drop success\n");
451 str_info->prev = STREAM_UN_INIT;
452 str_info->status = STREAM_INIT;
453 if (str_info->src != MAD_DRV) {
454 mutex_lock(&str_info->lock);
455 list_for_each_entry_safe(bufs, _bufs,
456 &str_info->bufs, node) {
457 list_del(&bufs->node);
460 mutex_unlock(&str_info->lock);
462 str_info->cumm_bytes += str_info->curr_bytes;
463 } else if (retval == -SST_ERR_INVALID_STREAM_ID) {
465 mutex_lock(&sst_drv_ctx->stream_lock);
466 sst_clean_stream(str_info);
467 mutex_unlock(&sst_drv_ctx->stream_lock);
469 if (str_info->data_blk.on == true) {
470 str_info->data_blk.condition = true;
471 str_info->data_blk.ret_code = retval;
472 wake_up(&sst_drv_ctx->wait_queue);
476 pr_err("SST ERR: BADQRC for stream\n");
482 * sst_drain_stream - Send msg for draining stream
485 * This function is called by any function which wants to drain
488 int sst_drain_stream(int str_id)
491 struct ipc_post *msg = NULL;
492 struct stream_info *str_info;
494 pr_debug("SST DBG:sst_drain_stream for %d\n", str_id);
495 retval = sst_validate_strid(str_id);
498 str_info = &sst_drv_ctx->streams[str_id];
500 if (str_info->status != STREAM_RUNNING &&
501 str_info->status != STREAM_INIT &&
502 str_info->status != STREAM_PAUSED) {
503 pr_err("SST ERR: BADQRC for stream = %d\n",
508 if (str_info->status == STREAM_INIT) {
509 if (sst_create_short_msg(&msg)) {
510 pr_err("SST ERR: mem allocation failed\n");
513 sst_fill_header(&msg->header, IPC_IA_DRAIN_STREAM, 0, str_id);
514 spin_lock(&sst_drv_ctx->list_spin_lock);
515 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
516 spin_unlock(&sst_drv_ctx->list_spin_lock);
517 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
519 str_info->need_draining = true;
520 str_info->data_blk.condition = false;
521 str_info->data_blk.ret_code = 0;
522 str_info->data_blk.on = true;
523 retval = sst_wait_interruptible(sst_drv_ctx, &str_info->data_blk);
524 str_info->need_draining = false;
529 * sst_free_stream - Frees a stream
532 * This function is called by any function which wants to free
535 int sst_free_stream(int str_id)
538 struct ipc_post *msg = NULL;
539 struct stream_info *str_info;
541 pr_debug("SST DBG:sst_free_stream for %d\n", str_id);
543 retval = sst_validate_strid(str_id);
546 str_info = &sst_drv_ctx->streams[str_id];
548 if (str_info->status != STREAM_UN_INIT) {
549 if (sst_create_short_msg(&msg)) {
550 pr_err("SST ERR: mem allocation failed\n");
553 sst_fill_header(&msg->header, IPC_IA_FREE_STREAM, 0, str_id);
554 spin_lock(&sst_drv_ctx->list_spin_lock);
555 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
556 spin_unlock(&sst_drv_ctx->list_spin_lock);
557 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
558 str_info->prev = str_info->status;
559 str_info->status = STREAM_UN_INIT;
560 if (str_info->data_blk.on == true) {
561 str_info->data_blk.condition = true;
562 str_info->data_blk.ret_code = 0;
563 wake_up(&sst_drv_ctx->wait_queue);
565 str_info->data_blk.on = true;
566 str_info->data_blk.condition = false;
567 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
568 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
569 pr_debug("wait for free returned %d\n", retval);
571 mutex_lock(&sst_drv_ctx->stream_lock);
572 sst_clean_stream(str_info);
573 mutex_unlock(&sst_drv_ctx->stream_lock);
574 pr_debug("SST DBG:Stream freed\n");
577 pr_debug("SST DBG:BADQRC for stream\n");