cx25821: sanitize cx25821_get_audio_data() a bit
[pandora-kernel.git] / drivers / media / pci / cx25821 / cx25821-audio-upstream.c
1 /*
2  *  Driver for the Conexant CX25821 PCIe bridge
3  *
4  *  Copyright (C) 2009 Conexant Systems Inc.
5  *  Authors  <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include "cx25821-video.h"
26 #include "cx25821-audio-upstream.h"
27
28 #include <linux/fs.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/syscalls.h>
34 #include <linux/file.h>
35 #include <linux/fcntl.h>
36 #include <linux/delay.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
39
40 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
41 MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
42 MODULE_LICENSE("GPL");
43
44 static int _intr_msk = FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF |
45                         FLD_AUD_SRC_SYNC | FLD_AUD_SRC_OPC_ERR;
46
47 static int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
48                                               struct sram_channel *ch,
49                                               unsigned int bpl, u32 risc)
50 {
51         unsigned int i, lines;
52         u32 cdt;
53
54         if (ch->cmds_start == 0) {
55                 cx_write(ch->ptr1_reg, 0);
56                 cx_write(ch->ptr2_reg, 0);
57                 cx_write(ch->cnt2_reg, 0);
58                 cx_write(ch->cnt1_reg, 0);
59                 return 0;
60         }
61
62         bpl = (bpl + 7) & ~7;   /* alignment */
63         cdt = ch->cdt;
64         lines = ch->fifo_size / bpl;
65
66         if (lines > 3)
67                 lines = 3;
68
69         BUG_ON(lines < 2);
70
71         /* write CDT */
72         for (i = 0; i < lines; i++) {
73                 cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
74                 cx_write(cdt + 16 * i + 4, 0);
75                 cx_write(cdt + 16 * i + 8, 0);
76                 cx_write(cdt + 16 * i + 12, 0);
77         }
78
79         /* write CMDS */
80         cx_write(ch->cmds_start + 0, risc);
81
82         cx_write(ch->cmds_start + 4, 0);
83         cx_write(ch->cmds_start + 8, cdt);
84         cx_write(ch->cmds_start + 12, AUDIO_CDT_SIZE_QW);
85         cx_write(ch->cmds_start + 16, ch->ctrl_start);
86
87         /* IQ size */
88         cx_write(ch->cmds_start + 20, AUDIO_IQ_SIZE_DW);
89
90         for (i = 24; i < 80; i += 4)
91                 cx_write(ch->cmds_start + i, 0);
92
93         /* fill registers */
94         cx_write(ch->ptr1_reg, ch->fifo_start);
95         cx_write(ch->ptr2_reg, cdt);
96         cx_write(ch->cnt2_reg, AUDIO_CDT_SIZE_QW);
97         cx_write(ch->cnt1_reg, AUDIO_CLUSTER_SIZE_QW - 1);
98
99         return 0;
100 }
101
102 static __le32 *cx25821_risc_field_upstream_audio(struct cx25821_dev *dev,
103                                                  __le32 *rp,
104                                                  dma_addr_t databuf_phys_addr,
105                                                  unsigned int bpl,
106                                                  int fifo_enable)
107 {
108         unsigned int line;
109         struct sram_channel *sram_ch =
110                 dev->channels[dev->_audio_upstream_channel].sram_channels;
111         int offset = 0;
112
113         /* scan lines */
114         for (line = 0; line < LINES_PER_AUDIO_BUFFER; line++) {
115                 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
116                 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
117                 *(rp++) = cpu_to_le32(0);       /* bits 63-32 */
118
119                 /* Check if we need to enable the FIFO
120                  * after the first 3 lines.
121                  * For the upstream audio channel,
122                  * the risc engine will enable the FIFO */
123                 if (fifo_enable && line == 2) {
124                         *(rp++) = RISC_WRITECR;
125                         *(rp++) = sram_ch->dma_ctl;
126                         *(rp++) = sram_ch->fld_aud_fifo_en;
127                         *(rp++) = 0x00000020;
128                 }
129
130                 offset += AUDIO_LINE_SIZE;
131         }
132
133         return rp;
134 }
135
136 static int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
137                                        struct pci_dev *pci,
138                                        unsigned int bpl, unsigned int lines)
139 {
140         __le32 *rp;
141         int fifo_enable = 0;
142         int frame = 0, i = 0;
143         int frame_size = AUDIO_DATA_BUF_SZ;
144         int databuf_offset = 0;
145         int risc_flag = RISC_CNT_INC;
146         dma_addr_t risc_phys_jump_addr;
147
148         /* Virtual address of Risc buffer program */
149         rp = dev->_risc_virt_addr;
150
151         /* sync instruction */
152         *(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);
153
154         for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
155                 databuf_offset = frame_size * frame;
156
157                 if (frame == 0) {
158                         fifo_enable = 1;
159                         risc_flag = RISC_CNT_RESET;
160                 } else {
161                         fifo_enable = 0;
162                         risc_flag = RISC_CNT_INC;
163                 }
164
165                 /* Calculate physical jump address */
166                 if ((frame + 1) == NUM_AUDIO_FRAMES) {
167                         risc_phys_jump_addr =
168                             dev->_risc_phys_start_addr +
169                             RISC_SYNC_INSTRUCTION_SIZE;
170                 } else {
171                         risc_phys_jump_addr =
172                             dev->_risc_phys_start_addr +
173                             RISC_SYNC_INSTRUCTION_SIZE +
174                             AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
175                 }
176
177                 rp = cx25821_risc_field_upstream_audio(dev, rp,
178                                 dev->_audiodata_buf_phys_addr + databuf_offset,
179                                 bpl, fifo_enable);
180
181                 if (USE_RISC_NOOP_AUDIO) {
182                         for (i = 0; i < NUM_NO_OPS; i++)
183                                 *(rp++) = cpu_to_le32(RISC_NOOP);
184                 }
185
186                 /* Loop to (Nth)FrameRISC or to Start of Risc program &
187                  * generate IRQ */
188                 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
189                 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
190                 *(rp++) = cpu_to_le32(0);
191
192                 /* Recalculate virtual address based on frame index */
193                 rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
194                         (AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
195         }
196
197         return 0;
198 }
199
200 static void cx25821_free_memory_audio(struct cx25821_dev *dev)
201 {
202         if (dev->_risc_virt_addr) {
203                 pci_free_consistent(dev->pci, dev->_audiorisc_size,
204                                     dev->_risc_virt_addr, dev->_risc_phys_addr);
205                 dev->_risc_virt_addr = NULL;
206         }
207
208         if (dev->_audiodata_buf_virt_addr) {
209                 pci_free_consistent(dev->pci, dev->_audiodata_buf_size,
210                                     dev->_audiodata_buf_virt_addr,
211                                     dev->_audiodata_buf_phys_addr);
212                 dev->_audiodata_buf_virt_addr = NULL;
213         }
214 }
215
216 void cx25821_stop_upstream_audio(struct cx25821_dev *dev)
217 {
218         struct sram_channel *sram_ch =
219                 dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
220         u32 tmp = 0;
221
222         if (!dev->_audio_is_running) {
223                 printk(KERN_DEBUG
224                        pr_fmt("No audio file is currently running so return!\n"));
225                 return;
226         }
227         /* Disable RISC interrupts */
228         cx_write(sram_ch->int_msk, 0);
229
230         /* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
231         tmp = cx_read(sram_ch->dma_ctl);
232         cx_write(sram_ch->dma_ctl,
233                  tmp & ~(sram_ch->fld_aud_fifo_en | sram_ch->fld_aud_risc_en));
234
235         /* Clear data buffer memory */
236         if (dev->_audiodata_buf_virt_addr)
237                 memset(dev->_audiodata_buf_virt_addr, 0,
238                        dev->_audiodata_buf_size);
239
240         dev->_audio_is_running = 0;
241         dev->_is_first_audio_frame = 0;
242         dev->_audioframe_count = 0;
243         dev->_audiofile_status = END_OF_FILE;
244
245         kfree(dev->_irq_audio_queues);
246         dev->_irq_audio_queues = NULL;
247
248         kfree(dev->_audiofilename);
249 }
250
251 void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
252 {
253         if (dev->_audio_is_running)
254                 cx25821_stop_upstream_audio(dev);
255
256         cx25821_free_memory_audio(dev);
257 }
258
259 static int cx25821_get_audio_data(struct cx25821_dev *dev,
260                            struct sram_channel *sram_ch)
261 {
262         struct file *file;
263         int frame_index_temp = dev->_audioframe_index;
264         int i = 0;
265         int frame_size = AUDIO_DATA_BUF_SZ;
266         int frame_offset = frame_size * frame_index_temp;
267         char mybuf[AUDIO_LINE_SIZE];
268         loff_t file_offset = dev->_audioframe_count * frame_size;
269         char *p = NULL;
270
271         if (dev->_audiofile_status == END_OF_FILE)
272                 return 0;
273
274         file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
275         if (IS_ERR(file)) {
276                 pr_err("%s(): ERROR opening file(%s) with errno = %ld!\n",
277                        __func__, dev->_audiofilename, -PTR_ERR(file));
278                 return PTR_ERR(file);
279         }
280
281         if (dev->_audiodata_buf_virt_addr)
282                 p = (char *)dev->_audiodata_buf_virt_addr + frame_offset;
283
284         for (i = 0; i < dev->_audio_lines_count; i++) {
285                 int n = kernel_read(file, file_offset, mybuf, AUDIO_LINE_SIZE);
286                 if (n < AUDIO_LINE_SIZE) {
287                         pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
288                                 __func__);
289                         dev->_audiofile_status = END_OF_FILE;
290                         fput(file);
291                         return 0;
292                 }
293                 dev->_audiofile_status = IN_PROGRESS;
294                 if (p) {
295                         memcpy(p, mybuf, n);
296                         p += n;
297                 }
298                 file_offset += n;
299         }
300         dev->_audioframe_count++;
301         fput(file);
302
303         return 0;
304 }
305
306 static void cx25821_audioups_handler(struct work_struct *work)
307 {
308         struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
309                         _audio_work_entry);
310
311         if (!dev) {
312                 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
313                         __func__);
314                 return;
315         }
316
317         cx25821_get_audio_data(dev, dev->channels[dev->_audio_upstream_channel].
318                         sram_channels);
319 }
320
321 static int cx25821_openfile_audio(struct cx25821_dev *dev,
322                            struct sram_channel *sram_ch)
323 {
324         struct file *myfile;
325         int i = 0, j = 0;
326         int line_size = AUDIO_LINE_SIZE;
327         ssize_t vfs_read_retval = 0;
328         char mybuf[line_size];
329         loff_t pos;
330         loff_t offset = (unsigned long)0;
331         mm_segment_t old_fs;
332
333         myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
334
335         if (IS_ERR(myfile)) {
336                 const int open_errno = -PTR_ERR(myfile);
337                 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
338                         __func__, dev->_audiofilename, open_errno);
339                 return PTR_ERR(myfile);
340         } else {
341                 if (!(myfile->f_op)) {
342                         pr_err("%s(): File has no file operations registered!\n",
343                                 __func__);
344                         filp_close(myfile, NULL);
345                         return -EIO;
346                 }
347
348                 if (!myfile->f_op->read) {
349                         pr_err("%s(): File has no READ operations registered!\n",
350                                 __func__);
351                         filp_close(myfile, NULL);
352                         return -EIO;
353                 }
354
355                 pos = myfile->f_pos;
356                 old_fs = get_fs();
357                 set_fs(KERNEL_DS);
358
359                 for (j = 0; j < NUM_AUDIO_FRAMES; j++) {
360                         for (i = 0; i < dev->_audio_lines_count; i++) {
361                                 pos = offset;
362
363                                 vfs_read_retval = vfs_read(myfile, mybuf,
364                                                 line_size, &pos);
365
366                                 if (vfs_read_retval > 0 &&
367                                     vfs_read_retval == line_size &&
368                                     dev->_audiodata_buf_virt_addr != NULL) {
369                                         memcpy((void *)(dev->
370                                                         _audiodata_buf_virt_addr
371                                                         + offset / 4), mybuf,
372                                                vfs_read_retval);
373                                 }
374
375                                 offset += vfs_read_retval;
376
377                                 if (vfs_read_retval < line_size) {
378                                         pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
379                                                 __func__);
380                                         break;
381                                 }
382                         }
383
384                         if (i > 0)
385                                 dev->_audioframe_count++;
386
387                         if (vfs_read_retval < line_size)
388                                 break;
389                 }
390
391                 dev->_audiofile_status = (vfs_read_retval == line_size) ?
392                                                 IN_PROGRESS : END_OF_FILE;
393
394                 set_fs(old_fs);
395                 myfile->f_pos = 0;
396                 filp_close(myfile, NULL);
397         }
398
399         return 0;
400 }
401
402 static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
403                                                  struct sram_channel *sram_ch,
404                                                  int bpl)
405 {
406         int ret = 0;
407         dma_addr_t dma_addr;
408         dma_addr_t data_dma_addr;
409
410         cx25821_free_memory_audio(dev);
411
412         dev->_risc_virt_addr = pci_alloc_consistent(dev->pci,
413                         dev->audio_upstream_riscbuf_size, &dma_addr);
414         dev->_risc_virt_start_addr = dev->_risc_virt_addr;
415         dev->_risc_phys_start_addr = dma_addr;
416         dev->_risc_phys_addr = dma_addr;
417         dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
418
419         if (!dev->_risc_virt_addr) {
420                 printk(KERN_DEBUG
421                         pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
422                 return -ENOMEM;
423         }
424         /* Clear out memory at address */
425         memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
426
427         /* For Audio Data buffer allocation */
428         dev->_audiodata_buf_virt_addr = pci_alloc_consistent(dev->pci,
429                         dev->audio_upstream_databuf_size, &data_dma_addr);
430         dev->_audiodata_buf_phys_addr = data_dma_addr;
431         dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
432
433         if (!dev->_audiodata_buf_virt_addr) {
434                 printk(KERN_DEBUG
435                         pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
436                 return -ENOMEM;
437         }
438         /* Clear out memory at address */
439         memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
440
441         ret = cx25821_openfile_audio(dev, sram_ch);
442         if (ret < 0)
443                 return ret;
444
445         /* Creating RISC programs */
446         ret = cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
447                                                 dev->_audio_lines_count);
448         if (ret < 0) {
449                 printk(KERN_DEBUG
450                         pr_fmt("ERROR creating audio upstream RISC programs!\n"));
451                 goto error;
452         }
453
454         return 0;
455
456 error:
457         return ret;
458 }
459
460 static int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
461                                u32 status)
462 {
463         int i = 0;
464         u32 int_msk_tmp;
465         struct sram_channel *channel = dev->channels[chan_num].sram_channels;
466         dma_addr_t risc_phys_jump_addr;
467         __le32 *rp;
468
469         if (status & FLD_AUD_SRC_RISCI1) {
470                 /* Get interrupt_index of the program that interrupted */
471                 u32 prog_cnt = cx_read(channel->gpcnt);
472
473                 /* Since we've identified our IRQ, clear our bits from the
474                  * interrupt mask and interrupt status registers */
475                 cx_write(channel->int_msk, 0);
476                 cx_write(channel->int_stat, cx_read(channel->int_stat));
477
478                 spin_lock(&dev->slock);
479
480                 while (prog_cnt != dev->_last_index_irq) {
481                         /* Update _last_index_irq */
482                         if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
483                                 dev->_last_index_irq++;
484                         else
485                                 dev->_last_index_irq = 0;
486
487                         dev->_audioframe_index = dev->_last_index_irq;
488
489                         queue_work(dev->_irq_audio_queues,
490                                    &dev->_audio_work_entry);
491                 }
492
493                 if (dev->_is_first_audio_frame) {
494                         dev->_is_first_audio_frame = 0;
495
496                         if (dev->_risc_virt_start_addr != NULL) {
497                                 risc_phys_jump_addr =
498                                         dev->_risc_phys_start_addr +
499                                         RISC_SYNC_INSTRUCTION_SIZE +
500                                         AUDIO_RISC_DMA_BUF_SIZE;
501
502                                 rp = cx25821_risc_field_upstream_audio(dev,
503                                                 dev->_risc_virt_start_addr + 1,
504                                                 dev->_audiodata_buf_phys_addr,
505                                                 AUDIO_LINE_SIZE, FIFO_DISABLE);
506
507                                 if (USE_RISC_NOOP_AUDIO) {
508                                         for (i = 0; i < NUM_NO_OPS; i++) {
509                                                 *(rp++) =
510                                                     cpu_to_le32(RISC_NOOP);
511                                         }
512                                 }
513                                 /* Jump to 2nd Audio Frame */
514                                 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
515                                                 RISC_CNT_RESET);
516                                 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
517                                 *(rp++) = cpu_to_le32(0);
518                         }
519                 }
520
521                 spin_unlock(&dev->slock);
522         } else {
523                 if (status & FLD_AUD_SRC_OF)
524                         pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
525                                 __func__);
526
527                 if (status & FLD_AUD_SRC_SYNC)
528                         pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
529                                 __func__);
530
531                 if (status & FLD_AUD_SRC_OPC_ERR)
532                         pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
533                                 __func__);
534
535                 /* Read and write back the interrupt status register to clear
536                  * our bits */
537                 cx_write(channel->int_stat, cx_read(channel->int_stat));
538         }
539
540         if (dev->_audiofile_status == END_OF_FILE) {
541                 pr_warn("EOF Channel Audio Framecount = %d\n",
542                         dev->_audioframe_count);
543                 return -1;
544         }
545         /* ElSE, set the interrupt mask register, re-enable irq. */
546         int_msk_tmp = cx_read(channel->int_msk);
547         cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
548
549         return 0;
550 }
551
552 static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
553 {
554         struct cx25821_dev *dev = dev_id;
555         u32 audio_status;
556         int handled = 0;
557         struct sram_channel *sram_ch;
558
559         if (!dev)
560                 return -1;
561
562         sram_ch = dev->channels[dev->_audio_upstream_channel].sram_channels;
563
564         audio_status = cx_read(sram_ch->int_stat);
565
566         /* Only deal with our interrupt */
567         if (audio_status) {
568                 handled = cx25821_audio_upstream_irq(dev,
569                                 dev->_audio_upstream_channel, audio_status);
570         }
571
572         if (handled < 0)
573                 cx25821_stop_upstream_audio(dev);
574         else
575                 handled += handled;
576
577         return IRQ_RETVAL(handled);
578 }
579
580 static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
581                                      struct sram_channel *sram_ch)
582 {
583         int count = 0;
584         u32 tmp;
585
586         do {
587                 /* Wait 10 microsecond before checking to see if the FIFO is
588                  * turned ON. */
589                 udelay(10);
590
591                 tmp = cx_read(sram_ch->dma_ctl);
592
593                 /* 10 millisecond timeout */
594                 if (count++ > 1000) {
595                         pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
596                                 __func__);
597                         return;
598                 }
599
600         } while (!(tmp & sram_ch->fld_aud_fifo_en));
601
602 }
603
604 static int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
605                                             struct sram_channel *sram_ch)
606 {
607         u32 tmp = 0;
608         int err = 0;
609
610         /* Set the physical start address of the RISC program in the initial
611          * program counter(IPC) member of the CMDS. */
612         cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
613         /* Risc IPC High 64 bits 63-32 */
614         cx_write(sram_ch->cmds_start + 4, 0);
615
616         /* reset counter */
617         cx_write(sram_ch->gpcnt_ctl, 3);
618
619         /* Set the line length       (It looks like we do not need to set the
620          * line length) */
621         cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
622
623         /* Set the input mode to 16-bit */
624         tmp = cx_read(sram_ch->aud_cfg);
625         tmp |= FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
626                 FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D |
627                 FLD_AUD_SONY_MODE;
628         cx_write(sram_ch->aud_cfg, tmp);
629
630         /* Read and write back the interrupt status register to clear it */
631         tmp = cx_read(sram_ch->int_stat);
632         cx_write(sram_ch->int_stat, tmp);
633
634         /* Clear our bits from the interrupt status register. */
635         cx_write(sram_ch->int_stat, _intr_msk);
636
637         /* Set the interrupt mask register, enable irq. */
638         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
639         tmp = cx_read(sram_ch->int_msk);
640         cx_write(sram_ch->int_msk, tmp |= _intr_msk);
641
642         err = request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
643                         IRQF_SHARED, dev->name, dev);
644         if (err < 0) {
645                 pr_err("%s: can't get upstream IRQ %d\n", dev->name,
646                                 dev->pci->irq);
647                 goto fail_irq;
648         }
649
650         /* Start the DMA  engine */
651         tmp = cx_read(sram_ch->dma_ctl);
652         cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
653
654         dev->_audio_is_running = 1;
655         dev->_is_first_audio_frame = 1;
656
657         /* The fifo_en bit turns on by the first Risc program */
658         cx25821_wait_fifo_enable(dev, sram_ch);
659
660         return 0;
661
662 fail_irq:
663         cx25821_dev_unregister(dev);
664         return err;
665 }
666
667 int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
668 {
669         struct sram_channel *sram_ch;
670         int err = 0;
671
672         if (dev->_audio_is_running) {
673                 pr_warn("Audio Channel is still running so return!\n");
674                 return 0;
675         }
676
677         dev->_audio_upstream_channel = channel_select;
678         sram_ch = dev->channels[channel_select].sram_channels;
679
680         /* Work queue */
681         INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
682         dev->_irq_audio_queues =
683             create_singlethread_workqueue("cx25821_audioworkqueue");
684
685         if (!dev->_irq_audio_queues) {
686                 printk(KERN_DEBUG
687                         pr_fmt("ERROR: create_singlethread_workqueue() for Audio FAILED!\n"));
688                 return -ENOMEM;
689         }
690
691         dev->_last_index_irq = 0;
692         dev->_audio_is_running = 0;
693         dev->_audioframe_count = 0;
694         dev->_audiofile_status = RESET_STATUS;
695         dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
696         _line_size = AUDIO_LINE_SIZE;
697
698         if (dev->input_audiofilename) {
699                 dev->_audiofilename = kstrdup(dev->input_audiofilename,
700                                               GFP_KERNEL);
701
702                 if (!dev->_audiofilename) {
703                         err = -ENOMEM;
704                         goto error;
705                 }
706
707                 /* Default if filename is empty string */
708                 if (strcmp(dev->input_audiofilename, "") == 0)
709                         dev->_audiofilename = "/root/audioGOOD.wav";
710         } else {
711                 dev->_audiofilename = kstrdup(_defaultAudioName,
712                                               GFP_KERNEL);
713
714                 if (!dev->_audiofilename) {
715                         err = -ENOMEM;
716                         goto error;
717                 }
718         }
719
720         cx25821_sram_channel_setup_upstream_audio(dev, sram_ch,
721                                                   _line_size, 0);
722
723         dev->audio_upstream_riscbuf_size =
724                 AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
725                 RISC_SYNC_INSTRUCTION_SIZE;
726         dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
727
728         /* Allocating buffers and prepare RISC program */
729         err = cx25821_audio_upstream_buffer_prepare(dev, sram_ch,
730                                                         _line_size);
731         if (err < 0) {
732                 pr_err("%s: Failed to set up Audio upstream buffers!\n",
733                         dev->name);
734                 goto error;
735         }
736         /* Start RISC engine */
737         cx25821_start_audio_dma_upstream(dev, sram_ch);
738
739         return 0;
740
741 error:
742         cx25821_dev_unregister(dev);
743
744         return err;
745 }