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 #include <linux/pci.h>
30 #include <linux/firmware.h>
31 #include <linux/sched.h>
32 #include "intel_sst_ioctl.h"
33 #include "intel_sst.h"
34 #include "intel_sst_fw_ipc.h"
35 #include "intel_sst_common.h"
38 * sst_check_device_type - Check the medfield device type
40 * @device: Device to be checked
41 * @num_ch: Number of channels queried
42 * @pcm_slot: slot to be enabled for this device
44 * This checks the deivce against the map and calculates pcm_slot value
46 int sst_check_device_type(u32 device, u32 num_chan, u32 *pcm_slot)
48 if (device > MAX_NUM_STREAMS_MFLD) {
49 pr_debug("sst: device type invalid %d\n", device);
52 if (sst_drv_ctx->streams[device].status == STREAM_UN_INIT) {
53 if (device == SND_SST_DEVICE_VIBRA && num_chan == 1)
55 else if (device == SND_SST_DEVICE_HAPTIC && num_chan == 1)
57 else if (device == SND_SST_DEVICE_IHF && num_chan == 1)
59 else if (device == SND_SST_DEVICE_IHF && num_chan == 2)
61 else if (device == SND_SST_DEVICE_HEADSET && num_chan == 1)
63 else if (device == SND_SST_DEVICE_HEADSET && num_chan == 2)
65 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 1)
67 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 2)
69 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 3)
71 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 4)
74 pr_debug("sst: No condition satisfied.. ret err\n");
78 pr_debug("sst: this stream state is not uni-init, is %d\n",
79 sst_drv_ctx->streams[device].status);
82 pr_debug("sst: returning slot %x\n", *pcm_slot);
86 * get_mrst_stream_id - gets a new stream id for use
88 * This functions searches the current streams and allocated an empty stream
89 * lock stream_lock required to be held before calling this
91 static unsigned int get_mrst_stream_id(void)
95 for (i = 1; i <= MAX_NUM_STREAMS_MRST; i++) {
96 if (sst_drv_ctx->streams[i].status == STREAM_UN_INIT)
99 pr_debug("sst: Didnt find empty stream for mrst\n");
104 * sst_alloc_stream - Send msg for a new stream ID
106 * @params: stream params
107 * @stream_ops: operation of stream PB/capture
108 * @codec: codec for stream
109 * @device: device stream to be allocated for
111 * This function is called by any function which wants to start
112 * a new stream. This also check if a stream exists which is idle
113 * it initializes idle stream id to this request
115 int sst_alloc_stream(char *params, unsigned int stream_ops,
116 u8 codec, unsigned int device)
118 struct ipc_post *msg = NULL;
119 struct snd_sst_alloc_params alloc_param;
120 unsigned int pcm_slot = 0, num_ch, str_id;
121 struct snd_sst_stream_params *sparams;
122 struct stream_info *str_info;
124 pr_debug("SST DBG:entering sst_alloc_stream\n");
125 pr_debug("SST DBG:%d %d %d\n", stream_ops, codec, device);
128 sparams = (struct snd_sst_stream_params *)params;
129 num_ch = sparams->uc.pcm_params.num_chan;
130 /*check the device type*/
131 if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID) {
132 if (sst_check_device_type(device, num_ch, &pcm_slot))
134 mutex_lock(&sst_drv_ctx->stream_lock);
136 mutex_unlock(&sst_drv_ctx->stream_lock);
137 pr_debug("SST_DBG: slot %x\n", pcm_slot);
139 mutex_lock(&sst_drv_ctx->stream_lock);
140 str_id = get_mrst_stream_id();
141 mutex_unlock(&sst_drv_ctx->stream_lock);
145 /*allocate device type context*/
146 sst_init_stream(&sst_drv_ctx->streams[str_id], codec,
147 str_id, stream_ops, pcm_slot, device);
148 /* send msg to FW to allocate a stream */
149 if (sst_create_large_msg(&msg))
152 sst_fill_header(&msg->header, IPC_IA_ALLOC_STREAM, 1, str_id);
153 msg->header.part.data = sizeof(alloc_param) + sizeof(u32);
154 alloc_param.str_type.codec_type = codec;
155 alloc_param.str_type.str_type = SST_STREAM_TYPE_MUSIC;
156 alloc_param.str_type.operation = stream_ops;
157 alloc_param.str_type.protected_str = 0; /* non drm */
158 alloc_param.str_type.time_slots = pcm_slot;
159 alloc_param.str_type.result = alloc_param.str_type.reserved = 0;
160 memcpy(&alloc_param.stream_params, params,
161 sizeof(struct snd_sst_stream_params));
163 memcpy(msg->mailbox_data, &msg->header, sizeof(u32));
164 memcpy(msg->mailbox_data + sizeof(u32), &alloc_param,
165 sizeof(alloc_param));
166 str_info = &sst_drv_ctx->streams[str_id];
167 str_info->ctrl_blk.condition = false;
168 str_info->ctrl_blk.ret_code = 0;
169 str_info->ctrl_blk.on = true;
170 spin_lock(&sst_drv_ctx->list_spin_lock);
171 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
172 spin_unlock(&sst_drv_ctx->list_spin_lock);
173 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
174 pr_debug("SST DBG:alloc stream done\n");
180 * sst_alloc_stream_response - process alloc reply
182 * @str_id: stream id for which the stream has been allocated
183 * @resp the stream response from firware
185 * This function is called by firmware as a response to stream allcoation
188 int sst_alloc_stream_response(unsigned int str_id,
189 struct snd_sst_alloc_response *resp)
192 struct stream_info *str_info;
193 struct snd_sst_lib_download *lib_dnld;
195 pr_debug("SST DEBUG: stream number given = %d\n", str_id);
196 str_info = &sst_drv_ctx->streams[str_id];
197 if (resp->str_type.result == SST_LIB_ERR_LIB_DNLD_REQUIRED) {
198 lib_dnld = kzalloc(sizeof(*lib_dnld), GFP_KERNEL);
199 memcpy(lib_dnld, &resp->lib_dnld, sizeof(*lib_dnld));
202 if (str_info->ctrl_blk.on == true) {
203 str_info->ctrl_blk.on = false;
204 str_info->ctrl_blk.data = lib_dnld;
205 str_info->ctrl_blk.condition = true;
206 str_info->ctrl_blk.ret_code = resp->str_type.result;
207 pr_debug("SST DEBUG: sst_alloc_stream_response: waking up.\n");
208 wake_up(&sst_drv_ctx->wait_queue);
215 * sst_get_fw_info - Send msg to query for firmware configurations
216 * @info: out param that holds the firmare configurations
218 * This function is called when the firmware configurations are queiried for
220 int sst_get_fw_info(struct snd_sst_fw_info *info)
223 struct ipc_post *msg = NULL;
225 pr_debug("SST DBG:sst_get_fw_info called\n");
227 if (sst_create_short_msg(&msg)) {
228 pr_err("SST ERR: message creation failed\n");
232 sst_fill_header(&msg->header, IPC_IA_GET_FW_INFO, 0, 0);
233 sst_drv_ctx->fw_info_blk.condition = false;
234 sst_drv_ctx->fw_info_blk.ret_code = 0;
235 sst_drv_ctx->fw_info_blk.on = true;
236 sst_drv_ctx->fw_info_blk.data = info;
237 spin_lock(&sst_drv_ctx->list_spin_lock);
238 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
239 spin_unlock(&sst_drv_ctx->list_spin_lock);
240 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
241 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
242 &sst_drv_ctx->fw_info_blk, SST_BLOCK_TIMEOUT);
244 pr_err("SST ERR: error in fw_info = %d\n", retval);
252 * sst_pause_stream - Send msg for a pausing stream
255 * This function is called by any function which wants to pause
256 * an already running stream.
258 int sst_start_stream(int str_id)
261 struct ipc_post *msg = NULL;
262 struct stream_info *str_info;
264 pr_debug("sst_start_stream for %d\n", str_id);
265 retval = sst_validate_strid(str_id);
268 str_info = &sst_drv_ctx->streams[str_id];
269 if (str_info->status != STREAM_INIT)
271 if (sst_create_short_msg(&msg))
274 sst_fill_header(&msg->header, IPC_IA_START_STREAM, 0, str_id);
275 spin_lock(&sst_drv_ctx->list_spin_lock);
276 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
277 spin_unlock(&sst_drv_ctx->list_spin_lock);
278 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
283 * sst_pause_stream - Send msg for a pausing stream
286 * This function is called by any function which wants to pause
287 * an already running stream.
289 int sst_pause_stream(int str_id)
292 struct ipc_post *msg = NULL;
293 struct stream_info *str_info;
295 pr_debug("SST DBG:sst_pause_stream for %d\n", str_id);
296 retval = sst_validate_strid(str_id);
299 str_info = &sst_drv_ctx->streams[str_id];
300 if (str_info->status == STREAM_PAUSED)
302 if (str_info->status == STREAM_RUNNING ||
303 str_info->status == STREAM_INIT) {
304 if (str_info->prev == STREAM_UN_INIT)
306 if (str_info->ctrl_blk.on == true) {
307 pr_err("SST ERR: control path is in use\n ");
310 if (sst_create_short_msg(&msg))
313 sst_fill_header(&msg->header, IPC_IA_PAUSE_STREAM, 0, str_id);
314 str_info->ctrl_blk.condition = false;
315 str_info->ctrl_blk.ret_code = 0;
316 str_info->ctrl_blk.on = true;
317 spin_lock(&sst_drv_ctx->list_spin_lock);
318 list_add_tail(&msg->node,
319 &sst_drv_ctx->ipc_dispatch_list);
320 spin_unlock(&sst_drv_ctx->list_spin_lock);
321 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
322 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
323 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
325 str_info->prev = str_info->status;
326 str_info->status = STREAM_PAUSED;
327 } else if (retval == SST_ERR_INVALID_STREAM_ID) {
329 mutex_lock(&sst_drv_ctx->stream_lock);
330 sst_clean_stream(str_info);
331 mutex_unlock(&sst_drv_ctx->stream_lock);
335 pr_err("SST ERR:BADQRC for stream\n ");
342 * sst_resume_stream - Send msg for resuming stream
345 * This function is called by any function which wants to resume
346 * an already paused stream.
348 int sst_resume_stream(int str_id)
351 struct ipc_post *msg = NULL;
352 struct stream_info *str_info;
354 pr_debug("SST DBG:sst_resume_stream for %d\n", str_id);
355 retval = sst_validate_strid(str_id);
358 str_info = &sst_drv_ctx->streams[str_id];
359 if (str_info->status == STREAM_RUNNING)
361 if (str_info->status == STREAM_PAUSED) {
362 if (str_info->ctrl_blk.on == true) {
363 pr_err("SST ERR: control path in use\n");
366 if (sst_create_short_msg(&msg)) {
367 pr_err("SST ERR: mem allocation failed\n");
370 sst_fill_header(&msg->header, IPC_IA_RESUME_STREAM, 0, str_id);
371 str_info->ctrl_blk.condition = false;
372 str_info->ctrl_blk.ret_code = 0;
373 str_info->ctrl_blk.on = true;
374 spin_lock(&sst_drv_ctx->list_spin_lock);
375 list_add_tail(&msg->node,
376 &sst_drv_ctx->ipc_dispatch_list);
377 spin_unlock(&sst_drv_ctx->list_spin_lock);
378 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
379 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
380 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
382 if (str_info->prev == STREAM_RUNNING)
383 str_info->status = STREAM_RUNNING;
385 str_info->status = STREAM_INIT;
386 str_info->prev = STREAM_PAUSED;
387 } else if (retval == -SST_ERR_INVALID_STREAM_ID) {
389 mutex_lock(&sst_drv_ctx->stream_lock);
390 sst_clean_stream(str_info);
391 mutex_unlock(&sst_drv_ctx->stream_lock);
395 pr_err("SST ERR: BADQRC for stream\n");
403 * sst_drop_stream - Send msg for stopping stream
406 * This function is called by any function which wants to stop
409 int sst_drop_stream(int str_id)
412 struct ipc_post *msg = NULL;
413 struct sst_stream_bufs *bufs = NULL, *_bufs;
414 struct stream_info *str_info;
416 pr_debug("SST DBG:sst_drop_stream for %d\n", str_id);
417 retval = sst_validate_strid(str_id);
420 str_info = &sst_drv_ctx->streams[str_id];
422 if (str_info->status != STREAM_UN_INIT &&
423 str_info->status != STREAM_DECODE) {
424 if (str_info->ctrl_blk.on == true) {
425 pr_err("SST ERR: control path in use\n");
428 if (sst_create_short_msg(&msg)) {
429 pr_err("SST ERR: mem allocation failed\n");
432 sst_fill_header(&msg->header, IPC_IA_DROP_STREAM, 0, str_id);
433 str_info->ctrl_blk.condition = false;
434 str_info->ctrl_blk.ret_code = 0;
435 str_info->ctrl_blk.on = true;
436 spin_lock(&sst_drv_ctx->list_spin_lock);
437 list_add_tail(&msg->node,
438 &sst_drv_ctx->ipc_dispatch_list);
439 spin_unlock(&sst_drv_ctx->list_spin_lock);
440 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
441 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
442 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
444 pr_debug("SST DBG:drop success\n");
445 str_info->prev = STREAM_UN_INIT;
446 str_info->status = STREAM_INIT;
447 if (str_info->src != MAD_DRV) {
448 mutex_lock(&str_info->lock);
449 list_for_each_entry_safe(bufs, _bufs,
450 &str_info->bufs, node) {
451 list_del(&bufs->node);
454 mutex_unlock(&str_info->lock);
456 str_info->cumm_bytes += str_info->curr_bytes;
457 } else if (retval == -SST_ERR_INVALID_STREAM_ID) {
459 mutex_lock(&sst_drv_ctx->stream_lock);
460 sst_clean_stream(str_info);
461 mutex_unlock(&sst_drv_ctx->stream_lock);
463 if (str_info->data_blk.on == true) {
464 str_info->data_blk.condition = true;
465 str_info->data_blk.ret_code = retval;
466 wake_up(&sst_drv_ctx->wait_queue);
470 pr_err("SST ERR:BADQRC for stream\n");
476 * sst_drain_stream - Send msg for draining stream
479 * This function is called by any function which wants to drain
482 int sst_drain_stream(int str_id)
485 struct ipc_post *msg = NULL;
486 struct stream_info *str_info;
488 pr_debug("SST DBG:sst_drain_stream for %d\n", str_id);
489 retval = sst_validate_strid(str_id);
492 str_info = &sst_drv_ctx->streams[str_id];
494 if (str_info->status != STREAM_RUNNING &&
495 str_info->status != STREAM_INIT &&
496 str_info->status != STREAM_PAUSED) {
497 pr_err("SST ERR: BADQRC for stream = %d\n",
502 if (str_info->status == STREAM_INIT) {
503 if (sst_create_short_msg(&msg)) {
504 pr_err("SST ERR: mem allocation failed\n");
507 sst_fill_header(&msg->header, IPC_IA_DRAIN_STREAM, 0, str_id);
508 spin_lock(&sst_drv_ctx->list_spin_lock);
509 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
510 spin_unlock(&sst_drv_ctx->list_spin_lock);
511 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
513 str_info->need_draining = true;
514 str_info->data_blk.condition = false;
515 str_info->data_blk.ret_code = 0;
516 str_info->data_blk.on = true;
517 retval = sst_wait_interruptible(sst_drv_ctx, &str_info->data_blk);
518 str_info->need_draining = false;
519 if (retval == -SST_ERR_INVALID_STREAM_ID) {
521 sst_clean_stream(str_info);
527 * sst_free_stream - Frees a stream
530 * This function is called by any function which wants to free
533 int sst_free_stream(int str_id)
536 struct ipc_post *msg = NULL;
537 struct stream_info *str_info;
539 pr_debug("SST DBG:sst_free_stream for %d\n", str_id);
541 retval = sst_validate_strid(str_id);
544 str_info = &sst_drv_ctx->streams[str_id];
546 if (str_info->status != STREAM_UN_INIT) {
547 if (sst_create_short_msg(&msg)) {
548 pr_err("SST ERR: mem allocation failed\n");
551 sst_fill_header(&msg->header, IPC_IA_FREE_STREAM, 0, str_id);
552 spin_lock(&sst_drv_ctx->list_spin_lock);
553 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
554 spin_unlock(&sst_drv_ctx->list_spin_lock);
555 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
556 str_info->prev = str_info->status;
557 str_info->status = STREAM_UN_INIT;
558 if (str_info->data_blk.on == true) {
559 str_info->data_blk.condition = true;
560 str_info->data_blk.ret_code = 0;
561 wake_up(&sst_drv_ctx->wait_queue);
563 mutex_lock(&sst_drv_ctx->stream_lock);
564 sst_clean_stream(str_info);
565 mutex_unlock(&sst_drv_ctx->stream_lock);
566 pr_debug("SST DBG:Stream freed\n");
569 pr_debug("SST DBG:BADQRC for stream\n");