2 * ngene.c: nGene PCIe bridge driver
4 * Copyright (C) 2005-2007 Micronas
6 * Copyright (C) 2008-2009 Ralph Metzler <rjkm@metzlerbros.de>
7 * Modifications for new nGene firmware,
8 * support for EEPROM-copying,
9 * support for new dual DVB-S2 card prototype
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * version 2 only, as published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/poll.h>
35 #include <asm/div64.h>
36 #include <linux/pci.h>
37 #include <linux/timer.h>
38 #include <linux/byteorder/generic.h>
39 #include <linux/firmware.h>
40 #include <linux/vmalloc.h>
44 static int one_adapter
;
45 module_param(one_adapter
, int, 0444);
46 MODULE_PARM_DESC(one_adapter
, "Use only one adapter.");
48 static int shutdown_workaround
;
49 module_param(shutdown_workaround
, int, 0644);
50 MODULE_PARM_DESC(shutdown_workaround
, "Activate workaround for shutdown problem with some chipsets.");
53 module_param(debug
, int, 0444);
54 MODULE_PARM_DESC(debug
, "Print debugging information.");
56 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
58 #define dprintk if (debug) printk
60 #define ngwriteb(dat, adr) writeb((dat), (char *)(dev->iomem + (adr)))
61 #define ngwritel(dat, adr) writel((dat), (char *)(dev->iomem + (adr)))
62 #define ngwriteb(dat, adr) writeb((dat), (char *)(dev->iomem + (adr)))
63 #define ngreadl(adr) readl(dev->iomem + (adr))
64 #define ngreadb(adr) readb(dev->iomem + (adr))
65 #define ngcpyto(adr, src, count) memcpy_toio((char *) \
66 (dev->iomem + (adr)), (src), (count))
67 #define ngcpyfrom(dst, adr, count) memcpy_fromio((dst), (char *) \
68 (dev->iomem + (adr)), (count))
70 /****************************************************************************/
71 /* nGene interrupt handler **************************************************/
72 /****************************************************************************/
74 static void event_tasklet(unsigned long data
)
76 struct ngene
*dev
= (struct ngene
*)data
;
78 while (dev
->EventQueueReadIndex
!= dev
->EventQueueWriteIndex
) {
79 struct EVENT_BUFFER Event
=
80 dev
->EventQueue
[dev
->EventQueueReadIndex
];
81 dev
->EventQueueReadIndex
=
82 (dev
->EventQueueReadIndex
+ 1) & (EVENT_QUEUE_SIZE
- 1);
84 if ((Event
.UARTStatus
& 0x01) && (dev
->TxEventNotify
))
85 dev
->TxEventNotify(dev
, Event
.TimeStamp
);
86 if ((Event
.UARTStatus
& 0x02) && (dev
->RxEventNotify
))
87 dev
->RxEventNotify(dev
, Event
.TimeStamp
,
92 static void demux_tasklet(unsigned long data
)
94 struct ngene_channel
*chan
= (struct ngene_channel
*)data
;
95 struct SBufferHeader
*Cur
= chan
->nextBuffer
;
97 spin_lock_irq(&chan
->state_lock
);
99 while (Cur
->ngeneBuffer
.SR
.Flags
& 0x80) {
100 if (chan
->mode
& NGENE_IO_TSOUT
) {
101 u32 Flags
= chan
->DataFormatFlags
;
102 if (Cur
->ngeneBuffer
.SR
.Flags
& 0x20)
103 Flags
|= BEF_OVERFLOW
;
104 if (chan
->pBufferExchange
) {
105 if (!chan
->pBufferExchange(chan
,
107 chan
->Capture1Length
,
112 Clear in service flag to make sure we
113 get called on next interrupt again.
114 leave fill/empty (0x80) flag alone
115 to avoid hardware running out of
116 buffers during startup, we hold only
117 in run state ( the source may be late
121 if (chan
->HWState
== HWSTATE_RUN
) {
122 Cur
->ngeneBuffer
.SR
.Flags
&=
125 /* Stop processing stream */
128 /* We got a valid buffer,
129 so switch to run state */
130 chan
->HWState
= HWSTATE_RUN
;
133 printk(KERN_ERR DEVICE_NAME
": OOPS\n");
134 if (chan
->HWState
== HWSTATE_RUN
) {
135 Cur
->ngeneBuffer
.SR
.Flags
&= ~0x40;
136 break; /* Stop processing stream */
139 if (chan
->AudioDTOUpdated
) {
140 printk(KERN_INFO DEVICE_NAME
141 ": Update AudioDTO = %d\n",
142 chan
->AudioDTOValue
);
143 Cur
->ngeneBuffer
.SR
.DTOUpdate
=
145 chan
->AudioDTOUpdated
= 0;
148 if (chan
->HWState
== HWSTATE_RUN
) {
149 u32 Flags
= chan
->DataFormatFlags
;
150 IBufferExchange
*exch1
= chan
->pBufferExchange
;
151 IBufferExchange
*exch2
= chan
->pBufferExchange2
;
152 if (Cur
->ngeneBuffer
.SR
.Flags
& 0x01)
153 Flags
|= BEF_EVEN_FIELD
;
154 if (Cur
->ngeneBuffer
.SR
.Flags
& 0x20)
155 Flags
|= BEF_OVERFLOW
;
156 spin_unlock_irq(&chan
->state_lock
);
158 exch1(chan
, Cur
->Buffer1
,
159 chan
->Capture1Length
,
160 Cur
->ngeneBuffer
.SR
.Clock
,
163 exch2(chan
, Cur
->Buffer2
,
164 chan
->Capture2Length
,
165 Cur
->ngeneBuffer
.SR
.Clock
,
167 spin_lock_irq(&chan
->state_lock
);
168 } else if (chan
->HWState
!= HWSTATE_STOP
)
169 chan
->HWState
= HWSTATE_RUN
;
171 Cur
->ngeneBuffer
.SR
.Flags
= 0x00;
174 chan
->nextBuffer
= Cur
;
176 spin_unlock_irq(&chan
->state_lock
);
179 static irqreturn_t
irq_handler(int irq
, void *dev_id
)
181 struct ngene
*dev
= (struct ngene
*)dev_id
;
183 irqreturn_t rc
= IRQ_NONE
;
187 if (dev
->BootFirmware
) {
188 icounts
= ngreadl(NGENE_INT_COUNTS
);
189 if (icounts
!= dev
->icounts
) {
190 ngwritel(0, FORCE_NMI
);
192 wake_up(&dev
->cmd_wq
);
193 dev
->icounts
= icounts
;
199 ngwritel(0, FORCE_NMI
);
201 spin_lock(&dev
->cmd_lock
);
202 tmpCmdDoneByte
= dev
->CmdDoneByte
;
203 if (tmpCmdDoneByte
&&
205 (dev
->ngenetohost
[0] == 1 && dev
->ngenetohost
[1] != 0))) {
206 dev
->CmdDoneByte
= NULL
;
208 wake_up(&dev
->cmd_wq
);
211 spin_unlock(&dev
->cmd_lock
);
213 if (dev
->EventBuffer
->EventStatus
& 0x80) {
215 (dev
->EventQueueWriteIndex
+ 1) &
216 (EVENT_QUEUE_SIZE
- 1);
217 if (nextWriteIndex
!= dev
->EventQueueReadIndex
) {
218 dev
->EventQueue
[dev
->EventQueueWriteIndex
] =
220 dev
->EventQueueWriteIndex
= nextWriteIndex
;
222 printk(KERN_ERR DEVICE_NAME
": event overflow\n");
223 dev
->EventQueueOverflowCount
+= 1;
224 dev
->EventQueueOverflowFlag
= 1;
226 dev
->EventBuffer
->EventStatus
&= ~0x80;
227 tasklet_schedule(&dev
->event_tasklet
);
233 spin_lock(&dev
->channel
[i
].state_lock
);
234 /* if (dev->channel[i].State>=KSSTATE_RUN) { */
235 if (dev
->channel
[i
].nextBuffer
) {
236 if ((dev
->channel
[i
].nextBuffer
->
237 ngeneBuffer
.SR
.Flags
& 0xC0) == 0x80) {
238 dev
->channel
[i
].nextBuffer
->
239 ngeneBuffer
.SR
.Flags
|= 0x40;
241 &dev
->channel
[i
].demux_tasklet
);
245 spin_unlock(&dev
->channel
[i
].state_lock
);
248 /* Request might have been processed by a previous call. */
252 /****************************************************************************/
253 /* nGene command interface **************************************************/
254 /****************************************************************************/
256 static void dump_command_io(struct ngene
*dev
)
260 ngcpyfrom(buf
, HOST_TO_NGENE
, 8);
261 printk(KERN_ERR
"host_to_ngene (%04x): %02x %02x %02x %02x %02x %02x %02x %02x\n",
262 HOST_TO_NGENE
, buf
[0], buf
[1], buf
[2], buf
[3],
263 buf
[4], buf
[5], buf
[6], buf
[7]);
265 ngcpyfrom(buf
, NGENE_TO_HOST
, 8);
266 printk(KERN_ERR
"ngene_to_host (%04x): %02x %02x %02x %02x %02x %02x %02x %02x\n",
267 NGENE_TO_HOST
, buf
[0], buf
[1], buf
[2], buf
[3],
268 buf
[4], buf
[5], buf
[6], buf
[7]);
270 b
= dev
->hosttongene
;
271 printk(KERN_ERR
"dev->hosttongene (%p): %02x %02x %02x %02x %02x %02x %02x %02x\n",
272 b
, b
[0], b
[1], b
[2], b
[3], b
[4], b
[5], b
[6], b
[7]);
274 b
= dev
->ngenetohost
;
275 printk(KERN_ERR
"dev->ngenetohost (%p): %02x %02x %02x %02x %02x %02x %02x %02x\n",
276 b
, b
[0], b
[1], b
[2], b
[3], b
[4], b
[5], b
[6], b
[7]);
279 static int ngene_command_mutex(struct ngene
*dev
, struct ngene_command
*com
)
286 if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_PREPARE
) {
287 dev
->BootFirmware
= 1;
288 dev
->icounts
= ngreadl(NGENE_INT_COUNTS
);
289 ngwritel(0, NGENE_COMMAND
);
290 ngwritel(0, NGENE_COMMAND_HI
);
291 ngwritel(0, NGENE_STATUS
);
292 ngwritel(0, NGENE_STATUS_HI
);
293 ngwritel(0, NGENE_EVENT
);
294 ngwritel(0, NGENE_EVENT_HI
);
295 } else if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_FINISH
) {
296 u64 fwio
= dev
->PAFWInterfaceBuffer
;
298 ngwritel(fwio
& 0xffffffff, NGENE_COMMAND
);
299 ngwritel(fwio
>> 32, NGENE_COMMAND_HI
);
300 ngwritel((fwio
+ 256) & 0xffffffff, NGENE_STATUS
);
301 ngwritel((fwio
+ 256) >> 32, NGENE_STATUS_HI
);
302 ngwritel((fwio
+ 512) & 0xffffffff, NGENE_EVENT
);
303 ngwritel((fwio
+ 512) >> 32, NGENE_EVENT_HI
);
306 memcpy(dev
->FWInterfaceBuffer
, com
->cmd
.raw8
, com
->in_len
+ 2);
308 if (dev
->BootFirmware
)
309 ngcpyto(HOST_TO_NGENE
, com
->cmd
.raw8
, com
->in_len
+ 2);
311 spin_lock_irq(&dev
->cmd_lock
);
312 tmpCmdDoneByte
= dev
->ngenetohost
+ com
->out_len
;
316 dev
->ngenetohost
[0] = 0;
317 dev
->ngenetohost
[1] = 0;
318 dev
->CmdDoneByte
= tmpCmdDoneByte
;
319 spin_unlock_irq(&dev
->cmd_lock
);
322 ngwritel(1, FORCE_INT
);
324 ret
= wait_event_timeout(dev
->cmd_wq
, dev
->cmd_done
== 1, 2 * HZ
);
326 /*ngwritel(0, FORCE_NMI);*/
328 printk(KERN_ERR DEVICE_NAME
329 ": Command timeout cmd=%02x prev=%02x\n",
330 com
->cmd
.hdr
.Opcode
, dev
->prev_cmd
);
331 dump_command_io(dev
);
334 if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_FINISH
)
335 dev
->BootFirmware
= 0;
337 dev
->prev_cmd
= com
->cmd
.hdr
.Opcode
;
342 memcpy(com
->cmd
.raw8
, dev
->ngenetohost
, com
->out_len
);
347 int ngene_command(struct ngene
*dev
, struct ngene_command
*com
)
351 down(&dev
->cmd_mutex
);
352 result
= ngene_command_mutex(dev
, com
);
358 static int ngene_command_load_firmware(struct ngene
*dev
,
359 u8
*ngene_fw
, u32 size
)
361 #define FIRSTCHUNK (1024)
363 struct ngene_command com
;
365 com
.cmd
.hdr
.Opcode
= CMD_FWLOAD_PREPARE
;
366 com
.cmd
.hdr
.Length
= 0;
370 ngene_command(dev
, &com
);
372 cleft
= (size
+ 3) & ~3;
373 if (cleft
> FIRSTCHUNK
) {
374 ngcpyto(PROGRAM_SRAM
+ FIRSTCHUNK
, ngene_fw
+ FIRSTCHUNK
,
378 ngcpyto(DATA_FIFO_AREA
, ngene_fw
, cleft
);
380 memset(&com
, 0, sizeof(struct ngene_command
));
381 com
.cmd
.hdr
.Opcode
= CMD_FWLOAD_FINISH
;
382 com
.cmd
.hdr
.Length
= 4;
383 com
.cmd
.FWLoadFinish
.Address
= DATA_FIFO_AREA
;
384 com
.cmd
.FWLoadFinish
.Length
= (unsigned short)cleft
;
388 return ngene_command(dev
, &com
);
392 static int ngene_command_config_buf(struct ngene
*dev
, u8 config
)
394 struct ngene_command com
;
396 com
.cmd
.hdr
.Opcode
= CMD_CONFIGURE_BUFFER
;
397 com
.cmd
.hdr
.Length
= 1;
398 com
.cmd
.ConfigureBuffers
.config
= config
;
402 if (ngene_command(dev
, &com
) < 0)
407 static int ngene_command_config_free_buf(struct ngene
*dev
, u8
*config
)
409 struct ngene_command com
;
411 com
.cmd
.hdr
.Opcode
= CMD_CONFIGURE_FREE_BUFFER
;
412 com
.cmd
.hdr
.Length
= 6;
413 memcpy(&com
.cmd
.ConfigureBuffers
.config
, config
, 6);
417 if (ngene_command(dev
, &com
) < 0)
423 int ngene_command_gpio_set(struct ngene
*dev
, u8 select
, u8 level
)
425 struct ngene_command com
;
427 com
.cmd
.hdr
.Opcode
= CMD_SET_GPIO_PIN
;
428 com
.cmd
.hdr
.Length
= 1;
429 com
.cmd
.SetGpioPin
.select
= select
| (level
<< 7);
433 return ngene_command(dev
, &com
);
438 02000640 is sample on rising edge.
439 02000740 is sample on falling edge.
440 02000040 is ignore "valid" signal
442 0: FD_CTL1 Bit 7,6 must be 0,1
443 7 disable(fw controlled)
448 1,0 0-no sync, 1-use ext. start, 2-use 0x47, 3-both
449 1: FD_CTL2 has 3-valid must be hi, 2-use valid, 1-edge
450 2: FD_STA is read-only. 0-sync
451 3: FD_INSYNC is number of 47s to trigger "in sync".
452 4: FD_OUTSYNC is number of 47s to trigger "out of sync".
453 5: FD_MAXBYTE1 is low-order of bytes per packet.
454 6: FD_MAXBYTE2 is high-order of bytes per packet.
455 7: Top byte is unused.
458 /****************************************************************************/
460 static u8 TSFeatureDecoderSetup
[8 * 5] = {
461 0x42, 0x00, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00,
462 0x40, 0x06, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* DRXH */
463 0x71, 0x07, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* DRXHser */
464 0x72, 0x00, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* S2ser */
465 0x40, 0x07, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* LGDT3303 */
468 /* Set NGENE I2S Config to 16 bit packed */
469 static u8 I2SConfiguration
[] = {
470 0x00, 0x10, 0x00, 0x00,
471 0x80, 0x10, 0x00, 0x00,
474 static u8 SPDIFConfiguration
[10] = {
475 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
478 /* Set NGENE I2S Config to transport stream compatible mode */
480 static u8 TS_I2SConfiguration
[4] = { 0x3E, 0x18, 0x00, 0x00 };
482 static u8 TS_I2SOutConfiguration
[4] = { 0x80, 0x04, 0x00, 0x00 };
484 static u8 ITUDecoderSetup
[4][16] = {
485 {0x1c, 0x13, 0x01, 0x68, 0x3d, 0x90, 0x14, 0x20, /* SDTV */
486 0x00, 0x00, 0x01, 0xb0, 0x9c, 0x00, 0x00, 0x00},
487 {0x9c, 0x03, 0x23, 0xC0, 0x60, 0x0E, 0x13, 0x00,
488 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
489 {0x9f, 0x00, 0x23, 0xC0, 0x60, 0x0F, 0x13, 0x00, /* HDTV 1080i50 */
490 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
491 {0x9c, 0x01, 0x23, 0xC0, 0x60, 0x0E, 0x13, 0x00, /* HDTV 1080i60 */
492 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
497 * 27p50 9f 00 22 80 42 69 18 ...
498 * 27p60 93 00 22 80 82 69 1c ...
501 /* Maxbyte to 1144 (for raw data) */
502 static u8 ITUFeatureDecoderSetup
[8] = {
503 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x04, 0x00
506 void FillTSBuffer(void *Buffer
, int Length
, u32 Flags
)
510 memset(Buffer
, TS_FILLER
, Length
);
512 if (Flags
& DF_SWAP32
)
522 static void flush_buffers(struct ngene_channel
*chan
)
528 spin_lock_irq(&chan
->state_lock
);
529 val
= chan
->nextBuffer
->ngeneBuffer
.SR
.Flags
& 0x80;
530 spin_unlock_irq(&chan
->state_lock
);
534 static void clear_buffers(struct ngene_channel
*chan
)
536 struct SBufferHeader
*Cur
= chan
->nextBuffer
;
539 memset(&Cur
->ngeneBuffer
.SR
, 0, sizeof(Cur
->ngeneBuffer
.SR
));
540 if (chan
->mode
& NGENE_IO_TSOUT
)
541 FillTSBuffer(Cur
->Buffer1
,
542 chan
->Capture1Length
,
543 chan
->DataFormatFlags
);
545 } while (Cur
!= chan
->nextBuffer
);
547 if (chan
->mode
& NGENE_IO_TSOUT
) {
548 chan
->nextBuffer
->ngeneBuffer
.SR
.DTOUpdate
=
550 chan
->AudioDTOUpdated
= 0;
552 Cur
= chan
->TSIdleBuffer
.Head
;
555 memset(&Cur
->ngeneBuffer
.SR
, 0,
556 sizeof(Cur
->ngeneBuffer
.SR
));
557 FillTSBuffer(Cur
->Buffer1
,
558 chan
->Capture1Length
,
559 chan
->DataFormatFlags
);
561 } while (Cur
!= chan
->TSIdleBuffer
.Head
);
565 static int ngene_command_stream_control(struct ngene
*dev
, u8 stream
,
566 u8 control
, u8 mode
, u8 flags
)
568 struct ngene_channel
*chan
= &dev
->channel
[stream
];
569 struct ngene_command com
;
570 u16 BsUVI
= ((stream
& 1) ? 0x9400 : 0x9300);
571 u16 BsSDI
= ((stream
& 1) ? 0x9600 : 0x9500);
572 u16 BsSPI
= ((stream
& 1) ? 0x9800 : 0x9700);
575 down(&dev
->stream_mutex
);
576 memset(&com
, 0, sizeof(com
));
577 com
.cmd
.hdr
.Opcode
= CMD_CONTROL
;
578 com
.cmd
.hdr
.Length
= sizeof(struct FW_STREAM_CONTROL
) - 2;
579 com
.cmd
.StreamControl
.Stream
= stream
| (control
? 8 : 0);
580 if (chan
->mode
& NGENE_IO_TSOUT
)
581 com
.cmd
.StreamControl
.Stream
|= 0x07;
582 com
.cmd
.StreamControl
.Control
= control
|
583 (flags
& SFLAG_ORDER_LUMA_CHROMA
);
584 com
.cmd
.StreamControl
.Mode
= mode
;
585 com
.in_len
= sizeof(struct FW_STREAM_CONTROL
);
588 dprintk(KERN_INFO DEVICE_NAME
589 ": Stream=%02x, Control=%02x, Mode=%02x\n",
590 com
.cmd
.StreamControl
.Stream
, com
.cmd
.StreamControl
.Control
,
591 com
.cmd
.StreamControl
.Mode
);
595 if (!(control
& 0x80)) {
596 spin_lock_irq(&chan
->state_lock
);
597 if (chan
->State
== KSSTATE_RUN
) {
598 chan
->State
= KSSTATE_ACQUIRE
;
599 chan
->HWState
= HWSTATE_STOP
;
600 spin_unlock_irq(&chan
->state_lock
);
601 if (ngene_command(dev
, &com
) < 0) {
602 up(&dev
->stream_mutex
);
605 /* clear_buffers(chan); */
607 up(&dev
->stream_mutex
);
610 spin_unlock_irq(&chan
->state_lock
);
611 up(&dev
->stream_mutex
);
615 if (mode
& SMODE_AUDIO_CAPTURE
) {
616 com
.cmd
.StreamControl
.CaptureBlockCount
=
617 chan
->Capture1Length
/ AUDIO_BLOCK_SIZE
;
618 com
.cmd
.StreamControl
.Buffer_Address
= chan
->RingBuffer
.PAHead
;
619 } else if (mode
& SMODE_TRANSPORT_STREAM
) {
620 com
.cmd
.StreamControl
.CaptureBlockCount
=
621 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
622 com
.cmd
.StreamControl
.MaxLinesPerField
=
623 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
624 com
.cmd
.StreamControl
.Buffer_Address
=
625 chan
->TSRingBuffer
.PAHead
;
626 if (chan
->mode
& NGENE_IO_TSOUT
) {
627 com
.cmd
.StreamControl
.BytesPerVBILine
=
628 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
629 com
.cmd
.StreamControl
.Stream
|= 0x07;
632 com
.cmd
.StreamControl
.BytesPerVideoLine
= chan
->nBytesPerLine
;
633 com
.cmd
.StreamControl
.MaxLinesPerField
= chan
->nLines
;
634 com
.cmd
.StreamControl
.MinLinesPerField
= 100;
635 com
.cmd
.StreamControl
.Buffer_Address
= chan
->RingBuffer
.PAHead
;
637 if (mode
& SMODE_VBI_CAPTURE
) {
638 com
.cmd
.StreamControl
.MaxVBILinesPerField
=
640 com
.cmd
.StreamControl
.MinVBILinesPerField
= 0;
641 com
.cmd
.StreamControl
.BytesPerVBILine
=
642 chan
->nBytesPerVBILine
;
644 if (flags
& SFLAG_COLORBAR
)
645 com
.cmd
.StreamControl
.Stream
|= 0x04;
648 spin_lock_irq(&chan
->state_lock
);
649 if (mode
& SMODE_AUDIO_CAPTURE
) {
650 chan
->nextBuffer
= chan
->RingBuffer
.Head
;
651 if (mode
& SMODE_AUDIO_SPDIF
) {
652 com
.cmd
.StreamControl
.SetupDataLen
=
653 sizeof(SPDIFConfiguration
);
654 com
.cmd
.StreamControl
.SetupDataAddr
= BsSPI
;
655 memcpy(com
.cmd
.StreamControl
.SetupData
,
656 SPDIFConfiguration
, sizeof(SPDIFConfiguration
));
658 com
.cmd
.StreamControl
.SetupDataLen
= 4;
659 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDI
;
660 memcpy(com
.cmd
.StreamControl
.SetupData
,
662 4 * dev
->card_info
->i2s
[stream
], 4);
664 } else if (mode
& SMODE_TRANSPORT_STREAM
) {
665 chan
->nextBuffer
= chan
->TSRingBuffer
.Head
;
666 if (stream
>= STREAM_AUDIOIN1
) {
667 if (chan
->mode
& NGENE_IO_TSOUT
) {
668 com
.cmd
.StreamControl
.SetupDataLen
=
669 sizeof(TS_I2SOutConfiguration
);
670 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDO
;
671 memcpy(com
.cmd
.StreamControl
.SetupData
,
672 TS_I2SOutConfiguration
,
673 sizeof(TS_I2SOutConfiguration
));
675 com
.cmd
.StreamControl
.SetupDataLen
=
676 sizeof(TS_I2SConfiguration
);
677 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDI
;
678 memcpy(com
.cmd
.StreamControl
.SetupData
,
680 sizeof(TS_I2SConfiguration
));
683 com
.cmd
.StreamControl
.SetupDataLen
= 8;
684 com
.cmd
.StreamControl
.SetupDataAddr
= BsUVI
+ 0x10;
685 memcpy(com
.cmd
.StreamControl
.SetupData
,
686 TSFeatureDecoderSetup
+
687 8 * dev
->card_info
->tsf
[stream
], 8);
690 chan
->nextBuffer
= chan
->RingBuffer
.Head
;
691 com
.cmd
.StreamControl
.SetupDataLen
=
692 16 + sizeof(ITUFeatureDecoderSetup
);
693 com
.cmd
.StreamControl
.SetupDataAddr
= BsUVI
;
694 memcpy(com
.cmd
.StreamControl
.SetupData
,
695 ITUDecoderSetup
[chan
->itumode
], 16);
696 memcpy(com
.cmd
.StreamControl
.SetupData
+ 16,
697 ITUFeatureDecoderSetup
, sizeof(ITUFeatureDecoderSetup
));
700 chan
->State
= KSSTATE_RUN
;
701 if (mode
& SMODE_TRANSPORT_STREAM
)
702 chan
->HWState
= HWSTATE_RUN
;
704 chan
->HWState
= HWSTATE_STARTUP
;
705 spin_unlock_irq(&chan
->state_lock
);
707 if (ngene_command(dev
, &com
) < 0) {
708 up(&dev
->stream_mutex
);
711 up(&dev
->stream_mutex
);
715 void set_transfer(struct ngene_channel
*chan
, int state
)
717 u8 control
= 0, mode
= 0, flags
= 0;
718 struct ngene
*dev
= chan
->dev
;
722 printk(KERN_INFO DEVICE_NAME ": st %d\n", state);
728 printk(KERN_INFO DEVICE_NAME
": already running\n");
732 if (!chan
->running
) {
733 printk(KERN_INFO DEVICE_NAME
": already stopped\n");
738 if (dev
->card_info
->switch_ctrl
)
739 dev
->card_info
->switch_ctrl(chan
, 1, state
^ 1);
742 spin_lock_irq(&chan
->state_lock
);
744 /* printk(KERN_INFO DEVICE_NAME ": lock=%08x\n",
746 dvb_ringbuffer_flush(&dev
->tsout_rbuf
);
748 if (chan
->mode
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
749 chan
->Capture1Length
= 512 * 188;
750 mode
= SMODE_TRANSPORT_STREAM
;
752 if (chan
->mode
& NGENE_IO_TSOUT
) {
753 chan
->pBufferExchange
= tsout_exchange
;
754 /* 0x66666666 = 50MHz *2^33 /250MHz */
755 chan
->AudioDTOValue
= 0x80000000;
756 chan
->AudioDTOUpdated
= 1;
758 if (chan
->mode
& NGENE_IO_TSIN
)
759 chan
->pBufferExchange
= tsin_exchange
;
760 spin_unlock_irq(&chan
->state_lock
);
762 ;/* printk(KERN_INFO DEVICE_NAME ": lock=%08x\n",
765 ret
= ngene_command_stream_control(dev
, chan
->number
,
766 control
, mode
, flags
);
768 chan
->running
= state
;
770 printk(KERN_ERR DEVICE_NAME
": set_transfer %d failed\n",
773 spin_lock_irq(&chan
->state_lock
);
774 chan
->pBufferExchange
= NULL
;
775 dvb_ringbuffer_flush(&dev
->tsout_rbuf
);
776 spin_unlock_irq(&chan
->state_lock
);
781 /****************************************************************************/
782 /* nGene hardware init and release functions ********************************/
783 /****************************************************************************/
785 static void free_ringbuffer(struct ngene
*dev
, struct SRingBufferDescriptor
*rb
)
787 struct SBufferHeader
*Cur
= rb
->Head
;
793 for (j
= 0; j
< rb
->NumBuffers
; j
++, Cur
= Cur
->Next
) {
795 pci_free_consistent(dev
->pci_dev
,
798 Cur
->scList1
->Address
);
801 pci_free_consistent(dev
->pci_dev
,
804 Cur
->scList2
->Address
);
808 pci_free_consistent(dev
->pci_dev
, rb
->SCListMemSize
,
809 rb
->SCListMem
, rb
->PASCListMem
);
811 pci_free_consistent(dev
->pci_dev
, rb
->MemSize
, rb
->Head
, rb
->PAHead
);
814 static void free_idlebuffer(struct ngene
*dev
,
815 struct SRingBufferDescriptor
*rb
,
816 struct SRingBufferDescriptor
*tb
)
819 struct SBufferHeader
*Cur
= tb
->Head
;
823 free_ringbuffer(dev
, rb
);
824 for (j
= 0; j
< tb
->NumBuffers
; j
++, Cur
= Cur
->Next
) {
827 Cur
->ngeneBuffer
.Address_of_first_entry_2
= 0;
828 Cur
->ngeneBuffer
.Number_of_entries_2
= 0;
832 static void free_common_buffers(struct ngene
*dev
)
835 struct ngene_channel
*chan
;
837 for (i
= STREAM_VIDEOIN1
; i
< MAX_STREAM
; i
++) {
838 chan
= &dev
->channel
[i
];
839 free_idlebuffer(dev
, &chan
->TSIdleBuffer
, &chan
->TSRingBuffer
);
840 free_ringbuffer(dev
, &chan
->RingBuffer
);
841 free_ringbuffer(dev
, &chan
->TSRingBuffer
);
844 if (dev
->OverflowBuffer
)
845 pci_free_consistent(dev
->pci_dev
,
846 OVERFLOW_BUFFER_SIZE
,
847 dev
->OverflowBuffer
, dev
->PAOverflowBuffer
);
849 if (dev
->FWInterfaceBuffer
)
850 pci_free_consistent(dev
->pci_dev
,
852 dev
->FWInterfaceBuffer
,
853 dev
->PAFWInterfaceBuffer
);
856 /****************************************************************************/
857 /* Ring buffer handling *****************************************************/
858 /****************************************************************************/
860 static int create_ring_buffer(struct pci_dev
*pci_dev
,
861 struct SRingBufferDescriptor
*descr
, u32 NumBuffers
)
864 struct SBufferHeader
*Head
;
866 u32 MemSize
= SIZEOF_SBufferHeader
* NumBuffers
;
867 u64 PARingBufferHead
;
869 u64 PARingBufferNext
;
870 struct SBufferHeader
*Cur
, *Next
;
875 descr
->NumBuffers
= 0;
880 Head
= pci_alloc_consistent(pci_dev
, MemSize
, &tmp
);
881 PARingBufferHead
= tmp
;
886 memset(Head
, 0, MemSize
);
888 PARingBufferCur
= PARingBufferHead
;
891 for (i
= 0; i
< NumBuffers
- 1; i
++) {
892 Next
= (struct SBufferHeader
*)
893 (((u8
*) Cur
) + SIZEOF_SBufferHeader
);
894 PARingBufferNext
= PARingBufferCur
+ SIZEOF_SBufferHeader
;
896 Cur
->ngeneBuffer
.Next
= PARingBufferNext
;
898 PARingBufferCur
= PARingBufferNext
;
900 /* Last Buffer points back to first one */
902 Cur
->ngeneBuffer
.Next
= PARingBufferHead
;
905 descr
->MemSize
= MemSize
;
906 descr
->PAHead
= PARingBufferHead
;
907 descr
->NumBuffers
= NumBuffers
;
912 static int AllocateRingBuffers(struct pci_dev
*pci_dev
,
914 struct SRingBufferDescriptor
*pRingBuffer
,
915 u32 Buffer1Length
, u32 Buffer2Length
)
920 u32 SCListMemSize
= pRingBuffer
->NumBuffers
921 * ((Buffer2Length
!= 0) ? (NUM_SCATTER_GATHER_ENTRIES
* 2) :
922 NUM_SCATTER_GATHER_ENTRIES
)
923 * sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
926 struct HW_SCATTER_GATHER_ELEMENT
*SCListEntry
;
928 struct SBufferHeader
*Cur
;
931 if (SCListMemSize
< 4096)
932 SCListMemSize
= 4096;
934 SCListMem
= pci_alloc_consistent(pci_dev
, SCListMemSize
, &tmp
);
937 if (SCListMem
== NULL
)
940 memset(SCListMem
, 0, SCListMemSize
);
942 pRingBuffer
->SCListMem
= SCListMem
;
943 pRingBuffer
->PASCListMem
= PASCListMem
;
944 pRingBuffer
->SCListMemSize
= SCListMemSize
;
945 pRingBuffer
->Buffer1Length
= Buffer1Length
;
946 pRingBuffer
->Buffer2Length
= Buffer2Length
;
948 SCListEntry
= SCListMem
;
949 PASCListEntry
= PASCListMem
;
950 Cur
= pRingBuffer
->Head
;
952 for (i
= 0; i
< pRingBuffer
->NumBuffers
; i
+= 1, Cur
= Cur
->Next
) {
955 void *Buffer
= pci_alloc_consistent(pci_dev
, Buffer1Length
,
962 Cur
->Buffer1
= Buffer
;
964 SCListEntry
->Address
= PABuffer
;
965 SCListEntry
->Length
= Buffer1Length
;
967 Cur
->scList1
= SCListEntry
;
968 Cur
->ngeneBuffer
.Address_of_first_entry_1
= PASCListEntry
;
969 Cur
->ngeneBuffer
.Number_of_entries_1
=
970 NUM_SCATTER_GATHER_ENTRIES
;
973 PASCListEntry
+= sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
975 #if NUM_SCATTER_GATHER_ENTRIES > 1
976 for (j
= 0; j
< NUM_SCATTER_GATHER_ENTRIES
- 1; j
+= 1) {
977 SCListEntry
->Address
= of
;
978 SCListEntry
->Length
= OVERFLOW_BUFFER_SIZE
;
981 sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
988 Buffer
= pci_alloc_consistent(pci_dev
, Buffer2Length
, &tmp
);
994 Cur
->Buffer2
= Buffer
;
996 SCListEntry
->Address
= PABuffer
;
997 SCListEntry
->Length
= Buffer2Length
;
999 Cur
->scList2
= SCListEntry
;
1000 Cur
->ngeneBuffer
.Address_of_first_entry_2
= PASCListEntry
;
1001 Cur
->ngeneBuffer
.Number_of_entries_2
=
1002 NUM_SCATTER_GATHER_ENTRIES
;
1005 PASCListEntry
+= sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
1007 #if NUM_SCATTER_GATHER_ENTRIES > 1
1008 for (j
= 0; j
< NUM_SCATTER_GATHER_ENTRIES
- 1; j
++) {
1009 SCListEntry
->Address
= of
;
1010 SCListEntry
->Length
= OVERFLOW_BUFFER_SIZE
;
1013 sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
1022 static int FillTSIdleBuffer(struct SRingBufferDescriptor
*pIdleBuffer
,
1023 struct SRingBufferDescriptor
*pRingBuffer
)
1027 /* Copy pointer to scatter gather list in TSRingbuffer
1028 structure for buffer 2
1029 Load number of buffer
1031 u32 n
= pRingBuffer
->NumBuffers
;
1033 /* Point to first buffer entry */
1034 struct SBufferHeader
*Cur
= pRingBuffer
->Head
;
1036 /* Loop thru all buffer and set Buffer 2 pointers to TSIdlebuffer */
1037 for (i
= 0; i
< n
; i
++) {
1038 Cur
->Buffer2
= pIdleBuffer
->Head
->Buffer1
;
1039 Cur
->scList2
= pIdleBuffer
->Head
->scList1
;
1040 Cur
->ngeneBuffer
.Address_of_first_entry_2
=
1041 pIdleBuffer
->Head
->ngeneBuffer
.
1042 Address_of_first_entry_1
;
1043 Cur
->ngeneBuffer
.Number_of_entries_2
=
1044 pIdleBuffer
->Head
->ngeneBuffer
.Number_of_entries_1
;
1050 static u32 RingBufferSizes
[MAX_STREAM
] = {
1058 static u32 Buffer1Sizes
[MAX_STREAM
] = {
1059 MAX_VIDEO_BUFFER_SIZE
,
1060 MAX_VIDEO_BUFFER_SIZE
,
1061 MAX_AUDIO_BUFFER_SIZE
,
1062 MAX_AUDIO_BUFFER_SIZE
,
1063 MAX_AUDIO_BUFFER_SIZE
1066 static u32 Buffer2Sizes
[MAX_STREAM
] = {
1067 MAX_VBI_BUFFER_SIZE
,
1068 MAX_VBI_BUFFER_SIZE
,
1075 static int AllocCommonBuffers(struct ngene
*dev
)
1079 dev
->FWInterfaceBuffer
= pci_alloc_consistent(dev
->pci_dev
, 4096,
1080 &dev
->PAFWInterfaceBuffer
);
1081 if (!dev
->FWInterfaceBuffer
)
1083 dev
->hosttongene
= dev
->FWInterfaceBuffer
;
1084 dev
->ngenetohost
= dev
->FWInterfaceBuffer
+ 256;
1085 dev
->EventBuffer
= dev
->FWInterfaceBuffer
+ 512;
1087 dev
->OverflowBuffer
= pci_alloc_consistent(dev
->pci_dev
,
1088 OVERFLOW_BUFFER_SIZE
,
1089 &dev
->PAOverflowBuffer
);
1090 if (!dev
->OverflowBuffer
)
1092 memset(dev
->OverflowBuffer
, 0, OVERFLOW_BUFFER_SIZE
);
1094 for (i
= STREAM_VIDEOIN1
; i
< MAX_STREAM
; i
++) {
1095 int type
= dev
->card_info
->io_type
[i
];
1097 dev
->channel
[i
].State
= KSSTATE_STOP
;
1099 if (type
& (NGENE_IO_TV
| NGENE_IO_HDTV
| NGENE_IO_AIN
)) {
1100 status
= create_ring_buffer(dev
->pci_dev
,
1101 &dev
->channel
[i
].RingBuffer
,
1102 RingBufferSizes
[i
]);
1106 if (type
& (NGENE_IO_TV
| NGENE_IO_AIN
)) {
1107 status
= AllocateRingBuffers(dev
->pci_dev
,
1116 } else if (type
& NGENE_IO_HDTV
) {
1117 status
= AllocateRingBuffers(dev
->pci_dev
,
1122 MAX_HDTV_BUFFER_SIZE
,
1129 if (type
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
1131 status
= create_ring_buffer(dev
->pci_dev
,
1133 TSRingBuffer
, RING_SIZE_TS
);
1137 status
= AllocateRingBuffers(dev
->pci_dev
,
1138 dev
->PAOverflowBuffer
,
1141 MAX_TS_BUFFER_SIZE
, 0);
1146 if (type
& NGENE_IO_TSOUT
) {
1147 status
= create_ring_buffer(dev
->pci_dev
,
1152 status
= AllocateRingBuffers(dev
->pci_dev
,
1153 dev
->PAOverflowBuffer
,
1156 MAX_TS_BUFFER_SIZE
, 0);
1159 FillTSIdleBuffer(&dev
->channel
[i
].TSIdleBuffer
,
1160 &dev
->channel
[i
].TSRingBuffer
);
1166 static void ngene_release_buffers(struct ngene
*dev
)
1169 iounmap(dev
->iomem
);
1170 free_common_buffers(dev
);
1171 vfree(dev
->tsout_buf
);
1172 vfree(dev
->tsin_buf
);
1173 vfree(dev
->ain_buf
);
1174 vfree(dev
->vin_buf
);
1178 static int ngene_get_buffers(struct ngene
*dev
)
1180 if (AllocCommonBuffers(dev
))
1182 if (dev
->card_info
->io_type
[4] & NGENE_IO_TSOUT
) {
1183 dev
->tsout_buf
= vmalloc(TSOUT_BUF_SIZE
);
1184 if (!dev
->tsout_buf
)
1186 dvb_ringbuffer_init(&dev
->tsout_rbuf
,
1187 dev
->tsout_buf
, TSOUT_BUF_SIZE
);
1189 if (dev
->card_info
->io_type
[2]&NGENE_IO_TSIN
) {
1190 dev
->tsin_buf
= vmalloc(TSIN_BUF_SIZE
);
1193 dvb_ringbuffer_init(&dev
->tsin_rbuf
,
1194 dev
->tsin_buf
, TSIN_BUF_SIZE
);
1196 if (dev
->card_info
->io_type
[2] & NGENE_IO_AIN
) {
1197 dev
->ain_buf
= vmalloc(AIN_BUF_SIZE
);
1200 dvb_ringbuffer_init(&dev
->ain_rbuf
, dev
->ain_buf
, AIN_BUF_SIZE
);
1202 if (dev
->card_info
->io_type
[0] & NGENE_IO_HDTV
) {
1203 dev
->vin_buf
= vmalloc(VIN_BUF_SIZE
);
1206 dvb_ringbuffer_init(&dev
->vin_rbuf
, dev
->vin_buf
, VIN_BUF_SIZE
);
1208 dev
->iomem
= ioremap(pci_resource_start(dev
->pci_dev
, 0),
1209 pci_resource_len(dev
->pci_dev
, 0));
1216 static void ngene_init(struct ngene
*dev
)
1220 tasklet_init(&dev
->event_tasklet
, event_tasklet
, (unsigned long)dev
);
1222 memset_io(dev
->iomem
+ 0xc000, 0x00, 0x220);
1223 memset_io(dev
->iomem
+ 0xc400, 0x00, 0x100);
1225 for (i
= 0; i
< MAX_STREAM
; i
++) {
1226 dev
->channel
[i
].dev
= dev
;
1227 dev
->channel
[i
].number
= i
;
1230 dev
->fw_interface_version
= 0;
1232 ngwritel(0, NGENE_INT_ENABLE
);
1234 dev
->icounts
= ngreadl(NGENE_INT_COUNTS
);
1236 dev
->device_version
= ngreadl(DEV_VER
) & 0x0f;
1237 printk(KERN_INFO DEVICE_NAME
": Device version %d\n",
1238 dev
->device_version
);
1241 static int ngene_load_firm(struct ngene
*dev
)
1244 const struct firmware
*fw
= NULL
;
1249 version
= dev
->card_info
->fw_version
;
1256 fw_name
= "ngene_15.fw";
1257 dev
->cmd_timeout_workaround
= true;
1261 fw_name
= "ngene_16.fw";
1262 dev
->cmd_timeout_workaround
= true;
1266 fw_name
= "ngene_17.fw";
1267 dev
->cmd_timeout_workaround
= true;
1271 fw_name
= "ngene_18.fw";
1275 if (request_firmware(&fw
, fw_name
, &dev
->pci_dev
->dev
) < 0) {
1276 printk(KERN_ERR DEVICE_NAME
1277 ": Could not load firmware file %s.\n", fw_name
);
1278 printk(KERN_INFO DEVICE_NAME
1279 ": Copy %s to your hotplug directory!\n", fw_name
);
1284 if (size
!= fw
->size
) {
1285 printk(KERN_ERR DEVICE_NAME
1286 ": Firmware %s has invalid size!", fw_name
);
1289 printk(KERN_INFO DEVICE_NAME
1290 ": Loading firmware file %s.\n", fw_name
);
1291 ngene_fw
= (u8
*) fw
->data
;
1292 err
= ngene_command_load_firmware(dev
, ngene_fw
, size
);
1295 release_firmware(fw
);
1300 static void ngene_stop(struct ngene
*dev
)
1302 down(&dev
->cmd_mutex
);
1303 i2c_del_adapter(&(dev
->channel
[0].i2c_adapter
));
1304 i2c_del_adapter(&(dev
->channel
[1].i2c_adapter
));
1305 ngwritel(0, NGENE_INT_ENABLE
);
1306 ngwritel(0, NGENE_COMMAND
);
1307 ngwritel(0, NGENE_COMMAND_HI
);
1308 ngwritel(0, NGENE_STATUS
);
1309 ngwritel(0, NGENE_STATUS_HI
);
1310 ngwritel(0, NGENE_EVENT
);
1311 ngwritel(0, NGENE_EVENT_HI
);
1312 free_irq(dev
->pci_dev
->irq
, dev
);
1313 #ifdef CONFIG_PCI_MSI
1314 if (dev
->msi_enabled
)
1315 pci_disable_msi(dev
->pci_dev
);
1319 static int ngene_buffer_config(struct ngene
*dev
)
1323 if (dev
->card_info
->fw_version
>= 17) {
1324 u8 tsin12_config
[6] = { 0x60, 0x60, 0x00, 0x00, 0x00, 0x00 };
1325 u8 tsin1234_config
[6] = { 0x30, 0x30, 0x00, 0x30, 0x30, 0x00 };
1326 u8 tsio1235_config
[6] = { 0x30, 0x30, 0x00, 0x28, 0x00, 0x38 };
1327 u8
*bconf
= tsin12_config
;
1329 if (dev
->card_info
->io_type
[2]&NGENE_IO_TSIN
&&
1330 dev
->card_info
->io_type
[3]&NGENE_IO_TSIN
) {
1331 bconf
= tsin1234_config
;
1332 if (dev
->card_info
->io_type
[4]&NGENE_IO_TSOUT
&&
1334 bconf
= tsio1235_config
;
1336 stat
= ngene_command_config_free_buf(dev
, bconf
);
1338 int bconf
= BUFFER_CONFIG_4422
;
1340 if (dev
->card_info
->io_type
[3] == NGENE_IO_TSIN
)
1341 bconf
= BUFFER_CONFIG_3333
;
1342 stat
= ngene_command_config_buf(dev
, bconf
);
1348 static int ngene_start(struct ngene
*dev
)
1353 pci_set_master(dev
->pci_dev
);
1356 stat
= request_irq(dev
->pci_dev
->irq
, irq_handler
,
1357 IRQF_SHARED
, "nGene",
1362 init_waitqueue_head(&dev
->cmd_wq
);
1363 init_waitqueue_head(&dev
->tx_wq
);
1364 init_waitqueue_head(&dev
->rx_wq
);
1365 sema_init(&dev
->cmd_mutex
, 1);
1366 sema_init(&dev
->stream_mutex
, 1);
1367 sema_init(&dev
->pll_mutex
, 1);
1368 sema_init(&dev
->i2c_switch_mutex
, 1);
1369 spin_lock_init(&dev
->cmd_lock
);
1370 for (i
= 0; i
< MAX_STREAM
; i
++)
1371 spin_lock_init(&dev
->channel
[i
].state_lock
);
1372 ngwritel(1, TIMESTAMPS
);
1374 ngwritel(1, NGENE_INT_ENABLE
);
1376 stat
= ngene_load_firm(dev
);
1380 #ifdef CONFIG_PCI_MSI
1381 /* enable MSI if kernel and card support it */
1382 if (pci_msi_enabled() && dev
->card_info
->msi_supported
) {
1383 unsigned long flags
;
1385 ngwritel(0, NGENE_INT_ENABLE
);
1386 free_irq(dev
->pci_dev
->irq
, dev
);
1387 stat
= pci_enable_msi(dev
->pci_dev
);
1389 printk(KERN_INFO DEVICE_NAME
1390 ": MSI not available\n");
1391 flags
= IRQF_SHARED
;
1394 dev
->msi_enabled
= true;
1396 stat
= request_irq(dev
->pci_dev
->irq
, irq_handler
,
1397 flags
, "nGene", dev
);
1400 ngwritel(1, NGENE_INT_ENABLE
);
1404 stat
= ngene_i2c_init(dev
, 0);
1408 stat
= ngene_i2c_init(dev
, 1);
1415 /* otherwise error: fall through */
1417 ngwritel(0, NGENE_INT_ENABLE
);
1418 free_irq(dev
->pci_dev
->irq
, dev
);
1419 #ifdef CONFIG_PCI_MSI
1421 if (dev
->msi_enabled
)
1422 pci_disable_msi(dev
->pci_dev
);
1427 /****************************************************************************/
1428 /****************************************************************************/
1429 /****************************************************************************/
1431 static void release_channel(struct ngene_channel
*chan
)
1433 struct dvb_demux
*dvbdemux
= &chan
->demux
;
1434 struct ngene
*dev
= chan
->dev
;
1437 set_transfer(chan
, 0);
1439 tasklet_kill(&chan
->demux_tasklet
);
1442 dvb_unregister_device(chan
->ci_dev
);
1443 chan
->ci_dev
= NULL
;
1447 dvb_unregister_frontend(chan
->fe2
);
1450 dvb_unregister_frontend(chan
->fe
);
1451 dvb_frontend_detach(chan
->fe
);
1455 if (chan
->has_demux
) {
1456 dvb_net_release(&chan
->dvbnet
);
1457 dvbdemux
->dmx
.close(&dvbdemux
->dmx
);
1458 dvbdemux
->dmx
.remove_frontend(&dvbdemux
->dmx
,
1459 &chan
->hw_frontend
);
1460 dvbdemux
->dmx
.remove_frontend(&dvbdemux
->dmx
,
1461 &chan
->mem_frontend
);
1462 dvb_dmxdev_release(&chan
->dmxdev
);
1463 dvb_dmx_release(&chan
->demux
);
1464 chan
->has_demux
= false;
1467 if (chan
->has_adapter
) {
1468 dvb_unregister_adapter(&dev
->adapter
[chan
->number
]);
1469 chan
->has_adapter
= false;
1473 static int init_channel(struct ngene_channel
*chan
)
1475 int ret
= 0, nr
= chan
->number
;
1476 struct dvb_adapter
*adapter
= NULL
;
1477 struct dvb_demux
*dvbdemux
= &chan
->demux
;
1478 struct ngene
*dev
= chan
->dev
;
1479 struct ngene_info
*ni
= dev
->card_info
;
1480 int io
= ni
->io_type
[nr
];
1482 tasklet_init(&chan
->demux_tasklet
, demux_tasklet
, (unsigned long)chan
);
1485 chan
->mode
= chan
->type
; /* for now only one mode */
1487 if (io
& NGENE_IO_TSIN
) {
1489 if (ni
->demod_attach
[nr
]) {
1490 ret
= ni
->demod_attach
[nr
](chan
);
1494 if (chan
->fe
&& ni
->tuner_attach
[nr
]) {
1495 ret
= ni
->tuner_attach
[nr
](chan
);
1501 if (!dev
->ci
.en
&& (io
& NGENE_IO_TSOUT
))
1504 if (io
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
1505 if (nr
>= STREAM_AUDIOIN1
)
1506 chan
->DataFormatFlags
= DF_SWAP32
;
1508 if (nr
== 0 || !one_adapter
|| dev
->first_adapter
== NULL
) {
1509 adapter
= &dev
->adapter
[nr
];
1510 ret
= dvb_register_adapter(adapter
, "nGene",
1512 &chan
->dev
->pci_dev
->dev
,
1516 if (dev
->first_adapter
== NULL
)
1517 dev
->first_adapter
= adapter
;
1518 chan
->has_adapter
= true;
1520 adapter
= dev
->first_adapter
;
1523 if (dev
->ci
.en
&& (io
& NGENE_IO_TSOUT
)) {
1524 dvb_ca_en50221_init(adapter
, dev
->ci
.en
, 0, 1);
1525 set_transfer(chan
, 1);
1526 chan
->dev
->channel
[2].DataFormatFlags
= DF_SWAP32
;
1527 set_transfer(&chan
->dev
->channel
[2], 1);
1528 dvb_register_device(adapter
, &chan
->ci_dev
,
1529 &ngene_dvbdev_ci
, (void *) chan
,
1536 if (dvb_register_frontend(adapter
, chan
->fe
) < 0)
1538 chan
->has_demux
= true;
1541 if (dvb_register_frontend(adapter
, chan
->fe2
) < 0)
1543 chan
->fe2
->tuner_priv
= chan
->fe
->tuner_priv
;
1544 memcpy(&chan
->fe2
->ops
.tuner_ops
,
1545 &chan
->fe
->ops
.tuner_ops
,
1546 sizeof(struct dvb_tuner_ops
));
1549 if (chan
->has_demux
) {
1550 ret
= my_dvb_dmx_ts_card_init(dvbdemux
, "SW demux",
1552 ngene_stop_feed
, chan
);
1553 ret
= my_dvb_dmxdev_ts_card_init(&chan
->dmxdev
, &chan
->demux
,
1555 &chan
->mem_frontend
, adapter
);
1556 ret
= dvb_net_init(adapter
, &chan
->dvbnet
, &chan
->demux
.dmx
);
1563 dvb_frontend_detach(chan
->fe
);
1566 release_channel(chan
);
1570 static int init_channels(struct ngene
*dev
)
1574 for (i
= 0; i
< MAX_STREAM
; i
++) {
1575 dev
->channel
[i
].number
= i
;
1576 if (init_channel(&dev
->channel
[i
]) < 0) {
1577 for (j
= i
- 1; j
>= 0; j
--)
1578 release_channel(&dev
->channel
[j
]);
1585 static struct cxd2099_cfg cxd_cfg
= {
1592 static void cxd_attach(struct ngene
*dev
)
1594 struct ngene_ci
*ci
= &dev
->ci
;
1596 ci
->en
= cxd2099_attach(&cxd_cfg
, dev
, &dev
->channel
[0].i2c_adapter
);
1601 static void cxd_detach(struct ngene
*dev
)
1603 struct ngene_ci
*ci
= &dev
->ci
;
1605 dvb_ca_en50221_release(ci
->en
);
1610 /***********************************/
1611 /* workaround for shutdown failure */
1612 /***********************************/
1614 static void ngene_unlink(struct ngene
*dev
)
1616 struct ngene_command com
;
1618 com
.cmd
.hdr
.Opcode
= CMD_MEM_WRITE
;
1619 com
.cmd
.hdr
.Length
= 3;
1620 com
.cmd
.MemoryWrite
.address
= 0x910c;
1621 com
.cmd
.MemoryWrite
.data
= 0xff;
1625 down(&dev
->cmd_mutex
);
1626 ngwritel(0, NGENE_INT_ENABLE
);
1627 ngene_command_mutex(dev
, &com
);
1628 up(&dev
->cmd_mutex
);
1631 void ngene_shutdown(struct pci_dev
*pdev
)
1633 struct ngene
*dev
= (struct ngene
*)pci_get_drvdata(pdev
);
1635 if (!dev
|| !shutdown_workaround
)
1638 printk(KERN_INFO DEVICE_NAME
": shutdown workaround...\n");
1640 pci_disable_device(pdev
);
1643 /****************************************************************************/
1644 /* device probe/remove calls ************************************************/
1645 /****************************************************************************/
1647 void __devexit
ngene_remove(struct pci_dev
*pdev
)
1649 struct ngene
*dev
= pci_get_drvdata(pdev
);
1652 tasklet_kill(&dev
->event_tasklet
);
1653 for (i
= MAX_STREAM
- 1; i
>= 0; i
--)
1654 release_channel(&dev
->channel
[i
]);
1658 ngene_release_buffers(dev
);
1659 pci_set_drvdata(pdev
, NULL
);
1660 pci_disable_device(pdev
);
1663 int __devinit
ngene_probe(struct pci_dev
*pci_dev
,
1664 const struct pci_device_id
*id
)
1669 if (pci_enable_device(pci_dev
) < 0)
1672 dev
= vzalloc(sizeof(struct ngene
));
1678 dev
->pci_dev
= pci_dev
;
1679 dev
->card_info
= (struct ngene_info
*)id
->driver_data
;
1680 printk(KERN_INFO DEVICE_NAME
": Found %s\n", dev
->card_info
->name
);
1682 pci_set_drvdata(pci_dev
, dev
);
1684 /* Alloc buffers and start nGene */
1685 stat
= ngene_get_buffers(dev
);
1688 stat
= ngene_start(dev
);
1694 stat
= ngene_buffer_config(dev
);
1699 dev
->i2c_current_bus
= -1;
1701 /* Register DVB adapters and devices for both channels */
1702 if (init_channels(dev
) < 0)
1710 ngene_release_buffers(dev
);
1712 pci_disable_device(pci_dev
);
1713 pci_set_drvdata(pci_dev
, NULL
);