]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/intel_sst/intel_sst_stream.c
ff46d5ccf78145c6e17e2e5e6d17ae6ad05a8e1e
[net-next-2.6.git] / drivers / staging / intel_sst / intel_sst_stream.c
1 /*
2  *  intel_sst_stream.c - Intel SST Driver for audio engine
3  *
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  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
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.
14  *
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.
19  *
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.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *
26  *  This file contains the stream operations of SST driver
27  */
28
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"
36
37 /*
38  * sst_check_device_type - Check the medfield device type
39  *
40  * @device: Device to be checked
41  * @num_ch: Number of channels queried
42  * @pcm_slot: slot to be enabled for this device
43  *
44  * This checks the deivce against the map and calculates pcm_slot value
45  */
46 int sst_check_device_type(u32 device, u32 num_chan, u32 *pcm_slot)
47 {
48         if (device > MAX_NUM_STREAMS_MFLD) {
49                 pr_debug("sst: device type invalid %d\n", device);
50                 return -EINVAL;
51         }
52         if (sst_drv_ctx->streams[device].status == STREAM_UN_INIT) {
53                 if (device == SND_SST_DEVICE_VIBRA && num_chan == 1)
54                         *pcm_slot = 0x10;
55                 else if (device == SND_SST_DEVICE_HAPTIC && num_chan == 1)
56                         *pcm_slot = 0x20;
57                 else if (device == SND_SST_DEVICE_IHF && num_chan == 1)
58                         *pcm_slot = 0x04;
59                 else if (device == SND_SST_DEVICE_IHF && num_chan == 2)
60                         *pcm_slot = 0x0C;
61                 else if (device == SND_SST_DEVICE_HEADSET && num_chan == 1)
62                         *pcm_slot = 0x01;
63                 else if (device == SND_SST_DEVICE_HEADSET && num_chan == 2)
64                         *pcm_slot = 0x03;
65                 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 1)
66                         *pcm_slot = 0x01;
67                 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 2)
68                         *pcm_slot = 0x03;
69                 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 3)
70                         *pcm_slot = 0x07;
71                 else if (device == SND_SST_DEVICE_CAPTURE && num_chan == 4)
72                         *pcm_slot = 0x0F;
73                 else {
74                         pr_debug("sst: No condition satisfied.. ret err\n");
75                         return -EINVAL;
76                 }
77         } else {
78                 pr_debug("sst: this stream state is not uni-init, is %d\n",
79                                 sst_drv_ctx->streams[device].status);
80                 return -EBADRQC;
81         }
82         pr_debug("sst: returning slot %x\n", *pcm_slot);
83         return 0;
84 }
85 /**
86  * get_mrst_stream_id   -       gets a new stream id for use
87  *
88  * This functions searches the current streams and allocated an empty stream
89  * lock stream_lock required to be held before calling this
90  */
91 static unsigned int get_mrst_stream_id(void)
92 {
93         int i;
94
95         for (i = 1; i <= MAX_NUM_STREAMS_MRST; i++) {
96                 if (sst_drv_ctx->streams[i].status == STREAM_UN_INIT)
97                         return i;
98         }
99         pr_debug("sst: Didnt find empty stream for mrst\n");
100         return -EBUSY;
101 }
102
103 /**
104  * sst_alloc_stream - Send msg for a new stream ID
105  *
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
110  *
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
114  */
115 int sst_alloc_stream(char *params, unsigned int stream_ops,
116                u8 codec, unsigned int device)
117 {
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;
123
124         pr_debug("SST DBG:entering sst_alloc_stream\n");
125         pr_debug("SST DBG:%d %d %d\n", stream_ops, codec, device);
126
127         BUG_ON(!params);
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))
133                         return -EINVAL;
134                 mutex_lock(&sst_drv_ctx->stream_lock);
135                 str_id = device;
136                 mutex_unlock(&sst_drv_ctx->stream_lock);
137                 pr_debug("SST_DBG: slot %x\n", pcm_slot);
138         } else {
139                 mutex_lock(&sst_drv_ctx->stream_lock);
140                 str_id = get_mrst_stream_id();
141                 mutex_unlock(&sst_drv_ctx->stream_lock);
142                 if (str_id <= 0)
143                         return -EBUSY;
144         }
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))
150                 return -ENOMEM;
151
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));
162
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");
175         return str_id;
176 }
177
178
179 /*
180  * sst_alloc_stream_response - process alloc reply
181  *
182  * @str_id:     stream id for which the stream has been allocated
183  * @resp                the stream response from firware
184  *
185  * This function is called by firmware as a response to stream allcoation
186  * request
187  */
188 int sst_alloc_stream_response(unsigned int str_id,
189                                 struct snd_sst_alloc_response *resp)
190 {
191         int retval = 0;
192         struct stream_info *str_info;
193         struct snd_sst_lib_download *lib_dnld;
194
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));
200         } else
201                 lib_dnld = NULL;
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);
209         }
210         return retval;
211 }
212
213
214 /**
215 * sst_get_fw_info - Send msg to query for firmware configurations
216 * @info: out param that holds the firmare configurations
217 *
218 * This function is called when the firmware configurations are queiried for
219 */
220 int sst_get_fw_info(struct snd_sst_fw_info *info)
221 {
222         int retval = 0;
223         struct ipc_post *msg = NULL;
224
225         pr_debug("SST DBG:sst_get_fw_info called\n");
226
227         if (sst_create_short_msg(&msg)) {
228                 pr_err("SST ERR: message creation failed\n");
229                 return -ENOMEM;
230         }
231
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);
243         if (retval) {
244                 pr_err("SST ERR: error in fw_info = %d\n", retval);
245                 retval = -EIO;
246         }
247         return retval;
248 }
249
250
251 /**
252 * sst_pause_stream - Send msg for a pausing stream
253 * @str_id:       stream ID
254 *
255 * This function is called by any function which wants to pause
256 * an already running stream.
257 */
258 int sst_start_stream(int str_id)
259 {
260         int retval = 0;
261         struct ipc_post *msg = NULL;
262         struct stream_info *str_info;
263
264         pr_debug("sst_start_stream for %d\n", str_id);
265         retval = sst_validate_strid(str_id);
266         if (retval)
267                 return retval;
268         str_info = &sst_drv_ctx->streams[str_id];
269         if (str_info->status != STREAM_INIT)
270                 return -EBADRQC;
271         if (sst_create_short_msg(&msg))
272                 return -ENOMEM;
273
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);
279         return retval;
280 }
281
282 /*
283  * sst_pause_stream - Send msg for a pausing stream
284  * @str_id:      stream ID
285  *
286  * This function is called by any function which wants to pause
287  * an already running stream.
288  */
289 int sst_pause_stream(int str_id)
290 {
291         int retval = 0;
292         struct ipc_post *msg = NULL;
293         struct stream_info *str_info;
294
295         pr_debug("SST DBG:sst_pause_stream for %d\n", str_id);
296         retval = sst_validate_strid(str_id);
297         if (retval)
298                 return retval;
299         str_info = &sst_drv_ctx->streams[str_id];
300         if (str_info->status == STREAM_PAUSED)
301                 return 0;
302         if (str_info->status == STREAM_RUNNING ||
303                 str_info->status == STREAM_INIT) {
304                 if (str_info->prev == STREAM_UN_INIT)
305                         return -EBADRQC;
306                 if (str_info->ctrl_blk.on == true) {
307                         pr_err("SST ERR: control path is in use\n ");
308                         return -EINVAL;
309                 }
310                 if (sst_create_short_msg(&msg))
311                         return -ENOMEM;
312
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);
324                 if (retval == 0) {
325                         str_info->prev = str_info->status;
326                         str_info->status = STREAM_PAUSED;
327                 } else if (retval == SST_ERR_INVALID_STREAM_ID) {
328                         retval = -EINVAL;
329                         mutex_lock(&sst_drv_ctx->stream_lock);
330                         sst_clean_stream(str_info);
331                         mutex_unlock(&sst_drv_ctx->stream_lock);
332                 }
333         } else {
334                 retval = -EBADRQC;
335                 pr_err("SST ERR:BADQRC for stream\n ");
336         }
337
338         return retval;
339 }
340
341 /**
342  * sst_resume_stream - Send msg for resuming stream
343  * @str_id:             stream ID
344  *
345  * This function is called by any function which wants to resume
346  * an already paused stream.
347  */
348 int sst_resume_stream(int str_id)
349 {
350         int retval = 0;
351         struct ipc_post *msg = NULL;
352         struct stream_info *str_info;
353
354         pr_debug("SST DBG:sst_resume_stream for %d\n", str_id);
355         retval = sst_validate_strid(str_id);
356         if (retval)
357                 return retval;
358         str_info = &sst_drv_ctx->streams[str_id];
359         if (str_info->status == STREAM_RUNNING)
360                         return 0;
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");
364                         return -EINVAL;
365                 }
366                 if (sst_create_short_msg(&msg)) {
367                         pr_err("SST ERR: mem allocation failed\n");
368                         return -ENOMEM;
369                 }
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);
381                 if (!retval) {
382                         if (str_info->prev == STREAM_RUNNING)
383                                 str_info->status = STREAM_RUNNING;
384                         else
385                                 str_info->status = STREAM_INIT;
386                         str_info->prev = STREAM_PAUSED;
387                 } else if (retval == -SST_ERR_INVALID_STREAM_ID) {
388                         retval = -EINVAL;
389                         mutex_lock(&sst_drv_ctx->stream_lock);
390                         sst_clean_stream(str_info);
391                         mutex_unlock(&sst_drv_ctx->stream_lock);
392                 }
393         } else {
394                 retval = -EBADRQC;
395                 pr_err("SST ERR: BADQRC for stream\n");
396         }
397
398         return retval;
399 }
400
401
402 /**
403  * sst_drop_stream - Send msg for stopping stream
404  * @str_id:             stream ID
405  *
406  * This function is called by any function which wants to stop
407  * a stream.
408  */
409 int sst_drop_stream(int str_id)
410 {
411         int retval = 0;
412         struct ipc_post *msg = NULL;
413         struct sst_stream_bufs *bufs = NULL, *_bufs;
414         struct stream_info *str_info;
415
416         pr_debug("SST DBG:sst_drop_stream for %d\n", str_id);
417         retval = sst_validate_strid(str_id);
418         if (retval)
419                 return retval;
420         str_info = &sst_drv_ctx->streams[str_id];
421
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");
426                         return -EINVAL;
427                 }
428                 if (sst_create_short_msg(&msg)) {
429                         pr_err("SST ERR: mem allocation failed\n");
430                         return -ENOMEM;
431                 }
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);
443                 if (!retval) {
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);
452                                         kfree(bufs);
453                                 }
454                                 mutex_unlock(&str_info->lock);
455                         }
456                         str_info->cumm_bytes += str_info->curr_bytes;
457                 } else if (retval == -SST_ERR_INVALID_STREAM_ID) {
458                         retval = -EINVAL;
459                         mutex_lock(&sst_drv_ctx->stream_lock);
460                         sst_clean_stream(str_info);
461                         mutex_unlock(&sst_drv_ctx->stream_lock);
462                 }
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);
467                 }
468         } else {
469                 retval = -EBADRQC;
470                 pr_err("SST ERR:BADQRC for stream\n");
471         }
472         return retval;
473 }
474
475 /**
476 * sst_drain_stream - Send msg for draining stream
477 * @str_id:              stream ID
478 *
479 * This function is called by any function which wants to drain
480 * a stream.
481 */
482 int sst_drain_stream(int str_id)
483 {
484         int retval = 0;
485         struct ipc_post *msg = NULL;
486         struct stream_info *str_info;
487
488         pr_debug("SST DBG:sst_drain_stream for %d\n", str_id);
489         retval = sst_validate_strid(str_id);
490         if (retval)
491                 return retval;
492         str_info = &sst_drv_ctx->streams[str_id];
493
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",
498                                        str_info->status);
499                         return -EBADRQC;
500         }
501
502         if (str_info->status == STREAM_INIT) {
503                 if (sst_create_short_msg(&msg)) {
504                         pr_err("SST ERR: mem allocation failed\n");
505                         return -ENOMEM;
506                 }
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);
512         } else
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) {
520                 retval = -EINVAL;
521                 sst_clean_stream(str_info);
522         }
523         return retval;
524 }
525
526 /**
527  * sst_free_stream - Frees a stream
528  * @str_id:             stream ID
529  *
530  * This function is called by any function which wants to free
531  * a stream.
532  */
533 int sst_free_stream(int str_id)
534 {
535         int retval = 0;
536         struct ipc_post *msg = NULL;
537         struct stream_info *str_info;
538
539         pr_debug("SST DBG:sst_free_stream for %d\n", str_id);
540
541         retval = sst_validate_strid(str_id);
542         if (retval)
543                 return retval;
544         str_info = &sst_drv_ctx->streams[str_id];
545
546         if (str_info->status != STREAM_UN_INIT) {
547                 if (sst_create_short_msg(&msg)) {
548                         pr_err("SST ERR: mem allocation failed\n");
549                         return -ENOMEM;
550                 }
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);
562                 }
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");
567         } else {
568                 retval = -EBADRQC;
569                 pr_debug("SST DBG:BADQRC for stream\n");
570         }
571
572         return retval;
573 }
574
575