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