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), dev->iomem + (adr))
61 #define ngwritel(dat, adr) writel((dat), dev->iomem + (adr))
62 #define ngwriteb(dat, adr) writeb((dat), 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(dev->iomem + (adr), (src), (count))
66 #define ngcpyfrom(dst, adr, count) memcpy_fromio((dst), dev->iomem + (adr), (count))
68 /****************************************************************************/
69 /* nGene interrupt handler **************************************************/
70 /****************************************************************************/
72 static void event_tasklet(unsigned long data
)
74 struct ngene
*dev
= (struct ngene
*)data
;
76 while (dev
->EventQueueReadIndex
!= dev
->EventQueueWriteIndex
) {
77 struct EVENT_BUFFER Event
=
78 dev
->EventQueue
[dev
->EventQueueReadIndex
];
79 dev
->EventQueueReadIndex
=
80 (dev
->EventQueueReadIndex
+ 1) & (EVENT_QUEUE_SIZE
- 1);
82 if ((Event
.UARTStatus
& 0x01) && (dev
->TxEventNotify
))
83 dev
->TxEventNotify(dev
, Event
.TimeStamp
);
84 if ((Event
.UARTStatus
& 0x02) && (dev
->RxEventNotify
))
85 dev
->RxEventNotify(dev
, Event
.TimeStamp
,
90 static void demux_tasklet(unsigned long data
)
92 struct ngene_channel
*chan
= (struct ngene_channel
*)data
;
93 struct SBufferHeader
*Cur
= chan
->nextBuffer
;
95 spin_lock_irq(&chan
->state_lock
);
97 while (Cur
->ngeneBuffer
.SR
.Flags
& 0x80) {
98 if (chan
->mode
& NGENE_IO_TSOUT
) {
99 u32 Flags
= chan
->DataFormatFlags
;
100 if (Cur
->ngeneBuffer
.SR
.Flags
& 0x20)
101 Flags
|= BEF_OVERFLOW
;
102 if (chan
->pBufferExchange
) {
103 if (!chan
->pBufferExchange(chan
,
105 chan
->Capture1Length
,
110 Clear in service flag to make sure we
111 get called on next interrupt again.
112 leave fill/empty (0x80) flag alone
113 to avoid hardware running out of
114 buffers during startup, we hold only
115 in run state ( the source may be late
119 if (chan
->HWState
== HWSTATE_RUN
) {
120 Cur
->ngeneBuffer
.SR
.Flags
&=
123 /* Stop processing stream */
126 /* We got a valid buffer,
127 so switch to run state */
128 chan
->HWState
= HWSTATE_RUN
;
131 printk(KERN_ERR DEVICE_NAME
": OOPS\n");
132 if (chan
->HWState
== HWSTATE_RUN
) {
133 Cur
->ngeneBuffer
.SR
.Flags
&= ~0x40;
134 break; /* Stop processing stream */
137 if (chan
->AudioDTOUpdated
) {
138 printk(KERN_INFO DEVICE_NAME
139 ": Update AudioDTO = %d\n",
140 chan
->AudioDTOValue
);
141 Cur
->ngeneBuffer
.SR
.DTOUpdate
=
143 chan
->AudioDTOUpdated
= 0;
146 if (chan
->HWState
== HWSTATE_RUN
) {
147 u32 Flags
= chan
->DataFormatFlags
;
148 IBufferExchange
*exch1
= chan
->pBufferExchange
;
149 IBufferExchange
*exch2
= chan
->pBufferExchange2
;
150 if (Cur
->ngeneBuffer
.SR
.Flags
& 0x01)
151 Flags
|= BEF_EVEN_FIELD
;
152 if (Cur
->ngeneBuffer
.SR
.Flags
& 0x20)
153 Flags
|= BEF_OVERFLOW
;
154 spin_unlock_irq(&chan
->state_lock
);
156 exch1(chan
, Cur
->Buffer1
,
157 chan
->Capture1Length
,
158 Cur
->ngeneBuffer
.SR
.Clock
,
161 exch2(chan
, Cur
->Buffer2
,
162 chan
->Capture2Length
,
163 Cur
->ngeneBuffer
.SR
.Clock
,
165 spin_lock_irq(&chan
->state_lock
);
166 } else if (chan
->HWState
!= HWSTATE_STOP
)
167 chan
->HWState
= HWSTATE_RUN
;
169 Cur
->ngeneBuffer
.SR
.Flags
= 0x00;
172 chan
->nextBuffer
= Cur
;
174 spin_unlock_irq(&chan
->state_lock
);
177 static irqreturn_t
irq_handler(int irq
, void *dev_id
)
179 struct ngene
*dev
= (struct ngene
*)dev_id
;
181 irqreturn_t rc
= IRQ_NONE
;
185 if (dev
->BootFirmware
) {
186 icounts
= ngreadl(NGENE_INT_COUNTS
);
187 if (icounts
!= dev
->icounts
) {
188 ngwritel(0, FORCE_NMI
);
190 wake_up(&dev
->cmd_wq
);
191 dev
->icounts
= icounts
;
197 ngwritel(0, FORCE_NMI
);
199 spin_lock(&dev
->cmd_lock
);
200 tmpCmdDoneByte
= dev
->CmdDoneByte
;
201 if (tmpCmdDoneByte
&&
203 (dev
->ngenetohost
[0] == 1 && dev
->ngenetohost
[1] != 0))) {
204 dev
->CmdDoneByte
= NULL
;
206 wake_up(&dev
->cmd_wq
);
209 spin_unlock(&dev
->cmd_lock
);
211 if (dev
->EventBuffer
->EventStatus
& 0x80) {
213 (dev
->EventQueueWriteIndex
+ 1) &
214 (EVENT_QUEUE_SIZE
- 1);
215 if (nextWriteIndex
!= dev
->EventQueueReadIndex
) {
216 dev
->EventQueue
[dev
->EventQueueWriteIndex
] =
218 dev
->EventQueueWriteIndex
= nextWriteIndex
;
220 printk(KERN_ERR DEVICE_NAME
": event overflow\n");
221 dev
->EventQueueOverflowCount
+= 1;
222 dev
->EventQueueOverflowFlag
= 1;
224 dev
->EventBuffer
->EventStatus
&= ~0x80;
225 tasklet_schedule(&dev
->event_tasklet
);
231 spin_lock(&dev
->channel
[i
].state_lock
);
232 /* if (dev->channel[i].State>=KSSTATE_RUN) { */
233 if (dev
->channel
[i
].nextBuffer
) {
234 if ((dev
->channel
[i
].nextBuffer
->
235 ngeneBuffer
.SR
.Flags
& 0xC0) == 0x80) {
236 dev
->channel
[i
].nextBuffer
->
237 ngeneBuffer
.SR
.Flags
|= 0x40;
239 &dev
->channel
[i
].demux_tasklet
);
243 spin_unlock(&dev
->channel
[i
].state_lock
);
246 /* Request might have been processed by a previous call. */
250 /****************************************************************************/
251 /* nGene command interface **************************************************/
252 /****************************************************************************/
254 static void dump_command_io(struct ngene
*dev
)
258 ngcpyfrom(buf
, HOST_TO_NGENE
, 8);
259 printk(KERN_ERR
"host_to_ngene (%04x): %*ph\n", HOST_TO_NGENE
, 8, buf
);
261 ngcpyfrom(buf
, NGENE_TO_HOST
, 8);
262 printk(KERN_ERR
"ngene_to_host (%04x): %*ph\n", NGENE_TO_HOST
, 8, buf
);
264 b
= dev
->hosttongene
;
265 printk(KERN_ERR
"dev->hosttongene (%p): %*ph\n", b
, 8, b
);
267 b
= dev
->ngenetohost
;
268 printk(KERN_ERR
"dev->ngenetohost (%p): %*ph\n", b
, 8, b
);
271 static int ngene_command_mutex(struct ngene
*dev
, struct ngene_command
*com
)
278 if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_PREPARE
) {
279 dev
->BootFirmware
= 1;
280 dev
->icounts
= ngreadl(NGENE_INT_COUNTS
);
281 ngwritel(0, NGENE_COMMAND
);
282 ngwritel(0, NGENE_COMMAND_HI
);
283 ngwritel(0, NGENE_STATUS
);
284 ngwritel(0, NGENE_STATUS_HI
);
285 ngwritel(0, NGENE_EVENT
);
286 ngwritel(0, NGENE_EVENT_HI
);
287 } else if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_FINISH
) {
288 u64 fwio
= dev
->PAFWInterfaceBuffer
;
290 ngwritel(fwio
& 0xffffffff, NGENE_COMMAND
);
291 ngwritel(fwio
>> 32, NGENE_COMMAND_HI
);
292 ngwritel((fwio
+ 256) & 0xffffffff, NGENE_STATUS
);
293 ngwritel((fwio
+ 256) >> 32, NGENE_STATUS_HI
);
294 ngwritel((fwio
+ 512) & 0xffffffff, NGENE_EVENT
);
295 ngwritel((fwio
+ 512) >> 32, NGENE_EVENT_HI
);
298 memcpy(dev
->FWInterfaceBuffer
, com
->cmd
.raw8
, com
->in_len
+ 2);
300 if (dev
->BootFirmware
)
301 ngcpyto(HOST_TO_NGENE
, com
->cmd
.raw8
, com
->in_len
+ 2);
303 spin_lock_irq(&dev
->cmd_lock
);
304 tmpCmdDoneByte
= dev
->ngenetohost
+ com
->out_len
;
308 dev
->ngenetohost
[0] = 0;
309 dev
->ngenetohost
[1] = 0;
310 dev
->CmdDoneByte
= tmpCmdDoneByte
;
311 spin_unlock_irq(&dev
->cmd_lock
);
314 ngwritel(1, FORCE_INT
);
316 ret
= wait_event_timeout(dev
->cmd_wq
, dev
->cmd_done
== 1, 2 * HZ
);
318 /*ngwritel(0, FORCE_NMI);*/
320 printk(KERN_ERR DEVICE_NAME
321 ": Command timeout cmd=%02x prev=%02x\n",
322 com
->cmd
.hdr
.Opcode
, dev
->prev_cmd
);
323 dump_command_io(dev
);
326 if (com
->cmd
.hdr
.Opcode
== CMD_FWLOAD_FINISH
)
327 dev
->BootFirmware
= 0;
329 dev
->prev_cmd
= com
->cmd
.hdr
.Opcode
;
334 memcpy(com
->cmd
.raw8
, dev
->ngenetohost
, com
->out_len
);
339 int ngene_command(struct ngene
*dev
, struct ngene_command
*com
)
343 down(&dev
->cmd_mutex
);
344 result
= ngene_command_mutex(dev
, com
);
350 static int ngene_command_load_firmware(struct ngene
*dev
,
351 u8
*ngene_fw
, u32 size
)
353 #define FIRSTCHUNK (1024)
355 struct ngene_command com
;
357 com
.cmd
.hdr
.Opcode
= CMD_FWLOAD_PREPARE
;
358 com
.cmd
.hdr
.Length
= 0;
362 ngene_command(dev
, &com
);
364 cleft
= (size
+ 3) & ~3;
365 if (cleft
> FIRSTCHUNK
) {
366 ngcpyto(PROGRAM_SRAM
+ FIRSTCHUNK
, ngene_fw
+ FIRSTCHUNK
,
370 ngcpyto(DATA_FIFO_AREA
, ngene_fw
, cleft
);
372 memset(&com
, 0, sizeof(struct ngene_command
));
373 com
.cmd
.hdr
.Opcode
= CMD_FWLOAD_FINISH
;
374 com
.cmd
.hdr
.Length
= 4;
375 com
.cmd
.FWLoadFinish
.Address
= DATA_FIFO_AREA
;
376 com
.cmd
.FWLoadFinish
.Length
= (unsigned short)cleft
;
380 return ngene_command(dev
, &com
);
384 static int ngene_command_config_buf(struct ngene
*dev
, u8 config
)
386 struct ngene_command com
;
388 com
.cmd
.hdr
.Opcode
= CMD_CONFIGURE_BUFFER
;
389 com
.cmd
.hdr
.Length
= 1;
390 com
.cmd
.ConfigureBuffers
.config
= config
;
394 if (ngene_command(dev
, &com
) < 0)
399 static int ngene_command_config_free_buf(struct ngene
*dev
, u8
*config
)
401 struct ngene_command com
;
403 com
.cmd
.hdr
.Opcode
= CMD_CONFIGURE_FREE_BUFFER
;
404 com
.cmd
.hdr
.Length
= 6;
405 memcpy(&com
.cmd
.ConfigureBuffers
.config
, config
, 6);
409 if (ngene_command(dev
, &com
) < 0)
415 int ngene_command_gpio_set(struct ngene
*dev
, u8 select
, u8 level
)
417 struct ngene_command com
;
419 com
.cmd
.hdr
.Opcode
= CMD_SET_GPIO_PIN
;
420 com
.cmd
.hdr
.Length
= 1;
421 com
.cmd
.SetGpioPin
.select
= select
| (level
<< 7);
425 return ngene_command(dev
, &com
);
430 02000640 is sample on rising edge.
431 02000740 is sample on falling edge.
432 02000040 is ignore "valid" signal
434 0: FD_CTL1 Bit 7,6 must be 0,1
435 7 disable(fw controlled)
440 1,0 0-no sync, 1-use ext. start, 2-use 0x47, 3-both
441 1: FD_CTL2 has 3-valid must be hi, 2-use valid, 1-edge
442 2: FD_STA is read-only. 0-sync
443 3: FD_INSYNC is number of 47s to trigger "in sync".
444 4: FD_OUTSYNC is number of 47s to trigger "out of sync".
445 5: FD_MAXBYTE1 is low-order of bytes per packet.
446 6: FD_MAXBYTE2 is high-order of bytes per packet.
447 7: Top byte is unused.
450 /****************************************************************************/
452 static u8 TSFeatureDecoderSetup
[8 * 5] = {
453 0x42, 0x00, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00,
454 0x40, 0x06, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* DRXH */
455 0x71, 0x07, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* DRXHser */
456 0x72, 0x00, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* S2ser */
457 0x40, 0x07, 0x00, 0x02, 0x02, 0xbc, 0x00, 0x00, /* LGDT3303 */
460 /* Set NGENE I2S Config to 16 bit packed */
461 static u8 I2SConfiguration
[] = {
462 0x00, 0x10, 0x00, 0x00,
463 0x80, 0x10, 0x00, 0x00,
466 static u8 SPDIFConfiguration
[10] = {
467 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
470 /* Set NGENE I2S Config to transport stream compatible mode */
472 static u8 TS_I2SConfiguration
[4] = { 0x3E, 0x18, 0x00, 0x00 };
474 static u8 TS_I2SOutConfiguration
[4] = { 0x80, 0x04, 0x00, 0x00 };
476 static u8 ITUDecoderSetup
[4][16] = {
477 {0x1c, 0x13, 0x01, 0x68, 0x3d, 0x90, 0x14, 0x20, /* SDTV */
478 0x00, 0x00, 0x01, 0xb0, 0x9c, 0x00, 0x00, 0x00},
479 {0x9c, 0x03, 0x23, 0xC0, 0x60, 0x0E, 0x13, 0x00,
480 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
481 {0x9f, 0x00, 0x23, 0xC0, 0x60, 0x0F, 0x13, 0x00, /* HDTV 1080i50 */
482 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
483 {0x9c, 0x01, 0x23, 0xC0, 0x60, 0x0E, 0x13, 0x00, /* HDTV 1080i60 */
484 0x00, 0x00, 0x00, 0x01, 0xB0, 0x00, 0x00, 0x00},
489 * 27p50 9f 00 22 80 42 69 18 ...
490 * 27p60 93 00 22 80 82 69 1c ...
493 /* Maxbyte to 1144 (for raw data) */
494 static u8 ITUFeatureDecoderSetup
[8] = {
495 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x04, 0x00
498 void FillTSBuffer(void *Buffer
, int Length
, u32 Flags
)
502 memset(Buffer
, TS_FILLER
, Length
);
504 if (Flags
& DF_SWAP32
)
514 static void flush_buffers(struct ngene_channel
*chan
)
520 spin_lock_irq(&chan
->state_lock
);
521 val
= chan
->nextBuffer
->ngeneBuffer
.SR
.Flags
& 0x80;
522 spin_unlock_irq(&chan
->state_lock
);
526 static void clear_buffers(struct ngene_channel
*chan
)
528 struct SBufferHeader
*Cur
= chan
->nextBuffer
;
531 memset(&Cur
->ngeneBuffer
.SR
, 0, sizeof(Cur
->ngeneBuffer
.SR
));
532 if (chan
->mode
& NGENE_IO_TSOUT
)
533 FillTSBuffer(Cur
->Buffer1
,
534 chan
->Capture1Length
,
535 chan
->DataFormatFlags
);
537 } while (Cur
!= chan
->nextBuffer
);
539 if (chan
->mode
& NGENE_IO_TSOUT
) {
540 chan
->nextBuffer
->ngeneBuffer
.SR
.DTOUpdate
=
542 chan
->AudioDTOUpdated
= 0;
544 Cur
= chan
->TSIdleBuffer
.Head
;
547 memset(&Cur
->ngeneBuffer
.SR
, 0,
548 sizeof(Cur
->ngeneBuffer
.SR
));
549 FillTSBuffer(Cur
->Buffer1
,
550 chan
->Capture1Length
,
551 chan
->DataFormatFlags
);
553 } while (Cur
!= chan
->TSIdleBuffer
.Head
);
557 static int ngene_command_stream_control(struct ngene
*dev
, u8 stream
,
558 u8 control
, u8 mode
, u8 flags
)
560 struct ngene_channel
*chan
= &dev
->channel
[stream
];
561 struct ngene_command com
;
562 u16 BsUVI
= ((stream
& 1) ? 0x9400 : 0x9300);
563 u16 BsSDI
= ((stream
& 1) ? 0x9600 : 0x9500);
564 u16 BsSPI
= ((stream
& 1) ? 0x9800 : 0x9700);
567 down(&dev
->stream_mutex
);
568 memset(&com
, 0, sizeof(com
));
569 com
.cmd
.hdr
.Opcode
= CMD_CONTROL
;
570 com
.cmd
.hdr
.Length
= sizeof(struct FW_STREAM_CONTROL
) - 2;
571 com
.cmd
.StreamControl
.Stream
= stream
| (control
? 8 : 0);
572 if (chan
->mode
& NGENE_IO_TSOUT
)
573 com
.cmd
.StreamControl
.Stream
|= 0x07;
574 com
.cmd
.StreamControl
.Control
= control
|
575 (flags
& SFLAG_ORDER_LUMA_CHROMA
);
576 com
.cmd
.StreamControl
.Mode
= mode
;
577 com
.in_len
= sizeof(struct FW_STREAM_CONTROL
);
580 dprintk(KERN_INFO DEVICE_NAME
581 ": Stream=%02x, Control=%02x, Mode=%02x\n",
582 com
.cmd
.StreamControl
.Stream
, com
.cmd
.StreamControl
.Control
,
583 com
.cmd
.StreamControl
.Mode
);
587 if (!(control
& 0x80)) {
588 spin_lock_irq(&chan
->state_lock
);
589 if (chan
->State
== KSSTATE_RUN
) {
590 chan
->State
= KSSTATE_ACQUIRE
;
591 chan
->HWState
= HWSTATE_STOP
;
592 spin_unlock_irq(&chan
->state_lock
);
593 if (ngene_command(dev
, &com
) < 0) {
594 up(&dev
->stream_mutex
);
597 /* clear_buffers(chan); */
599 up(&dev
->stream_mutex
);
602 spin_unlock_irq(&chan
->state_lock
);
603 up(&dev
->stream_mutex
);
607 if (mode
& SMODE_AUDIO_CAPTURE
) {
608 com
.cmd
.StreamControl
.CaptureBlockCount
=
609 chan
->Capture1Length
/ AUDIO_BLOCK_SIZE
;
610 com
.cmd
.StreamControl
.Buffer_Address
= chan
->RingBuffer
.PAHead
;
611 } else if (mode
& SMODE_TRANSPORT_STREAM
) {
612 com
.cmd
.StreamControl
.CaptureBlockCount
=
613 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
614 com
.cmd
.StreamControl
.MaxLinesPerField
=
615 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
616 com
.cmd
.StreamControl
.Buffer_Address
=
617 chan
->TSRingBuffer
.PAHead
;
618 if (chan
->mode
& NGENE_IO_TSOUT
) {
619 com
.cmd
.StreamControl
.BytesPerVBILine
=
620 chan
->Capture1Length
/ TS_BLOCK_SIZE
;
621 com
.cmd
.StreamControl
.Stream
|= 0x07;
624 com
.cmd
.StreamControl
.BytesPerVideoLine
= chan
->nBytesPerLine
;
625 com
.cmd
.StreamControl
.MaxLinesPerField
= chan
->nLines
;
626 com
.cmd
.StreamControl
.MinLinesPerField
= 100;
627 com
.cmd
.StreamControl
.Buffer_Address
= chan
->RingBuffer
.PAHead
;
629 if (mode
& SMODE_VBI_CAPTURE
) {
630 com
.cmd
.StreamControl
.MaxVBILinesPerField
=
632 com
.cmd
.StreamControl
.MinVBILinesPerField
= 0;
633 com
.cmd
.StreamControl
.BytesPerVBILine
=
634 chan
->nBytesPerVBILine
;
636 if (flags
& SFLAG_COLORBAR
)
637 com
.cmd
.StreamControl
.Stream
|= 0x04;
640 spin_lock_irq(&chan
->state_lock
);
641 if (mode
& SMODE_AUDIO_CAPTURE
) {
642 chan
->nextBuffer
= chan
->RingBuffer
.Head
;
643 if (mode
& SMODE_AUDIO_SPDIF
) {
644 com
.cmd
.StreamControl
.SetupDataLen
=
645 sizeof(SPDIFConfiguration
);
646 com
.cmd
.StreamControl
.SetupDataAddr
= BsSPI
;
647 memcpy(com
.cmd
.StreamControl
.SetupData
,
648 SPDIFConfiguration
, sizeof(SPDIFConfiguration
));
650 com
.cmd
.StreamControl
.SetupDataLen
= 4;
651 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDI
;
652 memcpy(com
.cmd
.StreamControl
.SetupData
,
654 4 * dev
->card_info
->i2s
[stream
], 4);
656 } else if (mode
& SMODE_TRANSPORT_STREAM
) {
657 chan
->nextBuffer
= chan
->TSRingBuffer
.Head
;
658 if (stream
>= STREAM_AUDIOIN1
) {
659 if (chan
->mode
& NGENE_IO_TSOUT
) {
660 com
.cmd
.StreamControl
.SetupDataLen
=
661 sizeof(TS_I2SOutConfiguration
);
662 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDO
;
663 memcpy(com
.cmd
.StreamControl
.SetupData
,
664 TS_I2SOutConfiguration
,
665 sizeof(TS_I2SOutConfiguration
));
667 com
.cmd
.StreamControl
.SetupDataLen
=
668 sizeof(TS_I2SConfiguration
);
669 com
.cmd
.StreamControl
.SetupDataAddr
= BsSDI
;
670 memcpy(com
.cmd
.StreamControl
.SetupData
,
672 sizeof(TS_I2SConfiguration
));
675 com
.cmd
.StreamControl
.SetupDataLen
= 8;
676 com
.cmd
.StreamControl
.SetupDataAddr
= BsUVI
+ 0x10;
677 memcpy(com
.cmd
.StreamControl
.SetupData
,
678 TSFeatureDecoderSetup
+
679 8 * dev
->card_info
->tsf
[stream
], 8);
682 chan
->nextBuffer
= chan
->RingBuffer
.Head
;
683 com
.cmd
.StreamControl
.SetupDataLen
=
684 16 + sizeof(ITUFeatureDecoderSetup
);
685 com
.cmd
.StreamControl
.SetupDataAddr
= BsUVI
;
686 memcpy(com
.cmd
.StreamControl
.SetupData
,
687 ITUDecoderSetup
[chan
->itumode
], 16);
688 memcpy(com
.cmd
.StreamControl
.SetupData
+ 16,
689 ITUFeatureDecoderSetup
, sizeof(ITUFeatureDecoderSetup
));
692 chan
->State
= KSSTATE_RUN
;
693 if (mode
& SMODE_TRANSPORT_STREAM
)
694 chan
->HWState
= HWSTATE_RUN
;
696 chan
->HWState
= HWSTATE_STARTUP
;
697 spin_unlock_irq(&chan
->state_lock
);
699 if (ngene_command(dev
, &com
) < 0) {
700 up(&dev
->stream_mutex
);
703 up(&dev
->stream_mutex
);
707 void set_transfer(struct ngene_channel
*chan
, int state
)
709 u8 control
= 0, mode
= 0, flags
= 0;
710 struct ngene
*dev
= chan
->dev
;
714 printk(KERN_INFO DEVICE_NAME ": st %d\n", state);
720 printk(KERN_INFO DEVICE_NAME
": already running\n");
724 if (!chan
->running
) {
725 printk(KERN_INFO DEVICE_NAME
": already stopped\n");
730 if (dev
->card_info
->switch_ctrl
)
731 dev
->card_info
->switch_ctrl(chan
, 1, state
^ 1);
734 spin_lock_irq(&chan
->state_lock
);
736 /* printk(KERN_INFO DEVICE_NAME ": lock=%08x\n",
738 dvb_ringbuffer_flush(&dev
->tsout_rbuf
);
740 if (chan
->mode
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
741 chan
->Capture1Length
= 512 * 188;
742 mode
= SMODE_TRANSPORT_STREAM
;
744 if (chan
->mode
& NGENE_IO_TSOUT
) {
745 chan
->pBufferExchange
= tsout_exchange
;
746 /* 0x66666666 = 50MHz *2^33 /250MHz */
747 chan
->AudioDTOValue
= 0x80000000;
748 chan
->AudioDTOUpdated
= 1;
750 if (chan
->mode
& NGENE_IO_TSIN
)
751 chan
->pBufferExchange
= tsin_exchange
;
752 spin_unlock_irq(&chan
->state_lock
);
754 /* else printk(KERN_INFO DEVICE_NAME ": lock=%08x\n",
757 ret
= ngene_command_stream_control(dev
, chan
->number
,
758 control
, mode
, flags
);
760 chan
->running
= state
;
762 printk(KERN_ERR DEVICE_NAME
": set_transfer %d failed\n",
765 spin_lock_irq(&chan
->state_lock
);
766 chan
->pBufferExchange
= NULL
;
767 dvb_ringbuffer_flush(&dev
->tsout_rbuf
);
768 spin_unlock_irq(&chan
->state_lock
);
773 /****************************************************************************/
774 /* nGene hardware init and release functions ********************************/
775 /****************************************************************************/
777 static void free_ringbuffer(struct ngene
*dev
, struct SRingBufferDescriptor
*rb
)
779 struct SBufferHeader
*Cur
= rb
->Head
;
785 for (j
= 0; j
< rb
->NumBuffers
; j
++, Cur
= Cur
->Next
) {
787 pci_free_consistent(dev
->pci_dev
,
790 Cur
->scList1
->Address
);
793 pci_free_consistent(dev
->pci_dev
,
796 Cur
->scList2
->Address
);
800 pci_free_consistent(dev
->pci_dev
, rb
->SCListMemSize
,
801 rb
->SCListMem
, rb
->PASCListMem
);
803 pci_free_consistent(dev
->pci_dev
, rb
->MemSize
, rb
->Head
, rb
->PAHead
);
806 static void free_idlebuffer(struct ngene
*dev
,
807 struct SRingBufferDescriptor
*rb
,
808 struct SRingBufferDescriptor
*tb
)
811 struct SBufferHeader
*Cur
= tb
->Head
;
815 free_ringbuffer(dev
, rb
);
816 for (j
= 0; j
< tb
->NumBuffers
; j
++, Cur
= Cur
->Next
) {
819 Cur
->ngeneBuffer
.Address_of_first_entry_2
= 0;
820 Cur
->ngeneBuffer
.Number_of_entries_2
= 0;
824 static void free_common_buffers(struct ngene
*dev
)
827 struct ngene_channel
*chan
;
829 for (i
= STREAM_VIDEOIN1
; i
< MAX_STREAM
; i
++) {
830 chan
= &dev
->channel
[i
];
831 free_idlebuffer(dev
, &chan
->TSIdleBuffer
, &chan
->TSRingBuffer
);
832 free_ringbuffer(dev
, &chan
->RingBuffer
);
833 free_ringbuffer(dev
, &chan
->TSRingBuffer
);
836 if (dev
->OverflowBuffer
)
837 pci_free_consistent(dev
->pci_dev
,
838 OVERFLOW_BUFFER_SIZE
,
839 dev
->OverflowBuffer
, dev
->PAOverflowBuffer
);
841 if (dev
->FWInterfaceBuffer
)
842 pci_free_consistent(dev
->pci_dev
,
844 dev
->FWInterfaceBuffer
,
845 dev
->PAFWInterfaceBuffer
);
848 /****************************************************************************/
849 /* Ring buffer handling *****************************************************/
850 /****************************************************************************/
852 static int create_ring_buffer(struct pci_dev
*pci_dev
,
853 struct SRingBufferDescriptor
*descr
, u32 NumBuffers
)
856 struct SBufferHeader
*Head
;
858 u32 MemSize
= SIZEOF_SBufferHeader
* NumBuffers
;
859 u64 PARingBufferHead
;
861 u64 PARingBufferNext
;
862 struct SBufferHeader
*Cur
, *Next
;
867 descr
->NumBuffers
= 0;
872 Head
= pci_alloc_consistent(pci_dev
, MemSize
, &tmp
);
873 PARingBufferHead
= tmp
;
878 memset(Head
, 0, MemSize
);
880 PARingBufferCur
= PARingBufferHead
;
883 for (i
= 0; i
< NumBuffers
- 1; i
++) {
884 Next
= (struct SBufferHeader
*)
885 (((u8
*) Cur
) + SIZEOF_SBufferHeader
);
886 PARingBufferNext
= PARingBufferCur
+ SIZEOF_SBufferHeader
;
888 Cur
->ngeneBuffer
.Next
= PARingBufferNext
;
890 PARingBufferCur
= PARingBufferNext
;
892 /* Last Buffer points back to first one */
894 Cur
->ngeneBuffer
.Next
= PARingBufferHead
;
897 descr
->MemSize
= MemSize
;
898 descr
->PAHead
= PARingBufferHead
;
899 descr
->NumBuffers
= NumBuffers
;
904 static int AllocateRingBuffers(struct pci_dev
*pci_dev
,
906 struct SRingBufferDescriptor
*pRingBuffer
,
907 u32 Buffer1Length
, u32 Buffer2Length
)
911 u32 SCListMemSize
= pRingBuffer
->NumBuffers
912 * ((Buffer2Length
!= 0) ? (NUM_SCATTER_GATHER_ENTRIES
* 2) :
913 NUM_SCATTER_GATHER_ENTRIES
)
914 * sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
917 struct HW_SCATTER_GATHER_ELEMENT
*SCListEntry
;
919 struct SBufferHeader
*Cur
;
922 if (SCListMemSize
< 4096)
923 SCListMemSize
= 4096;
925 SCListMem
= pci_alloc_consistent(pci_dev
, SCListMemSize
, &tmp
);
928 if (SCListMem
== NULL
)
931 memset(SCListMem
, 0, SCListMemSize
);
933 pRingBuffer
->SCListMem
= SCListMem
;
934 pRingBuffer
->PASCListMem
= PASCListMem
;
935 pRingBuffer
->SCListMemSize
= SCListMemSize
;
936 pRingBuffer
->Buffer1Length
= Buffer1Length
;
937 pRingBuffer
->Buffer2Length
= Buffer2Length
;
939 SCListEntry
= SCListMem
;
940 PASCListEntry
= PASCListMem
;
941 Cur
= pRingBuffer
->Head
;
943 for (i
= 0; i
< pRingBuffer
->NumBuffers
; i
+= 1, Cur
= Cur
->Next
) {
946 void *Buffer
= pci_alloc_consistent(pci_dev
, Buffer1Length
,
953 Cur
->Buffer1
= Buffer
;
955 SCListEntry
->Address
= PABuffer
;
956 SCListEntry
->Length
= Buffer1Length
;
958 Cur
->scList1
= SCListEntry
;
959 Cur
->ngeneBuffer
.Address_of_first_entry_1
= PASCListEntry
;
960 Cur
->ngeneBuffer
.Number_of_entries_1
=
961 NUM_SCATTER_GATHER_ENTRIES
;
964 PASCListEntry
+= sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
966 #if NUM_SCATTER_GATHER_ENTRIES > 1
967 for (j
= 0; j
< NUM_SCATTER_GATHER_ENTRIES
- 1; j
+= 1) {
968 SCListEntry
->Address
= of
;
969 SCListEntry
->Length
= OVERFLOW_BUFFER_SIZE
;
972 sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
979 Buffer
= pci_alloc_consistent(pci_dev
, Buffer2Length
, &tmp
);
985 Cur
->Buffer2
= Buffer
;
987 SCListEntry
->Address
= PABuffer
;
988 SCListEntry
->Length
= Buffer2Length
;
990 Cur
->scList2
= SCListEntry
;
991 Cur
->ngeneBuffer
.Address_of_first_entry_2
= PASCListEntry
;
992 Cur
->ngeneBuffer
.Number_of_entries_2
=
993 NUM_SCATTER_GATHER_ENTRIES
;
996 PASCListEntry
+= sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
998 #if NUM_SCATTER_GATHER_ENTRIES > 1
999 for (j
= 0; j
< NUM_SCATTER_GATHER_ENTRIES
- 1; j
++) {
1000 SCListEntry
->Address
= of
;
1001 SCListEntry
->Length
= OVERFLOW_BUFFER_SIZE
;
1004 sizeof(struct HW_SCATTER_GATHER_ELEMENT
);
1013 static int FillTSIdleBuffer(struct SRingBufferDescriptor
*pIdleBuffer
,
1014 struct SRingBufferDescriptor
*pRingBuffer
)
1016 /* Copy pointer to scatter gather list in TSRingbuffer
1017 structure for buffer 2
1018 Load number of buffer
1020 u32 n
= pRingBuffer
->NumBuffers
;
1022 /* Point to first buffer entry */
1023 struct SBufferHeader
*Cur
= pRingBuffer
->Head
;
1025 /* Loop thru all buffer and set Buffer 2 pointers to TSIdlebuffer */
1026 for (i
= 0; i
< n
; i
++) {
1027 Cur
->Buffer2
= pIdleBuffer
->Head
->Buffer1
;
1028 Cur
->scList2
= pIdleBuffer
->Head
->scList1
;
1029 Cur
->ngeneBuffer
.Address_of_first_entry_2
=
1030 pIdleBuffer
->Head
->ngeneBuffer
.
1031 Address_of_first_entry_1
;
1032 Cur
->ngeneBuffer
.Number_of_entries_2
=
1033 pIdleBuffer
->Head
->ngeneBuffer
.Number_of_entries_1
;
1039 static u32 RingBufferSizes
[MAX_STREAM
] = {
1047 static u32 Buffer1Sizes
[MAX_STREAM
] = {
1048 MAX_VIDEO_BUFFER_SIZE
,
1049 MAX_VIDEO_BUFFER_SIZE
,
1050 MAX_AUDIO_BUFFER_SIZE
,
1051 MAX_AUDIO_BUFFER_SIZE
,
1052 MAX_AUDIO_BUFFER_SIZE
1055 static u32 Buffer2Sizes
[MAX_STREAM
] = {
1056 MAX_VBI_BUFFER_SIZE
,
1057 MAX_VBI_BUFFER_SIZE
,
1064 static int AllocCommonBuffers(struct ngene
*dev
)
1068 dev
->FWInterfaceBuffer
= pci_alloc_consistent(dev
->pci_dev
, 4096,
1069 &dev
->PAFWInterfaceBuffer
);
1070 if (!dev
->FWInterfaceBuffer
)
1072 dev
->hosttongene
= dev
->FWInterfaceBuffer
;
1073 dev
->ngenetohost
= dev
->FWInterfaceBuffer
+ 256;
1074 dev
->EventBuffer
= dev
->FWInterfaceBuffer
+ 512;
1076 dev
->OverflowBuffer
= pci_zalloc_consistent(dev
->pci_dev
,
1077 OVERFLOW_BUFFER_SIZE
,
1078 &dev
->PAOverflowBuffer
);
1079 if (!dev
->OverflowBuffer
)
1082 for (i
= STREAM_VIDEOIN1
; i
< MAX_STREAM
; i
++) {
1083 int type
= dev
->card_info
->io_type
[i
];
1085 dev
->channel
[i
].State
= KSSTATE_STOP
;
1087 if (type
& (NGENE_IO_TV
| NGENE_IO_HDTV
| NGENE_IO_AIN
)) {
1088 status
= create_ring_buffer(dev
->pci_dev
,
1089 &dev
->channel
[i
].RingBuffer
,
1090 RingBufferSizes
[i
]);
1094 if (type
& (NGENE_IO_TV
| NGENE_IO_AIN
)) {
1095 status
= AllocateRingBuffers(dev
->pci_dev
,
1104 } else if (type
& NGENE_IO_HDTV
) {
1105 status
= AllocateRingBuffers(dev
->pci_dev
,
1110 MAX_HDTV_BUFFER_SIZE
,
1117 if (type
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
1119 status
= create_ring_buffer(dev
->pci_dev
,
1121 TSRingBuffer
, RING_SIZE_TS
);
1125 status
= AllocateRingBuffers(dev
->pci_dev
,
1126 dev
->PAOverflowBuffer
,
1129 MAX_TS_BUFFER_SIZE
, 0);
1134 if (type
& NGENE_IO_TSOUT
) {
1135 status
= create_ring_buffer(dev
->pci_dev
,
1140 status
= AllocateRingBuffers(dev
->pci_dev
,
1141 dev
->PAOverflowBuffer
,
1144 MAX_TS_BUFFER_SIZE
, 0);
1147 FillTSIdleBuffer(&dev
->channel
[i
].TSIdleBuffer
,
1148 &dev
->channel
[i
].TSRingBuffer
);
1154 static void ngene_release_buffers(struct ngene
*dev
)
1157 iounmap(dev
->iomem
);
1158 free_common_buffers(dev
);
1159 vfree(dev
->tsout_buf
);
1160 vfree(dev
->tsin_buf
);
1161 vfree(dev
->ain_buf
);
1162 vfree(dev
->vin_buf
);
1166 static int ngene_get_buffers(struct ngene
*dev
)
1168 if (AllocCommonBuffers(dev
))
1170 if (dev
->card_info
->io_type
[4] & NGENE_IO_TSOUT
) {
1171 dev
->tsout_buf
= vmalloc(TSOUT_BUF_SIZE
);
1172 if (!dev
->tsout_buf
)
1174 dvb_ringbuffer_init(&dev
->tsout_rbuf
,
1175 dev
->tsout_buf
, TSOUT_BUF_SIZE
);
1177 if (dev
->card_info
->io_type
[2]&NGENE_IO_TSIN
) {
1178 dev
->tsin_buf
= vmalloc(TSIN_BUF_SIZE
);
1181 dvb_ringbuffer_init(&dev
->tsin_rbuf
,
1182 dev
->tsin_buf
, TSIN_BUF_SIZE
);
1184 if (dev
->card_info
->io_type
[2] & NGENE_IO_AIN
) {
1185 dev
->ain_buf
= vmalloc(AIN_BUF_SIZE
);
1188 dvb_ringbuffer_init(&dev
->ain_rbuf
, dev
->ain_buf
, AIN_BUF_SIZE
);
1190 if (dev
->card_info
->io_type
[0] & NGENE_IO_HDTV
) {
1191 dev
->vin_buf
= vmalloc(VIN_BUF_SIZE
);
1194 dvb_ringbuffer_init(&dev
->vin_rbuf
, dev
->vin_buf
, VIN_BUF_SIZE
);
1196 dev
->iomem
= ioremap(pci_resource_start(dev
->pci_dev
, 0),
1197 pci_resource_len(dev
->pci_dev
, 0));
1204 static void ngene_init(struct ngene
*dev
)
1208 tasklet_init(&dev
->event_tasklet
, event_tasklet
, (unsigned long)dev
);
1210 memset_io(dev
->iomem
+ 0xc000, 0x00, 0x220);
1211 memset_io(dev
->iomem
+ 0xc400, 0x00, 0x100);
1213 for (i
= 0; i
< MAX_STREAM
; i
++) {
1214 dev
->channel
[i
].dev
= dev
;
1215 dev
->channel
[i
].number
= i
;
1218 dev
->fw_interface_version
= 0;
1220 ngwritel(0, NGENE_INT_ENABLE
);
1222 dev
->icounts
= ngreadl(NGENE_INT_COUNTS
);
1224 dev
->device_version
= ngreadl(DEV_VER
) & 0x0f;
1225 printk(KERN_INFO DEVICE_NAME
": Device version %d\n",
1226 dev
->device_version
);
1229 static int ngene_load_firm(struct ngene
*dev
)
1232 const struct firmware
*fw
= NULL
;
1237 version
= dev
->card_info
->fw_version
;
1244 fw_name
= "ngene_15.fw";
1245 dev
->cmd_timeout_workaround
= true;
1249 fw_name
= "ngene_16.fw";
1250 dev
->cmd_timeout_workaround
= true;
1254 fw_name
= "ngene_17.fw";
1255 dev
->cmd_timeout_workaround
= true;
1259 fw_name
= "ngene_18.fw";
1263 if (request_firmware(&fw
, fw_name
, &dev
->pci_dev
->dev
) < 0) {
1264 printk(KERN_ERR DEVICE_NAME
1265 ": Could not load firmware file %s.\n", fw_name
);
1266 printk(KERN_INFO DEVICE_NAME
1267 ": Copy %s to your hotplug directory!\n", fw_name
);
1272 if (size
!= fw
->size
) {
1273 printk(KERN_ERR DEVICE_NAME
1274 ": Firmware %s has invalid size!", fw_name
);
1277 printk(KERN_INFO DEVICE_NAME
1278 ": Loading firmware file %s.\n", fw_name
);
1279 ngene_fw
= (u8
*) fw
->data
;
1280 err
= ngene_command_load_firmware(dev
, ngene_fw
, size
);
1283 release_firmware(fw
);
1288 static void ngene_stop(struct ngene
*dev
)
1290 down(&dev
->cmd_mutex
);
1291 i2c_del_adapter(&(dev
->channel
[0].i2c_adapter
));
1292 i2c_del_adapter(&(dev
->channel
[1].i2c_adapter
));
1293 ngwritel(0, NGENE_INT_ENABLE
);
1294 ngwritel(0, NGENE_COMMAND
);
1295 ngwritel(0, NGENE_COMMAND_HI
);
1296 ngwritel(0, NGENE_STATUS
);
1297 ngwritel(0, NGENE_STATUS_HI
);
1298 ngwritel(0, NGENE_EVENT
);
1299 ngwritel(0, NGENE_EVENT_HI
);
1300 free_irq(dev
->pci_dev
->irq
, dev
);
1301 #ifdef CONFIG_PCI_MSI
1302 if (dev
->msi_enabled
)
1303 pci_disable_msi(dev
->pci_dev
);
1307 static int ngene_buffer_config(struct ngene
*dev
)
1311 if (dev
->card_info
->fw_version
>= 17) {
1312 u8 tsin12_config
[6] = { 0x60, 0x60, 0x00, 0x00, 0x00, 0x00 };
1313 u8 tsin1234_config
[6] = { 0x30, 0x30, 0x00, 0x30, 0x30, 0x00 };
1314 u8 tsio1235_config
[6] = { 0x30, 0x30, 0x00, 0x28, 0x00, 0x38 };
1315 u8
*bconf
= tsin12_config
;
1317 if (dev
->card_info
->io_type
[2]&NGENE_IO_TSIN
&&
1318 dev
->card_info
->io_type
[3]&NGENE_IO_TSIN
) {
1319 bconf
= tsin1234_config
;
1320 if (dev
->card_info
->io_type
[4]&NGENE_IO_TSOUT
&&
1322 bconf
= tsio1235_config
;
1324 stat
= ngene_command_config_free_buf(dev
, bconf
);
1326 int bconf
= BUFFER_CONFIG_4422
;
1328 if (dev
->card_info
->io_type
[3] == NGENE_IO_TSIN
)
1329 bconf
= BUFFER_CONFIG_3333
;
1330 stat
= ngene_command_config_buf(dev
, bconf
);
1336 static int ngene_start(struct ngene
*dev
)
1341 pci_set_master(dev
->pci_dev
);
1344 stat
= request_irq(dev
->pci_dev
->irq
, irq_handler
,
1345 IRQF_SHARED
, "nGene",
1350 init_waitqueue_head(&dev
->cmd_wq
);
1351 init_waitqueue_head(&dev
->tx_wq
);
1352 init_waitqueue_head(&dev
->rx_wq
);
1353 sema_init(&dev
->cmd_mutex
, 1);
1354 sema_init(&dev
->stream_mutex
, 1);
1355 sema_init(&dev
->pll_mutex
, 1);
1356 sema_init(&dev
->i2c_switch_mutex
, 1);
1357 spin_lock_init(&dev
->cmd_lock
);
1358 for (i
= 0; i
< MAX_STREAM
; i
++)
1359 spin_lock_init(&dev
->channel
[i
].state_lock
);
1360 ngwritel(1, TIMESTAMPS
);
1362 ngwritel(1, NGENE_INT_ENABLE
);
1364 stat
= ngene_load_firm(dev
);
1368 #ifdef CONFIG_PCI_MSI
1369 /* enable MSI if kernel and card support it */
1370 if (pci_msi_enabled() && dev
->card_info
->msi_supported
) {
1371 unsigned long flags
;
1373 ngwritel(0, NGENE_INT_ENABLE
);
1374 free_irq(dev
->pci_dev
->irq
, dev
);
1375 stat
= pci_enable_msi(dev
->pci_dev
);
1377 printk(KERN_INFO DEVICE_NAME
1378 ": MSI not available\n");
1379 flags
= IRQF_SHARED
;
1382 dev
->msi_enabled
= true;
1384 stat
= request_irq(dev
->pci_dev
->irq
, irq_handler
,
1385 flags
, "nGene", dev
);
1388 ngwritel(1, NGENE_INT_ENABLE
);
1392 stat
= ngene_i2c_init(dev
, 0);
1396 stat
= ngene_i2c_init(dev
, 1);
1403 ngwritel(0, NGENE_INT_ENABLE
);
1404 free_irq(dev
->pci_dev
->irq
, dev
);
1405 #ifdef CONFIG_PCI_MSI
1407 if (dev
->msi_enabled
)
1408 pci_disable_msi(dev
->pci_dev
);
1413 /****************************************************************************/
1414 /****************************************************************************/
1415 /****************************************************************************/
1417 static void release_channel(struct ngene_channel
*chan
)
1419 struct dvb_demux
*dvbdemux
= &chan
->demux
;
1420 struct ngene
*dev
= chan
->dev
;
1423 set_transfer(chan
, 0);
1425 tasklet_kill(&chan
->demux_tasklet
);
1428 dvb_unregister_device(chan
->ci_dev
);
1429 chan
->ci_dev
= NULL
;
1433 dvb_unregister_frontend(chan
->fe2
);
1436 dvb_unregister_frontend(chan
->fe
);
1437 dvb_frontend_detach(chan
->fe
);
1441 if (chan
->has_demux
) {
1442 dvb_net_release(&chan
->dvbnet
);
1443 dvbdemux
->dmx
.close(&dvbdemux
->dmx
);
1444 dvbdemux
->dmx
.remove_frontend(&dvbdemux
->dmx
,
1445 &chan
->hw_frontend
);
1446 dvbdemux
->dmx
.remove_frontend(&dvbdemux
->dmx
,
1447 &chan
->mem_frontend
);
1448 dvb_dmxdev_release(&chan
->dmxdev
);
1449 dvb_dmx_release(&chan
->demux
);
1450 chan
->has_demux
= false;
1453 if (chan
->has_adapter
) {
1454 dvb_unregister_adapter(&dev
->adapter
[chan
->number
]);
1455 chan
->has_adapter
= false;
1459 static int init_channel(struct ngene_channel
*chan
)
1461 int ret
= 0, nr
= chan
->number
;
1462 struct dvb_adapter
*adapter
= NULL
;
1463 struct dvb_demux
*dvbdemux
= &chan
->demux
;
1464 struct ngene
*dev
= chan
->dev
;
1465 struct ngene_info
*ni
= dev
->card_info
;
1466 int io
= ni
->io_type
[nr
];
1468 tasklet_init(&chan
->demux_tasklet
, demux_tasklet
, (unsigned long)chan
);
1471 chan
->mode
= chan
->type
; /* for now only one mode */
1473 if (io
& NGENE_IO_TSIN
) {
1475 if (ni
->demod_attach
[nr
]) {
1476 ret
= ni
->demod_attach
[nr
](chan
);
1480 if (chan
->fe
&& ni
->tuner_attach
[nr
]) {
1481 ret
= ni
->tuner_attach
[nr
](chan
);
1487 if (!dev
->ci
.en
&& (io
& NGENE_IO_TSOUT
))
1490 if (io
& (NGENE_IO_TSIN
| NGENE_IO_TSOUT
)) {
1491 if (nr
>= STREAM_AUDIOIN1
)
1492 chan
->DataFormatFlags
= DF_SWAP32
;
1494 if (nr
== 0 || !one_adapter
|| dev
->first_adapter
== NULL
) {
1495 adapter
= &dev
->adapter
[nr
];
1496 ret
= dvb_register_adapter(adapter
, "nGene",
1498 &chan
->dev
->pci_dev
->dev
,
1502 if (dev
->first_adapter
== NULL
)
1503 dev
->first_adapter
= adapter
;
1504 chan
->has_adapter
= true;
1506 adapter
= dev
->first_adapter
;
1509 if (dev
->ci
.en
&& (io
& NGENE_IO_TSOUT
)) {
1510 dvb_ca_en50221_init(adapter
, dev
->ci
.en
, 0, 1);
1511 set_transfer(chan
, 1);
1512 chan
->dev
->channel
[2].DataFormatFlags
= DF_SWAP32
;
1513 set_transfer(&chan
->dev
->channel
[2], 1);
1514 dvb_register_device(adapter
, &chan
->ci_dev
,
1515 &ngene_dvbdev_ci
, (void *) chan
,
1522 if (dvb_register_frontend(adapter
, chan
->fe
) < 0)
1524 chan
->has_demux
= true;
1527 if (dvb_register_frontend(adapter
, chan
->fe2
) < 0)
1530 chan
->fe2
->tuner_priv
= chan
->fe
->tuner_priv
;
1531 memcpy(&chan
->fe2
->ops
.tuner_ops
,
1532 &chan
->fe
->ops
.tuner_ops
,
1533 sizeof(struct dvb_tuner_ops
));
1537 if (chan
->has_demux
) {
1538 ret
= my_dvb_dmx_ts_card_init(dvbdemux
, "SW demux",
1540 ngene_stop_feed
, chan
);
1541 ret
= my_dvb_dmxdev_ts_card_init(&chan
->dmxdev
, &chan
->demux
,
1543 &chan
->mem_frontend
, adapter
);
1544 ret
= dvb_net_init(adapter
, &chan
->dvbnet
, &chan
->demux
.dmx
);
1551 dvb_frontend_detach(chan
->fe
);
1554 release_channel(chan
);
1558 static int init_channels(struct ngene
*dev
)
1562 for (i
= 0; i
< MAX_STREAM
; i
++) {
1563 dev
->channel
[i
].number
= i
;
1564 if (init_channel(&dev
->channel
[i
]) < 0) {
1565 for (j
= i
- 1; j
>= 0; j
--)
1566 release_channel(&dev
->channel
[j
]);
1573 static struct cxd2099_cfg cxd_cfg
= {
1580 static void cxd_attach(struct ngene
*dev
)
1582 struct ngene_ci
*ci
= &dev
->ci
;
1584 ci
->en
= cxd2099_attach(&cxd_cfg
, dev
, &dev
->channel
[0].i2c_adapter
);
1589 static void cxd_detach(struct ngene
*dev
)
1591 struct ngene_ci
*ci
= &dev
->ci
;
1593 dvb_ca_en50221_release(ci
->en
);
1598 /***********************************/
1599 /* workaround for shutdown failure */
1600 /***********************************/
1602 static void ngene_unlink(struct ngene
*dev
)
1604 struct ngene_command com
;
1606 com
.cmd
.hdr
.Opcode
= CMD_MEM_WRITE
;
1607 com
.cmd
.hdr
.Length
= 3;
1608 com
.cmd
.MemoryWrite
.address
= 0x910c;
1609 com
.cmd
.MemoryWrite
.data
= 0xff;
1613 down(&dev
->cmd_mutex
);
1614 ngwritel(0, NGENE_INT_ENABLE
);
1615 ngene_command_mutex(dev
, &com
);
1616 up(&dev
->cmd_mutex
);
1619 void ngene_shutdown(struct pci_dev
*pdev
)
1621 struct ngene
*dev
= pci_get_drvdata(pdev
);
1623 if (!dev
|| !shutdown_workaround
)
1626 printk(KERN_INFO DEVICE_NAME
": shutdown workaround...\n");
1628 pci_disable_device(pdev
);
1631 /****************************************************************************/
1632 /* device probe/remove calls ************************************************/
1633 /****************************************************************************/
1635 void ngene_remove(struct pci_dev
*pdev
)
1637 struct ngene
*dev
= pci_get_drvdata(pdev
);
1640 tasklet_kill(&dev
->event_tasklet
);
1641 for (i
= MAX_STREAM
- 1; i
>= 0; i
--)
1642 release_channel(&dev
->channel
[i
]);
1646 ngene_release_buffers(dev
);
1647 pci_disable_device(pdev
);
1650 int ngene_probe(struct pci_dev
*pci_dev
, const struct pci_device_id
*id
)
1655 if (pci_enable_device(pci_dev
) < 0)
1658 dev
= vzalloc(sizeof(struct ngene
));
1664 dev
->pci_dev
= pci_dev
;
1665 dev
->card_info
= (struct ngene_info
*)id
->driver_data
;
1666 printk(KERN_INFO DEVICE_NAME
": Found %s\n", dev
->card_info
->name
);
1668 pci_set_drvdata(pci_dev
, dev
);
1670 /* Alloc buffers and start nGene */
1671 stat
= ngene_get_buffers(dev
);
1674 stat
= ngene_start(dev
);
1680 stat
= ngene_buffer_config(dev
);
1685 dev
->i2c_current_bus
= -1;
1687 /* Register DVB adapters and devices for both channels */
1688 stat
= init_channels(dev
);
1697 ngene_release_buffers(dev
);
1699 pci_disable_device(pci_dev
);