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): %*ph\n", HOST_TO_NGENE
, 8, buf
);
263 ngcpyfrom(buf
, NGENE_TO_HOST
, 8);
264 printk(KERN_ERR
"ngene_to_host (%04x): %*ph\n", NGENE_TO_HOST
, 8, buf
);
266 b
= dev
->hosttongene
;
267 printk(KERN_ERR
"dev->hosttongene (%p): %*ph\n", b
, 8, b
);
269 b
= dev
->ngenetohost
;
270 printk(KERN_ERR
"dev->ngenetohost (%p): %*ph\n", b
, 8, b
);
273 static int ngene_command_mutex(struct ngene
*dev
, struct ngene_command
*com
)
280 if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_PREPARE
) {
281 dev
->BootFirmware
= 1;
282 dev
->icounts
= ngreadl(NGENE_INT_COUNTS
);
283 ngwritel(0, NGENE_COMMAND
);
284 ngwritel(0, NGENE_COMMAND_HI
);
285 ngwritel(0, NGENE_STATUS
);
286 ngwritel(0, NGENE_STATUS_HI
);
287 ngwritel(0, NGENE_EVENT
);
288 ngwritel(0, NGENE_EVENT_HI
);
289 } else if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_FINISH
) {
290 u64 fwio
= dev
->PAFWInterfaceBuffer
;
292 ngwritel(fwio
& 0xffffffff, NGENE_COMMAND
);
293 ngwritel(fwio
>> 32, NGENE_COMMAND_HI
);
294 ngwritel((fwio
+ 256) & 0xffffffff, NGENE_STATUS
);
295 ngwritel((fwio
+ 256) >> 32, NGENE_STATUS_HI
);
296 ngwritel((fwio
+ 512) & 0xffffffff, NGENE_EVENT
);
297 ngwritel((fwio
+ 512) >> 32, NGENE_EVENT_HI
);
300 memcpy(dev
->FWInterfaceBuffer
, com
->cmd
.raw8
, com
->in_len
+ 2);
302 if (dev
->BootFirmware
)
303 ngcpyto(HOST_TO_NGENE
, com
->cmd
.raw8
, com
->in_len
+ 2);
305 spin_lock_irq(&dev
->cmd_lock
);
306 tmpCmdDoneByte
= dev
->ngenetohost
+ com
->out_len
;
310 dev
->ngenetohost
[0] = 0;
311 dev
->ngenetohost
[1] = 0;
312 dev
->CmdDoneByte
= tmpCmdDoneByte
;
313 spin_unlock_irq(&dev
->cmd_lock
);
316 ngwritel(1, FORCE_INT
);
318 ret
= wait_event_timeout(dev
->cmd_wq
, dev
->cmd_done
== 1, 2 * HZ
);
320 /*ngwritel(0, FORCE_NMI);*/
322 printk(KERN_ERR DEVICE_NAME
323 ": Command timeout cmd=%02x prev=%02x\n",
324 com
->cmd
.hdr
.Opcode
, dev
->prev_cmd
);
325 dump_command_io(dev
);
328 if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_FINISH
)
329 dev
->BootFirmware
= 0;
331 dev
->prev_cmd
= com
->cmd
.hdr
.Opcode
;
336 memcpy(com
->cmd
.raw8
, dev
->ngenetohost
, com
->out_len
);
341 int ngene_command(struct ngene
*dev
, struct ngene_command
*com
)
345 down(&dev
->cmd_mutex
);
346 result
= ngene_command_mutex(dev
, com
);
352 static int ngene_command_load_firmware(struct ngene
*dev
,
353 u8
*ngene_fw
, u32 size
)
355 #define FIRSTCHUNK (1024)
357 struct ngene_command com
;
359 com
.cmd
.hdr
.Opcode
= CMD_FWLOAD_PREPARE
;
360 com
.cmd
.hdr
.Length
= 0;
364 ngene_command(dev
, &com
);
366 cleft
= (size
+ 3) & ~3;
367 if (cleft
> FIRSTCHUNK
) {
368 ngcpyto(PROGRAM_SRAM
+ FIRSTCHUNK
, ngene_fw
+ FIRSTCHUNK
,
372 ngcpyto(DATA_FIFO_AREA
, ngene_fw
, cleft
);
374 memset(&com
, 0, sizeof(struct ngene_command
));
375 com
.cmd
.hdr
.Opcode
= CMD_FWLOAD_FINISH
;
376 com
.cmd
.hdr
.Length
= 4;
377 com
.cmd
.FWLoadFinish
.Address
= DATA_FIFO_AREA
;
378 com
.cmd
.FWLoadFinish
.Length
= (unsigned short)cleft
;
382 return ngene_command(dev
, &com
);
386 static int ngene_command_config_buf(struct ngene
*dev
, u8 config
)
388 struct ngene_command com
;
390 com
.cmd
.hdr
.Opcode
= CMD_CONFIGURE_BUFFER
;
391 com
.cmd
.hdr
.Length
= 1;
392 com
.cmd
.ConfigureBuffers
.config
= config
;
396 if (ngene_command(dev
, &com
) < 0)
401 static int ngene_command_config_free_buf(struct ngene
*dev
, u8
*config
)
403 struct ngene_command com
;
405 com
.cmd
.hdr
.Opcode
= CMD_CONFIGURE_FREE_BUFFER
;
406 com
.cmd
.hdr
.Length
= 6;
407 memcpy(&com
.cmd
.ConfigureBuffers
.config
, config
, 6);
411 if (ngene_command(dev
, &com
) < 0)
417 int ngene_command_gpio_set(struct ngene
*dev
, u8 select
, u8 level
)
419 struct ngene_command com
;
421 com
.cmd
.hdr
.Opcode
= CMD_SET_GPIO_PIN
;
422 com
.cmd
.hdr
.Length
= 1;
423 com
.cmd
.SetGpioPin
.select
= select
| (level
<< 7);
427 return ngene_command(dev
, &com
);
432 02000640 is sample on rising edge.
433 02000740 is sample on falling edge.
434 02000040 is ignore "valid" signal
436 0: FD_CTL1 Bit 7,6 must be 0,1
437 7 disable(fw controlled)
442 1,0 0-no sync, 1-use ext. start, 2-use 0x47, 3-both
443 1: FD_CTL2 has 3-valid must be hi, 2-use valid, 1-edge
444 2: FD_STA is read-only. 0-sync
445 3: FD_INSYNC is number of 47s to trigger "in sync".
446 4: FD_OUTSYNC is number of 47s to trigger "out of sync".
447 5: FD_MAXBYTE1 is low-order of bytes per packet.
448 6: FD_MAXBYTE2 is high-order of bytes per packet.
449 7: Top byte is unused.
452 /****************************************************************************/
454 static u8 TSFeatureDecoderSetup
[8 * 5] = {
455 0x42, 0x00, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00,
456 0x40, 0x06, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* DRXH */
457 0x71, 0x07, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* DRXHser */
458 0x72, 0x00, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* S2ser */
459 0x40, 0x07, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* LGDT3303 */
462 /* Set NGENE I2S Config to 16 bit packed */
463 static u8 I2SConfiguration
[] = {
464 0x00, 0x10, 0x00, 0x00,
465 0x80, 0x10, 0x00, 0x00,
468 static u8 SPDIFConfiguration
[10] = {
469 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
472 /* Set NGENE I2S Config to transport stream compatible mode */
474 static u8 TS_I2SConfiguration
[4] = { 0x3E, 0x18, 0x00, 0x00 };
476 static u8 TS_I2SOutConfiguration
[4] = { 0x80, 0x04, 0x00, 0x00 };
478 static u8 ITUDecoderSetup
[4][16] = {
479 {0x1c, 0x13, 0x01, 0x68, 0x3d, 0x90, 0x14, 0x20, /* SDTV */
480 0x00, 0x00, 0x01, 0xb0, 0x9c, 0x00, 0x00, 0x00},
481 {0x9c, 0x03, 0x23, 0xC0, 0x60, 0x0E, 0x13, 0x00,
482 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
483 {0x9f, 0x00, 0x23, 0xC0, 0x60, 0x0F, 0x13, 0x00, /* HDTV 1080i50 */
484 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
485 {0x9c, 0x01, 0x23, 0xC0, 0x60, 0x0E, 0x13, 0x00, /* HDTV 1080i60 */
486 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
491 * 27p50 9f 00 22 80 42 69 18 ...
492 * 27p60 93 00 22 80 82 69 1c ...
495 /* Maxbyte to 1144 (for raw data) */
496 static u8 ITUFeatureDecoderSetup
[8] = {
497 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x04, 0x00
500 void FillTSBuffer(void *Buffer
, int Length
, u32 Flags
)
504 memset(Buffer
, TS_FILLER
, Length
);
506 if (Flags
& DF_SWAP32
)
516 static void flush_buffers(struct ngene_channel
*chan
)
522 spin_lock_irq(&chan
->state_lock
);
523 val
= chan
->nextBuffer
->ngeneBuffer
.SR
.Flags
& 0x80;
524 spin_unlock_irq(&chan
->state_lock
);
528 static void clear_buffers(struct ngene_channel
*chan
)
530 struct SBufferHeader
*Cur
= chan
->nextBuffer
;
533 memset(&Cur
->ngeneBuffer
.SR
, 0, sizeof(Cur
->ngeneBuffer
.SR
));
534 if (chan
->mode
& NGENE_IO_TSOUT
)
535 FillTSBuffer(Cur
->Buffer1
,
536 chan
->Capture1Length
,
537 chan
->DataFormatFlags
);
539 } while (Cur
!= chan
->nextBuffer
);
541 if (chan
->mode
& NGENE_IO_TSOUT
) {
542 chan
->nextBuffer
->ngeneBuffer
.SR
.DTOUpdate
=
544 chan
->AudioDTOUpdated
= 0;
546 Cur
= chan
->TSIdleBuffer
.Head
;
549 memset(&Cur
->ngeneBuffer
.SR
, 0,
550 sizeof(Cur
->ngeneBuffer
.SR
));
551 FillTSBuffer(Cur
->Buffer1
,
552 chan
->Capture1Length
,
553 chan
->DataFormatFlags
);
555 } while (Cur
!= chan
->TSIdleBuffer
.Head
);
559 static int ngene_command_stream_control(struct ngene
*dev
, u8 stream
,
560 u8 control
, u8 mode
, u8 flags
)
562 struct ngene_channel
*chan
= &dev
->channel
[stream
];
563 struct ngene_command com
;
564 u16 BsUVI
= ((stream
& 1) ? 0x9400 : 0x9300);
565 u16 BsSDI
= ((stream
& 1) ? 0x9600 : 0x9500);
566 u16 BsSPI
= ((stream
& 1) ? 0x9800 : 0x9700);
569 down(&dev
->stream_mutex
);
570 memset(&com
, 0, sizeof(com
));
571 com
.cmd
.hdr
.Opcode
= CMD_CONTROL
;
572 com
.cmd
.hdr
.Length
= sizeof(struct FW_STREAM_CONTROL
) - 2;
573 com
.cmd
.StreamControl
.Stream
= stream
| (control
? 8 : 0);
574 if (chan
->mode
& NGENE_IO_TSOUT
)
575 com
.cmd
.StreamControl
.Stream
|= 0x07;
576 com
.cmd
.StreamControl
.Control
= control
|
577 (flags
& SFLAG_ORDER_LUMA_CHROMA
);
578 com
.cmd
.StreamControl
.Mode
= mode
;
579 com
.in_len
= sizeof(struct FW_STREAM_CONTROL
);
582 dprintk(KERN_INFO DEVICE_NAME
583 ": Stream=%02x, Control=%02x, Mode=%02x\n",
584 com
.cmd
.StreamControl
.Stream
, com
.cmd
.StreamControl
.Control
,
585 com
.cmd
.StreamControl
.Mode
);
589 if (!(control
& 0x80)) {
590 spin_lock_irq(&chan
->state_lock
);
591 if (chan
->State
== KSSTATE_RUN
) {
592 chan
->State
= KSSTATE_ACQUIRE
;
593 chan
->HWState
= HWSTATE_STOP
;
594 spin_unlock_irq(&chan
->state_lock
);
595 if (ngene_command(dev
, &com
) < 0) {
596 up(&dev
->stream_mutex
);
599 /* clear_buffers(chan); */
601 up(&dev
->stream_mutex
);
604 spin_unlock_irq(&chan
->state_lock
);
605 up(&dev
->stream_mutex
);
609 if (mode
& SMODE_AUDIO_CAPTURE
) {
610 com
.cmd
.StreamControl
.CaptureBlockCount
=
611 chan
->Capture1Length
/ AUDIO_BLOCK_SIZE
;
612 com
.cmd
.StreamControl
.Buffer_Address
= chan
->RingBuffer
.PAHead
;
613 } else if (mode
& SMODE_TRANSPORT_STREAM
) {
614 com
.cmd
.StreamControl
.CaptureBlockCount
=
615 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
616 com
.cmd
.StreamControl
.MaxLinesPerField
=
617 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
618 com
.cmd
.StreamControl
.Buffer_Address
=
619 chan
->TSRingBuffer
.PAHead
;
620 if (chan
->mode
& NGENE_IO_TSOUT
) {
621 com
.cmd
.StreamControl
.BytesPerVBILine
=
622 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
623 com
.cmd
.StreamControl
.Stream
|= 0x07;
626 com
.cmd
.StreamControl
.BytesPerVideoLine
= chan
->nBytesPerLine
;
627 com
.cmd
.StreamControl
.MaxLinesPerField
= chan
->nLines
;
628 com
.cmd
.StreamControl
.MinLinesPerField
= 100;
629 com
.cmd
.StreamControl
.Buffer_Address
= chan
->RingBuffer
.PAHead
;
631 if (mode
& SMODE_VBI_CAPTURE
) {
632 com
.cmd
.StreamControl
.MaxVBILinesPerField
=
634 com
.cmd
.StreamControl
.MinVBILinesPerField
= 0;
635 com
.cmd
.StreamControl
.BytesPerVBILine
=
636 chan
->nBytesPerVBILine
;
638 if (flags
& SFLAG_COLORBAR
)
639 com
.cmd
.StreamControl
.Stream
|= 0x04;
642 spin_lock_irq(&chan
->state_lock
);
643 if (mode
& SMODE_AUDIO_CAPTURE
) {
644 chan
->nextBuffer
= chan
->RingBuffer
.Head
;
645 if (mode
& SMODE_AUDIO_SPDIF
) {
646 com
.cmd
.StreamControl
.SetupDataLen
=
647 sizeof(SPDIFConfiguration
);
648 com
.cmd
.StreamControl
.SetupDataAddr
= BsSPI
;
649 memcpy(com
.cmd
.StreamControl
.SetupData
,
650 SPDIFConfiguration
, sizeof(SPDIFConfiguration
));
652 com
.cmd
.StreamControl
.SetupDataLen
= 4;
653 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDI
;
654 memcpy(com
.cmd
.StreamControl
.SetupData
,
656 4 * dev
->card_info
->i2s
[stream
], 4);
658 } else if (mode
& SMODE_TRANSPORT_STREAM
) {
659 chan
->nextBuffer
= chan
->TSRingBuffer
.Head
;
660 if (stream
>= STREAM_AUDIOIN1
) {
661 if (chan
->mode
& NGENE_IO_TSOUT
) {
662 com
.cmd
.StreamControl
.SetupDataLen
=
663 sizeof(TS_I2SOutConfiguration
);
664 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDO
;
665 memcpy(com
.cmd
.StreamControl
.SetupData
,
666 TS_I2SOutConfiguration
,
667 sizeof(TS_I2SOutConfiguration
));
669 com
.cmd
.StreamControl
.SetupDataLen
=
670 sizeof(TS_I2SConfiguration
);
671 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDI
;
672 memcpy(com
.cmd
.StreamControl
.SetupData
,
674 sizeof(TS_I2SConfiguration
));
677 com
.cmd
.StreamControl
.SetupDataLen
= 8;
678 com
.cmd
.StreamControl
.SetupDataAddr
= BsUVI
+ 0x10;
679 memcpy(com
.cmd
.StreamControl
.SetupData
,
680 TSFeatureDecoderSetup
+
681 8 * dev
->card_info
->tsf
[stream
], 8);
684 chan
->nextBuffer
= chan
->RingBuffer
.Head
;
685 com
.cmd
.StreamControl
.SetupDataLen
=
686 16 + sizeof(ITUFeatureDecoderSetup
);
687 com
.cmd
.StreamControl
.SetupDataAddr
= BsUVI
;
688 memcpy(com
.cmd
.StreamControl
.SetupData
,
689 ITUDecoderSetup
[chan
->itumode
], 16);
690 memcpy(com
.cmd
.StreamControl
.SetupData
+ 16,
691 ITUFeatureDecoderSetup
, sizeof(ITUFeatureDecoderSetup
));
694 chan
->State
= KSSTATE_RUN
;
695 if (mode
& SMODE_TRANSPORT_STREAM
)
696 chan
->HWState
= HWSTATE_RUN
;
698 chan
->HWState
= HWSTATE_STARTUP
;
699 spin_unlock_irq(&chan
->state_lock
);
701 if (ngene_command(dev
, &com
) < 0) {
702 up(&dev
->stream_mutex
);
705 up(&dev
->stream_mutex
);
709 void set_transfer(struct ngene_channel
*chan
, int state
)
711 u8 control
= 0, mode
= 0, flags
= 0;
712 struct ngene
*dev
= chan
->dev
;
716 printk(KERN_INFO DEVICE_NAME ": st %d\n", state);
722 printk(KERN_INFO DEVICE_NAME
": already running\n");
726 if (!chan
->running
) {
727 printk(KERN_INFO DEVICE_NAME
": already stopped\n");
732 if (dev
->card_info
->switch_ctrl
)
733 dev
->card_info
->switch_ctrl(chan
, 1, state
^ 1);
736 spin_lock_irq(&chan
->state_lock
);
738 /* printk(KERN_INFO DEVICE_NAME ": lock=%08x\n",
740 dvb_ringbuffer_flush(&dev
->tsout_rbuf
);
742 if (chan
->mode
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
743 chan
->Capture1Length
= 512 * 188;
744 mode
= SMODE_TRANSPORT_STREAM
;
746 if (chan
->mode
& NGENE_IO_TSOUT
) {
747 chan
->pBufferExchange
= tsout_exchange
;
748 /* 0x66666666 = 50MHz *2^33 /250MHz */
749 chan
->AudioDTOValue
= 0x80000000;
750 chan
->AudioDTOUpdated
= 1;
752 if (chan
->mode
& NGENE_IO_TSIN
)
753 chan
->pBufferExchange
= tsin_exchange
;
754 spin_unlock_irq(&chan
->state_lock
);
756 /* else printk(KERN_INFO DEVICE_NAME ": lock=%08x\n",
759 ret
= ngene_command_stream_control(dev
, chan
->number
,
760 control
, mode
, flags
);
762 chan
->running
= state
;
764 printk(KERN_ERR DEVICE_NAME
": set_transfer %d failed\n",
767 spin_lock_irq(&chan
->state_lock
);
768 chan
->pBufferExchange
= NULL
;
769 dvb_ringbuffer_flush(&dev
->tsout_rbuf
);
770 spin_unlock_irq(&chan
->state_lock
);
775 /****************************************************************************/
776 /* nGene hardware init and release functions ********************************/
777 /****************************************************************************/
779 static void free_ringbuffer(struct ngene
*dev
, struct SRingBufferDescriptor
*rb
)
781 struct SBufferHeader
*Cur
= rb
->Head
;
787 for (j
= 0; j
< rb
->NumBuffers
; j
++, Cur
= Cur
->Next
) {
789 pci_free_consistent(dev
->pci_dev
,
792 Cur
->scList1
->Address
);
795 pci_free_consistent(dev
->pci_dev
,
798 Cur
->scList2
->Address
);
802 pci_free_consistent(dev
->pci_dev
, rb
->SCListMemSize
,
803 rb
->SCListMem
, rb
->PASCListMem
);
805 pci_free_consistent(dev
->pci_dev
, rb
->MemSize
, rb
->Head
, rb
->PAHead
);
808 static void free_idlebuffer(struct ngene
*dev
,
809 struct SRingBufferDescriptor
*rb
,
810 struct SRingBufferDescriptor
*tb
)
813 struct SBufferHeader
*Cur
= tb
->Head
;
817 free_ringbuffer(dev
, rb
);
818 for (j
= 0; j
< tb
->NumBuffers
; j
++, Cur
= Cur
->Next
) {
821 Cur
->ngeneBuffer
.Address_of_first_entry_2
= 0;
822 Cur
->ngeneBuffer
.Number_of_entries_2
= 0;
826 static void free_common_buffers(struct ngene
*dev
)
829 struct ngene_channel
*chan
;
831 for (i
= STREAM_VIDEOIN1
; i
< MAX_STREAM
; i
++) {
832 chan
= &dev
->channel
[i
];
833 free_idlebuffer(dev
, &chan
->TSIdleBuffer
, &chan
->TSRingBuffer
);
834 free_ringbuffer(dev
, &chan
->RingBuffer
);
835 free_ringbuffer(dev
, &chan
->TSRingBuffer
);
838 if (dev
->OverflowBuffer
)
839 pci_free_consistent(dev
->pci_dev
,
840 OVERFLOW_BUFFER_SIZE
,
841 dev
->OverflowBuffer
, dev
->PAOverflowBuffer
);
843 if (dev
->FWInterfaceBuffer
)
844 pci_free_consistent(dev
->pci_dev
,
846 dev
->FWInterfaceBuffer
,
847 dev
->PAFWInterfaceBuffer
);
850 /****************************************************************************/
851 /* Ring buffer handling *****************************************************/
852 /****************************************************************************/
854 static int create_ring_buffer(struct pci_dev
*pci_dev
,
855 struct SRingBufferDescriptor
*descr
, u32 NumBuffers
)
858 struct SBufferHeader
*Head
;
860 u32 MemSize
= SIZEOF_SBufferHeader
* NumBuffers
;
861 u64 PARingBufferHead
;
863 u64 PARingBufferNext
;
864 struct SBufferHeader
*Cur
, *Next
;
869 descr
->NumBuffers
= 0;
874 Head
= pci_alloc_consistent(pci_dev
, MemSize
, &tmp
);
875 PARingBufferHead
= tmp
;
880 memset(Head
, 0, MemSize
);
882 PARingBufferCur
= PARingBufferHead
;
885 for (i
= 0; i
< NumBuffers
- 1; i
++) {
886 Next
= (struct SBufferHeader
*)
887 (((u8
*) Cur
) + SIZEOF_SBufferHeader
);
888 PARingBufferNext
= PARingBufferCur
+ SIZEOF_SBufferHeader
;
890 Cur
->ngeneBuffer
.Next
= PARingBufferNext
;
892 PARingBufferCur
= PARingBufferNext
;
894 /* Last Buffer points back to first one */
896 Cur
->ngeneBuffer
.Next
= PARingBufferHead
;
899 descr
->MemSize
= MemSize
;
900 descr
->PAHead
= PARingBufferHead
;
901 descr
->NumBuffers
= NumBuffers
;
906 static int AllocateRingBuffers(struct pci_dev
*pci_dev
,
908 struct SRingBufferDescriptor
*pRingBuffer
,
909 u32 Buffer1Length
, u32 Buffer2Length
)
914 u32 SCListMemSize
= pRingBuffer
->NumBuffers
915 * ((Buffer2Length
!= 0) ? (NUM_SCATTER_GATHER_ENTRIES
* 2) :
916 NUM_SCATTER_GATHER_ENTRIES
)
917 * sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
920 struct HW_SCATTER_GATHER_ELEMENT
*SCListEntry
;
922 struct SBufferHeader
*Cur
;
925 if (SCListMemSize
< 4096)
926 SCListMemSize
= 4096;
928 SCListMem
= pci_alloc_consistent(pci_dev
, SCListMemSize
, &tmp
);
931 if (SCListMem
== NULL
)
934 memset(SCListMem
, 0, SCListMemSize
);
936 pRingBuffer
->SCListMem
= SCListMem
;
937 pRingBuffer
->PASCListMem
= PASCListMem
;
938 pRingBuffer
->SCListMemSize
= SCListMemSize
;
939 pRingBuffer
->Buffer1Length
= Buffer1Length
;
940 pRingBuffer
->Buffer2Length
= Buffer2Length
;
942 SCListEntry
= SCListMem
;
943 PASCListEntry
= PASCListMem
;
944 Cur
= pRingBuffer
->Head
;
946 for (i
= 0; i
< pRingBuffer
->NumBuffers
; i
+= 1, Cur
= Cur
->Next
) {
949 void *Buffer
= pci_alloc_consistent(pci_dev
, Buffer1Length
,
956 Cur
->Buffer1
= Buffer
;
958 SCListEntry
->Address
= PABuffer
;
959 SCListEntry
->Length
= Buffer1Length
;
961 Cur
->scList1
= SCListEntry
;
962 Cur
->ngeneBuffer
.Address_of_first_entry_1
= PASCListEntry
;
963 Cur
->ngeneBuffer
.Number_of_entries_1
=
964 NUM_SCATTER_GATHER_ENTRIES
;
967 PASCListEntry
+= sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
969 #if NUM_SCATTER_GATHER_ENTRIES > 1
970 for (j
= 0; j
< NUM_SCATTER_GATHER_ENTRIES
- 1; j
+= 1) {
971 SCListEntry
->Address
= of
;
972 SCListEntry
->Length
= OVERFLOW_BUFFER_SIZE
;
975 sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
982 Buffer
= pci_alloc_consistent(pci_dev
, Buffer2Length
, &tmp
);
988 Cur
->Buffer2
= Buffer
;
990 SCListEntry
->Address
= PABuffer
;
991 SCListEntry
->Length
= Buffer2Length
;
993 Cur
->scList2
= SCListEntry
;
994 Cur
->ngeneBuffer
.Address_of_first_entry_2
= PASCListEntry
;
995 Cur
->ngeneBuffer
.Number_of_entries_2
=
996 NUM_SCATTER_GATHER_ENTRIES
;
999 PASCListEntry
+= sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
1001 #if NUM_SCATTER_GATHER_ENTRIES > 1
1002 for (j
= 0; j
< NUM_SCATTER_GATHER_ENTRIES
- 1; j
++) {
1003 SCListEntry
->Address
= of
;
1004 SCListEntry
->Length
= OVERFLOW_BUFFER_SIZE
;
1007 sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
1016 static int FillTSIdleBuffer(struct SRingBufferDescriptor
*pIdleBuffer
,
1017 struct SRingBufferDescriptor
*pRingBuffer
)
1021 /* Copy pointer to scatter gather list in TSRingbuffer
1022 structure for buffer 2
1023 Load number of buffer
1025 u32 n
= pRingBuffer
->NumBuffers
;
1027 /* Point to first buffer entry */
1028 struct SBufferHeader
*Cur
= pRingBuffer
->Head
;
1030 /* Loop thru all buffer and set Buffer 2 pointers to TSIdlebuffer */
1031 for (i
= 0; i
< n
; i
++) {
1032 Cur
->Buffer2
= pIdleBuffer
->Head
->Buffer1
;
1033 Cur
->scList2
= pIdleBuffer
->Head
->scList1
;
1034 Cur
->ngeneBuffer
.Address_of_first_entry_2
=
1035 pIdleBuffer
->Head
->ngeneBuffer
.
1036 Address_of_first_entry_1
;
1037 Cur
->ngeneBuffer
.Number_of_entries_2
=
1038 pIdleBuffer
->Head
->ngeneBuffer
.Number_of_entries_1
;
1044 static u32 RingBufferSizes
[MAX_STREAM
] = {
1052 static u32 Buffer1Sizes
[MAX_STREAM
] = {
1053 MAX_VIDEO_BUFFER_SIZE
,
1054 MAX_VIDEO_BUFFER_SIZE
,
1055 MAX_AUDIO_BUFFER_SIZE
,
1056 MAX_AUDIO_BUFFER_SIZE
,
1057 MAX_AUDIO_BUFFER_SIZE
1060 static u32 Buffer2Sizes
[MAX_STREAM
] = {
1061 MAX_VBI_BUFFER_SIZE
,
1062 MAX_VBI_BUFFER_SIZE
,
1069 static int AllocCommonBuffers(struct ngene
*dev
)
1073 dev
->FWInterfaceBuffer
= pci_alloc_consistent(dev
->pci_dev
, 4096,
1074 &dev
->PAFWInterfaceBuffer
);
1075 if (!dev
->FWInterfaceBuffer
)
1077 dev
->hosttongene
= dev
->FWInterfaceBuffer
;
1078 dev
->ngenetohost
= dev
->FWInterfaceBuffer
+ 256;
1079 dev
->EventBuffer
= dev
->FWInterfaceBuffer
+ 512;
1081 dev
->OverflowBuffer
= pci_alloc_consistent(dev
->pci_dev
,
1082 OVERFLOW_BUFFER_SIZE
,
1083 &dev
->PAOverflowBuffer
);
1084 if (!dev
->OverflowBuffer
)
1086 memset(dev
->OverflowBuffer
, 0, OVERFLOW_BUFFER_SIZE
);
1088 for (i
= STREAM_VIDEOIN1
; i
< MAX_STREAM
; i
++) {
1089 int type
= dev
->card_info
->io_type
[i
];
1091 dev
->channel
[i
].State
= KSSTATE_STOP
;
1093 if (type
& (NGENE_IO_TV
| NGENE_IO_HDTV
| NGENE_IO_AIN
)) {
1094 status
= create_ring_buffer(dev
->pci_dev
,
1095 &dev
->channel
[i
].RingBuffer
,
1096 RingBufferSizes
[i
]);
1100 if (type
& (NGENE_IO_TV
| NGENE_IO_AIN
)) {
1101 status
= AllocateRingBuffers(dev
->pci_dev
,
1110 } else if (type
& NGENE_IO_HDTV
) {
1111 status
= AllocateRingBuffers(dev
->pci_dev
,
1116 MAX_HDTV_BUFFER_SIZE
,
1123 if (type
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
1125 status
= create_ring_buffer(dev
->pci_dev
,
1127 TSRingBuffer
, RING_SIZE_TS
);
1131 status
= AllocateRingBuffers(dev
->pci_dev
,
1132 dev
->PAOverflowBuffer
,
1135 MAX_TS_BUFFER_SIZE
, 0);
1140 if (type
& NGENE_IO_TSOUT
) {
1141 status
= create_ring_buffer(dev
->pci_dev
,
1146 status
= AllocateRingBuffers(dev
->pci_dev
,
1147 dev
->PAOverflowBuffer
,
1150 MAX_TS_BUFFER_SIZE
, 0);
1153 FillTSIdleBuffer(&dev
->channel
[i
].TSIdleBuffer
,
1154 &dev
->channel
[i
].TSRingBuffer
);
1160 static void ngene_release_buffers(struct ngene
*dev
)
1163 iounmap(dev
->iomem
);
1164 free_common_buffers(dev
);
1165 vfree(dev
->tsout_buf
);
1166 vfree(dev
->tsin_buf
);
1167 vfree(dev
->ain_buf
);
1168 vfree(dev
->vin_buf
);
1172 static int ngene_get_buffers(struct ngene
*dev
)
1174 if (AllocCommonBuffers(dev
))
1176 if (dev
->card_info
->io_type
[4] & NGENE_IO_TSOUT
) {
1177 dev
->tsout_buf
= vmalloc(TSOUT_BUF_SIZE
);
1178 if (!dev
->tsout_buf
)
1180 dvb_ringbuffer_init(&dev
->tsout_rbuf
,
1181 dev
->tsout_buf
, TSOUT_BUF_SIZE
);
1183 if (dev
->card_info
->io_type
[2]&NGENE_IO_TSIN
) {
1184 dev
->tsin_buf
= vmalloc(TSIN_BUF_SIZE
);
1187 dvb_ringbuffer_init(&dev
->tsin_rbuf
,
1188 dev
->tsin_buf
, TSIN_BUF_SIZE
);
1190 if (dev
->card_info
->io_type
[2] & NGENE_IO_AIN
) {
1191 dev
->ain_buf
= vmalloc(AIN_BUF_SIZE
);
1194 dvb_ringbuffer_init(&dev
->ain_rbuf
, dev
->ain_buf
, AIN_BUF_SIZE
);
1196 if (dev
->card_info
->io_type
[0] & NGENE_IO_HDTV
) {
1197 dev
->vin_buf
= vmalloc(VIN_BUF_SIZE
);
1200 dvb_ringbuffer_init(&dev
->vin_rbuf
, dev
->vin_buf
, VIN_BUF_SIZE
);
1202 dev
->iomem
= ioremap(pci_resource_start(dev
->pci_dev
, 0),
1203 pci_resource_len(dev
->pci_dev
, 0));
1210 static void ngene_init(struct ngene
*dev
)
1214 tasklet_init(&dev
->event_tasklet
, event_tasklet
, (unsigned long)dev
);
1216 memset_io(dev
->iomem
+ 0xc000, 0x00, 0x220);
1217 memset_io(dev
->iomem
+ 0xc400, 0x00, 0x100);
1219 for (i
= 0; i
< MAX_STREAM
; i
++) {
1220 dev
->channel
[i
].dev
= dev
;
1221 dev
->channel
[i
].number
= i
;
1224 dev
->fw_interface_version
= 0;
1226 ngwritel(0, NGENE_INT_ENABLE
);
1228 dev
->icounts
= ngreadl(NGENE_INT_COUNTS
);
1230 dev
->device_version
= ngreadl(DEV_VER
) & 0x0f;
1231 printk(KERN_INFO DEVICE_NAME
": Device version %d\n",
1232 dev
->device_version
);
1235 static int ngene_load_firm(struct ngene
*dev
)
1238 const struct firmware
*fw
= NULL
;
1243 version
= dev
->card_info
->fw_version
;
1250 fw_name
= "ngene_15.fw";
1251 dev
->cmd_timeout_workaround
= true;
1255 fw_name
= "ngene_16.fw";
1256 dev
->cmd_timeout_workaround
= true;
1260 fw_name
= "ngene_17.fw";
1261 dev
->cmd_timeout_workaround
= true;
1265 fw_name
= "ngene_18.fw";
1269 if (request_firmware(&fw
, fw_name
, &dev
->pci_dev
->dev
) < 0) {
1270 printk(KERN_ERR DEVICE_NAME
1271 ": Could not load firmware file %s.\n", fw_name
);
1272 printk(KERN_INFO DEVICE_NAME
1273 ": Copy %s to your hotplug directory!\n", fw_name
);
1278 if (size
!= fw
->size
) {
1279 printk(KERN_ERR DEVICE_NAME
1280 ": Firmware %s has invalid size!", fw_name
);
1283 printk(KERN_INFO DEVICE_NAME
1284 ": Loading firmware file %s.\n", fw_name
);
1285 ngene_fw
= (u8
*) fw
->data
;
1286 err
= ngene_command_load_firmware(dev
, ngene_fw
, size
);
1289 release_firmware(fw
);
1294 static void ngene_stop(struct ngene
*dev
)
1296 down(&dev
->cmd_mutex
);
1297 i2c_del_adapter(&(dev
->channel
[0].i2c_adapter
));
1298 i2c_del_adapter(&(dev
->channel
[1].i2c_adapter
));
1299 ngwritel(0, NGENE_INT_ENABLE
);
1300 ngwritel(0, NGENE_COMMAND
);
1301 ngwritel(0, NGENE_COMMAND_HI
);
1302 ngwritel(0, NGENE_STATUS
);
1303 ngwritel(0, NGENE_STATUS_HI
);
1304 ngwritel(0, NGENE_EVENT
);
1305 ngwritel(0, NGENE_EVENT_HI
);
1306 free_irq(dev
->pci_dev
->irq
, dev
);
1307 #ifdef CONFIG_PCI_MSI
1308 if (dev
->msi_enabled
)
1309 pci_disable_msi(dev
->pci_dev
);
1313 static int ngene_buffer_config(struct ngene
*dev
)
1317 if (dev
->card_info
->fw_version
>= 17) {
1318 u8 tsin12_config
[6] = { 0x60, 0x60, 0x00, 0x00, 0x00, 0x00 };
1319 u8 tsin1234_config
[6] = { 0x30, 0x30, 0x00, 0x30, 0x30, 0x00 };
1320 u8 tsio1235_config
[6] = { 0x30, 0x30, 0x00, 0x28, 0x00, 0x38 };
1321 u8
*bconf
= tsin12_config
;
1323 if (dev
->card_info
->io_type
[2]&NGENE_IO_TSIN
&&
1324 dev
->card_info
->io_type
[3]&NGENE_IO_TSIN
) {
1325 bconf
= tsin1234_config
;
1326 if (dev
->card_info
->io_type
[4]&NGENE_IO_TSOUT
&&
1328 bconf
= tsio1235_config
;
1330 stat
= ngene_command_config_free_buf(dev
, bconf
);
1332 int bconf
= BUFFER_CONFIG_4422
;
1334 if (dev
->card_info
->io_type
[3] == NGENE_IO_TSIN
)
1335 bconf
= BUFFER_CONFIG_3333
;
1336 stat
= ngene_command_config_buf(dev
, bconf
);
1342 static int ngene_start(struct ngene
*dev
)
1347 pci_set_master(dev
->pci_dev
);
1350 stat
= request_irq(dev
->pci_dev
->irq
, irq_handler
,
1351 IRQF_SHARED
, "nGene",
1356 init_waitqueue_head(&dev
->cmd_wq
);
1357 init_waitqueue_head(&dev
->tx_wq
);
1358 init_waitqueue_head(&dev
->rx_wq
);
1359 sema_init(&dev
->cmd_mutex
, 1);
1360 sema_init(&dev
->stream_mutex
, 1);
1361 sema_init(&dev
->pll_mutex
, 1);
1362 sema_init(&dev
->i2c_switch_mutex
, 1);
1363 spin_lock_init(&dev
->cmd_lock
);
1364 for (i
= 0; i
< MAX_STREAM
; i
++)
1365 spin_lock_init(&dev
->channel
[i
].state_lock
);
1366 ngwritel(1, TIMESTAMPS
);
1368 ngwritel(1, NGENE_INT_ENABLE
);
1370 stat
= ngene_load_firm(dev
);
1374 #ifdef CONFIG_PCI_MSI
1375 /* enable MSI if kernel and card support it */
1376 if (pci_msi_enabled() && dev
->card_info
->msi_supported
) {
1377 unsigned long flags
;
1379 ngwritel(0, NGENE_INT_ENABLE
);
1380 free_irq(dev
->pci_dev
->irq
, dev
);
1381 stat
= pci_enable_msi(dev
->pci_dev
);
1383 printk(KERN_INFO DEVICE_NAME
1384 ": MSI not available\n");
1385 flags
= IRQF_SHARED
;
1388 dev
->msi_enabled
= true;
1390 stat
= request_irq(dev
->pci_dev
->irq
, irq_handler
,
1391 flags
, "nGene", dev
);
1394 ngwritel(1, NGENE_INT_ENABLE
);
1398 stat
= ngene_i2c_init(dev
, 0);
1402 stat
= ngene_i2c_init(dev
, 1);
1409 ngwritel(0, NGENE_INT_ENABLE
);
1410 free_irq(dev
->pci_dev
->irq
, dev
);
1411 #ifdef CONFIG_PCI_MSI
1413 if (dev
->msi_enabled
)
1414 pci_disable_msi(dev
->pci_dev
);
1419 /****************************************************************************/
1420 /****************************************************************************/
1421 /****************************************************************************/
1423 static void release_channel(struct ngene_channel
*chan
)
1425 struct dvb_demux
*dvbdemux
= &chan
->demux
;
1426 struct ngene
*dev
= chan
->dev
;
1429 set_transfer(chan
, 0);
1431 tasklet_kill(&chan
->demux_tasklet
);
1434 dvb_unregister_device(chan
->ci_dev
);
1435 chan
->ci_dev
= NULL
;
1439 dvb_unregister_frontend(chan
->fe2
);
1442 dvb_unregister_frontend(chan
->fe
);
1443 dvb_frontend_detach(chan
->fe
);
1447 if (chan
->has_demux
) {
1448 dvb_net_release(&chan
->dvbnet
);
1449 dvbdemux
->dmx
.close(&dvbdemux
->dmx
);
1450 dvbdemux
->dmx
.remove_frontend(&dvbdemux
->dmx
,
1451 &chan
->hw_frontend
);
1452 dvbdemux
->dmx
.remove_frontend(&dvbdemux
->dmx
,
1453 &chan
->mem_frontend
);
1454 dvb_dmxdev_release(&chan
->dmxdev
);
1455 dvb_dmx_release(&chan
->demux
);
1456 chan
->has_demux
= false;
1459 if (chan
->has_adapter
) {
1460 dvb_unregister_adapter(&dev
->adapter
[chan
->number
]);
1461 chan
->has_adapter
= false;
1465 static int init_channel(struct ngene_channel
*chan
)
1467 int ret
= 0, nr
= chan
->number
;
1468 struct dvb_adapter
*adapter
= NULL
;
1469 struct dvb_demux
*dvbdemux
= &chan
->demux
;
1470 struct ngene
*dev
= chan
->dev
;
1471 struct ngene_info
*ni
= dev
->card_info
;
1472 int io
= ni
->io_type
[nr
];
1474 tasklet_init(&chan
->demux_tasklet
, demux_tasklet
, (unsigned long)chan
);
1477 chan
->mode
= chan
->type
; /* for now only one mode */
1479 if (io
& NGENE_IO_TSIN
) {
1481 if (ni
->demod_attach
[nr
]) {
1482 ret
= ni
->demod_attach
[nr
](chan
);
1486 if (chan
->fe
&& ni
->tuner_attach
[nr
]) {
1487 ret
= ni
->tuner_attach
[nr
](chan
);
1493 if (!dev
->ci
.en
&& (io
& NGENE_IO_TSOUT
))
1496 if (io
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
1497 if (nr
>= STREAM_AUDIOIN1
)
1498 chan
->DataFormatFlags
= DF_SWAP32
;
1500 if (nr
== 0 || !one_adapter
|| dev
->first_adapter
== NULL
) {
1501 adapter
= &dev
->adapter
[nr
];
1502 ret
= dvb_register_adapter(adapter
, "nGene",
1504 &chan
->dev
->pci_dev
->dev
,
1508 if (dev
->first_adapter
== NULL
)
1509 dev
->first_adapter
= adapter
;
1510 chan
->has_adapter
= true;
1512 adapter
= dev
->first_adapter
;
1515 if (dev
->ci
.en
&& (io
& NGENE_IO_TSOUT
)) {
1516 dvb_ca_en50221_init(adapter
, dev
->ci
.en
, 0, 1);
1517 set_transfer(chan
, 1);
1518 chan
->dev
->channel
[2].DataFormatFlags
= DF_SWAP32
;
1519 set_transfer(&chan
->dev
->channel
[2], 1);
1520 dvb_register_device(adapter
, &chan
->ci_dev
,
1521 &ngene_dvbdev_ci
, (void *) chan
,
1528 if (dvb_register_frontend(adapter
, chan
->fe
) < 0)
1530 chan
->has_demux
= true;
1533 if (dvb_register_frontend(adapter
, chan
->fe2
) < 0)
1535 chan
->fe2
->tuner_priv
= chan
->fe
->tuner_priv
;
1536 memcpy(&chan
->fe2
->ops
.tuner_ops
,
1537 &chan
->fe
->ops
.tuner_ops
,
1538 sizeof(struct dvb_tuner_ops
));
1541 if (chan
->has_demux
) {
1542 ret
= my_dvb_dmx_ts_card_init(dvbdemux
, "SW demux",
1544 ngene_stop_feed
, chan
);
1545 ret
= my_dvb_dmxdev_ts_card_init(&chan
->dmxdev
, &chan
->demux
,
1547 &chan
->mem_frontend
, adapter
);
1548 ret
= dvb_net_init(adapter
, &chan
->dvbnet
, &chan
->demux
.dmx
);
1555 dvb_frontend_detach(chan
->fe
);
1558 release_channel(chan
);
1562 static int init_channels(struct ngene
*dev
)
1566 for (i
= 0; i
< MAX_STREAM
; i
++) {
1567 dev
->channel
[i
].number
= i
;
1568 if (init_channel(&dev
->channel
[i
]) < 0) {
1569 for (j
= i
- 1; j
>= 0; j
--)
1570 release_channel(&dev
->channel
[j
]);
1577 static struct cxd2099_cfg cxd_cfg
= {
1584 static void cxd_attach(struct ngene
*dev
)
1586 struct ngene_ci
*ci
= &dev
->ci
;
1588 ci
->en
= cxd2099_attach(&cxd_cfg
, dev
, &dev
->channel
[0].i2c_adapter
);
1593 static void cxd_detach(struct ngene
*dev
)
1595 struct ngene_ci
*ci
= &dev
->ci
;
1597 dvb_ca_en50221_release(ci
->en
);
1602 /***********************************/
1603 /* workaround for shutdown failure */
1604 /***********************************/
1606 static void ngene_unlink(struct ngene
*dev
)
1608 struct ngene_command com
;
1610 com
.cmd
.hdr
.Opcode
= CMD_MEM_WRITE
;
1611 com
.cmd
.hdr
.Length
= 3;
1612 com
.cmd
.MemoryWrite
.address
= 0x910c;
1613 com
.cmd
.MemoryWrite
.data
= 0xff;
1617 down(&dev
->cmd_mutex
);
1618 ngwritel(0, NGENE_INT_ENABLE
);
1619 ngene_command_mutex(dev
, &com
);
1620 up(&dev
->cmd_mutex
);
1623 void ngene_shutdown(struct pci_dev
*pdev
)
1625 struct ngene
*dev
= (struct ngene
*)pci_get_drvdata(pdev
);
1627 if (!dev
|| !shutdown_workaround
)
1630 printk(KERN_INFO DEVICE_NAME
": shutdown workaround...\n");
1632 pci_disable_device(pdev
);
1635 /****************************************************************************/
1636 /* device probe/remove calls ************************************************/
1637 /****************************************************************************/
1639 void ngene_remove(struct pci_dev
*pdev
)
1641 struct ngene
*dev
= pci_get_drvdata(pdev
);
1644 tasklet_kill(&dev
->event_tasklet
);
1645 for (i
= MAX_STREAM
- 1; i
>= 0; i
--)
1646 release_channel(&dev
->channel
[i
]);
1650 ngene_release_buffers(dev
);
1651 pci_set_drvdata(pdev
, NULL
);
1652 pci_disable_device(pdev
);
1655 int ngene_probe(struct pci_dev
*pci_dev
, const struct pci_device_id
*id
)
1660 if (pci_enable_device(pci_dev
) < 0)
1663 dev
= vzalloc(sizeof(struct ngene
));
1669 dev
->pci_dev
= pci_dev
;
1670 dev
->card_info
= (struct ngene_info
*)id
->driver_data
;
1671 printk(KERN_INFO DEVICE_NAME
": Found %s\n", dev
->card_info
->name
);
1673 pci_set_drvdata(pci_dev
, dev
);
1675 /* Alloc buffers and start nGene */
1676 stat
= ngene_get_buffers(dev
);
1679 stat
= ngene_start(dev
);
1685 stat
= ngene_buffer_config(dev
);
1690 dev
->i2c_current_bus
= -1;
1692 /* Register DVB adapters and devices for both channels */
1693 stat
= init_channels(dev
);
1702 ngene_release_buffers(dev
);
1704 pci_disable_device(pci_dev
);
1705 pci_set_drvdata(pci_dev
, NULL
);