Staging: Drop memory allocation cast
[safe/jmp/linux-2.6] / drivers / staging / 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 #include "cx25821-video.h"
24 #include "cx25821-audio-upstream.h"
25
26 #include <linux/fs.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/syscalls.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/delay.h>
35 #include <linux/slab.h>
36 #include <asm/uaccess.h>
37
38 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
39 MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
40 MODULE_LICENSE("GPL");
41
42 static int _intr_msk =
43     FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF | FLD_AUD_SRC_SYNC |
44     FLD_AUD_SRC_OPC_ERR;
45
46 int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
47                                               struct sram_channel *ch,
48                                               unsigned int bpl, u32 risc)
49 {
50         unsigned int i, lines;
51         u32 cdt;
52
53         if (ch->cmds_start == 0) {
54                 cx_write(ch->ptr1_reg, 0);
55                 cx_write(ch->ptr2_reg, 0);
56                 cx_write(ch->cnt2_reg, 0);
57                 cx_write(ch->cnt1_reg, 0);
58                 return 0;
59         }
60
61         bpl = (bpl + 7) & ~7;   /* alignment */
62         cdt = ch->cdt;
63         lines = ch->fifo_size / bpl;
64
65         if (lines > 3) {
66                 lines = 3;
67         }
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->sram_channels[dev->_audio_upstream_channel_select];
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 after the first 3 lines
120                 // For the upstream audio channel, the risc engine will enable the FIFO.
121                 if (fifo_enable && line == 2) {
122                         *(rp++) = RISC_WRITECR;
123                         *(rp++) = sram_ch->dma_ctl;
124                         *(rp++) = sram_ch->fld_aud_fifo_en;
125                         *(rp++) = 0x00000020;
126                 }
127
128                 offset += AUDIO_LINE_SIZE;
129         }
130
131         return rp;
132 }
133
134 int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
135                                        struct pci_dev *pci,
136                                        unsigned int bpl, unsigned int lines)
137 {
138         __le32 *rp;
139         int fifo_enable = 0;
140         int frame = 0, i = 0;
141         int frame_size = AUDIO_DATA_BUF_SZ;
142         int databuf_offset = 0;
143         int risc_flag = RISC_CNT_INC;
144         dma_addr_t risc_phys_jump_addr;
145
146         /* Virtual address of Risc buffer program */
147         rp = dev->_risc_virt_addr;
148
149         /* sync instruction */
150         *(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);
151
152         for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
153                 databuf_offset = frame_size * frame;
154
155                 if (frame == 0) {
156                         fifo_enable = 1;
157                         risc_flag = RISC_CNT_RESET;
158                 } else {
159                         fifo_enable = 0;
160                         risc_flag = RISC_CNT_INC;
161                 }
162
163                 //Calculate physical jump address
164                 if ((frame + 1) == NUM_AUDIO_FRAMES) {
165                         risc_phys_jump_addr =
166                             dev->_risc_phys_start_addr +
167                             RISC_SYNC_INSTRUCTION_SIZE;
168                 } else {
169                         risc_phys_jump_addr =
170                             dev->_risc_phys_start_addr +
171                             RISC_SYNC_INSTRUCTION_SIZE +
172                             AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
173                 }
174
175                 rp = cx25821_risc_field_upstream_audio(dev, rp,
176                                                        dev->
177                                                        _audiodata_buf_phys_addr
178                                                        + databuf_offset, bpl,
179                                                        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
187                 // Loop to (Nth)FrameRISC or to Start of Risc program & 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 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->sram_channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B];
220         u32 tmp = 0;
221
222         if (!dev->_audio_is_running) {
223                 printk
224                     ("cx25821: 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         if (dev->_irq_audio_queues) {
246                 kfree(dev->_irq_audio_queues);
247                 dev->_irq_audio_queues = NULL;
248         }
249
250         if (dev->_audiofilename != NULL)
251                 kfree(dev->_audiofilename);
252 }
253
254 void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
255 {
256         if (dev->_audio_is_running) {
257                 cx25821_stop_upstream_audio(dev);
258         }
259
260         cx25821_free_memory_audio(dev);
261 }
262
263 int cx25821_get_audio_data(struct cx25821_dev *dev,
264                            struct sram_channel *sram_ch)
265 {
266         struct file *myfile;
267         int frame_index_temp = dev->_audioframe_index;
268         int i = 0;
269         int line_size = AUDIO_LINE_SIZE;
270         int frame_size = AUDIO_DATA_BUF_SZ;
271         int frame_offset = frame_size * frame_index_temp;
272         ssize_t vfs_read_retval = 0;
273         char mybuf[line_size];
274         loff_t file_offset = dev->_audioframe_count * frame_size;
275         loff_t pos;
276         mm_segment_t old_fs;
277
278         if (dev->_audiofile_status == END_OF_FILE)
279                 return 0;
280
281         myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
282
283         if (IS_ERR(myfile)) {
284                 const int open_errno = -PTR_ERR(myfile);
285                 printk("%s(): ERROR opening file(%s) with errno = %d! \n",
286                        __func__, dev->_audiofilename, open_errno);
287                 return PTR_ERR(myfile);
288         } else {
289                 if (!(myfile->f_op)) {
290                         printk("%s: File has no file operations registered!\n",
291                                __func__);
292                         filp_close(myfile, NULL);
293                         return -EIO;
294                 }
295
296                 if (!myfile->f_op->read) {
297                         printk("%s: File has no READ operations registered! \n",
298                                __func__);
299                         filp_close(myfile, NULL);
300                         return -EIO;
301                 }
302
303                 pos = myfile->f_pos;
304                 old_fs = get_fs();
305                 set_fs(KERNEL_DS);
306
307                 for (i = 0; i < dev->_audio_lines_count; i++) {
308                         pos = file_offset;
309
310                         vfs_read_retval =
311                             vfs_read(myfile, mybuf, line_size, &pos);
312
313                         if (vfs_read_retval > 0 && vfs_read_retval == line_size
314                             && dev->_audiodata_buf_virt_addr != NULL) {
315                                 memcpy((void *)(dev->_audiodata_buf_virt_addr +
316                                                 frame_offset / 4), mybuf,
317                                        vfs_read_retval);
318                         }
319
320                         file_offset += vfs_read_retval;
321                         frame_offset += vfs_read_retval;
322
323                         if (vfs_read_retval < line_size) {
324                                 printk(KERN_INFO
325                                        "Done: exit %s() since no more bytes to read from Audio file.\n",
326                                        __func__);
327                                 break;
328                         }
329                 }
330
331                 if (i > 0)
332                         dev->_audioframe_count++;
333
334                 dev->_audiofile_status =
335                     (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
336
337                 set_fs(old_fs);
338                 filp_close(myfile, NULL);
339         }
340
341         return 0;
342 }
343
344 static void cx25821_audioups_handler(struct work_struct *work)
345 {
346         struct cx25821_dev *dev =
347             container_of(work, struct cx25821_dev, _audio_work_entry);
348
349         if (!dev) {
350                 printk("ERROR %s(): since container_of(work_struct) FAILED! \n",
351                        __func__);
352                 return;
353         }
354
355         cx25821_get_audio_data(dev,
356                                &dev->sram_channels[dev->
357                                                    _audio_upstream_channel_select]);
358 }
359
360 int cx25821_openfile_audio(struct cx25821_dev *dev,
361                            struct sram_channel *sram_ch)
362 {
363         struct file *myfile;
364         int i = 0, j = 0;
365         int line_size = AUDIO_LINE_SIZE;
366         ssize_t vfs_read_retval = 0;
367         char mybuf[line_size];
368         loff_t pos;
369         loff_t offset = (unsigned long)0;
370         mm_segment_t old_fs;
371
372         myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
373
374         if (IS_ERR(myfile)) {
375                 const int open_errno = -PTR_ERR(myfile);
376                 printk("%s(): ERROR opening file(%s) with errno = %d! \n",
377                        __func__, dev->_audiofilename, open_errno);
378                 return PTR_ERR(myfile);
379         } else {
380                 if (!(myfile->f_op)) {
381                         printk("%s: File has no file operations registered! \n",
382                                __func__);
383                         filp_close(myfile, NULL);
384                         return -EIO;
385                 }
386
387                 if (!myfile->f_op->read) {
388                         printk("%s: File has no READ operations registered! \n",
389                                __func__);
390                         filp_close(myfile, NULL);
391                         return -EIO;
392                 }
393
394                 pos = myfile->f_pos;
395                 old_fs = get_fs();
396                 set_fs(KERNEL_DS);
397
398                 for (j = 0; j < NUM_AUDIO_FRAMES; j++) {
399                         for (i = 0; i < dev->_audio_lines_count; i++) {
400                                 pos = offset;
401
402                                 vfs_read_retval =
403                                     vfs_read(myfile, mybuf, line_size, &pos);
404
405                                 if (vfs_read_retval > 0
406                                     && vfs_read_retval == line_size
407                                     && dev->_audiodata_buf_virt_addr != NULL) {
408                                         memcpy((void *)(dev->
409                                                         _audiodata_buf_virt_addr
410                                                         + offset / 4), mybuf,
411                                                vfs_read_retval);
412                                 }
413
414                                 offset += vfs_read_retval;
415
416                                 if (vfs_read_retval < line_size) {
417                                         printk(KERN_INFO
418                                                "Done: exit %s() since no more bytes to read from Audio file.\n",
419                                                __func__);
420                                         break;
421                                 }
422                         }
423
424                         if (i > 0) {
425                                 dev->_audioframe_count++;
426                         }
427
428                         if (vfs_read_retval < line_size) {
429                                 break;
430                         }
431                 }
432
433                 dev->_audiofile_status =
434                     (vfs_read_retval == line_size) ? IN_PROGRESS : END_OF_FILE;
435
436                 set_fs(old_fs);
437                 myfile->f_pos = 0;
438                 filp_close(myfile, NULL);
439         }
440
441         return 0;
442 }
443
444 static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
445                                                  struct sram_channel *sram_ch,
446                                                  int bpl)
447 {
448         int ret = 0;
449         dma_addr_t dma_addr;
450         dma_addr_t data_dma_addr;
451
452         cx25821_free_memory_audio(dev);
453
454         dev->_risc_virt_addr =
455             pci_alloc_consistent(dev->pci, dev->audio_upstream_riscbuf_size,
456                                  &dma_addr);
457         dev->_risc_virt_start_addr = dev->_risc_virt_addr;
458         dev->_risc_phys_start_addr = dma_addr;
459         dev->_risc_phys_addr = dma_addr;
460         dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
461
462         if (!dev->_risc_virt_addr) {
463                 printk
464                     ("cx25821 ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning.\n");
465                 return -ENOMEM;
466         }
467         //Clear out memory at address
468         memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
469
470         //For Audio Data buffer allocation
471         dev->_audiodata_buf_virt_addr =
472             pci_alloc_consistent(dev->pci, dev->audio_upstream_databuf_size,
473                                  &data_dma_addr);
474         dev->_audiodata_buf_phys_addr = data_dma_addr;
475         dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
476
477         if (!dev->_audiodata_buf_virt_addr) {
478                 printk
479                     ("cx25821 ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning. \n");
480                 return -ENOMEM;
481         }
482         //Clear out memory at address
483         memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
484
485         ret = cx25821_openfile_audio(dev, sram_ch);
486         if (ret < 0)
487                 return ret;
488
489         //Creating RISC programs
490         ret =
491             cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
492                                                dev->_audio_lines_count);
493         if (ret < 0) {
494                 printk(KERN_DEBUG
495                        "cx25821 ERROR creating audio upstream RISC programs! \n");
496                 goto error;
497         }
498
499         return 0;
500
501       error:
502         return ret;
503 }
504
505 int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
506                                u32 status)
507 {
508         int i = 0;
509         u32 int_msk_tmp;
510         struct sram_channel *channel = &dev->sram_channels[chan_num];
511         dma_addr_t risc_phys_jump_addr;
512         __le32 *rp;
513
514         if (status & FLD_AUD_SRC_RISCI1) {
515                 //Get interrupt_index of the program that interrupted
516                 u32 prog_cnt = cx_read(channel->gpcnt);
517
518                 //Since we've identified our IRQ, clear our bits from the interrupt mask and interrupt status registers
519                 cx_write(channel->int_msk, 0);
520                 cx_write(channel->int_stat, cx_read(channel->int_stat));
521
522                 spin_lock(&dev->slock);
523
524                 while (prog_cnt != dev->_last_index_irq) {
525                         //Update _last_index_irq
526                         if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1)) {
527                                 dev->_last_index_irq++;
528                         } else {
529                                 dev->_last_index_irq = 0;
530                         }
531
532                         dev->_audioframe_index = dev->_last_index_irq;
533
534                         queue_work(dev->_irq_audio_queues,
535                                    &dev->_audio_work_entry);
536                 }
537
538                 if (dev->_is_first_audio_frame) {
539                         dev->_is_first_audio_frame = 0;
540
541                         if (dev->_risc_virt_start_addr != NULL) {
542                                 risc_phys_jump_addr =
543                                     dev->_risc_phys_start_addr +
544                                     RISC_SYNC_INSTRUCTION_SIZE +
545                                     AUDIO_RISC_DMA_BUF_SIZE;
546
547                                 rp = cx25821_risc_field_upstream_audio(dev,
548                                                                        dev->
549                                                                        _risc_virt_start_addr
550                                                                        + 1,
551                                                                        dev->
552                                                                        _audiodata_buf_phys_addr,
553                                                                        AUDIO_LINE_SIZE,
554                                                                        FIFO_DISABLE);
555
556                                 if (USE_RISC_NOOP_AUDIO) {
557                                         for (i = 0; i < NUM_NO_OPS; i++) {
558                                                 *(rp++) =
559                                                     cpu_to_le32(RISC_NOOP);
560                                         }
561                                 }
562                                 // Jump to 2nd Audio Frame
563                                 *(rp++) =
564                                     cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
565                                                 RISC_CNT_RESET);
566                                 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
567                                 *(rp++) = cpu_to_le32(0);
568                         }
569                 }
570
571                 spin_unlock(&dev->slock);
572         } else {
573                 if (status & FLD_AUD_SRC_OF)
574                         printk("%s: Audio Received Overflow Error Interrupt!\n",
575                                __func__);
576
577                 if (status & FLD_AUD_SRC_SYNC)
578                         printk("%s: Audio Received Sync Error Interrupt!\n",
579                                __func__);
580
581                 if (status & FLD_AUD_SRC_OPC_ERR)
582                         printk("%s: Audio Received OpCode Error Interrupt!\n",
583                                __func__);
584
585                 // Read and write back the interrupt status register to clear our bits
586                 cx_write(channel->int_stat, cx_read(channel->int_stat));
587         }
588
589         if (dev->_audiofile_status == END_OF_FILE) {
590                 printk("cx25821: EOF Channel Audio Framecount = %d\n",
591                        dev->_audioframe_count);
592                 return -1;
593         }
594         //ElSE, set the interrupt mask register, re-enable irq.
595         int_msk_tmp = cx_read(channel->int_msk);
596         cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
597
598         return 0;
599 }
600
601 static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
602 {
603         struct cx25821_dev *dev = dev_id;
604         u32 msk_stat, audio_status;
605         int handled = 0;
606         struct sram_channel *sram_ch;
607
608         if (!dev)
609                 return -1;
610
611         sram_ch = &dev->sram_channels[dev->_audio_upstream_channel_select];
612
613         msk_stat = cx_read(sram_ch->int_mstat);
614         audio_status = cx_read(sram_ch->int_stat);
615
616         // Only deal with our interrupt
617         if (audio_status) {
618                 handled =
619                     cx25821_audio_upstream_irq(dev,
620                                                dev->
621                                                _audio_upstream_channel_select,
622                                                audio_status);
623         }
624
625         if (handled < 0) {
626                 cx25821_stop_upstream_audio(dev);
627         } else {
628                 handled += handled;
629         }
630
631         return IRQ_RETVAL(handled);
632 }
633
634 static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
635                                      struct sram_channel *sram_ch)
636 {
637         int count = 0;
638         u32 tmp;
639
640         do {
641                 //Wait 10 microsecond before checking to see if the FIFO is turned ON.
642                 udelay(10);
643
644                 tmp = cx_read(sram_ch->dma_ctl);
645
646                 if (count++ > 1000)     //10 millisecond timeout
647                 {
648                         printk
649                             ("cx25821 ERROR: %s() fifo is NOT turned on. Timeout!\n",
650                              __func__);
651                         return;
652                 }
653
654         } while (!(tmp & sram_ch->fld_aud_fifo_en));
655
656 }
657
658 int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
659                                      struct sram_channel *sram_ch)
660 {
661         u32 tmp = 0;
662         int err = 0;
663
664         // Set the physical start address of the RISC program in the initial program counter(IPC) member of the CMDS.
665         cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
666         cx_write(sram_ch->cmds_start + 4, 0);   /* Risc IPC High 64 bits 63-32 */
667
668         /* reset counter */
669         cx_write(sram_ch->gpcnt_ctl, 3);
670
671         //Set the line length       (It looks like we do not need to set the line length)
672         cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
673
674         //Set the input mode to 16-bit
675         tmp = cx_read(sram_ch->aud_cfg);
676         tmp |=
677             FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
678             FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D | FLD_AUD_SONY_MODE;
679         cx_write(sram_ch->aud_cfg, tmp);
680
681         // Read and write back the interrupt status register to clear it
682         tmp = cx_read(sram_ch->int_stat);
683         cx_write(sram_ch->int_stat, tmp);
684
685         // Clear our bits from the interrupt status register.
686         cx_write(sram_ch->int_stat, _intr_msk);
687
688         //Set the interrupt mask register, enable irq.
689         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
690         tmp = cx_read(sram_ch->int_msk);
691         cx_write(sram_ch->int_msk, tmp |= _intr_msk);
692
693         err =
694             request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
695                         IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
696         if (err < 0) {
697                 printk(KERN_ERR "%s: can't get upstream IRQ %d\n", dev->name,
698                        dev->pci->irq);
699                 goto fail_irq;
700         }
701
702         // Start the DMA  engine
703         tmp = cx_read(sram_ch->dma_ctl);
704         cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
705
706         dev->_audio_is_running = 1;
707         dev->_is_first_audio_frame = 1;
708
709         // The fifo_en bit turns on by the first Risc program
710         cx25821_wait_fifo_enable(dev, sram_ch);
711
712         return 0;
713
714       fail_irq:
715         cx25821_dev_unregister(dev);
716         return err;
717 }
718
719 int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
720 {
721         struct sram_channel *sram_ch;
722         int retval = 0;
723         int err = 0;
724         int str_length = 0;
725
726         if (dev->_audio_is_running) {
727                 printk("Audio Channel is still running so return!\n");
728                 return 0;
729         }
730
731         dev->_audio_upstream_channel_select = channel_select;
732         sram_ch = &dev->sram_channels[channel_select];
733
734         //Work queue
735         INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
736         dev->_irq_audio_queues =
737             create_singlethread_workqueue("cx25821_audioworkqueue");
738
739         if (!dev->_irq_audio_queues) {
740                 printk
741                     ("cx25821 ERROR: create_singlethread_workqueue() for Audio FAILED!\n");
742                 return -ENOMEM;
743         }
744
745         dev->_last_index_irq = 0;
746         dev->_audio_is_running = 0;
747         dev->_audioframe_count = 0;
748         dev->_audiofile_status = RESET_STATUS;
749         dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
750         _line_size = AUDIO_LINE_SIZE;
751
752         if (dev->input_audiofilename) {
753                 str_length = strlen(dev->input_audiofilename);
754                 dev->_audiofilename = kmalloc(str_length + 1, GFP_KERNEL);
755
756                 if (!dev->_audiofilename)
757                         goto error;
758
759                 memcpy(dev->_audiofilename, dev->input_audiofilename,
760                        str_length + 1);
761
762                 //Default if filename is empty string
763                 if (strcmp(dev->input_audiofilename, "") == 0) {
764                         dev->_audiofilename = "/root/audioGOOD.wav";
765                 }
766         } else {
767                 str_length = strlen(_defaultAudioName);
768                 dev->_audiofilename = kmalloc(str_length + 1, GFP_KERNEL);
769
770                 if (!dev->_audiofilename)
771                         goto error;
772
773                 memcpy(dev->_audiofilename, _defaultAudioName, str_length + 1);
774         }
775
776         retval =
777             cx25821_sram_channel_setup_upstream_audio(dev, sram_ch, _line_size,
778                                                       0);
779
780         dev->audio_upstream_riscbuf_size =
781             AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
782             RISC_SYNC_INSTRUCTION_SIZE;
783         dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
784
785         //Allocating buffers and prepare RISC program
786         retval =
787             cx25821_audio_upstream_buffer_prepare(dev, sram_ch, _line_size);
788         if (retval < 0) {
789                 printk(KERN_ERR
790                        "%s: Failed to set up Audio upstream buffers!\n",
791                        dev->name);
792                 goto error;
793         }
794         //Start RISC engine
795         cx25821_start_audio_dma_upstream(dev, sram_ch);
796
797         return 0;
798
799       error:
800         cx25821_dev_unregister(dev);
801
802         return err;
803 }