aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/pci
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/pci')
-rw-r--r--drivers/media/pci/Kconfig50
-rw-r--r--drivers/media/pci/Makefile11
-rw-r--r--drivers/media/pci/cx18/Kconfig35
-rw-r--r--drivers/media/pci/cx18/Makefile13
-rw-r--r--drivers/media/pci/cx18/cx18-alsa-main.c295
-rw-r--r--drivers/media/pci/cx18/cx18-alsa-mixer.c175
-rw-r--r--drivers/media/pci/cx18/cx18-alsa-mixer.h23
-rw-r--r--drivers/media/pci/cx18/cx18-alsa-pcm.c356
-rw-r--r--drivers/media/pci/cx18/cx18-alsa-pcm.h27
-rw-r--r--drivers/media/pci/cx18/cx18-alsa.h75
-rw-r--r--drivers/media/pci/cx18/cx18-audio.c92
-rw-r--r--drivers/media/pci/cx18/cx18-audio.h24
-rw-r--r--drivers/media/pci/cx18/cx18-av-audio.c471
-rw-r--r--drivers/media/pci/cx18/cx18-av-core.c1401
-rw-r--r--drivers/media/pci/cx18/cx18-av-core.h391
-rw-r--r--drivers/media/pci/cx18/cx18-av-firmware.c225
-rw-r--r--drivers/media/pci/cx18/cx18-av-vbi.c311
-rw-r--r--drivers/media/pci/cx18/cx18-cards.c638
-rw-r--r--drivers/media/pci/cx18/cx18-cards.h157
-rw-r--r--drivers/media/pci/cx18/cx18-controls.c131
-rw-r--r--drivers/media/pci/cx18/cx18-controls.h24
-rw-r--r--drivers/media/pci/cx18/cx18-driver.c1360
-rw-r--r--drivers/media/pci/cx18/cx18-driver.h730
-rw-r--r--drivers/media/pci/cx18/cx18-dvb.c609
-rw-r--r--drivers/media/pci/cx18/cx18-dvb.h25
-rw-r--r--drivers/media/pci/cx18/cx18-fileops.c881
-rw-r--r--drivers/media/pci/cx18/cx18-fileops.h41
-rw-r--r--drivers/media/pci/cx18/cx18-firmware.c459
-rw-r--r--drivers/media/pci/cx18/cx18-firmware.h25
-rw-r--r--drivers/media/pci/cx18/cx18-gpio.c347
-rw-r--r--drivers/media/pci/cx18/cx18-gpio.h34
-rw-r--r--drivers/media/pci/cx18/cx18-i2c.c330
-rw-r--r--drivers/media/pci/cx18/cx18-i2c.h29
-rw-r--r--drivers/media/pci/cx18/cx18-io.c97
-rw-r--r--drivers/media/pci/cx18/cx18-io.h191
-rw-r--r--drivers/media/pci/cx18/cx18-ioctl.c1194
-rw-r--r--drivers/media/pci/cx18/cx18-ioctl.h31
-rw-r--r--drivers/media/pci/cx18/cx18-irq.c81
-rw-r--r--drivers/media/pci/cx18/cx18-irq.h35
-rw-r--r--drivers/media/pci/cx18/cx18-mailbox.c870
-rw-r--r--drivers/media/pci/cx18/cx18-mailbox.h95
-rw-r--r--drivers/media/pci/cx18/cx18-queue.c443
-rw-r--r--drivers/media/pci/cx18/cx18-queue.h98
-rw-r--r--drivers/media/pci/cx18/cx18-scb.c122
-rw-r--r--drivers/media/pci/cx18/cx18-scb.h280
-rw-r--r--drivers/media/pci/cx18/cx18-streams.c1060
-rw-r--r--drivers/media/pci/cx18/cx18-streams.h62
-rw-r--r--drivers/media/pci/cx18/cx18-vbi.c277
-rw-r--r--drivers/media/pci/cx18/cx18-vbi.h26
-rw-r--r--drivers/media/pci/cx18/cx18-version.h28
-rw-r--r--drivers/media/pci/cx18/cx18-video.c32
-rw-r--r--drivers/media/pci/cx18/cx18-video.h22
-rw-r--r--drivers/media/pci/cx18/cx23418.h492
-rw-r--r--drivers/media/pci/cx23885/Kconfig46
-rw-r--r--drivers/media/pci/cx23885/Makefile15
-rw-r--r--drivers/media/pci/cx23885/altera-ci.c837
-rw-r--r--drivers/media/pci/cx23885/altera-ci.h100
-rw-r--r--drivers/media/pci/cx23885/cimax2.c536
-rw-r--r--drivers/media/pci/cx23885/cimax2.h47
-rw-r--r--drivers/media/pci/cx23885/cx23885-417.c1790
-rw-r--r--drivers/media/pci/cx23885/cx23885-alsa.c535
-rw-r--r--drivers/media/pci/cx23885/cx23885-av.c35
-rw-r--r--drivers/media/pci/cx23885/cx23885-av.h27
-rw-r--r--drivers/media/pci/cx23885/cx23885-cards.c1684
-rw-r--r--drivers/media/pci/cx23885/cx23885-core.c2234
-rw-r--r--drivers/media/pci/cx23885/cx23885-dvb.c1356
-rw-r--r--drivers/media/pci/cx23885/cx23885-f300.c177
-rw-r--r--drivers/media/pci/cx23885/cx23885-f300.h2
-rw-r--r--drivers/media/pci/cx23885/cx23885-i2c.c396
-rw-r--r--drivers/media/pci/cx23885/cx23885-input.c365
-rw-r--r--drivers/media/pci/cx23885/cx23885-input.h30
-rw-r--r--drivers/media/pci/cx23885/cx23885-ioctl.c208
-rw-r--r--drivers/media/pci/cx23885/cx23885-ioctl.h39
-rw-r--r--drivers/media/pci/cx23885/cx23885-ir.c117
-rw-r--r--drivers/media/pci/cx23885/cx23885-ir.h31
-rw-r--r--drivers/media/pci/cx23885/cx23885-reg.h452
-rw-r--r--drivers/media/pci/cx23885/cx23885-vbi.c295
-rw-r--r--drivers/media/pci/cx23885/cx23885-video.c1926
-rw-r--r--drivers/media/pci/cx23885/cx23885.h653
-rw-r--r--drivers/media/pci/cx23885/cx23888-ir.c1271
-rw-r--r--drivers/media/pci/cx23885/cx23888-ir.h28
-rw-r--r--drivers/media/pci/cx23885/netup-eeprom.c107
-rw-r--r--drivers/media/pci/cx23885/netup-eeprom.h42
-rw-r--r--drivers/media/pci/cx23885/netup-init.c125
-rw-r--r--drivers/media/pci/cx23885/netup-init.h25
-rw-r--r--drivers/media/pci/cx25821/Kconfig34
-rw-r--r--drivers/media/pci/cx25821/Makefile13
-rw-r--r--drivers/media/pci/cx25821/cx25821-alsa.c784
-rw-r--r--drivers/media/pci/cx25821/cx25821-audio-upstream.c778
-rw-r--r--drivers/media/pci/cx25821/cx25821-audio-upstream.h62
-rw-r--r--drivers/media/pci/cx25821/cx25821-audio.h62
-rw-r--r--drivers/media/pci/cx25821/cx25821-biffuncs.h45
-rw-r--r--drivers/media/pci/cx25821/cx25821-cards.c72
-rw-r--r--drivers/media/pci/cx25821/cx25821-core.c1502
-rw-r--r--drivers/media/pci/cx25821/cx25821-gpio.c98
-rw-r--r--drivers/media/pci/cx25821/cx25821-i2c.c416
-rw-r--r--drivers/media/pci/cx25821/cx25821-medusa-defines.h42
-rw-r--r--drivers/media/pci/cx25821/cx25821-medusa-reg.h455
-rw-r--r--drivers/media/pci/cx25821/cx25821-medusa-video.c787
-rw-r--r--drivers/media/pci/cx25821/cx25821-medusa-video.h49
-rw-r--r--drivers/media/pci/cx25821/cx25821-reg.h1592
-rw-r--r--drivers/media/pci/cx25821/cx25821-sram.h261
-rw-r--r--drivers/media/pci/cx25821/cx25821-video-upstream-ch2.c802
-rw-r--r--drivers/media/pci/cx25821/cx25821-video-upstream-ch2.h138
-rw-r--r--drivers/media/pci/cx25821/cx25821-video-upstream.c856
-rw-r--r--drivers/media/pci/cx25821/cx25821-video-upstream.h139
-rw-r--r--drivers/media/pci/cx25821/cx25821-video.c1990
-rw-r--r--drivers/media/pci/cx25821/cx25821-video.h186
-rw-r--r--drivers/media/pci/cx25821/cx25821.h615
-rw-r--r--drivers/media/pci/cx88/Kconfig86
-rw-r--r--drivers/media/pci/cx88/Makefile16
-rw-r--r--drivers/media/pci/cx88/cx88-alsa.c975
-rw-r--r--drivers/media/pci/cx88/cx88-blackbird.c1299
-rw-r--r--drivers/media/pci/cx88/cx88-cards.c3811
-rw-r--r--drivers/media/pci/cx88/cx88-core.c1131
-rw-r--r--drivers/media/pci/cx88/cx88-dsp.c322
-rw-r--r--drivers/media/pci/cx88/cx88-dvb.c1778
-rw-r--r--drivers/media/pci/cx88/cx88-i2c.c184
-rw-r--r--drivers/media/pci/cx88/cx88-input.c635
-rw-r--r--drivers/media/pci/cx88/cx88-mpeg.c929
-rw-r--r--drivers/media/pci/cx88/cx88-reg.h836
-rw-r--r--drivers/media/pci/cx88/cx88-tvaudio.c1059
-rw-r--r--drivers/media/pci/cx88/cx88-vbi.c245
-rw-r--r--drivers/media/pci/cx88/cx88-video.c2075
-rw-r--r--drivers/media/pci/cx88/cx88-vp3054-i2c.c159
-rw-r--r--drivers/media/pci/cx88/cx88-vp3054-i2c.h41
-rw-r--r--drivers/media/pci/cx88/cx88.h748
-rw-r--r--drivers/media/pci/ivtv/Kconfig45
-rw-r--r--drivers/media/pci/ivtv/Makefile14
-rw-r--r--drivers/media/pci/ivtv/ivtv-cards.c1370
-rw-r--r--drivers/media/pci/ivtv/ivtv-cards.h309
-rw-r--r--drivers/media/pci/ivtv/ivtv-controls.c163
-rw-r--r--drivers/media/pci/ivtv/ivtv-controls.h28
-rw-r--r--drivers/media/pci/ivtv/ivtv-driver.c1498
-rw-r--r--drivers/media/pci/ivtv/ivtv-driver.h839
-rw-r--r--drivers/media/pci/ivtv/ivtv-fileops.c1070
-rw-r--r--drivers/media/pci/ivtv/ivtv-fileops.h44
-rw-r--r--drivers/media/pci/ivtv/ivtv-firmware.c402
-rw-r--r--drivers/media/pci/ivtv/ivtv-firmware.h31
-rw-r--r--drivers/media/pci/ivtv/ivtv-gpio.c374
-rw-r--r--drivers/media/pci/ivtv/ivtv-gpio.h29
-rw-r--r--drivers/media/pci/ivtv/ivtv-i2c.c760
-rw-r--r--drivers/media/pci/ivtv/ivtv-i2c.h32
-rw-r--r--drivers/media/pci/ivtv/ivtv-ioctl.c1899
-rw-r--r--drivers/media/pci/ivtv/ivtv-ioctl.h35
-rw-r--r--drivers/media/pci/ivtv/ivtv-irq.c1038
-rw-r--r--drivers/media/pci/ivtv/ivtv-irq.h53
-rw-r--r--drivers/media/pci/ivtv/ivtv-mailbox.c387
-rw-r--r--drivers/media/pci/ivtv/ivtv-mailbox.h35
-rw-r--r--drivers/media/pci/ivtv/ivtv-queue.c297
-rw-r--r--drivers/media/pci/ivtv/ivtv-queue.h96
-rw-r--r--drivers/media/pci/ivtv/ivtv-routing.c119
-rw-r--r--drivers/media/pci/ivtv/ivtv-routing.h27
-rw-r--r--drivers/media/pci/ivtv/ivtv-streams.c1014
-rw-r--r--drivers/media/pci/ivtv/ivtv-streams.h37
-rw-r--r--drivers/media/pci/ivtv/ivtv-udma.c234
-rw-r--r--drivers/media/pci/ivtv/ivtv-udma.h48
-rw-r--r--drivers/media/pci/ivtv/ivtv-vbi.c549
-rw-r--r--drivers/media/pci/ivtv/ivtv-vbi.h34
-rw-r--r--drivers/media/pci/ivtv/ivtv-version.h26
-rw-r--r--drivers/media/pci/ivtv/ivtv-yuv.c1296
-rw-r--r--drivers/media/pci/ivtv/ivtv-yuv.h44
-rw-r--r--drivers/media/pci/ivtv/ivtvfb.c1317
-rw-r--r--drivers/media/pci/saa7134/Kconfig64
-rw-r--r--drivers/media/pci/saa7134/Makefile16
-rw-r--r--drivers/media/pci/saa7134/saa6752hs.c1012
-rw-r--r--drivers/media/pci/saa7134/saa7134-alsa.c1209
-rw-r--r--drivers/media/pci/saa7134/saa7134-cards.c8026
-rw-r--r--drivers/media/pci/saa7134/saa7134-core.c1368
-rw-r--r--drivers/media/pci/saa7134/saa7134-dvb.c1936
-rw-r--r--drivers/media/pci/saa7134/saa7134-empress.c590
-rw-r--r--drivers/media/pci/saa7134/saa7134-i2c.c435
-rw-r--r--drivers/media/pci/saa7134/saa7134-input.c1041
-rw-r--r--drivers/media/pci/saa7134/saa7134-reg.h378
-rw-r--r--drivers/media/pci/saa7134/saa7134-ts.c327
-rw-r--r--drivers/media/pci/saa7134/saa7134-tvaudio.c1087
-rw-r--r--drivers/media/pci/saa7134/saa7134-vbi.c255
-rw-r--r--drivers/media/pci/saa7134/saa7134-video.c2661
-rw-r--r--drivers/media/pci/saa7134/saa7134.h855
-rw-r--r--drivers/media/pci/saa7164/Kconfig18
-rw-r--r--drivers/media/pci/saa7164/Makefile12
-rw-r--r--drivers/media/pci/saa7164/saa7164-api.c1524
-rw-r--r--drivers/media/pci/saa7164/saa7164-buffer.c322
-rw-r--r--drivers/media/pci/saa7164/saa7164-bus.c475
-rw-r--r--drivers/media/pci/saa7164/saa7164-cards.c773
-rw-r--r--drivers/media/pci/saa7164/saa7164-cmd.c589
-rw-r--r--drivers/media/pci/saa7164/saa7164-core.c1488
-rw-r--r--drivers/media/pci/saa7164/saa7164-dvb.c556
-rw-r--r--drivers/media/pci/saa7164/saa7164-encoder.c1500
-rw-r--r--drivers/media/pci/saa7164/saa7164-fw.c613
-rw-r--r--drivers/media/pci/saa7164/saa7164-i2c.c125
-rw-r--r--drivers/media/pci/saa7164/saa7164-reg.h219
-rw-r--r--drivers/media/pci/saa7164/saa7164-types.h442
-rw-r--r--drivers/media/pci/saa7164/saa7164-vbi.c1374
-rw-r--r--drivers/media/pci/saa7164/saa7164.h616
-rw-r--r--drivers/media/pci/zoran/Kconfig74
-rw-r--r--drivers/media/pci/zoran/Makefile6
-rw-r--r--drivers/media/pci/zoran/videocodec.c407
-rw-r--r--drivers/media/pci/zoran/videocodec.h353
-rw-r--r--drivers/media/pci/zoran/zoran.h403
-rw-r--r--drivers/media/pci/zoran/zoran_card.c1524
-rw-r--r--drivers/media/pci/zoran/zoran_card.h54
-rw-r--r--drivers/media/pci/zoran/zoran_device.c1640
-rw-r--r--drivers/media/pci/zoran/zoran_device.h95
-rw-r--r--drivers/media/pci/zoran/zoran_driver.c3090
-rw-r--r--drivers/media/pci/zoran/zoran_procfs.c225
-rw-r--r--drivers/media/pci/zoran/zoran_procfs.h36
-rw-r--r--drivers/media/pci/zoran/zr36016.c524
-rw-r--r--drivers/media/pci/zoran/zr36016.h111
-rw-r--r--drivers/media/pci/zoran/zr36050.c900
-rw-r--r--drivers/media/pci/zoran/zr36050.h184
-rw-r--r--drivers/media/pci/zoran/zr36057.h168
-rw-r--r--drivers/media/pci/zoran/zr36060.c1010
-rw-r--r--drivers/media/pci/zoran/zr36060.h220
214 files changed, 117464 insertions, 28 deletions
diff --git a/drivers/media/pci/Kconfig b/drivers/media/pci/Kconfig
index b16529bf71b8..b69cb1280f35 100644
--- a/drivers/media/pci/Kconfig
+++ b/drivers/media/pci/Kconfig
@@ -2,40 +2,36 @@
2# DVB device configuration 2# DVB device configuration
3# 3#
4 4
5menuconfig DVB_CAPTURE_DRIVERS 5menu "Media PCI Adapters"
6 bool "DVB/ATSC PCI adapters" 6 visible if PCI && MEDIA_SUPPORT
7 depends on DVB_CORE 7
8 default y 8if MEDIA_ANALOG_TV_SUPPORT
9 ---help--- 9 comment "Media capture/analog TV support"
10 Say Y to select Digital TV adapters 10source "drivers/media/pci/ivtv/Kconfig"
11 11source "drivers/media/pci/zoran/Kconfig"
12if DVB_CAPTURE_DRIVERS && DVB_CORE && PCI && I2C 12endif
13
14if MEDIA_ANALOG_TV_SUPPORT || MEDIA_DIGITAL_TV_SUPPORT
15 comment "Media capture/analog/hybrid TV support"
16source "drivers/media/pci/cx18/Kconfig"
17source "drivers/media/pci/cx23885/Kconfig"
18source "drivers/media/pci/cx25821/Kconfig"
19source "drivers/media/pci/cx88/Kconfig"
20source "drivers/media/pci/bt8xx/Kconfig"
21source "drivers/media/pci/saa7134/Kconfig"
22source "drivers/media/pci/saa7164/Kconfig"
23endif
13 24
14comment "Supported SAA7146 based PCI Adapters" 25if MEDIA_DIGITAL_TV_SUPPORT
26 comment "Media digital TV PCI Adapters"
15source "drivers/media/pci/ttpci/Kconfig" 27source "drivers/media/pci/ttpci/Kconfig"
16
17comment "Supported FlexCopII (B2C2) PCI Adapters"
18source "drivers/media/pci/b2c2/Kconfig" 28source "drivers/media/pci/b2c2/Kconfig"
19
20comment "Supported BT878 Adapters"
21source "drivers/media/pci/bt8xx/Kconfig"
22
23comment "Supported Pluto2 Adapters"
24source "drivers/media/pci/pluto2/Kconfig" 29source "drivers/media/pci/pluto2/Kconfig"
25
26comment "Supported SDMC DM1105 Adapters"
27source "drivers/media/pci/dm1105/Kconfig" 30source "drivers/media/pci/dm1105/Kconfig"
28
29comment "Supported Earthsoft PT1 Adapters"
30source "drivers/media/pci/pt1/Kconfig" 31source "drivers/media/pci/pt1/Kconfig"
31
32comment "Supported Mantis Adapters"
33source "drivers/media/pci/mantis/Kconfig" 32source "drivers/media/pci/mantis/Kconfig"
34
35comment "Supported nGene Adapters"
36source "drivers/media/pci/ngene/Kconfig" 33source "drivers/media/pci/ngene/Kconfig"
37
38comment "Supported ddbridge ('Octopus') Adapters"
39source "drivers/media/pci/ddbridge/Kconfig" 34source "drivers/media/pci/ddbridge/Kconfig"
35endif
40 36
41endif # DVB_CAPTURE_DRIVERS 37endmenu
diff --git a/drivers/media/pci/Makefile b/drivers/media/pci/Makefile
index 1d44fbd772b2..d47c222e6949 100644
--- a/drivers/media/pci/Makefile
+++ b/drivers/media/pci/Makefile
@@ -4,7 +4,6 @@
4 4
5obj-y := ttpci/ \ 5obj-y := ttpci/ \
6 b2c2/ \ 6 b2c2/ \
7 bt8xx/ \
8 pluto2/ \ 7 pluto2/ \
9 dm1105/ \ 8 dm1105/ \
10 pt1/ \ 9 pt1/ \
@@ -12,3 +11,13 @@ obj-y := ttpci/ \
12 ngene/ \ 11 ngene/ \
13 ddbridge/ \ 12 ddbridge/ \
14 b2c2/ 13 b2c2/
14
15obj-$(CONFIG_VIDEO_IVTV) += ivtv/
16obj-$(CONFIG_VIDEO_ZORAN) += zoran/
17obj-$(CONFIG_VIDEO_CX18) += cx18/
18obj-$(CONFIG_VIDEO_CX23885) += cx23885/
19obj-$(CONFIG_VIDEO_CX25821) += cx25821/
20obj-$(CONFIG_VIDEO_CX88) += cx88/
21obj-$(CONFIG_VIDEO_BT848) += bt8xx/
22obj-$(CONFIG_VIDEO_SAA7134) += saa7134/
23obj-$(CONFIG_VIDEO_SAA7164) += saa7164/
diff --git a/drivers/media/pci/cx18/Kconfig b/drivers/media/pci/cx18/Kconfig
new file mode 100644
index 000000000000..53b3c7702573
--- /dev/null
+++ b/drivers/media/pci/cx18/Kconfig
@@ -0,0 +1,35 @@
1config VIDEO_CX18
2 tristate "Conexant cx23418 MPEG encoder support"
3 depends on VIDEO_V4L2 && DVB_CORE && PCI && I2C && EXPERIMENTAL
4 select I2C_ALGOBIT
5 select VIDEOBUF_VMALLOC
6 depends on RC_CORE
7 select VIDEO_TUNER
8 select VIDEO_TVEEPROM
9 select VIDEO_CX2341X
10 select VIDEO_CS5345
11 select DVB_S5H1409 if !DVB_FE_CUSTOMISE
12 select MEDIA_TUNER_MXL5005S if !MEDIA_TUNER_CUSTOMISE
13 select DVB_S5H1411 if !DVB_FE_CUSTOMISE
14 select MEDIA_TUNER_TDA18271 if !MEDIA_TUNER_CUSTOMISE
15 select MEDIA_TUNER_TDA8290 if !MEDIA_TUNER_CUSTOMISE
16 ---help---
17 This is a video4linux driver for Conexant cx23418 based
18 PCI combo video recorder devices.
19
20 This is used in devices such as the Hauppauge HVR-1600
21 cards.
22
23 To compile this driver as a module, choose M here: the
24 module will be called cx18.
25
26config VIDEO_CX18_ALSA
27 tristate "Conexant 23418 DMA audio support"
28 depends on VIDEO_CX18 && SND && EXPERIMENTAL
29 select SND_PCM
30 ---help---
31 This is a video4linux driver for direct (DMA) audio on
32 Conexant 23418 based TV cards using ALSA.
33
34 To compile this driver as a module, choose M here: the
35 module will be called cx18-alsa.
diff --git a/drivers/media/pci/cx18/Makefile b/drivers/media/pci/cx18/Makefile
new file mode 100644
index 000000000000..d3ff1545c2c5
--- /dev/null
+++ b/drivers/media/pci/cx18/Makefile
@@ -0,0 +1,13 @@
1cx18-objs := cx18-driver.o cx18-cards.o cx18-i2c.o cx18-firmware.o cx18-gpio.o \
2 cx18-queue.o cx18-streams.o cx18-fileops.o cx18-ioctl.o cx18-controls.o \
3 cx18-mailbox.o cx18-vbi.o cx18-audio.o cx18-video.o cx18-irq.o \
4 cx18-av-core.o cx18-av-audio.o cx18-av-firmware.o cx18-av-vbi.o cx18-scb.o \
5 cx18-dvb.o cx18-io.o
6cx18-alsa-objs := cx18-alsa-main.o cx18-alsa-pcm.o
7
8obj-$(CONFIG_VIDEO_CX18) += cx18.o
9obj-$(CONFIG_VIDEO_CX18_ALSA) += cx18-alsa.o
10
11ccflags-y += -Idrivers/media/dvb-core
12ccflags-y += -Idrivers/media/dvb-frontends
13ccflags-y += -Idrivers/media/tuners
diff --git a/drivers/media/pci/cx18/cx18-alsa-main.c b/drivers/media/pci/cx18/cx18-alsa-main.c
new file mode 100644
index 000000000000..6d2a98246b6d
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-alsa-main.c
@@ -0,0 +1,295 @@
1/*
2 * ALSA interface to cx18 PCM capture streams
3 *
4 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
5 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
6 *
7 * Portions of this work were sponsored by ONELAN Limited.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/device.h>
30#include <linux/spinlock.h>
31
32#include <media/v4l2-device.h>
33
34#include <sound/core.h>
35#include <sound/initval.h>
36
37#include "cx18-driver.h"
38#include "cx18-version.h"
39#include "cx18-alsa.h"
40#include "cx18-alsa-mixer.h"
41#include "cx18-alsa-pcm.h"
42
43int cx18_alsa_debug;
44
45#define CX18_DEBUG_ALSA_INFO(fmt, arg...) \
46 do { \
47 if (cx18_alsa_debug & 2) \
48 printk(KERN_INFO "%s: " fmt, "cx18-alsa", ## arg); \
49 } while (0);
50
51module_param_named(debug, cx18_alsa_debug, int, 0644);
52MODULE_PARM_DESC(debug,
53 "Debug level (bitmask). Default: 0\n"
54 "\t\t\t 1/0x0001: warning\n"
55 "\t\t\t 2/0x0002: info\n");
56
57MODULE_AUTHOR("Andy Walls");
58MODULE_DESCRIPTION("CX23418 ALSA Interface");
59MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
60MODULE_LICENSE("GPL");
61
62MODULE_VERSION(CX18_VERSION);
63
64static inline
65struct snd_cx18_card *to_snd_cx18_card(struct v4l2_device *v4l2_dev)
66{
67 return to_cx18(v4l2_dev)->alsa;
68}
69
70static inline
71struct snd_cx18_card *p_to_snd_cx18_card(struct v4l2_device **v4l2_dev)
72{
73 return container_of(v4l2_dev, struct snd_cx18_card, v4l2_dev);
74}
75
76static void snd_cx18_card_free(struct snd_cx18_card *cxsc)
77{
78 if (cxsc == NULL)
79 return;
80
81 if (cxsc->v4l2_dev != NULL)
82 to_cx18(cxsc->v4l2_dev)->alsa = NULL;
83
84 /* FIXME - take any other stopping actions needed */
85
86 kfree(cxsc);
87}
88
89static void snd_cx18_card_private_free(struct snd_card *sc)
90{
91 if (sc == NULL)
92 return;
93 snd_cx18_card_free(sc->private_data);
94 sc->private_data = NULL;
95 sc->private_free = NULL;
96}
97
98static int snd_cx18_card_create(struct v4l2_device *v4l2_dev,
99 struct snd_card *sc,
100 struct snd_cx18_card **cxsc)
101{
102 *cxsc = kzalloc(sizeof(struct snd_cx18_card), GFP_KERNEL);
103 if (*cxsc == NULL)
104 return -ENOMEM;
105
106 (*cxsc)->v4l2_dev = v4l2_dev;
107 (*cxsc)->sc = sc;
108
109 sc->private_data = *cxsc;
110 sc->private_free = snd_cx18_card_private_free;
111
112 return 0;
113}
114
115static int snd_cx18_card_set_names(struct snd_cx18_card *cxsc)
116{
117 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
118 struct snd_card *sc = cxsc->sc;
119
120 /* sc->driver is used by alsa-lib's configurator: simple, unique */
121 strlcpy(sc->driver, "CX23418", sizeof(sc->driver));
122
123 /* sc->shortname is a symlink in /proc/asound: CX18-M -> cardN */
124 snprintf(sc->shortname, sizeof(sc->shortname), "CX18-%d",
125 cx->instance);
126
127 /* sc->longname is read from /proc/asound/cards */
128 snprintf(sc->longname, sizeof(sc->longname),
129 "CX23418 #%d %s TV/FM Radio/Line-In Capture",
130 cx->instance, cx->card_name);
131
132 return 0;
133}
134
135static int snd_cx18_init(struct v4l2_device *v4l2_dev)
136{
137 struct cx18 *cx = to_cx18(v4l2_dev);
138 struct snd_card *sc = NULL;
139 struct snd_cx18_card *cxsc;
140 int ret;
141
142 /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
143
144 /* (1) Check and increment the device index */
145 /* This is a no-op for us. We'll use the cx->instance */
146
147 /* (2) Create a card instance */
148 ret = snd_card_create(SNDRV_DEFAULT_IDX1, /* use first available id */
149 SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
150 THIS_MODULE, 0, &sc);
151 if (ret) {
152 CX18_ALSA_ERR("%s: snd_card_create() failed with err %d\n",
153 __func__, ret);
154 goto err_exit;
155 }
156
157 /* (3) Create a main component */
158 ret = snd_cx18_card_create(v4l2_dev, sc, &cxsc);
159 if (ret) {
160 CX18_ALSA_ERR("%s: snd_cx18_card_create() failed with err %d\n",
161 __func__, ret);
162 goto err_exit_free;
163 }
164
165 /* (4) Set the driver ID and name strings */
166 snd_cx18_card_set_names(cxsc);
167
168
169 ret = snd_cx18_pcm_create(cxsc);
170 if (ret) {
171 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
172 __func__, ret);
173 goto err_exit_free;
174 }
175 /* FIXME - proc files */
176
177 /* (7) Set the driver data and return 0 */
178 /* We do this out of normal order for PCI drivers to avoid races */
179 cx->alsa = cxsc;
180
181 /* (6) Register the card instance */
182 ret = snd_card_register(sc);
183 if (ret) {
184 cx->alsa = NULL;
185 CX18_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
186 __func__, ret);
187 goto err_exit_free;
188 }
189
190 return 0;
191
192err_exit_free:
193 if (sc != NULL)
194 snd_card_free(sc);
195 kfree(cxsc);
196err_exit:
197 return ret;
198}
199
200int cx18_alsa_load(struct cx18 *cx)
201{
202 struct v4l2_device *v4l2_dev = &cx->v4l2_dev;
203 struct cx18_stream *s;
204
205 if (v4l2_dev == NULL) {
206 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
207 __func__);
208 return 0;
209 }
210
211 cx = to_cx18(v4l2_dev);
212 if (cx == NULL) {
213 printk(KERN_ERR "cx18-alsa cx is NULL\n");
214 return 0;
215 }
216
217 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
218 if (s->video_dev == NULL) {
219 CX18_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - "
220 "skipping\n", __func__);
221 return 0;
222 }
223
224 if (cx->alsa != NULL) {
225 CX18_ALSA_ERR("%s: struct snd_cx18_card * already exists\n",
226 __func__);
227 return 0;
228 }
229
230 if (snd_cx18_init(v4l2_dev)) {
231 CX18_ALSA_ERR("%s: failed to create struct snd_cx18_card\n",
232 __func__);
233 } else {
234 CX18_DEBUG_ALSA_INFO("%s: created cx18 ALSA interface instance "
235 "\n", __func__);
236 }
237 return 0;
238}
239
240static int __init cx18_alsa_init(void)
241{
242 printk(KERN_INFO "cx18-alsa: module loading...\n");
243 cx18_ext_init = &cx18_alsa_load;
244 return 0;
245}
246
247static void __exit snd_cx18_exit(struct snd_cx18_card *cxsc)
248{
249 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
250
251 /* FIXME - pointer checks & shutdown cxsc */
252
253 snd_card_free(cxsc->sc);
254 cx->alsa = NULL;
255}
256
257static int __exit cx18_alsa_exit_callback(struct device *dev, void *data)
258{
259 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
260 struct snd_cx18_card *cxsc;
261
262 if (v4l2_dev == NULL) {
263 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
264 __func__);
265 return 0;
266 }
267
268 cxsc = to_snd_cx18_card(v4l2_dev);
269 if (cxsc == NULL) {
270 CX18_ALSA_WARN("%s: struct snd_cx18_card * is NULL\n",
271 __func__);
272 return 0;
273 }
274
275 snd_cx18_exit(cxsc);
276 return 0;
277}
278
279static void __exit cx18_alsa_exit(void)
280{
281 struct device_driver *drv;
282 int ret;
283
284 printk(KERN_INFO "cx18-alsa: module unloading...\n");
285
286 drv = driver_find("cx18", &pci_bus_type);
287 ret = driver_for_each_device(drv, NULL, NULL, cx18_alsa_exit_callback);
288 (void)ret; /* suppress compiler warning */
289
290 cx18_ext_init = NULL;
291 printk(KERN_INFO "cx18-alsa: module unload complete\n");
292}
293
294module_init(cx18_alsa_init);
295module_exit(cx18_alsa_exit);
diff --git a/drivers/media/pci/cx18/cx18-alsa-mixer.c b/drivers/media/pci/cx18/cx18-alsa-mixer.c
new file mode 100644
index 000000000000..341bddc00b77
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-alsa-mixer.c
@@ -0,0 +1,175 @@
1/*
2 * ALSA mixer controls for the
3 * ALSA interface to cx18 PCM capture streams
4 *
5 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#include <linux/init.h>
24#include <linux/kernel.h>
25#include <linux/device.h>
26#include <linux/spinlock.h>
27#include <linux/videodev2.h>
28
29#include <media/v4l2-device.h>
30
31#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/tlv.h>
34
35#include "cx18-alsa.h"
36#include "cx18-driver.h"
37
38/*
39 * Note the cx18-av-core volume scale is funny, due to the alignment of the
40 * scale with another chip's range:
41 *
42 * v4l2_control value /512 indicated dB actual dB reg 0x8d4
43 * 0x0000 - 0x01ff 0 -119 -96 228
44 * 0x0200 - 0x02ff 1 -118 -96 228
45 * ...
46 * 0x2c00 - 0x2dff 22 -97 -96 228
47 * 0x2e00 - 0x2fff 23 -96 -96 228
48 * 0x3000 - 0x31ff 24 -95 -95 226
49 * ...
50 * 0xee00 - 0xefff 119 0 0 36
51 * ...
52 * 0xfe00 - 0xffff 127 +8 +8 20
53 */
54static inline int dB_to_cx18_av_vol(int dB)
55{
56 if (dB < -96)
57 dB = -96;
58 else if (dB > 8)
59 dB = 8;
60 return (dB + 119) << 9;
61}
62
63static inline int cx18_av_vol_to_dB(int v)
64{
65 if (v < (23 << 9))
66 v = (23 << 9);
67 else if (v > (127 << 9))
68 v = (127 << 9);
69 return (v >> 9) - 119;
70}
71
72static int snd_cx18_mixer_tv_vol_info(struct snd_kcontrol *kcontrol,
73 struct snd_ctl_elem_info *uinfo)
74{
75 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
76 uinfo->count = 1;
77 /* We're already translating values, just keep this control in dB */
78 uinfo->value.integer.min = -96;
79 uinfo->value.integer.max = 8;
80 uinfo->value.integer.step = 1;
81 return 0;
82}
83
84static int snd_cx18_mixer_tv_vol_get(struct snd_kcontrol *kctl,
85 struct snd_ctl_elem_value *uctl)
86{
87 struct snd_cx18_card *cxsc = snd_kcontrol_chip(kctl);
88 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
89 struct v4l2_control vctrl;
90 int ret;
91
92 vctrl.id = V4L2_CID_AUDIO_VOLUME;
93 vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]);
94
95 snd_cx18_lock(cxsc);
96 ret = v4l2_subdev_call(cx->sd_av, core, g_ctrl, &vctrl);
97 snd_cx18_unlock(cxsc);
98
99 if (!ret)
100 uctl->value.integer.value[0] = cx18_av_vol_to_dB(vctrl.value);
101 return ret;
102}
103
104static int snd_cx18_mixer_tv_vol_put(struct snd_kcontrol *kctl,
105 struct snd_ctl_elem_value *uctl)
106{
107 struct snd_cx18_card *cxsc = snd_kcontrol_chip(kctl);
108 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
109 struct v4l2_control vctrl;
110 int ret;
111
112 vctrl.id = V4L2_CID_AUDIO_VOLUME;
113 vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]);
114
115 snd_cx18_lock(cxsc);
116
117 /* Fetch current state */
118 ret = v4l2_subdev_call(cx->sd_av, core, g_ctrl, &vctrl);
119
120 if (ret ||
121 (cx18_av_vol_to_dB(vctrl.value) != uctl->value.integer.value[0])) {
122
123 /* Set, if needed */
124 vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]);
125 ret = v4l2_subdev_call(cx->sd_av, core, s_ctrl, &vctrl);
126 if (!ret)
127 ret = 1; /* Indicate control was changed w/o error */
128 }
129 snd_cx18_unlock(cxsc);
130
131 return ret;
132}
133
134
135/* This is a bit of overkill, the slider is already in dB internally */
136static DECLARE_TLV_DB_SCALE(snd_cx18_mixer_tv_vol_db_scale, -9600, 100, 0);
137
138static struct snd_kcontrol_new snd_cx18_mixer_tv_vol __initdata = {
139 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
140 .name = "Analog TV Capture Volume",
141 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
142 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
143 .info = snd_cx18_mixer_tv_volume_info,
144 .get = snd_cx18_mixer_tv_volume_get,
145 .put = snd_cx18_mixer_tv_volume_put,
146 .tlv.p = snd_cx18_mixer_tv_vol_db_scale
147};
148
149/* FIXME - add mute switch and balance, bass, treble sliders:
150 V4L2_CID_AUDIO_MUTE
151
152 V4L2_CID_AUDIO_BALANCE
153
154 V4L2_CID_AUDIO_BASS
155 V4L2_CID_AUDIO_TREBLE
156*/
157
158/* FIXME - add stereo, lang1, lang2, mono menu */
159/* FIXME - add CS5345 I2S volume for HVR-1600 */
160
161int __init snd_cx18_mixer_create(struct snd_cx18_card *cxsc)
162{
163 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
164 struct snd_card *sc = cxsc->sc;
165 int ret;
166
167 strlcpy(sc->mixername, "CX23418 Mixer", sizeof(sc->mixername));
168
169 ret = snd_ctl_add(sc, snd_ctl_new1(snd_cx18_mixer_tv_vol, cxsc));
170 if (ret) {
171 CX18_ALSA_WARN("%s: failed to add %s control, err %d\n",
172 __func__, snd_cx18_mixer_tv_vol.name, ret);
173 }
174 return ret;
175}
diff --git a/drivers/media/pci/cx18/cx18-alsa-mixer.h b/drivers/media/pci/cx18/cx18-alsa-mixer.h
new file mode 100644
index 000000000000..ec9238793f6f
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-alsa-mixer.h
@@ -0,0 +1,23 @@
1/*
2 * ALSA mixer controls for the
3 * ALSA interface to cx18 PCM capture streams
4 *
5 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23int __init snd_cx18_mixer_create(struct snd_cx18_card *cxsc);
diff --git a/drivers/media/pci/cx18/cx18-alsa-pcm.c b/drivers/media/pci/cx18/cx18-alsa-pcm.c
new file mode 100644
index 000000000000..7a5b84a86bb3
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-alsa-pcm.c
@@ -0,0 +1,356 @@
1/*
2 * ALSA PCM device for the
3 * ALSA interface to cx18 PCM capture streams
4 *
5 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
6 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 *
8 * Portions of this work were sponsored by ONELAN Limited.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307 USA
24 */
25
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/vmalloc.h>
29
30#include <media/v4l2-device.h>
31
32#include <sound/core.h>
33#include <sound/pcm.h>
34
35#include "cx18-driver.h"
36#include "cx18-queue.h"
37#include "cx18-streams.h"
38#include "cx18-fileops.h"
39#include "cx18-alsa.h"
40
41static unsigned int pcm_debug;
42module_param(pcm_debug, int, 0644);
43MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
44
45#define dprintk(fmt, arg...) do { \
46 if (pcm_debug) \
47 printk(KERN_INFO "cx18-alsa-pcm %s: " fmt, \
48 __func__, ##arg); \
49 } while (0)
50
51static struct snd_pcm_hardware snd_cx18_hw_capture = {
52 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
53 SNDRV_PCM_INFO_MMAP |
54 SNDRV_PCM_INFO_INTERLEAVED |
55 SNDRV_PCM_INFO_MMAP_VALID,
56
57 .formats = SNDRV_PCM_FMTBIT_S16_LE,
58
59 .rates = SNDRV_PCM_RATE_48000,
60
61 .rate_min = 48000,
62 .rate_max = 48000,
63 .channels_min = 2,
64 .channels_max = 2,
65 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
66 .period_bytes_min = 64, /* 12544/2, */
67 .period_bytes_max = 12544,
68 .periods_min = 2,
69 .periods_max = 98, /* 12544, */
70};
71
72void cx18_alsa_announce_pcm_data(struct snd_cx18_card *cxsc, u8 *pcm_data,
73 size_t num_bytes)
74{
75 struct snd_pcm_substream *substream;
76 struct snd_pcm_runtime *runtime;
77 unsigned int oldptr;
78 unsigned int stride;
79 int period_elapsed = 0;
80 int length;
81
82 dprintk("cx18 alsa announce ptr=%p data=%p num_bytes=%zd\n", cxsc,
83 pcm_data, num_bytes);
84
85 substream = cxsc->capture_pcm_substream;
86 if (substream == NULL) {
87 dprintk("substream was NULL\n");
88 return;
89 }
90
91 runtime = substream->runtime;
92 if (runtime == NULL) {
93 dprintk("runtime was NULL\n");
94 return;
95 }
96
97 stride = runtime->frame_bits >> 3;
98 if (stride == 0) {
99 dprintk("stride is zero\n");
100 return;
101 }
102
103 length = num_bytes / stride;
104 if (length == 0) {
105 dprintk("%s: length was zero\n", __func__);
106 return;
107 }
108
109 if (runtime->dma_area == NULL) {
110 dprintk("dma area was NULL - ignoring\n");
111 return;
112 }
113
114 oldptr = cxsc->hwptr_done_capture;
115 if (oldptr + length >= runtime->buffer_size) {
116 unsigned int cnt =
117 runtime->buffer_size - oldptr;
118 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
119 cnt * stride);
120 memcpy(runtime->dma_area, pcm_data + cnt * stride,
121 length * stride - cnt * stride);
122 } else {
123 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
124 length * stride);
125 }
126 snd_pcm_stream_lock(substream);
127
128 cxsc->hwptr_done_capture += length;
129 if (cxsc->hwptr_done_capture >=
130 runtime->buffer_size)
131 cxsc->hwptr_done_capture -=
132 runtime->buffer_size;
133
134 cxsc->capture_transfer_done += length;
135 if (cxsc->capture_transfer_done >=
136 runtime->period_size) {
137 cxsc->capture_transfer_done -=
138 runtime->period_size;
139 period_elapsed = 1;
140 }
141
142 snd_pcm_stream_unlock(substream);
143
144 if (period_elapsed)
145 snd_pcm_period_elapsed(substream);
146}
147
148static int snd_cx18_pcm_capture_open(struct snd_pcm_substream *substream)
149{
150 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
151 struct snd_pcm_runtime *runtime = substream->runtime;
152 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
153 struct cx18 *cx = to_cx18(v4l2_dev);
154 struct cx18_stream *s;
155 struct cx18_open_id item;
156 int ret;
157
158 /* Instruct the cx18 to start sending packets */
159 snd_cx18_lock(cxsc);
160 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
161
162 item.cx = cx;
163 item.type = s->type;
164 item.open_id = cx->open_id++;
165
166 /* See if the stream is available */
167 if (cx18_claim_stream(&item, item.type)) {
168 /* No, it's already in use */
169 snd_cx18_unlock(cxsc);
170 return -EBUSY;
171 }
172
173 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
174 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
175 /* We're already streaming. No additional action required */
176 snd_cx18_unlock(cxsc);
177 return 0;
178 }
179
180
181 runtime->hw = snd_cx18_hw_capture;
182 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
183 cxsc->capture_pcm_substream = substream;
184 runtime->private_data = cx;
185
186 cx->pcm_announce_callback = cx18_alsa_announce_pcm_data;
187
188 /* Not currently streaming, so start it up */
189 set_bit(CX18_F_S_STREAMING, &s->s_flags);
190 ret = cx18_start_v4l2_encode_stream(s);
191 snd_cx18_unlock(cxsc);
192
193 return ret;
194}
195
196static int snd_cx18_pcm_capture_close(struct snd_pcm_substream *substream)
197{
198 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
199 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
200 struct cx18 *cx = to_cx18(v4l2_dev);
201 struct cx18_stream *s;
202
203 /* Instruct the cx18 to stop sending packets */
204 snd_cx18_lock(cxsc);
205 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
206 cx18_stop_v4l2_encode_stream(s, 0);
207 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
208
209 cx18_release_stream(s);
210
211 cx->pcm_announce_callback = NULL;
212 snd_cx18_unlock(cxsc);
213
214 return 0;
215}
216
217static int snd_cx18_pcm_ioctl(struct snd_pcm_substream *substream,
218 unsigned int cmd, void *arg)
219{
220 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
221 int ret;
222
223 snd_cx18_lock(cxsc);
224 ret = snd_pcm_lib_ioctl(substream, cmd, arg);
225 snd_cx18_unlock(cxsc);
226 return ret;
227}
228
229
230static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
231 size_t size)
232{
233 struct snd_pcm_runtime *runtime = subs->runtime;
234
235 dprintk("Allocating vbuffer\n");
236 if (runtime->dma_area) {
237 if (runtime->dma_bytes > size)
238 return 0;
239
240 vfree(runtime->dma_area);
241 }
242 runtime->dma_area = vmalloc(size);
243 if (!runtime->dma_area)
244 return -ENOMEM;
245
246 runtime->dma_bytes = size;
247
248 return 0;
249}
250
251static int snd_cx18_pcm_hw_params(struct snd_pcm_substream *substream,
252 struct snd_pcm_hw_params *params)
253{
254 dprintk("%s called\n", __func__);
255
256 return snd_pcm_alloc_vmalloc_buffer(substream,
257 params_buffer_bytes(params));
258}
259
260static int snd_cx18_pcm_hw_free(struct snd_pcm_substream *substream)
261{
262 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
263 unsigned long flags;
264
265 spin_lock_irqsave(&cxsc->slock, flags);
266 if (substream->runtime->dma_area) {
267 dprintk("freeing pcm capture region\n");
268 vfree(substream->runtime->dma_area);
269 substream->runtime->dma_area = NULL;
270 }
271 spin_unlock_irqrestore(&cxsc->slock, flags);
272
273 return 0;
274}
275
276static int snd_cx18_pcm_prepare(struct snd_pcm_substream *substream)
277{
278 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
279
280 cxsc->hwptr_done_capture = 0;
281 cxsc->capture_transfer_done = 0;
282
283 return 0;
284}
285
286static int snd_cx18_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
287{
288 return 0;
289}
290
291static
292snd_pcm_uframes_t snd_cx18_pcm_pointer(struct snd_pcm_substream *substream)
293{
294 unsigned long flags;
295 snd_pcm_uframes_t hwptr_done;
296 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
297
298 spin_lock_irqsave(&cxsc->slock, flags);
299 hwptr_done = cxsc->hwptr_done_capture;
300 spin_unlock_irqrestore(&cxsc->slock, flags);
301
302 return hwptr_done;
303}
304
305static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
306 unsigned long offset)
307{
308 void *pageptr = subs->runtime->dma_area + offset;
309
310 return vmalloc_to_page(pageptr);
311}
312
313static struct snd_pcm_ops snd_cx18_pcm_capture_ops = {
314 .open = snd_cx18_pcm_capture_open,
315 .close = snd_cx18_pcm_capture_close,
316 .ioctl = snd_cx18_pcm_ioctl,
317 .hw_params = snd_cx18_pcm_hw_params,
318 .hw_free = snd_cx18_pcm_hw_free,
319 .prepare = snd_cx18_pcm_prepare,
320 .trigger = snd_cx18_pcm_trigger,
321 .pointer = snd_cx18_pcm_pointer,
322 .page = snd_pcm_get_vmalloc_page,
323};
324
325int snd_cx18_pcm_create(struct snd_cx18_card *cxsc)
326{
327 struct snd_pcm *sp;
328 struct snd_card *sc = cxsc->sc;
329 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
330 struct cx18 *cx = to_cx18(v4l2_dev);
331 int ret;
332
333 ret = snd_pcm_new(sc, "CX23418 PCM",
334 0, /* PCM device 0, the only one for this card */
335 0, /* 0 playback substreams */
336 1, /* 1 capture substream */
337 &sp);
338 if (ret) {
339 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
340 __func__, ret);
341 goto err_exit;
342 }
343
344 spin_lock_init(&cxsc->slock);
345
346 snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
347 &snd_cx18_pcm_capture_ops);
348 sp->info_flags = 0;
349 sp->private_data = cxsc;
350 strlcpy(sp->name, cx->card_name, sizeof(sp->name));
351
352 return 0;
353
354err_exit:
355 return ret;
356}
diff --git a/drivers/media/pci/cx18/cx18-alsa-pcm.h b/drivers/media/pci/cx18/cx18-alsa-pcm.h
new file mode 100644
index 000000000000..d26e51f94577
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-alsa-pcm.h
@@ -0,0 +1,27 @@
1/*
2 * ALSA PCM device for the
3 * ALSA interface to cx18 PCM capture streams
4 *
5 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23int __init snd_cx18_pcm_create(struct snd_cx18_card *cxsc);
24
25/* Used by cx18-mailbox to announce the PCM data to the module */
26void cx18_alsa_announce_pcm_data(struct snd_cx18_card *card, u8 *pcm_data,
27 size_t num_bytes);
diff --git a/drivers/media/pci/cx18/cx18-alsa.h b/drivers/media/pci/cx18/cx18-alsa.h
new file mode 100644
index 000000000000..447da374c9e8
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-alsa.h
@@ -0,0 +1,75 @@
1/*
2 * ALSA interface to cx18 PCM capture streams
3 *
4 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 */
21
22struct snd_card;
23
24struct snd_cx18_card {
25 struct v4l2_device *v4l2_dev;
26 struct snd_card *sc;
27 unsigned int capture_transfer_done;
28 unsigned int hwptr_done_capture;
29 struct snd_pcm_substream *capture_pcm_substream;
30 spinlock_t slock;
31};
32
33extern int cx18_alsa_debug;
34
35/*
36 * File operations that manipulate the encoder or video or audio subdevices
37 * need to be serialized. Use the same lock we use for v4l2 file ops.
38 */
39static inline void snd_cx18_lock(struct snd_cx18_card *cxsc)
40{
41 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
42 mutex_lock(&cx->serialize_lock);
43}
44
45static inline void snd_cx18_unlock(struct snd_cx18_card *cxsc)
46{
47 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
48 mutex_unlock(&cx->serialize_lock);
49}
50
51#define CX18_ALSA_DBGFLG_WARN (1 << 0)
52#define CX18_ALSA_DBGFLG_WARN (1 << 0)
53#define CX18_ALSA_DBGFLG_INFO (1 << 1)
54
55#define CX18_ALSA_DEBUG(x, type, fmt, args...) \
56 do { \
57 if ((x) & cx18_alsa_debug) \
58 printk(KERN_INFO "%s-alsa: " type ": " fmt, \
59 v4l2_dev->name , ## args); \
60 } while (0)
61
62#define CX18_ALSA_DEBUG_WARN(fmt, args...) \
63 CX18_ALSA_DEBUG(CX18_ALSA_DBGFLG_WARN, "warning", fmt , ## args)
64
65#define CX18_ALSA_DEBUG_INFO(fmt, args...) \
66 CX18_ALSA_DEBUG(CX18_ALSA_DBGFLG_INFO, "info", fmt , ## args)
67
68#define CX18_ALSA_ERR(fmt, args...) \
69 printk(KERN_ERR "%s-alsa: " fmt, v4l2_dev->name , ## args)
70
71#define CX18_ALSA_WARN(fmt, args...) \
72 printk(KERN_WARNING "%s-alsa: " fmt, v4l2_dev->name , ## args)
73
74#define CX18_ALSA_INFO(fmt, args...) \
75 printk(KERN_INFO "%s-alsa: " fmt, v4l2_dev->name , ## args)
diff --git a/drivers/media/pci/cx18/cx18-audio.c b/drivers/media/pci/cx18/cx18-audio.c
new file mode 100644
index 000000000000..35268923911c
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-audio.c
@@ -0,0 +1,92 @@
1/*
2 * cx18 audio-related functions
3 *
4 * Derived from ivtv-audio.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24#include "cx18-driver.h"
25#include "cx18-io.h"
26#include "cx18-cards.h"
27#include "cx18-audio.h"
28
29#define CX18_AUDIO_ENABLE 0xc72014
30#define CX18_AI1_MUX_MASK 0x30
31#define CX18_AI1_MUX_I2S1 0x00
32#define CX18_AI1_MUX_I2S2 0x10
33#define CX18_AI1_MUX_843_I2S 0x20
34
35/* Selects the audio input and output according to the current
36 settings. */
37int cx18_audio_set_io(struct cx18 *cx)
38{
39 const struct cx18_card_audio_input *in;
40 u32 u, v;
41 int err;
42
43 /* Determine which input to use */
44 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
45 in = &cx->card->radio_input;
46 else
47 in = &cx->card->audio_inputs[cx->audio_input];
48
49 /* handle muxer chips */
50 v4l2_subdev_call(cx->sd_extmux, audio, s_routing,
51 (u32) in->muxer_input, 0, 0);
52
53 err = cx18_call_hw_err(cx, cx->card->hw_audio_ctrl,
54 audio, s_routing, in->audio_input, 0, 0);
55 if (err)
56 return err;
57
58 /* FIXME - this internal mux should be abstracted to a subdev */
59 u = cx18_read_reg(cx, CX18_AUDIO_ENABLE);
60 v = u & ~CX18_AI1_MUX_MASK;
61 switch (in->audio_input) {
62 case CX18_AV_AUDIO_SERIAL1:
63 v |= CX18_AI1_MUX_I2S1;
64 break;
65 case CX18_AV_AUDIO_SERIAL2:
66 v |= CX18_AI1_MUX_I2S2;
67 break;
68 default:
69 v |= CX18_AI1_MUX_843_I2S;
70 break;
71 }
72 if (v == u) {
73 /* force a toggle of some AI1 MUX control bits */
74 u &= ~CX18_AI1_MUX_MASK;
75 switch (in->audio_input) {
76 case CX18_AV_AUDIO_SERIAL1:
77 u |= CX18_AI1_MUX_843_I2S;
78 break;
79 case CX18_AV_AUDIO_SERIAL2:
80 u |= CX18_AI1_MUX_843_I2S;
81 break;
82 default:
83 u |= CX18_AI1_MUX_I2S1;
84 break;
85 }
86 cx18_write_reg_expect(cx, u | 0xb00, CX18_AUDIO_ENABLE,
87 u, CX18_AI1_MUX_MASK);
88 }
89 cx18_write_reg_expect(cx, v | 0xb00, CX18_AUDIO_ENABLE,
90 v, CX18_AI1_MUX_MASK);
91 return 0;
92}
diff --git a/drivers/media/pci/cx18/cx18-audio.h b/drivers/media/pci/cx18/cx18-audio.h
new file mode 100644
index 000000000000..2731d29b0ab9
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-audio.h
@@ -0,0 +1,24 @@
1/*
2 * cx18 audio-related functions
3 *
4 * Derived from ivtv-audio.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24int cx18_audio_set_io(struct cx18 *cx);
diff --git a/drivers/media/pci/cx18/cx18-av-audio.c b/drivers/media/pci/cx18/cx18-av-audio.c
new file mode 100644
index 000000000000..4a24ffb17a7d
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-av-audio.c
@@ -0,0 +1,471 @@
1/*
2 * cx18 ADEC audio functions
3 *
4 * Derived from cx25840-audio.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 */
24
25#include "cx18-driver.h"
26
27static int set_audclk_freq(struct cx18 *cx, u32 freq)
28{
29 struct cx18_av_state *state = &cx->av_state;
30
31 if (freq != 32000 && freq != 44100 && freq != 48000)
32 return -EINVAL;
33
34 /*
35 * The PLL parameters are based on the external crystal frequency that
36 * would ideally be:
37 *
38 * NTSC Color subcarrier freq * 8 =
39 * 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
40 *
41 * The accidents of history and rationale that explain from where this
42 * combination of magic numbers originate can be found in:
43 *
44 * [1] Abrahams, I. C., "Choice of Chrominance Subcarrier Frequency in
45 * the NTSC Standards", Proceedings of the I-R-E, January 1954, pp 79-80
46 *
47 * [2] Abrahams, I. C., "The 'Frequency Interleaving' Principle in the
48 * NTSC Standards", Proceedings of the I-R-E, January 1954, pp 81-83
49 *
50 * As Mike Bradley has rightly pointed out, it's not the exact crystal
51 * frequency that matters, only that all parts of the driver and
52 * firmware are using the same value (close to the ideal value).
53 *
54 * Since I have a strong suspicion that, if the firmware ever assumes a
55 * crystal value at all, it will assume 28.636360 MHz, the crystal
56 * freq used in calculations in this driver will be:
57 *
58 * xtal_freq = 28.636360 MHz
59 *
60 * an error of less than 0.13 ppm which is way, way better than any off
61 * the shelf crystal will have for accuracy anyway.
62 *
63 * Below I aim to run the PLLs' VCOs near 400 MHz to minimze error.
64 *
65 * Many thanks to Jeff Campbell and Mike Bradley for their extensive
66 * investigation, experimentation, testing, and suggested solutions of
67 * of audio/video sync problems with SVideo and CVBS captures.
68 */
69
70 if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
71 switch (freq) {
72 case 32000:
73 /*
74 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
75 * AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x20
76 */
77 cx18_av_write4(cx, 0x108, 0x200d040f);
78
79 /* VID_PLL Fraction = 0x2be2fe */
80 /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
81 cx18_av_write4(cx, 0x10c, 0x002be2fe);
82
83 /* AUX_PLL Fraction = 0x176740c */
84 /* xtal * 0xd.bb3a060/0x20 = 32000 * 384: 393 MHz p-pd*/
85 cx18_av_write4(cx, 0x110, 0x0176740c);
86
87 /* src3/4/6_ctl */
88 /* 0x1.f77f = (4 * xtal/8*2/455) / 32000 */
89 cx18_av_write4(cx, 0x900, 0x0801f77f);
90 cx18_av_write4(cx, 0x904, 0x0801f77f);
91 cx18_av_write4(cx, 0x90c, 0x0801f77f);
92
93 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x20 */
94 cx18_av_write(cx, 0x127, 0x60);
95
96 /* AUD_COUNT = 0x2fff = 8 samples * 4 * 384 - 1 */
97 cx18_av_write4(cx, 0x12c, 0x11202fff);
98
99 /*
100 * EN_AV_LOCK = 0
101 * VID_COUNT = 0x0d2ef8 = 107999.000 * 8 =
102 * ((8 samples/32,000) * (13,500,000 * 8) * 4 - 1) * 8
103 */
104 cx18_av_write4(cx, 0x128, 0xa00d2ef8);
105 break;
106
107 case 44100:
108 /*
109 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
110 * AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x18
111 */
112 cx18_av_write4(cx, 0x108, 0x180e040f);
113
114 /* VID_PLL Fraction = 0x2be2fe */
115 /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
116 cx18_av_write4(cx, 0x10c, 0x002be2fe);
117
118 /* AUX_PLL Fraction = 0x062a1f2 */
119 /* xtal * 0xe.3150f90/0x18 = 44100 * 384: 406 MHz p-pd*/
120 cx18_av_write4(cx, 0x110, 0x0062a1f2);
121
122 /* src3/4/6_ctl */
123 /* 0x1.6d59 = (4 * xtal/8*2/455) / 44100 */
124 cx18_av_write4(cx, 0x900, 0x08016d59);
125 cx18_av_write4(cx, 0x904, 0x08016d59);
126 cx18_av_write4(cx, 0x90c, 0x08016d59);
127
128 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x18 */
129 cx18_av_write(cx, 0x127, 0x58);
130
131 /* AUD_COUNT = 0x92ff = 49 samples * 2 * 384 - 1 */
132 cx18_av_write4(cx, 0x12c, 0x112092ff);
133
134 /*
135 * EN_AV_LOCK = 0
136 * VID_COUNT = 0x1d4bf8 = 239999.000 * 8 =
137 * ((49 samples/44,100) * (13,500,000 * 8) * 2 - 1) * 8
138 */
139 cx18_av_write4(cx, 0x128, 0xa01d4bf8);
140 break;
141
142 case 48000:
143 /*
144 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
145 * AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x16
146 */
147 cx18_av_write4(cx, 0x108, 0x160e040f);
148
149 /* VID_PLL Fraction = 0x2be2fe */
150 /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
151 cx18_av_write4(cx, 0x10c, 0x002be2fe);
152
153 /* AUX_PLL Fraction = 0x05227ad */
154 /* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz p-pd*/
155 cx18_av_write4(cx, 0x110, 0x005227ad);
156
157 /* src3/4/6_ctl */
158 /* 0x1.4faa = (4 * xtal/8*2/455) / 48000 */
159 cx18_av_write4(cx, 0x900, 0x08014faa);
160 cx18_av_write4(cx, 0x904, 0x08014faa);
161 cx18_av_write4(cx, 0x90c, 0x08014faa);
162
163 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */
164 cx18_av_write(cx, 0x127, 0x56);
165
166 /* AUD_COUNT = 0x5fff = 4 samples * 16 * 384 - 1 */
167 cx18_av_write4(cx, 0x12c, 0x11205fff);
168
169 /*
170 * EN_AV_LOCK = 0
171 * VID_COUNT = 0x1193f8 = 143999.000 * 8 =
172 * ((4 samples/48,000) * (13,500,000 * 8) * 16 - 1) * 8
173 */
174 cx18_av_write4(cx, 0x128, 0xa01193f8);
175 break;
176 }
177 } else {
178 switch (freq) {
179 case 32000:
180 /*
181 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
182 * AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x30
183 */
184 cx18_av_write4(cx, 0x108, 0x300d040f);
185
186 /* VID_PLL Fraction = 0x2be2fe */
187 /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
188 cx18_av_write4(cx, 0x10c, 0x002be2fe);
189
190 /* AUX_PLL Fraction = 0x176740c */
191 /* xtal * 0xd.bb3a060/0x30 = 32000 * 256: 393 MHz p-pd*/
192 cx18_av_write4(cx, 0x110, 0x0176740c);
193
194 /* src1_ctl */
195 /* 0x1.0000 = 32000/32000 */
196 cx18_av_write4(cx, 0x8f8, 0x08010000);
197
198 /* src3/4/6_ctl */
199 /* 0x2.0000 = 2 * (32000/32000) */
200 cx18_av_write4(cx, 0x900, 0x08020000);
201 cx18_av_write4(cx, 0x904, 0x08020000);
202 cx18_av_write4(cx, 0x90c, 0x08020000);
203
204 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x30 */
205 cx18_av_write(cx, 0x127, 0x70);
206
207 /* AUD_COUNT = 0x1fff = 8 samples * 4 * 256 - 1 */
208 cx18_av_write4(cx, 0x12c, 0x11201fff);
209
210 /*
211 * EN_AV_LOCK = 0
212 * VID_COUNT = 0x0d2ef8 = 107999.000 * 8 =
213 * ((8 samples/32,000) * (13,500,000 * 8) * 4 - 1) * 8
214 */
215 cx18_av_write4(cx, 0x128, 0xa00d2ef8);
216 break;
217
218 case 44100:
219 /*
220 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
221 * AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x24
222 */
223 cx18_av_write4(cx, 0x108, 0x240e040f);
224
225 /* VID_PLL Fraction = 0x2be2fe */
226 /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
227 cx18_av_write4(cx, 0x10c, 0x002be2fe);
228
229 /* AUX_PLL Fraction = 0x062a1f2 */
230 /* xtal * 0xe.3150f90/0x24 = 44100 * 256: 406 MHz p-pd*/
231 cx18_av_write4(cx, 0x110, 0x0062a1f2);
232
233 /* src1_ctl */
234 /* 0x1.60cd = 44100/32000 */
235 cx18_av_write4(cx, 0x8f8, 0x080160cd);
236
237 /* src3/4/6_ctl */
238 /* 0x1.7385 = 2 * (32000/44100) */
239 cx18_av_write4(cx, 0x900, 0x08017385);
240 cx18_av_write4(cx, 0x904, 0x08017385);
241 cx18_av_write4(cx, 0x90c, 0x08017385);
242
243 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x24 */
244 cx18_av_write(cx, 0x127, 0x64);
245
246 /* AUD_COUNT = 0x61ff = 49 samples * 2 * 256 - 1 */
247 cx18_av_write4(cx, 0x12c, 0x112061ff);
248
249 /*
250 * EN_AV_LOCK = 0
251 * VID_COUNT = 0x1d4bf8 = 239999.000 * 8 =
252 * ((49 samples/44,100) * (13,500,000 * 8) * 2 - 1) * 8
253 */
254 cx18_av_write4(cx, 0x128, 0xa01d4bf8);
255 break;
256
257 case 48000:
258 /*
259 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
260 * AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x20
261 */
262 cx18_av_write4(cx, 0x108, 0x200d040f);
263
264 /* VID_PLL Fraction = 0x2be2fe */
265 /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
266 cx18_av_write4(cx, 0x10c, 0x002be2fe);
267
268 /* AUX_PLL Fraction = 0x176740c */
269 /* xtal * 0xd.bb3a060/0x20 = 48000 * 256: 393 MHz p-pd*/
270 cx18_av_write4(cx, 0x110, 0x0176740c);
271
272 /* src1_ctl */
273 /* 0x1.8000 = 48000/32000 */
274 cx18_av_write4(cx, 0x8f8, 0x08018000);
275
276 /* src3/4/6_ctl */
277 /* 0x1.5555 = 2 * (32000/48000) */
278 cx18_av_write4(cx, 0x900, 0x08015555);
279 cx18_av_write4(cx, 0x904, 0x08015555);
280 cx18_av_write4(cx, 0x90c, 0x08015555);
281
282 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x20 */
283 cx18_av_write(cx, 0x127, 0x60);
284
285 /* AUD_COUNT = 0x3fff = 4 samples * 16 * 256 - 1 */
286 cx18_av_write4(cx, 0x12c, 0x11203fff);
287
288 /*
289 * EN_AV_LOCK = 0
290 * VID_COUNT = 0x1193f8 = 143999.000 * 8 =
291 * ((4 samples/48,000) * (13,500,000 * 8) * 16 - 1) * 8
292 */
293 cx18_av_write4(cx, 0x128, 0xa01193f8);
294 break;
295 }
296 }
297
298 state->audclk_freq = freq;
299
300 return 0;
301}
302
303void cx18_av_audio_set_path(struct cx18 *cx)
304{
305 struct cx18_av_state *state = &cx->av_state;
306 u8 v;
307
308 /* stop microcontroller */
309 v = cx18_av_read(cx, 0x803) & ~0x10;
310 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
311
312 /* assert soft reset */
313 v = cx18_av_read(cx, 0x810) | 0x01;
314 cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
315
316 /* Mute everything to prevent the PFFT! */
317 cx18_av_write(cx, 0x8d3, 0x1f);
318
319 if (state->aud_input <= CX18_AV_AUDIO_SERIAL2) {
320 /* Set Path1 to Serial Audio Input */
321 cx18_av_write4(cx, 0x8d0, 0x01011012);
322
323 /* The microcontroller should not be started for the
324 * non-tuner inputs: autodetection is specific for
325 * TV audio. */
326 } else {
327 /* Set Path1 to Analog Demod Main Channel */
328 cx18_av_write4(cx, 0x8d0, 0x1f063870);
329 }
330
331 set_audclk_freq(cx, state->audclk_freq);
332
333 /* deassert soft reset */
334 v = cx18_av_read(cx, 0x810) & ~0x01;
335 cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
336
337 if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
338 /* When the microcontroller detects the
339 * audio format, it will unmute the lines */
340 v = cx18_av_read(cx, 0x803) | 0x10;
341 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
342 }
343}
344
345static void set_volume(struct cx18 *cx, int volume)
346{
347 /* First convert the volume to msp3400 values (0-127) */
348 int vol = volume >> 9;
349 /* now scale it up to cx18_av values
350 * -114dB to -96dB maps to 0
351 * this should be 19, but in my testing that was 4dB too loud */
352 if (vol <= 23)
353 vol = 0;
354 else
355 vol -= 23;
356
357 /* PATH1_VOLUME */
358 cx18_av_write(cx, 0x8d4, 228 - (vol * 2));
359}
360
361static void set_bass(struct cx18 *cx, int bass)
362{
363 /* PATH1_EQ_BASS_VOL */
364 cx18_av_and_or(cx, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
365}
366
367static void set_treble(struct cx18 *cx, int treble)
368{
369 /* PATH1_EQ_TREBLE_VOL */
370 cx18_av_and_or(cx, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
371}
372
373static void set_balance(struct cx18 *cx, int balance)
374{
375 int bal = balance >> 8;
376 if (bal > 0x80) {
377 /* PATH1_BAL_LEFT */
378 cx18_av_and_or(cx, 0x8d5, 0x7f, 0x80);
379 /* PATH1_BAL_LEVEL */
380 cx18_av_and_or(cx, 0x8d5, ~0x7f, bal & 0x7f);
381 } else {
382 /* PATH1_BAL_LEFT */
383 cx18_av_and_or(cx, 0x8d5, 0x7f, 0x00);
384 /* PATH1_BAL_LEVEL */
385 cx18_av_and_or(cx, 0x8d5, ~0x7f, 0x80 - bal);
386 }
387}
388
389static void set_mute(struct cx18 *cx, int mute)
390{
391 struct cx18_av_state *state = &cx->av_state;
392 u8 v;
393
394 if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
395 /* Must turn off microcontroller in order to mute sound.
396 * Not sure if this is the best method, but it does work.
397 * If the microcontroller is running, then it will undo any
398 * changes to the mute register. */
399 v = cx18_av_read(cx, 0x803);
400 if (mute) {
401 /* disable microcontroller */
402 v &= ~0x10;
403 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
404 cx18_av_write(cx, 0x8d3, 0x1f);
405 } else {
406 /* enable microcontroller */
407 v |= 0x10;
408 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
409 }
410 } else {
411 /* SRC1_MUTE_EN */
412 cx18_av_and_or(cx, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
413 }
414}
415
416int cx18_av_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
417{
418 struct cx18 *cx = v4l2_get_subdevdata(sd);
419 struct cx18_av_state *state = &cx->av_state;
420 int retval;
421 u8 v;
422
423 if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
424 v = cx18_av_read(cx, 0x803) & ~0x10;
425 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
426 cx18_av_write(cx, 0x8d3, 0x1f);
427 }
428 v = cx18_av_read(cx, 0x810) | 0x1;
429 cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
430
431 retval = set_audclk_freq(cx, freq);
432
433 v = cx18_av_read(cx, 0x810) & ~0x1;
434 cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
435 if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
436 v = cx18_av_read(cx, 0x803) | 0x10;
437 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
438 }
439 return retval;
440}
441
442static int cx18_av_audio_s_ctrl(struct v4l2_ctrl *ctrl)
443{
444 struct v4l2_subdev *sd = to_sd(ctrl);
445 struct cx18 *cx = v4l2_get_subdevdata(sd);
446
447 switch (ctrl->id) {
448 case V4L2_CID_AUDIO_VOLUME:
449 set_volume(cx, ctrl->val);
450 break;
451 case V4L2_CID_AUDIO_BASS:
452 set_bass(cx, ctrl->val);
453 break;
454 case V4L2_CID_AUDIO_TREBLE:
455 set_treble(cx, ctrl->val);
456 break;
457 case V4L2_CID_AUDIO_BALANCE:
458 set_balance(cx, ctrl->val);
459 break;
460 case V4L2_CID_AUDIO_MUTE:
461 set_mute(cx, ctrl->val);
462 break;
463 default:
464 return -EINVAL;
465 }
466 return 0;
467}
468
469const struct v4l2_ctrl_ops cx18_av_audio_ctrl_ops = {
470 .s_ctrl = cx18_av_audio_s_ctrl,
471};
diff --git a/drivers/media/pci/cx18/cx18-av-core.c b/drivers/media/pci/cx18/cx18-av-core.c
new file mode 100644
index 000000000000..f164b7f610a5
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-av-core.c
@@ -0,0 +1,1401 @@
1/*
2 * cx18 ADEC audio functions
3 *
4 * Derived from cx25840-core.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 */
24
25#include <media/v4l2-chip-ident.h>
26#include "cx18-driver.h"
27#include "cx18-io.h"
28#include "cx18-cards.h"
29
30int cx18_av_write(struct cx18 *cx, u16 addr, u8 value)
31{
32 u32 reg = 0xc40000 + (addr & ~3);
33 u32 mask = 0xff;
34 int shift = (addr & 3) * 8;
35 u32 x = cx18_read_reg(cx, reg);
36
37 x = (x & ~(mask << shift)) | ((u32)value << shift);
38 cx18_write_reg(cx, x, reg);
39 return 0;
40}
41
42int cx18_av_write_expect(struct cx18 *cx, u16 addr, u8 value, u8 eval, u8 mask)
43{
44 u32 reg = 0xc40000 + (addr & ~3);
45 int shift = (addr & 3) * 8;
46 u32 x = cx18_read_reg(cx, reg);
47
48 x = (x & ~((u32)0xff << shift)) | ((u32)value << shift);
49 cx18_write_reg_expect(cx, x, reg,
50 ((u32)eval << shift), ((u32)mask << shift));
51 return 0;
52}
53
54int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value)
55{
56 cx18_write_reg(cx, value, 0xc40000 + addr);
57 return 0;
58}
59
60int
61cx18_av_write4_expect(struct cx18 *cx, u16 addr, u32 value, u32 eval, u32 mask)
62{
63 cx18_write_reg_expect(cx, value, 0xc40000 + addr, eval, mask);
64 return 0;
65}
66
67int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value)
68{
69 cx18_write_reg_noretry(cx, value, 0xc40000 + addr);
70 return 0;
71}
72
73u8 cx18_av_read(struct cx18 *cx, u16 addr)
74{
75 u32 x = cx18_read_reg(cx, 0xc40000 + (addr & ~3));
76 int shift = (addr & 3) * 8;
77
78 return (x >> shift) & 0xff;
79}
80
81u32 cx18_av_read4(struct cx18 *cx, u16 addr)
82{
83 return cx18_read_reg(cx, 0xc40000 + addr);
84}
85
86int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned and_mask,
87 u8 or_value)
88{
89 return cx18_av_write(cx, addr,
90 (cx18_av_read(cx, addr) & and_mask) |
91 or_value);
92}
93
94int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 and_mask,
95 u32 or_value)
96{
97 return cx18_av_write4(cx, addr,
98 (cx18_av_read4(cx, addr) & and_mask) |
99 or_value);
100}
101
102static void cx18_av_init(struct cx18 *cx)
103{
104 /*
105 * The crystal freq used in calculations in this driver will be
106 * 28.636360 MHz.
107 * Aim to run the PLLs' VCOs near 400 MHz to minimze errors.
108 */
109
110 /*
111 * VDCLK Integer = 0x0f, Post Divider = 0x04
112 * AIMCLK Integer = 0x0e, Post Divider = 0x16
113 */
114 cx18_av_write4(cx, CXADEC_PLL_CTRL1, 0x160e040f);
115
116 /* VDCLK Fraction = 0x2be2fe */
117 /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz before post divide */
118 cx18_av_write4(cx, CXADEC_VID_PLL_FRAC, 0x002be2fe);
119
120 /* AIMCLK Fraction = 0x05227ad */
121 /* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz pre post-div*/
122 cx18_av_write4(cx, CXADEC_AUX_PLL_FRAC, 0x005227ad);
123
124 /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */
125 cx18_av_write(cx, CXADEC_I2S_MCLK, 0x56);
126}
127
128static void cx18_av_initialize(struct v4l2_subdev *sd)
129{
130 struct cx18_av_state *state = to_cx18_av_state(sd);
131 struct cx18 *cx = v4l2_get_subdevdata(sd);
132 int default_volume;
133 u32 v;
134
135 cx18_av_loadfw(cx);
136 /* Stop 8051 code execution */
137 cx18_av_write4_expect(cx, CXADEC_DL_CTL, 0x03000000,
138 0x03000000, 0x13000000);
139
140 /* initallize the PLL by toggling sleep bit */
141 v = cx18_av_read4(cx, CXADEC_HOST_REG1);
142 /* enable sleep mode - register appears to be read only... */
143 cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v | 1, v, 0xfffe);
144 /* disable sleep mode */
145 cx18_av_write4_expect(cx, CXADEC_HOST_REG1, v & 0xfffe,
146 v & 0xfffe, 0xffff);
147
148 /* initialize DLLs */
149 v = cx18_av_read4(cx, CXADEC_DLL1_DIAG_CTRL) & 0xE1FFFEFF;
150 /* disable FLD */
151 cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v);
152 /* enable FLD */
153 cx18_av_write4(cx, CXADEC_DLL1_DIAG_CTRL, v | 0x10000100);
154
155 v = cx18_av_read4(cx, CXADEC_DLL2_DIAG_CTRL) & 0xE1FFFEFF;
156 /* disable FLD */
157 cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v);
158 /* enable FLD */
159 cx18_av_write4(cx, CXADEC_DLL2_DIAG_CTRL, v | 0x06000100);
160
161 /* set analog bias currents. Set Vreg to 1.20V. */
162 cx18_av_write4(cx, CXADEC_AFE_DIAG_CTRL1, 0x000A1802);
163
164 v = cx18_av_read4(cx, CXADEC_AFE_DIAG_CTRL3) | 1;
165 /* enable TUNE_FIL_RST */
166 cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3, v, v, 0x03009F0F);
167 /* disable TUNE_FIL_RST */
168 cx18_av_write4_expect(cx, CXADEC_AFE_DIAG_CTRL3,
169 v & 0xFFFFFFFE, v & 0xFFFFFFFE, 0x03009F0F);
170
171 /* enable 656 output */
172 cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x040C00);
173
174 /* video output drive strength */
175 cx18_av_and_or4(cx, CXADEC_PIN_CTRL2, ~0, 0x2);
176
177 /* reset video */
178 cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0x8000);
179 cx18_av_write4(cx, CXADEC_SOFT_RST_CTRL, 0);
180
181 /*
182 * Disable Video Auto-config of the Analog Front End and Video PLL.
183 *
184 * Since we only use BT.656 pixel mode, which works for both 525 and 625
185 * line systems, it's just easier for us to set registers
186 * 0x102 (CXADEC_CHIP_CTRL), 0x104-0x106 (CXADEC_AFE_CTRL),
187 * 0x108-0x109 (CXADEC_PLL_CTRL1), and 0x10c-0x10f (CXADEC_VID_PLL_FRAC)
188 * ourselves, than to run around cleaning up after the auto-config.
189 *
190 * (Note: my CX23418 chip doesn't seem to let the ACFG_DIS bit
191 * get set to 1, but OTOH, it doesn't seem to do AFE and VID PLL
192 * autoconfig either.)
193 *
194 * As a default, also turn off Dual mode for ADC2 and set ADC2 to CH3.
195 */
196 cx18_av_and_or4(cx, CXADEC_CHIP_CTRL, 0xFFFBFFFF, 0x00120000);
197
198 /* Setup the Video and and Aux/Audio PLLs */
199 cx18_av_init(cx);
200
201 /* set video to auto-detect */
202 /* Clear bits 11-12 to enable slow locking mode. Set autodetect mode */
203 /* set the comb notch = 1 */
204 cx18_av_and_or4(cx, CXADEC_MODE_CTRL, 0xFFF7E7F0, 0x02040800);
205
206 /* Enable wtw_en in CRUSH_CTRL (Set bit 22) */
207 /* Enable maj_sel in CRUSH_CTRL (Set bit 20) */
208 cx18_av_and_or4(cx, CXADEC_CRUSH_CTRL, ~0, 0x00500000);
209
210 /* Set VGA_TRACK_RANGE to 0x20 */
211 cx18_av_and_or4(cx, CXADEC_DFE_CTRL2, 0xFFFF00FF, 0x00002000);
212
213 /*
214 * Initial VBI setup
215 * VIP-1.1, 10 bit mode, enable Raw, disable sliced,
216 * don't clamp raw samples when codes are in use, 1 byte user D-words,
217 * IDID0 has line #, RP code V bit transition on VBLANK, data during
218 * blanking intervals
219 */
220 cx18_av_write4(cx, CXADEC_OUT_CTRL1, 0x4013252e);
221
222 /* Set the video input.
223 The setting in MODE_CTRL gets lost when we do the above setup */
224 /* EncSetSignalStd(dwDevNum, pEnc->dwSigStd); */
225 /* EncSetVideoInput(dwDevNum, pEnc->VidIndSelection); */
226
227 /*
228 * Analog Front End (AFE)
229 * Default to luma on ch1/ADC1, chroma on ch2/ADC2, SIF on ch3/ADC2
230 * bypass_ch[1-3] use filter
231 * droop_comp_ch[1-3] disable
232 * clamp_en_ch[1-3] disable
233 * aud_in_sel ADC2
234 * luma_in_sel ADC1
235 * chroma_in_sel ADC2
236 * clamp_sel_ch[2-3] midcode
237 * clamp_sel_ch1 video decoder
238 * vga_sel_ch3 audio decoder
239 * vga_sel_ch[1-2] video decoder
240 * half_bw_ch[1-3] disable
241 * +12db_ch[1-3] disable
242 */
243 cx18_av_and_or4(cx, CXADEC_AFE_CTRL, 0xFF000000, 0x00005D00);
244
245/* if(dwEnable && dw3DCombAvailable) { */
246/* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x7728021F); */
247/* } else { */
248/* CxDevWrReg(CXADEC_SRC_COMB_CFG, 0x6628021F); */
249/* } */
250 cx18_av_write4(cx, CXADEC_SRC_COMB_CFG, 0x6628021F);
251 default_volume = cx18_av_read(cx, 0x8d4);
252 /*
253 * Enforce the legacy volume scale mapping limits to avoid
254 * -ERANGE errors when initializing the volume control
255 */
256 if (default_volume > 228) {
257 /* Bottom out at -96 dB, v4l2 vol range 0x2e00-0x2fff */
258 default_volume = 228;
259 cx18_av_write(cx, 0x8d4, 228);
260 } else if (default_volume < 20) {
261 /* Top out at + 8 dB, v4l2 vol range 0xfe00-0xffff */
262 default_volume = 20;
263 cx18_av_write(cx, 0x8d4, 20);
264 }
265 default_volume = (((228 - default_volume) >> 1) + 23) << 9;
266 state->volume->cur.val = state->volume->default_value = default_volume;
267 v4l2_ctrl_handler_setup(&state->hdl);
268}
269
270static int cx18_av_reset(struct v4l2_subdev *sd, u32 val)
271{
272 cx18_av_initialize(sd);
273 return 0;
274}
275
276static int cx18_av_load_fw(struct v4l2_subdev *sd)
277{
278 struct cx18_av_state *state = to_cx18_av_state(sd);
279
280 if (!state->is_initialized) {
281 /* initialize on first use */
282 state->is_initialized = 1;
283 cx18_av_initialize(sd);
284 }
285 return 0;
286}
287
288void cx18_av_std_setup(struct cx18 *cx)
289{
290 struct cx18_av_state *state = &cx->av_state;
291 struct v4l2_subdev *sd = &state->sd;
292 v4l2_std_id std = state->std;
293
294 /*
295 * Video ADC crystal clock to pixel clock SRC decimation ratio
296 * 28.636360 MHz/13.5 Mpps * 256 = 0x21f.07b
297 */
298 const int src_decimation = 0x21f;
299
300 int hblank, hactive, burst, vblank, vactive, sc;
301 int vblank656;
302 int luma_lpf, uv_lpf, comb;
303 u32 pll_int, pll_frac, pll_post;
304
305 /* datasheet startup, step 8d */
306 if (std & ~V4L2_STD_NTSC)
307 cx18_av_write(cx, 0x49f, 0x11);
308 else
309 cx18_av_write(cx, 0x49f, 0x14);
310
311 /*
312 * Note: At the end of a field, there are 3 sets of half line duration
313 * (double horizontal rate) pulses:
314 *
315 * 5 (625) or 6 (525) half-lines to blank for the vertical retrace
316 * 5 (625) or 6 (525) vertical sync pulses of half line duration
317 * 5 (625) or 6 (525) half-lines of equalization pulses
318 */
319 if (std & V4L2_STD_625_50) {
320 /*
321 * The following relationships of half line counts should hold:
322 * 625 = vblank656 + vactive
323 * 10 = vblank656 - vblank = vsync pulses + equalization pulses
324 *
325 * vblank656: half lines after line 625/mid-313 of blanked video
326 * vblank: half lines, after line 5/317, of blanked video
327 * vactive: half lines of active video +
328 * 5 half lines after the end of active video
329 *
330 * As far as I can tell:
331 * vblank656 starts counting from the falling edge of the first
332 * vsync pulse (start of line 1 or mid-313)
333 * vblank starts counting from the after the 5 vsync pulses and
334 * 5 or 4 equalization pulses (start of line 6 or 318)
335 *
336 * For 625 line systems the driver will extract VBI information
337 * from lines 6-23 and lines 318-335 (but the slicer can only
338 * handle 17 lines, not the 18 in the vblank region).
339 * In addition, we need vblank656 and vblank to be one whole
340 * line longer, to cover line 24 and 336, so the SAV/EAV RP
341 * codes get generated such that the encoder can actually
342 * extract line 23 & 335 (WSS). We'll lose 1 line in each field
343 * at the top of the screen.
344 *
345 * It appears the 5 half lines that happen after active
346 * video must be included in vactive (579 instead of 574),
347 * otherwise the colors get badly displayed in various regions
348 * of the screen. I guess the chroma comb filter gets confused
349 * without them (at least when a PVR-350 is the PAL source).
350 */
351 vblank656 = 48; /* lines 1 - 24 & 313 - 336 */
352 vblank = 38; /* lines 6 - 24 & 318 - 336 */
353 vactive = 579; /* lines 24 - 313 & 337 - 626 */
354
355 /*
356 * For a 13.5 Mpps clock and 15,625 Hz line rate, a line is
357 * is 864 pixels = 720 active + 144 blanking. ITU-R BT.601
358 * specifies 12 luma clock periods or ~ 0.9 * 13.5 Mpps after
359 * the end of active video to start a horizontal line, so that
360 * leaves 132 pixels of hblank to ignore.
361 */
362 hblank = 132;
363 hactive = 720;
364
365 /*
366 * Burst gate delay (for 625 line systems)
367 * Hsync leading edge to color burst rise = 5.6 us
368 * Color burst width = 2.25 us
369 * Gate width = 4 pixel clocks
370 * (5.6 us + 2.25/2 us) * 13.5 Mpps + 4/2 clocks = 92.79 clocks
371 */
372 burst = 93;
373 luma_lpf = 2;
374 if (std & V4L2_STD_PAL) {
375 uv_lpf = 1;
376 comb = 0x20;
377 /* sc = 4433618.75 * src_decimation/28636360 * 2^13 */
378 sc = 688700;
379 } else if (std == V4L2_STD_PAL_Nc) {
380 uv_lpf = 1;
381 comb = 0x20;
382 /* sc = 3582056.25 * src_decimation/28636360 * 2^13 */
383 sc = 556422;
384 } else { /* SECAM */
385 uv_lpf = 0;
386 comb = 0;
387 /* (fr + fb)/2 = (4406260 + 4250000)/2 = 4328130 */
388 /* sc = 4328130 * src_decimation/28636360 * 2^13 */
389 sc = 672314;
390 }
391 } else {
392 /*
393 * The following relationships of half line counts should hold:
394 * 525 = prevsync + vblank656 + vactive
395 * 12 = vblank656 - vblank = vsync pulses + equalization pulses
396 *
397 * prevsync: 6 half-lines before the vsync pulses
398 * vblank656: half lines, after line 3/mid-266, of blanked video
399 * vblank: half lines, after line 9/272, of blanked video
400 * vactive: half lines of active video
401 *
402 * As far as I can tell:
403 * vblank656 starts counting from the falling edge of the first
404 * vsync pulse (start of line 4 or mid-266)
405 * vblank starts counting from the after the 6 vsync pulses and
406 * 6 or 5 equalization pulses (start of line 10 or 272)
407 *
408 * For 525 line systems the driver will extract VBI information
409 * from lines 10-21 and lines 273-284.
410 */
411 vblank656 = 38; /* lines 4 - 22 & 266 - 284 */
412 vblank = 26; /* lines 10 - 22 & 272 - 284 */
413 vactive = 481; /* lines 23 - 263 & 285 - 525 */
414
415 /*
416 * For a 13.5 Mpps clock and 15,734.26 Hz line rate, a line is
417 * is 858 pixels = 720 active + 138 blanking. The Hsync leading
418 * edge should happen 1.2 us * 13.5 Mpps ~= 16 pixels after the
419 * end of active video, leaving 122 pixels of hblank to ignore
420 * before active video starts.
421 */
422 hactive = 720;
423 hblank = 122;
424 luma_lpf = 1;
425 uv_lpf = 1;
426
427 /*
428 * Burst gate delay (for 525 line systems)
429 * Hsync leading edge to color burst rise = 5.3 us
430 * Color burst width = 2.5 us
431 * Gate width = 4 pixel clocks
432 * (5.3 us + 2.5/2 us) * 13.5 Mpps + 4/2 clocks = 90.425 clocks
433 */
434 if (std == V4L2_STD_PAL_60) {
435 burst = 90;
436 luma_lpf = 2;
437 comb = 0x20;
438 /* sc = 4433618.75 * src_decimation/28636360 * 2^13 */
439 sc = 688700;
440 } else if (std == V4L2_STD_PAL_M) {
441 /* The 97 needs to be verified against PAL-M timings */
442 burst = 97;
443 comb = 0x20;
444 /* sc = 3575611.49 * src_decimation/28636360 * 2^13 */
445 sc = 555421;
446 } else {
447 burst = 90;
448 comb = 0x66;
449 /* sc = 3579545.45.. * src_decimation/28636360 * 2^13 */
450 sc = 556032;
451 }
452 }
453
454 /* DEBUG: Displays configured PLL frequency */
455 pll_int = cx18_av_read(cx, 0x108);
456 pll_frac = cx18_av_read4(cx, 0x10c) & 0x1ffffff;
457 pll_post = cx18_av_read(cx, 0x109);
458 CX18_DEBUG_INFO_DEV(sd, "PLL regs = int: %u, frac: %u, post: %u\n",
459 pll_int, pll_frac, pll_post);
460
461 if (pll_post) {
462 int fsc, pll;
463 u64 tmp;
464
465 pll = (28636360L * ((((u64)pll_int) << 25) + pll_frac)) >> 25;
466 pll /= pll_post;
467 CX18_DEBUG_INFO_DEV(sd, "Video PLL = %d.%06d MHz\n",
468 pll / 1000000, pll % 1000000);
469 CX18_DEBUG_INFO_DEV(sd, "Pixel rate = %d.%06d Mpixel/sec\n",
470 pll / 8000000, (pll / 8) % 1000000);
471
472 CX18_DEBUG_INFO_DEV(sd, "ADC XTAL/pixel clock decimation ratio "
473 "= %d.%03d\n", src_decimation / 256,
474 ((src_decimation % 256) * 1000) / 256);
475
476 tmp = 28636360 * (u64) sc;
477 do_div(tmp, src_decimation);
478 fsc = tmp >> 13;
479 CX18_DEBUG_INFO_DEV(sd,
480 "Chroma sub-carrier initial freq = %d.%06d "
481 "MHz\n", fsc / 1000000, fsc % 1000000);
482
483 CX18_DEBUG_INFO_DEV(sd, "hblank %i, hactive %i, vblank %i, "
484 "vactive %i, vblank656 %i, src_dec %i, "
485 "burst 0x%02x, luma_lpf %i, uv_lpf %i, "
486 "comb 0x%02x, sc 0x%06x\n",
487 hblank, hactive, vblank, vactive, vblank656,
488 src_decimation, burst, luma_lpf, uv_lpf,
489 comb, sc);
490 }
491
492 /* Sets horizontal blanking delay and active lines */
493 cx18_av_write(cx, 0x470, hblank);
494 cx18_av_write(cx, 0x471, 0xff & (((hblank >> 8) & 0x3) |
495 (hactive << 4)));
496 cx18_av_write(cx, 0x472, hactive >> 4);
497
498 /* Sets burst gate delay */
499 cx18_av_write(cx, 0x473, burst);
500
501 /* Sets vertical blanking delay and active duration */
502 cx18_av_write(cx, 0x474, vblank);
503 cx18_av_write(cx, 0x475, 0xff & (((vblank >> 8) & 0x3) |
504 (vactive << 4)));
505 cx18_av_write(cx, 0x476, vactive >> 4);
506 cx18_av_write(cx, 0x477, vblank656);
507
508 /* Sets src decimation rate */
509 cx18_av_write(cx, 0x478, 0xff & src_decimation);
510 cx18_av_write(cx, 0x479, 0xff & (src_decimation >> 8));
511
512 /* Sets Luma and UV Low pass filters */
513 cx18_av_write(cx, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
514
515 /* Enables comb filters */
516 cx18_av_write(cx, 0x47b, comb);
517
518 /* Sets SC Step*/
519 cx18_av_write(cx, 0x47c, sc);
520 cx18_av_write(cx, 0x47d, 0xff & sc >> 8);
521 cx18_av_write(cx, 0x47e, 0xff & sc >> 16);
522
523 if (std & V4L2_STD_625_50) {
524 state->slicer_line_delay = 1;
525 state->slicer_line_offset = (6 + state->slicer_line_delay - 2);
526 } else {
527 state->slicer_line_delay = 0;
528 state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
529 }
530 cx18_av_write(cx, 0x47f, state->slicer_line_delay);
531}
532
533static void input_change(struct cx18 *cx)
534{
535 struct cx18_av_state *state = &cx->av_state;
536 v4l2_std_id std = state->std;
537 u8 v;
538
539 /* Follow step 8c and 8d of section 3.16 in the cx18_av datasheet */
540 cx18_av_write(cx, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
541 cx18_av_and_or(cx, 0x401, ~0x60, 0);
542 cx18_av_and_or(cx, 0x401, ~0x60, 0x60);
543
544 if (std & V4L2_STD_525_60) {
545 if (std == V4L2_STD_NTSC_M_JP) {
546 /* Japan uses EIAJ audio standard */
547 cx18_av_write_expect(cx, 0x808, 0xf7, 0xf7, 0xff);
548 cx18_av_write_expect(cx, 0x80b, 0x02, 0x02, 0x3f);
549 } else if (std == V4L2_STD_NTSC_M_KR) {
550 /* South Korea uses A2 audio standard */
551 cx18_av_write_expect(cx, 0x808, 0xf8, 0xf8, 0xff);
552 cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
553 } else {
554 /* Others use the BTSC audio standard */
555 cx18_av_write_expect(cx, 0x808, 0xf6, 0xf6, 0xff);
556 cx18_av_write_expect(cx, 0x80b, 0x01, 0x01, 0x3f);
557 }
558 } else if (std & V4L2_STD_PAL) {
559 /* Follow tuner change procedure for PAL */
560 cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
561 cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
562 } else if (std & V4L2_STD_SECAM) {
563 /* Select autodetect for SECAM */
564 cx18_av_write_expect(cx, 0x808, 0xff, 0xff, 0xff);
565 cx18_av_write_expect(cx, 0x80b, 0x03, 0x03, 0x3f);
566 }
567
568 v = cx18_av_read(cx, 0x803);
569 if (v & 0x10) {
570 /* restart audio decoder microcontroller */
571 v &= ~0x10;
572 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
573 v |= 0x10;
574 cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
575 }
576}
577
578static int cx18_av_s_frequency(struct v4l2_subdev *sd,
579 struct v4l2_frequency *freq)
580{
581 struct cx18 *cx = v4l2_get_subdevdata(sd);
582 input_change(cx);
583 return 0;
584}
585
586static int set_input(struct cx18 *cx, enum cx18_av_video_input vid_input,
587 enum cx18_av_audio_input aud_input)
588{
589 struct cx18_av_state *state = &cx->av_state;
590 struct v4l2_subdev *sd = &state->sd;
591
592 enum analog_signal_type {
593 NONE, CVBS, Y, C, SIF, Pb, Pr
594 } ch[3] = {NONE, NONE, NONE};
595
596 u8 afe_mux_cfg;
597 u8 adc2_cfg;
598 u8 input_mode;
599 u32 afe_cfg;
600 int i;
601
602 CX18_DEBUG_INFO_DEV(sd, "decoder set video input %d, audio input %d\n",
603 vid_input, aud_input);
604
605 if (vid_input >= CX18_AV_COMPOSITE1 &&
606 vid_input <= CX18_AV_COMPOSITE8) {
607 afe_mux_cfg = 0xf0 + (vid_input - CX18_AV_COMPOSITE1);
608 ch[0] = CVBS;
609 input_mode = 0x0;
610 } else if (vid_input >= CX18_AV_COMPONENT_LUMA1) {
611 int luma = vid_input & 0xf000;
612 int r_chroma = vid_input & 0xf0000;
613 int b_chroma = vid_input & 0xf00000;
614
615 if ((vid_input & ~0xfff000) ||
616 luma < CX18_AV_COMPONENT_LUMA1 ||
617 luma > CX18_AV_COMPONENT_LUMA8 ||
618 r_chroma < CX18_AV_COMPONENT_R_CHROMA4 ||
619 r_chroma > CX18_AV_COMPONENT_R_CHROMA6 ||
620 b_chroma < CX18_AV_COMPONENT_B_CHROMA7 ||
621 b_chroma > CX18_AV_COMPONENT_B_CHROMA8) {
622 CX18_ERR_DEV(sd, "0x%06x is not a valid video input!\n",
623 vid_input);
624 return -EINVAL;
625 }
626 afe_mux_cfg = (luma - CX18_AV_COMPONENT_LUMA1) >> 12;
627 ch[0] = Y;
628 afe_mux_cfg |= (r_chroma - CX18_AV_COMPONENT_R_CHROMA4) >> 12;
629 ch[1] = Pr;
630 afe_mux_cfg |= (b_chroma - CX18_AV_COMPONENT_B_CHROMA7) >> 14;
631 ch[2] = Pb;
632 input_mode = 0x6;
633 } else {
634 int luma = vid_input & 0xf0;
635 int chroma = vid_input & 0xf00;
636
637 if ((vid_input & ~0xff0) ||
638 luma < CX18_AV_SVIDEO_LUMA1 ||
639 luma > CX18_AV_SVIDEO_LUMA8 ||
640 chroma < CX18_AV_SVIDEO_CHROMA4 ||
641 chroma > CX18_AV_SVIDEO_CHROMA8) {
642 CX18_ERR_DEV(sd, "0x%06x is not a valid video input!\n",
643 vid_input);
644 return -EINVAL;
645 }
646 afe_mux_cfg = 0xf0 + ((luma - CX18_AV_SVIDEO_LUMA1) >> 4);
647 ch[0] = Y;
648 if (chroma >= CX18_AV_SVIDEO_CHROMA7) {
649 afe_mux_cfg &= 0x3f;
650 afe_mux_cfg |= (chroma - CX18_AV_SVIDEO_CHROMA7) >> 2;
651 ch[2] = C;
652 } else {
653 afe_mux_cfg &= 0xcf;
654 afe_mux_cfg |= (chroma - CX18_AV_SVIDEO_CHROMA4) >> 4;
655 ch[1] = C;
656 }
657 input_mode = 0x2;
658 }
659
660 switch (aud_input) {
661 case CX18_AV_AUDIO_SERIAL1:
662 case CX18_AV_AUDIO_SERIAL2:
663 /* do nothing, use serial audio input */
664 break;
665 case CX18_AV_AUDIO4:
666 afe_mux_cfg &= ~0x30;
667 ch[1] = SIF;
668 break;
669 case CX18_AV_AUDIO5:
670 afe_mux_cfg = (afe_mux_cfg & ~0x30) | 0x10;
671 ch[1] = SIF;
672 break;
673 case CX18_AV_AUDIO6:
674 afe_mux_cfg = (afe_mux_cfg & ~0x30) | 0x20;
675 ch[1] = SIF;
676 break;
677 case CX18_AV_AUDIO7:
678 afe_mux_cfg &= ~0xc0;
679 ch[2] = SIF;
680 break;
681 case CX18_AV_AUDIO8:
682 afe_mux_cfg = (afe_mux_cfg & ~0xc0) | 0x40;
683 ch[2] = SIF;
684 break;
685
686 default:
687 CX18_ERR_DEV(sd, "0x%04x is not a valid audio input!\n",
688 aud_input);
689 return -EINVAL;
690 }
691
692 /* Set up analog front end multiplexers */
693 cx18_av_write_expect(cx, 0x103, afe_mux_cfg, afe_mux_cfg, 0xf7);
694 /* Set INPUT_MODE to Composite, S-Video, or Component */
695 cx18_av_and_or(cx, 0x401, ~0x6, input_mode);
696
697 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
698 adc2_cfg = cx18_av_read(cx, 0x102);
699 if (ch[2] == NONE)
700 adc2_cfg &= ~0x2; /* No sig on CH3, set ADC2 to CH2 for input */
701 else
702 adc2_cfg |= 0x2; /* Signal on CH3, set ADC2 to CH3 for input */
703
704 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
705 if (ch[1] != NONE && ch[2] != NONE)
706 adc2_cfg |= 0x4; /* Set dual mode */
707 else
708 adc2_cfg &= ~0x4; /* Clear dual mode */
709 cx18_av_write_expect(cx, 0x102, adc2_cfg, adc2_cfg, 0x17);
710
711 /* Configure the analog front end */
712 afe_cfg = cx18_av_read4(cx, CXADEC_AFE_CTRL);
713 afe_cfg &= 0xff000000;
714 afe_cfg |= 0x00005000; /* CHROMA_IN, AUD_IN: ADC2; LUMA_IN: ADC1 */
715 if (ch[1] != NONE && ch[2] != NONE)
716 afe_cfg |= 0x00000030; /* half_bw_ch[2-3] since in dual mode */
717
718 for (i = 0; i < 3; i++) {
719 switch (ch[i]) {
720 default:
721 case NONE:
722 /* CLAMP_SEL = Fixed to midcode clamp level */
723 afe_cfg |= (0x00000200 << i);
724 break;
725 case CVBS:
726 case Y:
727 if (i > 0)
728 afe_cfg |= 0x00002000; /* LUMA_IN_SEL: ADC2 */
729 break;
730 case C:
731 case Pb:
732 case Pr:
733 /* CLAMP_SEL = Fixed to midcode clamp level */
734 afe_cfg |= (0x00000200 << i);
735 if (i == 0 && ch[i] == C)
736 afe_cfg &= ~0x00001000; /* CHROMA_IN_SEL ADC1 */
737 break;
738 case SIF:
739 /*
740 * VGA_GAIN_SEL = Audio Decoder
741 * CLAMP_SEL = Fixed to midcode clamp level
742 */
743 afe_cfg |= (0x00000240 << i);
744 if (i == 0)
745 afe_cfg &= ~0x00004000; /* AUD_IN_SEL ADC1 */
746 break;
747 }
748 }
749
750 cx18_av_write4(cx, CXADEC_AFE_CTRL, afe_cfg);
751
752 state->vid_input = vid_input;
753 state->aud_input = aud_input;
754 cx18_av_audio_set_path(cx);
755 input_change(cx);
756 return 0;
757}
758
759static int cx18_av_s_video_routing(struct v4l2_subdev *sd,
760 u32 input, u32 output, u32 config)
761{
762 struct cx18_av_state *state = to_cx18_av_state(sd);
763 struct cx18 *cx = v4l2_get_subdevdata(sd);
764 return set_input(cx, input, state->aud_input);
765}
766
767static int cx18_av_s_audio_routing(struct v4l2_subdev *sd,
768 u32 input, u32 output, u32 config)
769{
770 struct cx18_av_state *state = to_cx18_av_state(sd);
771 struct cx18 *cx = v4l2_get_subdevdata(sd);
772 return set_input(cx, state->vid_input, input);
773}
774
775static int cx18_av_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
776{
777 struct cx18_av_state *state = to_cx18_av_state(sd);
778 struct cx18 *cx = v4l2_get_subdevdata(sd);
779 u8 vpres;
780 u8 mode;
781 int val = 0;
782
783 if (state->radio)
784 return 0;
785
786 vpres = cx18_av_read(cx, 0x40e) & 0x20;
787 vt->signal = vpres ? 0xffff : 0x0;
788
789 vt->capability |=
790 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
791 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
792
793 mode = cx18_av_read(cx, 0x804);
794
795 /* get rxsubchans and audmode */
796 if ((mode & 0xf) == 1)
797 val |= V4L2_TUNER_SUB_STEREO;
798 else
799 val |= V4L2_TUNER_SUB_MONO;
800
801 if (mode == 2 || mode == 4)
802 val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
803
804 if (mode & 0x10)
805 val |= V4L2_TUNER_SUB_SAP;
806
807 vt->rxsubchans = val;
808 vt->audmode = state->audmode;
809 return 0;
810}
811
812static int cx18_av_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
813{
814 struct cx18_av_state *state = to_cx18_av_state(sd);
815 struct cx18 *cx = v4l2_get_subdevdata(sd);
816 u8 v;
817
818 if (state->radio)
819 return 0;
820
821 v = cx18_av_read(cx, 0x809);
822 v &= ~0xf;
823
824 switch (vt->audmode) {
825 case V4L2_TUNER_MODE_MONO:
826 /* mono -> mono
827 stereo -> mono
828 bilingual -> lang1 */
829 break;
830 case V4L2_TUNER_MODE_STEREO:
831 case V4L2_TUNER_MODE_LANG1:
832 /* mono -> mono
833 stereo -> stereo
834 bilingual -> lang1 */
835 v |= 0x4;
836 break;
837 case V4L2_TUNER_MODE_LANG1_LANG2:
838 /* mono -> mono
839 stereo -> stereo
840 bilingual -> lang1/lang2 */
841 v |= 0x7;
842 break;
843 case V4L2_TUNER_MODE_LANG2:
844 /* mono -> mono
845 stereo -> stereo
846 bilingual -> lang2 */
847 v |= 0x1;
848 break;
849 default:
850 return -EINVAL;
851 }
852 cx18_av_write_expect(cx, 0x809, v, v, 0xff);
853 state->audmode = vt->audmode;
854 return 0;
855}
856
857static int cx18_av_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
858{
859 struct cx18_av_state *state = to_cx18_av_state(sd);
860 struct cx18 *cx = v4l2_get_subdevdata(sd);
861
862 u8 fmt = 0; /* zero is autodetect */
863 u8 pal_m = 0;
864
865 if (state->radio == 0 && state->std == norm)
866 return 0;
867
868 state->radio = 0;
869 state->std = norm;
870
871 /* First tests should be against specific std */
872 if (state->std == V4L2_STD_NTSC_M_JP) {
873 fmt = 0x2;
874 } else if (state->std == V4L2_STD_NTSC_443) {
875 fmt = 0x3;
876 } else if (state->std == V4L2_STD_PAL_M) {
877 pal_m = 1;
878 fmt = 0x5;
879 } else if (state->std == V4L2_STD_PAL_N) {
880 fmt = 0x6;
881 } else if (state->std == V4L2_STD_PAL_Nc) {
882 fmt = 0x7;
883 } else if (state->std == V4L2_STD_PAL_60) {
884 fmt = 0x8;
885 } else {
886 /* Then, test against generic ones */
887 if (state->std & V4L2_STD_NTSC)
888 fmt = 0x1;
889 else if (state->std & V4L2_STD_PAL)
890 fmt = 0x4;
891 else if (state->std & V4L2_STD_SECAM)
892 fmt = 0xc;
893 }
894
895 CX18_DEBUG_INFO_DEV(sd, "changing video std to fmt %i\n", fmt);
896
897 /* Follow step 9 of section 3.16 in the cx18_av datasheet.
898 Without this PAL may display a vertical ghosting effect.
899 This happens for example with the Yuan MPC622. */
900 if (fmt >= 4 && fmt < 8) {
901 /* Set format to NTSC-M */
902 cx18_av_and_or(cx, 0x400, ~0xf, 1);
903 /* Turn off LCOMB */
904 cx18_av_and_or(cx, 0x47b, ~6, 0);
905 }
906 cx18_av_and_or(cx, 0x400, ~0x2f, fmt | 0x20);
907 cx18_av_and_or(cx, 0x403, ~0x3, pal_m);
908 cx18_av_std_setup(cx);
909 input_change(cx);
910 return 0;
911}
912
913static int cx18_av_s_radio(struct v4l2_subdev *sd)
914{
915 struct cx18_av_state *state = to_cx18_av_state(sd);
916 state->radio = 1;
917 return 0;
918}
919
920static int cx18_av_s_ctrl(struct v4l2_ctrl *ctrl)
921{
922 struct v4l2_subdev *sd = to_sd(ctrl);
923 struct cx18 *cx = v4l2_get_subdevdata(sd);
924
925 switch (ctrl->id) {
926 case V4L2_CID_BRIGHTNESS:
927 cx18_av_write(cx, 0x414, ctrl->val - 128);
928 break;
929
930 case V4L2_CID_CONTRAST:
931 cx18_av_write(cx, 0x415, ctrl->val << 1);
932 break;
933
934 case V4L2_CID_SATURATION:
935 cx18_av_write(cx, 0x420, ctrl->val << 1);
936 cx18_av_write(cx, 0x421, ctrl->val << 1);
937 break;
938
939 case V4L2_CID_HUE:
940 cx18_av_write(cx, 0x422, ctrl->val);
941 break;
942
943 default:
944 return -EINVAL;
945 }
946 return 0;
947}
948
949static int cx18_av_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)
950{
951 struct cx18_av_state *state = to_cx18_av_state(sd);
952 struct cx18 *cx = v4l2_get_subdevdata(sd);
953 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
954 int is_50Hz = !(state->std & V4L2_STD_525_60);
955
956 if (fmt->code != V4L2_MBUS_FMT_FIXED)
957 return -EINVAL;
958
959 fmt->field = V4L2_FIELD_INTERLACED;
960 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
961
962 Vsrc = (cx18_av_read(cx, 0x476) & 0x3f) << 4;
963 Vsrc |= (cx18_av_read(cx, 0x475) & 0xf0) >> 4;
964
965 Hsrc = (cx18_av_read(cx, 0x472) & 0x3f) << 4;
966 Hsrc |= (cx18_av_read(cx, 0x471) & 0xf0) >> 4;
967
968 /*
969 * This adjustment reflects the excess of vactive, set in
970 * cx18_av_std_setup(), above standard values:
971 *
972 * 480 + 1 for 60 Hz systems
973 * 576 + 3 for 50 Hz systems
974 */
975 Vlines = fmt->height + (is_50Hz ? 3 : 1);
976
977 /*
978 * Invalid height and width scaling requests are:
979 * 1. width less than 1/16 of the source width
980 * 2. width greater than the source width
981 * 3. height less than 1/8 of the source height
982 * 4. height greater than the source height
983 */
984 if ((fmt->width * 16 < Hsrc) || (Hsrc < fmt->width) ||
985 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
986 CX18_ERR_DEV(sd, "%dx%d is not a valid size!\n",
987 fmt->width, fmt->height);
988 return -ERANGE;
989 }
990
991 HSC = (Hsrc * (1 << 20)) / fmt->width - (1 << 20);
992 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
993 VSC &= 0x1fff;
994
995 if (fmt->width >= 385)
996 filter = 0;
997 else if (fmt->width > 192)
998 filter = 1;
999 else if (fmt->width > 96)
1000 filter = 2;
1001 else
1002 filter = 3;
1003
1004 CX18_DEBUG_INFO_DEV(sd,
1005 "decoder set size %dx%d -> scale %ux%u\n",
1006 fmt->width, fmt->height, HSC, VSC);
1007
1008 /* HSCALE=HSC */
1009 cx18_av_write(cx, 0x418, HSC & 0xff);
1010 cx18_av_write(cx, 0x419, (HSC >> 8) & 0xff);
1011 cx18_av_write(cx, 0x41a, HSC >> 16);
1012 /* VSCALE=VSC */
1013 cx18_av_write(cx, 0x41c, VSC & 0xff);
1014 cx18_av_write(cx, 0x41d, VSC >> 8);
1015 /* VS_INTRLACE=1 VFILT=filter */
1016 cx18_av_write(cx, 0x41e, 0x8 | filter);
1017 return 0;
1018}
1019
1020static int cx18_av_s_stream(struct v4l2_subdev *sd, int enable)
1021{
1022 struct cx18 *cx = v4l2_get_subdevdata(sd);
1023
1024 CX18_DEBUG_INFO_DEV(sd, "%s output\n", enable ? "enable" : "disable");
1025 if (enable) {
1026 cx18_av_write(cx, 0x115, 0x8c);
1027 cx18_av_write(cx, 0x116, 0x07);
1028 } else {
1029 cx18_av_write(cx, 0x115, 0x00);
1030 cx18_av_write(cx, 0x116, 0x00);
1031 }
1032 return 0;
1033}
1034
1035static void log_video_status(struct cx18 *cx)
1036{
1037 static const char *const fmt_strs[] = {
1038 "0x0",
1039 "NTSC-M", "NTSC-J", "NTSC-4.43",
1040 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
1041 "0x9", "0xA", "0xB",
1042 "SECAM",
1043 "0xD", "0xE", "0xF"
1044 };
1045
1046 struct cx18_av_state *state = &cx->av_state;
1047 struct v4l2_subdev *sd = &state->sd;
1048 u8 vidfmt_sel = cx18_av_read(cx, 0x400) & 0xf;
1049 u8 gen_stat1 = cx18_av_read(cx, 0x40d);
1050 u8 gen_stat2 = cx18_av_read(cx, 0x40e);
1051 int vid_input = state->vid_input;
1052
1053 CX18_INFO_DEV(sd, "Video signal: %spresent\n",
1054 (gen_stat2 & 0x20) ? "" : "not ");
1055 CX18_INFO_DEV(sd, "Detected format: %s\n",
1056 fmt_strs[gen_stat1 & 0xf]);
1057
1058 CX18_INFO_DEV(sd, "Specified standard: %s\n",
1059 vidfmt_sel ? fmt_strs[vidfmt_sel]
1060 : "automatic detection");
1061
1062 if (vid_input >= CX18_AV_COMPOSITE1 &&
1063 vid_input <= CX18_AV_COMPOSITE8) {
1064 CX18_INFO_DEV(sd, "Specified video input: Composite %d\n",
1065 vid_input - CX18_AV_COMPOSITE1 + 1);
1066 } else {
1067 CX18_INFO_DEV(sd, "Specified video input: "
1068 "S-Video (Luma In%d, Chroma In%d)\n",
1069 (vid_input & 0xf0) >> 4,
1070 (vid_input & 0xf00) >> 8);
1071 }
1072
1073 CX18_INFO_DEV(sd, "Specified audioclock freq: %d Hz\n",
1074 state->audclk_freq);
1075}
1076
1077static void log_audio_status(struct cx18 *cx)
1078{
1079 struct cx18_av_state *state = &cx->av_state;
1080 struct v4l2_subdev *sd = &state->sd;
1081 u8 download_ctl = cx18_av_read(cx, 0x803);
1082 u8 mod_det_stat0 = cx18_av_read(cx, 0x804);
1083 u8 mod_det_stat1 = cx18_av_read(cx, 0x805);
1084 u8 audio_config = cx18_av_read(cx, 0x808);
1085 u8 pref_mode = cx18_av_read(cx, 0x809);
1086 u8 afc0 = cx18_av_read(cx, 0x80b);
1087 u8 mute_ctl = cx18_av_read(cx, 0x8d3);
1088 int aud_input = state->aud_input;
1089 char *p;
1090
1091 switch (mod_det_stat0) {
1092 case 0x00: p = "mono"; break;
1093 case 0x01: p = "stereo"; break;
1094 case 0x02: p = "dual"; break;
1095 case 0x04: p = "tri"; break;
1096 case 0x10: p = "mono with SAP"; break;
1097 case 0x11: p = "stereo with SAP"; break;
1098 case 0x12: p = "dual with SAP"; break;
1099 case 0x14: p = "tri with SAP"; break;
1100 case 0xfe: p = "forced mode"; break;
1101 default: p = "not defined"; break;
1102 }
1103 CX18_INFO_DEV(sd, "Detected audio mode: %s\n", p);
1104
1105 switch (mod_det_stat1) {
1106 case 0x00: p = "not defined"; break;
1107 case 0x01: p = "EIAJ"; break;
1108 case 0x02: p = "A2-M"; break;
1109 case 0x03: p = "A2-BG"; break;
1110 case 0x04: p = "A2-DK1"; break;
1111 case 0x05: p = "A2-DK2"; break;
1112 case 0x06: p = "A2-DK3"; break;
1113 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1114 case 0x08: p = "AM-L"; break;
1115 case 0x09: p = "NICAM-BG"; break;
1116 case 0x0a: p = "NICAM-DK"; break;
1117 case 0x0b: p = "NICAM-I"; break;
1118 case 0x0c: p = "NICAM-L"; break;
1119 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1120 case 0x0e: p = "IF FM Radio"; break;
1121 case 0x0f: p = "BTSC"; break;
1122 case 0x10: p = "detected chrominance"; break;
1123 case 0xfd: p = "unknown audio standard"; break;
1124 case 0xfe: p = "forced audio standard"; break;
1125 case 0xff: p = "no detected audio standard"; break;
1126 default: p = "not defined"; break;
1127 }
1128 CX18_INFO_DEV(sd, "Detected audio standard: %s\n", p);
1129 CX18_INFO_DEV(sd, "Audio muted: %s\n",
1130 (mute_ctl & 0x2) ? "yes" : "no");
1131 CX18_INFO_DEV(sd, "Audio microcontroller: %s\n",
1132 (download_ctl & 0x10) ? "running" : "stopped");
1133
1134 switch (audio_config >> 4) {
1135 case 0x00: p = "undefined"; break;
1136 case 0x01: p = "BTSC"; break;
1137 case 0x02: p = "EIAJ"; break;
1138 case 0x03: p = "A2-M"; break;
1139 case 0x04: p = "A2-BG"; break;
1140 case 0x05: p = "A2-DK1"; break;
1141 case 0x06: p = "A2-DK2"; break;
1142 case 0x07: p = "A2-DK3"; break;
1143 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1144 case 0x09: p = "AM-L"; break;
1145 case 0x0a: p = "NICAM-BG"; break;
1146 case 0x0b: p = "NICAM-DK"; break;
1147 case 0x0c: p = "NICAM-I"; break;
1148 case 0x0d: p = "NICAM-L"; break;
1149 case 0x0e: p = "FM radio"; break;
1150 case 0x0f: p = "automatic detection"; break;
1151 default: p = "undefined"; break;
1152 }
1153 CX18_INFO_DEV(sd, "Configured audio standard: %s\n", p);
1154
1155 if ((audio_config >> 4) < 0xF) {
1156 switch (audio_config & 0xF) {
1157 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1158 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1159 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1160 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1161 case 0x04: p = "STEREO"; break;
1162 case 0x05: p = "DUAL1 (AC)"; break;
1163 case 0x06: p = "DUAL2 (BC)"; break;
1164 case 0x07: p = "DUAL3 (AB)"; break;
1165 default: p = "undefined";
1166 }
1167 CX18_INFO_DEV(sd, "Configured audio mode: %s\n", p);
1168 } else {
1169 switch (audio_config & 0xF) {
1170 case 0x00: p = "BG"; break;
1171 case 0x01: p = "DK1"; break;
1172 case 0x02: p = "DK2"; break;
1173 case 0x03: p = "DK3"; break;
1174 case 0x04: p = "I"; break;
1175 case 0x05: p = "L"; break;
1176 case 0x06: p = "BTSC"; break;
1177 case 0x07: p = "EIAJ"; break;
1178 case 0x08: p = "A2-M"; break;
1179 case 0x09: p = "FM Radio (4.5 MHz)"; break;
1180 case 0x0a: p = "FM Radio (5.5 MHz)"; break;
1181 case 0x0b: p = "S-Video"; break;
1182 case 0x0f: p = "automatic standard and mode detection"; break;
1183 default: p = "undefined"; break;
1184 }
1185 CX18_INFO_DEV(sd, "Configured audio system: %s\n", p);
1186 }
1187
1188 if (aud_input)
1189 CX18_INFO_DEV(sd, "Specified audio input: Tuner (In%d)\n",
1190 aud_input);
1191 else
1192 CX18_INFO_DEV(sd, "Specified audio input: External\n");
1193
1194 switch (pref_mode & 0xf) {
1195 case 0: p = "mono/language A"; break;
1196 case 1: p = "language B"; break;
1197 case 2: p = "language C"; break;
1198 case 3: p = "analog fallback"; break;
1199 case 4: p = "stereo"; break;
1200 case 5: p = "language AC"; break;
1201 case 6: p = "language BC"; break;
1202 case 7: p = "language AB"; break;
1203 default: p = "undefined"; break;
1204 }
1205 CX18_INFO_DEV(sd, "Preferred audio mode: %s\n", p);
1206
1207 if ((audio_config & 0xf) == 0xf) {
1208 switch ((afc0 >> 3) & 0x1) {
1209 case 0: p = "system DK"; break;
1210 case 1: p = "system L"; break;
1211 }
1212 CX18_INFO_DEV(sd, "Selected 65 MHz format: %s\n", p);
1213
1214 switch (afc0 & 0x7) {
1215 case 0: p = "Chroma"; break;
1216 case 1: p = "BTSC"; break;
1217 case 2: p = "EIAJ"; break;
1218 case 3: p = "A2-M"; break;
1219 case 4: p = "autodetect"; break;
1220 default: p = "undefined"; break;
1221 }
1222 CX18_INFO_DEV(sd, "Selected 45 MHz format: %s\n", p);
1223 }
1224}
1225
1226static int cx18_av_log_status(struct v4l2_subdev *sd)
1227{
1228 struct cx18 *cx = v4l2_get_subdevdata(sd);
1229 log_video_status(cx);
1230 log_audio_status(cx);
1231 return 0;
1232}
1233
1234static inline int cx18_av_dbg_match(const struct v4l2_dbg_match *match)
1235{
1236 return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 1;
1237}
1238
1239static int cx18_av_g_chip_ident(struct v4l2_subdev *sd,
1240 struct v4l2_dbg_chip_ident *chip)
1241{
1242 struct cx18_av_state *state = to_cx18_av_state(sd);
1243
1244 if (cx18_av_dbg_match(&chip->match)) {
1245 chip->ident = state->id;
1246 chip->revision = state->rev;
1247 }
1248 return 0;
1249}
1250
1251#ifdef CONFIG_VIDEO_ADV_DEBUG
1252static int cx18_av_g_register(struct v4l2_subdev *sd,
1253 struct v4l2_dbg_register *reg)
1254{
1255 struct cx18 *cx = v4l2_get_subdevdata(sd);
1256
1257 if (!cx18_av_dbg_match(&reg->match))
1258 return -EINVAL;
1259 if ((reg->reg & 0x3) != 0)
1260 return -EINVAL;
1261 if (!capable(CAP_SYS_ADMIN))
1262 return -EPERM;
1263 reg->size = 4;
1264 reg->val = cx18_av_read4(cx, reg->reg & 0x00000ffc);
1265 return 0;
1266}
1267
1268static int cx18_av_s_register(struct v4l2_subdev *sd,
1269 struct v4l2_dbg_register *reg)
1270{
1271 struct cx18 *cx = v4l2_get_subdevdata(sd);
1272
1273 if (!cx18_av_dbg_match(&reg->match))
1274 return -EINVAL;
1275 if ((reg->reg & 0x3) != 0)
1276 return -EINVAL;
1277 if (!capable(CAP_SYS_ADMIN))
1278 return -EPERM;
1279 cx18_av_write4(cx, reg->reg & 0x00000ffc, reg->val);
1280 return 0;
1281}
1282#endif
1283
1284static const struct v4l2_ctrl_ops cx18_av_ctrl_ops = {
1285 .s_ctrl = cx18_av_s_ctrl,
1286};
1287
1288static const struct v4l2_subdev_core_ops cx18_av_general_ops = {
1289 .g_chip_ident = cx18_av_g_chip_ident,
1290 .log_status = cx18_av_log_status,
1291 .load_fw = cx18_av_load_fw,
1292 .reset = cx18_av_reset,
1293 .g_ctrl = v4l2_subdev_g_ctrl,
1294 .s_ctrl = v4l2_subdev_s_ctrl,
1295 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1296 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1297 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1298 .queryctrl = v4l2_subdev_queryctrl,
1299 .querymenu = v4l2_subdev_querymenu,
1300 .s_std = cx18_av_s_std,
1301#ifdef CONFIG_VIDEO_ADV_DEBUG
1302 .g_register = cx18_av_g_register,
1303 .s_register = cx18_av_s_register,
1304#endif
1305};
1306
1307static const struct v4l2_subdev_tuner_ops cx18_av_tuner_ops = {
1308 .s_radio = cx18_av_s_radio,
1309 .s_frequency = cx18_av_s_frequency,
1310 .g_tuner = cx18_av_g_tuner,
1311 .s_tuner = cx18_av_s_tuner,
1312};
1313
1314static const struct v4l2_subdev_audio_ops cx18_av_audio_ops = {
1315 .s_clock_freq = cx18_av_s_clock_freq,
1316 .s_routing = cx18_av_s_audio_routing,
1317};
1318
1319static const struct v4l2_subdev_video_ops cx18_av_video_ops = {
1320 .s_routing = cx18_av_s_video_routing,
1321 .s_stream = cx18_av_s_stream,
1322 .s_mbus_fmt = cx18_av_s_mbus_fmt,
1323};
1324
1325static const struct v4l2_subdev_vbi_ops cx18_av_vbi_ops = {
1326 .decode_vbi_line = cx18_av_decode_vbi_line,
1327 .g_sliced_fmt = cx18_av_g_sliced_fmt,
1328 .s_sliced_fmt = cx18_av_s_sliced_fmt,
1329 .s_raw_fmt = cx18_av_s_raw_fmt,
1330};
1331
1332static const struct v4l2_subdev_ops cx18_av_ops = {
1333 .core = &cx18_av_general_ops,
1334 .tuner = &cx18_av_tuner_ops,
1335 .audio = &cx18_av_audio_ops,
1336 .video = &cx18_av_video_ops,
1337 .vbi = &cx18_av_vbi_ops,
1338};
1339
1340int cx18_av_probe(struct cx18 *cx)
1341{
1342 struct cx18_av_state *state = &cx->av_state;
1343 struct v4l2_subdev *sd;
1344 int err;
1345
1346 state->rev = cx18_av_read4(cx, CXADEC_CHIP_CTRL) & 0xffff;
1347 state->id = ((state->rev >> 4) == CXADEC_CHIP_TYPE_MAKO)
1348 ? V4L2_IDENT_CX23418_843 : V4L2_IDENT_UNKNOWN;
1349
1350 state->vid_input = CX18_AV_COMPOSITE7;
1351 state->aud_input = CX18_AV_AUDIO8;
1352 state->audclk_freq = 48000;
1353 state->audmode = V4L2_TUNER_MODE_LANG1;
1354 state->slicer_line_delay = 0;
1355 state->slicer_line_offset = (10 + state->slicer_line_delay - 2);
1356
1357 sd = &state->sd;
1358 v4l2_subdev_init(sd, &cx18_av_ops);
1359 v4l2_set_subdevdata(sd, cx);
1360 snprintf(sd->name, sizeof(sd->name),
1361 "%s %03x", cx->v4l2_dev.name, (state->rev >> 4));
1362 sd->grp_id = CX18_HW_418_AV;
1363 v4l2_ctrl_handler_init(&state->hdl, 9);
1364 v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,
1365 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1366 v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,
1367 V4L2_CID_CONTRAST, 0, 127, 1, 64);
1368 v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,
1369 V4L2_CID_SATURATION, 0, 127, 1, 64);
1370 v4l2_ctrl_new_std(&state->hdl, &cx18_av_ctrl_ops,
1371 V4L2_CID_HUE, -128, 127, 1, 0);
1372
1373 state->volume = v4l2_ctrl_new_std(&state->hdl,
1374 &cx18_av_audio_ctrl_ops, V4L2_CID_AUDIO_VOLUME,
1375 0, 65535, 65535 / 100, 0);
1376 v4l2_ctrl_new_std(&state->hdl,
1377 &cx18_av_audio_ctrl_ops, V4L2_CID_AUDIO_MUTE,
1378 0, 1, 1, 0);
1379 v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,
1380 V4L2_CID_AUDIO_BALANCE,
1381 0, 65535, 65535 / 100, 32768);
1382 v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,
1383 V4L2_CID_AUDIO_BASS,
1384 0, 65535, 65535 / 100, 32768);
1385 v4l2_ctrl_new_std(&state->hdl, &cx18_av_audio_ctrl_ops,
1386 V4L2_CID_AUDIO_TREBLE,
1387 0, 65535, 65535 / 100, 32768);
1388 sd->ctrl_handler = &state->hdl;
1389 if (state->hdl.error) {
1390 int err = state->hdl.error;
1391
1392 v4l2_ctrl_handler_free(&state->hdl);
1393 return err;
1394 }
1395 err = v4l2_device_register_subdev(&cx->v4l2_dev, sd);
1396 if (err)
1397 v4l2_ctrl_handler_free(&state->hdl);
1398 else
1399 cx18_av_init(cx);
1400 return err;
1401}
diff --git a/drivers/media/pci/cx18/cx18-av-core.h b/drivers/media/pci/cx18/cx18-av-core.h
new file mode 100644
index 000000000000..e9c69d9c9e4a
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-av-core.h
@@ -0,0 +1,391 @@
1/*
2 * cx18 ADEC header
3 *
4 * Derived from cx25840-core.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 */
24
25#ifndef _CX18_AV_CORE_H_
26#define _CX18_AV_CORE_H_
27
28#include <media/v4l2-device.h>
29#include <media/v4l2-ctrls.h>
30
31struct cx18;
32
33enum cx18_av_video_input {
34 /* Composite video inputs In1-In8 */
35 CX18_AV_COMPOSITE1 = 1,
36 CX18_AV_COMPOSITE2,
37 CX18_AV_COMPOSITE3,
38 CX18_AV_COMPOSITE4,
39 CX18_AV_COMPOSITE5,
40 CX18_AV_COMPOSITE6,
41 CX18_AV_COMPOSITE7,
42 CX18_AV_COMPOSITE8,
43
44 /* S-Video inputs consist of one luma input (In1-In8) ORed with one
45 chroma input (In5-In8) */
46 CX18_AV_SVIDEO_LUMA1 = 0x10,
47 CX18_AV_SVIDEO_LUMA2 = 0x20,
48 CX18_AV_SVIDEO_LUMA3 = 0x30,
49 CX18_AV_SVIDEO_LUMA4 = 0x40,
50 CX18_AV_SVIDEO_LUMA5 = 0x50,
51 CX18_AV_SVIDEO_LUMA6 = 0x60,
52 CX18_AV_SVIDEO_LUMA7 = 0x70,
53 CX18_AV_SVIDEO_LUMA8 = 0x80,
54 CX18_AV_SVIDEO_CHROMA4 = 0x400,
55 CX18_AV_SVIDEO_CHROMA5 = 0x500,
56 CX18_AV_SVIDEO_CHROMA6 = 0x600,
57 CX18_AV_SVIDEO_CHROMA7 = 0x700,
58 CX18_AV_SVIDEO_CHROMA8 = 0x800,
59
60 /* S-Video aliases for common luma/chroma combinations */
61 CX18_AV_SVIDEO1 = 0x510,
62 CX18_AV_SVIDEO2 = 0x620,
63 CX18_AV_SVIDEO3 = 0x730,
64 CX18_AV_SVIDEO4 = 0x840,
65
66 /* Component Video inputs consist of one luma input (In1-In8) ORed
67 with a red chroma (In4-In6) and blue chroma input (In7-In8) */
68 CX18_AV_COMPONENT_LUMA1 = 0x1000,
69 CX18_AV_COMPONENT_LUMA2 = 0x2000,
70 CX18_AV_COMPONENT_LUMA3 = 0x3000,
71 CX18_AV_COMPONENT_LUMA4 = 0x4000,
72 CX18_AV_COMPONENT_LUMA5 = 0x5000,
73 CX18_AV_COMPONENT_LUMA6 = 0x6000,
74 CX18_AV_COMPONENT_LUMA7 = 0x7000,
75 CX18_AV_COMPONENT_LUMA8 = 0x8000,
76 CX18_AV_COMPONENT_R_CHROMA4 = 0x40000,
77 CX18_AV_COMPONENT_R_CHROMA5 = 0x50000,
78 CX18_AV_COMPONENT_R_CHROMA6 = 0x60000,
79 CX18_AV_COMPONENT_B_CHROMA7 = 0x700000,
80 CX18_AV_COMPONENT_B_CHROMA8 = 0x800000,
81
82 /* Component Video aliases for common combinations */
83 CX18_AV_COMPONENT1 = 0x861000,
84};
85
86enum cx18_av_audio_input {
87 /* Audio inputs: serial or In4-In8 */
88 CX18_AV_AUDIO_SERIAL1,
89 CX18_AV_AUDIO_SERIAL2,
90 CX18_AV_AUDIO4 = 4,
91 CX18_AV_AUDIO5,
92 CX18_AV_AUDIO6,
93 CX18_AV_AUDIO7,
94 CX18_AV_AUDIO8,
95};
96
97struct cx18_av_state {
98 struct v4l2_subdev sd;
99 struct v4l2_ctrl_handler hdl;
100 struct v4l2_ctrl *volume;
101 int radio;
102 v4l2_std_id std;
103 enum cx18_av_video_input vid_input;
104 enum cx18_av_audio_input aud_input;
105 u32 audclk_freq;
106 int audmode;
107 u32 id;
108 u32 rev;
109 int is_initialized;
110
111 /*
112 * The VBI slicer starts operating and counting lines, beginning at
113 * slicer line count of 1, at D lines after the deassertion of VRESET.
114 * This staring field line, S, is 6 (& 319) or 10 (& 273) for 625 or 525
115 * line systems respectively. Sliced ancillary data captured on VBI
116 * slicer line M is inserted after the VBI slicer is done with line M,
117 * when VBI slicer line count is N = M+1. Thus when the VBI slicer
118 * reports a VBI slicer line number with ancillary data, the IDID0 byte
119 * indicates VBI slicer line N. The actual field line that the captured
120 * data comes from is
121 *
122 * L = M+(S+D-1) = N-1+(S+D-1) = N + (S+D-2).
123 *
124 * L is the line in the field, not frame, from which the VBI data came.
125 * N is the line reported by the slicer in the ancillary data.
126 * D is the slicer_line_delay value programmed into register 0x47f.
127 * S is 6 for 625 line systems or 10 for 525 line systems
128 * (S+D-2) is the slicer_line_offset used to convert slicer reported
129 * line counts to actual field lines.
130 */
131 int slicer_line_delay;
132 int slicer_line_offset;
133};
134
135
136/* Registers */
137#define CXADEC_CHIP_TYPE_TIGER 0x837
138#define CXADEC_CHIP_TYPE_MAKO 0x843
139
140#define CXADEC_HOST_REG1 0x000
141#define CXADEC_HOST_REG2 0x001
142
143#define CXADEC_CHIP_CTRL 0x100
144#define CXADEC_AFE_CTRL 0x104
145#define CXADEC_PLL_CTRL1 0x108
146#define CXADEC_VID_PLL_FRAC 0x10C
147#define CXADEC_AUX_PLL_FRAC 0x110
148#define CXADEC_PIN_CTRL1 0x114
149#define CXADEC_PIN_CTRL2 0x118
150#define CXADEC_PIN_CFG1 0x11C
151#define CXADEC_PIN_CFG2 0x120
152
153#define CXADEC_PIN_CFG3 0x124
154#define CXADEC_I2S_MCLK 0x127
155
156#define CXADEC_AUD_LOCK1 0x128
157#define CXADEC_AUD_LOCK2 0x12C
158#define CXADEC_POWER_CTRL 0x130
159#define CXADEC_AFE_DIAG_CTRL1 0x134
160#define CXADEC_AFE_DIAG_CTRL2 0x138
161#define CXADEC_AFE_DIAG_CTRL3 0x13C
162#define CXADEC_PLL_DIAG_CTRL 0x140
163#define CXADEC_TEST_CTRL1 0x144
164#define CXADEC_TEST_CTRL2 0x148
165#define CXADEC_BIST_STAT 0x14C
166#define CXADEC_DLL1_DIAG_CTRL 0x158
167#define CXADEC_DLL2_DIAG_CTRL 0x15C
168
169/* IR registers */
170#define CXADEC_IR_CTRL_REG 0x200
171#define CXADEC_IR_TXCLK_REG 0x204
172#define CXADEC_IR_RXCLK_REG 0x208
173#define CXADEC_IR_CDUTY_REG 0x20C
174#define CXADEC_IR_STAT_REG 0x210
175#define CXADEC_IR_IRQEN_REG 0x214
176#define CXADEC_IR_FILTER_REG 0x218
177#define CXADEC_IR_FIFO_REG 0x21C
178
179/* Video Registers */
180#define CXADEC_MODE_CTRL 0x400
181#define CXADEC_OUT_CTRL1 0x404
182#define CXADEC_OUT_CTRL2 0x408
183#define CXADEC_GEN_STAT 0x40C
184#define CXADEC_INT_STAT_MASK 0x410
185#define CXADEC_LUMA_CTRL 0x414
186
187#define CXADEC_BRIGHTNESS_CTRL_BYTE 0x414
188#define CXADEC_CONTRAST_CTRL_BYTE 0x415
189#define CXADEC_LUMA_CTRL_BYTE_3 0x416
190
191#define CXADEC_HSCALE_CTRL 0x418
192#define CXADEC_VSCALE_CTRL 0x41C
193
194#define CXADEC_CHROMA_CTRL 0x420
195
196#define CXADEC_USAT_CTRL_BYTE 0x420
197#define CXADEC_VSAT_CTRL_BYTE 0x421
198#define CXADEC_HUE_CTRL_BYTE 0x422
199
200#define CXADEC_VBI_LINE_CTRL1 0x424
201#define CXADEC_VBI_LINE_CTRL2 0x428
202#define CXADEC_VBI_LINE_CTRL3 0x42C
203#define CXADEC_VBI_LINE_CTRL4 0x430
204#define CXADEC_VBI_LINE_CTRL5 0x434
205#define CXADEC_VBI_FC_CFG 0x438
206#define CXADEC_VBI_MISC_CFG1 0x43C
207#define CXADEC_VBI_MISC_CFG2 0x440
208#define CXADEC_VBI_PAY1 0x444
209#define CXADEC_VBI_PAY2 0x448
210#define CXADEC_VBI_CUST1_CFG1 0x44C
211#define CXADEC_VBI_CUST1_CFG2 0x450
212#define CXADEC_VBI_CUST1_CFG3 0x454
213#define CXADEC_VBI_CUST2_CFG1 0x458
214#define CXADEC_VBI_CUST2_CFG2 0x45C
215#define CXADEC_VBI_CUST2_CFG3 0x460
216#define CXADEC_VBI_CUST3_CFG1 0x464
217#define CXADEC_VBI_CUST3_CFG2 0x468
218#define CXADEC_VBI_CUST3_CFG3 0x46C
219#define CXADEC_HORIZ_TIM_CTRL 0x470
220#define CXADEC_VERT_TIM_CTRL 0x474
221#define CXADEC_SRC_COMB_CFG 0x478
222#define CXADEC_CHROMA_VBIOFF_CFG 0x47C
223#define CXADEC_FIELD_COUNT 0x480
224#define CXADEC_MISC_TIM_CTRL 0x484
225#define CXADEC_DFE_CTRL1 0x488
226#define CXADEC_DFE_CTRL2 0x48C
227#define CXADEC_DFE_CTRL3 0x490
228#define CXADEC_PLL_CTRL2 0x494
229#define CXADEC_HTL_CTRL 0x498
230#define CXADEC_COMB_CTRL 0x49C
231#define CXADEC_CRUSH_CTRL 0x4A0
232#define CXADEC_SOFT_RST_CTRL 0x4A4
233#define CXADEC_MV_DT_CTRL2 0x4A8
234#define CXADEC_MV_DT_CTRL3 0x4AC
235#define CXADEC_MISC_DIAG_CTRL 0x4B8
236
237#define CXADEC_DL_CTL 0x800
238#define CXADEC_DL_CTL_ADDRESS_LOW 0x800 /* Byte 1 in DL_CTL */
239#define CXADEC_DL_CTL_ADDRESS_HIGH 0x801 /* Byte 2 in DL_CTL */
240#define CXADEC_DL_CTL_DATA 0x802 /* Byte 3 in DL_CTL */
241#define CXADEC_DL_CTL_CONTROL 0x803 /* Byte 4 in DL_CTL */
242
243#define CXADEC_STD_DET_STATUS 0x804
244
245#define CXADEC_STD_DET_CTL 0x808
246#define CXADEC_STD_DET_CTL_AUD_CTL 0x808 /* Byte 1 in STD_DET_CTL */
247#define CXADEC_STD_DET_CTL_PREF_MODE 0x809 /* Byte 2 in STD_DET_CTL */
248
249#define CXADEC_DW8051_INT 0x80C
250#define CXADEC_GENERAL_CTL 0x810
251#define CXADEC_AAGC_CTL 0x814
252#define CXADEC_IF_SRC_CTL 0x818
253#define CXADEC_ANLOG_DEMOD_CTL 0x81C
254#define CXADEC_ROT_FREQ_CTL 0x820
255#define CXADEC_FM1_CTL 0x824
256#define CXADEC_PDF_CTL 0x828
257#define CXADEC_DFT1_CTL1 0x82C
258#define CXADEC_DFT1_CTL2 0x830
259#define CXADEC_DFT_STATUS 0x834
260#define CXADEC_DFT2_CTL1 0x838
261#define CXADEC_DFT2_CTL2 0x83C
262#define CXADEC_DFT2_STATUS 0x840
263#define CXADEC_DFT3_CTL1 0x844
264#define CXADEC_DFT3_CTL2 0x848
265#define CXADEC_DFT3_STATUS 0x84C
266#define CXADEC_DFT4_CTL1 0x850
267#define CXADEC_DFT4_CTL2 0x854
268#define CXADEC_DFT4_STATUS 0x858
269#define CXADEC_AM_MTS_DET 0x85C
270#define CXADEC_ANALOG_MUX_CTL 0x860
271#define CXADEC_DIG_PLL_CTL1 0x864
272#define CXADEC_DIG_PLL_CTL2 0x868
273#define CXADEC_DIG_PLL_CTL3 0x86C
274#define CXADEC_DIG_PLL_CTL4 0x870
275#define CXADEC_DIG_PLL_CTL5 0x874
276#define CXADEC_DEEMPH_GAIN_CTL 0x878
277#define CXADEC_DEEMPH_COEF1 0x87C
278#define CXADEC_DEEMPH_COEF2 0x880
279#define CXADEC_DBX1_CTL1 0x884
280#define CXADEC_DBX1_CTL2 0x888
281#define CXADEC_DBX1_STATUS 0x88C
282#define CXADEC_DBX2_CTL1 0x890
283#define CXADEC_DBX2_CTL2 0x894
284#define CXADEC_DBX2_STATUS 0x898
285#define CXADEC_AM_FM_DIFF 0x89C
286
287/* NICAM registers go here */
288#define CXADEC_NICAM_STATUS 0x8C8
289#define CXADEC_DEMATRIX_CTL 0x8CC
290
291#define CXADEC_PATH1_CTL1 0x8D0
292#define CXADEC_PATH1_VOL_CTL 0x8D4
293#define CXADEC_PATH1_EQ_CTL 0x8D8
294#define CXADEC_PATH1_SC_CTL 0x8DC
295
296#define CXADEC_PATH2_CTL1 0x8E0
297#define CXADEC_PATH2_VOL_CTL 0x8E4
298#define CXADEC_PATH2_EQ_CTL 0x8E8
299#define CXADEC_PATH2_SC_CTL 0x8EC
300
301#define CXADEC_SRC_CTL 0x8F0
302#define CXADEC_SRC_LF_COEF 0x8F4
303#define CXADEC_SRC1_CTL 0x8F8
304#define CXADEC_SRC2_CTL 0x8FC
305#define CXADEC_SRC3_CTL 0x900
306#define CXADEC_SRC4_CTL 0x904
307#define CXADEC_SRC5_CTL 0x908
308#define CXADEC_SRC6_CTL 0x90C
309
310#define CXADEC_BASEBAND_OUT_SEL 0x910
311#define CXADEC_I2S_IN_CTL 0x914
312#define CXADEC_I2S_OUT_CTL 0x918
313#define CXADEC_AC97_CTL 0x91C
314#define CXADEC_QAM_PDF 0x920
315#define CXADEC_QAM_CONST_DEC 0x924
316#define CXADEC_QAM_ROTATOR_FREQ 0x948
317
318/* Bit definitions / settings used in Mako Audio */
319#define CXADEC_PREF_MODE_MONO_LANGA 0
320#define CXADEC_PREF_MODE_MONO_LANGB 1
321#define CXADEC_PREF_MODE_MONO_LANGC 2
322#define CXADEC_PREF_MODE_FALLBACK 3
323#define CXADEC_PREF_MODE_STEREO 4
324#define CXADEC_PREF_MODE_DUAL_LANG_AC 5
325#define CXADEC_PREF_MODE_DUAL_LANG_BC 6
326#define CXADEC_PREF_MODE_DUAL_LANG_AB 7
327
328
329#define CXADEC_DETECT_STEREO 1
330#define CXADEC_DETECT_DUAL 2
331#define CXADEC_DETECT_TRI 4
332#define CXADEC_DETECT_SAP 0x10
333#define CXADEC_DETECT_NO_SIGNAL 0xFF
334
335#define CXADEC_SELECT_AUDIO_STANDARD_BG 0xF0 /* NICAM BG and A2 BG */
336#define CXADEC_SELECT_AUDIO_STANDARD_DK1 0xF1 /* NICAM DK and A2 DK */
337#define CXADEC_SELECT_AUDIO_STANDARD_DK2 0xF2
338#define CXADEC_SELECT_AUDIO_STANDARD_DK3 0xF3
339#define CXADEC_SELECT_AUDIO_STANDARD_I 0xF4 /* NICAM I and A1 */
340#define CXADEC_SELECT_AUDIO_STANDARD_L 0xF5 /* NICAM L and System L AM */
341#define CXADEC_SELECT_AUDIO_STANDARD_BTSC 0xF6
342#define CXADEC_SELECT_AUDIO_STANDARD_EIAJ 0xF7
343#define CXADEC_SELECT_AUDIO_STANDARD_A2_M 0xF8 /* A2 M */
344#define CXADEC_SELECT_AUDIO_STANDARD_FM 0xF9 /* FM radio */
345#define CXADEC_SELECT_AUDIO_STANDARD_AUTO 0xFF /* Auto detect */
346
347static inline struct cx18_av_state *to_cx18_av_state(struct v4l2_subdev *sd)
348{
349 return container_of(sd, struct cx18_av_state, sd);
350}
351
352static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
353{
354 return &container_of(ctrl->handler, struct cx18_av_state, hdl)->sd;
355}
356
357/* ----------------------------------------------------------------------- */
358/* cx18_av-core.c */
359int cx18_av_write(struct cx18 *cx, u16 addr, u8 value);
360int cx18_av_write4(struct cx18 *cx, u16 addr, u32 value);
361int cx18_av_write4_noretry(struct cx18 *cx, u16 addr, u32 value);
362int cx18_av_write_expect(struct cx18 *cx, u16 addr, u8 value, u8 eval, u8 mask);
363int cx18_av_write4_expect(struct cx18 *cx, u16 addr, u32 value, u32 eval,
364 u32 mask);
365u8 cx18_av_read(struct cx18 *cx, u16 addr);
366u32 cx18_av_read4(struct cx18 *cx, u16 addr);
367int cx18_av_and_or(struct cx18 *cx, u16 addr, unsigned mask, u8 value);
368int cx18_av_and_or4(struct cx18 *cx, u16 addr, u32 mask, u32 value);
369void cx18_av_std_setup(struct cx18 *cx);
370
371int cx18_av_probe(struct cx18 *cx);
372
373/* ----------------------------------------------------------------------- */
374/* cx18_av-firmware.c */
375int cx18_av_loadfw(struct cx18 *cx);
376
377/* ----------------------------------------------------------------------- */
378/* cx18_av-audio.c */
379int cx18_av_s_clock_freq(struct v4l2_subdev *sd, u32 freq);
380void cx18_av_audio_set_path(struct cx18 *cx);
381extern const struct v4l2_ctrl_ops cx18_av_audio_ctrl_ops;
382
383/* ----------------------------------------------------------------------- */
384/* cx18_av-vbi.c */
385int cx18_av_decode_vbi_line(struct v4l2_subdev *sd,
386 struct v4l2_decode_vbi_line *vbi);
387int cx18_av_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt);
388int cx18_av_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
389int cx18_av_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
390
391#endif
diff --git a/drivers/media/pci/cx18/cx18-av-firmware.c b/drivers/media/pci/cx18/cx18-av-firmware.c
new file mode 100644
index 000000000000..a34fd082b76e
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-av-firmware.c
@@ -0,0 +1,225 @@
1/*
2 * cx18 ADEC firmware functions
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 */
22
23#include "cx18-driver.h"
24#include "cx18-io.h"
25#include <linux/firmware.h>
26
27#define CX18_AUDIO_ENABLE 0xc72014
28#define CX18_AI1_MUX_MASK 0x30
29#define CX18_AI1_MUX_I2S1 0x00
30#define CX18_AI1_MUX_I2S2 0x10
31#define CX18_AI1_MUX_843_I2S 0x20
32#define CX18_AI1_MUX_INVALID 0x30
33
34#define FWFILE "v4l-cx23418-dig.fw"
35
36static int cx18_av_verifyfw(struct cx18 *cx, const struct firmware *fw)
37{
38 struct v4l2_subdev *sd = &cx->av_state.sd;
39 int ret = 0;
40 const u8 *data;
41 u32 size;
42 int addr;
43 u32 expected, dl_control;
44
45 /* Ensure we put the 8051 in reset and enable firmware upload mode */
46 dl_control = cx18_av_read4(cx, CXADEC_DL_CTL);
47 do {
48 dl_control &= 0x00ffffff;
49 dl_control |= 0x0f000000;
50 cx18_av_write4_noretry(cx, CXADEC_DL_CTL, dl_control);
51 dl_control = cx18_av_read4(cx, CXADEC_DL_CTL);
52 } while ((dl_control & 0xff000000) != 0x0f000000);
53
54 /* Read and auto increment until at address 0x0000 */
55 while (dl_control & 0x3fff)
56 dl_control = cx18_av_read4(cx, CXADEC_DL_CTL);
57
58 data = fw->data;
59 size = fw->size;
60 for (addr = 0; addr < size; addr++) {
61 dl_control &= 0xffff3fff; /* ignore top 2 bits of address */
62 expected = 0x0f000000 | ((u32)data[addr] << 16) | addr;
63 if (expected != dl_control) {
64 CX18_ERR_DEV(sd, "verification of %s firmware load "
65 "failed: expected %#010x got %#010x\n",
66 FWFILE, expected, dl_control);
67 ret = -EIO;
68 break;
69 }
70 dl_control = cx18_av_read4(cx, CXADEC_DL_CTL);
71 }
72 if (ret == 0)
73 CX18_INFO_DEV(sd, "verified load of %s firmware (%d bytes)\n",
74 FWFILE, size);
75 return ret;
76}
77
78int cx18_av_loadfw(struct cx18 *cx)
79{
80 struct v4l2_subdev *sd = &cx->av_state.sd;
81 const struct firmware *fw = NULL;
82 u32 size;
83 u32 u, v;
84 const u8 *ptr;
85 int i;
86 int retries1 = 0;
87
88 if (request_firmware(&fw, FWFILE, &cx->pci_dev->dev) != 0) {
89 CX18_ERR_DEV(sd, "unable to open firmware %s\n", FWFILE);
90 return -EINVAL;
91 }
92
93 /* The firmware load often has byte errors, so allow for several
94 retries, both at byte level and at the firmware load level. */
95 while (retries1 < 5) {
96 cx18_av_write4_expect(cx, CXADEC_CHIP_CTRL, 0x00010000,
97 0x00008430, 0xffffffff); /* cx25843 */
98 cx18_av_write_expect(cx, CXADEC_STD_DET_CTL, 0xf6, 0xf6, 0xff);
99
100 /* Reset the Mako core, Register is alias of CXADEC_CHIP_CTRL */
101 cx18_av_write4_expect(cx, 0x8100, 0x00010000,
102 0x00008430, 0xffffffff); /* cx25843 */
103
104 /* Put the 8051 in reset and enable firmware upload */
105 cx18_av_write4_noretry(cx, CXADEC_DL_CTL, 0x0F000000);
106
107 ptr = fw->data;
108 size = fw->size;
109
110 for (i = 0; i < size; i++) {
111 u32 dl_control = 0x0F000000 | i | ((u32)ptr[i] << 16);
112 u32 value = 0;
113 int retries2;
114 int unrec_err = 0;
115
116 for (retries2 = 0; retries2 < CX18_MAX_MMIO_WR_RETRIES;
117 retries2++) {
118 cx18_av_write4_noretry(cx, CXADEC_DL_CTL,
119 dl_control);
120 udelay(10);
121 value = cx18_av_read4(cx, CXADEC_DL_CTL);
122 if (value == dl_control)
123 break;
124 /* Check if we can correct the byte by changing
125 the address. We can only write the lower
126 address byte of the address. */
127 if ((value & 0x3F00) != (dl_control & 0x3F00)) {
128 unrec_err = 1;
129 break;
130 }
131 }
132 if (unrec_err || retries2 >= CX18_MAX_MMIO_WR_RETRIES)
133 break;
134 }
135 if (i == size)
136 break;
137 retries1++;
138 }
139 if (retries1 >= 5) {
140 CX18_ERR_DEV(sd, "unable to load firmware %s\n", FWFILE);
141 release_firmware(fw);
142 return -EIO;
143 }
144
145 cx18_av_write4_expect(cx, CXADEC_DL_CTL,
146 0x03000000 | fw->size, 0x03000000, 0x13000000);
147
148 CX18_INFO_DEV(sd, "loaded %s firmware (%d bytes)\n", FWFILE, size);
149
150 if (cx18_av_verifyfw(cx, fw) == 0)
151 cx18_av_write4_expect(cx, CXADEC_DL_CTL,
152 0x13000000 | fw->size, 0x13000000, 0x13000000);
153
154 /* Output to the 416 */
155 cx18_av_and_or4(cx, CXADEC_PIN_CTRL1, ~0, 0x78000);
156
157 /* Audio input control 1 set to Sony mode */
158 /* Audio output input 2 is 0 for slave operation input */
159 /* 0xC4000914[5]: 0 = left sample on WS=0, 1 = left sample on WS=1 */
160 /* 0xC4000914[7]: 0 = Philips mode, 1 = Sony mode (1st SCK rising edge
161 after WS transition for first bit of audio word. */
162 cx18_av_write4(cx, CXADEC_I2S_IN_CTL, 0x000000A0);
163
164 /* Audio output control 1 is set to Sony mode */
165 /* Audio output control 2 is set to 1 for master mode */
166 /* 0xC4000918[5]: 0 = left sample on WS=0, 1 = left sample on WS=1 */
167 /* 0xC4000918[7]: 0 = Philips mode, 1 = Sony mode (1st SCK rising edge
168 after WS transition for first bit of audio word. */
169 /* 0xC4000918[8]: 0 = slave operation, 1 = master (SCK_OUT and WS_OUT
170 are generated) */
171 cx18_av_write4(cx, CXADEC_I2S_OUT_CTL, 0x000001A0);
172
173 /* set alt I2s master clock to /0x16 and enable alt divider i2s
174 passthrough */
175 cx18_av_write4(cx, CXADEC_PIN_CFG3, 0x5600B687);
176
177 cx18_av_write4_expect(cx, CXADEC_STD_DET_CTL, 0x000000F6, 0x000000F6,
178 0x3F00FFFF);
179 /* CxDevWrReg(CXADEC_STD_DET_CTL, 0x000000FF); */
180
181 /* Set bit 0 in register 0x9CC to signify that this is MiniMe. */
182 /* Register 0x09CC is defined by the Merlin firmware, and doesn't
183 have a name in the spec. */
184 cx18_av_write4(cx, 0x09CC, 1);
185
186 v = cx18_read_reg(cx, CX18_AUDIO_ENABLE);
187 /* If bit 11 is 1, clear bit 10 */
188 if (v & 0x800)
189 cx18_write_reg_expect(cx, v & 0xFFFFFBFF, CX18_AUDIO_ENABLE,
190 0, 0x400);
191
192 /* Toggle the AI1 MUX */
193 v = cx18_read_reg(cx, CX18_AUDIO_ENABLE);
194 u = v & CX18_AI1_MUX_MASK;
195 v &= ~CX18_AI1_MUX_MASK;
196 if (u == CX18_AI1_MUX_843_I2S || u == CX18_AI1_MUX_INVALID) {
197 /* Switch to I2S1 */
198 v |= CX18_AI1_MUX_I2S1;
199 cx18_write_reg_expect(cx, v | 0xb00, CX18_AUDIO_ENABLE,
200 v, CX18_AI1_MUX_MASK);
201 /* Switch back to the A/V decoder core I2S output */
202 v = (v & ~CX18_AI1_MUX_MASK) | CX18_AI1_MUX_843_I2S;
203 } else {
204 /* Switch to the A/V decoder core I2S output */
205 v |= CX18_AI1_MUX_843_I2S;
206 cx18_write_reg_expect(cx, v | 0xb00, CX18_AUDIO_ENABLE,
207 v, CX18_AI1_MUX_MASK);
208 /* Switch back to I2S1 or I2S2 */
209 v = (v & ~CX18_AI1_MUX_MASK) | u;
210 }
211 cx18_write_reg_expect(cx, v | 0xb00, CX18_AUDIO_ENABLE,
212 v, CX18_AI1_MUX_MASK);
213
214 /* Enable WW auto audio standard detection */
215 v = cx18_av_read4(cx, CXADEC_STD_DET_CTL);
216 v |= 0xFF; /* Auto by default */
217 v |= 0x400; /* Stereo by default */
218 v |= 0x14000000;
219 cx18_av_write4_expect(cx, CXADEC_STD_DET_CTL, v, v, 0x3F00FFFF);
220
221 release_firmware(fw);
222 return 0;
223}
224
225MODULE_FIRMWARE(FWFILE);
diff --git a/drivers/media/pci/cx18/cx18-av-vbi.c b/drivers/media/pci/cx18/cx18-av-vbi.c
new file mode 100644
index 000000000000..baa36fbcd4d4
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-av-vbi.c
@@ -0,0 +1,311 @@
1/*
2 * cx18 ADEC VBI functions
3 *
4 * Derived from cx25840-vbi.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24
25#include "cx18-driver.h"
26
27/*
28 * For sliced VBI output, we set up to use VIP-1.1, 8-bit mode,
29 * NN counts 1 byte Dwords, an IDID with the VBI line # in it.
30 * Thus, according to the VIP-2 Spec, our VBI ancillary data lines
31 * (should!) look like:
32 * 4 byte EAV code: 0xff 0x00 0x00 0xRP
33 * unknown number of possible idle bytes
34 * 3 byte Anc data preamble: 0x00 0xff 0xff
35 * 1 byte data identifier: ne010iii (parity bits, 010, DID bits)
36 * 1 byte secondary data id: nessssss (parity bits, SDID bits)
37 * 1 byte data word count: necccccc (parity bits, NN Dword count)
38 * 2 byte Internal DID: VBI-line-# 0x80
39 * NN data bytes
40 * 1 byte checksum
41 * Fill bytes needed to fil out to 4*NN bytes of payload
42 *
43 * The RP codes for EAVs when in VIP-1.1 mode, not in raw mode, &
44 * in the vertical blanking interval are:
45 * 0xb0 (Task 0 VerticalBlank HorizontalBlank 0 0 0 0)
46 * 0xf0 (Task EvenField VerticalBlank HorizontalBlank 0 0 0 0)
47 *
48 * Since the V bit is only allowed to toggle in the EAV RP code, just
49 * before the first active region line and for active lines, they are:
50 * 0x90 (Task 0 0 HorizontalBlank 0 0 0 0)
51 * 0xd0 (Task EvenField 0 HorizontalBlank 0 0 0 0)
52 *
53 * The user application DID bytes we care about are:
54 * 0x91 (1 0 010 0 !ActiveLine AncDataPresent)
55 * 0x55 (0 1 010 2ndField !ActiveLine AncDataPresent)
56 *
57 */
58static const u8 sliced_vbi_did[2] = { 0x91, 0x55 };
59
60struct vbi_anc_data {
61 /* u8 eav[4]; */
62 /* u8 idle[]; Variable number of idle bytes */
63 u8 preamble[3];
64 u8 did;
65 u8 sdid;
66 u8 data_count;
67 u8 idid[2];
68 u8 payload[1]; /* data_count of payload */
69 /* u8 checksum; */
70 /* u8 fill[]; Variable number of fill bytes */
71};
72
73static int odd_parity(u8 c)
74{
75 c ^= (c >> 4);
76 c ^= (c >> 2);
77 c ^= (c >> 1);
78
79 return c & 1;
80}
81
82static int decode_vps(u8 *dst, u8 *p)
83{
84 static const u8 biphase_tbl[] = {
85 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
86 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
87 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
88 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
89 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
90 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
91 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
92 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
93 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
94 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
95 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
96 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
97 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
98 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
99 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
100 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
101 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
102 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
103 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
104 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
105 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
106 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
107 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
108 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
109 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
110 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
111 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
112 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
113 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
114 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
115 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
116 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
117 };
118
119 u8 c, err = 0;
120 int i;
121
122 for (i = 0; i < 2 * 13; i += 2) {
123 err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];
124 c = (biphase_tbl[p[i + 1]] & 0xf) |
125 ((biphase_tbl[p[i]] & 0xf) << 4);
126 dst[i / 2] = c;
127 }
128
129 return err & 0xf0;
130}
131
132int cx18_av_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
133{
134 struct cx18 *cx = v4l2_get_subdevdata(sd);
135 struct cx18_av_state *state = &cx->av_state;
136 static const u16 lcr2vbi[] = {
137 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */
138 0, V4L2_SLICED_WSS_625, 0, /* 4 */
139 V4L2_SLICED_CAPTION_525, /* 6 */
140 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */
141 0, 0, 0, 0
142 };
143 int is_pal = !(state->std & V4L2_STD_525_60);
144 int i;
145
146 memset(svbi, 0, sizeof(*svbi));
147 /* we're done if raw VBI is active */
148 if ((cx18_av_read(cx, 0x404) & 0x10) == 0)
149 return 0;
150
151 if (is_pal) {
152 for (i = 7; i <= 23; i++) {
153 u8 v = cx18_av_read(cx, 0x424 + i - 7);
154
155 svbi->service_lines[0][i] = lcr2vbi[v >> 4];
156 svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
157 svbi->service_set |= svbi->service_lines[0][i] |
158 svbi->service_lines[1][i];
159 }
160 } else {
161 for (i = 10; i <= 21; i++) {
162 u8 v = cx18_av_read(cx, 0x424 + i - 10);
163
164 svbi->service_lines[0][i] = lcr2vbi[v >> 4];
165 svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
166 svbi->service_set |= svbi->service_lines[0][i] |
167 svbi->service_lines[1][i];
168 }
169 }
170 return 0;
171}
172
173int cx18_av_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
174{
175 struct cx18 *cx = v4l2_get_subdevdata(sd);
176 struct cx18_av_state *state = &cx->av_state;
177
178 /* Setup standard */
179 cx18_av_std_setup(cx);
180
181 /* VBI Offset */
182 cx18_av_write(cx, 0x47f, state->slicer_line_delay);
183 cx18_av_write(cx, 0x404, 0x2e);
184 return 0;
185}
186
187int cx18_av_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
188{
189 struct cx18 *cx = v4l2_get_subdevdata(sd);
190 struct cx18_av_state *state = &cx->av_state;
191 int is_pal = !(state->std & V4L2_STD_525_60);
192 int i, x;
193 u8 lcr[24];
194
195 for (x = 0; x <= 23; x++)
196 lcr[x] = 0x00;
197
198 /* Setup standard */
199 cx18_av_std_setup(cx);
200
201 /* Sliced VBI */
202 cx18_av_write(cx, 0x404, 0x32); /* Ancillary data */
203 cx18_av_write(cx, 0x406, 0x13);
204 cx18_av_write(cx, 0x47f, state->slicer_line_delay);
205
206 /* Force impossible lines to 0 */
207 if (is_pal) {
208 for (i = 0; i <= 6; i++)
209 svbi->service_lines[0][i] =
210 svbi->service_lines[1][i] = 0;
211 } else {
212 for (i = 0; i <= 9; i++)
213 svbi->service_lines[0][i] =
214 svbi->service_lines[1][i] = 0;
215
216 for (i = 22; i <= 23; i++)
217 svbi->service_lines[0][i] =
218 svbi->service_lines[1][i] = 0;
219 }
220
221 /* Build register values for requested service lines */
222 for (i = 7; i <= 23; i++) {
223 for (x = 0; x <= 1; x++) {
224 switch (svbi->service_lines[1-x][i]) {
225 case V4L2_SLICED_TELETEXT_B:
226 lcr[i] |= 1 << (4 * x);
227 break;
228 case V4L2_SLICED_WSS_625:
229 lcr[i] |= 4 << (4 * x);
230 break;
231 case V4L2_SLICED_CAPTION_525:
232 lcr[i] |= 6 << (4 * x);
233 break;
234 case V4L2_SLICED_VPS:
235 lcr[i] |= 9 << (4 * x);
236 break;
237 }
238 }
239 }
240
241 if (is_pal) {
242 for (x = 1, i = 0x424; i <= 0x434; i++, x++)
243 cx18_av_write(cx, i, lcr[6 + x]);
244 } else {
245 for (x = 1, i = 0x424; i <= 0x430; i++, x++)
246 cx18_av_write(cx, i, lcr[9 + x]);
247 for (i = 0x431; i <= 0x434; i++)
248 cx18_av_write(cx, i, 0);
249 }
250
251 cx18_av_write(cx, 0x43c, 0x16);
252 /* Should match vblank set in cx18_av_std_setup() */
253 cx18_av_write(cx, 0x474, is_pal ? 38 : 26);
254 return 0;
255}
256
257int cx18_av_decode_vbi_line(struct v4l2_subdev *sd,
258 struct v4l2_decode_vbi_line *vbi)
259{
260 struct cx18 *cx = v4l2_get_subdevdata(sd);
261 struct cx18_av_state *state = &cx->av_state;
262 struct vbi_anc_data *anc = (struct vbi_anc_data *)vbi->p;
263 u8 *p;
264 int did, sdid, l, err = 0;
265
266 /*
267 * Check for the ancillary data header for sliced VBI
268 */
269 if (anc->preamble[0] ||
270 anc->preamble[1] != 0xff || anc->preamble[2] != 0xff ||
271 (anc->did != sliced_vbi_did[0] &&
272 anc->did != sliced_vbi_did[1])) {
273 vbi->line = vbi->type = 0;
274 return 0;
275 }
276
277 did = anc->did;
278 sdid = anc->sdid & 0xf;
279 l = anc->idid[0] & 0x3f;
280 l += state->slicer_line_offset;
281 p = anc->payload;
282
283 /* Decode the SDID set by the slicer */
284 switch (sdid) {
285 case 1:
286 sdid = V4L2_SLICED_TELETEXT_B;
287 break;
288 case 4:
289 sdid = V4L2_SLICED_WSS_625;
290 break;
291 case 6:
292 sdid = V4L2_SLICED_CAPTION_525;
293 err = !odd_parity(p[0]) || !odd_parity(p[1]);
294 break;
295 case 9:
296 sdid = V4L2_SLICED_VPS;
297 if (decode_vps(p, p) != 0)
298 err = 1;
299 break;
300 default:
301 sdid = 0;
302 err = 1;
303 break;
304 }
305
306 vbi->type = err ? 0 : sdid;
307 vbi->line = err ? 0 : l;
308 vbi->is_second_field = err ? 0 : (did == sliced_vbi_did[1]);
309 vbi->p = p;
310 return 0;
311}
diff --git a/drivers/media/pci/cx18/cx18-cards.c b/drivers/media/pci/cx18/cx18-cards.c
new file mode 100644
index 000000000000..c07c849b1aaf
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-cards.c
@@ -0,0 +1,638 @@
1/*
2 * cx18 functions to query card hardware
3 *
4 * Derived from ivtv-cards.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-cards.h"
27#include "cx18-av-core.h"
28#include "cx18-i2c.h"
29#include <media/cs5345.h>
30
31#define V4L2_STD_PAL_SECAM (V4L2_STD_PAL|V4L2_STD_SECAM)
32
33/********************** card configuration *******************************/
34
35/* usual i2c tuner addresses to probe */
36static struct cx18_card_tuner_i2c cx18_i2c_std = {
37 .radio = { I2C_CLIENT_END },
38 .demod = { 0x43, I2C_CLIENT_END },
39 .tv = { 0x61, 0x60, I2C_CLIENT_END },
40};
41
42/*
43 * usual i2c tuner addresses to probe with additional demod address for
44 * an NXP TDA8295 at 0x42 (N.B. it can possibly be at 0x4b or 0x4c too).
45 */
46static struct cx18_card_tuner_i2c cx18_i2c_nxp = {
47 .radio = { I2C_CLIENT_END },
48 .demod = { 0x42, 0x43, I2C_CLIENT_END },
49 .tv = { 0x61, 0x60, I2C_CLIENT_END },
50};
51
52/* Please add new PCI IDs to: http://pci-ids.ucw.cz/
53 This keeps the PCI ID database up to date. Note that the entries
54 must be added under vendor 0x4444 (Conexant) as subsystem IDs.
55 New vendor IDs should still be added to the vendor ID list. */
56
57/* Hauppauge HVR-1600 cards */
58
59/* Note: for Hauppauge cards the tveeprom information is used instead
60 of PCI IDs */
61static const struct cx18_card cx18_card_hvr1600_esmt = {
62 .type = CX18_CARD_HVR_1600_ESMT,
63 .name = "Hauppauge HVR-1600",
64 .comment = "Simultaneous Digital and Analog TV capture supported\n",
65 .v4l2_capabilities = CX18_CAP_ENCODER,
66 .hw_audio_ctrl = CX18_HW_418_AV,
67 .hw_muxer = CX18_HW_CS5345,
68 .hw_all = CX18_HW_TVEEPROM | CX18_HW_418_AV | CX18_HW_TUNER |
69 CX18_HW_CS5345 | CX18_HW_DVB | CX18_HW_GPIO_RESET_CTRL |
70 CX18_HW_Z8F0811_IR_HAUP,
71 .video_inputs = {
72 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE7 },
73 { CX18_CARD_INPUT_SVIDEO1, 1, CX18_AV_SVIDEO1 },
74 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE3 },
75 { CX18_CARD_INPUT_SVIDEO2, 2, CX18_AV_SVIDEO2 },
76 { CX18_CARD_INPUT_COMPOSITE2, 2, CX18_AV_COMPOSITE4 },
77 },
78 .audio_inputs = {
79 { CX18_CARD_INPUT_AUD_TUNER,
80 CX18_AV_AUDIO8, CS5345_IN_1 | CS5345_MCLK_1_5 },
81 { CX18_CARD_INPUT_LINE_IN1,
82 CX18_AV_AUDIO_SERIAL1, CS5345_IN_2 },
83 { CX18_CARD_INPUT_LINE_IN2,
84 CX18_AV_AUDIO_SERIAL1, CS5345_IN_3 },
85 },
86 .radio_input = { CX18_CARD_INPUT_AUD_TUNER,
87 CX18_AV_AUDIO_SERIAL1, CS5345_IN_4 },
88 .ddr = {
89 /* ESMT M13S128324A-5B memory */
90 .chip_config = 0x003,
91 .refresh = 0x30c,
92 .timing1 = 0x44220e82,
93 .timing2 = 0x08,
94 .tune_lane = 0,
95 .initial_emrs = 0,
96 },
97 .gpio_init.initial_value = 0x3001,
98 .gpio_init.direction = 0x3001,
99 .gpio_i2c_slave_reset = {
100 .active_lo_mask = 0x3001,
101 .msecs_asserted = 10,
102 .msecs_recovery = 40,
103 .ir_reset_mask = 0x0001,
104 },
105 .i2c = &cx18_i2c_std,
106};
107
108static const struct cx18_card cx18_card_hvr1600_s5h1411 = {
109 .type = CX18_CARD_HVR_1600_S5H1411,
110 .name = "Hauppauge HVR-1600",
111 .comment = "Simultaneous Digital and Analog TV capture supported\n",
112 .v4l2_capabilities = CX18_CAP_ENCODER,
113 .hw_audio_ctrl = CX18_HW_418_AV,
114 .hw_muxer = CX18_HW_CS5345,
115 .hw_all = CX18_HW_TVEEPROM | CX18_HW_418_AV | CX18_HW_TUNER |
116 CX18_HW_CS5345 | CX18_HW_DVB | CX18_HW_GPIO_RESET_CTRL |
117 CX18_HW_Z8F0811_IR_HAUP,
118 .video_inputs = {
119 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE7 },
120 { CX18_CARD_INPUT_SVIDEO1, 1, CX18_AV_SVIDEO1 },
121 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE3 },
122 { CX18_CARD_INPUT_SVIDEO2, 2, CX18_AV_SVIDEO2 },
123 { CX18_CARD_INPUT_COMPOSITE2, 2, CX18_AV_COMPOSITE4 },
124 },
125 .audio_inputs = {
126 { CX18_CARD_INPUT_AUD_TUNER,
127 CX18_AV_AUDIO8, CS5345_IN_1 | CS5345_MCLK_1_5 },
128 { CX18_CARD_INPUT_LINE_IN1,
129 CX18_AV_AUDIO_SERIAL1, CS5345_IN_2 },
130 { CX18_CARD_INPUT_LINE_IN2,
131 CX18_AV_AUDIO_SERIAL1, CS5345_IN_3 },
132 },
133 .radio_input = { CX18_CARD_INPUT_AUD_TUNER,
134 CX18_AV_AUDIO_SERIAL1, CS5345_IN_4 },
135 .ddr = {
136 /* ESMT M13S128324A-5B memory */
137 .chip_config = 0x003,
138 .refresh = 0x30c,
139 .timing1 = 0x44220e82,
140 .timing2 = 0x08,
141 .tune_lane = 0,
142 .initial_emrs = 0,
143 },
144 .gpio_init.initial_value = 0x3801,
145 .gpio_init.direction = 0x3801,
146 .gpio_i2c_slave_reset = {
147 .active_lo_mask = 0x3801,
148 .msecs_asserted = 10,
149 .msecs_recovery = 40,
150 .ir_reset_mask = 0x0001,
151 },
152 .i2c = &cx18_i2c_nxp,
153};
154
155static const struct cx18_card cx18_card_hvr1600_samsung = {
156 .type = CX18_CARD_HVR_1600_SAMSUNG,
157 .name = "Hauppauge HVR-1600 (Preproduction)",
158 .comment = "Simultaneous Digital and Analog TV capture supported\n",
159 .v4l2_capabilities = CX18_CAP_ENCODER,
160 .hw_audio_ctrl = CX18_HW_418_AV,
161 .hw_muxer = CX18_HW_CS5345,
162 .hw_all = CX18_HW_TVEEPROM | CX18_HW_418_AV | CX18_HW_TUNER |
163 CX18_HW_CS5345 | CX18_HW_DVB | CX18_HW_GPIO_RESET_CTRL |
164 CX18_HW_Z8F0811_IR_HAUP,
165 .video_inputs = {
166 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE7 },
167 { CX18_CARD_INPUT_SVIDEO1, 1, CX18_AV_SVIDEO1 },
168 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE3 },
169 { CX18_CARD_INPUT_SVIDEO2, 2, CX18_AV_SVIDEO2 },
170 { CX18_CARD_INPUT_COMPOSITE2, 2, CX18_AV_COMPOSITE4 },
171 },
172 .audio_inputs = {
173 { CX18_CARD_INPUT_AUD_TUNER,
174 CX18_AV_AUDIO8, CS5345_IN_1 | CS5345_MCLK_1_5 },
175 { CX18_CARD_INPUT_LINE_IN1,
176 CX18_AV_AUDIO_SERIAL1, CS5345_IN_2 },
177 { CX18_CARD_INPUT_LINE_IN2,
178 CX18_AV_AUDIO_SERIAL1, CS5345_IN_3 },
179 },
180 .radio_input = { CX18_CARD_INPUT_AUD_TUNER,
181 CX18_AV_AUDIO_SERIAL1, CS5345_IN_4 },
182 .ddr = {
183 /* Samsung K4D263238G-VC33 memory */
184 .chip_config = 0x003,
185 .refresh = 0x30c,
186 .timing1 = 0x23230b73,
187 .timing2 = 0x08,
188 .tune_lane = 0,
189 .initial_emrs = 2,
190 },
191 .gpio_init.initial_value = 0x3001,
192 .gpio_init.direction = 0x3001,
193 .gpio_i2c_slave_reset = {
194 .active_lo_mask = 0x3001,
195 .msecs_asserted = 10,
196 .msecs_recovery = 40,
197 .ir_reset_mask = 0x0001,
198 },
199 .i2c = &cx18_i2c_std,
200};
201
202/* ------------------------------------------------------------------------- */
203
204/* Compro VideoMate H900: note that this card is analog only! */
205
206static const struct cx18_card_pci_info cx18_pci_h900[] = {
207 { PCI_DEVICE_ID_CX23418, CX18_PCI_ID_COMPRO, 0xe100 },
208 { 0, 0, 0 }
209};
210
211static const struct cx18_card cx18_card_h900 = {
212 .type = CX18_CARD_COMPRO_H900,
213 .name = "Compro VideoMate H900",
214 .comment = "Analog TV capture supported\n",
215 .v4l2_capabilities = CX18_CAP_ENCODER,
216 .hw_audio_ctrl = CX18_HW_418_AV,
217 .hw_all = CX18_HW_418_AV | CX18_HW_TUNER | CX18_HW_GPIO_RESET_CTRL,
218 .video_inputs = {
219 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE2 },
220 { CX18_CARD_INPUT_SVIDEO1, 1,
221 CX18_AV_SVIDEO_LUMA3 | CX18_AV_SVIDEO_CHROMA4 },
222 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE1 },
223 },
224 .audio_inputs = {
225 { CX18_CARD_INPUT_AUD_TUNER,
226 CX18_AV_AUDIO5, 0 },
227 { CX18_CARD_INPUT_LINE_IN1,
228 CX18_AV_AUDIO_SERIAL1, 0 },
229 },
230 .radio_input = { CX18_CARD_INPUT_AUD_TUNER,
231 CX18_AV_AUDIO_SERIAL1, 0 },
232 .tuners = {
233 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
234 },
235 .ddr = {
236 /* EtronTech EM6A9160TS-5G memory */
237 .chip_config = 0x50003,
238 .refresh = 0x753,
239 .timing1 = 0x24330e84,
240 .timing2 = 0x1f,
241 .tune_lane = 0,
242 .initial_emrs = 0,
243 },
244 .xceive_pin = 15,
245 .pci_list = cx18_pci_h900,
246 .i2c = &cx18_i2c_std,
247};
248
249/* ------------------------------------------------------------------------- */
250
251/* Yuan MPC718: not working at the moment! */
252
253static const struct cx18_card_pci_info cx18_pci_mpc718[] = {
254 { PCI_DEVICE_ID_CX23418, CX18_PCI_ID_YUAN, 0x0718 },
255 { 0, 0, 0 }
256};
257
258static const struct cx18_card cx18_card_mpc718 = {
259 .type = CX18_CARD_YUAN_MPC718,
260 .name = "Yuan MPC718 MiniPCI DVB-T/Analog",
261 .comment = "Experimenters needed for device to work well.\n"
262 "\tTo help, mail the ivtv-devel list (www.ivtvdriver.org).\n",
263 .v4l2_capabilities = CX18_CAP_ENCODER,
264 .hw_audio_ctrl = CX18_HW_418_AV,
265 .hw_muxer = CX18_HW_GPIO_MUX,
266 .hw_all = CX18_HW_TVEEPROM | CX18_HW_418_AV | CX18_HW_TUNER |
267 CX18_HW_GPIO_MUX | CX18_HW_DVB | CX18_HW_GPIO_RESET_CTRL,
268 .video_inputs = {
269 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE2 },
270 { CX18_CARD_INPUT_SVIDEO1, 1,
271 CX18_AV_SVIDEO_LUMA3 | CX18_AV_SVIDEO_CHROMA4 },
272 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE1 },
273 { CX18_CARD_INPUT_SVIDEO2, 2,
274 CX18_AV_SVIDEO_LUMA7 | CX18_AV_SVIDEO_CHROMA8 },
275 { CX18_CARD_INPUT_COMPOSITE2, 2, CX18_AV_COMPOSITE6 },
276 },
277 .audio_inputs = {
278 { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 0 },
279 { CX18_CARD_INPUT_LINE_IN1, CX18_AV_AUDIO_SERIAL1, 1 },
280 { CX18_CARD_INPUT_LINE_IN2, CX18_AV_AUDIO_SERIAL2, 1 },
281 },
282 .tuners = {
283 /* XC3028 tuner */
284 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
285 },
286 /* FIXME - the FM radio is just a guess and driver doesn't use SIF */
287 .radio_input = { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 2 },
288 .ddr = {
289 /* Hynix HY5DU283222B DDR RAM */
290 .chip_config = 0x303,
291 .refresh = 0x3bd,
292 .timing1 = 0x36320966,
293 .timing2 = 0x1f,
294 .tune_lane = 0,
295 .initial_emrs = 2,
296 },
297 .gpio_init.initial_value = 0x1,
298 .gpio_init.direction = 0x3,
299 /* FIXME - these GPIO's are just guesses */
300 .gpio_audio_input = { .mask = 0x3,
301 .tuner = 0x1,
302 .linein = 0x3,
303 .radio = 0x1 },
304 .xceive_pin = 0,
305 .pci_list = cx18_pci_mpc718,
306 .i2c = &cx18_i2c_std,
307};
308
309/* ------------------------------------------------------------------------- */
310
311/* GoTView PCI */
312
313static const struct cx18_card_pci_info cx18_pci_gotview_dvd3[] = {
314 { PCI_DEVICE_ID_CX23418, CX18_PCI_ID_GOTVIEW, 0x3343 },
315 { 0, 0, 0 }
316};
317
318static const struct cx18_card cx18_card_gotview_dvd3 = {
319 .type = CX18_CARD_GOTVIEW_PCI_DVD3,
320 .name = "GoTView PCI DVD3 Hybrid",
321 .comment = "Experimenters needed for device to work well.\n"
322 "\tTo help, mail the ivtv-devel list (www.ivtvdriver.org).\n",
323 .v4l2_capabilities = CX18_CAP_ENCODER,
324 .hw_audio_ctrl = CX18_HW_418_AV,
325 .hw_muxer = CX18_HW_GPIO_MUX,
326 .hw_all = CX18_HW_TVEEPROM | CX18_HW_418_AV | CX18_HW_TUNER |
327 CX18_HW_GPIO_MUX | CX18_HW_DVB | CX18_HW_GPIO_RESET_CTRL,
328 .video_inputs = {
329 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE2 },
330 { CX18_CARD_INPUT_SVIDEO1, 1,
331 CX18_AV_SVIDEO_LUMA3 | CX18_AV_SVIDEO_CHROMA4 },
332 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE1 },
333 { CX18_CARD_INPUT_SVIDEO2, 2,
334 CX18_AV_SVIDEO_LUMA7 | CX18_AV_SVIDEO_CHROMA8 },
335 { CX18_CARD_INPUT_COMPOSITE2, 2, CX18_AV_COMPOSITE6 },
336 },
337 .audio_inputs = {
338 { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 0 },
339 { CX18_CARD_INPUT_LINE_IN1, CX18_AV_AUDIO_SERIAL1, 1 },
340 { CX18_CARD_INPUT_LINE_IN2, CX18_AV_AUDIO_SERIAL2, 1 },
341 },
342 .tuners = {
343 /* XC3028 tuner */
344 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
345 },
346 /* FIXME - the FM radio is just a guess and driver doesn't use SIF */
347 .radio_input = { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 2 },
348 .ddr = {
349 /* Hynix HY5DU283222B DDR RAM */
350 .chip_config = 0x303,
351 .refresh = 0x3bd,
352 .timing1 = 0x36320966,
353 .timing2 = 0x1f,
354 .tune_lane = 0,
355 .initial_emrs = 2,
356 },
357 .gpio_init.initial_value = 0x1,
358 .gpio_init.direction = 0x3,
359
360 .gpio_audio_input = { .mask = 0x3,
361 .tuner = 0x1,
362 .linein = 0x2,
363 .radio = 0x1 },
364 .xceive_pin = 0,
365 .pci_list = cx18_pci_gotview_dvd3,
366 .i2c = &cx18_i2c_std,
367};
368
369/* ------------------------------------------------------------------------- */
370
371/* Conexant Raptor PAL/SECAM: note that this card is analog only! */
372
373static const struct cx18_card_pci_info cx18_pci_cnxt_raptor_pal[] = {
374 { PCI_DEVICE_ID_CX23418, CX18_PCI_ID_CONEXANT, 0x0009 },
375 { 0, 0, 0 }
376};
377
378static const struct cx18_card cx18_card_cnxt_raptor_pal = {
379 .type = CX18_CARD_CNXT_RAPTOR_PAL,
380 .name = "Conexant Raptor PAL/SECAM",
381 .comment = "Analog TV capture supported\n",
382 .v4l2_capabilities = CX18_CAP_ENCODER,
383 .hw_audio_ctrl = CX18_HW_418_AV,
384 .hw_muxer = CX18_HW_GPIO_MUX,
385 .hw_all = CX18_HW_418_AV | CX18_HW_TUNER | CX18_HW_GPIO_MUX,
386 .video_inputs = {
387 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE2 },
388 { CX18_CARD_INPUT_SVIDEO1, 1,
389 CX18_AV_SVIDEO_LUMA3 | CX18_AV_SVIDEO_CHROMA4 },
390 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE1 },
391 { CX18_CARD_INPUT_SVIDEO2, 2,
392 CX18_AV_SVIDEO_LUMA7 | CX18_AV_SVIDEO_CHROMA8 },
393 { CX18_CARD_INPUT_COMPOSITE2, 2, CX18_AV_COMPOSITE6 },
394 },
395 .audio_inputs = {
396 { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 0 },
397 { CX18_CARD_INPUT_LINE_IN1, CX18_AV_AUDIO_SERIAL1, 1 },
398 { CX18_CARD_INPUT_LINE_IN2, CX18_AV_AUDIO_SERIAL2, 1 },
399 },
400 .tuners = {
401 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
402 },
403 .radio_input = { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO_SERIAL1, 2 },
404 .ddr = {
405 /* MT 46V16M16 memory */
406 .chip_config = 0x50306,
407 .refresh = 0x753,
408 .timing1 = 0x33220953,
409 .timing2 = 0x09,
410 .tune_lane = 0,
411 .initial_emrs = 0,
412 },
413 .gpio_init.initial_value = 0x1002,
414 .gpio_init.direction = 0xf002,
415 .gpio_audio_input = { .mask = 0xf002,
416 .tuner = 0x1002, /* LED D1 Tuner AF */
417 .linein = 0x2000, /* LED D2 Line In 1 */
418 .radio = 0x4002 }, /* LED D3 Tuner AF */
419 .pci_list = cx18_pci_cnxt_raptor_pal,
420 .i2c = &cx18_i2c_std,
421};
422
423/* ------------------------------------------------------------------------- */
424
425/* Toshiba Qosmio laptop internal DVB-T/Analog Hybrid Tuner */
426
427static const struct cx18_card_pci_info cx18_pci_toshiba_qosmio_dvbt[] = {
428 { PCI_DEVICE_ID_CX23418, CX18_PCI_ID_TOSHIBA, 0x0110 },
429 { 0, 0, 0 }
430};
431
432static const struct cx18_card cx18_card_toshiba_qosmio_dvbt = {
433 .type = CX18_CARD_TOSHIBA_QOSMIO_DVBT,
434 .name = "Toshiba Qosmio DVB-T/Analog",
435 .comment = "Experimenters and photos needed for device to work well.\n"
436 "\tTo help, mail the ivtv-devel list (www.ivtvdriver.org).\n",
437 .v4l2_capabilities = CX18_CAP_ENCODER,
438 .hw_audio_ctrl = CX18_HW_418_AV,
439 .hw_all = CX18_HW_418_AV | CX18_HW_TUNER | CX18_HW_GPIO_RESET_CTRL,
440 .video_inputs = {
441 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE6 },
442 { CX18_CARD_INPUT_SVIDEO1, 1,
443 CX18_AV_SVIDEO_LUMA3 | CX18_AV_SVIDEO_CHROMA4 },
444 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE1 },
445 },
446 .audio_inputs = {
447 { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 0 },
448 { CX18_CARD_INPUT_LINE_IN1, CX18_AV_AUDIO_SERIAL1, 1 },
449 },
450 .tuners = {
451 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
452 },
453 .ddr = {
454 .chip_config = 0x202,
455 .refresh = 0x3bb,
456 .timing1 = 0x33320a63,
457 .timing2 = 0x0a,
458 .tune_lane = 0,
459 .initial_emrs = 0x42,
460 },
461 .xceive_pin = 15,
462 .pci_list = cx18_pci_toshiba_qosmio_dvbt,
463 .i2c = &cx18_i2c_std,
464};
465
466/* ------------------------------------------------------------------------- */
467
468/* Leadtek WinFast PVR2100 */
469
470static const struct cx18_card_pci_info cx18_pci_leadtek_pvr2100[] = {
471 { PCI_DEVICE_ID_CX23418, CX18_PCI_ID_LEADTEK, 0x6f27 }, /* PVR2100 */
472 { 0, 0, 0 }
473};
474
475static const struct cx18_card cx18_card_leadtek_pvr2100 = {
476 .type = CX18_CARD_LEADTEK_PVR2100,
477 .name = "Leadtek WinFast PVR2100",
478 .comment = "Experimenters and photos needed for device to work well.\n"
479 "\tTo help, mail the ivtv-devel list (www.ivtvdriver.org).\n",
480 .v4l2_capabilities = CX18_CAP_ENCODER,
481 .hw_audio_ctrl = CX18_HW_418_AV,
482 .hw_muxer = CX18_HW_GPIO_MUX,
483 .hw_all = CX18_HW_418_AV | CX18_HW_TUNER | CX18_HW_GPIO_MUX |
484 CX18_HW_GPIO_RESET_CTRL,
485 .video_inputs = {
486 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE2 },
487 { CX18_CARD_INPUT_SVIDEO1, 1,
488 CX18_AV_SVIDEO_LUMA3 | CX18_AV_SVIDEO_CHROMA4 },
489 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE7 },
490 { CX18_CARD_INPUT_COMPONENT1, 1, CX18_AV_COMPONENT1 },
491 },
492 .audio_inputs = {
493 { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 0 },
494 { CX18_CARD_INPUT_LINE_IN1, CX18_AV_AUDIO_SERIAL1, 1 },
495 },
496 .tuners = {
497 /* XC2028 tuner */
498 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
499 },
500 .radio_input = { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 2 },
501 .ddr = {
502 /* Pointer to proper DDR config values provided by Terry Wu */
503 .chip_config = 0x303,
504 .refresh = 0x3bb,
505 .timing1 = 0x24220e83,
506 .timing2 = 0x1f,
507 .tune_lane = 0,
508 .initial_emrs = 0x2,
509 },
510 .gpio_init.initial_value = 0x6,
511 .gpio_init.direction = 0x7,
512 .gpio_audio_input = { .mask = 0x7,
513 .tuner = 0x6, .linein = 0x2, .radio = 0x2 },
514 .xceive_pin = 1,
515 .pci_list = cx18_pci_leadtek_pvr2100,
516 .i2c = &cx18_i2c_std,
517};
518
519/* ------------------------------------------------------------------------- */
520
521/* Leadtek WinFast DVR3100 H */
522
523static const struct cx18_card_pci_info cx18_pci_leadtek_dvr3100h[] = {
524 { PCI_DEVICE_ID_CX23418, CX18_PCI_ID_LEADTEK, 0x6690 }, /* DVR3100 H */
525 { 0, 0, 0 }
526};
527
528static const struct cx18_card cx18_card_leadtek_dvr3100h = {
529 .type = CX18_CARD_LEADTEK_DVR3100H,
530 .name = "Leadtek WinFast DVR3100 H",
531 .comment = "Simultaneous DVB-T and Analog capture supported,\n"
532 "\texcept when capturing Analog from the antenna input.\n",
533 .v4l2_capabilities = CX18_CAP_ENCODER,
534 .hw_audio_ctrl = CX18_HW_418_AV,
535 .hw_muxer = CX18_HW_GPIO_MUX,
536 .hw_all = CX18_HW_418_AV | CX18_HW_TUNER | CX18_HW_GPIO_MUX |
537 CX18_HW_DVB | CX18_HW_GPIO_RESET_CTRL,
538 .video_inputs = {
539 { CX18_CARD_INPUT_VID_TUNER, 0, CX18_AV_COMPOSITE2 },
540 { CX18_CARD_INPUT_SVIDEO1, 1,
541 CX18_AV_SVIDEO_LUMA3 | CX18_AV_SVIDEO_CHROMA4 },
542 { CX18_CARD_INPUT_COMPOSITE1, 1, CX18_AV_COMPOSITE7 },
543 { CX18_CARD_INPUT_COMPONENT1, 1, CX18_AV_COMPONENT1 },
544 },
545 .audio_inputs = {
546 { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 0 },
547 { CX18_CARD_INPUT_LINE_IN1, CX18_AV_AUDIO_SERIAL1, 1 },
548 },
549 .tuners = {
550 /* XC3028 tuner */
551 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
552 },
553 .radio_input = { CX18_CARD_INPUT_AUD_TUNER, CX18_AV_AUDIO5, 2 },
554 .ddr = {
555 /* Pointer to proper DDR config values provided by Terry Wu */
556 .chip_config = 0x303,
557 .refresh = 0x3bb,
558 .timing1 = 0x24220e83,
559 .timing2 = 0x1f,
560 .tune_lane = 0,
561 .initial_emrs = 0x2,
562 },
563 .gpio_init.initial_value = 0x6,
564 .gpio_init.direction = 0x7,
565 .gpio_audio_input = { .mask = 0x7,
566 .tuner = 0x6, .linein = 0x2, .radio = 0x2 },
567 .xceive_pin = 1,
568 .pci_list = cx18_pci_leadtek_dvr3100h,
569 .i2c = &cx18_i2c_std,
570};
571
572/* ------------------------------------------------------------------------- */
573
574static const struct cx18_card *cx18_card_list[] = {
575 &cx18_card_hvr1600_esmt,
576 &cx18_card_hvr1600_samsung,
577 &cx18_card_h900,
578 &cx18_card_mpc718,
579 &cx18_card_cnxt_raptor_pal,
580 &cx18_card_toshiba_qosmio_dvbt,
581 &cx18_card_leadtek_pvr2100,
582 &cx18_card_leadtek_dvr3100h,
583 &cx18_card_gotview_dvd3,
584 &cx18_card_hvr1600_s5h1411
585};
586
587const struct cx18_card *cx18_get_card(u16 index)
588{
589 if (index >= ARRAY_SIZE(cx18_card_list))
590 return NULL;
591 return cx18_card_list[index];
592}
593
594int cx18_get_input(struct cx18 *cx, u16 index, struct v4l2_input *input)
595{
596 const struct cx18_card_video_input *card_input =
597 cx->card->video_inputs + index;
598 static const char * const input_strs[] = {
599 "Tuner 1",
600 "S-Video 1",
601 "S-Video 2",
602 "Composite 1",
603 "Composite 2",
604 "Component 1"
605 };
606
607 if (index >= cx->nof_inputs)
608 return -EINVAL;
609 input->index = index;
610 strlcpy(input->name, input_strs[card_input->video_type - 1],
611 sizeof(input->name));
612 input->type = (card_input->video_type == CX18_CARD_INPUT_VID_TUNER ?
613 V4L2_INPUT_TYPE_TUNER : V4L2_INPUT_TYPE_CAMERA);
614 input->audioset = (1 << cx->nof_audio_inputs) - 1;
615 input->std = (input->type == V4L2_INPUT_TYPE_TUNER) ?
616 cx->tuner_std : V4L2_STD_ALL;
617 return 0;
618}
619
620int cx18_get_audio_input(struct cx18 *cx, u16 index, struct v4l2_audio *audio)
621{
622 const struct cx18_card_audio_input *aud_input =
623 cx->card->audio_inputs + index;
624 static const char * const input_strs[] = {
625 "Tuner 1",
626 "Line In 1",
627 "Line In 2"
628 };
629
630 memset(audio, 0, sizeof(*audio));
631 if (index >= cx->nof_audio_inputs)
632 return -EINVAL;
633 strlcpy(audio->name, input_strs[aud_input->audio_type - 1],
634 sizeof(audio->name));
635 audio->index = index;
636 audio->capability = V4L2_AUDCAP_STEREO;
637 return 0;
638}
diff --git a/drivers/media/pci/cx18/cx18-cards.h b/drivers/media/pci/cx18/cx18-cards.h
new file mode 100644
index 000000000000..add7391ecaba
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-cards.h
@@ -0,0 +1,157 @@
1/*
2 * cx18 functions to query card hardware
3 *
4 * Derived from ivtv-cards.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24/* hardware flags */
25#define CX18_HW_TUNER (1 << 0)
26#define CX18_HW_TVEEPROM (1 << 1)
27#define CX18_HW_CS5345 (1 << 2)
28#define CX18_HW_DVB (1 << 3)
29#define CX18_HW_418_AV (1 << 4)
30#define CX18_HW_GPIO_MUX (1 << 5)
31#define CX18_HW_GPIO_RESET_CTRL (1 << 6)
32#define CX18_HW_Z8F0811_IR_TX_HAUP (1 << 7)
33#define CX18_HW_Z8F0811_IR_RX_HAUP (1 << 8)
34#define CX18_HW_Z8F0811_IR_HAUP (CX18_HW_Z8F0811_IR_RX_HAUP | \
35 CX18_HW_Z8F0811_IR_TX_HAUP)
36
37#define CX18_HW_IR_ANY (CX18_HW_Z8F0811_IR_RX_HAUP | \
38 CX18_HW_Z8F0811_IR_TX_HAUP)
39
40/* video inputs */
41#define CX18_CARD_INPUT_VID_TUNER 1
42#define CX18_CARD_INPUT_SVIDEO1 2
43#define CX18_CARD_INPUT_SVIDEO2 3
44#define CX18_CARD_INPUT_COMPOSITE1 4
45#define CX18_CARD_INPUT_COMPOSITE2 5
46#define CX18_CARD_INPUT_COMPONENT1 6
47
48/* audio inputs */
49#define CX18_CARD_INPUT_AUD_TUNER 1
50#define CX18_CARD_INPUT_LINE_IN1 2
51#define CX18_CARD_INPUT_LINE_IN2 3
52
53#define CX18_CARD_MAX_VIDEO_INPUTS 6
54#define CX18_CARD_MAX_AUDIO_INPUTS 3
55#define CX18_CARD_MAX_TUNERS 2
56
57/* V4L2 capability aliases */
58#define CX18_CAP_ENCODER (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER | \
59 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | \
60 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)
61
62struct cx18_card_video_input {
63 u8 video_type; /* video input type */
64 u8 audio_index; /* index in cx18_card_audio_input array */
65 u32 video_input; /* hardware video input */
66};
67
68struct cx18_card_audio_input {
69 u8 audio_type; /* audio input type */
70 u32 audio_input; /* hardware audio input */
71 u16 muxer_input; /* hardware muxer input for boards with a
72 multiplexer chip */
73};
74
75struct cx18_card_pci_info {
76 u16 device;
77 u16 subsystem_vendor;
78 u16 subsystem_device;
79};
80
81/* GPIO definitions */
82
83/* The mask is the set of bits used by the operation */
84
85struct cx18_gpio_init { /* set initial GPIO DIR and OUT values */
86 u32 direction; /* DIR setting. Leave to 0 if no init is needed */
87 u32 initial_value;
88};
89
90struct cx18_gpio_i2c_slave_reset {
91 u32 active_lo_mask; /* GPIO outputs that reset i2c chips when low */
92 u32 active_hi_mask; /* GPIO outputs that reset i2c chips when high */
93 int msecs_asserted; /* time period reset must remain asserted */
94 int msecs_recovery; /* time after deassert for chips to be ready */
95 u32 ir_reset_mask; /* GPIO to reset the Zilog Z8F0811 IR contoller */
96};
97
98struct cx18_gpio_audio_input { /* select tuner/line in input */
99 u32 mask; /* leave to 0 if not supported */
100 u32 tuner;
101 u32 linein;
102 u32 radio;
103};
104
105struct cx18_card_tuner {
106 v4l2_std_id std; /* standard for which the tuner is suitable */
107 int tuner; /* tuner ID (from tuner.h) */
108};
109
110struct cx18_card_tuner_i2c {
111 unsigned short radio[2];/* radio tuner i2c address to probe */
112 unsigned short demod[3];/* demodulator i2c address to probe */
113 unsigned short tv[4]; /* tv tuner i2c addresses to probe */
114};
115
116struct cx18_ddr { /* DDR config data */
117 u32 chip_config;
118 u32 refresh;
119 u32 timing1;
120 u32 timing2;
121 u32 tune_lane;
122 u32 initial_emrs;
123};
124
125/* for card information/parameters */
126struct cx18_card {
127 int type;
128 char *name;
129 char *comment;
130 u32 v4l2_capabilities;
131 u32 hw_audio_ctrl; /* hardware used for the V4L2 controls (only
132 1 dev allowed currently) */
133 u32 hw_muxer; /* hardware used to multiplex audio input */
134 u32 hw_all; /* all hardware used by the board */
135 struct cx18_card_video_input video_inputs[CX18_CARD_MAX_VIDEO_INPUTS];
136 struct cx18_card_audio_input audio_inputs[CX18_CARD_MAX_AUDIO_INPUTS];
137 struct cx18_card_audio_input radio_input;
138
139 /* GPIO card-specific settings */
140 u8 xceive_pin; /* XCeive tuner GPIO reset pin */
141 struct cx18_gpio_init gpio_init;
142 struct cx18_gpio_i2c_slave_reset gpio_i2c_slave_reset;
143 struct cx18_gpio_audio_input gpio_audio_input;
144
145 struct cx18_card_tuner tuners[CX18_CARD_MAX_TUNERS];
146 struct cx18_card_tuner_i2c *i2c;
147
148 struct cx18_ddr ddr;
149
150 /* list of device and subsystem vendor/devices that
151 correspond to this card type. */
152 const struct cx18_card_pci_info *pci_list;
153};
154
155int cx18_get_input(struct cx18 *cx, u16 index, struct v4l2_input *input);
156int cx18_get_audio_input(struct cx18 *cx, u16 index, struct v4l2_audio *input);
157const struct cx18_card *cx18_get_card(u16 index);
diff --git a/drivers/media/pci/cx18/cx18-controls.c b/drivers/media/pci/cx18/cx18-controls.c
new file mode 100644
index 000000000000..282a3d29fdaa
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-controls.c
@@ -0,0 +1,131 @@
1/*
2 * cx18 ioctl control functions
3 *
4 * Derived from ivtv-controls.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23#include <linux/kernel.h>
24#include <linux/slab.h>
25
26#include "cx18-driver.h"
27#include "cx18-cards.h"
28#include "cx18-ioctl.h"
29#include "cx18-audio.h"
30#include "cx18-mailbox.h"
31#include "cx18-controls.h"
32
33static int cx18_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
34{
35 struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
36 int type = cxhdl->stream_type->val;
37
38 if (atomic_read(&cx->ana_capturing) > 0)
39 return -EBUSY;
40
41 if (fmt != V4L2_MPEG_STREAM_VBI_FMT_IVTV ||
42 !(type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS ||
43 type == V4L2_MPEG_STREAM_TYPE_MPEG2_DVD ||
44 type == V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD)) {
45 /* Only IVTV fmt VBI insertion & only MPEG-2 PS type streams */
46 cx->vbi.insert_mpeg = V4L2_MPEG_STREAM_VBI_FMT_NONE;
47 CX18_DEBUG_INFO("disabled insertion of sliced VBI data into "
48 "the MPEG stream\n");
49 return 0;
50 }
51
52 /* Allocate sliced VBI buffers if needed. */
53 if (cx->vbi.sliced_mpeg_data[0] == NULL) {
54 int i;
55
56 for (i = 0; i < CX18_VBI_FRAMES; i++) {
57 cx->vbi.sliced_mpeg_data[i] =
58 kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
59 if (cx->vbi.sliced_mpeg_data[i] == NULL) {
60 while (--i >= 0) {
61 kfree(cx->vbi.sliced_mpeg_data[i]);
62 cx->vbi.sliced_mpeg_data[i] = NULL;
63 }
64 cx->vbi.insert_mpeg =
65 V4L2_MPEG_STREAM_VBI_FMT_NONE;
66 CX18_WARN("Unable to allocate buffers for "
67 "sliced VBI data insertion\n");
68 return -ENOMEM;
69 }
70 }
71 }
72
73 cx->vbi.insert_mpeg = fmt;
74 CX18_DEBUG_INFO("enabled insertion of sliced VBI data into the MPEG PS,"
75 "when sliced VBI is enabled\n");
76
77 /*
78 * If our current settings have no lines set for capture, store a valid,
79 * default set of service lines to capture, in our current settings.
80 */
81 if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
82 if (cx->is_60hz)
83 cx->vbi.sliced_in->service_set =
84 V4L2_SLICED_CAPTION_525;
85 else
86 cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
87 cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
88 }
89 return 0;
90}
91
92static int cx18_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
93{
94 struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
95 int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
96 struct v4l2_mbus_framefmt fmt;
97
98 /* fix videodecoder resolution */
99 fmt.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
100 fmt.height = cxhdl->height;
101 fmt.code = V4L2_MBUS_FMT_FIXED;
102 v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &fmt);
103 return 0;
104}
105
106static int cx18_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
107{
108 static const u32 freqs[3] = { 44100, 48000, 32000 };
109 struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
110
111 /* The audio clock of the digitizer must match the codec sample
112 rate otherwise you get some very strange effects. */
113 if (idx < ARRAY_SIZE(freqs))
114 cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
115 return 0;
116}
117
118static int cx18_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
119{
120 struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
121
122 cx->dualwatch_stereo_mode = val;
123 return 0;
124}
125
126struct cx2341x_handler_ops cx18_cxhdl_ops = {
127 .s_audio_mode = cx18_s_audio_mode,
128 .s_audio_sampling_freq = cx18_s_audio_sampling_freq,
129 .s_video_encoding = cx18_s_video_encoding,
130 .s_stream_vbi_fmt = cx18_s_stream_vbi_fmt,
131};
diff --git a/drivers/media/pci/cx18/cx18-controls.h b/drivers/media/pci/cx18/cx18-controls.h
new file mode 100644
index 000000000000..cb5dfc7b2054
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-controls.h
@@ -0,0 +1,24 @@
1/*
2 * cx18 ioctl control functions
3 *
4 * Derived from ivtv-controls.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24extern struct cx2341x_handler_ops cx18_cxhdl_ops;
diff --git a/drivers/media/pci/cx18/cx18-driver.c b/drivers/media/pci/cx18/cx18-driver.c
new file mode 100644
index 000000000000..c67733d32c8a
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-driver.c
@@ -0,0 +1,1360 @@
1/*
2 * cx18 driver initialization and card probing
3 *
4 * Derived from ivtv-driver.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-io.h"
27#include "cx18-version.h"
28#include "cx18-cards.h"
29#include "cx18-i2c.h"
30#include "cx18-irq.h"
31#include "cx18-gpio.h"
32#include "cx18-firmware.h"
33#include "cx18-queue.h"
34#include "cx18-streams.h"
35#include "cx18-av-core.h"
36#include "cx18-scb.h"
37#include "cx18-mailbox.h"
38#include "cx18-ioctl.h"
39#include "cx18-controls.h"
40#include "tuner-xc2028.h"
41#include <linux/dma-mapping.h>
42#include <media/tveeprom.h>
43
44/* If you have already X v4l cards, then set this to X. This way
45 the device numbers stay matched. Example: you have a WinTV card
46 without radio and a Compro H900 with. Normally this would give a
47 video1 device together with a radio0 device for the Compro. By
48 setting this to 1 you ensure that radio0 is now also radio1. */
49int cx18_first_minor;
50
51/* Callback for registering extensions */
52int (*cx18_ext_init)(struct cx18 *);
53EXPORT_SYMBOL(cx18_ext_init);
54
55/* add your revision and whatnot here */
56static struct pci_device_id cx18_pci_tbl[] __devinitdata = {
57 {PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418,
58 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
59 {0,}
60};
61
62MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);
63
64static atomic_t cx18_instance = ATOMIC_INIT(0);
65
66/* Parameter declarations */
67static int cardtype[CX18_MAX_CARDS];
68static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1 };
72static int radio[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1 };
76static unsigned cardtype_c = 1;
77static unsigned tuner_c = 1;
78static unsigned radio_c = 1;
79static char pal[] = "--";
80static char secam[] = "--";
81static char ntsc[] = "-";
82
83/* Buffers */
84static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS;
85static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS;
86static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS;
87static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS;
88static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS;
89static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS;
90
91static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
92static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
93static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
94static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
95static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;
96
97static int enc_ts_bufs = -1;
98static int enc_mpg_bufs = -1;
99static int enc_idx_bufs = CX18_MAX_FW_MDLS_PER_STREAM;
100static int enc_yuv_bufs = -1;
101static int enc_vbi_bufs = -1;
102static int enc_pcm_bufs = -1;
103
104
105static int cx18_pci_latency = 1;
106
107static int mmio_ndelay;
108static int retry_mmio = 1;
109
110int cx18_debug;
111
112module_param_array(tuner, int, &tuner_c, 0644);
113module_param_array(radio, int, &radio_c, 0644);
114module_param_array(cardtype, int, &cardtype_c, 0644);
115module_param_string(pal, pal, sizeof(pal), 0644);
116module_param_string(secam, secam, sizeof(secam), 0644);
117module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
118module_param_named(debug, cx18_debug, int, 0644);
119module_param(mmio_ndelay, int, 0644);
120module_param(retry_mmio, int, 0644);
121module_param(cx18_pci_latency, int, 0644);
122module_param(cx18_first_minor, int, 0644);
123
124module_param(enc_ts_buffers, int, 0644);
125module_param(enc_mpg_buffers, int, 0644);
126module_param(enc_idx_buffers, int, 0644);
127module_param(enc_yuv_buffers, int, 0644);
128module_param(enc_vbi_buffers, int, 0644);
129module_param(enc_pcm_buffers, int, 0644);
130
131module_param(enc_ts_bufsize, int, 0644);
132module_param(enc_mpg_bufsize, int, 0644);
133module_param(enc_idx_bufsize, int, 0644);
134module_param(enc_yuv_bufsize, int, 0644);
135module_param(enc_pcm_bufsize, int, 0644);
136
137module_param(enc_ts_bufs, int, 0644);
138module_param(enc_mpg_bufs, int, 0644);
139module_param(enc_idx_bufs, int, 0644);
140module_param(enc_yuv_bufs, int, 0644);
141module_param(enc_vbi_bufs, int, 0644);
142module_param(enc_pcm_bufs, int, 0644);
143
144MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
145 "\t\t\tsee tuner.h for values");
146MODULE_PARM_DESC(radio,
147 "Enable or disable the radio. Use only if autodetection\n"
148 "\t\t\tfails. 0 = disable, 1 = enable");
149MODULE_PARM_DESC(cardtype,
150 "Only use this option if your card is not detected properly.\n"
151 "\t\tSpecify card type:\n"
152 "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n"
153 "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n"
154 "\t\t\t 3 = Compro VideoMate H900\n"
155 "\t\t\t 4 = Yuan MPC718\n"
156 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
157 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
158 "\t\t\t 7 = Leadtek WinFast PVR2100\n"
159 "\t\t\t 8 = Leadtek WinFast DVR3100 H\n"
160 "\t\t\t 9 = GoTView PCI DVD3 Hybrid\n"
161 "\t\t\t 10 = Hauppauge HVR 1600 (S5H1411)\n"
162 "\t\t\t 0 = Autodetect (default)\n"
163 "\t\t\t-1 = Ignore this card\n\t\t");
164MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
165MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC");
166MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K");
167MODULE_PARM_DESC(debug,
168 "Debug level (bitmask). Default: 0\n"
169 "\t\t\t 1/0x0001: warning\n"
170 "\t\t\t 2/0x0002: info\n"
171 "\t\t\t 4/0x0004: mailbox\n"
172 "\t\t\t 8/0x0008: dma\n"
173 "\t\t\t 16/0x0010: ioctl\n"
174 "\t\t\t 32/0x0020: file\n"
175 "\t\t\t 64/0x0040: i2c\n"
176 "\t\t\t128/0x0080: irq\n"
177 "\t\t\t256/0x0100: high volume\n");
178MODULE_PARM_DESC(cx18_pci_latency,
179 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
180 "\t\t\tDefault: Yes");
181MODULE_PARM_DESC(retry_mmio,
182 "(Deprecated) MMIO writes are now always checked and retried\n"
183 "\t\t\tEffectively: 1 [Yes]");
184MODULE_PARM_DESC(mmio_ndelay,
185 "(Deprecated) MMIO accesses are now never purposely delayed\n"
186 "\t\t\tEffectively: 0 ns");
187MODULE_PARM_DESC(enc_ts_buffers,
188 "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n"
189 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS));
190MODULE_PARM_DESC(enc_ts_bufsize,
191 "Size of an encoder TS buffer (kB)\n"
192 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE));
193MODULE_PARM_DESC(enc_ts_bufs,
194 "Number of encoder TS buffers\n"
195 "\t\t\tDefault is computed from other enc_ts_* parameters");
196MODULE_PARM_DESC(enc_mpg_buffers,
197 "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n"
198 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS));
199MODULE_PARM_DESC(enc_mpg_bufsize,
200 "Size of an encoder MPG buffer (kB)\n"
201 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE));
202MODULE_PARM_DESC(enc_mpg_bufs,
203 "Number of encoder MPG buffers\n"
204 "\t\t\tDefault is computed from other enc_mpg_* parameters");
205MODULE_PARM_DESC(enc_idx_buffers,
206 "(Deprecated) Encoder IDX buffer memory (MB)\n"
207 "\t\t\tIgnored, except 0 disables IDX buffer allocations\n"
208 "\t\t\tDefault: 1 [Enabled]");
209MODULE_PARM_DESC(enc_idx_bufsize,
210 "Size of an encoder IDX buffer (kB)\n"
211 "\t\t\tAllowed values are multiples of 1.5 kB rounded up\n"
212 "\t\t\t(multiples of size required for 64 index entries)\n"
213 "\t\t\tDefault: 2");
214MODULE_PARM_DESC(enc_idx_bufs,
215 "Number of encoder IDX buffers\n"
216 "\t\t\tDefault: " __stringify(CX18_MAX_FW_MDLS_PER_STREAM));
217MODULE_PARM_DESC(enc_yuv_buffers,
218 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
219 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
220MODULE_PARM_DESC(enc_yuv_bufsize,
221 "Size of an encoder YUV buffer (kB)\n"
222 "\t\t\tAllowed values are multiples of 33.75 kB rounded up\n"
223 "\t\t\t(multiples of size required for 32 screen lines)\n"
224 "\t\t\tDefault: 102");
225MODULE_PARM_DESC(enc_yuv_bufs,
226 "Number of encoder YUV buffers\n"
227 "\t\t\tDefault is computed from other enc_yuv_* parameters");
228MODULE_PARM_DESC(enc_vbi_buffers,
229 "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n"
230 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
231MODULE_PARM_DESC(enc_vbi_bufs,
232 "Number of encoder VBI buffers\n"
233 "\t\t\tDefault is computed from enc_vbi_buffers");
234MODULE_PARM_DESC(enc_pcm_buffers,
235 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
236 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
237MODULE_PARM_DESC(enc_pcm_bufsize,
238 "Size of an encoder PCM buffer (kB)\n"
239 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE));
240MODULE_PARM_DESC(enc_pcm_bufs,
241 "Number of encoder PCM buffers\n"
242 "\t\t\tDefault is computed from other enc_pcm_* parameters");
243
244MODULE_PARM_DESC(cx18_first_minor,
245 "Set device node number assigned to first card");
246
247MODULE_AUTHOR("Hans Verkuil");
248MODULE_DESCRIPTION("CX23418 driver");
249MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
250MODULE_LICENSE("GPL");
251
252MODULE_VERSION(CX18_VERSION);
253
254#if defined(CONFIG_MODULES) && defined(MODULE)
255static void request_module_async(struct work_struct *work)
256{
257 struct cx18 *dev = container_of(work, struct cx18, request_module_wk);
258
259 /* Make sure cx18-alsa module is loaded */
260 request_module("cx18-alsa");
261
262 /* Initialize cx18-alsa for this instance of the cx18 device */
263 if (cx18_ext_init != NULL)
264 cx18_ext_init(dev);
265}
266
267static void request_modules(struct cx18 *dev)
268{
269 INIT_WORK(&dev->request_module_wk, request_module_async);
270 schedule_work(&dev->request_module_wk);
271}
272
273static void flush_request_modules(struct cx18 *dev)
274{
275 flush_work_sync(&dev->request_module_wk);
276}
277#else
278#define request_modules(dev)
279#define flush_request_modules(dev)
280#endif /* CONFIG_MODULES */
281
282/* Generic utility functions */
283int cx18_msleep_timeout(unsigned int msecs, int intr)
284{
285 long int timeout = msecs_to_jiffies(msecs);
286 int sig;
287
288 do {
289 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
290 timeout = schedule_timeout(timeout);
291 sig = intr ? signal_pending(current) : 0;
292 } while (!sig && timeout);
293 return sig;
294}
295
296/* Release ioremapped memory */
297static void cx18_iounmap(struct cx18 *cx)
298{
299 if (cx == NULL)
300 return;
301
302 /* Release io memory */
303 if (cx->enc_mem != NULL) {
304 CX18_DEBUG_INFO("releasing enc_mem\n");
305 iounmap(cx->enc_mem);
306 cx->enc_mem = NULL;
307 }
308}
309
310static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len)
311{
312 int i;
313
314 CX18_INFO("eeprom dump:\n");
315 for (i = 0; i < len; i++) {
316 if (0 == (i % 16))
317 CX18_INFO("eeprom %02x:", i);
318 printk(KERN_CONT " %02x", eedata[i]);
319 if (15 == (i % 16))
320 printk(KERN_CONT "\n");
321 }
322}
323
324/* Hauppauge card? get values from tveeprom */
325void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
326{
327 struct i2c_client c;
328 u8 eedata[256];
329
330 memset(&c, 0, sizeof(c));
331 strlcpy(c.name, "cx18 tveeprom tmp", sizeof(c.name));
332 c.adapter = &cx->i2c_adap[0];
333 c.addr = 0xA0 >> 1;
334
335 memset(tv, 0, sizeof(*tv));
336 if (tveeprom_read(&c, eedata, sizeof(eedata)))
337 return;
338
339 switch (cx->card->type) {
340 case CX18_CARD_HVR_1600_ESMT:
341 case CX18_CARD_HVR_1600_SAMSUNG:
342 case CX18_CARD_HVR_1600_S5H1411:
343 tveeprom_hauppauge_analog(&c, tv, eedata);
344 break;
345 case CX18_CARD_YUAN_MPC718:
346 case CX18_CARD_GOTVIEW_PCI_DVD3:
347 tv->model = 0x718;
348 cx18_eeprom_dump(cx, eedata, sizeof(eedata));
349 CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n",
350 eedata[2], eedata[1], eedata[4], eedata[3]);
351 break;
352 default:
353 tv->model = 0xffffffff;
354 cx18_eeprom_dump(cx, eedata, sizeof(eedata));
355 break;
356 }
357}
358
359static void cx18_process_eeprom(struct cx18 *cx)
360{
361 struct tveeprom tv;
362
363 cx18_read_eeprom(cx, &tv);
364
365 /* Many thanks to Steven Toth from Hauppauge for providing the
366 model numbers */
367 /* Note: the Samsung memory models cannot be reliably determined
368 from the model number. Use the cardtype module option if you
369 have one of these preproduction models. */
370 switch (tv.model) {
371 case 74301: /* Retail models */
372 case 74321:
373 case 74351: /* OEM models */
374 case 74361:
375 /* Digital side is s5h1411/tda18271 */
376 cx->card = cx18_get_card(CX18_CARD_HVR_1600_S5H1411);
377 break;
378 case 74021: /* Retail models */
379 case 74031:
380 case 74041:
381 case 74141:
382 case 74541: /* OEM models */
383 case 74551:
384 case 74591:
385 case 74651:
386 case 74691:
387 case 74751:
388 case 74891:
389 /* Digital side is s5h1409/mxl5005s */
390 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
391 break;
392 case 0x718:
393 return;
394 case 0xffffffff:
395 CX18_INFO("Unknown EEPROM encoding\n");
396 return;
397 case 0:
398 CX18_ERR("Invalid EEPROM\n");
399 return;
400 default:
401 CX18_ERR("Unknown model %d, defaulting to original HVR-1600 "
402 "(cardtype=1)\n", tv.model);
403 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
404 break;
405 }
406
407 cx->v4l2_cap = cx->card->v4l2_capabilities;
408 cx->card_name = cx->card->name;
409 cx->card_i2c = cx->card->i2c;
410
411 CX18_INFO("Autodetected %s\n", cx->card_name);
412
413 if (tv.tuner_type == TUNER_ABSENT)
414 CX18_ERR("tveeprom cannot autodetect tuner!\n");
415
416 if (cx->options.tuner == -1)
417 cx->options.tuner = tv.tuner_type;
418 if (cx->options.radio == -1)
419 cx->options.radio = (tv.has_radio != 0);
420
421 if (cx->std != 0)
422 /* user specified tuner standard */
423 return;
424
425 /* autodetect tuner standard */
426#define TVEEPROM_TUNER_FORMAT_ALL (V4L2_STD_B | V4L2_STD_GH | \
427 V4L2_STD_MN | \
428 V4L2_STD_PAL_I | \
429 V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC | \
430 V4L2_STD_DK)
431 if ((tv.tuner_formats & TVEEPROM_TUNER_FORMAT_ALL)
432 == TVEEPROM_TUNER_FORMAT_ALL) {
433 CX18_DEBUG_INFO("Worldwide tuner detected\n");
434 cx->std = V4L2_STD_ALL;
435 } else if (tv.tuner_formats & V4L2_STD_PAL) {
436 CX18_DEBUG_INFO("PAL tuner detected\n");
437 cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
438 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
439 CX18_DEBUG_INFO("NTSC tuner detected\n");
440 cx->std |= V4L2_STD_NTSC_M;
441 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
442 CX18_DEBUG_INFO("SECAM tuner detected\n");
443 cx->std |= V4L2_STD_SECAM_L;
444 } else {
445 CX18_INFO("No tuner detected, default to NTSC-M\n");
446 cx->std |= V4L2_STD_NTSC_M;
447 }
448}
449
450static v4l2_std_id cx18_parse_std(struct cx18 *cx)
451{
452 switch (pal[0]) {
453 case '6':
454 return V4L2_STD_PAL_60;
455 case 'b':
456 case 'B':
457 case 'g':
458 case 'G':
459 return V4L2_STD_PAL_BG;
460 case 'h':
461 case 'H':
462 return V4L2_STD_PAL_H;
463 case 'n':
464 case 'N':
465 if (pal[1] == 'c' || pal[1] == 'C')
466 return V4L2_STD_PAL_Nc;
467 return V4L2_STD_PAL_N;
468 case 'i':
469 case 'I':
470 return V4L2_STD_PAL_I;
471 case 'd':
472 case 'D':
473 case 'k':
474 case 'K':
475 return V4L2_STD_PAL_DK;
476 case 'M':
477 case 'm':
478 return V4L2_STD_PAL_M;
479 case '-':
480 break;
481 default:
482 CX18_WARN("pal= argument not recognised\n");
483 return 0;
484 }
485
486 switch (secam[0]) {
487 case 'b':
488 case 'B':
489 case 'g':
490 case 'G':
491 case 'h':
492 case 'H':
493 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
494 case 'd':
495 case 'D':
496 case 'k':
497 case 'K':
498 return V4L2_STD_SECAM_DK;
499 case 'l':
500 case 'L':
501 if (secam[1] == 'C' || secam[1] == 'c')
502 return V4L2_STD_SECAM_LC;
503 return V4L2_STD_SECAM_L;
504 case '-':
505 break;
506 default:
507 CX18_WARN("secam= argument not recognised\n");
508 return 0;
509 }
510
511 switch (ntsc[0]) {
512 case 'm':
513 case 'M':
514 return V4L2_STD_NTSC_M;
515 case 'j':
516 case 'J':
517 return V4L2_STD_NTSC_M_JP;
518 case 'k':
519 case 'K':
520 return V4L2_STD_NTSC_M_KR;
521 case '-':
522 break;
523 default:
524 CX18_WARN("ntsc= argument not recognised\n");
525 return 0;
526 }
527
528 /* no match found */
529 return 0;
530}
531
532static void cx18_process_options(struct cx18 *cx)
533{
534 int i, j;
535
536 cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers;
537 cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers;
538 cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers;
539 cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers;
540 cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers;
541 cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
542 cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */
543
544 cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs;
545 cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs;
546 cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs;
547 cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs;
548 cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs;
549 cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs;
550 cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */
551
552 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize;
553 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
554 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
555 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
556 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36;
557 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
558 cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
559
560 /* Ensure stream_buffers & stream_buf_size are valid */
561 for (i = 0; i < CX18_MAX_STREAMS; i++) {
562 if (cx->stream_buffers[i] == 0 || /* User said 0 buffers */
563 cx->options.megabytes[i] <= 0 || /* User said 0 MB total */
564 cx->stream_buf_size[i] <= 0) { /* User said buf size 0 */
565 cx->options.megabytes[i] = 0;
566 cx->stream_buffers[i] = 0;
567 cx->stream_buf_size[i] = 0;
568 continue;
569 }
570 /*
571 * YUV is a special case where the stream_buf_size needs to be
572 * an integral multiple of 33.75 kB (storage for 32 screens
573 * lines to maintain alignment in case of lost buffers).
574 *
575 * IDX is a special case where the stream_buf_size should be
576 * an integral multiple of 1.5 kB (storage for 64 index entries
577 * to maintain alignment in case of lost buffers).
578 *
579 */
580 if (i == CX18_ENC_STREAM_TYPE_YUV) {
581 cx->stream_buf_size[i] *= 1024;
582 cx->stream_buf_size[i] -=
583 (cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE);
584
585 if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE)
586 cx->stream_buf_size[i] =
587 CX18_UNIT_ENC_YUV_BUFSIZE;
588 } else if (i == CX18_ENC_STREAM_TYPE_IDX) {
589 cx->stream_buf_size[i] *= 1024;
590 cx->stream_buf_size[i] -=
591 (cx->stream_buf_size[i] % CX18_UNIT_ENC_IDX_BUFSIZE);
592
593 if (cx->stream_buf_size[i] < CX18_UNIT_ENC_IDX_BUFSIZE)
594 cx->stream_buf_size[i] =
595 CX18_UNIT_ENC_IDX_BUFSIZE;
596 }
597 /*
598 * YUV and IDX are special cases where the stream_buf_size is
599 * now in bytes.
600 * VBI is a special case where the stream_buf_size is fixed
601 * and already in bytes
602 */
603 if (i == CX18_ENC_STREAM_TYPE_VBI ||
604 i == CX18_ENC_STREAM_TYPE_YUV ||
605 i == CX18_ENC_STREAM_TYPE_IDX) {
606 if (cx->stream_buffers[i] < 0) {
607 cx->stream_buffers[i] =
608 cx->options.megabytes[i] * 1024 * 1024
609 / cx->stream_buf_size[i];
610 } else {
611 /* N.B. This might round down to 0 */
612 cx->options.megabytes[i] =
613 cx->stream_buffers[i]
614 * cx->stream_buf_size[i]/(1024 * 1024);
615 }
616 } else {
617 /* All other streams have stream_buf_size in kB here */
618 if (cx->stream_buffers[i] < 0) {
619 cx->stream_buffers[i] =
620 cx->options.megabytes[i] * 1024
621 / cx->stream_buf_size[i];
622 } else {
623 /* N.B. This might round down to 0 */
624 cx->options.megabytes[i] =
625 cx->stream_buffers[i]
626 * cx->stream_buf_size[i] / 1024;
627 }
628 /* convert from kB to bytes */
629 cx->stream_buf_size[i] *= 1024;
630 }
631 CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, "
632 "%d bytes\n", i, cx->options.megabytes[i],
633 cx->stream_buffers[i], cx->stream_buf_size[i]);
634 }
635
636 cx->options.cardtype = cardtype[cx->instance];
637 cx->options.tuner = tuner[cx->instance];
638 cx->options.radio = radio[cx->instance];
639
640 cx->std = cx18_parse_std(cx);
641 if (cx->options.cardtype == -1) {
642 CX18_INFO("Ignore card\n");
643 return;
644 }
645 cx->card = cx18_get_card(cx->options.cardtype - 1);
646 if (cx->card)
647 CX18_INFO("User specified %s card\n", cx->card->name);
648 else if (cx->options.cardtype != 0)
649 CX18_ERR("Unknown user specified type, trying to autodetect card\n");
650 if (cx->card == NULL) {
651 if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) {
652 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
653 CX18_INFO("Autodetected Hauppauge card\n");
654 }
655 }
656 if (cx->card == NULL) {
657 for (i = 0; (cx->card = cx18_get_card(i)); i++) {
658 if (cx->card->pci_list == NULL)
659 continue;
660 for (j = 0; cx->card->pci_list[j].device; j++) {
661 if (cx->pci_dev->device !=
662 cx->card->pci_list[j].device)
663 continue;
664 if (cx->pci_dev->subsystem_vendor !=
665 cx->card->pci_list[j].subsystem_vendor)
666 continue;
667 if (cx->pci_dev->subsystem_device !=
668 cx->card->pci_list[j].subsystem_device)
669 continue;
670 CX18_INFO("Autodetected %s card\n", cx->card->name);
671 goto done;
672 }
673 }
674 }
675done:
676
677 if (cx->card == NULL) {
678 cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
679 CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
680 cx->pci_dev->vendor, cx->pci_dev->device);
681 CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n",
682 cx->pci_dev->subsystem_vendor,
683 cx->pci_dev->subsystem_device);
684 CX18_ERR("Defaulting to %s card\n", cx->card->name);
685 CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
686 CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
687 CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n");
688 }
689 cx->v4l2_cap = cx->card->v4l2_capabilities;
690 cx->card_name = cx->card->name;
691 cx->card_i2c = cx->card->i2c;
692}
693
694static int __devinit cx18_create_in_workq(struct cx18 *cx)
695{
696 snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in",
697 cx->v4l2_dev.name);
698 cx->in_work_queue = alloc_ordered_workqueue(cx->in_workq_name, 0);
699 if (cx->in_work_queue == NULL) {
700 CX18_ERR("Unable to create incoming mailbox handler thread\n");
701 return -ENOMEM;
702 }
703 return 0;
704}
705
706static void __devinit cx18_init_in_work_orders(struct cx18 *cx)
707{
708 int i;
709 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
710 cx->in_work_order[i].cx = cx;
711 cx->in_work_order[i].str = cx->epu_debug_str;
712 INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler);
713 }
714}
715
716/* Precondition: the cx18 structure has been memset to 0. Only
717 the dev and instance fields have been filled in.
718 No assumptions on the card type may be made here (see cx18_init_struct2
719 for that).
720 */
721static int __devinit cx18_init_struct1(struct cx18 *cx)
722{
723 int ret;
724
725 cx->base_addr = pci_resource_start(cx->pci_dev, 0);
726
727 mutex_init(&cx->serialize_lock);
728 mutex_init(&cx->gpio_lock);
729 mutex_init(&cx->epu2apu_mb_lock);
730 mutex_init(&cx->epu2cpu_mb_lock);
731
732 ret = cx18_create_in_workq(cx);
733 if (ret)
734 return ret;
735
736 cx18_init_in_work_orders(cx);
737
738 /* start counting open_id at 1 */
739 cx->open_id = 1;
740
741 /* Initial settings */
742 cx->cxhdl.port = CX2341X_PORT_MEMORY;
743 cx->cxhdl.capabilities = CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI;
744 cx->cxhdl.ops = &cx18_cxhdl_ops;
745 cx->cxhdl.func = cx18_api_func;
746 cx->cxhdl.priv = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
747 ret = cx2341x_handler_init(&cx->cxhdl, 50);
748 if (ret)
749 return ret;
750 cx->v4l2_dev.ctrl_handler = &cx->cxhdl.hdl;
751
752 cx->temporal_strength = cx->cxhdl.video_temporal_filter->cur.val;
753 cx->spatial_strength = cx->cxhdl.video_spatial_filter->cur.val;
754 cx->filter_mode = cx->cxhdl.video_spatial_filter_mode->cur.val |
755 (cx->cxhdl.video_temporal_filter_mode->cur.val << 1) |
756 (cx->cxhdl.video_median_filter_type->cur.val << 2);
757
758 init_waitqueue_head(&cx->cap_w);
759 init_waitqueue_head(&cx->mb_apu_waitq);
760 init_waitqueue_head(&cx->mb_cpu_waitq);
761 init_waitqueue_head(&cx->dma_waitq);
762
763 /* VBI */
764 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
765 cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
766
767 /* IVTV style VBI insertion into MPEG streams */
768 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list);
769 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list);
770 INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list);
771 list_add(&cx->vbi.sliced_mpeg_buf.list,
772 &cx->vbi.sliced_mpeg_mdl.buf_list);
773 return 0;
774}
775
776/* Second initialization part. Here the card type has been
777 autodetected. */
778static void __devinit cx18_init_struct2(struct cx18 *cx)
779{
780 int i;
781
782 for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++)
783 if (cx->card->video_inputs[i].video_type == 0)
784 break;
785 cx->nof_inputs = i;
786 for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++)
787 if (cx->card->audio_inputs[i].audio_type == 0)
788 break;
789 cx->nof_audio_inputs = i;
790
791 /* Find tuner input */
792 for (i = 0; i < cx->nof_inputs; i++) {
793 if (cx->card->video_inputs[i].video_type ==
794 CX18_CARD_INPUT_VID_TUNER)
795 break;
796 }
797 if (i == cx->nof_inputs)
798 i = 0;
799 cx->active_input = i;
800 cx->audio_input = cx->card->video_inputs[i].audio_index;
801}
802
803static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
804 const struct pci_device_id *pci_id)
805{
806 u16 cmd;
807 unsigned char pci_latency;
808
809 CX18_DEBUG_INFO("Enabling pci device\n");
810
811 if (pci_enable_device(pci_dev)) {
812 CX18_ERR("Can't enable device %d!\n", cx->instance);
813 return -EIO;
814 }
815 if (pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32))) {
816 CX18_ERR("No suitable DMA available, card %d\n", cx->instance);
817 return -EIO;
818 }
819 if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
820 CX18_ERR("Cannot request encoder memory region, card %d\n",
821 cx->instance);
822 return -EIO;
823 }
824
825 /* Enable bus mastering and memory mapped IO for the CX23418 */
826 pci_read_config_word(pci_dev, PCI_COMMAND, &cmd);
827 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
828 pci_write_config_word(pci_dev, PCI_COMMAND, cmd);
829
830 cx->card_rev = pci_dev->revision;
831 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
832
833 if (pci_latency < 64 && cx18_pci_latency) {
834 CX18_INFO("Unreasonably low latency timer, "
835 "setting to 64 (was %d)\n", pci_latency);
836 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64);
837 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency);
838 }
839
840 CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, "
841 "irq: %d, latency: %d, memory: 0x%llx\n",
842 cx->pci_dev->device, cx->card_rev, pci_dev->bus->number,
843 PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn),
844 cx->pci_dev->irq, pci_latency, (u64)cx->base_addr);
845
846 return 0;
847}
848
849static void cx18_init_subdevs(struct cx18 *cx)
850{
851 u32 hw = cx->card->hw_all;
852 u32 device;
853 int i;
854
855 for (i = 0, device = 1; i < 32; i++, device <<= 1) {
856
857 if (!(device & hw))
858 continue;
859
860 switch (device) {
861 case CX18_HW_DVB:
862 case CX18_HW_TVEEPROM:
863 /* These subordinate devices do not use probing */
864 cx->hw_flags |= device;
865 break;
866 case CX18_HW_418_AV:
867 /* The A/V decoder gets probed earlier to set PLLs */
868 /* Just note that the card uses it (i.e. has analog) */
869 cx->hw_flags |= device;
870 break;
871 case CX18_HW_GPIO_RESET_CTRL:
872 /*
873 * The Reset Controller gets probed and added to
874 * hw_flags earlier for i2c adapter/bus initialization
875 */
876 break;
877 case CX18_HW_GPIO_MUX:
878 if (cx18_gpio_register(cx, device) == 0)
879 cx->hw_flags |= device;
880 break;
881 default:
882 if (cx18_i2c_register(cx, i) == 0)
883 cx->hw_flags |= device;
884 break;
885 }
886 }
887
888 if (cx->hw_flags & CX18_HW_418_AV)
889 cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV);
890
891 if (cx->card->hw_muxer != 0)
892 cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer);
893}
894
895static int __devinit cx18_probe(struct pci_dev *pci_dev,
896 const struct pci_device_id *pci_id)
897{
898 int retval = 0;
899 int i;
900 u32 devtype;
901 struct cx18 *cx;
902
903 /* FIXME - module parameter arrays constrain max instances */
904 i = atomic_inc_return(&cx18_instance) - 1;
905 if (i >= CX18_MAX_CARDS) {
906 printk(KERN_ERR "cx18: cannot manage card %d, driver has a "
907 "limit of 0 - %d\n", i, CX18_MAX_CARDS - 1);
908 return -ENOMEM;
909 }
910
911 cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
912 if (cx == NULL) {
913 printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n",
914 i);
915 return -ENOMEM;
916 }
917 cx->pci_dev = pci_dev;
918 cx->instance = i;
919
920 retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);
921 if (retval) {
922 printk(KERN_ERR "cx18: v4l2_device_register of card %d failed"
923 "\n", cx->instance);
924 kfree(cx);
925 return retval;
926 }
927 snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d",
928 cx->instance);
929 CX18_INFO("Initializing card %d\n", cx->instance);
930
931 cx18_process_options(cx);
932 if (cx->options.cardtype == -1) {
933 retval = -ENODEV;
934 goto err;
935 }
936
937 retval = cx18_init_struct1(cx);
938 if (retval)
939 goto err;
940
941 CX18_DEBUG_INFO("base addr: 0x%llx\n", (u64)cx->base_addr);
942
943 /* PCI Device Setup */
944 retval = cx18_setup_pci(cx, pci_dev, pci_id);
945 if (retval != 0)
946 goto free_workqueues;
947
948 /* map io memory */
949 CX18_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
950 (u64)cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE);
951 cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET,
952 CX18_MEM_SIZE);
953 if (!cx->enc_mem) {
954 CX18_ERR("ioremap failed. Can't get a window into CX23418 "
955 "memory and register space\n");
956 CX18_ERR("Each capture card with a CX23418 needs 64 MB of "
957 "vmalloc address space for the window\n");
958 CX18_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
959 CX18_ERR("Use the vmalloc= kernel command line option to set "
960 "VmallocTotal to a larger value\n");
961 retval = -ENOMEM;
962 goto free_mem;
963 }
964 cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET;
965 devtype = cx18_read_reg(cx, 0xC72028);
966 switch (devtype & 0xff000000) {
967 case 0xff000000:
968 CX18_INFO("cx23418 revision %08x (A)\n", devtype);
969 break;
970 case 0x01000000:
971 CX18_INFO("cx23418 revision %08x (B)\n", devtype);
972 break;
973 default:
974 CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype);
975 break;
976 }
977
978 cx18_init_power(cx, 1);
979 cx18_init_memory(cx);
980
981 cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET);
982 cx18_init_scb(cx);
983
984 cx18_gpio_init(cx);
985
986 /* Initialize integrated A/V decoder early to set PLLs, just in case */
987 retval = cx18_av_probe(cx);
988 if (retval) {
989 CX18_ERR("Could not register A/V decoder subdevice\n");
990 goto free_map;
991 }
992
993 /* Initialize GPIO Reset Controller to do chip resets during i2c init */
994 if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) {
995 if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0)
996 CX18_WARN("Could not register GPIO reset controller"
997 "subdevice; proceeding anyway.\n");
998 else
999 cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL;
1000 }
1001
1002 /* active i2c */
1003 CX18_DEBUG_INFO("activating i2c...\n");
1004 retval = init_cx18_i2c(cx);
1005 if (retval) {
1006 CX18_ERR("Could not initialize i2c\n");
1007 goto free_map;
1008 }
1009
1010 if (cx->card->hw_all & CX18_HW_TVEEPROM) {
1011 /* Based on the model number the cardtype may be changed.
1012 The PCI IDs are not always reliable. */
1013 const struct cx18_card *orig_card = cx->card;
1014 cx18_process_eeprom(cx);
1015
1016 if (cx->card != orig_card) {
1017 /* Changed the cardtype; re-reset the I2C chips */
1018 cx18_gpio_init(cx);
1019 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL,
1020 core, reset, (u32) CX18_GPIO_RESET_I2C);
1021 }
1022 }
1023 if (cx->card->comment)
1024 CX18_INFO("%s", cx->card->comment);
1025 if (cx->card->v4l2_capabilities == 0) {
1026 retval = -ENODEV;
1027 goto free_i2c;
1028 }
1029 cx18_init_memory(cx);
1030 cx18_init_scb(cx);
1031
1032 /* Register IRQ */
1033 retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
1034 IRQF_SHARED | IRQF_DISABLED,
1035 cx->v4l2_dev.name, (void *)cx);
1036 if (retval) {
1037 CX18_ERR("Failed to register irq %d\n", retval);
1038 goto free_i2c;
1039 }
1040
1041 if (cx->std == 0)
1042 cx->std = V4L2_STD_NTSC_M;
1043
1044 if (cx->options.tuner == -1) {
1045 for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) {
1046 if ((cx->std & cx->card->tuners[i].std) == 0)
1047 continue;
1048 cx->options.tuner = cx->card->tuners[i].tuner;
1049 break;
1050 }
1051 }
1052 /* if no tuner was found, then pick the first tuner in the card list */
1053 if (cx->options.tuner == -1 && cx->card->tuners[0].std) {
1054 cx->std = cx->card->tuners[0].std;
1055 if (cx->std & V4L2_STD_PAL)
1056 cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
1057 else if (cx->std & V4L2_STD_NTSC)
1058 cx->std = V4L2_STD_NTSC_M;
1059 else if (cx->std & V4L2_STD_SECAM)
1060 cx->std = V4L2_STD_SECAM_L;
1061 cx->options.tuner = cx->card->tuners[0].tuner;
1062 }
1063 if (cx->options.radio == -1)
1064 cx->options.radio = (cx->card->radio_input.audio_type != 0);
1065
1066 /* The card is now fully identified, continue with card-specific
1067 initialization. */
1068 cx18_init_struct2(cx);
1069
1070 cx18_init_subdevs(cx);
1071
1072 if (cx->std & V4L2_STD_525_60)
1073 cx->is_60hz = 1;
1074 else
1075 cx->is_50hz = 1;
1076
1077 cx2341x_handler_set_50hz(&cx->cxhdl, !cx->is_60hz);
1078
1079 if (cx->options.radio > 0)
1080 cx->v4l2_cap |= V4L2_CAP_RADIO;
1081
1082 if (cx->options.tuner > -1) {
1083 struct tuner_setup setup;
1084
1085 setup.addr = ADDR_UNSET;
1086 setup.type = cx->options.tuner;
1087 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
1088 if (cx->options.radio > 0)
1089 setup.mode_mask |= T_RADIO;
1090 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
1091 cx18_reset_tuner_gpio : NULL;
1092 cx18_call_all(cx, tuner, s_type_addr, &setup);
1093 if (setup.type == TUNER_XC2028) {
1094 static struct xc2028_ctrl ctrl = {
1095 .fname = XC2028_DEFAULT_FIRMWARE,
1096 .max_len = 64,
1097 };
1098 struct v4l2_priv_tun_config cfg = {
1099 .tuner = cx->options.tuner,
1100 .priv = &ctrl,
1101 };
1102 cx18_call_all(cx, tuner, s_config, &cfg);
1103 }
1104 }
1105
1106 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
1107 are not. */
1108 cx->tuner_std = cx->std;
1109 if (cx->std == V4L2_STD_ALL)
1110 cx->std = V4L2_STD_NTSC_M;
1111
1112 retval = cx18_streams_setup(cx);
1113 if (retval) {
1114 CX18_ERR("Error %d setting up streams\n", retval);
1115 goto free_irq;
1116 }
1117 retval = cx18_streams_register(cx);
1118 if (retval) {
1119 CX18_ERR("Error %d registering devices\n", retval);
1120 goto free_streams;
1121 }
1122
1123 CX18_INFO("Initialized card: %s\n", cx->card_name);
1124
1125 /* Load cx18 submodules (cx18-alsa) */
1126 request_modules(cx);
1127 return 0;
1128
1129free_streams:
1130 cx18_streams_cleanup(cx, 1);
1131free_irq:
1132 free_irq(cx->pci_dev->irq, (void *)cx);
1133free_i2c:
1134 exit_cx18_i2c(cx);
1135free_map:
1136 cx18_iounmap(cx);
1137free_mem:
1138 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1139free_workqueues:
1140 destroy_workqueue(cx->in_work_queue);
1141err:
1142 if (retval == 0)
1143 retval = -ENODEV;
1144 CX18_ERR("Error %d on initialization\n", retval);
1145
1146 v4l2_device_unregister(&cx->v4l2_dev);
1147 kfree(cx);
1148 return retval;
1149}
1150
1151int cx18_init_on_first_open(struct cx18 *cx)
1152{
1153 int video_input;
1154 int fw_retry_count = 3;
1155 struct v4l2_frequency vf;
1156 struct cx18_open_id fh;
1157 v4l2_std_id std;
1158
1159 fh.cx = cx;
1160
1161 if (test_bit(CX18_F_I_FAILED, &cx->i_flags))
1162 return -ENXIO;
1163
1164 if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags))
1165 return 0;
1166
1167 while (--fw_retry_count > 0) {
1168 /* load firmware */
1169 if (cx18_firmware_init(cx) == 0)
1170 break;
1171 if (fw_retry_count > 1)
1172 CX18_WARN("Retry loading firmware\n");
1173 }
1174
1175 if (fw_retry_count == 0) {
1176 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1177 return -ENXIO;
1178 }
1179 set_bit(CX18_F_I_LOADED_FW, &cx->i_flags);
1180
1181 /*
1182 * Init the firmware twice to work around a silicon bug
1183 * with the digital TS.
1184 *
1185 * The second firmware load requires us to normalize the APU state,
1186 * or the audio for the first analog capture will be badly incorrect.
1187 *
1188 * I can't seem to call APU_RESETAI and have it succeed without the
1189 * APU capturing audio, so we start and stop it here to do the reset
1190 */
1191
1192 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1193 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1194 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1195 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1196
1197 fw_retry_count = 3;
1198 while (--fw_retry_count > 0) {
1199 /* load firmware */
1200 if (cx18_firmware_init(cx) == 0)
1201 break;
1202 if (fw_retry_count > 1)
1203 CX18_WARN("Retry loading firmware\n");
1204 }
1205
1206 if (fw_retry_count == 0) {
1207 set_bit(CX18_F_I_FAILED, &cx->i_flags);
1208 return -ENXIO;
1209 }
1210
1211 /*
1212 * The second firmware load requires us to normalize the APU state,
1213 * or the audio for the first analog capture will be badly incorrect.
1214 *
1215 * I can't seem to call APU_RESETAI and have it succeed without the
1216 * APU capturing audio, so we start and stop it here to do the reset
1217 */
1218
1219 /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */
1220 cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0);
1221 cx18_vapi(cx, CX18_APU_RESETAI, 0);
1222 cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
1223
1224 /* Init the A/V decoder, if it hasn't been already */
1225 v4l2_subdev_call(cx->sd_av, core, load_fw);
1226
1227 vf.tuner = 0;
1228 vf.type = V4L2_TUNER_ANALOG_TV;
1229 vf.frequency = 6400; /* the tuner 'baseline' frequency */
1230
1231 /* Set initial frequency. For PAL/SECAM broadcasts no
1232 'default' channel exists AFAIK. */
1233 if (cx->std == V4L2_STD_NTSC_M_JP)
1234 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
1235 else if (cx->std & V4L2_STD_NTSC_M)
1236 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
1237
1238 video_input = cx->active_input;
1239 cx->active_input++; /* Force update of input */
1240 cx18_s_input(NULL, &fh, video_input);
1241
1242 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1243 in one place. */
1244 cx->std++; /* Force full standard initialization */
1245 std = (cx->tuner_std == V4L2_STD_ALL) ? V4L2_STD_NTSC_M : cx->tuner_std;
1246 cx18_s_std(NULL, &fh, &std);
1247 cx18_s_frequency(NULL, &fh, &vf);
1248 return 0;
1249}
1250
1251static void cx18_cancel_in_work_orders(struct cx18 *cx)
1252{
1253 int i;
1254 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++)
1255 cancel_work_sync(&cx->in_work_order[i].work);
1256}
1257
1258static void cx18_cancel_out_work_orders(struct cx18 *cx)
1259{
1260 int i;
1261 for (i = 0; i < CX18_MAX_STREAMS; i++)
1262 if (&cx->streams[i].video_dev != NULL)
1263 cancel_work_sync(&cx->streams[i].out_work_order);
1264}
1265
1266static void cx18_remove(struct pci_dev *pci_dev)
1267{
1268 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1269 struct cx18 *cx = to_cx18(v4l2_dev);
1270 int i;
1271
1272 CX18_DEBUG_INFO("Removing Card\n");
1273
1274 flush_request_modules(cx);
1275
1276 /* Stop all captures */
1277 CX18_DEBUG_INFO("Stopping all streams\n");
1278 if (atomic_read(&cx->tot_capturing) > 0)
1279 cx18_stop_all_captures(cx);
1280
1281 /* Stop interrupts that cause incoming work to be queued */
1282 cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
1283
1284 /* Incoming work can cause outgoing work, so clean up incoming first */
1285 cx18_cancel_in_work_orders(cx);
1286 cx18_cancel_out_work_orders(cx);
1287
1288 /* Stop ack interrupts that may have been needed for work to finish */
1289 cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
1290
1291 cx18_halt_firmware(cx);
1292
1293 destroy_workqueue(cx->in_work_queue);
1294
1295 cx18_streams_cleanup(cx, 1);
1296
1297 exit_cx18_i2c(cx);
1298
1299 free_irq(cx->pci_dev->irq, (void *)cx);
1300
1301 cx18_iounmap(cx);
1302
1303 release_mem_region(cx->base_addr, CX18_MEM_SIZE);
1304
1305 pci_disable_device(cx->pci_dev);
1306
1307 if (cx->vbi.sliced_mpeg_data[0] != NULL)
1308 for (i = 0; i < CX18_VBI_FRAMES; i++)
1309 kfree(cx->vbi.sliced_mpeg_data[i]);
1310
1311 v4l2_ctrl_handler_free(&cx->av_state.hdl);
1312
1313 CX18_INFO("Removed %s\n", cx->card_name);
1314
1315 v4l2_device_unregister(v4l2_dev);
1316 kfree(cx);
1317}
1318
1319
1320/* define a pci_driver for card detection */
1321static struct pci_driver cx18_pci_driver = {
1322 .name = "cx18",
1323 .id_table = cx18_pci_tbl,
1324 .probe = cx18_probe,
1325 .remove = cx18_remove,
1326};
1327
1328static int __init module_start(void)
1329{
1330 printk(KERN_INFO "cx18: Start initialization, version %s\n",
1331 CX18_VERSION);
1332
1333 /* Validate parameters */
1334 if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
1335 printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n",
1336 CX18_MAX_CARDS - 1);
1337 return -1;
1338 }
1339
1340 if (cx18_debug < 0 || cx18_debug > 511) {
1341 cx18_debug = 0;
1342 printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n");
1343 }
1344
1345 if (pci_register_driver(&cx18_pci_driver)) {
1346 printk(KERN_ERR "cx18: Error detecting PCI card\n");
1347 return -ENODEV;
1348 }
1349 printk(KERN_INFO "cx18: End initialization\n");
1350 return 0;
1351}
1352
1353static void __exit module_cleanup(void)
1354{
1355 pci_unregister_driver(&cx18_pci_driver);
1356}
1357
1358module_init(module_start);
1359module_exit(module_cleanup);
1360MODULE_FIRMWARE(XC2028_DEFAULT_FIRMWARE);
diff --git a/drivers/media/pci/cx18/cx18-driver.h b/drivers/media/pci/cx18/cx18-driver.h
new file mode 100644
index 000000000000..2767c64df0c8
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-driver.h
@@ -0,0 +1,730 @@
1/*
2 * cx18 driver internal defines and structures
3 *
4 * Derived from ivtv-driver.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#ifndef CX18_DRIVER_H
26#define CX18_DRIVER_H
27
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/delay.h>
32#include <linux/sched.h>
33#include <linux/fs.h>
34#include <linux/pci.h>
35#include <linux/interrupt.h>
36#include <linux/spinlock.h>
37#include <linux/i2c.h>
38#include <linux/i2c-algo-bit.h>
39#include <linux/list.h>
40#include <linux/unistd.h>
41#include <linux/pagemap.h>
42#include <linux/workqueue.h>
43#include <linux/mutex.h>
44#include <linux/slab.h>
45#include <asm/byteorder.h>
46
47#include <media/v4l2-common.h>
48#include <media/v4l2-ioctl.h>
49#include <media/v4l2-device.h>
50#include <media/v4l2-fh.h>
51#include <media/tuner.h>
52#include <media/ir-kbd-i2c.h>
53#include "cx18-mailbox.h"
54#include "cx18-av-core.h"
55#include "cx23418.h"
56
57/* DVB */
58#include "demux.h"
59#include "dmxdev.h"
60#include "dvb_demux.h"
61#include "dvb_frontend.h"
62#include "dvb_net.h"
63#include "dvbdev.h"
64
65/* Videobuf / YUV support */
66#include <media/videobuf-core.h>
67#include <media/videobuf-vmalloc.h>
68
69#ifndef CONFIG_PCI
70# error "This driver requires kernel PCI support."
71#endif
72
73#define CX18_MEM_OFFSET 0x00000000
74#define CX18_MEM_SIZE 0x04000000
75#define CX18_REG_OFFSET 0x02000000
76
77/* Maximum cx18 driver instances. */
78#define CX18_MAX_CARDS 32
79
80/* Supported cards */
81#define CX18_CARD_HVR_1600_ESMT 0 /* Hauppauge HVR 1600 (ESMT memory) */
82#define CX18_CARD_HVR_1600_SAMSUNG 1 /* Hauppauge HVR 1600 (Samsung memory) */
83#define CX18_CARD_COMPRO_H900 2 /* Compro VideoMate H900 */
84#define CX18_CARD_YUAN_MPC718 3 /* Yuan MPC718 */
85#define CX18_CARD_CNXT_RAPTOR_PAL 4 /* Conexant Raptor PAL */
86#define CX18_CARD_TOSHIBA_QOSMIO_DVBT 5 /* Toshiba Qosmio Interal DVB-T/Analog*/
87#define CX18_CARD_LEADTEK_PVR2100 6 /* Leadtek WinFast PVR2100 */
88#define CX18_CARD_LEADTEK_DVR3100H 7 /* Leadtek WinFast DVR3100 H */
89#define CX18_CARD_GOTVIEW_PCI_DVD3 8 /* GoTView PCI DVD3 Hybrid */
90#define CX18_CARD_HVR_1600_S5H1411 9 /* Hauppauge HVR 1600 s5h1411/tda18271*/
91#define CX18_CARD_LAST 9
92
93#define CX18_ENC_STREAM_TYPE_MPG 0
94#define CX18_ENC_STREAM_TYPE_TS 1
95#define CX18_ENC_STREAM_TYPE_YUV 2
96#define CX18_ENC_STREAM_TYPE_VBI 3
97#define CX18_ENC_STREAM_TYPE_PCM 4
98#define CX18_ENC_STREAM_TYPE_IDX 5
99#define CX18_ENC_STREAM_TYPE_RAD 6
100#define CX18_MAX_STREAMS 7
101
102/* system vendor and device IDs */
103#define PCI_VENDOR_ID_CX 0x14f1
104#define PCI_DEVICE_ID_CX23418 0x5b7a
105
106/* subsystem vendor ID */
107#define CX18_PCI_ID_HAUPPAUGE 0x0070
108#define CX18_PCI_ID_COMPRO 0x185b
109#define CX18_PCI_ID_YUAN 0x12ab
110#define CX18_PCI_ID_CONEXANT 0x14f1
111#define CX18_PCI_ID_TOSHIBA 0x1179
112#define CX18_PCI_ID_LEADTEK 0x107D
113#define CX18_PCI_ID_GOTVIEW 0x5854
114
115/* ======================================================================== */
116/* ========================== START USER SETTABLE DMA VARIABLES =========== */
117/* ======================================================================== */
118
119/* DMA Buffers, Default size in MB allocated */
120#define CX18_DEFAULT_ENC_TS_BUFFERS 1
121#define CX18_DEFAULT_ENC_MPG_BUFFERS 2
122#define CX18_DEFAULT_ENC_IDX_BUFFERS 1
123#define CX18_DEFAULT_ENC_YUV_BUFFERS 2
124#define CX18_DEFAULT_ENC_VBI_BUFFERS 1
125#define CX18_DEFAULT_ENC_PCM_BUFFERS 1
126
127/* Maximum firmware DMA buffers per stream */
128#define CX18_MAX_FW_MDLS_PER_STREAM 63
129
130/* YUV buffer sizes in bytes to ensure integer # of frames per buffer */
131#define CX18_UNIT_ENC_YUV_BUFSIZE (720 * 32 * 3 / 2) /* bytes */
132#define CX18_625_LINE_ENC_YUV_BUFSIZE (CX18_UNIT_ENC_YUV_BUFSIZE * 576/32)
133#define CX18_525_LINE_ENC_YUV_BUFSIZE (CX18_UNIT_ENC_YUV_BUFSIZE * 480/32)
134
135/* IDX buffer size should be a multiple of the index entry size from the chip */
136struct cx18_enc_idx_entry {
137 __le32 length;
138 __le32 offset_low;
139 __le32 offset_high;
140 __le32 flags;
141 __le32 pts_low;
142 __le32 pts_high;
143} __attribute__ ((packed));
144#define CX18_UNIT_ENC_IDX_BUFSIZE \
145 (sizeof(struct cx18_enc_idx_entry) * V4L2_ENC_IDX_ENTRIES)
146
147/* DMA buffer, default size in kB allocated */
148#define CX18_DEFAULT_ENC_TS_BUFSIZE 32
149#define CX18_DEFAULT_ENC_MPG_BUFSIZE 32
150#define CX18_DEFAULT_ENC_IDX_BUFSIZE (CX18_UNIT_ENC_IDX_BUFSIZE * 1 / 1024 + 1)
151#define CX18_DEFAULT_ENC_YUV_BUFSIZE (CX18_UNIT_ENC_YUV_BUFSIZE * 3 / 1024 + 1)
152#define CX18_DEFAULT_ENC_PCM_BUFSIZE 4
153
154/* i2c stuff */
155#define I2C_CLIENTS_MAX 16
156
157/* debugging */
158
159/* Flag to turn on high volume debugging */
160#define CX18_DBGFLG_WARN (1 << 0)
161#define CX18_DBGFLG_INFO (1 << 1)
162#define CX18_DBGFLG_API (1 << 2)
163#define CX18_DBGFLG_DMA (1 << 3)
164#define CX18_DBGFLG_IOCTL (1 << 4)
165#define CX18_DBGFLG_FILE (1 << 5)
166#define CX18_DBGFLG_I2C (1 << 6)
167#define CX18_DBGFLG_IRQ (1 << 7)
168/* Flag to turn on high volume debugging */
169#define CX18_DBGFLG_HIGHVOL (1 << 8)
170
171/* NOTE: extra space before comma in 'fmt , ## args' is required for
172 gcc-2.95, otherwise it won't compile. */
173#define CX18_DEBUG(x, type, fmt, args...) \
174 do { \
175 if ((x) & cx18_debug) \
176 v4l2_info(&cx->v4l2_dev, " " type ": " fmt , ## args); \
177 } while (0)
178#define CX18_DEBUG_WARN(fmt, args...) CX18_DEBUG(CX18_DBGFLG_WARN, "warning", fmt , ## args)
179#define CX18_DEBUG_INFO(fmt, args...) CX18_DEBUG(CX18_DBGFLG_INFO, "info", fmt , ## args)
180#define CX18_DEBUG_API(fmt, args...) CX18_DEBUG(CX18_DBGFLG_API, "api", fmt , ## args)
181#define CX18_DEBUG_DMA(fmt, args...) CX18_DEBUG(CX18_DBGFLG_DMA, "dma", fmt , ## args)
182#define CX18_DEBUG_IOCTL(fmt, args...) CX18_DEBUG(CX18_DBGFLG_IOCTL, "ioctl", fmt , ## args)
183#define CX18_DEBUG_FILE(fmt, args...) CX18_DEBUG(CX18_DBGFLG_FILE, "file", fmt , ## args)
184#define CX18_DEBUG_I2C(fmt, args...) CX18_DEBUG(CX18_DBGFLG_I2C, "i2c", fmt , ## args)
185#define CX18_DEBUG_IRQ(fmt, args...) CX18_DEBUG(CX18_DBGFLG_IRQ, "irq", fmt , ## args)
186
187#define CX18_DEBUG_HIGH_VOL(x, type, fmt, args...) \
188 do { \
189 if (((x) & cx18_debug) && (cx18_debug & CX18_DBGFLG_HIGHVOL)) \
190 v4l2_info(&cx->v4l2_dev, " " type ": " fmt , ## args); \
191 } while (0)
192#define CX18_DEBUG_HI_WARN(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_WARN, "warning", fmt , ## args)
193#define CX18_DEBUG_HI_INFO(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_INFO, "info", fmt , ## args)
194#define CX18_DEBUG_HI_API(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_API, "api", fmt , ## args)
195#define CX18_DEBUG_HI_DMA(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_DMA, "dma", fmt , ## args)
196#define CX18_DEBUG_HI_IOCTL(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_IOCTL, "ioctl", fmt , ## args)
197#define CX18_DEBUG_HI_FILE(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_FILE, "file", fmt , ## args)
198#define CX18_DEBUG_HI_I2C(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_I2C, "i2c", fmt , ## args)
199#define CX18_DEBUG_HI_IRQ(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_IRQ, "irq", fmt , ## args)
200
201/* Standard kernel messages */
202#define CX18_ERR(fmt, args...) v4l2_err(&cx->v4l2_dev, fmt , ## args)
203#define CX18_WARN(fmt, args...) v4l2_warn(&cx->v4l2_dev, fmt , ## args)
204#define CX18_INFO(fmt, args...) v4l2_info(&cx->v4l2_dev, fmt , ## args)
205
206/* Messages for internal subdevs to use */
207#define CX18_DEBUG_DEV(x, dev, type, fmt, args...) \
208 do { \
209 if ((x) & cx18_debug) \
210 v4l2_info(dev, " " type ": " fmt , ## args); \
211 } while (0)
212#define CX18_DEBUG_WARN_DEV(dev, fmt, args...) \
213 CX18_DEBUG_DEV(CX18_DBGFLG_WARN, dev, "warning", fmt , ## args)
214#define CX18_DEBUG_INFO_DEV(dev, fmt, args...) \
215 CX18_DEBUG_DEV(CX18_DBGFLG_INFO, dev, "info", fmt , ## args)
216#define CX18_DEBUG_API_DEV(dev, fmt, args...) \
217 CX18_DEBUG_DEV(CX18_DBGFLG_API, dev, "api", fmt , ## args)
218#define CX18_DEBUG_DMA_DEV(dev, fmt, args...) \
219 CX18_DEBUG_DEV(CX18_DBGFLG_DMA, dev, "dma", fmt , ## args)
220#define CX18_DEBUG_IOCTL_DEV(dev, fmt, args...) \
221 CX18_DEBUG_DEV(CX18_DBGFLG_IOCTL, dev, "ioctl", fmt , ## args)
222#define CX18_DEBUG_FILE_DEV(dev, fmt, args...) \
223 CX18_DEBUG_DEV(CX18_DBGFLG_FILE, dev, "file", fmt , ## args)
224#define CX18_DEBUG_I2C_DEV(dev, fmt, args...) \
225 CX18_DEBUG_DEV(CX18_DBGFLG_I2C, dev, "i2c", fmt , ## args)
226#define CX18_DEBUG_IRQ_DEV(dev, fmt, args...) \
227 CX18_DEBUG_DEV(CX18_DBGFLG_IRQ, dev, "irq", fmt , ## args)
228
229#define CX18_DEBUG_HIGH_VOL_DEV(x, dev, type, fmt, args...) \
230 do { \
231 if (((x) & cx18_debug) && (cx18_debug & CX18_DBGFLG_HIGHVOL)) \
232 v4l2_info(dev, " " type ": " fmt , ## args); \
233 } while (0)
234#define CX18_DEBUG_HI_WARN_DEV(dev, fmt, args...) \
235 CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_WARN, dev, "warning", fmt , ## args)
236#define CX18_DEBUG_HI_INFO_DEV(dev, fmt, args...) \
237 CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_INFO, dev, "info", fmt , ## args)
238#define CX18_DEBUG_HI_API_DEV(dev, fmt, args...) \
239 CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_API, dev, "api", fmt , ## args)
240#define CX18_DEBUG_HI_DMA_DEV(dev, fmt, args...) \
241 CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_DMA, dev, "dma", fmt , ## args)
242#define CX18_DEBUG_HI_IOCTL_DEV(dev, fmt, args...) \
243 CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_IOCTL, dev, "ioctl", fmt , ## args)
244#define CX18_DEBUG_HI_FILE_DEV(dev, fmt, args...) \
245 CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_FILE, dev, "file", fmt , ## args)
246#define CX18_DEBUG_HI_I2C_DEV(dev, fmt, args...) \
247 CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_I2C, dev, "i2c", fmt , ## args)
248#define CX18_DEBUG_HI_IRQ_DEV(dev, fmt, args...) \
249 CX18_DEBUG_HIGH_VOL_DEV(CX18_DBGFLG_IRQ, dev, "irq", fmt , ## args)
250
251#define CX18_ERR_DEV(dev, fmt, args...) v4l2_err(dev, fmt , ## args)
252#define CX18_WARN_DEV(dev, fmt, args...) v4l2_warn(dev, fmt , ## args)
253#define CX18_INFO_DEV(dev, fmt, args...) v4l2_info(dev, fmt , ## args)
254
255extern int cx18_debug;
256
257struct cx18_options {
258 int megabytes[CX18_MAX_STREAMS]; /* Size in megabytes of each stream */
259 int cardtype; /* force card type on load */
260 int tuner; /* set tuner on load */
261 int radio; /* enable/disable radio */
262};
263
264/* per-mdl bit flags */
265#define CX18_F_M_NEED_SWAP 0 /* mdl buffer data must be endianess swapped */
266
267/* per-stream, s_flags */
268#define CX18_F_S_CLAIMED 3 /* this stream is claimed */
269#define CX18_F_S_STREAMING 4 /* the fw is decoding/encoding this stream */
270#define CX18_F_S_INTERNAL_USE 5 /* this stream is used internally (sliced VBI processing) */
271#define CX18_F_S_STREAMOFF 7 /* signal end of stream EOS */
272#define CX18_F_S_APPL_IO 8 /* this stream is used read/written by an application */
273#define CX18_F_S_STOPPING 9 /* telling the fw to stop capturing */
274
275/* per-cx18, i_flags */
276#define CX18_F_I_LOADED_FW 0 /* Loaded firmware 1st time */
277#define CX18_F_I_EOS 4 /* End of encoder stream */
278#define CX18_F_I_RADIO_USER 5 /* radio tuner is selected */
279#define CX18_F_I_ENC_PAUSED 13 /* the encoder is paused */
280#define CX18_F_I_INITED 21 /* set after first open */
281#define CX18_F_I_FAILED 22 /* set if first open failed */
282
283/* These are the VBI types as they appear in the embedded VBI private packets. */
284#define CX18_SLICED_TYPE_TELETEXT_B (1)
285#define CX18_SLICED_TYPE_CAPTION_525 (4)
286#define CX18_SLICED_TYPE_WSS_625 (5)
287#define CX18_SLICED_TYPE_VPS (7)
288
289/**
290 * list_entry_is_past_end - check if a previous loop cursor is off list end
291 * @pos: the type * previously used as a loop cursor.
292 * @head: the head for your list.
293 * @member: the name of the list_struct within the struct.
294 *
295 * Check if the entry's list_head is the head of the list, thus it's not a
296 * real entry but was the loop cursor that walked past the end
297 */
298#define list_entry_is_past_end(pos, head, member) \
299 (&pos->member == (head))
300
301struct cx18_buffer {
302 struct list_head list;
303 dma_addr_t dma_handle;
304 char *buf;
305
306 u32 bytesused;
307 u32 readpos;
308};
309
310struct cx18_mdl {
311 struct list_head list;
312 u32 id; /* index into cx->scb->cpu_mdl[] of 1st cx18_mdl_ent */
313
314 unsigned int skipped;
315 unsigned long m_flags;
316
317 struct list_head buf_list;
318 struct cx18_buffer *curr_buf; /* current buffer in list for reading */
319
320 u32 bytesused;
321 u32 readpos;
322};
323
324struct cx18_queue {
325 struct list_head list;
326 atomic_t depth;
327 u32 bytesused;
328 spinlock_t lock;
329};
330
331struct cx18_stream; /* forward reference */
332
333struct cx18_dvb {
334 struct cx18_stream *stream;
335 struct dmx_frontend hw_frontend;
336 struct dmx_frontend mem_frontend;
337 struct dmxdev dmxdev;
338 struct dvb_adapter dvb_adapter;
339 struct dvb_demux demux;
340 struct dvb_frontend *fe;
341 struct dvb_net dvbnet;
342 int enabled;
343 int feeding;
344 struct mutex feedlock;
345};
346
347struct cx18; /* forward reference */
348struct cx18_scb; /* forward reference */
349
350
351#define CX18_MAX_MDL_ACKS 2
352#define CX18_MAX_IN_WORK_ORDERS (CX18_MAX_FW_MDLS_PER_STREAM + 7)
353/* CPU_DE_RELEASE_MDL can burst CX18_MAX_FW_MDLS_PER_STREAM orders in a group */
354
355#define CX18_F_EWO_MB_STALE_UPON_RECEIPT 0x1
356#define CX18_F_EWO_MB_STALE_WHILE_PROC 0x2
357#define CX18_F_EWO_MB_STALE \
358 (CX18_F_EWO_MB_STALE_UPON_RECEIPT | CX18_F_EWO_MB_STALE_WHILE_PROC)
359
360struct cx18_in_work_order {
361 struct work_struct work;
362 atomic_t pending;
363 struct cx18 *cx;
364 unsigned long flags;
365 int rpu;
366 struct cx18_mailbox mb;
367 struct cx18_mdl_ack mdl_ack[CX18_MAX_MDL_ACKS];
368 char *str;
369};
370
371#define CX18_INVALID_TASK_HANDLE 0xffffffff
372
373struct cx18_stream {
374 /* These first five fields are always set, even if the stream
375 is not actually created. */
376 struct video_device *video_dev; /* NULL when stream not created */
377 struct cx18_dvb *dvb; /* DVB / Digital Transport */
378 struct cx18 *cx; /* for ease of use */
379 const char *name; /* name of the stream */
380 int type; /* stream type */
381 u32 handle; /* task handle */
382 unsigned int mdl_base_idx;
383
384 u32 id;
385 unsigned long s_flags; /* status flags, see above */
386 int dma; /* can be PCI_DMA_TODEVICE,
387 PCI_DMA_FROMDEVICE or
388 PCI_DMA_NONE */
389 wait_queue_head_t waitq;
390
391 /* Buffers */
392 struct list_head buf_pool; /* buffers not attached to an MDL */
393 u32 buffers; /* total buffers owned by this stream */
394 u32 buf_size; /* size in bytes of a single buffer */
395
396 /* MDL sizes - all stream MDLs are the same size */
397 u32 bufs_per_mdl;
398 u32 mdl_size; /* total bytes in all buffers in a mdl */
399
400 /* MDL Queues */
401 struct cx18_queue q_free; /* free - in rotation, not committed */
402 struct cx18_queue q_busy; /* busy - in use by firmware */
403 struct cx18_queue q_full; /* full - data for user apps */
404 struct cx18_queue q_idle; /* idle - not in rotation */
405
406 struct work_struct out_work_order;
407
408 /* Videobuf for YUV video */
409 u32 pixelformat;
410 u32 vb_bytes_per_frame;
411 struct list_head vb_capture; /* video capture queue */
412 spinlock_t vb_lock;
413 struct timer_list vb_timeout;
414
415 struct videobuf_queue vbuf_q;
416 spinlock_t vbuf_q_lock; /* Protect vbuf_q */
417 enum v4l2_buf_type vb_type;
418};
419
420struct cx18_videobuf_buffer {
421 /* Common video buffer sub-system struct */
422 struct videobuf_buffer vb;
423 v4l2_std_id tvnorm; /* selected tv norm */
424 u32 bytes_used;
425};
426
427struct cx18_open_id {
428 struct v4l2_fh fh;
429 u32 open_id;
430 int type;
431 struct cx18 *cx;
432};
433
434static inline struct cx18_open_id *fh2id(struct v4l2_fh *fh)
435{
436 return container_of(fh, struct cx18_open_id, fh);
437}
438
439static inline struct cx18_open_id *file2id(struct file *file)
440{
441 return fh2id(file->private_data);
442}
443
444/* forward declaration of struct defined in cx18-cards.h */
445struct cx18_card;
446
447/*
448 * A note about "sliced" VBI data as implemented in this driver:
449 *
450 * Currently we collect the sliced VBI in the form of Ancillary Data
451 * packets, inserted by the AV core decoder/digitizer/slicer in the
452 * horizontal blanking region of the VBI lines, in "raw" mode as far as
453 * the Encoder is concerned. We don't ever tell the Encoder itself
454 * to provide sliced VBI. (AV Core: sliced mode - Encoder: raw mode)
455 *
456 * We then process the ancillary data ourselves to send the sliced data
457 * to the user application directly or build up MPEG-2 private stream 1
458 * packets to splice into (only!) MPEG-2 PS streams for the user app.
459 *
460 * (That's how ivtv essentially does it.)
461 *
462 * The Encoder should be able to extract certain sliced VBI data for
463 * us and provide it in a separate stream or splice it into any type of
464 * MPEG PS or TS stream, but this isn't implemented yet.
465 */
466
467/*
468 * Number of "raw" VBI samples per horizontal line we tell the Encoder to
469 * grab from the decoder/digitizer/slicer output for raw or sliced VBI.
470 * It depends on the pixel clock and the horiz rate:
471 *
472 * (1/Fh)*(2*Fp) = Samples/line
473 * = 4 bytes EAV + Anc data in hblank + 4 bytes SAV + active samples
474 *
475 * Sliced VBI data is sent as ancillary data during horizontal blanking
476 * Raw VBI is sent as active video samples during vertcal blanking
477 *
478 * We use a BT.656 pxiel clock of 13.5 MHz and a BT.656 active line
479 * length of 720 pixels @ 4:2:2 sampling. Thus...
480 *
481 * For systems that use a 15.734 kHz horizontal rate, such as
482 * NTSC-M, PAL-M, PAL-60, and other 60 Hz/525 line systems, we have:
483 *
484 * (1/15.734 kHz) * 2 * 13.5 MHz = 1716 samples/line =
485 * 4 bytes SAV + 268 bytes anc data + 4 bytes SAV + 1440 active samples
486 *
487 * For systems that use a 15.625 kHz horizontal rate, such as
488 * PAL-B/G/H, PAL-I, SECAM-L and other 50 Hz/625 line systems, we have:
489 *
490 * (1/15.625 kHz) * 2 * 13.5 MHz = 1728 samples/line =
491 * 4 bytes SAV + 280 bytes anc data + 4 bytes SAV + 1440 active samples
492 */
493static const u32 vbi_active_samples = 1444; /* 4 byte SAV + 720 Y + 720 U/V */
494static const u32 vbi_hblank_samples_60Hz = 272; /* 4 byte EAV + 268 anc/fill */
495static const u32 vbi_hblank_samples_50Hz = 284; /* 4 byte EAV + 280 anc/fill */
496
497#define CX18_VBI_FRAMES 32
498
499struct vbi_info {
500 /* Current state of v4l2 VBI settings for this device */
501 struct v4l2_format in;
502 struct v4l2_sliced_vbi_format *sliced_in; /* pointer to in.fmt.sliced */
503 u32 count; /* Count of VBI data lines: 60 Hz: 12 or 50 Hz: 18 */
504 u32 start[2]; /* First VBI data line per field: 10 & 273 or 6 & 318 */
505
506 u32 frame; /* Count of VBI buffers/frames received from Encoder */
507
508 /*
509 * Vars for creation and insertion of MPEG Private Stream 1 packets
510 * of sliced VBI data into an MPEG PS
511 */
512
513 /* Boolean: create and insert Private Stream 1 packets into the PS */
514 int insert_mpeg;
515
516 /*
517 * Buffer for the maximum of 2 * 18 * packet_size sliced VBI lines.
518 * Used in cx18-vbi.c only for collecting sliced data, and as a source
519 * during conversion of sliced VBI data into MPEG Priv Stream 1 packets.
520 * We don't need to save state here, but the array may have been a bit
521 * too big (2304 bytes) to alloc from the stack.
522 */
523 struct v4l2_sliced_vbi_data sliced_data[36];
524
525 /*
526 * A ring buffer of driver-generated MPEG-2 PS
527 * Program Pack/Private Stream 1 packets for sliced VBI data insertion
528 * into the MPEG PS stream.
529 *
530 * In each sliced_mpeg_data[] buffer is:
531 * 16 byte MPEG-2 PS Program Pack Header
532 * 16 byte MPEG-2 Private Stream 1 PES Header
533 * 4 byte magic number: "itv0" or "ITV0"
534 * 4 byte first field line mask, if "itv0"
535 * 4 byte second field line mask, if "itv0"
536 * 36 lines, if "ITV0"; or <36 lines, if "itv0"; of sliced VBI data
537 *
538 * Each line in the payload is
539 * 1 byte line header derived from the SDID (WSS, CC, VPS, etc.)
540 * 42 bytes of line data
541 *
542 * That's a maximum 1552 bytes of payload in the Private Stream 1 packet
543 * which is the payload size a PVR-350 (CX23415) MPEG decoder will
544 * accept for VBI data. So, including the headers, it's a maximum 1584
545 * bytes total.
546 */
547#define CX18_SLICED_MPEG_DATA_MAXSZ 1584
548 /* copy_vbi_buf() needs 8 temp bytes on the end for the worst case */
549#define CX18_SLICED_MPEG_DATA_BUFSZ (CX18_SLICED_MPEG_DATA_MAXSZ+8)
550 u8 *sliced_mpeg_data[CX18_VBI_FRAMES];
551 u32 sliced_mpeg_size[CX18_VBI_FRAMES];
552
553 /* Count of Program Pack/Program Stream 1 packets inserted into PS */
554 u32 inserted_frame;
555
556 /*
557 * A dummy driver stream transfer mdl & buffer with a copy of the next
558 * sliced_mpeg_data[] buffer for output to userland apps.
559 * Only used in cx18-fileops.c, but its state needs to persist at times.
560 */
561 struct cx18_mdl sliced_mpeg_mdl;
562 struct cx18_buffer sliced_mpeg_buf;
563};
564
565/* Per cx23418, per I2C bus private algo callback data */
566struct cx18_i2c_algo_callback_data {
567 struct cx18 *cx;
568 int bus_index; /* 0 or 1 for the cx23418's 1st or 2nd I2C bus */
569};
570
571#define CX18_MAX_MMIO_WR_RETRIES 10
572
573/* Struct to hold info about cx18 cards */
574struct cx18 {
575 int instance;
576 struct pci_dev *pci_dev;
577 struct v4l2_device v4l2_dev;
578 struct v4l2_subdev *sd_av; /* A/V decoder/digitizer sub-device */
579 struct v4l2_subdev *sd_extmux; /* External multiplexer sub-dev */
580
581 const struct cx18_card *card; /* card information */
582 const char *card_name; /* full name of the card */
583 const struct cx18_card_tuner_i2c *card_i2c; /* i2c addresses to probe for tuner */
584 u8 is_50hz;
585 u8 is_60hz;
586 u8 nof_inputs; /* number of video inputs */
587 u8 nof_audio_inputs; /* number of audio inputs */
588 u32 v4l2_cap; /* V4L2 capabilities of card */
589 u32 hw_flags; /* Hardware description of the board */
590 unsigned int free_mdl_idx;
591 struct cx18_scb __iomem *scb; /* pointer to SCB */
592 struct mutex epu2apu_mb_lock; /* protect driver to chip mailbox in SCB*/
593 struct mutex epu2cpu_mb_lock; /* protect driver to chip mailbox in SCB*/
594
595 struct cx18_av_state av_state;
596
597 /* codec settings */
598 struct cx2341x_handler cxhdl;
599 u32 filter_mode;
600 u32 temporal_strength;
601 u32 spatial_strength;
602
603 /* dualwatch */
604 unsigned long dualwatch_jiffies;
605 u32 dualwatch_stereo_mode;
606
607 struct mutex serialize_lock; /* mutex used to serialize open/close/start/stop/ioctl operations */
608 struct cx18_options options; /* User options */
609 int stream_buffers[CX18_MAX_STREAMS]; /* # of buffers for each stream */
610 int stream_buf_size[CX18_MAX_STREAMS]; /* Stream buffer size */
611 struct cx18_stream streams[CX18_MAX_STREAMS]; /* Stream data */
612 struct snd_cx18_card *alsa; /* ALSA interface for PCM capture stream */
613 void (*pcm_announce_callback)(struct snd_cx18_card *card, u8 *pcm_data,
614 size_t num_bytes);
615
616 unsigned long i_flags; /* global cx18 flags */
617 atomic_t ana_capturing; /* count number of active analog capture streams */
618 atomic_t tot_capturing; /* total count number of active capture streams */
619 int search_pack_header;
620
621 int open_id; /* incremented each time an open occurs, used as
622 unique ID. Starts at 1, so 0 can be used as
623 uninitialized value in the stream->id. */
624
625 resource_size_t base_addr;
626
627 u8 card_rev;
628 void __iomem *enc_mem, *reg_mem;
629
630 struct vbi_info vbi;
631
632 u64 mpg_data_received;
633 u64 vbi_data_inserted;
634
635 wait_queue_head_t mb_apu_waitq;
636 wait_queue_head_t mb_cpu_waitq;
637 wait_queue_head_t cap_w;
638 /* when the current DMA is finished this queue is woken up */
639 wait_queue_head_t dma_waitq;
640
641 u32 sw1_irq_mask;
642 u32 sw2_irq_mask;
643 u32 hw2_irq_mask;
644
645 struct workqueue_struct *in_work_queue;
646 char in_workq_name[11]; /* "cx18-NN-in" */
647 struct cx18_in_work_order in_work_order[CX18_MAX_IN_WORK_ORDERS];
648 char epu_debug_str[256]; /* CX18_EPU_DEBUG is rare: use shared space */
649
650 /* i2c */
651 struct i2c_adapter i2c_adap[2];
652 struct i2c_algo_bit_data i2c_algo[2];
653 struct cx18_i2c_algo_callback_data i2c_algo_cb_data[2];
654
655 struct IR_i2c_init_data ir_i2c_init_data;
656
657 /* gpio */
658 u32 gpio_dir;
659 u32 gpio_val;
660 struct mutex gpio_lock;
661 struct v4l2_subdev sd_gpiomux;
662 struct v4l2_subdev sd_resetctrl;
663
664 /* v4l2 and User settings */
665
666 /* codec settings */
667 u32 audio_input;
668 u32 active_input;
669 v4l2_std_id std;
670 v4l2_std_id tuner_std; /* The norm of the tuner (fixed) */
671
672 /* Used for cx18-alsa module loading */
673 struct work_struct request_module_wk;
674};
675
676static inline struct cx18 *to_cx18(struct v4l2_device *v4l2_dev)
677{
678 return container_of(v4l2_dev, struct cx18, v4l2_dev);
679}
680
681/* cx18 extensions to be loaded */
682extern int (*cx18_ext_init)(struct cx18 *);
683
684/* Globals */
685extern int cx18_first_minor;
686
687/*==============Prototypes==================*/
688
689/* Return non-zero if a signal is pending */
690int cx18_msleep_timeout(unsigned int msecs, int intr);
691
692/* Read Hauppauge eeprom */
693struct tveeprom; /* forward reference */
694void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv);
695
696/* First-open initialization: load firmware, etc. */
697int cx18_init_on_first_open(struct cx18 *cx);
698
699/* Test if the current VBI mode is raw (1) or sliced (0) */
700static inline int cx18_raw_vbi(const struct cx18 *cx)
701{
702 return cx->vbi.in.type == V4L2_BUF_TYPE_VBI_CAPTURE;
703}
704
705/* Call the specified callback for all subdevs with a grp_id bit matching the
706 * mask in hw (if 0, then match them all). Ignore any errors. */
707#define cx18_call_hw(cx, hw, o, f, args...) \
708 do { \
709 struct v4l2_subdev *__sd; \
710 __v4l2_device_call_subdevs_p(&(cx)->v4l2_dev, __sd, \
711 !(hw) || (__sd->grp_id & (hw)), o, f , ##args); \
712 } while (0)
713
714#define cx18_call_all(cx, o, f, args...) cx18_call_hw(cx, 0, o, f , ##args)
715
716/* Call the specified callback for all subdevs with a grp_id bit matching the
717 * mask in hw (if 0, then match them all). If the callback returns an error
718 * other than 0 or -ENOIOCTLCMD, then return with that error code. */
719#define cx18_call_hw_err(cx, hw, o, f, args...) \
720({ \
721 struct v4l2_subdev *__sd; \
722 __v4l2_device_call_subdevs_until_err_p(&(cx)->v4l2_dev, \
723 __sd, !(hw) || (__sd->grp_id & (hw)), o, f, \
724 ##args); \
725})
726
727#define cx18_call_all_err(cx, o, f, args...) \
728 cx18_call_hw_err(cx, 0, o, f , ##args)
729
730#endif /* CX18_DRIVER_H */
diff --git a/drivers/media/pci/cx18/cx18-dvb.c b/drivers/media/pci/cx18/cx18-dvb.c
new file mode 100644
index 000000000000..3eac59c51231
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-dvb.c
@@ -0,0 +1,609 @@
1/*
2 * cx18 functions for DVB support
3 *
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "cx18-version.h"
24#include "cx18-dvb.h"
25#include "cx18-io.h"
26#include "cx18-queue.h"
27#include "cx18-streams.h"
28#include "cx18-cards.h"
29#include "cx18-gpio.h"
30#include "s5h1409.h"
31#include "mxl5005s.h"
32#include "s5h1411.h"
33#include "tda18271.h"
34#include "zl10353.h"
35
36#include <linux/firmware.h>
37#include "mt352.h"
38#include "mt352_priv.h"
39#include "tuner-xc2028.h"
40
41DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42
43#define FWFILE "dvb-cx18-mpc718-mt352.fw"
44
45#define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000
46#define CX18_CLOCK_ENABLE2 0xc71024
47#define CX18_DMUX_CLK_MASK 0x0080
48
49/*
50 * CX18_CARD_HVR_1600_ESMT
51 * CX18_CARD_HVR_1600_SAMSUNG
52 */
53
54static struct mxl5005s_config hauppauge_hvr1600_tuner = {
55 .i2c_address = 0xC6 >> 1,
56 .if_freq = IF_FREQ_5380000HZ,
57 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
58 .agc_mode = MXL_SINGLE_AGC,
59 .tracking_filter = MXL_TF_C_H,
60 .rssi_enable = MXL_RSSI_ENABLE,
61 .cap_select = MXL_CAP_SEL_ENABLE,
62 .div_out = MXL_DIV_OUT_4,
63 .clock_out = MXL_CLOCK_OUT_DISABLE,
64 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
65 .top = MXL5005S_TOP_25P2,
66 .mod_mode = MXL_DIGITAL_MODE,
67 .if_mode = MXL_ZERO_IF,
68 .qam_gain = 0x02,
69 .AgcMasterByte = 0x00,
70};
71
72static struct s5h1409_config hauppauge_hvr1600_config = {
73 .demod_address = 0x32 >> 1,
74 .output_mode = S5H1409_SERIAL_OUTPUT,
75 .gpio = S5H1409_GPIO_ON,
76 .qam_if = 44000,
77 .inversion = S5H1409_INVERSION_OFF,
78 .status_mode = S5H1409_DEMODLOCKING,
79 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
80 .hvr1600_opt = S5H1409_HVR1600_OPTIMIZE
81};
82
83/*
84 * CX18_CARD_HVR_1600_S5H1411
85 */
86static struct s5h1411_config hcw_s5h1411_config = {
87 .output_mode = S5H1411_SERIAL_OUTPUT,
88 .gpio = S5H1411_GPIO_OFF,
89 .vsb_if = S5H1411_IF_44000,
90 .qam_if = S5H1411_IF_4000,
91 .inversion = S5H1411_INVERSION_ON,
92 .status_mode = S5H1411_DEMODLOCKING,
93 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
94};
95
96static struct tda18271_std_map hauppauge_tda18271_std_map = {
97 .atsc_6 = { .if_freq = 5380, .agc_mode = 3, .std = 3,
98 .if_lvl = 6, .rfagc_top = 0x37 },
99 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0,
100 .if_lvl = 6, .rfagc_top = 0x37 },
101};
102
103static struct tda18271_config hauppauge_tda18271_config = {
104 .std_map = &hauppauge_tda18271_std_map,
105 .gate = TDA18271_GATE_DIGITAL,
106 .output_opt = TDA18271_OUTPUT_LT_OFF,
107};
108
109/*
110 * CX18_CARD_LEADTEK_DVR3100H
111 */
112/* Information/confirmation of proper config values provided by Terry Wu */
113static struct zl10353_config leadtek_dvr3100h_demod = {
114 .demod_address = 0x1e >> 1, /* Datasheet suggested straps */
115 .if2 = 45600, /* 4.560 MHz IF from the XC3028 */
116 .parallel_ts = 1, /* Not a serial TS */
117 .no_tuner = 1, /* XC3028 is not behind the gate */
118 .disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */
119};
120
121/*
122 * CX18_CARD_YUAN_MPC718
123 */
124/*
125 * Due to
126 *
127 * 1. an absence of information on how to prgram the MT352
128 * 2. the Linux mt352 module pushing MT352 initialzation off onto us here
129 *
130 * We have to use an init sequence that *you* must extract from the Windows
131 * driver (yuanrap.sys) and which we load as a firmware.
132 *
133 * If someone can provide me with a Zarlink MT352 (Intel CE6352?) Design Manual
134 * with chip programming details, then I can remove this annoyance.
135 */
136static int yuan_mpc718_mt352_reqfw(struct cx18_stream *stream,
137 const struct firmware **fw)
138{
139 struct cx18 *cx = stream->cx;
140 const char *fn = FWFILE;
141 int ret;
142
143 ret = request_firmware(fw, fn, &cx->pci_dev->dev);
144 if (ret)
145 CX18_ERR("Unable to open firmware file %s\n", fn);
146 else {
147 size_t sz = (*fw)->size;
148 if (sz < 2 || sz > 64 || (sz % 2) != 0) {
149 CX18_ERR("Firmware %s has a bad size: %lu bytes\n",
150 fn, (unsigned long) sz);
151 ret = -EILSEQ;
152 release_firmware(*fw);
153 *fw = NULL;
154 }
155 }
156
157 if (ret) {
158 CX18_ERR("The MPC718 board variant with the MT352 DVB-T"
159 "demodualtor will not work without it\n");
160 CX18_ERR("Run 'linux/Documentation/dvb/get_dvb_firmware "
161 "mpc718' if you need the firmware\n");
162 }
163 return ret;
164}
165
166static int yuan_mpc718_mt352_init(struct dvb_frontend *fe)
167{
168 struct cx18_dvb *dvb = container_of(fe->dvb,
169 struct cx18_dvb, dvb_adapter);
170 struct cx18_stream *stream = dvb->stream;
171 const struct firmware *fw = NULL;
172 int ret;
173 int i;
174 u8 buf[3];
175
176 ret = yuan_mpc718_mt352_reqfw(stream, &fw);
177 if (ret)
178 return ret;
179
180 /* Loop through all the register-value pairs in the firmware file */
181 for (i = 0; i < fw->size; i += 2) {
182 buf[0] = fw->data[i];
183 /* Intercept a few registers we want to set ourselves */
184 switch (buf[0]) {
185 case TRL_NOMINAL_RATE_0:
186 /* Set our custom OFDM bandwidth in the case below */
187 break;
188 case TRL_NOMINAL_RATE_1:
189 /* 6 MHz: 64/7 * 6/8 / 20.48 * 2^16 = 0x55b6.db6 */
190 /* 7 MHz: 64/7 * 7/8 / 20.48 * 2^16 = 0x6400 */
191 /* 8 MHz: 64/7 * 8/8 / 20.48 * 2^16 = 0x7249.249 */
192 buf[1] = 0x72;
193 buf[2] = 0x49;
194 mt352_write(fe, buf, 3);
195 break;
196 case INPUT_FREQ_0:
197 /* Set our custom IF in the case below */
198 break;
199 case INPUT_FREQ_1:
200 /* 4.56 MHz IF: (20.48 - 4.56)/20.48 * 2^14 = 0x31c0 */
201 buf[1] = 0x31;
202 buf[2] = 0xc0;
203 mt352_write(fe, buf, 3);
204 break;
205 default:
206 /* Pass through the register-value pair from the fw */
207 buf[1] = fw->data[i+1];
208 mt352_write(fe, buf, 2);
209 break;
210 }
211 }
212
213 buf[0] = (u8) TUNER_GO;
214 buf[1] = 0x01; /* Go */
215 mt352_write(fe, buf, 2);
216 release_firmware(fw);
217 return 0;
218}
219
220static struct mt352_config yuan_mpc718_mt352_demod = {
221 .demod_address = 0x1e >> 1,
222 .adc_clock = 20480, /* 20.480 MHz */
223 .if2 = 4560, /* 4.560 MHz */
224 .no_tuner = 1, /* XC3028 is not behind the gate */
225 .demod_init = yuan_mpc718_mt352_init,
226};
227
228static struct zl10353_config yuan_mpc718_zl10353_demod = {
229 .demod_address = 0x1e >> 1, /* Datasheet suggested straps */
230 .if2 = 45600, /* 4.560 MHz IF from the XC3028 */
231 .parallel_ts = 1, /* Not a serial TS */
232 .no_tuner = 1, /* XC3028 is not behind the gate */
233 .disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */
234};
235
236static struct zl10353_config gotview_dvd3_zl10353_demod = {
237 .demod_address = 0x1e >> 1, /* Datasheet suggested straps */
238 .if2 = 45600, /* 4.560 MHz IF from the XC3028 */
239 .parallel_ts = 1, /* Not a serial TS */
240 .no_tuner = 1, /* XC3028 is not behind the gate */
241 .disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */
242};
243
244static int dvb_register(struct cx18_stream *stream);
245
246/* Kernel DVB framework calls this when the feed needs to start.
247 * The CX18 framework should enable the transport DMA handling
248 * and queue processing.
249 */
250static int cx18_dvb_start_feed(struct dvb_demux_feed *feed)
251{
252 struct dvb_demux *demux = feed->demux;
253 struct cx18_stream *stream = (struct cx18_stream *) demux->priv;
254 struct cx18 *cx;
255 int ret;
256 u32 v;
257
258 if (!stream)
259 return -EINVAL;
260
261 cx = stream->cx;
262 CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",
263 feed->pid, feed->index);
264
265 mutex_lock(&cx->serialize_lock);
266 ret = cx18_init_on_first_open(cx);
267 mutex_unlock(&cx->serialize_lock);
268 if (ret) {
269 CX18_ERR("Failed to initialize firmware starting DVB feed\n");
270 return ret;
271 }
272 ret = -EINVAL;
273
274 switch (cx->card->type) {
275 case CX18_CARD_HVR_1600_ESMT:
276 case CX18_CARD_HVR_1600_SAMSUNG:
277 case CX18_CARD_HVR_1600_S5H1411:
278 v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
279 v |= 0x00400000; /* Serial Mode */
280 v |= 0x00002000; /* Data Length - Byte */
281 v |= 0x00010000; /* Error - Polarity */
282 v |= 0x00020000; /* Error - Passthru */
283 v |= 0x000c0000; /* Error - Ignore */
284 cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
285 break;
286
287 case CX18_CARD_LEADTEK_DVR3100H:
288 case CX18_CARD_YUAN_MPC718:
289 case CX18_CARD_GOTVIEW_PCI_DVD3:
290 default:
291 /* Assumption - Parallel transport - Signalling
292 * undefined or default.
293 */
294 break;
295 }
296
297 if (!demux->dmx.frontend)
298 return -EINVAL;
299
300 mutex_lock(&stream->dvb->feedlock);
301 if (stream->dvb->feeding++ == 0) {
302 CX18_DEBUG_INFO("Starting Transport DMA\n");
303 mutex_lock(&cx->serialize_lock);
304 set_bit(CX18_F_S_STREAMING, &stream->s_flags);
305 ret = cx18_start_v4l2_encode_stream(stream);
306 if (ret < 0) {
307 CX18_DEBUG_INFO("Failed to start Transport DMA\n");
308 stream->dvb->feeding--;
309 if (stream->dvb->feeding == 0)
310 clear_bit(CX18_F_S_STREAMING, &stream->s_flags);
311 }
312 mutex_unlock(&cx->serialize_lock);
313 } else
314 ret = 0;
315 mutex_unlock(&stream->dvb->feedlock);
316
317 return ret;
318}
319
320/* Kernel DVB framework calls this when the feed needs to stop. */
321static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed)
322{
323 struct dvb_demux *demux = feed->demux;
324 struct cx18_stream *stream = (struct cx18_stream *)demux->priv;
325 struct cx18 *cx;
326 int ret = -EINVAL;
327
328 if (stream) {
329 cx = stream->cx;
330 CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",
331 feed->pid, feed->index);
332
333 mutex_lock(&stream->dvb->feedlock);
334 if (--stream->dvb->feeding == 0) {
335 CX18_DEBUG_INFO("Stopping Transport DMA\n");
336 mutex_lock(&cx->serialize_lock);
337 ret = cx18_stop_v4l2_encode_stream(stream, 0);
338 mutex_unlock(&cx->serialize_lock);
339 } else
340 ret = 0;
341 mutex_unlock(&stream->dvb->feedlock);
342 }
343
344 return ret;
345}
346
347int cx18_dvb_register(struct cx18_stream *stream)
348{
349 struct cx18 *cx = stream->cx;
350 struct cx18_dvb *dvb = stream->dvb;
351 struct dvb_adapter *dvb_adapter;
352 struct dvb_demux *dvbdemux;
353 struct dmx_demux *dmx;
354 int ret;
355
356 if (!dvb)
357 return -EINVAL;
358
359 dvb->enabled = 0;
360 dvb->stream = stream;
361
362 ret = dvb_register_adapter(&dvb->dvb_adapter,
363 CX18_DRIVER_NAME,
364 THIS_MODULE, &cx->pci_dev->dev, adapter_nr);
365 if (ret < 0)
366 goto err_out;
367
368 dvb_adapter = &dvb->dvb_adapter;
369
370 dvbdemux = &dvb->demux;
371
372 dvbdemux->priv = (void *)stream;
373
374 dvbdemux->filternum = 256;
375 dvbdemux->feednum = 256;
376 dvbdemux->start_feed = cx18_dvb_start_feed;
377 dvbdemux->stop_feed = cx18_dvb_stop_feed;
378 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
379 DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
380 ret = dvb_dmx_init(dvbdemux);
381 if (ret < 0)
382 goto err_dvb_unregister_adapter;
383
384 dmx = &dvbdemux->dmx;
385
386 dvb->hw_frontend.source = DMX_FRONTEND_0;
387 dvb->mem_frontend.source = DMX_MEMORY_FE;
388 dvb->dmxdev.filternum = 256;
389 dvb->dmxdev.demux = dmx;
390
391 ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter);
392 if (ret < 0)
393 goto err_dvb_dmx_release;
394
395 ret = dmx->add_frontend(dmx, &dvb->hw_frontend);
396 if (ret < 0)
397 goto err_dvb_dmxdev_release;
398
399 ret = dmx->add_frontend(dmx, &dvb->mem_frontend);
400 if (ret < 0)
401 goto err_remove_hw_frontend;
402
403 ret = dmx->connect_frontend(dmx, &dvb->hw_frontend);
404 if (ret < 0)
405 goto err_remove_mem_frontend;
406
407 ret = dvb_register(stream);
408 if (ret < 0)
409 goto err_disconnect_frontend;
410
411 dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx);
412
413 CX18_INFO("DVB Frontend registered\n");
414 CX18_INFO("Registered DVB adapter%d for %s (%d x %d.%02d kB)\n",
415 stream->dvb->dvb_adapter.num, stream->name,
416 stream->buffers, stream->buf_size/1024,
417 (stream->buf_size * 100 / 1024) % 100);
418
419 mutex_init(&dvb->feedlock);
420 dvb->enabled = 1;
421 return ret;
422
423err_disconnect_frontend:
424 dmx->disconnect_frontend(dmx);
425err_remove_mem_frontend:
426 dmx->remove_frontend(dmx, &dvb->mem_frontend);
427err_remove_hw_frontend:
428 dmx->remove_frontend(dmx, &dvb->hw_frontend);
429err_dvb_dmxdev_release:
430 dvb_dmxdev_release(&dvb->dmxdev);
431err_dvb_dmx_release:
432 dvb_dmx_release(dvbdemux);
433err_dvb_unregister_adapter:
434 dvb_unregister_adapter(dvb_adapter);
435err_out:
436 return ret;
437}
438
439void cx18_dvb_unregister(struct cx18_stream *stream)
440{
441 struct cx18 *cx = stream->cx;
442 struct cx18_dvb *dvb = stream->dvb;
443 struct dvb_adapter *dvb_adapter;
444 struct dvb_demux *dvbdemux;
445 struct dmx_demux *dmx;
446
447 CX18_INFO("unregister DVB\n");
448
449 if (dvb == NULL || !dvb->enabled)
450 return;
451
452 dvb_adapter = &dvb->dvb_adapter;
453 dvbdemux = &dvb->demux;
454 dmx = &dvbdemux->dmx;
455
456 dmx->close(dmx);
457 dvb_net_release(&dvb->dvbnet);
458 dmx->remove_frontend(dmx, &dvb->mem_frontend);
459 dmx->remove_frontend(dmx, &dvb->hw_frontend);
460 dvb_dmxdev_release(&dvb->dmxdev);
461 dvb_dmx_release(dvbdemux);
462 dvb_unregister_frontend(dvb->fe);
463 dvb_frontend_detach(dvb->fe);
464 dvb_unregister_adapter(dvb_adapter);
465}
466
467/* All the DVB attach calls go here, this function get's modified
468 * for each new card. cx18_dvb_start_feed() will also need changes.
469 */
470static int dvb_register(struct cx18_stream *stream)
471{
472 struct cx18_dvb *dvb = stream->dvb;
473 struct cx18 *cx = stream->cx;
474 int ret = 0;
475
476 switch (cx->card->type) {
477 case CX18_CARD_HVR_1600_ESMT:
478 case CX18_CARD_HVR_1600_SAMSUNG:
479 dvb->fe = dvb_attach(s5h1409_attach,
480 &hauppauge_hvr1600_config,
481 &cx->i2c_adap[0]);
482 if (dvb->fe != NULL) {
483 dvb_attach(mxl5005s_attach, dvb->fe,
484 &cx->i2c_adap[0],
485 &hauppauge_hvr1600_tuner);
486 ret = 0;
487 }
488 break;
489 case CX18_CARD_HVR_1600_S5H1411:
490 dvb->fe = dvb_attach(s5h1411_attach,
491 &hcw_s5h1411_config,
492 &cx->i2c_adap[0]);
493 if (dvb->fe != NULL)
494 dvb_attach(tda18271_attach, dvb->fe,
495 0x60, &cx->i2c_adap[0],
496 &hauppauge_tda18271_config);
497 break;
498 case CX18_CARD_LEADTEK_DVR3100H:
499 dvb->fe = dvb_attach(zl10353_attach,
500 &leadtek_dvr3100h_demod,
501 &cx->i2c_adap[1]);
502 if (dvb->fe != NULL) {
503 struct dvb_frontend *fe;
504 struct xc2028_config cfg = {
505 .i2c_adap = &cx->i2c_adap[1],
506 .i2c_addr = 0xc2 >> 1,
507 .ctrl = NULL,
508 };
509 static struct xc2028_ctrl ctrl = {
510 .fname = XC2028_DEFAULT_FIRMWARE,
511 .max_len = 64,
512 .demod = XC3028_FE_ZARLINK456,
513 .type = XC2028_AUTO,
514 };
515
516 fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);
517 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
518 fe->ops.tuner_ops.set_config(fe, &ctrl);
519 }
520 break;
521 case CX18_CARD_YUAN_MPC718:
522 /*
523 * TODO
524 * Apparently, these cards also could instead have a
525 * DiBcom demod supported by one of the db7000 drivers
526 */
527 dvb->fe = dvb_attach(mt352_attach,
528 &yuan_mpc718_mt352_demod,
529 &cx->i2c_adap[1]);
530 if (dvb->fe == NULL)
531 dvb->fe = dvb_attach(zl10353_attach,
532 &yuan_mpc718_zl10353_demod,
533 &cx->i2c_adap[1]);
534 if (dvb->fe != NULL) {
535 struct dvb_frontend *fe;
536 struct xc2028_config cfg = {
537 .i2c_adap = &cx->i2c_adap[1],
538 .i2c_addr = 0xc2 >> 1,
539 .ctrl = NULL,
540 };
541 static struct xc2028_ctrl ctrl = {
542 .fname = XC2028_DEFAULT_FIRMWARE,
543 .max_len = 64,
544 .demod = XC3028_FE_ZARLINK456,
545 .type = XC2028_AUTO,
546 };
547
548 fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);
549 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
550 fe->ops.tuner_ops.set_config(fe, &ctrl);
551 }
552 break;
553 case CX18_CARD_GOTVIEW_PCI_DVD3:
554 dvb->fe = dvb_attach(zl10353_attach,
555 &gotview_dvd3_zl10353_demod,
556 &cx->i2c_adap[1]);
557 if (dvb->fe != NULL) {
558 struct dvb_frontend *fe;
559 struct xc2028_config cfg = {
560 .i2c_adap = &cx->i2c_adap[1],
561 .i2c_addr = 0xc2 >> 1,
562 .ctrl = NULL,
563 };
564 static struct xc2028_ctrl ctrl = {
565 .fname = XC2028_DEFAULT_FIRMWARE,
566 .max_len = 64,
567 .demod = XC3028_FE_ZARLINK456,
568 .type = XC2028_AUTO,
569 };
570
571 fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);
572 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
573 fe->ops.tuner_ops.set_config(fe, &ctrl);
574 }
575 break;
576 default:
577 /* No Digital Tv Support */
578 break;
579 }
580
581 if (dvb->fe == NULL) {
582 CX18_ERR("frontend initialization failed\n");
583 return -1;
584 }
585
586 dvb->fe->callback = cx18_reset_tuner_gpio;
587
588 ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
589 if (ret < 0) {
590 if (dvb->fe->ops.release)
591 dvb->fe->ops.release(dvb->fe);
592 return ret;
593 }
594
595 /*
596 * The firmware seems to enable the TS DMUX clock
597 * under various circumstances. However, since we know we
598 * might use it, let's just turn it on ourselves here.
599 */
600 cx18_write_reg_expect(cx,
601 (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK,
602 CX18_CLOCK_ENABLE2,
603 CX18_DMUX_CLK_MASK,
604 (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK);
605
606 return ret;
607}
608
609MODULE_FIRMWARE(FWFILE);
diff --git a/drivers/media/pci/cx18/cx18-dvb.h b/drivers/media/pci/cx18/cx18-dvb.h
new file mode 100644
index 000000000000..bf8d8f6f5455
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-dvb.h
@@ -0,0 +1,25 @@
1/*
2 * cx18 functions for DVB support
3 *
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "cx18-driver.h"
23
24int cx18_dvb_register(struct cx18_stream *stream);
25void cx18_dvb_unregister(struct cx18_stream *stream);
diff --git a/drivers/media/pci/cx18/cx18-fileops.c b/drivers/media/pci/cx18/cx18-fileops.c
new file mode 100644
index 000000000000..4bfd865a4106
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-fileops.c
@@ -0,0 +1,881 @@
1/*
2 * cx18 file operation functions
3 *
4 * Derived from ivtv-fileops.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-fileops.h"
27#include "cx18-i2c.h"
28#include "cx18-queue.h"
29#include "cx18-vbi.h"
30#include "cx18-audio.h"
31#include "cx18-mailbox.h"
32#include "cx18-scb.h"
33#include "cx18-streams.h"
34#include "cx18-controls.h"
35#include "cx18-ioctl.h"
36#include "cx18-cards.h"
37
38/* This function tries to claim the stream for a specific file descriptor.
39 If no one else is using this stream then the stream is claimed and
40 associated VBI and IDX streams are also automatically claimed.
41 Possible error returns: -EBUSY if someone else has claimed
42 the stream or 0 on success. */
43int cx18_claim_stream(struct cx18_open_id *id, int type)
44{
45 struct cx18 *cx = id->cx;
46 struct cx18_stream *s = &cx->streams[type];
47 struct cx18_stream *s_assoc;
48
49 /* Nothing should ever try to directly claim the IDX stream */
50 if (type == CX18_ENC_STREAM_TYPE_IDX) {
51 CX18_WARN("MPEG Index stream cannot be claimed "
52 "directly, but something tried.\n");
53 return -EINVAL;
54 }
55
56 if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
57 /* someone already claimed this stream */
58 if (s->id == id->open_id) {
59 /* yes, this file descriptor did. So that's OK. */
60 return 0;
61 }
62 if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
63 /* VBI is handled already internally, now also assign
64 the file descriptor to this stream for external
65 reading of the stream. */
66 s->id = id->open_id;
67 CX18_DEBUG_INFO("Start Read VBI\n");
68 return 0;
69 }
70 /* someone else is using this stream already */
71 CX18_DEBUG_INFO("Stream %d is busy\n", type);
72 return -EBUSY;
73 }
74 s->id = id->open_id;
75
76 /*
77 * CX18_ENC_STREAM_TYPE_MPG needs to claim:
78 * CX18_ENC_STREAM_TYPE_VBI, if VBI insertion is on for sliced VBI, or
79 * CX18_ENC_STREAM_TYPE_IDX, if VBI insertion is off for sliced VBI
80 * (We don't yet fix up MPEG Index entries for our inserted packets).
81 *
82 * For all other streams we're done.
83 */
84 if (type != CX18_ENC_STREAM_TYPE_MPG)
85 return 0;
86
87 s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
88 if (cx->vbi.insert_mpeg && !cx18_raw_vbi(cx))
89 s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
90 else if (!cx18_stream_enabled(s_assoc))
91 return 0;
92
93 set_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
94
95 /* mark that it is used internally */
96 set_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags);
97 return 0;
98}
99EXPORT_SYMBOL(cx18_claim_stream);
100
101/* This function releases a previously claimed stream. It will take into
102 account associated VBI streams. */
103void cx18_release_stream(struct cx18_stream *s)
104{
105 struct cx18 *cx = s->cx;
106 struct cx18_stream *s_assoc;
107
108 s->id = -1;
109 if (s->type == CX18_ENC_STREAM_TYPE_IDX) {
110 /*
111 * The IDX stream is only used internally, and can
112 * only be indirectly unclaimed by unclaiming the MPG stream.
113 */
114 return;
115 }
116
117 if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
118 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) {
119 /* this stream is still in use internally */
120 return;
121 }
122 if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
123 CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name);
124 return;
125 }
126
127 cx18_flush_queues(s);
128
129 /*
130 * CX18_ENC_STREAM_TYPE_MPG needs to release the
131 * CX18_ENC_STREAM_TYPE_VBI and/or CX18_ENC_STREAM_TYPE_IDX streams.
132 *
133 * For all other streams we're done.
134 */
135 if (s->type != CX18_ENC_STREAM_TYPE_MPG)
136 return;
137
138 /* Unclaim the associated MPEG Index stream */
139 s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
140 if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) {
141 clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
142 cx18_flush_queues(s_assoc);
143 }
144
145 /* Unclaim the associated VBI stream */
146 s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
147 if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) {
148 if (s_assoc->id == -1) {
149 /*
150 * The VBI stream is not still claimed by a file
151 * descriptor, so completely unclaim it.
152 */
153 clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
154 cx18_flush_queues(s_assoc);
155 }
156 }
157}
158EXPORT_SYMBOL(cx18_release_stream);
159
160static void cx18_dualwatch(struct cx18 *cx)
161{
162 struct v4l2_tuner vt;
163 u32 new_stereo_mode;
164 const u32 dual = 0x0200;
165
166 new_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
167 memset(&vt, 0, sizeof(vt));
168 cx18_call_all(cx, tuner, g_tuner, &vt);
169 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
170 (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
171 new_stereo_mode = dual;
172
173 if (new_stereo_mode == cx->dualwatch_stereo_mode)
174 return;
175
176 CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
177 cx->dualwatch_stereo_mode, new_stereo_mode);
178 if (v4l2_ctrl_s_ctrl(cx->cxhdl.audio_mode, new_stereo_mode))
179 CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
180}
181
182
183static struct cx18_mdl *cx18_get_mdl(struct cx18_stream *s, int non_block,
184 int *err)
185{
186 struct cx18 *cx = s->cx;
187 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
188 struct cx18_mdl *mdl;
189 DEFINE_WAIT(wait);
190
191 *err = 0;
192 while (1) {
193 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
194 /* Process pending program updates and VBI data */
195 if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
196 cx->dualwatch_jiffies = jiffies;
197 cx18_dualwatch(cx);
198 }
199 if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
200 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
201 while ((mdl = cx18_dequeue(s_vbi,
202 &s_vbi->q_full))) {
203 /* byteswap and process VBI data */
204 cx18_process_vbi_data(cx, mdl,
205 s_vbi->type);
206 cx18_stream_put_mdl_fw(s_vbi, mdl);
207 }
208 }
209 mdl = &cx->vbi.sliced_mpeg_mdl;
210 if (mdl->readpos != mdl->bytesused)
211 return mdl;
212 }
213
214 /* do we have new data? */
215 mdl = cx18_dequeue(s, &s->q_full);
216 if (mdl) {
217 if (!test_and_clear_bit(CX18_F_M_NEED_SWAP,
218 &mdl->m_flags))
219 return mdl;
220 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
221 /* byteswap MPG data */
222 cx18_mdl_swap(mdl);
223 else {
224 /* byteswap and process VBI data */
225 cx18_process_vbi_data(cx, mdl, s->type);
226 }
227 return mdl;
228 }
229
230 /* return if end of stream */
231 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
232 CX18_DEBUG_INFO("EOS %s\n", s->name);
233 return NULL;
234 }
235
236 /* return if file was opened with O_NONBLOCK */
237 if (non_block) {
238 *err = -EAGAIN;
239 return NULL;
240 }
241
242 /* wait for more data to arrive */
243 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
244 /* New buffers might have become available before we were added
245 to the waitqueue */
246 if (!atomic_read(&s->q_full.depth))
247 schedule();
248 finish_wait(&s->waitq, &wait);
249 if (signal_pending(current)) {
250 /* return if a signal was received */
251 CX18_DEBUG_INFO("User stopped %s\n", s->name);
252 *err = -EINTR;
253 return NULL;
254 }
255 }
256}
257
258static void cx18_setup_sliced_vbi_mdl(struct cx18 *cx)
259{
260 struct cx18_mdl *mdl = &cx->vbi.sliced_mpeg_mdl;
261 struct cx18_buffer *buf = &cx->vbi.sliced_mpeg_buf;
262 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
263
264 buf->buf = cx->vbi.sliced_mpeg_data[idx];
265 buf->bytesused = cx->vbi.sliced_mpeg_size[idx];
266 buf->readpos = 0;
267
268 mdl->curr_buf = NULL;
269 mdl->bytesused = cx->vbi.sliced_mpeg_size[idx];
270 mdl->readpos = 0;
271}
272
273static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
274 struct cx18_buffer *buf, char __user *ubuf, size_t ucount, bool *stop)
275{
276 struct cx18 *cx = s->cx;
277 size_t len = buf->bytesused - buf->readpos;
278
279 *stop = false;
280 if (len > ucount)
281 len = ucount;
282 if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
283 !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
284 /*
285 * Try to find a good splice point in the PS, just before
286 * an MPEG-2 Program Pack start code, and provide only
287 * up to that point to the user, so it's easy to insert VBI data
288 * the next time around.
289 *
290 * This will not work for an MPEG-2 TS and has only been
291 * verified by analysis to work for an MPEG-2 PS. Helen Buus
292 * pointed out this works for the CX23416 MPEG-2 DVD compatible
293 * stream, and research indicates both the MPEG 2 SVCD and DVD
294 * stream types use an MPEG-2 PS container.
295 */
296 /*
297 * An MPEG-2 Program Stream (PS) is a series of
298 * MPEG-2 Program Packs terminated by an
299 * MPEG Program End Code after the last Program Pack.
300 * A Program Pack may hold a PS System Header packet and any
301 * number of Program Elementary Stream (PES) Packets
302 */
303 const char *start = buf->buf + buf->readpos;
304 const char *p = start + 1;
305 const u8 *q;
306 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
307 int stuffing, i;
308
309 while (start + len > p) {
310 /* Scan for a 0 to find a potential MPEG-2 start code */
311 q = memchr(p, 0, start + len - p);
312 if (q == NULL)
313 break;
314 p = q + 1;
315 /*
316 * Keep looking if not a
317 * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba
318 * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
319 */
320 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
321 q[1] != 0 || q[2] != 1 || q[3] != ch)
322 continue;
323
324 /* If expecting the primary video PES */
325 if (!cx->search_pack_header) {
326 /* Continue if it couldn't be a PES packet */
327 if ((q[6] & 0xc0) != 0x80)
328 continue;
329 /* Check if a PTS or PTS & DTS follow */
330 if (((q[7] & 0xc0) == 0x80 && /* PTS only */
331 (q[9] & 0xf0) == 0x20) || /* PTS only */
332 ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */
333 (q[9] & 0xf0) == 0x30)) { /* DTS follows */
334 /* Assume we found the video PES hdr */
335 ch = 0xba; /* next want a Program Pack*/
336 cx->search_pack_header = 1;
337 p = q + 9; /* Skip this video PES hdr */
338 }
339 continue;
340 }
341
342 /* We may have found a Program Pack start code */
343
344 /* Get the count of stuffing bytes & verify them */
345 stuffing = q[13] & 7;
346 /* all stuffing bytes must be 0xff */
347 for (i = 0; i < stuffing; i++)
348 if (q[14 + i] != 0xff)
349 break;
350 if (i == stuffing && /* right number of stuffing bytes*/
351 (q[4] & 0xc4) == 0x44 && /* marker check */
352 (q[12] & 3) == 3 && /* marker check */
353 q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
354 q[15 + stuffing] == 0 &&
355 q[16 + stuffing] == 1) {
356 /* We declare we actually found a Program Pack*/
357 cx->search_pack_header = 0; /* expect vid PES */
358 len = (char *)q - start;
359 cx18_setup_sliced_vbi_mdl(cx);
360 *stop = true;
361 break;
362 }
363 }
364 }
365 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
366 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
367 len, s->name);
368 return -EFAULT;
369 }
370 buf->readpos += len;
371 if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
372 buf != &cx->vbi.sliced_mpeg_buf)
373 cx->mpg_data_received += len;
374 return len;
375}
376
377static size_t cx18_copy_mdl_to_user(struct cx18_stream *s,
378 struct cx18_mdl *mdl, char __user *ubuf, size_t ucount)
379{
380 size_t tot_written = 0;
381 int rc;
382 bool stop = false;
383
384 if (mdl->curr_buf == NULL)
385 mdl->curr_buf = list_first_entry(&mdl->buf_list,
386 struct cx18_buffer, list);
387
388 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
389 /*
390 * For some reason we've exhausted the buffers, but the MDL
391 * object still said some data was unread.
392 * Fix that and bail out.
393 */
394 mdl->readpos = mdl->bytesused;
395 return 0;
396 }
397
398 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
399
400 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
401 continue;
402
403 rc = cx18_copy_buf_to_user(s, mdl->curr_buf, ubuf + tot_written,
404 ucount - tot_written, &stop);
405 if (rc < 0)
406 return rc;
407 mdl->readpos += rc;
408 tot_written += rc;
409
410 if (stop || /* Forced stopping point for VBI insertion */
411 tot_written >= ucount || /* Reader request statisfied */
412 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
413 mdl->readpos >= mdl->bytesused) /* MDL buffers drained */
414 break;
415 }
416 return tot_written;
417}
418
419static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
420 size_t tot_count, int non_block)
421{
422 struct cx18 *cx = s->cx;
423 size_t tot_written = 0;
424 int single_frame = 0;
425
426 if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
427 /* shouldn't happen */
428 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
429 s->name);
430 return -EIO;
431 }
432
433 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
434 frames should arrive one-by-one, so make sure we never output more
435 than one VBI frame at a time */
436 if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
437 single_frame = 1;
438
439 for (;;) {
440 struct cx18_mdl *mdl;
441 int rc;
442
443 mdl = cx18_get_mdl(s, non_block, &rc);
444 /* if there is no data available... */
445 if (mdl == NULL) {
446 /* if we got data, then return that regardless */
447 if (tot_written)
448 break;
449 /* EOS condition */
450 if (rc == 0) {
451 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
452 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
453 cx18_release_stream(s);
454 }
455 /* set errno */
456 return rc;
457 }
458
459 rc = cx18_copy_mdl_to_user(s, mdl, ubuf + tot_written,
460 tot_count - tot_written);
461
462 if (mdl != &cx->vbi.sliced_mpeg_mdl) {
463 if (mdl->readpos == mdl->bytesused)
464 cx18_stream_put_mdl_fw(s, mdl);
465 else
466 cx18_push(s, mdl, &s->q_full);
467 } else if (mdl->readpos == mdl->bytesused) {
468 int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
469
470 cx->vbi.sliced_mpeg_size[idx] = 0;
471 cx->vbi.inserted_frame++;
472 cx->vbi_data_inserted += mdl->bytesused;
473 }
474 if (rc < 0)
475 return rc;
476 tot_written += rc;
477
478 if (tot_written == tot_count || single_frame)
479 break;
480 }
481 return tot_written;
482}
483
484static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
485 size_t count, loff_t *pos, int non_block)
486{
487 ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
488 struct cx18 *cx = s->cx;
489
490 CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
491 if (rc > 0)
492 pos += rc;
493 return rc;
494}
495
496int cx18_start_capture(struct cx18_open_id *id)
497{
498 struct cx18 *cx = id->cx;
499 struct cx18_stream *s = &cx->streams[id->type];
500 struct cx18_stream *s_vbi;
501 struct cx18_stream *s_idx;
502
503 if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
504 /* you cannot read from these stream types. */
505 return -EPERM;
506 }
507
508 /* Try to claim this stream. */
509 if (cx18_claim_stream(id, s->type))
510 return -EBUSY;
511
512 /* If capture is already in progress, then we also have to
513 do nothing extra. */
514 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
515 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
516 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
517 return 0;
518 }
519
520 /* Start associated VBI or IDX stream capture if required */
521 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
522 s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
523 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
524 /*
525 * The VBI and IDX streams should have been claimed
526 * automatically, if for internal use, when the MPG stream was
527 * claimed. We only need to start these streams capturing.
528 */
529 if (test_bit(CX18_F_S_INTERNAL_USE, &s_idx->s_flags) &&
530 !test_and_set_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
531 if (cx18_start_v4l2_encode_stream(s_idx)) {
532 CX18_DEBUG_WARN("IDX capture start failed\n");
533 clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags);
534 goto start_failed;
535 }
536 CX18_DEBUG_INFO("IDX capture started\n");
537 }
538 if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
539 !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
540 if (cx18_start_v4l2_encode_stream(s_vbi)) {
541 CX18_DEBUG_WARN("VBI capture start failed\n");
542 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
543 goto start_failed;
544 }
545 CX18_DEBUG_INFO("VBI insertion started\n");
546 }
547 }
548
549 /* Tell the card to start capturing */
550 if (!cx18_start_v4l2_encode_stream(s)) {
551 /* We're done */
552 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
553 /* Resume a possibly paused encoder */
554 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
555 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
556 return 0;
557 }
558
559start_failed:
560 CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
561
562 /*
563 * The associated VBI and IDX streams for internal use are released
564 * automatically when the MPG stream is released. We only need to stop
565 * the associated stream.
566 */
567 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
568 /* Stop the IDX stream which is always for internal use */
569 if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
570 cx18_stop_v4l2_encode_stream(s_idx, 0);
571 clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags);
572 }
573 /* Stop the VBI stream, if only running for internal use */
574 if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
575 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
576 cx18_stop_v4l2_encode_stream(s_vbi, 0);
577 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
578 }
579 }
580 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
581 cx18_release_stream(s); /* Also releases associated streams */
582 return -EIO;
583}
584
585ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
586 loff_t *pos)
587{
588 struct cx18_open_id *id = file2id(filp);
589 struct cx18 *cx = id->cx;
590 struct cx18_stream *s = &cx->streams[id->type];
591 int rc;
592
593 CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
594
595 mutex_lock(&cx->serialize_lock);
596 rc = cx18_start_capture(id);
597 mutex_unlock(&cx->serialize_lock);
598 if (rc)
599 return rc;
600
601 if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
602 (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
603 return videobuf_read_stream(&s->vbuf_q, buf, count, pos, 0,
604 filp->f_flags & O_NONBLOCK);
605 }
606
607 return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
608}
609
610unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
611{
612 struct cx18_open_id *id = file2id(filp);
613 struct cx18 *cx = id->cx;
614 struct cx18_stream *s = &cx->streams[id->type];
615 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
616
617 /* Start a capture if there is none */
618 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
619 int rc;
620
621 mutex_lock(&cx->serialize_lock);
622 rc = cx18_start_capture(id);
623 mutex_unlock(&cx->serialize_lock);
624 if (rc) {
625 CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
626 s->name, rc);
627 return POLLERR;
628 }
629 CX18_DEBUG_FILE("Encoder poll started capture\n");
630 }
631
632 if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
633 (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
634 int videobuf_poll = videobuf_poll_stream(filp, &s->vbuf_q, wait);
635 if (eof && videobuf_poll == POLLERR)
636 return POLLHUP;
637 else
638 return videobuf_poll;
639 }
640
641 /* add stream's waitq to the poll list */
642 CX18_DEBUG_HI_FILE("Encoder poll\n");
643 poll_wait(filp, &s->waitq, wait);
644
645 if (atomic_read(&s->q_full.depth))
646 return POLLIN | POLLRDNORM;
647 if (eof)
648 return POLLHUP;
649 return 0;
650}
651
652int cx18_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
653{
654 struct cx18_open_id *id = file->private_data;
655 struct cx18 *cx = id->cx;
656 struct cx18_stream *s = &cx->streams[id->type];
657 int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
658
659 if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
660 (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
661
662 /* Start a capture if there is none */
663 if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
664 int rc;
665
666 mutex_lock(&cx->serialize_lock);
667 rc = cx18_start_capture(id);
668 mutex_unlock(&cx->serialize_lock);
669 if (rc) {
670 CX18_DEBUG_INFO(
671 "Could not start capture for %s (%d)\n",
672 s->name, rc);
673 return -EINVAL;
674 }
675 CX18_DEBUG_FILE("Encoder mmap started capture\n");
676 }
677
678 return videobuf_mmap_mapper(&s->vbuf_q, vma);
679 }
680
681 return -EINVAL;
682}
683
684void cx18_vb_timeout(unsigned long data)
685{
686 struct cx18_stream *s = (struct cx18_stream *)data;
687 struct cx18_videobuf_buffer *buf;
688 unsigned long flags;
689
690 /* Return all of the buffers in error state, so the vbi/vid inode
691 * can return from blocking.
692 */
693 spin_lock_irqsave(&s->vb_lock, flags);
694 while (!list_empty(&s->vb_capture)) {
695 buf = list_entry(s->vb_capture.next,
696 struct cx18_videobuf_buffer, vb.queue);
697 list_del(&buf->vb.queue);
698 buf->vb.state = VIDEOBUF_ERROR;
699 wake_up(&buf->vb.done);
700 }
701 spin_unlock_irqrestore(&s->vb_lock, flags);
702}
703
704void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
705{
706 struct cx18 *cx = id->cx;
707 struct cx18_stream *s = &cx->streams[id->type];
708 struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
709 struct cx18_stream *s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
710
711 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
712
713 /* 'Unclaim' this stream */
714
715 /* Stop capturing */
716 if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
717 CX18_DEBUG_INFO("close stopping capture\n");
718 if (id->type == CX18_ENC_STREAM_TYPE_MPG) {
719 /* Stop internal use associated VBI and IDX streams */
720 if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
721 !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
722 CX18_DEBUG_INFO("close stopping embedded VBI "
723 "capture\n");
724 cx18_stop_v4l2_encode_stream(s_vbi, 0);
725 }
726 if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
727 CX18_DEBUG_INFO("close stopping IDX capture\n");
728 cx18_stop_v4l2_encode_stream(s_idx, 0);
729 }
730 }
731 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
732 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
733 /* Also used internally, don't stop capturing */
734 s->id = -1;
735 else
736 cx18_stop_v4l2_encode_stream(s, gop_end);
737 }
738 if (!gop_end) {
739 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
740 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
741 cx18_release_stream(s);
742 }
743}
744
745int cx18_v4l2_close(struct file *filp)
746{
747 struct v4l2_fh *fh = filp->private_data;
748 struct cx18_open_id *id = fh2id(fh);
749 struct cx18 *cx = id->cx;
750 struct cx18_stream *s = &cx->streams[id->type];
751
752 CX18_DEBUG_IOCTL("close() of %s\n", s->name);
753
754 mutex_lock(&cx->serialize_lock);
755 /* Stop radio */
756 if (id->type == CX18_ENC_STREAM_TYPE_RAD &&
757 v4l2_fh_is_singular_file(filp)) {
758 /* Closing radio device, return to TV mode */
759 cx18_mute(cx);
760 /* Mark that the radio is no longer in use */
761 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
762 /* Switch tuner to TV */
763 cx18_call_all(cx, core, s_std, cx->std);
764 /* Select correct audio input (i.e. TV tuner or Line in) */
765 cx18_audio_set_io(cx);
766 if (atomic_read(&cx->ana_capturing) > 0) {
767 /* Undo video mute */
768 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
769 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute) |
770 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8)));
771 }
772 /* Done! Unmute and continue. */
773 cx18_unmute(cx);
774 }
775
776 v4l2_fh_del(fh);
777 v4l2_fh_exit(fh);
778
779 /* 'Unclaim' this stream */
780 if (s->id == id->open_id)
781 cx18_stop_capture(id, 0);
782 kfree(id);
783 mutex_unlock(&cx->serialize_lock);
784 return 0;
785}
786
787static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
788{
789 struct cx18 *cx = s->cx;
790 struct cx18_open_id *item;
791
792 CX18_DEBUG_FILE("open %s\n", s->name);
793
794 /* Allocate memory */
795 item = kzalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
796 if (NULL == item) {
797 CX18_DEBUG_WARN("nomem on v4l2 open\n");
798 return -ENOMEM;
799 }
800 v4l2_fh_init(&item->fh, s->video_dev);
801
802 item->cx = cx;
803 item->type = s->type;
804
805 item->open_id = cx->open_id++;
806 filp->private_data = &item->fh;
807 v4l2_fh_add(&item->fh);
808
809 if (item->type == CX18_ENC_STREAM_TYPE_RAD &&
810 v4l2_fh_is_singular_file(filp)) {
811 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
812 if (atomic_read(&cx->ana_capturing) > 0) {
813 /* switching to radio while capture is
814 in progress is not polite */
815 v4l2_fh_del(&item->fh);
816 v4l2_fh_exit(&item->fh);
817 kfree(item);
818 return -EBUSY;
819 }
820 }
821
822 /* Mark that the radio is being used. */
823 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
824 /* We have the radio */
825 cx18_mute(cx);
826 /* Switch tuner to radio */
827 cx18_call_all(cx, tuner, s_radio);
828 /* Select the correct audio input (i.e. radio tuner) */
829 cx18_audio_set_io(cx);
830 /* Done! Unmute and continue. */
831 cx18_unmute(cx);
832 }
833 return 0;
834}
835
836int cx18_v4l2_open(struct file *filp)
837{
838 int res;
839 struct video_device *video_dev = video_devdata(filp);
840 struct cx18_stream *s = video_get_drvdata(video_dev);
841 struct cx18 *cx = s->cx;
842
843 mutex_lock(&cx->serialize_lock);
844 if (cx18_init_on_first_open(cx)) {
845 CX18_ERR("Failed to initialize on %s\n",
846 video_device_node_name(video_dev));
847 mutex_unlock(&cx->serialize_lock);
848 return -ENXIO;
849 }
850 res = cx18_serialized_open(s, filp);
851 mutex_unlock(&cx->serialize_lock);
852 return res;
853}
854
855void cx18_mute(struct cx18 *cx)
856{
857 u32 h;
858 if (atomic_read(&cx->ana_capturing)) {
859 h = cx18_find_handle(cx);
860 if (h != CX18_INVALID_TASK_HANDLE)
861 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
862 else
863 CX18_ERR("Can't find valid task handle for mute\n");
864 }
865 CX18_DEBUG_INFO("Mute\n");
866}
867
868void cx18_unmute(struct cx18 *cx)
869{
870 u32 h;
871 if (atomic_read(&cx->ana_capturing)) {
872 h = cx18_find_handle(cx);
873 if (h != CX18_INVALID_TASK_HANDLE) {
874 cx18_msleep_timeout(100, 0);
875 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
876 cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
877 } else
878 CX18_ERR("Can't find valid task handle for unmute\n");
879 }
880 CX18_DEBUG_INFO("Unmute\n");
881}
diff --git a/drivers/media/pci/cx18/cx18-fileops.h b/drivers/media/pci/cx18/cx18-fileops.h
new file mode 100644
index 000000000000..b9e5110ad043
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-fileops.h
@@ -0,0 +1,41 @@
1/*
2 * cx18 file operation functions
3 *
4 * Derived from ivtv-fileops.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24/* Testing/Debugging */
25int cx18_v4l2_open(struct file *filp);
26ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
27 loff_t *pos);
28ssize_t cx18_v4l2_write(struct file *filp, const char __user *buf, size_t count,
29 loff_t *pos);
30int cx18_v4l2_close(struct file *filp);
31unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait);
32int cx18_start_capture(struct cx18_open_id *id);
33void cx18_stop_capture(struct cx18_open_id *id, int gop_end);
34void cx18_mute(struct cx18 *cx);
35void cx18_unmute(struct cx18 *cx);
36int cx18_v4l2_mmap(struct file *file, struct vm_area_struct *vma);
37void cx18_vb_timeout(unsigned long data);
38
39/* Shared with cx18-alsa module */
40int cx18_claim_stream(struct cx18_open_id *id, int type);
41void cx18_release_stream(struct cx18_stream *s);
diff --git a/drivers/media/pci/cx18/cx18-firmware.c b/drivers/media/pci/cx18/cx18-firmware.c
new file mode 100644
index 000000000000..a1c1cec05f98
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-firmware.c
@@ -0,0 +1,459 @@
1/*
2 * cx18 firmware functions
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#include "cx18-driver.h"
24#include "cx18-io.h"
25#include "cx18-scb.h"
26#include "cx18-irq.h"
27#include "cx18-firmware.h"
28#include "cx18-cards.h"
29#include <linux/firmware.h>
30
31#define CX18_PROC_SOFT_RESET 0xc70010
32#define CX18_DDR_SOFT_RESET 0xc70014
33#define CX18_CLOCK_SELECT1 0xc71000
34#define CX18_CLOCK_SELECT2 0xc71004
35#define CX18_HALF_CLOCK_SELECT1 0xc71008
36#define CX18_HALF_CLOCK_SELECT2 0xc7100C
37#define CX18_CLOCK_POLARITY1 0xc71010
38#define CX18_CLOCK_POLARITY2 0xc71014
39#define CX18_ADD_DELAY_ENABLE1 0xc71018
40#define CX18_ADD_DELAY_ENABLE2 0xc7101C
41#define CX18_CLOCK_ENABLE1 0xc71020
42#define CX18_CLOCK_ENABLE2 0xc71024
43
44#define CX18_REG_BUS_TIMEOUT_EN 0xc72024
45
46#define CX18_FAST_CLOCK_PLL_INT 0xc78000
47#define CX18_FAST_CLOCK_PLL_FRAC 0xc78004
48#define CX18_FAST_CLOCK_PLL_POST 0xc78008
49#define CX18_FAST_CLOCK_PLL_PRESCALE 0xc7800C
50#define CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH 0xc78010
51
52#define CX18_SLOW_CLOCK_PLL_INT 0xc78014
53#define CX18_SLOW_CLOCK_PLL_FRAC 0xc78018
54#define CX18_SLOW_CLOCK_PLL_POST 0xc7801C
55#define CX18_MPEG_CLOCK_PLL_INT 0xc78040
56#define CX18_MPEG_CLOCK_PLL_FRAC 0xc78044
57#define CX18_MPEG_CLOCK_PLL_POST 0xc78048
58#define CX18_PLL_POWER_DOWN 0xc78088
59#define CX18_SW1_INT_STATUS 0xc73104
60#define CX18_SW1_INT_ENABLE_PCI 0xc7311C
61#define CX18_SW2_INT_SET 0xc73140
62#define CX18_SW2_INT_STATUS 0xc73144
63#define CX18_ADEC_CONTROL 0xc78120
64
65#define CX18_DDR_REQUEST_ENABLE 0xc80000
66#define CX18_DDR_CHIP_CONFIG 0xc80004
67#define CX18_DDR_REFRESH 0xc80008
68#define CX18_DDR_TIMING1 0xc8000C
69#define CX18_DDR_TIMING2 0xc80010
70#define CX18_DDR_POWER_REG 0xc8001C
71
72#define CX18_DDR_TUNE_LANE 0xc80048
73#define CX18_DDR_INITIAL_EMRS 0xc80054
74#define CX18_DDR_MB_PER_ROW_7 0xc8009C
75#define CX18_DDR_BASE_63_ADDR 0xc804FC
76
77#define CX18_WMB_CLIENT02 0xc90108
78#define CX18_WMB_CLIENT05 0xc90114
79#define CX18_WMB_CLIENT06 0xc90118
80#define CX18_WMB_CLIENT07 0xc9011C
81#define CX18_WMB_CLIENT08 0xc90120
82#define CX18_WMB_CLIENT09 0xc90124
83#define CX18_WMB_CLIENT10 0xc90128
84#define CX18_WMB_CLIENT11 0xc9012C
85#define CX18_WMB_CLIENT12 0xc90130
86#define CX18_WMB_CLIENT13 0xc90134
87#define CX18_WMB_CLIENT14 0xc90138
88
89#define CX18_DSP0_INTERRUPT_MASK 0xd0004C
90
91#define APU_ROM_SYNC1 0x6D676553 /* "mgeS" */
92#define APU_ROM_SYNC2 0x72646548 /* "rdeH" */
93
94struct cx18_apu_rom_seghdr {
95 u32 sync1;
96 u32 sync2;
97 u32 addr;
98 u32 size;
99};
100
101static int load_cpu_fw_direct(const char *fn, u8 __iomem *mem, struct cx18 *cx)
102{
103 const struct firmware *fw = NULL;
104 int i, j;
105 unsigned size;
106 u32 __iomem *dst = (u32 __iomem *)mem;
107 const u32 *src;
108
109 if (request_firmware(&fw, fn, &cx->pci_dev->dev)) {
110 CX18_ERR("Unable to open firmware %s\n", fn);
111 CX18_ERR("Did you put the firmware in the hotplug firmware directory?\n");
112 return -ENOMEM;
113 }
114
115 src = (const u32 *)fw->data;
116
117 for (i = 0; i < fw->size; i += 4096) {
118 cx18_setup_page(cx, i);
119 for (j = i; j < fw->size && j < i + 4096; j += 4) {
120 /* no need for endianness conversion on the ppc */
121 cx18_raw_writel(cx, *src, dst);
122 if (cx18_raw_readl(cx, dst) != *src) {
123 CX18_ERR("Mismatch at offset %x\n", i);
124 release_firmware(fw);
125 cx18_setup_page(cx, 0);
126 return -EIO;
127 }
128 dst++;
129 src++;
130 }
131 }
132 if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
133 CX18_INFO("loaded %s firmware (%zd bytes)\n", fn, fw->size);
134 size = fw->size;
135 release_firmware(fw);
136 cx18_setup_page(cx, SCB_OFFSET);
137 return size;
138}
139
140static int load_apu_fw_direct(const char *fn, u8 __iomem *dst, struct cx18 *cx,
141 u32 *entry_addr)
142{
143 const struct firmware *fw = NULL;
144 int i, j;
145 unsigned size;
146 const u32 *src;
147 struct cx18_apu_rom_seghdr seghdr;
148 const u8 *vers;
149 u32 offset = 0;
150 u32 apu_version = 0;
151 int sz;
152
153 if (request_firmware(&fw, fn, &cx->pci_dev->dev)) {
154 CX18_ERR("unable to open firmware %s\n", fn);
155 CX18_ERR("did you put the firmware in the hotplug firmware directory?\n");
156 cx18_setup_page(cx, 0);
157 return -ENOMEM;
158 }
159
160 *entry_addr = 0;
161 src = (const u32 *)fw->data;
162 vers = fw->data + sizeof(seghdr);
163 sz = fw->size;
164
165 apu_version = (vers[0] << 24) | (vers[4] << 16) | vers[32];
166 while (offset + sizeof(seghdr) < fw->size) {
167 const u32 *shptr = src + offset / 4;
168
169 seghdr.sync1 = le32_to_cpu(shptr[0]);
170 seghdr.sync2 = le32_to_cpu(shptr[1]);
171 seghdr.addr = le32_to_cpu(shptr[2]);
172 seghdr.size = le32_to_cpu(shptr[3]);
173
174 offset += sizeof(seghdr);
175 if (seghdr.sync1 != APU_ROM_SYNC1 ||
176 seghdr.sync2 != APU_ROM_SYNC2) {
177 offset += seghdr.size;
178 continue;
179 }
180 CX18_DEBUG_INFO("load segment %x-%x\n", seghdr.addr,
181 seghdr.addr + seghdr.size - 1);
182 if (*entry_addr == 0)
183 *entry_addr = seghdr.addr;
184 if (offset + seghdr.size > sz)
185 break;
186 for (i = 0; i < seghdr.size; i += 4096) {
187 cx18_setup_page(cx, seghdr.addr + i);
188 for (j = i; j < seghdr.size && j < i + 4096; j += 4) {
189 /* no need for endianness conversion on the ppc */
190 cx18_raw_writel(cx, src[(offset + j) / 4],
191 dst + seghdr.addr + j);
192 if (cx18_raw_readl(cx, dst + seghdr.addr + j)
193 != src[(offset + j) / 4]) {
194 CX18_ERR("Mismatch at offset %x\n",
195 offset + j);
196 release_firmware(fw);
197 cx18_setup_page(cx, 0);
198 return -EIO;
199 }
200 }
201 }
202 offset += seghdr.size;
203 }
204 if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
205 CX18_INFO("loaded %s firmware V%08x (%zd bytes)\n",
206 fn, apu_version, fw->size);
207 size = fw->size;
208 release_firmware(fw);
209 cx18_setup_page(cx, 0);
210 return size;
211}
212
213void cx18_halt_firmware(struct cx18 *cx)
214{
215 CX18_DEBUG_INFO("Preparing for firmware halt.\n");
216 cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,
217 0x0000000F, 0x000F000F);
218 cx18_write_reg_expect(cx, 0x00020002, CX18_ADEC_CONTROL,
219 0x00000002, 0x00020002);
220}
221
222void cx18_init_power(struct cx18 *cx, int lowpwr)
223{
224 /* power-down Spare and AOM PLLs */
225 /* power-up fast, slow and mpeg PLLs */
226 cx18_write_reg(cx, 0x00000008, CX18_PLL_POWER_DOWN);
227
228 /* ADEC out of sleep */
229 cx18_write_reg_expect(cx, 0x00020000, CX18_ADEC_CONTROL,
230 0x00000000, 0x00020002);
231
232 /*
233 * The PLL parameters are based on the external crystal frequency that
234 * would ideally be:
235 *
236 * NTSC Color subcarrier freq * 8 =
237 * 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
238 *
239 * The accidents of history and rationale that explain from where this
240 * combination of magic numbers originate can be found in:
241 *
242 * [1] Abrahams, I. C., "Choice of Chrominance Subcarrier Frequency in
243 * the NTSC Standards", Proceedings of the I-R-E, January 1954, pp 79-80
244 *
245 * [2] Abrahams, I. C., "The 'Frequency Interleaving' Principle in the
246 * NTSC Standards", Proceedings of the I-R-E, January 1954, pp 81-83
247 *
248 * As Mike Bradley has rightly pointed out, it's not the exact crystal
249 * frequency that matters, only that all parts of the driver and
250 * firmware are using the same value (close to the ideal value).
251 *
252 * Since I have a strong suspicion that, if the firmware ever assumes a
253 * crystal value at all, it will assume 28.636360 MHz, the crystal
254 * freq used in calculations in this driver will be:
255 *
256 * xtal_freq = 28.636360 MHz
257 *
258 * an error of less than 0.13 ppm which is way, way better than any off
259 * the shelf crystal will have for accuracy anyway.
260 *
261 * Below I aim to run the PLLs' VCOs near 400 MHz to minimze errors.
262 *
263 * Many thanks to Jeff Campbell and Mike Bradley for their extensive
264 * investigation, experimentation, testing, and suggested solutions of
265 * of audio/video sync problems with SVideo and CVBS captures.
266 */
267
268 /* the fast clock is at 200/245 MHz */
269 /* 1 * xtal_freq * 0x0d.f7df9b8 / 2 = 200 MHz: 400 MHz pre post-divide*/
270 /* 1 * xtal_freq * 0x11.1c71eb8 / 2 = 245 MHz: 490 MHz pre post-divide*/
271 cx18_write_reg(cx, lowpwr ? 0xD : 0x11, CX18_FAST_CLOCK_PLL_INT);
272 cx18_write_reg(cx, lowpwr ? 0x1EFBF37 : 0x038E3D7,
273 CX18_FAST_CLOCK_PLL_FRAC);
274
275 cx18_write_reg(cx, 2, CX18_FAST_CLOCK_PLL_POST);
276 cx18_write_reg(cx, 1, CX18_FAST_CLOCK_PLL_PRESCALE);
277 cx18_write_reg(cx, 4, CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH);
278
279 /* set slow clock to 125/120 MHz */
280 /* xtal_freq * 0x0d.1861a20 / 3 = 125 MHz: 375 MHz before post-divide */
281 /* xtal_freq * 0x0c.92493f8 / 3 = 120 MHz: 360 MHz before post-divide */
282 cx18_write_reg(cx, lowpwr ? 0xD : 0xC, CX18_SLOW_CLOCK_PLL_INT);
283 cx18_write_reg(cx, lowpwr ? 0x30C344 : 0x124927F,
284 CX18_SLOW_CLOCK_PLL_FRAC);
285 cx18_write_reg(cx, 3, CX18_SLOW_CLOCK_PLL_POST);
286
287 /* mpeg clock pll 54MHz */
288 /* xtal_freq * 0xf.15f17f0 / 8 = 54 MHz: 432 MHz before post-divide */
289 cx18_write_reg(cx, 0xF, CX18_MPEG_CLOCK_PLL_INT);
290 cx18_write_reg(cx, 0x2BE2FE, CX18_MPEG_CLOCK_PLL_FRAC);
291 cx18_write_reg(cx, 8, CX18_MPEG_CLOCK_PLL_POST);
292
293 /* Defaults */
294 /* APU = SC or SC/2 = 125/62.5 */
295 /* EPU = SC = 125 */
296 /* DDR = FC = 180 */
297 /* ENC = SC = 125 */
298 /* AI1 = SC = 125 */
299 /* VIM2 = disabled */
300 /* PCI = FC/2 = 90 */
301 /* AI2 = disabled */
302 /* DEMUX = disabled */
303 /* AO = SC/2 = 62.5 */
304 /* SER = 54MHz */
305 /* VFC = disabled */
306 /* USB = disabled */
307
308 if (lowpwr) {
309 cx18_write_reg_expect(cx, 0xFFFF0020, CX18_CLOCK_SELECT1,
310 0x00000020, 0xFFFFFFFF);
311 cx18_write_reg_expect(cx, 0xFFFF0004, CX18_CLOCK_SELECT2,
312 0x00000004, 0xFFFFFFFF);
313 } else {
314 /* This doesn't explicitly set every clock select */
315 cx18_write_reg_expect(cx, 0x00060004, CX18_CLOCK_SELECT1,
316 0x00000004, 0x00060006);
317 cx18_write_reg_expect(cx, 0x00060006, CX18_CLOCK_SELECT2,
318 0x00000006, 0x00060006);
319 }
320
321 cx18_write_reg_expect(cx, 0xFFFF0002, CX18_HALF_CLOCK_SELECT1,
322 0x00000002, 0xFFFFFFFF);
323 cx18_write_reg_expect(cx, 0xFFFF0104, CX18_HALF_CLOCK_SELECT2,
324 0x00000104, 0xFFFFFFFF);
325 cx18_write_reg_expect(cx, 0xFFFF9026, CX18_CLOCK_ENABLE1,
326 0x00009026, 0xFFFFFFFF);
327 cx18_write_reg_expect(cx, 0xFFFF3105, CX18_CLOCK_ENABLE2,
328 0x00003105, 0xFFFFFFFF);
329}
330
331void cx18_init_memory(struct cx18 *cx)
332{
333 cx18_msleep_timeout(10, 0);
334 cx18_write_reg_expect(cx, 0x00010000, CX18_DDR_SOFT_RESET,
335 0x00000000, 0x00010001);
336 cx18_msleep_timeout(10, 0);
337
338 cx18_write_reg(cx, cx->card->ddr.chip_config, CX18_DDR_CHIP_CONFIG);
339
340 cx18_msleep_timeout(10, 0);
341
342 cx18_write_reg(cx, cx->card->ddr.refresh, CX18_DDR_REFRESH);
343 cx18_write_reg(cx, cx->card->ddr.timing1, CX18_DDR_TIMING1);
344 cx18_write_reg(cx, cx->card->ddr.timing2, CX18_DDR_TIMING2);
345
346 cx18_msleep_timeout(10, 0);
347
348 /* Initialize DQS pad time */
349 cx18_write_reg(cx, cx->card->ddr.tune_lane, CX18_DDR_TUNE_LANE);
350 cx18_write_reg(cx, cx->card->ddr.initial_emrs, CX18_DDR_INITIAL_EMRS);
351
352 cx18_msleep_timeout(10, 0);
353
354 cx18_write_reg_expect(cx, 0x00020000, CX18_DDR_SOFT_RESET,
355 0x00000000, 0x00020002);
356 cx18_msleep_timeout(10, 0);
357
358 /* use power-down mode when idle */
359 cx18_write_reg(cx, 0x00000010, CX18_DDR_POWER_REG);
360
361 cx18_write_reg_expect(cx, 0x00010001, CX18_REG_BUS_TIMEOUT_EN,
362 0x00000001, 0x00010001);
363
364 cx18_write_reg(cx, 0x48, CX18_DDR_MB_PER_ROW_7);
365 cx18_write_reg(cx, 0xE0000, CX18_DDR_BASE_63_ADDR);
366
367 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT02); /* AO */
368 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT09); /* AI2 */
369 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT05); /* VIM1 */
370 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT06); /* AI1 */
371 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT07); /* 3D comb */
372 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT10); /* ME */
373 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT12); /* ENC */
374 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT13); /* PK */
375 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT11); /* RC */
376 cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT14); /* AVO */
377}
378
379#define CX18_CPU_FIRMWARE "v4l-cx23418-cpu.fw"
380#define CX18_APU_FIRMWARE "v4l-cx23418-apu.fw"
381
382int cx18_firmware_init(struct cx18 *cx)
383{
384 u32 fw_entry_addr;
385 int sz, retries;
386 u32 api_args[MAX_MB_ARGUMENTS];
387
388 /* Allow chip to control CLKRUN */
389 cx18_write_reg(cx, 0x5, CX18_DSP0_INTERRUPT_MASK);
390
391 /* Stop the firmware */
392 cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,
393 0x0000000F, 0x000F000F);
394
395 cx18_msleep_timeout(1, 0);
396
397 /* If the CPU is still running */
398 if ((cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 8) == 0) {
399 CX18_ERR("%s: couldn't stop CPU to load firmware\n", __func__);
400 return -EIO;
401 }
402
403 cx18_sw1_irq_enable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
404 cx18_sw2_irq_enable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
405
406 sz = load_cpu_fw_direct(CX18_CPU_FIRMWARE, cx->enc_mem, cx);
407 if (sz <= 0)
408 return sz;
409
410 /* The SCB & IPC area *must* be correct before starting the firmwares */
411 cx18_init_scb(cx);
412
413 fw_entry_addr = 0;
414 sz = load_apu_fw_direct(CX18_APU_FIRMWARE, cx->enc_mem, cx,
415 &fw_entry_addr);
416 if (sz <= 0)
417 return sz;
418
419 /* Start the CPU. The CPU will take care of the APU for us. */
420 cx18_write_reg_expect(cx, 0x00080000, CX18_PROC_SOFT_RESET,
421 0x00000000, 0x00080008);
422
423 /* Wait up to 500 ms for the APU to come out of reset */
424 for (retries = 0;
425 retries < 50 && (cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 1) == 1;
426 retries++)
427 cx18_msleep_timeout(10, 0);
428
429 cx18_msleep_timeout(200, 0);
430
431 if (retries == 50 &&
432 (cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 1) == 1) {
433 CX18_ERR("Could not start the CPU\n");
434 return -EIO;
435 }
436
437 /*
438 * The CPU had once before set up to receive an interrupt for it's
439 * outgoing IRQ_CPU_TO_EPU_ACK to us. If it ever does this, we get an
440 * interrupt when it sends us an ack, but by the time we process it,
441 * that flag in the SW2 status register has been cleared by the CPU
442 * firmware. We'll prevent that not so useful condition from happening
443 * by clearing the CPU's interrupt enables for Ack IRQ's we want to
444 * process.
445 */
446 cx18_sw2_irq_disable_cpu(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
447
448 /* Try a benign command to see if the CPU is alive and well */
449 sz = cx18_vapi_result(cx, api_args, CX18_CPU_DEBUG_PEEK32, 1, 0);
450 if (sz < 0)
451 return sz;
452
453 /* initialize GPIO */
454 cx18_write_reg_expect(cx, 0x14001400, 0xc78110, 0x00001400, 0x14001400);
455 return 0;
456}
457
458MODULE_FIRMWARE(CX18_CPU_FIRMWARE);
459MODULE_FIRMWARE(CX18_APU_FIRMWARE);
diff --git a/drivers/media/pci/cx18/cx18-firmware.h b/drivers/media/pci/cx18/cx18-firmware.h
new file mode 100644
index 000000000000..38d4c05e8499
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-firmware.h
@@ -0,0 +1,25 @@
1/*
2 * cx18 firmware functions
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 */
21
22int cx18_firmware_init(struct cx18 *cx);
23void cx18_halt_firmware(struct cx18 *cx);
24void cx18_init_memory(struct cx18 *cx);
25void cx18_init_power(struct cx18 *cx, int lowpwr);
diff --git a/drivers/media/pci/cx18/cx18-gpio.c b/drivers/media/pci/cx18/cx18-gpio.c
new file mode 100644
index 000000000000..5374aeb0cd22
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-gpio.c
@@ -0,0 +1,347 @@
1/*
2 * cx18 gpio functions
3 *
4 * Derived from ivtv-gpio.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-io.h"
27#include "cx18-cards.h"
28#include "cx18-gpio.h"
29#include "tuner-xc2028.h"
30
31/********************* GPIO stuffs *********************/
32
33/* GPIO registers */
34#define CX18_REG_GPIO_IN 0xc72010
35#define CX18_REG_GPIO_OUT1 0xc78100
36#define CX18_REG_GPIO_DIR1 0xc78108
37#define CX18_REG_GPIO_OUT2 0xc78104
38#define CX18_REG_GPIO_DIR2 0xc7810c
39
40/*
41 * HVR-1600 GPIO pins, courtesy of Hauppauge:
42 *
43 * gpio0: zilog ir process reset pin
44 * gpio1: zilog programming pin (you should never use this)
45 * gpio12: cx24227 reset pin
46 * gpio13: cs5345 reset pin
47*/
48
49/*
50 * File scope utility functions
51 */
52static void gpio_write(struct cx18 *cx)
53{
54 u32 dir_lo = cx->gpio_dir & 0xffff;
55 u32 val_lo = cx->gpio_val & 0xffff;
56 u32 dir_hi = cx->gpio_dir >> 16;
57 u32 val_hi = cx->gpio_val >> 16;
58
59 cx18_write_reg_expect(cx, dir_lo << 16,
60 CX18_REG_GPIO_DIR1, ~dir_lo, dir_lo);
61 cx18_write_reg_expect(cx, (dir_lo << 16) | val_lo,
62 CX18_REG_GPIO_OUT1, val_lo, dir_lo);
63 cx18_write_reg_expect(cx, dir_hi << 16,
64 CX18_REG_GPIO_DIR2, ~dir_hi, dir_hi);
65 cx18_write_reg_expect(cx, (dir_hi << 16) | val_hi,
66 CX18_REG_GPIO_OUT2, val_hi, dir_hi);
67}
68
69static void gpio_update(struct cx18 *cx, u32 mask, u32 data)
70{
71 if (mask == 0)
72 return;
73
74 mutex_lock(&cx->gpio_lock);
75 cx->gpio_val = (cx->gpio_val & ~mask) | (data & mask);
76 gpio_write(cx);
77 mutex_unlock(&cx->gpio_lock);
78}
79
80static void gpio_reset_seq(struct cx18 *cx, u32 active_lo, u32 active_hi,
81 unsigned int assert_msecs,
82 unsigned int recovery_msecs)
83{
84 u32 mask;
85
86 mask = active_lo | active_hi;
87 if (mask == 0)
88 return;
89
90 /*
91 * Assuming that active_hi and active_lo are a subsets of the bits in
92 * gpio_dir. Also assumes that active_lo and active_hi don't overlap
93 * in any bit position
94 */
95
96 /* Assert */
97 gpio_update(cx, mask, ~active_lo);
98 schedule_timeout_uninterruptible(msecs_to_jiffies(assert_msecs));
99
100 /* Deassert */
101 gpio_update(cx, mask, ~active_hi);
102 schedule_timeout_uninterruptible(msecs_to_jiffies(recovery_msecs));
103}
104
105/*
106 * GPIO Multiplexer - logical device
107 */
108static int gpiomux_log_status(struct v4l2_subdev *sd)
109{
110 struct cx18 *cx = v4l2_get_subdevdata(sd);
111
112 mutex_lock(&cx->gpio_lock);
113 CX18_INFO_DEV(sd, "GPIO: direction 0x%08x, value 0x%08x\n",
114 cx->gpio_dir, cx->gpio_val);
115 mutex_unlock(&cx->gpio_lock);
116 return 0;
117}
118
119static int gpiomux_s_radio(struct v4l2_subdev *sd)
120{
121 struct cx18 *cx = v4l2_get_subdevdata(sd);
122
123 /*
124 * FIXME - work out the cx->active/audio_input mess - this is
125 * intended to handle the switch to radio mode and set the
126 * audio routing, but we need to update the state in cx
127 */
128 gpio_update(cx, cx->card->gpio_audio_input.mask,
129 cx->card->gpio_audio_input.radio);
130 return 0;
131}
132
133static int gpiomux_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
134{
135 struct cx18 *cx = v4l2_get_subdevdata(sd);
136 u32 data;
137
138 switch (cx->card->audio_inputs[cx->audio_input].muxer_input) {
139 case 1:
140 data = cx->card->gpio_audio_input.linein;
141 break;
142 case 0:
143 data = cx->card->gpio_audio_input.tuner;
144 break;
145 default:
146 /*
147 * FIXME - work out the cx->active/audio_input mess - this is
148 * intended to handle the switch from radio mode and set the
149 * audio routing, but we need to update the state in cx
150 */
151 data = cx->card->gpio_audio_input.tuner;
152 break;
153 }
154 gpio_update(cx, cx->card->gpio_audio_input.mask, data);
155 return 0;
156}
157
158static int gpiomux_s_audio_routing(struct v4l2_subdev *sd,
159 u32 input, u32 output, u32 config)
160{
161 struct cx18 *cx = v4l2_get_subdevdata(sd);
162 u32 data;
163
164 switch (input) {
165 case 0:
166 data = cx->card->gpio_audio_input.tuner;
167 break;
168 case 1:
169 data = cx->card->gpio_audio_input.linein;
170 break;
171 case 2:
172 data = cx->card->gpio_audio_input.radio;
173 break;
174 default:
175 return -EINVAL;
176 }
177 gpio_update(cx, cx->card->gpio_audio_input.mask, data);
178 return 0;
179}
180
181static const struct v4l2_subdev_core_ops gpiomux_core_ops = {
182 .log_status = gpiomux_log_status,
183 .s_std = gpiomux_s_std,
184};
185
186static const struct v4l2_subdev_tuner_ops gpiomux_tuner_ops = {
187 .s_radio = gpiomux_s_radio,
188};
189
190static const struct v4l2_subdev_audio_ops gpiomux_audio_ops = {
191 .s_routing = gpiomux_s_audio_routing,
192};
193
194static const struct v4l2_subdev_ops gpiomux_ops = {
195 .core = &gpiomux_core_ops,
196 .tuner = &gpiomux_tuner_ops,
197 .audio = &gpiomux_audio_ops,
198};
199
200/*
201 * GPIO Reset Controller - logical device
202 */
203static int resetctrl_log_status(struct v4l2_subdev *sd)
204{
205 struct cx18 *cx = v4l2_get_subdevdata(sd);
206
207 mutex_lock(&cx->gpio_lock);
208 CX18_INFO_DEV(sd, "GPIO: direction 0x%08x, value 0x%08x\n",
209 cx->gpio_dir, cx->gpio_val);
210 mutex_unlock(&cx->gpio_lock);
211 return 0;
212}
213
214static int resetctrl_reset(struct v4l2_subdev *sd, u32 val)
215{
216 struct cx18 *cx = v4l2_get_subdevdata(sd);
217 const struct cx18_gpio_i2c_slave_reset *p;
218
219 p = &cx->card->gpio_i2c_slave_reset;
220 switch (val) {
221 case CX18_GPIO_RESET_I2C:
222 gpio_reset_seq(cx, p->active_lo_mask, p->active_hi_mask,
223 p->msecs_asserted, p->msecs_recovery);
224 break;
225 case CX18_GPIO_RESET_Z8F0811:
226 /*
227 * Assert timing for the Z8F0811 on HVR-1600 boards:
228 * 1. Assert RESET for min of 4 clock cycles at 18.432 MHz to
229 * initiate
230 * 2. Reset then takes 66 WDT cycles at 10 kHz + 16 xtal clock
231 * cycles (6,601,085 nanoseconds ~= 7 milliseconds)
232 * 3. DBG pin must be high before chip exits reset for normal
233 * operation. DBG is open drain and hopefully pulled high
234 * since we don't normally drive it (GPIO 1?) for the
235 * HVR-1600
236 * 4. Z8F0811 won't exit reset until RESET is deasserted
237 * 5. Zilog comes out of reset, loads reset vector address and
238 * executes from there. Required recovery delay unknown.
239 */
240 gpio_reset_seq(cx, p->ir_reset_mask, 0,
241 p->msecs_asserted, p->msecs_recovery);
242 break;
243 case CX18_GPIO_RESET_XC2028:
244 if (cx->card->tuners[0].tuner == TUNER_XC2028)
245 gpio_reset_seq(cx, (1 << cx->card->xceive_pin), 0,
246 1, 1);
247 break;
248 }
249 return 0;
250}
251
252static const struct v4l2_subdev_core_ops resetctrl_core_ops = {
253 .log_status = resetctrl_log_status,
254 .reset = resetctrl_reset,
255};
256
257static const struct v4l2_subdev_ops resetctrl_ops = {
258 .core = &resetctrl_core_ops,
259};
260
261/*
262 * External entry points
263 */
264void cx18_gpio_init(struct cx18 *cx)
265{
266 mutex_lock(&cx->gpio_lock);
267 cx->gpio_dir = cx->card->gpio_init.direction;
268 cx->gpio_val = cx->card->gpio_init.initial_value;
269
270 if (cx->card->tuners[0].tuner == TUNER_XC2028) {
271 cx->gpio_dir |= 1 << cx->card->xceive_pin;
272 cx->gpio_val |= 1 << cx->card->xceive_pin;
273 }
274
275 if (cx->gpio_dir == 0) {
276 mutex_unlock(&cx->gpio_lock);
277 return;
278 }
279
280 CX18_DEBUG_INFO("GPIO initial dir: %08x/%08x out: %08x/%08x\n",
281 cx18_read_reg(cx, CX18_REG_GPIO_DIR1),
282 cx18_read_reg(cx, CX18_REG_GPIO_DIR2),
283 cx18_read_reg(cx, CX18_REG_GPIO_OUT1),
284 cx18_read_reg(cx, CX18_REG_GPIO_OUT2));
285
286 gpio_write(cx);
287 mutex_unlock(&cx->gpio_lock);
288}
289
290int cx18_gpio_register(struct cx18 *cx, u32 hw)
291{
292 struct v4l2_subdev *sd;
293 const struct v4l2_subdev_ops *ops;
294 char *str;
295
296 switch (hw) {
297 case CX18_HW_GPIO_MUX:
298 sd = &cx->sd_gpiomux;
299 ops = &gpiomux_ops;
300 str = "gpio-mux";
301 break;
302 case CX18_HW_GPIO_RESET_CTRL:
303 sd = &cx->sd_resetctrl;
304 ops = &resetctrl_ops;
305 str = "gpio-reset-ctrl";
306 break;
307 default:
308 return -EINVAL;
309 }
310
311 v4l2_subdev_init(sd, ops);
312 v4l2_set_subdevdata(sd, cx);
313 snprintf(sd->name, sizeof(sd->name), "%s %s", cx->v4l2_dev.name, str);
314 sd->grp_id = hw;
315 return v4l2_device_register_subdev(&cx->v4l2_dev, sd);
316}
317
318void cx18_reset_ir_gpio(void *data)
319{
320 struct cx18 *cx = to_cx18((struct v4l2_device *)data);
321
322 if (cx->card->gpio_i2c_slave_reset.ir_reset_mask == 0)
323 return;
324
325 CX18_DEBUG_INFO("Resetting IR microcontroller\n");
326
327 v4l2_subdev_call(&cx->sd_resetctrl,
328 core, reset, CX18_GPIO_RESET_Z8F0811);
329}
330EXPORT_SYMBOL(cx18_reset_ir_gpio);
331/* This symbol is exported for use by lirc_pvr150 for the IR-blaster */
332
333/* Xceive tuner reset function */
334int cx18_reset_tuner_gpio(void *dev, int component, int cmd, int value)
335{
336 struct i2c_algo_bit_data *algo = dev;
337 struct cx18_i2c_algo_callback_data *cb_data = algo->data;
338 struct cx18 *cx = cb_data->cx;
339
340 if (cmd != XC2028_TUNER_RESET ||
341 cx->card->tuners[0].tuner != TUNER_XC2028)
342 return 0;
343
344 CX18_DEBUG_INFO("Resetting XCeive tuner\n");
345 return v4l2_subdev_call(&cx->sd_resetctrl,
346 core, reset, CX18_GPIO_RESET_XC2028);
347}
diff --git a/drivers/media/pci/cx18/cx18-gpio.h b/drivers/media/pci/cx18/cx18-gpio.h
new file mode 100644
index 000000000000..4aea2ef88e8d
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-gpio.h
@@ -0,0 +1,34 @@
1/*
2 * cx18 gpio functions
3 *
4 * Derived from ivtv-gpio.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24void cx18_gpio_init(struct cx18 *cx);
25int cx18_gpio_register(struct cx18 *cx, u32 hw);
26
27enum cx18_gpio_reset_type {
28 CX18_GPIO_RESET_I2C = 0,
29 CX18_GPIO_RESET_Z8F0811 = 1,
30 CX18_GPIO_RESET_XC2028 = 2,
31};
32
33void cx18_reset_ir_gpio(void *data);
34int cx18_reset_tuner_gpio(void *dev, int component, int cmd, int value);
diff --git a/drivers/media/pci/cx18/cx18-i2c.c b/drivers/media/pci/cx18/cx18-i2c.c
new file mode 100644
index 000000000000..51609d5c88ce
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-i2c.c
@@ -0,0 +1,330 @@
1/*
2 * cx18 I2C functions
3 *
4 * Derived from ivtv-i2c.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-io.h"
27#include "cx18-cards.h"
28#include "cx18-gpio.h"
29#include "cx18-i2c.h"
30#include "cx18-irq.h"
31
32#define CX18_REG_I2C_1_WR 0xf15000
33#define CX18_REG_I2C_1_RD 0xf15008
34#define CX18_REG_I2C_2_WR 0xf25100
35#define CX18_REG_I2C_2_RD 0xf25108
36
37#define SETSCL_BIT 0x0001
38#define SETSDL_BIT 0x0002
39#define GETSCL_BIT 0x0004
40#define GETSDL_BIT 0x0008
41
42#define CX18_CS5345_I2C_ADDR 0x4c
43#define CX18_Z8F0811_IR_TX_I2C_ADDR 0x70
44#define CX18_Z8F0811_IR_RX_I2C_ADDR 0x71
45
46/* This array should match the CX18_HW_ defines */
47static const u8 hw_addrs[] = {
48 0, /* CX18_HW_TUNER */
49 0, /* CX18_HW_TVEEPROM */
50 CX18_CS5345_I2C_ADDR, /* CX18_HW_CS5345 */
51 0, /* CX18_HW_DVB */
52 0, /* CX18_HW_418_AV */
53 0, /* CX18_HW_GPIO_MUX */
54 0, /* CX18_HW_GPIO_RESET_CTRL */
55 CX18_Z8F0811_IR_TX_I2C_ADDR, /* CX18_HW_Z8F0811_IR_TX_HAUP */
56 CX18_Z8F0811_IR_RX_I2C_ADDR, /* CX18_HW_Z8F0811_IR_RX_HAUP */
57};
58
59/* This array should match the CX18_HW_ defines */
60/* This might well become a card-specific array */
61static const u8 hw_bus[] = {
62 1, /* CX18_HW_TUNER */
63 0, /* CX18_HW_TVEEPROM */
64 0, /* CX18_HW_CS5345 */
65 0, /* CX18_HW_DVB */
66 0, /* CX18_HW_418_AV */
67 0, /* CX18_HW_GPIO_MUX */
68 0, /* CX18_HW_GPIO_RESET_CTRL */
69 0, /* CX18_HW_Z8F0811_IR_TX_HAUP */
70 0, /* CX18_HW_Z8F0811_IR_RX_HAUP */
71};
72
73/* This array should match the CX18_HW_ defines */
74static const char * const hw_devicenames[] = {
75 "tuner",
76 "tveeprom",
77 "cs5345",
78 "cx23418_DTV",
79 "cx23418_AV",
80 "gpio_mux",
81 "gpio_reset_ctrl",
82 "ir_tx_z8f0811_haup",
83 "ir_rx_z8f0811_haup",
84};
85
86static int cx18_i2c_new_ir(struct cx18 *cx, struct i2c_adapter *adap, u32 hw,
87 const char *type, u8 addr)
88{
89 struct i2c_board_info info;
90 struct IR_i2c_init_data *init_data = &cx->ir_i2c_init_data;
91 unsigned short addr_list[2] = { addr, I2C_CLIENT_END };
92
93 memset(&info, 0, sizeof(struct i2c_board_info));
94 strlcpy(info.type, type, I2C_NAME_SIZE);
95
96 /* Our default information for ir-kbd-i2c.c to use */
97 switch (hw) {
98 case CX18_HW_Z8F0811_IR_RX_HAUP:
99 init_data->ir_codes = RC_MAP_HAUPPAUGE;
100 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
101 init_data->type = RC_TYPE_RC5;
102 init_data->name = cx->card_name;
103 info.platform_data = init_data;
104 break;
105 }
106
107 return i2c_new_probed_device(adap, &info, addr_list, NULL) == NULL ?
108 -1 : 0;
109}
110
111int cx18_i2c_register(struct cx18 *cx, unsigned idx)
112{
113 struct v4l2_subdev *sd;
114 int bus = hw_bus[idx];
115 struct i2c_adapter *adap = &cx->i2c_adap[bus];
116 const char *type = hw_devicenames[idx];
117 u32 hw = 1 << idx;
118
119 if (idx >= ARRAY_SIZE(hw_addrs))
120 return -1;
121
122 if (hw == CX18_HW_TUNER) {
123 /* special tuner group handling */
124 sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,
125 adap, type, 0, cx->card_i2c->radio);
126 if (sd != NULL)
127 sd->grp_id = hw;
128 sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,
129 adap, type, 0, cx->card_i2c->demod);
130 if (sd != NULL)
131 sd->grp_id = hw;
132 sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,
133 adap, type, 0, cx->card_i2c->tv);
134 if (sd != NULL)
135 sd->grp_id = hw;
136 return sd != NULL ? 0 : -1;
137 }
138
139 if (hw & CX18_HW_IR_ANY)
140 return cx18_i2c_new_ir(cx, adap, hw, type, hw_addrs[idx]);
141
142 /* Is it not an I2C device or one we do not wish to register? */
143 if (!hw_addrs[idx])
144 return -1;
145
146 /* It's an I2C device other than an analog tuner or IR chip */
147 sd = v4l2_i2c_new_subdev(&cx->v4l2_dev, adap, type, hw_addrs[idx],
148 NULL);
149 if (sd != NULL)
150 sd->grp_id = hw;
151 return sd != NULL ? 0 : -1;
152}
153
154/* Find the first member of the subdev group id in hw */
155struct v4l2_subdev *cx18_find_hw(struct cx18 *cx, u32 hw)
156{
157 struct v4l2_subdev *result = NULL;
158 struct v4l2_subdev *sd;
159
160 spin_lock(&cx->v4l2_dev.lock);
161 v4l2_device_for_each_subdev(sd, &cx->v4l2_dev) {
162 if (sd->grp_id == hw) {
163 result = sd;
164 break;
165 }
166 }
167 spin_unlock(&cx->v4l2_dev.lock);
168 return result;
169}
170
171static void cx18_setscl(void *data, int state)
172{
173 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
174 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
175 u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
176 u32 r = cx18_read_reg(cx, addr);
177
178 if (state)
179 cx18_write_reg(cx, r | SETSCL_BIT, addr);
180 else
181 cx18_write_reg(cx, r & ~SETSCL_BIT, addr);
182}
183
184static void cx18_setsda(void *data, int state)
185{
186 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
187 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
188 u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
189 u32 r = cx18_read_reg(cx, addr);
190
191 if (state)
192 cx18_write_reg(cx, r | SETSDL_BIT, addr);
193 else
194 cx18_write_reg(cx, r & ~SETSDL_BIT, addr);
195}
196
197static int cx18_getscl(void *data)
198{
199 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
200 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
201 u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
202
203 return cx18_read_reg(cx, addr) & GETSCL_BIT;
204}
205
206static int cx18_getsda(void *data)
207{
208 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
209 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
210 u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
211
212 return cx18_read_reg(cx, addr) & GETSDL_BIT;
213}
214
215/* template for i2c-bit-algo */
216static struct i2c_adapter cx18_i2c_adap_template = {
217 .name = "cx18 i2c driver",
218 .algo = NULL, /* set by i2c-algo-bit */
219 .algo_data = NULL, /* filled from template */
220 .owner = THIS_MODULE,
221};
222
223#define CX18_SCL_PERIOD (10) /* usecs. 10 usec is period for a 100 KHz clock */
224#define CX18_ALGO_BIT_TIMEOUT (2) /* seconds */
225
226static struct i2c_algo_bit_data cx18_i2c_algo_template = {
227 .setsda = cx18_setsda,
228 .setscl = cx18_setscl,
229 .getsda = cx18_getsda,
230 .getscl = cx18_getscl,
231 .udelay = CX18_SCL_PERIOD/2, /* 1/2 clock period in usec*/
232 .timeout = CX18_ALGO_BIT_TIMEOUT*HZ /* jiffies */
233};
234
235/* init + register i2c adapter */
236int init_cx18_i2c(struct cx18 *cx)
237{
238 int i, err;
239 CX18_DEBUG_I2C("i2c init\n");
240
241 for (i = 0; i < 2; i++) {
242 /* Setup algorithm for adapter */
243 memcpy(&cx->i2c_algo[i], &cx18_i2c_algo_template,
244 sizeof(struct i2c_algo_bit_data));
245 cx->i2c_algo_cb_data[i].cx = cx;
246 cx->i2c_algo_cb_data[i].bus_index = i;
247 cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];
248
249 /* Setup adapter */
250 memcpy(&cx->i2c_adap[i], &cx18_i2c_adap_template,
251 sizeof(struct i2c_adapter));
252 cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];
253 sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),
254 " #%d-%d", cx->instance, i);
255 i2c_set_adapdata(&cx->i2c_adap[i], &cx->v4l2_dev);
256 cx->i2c_adap[i].dev.parent = &cx->pci_dev->dev;
257 }
258
259 if (cx18_read_reg(cx, CX18_REG_I2C_2_WR) != 0x0003c02f) {
260 /* Reset/Unreset I2C hardware block */
261 /* Clock select 220MHz */
262 cx18_write_reg_expect(cx, 0x10000000, 0xc71004,
263 0x00000000, 0x10001000);
264 /* Clock Enable */
265 cx18_write_reg_expect(cx, 0x10001000, 0xc71024,
266 0x00001000, 0x10001000);
267 }
268 /* courtesy of Steven Toth <stoth@hauppauge.com> */
269 cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
270 mdelay(10);
271 cx18_write_reg_expect(cx, 0x00c000c0, 0xc7001c, 0x000000c0, 0x00c000c0);
272 mdelay(10);
273 cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
274 mdelay(10);
275
276 /* Set to edge-triggered intrs. */
277 cx18_write_reg(cx, 0x00c00000, 0xc730c8);
278 /* Clear any stale intrs */
279 cx18_write_reg_expect(cx, HW2_I2C1_INT|HW2_I2C2_INT, HW2_INT_CLR_STATUS,
280 ~(HW2_I2C1_INT|HW2_I2C2_INT), HW2_I2C1_INT|HW2_I2C2_INT);
281
282 /* Hw I2C1 Clock Freq ~100kHz */
283 cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_1_WR);
284 cx18_setscl(&cx->i2c_algo_cb_data[0], 1);
285 cx18_setsda(&cx->i2c_algo_cb_data[0], 1);
286
287 /* Hw I2C2 Clock Freq ~100kHz */
288 cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_2_WR);
289 cx18_setscl(&cx->i2c_algo_cb_data[1], 1);
290 cx18_setsda(&cx->i2c_algo_cb_data[1], 1);
291
292 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL,
293 core, reset, (u32) CX18_GPIO_RESET_I2C);
294
295 err = i2c_bit_add_bus(&cx->i2c_adap[0]);
296 if (err)
297 goto err;
298 err = i2c_bit_add_bus(&cx->i2c_adap[1]);
299 if (err)
300 goto err_del_bus_0;
301 return 0;
302
303 err_del_bus_0:
304 i2c_del_adapter(&cx->i2c_adap[0]);
305 err:
306 return err;
307}
308
309void exit_cx18_i2c(struct cx18 *cx)
310{
311 int i;
312 CX18_DEBUG_I2C("i2c exit\n");
313 cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_1_WR) | 4,
314 CX18_REG_I2C_1_WR);
315 cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_2_WR) | 4,
316 CX18_REG_I2C_2_WR);
317
318 for (i = 0; i < 2; i++) {
319 i2c_del_adapter(&cx->i2c_adap[i]);
320 }
321}
322
323/*
324 Hauppauge HVR1600 should have:
325 32 cx24227
326 98 unknown
327 a0 eeprom
328 c2 tuner
329 e? zilog ir
330 */
diff --git a/drivers/media/pci/cx18/cx18-i2c.h b/drivers/media/pci/cx18/cx18-i2c.h
new file mode 100644
index 000000000000..1180fdc8d983
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-i2c.h
@@ -0,0 +1,29 @@
1/*
2 * cx18 I2C functions
3 *
4 * Derived from ivtv-i2c.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24int cx18_i2c_register(struct cx18 *cx, unsigned idx);
25struct v4l2_subdev *cx18_find_hw(struct cx18 *cx, u32 hw);
26
27/* init + register i2c adapter */
28int init_cx18_i2c(struct cx18 *cx);
29void exit_cx18_i2c(struct cx18 *cx);
diff --git a/drivers/media/pci/cx18/cx18-io.c b/drivers/media/pci/cx18/cx18-io.c
new file mode 100644
index 000000000000..49b9dbd06248
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-io.c
@@ -0,0 +1,97 @@
1/*
2 * cx18 driver PCI memory mapped IO access routines
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#include "cx18-driver.h"
24#include "cx18-io.h"
25#include "cx18-irq.h"
26
27void cx18_memset_io(struct cx18 *cx, void __iomem *addr, int val, size_t count)
28{
29 u8 __iomem *dst = addr;
30 u16 val2 = val | (val << 8);
31 u32 val4 = val2 | (val2 << 16);
32
33 /* Align writes on the CX23418's addresses */
34 if ((count > 0) && ((unsigned long)dst & 1)) {
35 cx18_writeb(cx, (u8) val, dst);
36 count--;
37 dst++;
38 }
39 if ((count > 1) && ((unsigned long)dst & 2)) {
40 cx18_writew(cx, val2, dst);
41 count -= 2;
42 dst += 2;
43 }
44 while (count > 3) {
45 cx18_writel(cx, val4, dst);
46 count -= 4;
47 dst += 4;
48 }
49 if (count > 1) {
50 cx18_writew(cx, val2, dst);
51 count -= 2;
52 dst += 2;
53 }
54 if (count > 0)
55 cx18_writeb(cx, (u8) val, dst);
56}
57
58void cx18_sw1_irq_enable(struct cx18 *cx, u32 val)
59{
60 cx18_write_reg_expect(cx, val, SW1_INT_STATUS, ~val, val);
61 cx->sw1_irq_mask = cx18_read_reg(cx, SW1_INT_ENABLE_PCI) | val;
62 cx18_write_reg(cx, cx->sw1_irq_mask, SW1_INT_ENABLE_PCI);
63}
64
65void cx18_sw1_irq_disable(struct cx18 *cx, u32 val)
66{
67 cx->sw1_irq_mask = cx18_read_reg(cx, SW1_INT_ENABLE_PCI) & ~val;
68 cx18_write_reg(cx, cx->sw1_irq_mask, SW1_INT_ENABLE_PCI);
69}
70
71void cx18_sw2_irq_enable(struct cx18 *cx, u32 val)
72{
73 cx18_write_reg_expect(cx, val, SW2_INT_STATUS, ~val, val);
74 cx->sw2_irq_mask = cx18_read_reg(cx, SW2_INT_ENABLE_PCI) | val;
75 cx18_write_reg(cx, cx->sw2_irq_mask, SW2_INT_ENABLE_PCI);
76}
77
78void cx18_sw2_irq_disable(struct cx18 *cx, u32 val)
79{
80 cx->sw2_irq_mask = cx18_read_reg(cx, SW2_INT_ENABLE_PCI) & ~val;
81 cx18_write_reg(cx, cx->sw2_irq_mask, SW2_INT_ENABLE_PCI);
82}
83
84void cx18_sw2_irq_disable_cpu(struct cx18 *cx, u32 val)
85{
86 u32 r;
87 r = cx18_read_reg(cx, SW2_INT_ENABLE_CPU);
88 cx18_write_reg(cx, r & ~val, SW2_INT_ENABLE_CPU);
89}
90
91void cx18_setup_page(struct cx18 *cx, u32 addr)
92{
93 u32 val;
94 val = cx18_read_reg(cx, 0xD000F8);
95 val = (val & ~0x1f00) | ((addr >> 17) & 0x1f00);
96 cx18_write_reg(cx, val, 0xD000F8);
97}
diff --git a/drivers/media/pci/cx18/cx18-io.h b/drivers/media/pci/cx18/cx18-io.h
new file mode 100644
index 000000000000..18974d886cf7
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-io.h
@@ -0,0 +1,191 @@
1/*
2 * cx18 driver PCI memory mapped IO access routines
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#ifndef CX18_IO_H
24#define CX18_IO_H
25
26#include "cx18-driver.h"
27
28/*
29 * Readback and retry of MMIO access for reliability:
30 * The concept was suggested by Steve Toth <stoth@linuxtv.org>.
31 * The implmentation is the fault of Andy Walls <awalls@md.metrocast.net>.
32 *
33 * *write* functions are implied to retry the mmio unless suffixed with _noretry
34 * *read* functions never retry the mmio (it never helps to do so)
35 */
36
37/* Non byteswapping memory mapped IO */
38static inline u32 cx18_raw_readl(struct cx18 *cx, const void __iomem *addr)
39{
40 return __raw_readl(addr);
41}
42
43static inline
44void cx18_raw_writel_noretry(struct cx18 *cx, u32 val, void __iomem *addr)
45{
46 __raw_writel(val, addr);
47}
48
49static inline void cx18_raw_writel(struct cx18 *cx, u32 val, void __iomem *addr)
50{
51 int i;
52 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
53 cx18_raw_writel_noretry(cx, val, addr);
54 if (val == cx18_raw_readl(cx, addr))
55 break;
56 }
57}
58
59/* Normal memory mapped IO */
60static inline u32 cx18_readl(struct cx18 *cx, const void __iomem *addr)
61{
62 return readl(addr);
63}
64
65static inline
66void cx18_writel_noretry(struct cx18 *cx, u32 val, void __iomem *addr)
67{
68 writel(val, addr);
69}
70
71static inline void cx18_writel(struct cx18 *cx, u32 val, void __iomem *addr)
72{
73 int i;
74 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
75 cx18_writel_noretry(cx, val, addr);
76 if (val == cx18_readl(cx, addr))
77 break;
78 }
79}
80
81static inline
82void cx18_writel_expect(struct cx18 *cx, u32 val, void __iomem *addr,
83 u32 eval, u32 mask)
84{
85 int i;
86 u32 r;
87 eval &= mask;
88 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
89 cx18_writel_noretry(cx, val, addr);
90 r = cx18_readl(cx, addr);
91 if (r == 0xffffffff && eval != 0xffffffff)
92 continue;
93 if (eval == (r & mask))
94 break;
95 }
96}
97
98static inline u16 cx18_readw(struct cx18 *cx, const void __iomem *addr)
99{
100 return readw(addr);
101}
102
103static inline
104void cx18_writew_noretry(struct cx18 *cx, u16 val, void __iomem *addr)
105{
106 writew(val, addr);
107}
108
109static inline void cx18_writew(struct cx18 *cx, u16 val, void __iomem *addr)
110{
111 int i;
112 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
113 cx18_writew_noretry(cx, val, addr);
114 if (val == cx18_readw(cx, addr))
115 break;
116 }
117}
118
119static inline u8 cx18_readb(struct cx18 *cx, const void __iomem *addr)
120{
121 return readb(addr);
122}
123
124static inline
125void cx18_writeb_noretry(struct cx18 *cx, u8 val, void __iomem *addr)
126{
127 writeb(val, addr);
128}
129
130static inline void cx18_writeb(struct cx18 *cx, u8 val, void __iomem *addr)
131{
132 int i;
133 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
134 cx18_writeb_noretry(cx, val, addr);
135 if (val == cx18_readb(cx, addr))
136 break;
137 }
138}
139
140static inline
141void cx18_memcpy_fromio(struct cx18 *cx, void *to,
142 const void __iomem *from, unsigned int len)
143{
144 memcpy_fromio(to, from, len);
145}
146
147void cx18_memset_io(struct cx18 *cx, void __iomem *addr, int val, size_t count);
148
149
150/* Access "register" region of CX23418 memory mapped I/O */
151static inline void cx18_write_reg_noretry(struct cx18 *cx, u32 val, u32 reg)
152{
153 cx18_writel_noretry(cx, val, cx->reg_mem + reg);
154}
155
156static inline void cx18_write_reg(struct cx18 *cx, u32 val, u32 reg)
157{
158 cx18_writel(cx, val, cx->reg_mem + reg);
159}
160
161static inline void cx18_write_reg_expect(struct cx18 *cx, u32 val, u32 reg,
162 u32 eval, u32 mask)
163{
164 cx18_writel_expect(cx, val, cx->reg_mem + reg, eval, mask);
165}
166
167static inline u32 cx18_read_reg(struct cx18 *cx, u32 reg)
168{
169 return cx18_readl(cx, cx->reg_mem + reg);
170}
171
172
173/* Access "encoder memory" region of CX23418 memory mapped I/O */
174static inline void cx18_write_enc(struct cx18 *cx, u32 val, u32 addr)
175{
176 cx18_writel(cx, val, cx->enc_mem + addr);
177}
178
179static inline u32 cx18_read_enc(struct cx18 *cx, u32 addr)
180{
181 return cx18_readl(cx, cx->enc_mem + addr);
182}
183
184void cx18_sw1_irq_enable(struct cx18 *cx, u32 val);
185void cx18_sw1_irq_disable(struct cx18 *cx, u32 val);
186void cx18_sw2_irq_enable(struct cx18 *cx, u32 val);
187void cx18_sw2_irq_disable(struct cx18 *cx, u32 val);
188void cx18_sw2_irq_disable_cpu(struct cx18 *cx, u32 val);
189void cx18_setup_page(struct cx18 *cx, u32 addr);
190
191#endif /* CX18_IO_H */
diff --git a/drivers/media/pci/cx18/cx18-ioctl.c b/drivers/media/pci/cx18/cx18-ioctl.c
new file mode 100644
index 000000000000..e9912db3b496
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-ioctl.c
@@ -0,0 +1,1194 @@
1/*
2 * cx18 ioctl system call
3 *
4 * Derived from ivtv-ioctl.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-io.h"
27#include "cx18-version.h"
28#include "cx18-mailbox.h"
29#include "cx18-i2c.h"
30#include "cx18-queue.h"
31#include "cx18-fileops.h"
32#include "cx18-vbi.h"
33#include "cx18-audio.h"
34#include "cx18-video.h"
35#include "cx18-streams.h"
36#include "cx18-ioctl.h"
37#include "cx18-gpio.h"
38#include "cx18-controls.h"
39#include "cx18-cards.h"
40#include "cx18-av-core.h"
41#include <media/tveeprom.h>
42#include <media/v4l2-chip-ident.h>
43
44u16 cx18_service2vbi(int type)
45{
46 switch (type) {
47 case V4L2_SLICED_TELETEXT_B:
48 return CX18_SLICED_TYPE_TELETEXT_B;
49 case V4L2_SLICED_CAPTION_525:
50 return CX18_SLICED_TYPE_CAPTION_525;
51 case V4L2_SLICED_WSS_625:
52 return CX18_SLICED_TYPE_WSS_625;
53 case V4L2_SLICED_VPS:
54 return CX18_SLICED_TYPE_VPS;
55 default:
56 return 0;
57 }
58}
59
60/* Check if VBI services are allowed on the (field, line) for the video std */
61static int valid_service_line(int field, int line, int is_pal)
62{
63 return (is_pal && line >= 6 &&
64 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
65 (!is_pal && line >= 10 && line < 22);
66}
67
68/*
69 * For a (field, line, std) and inbound potential set of services for that line,
70 * return the first valid service of those passed in the incoming set for that
71 * line in priority order:
72 * CC, VPS, or WSS over TELETEXT for well known lines
73 * TELETEXT, before VPS, before CC, before WSS, for other lines
74 */
75static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
76{
77 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
78 int i;
79
80 set = set & valid_set;
81 if (set == 0 || !valid_service_line(field, line, is_pal))
82 return 0;
83 if (!is_pal) {
84 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
85 return V4L2_SLICED_CAPTION_525;
86 } else {
87 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
88 return V4L2_SLICED_VPS;
89 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
90 return V4L2_SLICED_WSS_625;
91 if (line == 23)
92 return 0;
93 }
94 for (i = 0; i < 32; i++) {
95 if ((1 << i) & set)
96 return 1 << i;
97 }
98 return 0;
99}
100
101/*
102 * Expand the service_set of *fmt into valid service_lines for the std,
103 * and clear the passed in fmt->service_set
104 */
105void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
106{
107 u16 set = fmt->service_set;
108 int f, l;
109
110 fmt->service_set = 0;
111 for (f = 0; f < 2; f++) {
112 for (l = 0; l < 24; l++)
113 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
114 }
115}
116
117/*
118 * Sanitize the service_lines in *fmt per the video std, and return 1
119 * if any service_line is left as valid after santization
120 */
121static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
122{
123 int f, l;
124 u16 set = 0;
125
126 for (f = 0; f < 2; f++) {
127 for (l = 0; l < 24; l++) {
128 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
129 set |= fmt->service_lines[f][l];
130 }
131 }
132 return set != 0;
133}
134
135/* Compute the service_set from the assumed valid service_lines of *fmt */
136u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
137{
138 int f, l;
139 u16 set = 0;
140
141 for (f = 0; f < 2; f++) {
142 for (l = 0; l < 24; l++)
143 set |= fmt->service_lines[f][l];
144 }
145 return set;
146}
147
148static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
149 struct v4l2_format *fmt)
150{
151 struct cx18_open_id *id = fh2id(fh);
152 struct cx18 *cx = id->cx;
153 struct cx18_stream *s = &cx->streams[id->type];
154 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
155
156 pixfmt->width = cx->cxhdl.width;
157 pixfmt->height = cx->cxhdl.height;
158 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
159 pixfmt->field = V4L2_FIELD_INTERLACED;
160 pixfmt->priv = 0;
161 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
162 pixfmt->pixelformat = s->pixelformat;
163 pixfmt->sizeimage = s->vb_bytes_per_frame;
164 pixfmt->bytesperline = 720;
165 } else {
166 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
167 pixfmt->sizeimage = 128 * 1024;
168 pixfmt->bytesperline = 0;
169 }
170 return 0;
171}
172
173static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
174 struct v4l2_format *fmt)
175{
176 struct cx18 *cx = fh2id(fh)->cx;
177 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
178
179 vbifmt->sampling_rate = 27000000;
180 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
181 vbifmt->samples_per_line = vbi_active_samples - 4;
182 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
183 vbifmt->start[0] = cx->vbi.start[0];
184 vbifmt->start[1] = cx->vbi.start[1];
185 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
186 vbifmt->flags = 0;
187 vbifmt->reserved[0] = 0;
188 vbifmt->reserved[1] = 0;
189 return 0;
190}
191
192static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
193 struct v4l2_format *fmt)
194{
195 struct cx18 *cx = fh2id(fh)->cx;
196 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
197
198 /* sane, V4L2 spec compliant, defaults */
199 vbifmt->reserved[0] = 0;
200 vbifmt->reserved[1] = 0;
201 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
202 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
203 vbifmt->service_set = 0;
204
205 /*
206 * Fetch the configured service_lines and total service_set from the
207 * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
208 * fmt->fmt.sliced under valid calling conditions
209 */
210 if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
211 return -EINVAL;
212
213 /* Ensure V4L2 spec compliant output */
214 vbifmt->reserved[0] = 0;
215 vbifmt->reserved[1] = 0;
216 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
217 vbifmt->service_set = cx18_get_service_set(vbifmt);
218 return 0;
219}
220
221static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
222 struct v4l2_format *fmt)
223{
224 struct cx18_open_id *id = fh2id(fh);
225 struct cx18 *cx = id->cx;
226 int w = fmt->fmt.pix.width;
227 int h = fmt->fmt.pix.height;
228 int min_h = 2;
229
230 w = min(w, 720);
231 w = max(w, 2);
232 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
233 /* YUV height must be a multiple of 32 */
234 h &= ~0x1f;
235 min_h = 32;
236 }
237 h = min(h, cx->is_50hz ? 576 : 480);
238 h = max(h, min_h);
239
240 fmt->fmt.pix.width = w;
241 fmt->fmt.pix.height = h;
242 return 0;
243}
244
245static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
246 struct v4l2_format *fmt)
247{
248 return cx18_g_fmt_vbi_cap(file, fh, fmt);
249}
250
251static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
252 struct v4l2_format *fmt)
253{
254 struct cx18 *cx = fh2id(fh)->cx;
255 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
256
257 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
258 vbifmt->reserved[0] = 0;
259 vbifmt->reserved[1] = 0;
260
261 /* If given a service set, expand it validly & clear passed in set */
262 if (vbifmt->service_set)
263 cx18_expand_service_set(vbifmt, cx->is_50hz);
264 /* Sanitize the service_lines, and compute the new set if any valid */
265 if (check_service_set(vbifmt, cx->is_50hz))
266 vbifmt->service_set = cx18_get_service_set(vbifmt);
267 return 0;
268}
269
270static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
271 struct v4l2_format *fmt)
272{
273 struct cx18_open_id *id = fh2id(fh);
274 struct cx18 *cx = id->cx;
275 struct v4l2_mbus_framefmt mbus_fmt;
276 struct cx18_stream *s = &cx->streams[id->type];
277 int ret;
278 int w, h;
279
280 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
281 if (ret)
282 return ret;
283 w = fmt->fmt.pix.width;
284 h = fmt->fmt.pix.height;
285
286 if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
287 s->pixelformat == fmt->fmt.pix.pixelformat)
288 return 0;
289
290 if (atomic_read(&cx->ana_capturing) > 0)
291 return -EBUSY;
292
293 s->pixelformat = fmt->fmt.pix.pixelformat;
294 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
295 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
296 if (s->pixelformat == V4L2_PIX_FMT_HM12)
297 s->vb_bytes_per_frame = h * 720 * 3 / 2;
298 else
299 s->vb_bytes_per_frame = h * 720 * 2;
300
301 mbus_fmt.width = cx->cxhdl.width = w;
302 mbus_fmt.height = cx->cxhdl.height = h;
303 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
304 v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
305 return cx18_g_fmt_vid_cap(file, fh, fmt);
306}
307
308static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
309 struct v4l2_format *fmt)
310{
311 struct cx18_open_id *id = fh2id(fh);
312 struct cx18 *cx = id->cx;
313 int ret;
314
315 /*
316 * Changing the Encoder's Raw VBI parameters won't have any effect
317 * if any analog capture is ongoing
318 */
319 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
320 return -EBUSY;
321
322 /*
323 * Set the digitizer registers for raw active VBI.
324 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
325 * calling conditions
326 */
327 ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
328 if (ret)
329 return ret;
330
331 /* Store our new v4l2 (non-)sliced VBI state */
332 cx->vbi.sliced_in->service_set = 0;
333 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
334
335 return cx18_g_fmt_vbi_cap(file, fh, fmt);
336}
337
338static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
339 struct v4l2_format *fmt)
340{
341 struct cx18_open_id *id = fh2id(fh);
342 struct cx18 *cx = id->cx;
343 int ret;
344 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
345
346 cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
347
348 /*
349 * Changing the Encoder's Raw VBI parameters won't have any effect
350 * if any analog capture is ongoing
351 */
352 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
353 return -EBUSY;
354
355 /*
356 * Set the service_lines requested in the digitizer/slicer registers.
357 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
358 * passed in fmt->fmt.sliced under valid calling conditions
359 */
360 ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
361 if (ret)
362 return ret;
363 /* Store our current v4l2 sliced VBI settings */
364 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
365 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
366 return 0;
367}
368
369static int cx18_g_chip_ident(struct file *file, void *fh,
370 struct v4l2_dbg_chip_ident *chip)
371{
372 struct cx18 *cx = fh2id(fh)->cx;
373 int err = 0;
374
375 chip->ident = V4L2_IDENT_NONE;
376 chip->revision = 0;
377 switch (chip->match.type) {
378 case V4L2_CHIP_MATCH_HOST:
379 switch (chip->match.addr) {
380 case 0:
381 chip->ident = V4L2_IDENT_CX23418;
382 chip->revision = cx18_read_reg(cx, 0xC72028);
383 break;
384 case 1:
385 /*
386 * The A/V decoder is always present, but in the rare
387 * case that the card doesn't have analog, we don't
388 * use it. We find it w/o using the cx->sd_av pointer
389 */
390 cx18_call_hw(cx, CX18_HW_418_AV,
391 core, g_chip_ident, chip);
392 break;
393 default:
394 /*
395 * Could return ident = V4L2_IDENT_UNKNOWN if we had
396 * other host chips at higher addresses, but we don't
397 */
398 err = -EINVAL; /* per V4L2 spec */
399 break;
400 }
401 break;
402 case V4L2_CHIP_MATCH_I2C_DRIVER:
403 /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
404 cx18_call_all(cx, core, g_chip_ident, chip);
405 break;
406 case V4L2_CHIP_MATCH_I2C_ADDR:
407 /*
408 * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
409 * to look if a chip is at the address with no driver. That's a
410 * dangerous thing to do with EEPROMs anyway.
411 */
412 cx18_call_all(cx, core, g_chip_ident, chip);
413 break;
414 default:
415 err = -EINVAL;
416 break;
417 }
418 return err;
419}
420
421#ifdef CONFIG_VIDEO_ADV_DEBUG
422static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
423{
424 struct v4l2_dbg_register *regs = arg;
425
426 if (!capable(CAP_SYS_ADMIN))
427 return -EPERM;
428 if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
429 return -EINVAL;
430
431 regs->size = 4;
432 if (cmd == VIDIOC_DBG_S_REGISTER)
433 cx18_write_enc(cx, regs->val, regs->reg);
434 else
435 regs->val = cx18_read_enc(cx, regs->reg);
436 return 0;
437}
438
439static int cx18_g_register(struct file *file, void *fh,
440 struct v4l2_dbg_register *reg)
441{
442 struct cx18 *cx = fh2id(fh)->cx;
443
444 if (v4l2_chip_match_host(&reg->match))
445 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
446 /* FIXME - errors shouldn't be ignored */
447 cx18_call_all(cx, core, g_register, reg);
448 return 0;
449}
450
451static int cx18_s_register(struct file *file, void *fh,
452 struct v4l2_dbg_register *reg)
453{
454 struct cx18 *cx = fh2id(fh)->cx;
455
456 if (v4l2_chip_match_host(&reg->match))
457 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
458 /* FIXME - errors shouldn't be ignored */
459 cx18_call_all(cx, core, s_register, reg);
460 return 0;
461}
462#endif
463
464static int cx18_querycap(struct file *file, void *fh,
465 struct v4l2_capability *vcap)
466{
467 struct cx18_open_id *id = fh2id(fh);
468 struct cx18 *cx = id->cx;
469
470 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
471 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
472 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
473 "PCI:%s", pci_name(cx->pci_dev));
474 vcap->capabilities = cx->v4l2_cap; /* capabilities */
475 if (id->type == CX18_ENC_STREAM_TYPE_YUV)
476 vcap->capabilities |= V4L2_CAP_STREAMING;
477 return 0;
478}
479
480static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
481{
482 struct cx18 *cx = fh2id(fh)->cx;
483
484 return cx18_get_audio_input(cx, vin->index, vin);
485}
486
487static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
488{
489 struct cx18 *cx = fh2id(fh)->cx;
490
491 vin->index = cx->audio_input;
492 return cx18_get_audio_input(cx, vin->index, vin);
493}
494
495static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
496{
497 struct cx18 *cx = fh2id(fh)->cx;
498
499 if (vout->index >= cx->nof_audio_inputs)
500 return -EINVAL;
501 cx->audio_input = vout->index;
502 cx18_audio_set_io(cx);
503 return 0;
504}
505
506static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
507{
508 struct cx18 *cx = fh2id(fh)->cx;
509
510 /* set it to defaults from our table */
511 return cx18_get_input(cx, vin->index, vin);
512}
513
514static int cx18_cropcap(struct file *file, void *fh,
515 struct v4l2_cropcap *cropcap)
516{
517 struct cx18 *cx = fh2id(fh)->cx;
518
519 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
520 return -EINVAL;
521 cropcap->bounds.top = cropcap->bounds.left = 0;
522 cropcap->bounds.width = 720;
523 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
524 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
525 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
526 cropcap->defrect = cropcap->bounds;
527 return 0;
528}
529
530static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
531{
532 struct cx18_open_id *id = fh2id(fh);
533 struct cx18 *cx = id->cx;
534
535 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
536 return -EINVAL;
537 CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
538 return -EINVAL;
539}
540
541static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
542{
543 struct cx18 *cx = fh2id(fh)->cx;
544
545 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
546 return -EINVAL;
547 CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
548 return -EINVAL;
549}
550
551static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
552 struct v4l2_fmtdesc *fmt)
553{
554 static const struct v4l2_fmtdesc formats[] = {
555 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
556 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
557 },
558 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
559 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
560 },
561 { 2, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
562 "UYVY 4:2:2", V4L2_PIX_FMT_UYVY, { 0, 0, 0, 0 }
563 },
564 };
565
566 if (fmt->index > ARRAY_SIZE(formats) - 1)
567 return -EINVAL;
568 *fmt = formats[fmt->index];
569 return 0;
570}
571
572static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
573{
574 struct cx18 *cx = fh2id(fh)->cx;
575
576 *i = cx->active_input;
577 return 0;
578}
579
580int cx18_s_input(struct file *file, void *fh, unsigned int inp)
581{
582 struct cx18_open_id *id = fh2id(fh);
583 struct cx18 *cx = id->cx;
584
585 if (inp >= cx->nof_inputs)
586 return -EINVAL;
587
588 if (inp == cx->active_input) {
589 CX18_DEBUG_INFO("Input unchanged\n");
590 return 0;
591 }
592
593 CX18_DEBUG_INFO("Changing input from %d to %d\n",
594 cx->active_input, inp);
595
596 cx->active_input = inp;
597 /* Set the audio input to whatever is appropriate for the input type. */
598 cx->audio_input = cx->card->video_inputs[inp].audio_index;
599
600 /* prevent others from messing with the streams until
601 we're finished changing inputs. */
602 cx18_mute(cx);
603 cx18_video_set_io(cx);
604 cx18_audio_set_io(cx);
605 cx18_unmute(cx);
606 return 0;
607}
608
609static int cx18_g_frequency(struct file *file, void *fh,
610 struct v4l2_frequency *vf)
611{
612 struct cx18 *cx = fh2id(fh)->cx;
613
614 if (vf->tuner != 0)
615 return -EINVAL;
616
617 cx18_call_all(cx, tuner, g_frequency, vf);
618 return 0;
619}
620
621int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
622{
623 struct cx18_open_id *id = fh2id(fh);
624 struct cx18 *cx = id->cx;
625
626 if (vf->tuner != 0)
627 return -EINVAL;
628
629 cx18_mute(cx);
630 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
631 cx18_call_all(cx, tuner, s_frequency, vf);
632 cx18_unmute(cx);
633 return 0;
634}
635
636static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
637{
638 struct cx18 *cx = fh2id(fh)->cx;
639
640 *std = cx->std;
641 return 0;
642}
643
644int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
645{
646 struct cx18_open_id *id = fh2id(fh);
647 struct cx18 *cx = id->cx;
648
649 if ((*std & V4L2_STD_ALL) == 0)
650 return -EINVAL;
651
652 if (*std == cx->std)
653 return 0;
654
655 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
656 atomic_read(&cx->ana_capturing) > 0) {
657 /* Switching standard would turn off the radio or mess
658 with already running streams, prevent that by
659 returning EBUSY. */
660 return -EBUSY;
661 }
662
663 cx->std = *std;
664 cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
665 cx->is_50hz = !cx->is_60hz;
666 cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
667 cx->cxhdl.width = 720;
668 cx->cxhdl.height = cx->is_50hz ? 576 : 480;
669 cx->vbi.count = cx->is_50hz ? 18 : 12;
670 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
671 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
672 CX18_DEBUG_INFO("Switching standard to %llx.\n",
673 (unsigned long long) cx->std);
674
675 /* Tuner */
676 cx18_call_all(cx, core, s_std, cx->std);
677 return 0;
678}
679
680static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
681{
682 struct cx18_open_id *id = fh2id(fh);
683 struct cx18 *cx = id->cx;
684
685 if (vt->index != 0)
686 return -EINVAL;
687
688 cx18_call_all(cx, tuner, s_tuner, vt);
689 return 0;
690}
691
692static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
693{
694 struct cx18 *cx = fh2id(fh)->cx;
695
696 if (vt->index != 0)
697 return -EINVAL;
698
699 cx18_call_all(cx, tuner, g_tuner, vt);
700
701 if (vt->type == V4L2_TUNER_RADIO)
702 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
703 else
704 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
705 return 0;
706}
707
708static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
709 struct v4l2_sliced_vbi_cap *cap)
710{
711 struct cx18 *cx = fh2id(fh)->cx;
712 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
713 int f, l;
714
715 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
716 return -EINVAL;
717
718 cap->service_set = 0;
719 for (f = 0; f < 2; f++) {
720 for (l = 0; l < 24; l++) {
721 if (valid_service_line(f, l, cx->is_50hz)) {
722 /*
723 * We can find all v4l2 supported vbi services
724 * for the standard, on a valid line for the std
725 */
726 cap->service_lines[f][l] = set;
727 cap->service_set |= set;
728 } else
729 cap->service_lines[f][l] = 0;
730 }
731 }
732 for (f = 0; f < 3; f++)
733 cap->reserved[f] = 0;
734 return 0;
735}
736
737static int _cx18_process_idx_data(struct cx18_buffer *buf,
738 struct v4l2_enc_idx *idx)
739{
740 int consumed, remaining;
741 struct v4l2_enc_idx_entry *e_idx;
742 struct cx18_enc_idx_entry *e_buf;
743
744 /* Frame type lookup: 1=I, 2=P, 4=B */
745 const int mapping[8] = {
746 -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
747 -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
748 };
749
750 /*
751 * Assumption here is that a buf holds an integral number of
752 * struct cx18_enc_idx_entry objects and is properly aligned.
753 * This is enforced by the module options on IDX buffer sizes.
754 */
755 remaining = buf->bytesused - buf->readpos;
756 consumed = 0;
757 e_idx = &idx->entry[idx->entries];
758 e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
759
760 while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
761 idx->entries < V4L2_ENC_IDX_ENTRIES) {
762
763 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
764 | le32_to_cpu(e_buf->offset_low);
765
766 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
767 | le32_to_cpu(e_buf->pts_low);
768
769 e_idx->length = le32_to_cpu(e_buf->length);
770
771 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
772
773 e_idx->reserved[0] = 0;
774 e_idx->reserved[1] = 0;
775
776 idx->entries++;
777 e_idx = &idx->entry[idx->entries];
778 e_buf++;
779
780 remaining -= sizeof(struct cx18_enc_idx_entry);
781 consumed += sizeof(struct cx18_enc_idx_entry);
782 }
783
784 /* Swallow any partial entries at the end, if there are any */
785 if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
786 consumed += remaining;
787
788 buf->readpos += consumed;
789 return consumed;
790}
791
792static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
793 struct v4l2_enc_idx *idx)
794{
795 if (s->type != CX18_ENC_STREAM_TYPE_IDX)
796 return -EINVAL;
797
798 if (mdl->curr_buf == NULL)
799 mdl->curr_buf = list_first_entry(&mdl->buf_list,
800 struct cx18_buffer, list);
801
802 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
803 /*
804 * For some reason we've exhausted the buffers, but the MDL
805 * object still said some data was unread.
806 * Fix that and bail out.
807 */
808 mdl->readpos = mdl->bytesused;
809 return 0;
810 }
811
812 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
813
814 /* Skip any empty buffers in the MDL */
815 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
816 continue;
817
818 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
819
820 /* exit when MDL drained or request satisfied */
821 if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
822 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
823 mdl->readpos >= mdl->bytesused)
824 break;
825 }
826 return 0;
827}
828
829static int cx18_g_enc_index(struct file *file, void *fh,
830 struct v4l2_enc_idx *idx)
831{
832 struct cx18 *cx = fh2id(fh)->cx;
833 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
834 s32 tmp;
835 struct cx18_mdl *mdl;
836
837 if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
838 return -EINVAL;
839
840 /* Compute the best case number of entries we can buffer */
841 tmp = s->buffers -
842 s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
843 if (tmp <= 0)
844 tmp = 1;
845 tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
846
847 /* Fill out the header of the return structure */
848 idx->entries = 0;
849 idx->entries_cap = tmp;
850 memset(idx->reserved, 0, sizeof(idx->reserved));
851
852 /* Pull IDX MDLs and buffers from q_full and populate the entries */
853 do {
854 mdl = cx18_dequeue(s, &s->q_full);
855 if (mdl == NULL) /* No more IDX data right now */
856 break;
857
858 /* Extract the Index entry data from the MDL and buffers */
859 cx18_process_idx_data(s, mdl, idx);
860 if (mdl->readpos < mdl->bytesused) {
861 /* We finished with data remaining, push the MDL back */
862 cx18_push(s, mdl, &s->q_full);
863 break;
864 }
865
866 /* We drained this MDL, schedule it to go to the firmware */
867 cx18_enqueue(s, mdl, &s->q_free);
868
869 } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
870
871 /* Tell the work handler to send free IDX MDLs to the firmware */
872 cx18_stream_load_fw_queue(s);
873 return 0;
874}
875
876static struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
877{
878 struct videobuf_queue *q = NULL;
879 struct cx18 *cx = id->cx;
880 struct cx18_stream *s = &cx->streams[id->type];
881
882 switch (s->vb_type) {
883 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
884 q = &s->vbuf_q;
885 break;
886 case V4L2_BUF_TYPE_VBI_CAPTURE:
887 break;
888 default:
889 break;
890 }
891 return q;
892}
893
894static int cx18_streamon(struct file *file, void *priv,
895 enum v4l2_buf_type type)
896{
897 struct cx18_open_id *id = file->private_data;
898 struct cx18 *cx = id->cx;
899 struct cx18_stream *s = &cx->streams[id->type];
900
901 /* Start the hardware only if we're the video device */
902 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
903 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
904 return -EINVAL;
905
906 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
907 return -EINVAL;
908
909 /* Establish a buffer timeout */
910 mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
911
912 return videobuf_streamon(cx18_vb_queue(id));
913}
914
915static int cx18_streamoff(struct file *file, void *priv,
916 enum v4l2_buf_type type)
917{
918 struct cx18_open_id *id = file->private_data;
919 struct cx18 *cx = id->cx;
920 struct cx18_stream *s = &cx->streams[id->type];
921
922 /* Start the hardware only if we're the video device */
923 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
924 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
925 return -EINVAL;
926
927 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
928 return -EINVAL;
929
930 return videobuf_streamoff(cx18_vb_queue(id));
931}
932
933static int cx18_reqbufs(struct file *file, void *priv,
934 struct v4l2_requestbuffers *rb)
935{
936 struct cx18_open_id *id = file->private_data;
937 struct cx18 *cx = id->cx;
938 struct cx18_stream *s = &cx->streams[id->type];
939
940 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
941 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
942 return -EINVAL;
943
944 return videobuf_reqbufs(cx18_vb_queue(id), rb);
945}
946
947static int cx18_querybuf(struct file *file, void *priv,
948 struct v4l2_buffer *b)
949{
950 struct cx18_open_id *id = file->private_data;
951 struct cx18 *cx = id->cx;
952 struct cx18_stream *s = &cx->streams[id->type];
953
954 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
955 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
956 return -EINVAL;
957
958 return videobuf_querybuf(cx18_vb_queue(id), b);
959}
960
961static int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
962{
963 struct cx18_open_id *id = file->private_data;
964 struct cx18 *cx = id->cx;
965 struct cx18_stream *s = &cx->streams[id->type];
966
967 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
968 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
969 return -EINVAL;
970
971 return videobuf_qbuf(cx18_vb_queue(id), b);
972}
973
974static int cx18_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
975{
976 struct cx18_open_id *id = file->private_data;
977 struct cx18 *cx = id->cx;
978 struct cx18_stream *s = &cx->streams[id->type];
979
980 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
981 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
982 return -EINVAL;
983
984 return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
985}
986
987static int cx18_encoder_cmd(struct file *file, void *fh,
988 struct v4l2_encoder_cmd *enc)
989{
990 struct cx18_open_id *id = fh2id(fh);
991 struct cx18 *cx = id->cx;
992 u32 h;
993
994 switch (enc->cmd) {
995 case V4L2_ENC_CMD_START:
996 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
997 enc->flags = 0;
998 return cx18_start_capture(id);
999
1000 case V4L2_ENC_CMD_STOP:
1001 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1002 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1003 cx18_stop_capture(id,
1004 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1005 break;
1006
1007 case V4L2_ENC_CMD_PAUSE:
1008 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1009 enc->flags = 0;
1010 if (!atomic_read(&cx->ana_capturing))
1011 return -EPERM;
1012 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
1013 return 0;
1014 h = cx18_find_handle(cx);
1015 if (h == CX18_INVALID_TASK_HANDLE) {
1016 CX18_ERR("Can't find valid task handle for "
1017 "V4L2_ENC_CMD_PAUSE\n");
1018 return -EBADFD;
1019 }
1020 cx18_mute(cx);
1021 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
1022 break;
1023
1024 case V4L2_ENC_CMD_RESUME:
1025 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1026 enc->flags = 0;
1027 if (!atomic_read(&cx->ana_capturing))
1028 return -EPERM;
1029 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
1030 return 0;
1031 h = cx18_find_handle(cx);
1032 if (h == CX18_INVALID_TASK_HANDLE) {
1033 CX18_ERR("Can't find valid task handle for "
1034 "V4L2_ENC_CMD_RESUME\n");
1035 return -EBADFD;
1036 }
1037 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
1038 cx18_unmute(cx);
1039 break;
1040
1041 default:
1042 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1043 return -EINVAL;
1044 }
1045 return 0;
1046}
1047
1048static int cx18_try_encoder_cmd(struct file *file, void *fh,
1049 struct v4l2_encoder_cmd *enc)
1050{
1051 struct cx18 *cx = fh2id(fh)->cx;
1052
1053 switch (enc->cmd) {
1054 case V4L2_ENC_CMD_START:
1055 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1056 enc->flags = 0;
1057 break;
1058
1059 case V4L2_ENC_CMD_STOP:
1060 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1061 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1062 break;
1063
1064 case V4L2_ENC_CMD_PAUSE:
1065 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1066 enc->flags = 0;
1067 break;
1068
1069 case V4L2_ENC_CMD_RESUME:
1070 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1071 enc->flags = 0;
1072 break;
1073
1074 default:
1075 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1076 return -EINVAL;
1077 }
1078 return 0;
1079}
1080
1081static int cx18_log_status(struct file *file, void *fh)
1082{
1083 struct cx18 *cx = fh2id(fh)->cx;
1084 struct v4l2_input vidin;
1085 struct v4l2_audio audin;
1086 int i;
1087
1088 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
1089 if (cx->hw_flags & CX18_HW_TVEEPROM) {
1090 struct tveeprom tv;
1091
1092 cx18_read_eeprom(cx, &tv);
1093 }
1094 cx18_call_all(cx, core, log_status);
1095 cx18_get_input(cx, cx->active_input, &vidin);
1096 cx18_get_audio_input(cx, cx->audio_input, &audin);
1097 CX18_INFO("Video Input: %s\n", vidin.name);
1098 CX18_INFO("Audio Input: %s\n", audin.name);
1099 mutex_lock(&cx->gpio_lock);
1100 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1101 cx->gpio_dir, cx->gpio_val);
1102 mutex_unlock(&cx->gpio_lock);
1103 CX18_INFO("Tuner: %s\n",
1104 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
1105 v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
1106 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1107 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1108 struct cx18_stream *s = &cx->streams[i];
1109
1110 if (s->video_dev == NULL || s->buffers == 0)
1111 continue;
1112 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1113 s->name, s->s_flags,
1114 atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1115 / s->buffers,
1116 (s->buffers * s->buf_size) / 1024, s->buffers);
1117 }
1118 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1119 (long long)cx->mpg_data_received,
1120 (long long)cx->vbi_data_inserted);
1121 return 0;
1122}
1123
1124static long cx18_default(struct file *file, void *fh, bool valid_prio,
1125 int cmd, void *arg)
1126{
1127 struct cx18 *cx = fh2id(fh)->cx;
1128
1129 switch (cmd) {
1130 case VIDIOC_INT_RESET: {
1131 u32 val = *(u32 *)arg;
1132
1133 if ((val == 0) || (val & 0x01))
1134 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1135 (u32) CX18_GPIO_RESET_Z8F0811);
1136 break;
1137 }
1138
1139 default:
1140 return -ENOTTY;
1141 }
1142 return 0;
1143}
1144
1145static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1146 .vidioc_querycap = cx18_querycap,
1147 .vidioc_s_audio = cx18_s_audio,
1148 .vidioc_g_audio = cx18_g_audio,
1149 .vidioc_enumaudio = cx18_enumaudio,
1150 .vidioc_enum_input = cx18_enum_input,
1151 .vidioc_cropcap = cx18_cropcap,
1152 .vidioc_s_crop = cx18_s_crop,
1153 .vidioc_g_crop = cx18_g_crop,
1154 .vidioc_g_input = cx18_g_input,
1155 .vidioc_s_input = cx18_s_input,
1156 .vidioc_g_frequency = cx18_g_frequency,
1157 .vidioc_s_frequency = cx18_s_frequency,
1158 .vidioc_s_tuner = cx18_s_tuner,
1159 .vidioc_g_tuner = cx18_g_tuner,
1160 .vidioc_g_enc_index = cx18_g_enc_index,
1161 .vidioc_g_std = cx18_g_std,
1162 .vidioc_s_std = cx18_s_std,
1163 .vidioc_log_status = cx18_log_status,
1164 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
1165 .vidioc_encoder_cmd = cx18_encoder_cmd,
1166 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
1167 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
1168 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
1169 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
1170 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
1171 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
1172 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
1173 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
1174 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
1175 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
1176 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
1177 .vidioc_g_chip_ident = cx18_g_chip_ident,
1178#ifdef CONFIG_VIDEO_ADV_DEBUG
1179 .vidioc_g_register = cx18_g_register,
1180 .vidioc_s_register = cx18_s_register,
1181#endif
1182 .vidioc_default = cx18_default,
1183 .vidioc_streamon = cx18_streamon,
1184 .vidioc_streamoff = cx18_streamoff,
1185 .vidioc_reqbufs = cx18_reqbufs,
1186 .vidioc_querybuf = cx18_querybuf,
1187 .vidioc_qbuf = cx18_qbuf,
1188 .vidioc_dqbuf = cx18_dqbuf,
1189};
1190
1191void cx18_set_funcs(struct video_device *vdev)
1192{
1193 vdev->ioctl_ops = &cx18_ioctl_ops;
1194}
diff --git a/drivers/media/pci/cx18/cx18-ioctl.h b/drivers/media/pci/cx18/cx18-ioctl.h
new file mode 100644
index 000000000000..2f9dd591ee0f
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-ioctl.h
@@ -0,0 +1,31 @@
1/*
2 * cx18 ioctl system call
3 *
4 * Derived from ivtv-ioctl.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25u16 cx18_service2vbi(int type);
26void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal);
27u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt);
28void cx18_set_funcs(struct video_device *vdev);
29int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std);
30int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf);
31int cx18_s_input(struct file *file, void *fh, unsigned int inp);
diff --git a/drivers/media/pci/cx18/cx18-irq.c b/drivers/media/pci/cx18/cx18-irq.c
new file mode 100644
index 000000000000..80edfe93a3d8
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-irq.c
@@ -0,0 +1,81 @@
1/*
2 * cx18 interrupt handling
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#include "cx18-driver.h"
24#include "cx18-io.h"
25#include "cx18-irq.h"
26#include "cx18-mailbox.h"
27#include "cx18-scb.h"
28
29static void xpu_ack(struct cx18 *cx, u32 sw2)
30{
31 if (sw2 & IRQ_CPU_TO_EPU_ACK)
32 wake_up(&cx->mb_cpu_waitq);
33 if (sw2 & IRQ_APU_TO_EPU_ACK)
34 wake_up(&cx->mb_apu_waitq);
35}
36
37static void epu_cmd(struct cx18 *cx, u32 sw1)
38{
39 if (sw1 & IRQ_CPU_TO_EPU)
40 cx18_api_epu_cmd_irq(cx, CPU);
41 if (sw1 & IRQ_APU_TO_EPU)
42 cx18_api_epu_cmd_irq(cx, APU);
43}
44
45irqreturn_t cx18_irq_handler(int irq, void *dev_id)
46{
47 struct cx18 *cx = (struct cx18 *)dev_id;
48 u32 sw1, sw2, hw2;
49
50 sw1 = cx18_read_reg(cx, SW1_INT_STATUS) & cx->sw1_irq_mask;
51 sw2 = cx18_read_reg(cx, SW2_INT_STATUS) & cx->sw2_irq_mask;
52 hw2 = cx18_read_reg(cx, HW2_INT_CLR_STATUS) & cx->hw2_irq_mask;
53
54 if (sw1)
55 cx18_write_reg_expect(cx, sw1, SW1_INT_STATUS, ~sw1, sw1);
56 if (sw2)
57 cx18_write_reg_expect(cx, sw2, SW2_INT_STATUS, ~sw2, sw2);
58 if (hw2)
59 cx18_write_reg_expect(cx, hw2, HW2_INT_CLR_STATUS, ~hw2, hw2);
60
61 if (sw1 || sw2 || hw2)
62 CX18_DEBUG_HI_IRQ("received interrupts "
63 "SW1: %x SW2: %x HW2: %x\n", sw1, sw2, hw2);
64
65 /*
66 * SW1 responses have to happen first. The sending XPU times out the
67 * incoming mailboxes on us rather rapidly.
68 */
69 if (sw1)
70 epu_cmd(cx, sw1);
71
72 /* To do: interrupt-based I2C handling
73 if (hw2 & (HW2_I2C1_INT|HW2_I2C2_INT)) {
74 }
75 */
76
77 if (sw2)
78 xpu_ack(cx, sw2);
79
80 return (sw1 || sw2 || hw2) ? IRQ_HANDLED : IRQ_NONE;
81}
diff --git a/drivers/media/pci/cx18/cx18-irq.h b/drivers/media/pci/cx18/cx18-irq.h
new file mode 100644
index 000000000000..30e7eaf8cb55
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-irq.h
@@ -0,0 +1,35 @@
1/*
2 * cx18 interrupt handling
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#define HW2_I2C1_INT (1 << 22)
24#define HW2_I2C2_INT (1 << 23)
25#define HW2_INT_CLR_STATUS 0xc730c4
26#define HW2_INT_MASK5_PCI 0xc730e4
27#define SW1_INT_SET 0xc73100
28#define SW1_INT_STATUS 0xc73104
29#define SW1_INT_ENABLE_PCI 0xc7311c
30#define SW2_INT_SET 0xc73140
31#define SW2_INT_STATUS 0xc73144
32#define SW2_INT_ENABLE_CPU 0xc73158
33#define SW2_INT_ENABLE_PCI 0xc7315c
34
35irqreturn_t cx18_irq_handler(int irq, void *dev_id);
diff --git a/drivers/media/pci/cx18/cx18-mailbox.c b/drivers/media/pci/cx18/cx18-mailbox.c
new file mode 100644
index 000000000000..eabf00c6351b
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-mailbox.c
@@ -0,0 +1,870 @@
1/*
2 * cx18 mailbox functions
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#include <stdarg.h>
24
25#include "cx18-driver.h"
26#include "cx18-io.h"
27#include "cx18-scb.h"
28#include "cx18-irq.h"
29#include "cx18-mailbox.h"
30#include "cx18-queue.h"
31#include "cx18-streams.h"
32#include "cx18-alsa-pcm.h" /* FIXME make configurable */
33
34static const char *rpu_str[] = { "APU", "CPU", "EPU", "HPU" };
35
36#define API_FAST (1 << 2) /* Short timeout */
37#define API_SLOW (1 << 3) /* Additional 300ms timeout */
38
39struct cx18_api_info {
40 u32 cmd;
41 u8 flags; /* Flags, see above */
42 u8 rpu; /* Processing unit */
43 const char *name; /* The name of the command */
44};
45
46#define API_ENTRY(rpu, x, f) { (x), (f), (rpu), #x }
47
48static const struct cx18_api_info api_info[] = {
49 /* MPEG encoder API */
50 API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE, 0),
51 API_ENTRY(CPU, CX18_EPU_DEBUG, 0),
52 API_ENTRY(CPU, CX18_CREATE_TASK, 0),
53 API_ENTRY(CPU, CX18_DESTROY_TASK, 0),
54 API_ENTRY(CPU, CX18_CPU_CAPTURE_START, API_SLOW),
55 API_ENTRY(CPU, CX18_CPU_CAPTURE_STOP, API_SLOW),
56 API_ENTRY(CPU, CX18_CPU_CAPTURE_PAUSE, 0),
57 API_ENTRY(CPU, CX18_CPU_CAPTURE_RESUME, 0),
58 API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE, 0),
59 API_ENTRY(CPU, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 0),
60 API_ENTRY(CPU, CX18_CPU_SET_VIDEO_IN, 0),
61 API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RATE, 0),
62 API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RESOLUTION, 0),
63 API_ENTRY(CPU, CX18_CPU_SET_FILTER_PARAM, 0),
64 API_ENTRY(CPU, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 0),
65 API_ENTRY(CPU, CX18_CPU_SET_MEDIAN_CORING, 0),
66 API_ENTRY(CPU, CX18_CPU_SET_INDEXTABLE, 0),
67 API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PARAMETERS, 0),
68 API_ENTRY(CPU, CX18_CPU_SET_VIDEO_MUTE, 0),
69 API_ENTRY(CPU, CX18_CPU_SET_AUDIO_MUTE, 0),
70 API_ENTRY(CPU, CX18_CPU_SET_MISC_PARAMETERS, 0),
71 API_ENTRY(CPU, CX18_CPU_SET_RAW_VBI_PARAM, API_SLOW),
72 API_ENTRY(CPU, CX18_CPU_SET_CAPTURE_LINE_NO, 0),
73 API_ENTRY(CPU, CX18_CPU_SET_COPYRIGHT, 0),
74 API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PID, 0),
75 API_ENTRY(CPU, CX18_CPU_SET_VIDEO_PID, 0),
76 API_ENTRY(CPU, CX18_CPU_SET_VER_CROP_LINE, 0),
77 API_ENTRY(CPU, CX18_CPU_SET_GOP_STRUCTURE, 0),
78 API_ENTRY(CPU, CX18_CPU_SET_SCENE_CHANGE_DETECTION, 0),
79 API_ENTRY(CPU, CX18_CPU_SET_ASPECT_RATIO, 0),
80 API_ENTRY(CPU, CX18_CPU_SET_SKIP_INPUT_FRAME, 0),
81 API_ENTRY(CPU, CX18_CPU_SET_SLICED_VBI_PARAM, 0),
82 API_ENTRY(CPU, CX18_CPU_SET_USERDATA_PLACE_HOLDER, 0),
83 API_ENTRY(CPU, CX18_CPU_GET_ENC_PTS, 0),
84 API_ENTRY(CPU, CX18_CPU_SET_VFC_PARAM, 0),
85 API_ENTRY(CPU, CX18_CPU_DE_SET_MDL_ACK, 0),
86 API_ENTRY(CPU, CX18_CPU_DE_SET_MDL, API_FAST),
87 API_ENTRY(CPU, CX18_CPU_DE_RELEASE_MDL, API_SLOW),
88 API_ENTRY(APU, CX18_APU_START, 0),
89 API_ENTRY(APU, CX18_APU_STOP, 0),
90 API_ENTRY(APU, CX18_APU_RESETAI, 0),
91 API_ENTRY(CPU, CX18_CPU_DEBUG_PEEK32, 0),
92 API_ENTRY(0, 0, 0),
93};
94
95static const struct cx18_api_info *find_api_info(u32 cmd)
96{
97 int i;
98
99 for (i = 0; api_info[i].cmd; i++)
100 if (api_info[i].cmd == cmd)
101 return &api_info[i];
102 return NULL;
103}
104
105/* Call with buf of n*11+1 bytes */
106static char *u32arr2hex(u32 data[], int n, char *buf)
107{
108 char *p;
109 int i;
110
111 for (i = 0, p = buf; i < n; i++, p += 11) {
112 /* kernel snprintf() appends '\0' always */
113 snprintf(p, 12, " %#010x", data[i]);
114 }
115 *p = '\0';
116 return buf;
117}
118
119static void dump_mb(struct cx18 *cx, struct cx18_mailbox *mb, char *name)
120{
121 char argstr[MAX_MB_ARGUMENTS*11+1];
122
123 if (!(cx18_debug & CX18_DBGFLG_API))
124 return;
125
126 CX18_DEBUG_API("%s: req %#010x ack %#010x cmd %#010x err %#010x args%s"
127 "\n", name, mb->request, mb->ack, mb->cmd, mb->error,
128 u32arr2hex(mb->args, MAX_MB_ARGUMENTS, argstr));
129}
130
131
132/*
133 * Functions that run in a work_queue work handling context
134 */
135
136static void cx18_mdl_send_to_dvb(struct cx18_stream *s, struct cx18_mdl *mdl)
137{
138 struct cx18_buffer *buf;
139
140 if (s->dvb == NULL || !s->dvb->enabled || mdl->bytesused == 0)
141 return;
142
143 /* We ignore mdl and buf readpos accounting here - it doesn't matter */
144
145 /* The likely case */
146 if (list_is_singular(&mdl->buf_list)) {
147 buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
148 list);
149 if (buf->bytesused)
150 dvb_dmx_swfilter(&s->dvb->demux,
151 buf->buf, buf->bytesused);
152 return;
153 }
154
155 list_for_each_entry(buf, &mdl->buf_list, list) {
156 if (buf->bytesused == 0)
157 break;
158 dvb_dmx_swfilter(&s->dvb->demux, buf->buf, buf->bytesused);
159 }
160}
161
162static void cx18_mdl_send_to_videobuf(struct cx18_stream *s,
163 struct cx18_mdl *mdl)
164{
165 struct cx18_videobuf_buffer *vb_buf;
166 struct cx18_buffer *buf;
167 u8 *p;
168 u32 offset = 0;
169 int dispatch = 0;
170
171 if (mdl->bytesused == 0)
172 return;
173
174 /* Acquire a videobuf buffer, clone to and and release it */
175 spin_lock(&s->vb_lock);
176 if (list_empty(&s->vb_capture))
177 goto out;
178
179 vb_buf = list_first_entry(&s->vb_capture, struct cx18_videobuf_buffer,
180 vb.queue);
181
182 p = videobuf_to_vmalloc(&vb_buf->vb);
183 if (!p)
184 goto out;
185
186 offset = vb_buf->bytes_used;
187 list_for_each_entry(buf, &mdl->buf_list, list) {
188 if (buf->bytesused == 0)
189 break;
190
191 if ((offset + buf->bytesused) <= vb_buf->vb.bsize) {
192 memcpy(p + offset, buf->buf, buf->bytesused);
193 offset += buf->bytesused;
194 vb_buf->bytes_used += buf->bytesused;
195 }
196 }
197
198 /* If we've filled the buffer as per the callers res then dispatch it */
199 if (vb_buf->bytes_used >= s->vb_bytes_per_frame) {
200 dispatch = 1;
201 vb_buf->bytes_used = 0;
202 }
203
204 if (dispatch) {
205 vb_buf->vb.ts = ktime_to_timeval(ktime_get());
206 list_del(&vb_buf->vb.queue);
207 vb_buf->vb.state = VIDEOBUF_DONE;
208 wake_up(&vb_buf->vb.done);
209 }
210
211 mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
212
213out:
214 spin_unlock(&s->vb_lock);
215}
216
217static void cx18_mdl_send_to_alsa(struct cx18 *cx, struct cx18_stream *s,
218 struct cx18_mdl *mdl)
219{
220 struct cx18_buffer *buf;
221
222 if (mdl->bytesused == 0)
223 return;
224
225 /* We ignore mdl and buf readpos accounting here - it doesn't matter */
226
227 /* The likely case */
228 if (list_is_singular(&mdl->buf_list)) {
229 buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
230 list);
231 if (buf->bytesused)
232 cx->pcm_announce_callback(cx->alsa, buf->buf,
233 buf->bytesused);
234 return;
235 }
236
237 list_for_each_entry(buf, &mdl->buf_list, list) {
238 if (buf->bytesused == 0)
239 break;
240 cx->pcm_announce_callback(cx->alsa, buf->buf, buf->bytesused);
241 }
242}
243
244static void epu_dma_done(struct cx18 *cx, struct cx18_in_work_order *order)
245{
246 u32 handle, mdl_ack_count, id;
247 struct cx18_mailbox *mb;
248 struct cx18_mdl_ack *mdl_ack;
249 struct cx18_stream *s;
250 struct cx18_mdl *mdl;
251 int i;
252
253 mb = &order->mb;
254 handle = mb->args[0];
255 s = cx18_handle_to_stream(cx, handle);
256
257 if (s == NULL) {
258 CX18_WARN("Got DMA done notification for unknown/inactive"
259 " handle %d, %s mailbox seq no %d\n", handle,
260 (order->flags & CX18_F_EWO_MB_STALE_UPON_RECEIPT) ?
261 "stale" : "good", mb->request);
262 return;
263 }
264
265 mdl_ack_count = mb->args[2];
266 mdl_ack = order->mdl_ack;
267 for (i = 0; i < mdl_ack_count; i++, mdl_ack++) {
268 id = mdl_ack->id;
269 /*
270 * Simple integrity check for processing a stale (and possibly
271 * inconsistent mailbox): make sure the MDL id is in the
272 * valid range for the stream.
273 *
274 * We go through the trouble of dealing with stale mailboxes
275 * because most of the time, the mailbox data is still valid and
276 * unchanged (and in practice the firmware ping-pongs the
277 * two mdl_ack buffers so mdl_acks are not stale).
278 *
279 * There are occasions when we get a half changed mailbox,
280 * which this check catches for a handle & id mismatch. If the
281 * handle and id do correspond, the worst case is that we
282 * completely lost the old MDL, but pick up the new MDL
283 * early (but the new mdl_ack is guaranteed to be good in this
284 * case as the firmware wouldn't point us to a new mdl_ack until
285 * it's filled in).
286 *
287 * cx18_queue_get_mdl() will detect the lost MDLs
288 * and send them back to q_free for fw rotation eventually.
289 */
290 if ((order->flags & CX18_F_EWO_MB_STALE_UPON_RECEIPT) &&
291 !(id >= s->mdl_base_idx &&
292 id < (s->mdl_base_idx + s->buffers))) {
293 CX18_WARN("Fell behind! Ignoring stale mailbox with "
294 " inconsistent data. Lost MDL for mailbox "
295 "seq no %d\n", mb->request);
296 break;
297 }
298 mdl = cx18_queue_get_mdl(s, id, mdl_ack->data_used);
299
300 CX18_DEBUG_HI_DMA("DMA DONE for %s (MDL %d)\n", s->name, id);
301 if (mdl == NULL) {
302 CX18_WARN("Could not find MDL %d for stream %s\n",
303 id, s->name);
304 continue;
305 }
306
307 CX18_DEBUG_HI_DMA("%s recv bytesused = %d\n",
308 s->name, mdl->bytesused);
309
310 if (s->type == CX18_ENC_STREAM_TYPE_TS) {
311 cx18_mdl_send_to_dvb(s, mdl);
312 cx18_enqueue(s, mdl, &s->q_free);
313 } else if (s->type == CX18_ENC_STREAM_TYPE_PCM) {
314 /* Pass the data to cx18-alsa */
315 if (cx->pcm_announce_callback != NULL) {
316 cx18_mdl_send_to_alsa(cx, s, mdl);
317 cx18_enqueue(s, mdl, &s->q_free);
318 } else {
319 cx18_enqueue(s, mdl, &s->q_full);
320 }
321 } else if (s->type == CX18_ENC_STREAM_TYPE_YUV) {
322 cx18_mdl_send_to_videobuf(s, mdl);
323 cx18_enqueue(s, mdl, &s->q_free);
324 } else {
325 cx18_enqueue(s, mdl, &s->q_full);
326 if (s->type == CX18_ENC_STREAM_TYPE_IDX)
327 cx18_stream_rotate_idx_mdls(cx);
328 }
329 }
330 /* Put as many MDLs as possible back into fw use */
331 cx18_stream_load_fw_queue(s);
332
333 wake_up(&cx->dma_waitq);
334 if (s->id != -1)
335 wake_up(&s->waitq);
336}
337
338static void epu_debug(struct cx18 *cx, struct cx18_in_work_order *order)
339{
340 char *p;
341 char *str = order->str;
342
343 CX18_DEBUG_INFO("%x %s\n", order->mb.args[0], str);
344 p = strchr(str, '.');
345 if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags) && p && p > str)
346 CX18_INFO("FW version: %s\n", p - 1);
347}
348
349static void epu_cmd(struct cx18 *cx, struct cx18_in_work_order *order)
350{
351 switch (order->rpu) {
352 case CPU:
353 {
354 switch (order->mb.cmd) {
355 case CX18_EPU_DMA_DONE:
356 epu_dma_done(cx, order);
357 break;
358 case CX18_EPU_DEBUG:
359 epu_debug(cx, order);
360 break;
361 default:
362 CX18_WARN("Unknown CPU to EPU mailbox command %#0x\n",
363 order->mb.cmd);
364 break;
365 }
366 break;
367 }
368 case APU:
369 CX18_WARN("Unknown APU to EPU mailbox command %#0x\n",
370 order->mb.cmd);
371 break;
372 default:
373 break;
374 }
375}
376
377static
378void free_in_work_order(struct cx18 *cx, struct cx18_in_work_order *order)
379{
380 atomic_set(&order->pending, 0);
381}
382
383void cx18_in_work_handler(struct work_struct *work)
384{
385 struct cx18_in_work_order *order =
386 container_of(work, struct cx18_in_work_order, work);
387 struct cx18 *cx = order->cx;
388 epu_cmd(cx, order);
389 free_in_work_order(cx, order);
390}
391
392
393/*
394 * Functions that run in an interrupt handling context
395 */
396
397static void mb_ack_irq(struct cx18 *cx, struct cx18_in_work_order *order)
398{
399 struct cx18_mailbox __iomem *ack_mb;
400 u32 ack_irq, req;
401
402 switch (order->rpu) {
403 case APU:
404 ack_irq = IRQ_EPU_TO_APU_ACK;
405 ack_mb = &cx->scb->apu2epu_mb;
406 break;
407 case CPU:
408 ack_irq = IRQ_EPU_TO_CPU_ACK;
409 ack_mb = &cx->scb->cpu2epu_mb;
410 break;
411 default:
412 CX18_WARN("Unhandled RPU (%d) for command %x ack\n",
413 order->rpu, order->mb.cmd);
414 return;
415 }
416
417 req = order->mb.request;
418 /* Don't ack if the RPU has gotten impatient and timed us out */
419 if (req != cx18_readl(cx, &ack_mb->request) ||
420 req == cx18_readl(cx, &ack_mb->ack)) {
421 CX18_DEBUG_WARN("Possibly falling behind: %s self-ack'ed our "
422 "incoming %s to EPU mailbox (sequence no. %u) "
423 "while processing\n",
424 rpu_str[order->rpu], rpu_str[order->rpu], req);
425 order->flags |= CX18_F_EWO_MB_STALE_WHILE_PROC;
426 return;
427 }
428 cx18_writel(cx, req, &ack_mb->ack);
429 cx18_write_reg_expect(cx, ack_irq, SW2_INT_SET, ack_irq, ack_irq);
430 return;
431}
432
433static int epu_dma_done_irq(struct cx18 *cx, struct cx18_in_work_order *order)
434{
435 u32 handle, mdl_ack_offset, mdl_ack_count;
436 struct cx18_mailbox *mb;
437 int i;
438
439 mb = &order->mb;
440 handle = mb->args[0];
441 mdl_ack_offset = mb->args[1];
442 mdl_ack_count = mb->args[2];
443
444 if (handle == CX18_INVALID_TASK_HANDLE ||
445 mdl_ack_count == 0 || mdl_ack_count > CX18_MAX_MDL_ACKS) {
446 if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
447 mb_ack_irq(cx, order);
448 return -1;
449 }
450
451 for (i = 0; i < sizeof(struct cx18_mdl_ack) * mdl_ack_count; i += sizeof(u32))
452 ((u32 *)order->mdl_ack)[i / sizeof(u32)] =
453 cx18_readl(cx, cx->enc_mem + mdl_ack_offset + i);
454
455 if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
456 mb_ack_irq(cx, order);
457 return 1;
458}
459
460static
461int epu_debug_irq(struct cx18 *cx, struct cx18_in_work_order *order)
462{
463 u32 str_offset;
464 char *str = order->str;
465
466 str[0] = '\0';
467 str_offset = order->mb.args[1];
468 if (str_offset) {
469 cx18_setup_page(cx, str_offset);
470 cx18_memcpy_fromio(cx, str, cx->enc_mem + str_offset, 252);
471 str[252] = '\0';
472 cx18_setup_page(cx, SCB_OFFSET);
473 }
474
475 if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
476 mb_ack_irq(cx, order);
477
478 return str_offset ? 1 : 0;
479}
480
481static inline
482int epu_cmd_irq(struct cx18 *cx, struct cx18_in_work_order *order)
483{
484 int ret = -1;
485
486 switch (order->rpu) {
487 case CPU:
488 {
489 switch (order->mb.cmd) {
490 case CX18_EPU_DMA_DONE:
491 ret = epu_dma_done_irq(cx, order);
492 break;
493 case CX18_EPU_DEBUG:
494 ret = epu_debug_irq(cx, order);
495 break;
496 default:
497 CX18_WARN("Unknown CPU to EPU mailbox command %#0x\n",
498 order->mb.cmd);
499 break;
500 }
501 break;
502 }
503 case APU:
504 CX18_WARN("Unknown APU to EPU mailbox command %#0x\n",
505 order->mb.cmd);
506 break;
507 default:
508 break;
509 }
510 return ret;
511}
512
513static inline
514struct cx18_in_work_order *alloc_in_work_order_irq(struct cx18 *cx)
515{
516 int i;
517 struct cx18_in_work_order *order = NULL;
518
519 for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
520 /*
521 * We only need "pending" atomic to inspect its contents,
522 * and need not do a check and set because:
523 * 1. Any work handler thread only clears "pending" and only
524 * on one, particular work order at a time, per handler thread.
525 * 2. "pending" is only set here, and we're serialized because
526 * we're called in an IRQ handler context.
527 */
528 if (atomic_read(&cx->in_work_order[i].pending) == 0) {
529 order = &cx->in_work_order[i];
530 atomic_set(&order->pending, 1);
531 break;
532 }
533 }
534 return order;
535}
536
537void cx18_api_epu_cmd_irq(struct cx18 *cx, int rpu)
538{
539 struct cx18_mailbox __iomem *mb;
540 struct cx18_mailbox *order_mb;
541 struct cx18_in_work_order *order;
542 int submit;
543 int i;
544
545 switch (rpu) {
546 case CPU:
547 mb = &cx->scb->cpu2epu_mb;
548 break;
549 case APU:
550 mb = &cx->scb->apu2epu_mb;
551 break;
552 default:
553 return;
554 }
555
556 order = alloc_in_work_order_irq(cx);
557 if (order == NULL) {
558 CX18_WARN("Unable to find blank work order form to schedule "
559 "incoming mailbox command processing\n");
560 return;
561 }
562
563 order->flags = 0;
564 order->rpu = rpu;
565 order_mb = &order->mb;
566
567 /* mb->cmd and mb->args[0] through mb->args[2] */
568 for (i = 0; i < 4; i++)
569 (&order_mb->cmd)[i] = cx18_readl(cx, &mb->cmd + i);
570
571 /* mb->request and mb->ack. N.B. we want to read mb->ack last */
572 for (i = 0; i < 2; i++)
573 (&order_mb->request)[i] = cx18_readl(cx, &mb->request + i);
574
575 if (order_mb->request == order_mb->ack) {
576 CX18_DEBUG_WARN("Possibly falling behind: %s self-ack'ed our "
577 "incoming %s to EPU mailbox (sequence no. %u)"
578 "\n",
579 rpu_str[rpu], rpu_str[rpu], order_mb->request);
580 if (cx18_debug & CX18_DBGFLG_WARN)
581 dump_mb(cx, order_mb, "incoming");
582 order->flags = CX18_F_EWO_MB_STALE_UPON_RECEIPT;
583 }
584
585 /*
586 * Individual EPU command processing is responsible for ack-ing
587 * a non-stale mailbox as soon as possible
588 */
589 submit = epu_cmd_irq(cx, order);
590 if (submit > 0) {
591 queue_work(cx->in_work_queue, &order->work);
592 }
593}
594
595
596/*
597 * Functions called from a non-interrupt, non work_queue context
598 */
599
600static int cx18_api_call(struct cx18 *cx, u32 cmd, int args, u32 data[])
601{
602 const struct cx18_api_info *info = find_api_info(cmd);
603 u32 irq, req, ack, err;
604 struct cx18_mailbox __iomem *mb;
605 wait_queue_head_t *waitq;
606 struct mutex *mb_lock;
607 unsigned long int t0, timeout, ret;
608 int i;
609 char argstr[MAX_MB_ARGUMENTS*11+1];
610 DEFINE_WAIT(w);
611
612 if (info == NULL) {
613 CX18_WARN("unknown cmd %x\n", cmd);
614 return -EINVAL;
615 }
616
617 if (cx18_debug & CX18_DBGFLG_API) { /* only call u32arr2hex if needed */
618 if (cmd == CX18_CPU_DE_SET_MDL) {
619 if (cx18_debug & CX18_DBGFLG_HIGHVOL)
620 CX18_DEBUG_HI_API("%s\tcmd %#010x args%s\n",
621 info->name, cmd,
622 u32arr2hex(data, args, argstr));
623 } else
624 CX18_DEBUG_API("%s\tcmd %#010x args%s\n",
625 info->name, cmd,
626 u32arr2hex(data, args, argstr));
627 }
628
629 switch (info->rpu) {
630 case APU:
631 waitq = &cx->mb_apu_waitq;
632 mb_lock = &cx->epu2apu_mb_lock;
633 irq = IRQ_EPU_TO_APU;
634 mb = &cx->scb->epu2apu_mb;
635 break;
636 case CPU:
637 waitq = &cx->mb_cpu_waitq;
638 mb_lock = &cx->epu2cpu_mb_lock;
639 irq = IRQ_EPU_TO_CPU;
640 mb = &cx->scb->epu2cpu_mb;
641 break;
642 default:
643 CX18_WARN("Unknown RPU (%d) for API call\n", info->rpu);
644 return -EINVAL;
645 }
646
647 mutex_lock(mb_lock);
648 /*
649 * Wait for an in-use mailbox to complete
650 *
651 * If the XPU is responding with Ack's, the mailbox shouldn't be in
652 * a busy state, since we serialize access to it on our end.
653 *
654 * If the wait for ack after sending a previous command was interrupted
655 * by a signal, we may get here and find a busy mailbox. After waiting,
656 * mark it "not busy" from our end, if the XPU hasn't ack'ed it still.
657 */
658 req = cx18_readl(cx, &mb->request);
659 timeout = msecs_to_jiffies(10);
660 ret = wait_event_timeout(*waitq,
661 (ack = cx18_readl(cx, &mb->ack)) == req,
662 timeout);
663 if (req != ack) {
664 /* waited long enough, make the mbox "not busy" from our end */
665 cx18_writel(cx, req, &mb->ack);
666 CX18_ERR("mbox was found stuck busy when setting up for %s; "
667 "clearing busy and trying to proceed\n", info->name);
668 } else if (ret != timeout)
669 CX18_DEBUG_API("waited %u msecs for busy mbox to be acked\n",
670 jiffies_to_msecs(timeout-ret));
671
672 /* Build the outgoing mailbox */
673 req = ((req & 0xfffffffe) == 0xfffffffe) ? 1 : req + 1;
674
675 cx18_writel(cx, cmd, &mb->cmd);
676 for (i = 0; i < args; i++)
677 cx18_writel(cx, data[i], &mb->args[i]);
678 cx18_writel(cx, 0, &mb->error);
679 cx18_writel(cx, req, &mb->request);
680 cx18_writel(cx, req - 1, &mb->ack); /* ensure ack & req are distinct */
681
682 /*
683 * Notify the XPU and wait for it to send an Ack back
684 */
685 timeout = msecs_to_jiffies((info->flags & API_FAST) ? 10 : 20);
686
687 CX18_DEBUG_HI_IRQ("sending interrupt SW1: %x to send %s\n",
688 irq, info->name);
689
690 /* So we don't miss the wakeup, prepare to wait before notifying fw */
691 prepare_to_wait(waitq, &w, TASK_UNINTERRUPTIBLE);
692 cx18_write_reg_expect(cx, irq, SW1_INT_SET, irq, irq);
693
694 t0 = jiffies;
695 ack = cx18_readl(cx, &mb->ack);
696 if (ack != req) {
697 schedule_timeout(timeout);
698 ret = jiffies - t0;
699 ack = cx18_readl(cx, &mb->ack);
700 } else {
701 ret = jiffies - t0;
702 }
703
704 finish_wait(waitq, &w);
705
706 if (req != ack) {
707 mutex_unlock(mb_lock);
708 if (ret >= timeout) {
709 /* Timed out */
710 CX18_DEBUG_WARN("sending %s timed out waiting %d msecs "
711 "for RPU acknowledgement\n",
712 info->name, jiffies_to_msecs(ret));
713 } else {
714 CX18_DEBUG_WARN("woken up before mailbox ack was ready "
715 "after submitting %s to RPU. only "
716 "waited %d msecs on req %u but awakened"
717 " with unmatched ack %u\n",
718 info->name,
719 jiffies_to_msecs(ret),
720 req, ack);
721 }
722 return -EINVAL;
723 }
724
725 if (ret >= timeout)
726 CX18_DEBUG_WARN("failed to be awakened upon RPU acknowledgment "
727 "sending %s; timed out waiting %d msecs\n",
728 info->name, jiffies_to_msecs(ret));
729 else
730 CX18_DEBUG_HI_API("waited %u msecs for %s to be acked\n",
731 jiffies_to_msecs(ret), info->name);
732
733 /* Collect data returned by the XPU */
734 for (i = 0; i < MAX_MB_ARGUMENTS; i++)
735 data[i] = cx18_readl(cx, &mb->args[i]);
736 err = cx18_readl(cx, &mb->error);
737 mutex_unlock(mb_lock);
738
739 /*
740 * Wait for XPU to perform extra actions for the caller in some cases.
741 * e.g. CX18_CPU_DE_RELEASE_MDL will cause the CPU to send all MDLs
742 * back in a burst shortly thereafter
743 */
744 if (info->flags & API_SLOW)
745 cx18_msleep_timeout(300, 0);
746
747 if (err)
748 CX18_DEBUG_API("mailbox error %08x for command %s\n", err,
749 info->name);
750 return err ? -EIO : 0;
751}
752
753int cx18_api(struct cx18 *cx, u32 cmd, int args, u32 data[])
754{
755 return cx18_api_call(cx, cmd, args, data);
756}
757
758static int cx18_set_filter_param(struct cx18_stream *s)
759{
760 struct cx18 *cx = s->cx;
761 u32 mode;
762 int ret;
763
764 mode = (cx->filter_mode & 1) ? 2 : (cx->spatial_strength ? 1 : 0);
765 ret = cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
766 s->handle, 1, mode, cx->spatial_strength);
767 mode = (cx->filter_mode & 2) ? 2 : (cx->temporal_strength ? 1 : 0);
768 ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
769 s->handle, 0, mode, cx->temporal_strength);
770 ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
771 s->handle, 2, cx->filter_mode >> 2, 0);
772 return ret;
773}
774
775int cx18_api_func(void *priv, u32 cmd, int in, int out,
776 u32 data[CX2341X_MBOX_MAX_DATA])
777{
778 struct cx18_stream *s = priv;
779 struct cx18 *cx = s->cx;
780
781 switch (cmd) {
782 case CX2341X_ENC_SET_OUTPUT_PORT:
783 return 0;
784 case CX2341X_ENC_SET_FRAME_RATE:
785 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_IN, 6,
786 s->handle, 0, 0, 0, 0, data[0]);
787 case CX2341X_ENC_SET_FRAME_SIZE:
788 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RESOLUTION, 3,
789 s->handle, data[1], data[0]);
790 case CX2341X_ENC_SET_STREAM_TYPE:
791 return cx18_vapi(cx, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 2,
792 s->handle, data[0]);
793 case CX2341X_ENC_SET_ASPECT_RATIO:
794 return cx18_vapi(cx, CX18_CPU_SET_ASPECT_RATIO, 2,
795 s->handle, data[0]);
796
797 case CX2341X_ENC_SET_GOP_PROPERTIES:
798 return cx18_vapi(cx, CX18_CPU_SET_GOP_STRUCTURE, 3,
799 s->handle, data[0], data[1]);
800 case CX2341X_ENC_SET_GOP_CLOSURE:
801 return 0;
802 case CX2341X_ENC_SET_AUDIO_PROPERTIES:
803 return cx18_vapi(cx, CX18_CPU_SET_AUDIO_PARAMETERS, 2,
804 s->handle, data[0]);
805 case CX2341X_ENC_MUTE_AUDIO:
806 return cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2,
807 s->handle, data[0]);
808 case CX2341X_ENC_SET_BIT_RATE:
809 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RATE, 5,
810 s->handle, data[0], data[1], data[2], data[3]);
811 case CX2341X_ENC_MUTE_VIDEO:
812 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
813 s->handle, data[0]);
814 case CX2341X_ENC_SET_FRAME_DROP_RATE:
815 return cx18_vapi(cx, CX18_CPU_SET_SKIP_INPUT_FRAME, 2,
816 s->handle, data[0]);
817 case CX2341X_ENC_MISC:
818 return cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 4,
819 s->handle, data[0], data[1], data[2]);
820 case CX2341X_ENC_SET_DNR_FILTER_MODE:
821 cx->filter_mode = (data[0] & 3) | (data[1] << 2);
822 return cx18_set_filter_param(s);
823 case CX2341X_ENC_SET_DNR_FILTER_PROPS:
824 cx->spatial_strength = data[0];
825 cx->temporal_strength = data[1];
826 return cx18_set_filter_param(s);
827 case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
828 return cx18_vapi(cx, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 3,
829 s->handle, data[0], data[1]);
830 case CX2341X_ENC_SET_CORING_LEVELS:
831 return cx18_vapi(cx, CX18_CPU_SET_MEDIAN_CORING, 5,
832 s->handle, data[0], data[1], data[2], data[3]);
833 }
834 CX18_WARN("Unknown cmd %x\n", cmd);
835 return 0;
836}
837
838int cx18_vapi_result(struct cx18 *cx, u32 data[MAX_MB_ARGUMENTS],
839 u32 cmd, int args, ...)
840{
841 va_list ap;
842 int i;
843
844 va_start(ap, args);
845 for (i = 0; i < args; i++)
846 data[i] = va_arg(ap, u32);
847 va_end(ap);
848 return cx18_api(cx, cmd, args, data);
849}
850
851int cx18_vapi(struct cx18 *cx, u32 cmd, int args, ...)
852{
853 u32 data[MAX_MB_ARGUMENTS];
854 va_list ap;
855 int i;
856
857 if (cx == NULL) {
858 CX18_ERR("cx == NULL (cmd=%x)\n", cmd);
859 return 0;
860 }
861 if (args > MAX_MB_ARGUMENTS) {
862 CX18_ERR("args too big (cmd=%x)\n", cmd);
863 args = MAX_MB_ARGUMENTS;
864 }
865 va_start(ap, args);
866 for (i = 0; i < args; i++)
867 data[i] = va_arg(ap, u32);
868 va_end(ap);
869 return cx18_api(cx, cmd, args, data);
870}
diff --git a/drivers/media/pci/cx18/cx18-mailbox.h b/drivers/media/pci/cx18/cx18-mailbox.h
new file mode 100644
index 000000000000..b63fdfaac49e
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-mailbox.h
@@ -0,0 +1,95 @@
1/*
2 * cx18 mailbox functions
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#ifndef _CX18_MAILBOX_H_
24#define _CX18_MAILBOX_H_
25
26/* mailbox max args */
27#define MAX_MB_ARGUMENTS 6
28/* compatibility, should be same as the define in cx2341x.h */
29#define CX2341X_MBOX_MAX_DATA 16
30
31#define MB_RESERVED_HANDLE_0 0
32#define MB_RESERVED_HANDLE_1 0xFFFFFFFF
33
34#define APU 0
35#define CPU 1
36#define EPU 2
37#define HPU 3
38
39struct cx18;
40
41/*
42 * This structure is used by CPU to provide completed MDL & buffers information.
43 * Its structure is dictated by the layout of the SCB, required by the
44 * firmware, but its definition needs to be here, instead of in cx18-scb.h,
45 * for mailbox work order scheduling
46 */
47struct cx18_mdl_ack {
48 u32 id; /* ID of a completed MDL */
49 u32 data_used; /* Total data filled in the MDL with 'id' */
50};
51
52/* The cx18_mailbox struct is the mailbox structure which is used for passing
53 messages between processors */
54struct cx18_mailbox {
55 /* The sender sets a handle in 'request' after he fills the command. The
56 'request' should be different than 'ack'. The sender, also, generates
57 an interrupt on XPU2YPU_irq where XPU is the sender and YPU is the
58 receiver. */
59 u32 request;
60 /* The receiver detects a new command when 'req' is different than 'ack'.
61 He sets 'ack' to the same value as 'req' to clear the command. He, also,
62 generates an interrupt on YPU2XPU_irq where XPU is the sender and YPU
63 is the receiver. */
64 u32 ack;
65 u32 reserved[6];
66 /* 'cmd' identifies the command. The list of these commands are in
67 cx23418.h */
68 u32 cmd;
69 /* Each command can have up to 6 arguments */
70 u32 args[MAX_MB_ARGUMENTS];
71 /* The return code can be one of the codes in the file cx23418.h. If the
72 command is completed successfully, the error will be ERR_SYS_SUCCESS.
73 If it is pending, the code is ERR_SYS_PENDING. If it failed, the error
74 code would indicate the task from which the error originated and will
75 be one of the errors in cx23418.h. In that case, the following
76 applies ((error & 0xff) != 0).
77 If the command is pending, the return will be passed in a MB from the
78 receiver to the sender. 'req' will be returned in args[0] */
79 u32 error;
80};
81
82struct cx18_stream;
83
84int cx18_api(struct cx18 *cx, u32 cmd, int args, u32 data[]);
85int cx18_vapi_result(struct cx18 *cx, u32 data[MAX_MB_ARGUMENTS], u32 cmd,
86 int args, ...);
87int cx18_vapi(struct cx18 *cx, u32 cmd, int args, ...);
88int cx18_api_func(void *priv, u32 cmd, int in, int out,
89 u32 data[CX2341X_MBOX_MAX_DATA]);
90
91void cx18_api_epu_cmd_irq(struct cx18 *cx, int rpu);
92
93void cx18_in_work_handler(struct work_struct *work);
94
95#endif
diff --git a/drivers/media/pci/cx18/cx18-queue.c b/drivers/media/pci/cx18/cx18-queue.c
new file mode 100644
index 000000000000..8884537bd62f
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-queue.c
@@ -0,0 +1,443 @@
1/*
2 * cx18 buffer queues
3 *
4 * Derived from ivtv-queue.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-queue.h"
27#include "cx18-streams.h"
28#include "cx18-scb.h"
29#include "cx18-io.h"
30
31void cx18_buf_swap(struct cx18_buffer *buf)
32{
33 int i;
34
35 for (i = 0; i < buf->bytesused; i += 4)
36 swab32s((u32 *)(buf->buf + i));
37}
38
39void _cx18_mdl_swap(struct cx18_mdl *mdl)
40{
41 struct cx18_buffer *buf;
42
43 list_for_each_entry(buf, &mdl->buf_list, list) {
44 if (buf->bytesused == 0)
45 break;
46 cx18_buf_swap(buf);
47 }
48}
49
50void cx18_queue_init(struct cx18_queue *q)
51{
52 INIT_LIST_HEAD(&q->list);
53 atomic_set(&q->depth, 0);
54 q->bytesused = 0;
55}
56
57struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
58 struct cx18_queue *q, int to_front)
59{
60 /* clear the mdl if it is not to be enqueued to the full queue */
61 if (q != &s->q_full) {
62 mdl->bytesused = 0;
63 mdl->readpos = 0;
64 mdl->m_flags = 0;
65 mdl->skipped = 0;
66 mdl->curr_buf = NULL;
67 }
68
69 /* q_busy is restricted to a max buffer count imposed by firmware */
70 if (q == &s->q_busy &&
71 atomic_read(&q->depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
72 q = &s->q_free;
73
74 spin_lock(&q->lock);
75
76 if (to_front)
77 list_add(&mdl->list, &q->list); /* LIFO */
78 else
79 list_add_tail(&mdl->list, &q->list); /* FIFO */
80 q->bytesused += mdl->bytesused - mdl->readpos;
81 atomic_inc(&q->depth);
82
83 spin_unlock(&q->lock);
84 return q;
85}
86
87struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
88{
89 struct cx18_mdl *mdl = NULL;
90
91 spin_lock(&q->lock);
92 if (!list_empty(&q->list)) {
93 mdl = list_first_entry(&q->list, struct cx18_mdl, list);
94 list_del_init(&mdl->list);
95 q->bytesused -= mdl->bytesused - mdl->readpos;
96 mdl->skipped = 0;
97 atomic_dec(&q->depth);
98 }
99 spin_unlock(&q->lock);
100 return mdl;
101}
102
103static void _cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
104 struct cx18_mdl *mdl)
105{
106 struct cx18_buffer *buf;
107 u32 buf_size = s->buf_size;
108 u32 bytesused = mdl->bytesused;
109
110 list_for_each_entry(buf, &mdl->buf_list, list) {
111 buf->readpos = 0;
112 if (bytesused >= buf_size) {
113 buf->bytesused = buf_size;
114 bytesused -= buf_size;
115 } else {
116 buf->bytesused = bytesused;
117 bytesused = 0;
118 }
119 cx18_buf_sync_for_cpu(s, buf);
120 }
121}
122
123static inline void cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
124 struct cx18_mdl *mdl)
125{
126 struct cx18_buffer *buf;
127
128 if (list_is_singular(&mdl->buf_list)) {
129 buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
130 list);
131 buf->bytesused = mdl->bytesused;
132 buf->readpos = 0;
133 cx18_buf_sync_for_cpu(s, buf);
134 } else {
135 _cx18_mdl_update_bufs_for_cpu(s, mdl);
136 }
137}
138
139struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id,
140 u32 bytesused)
141{
142 struct cx18 *cx = s->cx;
143 struct cx18_mdl *mdl;
144 struct cx18_mdl *tmp;
145 struct cx18_mdl *ret = NULL;
146 LIST_HEAD(sweep_up);
147
148 /*
149 * We don't have to acquire multiple q locks here, because we are
150 * serialized by the single threaded work handler.
151 * MDLs from the firmware will thus remain in order as
152 * they are moved from q_busy to q_full or to the dvb ring buffer.
153 */
154 spin_lock(&s->q_busy.lock);
155 list_for_each_entry_safe(mdl, tmp, &s->q_busy.list, list) {
156 /*
157 * We should find what the firmware told us is done,
158 * right at the front of the queue. If we don't, we likely have
159 * missed an mdl done message from the firmware.
160 * Once we skip an mdl repeatedly, relative to the size of
161 * q_busy, we have high confidence we've missed it.
162 */
163 if (mdl->id != id) {
164 mdl->skipped++;
165 if (mdl->skipped >= atomic_read(&s->q_busy.depth)-1) {
166 /* mdl must have fallen out of rotation */
167 CX18_WARN("Skipped %s, MDL %d, %d "
168 "times - it must have dropped out of "
169 "rotation\n", s->name, mdl->id,
170 mdl->skipped);
171 /* Sweep it up to put it back into rotation */
172 list_move_tail(&mdl->list, &sweep_up);
173 atomic_dec(&s->q_busy.depth);
174 }
175 continue;
176 }
177 /*
178 * We pull the desired mdl off of the queue here. Something
179 * will have to put it back on a queue later.
180 */
181 list_del_init(&mdl->list);
182 atomic_dec(&s->q_busy.depth);
183 ret = mdl;
184 break;
185 }
186 spin_unlock(&s->q_busy.lock);
187
188 /*
189 * We found the mdl for which we were looking. Get it ready for
190 * the caller to put on q_full or in the dvb ring buffer.
191 */
192 if (ret != NULL) {
193 ret->bytesused = bytesused;
194 ret->skipped = 0;
195 /* 0'ed readpos, m_flags & curr_buf when mdl went on q_busy */
196 cx18_mdl_update_bufs_for_cpu(s, ret);
197 if (s->type != CX18_ENC_STREAM_TYPE_TS)
198 set_bit(CX18_F_M_NEED_SWAP, &ret->m_flags);
199 }
200
201 /* Put any mdls the firmware is ignoring back into normal rotation */
202 list_for_each_entry_safe(mdl, tmp, &sweep_up, list) {
203 list_del_init(&mdl->list);
204 cx18_enqueue(s, mdl, &s->q_free);
205 }
206 return ret;
207}
208
209/* Move all mdls of a queue, while flushing the mdl */
210static void cx18_queue_flush(struct cx18_stream *s,
211 struct cx18_queue *q_src, struct cx18_queue *q_dst)
212{
213 struct cx18_mdl *mdl;
214
215 /* It only makes sense to flush to q_free or q_idle */
216 if (q_src == q_dst || q_dst == &s->q_full || q_dst == &s->q_busy)
217 return;
218
219 spin_lock(&q_src->lock);
220 spin_lock(&q_dst->lock);
221 while (!list_empty(&q_src->list)) {
222 mdl = list_first_entry(&q_src->list, struct cx18_mdl, list);
223 list_move_tail(&mdl->list, &q_dst->list);
224 mdl->bytesused = 0;
225 mdl->readpos = 0;
226 mdl->m_flags = 0;
227 mdl->skipped = 0;
228 mdl->curr_buf = NULL;
229 atomic_inc(&q_dst->depth);
230 }
231 cx18_queue_init(q_src);
232 spin_unlock(&q_src->lock);
233 spin_unlock(&q_dst->lock);
234}
235
236void cx18_flush_queues(struct cx18_stream *s)
237{
238 cx18_queue_flush(s, &s->q_busy, &s->q_free);
239 cx18_queue_flush(s, &s->q_full, &s->q_free);
240}
241
242/*
243 * Note, s->buf_pool is not protected by a lock,
244 * the stream better not have *anything* going on when calling this
245 */
246void cx18_unload_queues(struct cx18_stream *s)
247{
248 struct cx18_queue *q_idle = &s->q_idle;
249 struct cx18_mdl *mdl;
250 struct cx18_buffer *buf;
251
252 /* Move all MDLS to q_idle */
253 cx18_queue_flush(s, &s->q_busy, q_idle);
254 cx18_queue_flush(s, &s->q_full, q_idle);
255 cx18_queue_flush(s, &s->q_free, q_idle);
256
257 /* Reset MDL id's and move all buffers back to the stream's buf_pool */
258 spin_lock(&q_idle->lock);
259 list_for_each_entry(mdl, &q_idle->list, list) {
260 while (!list_empty(&mdl->buf_list)) {
261 buf = list_first_entry(&mdl->buf_list,
262 struct cx18_buffer, list);
263 list_move_tail(&buf->list, &s->buf_pool);
264 buf->bytesused = 0;
265 buf->readpos = 0;
266 }
267 mdl->id = s->mdl_base_idx; /* reset id to a "safe" value */
268 /* all other mdl fields were cleared by cx18_queue_flush() */
269 }
270 spin_unlock(&q_idle->lock);
271}
272
273/*
274 * Note, s->buf_pool is not protected by a lock,
275 * the stream better not have *anything* going on when calling this
276 */
277void cx18_load_queues(struct cx18_stream *s)
278{
279 struct cx18 *cx = s->cx;
280 struct cx18_mdl *mdl;
281 struct cx18_buffer *buf;
282 int mdl_id;
283 int i;
284 u32 partial_buf_size;
285
286 /*
287 * Attach buffers to MDLs, give the MDLs ids, and add MDLs to q_free
288 * Excess MDLs are left on q_idle
289 * Excess buffers are left in buf_pool and/or on an MDL in q_idle
290 */
291 mdl_id = s->mdl_base_idx;
292 for (mdl = cx18_dequeue(s, &s->q_idle), i = s->bufs_per_mdl;
293 mdl != NULL && i == s->bufs_per_mdl;
294 mdl = cx18_dequeue(s, &s->q_idle)) {
295
296 mdl->id = mdl_id;
297
298 for (i = 0; i < s->bufs_per_mdl; i++) {
299 if (list_empty(&s->buf_pool))
300 break;
301
302 buf = list_first_entry(&s->buf_pool, struct cx18_buffer,
303 list);
304 list_move_tail(&buf->list, &mdl->buf_list);
305
306 /* update the firmware's MDL array with this buffer */
307 cx18_writel(cx, buf->dma_handle,
308 &cx->scb->cpu_mdl[mdl_id + i].paddr);
309 cx18_writel(cx, s->buf_size,
310 &cx->scb->cpu_mdl[mdl_id + i].length);
311 }
312
313 if (i == s->bufs_per_mdl) {
314 /*
315 * The encoder doesn't honor s->mdl_size. So in the
316 * case of a non-integral number of buffers to meet
317 * mdl_size, we lie about the size of the last buffer
318 * in the MDL to get the encoder to really only send
319 * us mdl_size bytes per MDL transfer.
320 */
321 partial_buf_size = s->mdl_size % s->buf_size;
322 if (partial_buf_size) {
323 cx18_writel(cx, partial_buf_size,
324 &cx->scb->cpu_mdl[mdl_id + i - 1].length);
325 }
326 cx18_enqueue(s, mdl, &s->q_free);
327 } else {
328 /* Not enough buffers for this MDL; we won't use it */
329 cx18_push(s, mdl, &s->q_idle);
330 }
331 mdl_id += i;
332 }
333}
334
335void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl)
336{
337 int dma = s->dma;
338 u32 buf_size = s->buf_size;
339 struct pci_dev *pci_dev = s->cx->pci_dev;
340 struct cx18_buffer *buf;
341
342 list_for_each_entry(buf, &mdl->buf_list, list)
343 pci_dma_sync_single_for_device(pci_dev, buf->dma_handle,
344 buf_size, dma);
345}
346
347int cx18_stream_alloc(struct cx18_stream *s)
348{
349 struct cx18 *cx = s->cx;
350 int i;
351
352 if (s->buffers == 0)
353 return 0;
354
355 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers "
356 "(%d.%02d kB total)\n",
357 s->name, s->buffers, s->buf_size,
358 s->buffers * s->buf_size / 1024,
359 (s->buffers * s->buf_size * 100 / 1024) % 100);
360
361 if (((char __iomem *)&cx->scb->cpu_mdl[cx->free_mdl_idx + s->buffers] -
362 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
363 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
364 ((char __iomem *)cx->scb->cpu_mdl));
365
366 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
367 CX18_ERR("Max buffers = %zd\n",
368 bufsz / sizeof(struct cx18_mdl_ent));
369 return -ENOMEM;
370 }
371
372 s->mdl_base_idx = cx->free_mdl_idx;
373
374 /* allocate stream buffers and MDLs */
375 for (i = 0; i < s->buffers; i++) {
376 struct cx18_mdl *mdl;
377 struct cx18_buffer *buf;
378
379 /* 1 MDL per buffer to handle the worst & also default case */
380 mdl = kzalloc(sizeof(struct cx18_mdl), GFP_KERNEL|__GFP_NOWARN);
381 if (mdl == NULL)
382 break;
383
384 buf = kzalloc(sizeof(struct cx18_buffer),
385 GFP_KERNEL|__GFP_NOWARN);
386 if (buf == NULL) {
387 kfree(mdl);
388 break;
389 }
390
391 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
392 if (buf->buf == NULL) {
393 kfree(mdl);
394 kfree(buf);
395 break;
396 }
397
398 INIT_LIST_HEAD(&mdl->list);
399 INIT_LIST_HEAD(&mdl->buf_list);
400 mdl->id = s->mdl_base_idx; /* a somewhat safe value */
401 cx18_enqueue(s, mdl, &s->q_idle);
402
403 INIT_LIST_HEAD(&buf->list);
404 buf->dma_handle = pci_map_single(s->cx->pci_dev,
405 buf->buf, s->buf_size, s->dma);
406 cx18_buf_sync_for_cpu(s, buf);
407 list_add_tail(&buf->list, &s->buf_pool);
408 }
409 if (i == s->buffers) {
410 cx->free_mdl_idx += s->buffers;
411 return 0;
412 }
413 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
414 cx18_stream_free(s);
415 return -ENOMEM;
416}
417
418void cx18_stream_free(struct cx18_stream *s)
419{
420 struct cx18_mdl *mdl;
421 struct cx18_buffer *buf;
422 struct cx18 *cx = s->cx;
423
424 CX18_DEBUG_INFO("Deallocating buffers for %s stream\n", s->name);
425
426 /* move all buffers to buf_pool and all MDLs to q_idle */
427 cx18_unload_queues(s);
428
429 /* empty q_idle */
430 while ((mdl = cx18_dequeue(s, &s->q_idle)))
431 kfree(mdl);
432
433 /* empty buf_pool */
434 while (!list_empty(&s->buf_pool)) {
435 buf = list_first_entry(&s->buf_pool, struct cx18_buffer, list);
436 list_del_init(&buf->list);
437
438 pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
439 s->buf_size, s->dma);
440 kfree(buf->buf);
441 kfree(buf);
442 }
443}
diff --git a/drivers/media/pci/cx18/cx18-queue.h b/drivers/media/pci/cx18/cx18-queue.h
new file mode 100644
index 000000000000..4201ddc16091
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-queue.h
@@ -0,0 +1,98 @@
1/*
2 * cx18 buffer queues
3 *
4 * Derived from ivtv-queue.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#define CX18_DMA_UNMAPPED ((u32) -1)
26
27/* cx18_buffer utility functions */
28
29static inline void cx18_buf_sync_for_cpu(struct cx18_stream *s,
30 struct cx18_buffer *buf)
31{
32 pci_dma_sync_single_for_cpu(s->cx->pci_dev, buf->dma_handle,
33 s->buf_size, s->dma);
34}
35
36static inline void cx18_buf_sync_for_device(struct cx18_stream *s,
37 struct cx18_buffer *buf)
38{
39 pci_dma_sync_single_for_device(s->cx->pci_dev, buf->dma_handle,
40 s->buf_size, s->dma);
41}
42
43void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl);
44
45static inline void cx18_mdl_sync_for_device(struct cx18_stream *s,
46 struct cx18_mdl *mdl)
47{
48 if (list_is_singular(&mdl->buf_list))
49 cx18_buf_sync_for_device(s, list_first_entry(&mdl->buf_list,
50 struct cx18_buffer,
51 list));
52 else
53 _cx18_mdl_sync_for_device(s, mdl);
54}
55
56void cx18_buf_swap(struct cx18_buffer *buf);
57void _cx18_mdl_swap(struct cx18_mdl *mdl);
58
59static inline void cx18_mdl_swap(struct cx18_mdl *mdl)
60{
61 if (list_is_singular(&mdl->buf_list))
62 cx18_buf_swap(list_first_entry(&mdl->buf_list,
63 struct cx18_buffer, list));
64 else
65 _cx18_mdl_swap(mdl);
66}
67
68/* cx18_queue utility functions */
69struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
70 struct cx18_queue *q, int to_front);
71
72static inline
73struct cx18_queue *cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
74 struct cx18_queue *q)
75{
76 return _cx18_enqueue(s, mdl, q, 0); /* FIFO */
77}
78
79static inline
80struct cx18_queue *cx18_push(struct cx18_stream *s, struct cx18_mdl *mdl,
81 struct cx18_queue *q)
82{
83 return _cx18_enqueue(s, mdl, q, 1); /* LIFO */
84}
85
86void cx18_queue_init(struct cx18_queue *q);
87struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q);
88struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id,
89 u32 bytesused);
90void cx18_flush_queues(struct cx18_stream *s);
91
92/* queue MDL reconfiguration helpers */
93void cx18_unload_queues(struct cx18_stream *s);
94void cx18_load_queues(struct cx18_stream *s);
95
96/* cx18_stream utility functions */
97int cx18_stream_alloc(struct cx18_stream *s);
98void cx18_stream_free(struct cx18_stream *s);
diff --git a/drivers/media/pci/cx18/cx18-scb.c b/drivers/media/pci/cx18/cx18-scb.c
new file mode 100644
index 000000000000..85cc59637e54
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-scb.c
@@ -0,0 +1,122 @@
1/*
2 * cx18 System Control Block initialization
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#include "cx18-driver.h"
24#include "cx18-io.h"
25#include "cx18-scb.h"
26
27void cx18_init_scb(struct cx18 *cx)
28{
29 cx18_setup_page(cx, SCB_OFFSET);
30 cx18_memset_io(cx, cx->scb, 0, 0x10000);
31
32 cx18_writel(cx, IRQ_APU_TO_CPU, &cx->scb->apu2cpu_irq);
33 cx18_writel(cx, IRQ_CPU_TO_APU_ACK, &cx->scb->cpu2apu_irq_ack);
34 cx18_writel(cx, IRQ_HPU_TO_CPU, &cx->scb->hpu2cpu_irq);
35 cx18_writel(cx, IRQ_CPU_TO_HPU_ACK, &cx->scb->cpu2hpu_irq_ack);
36 cx18_writel(cx, IRQ_PPU_TO_CPU, &cx->scb->ppu2cpu_irq);
37 cx18_writel(cx, IRQ_CPU_TO_PPU_ACK, &cx->scb->cpu2ppu_irq_ack);
38 cx18_writel(cx, IRQ_EPU_TO_CPU, &cx->scb->epu2cpu_irq);
39 cx18_writel(cx, IRQ_CPU_TO_EPU_ACK, &cx->scb->cpu2epu_irq_ack);
40
41 cx18_writel(cx, IRQ_CPU_TO_APU, &cx->scb->cpu2apu_irq);
42 cx18_writel(cx, IRQ_APU_TO_CPU_ACK, &cx->scb->apu2cpu_irq_ack);
43 cx18_writel(cx, IRQ_HPU_TO_APU, &cx->scb->hpu2apu_irq);
44 cx18_writel(cx, IRQ_APU_TO_HPU_ACK, &cx->scb->apu2hpu_irq_ack);
45 cx18_writel(cx, IRQ_PPU_TO_APU, &cx->scb->ppu2apu_irq);
46 cx18_writel(cx, IRQ_APU_TO_PPU_ACK, &cx->scb->apu2ppu_irq_ack);
47 cx18_writel(cx, IRQ_EPU_TO_APU, &cx->scb->epu2apu_irq);
48 cx18_writel(cx, IRQ_APU_TO_EPU_ACK, &cx->scb->apu2epu_irq_ack);
49
50 cx18_writel(cx, IRQ_CPU_TO_HPU, &cx->scb->cpu2hpu_irq);
51 cx18_writel(cx, IRQ_HPU_TO_CPU_ACK, &cx->scb->hpu2cpu_irq_ack);
52 cx18_writel(cx, IRQ_APU_TO_HPU, &cx->scb->apu2hpu_irq);
53 cx18_writel(cx, IRQ_HPU_TO_APU_ACK, &cx->scb->hpu2apu_irq_ack);
54 cx18_writel(cx, IRQ_PPU_TO_HPU, &cx->scb->ppu2hpu_irq);
55 cx18_writel(cx, IRQ_HPU_TO_PPU_ACK, &cx->scb->hpu2ppu_irq_ack);
56 cx18_writel(cx, IRQ_EPU_TO_HPU, &cx->scb->epu2hpu_irq);
57 cx18_writel(cx, IRQ_HPU_TO_EPU_ACK, &cx->scb->hpu2epu_irq_ack);
58
59 cx18_writel(cx, IRQ_CPU_TO_PPU, &cx->scb->cpu2ppu_irq);
60 cx18_writel(cx, IRQ_PPU_TO_CPU_ACK, &cx->scb->ppu2cpu_irq_ack);
61 cx18_writel(cx, IRQ_APU_TO_PPU, &cx->scb->apu2ppu_irq);
62 cx18_writel(cx, IRQ_PPU_TO_APU_ACK, &cx->scb->ppu2apu_irq_ack);
63 cx18_writel(cx, IRQ_HPU_TO_PPU, &cx->scb->hpu2ppu_irq);
64 cx18_writel(cx, IRQ_PPU_TO_HPU_ACK, &cx->scb->ppu2hpu_irq_ack);
65 cx18_writel(cx, IRQ_EPU_TO_PPU, &cx->scb->epu2ppu_irq);
66 cx18_writel(cx, IRQ_PPU_TO_EPU_ACK, &cx->scb->ppu2epu_irq_ack);
67
68 cx18_writel(cx, IRQ_CPU_TO_EPU, &cx->scb->cpu2epu_irq);
69 cx18_writel(cx, IRQ_EPU_TO_CPU_ACK, &cx->scb->epu2cpu_irq_ack);
70 cx18_writel(cx, IRQ_APU_TO_EPU, &cx->scb->apu2epu_irq);
71 cx18_writel(cx, IRQ_EPU_TO_APU_ACK, &cx->scb->epu2apu_irq_ack);
72 cx18_writel(cx, IRQ_HPU_TO_EPU, &cx->scb->hpu2epu_irq);
73 cx18_writel(cx, IRQ_EPU_TO_HPU_ACK, &cx->scb->epu2hpu_irq_ack);
74 cx18_writel(cx, IRQ_PPU_TO_EPU, &cx->scb->ppu2epu_irq);
75 cx18_writel(cx, IRQ_EPU_TO_PPU_ACK, &cx->scb->epu2ppu_irq_ack);
76
77 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, apu2cpu_mb),
78 &cx->scb->apu2cpu_mb_offset);
79 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, hpu2cpu_mb),
80 &cx->scb->hpu2cpu_mb_offset);
81 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, ppu2cpu_mb),
82 &cx->scb->ppu2cpu_mb_offset);
83 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, epu2cpu_mb),
84 &cx->scb->epu2cpu_mb_offset);
85 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, cpu2apu_mb),
86 &cx->scb->cpu2apu_mb_offset);
87 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, hpu2apu_mb),
88 &cx->scb->hpu2apu_mb_offset);
89 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, ppu2apu_mb),
90 &cx->scb->ppu2apu_mb_offset);
91 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, epu2apu_mb),
92 &cx->scb->epu2apu_mb_offset);
93 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, cpu2hpu_mb),
94 &cx->scb->cpu2hpu_mb_offset);
95 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, apu2hpu_mb),
96 &cx->scb->apu2hpu_mb_offset);
97 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, ppu2hpu_mb),
98 &cx->scb->ppu2hpu_mb_offset);
99 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, epu2hpu_mb),
100 &cx->scb->epu2hpu_mb_offset);
101 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, cpu2ppu_mb),
102 &cx->scb->cpu2ppu_mb_offset);
103 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, apu2ppu_mb),
104 &cx->scb->apu2ppu_mb_offset);
105 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, hpu2ppu_mb),
106 &cx->scb->hpu2ppu_mb_offset);
107 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, epu2ppu_mb),
108 &cx->scb->epu2ppu_mb_offset);
109 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, cpu2epu_mb),
110 &cx->scb->cpu2epu_mb_offset);
111 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, apu2epu_mb),
112 &cx->scb->apu2epu_mb_offset);
113 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, hpu2epu_mb),
114 &cx->scb->hpu2epu_mb_offset);
115 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, ppu2epu_mb),
116 &cx->scb->ppu2epu_mb_offset);
117
118 cx18_writel(cx, SCB_OFFSET + offsetof(struct cx18_scb, cpu_state),
119 &cx->scb->ipc_offset);
120
121 cx18_writel(cx, 1, &cx->scb->epu_state);
122}
diff --git a/drivers/media/pci/cx18/cx18-scb.h b/drivers/media/pci/cx18/cx18-scb.h
new file mode 100644
index 000000000000..08877652e321
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-scb.h
@@ -0,0 +1,280 @@
1/*
2 * cx18 System Control Block initialization
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 */
22
23#ifndef CX18_SCB_H
24#define CX18_SCB_H
25
26#include "cx18-mailbox.h"
27
28/* NOTE: All ACK interrupts are in the SW2 register. All non-ACK interrupts
29 are in the SW1 register. */
30
31#define IRQ_APU_TO_CPU 0x00000001
32#define IRQ_CPU_TO_APU_ACK 0x00000001
33#define IRQ_HPU_TO_CPU 0x00000002
34#define IRQ_CPU_TO_HPU_ACK 0x00000002
35#define IRQ_PPU_TO_CPU 0x00000004
36#define IRQ_CPU_TO_PPU_ACK 0x00000004
37#define IRQ_EPU_TO_CPU 0x00000008
38#define IRQ_CPU_TO_EPU_ACK 0x00000008
39
40#define IRQ_CPU_TO_APU 0x00000010
41#define IRQ_APU_TO_CPU_ACK 0x00000010
42#define IRQ_HPU_TO_APU 0x00000020
43#define IRQ_APU_TO_HPU_ACK 0x00000020
44#define IRQ_PPU_TO_APU 0x00000040
45#define IRQ_APU_TO_PPU_ACK 0x00000040
46#define IRQ_EPU_TO_APU 0x00000080
47#define IRQ_APU_TO_EPU_ACK 0x00000080
48
49#define IRQ_CPU_TO_HPU 0x00000100
50#define IRQ_HPU_TO_CPU_ACK 0x00000100
51#define IRQ_APU_TO_HPU 0x00000200
52#define IRQ_HPU_TO_APU_ACK 0x00000200
53#define IRQ_PPU_TO_HPU 0x00000400
54#define IRQ_HPU_TO_PPU_ACK 0x00000400
55#define IRQ_EPU_TO_HPU 0x00000800
56#define IRQ_HPU_TO_EPU_ACK 0x00000800
57
58#define IRQ_CPU_TO_PPU 0x00001000
59#define IRQ_PPU_TO_CPU_ACK 0x00001000
60#define IRQ_APU_TO_PPU 0x00002000
61#define IRQ_PPU_TO_APU_ACK 0x00002000
62#define IRQ_HPU_TO_PPU 0x00004000
63#define IRQ_PPU_TO_HPU_ACK 0x00004000
64#define IRQ_EPU_TO_PPU 0x00008000
65#define IRQ_PPU_TO_EPU_ACK 0x00008000
66
67#define IRQ_CPU_TO_EPU 0x00010000
68#define IRQ_EPU_TO_CPU_ACK 0x00010000
69#define IRQ_APU_TO_EPU 0x00020000
70#define IRQ_EPU_TO_APU_ACK 0x00020000
71#define IRQ_HPU_TO_EPU 0x00040000
72#define IRQ_EPU_TO_HPU_ACK 0x00040000
73#define IRQ_PPU_TO_EPU 0x00080000
74#define IRQ_EPU_TO_PPU_ACK 0x00080000
75
76#define SCB_OFFSET 0xDC0000
77
78/* If Firmware uses fixed memory map, it shall not allocate the area
79 between SCB_OFFSET and SCB_OFFSET+SCB_RESERVED_SIZE-1 inclusive */
80#define SCB_RESERVED_SIZE 0x10000
81
82
83/* This structure is used by EPU to provide memory descriptors in its memory */
84struct cx18_mdl_ent {
85 u32 paddr; /* Physical address of a buffer segment */
86 u32 length; /* Length of the buffer segment */
87};
88
89struct cx18_scb {
90 /* These fields form the System Control Block which is used at boot time
91 for localizing the IPC data as well as the code positions for all
92 processors. The offsets are from the start of this struct. */
93
94 /* Offset where to find the Inter-Processor Communication data */
95 u32 ipc_offset;
96 u32 reserved01[7];
97 /* Offset where to find the start of the CPU code */
98 u32 cpu_code_offset;
99 u32 reserved02[3];
100 /* Offset where to find the start of the APU code */
101 u32 apu_code_offset;
102 u32 reserved03[3];
103 /* Offset where to find the start of the HPU code */
104 u32 hpu_code_offset;
105 u32 reserved04[3];
106 /* Offset where to find the start of the PPU code */
107 u32 ppu_code_offset;
108 u32 reserved05[3];
109
110 /* These fields form Inter-Processor Communication data which is used
111 by all processors to locate the information needed for communicating
112 with other processors */
113
114 /* Fields for CPU: */
115
116 /* bit 0: 1/0 processor ready/not ready. Set other bits to 0. */
117 u32 cpu_state;
118 u32 reserved1[7];
119 /* Offset to the mailbox used for sending commands from APU to CPU */
120 u32 apu2cpu_mb_offset;
121 /* Value to write to register SW1 register set (0xC7003100) after the
122 command is ready */
123 u32 apu2cpu_irq;
124 /* Value to write to register SW2 register set (0xC7003140) after the
125 command is cleared */
126 u32 cpu2apu_irq_ack;
127 u32 reserved2[13];
128
129 u32 hpu2cpu_mb_offset;
130 u32 hpu2cpu_irq;
131 u32 cpu2hpu_irq_ack;
132 u32 reserved3[13];
133
134 u32 ppu2cpu_mb_offset;
135 u32 ppu2cpu_irq;
136 u32 cpu2ppu_irq_ack;
137 u32 reserved4[13];
138
139 u32 epu2cpu_mb_offset;
140 u32 epu2cpu_irq;
141 u32 cpu2epu_irq_ack;
142 u32 reserved5[13];
143 u32 reserved6[8];
144
145 /* Fields for APU: */
146
147 u32 apu_state;
148 u32 reserved11[7];
149 u32 cpu2apu_mb_offset;
150 u32 cpu2apu_irq;
151 u32 apu2cpu_irq_ack;
152 u32 reserved12[13];
153
154 u32 hpu2apu_mb_offset;
155 u32 hpu2apu_irq;
156 u32 apu2hpu_irq_ack;
157 u32 reserved13[13];
158
159 u32 ppu2apu_mb_offset;
160 u32 ppu2apu_irq;
161 u32 apu2ppu_irq_ack;
162 u32 reserved14[13];
163
164 u32 epu2apu_mb_offset;
165 u32 epu2apu_irq;
166 u32 apu2epu_irq_ack;
167 u32 reserved15[13];
168 u32 reserved16[8];
169
170 /* Fields for HPU: */
171
172 u32 hpu_state;
173 u32 reserved21[7];
174 u32 cpu2hpu_mb_offset;
175 u32 cpu2hpu_irq;
176 u32 hpu2cpu_irq_ack;
177 u32 reserved22[13];
178
179 u32 apu2hpu_mb_offset;
180 u32 apu2hpu_irq;
181 u32 hpu2apu_irq_ack;
182 u32 reserved23[13];
183
184 u32 ppu2hpu_mb_offset;
185 u32 ppu2hpu_irq;
186 u32 hpu2ppu_irq_ack;
187 u32 reserved24[13];
188
189 u32 epu2hpu_mb_offset;
190 u32 epu2hpu_irq;
191 u32 hpu2epu_irq_ack;
192 u32 reserved25[13];
193 u32 reserved26[8];
194
195 /* Fields for PPU: */
196
197 u32 ppu_state;
198 u32 reserved31[7];
199 u32 cpu2ppu_mb_offset;
200 u32 cpu2ppu_irq;
201 u32 ppu2cpu_irq_ack;
202 u32 reserved32[13];
203
204 u32 apu2ppu_mb_offset;
205 u32 apu2ppu_irq;
206 u32 ppu2apu_irq_ack;
207 u32 reserved33[13];
208
209 u32 hpu2ppu_mb_offset;
210 u32 hpu2ppu_irq;
211 u32 ppu2hpu_irq_ack;
212 u32 reserved34[13];
213
214 u32 epu2ppu_mb_offset;
215 u32 epu2ppu_irq;
216 u32 ppu2epu_irq_ack;
217 u32 reserved35[13];
218 u32 reserved36[8];
219
220 /* Fields for EPU: */
221
222 u32 epu_state;
223 u32 reserved41[7];
224 u32 cpu2epu_mb_offset;
225 u32 cpu2epu_irq;
226 u32 epu2cpu_irq_ack;
227 u32 reserved42[13];
228
229 u32 apu2epu_mb_offset;
230 u32 apu2epu_irq;
231 u32 epu2apu_irq_ack;
232 u32 reserved43[13];
233
234 u32 hpu2epu_mb_offset;
235 u32 hpu2epu_irq;
236 u32 epu2hpu_irq_ack;
237 u32 reserved44[13];
238
239 u32 ppu2epu_mb_offset;
240 u32 ppu2epu_irq;
241 u32 epu2ppu_irq_ack;
242 u32 reserved45[13];
243 u32 reserved46[8];
244
245 u32 semaphores[8]; /* Semaphores */
246
247 u32 reserved50[32]; /* Reserved for future use */
248
249 struct cx18_mailbox apu2cpu_mb;
250 struct cx18_mailbox hpu2cpu_mb;
251 struct cx18_mailbox ppu2cpu_mb;
252 struct cx18_mailbox epu2cpu_mb;
253
254 struct cx18_mailbox cpu2apu_mb;
255 struct cx18_mailbox hpu2apu_mb;
256 struct cx18_mailbox ppu2apu_mb;
257 struct cx18_mailbox epu2apu_mb;
258
259 struct cx18_mailbox cpu2hpu_mb;
260 struct cx18_mailbox apu2hpu_mb;
261 struct cx18_mailbox ppu2hpu_mb;
262 struct cx18_mailbox epu2hpu_mb;
263
264 struct cx18_mailbox cpu2ppu_mb;
265 struct cx18_mailbox apu2ppu_mb;
266 struct cx18_mailbox hpu2ppu_mb;
267 struct cx18_mailbox epu2ppu_mb;
268
269 struct cx18_mailbox cpu2epu_mb;
270 struct cx18_mailbox apu2epu_mb;
271 struct cx18_mailbox hpu2epu_mb;
272 struct cx18_mailbox ppu2epu_mb;
273
274 struct cx18_mdl_ack cpu_mdl_ack[CX18_MAX_STREAMS][CX18_MAX_MDL_ACKS];
275 struct cx18_mdl_ent cpu_mdl[1];
276};
277
278void cx18_init_scb(struct cx18 *cx);
279
280#endif
diff --git a/drivers/media/pci/cx18/cx18-streams.c b/drivers/media/pci/cx18/cx18-streams.c
new file mode 100644
index 000000000000..9d598ab88615
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-streams.c
@@ -0,0 +1,1060 @@
1/*
2 * cx18 init/start/stop/exit stream functions
3 *
4 * Derived from ivtv-streams.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-io.h"
27#include "cx18-fileops.h"
28#include "cx18-mailbox.h"
29#include "cx18-i2c.h"
30#include "cx18-queue.h"
31#include "cx18-ioctl.h"
32#include "cx18-streams.h"
33#include "cx18-cards.h"
34#include "cx18-scb.h"
35#include "cx18-dvb.h"
36
37#define CX18_DSP0_INTERRUPT_MASK 0xd0004C
38
39static struct v4l2_file_operations cx18_v4l2_enc_fops = {
40 .owner = THIS_MODULE,
41 .read = cx18_v4l2_read,
42 .open = cx18_v4l2_open,
43 .unlocked_ioctl = video_ioctl2,
44 .release = cx18_v4l2_close,
45 .poll = cx18_v4l2_enc_poll,
46 .mmap = cx18_v4l2_mmap,
47};
48
49/* offset from 0 to register ts v4l2 minors on */
50#define CX18_V4L2_ENC_TS_OFFSET 16
51/* offset from 0 to register pcm v4l2 minors on */
52#define CX18_V4L2_ENC_PCM_OFFSET 24
53/* offset from 0 to register yuv v4l2 minors on */
54#define CX18_V4L2_ENC_YUV_OFFSET 32
55
56static struct {
57 const char *name;
58 int vfl_type;
59 int num_offset;
60 int dma;
61 enum v4l2_buf_type buf_type;
62} cx18_stream_info[] = {
63 { /* CX18_ENC_STREAM_TYPE_MPG */
64 "encoder MPEG",
65 VFL_TYPE_GRABBER, 0,
66 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
67 },
68 { /* CX18_ENC_STREAM_TYPE_TS */
69 "TS",
70 VFL_TYPE_GRABBER, -1,
71 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
72 },
73 { /* CX18_ENC_STREAM_TYPE_YUV */
74 "encoder YUV",
75 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
76 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
77 },
78 { /* CX18_ENC_STREAM_TYPE_VBI */
79 "encoder VBI",
80 VFL_TYPE_VBI, 0,
81 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
82 },
83 { /* CX18_ENC_STREAM_TYPE_PCM */
84 "encoder PCM audio",
85 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
86 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
87 },
88 { /* CX18_ENC_STREAM_TYPE_IDX */
89 "encoder IDX",
90 VFL_TYPE_GRABBER, -1,
91 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
92 },
93 { /* CX18_ENC_STREAM_TYPE_RAD */
94 "encoder radio",
95 VFL_TYPE_RADIO, 0,
96 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
97 },
98};
99
100
101void cx18_dma_free(struct videobuf_queue *q,
102 struct cx18_stream *s, struct cx18_videobuf_buffer *buf)
103{
104 videobuf_waiton(q, &buf->vb, 0, 0);
105 videobuf_vmalloc_free(&buf->vb);
106 buf->vb.state = VIDEOBUF_NEEDS_INIT;
107}
108
109static int cx18_prepare_buffer(struct videobuf_queue *q,
110 struct cx18_stream *s,
111 struct cx18_videobuf_buffer *buf,
112 u32 pixelformat,
113 unsigned int width, unsigned int height,
114 enum v4l2_field field)
115{
116 struct cx18 *cx = s->cx;
117 int rc = 0;
118
119 /* check settings */
120 buf->bytes_used = 0;
121
122 if ((width < 48) || (height < 32))
123 return -EINVAL;
124
125 buf->vb.size = (width * height * 2);
126 if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
127 return -EINVAL;
128
129 /* alloc + fill struct (if changed) */
130 if (buf->vb.width != width || buf->vb.height != height ||
131 buf->vb.field != field || s->pixelformat != pixelformat ||
132 buf->tvnorm != cx->std) {
133
134 buf->vb.width = width;
135 buf->vb.height = height;
136 buf->vb.field = field;
137 buf->tvnorm = cx->std;
138 s->pixelformat = pixelformat;
139
140 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
141 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
142 if (s->pixelformat == V4L2_PIX_FMT_HM12)
143 s->vb_bytes_per_frame = height * 720 * 3 / 2;
144 else
145 s->vb_bytes_per_frame = height * 720 * 2;
146 cx18_dma_free(q, s, buf);
147 }
148
149 if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
150 return -EINVAL;
151
152 if (buf->vb.field == 0)
153 buf->vb.field = V4L2_FIELD_INTERLACED;
154
155 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
156 buf->vb.width = width;
157 buf->vb.height = height;
158 buf->vb.field = field;
159 buf->tvnorm = cx->std;
160 s->pixelformat = pixelformat;
161
162 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
163 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
164 if (s->pixelformat == V4L2_PIX_FMT_HM12)
165 s->vb_bytes_per_frame = height * 720 * 3 / 2;
166 else
167 s->vb_bytes_per_frame = height * 720 * 2;
168 rc = videobuf_iolock(q, &buf->vb, NULL);
169 if (rc != 0)
170 goto fail;
171 }
172 buf->vb.state = VIDEOBUF_PREPARED;
173 return 0;
174
175fail:
176 cx18_dma_free(q, s, buf);
177 return rc;
178
179}
180
181/* VB_MIN_BUFSIZE is lcm(1440 * 480, 1440 * 576)
182 1440 is a single line of 4:2:2 YUV at 720 luma samples wide
183*/
184#define VB_MIN_BUFFERS 32
185#define VB_MIN_BUFSIZE 4147200
186
187static int buffer_setup(struct videobuf_queue *q,
188 unsigned int *count, unsigned int *size)
189{
190 struct cx18_stream *s = q->priv_data;
191 struct cx18 *cx = s->cx;
192
193 *size = 2 * cx->cxhdl.width * cx->cxhdl.height;
194 if (*count == 0)
195 *count = VB_MIN_BUFFERS;
196
197 while (*size * *count > VB_MIN_BUFFERS * VB_MIN_BUFSIZE)
198 (*count)--;
199
200 q->field = V4L2_FIELD_INTERLACED;
201 q->last = V4L2_FIELD_INTERLACED;
202
203 return 0;
204}
205
206static int buffer_prepare(struct videobuf_queue *q,
207 struct videobuf_buffer *vb,
208 enum v4l2_field field)
209{
210 struct cx18_videobuf_buffer *buf =
211 container_of(vb, struct cx18_videobuf_buffer, vb);
212 struct cx18_stream *s = q->priv_data;
213 struct cx18 *cx = s->cx;
214
215 return cx18_prepare_buffer(q, s, buf, s->pixelformat,
216 cx->cxhdl.width, cx->cxhdl.height, field);
217}
218
219static void buffer_release(struct videobuf_queue *q,
220 struct videobuf_buffer *vb)
221{
222 struct cx18_videobuf_buffer *buf =
223 container_of(vb, struct cx18_videobuf_buffer, vb);
224 struct cx18_stream *s = q->priv_data;
225
226 cx18_dma_free(q, s, buf);
227}
228
229static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
230{
231 struct cx18_videobuf_buffer *buf =
232 container_of(vb, struct cx18_videobuf_buffer, vb);
233 struct cx18_stream *s = q->priv_data;
234
235 buf->vb.state = VIDEOBUF_QUEUED;
236
237 list_add_tail(&buf->vb.queue, &s->vb_capture);
238}
239
240static struct videobuf_queue_ops cx18_videobuf_qops = {
241 .buf_setup = buffer_setup,
242 .buf_prepare = buffer_prepare,
243 .buf_queue = buffer_queue,
244 .buf_release = buffer_release,
245};
246
247static void cx18_stream_init(struct cx18 *cx, int type)
248{
249 struct cx18_stream *s = &cx->streams[type];
250 struct video_device *video_dev = s->video_dev;
251
252 /* we need to keep video_dev, so restore it afterwards */
253 memset(s, 0, sizeof(*s));
254 s->video_dev = video_dev;
255
256 /* initialize cx18_stream fields */
257 s->dvb = NULL;
258 s->cx = cx;
259 s->type = type;
260 s->name = cx18_stream_info[type].name;
261 s->handle = CX18_INVALID_TASK_HANDLE;
262
263 s->dma = cx18_stream_info[type].dma;
264 s->buffers = cx->stream_buffers[type];
265 s->buf_size = cx->stream_buf_size[type];
266 INIT_LIST_HEAD(&s->buf_pool);
267 s->bufs_per_mdl = 1;
268 s->mdl_size = s->buf_size * s->bufs_per_mdl;
269
270 init_waitqueue_head(&s->waitq);
271 s->id = -1;
272 spin_lock_init(&s->q_free.lock);
273 cx18_queue_init(&s->q_free);
274 spin_lock_init(&s->q_busy.lock);
275 cx18_queue_init(&s->q_busy);
276 spin_lock_init(&s->q_full.lock);
277 cx18_queue_init(&s->q_full);
278 spin_lock_init(&s->q_idle.lock);
279 cx18_queue_init(&s->q_idle);
280
281 INIT_WORK(&s->out_work_order, cx18_out_work_handler);
282
283 INIT_LIST_HEAD(&s->vb_capture);
284 s->vb_timeout.function = cx18_vb_timeout;
285 s->vb_timeout.data = (unsigned long)s;
286 init_timer(&s->vb_timeout);
287 spin_lock_init(&s->vb_lock);
288 if (type == CX18_ENC_STREAM_TYPE_YUV) {
289 spin_lock_init(&s->vbuf_q_lock);
290
291 s->vb_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
292 videobuf_queue_vmalloc_init(&s->vbuf_q, &cx18_videobuf_qops,
293 &cx->pci_dev->dev, &s->vbuf_q_lock,
294 V4L2_BUF_TYPE_VIDEO_CAPTURE,
295 V4L2_FIELD_INTERLACED,
296 sizeof(struct cx18_videobuf_buffer),
297 s, &cx->serialize_lock);
298
299 /* Assume the previous pixel default */
300 s->pixelformat = V4L2_PIX_FMT_HM12;
301 s->vb_bytes_per_frame = cx->cxhdl.height * 720 * 3 / 2;
302 }
303}
304
305static int cx18_prep_dev(struct cx18 *cx, int type)
306{
307 struct cx18_stream *s = &cx->streams[type];
308 u32 cap = cx->v4l2_cap;
309 int num_offset = cx18_stream_info[type].num_offset;
310 int num = cx->instance + cx18_first_minor + num_offset;
311
312 /*
313 * These five fields are always initialized.
314 * For analog capture related streams, if video_dev == NULL then the
315 * stream is not in use.
316 * For the TS stream, if dvb == NULL then the stream is not in use.
317 * In those cases no other fields but these four can be used.
318 */
319 s->video_dev = NULL;
320 s->dvb = NULL;
321 s->cx = cx;
322 s->type = type;
323 s->name = cx18_stream_info[type].name;
324
325 /* Check whether the radio is supported */
326 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
327 return 0;
328
329 /* Check whether VBI is supported */
330 if (type == CX18_ENC_STREAM_TYPE_VBI &&
331 !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
332 return 0;
333
334 /* User explicitly selected 0 buffers for these streams, so don't
335 create them. */
336 if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
337 cx->stream_buffers[type] == 0) {
338 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
339 return 0;
340 }
341
342 cx18_stream_init(cx, type);
343
344 /* Allocate the cx18_dvb struct only for the TS on cards with DTV */
345 if (type == CX18_ENC_STREAM_TYPE_TS) {
346 if (cx->card->hw_all & CX18_HW_DVB) {
347 s->dvb = kzalloc(sizeof(struct cx18_dvb), GFP_KERNEL);
348 if (s->dvb == NULL) {
349 CX18_ERR("Couldn't allocate cx18_dvb structure"
350 " for %s\n", s->name);
351 return -ENOMEM;
352 }
353 } else {
354 /* Don't need buffers for the TS, if there is no DVB */
355 s->buffers = 0;
356 }
357 }
358
359 if (num_offset == -1)
360 return 0;
361
362 /* allocate and initialize the v4l2 video device structure */
363 s->video_dev = video_device_alloc();
364 if (s->video_dev == NULL) {
365 CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
366 s->name);
367 return -ENOMEM;
368 }
369
370 snprintf(s->video_dev->name, sizeof(s->video_dev->name), "%s %s",
371 cx->v4l2_dev.name, s->name);
372
373 s->video_dev->num = num;
374 s->video_dev->v4l2_dev = &cx->v4l2_dev;
375 s->video_dev->fops = &cx18_v4l2_enc_fops;
376 s->video_dev->release = video_device_release;
377 s->video_dev->tvnorms = V4L2_STD_ALL;
378 s->video_dev->lock = &cx->serialize_lock;
379 set_bit(V4L2_FL_USE_FH_PRIO, &s->video_dev->flags);
380 cx18_set_funcs(s->video_dev);
381 return 0;
382}
383
384/* Initialize v4l2 variables and register v4l2 devices */
385int cx18_streams_setup(struct cx18 *cx)
386{
387 int type, ret;
388
389 /* Setup V4L2 Devices */
390 for (type = 0; type < CX18_MAX_STREAMS; type++) {
391 /* Prepare device */
392 ret = cx18_prep_dev(cx, type);
393 if (ret < 0)
394 break;
395
396 /* Allocate Stream */
397 ret = cx18_stream_alloc(&cx->streams[type]);
398 if (ret < 0)
399 break;
400 }
401 if (type == CX18_MAX_STREAMS)
402 return 0;
403
404 /* One or more streams could not be initialized. Clean 'em all up. */
405 cx18_streams_cleanup(cx, 0);
406 return ret;
407}
408
409static int cx18_reg_dev(struct cx18 *cx, int type)
410{
411 struct cx18_stream *s = &cx->streams[type];
412 int vfl_type = cx18_stream_info[type].vfl_type;
413 const char *name;
414 int num, ret;
415
416 if (type == CX18_ENC_STREAM_TYPE_TS && s->dvb != NULL) {
417 ret = cx18_dvb_register(s);
418 if (ret < 0) {
419 CX18_ERR("DVB failed to register\n");
420 return ret;
421 }
422 }
423
424 if (s->video_dev == NULL)
425 return 0;
426
427 num = s->video_dev->num;
428 /* card number + user defined offset + device offset */
429 if (type != CX18_ENC_STREAM_TYPE_MPG) {
430 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
431
432 if (s_mpg->video_dev)
433 num = s_mpg->video_dev->num
434 + cx18_stream_info[type].num_offset;
435 }
436 video_set_drvdata(s->video_dev, s);
437
438 /* Register device. First try the desired minor, then any free one. */
439 ret = video_register_device_no_warn(s->video_dev, vfl_type, num);
440 if (ret < 0) {
441 CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
442 s->name, num);
443 video_device_release(s->video_dev);
444 s->video_dev = NULL;
445 return ret;
446 }
447
448 name = video_device_node_name(s->video_dev);
449
450 switch (vfl_type) {
451 case VFL_TYPE_GRABBER:
452 CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n",
453 name, s->name, cx->stream_buffers[type],
454 cx->stream_buf_size[type] / 1024,
455 (cx->stream_buf_size[type] * 100 / 1024) % 100);
456 break;
457
458 case VFL_TYPE_RADIO:
459 CX18_INFO("Registered device %s for %s\n", name, s->name);
460 break;
461
462 case VFL_TYPE_VBI:
463 if (cx->stream_buffers[type])
464 CX18_INFO("Registered device %s for %s "
465 "(%d x %d bytes)\n",
466 name, s->name, cx->stream_buffers[type],
467 cx->stream_buf_size[type]);
468 else
469 CX18_INFO("Registered device %s for %s\n",
470 name, s->name);
471 break;
472 }
473
474 return 0;
475}
476
477/* Register v4l2 devices */
478int cx18_streams_register(struct cx18 *cx)
479{
480 int type;
481 int err;
482 int ret = 0;
483
484 /* Register V4L2 devices */
485 for (type = 0; type < CX18_MAX_STREAMS; type++) {
486 err = cx18_reg_dev(cx, type);
487 if (err && ret == 0)
488 ret = err;
489 }
490
491 if (ret == 0)
492 return 0;
493
494 /* One or more streams could not be initialized. Clean 'em all up. */
495 cx18_streams_cleanup(cx, 1);
496 return ret;
497}
498
499/* Unregister v4l2 devices */
500void cx18_streams_cleanup(struct cx18 *cx, int unregister)
501{
502 struct video_device *vdev;
503 int type;
504
505 /* Teardown all streams */
506 for (type = 0; type < CX18_MAX_STREAMS; type++) {
507
508 /* The TS has a cx18_dvb structure, not a video_device */
509 if (type == CX18_ENC_STREAM_TYPE_TS) {
510 if (cx->streams[type].dvb != NULL) {
511 if (unregister)
512 cx18_dvb_unregister(&cx->streams[type]);
513 kfree(cx->streams[type].dvb);
514 cx->streams[type].dvb = NULL;
515 cx18_stream_free(&cx->streams[type]);
516 }
517 continue;
518 }
519
520 /* No struct video_device, but can have buffers allocated */
521 if (type == CX18_ENC_STREAM_TYPE_IDX) {
522 /* If the module params didn't inhibit IDX ... */
523 if (cx->stream_buffers[type] != 0) {
524 cx->stream_buffers[type] = 0;
525 /*
526 * Before calling cx18_stream_free(),
527 * check if the IDX stream was actually set up.
528 * Needed, since the cx18_probe() error path
529 * exits through here as well as normal clean up
530 */
531 if (cx->streams[type].buffers != 0)
532 cx18_stream_free(&cx->streams[type]);
533 }
534 continue;
535 }
536
537 /* If struct video_device exists, can have buffers allocated */
538 vdev = cx->streams[type].video_dev;
539
540 cx->streams[type].video_dev = NULL;
541 if (vdev == NULL)
542 continue;
543
544 if (type == CX18_ENC_STREAM_TYPE_YUV)
545 videobuf_mmap_free(&cx->streams[type].vbuf_q);
546
547 cx18_stream_free(&cx->streams[type]);
548
549 /* Unregister or release device */
550 if (unregister)
551 video_unregister_device(vdev);
552 else
553 video_device_release(vdev);
554 }
555}
556
557static void cx18_vbi_setup(struct cx18_stream *s)
558{
559 struct cx18 *cx = s->cx;
560 int raw = cx18_raw_vbi(cx);
561 u32 data[CX2341X_MBOX_MAX_DATA];
562 int lines;
563
564 if (cx->is_60hz) {
565 cx->vbi.count = 12;
566 cx->vbi.start[0] = 10;
567 cx->vbi.start[1] = 273;
568 } else { /* PAL/SECAM */
569 cx->vbi.count = 18;
570 cx->vbi.start[0] = 6;
571 cx->vbi.start[1] = 318;
572 }
573
574 /* setup VBI registers */
575 if (raw)
576 v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &cx->vbi.in.fmt.vbi);
577 else
578 v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &cx->vbi.in.fmt.sliced);
579
580 /*
581 * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
582 * VBI when the first analog capture channel starts, as once it starts
583 * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
584 * (i.e. for the VBI capture channels). We also send it for each
585 * analog capture channel anyway just to make sure we get the proper
586 * behavior
587 */
588 if (raw) {
589 lines = cx->vbi.count * 2;
590 } else {
591 /*
592 * For 525/60 systems, according to the VIP 2 & BT.656 std:
593 * The EAV RP code's Field bit toggles on line 4, a few lines
594 * after the Vertcal Blank bit has already toggled.
595 * Tell the encoder to capture 21-4+1=18 lines per field,
596 * since we want lines 10 through 21.
597 *
598 * For 625/50 systems, according to the VIP 2 & BT.656 std:
599 * The EAV RP code's Field bit toggles on line 1, a few lines
600 * after the Vertcal Blank bit has already toggled.
601 * (We've actually set the digitizer so that the Field bit
602 * toggles on line 2.) Tell the encoder to capture 23-2+1=22
603 * lines per field, since we want lines 6 through 23.
604 */
605 lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
606 }
607
608 data[0] = s->handle;
609 /* Lines per field */
610 data[1] = (lines / 2) | ((lines / 2) << 16);
611 /* bytes per line */
612 data[2] = (raw ? vbi_active_samples
613 : (cx->is_60hz ? vbi_hblank_samples_60Hz
614 : vbi_hblank_samples_50Hz));
615 /* Every X number of frames a VBI interrupt arrives
616 (frames as in 25 or 30 fps) */
617 data[3] = 1;
618 /*
619 * Set the SAV/EAV RP codes to look for as start/stop points
620 * when in VIP-1.1 mode
621 */
622 if (raw) {
623 /*
624 * Start codes for beginning of "active" line in vertical blank
625 * 0x20 ( VerticalBlank )
626 * 0x60 ( EvenField VerticalBlank )
627 */
628 data[4] = 0x20602060;
629 /*
630 * End codes for end of "active" raw lines and regular lines
631 * 0x30 ( VerticalBlank HorizontalBlank)
632 * 0x70 ( EvenField VerticalBlank HorizontalBlank)
633 * 0x90 (Task HorizontalBlank)
634 * 0xd0 (Task EvenField HorizontalBlank)
635 */
636 data[5] = 0x307090d0;
637 } else {
638 /*
639 * End codes for active video, we want data in the hblank region
640 * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
641 * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
642 *
643 * Since the V bit is only allowed to toggle in the EAV RP code,
644 * just before the first active region line, these two
645 * are problematic:
646 * 0x90 (Task HorizontalBlank)
647 * 0xd0 (Task EvenField HorizontalBlank)
648 *
649 * We have set the digitzer such that we don't have to worry
650 * about these problem codes.
651 */
652 data[4] = 0xB0F0B0F0;
653 /*
654 * Start codes for beginning of active line in vertical blank
655 * 0xa0 (Task VerticalBlank )
656 * 0xe0 (Task EvenField VerticalBlank )
657 */
658 data[5] = 0xA0E0A0E0;
659 }
660
661 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
662 data[0], data[1], data[2], data[3], data[4], data[5]);
663
664 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
665}
666
667void cx18_stream_rotate_idx_mdls(struct cx18 *cx)
668{
669 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
670 struct cx18_mdl *mdl;
671
672 if (!cx18_stream_enabled(s))
673 return;
674
675 /* Return if the firmware is not running low on MDLs */
676 if ((atomic_read(&s->q_free.depth) + atomic_read(&s->q_busy.depth)) >=
677 CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN)
678 return;
679
680 /* Return if there are no MDLs to rotate back to the firmware */
681 if (atomic_read(&s->q_full.depth) < 2)
682 return;
683
684 /*
685 * Take the oldest IDX MDL still holding data, and discard its index
686 * entries by scheduling the MDL to go back to the firmware
687 */
688 mdl = cx18_dequeue(s, &s->q_full);
689 if (mdl != NULL)
690 cx18_enqueue(s, mdl, &s->q_free);
691}
692
693static
694struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s,
695 struct cx18_mdl *mdl)
696{
697 struct cx18 *cx = s->cx;
698 struct cx18_queue *q;
699
700 /* Don't give it to the firmware, if we're not running a capture */
701 if (s->handle == CX18_INVALID_TASK_HANDLE ||
702 test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
703 !test_bit(CX18_F_S_STREAMING, &s->s_flags))
704 return cx18_enqueue(s, mdl, &s->q_free);
705
706 q = cx18_enqueue(s, mdl, &s->q_busy);
707 if (q != &s->q_busy)
708 return q; /* The firmware has the max MDLs it can handle */
709
710 cx18_mdl_sync_for_device(s, mdl);
711 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
712 (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
713 s->bufs_per_mdl, mdl->id, s->mdl_size);
714 return q;
715}
716
717static
718void _cx18_stream_load_fw_queue(struct cx18_stream *s)
719{
720 struct cx18_queue *q;
721 struct cx18_mdl *mdl;
722
723 if (atomic_read(&s->q_free.depth) == 0 ||
724 atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
725 return;
726
727 /* Move from q_free to q_busy notifying the firmware, until the limit */
728 do {
729 mdl = cx18_dequeue(s, &s->q_free);
730 if (mdl == NULL)
731 break;
732 q = _cx18_stream_put_mdl_fw(s, mdl);
733 } while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM
734 && q == &s->q_busy);
735}
736
737void cx18_out_work_handler(struct work_struct *work)
738{
739 struct cx18_stream *s =
740 container_of(work, struct cx18_stream, out_work_order);
741
742 _cx18_stream_load_fw_queue(s);
743}
744
745static void cx18_stream_configure_mdls(struct cx18_stream *s)
746{
747 cx18_unload_queues(s);
748
749 switch (s->type) {
750 case CX18_ENC_STREAM_TYPE_YUV:
751 /*
752 * Height should be a multiple of 32 lines.
753 * Set the MDL size to the exact size needed for one frame.
754 * Use enough buffers per MDL to cover the MDL size
755 */
756 if (s->pixelformat == V4L2_PIX_FMT_HM12)
757 s->mdl_size = 720 * s->cx->cxhdl.height * 3 / 2;
758 else
759 s->mdl_size = 720 * s->cx->cxhdl.height * 2;
760 s->bufs_per_mdl = s->mdl_size / s->buf_size;
761 if (s->mdl_size % s->buf_size)
762 s->bufs_per_mdl++;
763 break;
764 case CX18_ENC_STREAM_TYPE_VBI:
765 s->bufs_per_mdl = 1;
766 if (cx18_raw_vbi(s->cx)) {
767 s->mdl_size = (s->cx->is_60hz ? 12 : 18)
768 * 2 * vbi_active_samples;
769 } else {
770 /*
771 * See comment in cx18_vbi_setup() below about the
772 * extra lines we capture in sliced VBI mode due to
773 * the lines on which EAV RP codes toggle.
774 */
775 s->mdl_size = s->cx->is_60hz
776 ? (21 - 4 + 1) * 2 * vbi_hblank_samples_60Hz
777 : (23 - 2 + 1) * 2 * vbi_hblank_samples_50Hz;
778 }
779 break;
780 default:
781 s->bufs_per_mdl = 1;
782 s->mdl_size = s->buf_size * s->bufs_per_mdl;
783 break;
784 }
785
786 cx18_load_queues(s);
787}
788
789int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
790{
791 u32 data[MAX_MB_ARGUMENTS];
792 struct cx18 *cx = s->cx;
793 int captype = 0;
794 struct cx18_stream *s_idx;
795
796 if (!cx18_stream_enabled(s))
797 return -EINVAL;
798
799 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
800
801 switch (s->type) {
802 case CX18_ENC_STREAM_TYPE_MPG:
803 captype = CAPTURE_CHANNEL_TYPE_MPEG;
804 cx->mpg_data_received = cx->vbi_data_inserted = 0;
805 cx->dualwatch_jiffies = jiffies;
806 cx->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
807 cx->search_pack_header = 0;
808 break;
809
810 case CX18_ENC_STREAM_TYPE_IDX:
811 captype = CAPTURE_CHANNEL_TYPE_INDEX;
812 break;
813 case CX18_ENC_STREAM_TYPE_TS:
814 captype = CAPTURE_CHANNEL_TYPE_TS;
815 break;
816 case CX18_ENC_STREAM_TYPE_YUV:
817 captype = CAPTURE_CHANNEL_TYPE_YUV;
818 break;
819 case CX18_ENC_STREAM_TYPE_PCM:
820 captype = CAPTURE_CHANNEL_TYPE_PCM;
821 break;
822 case CX18_ENC_STREAM_TYPE_VBI:
823#ifdef CX18_ENCODER_PARSES_SLICED
824 captype = cx18_raw_vbi(cx) ?
825 CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
826#else
827 /*
828 * Currently we set things up so that Sliced VBI from the
829 * digitizer is handled as Raw VBI by the encoder
830 */
831 captype = CAPTURE_CHANNEL_TYPE_VBI;
832#endif
833 cx->vbi.frame = 0;
834 cx->vbi.inserted_frame = 0;
835 memset(cx->vbi.sliced_mpeg_size,
836 0, sizeof(cx->vbi.sliced_mpeg_size));
837 break;
838 default:
839 return -EINVAL;
840 }
841
842 /* Clear Streamoff flags in case left from last capture */
843 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
844
845 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
846 s->handle = data[0];
847 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
848
849 /*
850 * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
851 * set up all the parameters, as it is not obvious which parameters the
852 * firmware shares across capture channel types and which it does not.
853 *
854 * Some of the cx18_vapi() calls below apply to only certain capture
855 * channel types. We're hoping there's no harm in calling most of them
856 * anyway, as long as the values are all consistent. Setting some
857 * shared parameters will have no effect once an analog capture channel
858 * has started streaming.
859 */
860 if (captype != CAPTURE_CHANNEL_TYPE_TS) {
861 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
862 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
863 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
864 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
865
866 /*
867 * Audio related reset according to
868 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
869 */
870 if (atomic_read(&cx->ana_capturing) == 0)
871 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
872 s->handle, 12);
873
874 /*
875 * Number of lines for Field 1 & Field 2 according to
876 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
877 * Field 1 is 312 for 625 line systems in BT.656
878 * Field 2 is 313 for 625 line systems in BT.656
879 */
880 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
881 s->handle, 312, 313);
882
883 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
884 cx18_vbi_setup(s);
885
886 /*
887 * Select to receive I, P, and B frame index entries, if the
888 * index stream is enabled. Otherwise disable index entry
889 * generation.
890 */
891 s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
892 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 2,
893 s->handle, cx18_stream_enabled(s_idx) ? 7 : 0);
894
895 /* Call out to the common CX2341x API setup for user controls */
896 cx->cxhdl.priv = s;
897 cx2341x_handler_setup(&cx->cxhdl);
898
899 /*
900 * When starting a capture and we're set for radio,
901 * ensure the video is muted, despite the user control.
902 */
903 if (!cx->cxhdl.video_mute &&
904 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
905 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
906 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8) | 1);
907
908 /* Enable the Video Format Converter for UYVY 4:2:2 support,
909 * rather than the default HM12 Macroblovk 4:2:0 support.
910 */
911 if (captype == CAPTURE_CHANNEL_TYPE_YUV) {
912 if (s->pixelformat == V4L2_PIX_FMT_UYVY)
913 cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
914 s->handle, 1);
915 else
916 /* If in doubt, default to HM12 */
917 cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
918 s->handle, 0);
919 }
920 }
921
922 if (atomic_read(&cx->tot_capturing) == 0) {
923 cx2341x_handler_set_busy(&cx->cxhdl, 1);
924 clear_bit(CX18_F_I_EOS, &cx->i_flags);
925 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
926 }
927
928 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
929 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
930 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
931
932 /* Init all the cpu_mdls for this stream */
933 cx18_stream_configure_mdls(s);
934 _cx18_stream_load_fw_queue(s);
935
936 /* begin_capture */
937 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
938 CX18_DEBUG_WARN("Error starting capture!\n");
939 /* Ensure we're really not capturing before releasing MDLs */
940 set_bit(CX18_F_S_STOPPING, &s->s_flags);
941 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
942 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
943 else
944 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
945 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
946 /* FIXME - CX18_F_S_STREAMOFF as well? */
947 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
948 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
949 s->handle = CX18_INVALID_TASK_HANDLE;
950 clear_bit(CX18_F_S_STOPPING, &s->s_flags);
951 if (atomic_read(&cx->tot_capturing) == 0) {
952 set_bit(CX18_F_I_EOS, &cx->i_flags);
953 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
954 }
955 return -EINVAL;
956 }
957
958 /* you're live! sit back and await interrupts :) */
959 if (captype != CAPTURE_CHANNEL_TYPE_TS)
960 atomic_inc(&cx->ana_capturing);
961 atomic_inc(&cx->tot_capturing);
962 return 0;
963}
964EXPORT_SYMBOL(cx18_start_v4l2_encode_stream);
965
966void cx18_stop_all_captures(struct cx18 *cx)
967{
968 int i;
969
970 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
971 struct cx18_stream *s = &cx->streams[i];
972
973 if (!cx18_stream_enabled(s))
974 continue;
975 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
976 cx18_stop_v4l2_encode_stream(s, 0);
977 }
978}
979
980int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
981{
982 struct cx18 *cx = s->cx;
983
984 if (!cx18_stream_enabled(s))
985 return -EINVAL;
986
987 /* This function assumes that you are allowed to stop the capture
988 and that we are actually capturing */
989
990 CX18_DEBUG_INFO("Stop Capture\n");
991
992 if (atomic_read(&cx->tot_capturing) == 0)
993 return 0;
994
995 set_bit(CX18_F_S_STOPPING, &s->s_flags);
996 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
997 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
998 else
999 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
1000
1001 if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
1002 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
1003 }
1004
1005 if (s->type != CX18_ENC_STREAM_TYPE_TS)
1006 atomic_dec(&cx->ana_capturing);
1007 atomic_dec(&cx->tot_capturing);
1008
1009 /* Clear capture and no-read bits */
1010 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
1011
1012 /* Tell the CX23418 it can't use our buffers anymore */
1013 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
1014
1015 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
1016 s->handle = CX18_INVALID_TASK_HANDLE;
1017 clear_bit(CX18_F_S_STOPPING, &s->s_flags);
1018
1019 if (atomic_read(&cx->tot_capturing) > 0)
1020 return 0;
1021
1022 cx2341x_handler_set_busy(&cx->cxhdl, 0);
1023 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
1024 wake_up(&s->waitq);
1025
1026 return 0;
1027}
1028EXPORT_SYMBOL(cx18_stop_v4l2_encode_stream);
1029
1030u32 cx18_find_handle(struct cx18 *cx)
1031{
1032 int i;
1033
1034 /* find first available handle to be used for global settings */
1035 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1036 struct cx18_stream *s = &cx->streams[i];
1037
1038 if (s->video_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
1039 return s->handle;
1040 }
1041 return CX18_INVALID_TASK_HANDLE;
1042}
1043
1044struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
1045{
1046 int i;
1047 struct cx18_stream *s;
1048
1049 if (handle == CX18_INVALID_TASK_HANDLE)
1050 return NULL;
1051
1052 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1053 s = &cx->streams[i];
1054 if (s->handle != handle)
1055 continue;
1056 if (cx18_stream_enabled(s))
1057 return s;
1058 }
1059 return NULL;
1060}
diff --git a/drivers/media/pci/cx18/cx18-streams.h b/drivers/media/pci/cx18/cx18-streams.h
new file mode 100644
index 000000000000..713b0e61536d
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-streams.h
@@ -0,0 +1,62 @@
1/*
2 * cx18 init/start/stop/exit stream functions
3 *
4 * Derived from ivtv-streams.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25u32 cx18_find_handle(struct cx18 *cx);
26struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle);
27int cx18_streams_setup(struct cx18 *cx);
28int cx18_streams_register(struct cx18 *cx);
29void cx18_streams_cleanup(struct cx18 *cx, int unregister);
30
31#define CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN (3)
32void cx18_stream_rotate_idx_mdls(struct cx18 *cx);
33
34static inline bool cx18_stream_enabled(struct cx18_stream *s)
35{
36 return s->video_dev ||
37 (s->dvb && s->dvb->enabled) ||
38 (s->type == CX18_ENC_STREAM_TYPE_IDX &&
39 s->cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] != 0);
40}
41
42/* Related to submission of mdls to firmware */
43static inline void cx18_stream_load_fw_queue(struct cx18_stream *s)
44{
45 schedule_work(&s->out_work_order);
46}
47
48static inline void cx18_stream_put_mdl_fw(struct cx18_stream *s,
49 struct cx18_mdl *mdl)
50{
51 /* Put mdl on q_free; the out work handler will move mdl(s) to q_busy */
52 cx18_enqueue(s, mdl, &s->q_free);
53 cx18_stream_load_fw_queue(s);
54}
55
56void cx18_out_work_handler(struct work_struct *work);
57
58/* Capture related */
59int cx18_start_v4l2_encode_stream(struct cx18_stream *s);
60int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end);
61
62void cx18_stop_all_captures(struct cx18 *cx);
diff --git a/drivers/media/pci/cx18/cx18-vbi.c b/drivers/media/pci/cx18/cx18-vbi.c
new file mode 100644
index 000000000000..6d3121ff45a2
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-vbi.c
@@ -0,0 +1,277 @@
1/*
2 * cx18 Vertical Blank Interval support functions
3 *
4 * Derived from ivtv-vbi.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24#include "cx18-driver.h"
25#include "cx18-vbi.h"
26#include "cx18-ioctl.h"
27#include "cx18-queue.h"
28
29/*
30 * Raster Reference/Protection (RP) bytes, used in Start/End Active
31 * Video codes emitted from the digitzer in VIP 1.x mode, that flag the start
32 * of VBI sample or VBI ancillary data regions in the digitial ratser line.
33 *
34 * Task FieldEven VerticalBlank HorizontalBlank 0 0 0 0
35 */
36static const u8 raw_vbi_sav_rp[2] = { 0x20, 0x60 }; /* __V_, _FV_ */
37static const u8 sliced_vbi_eav_rp[2] = { 0xb0, 0xf0 }; /* T_VH, TFVH */
38
39static void copy_vbi_data(struct cx18 *cx, int lines, u32 pts_stamp)
40{
41 int line = 0;
42 int i;
43 u32 linemask[2] = { 0, 0 };
44 unsigned short size;
45 static const u8 mpeg_hdr_data[] = {
46 /* MPEG-2 Program Pack */
47 0x00, 0x00, 0x01, 0xba, /* Prog Pack start code */
48 0x44, 0x00, 0x0c, 0x66, 0x24, 0x01, /* SCR, SCR Ext, markers */
49 0x01, 0xd1, 0xd3, /* Mux Rate, markers */
50 0xfa, 0xff, 0xff, /* Res, Suff cnt, Stuff */
51 /* MPEG-2 Private Stream 1 PES Packet */
52 0x00, 0x00, 0x01, 0xbd, /* Priv Stream 1 start */
53 0x00, 0x1a, /* length */
54 0x84, 0x80, 0x07, /* flags, hdr data len */
55 0x21, 0x00, 0x5d, 0x63, 0xa7, /* PTS, markers */
56 0xff, 0xff /* stuffing */
57 };
58 const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
59 int idx = cx->vbi.frame % CX18_VBI_FRAMES;
60 u8 *dst = &cx->vbi.sliced_mpeg_data[idx][0];
61
62 for (i = 0; i < lines; i++) {
63 struct v4l2_sliced_vbi_data *sdata = cx->vbi.sliced_data + i;
64 int f, l;
65
66 if (sdata->id == 0)
67 continue;
68
69 l = sdata->line - 6;
70 f = sdata->field;
71 if (f)
72 l += 18;
73 if (l < 32)
74 linemask[0] |= (1 << l);
75 else
76 linemask[1] |= (1 << (l - 32));
77 dst[sd + 12 + line * 43] = cx18_service2vbi(sdata->id);
78 memcpy(dst + sd + 12 + line * 43 + 1, sdata->data, 42);
79 line++;
80 }
81 memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
82 if (line == 36) {
83 /* All lines are used, so there is no space for the linemask
84 (the max size of the VBI data is 36 * 43 + 4 bytes).
85 So in this case we use the magic number 'ITV0'. */
86 memcpy(dst + sd, "ITV0", 4);
87 memcpy(dst + sd + 4, dst + sd + 12, line * 43);
88 size = 4 + ((43 * line + 3) & ~3);
89 } else {
90 memcpy(dst + sd, "itv0", 4);
91 cpu_to_le32s(&linemask[0]);
92 cpu_to_le32s(&linemask[1]);
93 memcpy(dst + sd + 4, &linemask[0], 8);
94 size = 12 + ((43 * line + 3) & ~3);
95 }
96 dst[4+16] = (size + 10) >> 8;
97 dst[5+16] = (size + 10) & 0xff;
98 dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
99 dst[10+16] = (pts_stamp >> 22) & 0xff;
100 dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
101 dst[12+16] = (pts_stamp >> 7) & 0xff;
102 dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
103 cx->vbi.sliced_mpeg_size[idx] = sd + size;
104}
105
106/* Compress raw VBI format, removes leading SAV codes and surplus space
107 after the frame. Returns new compressed size. */
108/* FIXME - this function ignores the input size. */
109static u32 compress_raw_buf(struct cx18 *cx, u8 *buf, u32 size, u32 hdr_size)
110{
111 u32 line_size = vbi_active_samples;
112 u32 lines = cx->vbi.count * 2;
113 u8 *q = buf;
114 u8 *p;
115 int i;
116
117 /* Skip the header */
118 buf += hdr_size;
119
120 for (i = 0; i < lines; i++) {
121 p = buf + i * line_size;
122
123 /* Look for SAV code */
124 if (p[0] != 0xff || p[1] || p[2] ||
125 (p[3] != raw_vbi_sav_rp[0] &&
126 p[3] != raw_vbi_sav_rp[1]))
127 break;
128 if (i == lines - 1) {
129 /* last line is hdr_size bytes short - extrapolate it */
130 memcpy(q, p + 4, line_size - 4 - hdr_size);
131 q += line_size - 4 - hdr_size;
132 p += line_size - hdr_size - 1;
133 memset(q, (int) *p, hdr_size);
134 } else {
135 memcpy(q, p + 4, line_size - 4);
136 q += line_size - 4;
137 }
138 }
139 return lines * (line_size - 4);
140}
141
142static u32 compress_sliced_buf(struct cx18 *cx, u8 *buf, u32 size,
143 const u32 hdr_size)
144{
145 struct v4l2_decode_vbi_line vbi;
146 int i;
147 u32 line = 0;
148 u32 line_size = cx->is_60hz ? vbi_hblank_samples_60Hz
149 : vbi_hblank_samples_50Hz;
150
151 /* find the first valid line */
152 for (i = hdr_size, buf += hdr_size; i < size; i++, buf++) {
153 if (buf[0] == 0xff && !buf[1] && !buf[2] &&
154 (buf[3] == sliced_vbi_eav_rp[0] ||
155 buf[3] == sliced_vbi_eav_rp[1]))
156 break;
157 }
158
159 /*
160 * The last line is short by hdr_size bytes, but for the remaining
161 * checks against size, we pretend that it is not, by counting the
162 * header bytes we knowingly skipped
163 */
164 size -= (i - hdr_size);
165 if (size < line_size)
166 return line;
167
168 for (i = 0; i < size / line_size; i++) {
169 u8 *p = buf + i * line_size;
170
171 /* Look for EAV code */
172 if (p[0] != 0xff || p[1] || p[2] ||
173 (p[3] != sliced_vbi_eav_rp[0] &&
174 p[3] != sliced_vbi_eav_rp[1]))
175 continue;
176 vbi.p = p + 4;
177 v4l2_subdev_call(cx->sd_av, vbi, decode_vbi_line, &vbi);
178 if (vbi.type) {
179 cx->vbi.sliced_data[line].id = vbi.type;
180 cx->vbi.sliced_data[line].field = vbi.is_second_field;
181 cx->vbi.sliced_data[line].line = vbi.line;
182 memcpy(cx->vbi.sliced_data[line].data, vbi.p, 42);
183 line++;
184 }
185 }
186 return line;
187}
188
189static void _cx18_process_vbi_data(struct cx18 *cx, struct cx18_buffer *buf)
190{
191 /*
192 * The CX23418 provides a 12 byte header in its raw VBI buffers to us:
193 * 0x3fffffff [4 bytes of something] [4 byte presentation time stamp]
194 */
195 struct vbi_data_hdr {
196 __be32 magic;
197 __be32 unknown;
198 __be32 pts;
199 } *hdr = (struct vbi_data_hdr *) buf->buf;
200
201 u8 *p = (u8 *) buf->buf;
202 u32 size = buf->bytesused;
203 u32 pts;
204 int lines;
205
206 /*
207 * The CX23418 sends us data that is 32 bit little-endian swapped,
208 * but we want the raw VBI bytes in the order they were in the raster
209 * line. This has a side effect of making the header big endian
210 */
211 cx18_buf_swap(buf);
212
213 /* Raw VBI data */
214 if (cx18_raw_vbi(cx)) {
215
216 size = buf->bytesused =
217 compress_raw_buf(cx, p, size, sizeof(struct vbi_data_hdr));
218
219 /*
220 * Hack needed for compatibility with old VBI software.
221 * Write the frame # at the last 4 bytes of the frame
222 */
223 p += size - 4;
224 memcpy(p, &cx->vbi.frame, 4);
225 cx->vbi.frame++;
226 return;
227 }
228
229 /* Sliced VBI data with data insertion */
230
231 pts = (be32_to_cpu(hdr->magic) == 0x3fffffff) ? be32_to_cpu(hdr->pts)
232 : 0;
233
234 lines = compress_sliced_buf(cx, p, size, sizeof(struct vbi_data_hdr));
235
236 /* always return at least one empty line */
237 if (lines == 0) {
238 cx->vbi.sliced_data[0].id = 0;
239 cx->vbi.sliced_data[0].line = 0;
240 cx->vbi.sliced_data[0].field = 0;
241 lines = 1;
242 }
243 buf->bytesused = size = lines * sizeof(cx->vbi.sliced_data[0]);
244 memcpy(p, &cx->vbi.sliced_data[0], size);
245
246 if (cx->vbi.insert_mpeg)
247 copy_vbi_data(cx, lines, pts);
248 cx->vbi.frame++;
249}
250
251void cx18_process_vbi_data(struct cx18 *cx, struct cx18_mdl *mdl,
252 int streamtype)
253{
254 struct cx18_buffer *buf;
255 u32 orig_used;
256
257 if (streamtype != CX18_ENC_STREAM_TYPE_VBI)
258 return;
259
260 /*
261 * Big assumption here:
262 * Every buffer hooked to the MDL's buf_list is a complete VBI frame
263 * that ends at the end of the buffer.
264 *
265 * To assume anything else would make the code in this file
266 * more complex, or require extra memcpy()'s to make the
267 * buffers satisfy the above assumption. It's just simpler to set
268 * up the encoder buffer transfers to make the assumption true.
269 */
270 list_for_each_entry(buf, &mdl->buf_list, list) {
271 orig_used = buf->bytesused;
272 if (orig_used == 0)
273 break;
274 _cx18_process_vbi_data(cx, buf);
275 mdl->bytesused -= (orig_used - buf->bytesused);
276 }
277}
diff --git a/drivers/media/pci/cx18/cx18-vbi.h b/drivers/media/pci/cx18/cx18-vbi.h
new file mode 100644
index 000000000000..b365cf4b4668
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-vbi.h
@@ -0,0 +1,26 @@
1/*
2 * cx18 Vertical Blank Interval support functions
3 *
4 * Derived from ivtv-vbi.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24void cx18_process_vbi_data(struct cx18 *cx, struct cx18_mdl *mdl,
25 int streamtype);
26int cx18_used_line(struct cx18 *cx, int line, int field);
diff --git a/drivers/media/pci/cx18/cx18-version.h b/drivers/media/pci/cx18/cx18-version.h
new file mode 100644
index 000000000000..fed48b6bb67b
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-version.h
@@ -0,0 +1,28 @@
1/*
2 * cx18 driver version information
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 */
21
22#ifndef CX18_VERSION_H
23#define CX18_VERSION_H
24
25#define CX18_DRIVER_NAME "cx18"
26#define CX18_VERSION "1.5.1"
27
28#endif
diff --git a/drivers/media/pci/cx18/cx18-video.c b/drivers/media/pci/cx18/cx18-video.c
new file mode 100644
index 000000000000..6dc84aac8f44
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-video.c
@@ -0,0 +1,32 @@
1/*
2 * cx18 video interface functions
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 */
21
22#include "cx18-driver.h"
23#include "cx18-video.h"
24#include "cx18-cards.h"
25
26void cx18_video_set_io(struct cx18 *cx)
27{
28 int inp = cx->active_input;
29
30 v4l2_subdev_call(cx->sd_av, video, s_routing,
31 cx->card->video_inputs[inp].video_input, 0, 0);
32}
diff --git a/drivers/media/pci/cx18/cx18-video.h b/drivers/media/pci/cx18/cx18-video.h
new file mode 100644
index 000000000000..529006a06e5c
--- /dev/null
+++ b/drivers/media/pci/cx18/cx18-video.h
@@ -0,0 +1,22 @@
1/*
2 * cx18 video interface functions
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 */
21
22void cx18_video_set_io(struct cx18 *cx);
diff --git a/drivers/media/pci/cx18/cx23418.h b/drivers/media/pci/cx18/cx23418.h
new file mode 100644
index 000000000000..767a8d23e3f2
--- /dev/null
+++ b/drivers/media/pci/cx18/cx23418.h
@@ -0,0 +1,492 @@
1/*
2 * cx18 header containing common defines.
3 *
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 */
21
22#ifndef CX23418_H
23#define CX23418_H
24
25#include <media/cx2341x.h>
26
27#define MGR_CMD_MASK 0x40000000
28/* The MSB of the command code indicates that this is the completion of a
29 command */
30#define MGR_CMD_MASK_ACK (MGR_CMD_MASK | 0x80000000)
31
32/* Description: This command creates a new instance of a certain task
33 IN[0] - Task ID. This is one of the XPU_CMD_MASK_YYY where XPU is
34 the processor on which the task YYY will be created
35 OUT[0] - Task handle. This handle is passed along with commands to
36 dispatch to the right instance of the task
37 ReturnCode - One of the ERR_SYS_... */
38#define CX18_CREATE_TASK (MGR_CMD_MASK | 0x0001)
39
40/* Description: This command destroys an instance of a task
41 IN[0] - Task handle. Hanlde of the task to destroy
42 ReturnCode - One of the ERR_SYS_... */
43#define CX18_DESTROY_TASK (MGR_CMD_MASK | 0x0002)
44
45/* All commands for CPU have the following mask set */
46#define CPU_CMD_MASK 0x20000000
47#define CPU_CMD_MASK_DEBUG (CPU_CMD_MASK | 0x00000000)
48#define CPU_CMD_MASK_ACK (CPU_CMD_MASK | 0x80000000)
49#define CPU_CMD_MASK_CAPTURE (CPU_CMD_MASK | 0x00020000)
50#define CPU_CMD_MASK_TS (CPU_CMD_MASK | 0x00040000)
51
52#define EPU_CMD_MASK 0x02000000
53#define EPU_CMD_MASK_DEBUG (EPU_CMD_MASK | 0x000000)
54#define EPU_CMD_MASK_DE (EPU_CMD_MASK | 0x040000)
55
56#define APU_CMD_MASK 0x10000000
57#define APU_CMD_MASK_ACK (APU_CMD_MASK | 0x80000000)
58
59#define CX18_APU_ENCODING_METHOD_MPEG (0 << 28)
60#define CX18_APU_ENCODING_METHOD_AC3 (1 << 28)
61
62/* Description: Command APU to start audio
63 IN[0] - audio parameters (same as CX18_CPU_SET_AUDIO_PARAMETERS?)
64 IN[1] - caller buffer address, or 0
65 ReturnCode - ??? */
66#define CX18_APU_START (APU_CMD_MASK | 0x01)
67
68/* Description: Command APU to stop audio
69 IN[0] - encoding method to stop
70 ReturnCode - ??? */
71#define CX18_APU_STOP (APU_CMD_MASK | 0x02)
72
73/* Description: Command APU to reset the AI
74 ReturnCode - ??? */
75#define CX18_APU_RESETAI (APU_CMD_MASK | 0x05)
76
77/* Description: This command indicates that a Memory Descriptor List has been
78 filled with the requested channel type
79 IN[0] - Task handle. Handle of the task
80 IN[1] - Offset of the MDL_ACK from the beginning of the local DDR.
81 IN[2] - Number of CNXT_MDL_ACK structures in the array pointed to by IN[1]
82 ReturnCode - One of the ERR_DE_... */
83#define CX18_EPU_DMA_DONE (EPU_CMD_MASK_DE | 0x0001)
84
85/* Something interesting happened
86 IN[0] - A value to log
87 IN[1] - An offset of a string in the MiniMe memory;
88 0/zero/NULL means "I have nothing to say" */
89#define CX18_EPU_DEBUG (EPU_CMD_MASK_DEBUG | 0x0003)
90
91/* Reads memory/registers (32-bit)
92 IN[0] - Address
93 OUT[1] - Value */
94#define CX18_CPU_DEBUG_PEEK32 (CPU_CMD_MASK_DEBUG | 0x0003)
95
96/* Description: This command starts streaming with the set channel type
97 IN[0] - Task handle. Handle of the task to start
98 ReturnCode - One of the ERR_CAPTURE_... */
99#define CX18_CPU_CAPTURE_START (CPU_CMD_MASK_CAPTURE | 0x0002)
100
101/* Description: This command stops streaming with the set channel type
102 IN[0] - Task handle. Handle of the task to stop
103 IN[1] - 0 = stop at end of GOP, 1 = stop at end of frame (MPEG only)
104 ReturnCode - One of the ERR_CAPTURE_... */
105#define CX18_CPU_CAPTURE_STOP (CPU_CMD_MASK_CAPTURE | 0x0003)
106
107/* Description: This command pauses streaming with the set channel type
108 IN[0] - Task handle. Handle of the task to pause
109 ReturnCode - One of the ERR_CAPTURE_... */
110#define CX18_CPU_CAPTURE_PAUSE (CPU_CMD_MASK_CAPTURE | 0x0007)
111
112/* Description: This command resumes streaming with the set channel type
113 IN[0] - Task handle. Handle of the task to resume
114 ReturnCode - One of the ERR_CAPTURE_... */
115#define CX18_CPU_CAPTURE_RESUME (CPU_CMD_MASK_CAPTURE | 0x0008)
116
117#define CAPTURE_CHANNEL_TYPE_NONE 0
118#define CAPTURE_CHANNEL_TYPE_MPEG 1
119#define CAPTURE_CHANNEL_TYPE_INDEX 2
120#define CAPTURE_CHANNEL_TYPE_YUV 3
121#define CAPTURE_CHANNEL_TYPE_PCM 4
122#define CAPTURE_CHANNEL_TYPE_VBI 5
123#define CAPTURE_CHANNEL_TYPE_SLICED_VBI 6
124#define CAPTURE_CHANNEL_TYPE_TS 7
125#define CAPTURE_CHANNEL_TYPE_MAX 15
126
127/* Description: This command sets the channel type. This can only be done
128 when stopped.
129 IN[0] - Task handle. Handle of the task to start
130 IN[1] - Channel Type. See Below.
131 ReturnCode - One of the ERR_CAPTURE_... */
132#define CX18_CPU_SET_CHANNEL_TYPE (CPU_CMD_MASK_CAPTURE + 1)
133
134/* Description: Set stream output type
135 IN[0] - task handle. Handle of the task to start
136 IN[1] - type
137 ReturnCode - One of the ERR_CAPTURE_... */
138#define CX18_CPU_SET_STREAM_OUTPUT_TYPE (CPU_CMD_MASK_CAPTURE | 0x0012)
139
140/* Description: Set video input resolution and frame rate
141 IN[0] - task handle
142 IN[1] - reserved
143 IN[2] - reserved
144 IN[3] - reserved
145 IN[4] - reserved
146 IN[5] - frame rate, 0 - 29.97f/s, 1 - 25f/s
147 ReturnCode - One of the ERR_CAPTURE_... */
148#define CX18_CPU_SET_VIDEO_IN (CPU_CMD_MASK_CAPTURE | 0x0004)
149
150/* Description: Set video frame rate
151 IN[0] - task handle. Handle of the task to start
152 IN[1] - video bit rate mode
153 IN[2] - video average rate
154 IN[3] - video peak rate
155 IN[4] - system mux rate
156 ReturnCode - One of the ERR_CAPTURE_... */
157#define CX18_CPU_SET_VIDEO_RATE (CPU_CMD_MASK_CAPTURE | 0x0005)
158
159/* Description: Set video output resolution
160 IN[0] - task handle
161 IN[1] - horizontal size
162 IN[2] - vertical size
163 ReturnCode - One of the ERR_CAPTURE_... */
164#define CX18_CPU_SET_VIDEO_RESOLUTION (CPU_CMD_MASK_CAPTURE | 0x0006)
165
166/* Description: This command set filter parameters
167 IN[0] - Task handle. Handle of the task
168 IN[1] - type, 0 - temporal, 1 - spatial, 2 - median
169 IN[2] - mode, temporal/spatial: 0 - disable, 1 - static, 2 - dynamic
170 median: 0 = disable, 1 = horizontal, 2 = vertical,
171 3 = horizontal/vertical, 4 = diagonal
172 IN[3] - strength, temporal 0 - 31, spatial 0 - 15
173 ReturnCode - One of the ERR_CAPTURE_... */
174#define CX18_CPU_SET_FILTER_PARAM (CPU_CMD_MASK_CAPTURE | 0x0009)
175
176/* Description: This command set spatial filter type
177 IN[0] - Task handle.
178 IN[1] - luma type: 0 = disable, 1 = 1D horizontal only, 2 = 1D vertical only,
179 3 = 2D H/V separable, 4 = 2D symmetric non-separable
180 IN[2] - chroma type: 0 - disable, 1 = 1D horizontal
181 ReturnCode - One of the ERR_CAPTURE_... */
182#define CX18_CPU_SET_SPATIAL_FILTER_TYPE (CPU_CMD_MASK_CAPTURE | 0x000C)
183
184/* Description: This command set coring levels for median filter
185 IN[0] - Task handle.
186 IN[1] - luma_high
187 IN[2] - luma_low
188 IN[3] - chroma_high
189 IN[4] - chroma_low
190 ReturnCode - One of the ERR_CAPTURE_... */
191#define CX18_CPU_SET_MEDIAN_CORING (CPU_CMD_MASK_CAPTURE | 0x000E)
192
193/* Description: This command set the picture type mask for index file
194 IN[0] - Task handle (ignored by firmware)
195 IN[1] - 0 = disable index file output
196 1 = output I picture
197 2 = P picture
198 4 = B picture
199 other = illegal */
200#define CX18_CPU_SET_INDEXTABLE (CPU_CMD_MASK_CAPTURE | 0x0010)
201
202/* Description: Set audio parameters
203 IN[0] - task handle. Handle of the task to start
204 IN[1] - audio parameter
205 ReturnCode - One of the ERR_CAPTURE_... */
206#define CX18_CPU_SET_AUDIO_PARAMETERS (CPU_CMD_MASK_CAPTURE | 0x0011)
207
208/* Description: Set video mute
209 IN[0] - task handle. Handle of the task to start
210 IN[1] - bit31-24: muteYvalue
211 bit23-16: muteUvalue
212 bit15-8: muteVvalue
213 bit0: 1:mute, 0: unmute
214 ReturnCode - One of the ERR_CAPTURE_... */
215#define CX18_CPU_SET_VIDEO_MUTE (CPU_CMD_MASK_CAPTURE | 0x0013)
216
217/* Description: Set audio mute
218 IN[0] - task handle. Handle of the task to start
219 IN[1] - mute/unmute
220 ReturnCode - One of the ERR_CAPTURE_... */
221#define CX18_CPU_SET_AUDIO_MUTE (CPU_CMD_MASK_CAPTURE | 0x0014)
222
223/* Description: Set stream output type
224 IN[0] - task handle. Handle of the task to start
225 IN[1] - subType
226 SET_INITIAL_SCR 1
227 SET_QUALITY_MODE 2
228 SET_VIM_PROTECT_MODE 3
229 SET_PTS_CORRECTION 4
230 SET_USB_FLUSH_MODE 5
231 SET_MERAQPAR_ENABLE 6
232 SET_NAV_PACK_INSERTION 7
233 SET_SCENE_CHANGE_ENABLE 8
234 IN[2] - parameter 1
235 IN[3] - parameter 2
236 ReturnCode - One of the ERR_CAPTURE_... */
237#define CX18_CPU_SET_MISC_PARAMETERS (CPU_CMD_MASK_CAPTURE | 0x0015)
238
239/* Description: Set raw VBI parameters
240 IN[0] - Task handle
241 IN[1] - No. of input lines per field:
242 bit[15:0]: field 1,
243 bit[31:16]: field 2
244 IN[2] - No. of input bytes per line
245 IN[3] - No. of output frames per transfer
246 IN[4] - start code
247 IN[5] - stop code
248 ReturnCode */
249#define CX18_CPU_SET_RAW_VBI_PARAM (CPU_CMD_MASK_CAPTURE | 0x0016)
250
251/* Description: Set capture line No.
252 IN[0] - task handle. Handle of the task to start
253 IN[1] - height1
254 IN[2] - height2
255 ReturnCode - One of the ERR_CAPTURE_... */
256#define CX18_CPU_SET_CAPTURE_LINE_NO (CPU_CMD_MASK_CAPTURE | 0x0017)
257
258/* Description: Set copyright
259 IN[0] - task handle. Handle of the task to start
260 IN[1] - copyright
261 ReturnCode - One of the ERR_CAPTURE_... */
262#define CX18_CPU_SET_COPYRIGHT (CPU_CMD_MASK_CAPTURE | 0x0018)
263
264/* Description: Set audio PID
265 IN[0] - task handle. Handle of the task to start
266 IN[1] - PID
267 ReturnCode - One of the ERR_CAPTURE_... */
268#define CX18_CPU_SET_AUDIO_PID (CPU_CMD_MASK_CAPTURE | 0x0019)
269
270/* Description: Set video PID
271 IN[0] - task handle. Handle of the task to start
272 IN[1] - PID
273 ReturnCode - One of the ERR_CAPTURE_... */
274#define CX18_CPU_SET_VIDEO_PID (CPU_CMD_MASK_CAPTURE | 0x001A)
275
276/* Description: Set Vertical Crop Line
277 IN[0] - task handle. Handle of the task to start
278 IN[1] - Line
279 ReturnCode - One of the ERR_CAPTURE_... */
280#define CX18_CPU_SET_VER_CROP_LINE (CPU_CMD_MASK_CAPTURE | 0x001B)
281
282/* Description: Set COP structure
283 IN[0] - task handle. Handle of the task to start
284 IN[1] - M
285 IN[2] - N
286 ReturnCode - One of the ERR_CAPTURE_... */
287#define CX18_CPU_SET_GOP_STRUCTURE (CPU_CMD_MASK_CAPTURE | 0x001C)
288
289/* Description: Set Scene Change Detection
290 IN[0] - task handle. Handle of the task to start
291 IN[1] - scene change
292 ReturnCode - One of the ERR_CAPTURE_... */
293#define CX18_CPU_SET_SCENE_CHANGE_DETECTION (CPU_CMD_MASK_CAPTURE | 0x001D)
294
295/* Description: Set Aspect Ratio
296 IN[0] - task handle. Handle of the task to start
297 IN[1] - AspectRatio
298 ReturnCode - One of the ERR_CAPTURE_... */
299#define CX18_CPU_SET_ASPECT_RATIO (CPU_CMD_MASK_CAPTURE | 0x001E)
300
301/* Description: Set Skip Input Frame
302 IN[0] - task handle. Handle of the task to start
303 IN[1] - skip input frames
304 ReturnCode - One of the ERR_CAPTURE_... */
305#define CX18_CPU_SET_SKIP_INPUT_FRAME (CPU_CMD_MASK_CAPTURE | 0x001F)
306
307/* Description: Set sliced VBI parameters -
308 Note This API will only apply to MPEG and Sliced VBI Channels
309 IN[0] - Task handle
310 IN[1] - output type, 0 - CC, 1 - Moji, 2 - Teletext
311 IN[2] - start / stop line
312 bit[15:0] start line number
313 bit[31:16] stop line number
314 IN[3] - number of output frames per interrupt
315 IN[4] - VBI insertion mode
316 bit 0: output user data, 1 - enable
317 bit 1: output private stream, 1 - enable
318 bit 2: mux option, 0 - in GOP, 1 - in picture
319 bit[7:0] private stream ID
320 IN[5] - insertion period while mux option is in picture
321 ReturnCode - VBI data offset */
322#define CX18_CPU_SET_SLICED_VBI_PARAM (CPU_CMD_MASK_CAPTURE | 0x0020)
323
324/* Description: Set the user data place holder
325 IN[0] - type of data (0 for user)
326 IN[1] - Stuffing period
327 IN[2] - ID data size in word (less than 10)
328 IN[3] - Pointer to ID buffer */
329#define CX18_CPU_SET_USERDATA_PLACE_HOLDER (CPU_CMD_MASK_CAPTURE | 0x0021)
330
331
332/* Description:
333 In[0] Task Handle
334 return parameter:
335 Out[0] Reserved
336 Out[1] Video PTS bit[32:2] of last output video frame.
337 Out[2] Video PTS bit[ 1:0] of last output video frame.
338 Out[3] Hardware Video PTS counter bit[31:0],
339 these bits get incremented on every 90kHz clock tick.
340 Out[4] Hardware Video PTS counter bit32,
341 these bits get incremented on every 90kHz clock tick.
342 ReturnCode */
343#define CX18_CPU_GET_ENC_PTS (CPU_CMD_MASK_CAPTURE | 0x0022)
344
345/* Description: Set VFC parameters
346 IN[0] - task handle
347 IN[1] - VFC enable flag, 1 - enable, 0 - disable
348*/
349#define CX18_CPU_SET_VFC_PARAM (CPU_CMD_MASK_CAPTURE | 0x0023)
350
351/* Below is the list of commands related to the data exchange */
352#define CPU_CMD_MASK_DE (CPU_CMD_MASK | 0x040000)
353
354/* Description: This command provides the physical base address of the local
355 DDR as viewed by EPU
356 IN[0] - Physical offset where EPU has the local DDR mapped
357 ReturnCode - One of the ERR_DE_... */
358#define CPU_CMD_DE_SetBase (CPU_CMD_MASK_DE | 0x0001)
359
360/* Description: This command provides the offsets in the device memory where
361 the 2 cx18_mdl_ack blocks reside
362 IN[0] - Task handle. Handle of the task to start
363 IN[1] - Offset of the first cx18_mdl_ack from the beginning of the
364 local DDR.
365 IN[2] - Offset of the second cx18_mdl_ack from the beginning of the
366 local DDR.
367 ReturnCode - One of the ERR_DE_... */
368#define CX18_CPU_DE_SET_MDL_ACK (CPU_CMD_MASK_DE | 0x0002)
369
370/* Description: This command provides the offset to a Memory Descriptor List
371 IN[0] - Task handle. Handle of the task to start
372 IN[1] - Offset of the MDL from the beginning of the local DDR.
373 IN[2] - Number of cx18_mdl_ent structures in the array pointed to by IN[1]
374 IN[3] - Buffer ID
375 IN[4] - Total buffer length
376 ReturnCode - One of the ERR_DE_... */
377#define CX18_CPU_DE_SET_MDL (CPU_CMD_MASK_DE | 0x0005)
378
379/* Description: This command requests return of all current Memory
380 Descriptor Lists to the driver
381 IN[0] - Task handle. Handle of the task to start
382 ReturnCode - One of the ERR_DE_... */
383#define CX18_CPU_DE_RELEASE_MDL (CPU_CMD_MASK_DE | 0x0006)
384
385/* Description: This command signals the cpu that the dat buffer has been
386 consumed and ready for re-use.
387 IN[0] - Task handle. Handle of the task
388 IN[1] - Offset of the data block from the beginning of the local DDR.
389 IN[2] - Number of bytes in the data block
390 ReturnCode - One of the ERR_DE_... */
391/* #define CX18_CPU_DE_RELEASE_BUFFER (CPU_CMD_MASK_DE | 0x0007) */
392
393/* No Error / Success */
394#define CNXT_OK 0x000000
395
396/* Received unknown command */
397#define CXERR_UNK_CMD 0x000001
398
399/* First parameter in the command is invalid */
400#define CXERR_INVALID_PARAM1 0x000002
401
402/* Second parameter in the command is invalid */
403#define CXERR_INVALID_PARAM2 0x000003
404
405/* Device interface is not open/found */
406#define CXERR_DEV_NOT_FOUND 0x000004
407
408/* Requested function is not implemented/available */
409#define CXERR_NOTSUPPORTED 0x000005
410
411/* Invalid pointer is provided */
412#define CXERR_BADPTR 0x000006
413
414/* Unable to allocate memory */
415#define CXERR_NOMEM 0x000007
416
417/* Object/Link not found */
418#define CXERR_LINK 0x000008
419
420/* Device busy, command cannot be executed */
421#define CXERR_BUSY 0x000009
422
423/* File/device/handle is not open. */
424#define CXERR_NOT_OPEN 0x00000A
425
426/* Value is out of range */
427#define CXERR_OUTOFRANGE 0x00000B
428
429/* Buffer overflow */
430#define CXERR_OVERFLOW 0x00000C
431
432/* Version mismatch */
433#define CXERR_BADVER 0x00000D
434
435/* Operation timed out */
436#define CXERR_TIMEOUT 0x00000E
437
438/* Operation aborted */
439#define CXERR_ABORT 0x00000F
440
441/* Specified I2C device not found for read/write */
442#define CXERR_I2CDEV_NOTFOUND 0x000010
443
444/* Error in I2C data xfer (but I2C device is present) */
445#define CXERR_I2CDEV_XFERERR 0x000011
446
447/* Chanel changing component not ready */
448#define CXERR_CHANNELNOTREADY 0x000012
449
450/* PPU (Presensation/Decoder) mail box is corrupted */
451#define CXERR_PPU_MB_CORRUPT 0x000013
452
453/* CPU (Capture/Encoder) mail box is corrupted */
454#define CXERR_CPU_MB_CORRUPT 0x000014
455
456/* APU (Audio) mail box is corrupted */
457#define CXERR_APU_MB_CORRUPT 0x000015
458
459/* Unable to open file for reading */
460#define CXERR_FILE_OPEN_READ 0x000016
461
462/* Unable to open file for writing */
463#define CXERR_FILE_OPEN_WRITE 0x000017
464
465/* Unable to find the I2C section specified */
466#define CXERR_I2C_BADSECTION 0x000018
467
468/* Error in I2C data xfer (but I2C device is present) */
469#define CXERR_I2CDEV_DATALOW 0x000019
470
471/* Error in I2C data xfer (but I2C device is present) */
472#define CXERR_I2CDEV_CLOCKLOW 0x00001A
473
474/* No Interrupt received from HW (for I2C access) */
475#define CXERR_NO_HW_I2C_INTR 0x00001B
476
477/* RPU is not ready to accept commands! */
478#define CXERR_RPU_NOT_READY 0x00001C
479
480/* RPU is not ready to accept commands! */
481#define CXERR_RPU_NO_ACK 0x00001D
482
483/* The are no buffers ready. Try again soon! */
484#define CXERR_NODATA_AGAIN 0x00001E
485
486/* The stream is stopping. Function not allowed now! */
487#define CXERR_STOPPING_STATUS 0x00001F
488
489/* Trying to access hardware when the power is turned OFF */
490#define CXERR_DEVPOWER_OFF 0x000020
491
492#endif /* CX23418_H */
diff --git a/drivers/media/pci/cx23885/Kconfig b/drivers/media/pci/cx23885/Kconfig
new file mode 100644
index 000000000000..b391e9bda877
--- /dev/null
+++ b/drivers/media/pci/cx23885/Kconfig
@@ -0,0 +1,46 @@
1config VIDEO_CX23885
2 tristate "Conexant cx23885 (2388x successor) support"
3 depends on DVB_CORE && VIDEO_DEV && PCI && I2C && INPUT && SND
4 select SND_PCM
5 select I2C_ALGOBIT
6 select VIDEO_BTCX
7 select VIDEO_TUNER
8 select VIDEO_TVEEPROM
9 depends on RC_CORE
10 select VIDEOBUF_DVB
11 select VIDEOBUF_DMA_SG
12 select VIDEO_CX25840
13 select VIDEO_CX2341X
14 select DVB_DIB7000P if !DVB_FE_CUSTOMISE
15 select DVB_S5H1409 if !DVB_FE_CUSTOMISE
16 select DVB_S5H1411 if !DVB_FE_CUSTOMISE
17 select DVB_LGDT330X if !DVB_FE_CUSTOMISE
18 select DVB_ZL10353 if !DVB_FE_CUSTOMISE
19 select DVB_TDA10048 if !DVB_FE_CUSTOMISE
20 select DVB_LNBP21 if !DVB_FE_CUSTOMISE
21 select DVB_STV6110 if !DVB_FE_CUSTOMISE
22 select DVB_CX24116 if !DVB_FE_CUSTOMISE
23 select DVB_STV0900 if !DVB_FE_CUSTOMISE
24 select DVB_DS3000 if !DVB_FE_CUSTOMISE
25 select DVB_STV0367 if !DVB_FE_CUSTOMISE
26 select MEDIA_TUNER_MT2131 if !MEDIA_TUNER_CUSTOMISE
27 select MEDIA_TUNER_XC2028 if !MEDIA_TUNER_CUSTOMISE
28 select MEDIA_TUNER_TDA8290 if !MEDIA_TUNER_CUSTOMISE
29 select MEDIA_TUNER_TDA18271 if !MEDIA_TUNER_CUSTOMISE
30 select MEDIA_TUNER_XC5000 if !MEDIA_TUNER_CUSTOMISE
31 ---help---
32 This is a video4linux driver for Conexant 23885 based
33 TV cards.
34
35 To compile this driver as a module, choose M here: the
36 module will be called cx23885
37
38config MEDIA_ALTERA_CI
39 tristate "Altera FPGA based CI module"
40 depends on VIDEO_CX23885 && DVB_CORE
41 select ALTERA_STAPL
42 ---help---
43 An Altera FPGA CI module for NetUP Dual DVB-T/C RF CI card.
44
45 To compile this driver as a module, choose M here: the
46 module will be called altera-ci
diff --git a/drivers/media/pci/cx23885/Makefile b/drivers/media/pci/cx23885/Makefile
new file mode 100644
index 000000000000..f92cc4c14f0c
--- /dev/null
+++ b/drivers/media/pci/cx23885/Makefile
@@ -0,0 +1,15 @@
1cx23885-objs := cx23885-cards.o cx23885-video.o cx23885-vbi.o \
2 cx23885-core.o cx23885-i2c.o cx23885-dvb.o cx23885-417.o \
3 cx23885-ioctl.o cx23885-ir.o cx23885-av.o cx23885-input.o \
4 cx23888-ir.o netup-init.o cimax2.o netup-eeprom.o \
5 cx23885-f300.o cx23885-alsa.o
6
7obj-$(CONFIG_VIDEO_CX23885) += cx23885.o
8obj-$(CONFIG_MEDIA_ALTERA_CI) += altera-ci.o
9
10ccflags-y += -Idrivers/media/video
11ccflags-y += -Idrivers/media/tuners
12ccflags-y += -Idrivers/media/dvb-core
13ccflags-y += -Idrivers/media/dvb-frontends
14
15ccflags-y += $(extra-cflags-y) $(extra-cflags-m)
diff --git a/drivers/media/pci/cx23885/altera-ci.c b/drivers/media/pci/cx23885/altera-ci.c
new file mode 100644
index 000000000000..1fa8927f0d36
--- /dev/null
+++ b/drivers/media/pci/cx23885/altera-ci.c
@@ -0,0 +1,837 @@
1/*
2 * altera-ci.c
3 *
4 * CI driver in conjunction with NetUp Dual DVB-T/C RF CI card
5 *
6 * Copyright (C) 2010,2011 NetUP Inc.
7 * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 *
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25/*
26 * currently cx23885 GPIO's used.
27 * GPIO-0 ~INT in
28 * GPIO-1 TMS out
29 * GPIO-2 ~reset chips out
30 * GPIO-3 to GPIO-10 data/addr for CA in/out
31 * GPIO-11 ~CS out
32 * GPIO-12 AD_RG out
33 * GPIO-13 ~WR out
34 * GPIO-14 ~RD out
35 * GPIO-15 ~RDY in
36 * GPIO-16 TCK out
37 * GPIO-17 TDO in
38 * GPIO-18 TDI out
39 */
40/*
41 * Bit definitions for MC417_RWD and MC417_OEN registers
42 * bits 31-16
43 * +-----------+
44 * | Reserved |
45 * +-----------+
46 * bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
47 * +-------+-------+-------+-------+-------+-------+-------+-------+
48 * | TDI | TDO | TCK | RDY# | #RD | #WR | AD_RG | #CS |
49 * +-------+-------+-------+-------+-------+-------+-------+-------+
50 * bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
51 * +-------+-------+-------+-------+-------+-------+-------+-------+
52 * | DATA7| DATA6| DATA5| DATA4| DATA3| DATA2| DATA1| DATA0|
53 * +-------+-------+-------+-------+-------+-------+-------+-------+
54 */
55#include <media/videobuf-dma-sg.h>
56#include <media/videobuf-dvb.h>
57#include "altera-ci.h"
58#include "dvb_ca_en50221.h"
59
60/* FPGA regs */
61#define NETUP_CI_INT_CTRL 0x00
62#define NETUP_CI_BUSCTRL2 0x01
63#define NETUP_CI_ADDR0 0x04
64#define NETUP_CI_ADDR1 0x05
65#define NETUP_CI_DATA 0x06
66#define NETUP_CI_BUSCTRL 0x07
67#define NETUP_CI_PID_ADDR0 0x08
68#define NETUP_CI_PID_ADDR1 0x09
69#define NETUP_CI_PID_DATA 0x0a
70#define NETUP_CI_TSA_DIV 0x0c
71#define NETUP_CI_TSB_DIV 0x0d
72#define NETUP_CI_REVISION 0x0f
73
74/* const for ci op */
75#define NETUP_CI_FLG_CTL 1
76#define NETUP_CI_FLG_RD 1
77#define NETUP_CI_FLG_AD 1
78
79static unsigned int ci_dbg;
80module_param(ci_dbg, int, 0644);
81MODULE_PARM_DESC(ci_dbg, "Enable CI debugging");
82
83static unsigned int pid_dbg;
84module_param(pid_dbg, int, 0644);
85MODULE_PARM_DESC(pid_dbg, "Enable PID filtering debugging");
86
87MODULE_DESCRIPTION("altera FPGA CI module");
88MODULE_AUTHOR("Igor M. Liplianin <liplianin@netup.ru>");
89MODULE_LICENSE("GPL");
90
91#define ci_dbg_print(args...) \
92 do { \
93 if (ci_dbg) \
94 printk(KERN_DEBUG args); \
95 } while (0)
96
97#define pid_dbg_print(args...) \
98 do { \
99 if (pid_dbg) \
100 printk(KERN_DEBUG args); \
101 } while (0)
102
103struct altera_ci_state;
104struct netup_hw_pid_filter;
105
106struct fpga_internal {
107 void *dev;
108 struct mutex fpga_mutex;/* two CI's on the same fpga */
109 struct netup_hw_pid_filter *pid_filt[2];
110 struct altera_ci_state *state[2];
111 struct work_struct work;
112 int (*fpga_rw) (void *dev, int flag, int data, int rw);
113 int cis_used;
114 int filts_used;
115 int strt_wrk;
116};
117
118/* stores all private variables for communication with CI */
119struct altera_ci_state {
120 struct fpga_internal *internal;
121 struct dvb_ca_en50221 ca;
122 int status;
123 int nr;
124};
125
126/* stores all private variables for hardware pid filtering */
127struct netup_hw_pid_filter {
128 struct fpga_internal *internal;
129 struct dvb_demux *demux;
130 /* save old functions */
131 int (*start_feed)(struct dvb_demux_feed *feed);
132 int (*stop_feed)(struct dvb_demux_feed *feed);
133
134 int status;
135 int nr;
136};
137
138/* internal params node */
139struct fpga_inode {
140 /* pointer for internal params, one for each pair of CI's */
141 struct fpga_internal *internal;
142 struct fpga_inode *next_inode;
143};
144
145/* first internal params */
146static struct fpga_inode *fpga_first_inode;
147
148/* find chip by dev */
149static struct fpga_inode *find_inode(void *dev)
150{
151 struct fpga_inode *temp_chip = fpga_first_inode;
152
153 if (temp_chip == NULL)
154 return temp_chip;
155
156 /*
157 Search for the last fpga CI chip or
158 find it by dev */
159 while ((temp_chip != NULL) &&
160 (temp_chip->internal->dev != dev))
161 temp_chip = temp_chip->next_inode;
162
163 return temp_chip;
164}
165/* check demux */
166static struct fpga_internal *check_filter(struct fpga_internal *temp_int,
167 void *demux_dev, int filt_nr)
168{
169 if (temp_int == NULL)
170 return NULL;
171
172 if ((temp_int->pid_filt[filt_nr]) == NULL)
173 return NULL;
174
175 if (temp_int->pid_filt[filt_nr]->demux == demux_dev)
176 return temp_int;
177
178 return NULL;
179}
180
181/* find chip by demux */
182static struct fpga_inode *find_dinode(void *demux_dev)
183{
184 struct fpga_inode *temp_chip = fpga_first_inode;
185 struct fpga_internal *temp_int;
186
187 /*
188 * Search of the last fpga CI chip or
189 * find it by demux
190 */
191 while (temp_chip != NULL) {
192 if (temp_chip->internal != NULL) {
193 temp_int = temp_chip->internal;
194 if (check_filter(temp_int, demux_dev, 0))
195 break;
196 if (check_filter(temp_int, demux_dev, 1))
197 break;
198 }
199
200 temp_chip = temp_chip->next_inode;
201 }
202
203 return temp_chip;
204}
205
206/* deallocating chip */
207static void remove_inode(struct fpga_internal *internal)
208{
209 struct fpga_inode *prev_node = fpga_first_inode;
210 struct fpga_inode *del_node = find_inode(internal->dev);
211
212 if (del_node != NULL) {
213 if (del_node == fpga_first_inode) {
214 fpga_first_inode = del_node->next_inode;
215 } else {
216 while (prev_node->next_inode != del_node)
217 prev_node = prev_node->next_inode;
218
219 if (del_node->next_inode == NULL)
220 prev_node->next_inode = NULL;
221 else
222 prev_node->next_inode =
223 prev_node->next_inode->next_inode;
224 }
225
226 kfree(del_node);
227 }
228}
229
230/* allocating new chip */
231static struct fpga_inode *append_internal(struct fpga_internal *internal)
232{
233 struct fpga_inode *new_node = fpga_first_inode;
234
235 if (new_node == NULL) {
236 new_node = kmalloc(sizeof(struct fpga_inode), GFP_KERNEL);
237 fpga_first_inode = new_node;
238 } else {
239 while (new_node->next_inode != NULL)
240 new_node = new_node->next_inode;
241
242 new_node->next_inode =
243 kmalloc(sizeof(struct fpga_inode), GFP_KERNEL);
244 if (new_node->next_inode != NULL)
245 new_node = new_node->next_inode;
246 else
247 new_node = NULL;
248 }
249
250 if (new_node != NULL) {
251 new_node->internal = internal;
252 new_node->next_inode = NULL;
253 }
254
255 return new_node;
256}
257
258static int netup_fpga_op_rw(struct fpga_internal *inter, int addr,
259 u8 val, u8 read)
260{
261 inter->fpga_rw(inter->dev, NETUP_CI_FLG_AD, addr, 0);
262 return inter->fpga_rw(inter->dev, 0, val, read);
263}
264
265/* flag - mem/io, read - read/write */
266int altera_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot,
267 u8 flag, u8 read, int addr, u8 val)
268{
269
270 struct altera_ci_state *state = en50221->data;
271 struct fpga_internal *inter = state->internal;
272
273 u8 store;
274 int mem = 0;
275
276 if (0 != slot)
277 return -EINVAL;
278
279 mutex_lock(&inter->fpga_mutex);
280
281 netup_fpga_op_rw(inter, NETUP_CI_ADDR0, ((addr << 1) & 0xfe), 0);
282 netup_fpga_op_rw(inter, NETUP_CI_ADDR1, ((addr >> 7) & 0x7f), 0);
283 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
284
285 store &= 0x0f;
286 store |= ((state->nr << 7) | (flag << 6));
287
288 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, store, 0);
289 mem = netup_fpga_op_rw(inter, NETUP_CI_DATA, val, read);
290
291 mutex_unlock(&inter->fpga_mutex);
292
293 ci_dbg_print("%s: %s: addr=[0x%02x], %s=%x\n", __func__,
294 (read) ? "read" : "write", addr,
295 (flag == NETUP_CI_FLG_CTL) ? "ctl" : "mem",
296 (read) ? mem : val);
297
298 return mem;
299}
300
301int altera_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
302 int slot, int addr)
303{
304 return altera_ci_op_cam(en50221, slot, 0, NETUP_CI_FLG_RD, addr, 0);
305}
306
307int altera_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
308 int slot, int addr, u8 data)
309{
310 return altera_ci_op_cam(en50221, slot, 0, 0, addr, data);
311}
312
313int altera_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, u8 addr)
314{
315 return altera_ci_op_cam(en50221, slot, NETUP_CI_FLG_CTL,
316 NETUP_CI_FLG_RD, addr, 0);
317}
318
319int altera_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot,
320 u8 addr, u8 data)
321{
322 return altera_ci_op_cam(en50221, slot, NETUP_CI_FLG_CTL, 0, addr, data);
323}
324
325int altera_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
326{
327 struct altera_ci_state *state = en50221->data;
328 struct fpga_internal *inter = state->internal;
329 /* reasonable timeout for CI reset is 10 seconds */
330 unsigned long t_out = jiffies + msecs_to_jiffies(9999);
331 int ret;
332
333 ci_dbg_print("%s\n", __func__);
334
335 if (0 != slot)
336 return -EINVAL;
337
338 mutex_lock(&inter->fpga_mutex);
339
340 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
341 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
342 (ret & 0xcf) | (1 << (5 - state->nr)), 0);
343
344 mutex_unlock(&inter->fpga_mutex);
345
346 for (;;) {
347 mdelay(50);
348
349 mutex_lock(&inter->fpga_mutex);
350
351 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
352 0, NETUP_CI_FLG_RD);
353 mutex_unlock(&inter->fpga_mutex);
354
355 if ((ret & (1 << (5 - state->nr))) == 0)
356 break;
357 if (time_after(jiffies, t_out))
358 break;
359 }
360
361
362 ci_dbg_print("%s: %d msecs\n", __func__,
363 jiffies_to_msecs(jiffies + msecs_to_jiffies(9999) - t_out));
364
365 return 0;
366}
367
368int altera_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
369{
370 /* not implemented */
371 return 0;
372}
373
374int altera_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot)
375{
376 struct altera_ci_state *state = en50221->data;
377 struct fpga_internal *inter = state->internal;
378 int ret;
379
380 ci_dbg_print("%s\n", __func__);
381
382 if (0 != slot)
383 return -EINVAL;
384
385 mutex_lock(&inter->fpga_mutex);
386
387 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
388 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL,
389 (ret & 0x0f) | (1 << (3 - state->nr)), 0);
390
391 mutex_unlock(&inter->fpga_mutex);
392
393 return 0;
394}
395
396/* work handler */
397static void netup_read_ci_status(struct work_struct *work)
398{
399 struct fpga_internal *inter =
400 container_of(work, struct fpga_internal, work);
401 int ret;
402
403 ci_dbg_print("%s\n", __func__);
404
405 mutex_lock(&inter->fpga_mutex);
406 /* ack' irq */
407 ret = netup_fpga_op_rw(inter, NETUP_CI_INT_CTRL, 0, NETUP_CI_FLG_RD);
408 ret = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL, 0, NETUP_CI_FLG_RD);
409
410 mutex_unlock(&inter->fpga_mutex);
411
412 if (inter->state[1] != NULL) {
413 inter->state[1]->status =
414 ((ret & 1) == 0 ?
415 DVB_CA_EN50221_POLL_CAM_PRESENT |
416 DVB_CA_EN50221_POLL_CAM_READY : 0);
417 ci_dbg_print("%s: setting CI[1] status = 0x%x\n",
418 __func__, inter->state[1]->status);
419 };
420
421 if (inter->state[0] != NULL) {
422 inter->state[0]->status =
423 ((ret & 2) == 0 ?
424 DVB_CA_EN50221_POLL_CAM_PRESENT |
425 DVB_CA_EN50221_POLL_CAM_READY : 0);
426 ci_dbg_print("%s: setting CI[0] status = 0x%x\n",
427 __func__, inter->state[0]->status);
428 };
429}
430
431/* CI irq handler */
432int altera_ci_irq(void *dev)
433{
434 struct fpga_inode *temp_int = NULL;
435 struct fpga_internal *inter = NULL;
436
437 ci_dbg_print("%s\n", __func__);
438
439 if (dev != NULL) {
440 temp_int = find_inode(dev);
441 if (temp_int != NULL) {
442 inter = temp_int->internal;
443 schedule_work(&inter->work);
444 }
445 }
446
447 return 1;
448}
449EXPORT_SYMBOL(altera_ci_irq);
450
451int altera_poll_ci_slot_status(struct dvb_ca_en50221 *en50221, int slot,
452 int open)
453{
454 struct altera_ci_state *state = en50221->data;
455
456 if (0 != slot)
457 return -EINVAL;
458
459 return state->status;
460}
461
462void altera_hw_filt_release(void *main_dev, int filt_nr)
463{
464 struct fpga_inode *temp_int = find_inode(main_dev);
465 struct netup_hw_pid_filter *pid_filt = NULL;
466
467 ci_dbg_print("%s\n", __func__);
468
469 if (temp_int != NULL) {
470 pid_filt = temp_int->internal->pid_filt[filt_nr - 1];
471 /* stored old feed controls */
472 pid_filt->demux->start_feed = pid_filt->start_feed;
473 pid_filt->demux->stop_feed = pid_filt->stop_feed;
474
475 if (((--(temp_int->internal->filts_used)) <= 0) &&
476 ((temp_int->internal->cis_used) <= 0)) {
477
478 ci_dbg_print("%s: Actually removing\n", __func__);
479
480 remove_inode(temp_int->internal);
481 kfree(pid_filt->internal);
482 }
483
484 kfree(pid_filt);
485
486 }
487
488}
489EXPORT_SYMBOL(altera_hw_filt_release);
490
491void altera_ci_release(void *dev, int ci_nr)
492{
493 struct fpga_inode *temp_int = find_inode(dev);
494 struct altera_ci_state *state = NULL;
495
496 ci_dbg_print("%s\n", __func__);
497
498 if (temp_int != NULL) {
499 state = temp_int->internal->state[ci_nr - 1];
500 altera_hw_filt_release(dev, ci_nr);
501
502
503 if (((temp_int->internal->filts_used) <= 0) &&
504 ((--(temp_int->internal->cis_used)) <= 0)) {
505
506 ci_dbg_print("%s: Actually removing\n", __func__);
507
508 remove_inode(temp_int->internal);
509 kfree(state->internal);
510 }
511
512 if (state != NULL) {
513 if (state->ca.data != NULL)
514 dvb_ca_en50221_release(&state->ca);
515
516 kfree(state);
517 }
518 }
519
520}
521EXPORT_SYMBOL(altera_ci_release);
522
523static void altera_pid_control(struct netup_hw_pid_filter *pid_filt,
524 u16 pid, int onoff)
525{
526 struct fpga_internal *inter = pid_filt->internal;
527 u8 store = 0;
528
529 /* pid 0-0x1f always enabled, don't touch them */
530 if ((pid == 0x2000) || (pid < 0x20))
531 return;
532
533 mutex_lock(&inter->fpga_mutex);
534
535 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR0, (pid >> 3) & 0xff, 0);
536 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR1,
537 ((pid >> 11) & 0x03) | (pid_filt->nr << 2), 0);
538
539 store = netup_fpga_op_rw(inter, NETUP_CI_PID_DATA, 0, NETUP_CI_FLG_RD);
540
541 if (onoff)/* 0 - on, 1 - off */
542 store |= (1 << (pid & 7));
543 else
544 store &= ~(1 << (pid & 7));
545
546 netup_fpga_op_rw(inter, NETUP_CI_PID_DATA, store, 0);
547
548 mutex_unlock(&inter->fpga_mutex);
549
550 pid_dbg_print("%s: (%d) set pid: %5d 0x%04x '%s'\n", __func__,
551 pid_filt->nr, pid, pid, onoff ? "off" : "on");
552}
553
554static void altera_toggle_fullts_streaming(struct netup_hw_pid_filter *pid_filt,
555 int filt_nr, int onoff)
556{
557 struct fpga_internal *inter = pid_filt->internal;
558 u8 store = 0;
559 int i;
560
561 pid_dbg_print("%s: pid_filt->nr[%d] now %s\n", __func__, pid_filt->nr,
562 onoff ? "off" : "on");
563
564 if (onoff)/* 0 - on, 1 - off */
565 store = 0xff;/* ignore pid */
566 else
567 store = 0;/* enable pid */
568
569 mutex_lock(&inter->fpga_mutex);
570
571 for (i = 0; i < 1024; i++) {
572 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR0, i & 0xff, 0);
573
574 netup_fpga_op_rw(inter, NETUP_CI_PID_ADDR1,
575 ((i >> 8) & 0x03) | (pid_filt->nr << 2), 0);
576 /* pid 0-0x1f always enabled */
577 netup_fpga_op_rw(inter, NETUP_CI_PID_DATA,
578 (i > 3 ? store : 0), 0);
579 }
580
581 mutex_unlock(&inter->fpga_mutex);
582}
583
584int altera_pid_feed_control(void *demux_dev, int filt_nr,
585 struct dvb_demux_feed *feed, int onoff)
586{
587 struct fpga_inode *temp_int = find_dinode(demux_dev);
588 struct fpga_internal *inter = temp_int->internal;
589 struct netup_hw_pid_filter *pid_filt = inter->pid_filt[filt_nr - 1];
590
591 altera_pid_control(pid_filt, feed->pid, onoff ? 0 : 1);
592 /* call old feed proc's */
593 if (onoff)
594 pid_filt->start_feed(feed);
595 else
596 pid_filt->stop_feed(feed);
597
598 if (feed->pid == 0x2000)
599 altera_toggle_fullts_streaming(pid_filt, filt_nr,
600 onoff ? 0 : 1);
601
602 return 0;
603}
604EXPORT_SYMBOL(altera_pid_feed_control);
605
606int altera_ci_start_feed(struct dvb_demux_feed *feed, int num)
607{
608 altera_pid_feed_control(feed->demux, num, feed, 1);
609
610 return 0;
611}
612
613int altera_ci_stop_feed(struct dvb_demux_feed *feed, int num)
614{
615 altera_pid_feed_control(feed->demux, num, feed, 0);
616
617 return 0;
618}
619
620int altera_ci_start_feed_1(struct dvb_demux_feed *feed)
621{
622 return altera_ci_start_feed(feed, 1);
623}
624
625int altera_ci_stop_feed_1(struct dvb_demux_feed *feed)
626{
627 return altera_ci_stop_feed(feed, 1);
628}
629
630int altera_ci_start_feed_2(struct dvb_demux_feed *feed)
631{
632 return altera_ci_start_feed(feed, 2);
633}
634
635int altera_ci_stop_feed_2(struct dvb_demux_feed *feed)
636{
637 return altera_ci_stop_feed(feed, 2);
638}
639
640int altera_hw_filt_init(struct altera_ci_config *config, int hw_filt_nr)
641{
642 struct netup_hw_pid_filter *pid_filt = NULL;
643 struct fpga_inode *temp_int = find_inode(config->dev);
644 struct fpga_internal *inter = NULL;
645 int ret = 0;
646
647 pid_filt = kzalloc(sizeof(struct netup_hw_pid_filter), GFP_KERNEL);
648
649 ci_dbg_print("%s\n", __func__);
650
651 if (!pid_filt) {
652 ret = -ENOMEM;
653 goto err;
654 }
655
656 if (temp_int != NULL) {
657 inter = temp_int->internal;
658 (inter->filts_used)++;
659 ci_dbg_print("%s: Find Internal Structure!\n", __func__);
660 } else {
661 inter = kzalloc(sizeof(struct fpga_internal), GFP_KERNEL);
662 if (!inter) {
663 ret = -ENOMEM;
664 goto err;
665 }
666
667 temp_int = append_internal(inter);
668 inter->filts_used = 1;
669 inter->dev = config->dev;
670 inter->fpga_rw = config->fpga_rw;
671 mutex_init(&inter->fpga_mutex);
672 inter->strt_wrk = 1;
673 ci_dbg_print("%s: Create New Internal Structure!\n", __func__);
674 }
675
676 ci_dbg_print("%s: setting hw pid filter = %p for ci = %d\n", __func__,
677 pid_filt, hw_filt_nr - 1);
678 inter->pid_filt[hw_filt_nr - 1] = pid_filt;
679 pid_filt->demux = config->demux;
680 pid_filt->internal = inter;
681 pid_filt->nr = hw_filt_nr - 1;
682 /* store old feed controls */
683 pid_filt->start_feed = config->demux->start_feed;
684 pid_filt->stop_feed = config->demux->stop_feed;
685 /* replace with new feed controls */
686 if (hw_filt_nr == 1) {
687 pid_filt->demux->start_feed = altera_ci_start_feed_1;
688 pid_filt->demux->stop_feed = altera_ci_stop_feed_1;
689 } else if (hw_filt_nr == 2) {
690 pid_filt->demux->start_feed = altera_ci_start_feed_2;
691 pid_filt->demux->stop_feed = altera_ci_stop_feed_2;
692 }
693
694 altera_toggle_fullts_streaming(pid_filt, 0, 1);
695
696 return 0;
697err:
698 ci_dbg_print("%s: Can't init hardware filter: Error %d\n",
699 __func__, ret);
700
701 kfree(pid_filt);
702
703 return ret;
704}
705EXPORT_SYMBOL(altera_hw_filt_init);
706
707int altera_ci_init(struct altera_ci_config *config, int ci_nr)
708{
709 struct altera_ci_state *state;
710 struct fpga_inode *temp_int = find_inode(config->dev);
711 struct fpga_internal *inter = NULL;
712 int ret = 0;
713 u8 store = 0;
714
715 state = kzalloc(sizeof(struct altera_ci_state), GFP_KERNEL);
716
717 ci_dbg_print("%s\n", __func__);
718
719 if (!state) {
720 ret = -ENOMEM;
721 goto err;
722 }
723
724 if (temp_int != NULL) {
725 inter = temp_int->internal;
726 (inter->cis_used)++;
727 ci_dbg_print("%s: Find Internal Structure!\n", __func__);
728 } else {
729 inter = kzalloc(sizeof(struct fpga_internal), GFP_KERNEL);
730 if (!inter) {
731 ret = -ENOMEM;
732 goto err;
733 }
734
735 temp_int = append_internal(inter);
736 inter->cis_used = 1;
737 inter->dev = config->dev;
738 inter->fpga_rw = config->fpga_rw;
739 mutex_init(&inter->fpga_mutex);
740 inter->strt_wrk = 1;
741 ci_dbg_print("%s: Create New Internal Structure!\n", __func__);
742 }
743
744 ci_dbg_print("%s: setting state = %p for ci = %d\n", __func__,
745 state, ci_nr - 1);
746 inter->state[ci_nr - 1] = state;
747 state->internal = inter;
748 state->nr = ci_nr - 1;
749
750 state->ca.owner = THIS_MODULE;
751 state->ca.read_attribute_mem = altera_ci_read_attribute_mem;
752 state->ca.write_attribute_mem = altera_ci_write_attribute_mem;
753 state->ca.read_cam_control = altera_ci_read_cam_ctl;
754 state->ca.write_cam_control = altera_ci_write_cam_ctl;
755 state->ca.slot_reset = altera_ci_slot_reset;
756 state->ca.slot_shutdown = altera_ci_slot_shutdown;
757 state->ca.slot_ts_enable = altera_ci_slot_ts_ctl;
758 state->ca.poll_slot_status = altera_poll_ci_slot_status;
759 state->ca.data = state;
760
761 ret = dvb_ca_en50221_init(config->adapter,
762 &state->ca,
763 /* flags */ 0,
764 /* n_slots */ 1);
765 if (0 != ret)
766 goto err;
767
768 altera_hw_filt_init(config, ci_nr);
769
770 if (inter->strt_wrk) {
771 INIT_WORK(&inter->work, netup_read_ci_status);
772 inter->strt_wrk = 0;
773 }
774
775 ci_dbg_print("%s: CI initialized!\n", __func__);
776
777 mutex_lock(&inter->fpga_mutex);
778
779 /* Enable div */
780 netup_fpga_op_rw(inter, NETUP_CI_TSA_DIV, 0x0, 0);
781 netup_fpga_op_rw(inter, NETUP_CI_TSB_DIV, 0x0, 0);
782
783 /* enable TS out */
784 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, 0, NETUP_CI_FLG_RD);
785 store |= (3 << 4);
786 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
787
788 ret = netup_fpga_op_rw(inter, NETUP_CI_REVISION, 0, NETUP_CI_FLG_RD);
789 /* enable irq */
790 netup_fpga_op_rw(inter, NETUP_CI_INT_CTRL, 0x44, 0);
791
792 mutex_unlock(&inter->fpga_mutex);
793
794 ci_dbg_print("%s: NetUP CI Revision = 0x%x\n", __func__, ret);
795
796 schedule_work(&inter->work);
797
798 return 0;
799err:
800 ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__, ret);
801
802 kfree(state);
803
804 return ret;
805}
806EXPORT_SYMBOL(altera_ci_init);
807
808int altera_ci_tuner_reset(void *dev, int ci_nr)
809{
810 struct fpga_inode *temp_int = find_inode(dev);
811 struct fpga_internal *inter = NULL;
812 u8 store;
813
814 ci_dbg_print("%s\n", __func__);
815
816 if (temp_int == NULL)
817 return -1;
818
819 if (temp_int->internal == NULL)
820 return -1;
821
822 inter = temp_int->internal;
823
824 mutex_lock(&inter->fpga_mutex);
825
826 store = netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, 0, NETUP_CI_FLG_RD);
827 store &= ~(4 << (2 - ci_nr));
828 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
829 msleep(100);
830 store |= (4 << (2 - ci_nr));
831 netup_fpga_op_rw(inter, NETUP_CI_BUSCTRL2, store, 0);
832
833 mutex_unlock(&inter->fpga_mutex);
834
835 return 0;
836}
837EXPORT_SYMBOL(altera_ci_tuner_reset);
diff --git a/drivers/media/pci/cx23885/altera-ci.h b/drivers/media/pci/cx23885/altera-ci.h
new file mode 100644
index 000000000000..70e4fd69ad9e
--- /dev/null
+++ b/drivers/media/pci/cx23885/altera-ci.h
@@ -0,0 +1,100 @@
1/*
2 * altera-ci.c
3 *
4 * CI driver in conjunction with NetUp Dual DVB-T/C RF CI card
5 *
6 * Copyright (C) 2010 NetUP Inc.
7 * Copyright (C) 2010 Igor M. Liplianin <liplianin@netup.ru>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 *
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24#ifndef __ALTERA_CI_H
25#define __ALTERA_CI_H
26
27#define ALT_DATA 0x000000ff
28#define ALT_TDI 0x00008000
29#define ALT_TDO 0x00004000
30#define ALT_TCK 0x00002000
31#define ALT_RDY 0x00001000
32#define ALT_RD 0x00000800
33#define ALT_WR 0x00000400
34#define ALT_AD_RG 0x00000200
35#define ALT_CS 0x00000100
36
37struct altera_ci_config {
38 void *dev;/* main dev, for example cx23885_dev */
39 void *adapter;/* for CI to connect to */
40 struct dvb_demux *demux;/* for hardware PID filter to connect to */
41 int (*fpga_rw) (void *dev, int ad_rg, int val, int rw);
42};
43
44#if defined(CONFIG_MEDIA_ALTERA_CI) || (defined(CONFIG_MEDIA_ALTERA_CI_MODULE) \
45 && defined(MODULE))
46
47extern int altera_ci_init(struct altera_ci_config *config, int ci_nr);
48extern void altera_ci_release(void *dev, int ci_nr);
49extern int altera_ci_irq(void *dev);
50extern int altera_ci_tuner_reset(void *dev, int ci_nr);
51
52#else
53
54static inline int altera_ci_init(struct altera_ci_config *config, int ci_nr)
55{
56 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);
57 return 0;
58}
59
60static inline void altera_ci_release(void *dev, int ci_nr)
61{
62 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);
63}
64
65static inline int altera_ci_irq(void *dev)
66{
67 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);
68 return 0;
69}
70
71static inline int altera_ci_tuner_reset(void *dev, int ci_nr)
72{
73 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);
74 return 0;
75}
76
77#endif
78#if 0
79static inline int altera_hw_filt_init(struct altera_ci_config *config,
80 int hw_filt_nr)
81{
82 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);
83 return 0;
84}
85
86static inline void altera_hw_filt_release(void *dev, int filt_nr)
87{
88 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);
89}
90
91static inline int altera_pid_feed_control(void *dev, int filt_nr,
92 struct dvb_demux_feed *dvbdmxfeed, int onoff)
93{
94 printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);
95 return 0;
96}
97
98#endif /* CONFIG_MEDIA_ALTERA_CI */
99
100#endif /* __ALTERA_CI_H */
diff --git a/drivers/media/pci/cx23885/cimax2.c b/drivers/media/pci/cx23885/cimax2.c
new file mode 100644
index 000000000000..c9f15d6dec40
--- /dev/null
+++ b/drivers/media/pci/cx23885/cimax2.c
@@ -0,0 +1,536 @@
1/*
2 * cimax2.c
3 *
4 * CIMax2(R) SP2 driver in conjunction with NetUp Dual DVB-S2 CI card
5 *
6 * Copyright (C) 2009 NetUP Inc.
7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
8 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 *
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include "cx23885.h"
27#include "dvb_ca_en50221.h"
28/**** Bit definitions for MC417_RWD and MC417_OEN registers ***
29 bits 31-16
30+-----------+
31| Reserved |
32+-----------+
33 bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
34+-------+-------+-------+-------+-------+-------+-------+-------+
35| WR# | RD# | | ACK# | ADHI | ADLO | CS1# | CS0# |
36+-------+-------+-------+-------+-------+-------+-------+-------+
37 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
38+-------+-------+-------+-------+-------+-------+-------+-------+
39| DATA7| DATA6| DATA5| DATA4| DATA3| DATA2| DATA1| DATA0|
40+-------+-------+-------+-------+-------+-------+-------+-------+
41***/
42/* MC417 */
43#define NETUP_DATA 0x000000ff
44#define NETUP_WR 0x00008000
45#define NETUP_RD 0x00004000
46#define NETUP_ACK 0x00001000
47#define NETUP_ADHI 0x00000800
48#define NETUP_ADLO 0x00000400
49#define NETUP_CS1 0x00000200
50#define NETUP_CS0 0x00000100
51#define NETUP_EN_ALL 0x00001000
52#define NETUP_CTRL_OFF (NETUP_CS1 | NETUP_CS0 | NETUP_WR | NETUP_RD)
53#define NETUP_CI_CTL 0x04
54#define NETUP_CI_RD 1
55
56#define NETUP_IRQ_DETAM 0x1
57#define NETUP_IRQ_IRQAM 0x4
58
59static unsigned int ci_dbg;
60module_param(ci_dbg, int, 0644);
61MODULE_PARM_DESC(ci_dbg, "Enable CI debugging");
62
63static unsigned int ci_irq_enable;
64module_param(ci_irq_enable, int, 0644);
65MODULE_PARM_DESC(ci_irq_enable, "Enable IRQ from CAM");
66
67#define ci_dbg_print(args...) \
68 do { \
69 if (ci_dbg) \
70 printk(KERN_DEBUG args); \
71 } while (0)
72
73#define ci_irq_flags() (ci_irq_enable ? NETUP_IRQ_IRQAM : 0)
74
75/* stores all private variables for communication with CI */
76struct netup_ci_state {
77 struct dvb_ca_en50221 ca;
78 struct mutex ca_mutex;
79 struct i2c_adapter *i2c_adap;
80 u8 ci_i2c_addr;
81 int status;
82 struct work_struct work;
83 void *priv;
84 u8 current_irq_mode;
85 int current_ci_flag;
86 unsigned long next_status_checked_time;
87};
88
89
90int netup_read_i2c(struct i2c_adapter *i2c_adap, u8 addr, u8 reg,
91 u8 *buf, int len)
92{
93 int ret;
94 struct i2c_msg msg[] = {
95 {
96 .addr = addr,
97 .flags = 0,
98 .buf = &reg,
99 .len = 1
100 }, {
101 .addr = addr,
102 .flags = I2C_M_RD,
103 .buf = buf,
104 .len = len
105 }
106 };
107
108 ret = i2c_transfer(i2c_adap, msg, 2);
109
110 if (ret != 2) {
111 ci_dbg_print("%s: i2c read error, Reg = 0x%02x, Status = %d\n",
112 __func__, reg, ret);
113
114 return -1;
115 }
116
117 ci_dbg_print("%s: i2c read Addr=0x%04x, Reg = 0x%02x, data = %02x\n",
118 __func__, addr, reg, buf[0]);
119
120 return 0;
121}
122
123int netup_write_i2c(struct i2c_adapter *i2c_adap, u8 addr, u8 reg,
124 u8 *buf, int len)
125{
126 int ret;
127 u8 buffer[len + 1];
128
129 struct i2c_msg msg = {
130 .addr = addr,
131 .flags = 0,
132 .buf = &buffer[0],
133 .len = len + 1
134 };
135
136 buffer[0] = reg;
137 memcpy(&buffer[1], buf, len);
138
139 ret = i2c_transfer(i2c_adap, &msg, 1);
140
141 if (ret != 1) {
142 ci_dbg_print("%s: i2c write error, Reg=[0x%02x], Status=%d\n",
143 __func__, reg, ret);
144 return -1;
145 }
146
147 return 0;
148}
149
150int netup_ci_get_mem(struct cx23885_dev *dev)
151{
152 int mem;
153 unsigned long timeout = jiffies + msecs_to_jiffies(1);
154
155 for (;;) {
156 mem = cx_read(MC417_RWD);
157 if ((mem & NETUP_ACK) == 0)
158 break;
159 if (time_after(jiffies, timeout))
160 break;
161 udelay(1);
162 }
163
164 cx_set(MC417_RWD, NETUP_CTRL_OFF);
165
166 return mem & 0xff;
167}
168
169int netup_ci_op_cam(struct dvb_ca_en50221 *en50221, int slot,
170 u8 flag, u8 read, int addr, u8 data)
171{
172 struct netup_ci_state *state = en50221->data;
173 struct cx23885_tsport *port = state->priv;
174 struct cx23885_dev *dev = port->dev;
175
176 u8 store;
177 int mem;
178 int ret;
179
180 if (0 != slot)
181 return -EINVAL;
182
183 if (state->current_ci_flag != flag) {
184 ret = netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,
185 0, &store, 1);
186 if (ret != 0)
187 return ret;
188
189 store &= ~0x0c;
190 store |= flag;
191
192 ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
193 0, &store, 1);
194 if (ret != 0)
195 return ret;
196 };
197 state->current_ci_flag = flag;
198
199 mutex_lock(&dev->gpio_lock);
200
201 /* write addr */
202 cx_write(MC417_OEN, NETUP_EN_ALL);
203 cx_write(MC417_RWD, NETUP_CTRL_OFF |
204 NETUP_ADLO | (0xff & addr));
205 cx_clear(MC417_RWD, NETUP_ADLO);
206 cx_write(MC417_RWD, NETUP_CTRL_OFF |
207 NETUP_ADHI | (0xff & (addr >> 8)));
208 cx_clear(MC417_RWD, NETUP_ADHI);
209
210 if (read) { /* data in */
211 cx_write(MC417_OEN, NETUP_EN_ALL | NETUP_DATA);
212 } else /* data out */
213 cx_write(MC417_RWD, NETUP_CTRL_OFF | data);
214
215 /* choose chip */
216 cx_clear(MC417_RWD,
217 (state->ci_i2c_addr == 0x40) ? NETUP_CS0 : NETUP_CS1);
218 /* read/write */
219 cx_clear(MC417_RWD, (read) ? NETUP_RD : NETUP_WR);
220 mem = netup_ci_get_mem(dev);
221
222 mutex_unlock(&dev->gpio_lock);
223
224 if (!read)
225 if (mem < 0)
226 return -EREMOTEIO;
227
228 ci_dbg_print("%s: %s: chipaddr=[0x%x] addr=[0x%02x], %s=%x\n", __func__,
229 (read) ? "read" : "write", state->ci_i2c_addr, addr,
230 (flag == NETUP_CI_CTL) ? "ctl" : "mem",
231 (read) ? mem : data);
232
233 if (read)
234 return mem;
235
236 return 0;
237}
238
239int netup_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
240 int slot, int addr)
241{
242 return netup_ci_op_cam(en50221, slot, 0, NETUP_CI_RD, addr, 0);
243}
244
245int netup_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
246 int slot, int addr, u8 data)
247{
248 return netup_ci_op_cam(en50221, slot, 0, 0, addr, data);
249}
250
251int netup_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221, int slot, u8 addr)
252{
253 return netup_ci_op_cam(en50221, slot, NETUP_CI_CTL,
254 NETUP_CI_RD, addr, 0);
255}
256
257int netup_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221, int slot,
258 u8 addr, u8 data)
259{
260 return netup_ci_op_cam(en50221, slot, NETUP_CI_CTL, 0, addr, data);
261}
262
263int netup_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot)
264{
265 struct netup_ci_state *state = en50221->data;
266 u8 buf = 0x80;
267 int ret;
268
269 if (0 != slot)
270 return -EINVAL;
271
272 udelay(500);
273 ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
274 0, &buf, 1);
275
276 if (ret != 0)
277 return ret;
278
279 udelay(500);
280
281 buf = 0x00;
282 ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
283 0, &buf, 1);
284
285 msleep(1000);
286 dvb_ca_en50221_camready_irq(&state->ca, 0);
287
288 return 0;
289
290}
291
292int netup_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot)
293{
294 /* not implemented */
295 return 0;
296}
297
298int netup_ci_set_irq(struct dvb_ca_en50221 *en50221, u8 irq_mode)
299{
300 struct netup_ci_state *state = en50221->data;
301 int ret;
302
303 if (irq_mode == state->current_irq_mode)
304 return 0;
305
306 ci_dbg_print("%s: chipaddr=[0x%x] setting ci IRQ to [0x%x] \n",
307 __func__, state->ci_i2c_addr, irq_mode);
308 ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
309 0x1b, &irq_mode, 1);
310
311 if (ret != 0)
312 return ret;
313
314 state->current_irq_mode = irq_mode;
315
316 return 0;
317}
318
319int netup_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot)
320{
321 struct netup_ci_state *state = en50221->data;
322 u8 buf;
323
324 if (0 != slot)
325 return -EINVAL;
326
327 netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,
328 0, &buf, 1);
329 buf |= 0x60;
330
331 return netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
332 0, &buf, 1);
333}
334
335/* work handler */
336static void netup_read_ci_status(struct work_struct *work)
337{
338 struct netup_ci_state *state =
339 container_of(work, struct netup_ci_state, work);
340 u8 buf[33];
341 int ret;
342
343 /* CAM module IRQ processing. fast operation */
344 dvb_ca_en50221_frda_irq(&state->ca, 0);
345
346 /* CAM module INSERT/REMOVE processing. slow operation because of i2c
347 * transfers */
348 if (time_after(jiffies, state->next_status_checked_time)
349 || !state->status) {
350 ret = netup_read_i2c(state->i2c_adap, state->ci_i2c_addr,
351 0, &buf[0], 33);
352
353 state->next_status_checked_time = jiffies
354 + msecs_to_jiffies(1000);
355
356 if (ret != 0)
357 return;
358
359 ci_dbg_print("%s: Slot Status Addr=[0x%04x], "
360 "Reg=[0x%02x], data=%02x, "
361 "TS config = %02x\n", __func__,
362 state->ci_i2c_addr, 0, buf[0],
363 buf[0]);
364
365
366 if (buf[0] & 1)
367 state->status = DVB_CA_EN50221_POLL_CAM_PRESENT |
368 DVB_CA_EN50221_POLL_CAM_READY;
369 else
370 state->status = 0;
371 }
372}
373
374/* CI irq handler */
375int netup_ci_slot_status(struct cx23885_dev *dev, u32 pci_status)
376{
377 struct cx23885_tsport *port = NULL;
378 struct netup_ci_state *state = NULL;
379
380 ci_dbg_print("%s:\n", __func__);
381
382 if (0 == (pci_status & (PCI_MSK_GPIO0 | PCI_MSK_GPIO1)))
383 return 0;
384
385 if (pci_status & PCI_MSK_GPIO0) {
386 port = &dev->ts1;
387 state = port->port_priv;
388 schedule_work(&state->work);
389 ci_dbg_print("%s: Wakeup CI0\n", __func__);
390 }
391
392 if (pci_status & PCI_MSK_GPIO1) {
393 port = &dev->ts2;
394 state = port->port_priv;
395 schedule_work(&state->work);
396 ci_dbg_print("%s: Wakeup CI1\n", __func__);
397 }
398
399 return 1;
400}
401
402int netup_poll_ci_slot_status(struct dvb_ca_en50221 *en50221, int slot, int open)
403{
404 struct netup_ci_state *state = en50221->data;
405
406 if (0 != slot)
407 return -EINVAL;
408
409 netup_ci_set_irq(en50221, open ? (NETUP_IRQ_DETAM | ci_irq_flags())
410 : NETUP_IRQ_DETAM);
411
412 return state->status;
413}
414
415int netup_ci_init(struct cx23885_tsport *port)
416{
417 struct netup_ci_state *state;
418 u8 cimax_init[34] = {
419 0x00, /* module A control*/
420 0x00, /* auto select mask high A */
421 0x00, /* auto select mask low A */
422 0x00, /* auto select pattern high A */
423 0x00, /* auto select pattern low A */
424 0x44, /* memory access time A */
425 0x00, /* invert input A */
426 0x00, /* RFU */
427 0x00, /* RFU */
428 0x00, /* module B control*/
429 0x00, /* auto select mask high B */
430 0x00, /* auto select mask low B */
431 0x00, /* auto select pattern high B */
432 0x00, /* auto select pattern low B */
433 0x44, /* memory access time B */
434 0x00, /* invert input B */
435 0x00, /* RFU */
436 0x00, /* RFU */
437 0x00, /* auto select mask high Ext */
438 0x00, /* auto select mask low Ext */
439 0x00, /* auto select pattern high Ext */
440 0x00, /* auto select pattern low Ext */
441 0x00, /* RFU */
442 0x02, /* destination - module A */
443 0x01, /* power on (use it like store place) */
444 0x00, /* RFU */
445 0x00, /* int status read only */
446 ci_irq_flags() | NETUP_IRQ_DETAM, /* DETAM, IRQAM unmasked */
447 0x05, /* EXTINT=active-high, INT=push-pull */
448 0x00, /* USCG1 */
449 0x04, /* ack active low */
450 0x00, /* LOCK = 0 */
451 0x33, /* serial mode, rising in, rising out, MSB first*/
452 0x31, /* synchronization */
453 };
454 int ret;
455
456 ci_dbg_print("%s\n", __func__);
457 state = kzalloc(sizeof(struct netup_ci_state), GFP_KERNEL);
458 if (!state) {
459 ci_dbg_print("%s: Unable create CI structure!\n", __func__);
460 ret = -ENOMEM;
461 goto err;
462 }
463
464 port->port_priv = state;
465
466 switch (port->nr) {
467 case 1:
468 state->ci_i2c_addr = 0x40;
469 break;
470 case 2:
471 state->ci_i2c_addr = 0x41;
472 break;
473 }
474
475 state->i2c_adap = &port->dev->i2c_bus[0].i2c_adap;
476 state->ca.owner = THIS_MODULE;
477 state->ca.read_attribute_mem = netup_ci_read_attribute_mem;
478 state->ca.write_attribute_mem = netup_ci_write_attribute_mem;
479 state->ca.read_cam_control = netup_ci_read_cam_ctl;
480 state->ca.write_cam_control = netup_ci_write_cam_ctl;
481 state->ca.slot_reset = netup_ci_slot_reset;
482 state->ca.slot_shutdown = netup_ci_slot_shutdown;
483 state->ca.slot_ts_enable = netup_ci_slot_ts_ctl;
484 state->ca.poll_slot_status = netup_poll_ci_slot_status;
485 state->ca.data = state;
486 state->priv = port;
487 state->current_irq_mode = ci_irq_flags() | NETUP_IRQ_DETAM;
488
489 ret = netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
490 0, &cimax_init[0], 34);
491 /* lock registers */
492 ret |= netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
493 0x1f, &cimax_init[0x18], 1);
494 /* power on slots */
495 ret |= netup_write_i2c(state->i2c_adap, state->ci_i2c_addr,
496 0x18, &cimax_init[0x18], 1);
497
498 if (0 != ret)
499 goto err;
500
501 ret = dvb_ca_en50221_init(&port->frontends.adapter,
502 &state->ca,
503 /* flags */ 0,
504 /* n_slots */ 1);
505 if (0 != ret)
506 goto err;
507
508 INIT_WORK(&state->work, netup_read_ci_status);
509 schedule_work(&state->work);
510
511 ci_dbg_print("%s: CI initialized!\n", __func__);
512
513 return 0;
514err:
515 ci_dbg_print("%s: Cannot initialize CI: Error %d.\n", __func__, ret);
516 kfree(state);
517 return ret;
518}
519
520void netup_ci_exit(struct cx23885_tsport *port)
521{
522 struct netup_ci_state *state;
523
524 if (NULL == port)
525 return;
526
527 state = (struct netup_ci_state *)port->port_priv;
528 if (NULL == state)
529 return;
530
531 if (NULL == state->ca.data)
532 return;
533
534 dvb_ca_en50221_release(&state->ca);
535 kfree(state);
536}
diff --git a/drivers/media/pci/cx23885/cimax2.h b/drivers/media/pci/cx23885/cimax2.h
new file mode 100644
index 000000000000..518744a4c8a5
--- /dev/null
+++ b/drivers/media/pci/cx23885/cimax2.h
@@ -0,0 +1,47 @@
1/*
2 * cimax2.h
3 *
4 * CIMax(R) SP2 driver in conjunction with NetUp Dual DVB-S2 CI card
5 *
6 * Copyright (C) 2009 NetUP Inc.
7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
8 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 *
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#ifndef CIMAX2_H
27#define CIMAX2_H
28#include "dvb_ca_en50221.h"
29
30extern int netup_ci_read_attribute_mem(struct dvb_ca_en50221 *en50221,
31 int slot, int addr);
32extern int netup_ci_write_attribute_mem(struct dvb_ca_en50221 *en50221,
33 int slot, int addr, u8 data);
34extern int netup_ci_read_cam_ctl(struct dvb_ca_en50221 *en50221,
35 int slot, u8 addr);
36extern int netup_ci_write_cam_ctl(struct dvb_ca_en50221 *en50221,
37 int slot, u8 addr, u8 data);
38extern int netup_ci_slot_reset(struct dvb_ca_en50221 *en50221, int slot);
39extern int netup_ci_slot_shutdown(struct dvb_ca_en50221 *en50221, int slot);
40extern int netup_ci_slot_ts_ctl(struct dvb_ca_en50221 *en50221, int slot);
41extern int netup_ci_slot_status(struct cx23885_dev *dev, u32 pci_status);
42extern int netup_poll_ci_slot_status(struct dvb_ca_en50221 *en50221,
43 int slot, int open);
44extern int netup_ci_init(struct cx23885_tsport *port);
45extern void netup_ci_exit(struct cx23885_tsport *port);
46
47#endif
diff --git a/drivers/media/pci/cx23885/cx23885-417.c b/drivers/media/pci/cx23885/cx23885-417.c
new file mode 100644
index 000000000000..5d5052d0253f
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-417.c
@@ -0,0 +1,1790 @@
1/*
2 *
3 * Support for a cx23417 mpeg encoder via cx23885 host port.
4 *
5 * (c) 2004 Jelle Foks <jelle@foks.us>
6 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
7 * (c) 2008 Steven Toth <stoth@linuxtv.org>
8 * - CX23885/7/8 support
9 *
10 * Includes parts from the ivtv driver <http://sourceforge.net/projects/ivtv/>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/init.h>
30#include <linux/fs.h>
31#include <linux/delay.h>
32#include <linux/device.h>
33#include <linux/firmware.h>
34#include <linux/slab.h>
35#include <media/v4l2-common.h>
36#include <media/v4l2-ioctl.h>
37#include <media/cx2341x.h>
38
39#include "cx23885.h"
40#include "cx23885-ioctl.h"
41
42#define CX23885_FIRM_IMAGE_SIZE 376836
43#define CX23885_FIRM_IMAGE_NAME "v4l-cx23885-enc.fw"
44
45static unsigned int mpegbufs = 32;
46module_param(mpegbufs, int, 0644);
47MODULE_PARM_DESC(mpegbufs, "number of mpeg buffers, range 2-32");
48static unsigned int mpeglines = 32;
49module_param(mpeglines, int, 0644);
50MODULE_PARM_DESC(mpeglines, "number of lines in an MPEG buffer, range 2-32");
51static unsigned int mpeglinesize = 512;
52module_param(mpeglinesize, int, 0644);
53MODULE_PARM_DESC(mpeglinesize,
54 "number of bytes in each line of an MPEG buffer, range 512-1024");
55
56static unsigned int v4l_debug;
57module_param(v4l_debug, int, 0644);
58MODULE_PARM_DESC(v4l_debug, "enable V4L debug messages");
59
60#define dprintk(level, fmt, arg...)\
61 do { if (v4l_debug >= level) \
62 printk(KERN_DEBUG "%s: " fmt, \
63 (dev) ? dev->name : "cx23885[?]", ## arg); \
64 } while (0)
65
66static struct cx23885_tvnorm cx23885_tvnorms[] = {
67 {
68 .name = "NTSC-M",
69 .id = V4L2_STD_NTSC_M,
70 }, {
71 .name = "NTSC-JP",
72 .id = V4L2_STD_NTSC_M_JP,
73 }, {
74 .name = "PAL-BG",
75 .id = V4L2_STD_PAL_BG,
76 }, {
77 .name = "PAL-DK",
78 .id = V4L2_STD_PAL_DK,
79 }, {
80 .name = "PAL-I",
81 .id = V4L2_STD_PAL_I,
82 }, {
83 .name = "PAL-M",
84 .id = V4L2_STD_PAL_M,
85 }, {
86 .name = "PAL-N",
87 .id = V4L2_STD_PAL_N,
88 }, {
89 .name = "PAL-Nc",
90 .id = V4L2_STD_PAL_Nc,
91 }, {
92 .name = "PAL-60",
93 .id = V4L2_STD_PAL_60,
94 }, {
95 .name = "SECAM-L",
96 .id = V4L2_STD_SECAM_L,
97 }, {
98 .name = "SECAM-DK",
99 .id = V4L2_STD_SECAM_DK,
100 }
101};
102
103/* ------------------------------------------------------------------ */
104enum cx23885_capture_type {
105 CX23885_MPEG_CAPTURE,
106 CX23885_RAW_CAPTURE,
107 CX23885_RAW_PASSTHRU_CAPTURE
108};
109enum cx23885_capture_bits {
110 CX23885_RAW_BITS_NONE = 0x00,
111 CX23885_RAW_BITS_YUV_CAPTURE = 0x01,
112 CX23885_RAW_BITS_PCM_CAPTURE = 0x02,
113 CX23885_RAW_BITS_VBI_CAPTURE = 0x04,
114 CX23885_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
115 CX23885_RAW_BITS_TO_HOST_CAPTURE = 0x10
116};
117enum cx23885_capture_end {
118 CX23885_END_AT_GOP, /* stop at the end of gop, generate irq */
119 CX23885_END_NOW, /* stop immediately, no irq */
120};
121enum cx23885_framerate {
122 CX23885_FRAMERATE_NTSC_30, /* NTSC: 30fps */
123 CX23885_FRAMERATE_PAL_25 /* PAL: 25fps */
124};
125enum cx23885_stream_port {
126 CX23885_OUTPUT_PORT_MEMORY,
127 CX23885_OUTPUT_PORT_STREAMING,
128 CX23885_OUTPUT_PORT_SERIAL
129};
130enum cx23885_data_xfer_status {
131 CX23885_MORE_BUFFERS_FOLLOW,
132 CX23885_LAST_BUFFER,
133};
134enum cx23885_picture_mask {
135 CX23885_PICTURE_MASK_NONE,
136 CX23885_PICTURE_MASK_I_FRAMES,
137 CX23885_PICTURE_MASK_I_P_FRAMES = 0x3,
138 CX23885_PICTURE_MASK_ALL_FRAMES = 0x7,
139};
140enum cx23885_vbi_mode_bits {
141 CX23885_VBI_BITS_SLICED,
142 CX23885_VBI_BITS_RAW,
143};
144enum cx23885_vbi_insertion_bits {
145 CX23885_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
146 CX23885_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
147 CX23885_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
148 CX23885_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
149 CX23885_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
150};
151enum cx23885_dma_unit {
152 CX23885_DMA_BYTES,
153 CX23885_DMA_FRAMES,
154};
155enum cx23885_dma_transfer_status_bits {
156 CX23885_DMA_TRANSFER_BITS_DONE = 0x01,
157 CX23885_DMA_TRANSFER_BITS_ERROR = 0x04,
158 CX23885_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
159};
160enum cx23885_pause {
161 CX23885_PAUSE_ENCODING,
162 CX23885_RESUME_ENCODING,
163};
164enum cx23885_copyright {
165 CX23885_COPYRIGHT_OFF,
166 CX23885_COPYRIGHT_ON,
167};
168enum cx23885_notification_type {
169 CX23885_NOTIFICATION_REFRESH,
170};
171enum cx23885_notification_status {
172 CX23885_NOTIFICATION_OFF,
173 CX23885_NOTIFICATION_ON,
174};
175enum cx23885_notification_mailbox {
176 CX23885_NOTIFICATION_NO_MAILBOX = -1,
177};
178enum cx23885_field1_lines {
179 CX23885_FIELD1_SAA7114 = 0x00EF, /* 239 */
180 CX23885_FIELD1_SAA7115 = 0x00F0, /* 240 */
181 CX23885_FIELD1_MICRONAS = 0x0105, /* 261 */
182};
183enum cx23885_field2_lines {
184 CX23885_FIELD2_SAA7114 = 0x00EF, /* 239 */
185 CX23885_FIELD2_SAA7115 = 0x00F0, /* 240 */
186 CX23885_FIELD2_MICRONAS = 0x0106, /* 262 */
187};
188enum cx23885_custom_data_type {
189 CX23885_CUSTOM_EXTENSION_USR_DATA,
190 CX23885_CUSTOM_PRIVATE_PACKET,
191};
192enum cx23885_mute {
193 CX23885_UNMUTE,
194 CX23885_MUTE,
195};
196enum cx23885_mute_video_mask {
197 CX23885_MUTE_VIDEO_V_MASK = 0x0000FF00,
198 CX23885_MUTE_VIDEO_U_MASK = 0x00FF0000,
199 CX23885_MUTE_VIDEO_Y_MASK = 0xFF000000,
200};
201enum cx23885_mute_video_shift {
202 CX23885_MUTE_VIDEO_V_SHIFT = 8,
203 CX23885_MUTE_VIDEO_U_SHIFT = 16,
204 CX23885_MUTE_VIDEO_Y_SHIFT = 24,
205};
206
207/* defines below are from ivtv-driver.h */
208#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
209
210/* Firmware API commands */
211#define IVTV_API_STD_TIMEOUT 500
212
213/* Registers */
214/* IVTV_REG_OFFSET */
215#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
216#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
217#define IVTV_REG_SPU (0x9050)
218#define IVTV_REG_HW_BLOCKS (0x9054)
219#define IVTV_REG_VPU (0x9058)
220#define IVTV_REG_APU (0xA064)
221
222/**** Bit definitions for MC417_RWD and MC417_OEN registers ***
223 bits 31-16
224+-----------+
225| Reserved |
226+-----------+
227 bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
228+-------+-------+-------+-------+-------+-------+-------+-------+
229| MIWR# | MIRD# | MICS# |MIRDY# |MIADDR3|MIADDR2|MIADDR1|MIADDR0|
230+-------+-------+-------+-------+-------+-------+-------+-------+
231 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
232+-------+-------+-------+-------+-------+-------+-------+-------+
233|MIDATA7|MIDATA6|MIDATA5|MIDATA4|MIDATA3|MIDATA2|MIDATA1|MIDATA0|
234+-------+-------+-------+-------+-------+-------+-------+-------+
235***/
236#define MC417_MIWR 0x8000
237#define MC417_MIRD 0x4000
238#define MC417_MICS 0x2000
239#define MC417_MIRDY 0x1000
240#define MC417_MIADDR 0x0F00
241#define MC417_MIDATA 0x00FF
242
243/* MIADDR* nibble definitions */
244#define MCI_MEMORY_DATA_BYTE0 0x000
245#define MCI_MEMORY_DATA_BYTE1 0x100
246#define MCI_MEMORY_DATA_BYTE2 0x200
247#define MCI_MEMORY_DATA_BYTE3 0x300
248#define MCI_MEMORY_ADDRESS_BYTE2 0x400
249#define MCI_MEMORY_ADDRESS_BYTE1 0x500
250#define MCI_MEMORY_ADDRESS_BYTE0 0x600
251#define MCI_REGISTER_DATA_BYTE0 0x800
252#define MCI_REGISTER_DATA_BYTE1 0x900
253#define MCI_REGISTER_DATA_BYTE2 0xA00
254#define MCI_REGISTER_DATA_BYTE3 0xB00
255#define MCI_REGISTER_ADDRESS_BYTE0 0xC00
256#define MCI_REGISTER_ADDRESS_BYTE1 0xD00
257#define MCI_REGISTER_MODE 0xE00
258
259/* Read and write modes */
260#define MCI_MODE_REGISTER_READ 0
261#define MCI_MODE_REGISTER_WRITE 1
262#define MCI_MODE_MEMORY_READ 0
263#define MCI_MODE_MEMORY_WRITE 0x40
264
265/*** Bit definitions for MC417_CTL register ****
266 bits 31-6 bits 5-4 bit 3 bits 2-1 Bit 0
267+--------+-------------+--------+--------------+------------+
268|Reserved|MC417_SPD_CTL|Reserved|MC417_GPIO_SEL|UART_GPIO_EN|
269+--------+-------------+--------+--------------+------------+
270***/
271#define MC417_SPD_CTL(x) (((x) << 4) & 0x00000030)
272#define MC417_GPIO_SEL(x) (((x) << 1) & 0x00000006)
273#define MC417_UART_GPIO_EN 0x00000001
274
275/* Values for speed control */
276#define MC417_SPD_CTL_SLOW 0x1
277#define MC417_SPD_CTL_MEDIUM 0x0
278#define MC417_SPD_CTL_FAST 0x3 /* b'1x, but we use b'11 */
279
280/* Values for GPIO select */
281#define MC417_GPIO_SEL_GPIO3 0x3
282#define MC417_GPIO_SEL_GPIO2 0x2
283#define MC417_GPIO_SEL_GPIO1 0x1
284#define MC417_GPIO_SEL_GPIO0 0x0
285
286void cx23885_mc417_init(struct cx23885_dev *dev)
287{
288 u32 regval;
289
290 dprintk(2, "%s()\n", __func__);
291
292 /* Configure MC417_CTL register to defaults. */
293 regval = MC417_SPD_CTL(MC417_SPD_CTL_FAST) |
294 MC417_GPIO_SEL(MC417_GPIO_SEL_GPIO3) |
295 MC417_UART_GPIO_EN;
296 cx_write(MC417_CTL, regval);
297
298 /* Configure MC417_OEN to defaults. */
299 regval = MC417_MIRDY;
300 cx_write(MC417_OEN, regval);
301
302 /* Configure MC417_RWD to defaults. */
303 regval = MC417_MIWR | MC417_MIRD | MC417_MICS;
304 cx_write(MC417_RWD, regval);
305}
306
307static int mc417_wait_ready(struct cx23885_dev *dev)
308{
309 u32 mi_ready;
310 unsigned long timeout = jiffies + msecs_to_jiffies(1);
311
312 for (;;) {
313 mi_ready = cx_read(MC417_RWD) & MC417_MIRDY;
314 if (mi_ready != 0)
315 return 0;
316 if (time_after(jiffies, timeout))
317 return -1;
318 udelay(1);
319 }
320}
321
322int mc417_register_write(struct cx23885_dev *dev, u16 address, u32 value)
323{
324 u32 regval;
325
326 /* Enable MC417 GPIO outputs except for MC417_MIRDY,
327 * which is an input.
328 */
329 cx_write(MC417_OEN, MC417_MIRDY);
330
331 /* Write data byte 0 */
332 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_DATA_BYTE0 |
333 (value & 0x000000FF);
334 cx_write(MC417_RWD, regval);
335
336 /* Transition CS/WR to effect write transaction across bus. */
337 regval |= MC417_MICS | MC417_MIWR;
338 cx_write(MC417_RWD, regval);
339
340 /* Write data byte 1 */
341 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_DATA_BYTE1 |
342 ((value >> 8) & 0x000000FF);
343 cx_write(MC417_RWD, regval);
344 regval |= MC417_MICS | MC417_MIWR;
345 cx_write(MC417_RWD, regval);
346
347 /* Write data byte 2 */
348 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_DATA_BYTE2 |
349 ((value >> 16) & 0x000000FF);
350 cx_write(MC417_RWD, regval);
351 regval |= MC417_MICS | MC417_MIWR;
352 cx_write(MC417_RWD, regval);
353
354 /* Write data byte 3 */
355 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_DATA_BYTE3 |
356 ((value >> 24) & 0x000000FF);
357 cx_write(MC417_RWD, regval);
358 regval |= MC417_MICS | MC417_MIWR;
359 cx_write(MC417_RWD, regval);
360
361 /* Write address byte 0 */
362 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_ADDRESS_BYTE0 |
363 (address & 0xFF);
364 cx_write(MC417_RWD, regval);
365 regval |= MC417_MICS | MC417_MIWR;
366 cx_write(MC417_RWD, regval);
367
368 /* Write address byte 1 */
369 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_ADDRESS_BYTE1 |
370 ((address >> 8) & 0xFF);
371 cx_write(MC417_RWD, regval);
372 regval |= MC417_MICS | MC417_MIWR;
373 cx_write(MC417_RWD, regval);
374
375 /* Indicate that this is a write. */
376 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_MODE |
377 MCI_MODE_REGISTER_WRITE;
378 cx_write(MC417_RWD, regval);
379 regval |= MC417_MICS | MC417_MIWR;
380 cx_write(MC417_RWD, regval);
381
382 /* Wait for the trans to complete (MC417_MIRDY asserted). */
383 return mc417_wait_ready(dev);
384}
385
386int mc417_register_read(struct cx23885_dev *dev, u16 address, u32 *value)
387{
388 int retval;
389 u32 regval;
390 u32 tempval;
391 u32 dataval;
392
393 /* Enable MC417 GPIO outputs except for MC417_MIRDY,
394 * which is an input.
395 */
396 cx_write(MC417_OEN, MC417_MIRDY);
397
398 /* Write address byte 0 */
399 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_ADDRESS_BYTE0 |
400 ((address & 0x00FF));
401 cx_write(MC417_RWD, regval);
402 regval |= MC417_MICS | MC417_MIWR;
403 cx_write(MC417_RWD, regval);
404
405 /* Write address byte 1 */
406 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_ADDRESS_BYTE1 |
407 ((address >> 8) & 0xFF);
408 cx_write(MC417_RWD, regval);
409 regval |= MC417_MICS | MC417_MIWR;
410 cx_write(MC417_RWD, regval);
411
412 /* Indicate that this is a register read. */
413 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_MODE |
414 MCI_MODE_REGISTER_READ;
415 cx_write(MC417_RWD, regval);
416 regval |= MC417_MICS | MC417_MIWR;
417 cx_write(MC417_RWD, regval);
418
419 /* Wait for the trans to complete (MC417_MIRDY asserted). */
420 retval = mc417_wait_ready(dev);
421
422 /* switch the DAT0-7 GPIO[10:3] to input mode */
423 cx_write(MC417_OEN, MC417_MIRDY | MC417_MIDATA);
424
425 /* Read data byte 0 */
426 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_DATA_BYTE0;
427 cx_write(MC417_RWD, regval);
428
429 /* Transition RD to effect read transaction across bus.
430 * Transtion 0x5000 -> 0x9000 correct (RD/RDY -> WR/RDY)?
431 * Should it be 0x9000 -> 0xF000 (also why is RDY being set, its
432 * input only...)
433 */
434 regval = MC417_MIWR | MC417_MIRDY | MCI_REGISTER_DATA_BYTE0;
435 cx_write(MC417_RWD, regval);
436
437 /* Collect byte */
438 tempval = cx_read(MC417_RWD);
439 dataval = tempval & 0x000000FF;
440
441 /* Bring CS and RD high. */
442 regval = MC417_MIWR | MC417_MIRD | MC417_MICS | MC417_MIRDY;
443 cx_write(MC417_RWD, regval);
444
445 /* Read data byte 1 */
446 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_DATA_BYTE1;
447 cx_write(MC417_RWD, regval);
448 regval = MC417_MIWR | MC417_MIRDY | MCI_REGISTER_DATA_BYTE1;
449 cx_write(MC417_RWD, regval);
450 tempval = cx_read(MC417_RWD);
451 dataval |= ((tempval & 0x000000FF) << 8);
452 regval = MC417_MIWR | MC417_MIRD | MC417_MICS | MC417_MIRDY;
453 cx_write(MC417_RWD, regval);
454
455 /* Read data byte 2 */
456 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_DATA_BYTE2;
457 cx_write(MC417_RWD, regval);
458 regval = MC417_MIWR | MC417_MIRDY | MCI_REGISTER_DATA_BYTE2;
459 cx_write(MC417_RWD, regval);
460 tempval = cx_read(MC417_RWD);
461 dataval |= ((tempval & 0x000000FF) << 16);
462 regval = MC417_MIWR | MC417_MIRD | MC417_MICS | MC417_MIRDY;
463 cx_write(MC417_RWD, regval);
464
465 /* Read data byte 3 */
466 regval = MC417_MIRD | MC417_MIRDY | MCI_REGISTER_DATA_BYTE3;
467 cx_write(MC417_RWD, regval);
468 regval = MC417_MIWR | MC417_MIRDY | MCI_REGISTER_DATA_BYTE3;
469 cx_write(MC417_RWD, regval);
470 tempval = cx_read(MC417_RWD);
471 dataval |= ((tempval & 0x000000FF) << 24);
472 regval = MC417_MIWR | MC417_MIRD | MC417_MICS | MC417_MIRDY;
473 cx_write(MC417_RWD, regval);
474
475 *value = dataval;
476
477 return retval;
478}
479
480int mc417_memory_write(struct cx23885_dev *dev, u32 address, u32 value)
481{
482 u32 regval;
483
484 /* Enable MC417 GPIO outputs except for MC417_MIRDY,
485 * which is an input.
486 */
487 cx_write(MC417_OEN, MC417_MIRDY);
488
489 /* Write data byte 0 */
490 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_DATA_BYTE0 |
491 (value & 0x000000FF);
492 cx_write(MC417_RWD, regval);
493
494 /* Transition CS/WR to effect write transaction across bus. */
495 regval |= MC417_MICS | MC417_MIWR;
496 cx_write(MC417_RWD, regval);
497
498 /* Write data byte 1 */
499 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_DATA_BYTE1 |
500 ((value >> 8) & 0x000000FF);
501 cx_write(MC417_RWD, regval);
502 regval |= MC417_MICS | MC417_MIWR;
503 cx_write(MC417_RWD, regval);
504
505 /* Write data byte 2 */
506 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_DATA_BYTE2 |
507 ((value >> 16) & 0x000000FF);
508 cx_write(MC417_RWD, regval);
509 regval |= MC417_MICS | MC417_MIWR;
510 cx_write(MC417_RWD, regval);
511
512 /* Write data byte 3 */
513 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_DATA_BYTE3 |
514 ((value >> 24) & 0x000000FF);
515 cx_write(MC417_RWD, regval);
516 regval |= MC417_MICS | MC417_MIWR;
517 cx_write(MC417_RWD, regval);
518
519 /* Write address byte 2 */
520 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_ADDRESS_BYTE2 |
521 MCI_MODE_MEMORY_WRITE | ((address >> 16) & 0x3F);
522 cx_write(MC417_RWD, regval);
523 regval |= MC417_MICS | MC417_MIWR;
524 cx_write(MC417_RWD, regval);
525
526 /* Write address byte 1 */
527 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_ADDRESS_BYTE1 |
528 ((address >> 8) & 0xFF);
529 cx_write(MC417_RWD, regval);
530 regval |= MC417_MICS | MC417_MIWR;
531 cx_write(MC417_RWD, regval);
532
533 /* Write address byte 0 */
534 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_ADDRESS_BYTE0 |
535 (address & 0xFF);
536 cx_write(MC417_RWD, regval);
537 regval |= MC417_MICS | MC417_MIWR;
538 cx_write(MC417_RWD, regval);
539
540 /* Wait for the trans to complete (MC417_MIRDY asserted). */
541 return mc417_wait_ready(dev);
542}
543
544int mc417_memory_read(struct cx23885_dev *dev, u32 address, u32 *value)
545{
546 int retval;
547 u32 regval;
548 u32 tempval;
549 u32 dataval;
550
551 /* Enable MC417 GPIO outputs except for MC417_MIRDY,
552 * which is an input.
553 */
554 cx_write(MC417_OEN, MC417_MIRDY);
555
556 /* Write address byte 2 */
557 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_ADDRESS_BYTE2 |
558 MCI_MODE_MEMORY_READ | ((address >> 16) & 0x3F);
559 cx_write(MC417_RWD, regval);
560 regval |= MC417_MICS | MC417_MIWR;
561 cx_write(MC417_RWD, regval);
562
563 /* Write address byte 1 */
564 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_ADDRESS_BYTE1 |
565 ((address >> 8) & 0xFF);
566 cx_write(MC417_RWD, regval);
567 regval |= MC417_MICS | MC417_MIWR;
568 cx_write(MC417_RWD, regval);
569
570 /* Write address byte 0 */
571 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_ADDRESS_BYTE0 |
572 (address & 0xFF);
573 cx_write(MC417_RWD, regval);
574 regval |= MC417_MICS | MC417_MIWR;
575 cx_write(MC417_RWD, regval);
576
577 /* Wait for the trans to complete (MC417_MIRDY asserted). */
578 retval = mc417_wait_ready(dev);
579
580 /* switch the DAT0-7 GPIO[10:3] to input mode */
581 cx_write(MC417_OEN, MC417_MIRDY | MC417_MIDATA);
582
583 /* Read data byte 3 */
584 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_DATA_BYTE3;
585 cx_write(MC417_RWD, regval);
586
587 /* Transition RD to effect read transaction across bus. */
588 regval = MC417_MIWR | MC417_MIRDY | MCI_MEMORY_DATA_BYTE3;
589 cx_write(MC417_RWD, regval);
590
591 /* Collect byte */
592 tempval = cx_read(MC417_RWD);
593 dataval = ((tempval & 0x000000FF) << 24);
594
595 /* Bring CS and RD high. */
596 regval = MC417_MIWR | MC417_MIRD | MC417_MICS | MC417_MIRDY;
597 cx_write(MC417_RWD, regval);
598
599 /* Read data byte 2 */
600 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_DATA_BYTE2;
601 cx_write(MC417_RWD, regval);
602 regval = MC417_MIWR | MC417_MIRDY | MCI_MEMORY_DATA_BYTE2;
603 cx_write(MC417_RWD, regval);
604 tempval = cx_read(MC417_RWD);
605 dataval |= ((tempval & 0x000000FF) << 16);
606 regval = MC417_MIWR | MC417_MIRD | MC417_MICS | MC417_MIRDY;
607 cx_write(MC417_RWD, regval);
608
609 /* Read data byte 1 */
610 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_DATA_BYTE1;
611 cx_write(MC417_RWD, regval);
612 regval = MC417_MIWR | MC417_MIRDY | MCI_MEMORY_DATA_BYTE1;
613 cx_write(MC417_RWD, regval);
614 tempval = cx_read(MC417_RWD);
615 dataval |= ((tempval & 0x000000FF) << 8);
616 regval = MC417_MIWR | MC417_MIRD | MC417_MICS | MC417_MIRDY;
617 cx_write(MC417_RWD, regval);
618
619 /* Read data byte 0 */
620 regval = MC417_MIRD | MC417_MIRDY | MCI_MEMORY_DATA_BYTE0;
621 cx_write(MC417_RWD, regval);
622 regval = MC417_MIWR | MC417_MIRDY | MCI_MEMORY_DATA_BYTE0;
623 cx_write(MC417_RWD, regval);
624 tempval = cx_read(MC417_RWD);
625 dataval |= (tempval & 0x000000FF);
626 regval = MC417_MIWR | MC417_MIRD | MC417_MICS | MC417_MIRDY;
627 cx_write(MC417_RWD, regval);
628
629 *value = dataval;
630
631 return retval;
632}
633
634void mc417_gpio_set(struct cx23885_dev *dev, u32 mask)
635{
636 u32 val;
637
638 /* Set the gpio value */
639 mc417_register_read(dev, 0x900C, &val);
640 val |= (mask & 0x000ffff);
641 mc417_register_write(dev, 0x900C, val);
642}
643
644void mc417_gpio_clear(struct cx23885_dev *dev, u32 mask)
645{
646 u32 val;
647
648 /* Clear the gpio value */
649 mc417_register_read(dev, 0x900C, &val);
650 val &= ~(mask & 0x0000ffff);
651 mc417_register_write(dev, 0x900C, val);
652}
653
654void mc417_gpio_enable(struct cx23885_dev *dev, u32 mask, int asoutput)
655{
656 u32 val;
657
658 /* Enable GPIO direction bits */
659 mc417_register_read(dev, 0x9020, &val);
660 if (asoutput)
661 val |= (mask & 0x0000ffff);
662 else
663 val &= ~(mask & 0x0000ffff);
664
665 mc417_register_write(dev, 0x9020, val);
666}
667/* ------------------------------------------------------------------ */
668
669/* MPEG encoder API */
670static char *cmd_to_str(int cmd)
671{
672 switch (cmd) {
673 case CX2341X_ENC_PING_FW:
674 return "PING_FW";
675 case CX2341X_ENC_START_CAPTURE:
676 return "START_CAPTURE";
677 case CX2341X_ENC_STOP_CAPTURE:
678 return "STOP_CAPTURE";
679 case CX2341X_ENC_SET_AUDIO_ID:
680 return "SET_AUDIO_ID";
681 case CX2341X_ENC_SET_VIDEO_ID:
682 return "SET_VIDEO_ID";
683 case CX2341X_ENC_SET_PCR_ID:
684 return "SET_PCR_ID";
685 case CX2341X_ENC_SET_FRAME_RATE:
686 return "SET_FRAME_RATE";
687 case CX2341X_ENC_SET_FRAME_SIZE:
688 return "SET_FRAME_SIZE";
689 case CX2341X_ENC_SET_BIT_RATE:
690 return "SET_BIT_RATE";
691 case CX2341X_ENC_SET_GOP_PROPERTIES:
692 return "SET_GOP_PROPERTIES";
693 case CX2341X_ENC_SET_ASPECT_RATIO:
694 return "SET_ASPECT_RATIO";
695 case CX2341X_ENC_SET_DNR_FILTER_MODE:
696 return "SET_DNR_FILTER_MODE";
697 case CX2341X_ENC_SET_DNR_FILTER_PROPS:
698 return "SET_DNR_FILTER_PROPS";
699 case CX2341X_ENC_SET_CORING_LEVELS:
700 return "SET_CORING_LEVELS";
701 case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
702 return "SET_SPATIAL_FILTER_TYPE";
703 case CX2341X_ENC_SET_VBI_LINE:
704 return "SET_VBI_LINE";
705 case CX2341X_ENC_SET_STREAM_TYPE:
706 return "SET_STREAM_TYPE";
707 case CX2341X_ENC_SET_OUTPUT_PORT:
708 return "SET_OUTPUT_PORT";
709 case CX2341X_ENC_SET_AUDIO_PROPERTIES:
710 return "SET_AUDIO_PROPERTIES";
711 case CX2341X_ENC_HALT_FW:
712 return "HALT_FW";
713 case CX2341X_ENC_GET_VERSION:
714 return "GET_VERSION";
715 case CX2341X_ENC_SET_GOP_CLOSURE:
716 return "SET_GOP_CLOSURE";
717 case CX2341X_ENC_GET_SEQ_END:
718 return "GET_SEQ_END";
719 case CX2341X_ENC_SET_PGM_INDEX_INFO:
720 return "SET_PGM_INDEX_INFO";
721 case CX2341X_ENC_SET_VBI_CONFIG:
722 return "SET_VBI_CONFIG";
723 case CX2341X_ENC_SET_DMA_BLOCK_SIZE:
724 return "SET_DMA_BLOCK_SIZE";
725 case CX2341X_ENC_GET_PREV_DMA_INFO_MB_10:
726 return "GET_PREV_DMA_INFO_MB_10";
727 case CX2341X_ENC_GET_PREV_DMA_INFO_MB_9:
728 return "GET_PREV_DMA_INFO_MB_9";
729 case CX2341X_ENC_SCHED_DMA_TO_HOST:
730 return "SCHED_DMA_TO_HOST";
731 case CX2341X_ENC_INITIALIZE_INPUT:
732 return "INITIALIZE_INPUT";
733 case CX2341X_ENC_SET_FRAME_DROP_RATE:
734 return "SET_FRAME_DROP_RATE";
735 case CX2341X_ENC_PAUSE_ENCODER:
736 return "PAUSE_ENCODER";
737 case CX2341X_ENC_REFRESH_INPUT:
738 return "REFRESH_INPUT";
739 case CX2341X_ENC_SET_COPYRIGHT:
740 return "SET_COPYRIGHT";
741 case CX2341X_ENC_SET_EVENT_NOTIFICATION:
742 return "SET_EVENT_NOTIFICATION";
743 case CX2341X_ENC_SET_NUM_VSYNC_LINES:
744 return "SET_NUM_VSYNC_LINES";
745 case CX2341X_ENC_SET_PLACEHOLDER:
746 return "SET_PLACEHOLDER";
747 case CX2341X_ENC_MUTE_VIDEO:
748 return "MUTE_VIDEO";
749 case CX2341X_ENC_MUTE_AUDIO:
750 return "MUTE_AUDIO";
751 case CX2341X_ENC_MISC:
752 return "MISC";
753 default:
754 return "UNKNOWN";
755 }
756}
757
758static int cx23885_mbox_func(void *priv,
759 u32 command,
760 int in,
761 int out,
762 u32 data[CX2341X_MBOX_MAX_DATA])
763{
764 struct cx23885_dev *dev = priv;
765 unsigned long timeout;
766 u32 value, flag, retval = 0;
767 int i;
768
769 dprintk(3, "%s: command(0x%X) = %s\n", __func__, command,
770 cmd_to_str(command));
771
772 /* this may not be 100% safe if we can't read any memory location
773 without side effects */
774 mc417_memory_read(dev, dev->cx23417_mailbox - 4, &value);
775 if (value != 0x12345678) {
776 printk(KERN_ERR
777 "Firmware and/or mailbox pointer not initialized "
778 "or corrupted, signature = 0x%x, cmd = %s\n", value,
779 cmd_to_str(command));
780 return -1;
781 }
782
783 /* This read looks at 32 bits, but flag is only 8 bits.
784 * Seems we also bail if CMD or TIMEOUT bytes are set???
785 */
786 mc417_memory_read(dev, dev->cx23417_mailbox, &flag);
787 if (flag) {
788 printk(KERN_ERR "ERROR: Mailbox appears to be in use "
789 "(%x), cmd = %s\n", flag, cmd_to_str(command));
790 return -1;
791 }
792
793 flag |= 1; /* tell 'em we're working on it */
794 mc417_memory_write(dev, dev->cx23417_mailbox, flag);
795
796 /* write command + args + fill remaining with zeros */
797 /* command code */
798 mc417_memory_write(dev, dev->cx23417_mailbox + 1, command);
799 mc417_memory_write(dev, dev->cx23417_mailbox + 3,
800 IVTV_API_STD_TIMEOUT); /* timeout */
801 for (i = 0; i < in; i++) {
802 mc417_memory_write(dev, dev->cx23417_mailbox + 4 + i, data[i]);
803 dprintk(3, "API Input %d = %d\n", i, data[i]);
804 }
805 for (; i < CX2341X_MBOX_MAX_DATA; i++)
806 mc417_memory_write(dev, dev->cx23417_mailbox + 4 + i, 0);
807
808 flag |= 3; /* tell 'em we're done writing */
809 mc417_memory_write(dev, dev->cx23417_mailbox, flag);
810
811 /* wait for firmware to handle the API command */
812 timeout = jiffies + msecs_to_jiffies(10);
813 for (;;) {
814 mc417_memory_read(dev, dev->cx23417_mailbox, &flag);
815 if (0 != (flag & 4))
816 break;
817 if (time_after(jiffies, timeout)) {
818 printk(KERN_ERR "ERROR: API Mailbox timeout\n");
819 return -1;
820 }
821 udelay(10);
822 }
823
824 /* read output values */
825 for (i = 0; i < out; i++) {
826 mc417_memory_read(dev, dev->cx23417_mailbox + 4 + i, data + i);
827 dprintk(3, "API Output %d = %d\n", i, data[i]);
828 }
829
830 mc417_memory_read(dev, dev->cx23417_mailbox + 2, &retval);
831 dprintk(3, "API result = %d\n", retval);
832
833 flag = 0;
834 mc417_memory_write(dev, dev->cx23417_mailbox, flag);
835
836 return retval;
837}
838
839/* We don't need to call the API often, so using just one
840 * mailbox will probably suffice
841 */
842static int cx23885_api_cmd(struct cx23885_dev *dev,
843 u32 command,
844 u32 inputcnt,
845 u32 outputcnt,
846 ...)
847{
848 u32 data[CX2341X_MBOX_MAX_DATA];
849 va_list vargs;
850 int i, err;
851
852 dprintk(3, "%s() cmds = 0x%08x\n", __func__, command);
853
854 va_start(vargs, outputcnt);
855 for (i = 0; i < inputcnt; i++)
856 data[i] = va_arg(vargs, int);
857
858 err = cx23885_mbox_func(dev, command, inputcnt, outputcnt, data);
859 for (i = 0; i < outputcnt; i++) {
860 int *vptr = va_arg(vargs, int *);
861 *vptr = data[i];
862 }
863 va_end(vargs);
864
865 return err;
866}
867
868static int cx23885_find_mailbox(struct cx23885_dev *dev)
869{
870 u32 signature[4] = {
871 0x12345678, 0x34567812, 0x56781234, 0x78123456
872 };
873 int signaturecnt = 0;
874 u32 value;
875 int i;
876
877 dprintk(2, "%s()\n", __func__);
878
879 for (i = 0; i < CX23885_FIRM_IMAGE_SIZE; i++) {
880 mc417_memory_read(dev, i, &value);
881 if (value == signature[signaturecnt])
882 signaturecnt++;
883 else
884 signaturecnt = 0;
885 if (4 == signaturecnt) {
886 dprintk(1, "Mailbox signature found at 0x%x\n", i+1);
887 return i+1;
888 }
889 }
890 printk(KERN_ERR "Mailbox signature values not found!\n");
891 return -1;
892}
893
894static int cx23885_load_firmware(struct cx23885_dev *dev)
895{
896 static const unsigned char magic[8] = {
897 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
898 };
899 const struct firmware *firmware;
900 int i, retval = 0;
901 u32 value = 0;
902 u32 gpio_output = 0;
903 u32 gpio_value;
904 u32 checksum = 0;
905 u32 *dataptr;
906
907 dprintk(2, "%s()\n", __func__);
908
909 /* Save GPIO settings before reset of APU */
910 retval |= mc417_memory_read(dev, 0x9020, &gpio_output);
911 retval |= mc417_memory_read(dev, 0x900C, &gpio_value);
912
913 retval = mc417_register_write(dev,
914 IVTV_REG_VPU, 0xFFFFFFED);
915 retval |= mc417_register_write(dev,
916 IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
917 retval |= mc417_register_write(dev,
918 IVTV_REG_ENC_SDRAM_REFRESH, 0x80000800);
919 retval |= mc417_register_write(dev,
920 IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
921 retval |= mc417_register_write(dev,
922 IVTV_REG_APU, 0);
923
924 if (retval != 0) {
925 printk(KERN_ERR "%s: Error with mc417_register_write\n",
926 __func__);
927 return -1;
928 }
929
930 retval = request_firmware(&firmware, CX23885_FIRM_IMAGE_NAME,
931 &dev->pci->dev);
932
933 if (retval != 0) {
934 printk(KERN_ERR
935 "ERROR: Hotplug firmware request failed (%s).\n",
936 CX23885_FIRM_IMAGE_NAME);
937 printk(KERN_ERR "Please fix your hotplug setup, the board will "
938 "not work without firmware loaded!\n");
939 return -1;
940 }
941
942 if (firmware->size != CX23885_FIRM_IMAGE_SIZE) {
943 printk(KERN_ERR "ERROR: Firmware size mismatch "
944 "(have %zd, expected %d)\n",
945 firmware->size, CX23885_FIRM_IMAGE_SIZE);
946 release_firmware(firmware);
947 return -1;
948 }
949
950 if (0 != memcmp(firmware->data, magic, 8)) {
951 printk(KERN_ERR
952 "ERROR: Firmware magic mismatch, wrong file?\n");
953 release_firmware(firmware);
954 return -1;
955 }
956
957 /* transfer to the chip */
958 dprintk(2, "Loading firmware ...\n");
959 dataptr = (u32 *)firmware->data;
960 for (i = 0; i < (firmware->size >> 2); i++) {
961 value = *dataptr;
962 checksum += ~value;
963 if (mc417_memory_write(dev, i, value) != 0) {
964 printk(KERN_ERR "ERROR: Loading firmware failed!\n");
965 release_firmware(firmware);
966 return -1;
967 }
968 dataptr++;
969 }
970
971 /* read back to verify with the checksum */
972 dprintk(1, "Verifying firmware ...\n");
973 for (i--; i >= 0; i--) {
974 if (mc417_memory_read(dev, i, &value) != 0) {
975 printk(KERN_ERR "ERROR: Reading firmware failed!\n");
976 release_firmware(firmware);
977 return -1;
978 }
979 checksum -= ~value;
980 }
981 if (checksum) {
982 printk(KERN_ERR
983 "ERROR: Firmware load failed (checksum mismatch).\n");
984 release_firmware(firmware);
985 return -1;
986 }
987 release_firmware(firmware);
988 dprintk(1, "Firmware upload successful.\n");
989
990 retval |= mc417_register_write(dev, IVTV_REG_HW_BLOCKS,
991 IVTV_CMD_HW_BLOCKS_RST);
992
993 /* F/W power up disturbs the GPIOs, restore state */
994 retval |= mc417_register_write(dev, 0x9020, gpio_output);
995 retval |= mc417_register_write(dev, 0x900C, gpio_value);
996
997 retval |= mc417_register_read(dev, IVTV_REG_VPU, &value);
998 retval |= mc417_register_write(dev, IVTV_REG_VPU, value & 0xFFFFFFE8);
999
1000 /* Hardcoded GPIO's here */
1001 retval |= mc417_register_write(dev, 0x9020, 0x4000);
1002 retval |= mc417_register_write(dev, 0x900C, 0x4000);
1003
1004 mc417_register_read(dev, 0x9020, &gpio_output);
1005 mc417_register_read(dev, 0x900C, &gpio_value);
1006
1007 if (retval < 0)
1008 printk(KERN_ERR "%s: Error with mc417_register_write\n",
1009 __func__);
1010 return 0;
1011}
1012
1013void cx23885_417_check_encoder(struct cx23885_dev *dev)
1014{
1015 u32 status, seq;
1016
1017 status = seq = 0;
1018 cx23885_api_cmd(dev, CX2341X_ENC_GET_SEQ_END, 0, 2, &status, &seq);
1019 dprintk(1, "%s() status = %d, seq = %d\n", __func__, status, seq);
1020}
1021
1022static void cx23885_codec_settings(struct cx23885_dev *dev)
1023{
1024 dprintk(1, "%s()\n", __func__);
1025
1026 /* Dynamically change the height based on video standard */
1027 if (dev->encodernorm.id & V4L2_STD_525_60)
1028 dev->ts1.height = 480;
1029 else
1030 dev->ts1.height = 576;
1031
1032 /* assign frame size */
1033 cx23885_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
1034 dev->ts1.height, dev->ts1.width);
1035
1036 dev->mpeg_params.width = dev->ts1.width;
1037 dev->mpeg_params.height = dev->ts1.height;
1038 dev->mpeg_params.is_50hz =
1039 (dev->encodernorm.id & V4L2_STD_625_50) != 0;
1040
1041 cx2341x_update(dev, cx23885_mbox_func, NULL, &dev->mpeg_params);
1042
1043 cx23885_api_cmd(dev, CX2341X_ENC_MISC, 2, 0, 3, 1);
1044 cx23885_api_cmd(dev, CX2341X_ENC_MISC, 2, 0, 4, 1);
1045}
1046
1047static int cx23885_initialize_codec(struct cx23885_dev *dev, int startencoder)
1048{
1049 int version;
1050 int retval;
1051 u32 i, data[7];
1052
1053 dprintk(1, "%s()\n", __func__);
1054
1055 retval = cx23885_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
1056 if (retval < 0) {
1057 dprintk(2, "%s() PING OK\n", __func__);
1058 retval = cx23885_load_firmware(dev);
1059 if (retval < 0) {
1060 printk(KERN_ERR "%s() f/w load failed\n", __func__);
1061 return retval;
1062 }
1063 retval = cx23885_find_mailbox(dev);
1064 if (retval < 0) {
1065 printk(KERN_ERR "%s() mailbox < 0, error\n",
1066 __func__);
1067 return -1;
1068 }
1069 dev->cx23417_mailbox = retval;
1070 retval = cx23885_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0);
1071 if (retval < 0) {
1072 printk(KERN_ERR
1073 "ERROR: cx23417 firmware ping failed!\n");
1074 return -1;
1075 }
1076 retval = cx23885_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1,
1077 &version);
1078 if (retval < 0) {
1079 printk(KERN_ERR "ERROR: cx23417 firmware get encoder :"
1080 "version failed!\n");
1081 return -1;
1082 }
1083 dprintk(1, "cx23417 firmware version is 0x%08x\n", version);
1084 msleep(200);
1085 }
1086
1087 cx23885_codec_settings(dev);
1088 msleep(60);
1089
1090 cx23885_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
1091 CX23885_FIELD1_SAA7115, CX23885_FIELD2_SAA7115);
1092 cx23885_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
1093 CX23885_CUSTOM_EXTENSION_USR_DATA, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1094 0, 0);
1095
1096 /* Setup to capture VBI */
1097 data[0] = 0x0001BD00;
1098 data[1] = 1; /* frames per interrupt */
1099 data[2] = 4; /* total bufs */
1100 data[3] = 0x91559155; /* start codes */
1101 data[4] = 0x206080C0; /* stop codes */
1102 data[5] = 6; /* lines */
1103 data[6] = 64; /* BPL */
1104
1105 cx23885_api_cmd(dev, CX2341X_ENC_SET_VBI_CONFIG, 7, 0, data[0], data[1],
1106 data[2], data[3], data[4], data[5], data[6]);
1107
1108 for (i = 2; i <= 24; i++) {
1109 int valid;
1110
1111 valid = ((i >= 19) && (i <= 21));
1112 cx23885_api_cmd(dev, CX2341X_ENC_SET_VBI_LINE, 5, 0, i,
1113 valid, 0 , 0, 0);
1114 cx23885_api_cmd(dev, CX2341X_ENC_SET_VBI_LINE, 5, 0,
1115 i | 0x80000000, valid, 0, 0, 0);
1116 }
1117
1118 cx23885_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, CX23885_UNMUTE);
1119 msleep(60);
1120
1121 /* initialize the video input */
1122 cx23885_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
1123 msleep(60);
1124
1125 /* Enable VIP style pixel invalidation so we work with scaled mode */
1126 mc417_memory_write(dev, 2120, 0x00000080);
1127
1128 /* start capturing to the host interface */
1129 if (startencoder) {
1130 cx23885_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
1131 CX23885_MPEG_CAPTURE, CX23885_RAW_BITS_NONE);
1132 msleep(10);
1133 }
1134
1135 return 0;
1136}
1137
1138/* ------------------------------------------------------------------ */
1139
1140static int bb_buf_setup(struct videobuf_queue *q,
1141 unsigned int *count, unsigned int *size)
1142{
1143 struct cx23885_fh *fh = q->priv_data;
1144
1145 fh->dev->ts1.ts_packet_size = mpeglinesize;
1146 fh->dev->ts1.ts_packet_count = mpeglines;
1147
1148 *size = fh->dev->ts1.ts_packet_size * fh->dev->ts1.ts_packet_count;
1149 *count = mpegbufs;
1150
1151 return 0;
1152}
1153
1154static int bb_buf_prepare(struct videobuf_queue *q,
1155 struct videobuf_buffer *vb, enum v4l2_field field)
1156{
1157 struct cx23885_fh *fh = q->priv_data;
1158 return cx23885_buf_prepare(q, &fh->dev->ts1,
1159 (struct cx23885_buffer *)vb,
1160 field);
1161}
1162
1163static void bb_buf_queue(struct videobuf_queue *q,
1164 struct videobuf_buffer *vb)
1165{
1166 struct cx23885_fh *fh = q->priv_data;
1167 cx23885_buf_queue(&fh->dev->ts1, (struct cx23885_buffer *)vb);
1168}
1169
1170static void bb_buf_release(struct videobuf_queue *q,
1171 struct videobuf_buffer *vb)
1172{
1173 cx23885_free_buffer(q, (struct cx23885_buffer *)vb);
1174}
1175
1176static struct videobuf_queue_ops cx23885_qops = {
1177 .buf_setup = bb_buf_setup,
1178 .buf_prepare = bb_buf_prepare,
1179 .buf_queue = bb_buf_queue,
1180 .buf_release = bb_buf_release,
1181};
1182
1183/* ------------------------------------------------------------------ */
1184
1185static const u32 *ctrl_classes[] = {
1186 cx2341x_mpeg_ctrls,
1187 NULL
1188};
1189
1190static int cx23885_queryctrl(struct cx23885_dev *dev,
1191 struct v4l2_queryctrl *qctrl)
1192{
1193 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1194 if (qctrl->id == 0)
1195 return -EINVAL;
1196
1197 /* MPEG V4L2 controls */
1198 if (cx2341x_ctrl_query(&dev->mpeg_params, qctrl))
1199 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
1200
1201 return 0;
1202}
1203
1204static int cx23885_querymenu(struct cx23885_dev *dev,
1205 struct v4l2_querymenu *qmenu)
1206{
1207 struct v4l2_queryctrl qctrl;
1208
1209 qctrl.id = qmenu->id;
1210 cx23885_queryctrl(dev, &qctrl);
1211 return v4l2_ctrl_query_menu(qmenu, &qctrl,
1212 cx2341x_ctrl_get_menu(&dev->mpeg_params, qmenu->id));
1213}
1214
1215static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1216{
1217 struct cx23885_fh *fh = file->private_data;
1218 struct cx23885_dev *dev = fh->dev;
1219
1220 call_all(dev, core, g_std, id);
1221
1222 return 0;
1223}
1224
1225static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
1226{
1227 struct cx23885_fh *fh = file->private_data;
1228 struct cx23885_dev *dev = fh->dev;
1229 unsigned int i;
1230
1231 for (i = 0; i < ARRAY_SIZE(cx23885_tvnorms); i++)
1232 if (*id & cx23885_tvnorms[i].id)
1233 break;
1234 if (i == ARRAY_SIZE(cx23885_tvnorms))
1235 return -EINVAL;
1236 dev->encodernorm = cx23885_tvnorms[i];
1237
1238 /* Have the drier core notify the subdevices */
1239 mutex_lock(&dev->lock);
1240 cx23885_set_tvnorm(dev, *id);
1241 mutex_unlock(&dev->lock);
1242
1243 return 0;
1244}
1245
1246static int vidioc_enum_input(struct file *file, void *priv,
1247 struct v4l2_input *i)
1248{
1249 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1250 dprintk(1, "%s()\n", __func__);
1251 return cx23885_enum_input(dev, i);
1252}
1253
1254static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1255{
1256 return cx23885_get_input(file, priv, i);
1257}
1258
1259static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1260{
1261 return cx23885_set_input(file, priv, i);
1262}
1263
1264static int vidioc_g_tuner(struct file *file, void *priv,
1265 struct v4l2_tuner *t)
1266{
1267 struct cx23885_fh *fh = file->private_data;
1268 struct cx23885_dev *dev = fh->dev;
1269
1270 if (UNSET == dev->tuner_type)
1271 return -EINVAL;
1272 if (0 != t->index)
1273 return -EINVAL;
1274 strcpy(t->name, "Television");
1275 call_all(dev, tuner, g_tuner, t);
1276
1277 dprintk(1, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
1278
1279 return 0;
1280}
1281
1282static int vidioc_s_tuner(struct file *file, void *priv,
1283 struct v4l2_tuner *t)
1284{
1285 struct cx23885_fh *fh = file->private_data;
1286 struct cx23885_dev *dev = fh->dev;
1287
1288 if (UNSET == dev->tuner_type)
1289 return -EINVAL;
1290
1291 /* Update the A/V core */
1292 call_all(dev, tuner, s_tuner, t);
1293
1294 return 0;
1295}
1296
1297static int vidioc_g_frequency(struct file *file, void *priv,
1298 struct v4l2_frequency *f)
1299{
1300 struct cx23885_fh *fh = file->private_data;
1301 struct cx23885_dev *dev = fh->dev;
1302
1303 if (UNSET == dev->tuner_type)
1304 return -EINVAL;
1305 f->type = V4L2_TUNER_ANALOG_TV;
1306 f->frequency = dev->freq;
1307
1308 call_all(dev, tuner, g_frequency, f);
1309
1310 return 0;
1311}
1312
1313static int vidioc_s_frequency(struct file *file, void *priv,
1314 struct v4l2_frequency *f)
1315{
1316 return cx23885_set_frequency(file, priv, f);
1317}
1318
1319static int vidioc_g_ctrl(struct file *file, void *priv,
1320 struct v4l2_control *ctl)
1321{
1322 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1323
1324 return cx23885_get_control(dev, ctl);
1325}
1326
1327static int vidioc_s_ctrl(struct file *file, void *priv,
1328 struct v4l2_control *ctl)
1329{
1330 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1331
1332 return cx23885_set_control(dev, ctl);
1333}
1334
1335static int vidioc_querycap(struct file *file, void *priv,
1336 struct v4l2_capability *cap)
1337{
1338 struct cx23885_fh *fh = file->private_data;
1339 struct cx23885_dev *dev = fh->dev;
1340 struct cx23885_tsport *tsport = &dev->ts1;
1341
1342 strlcpy(cap->driver, dev->name, sizeof(cap->driver));
1343 strlcpy(cap->card, cx23885_boards[tsport->dev->board].name,
1344 sizeof(cap->card));
1345 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1346 cap->capabilities =
1347 V4L2_CAP_VIDEO_CAPTURE |
1348 V4L2_CAP_READWRITE |
1349 V4L2_CAP_STREAMING |
1350 0;
1351 if (UNSET != dev->tuner_type)
1352 cap->capabilities |= V4L2_CAP_TUNER;
1353
1354 return 0;
1355}
1356
1357static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1358 struct v4l2_fmtdesc *f)
1359{
1360 if (f->index != 0)
1361 return -EINVAL;
1362
1363 strlcpy(f->description, "MPEG", sizeof(f->description));
1364 f->pixelformat = V4L2_PIX_FMT_MPEG;
1365
1366 return 0;
1367}
1368
1369static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1370 struct v4l2_format *f)
1371{
1372 struct cx23885_fh *fh = file->private_data;
1373 struct cx23885_dev *dev = fh->dev;
1374
1375 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1376 f->fmt.pix.bytesperline = 0;
1377 f->fmt.pix.sizeimage =
1378 dev->ts1.ts_packet_size * dev->ts1.ts_packet_count;
1379 f->fmt.pix.colorspace = 0;
1380 f->fmt.pix.width = dev->ts1.width;
1381 f->fmt.pix.height = dev->ts1.height;
1382 f->fmt.pix.field = fh->mpegq.field;
1383 dprintk(1, "VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
1384 dev->ts1.width, dev->ts1.height, fh->mpegq.field);
1385 return 0;
1386}
1387
1388static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1389 struct v4l2_format *f)
1390{
1391 struct cx23885_fh *fh = file->private_data;
1392 struct cx23885_dev *dev = fh->dev;
1393
1394 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1395 f->fmt.pix.bytesperline = 0;
1396 f->fmt.pix.sizeimage =
1397 dev->ts1.ts_packet_size * dev->ts1.ts_packet_count;
1398 f->fmt.pix.colorspace = 0;
1399 dprintk(1, "VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
1400 dev->ts1.width, dev->ts1.height, fh->mpegq.field);
1401 return 0;
1402}
1403
1404static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1405 struct v4l2_format *f)
1406{
1407 struct cx23885_fh *fh = file->private_data;
1408 struct cx23885_dev *dev = fh->dev;
1409
1410 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1411 f->fmt.pix.bytesperline = 0;
1412 f->fmt.pix.sizeimage =
1413 dev->ts1.ts_packet_size * dev->ts1.ts_packet_count;
1414 f->fmt.pix.colorspace = 0;
1415 dprintk(1, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
1416 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
1417 return 0;
1418}
1419
1420static int vidioc_reqbufs(struct file *file, void *priv,
1421 struct v4l2_requestbuffers *p)
1422{
1423 struct cx23885_fh *fh = file->private_data;
1424
1425 return videobuf_reqbufs(&fh->mpegq, p);
1426}
1427
1428static int vidioc_querybuf(struct file *file, void *priv,
1429 struct v4l2_buffer *p)
1430{
1431 struct cx23885_fh *fh = file->private_data;
1432
1433 return videobuf_querybuf(&fh->mpegq, p);
1434}
1435
1436static int vidioc_qbuf(struct file *file, void *priv,
1437 struct v4l2_buffer *p)
1438{
1439 struct cx23885_fh *fh = file->private_data;
1440
1441 return videobuf_qbuf(&fh->mpegq, p);
1442}
1443
1444static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1445{
1446 struct cx23885_fh *fh = priv;
1447
1448 return videobuf_dqbuf(&fh->mpegq, b, file->f_flags & O_NONBLOCK);
1449}
1450
1451
1452static int vidioc_streamon(struct file *file, void *priv,
1453 enum v4l2_buf_type i)
1454{
1455 struct cx23885_fh *fh = file->private_data;
1456
1457 return videobuf_streamon(&fh->mpegq);
1458}
1459
1460static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1461{
1462 struct cx23885_fh *fh = file->private_data;
1463
1464 return videobuf_streamoff(&fh->mpegq);
1465}
1466
1467static int vidioc_g_ext_ctrls(struct file *file, void *priv,
1468 struct v4l2_ext_controls *f)
1469{
1470 struct cx23885_fh *fh = priv;
1471 struct cx23885_dev *dev = fh->dev;
1472
1473 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
1474 return -EINVAL;
1475 return cx2341x_ext_ctrls(&dev->mpeg_params, 0, f, VIDIOC_G_EXT_CTRLS);
1476}
1477
1478static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1479 struct v4l2_ext_controls *f)
1480{
1481 struct cx23885_fh *fh = priv;
1482 struct cx23885_dev *dev = fh->dev;
1483 struct cx2341x_mpeg_params p;
1484 int err;
1485
1486 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
1487 return -EINVAL;
1488
1489 p = dev->mpeg_params;
1490 err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_S_EXT_CTRLS);
1491
1492 if (err == 0) {
1493 err = cx2341x_update(dev, cx23885_mbox_func,
1494 &dev->mpeg_params, &p);
1495 dev->mpeg_params = p;
1496 }
1497 return err;
1498}
1499
1500static int vidioc_try_ext_ctrls(struct file *file, void *priv,
1501 struct v4l2_ext_controls *f)
1502{
1503 struct cx23885_fh *fh = priv;
1504 struct cx23885_dev *dev = fh->dev;
1505 struct cx2341x_mpeg_params p;
1506 int err;
1507
1508 if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG)
1509 return -EINVAL;
1510
1511 p = dev->mpeg_params;
1512 err = cx2341x_ext_ctrls(&p, 0, f, VIDIOC_TRY_EXT_CTRLS);
1513 return err;
1514}
1515
1516static int vidioc_log_status(struct file *file, void *priv)
1517{
1518 struct cx23885_fh *fh = priv;
1519 struct cx23885_dev *dev = fh->dev;
1520 char name[32 + 2];
1521
1522 snprintf(name, sizeof(name), "%s/2", dev->name);
1523 printk(KERN_INFO
1524 "%s/2: ============ START LOG STATUS ============\n",
1525 dev->name);
1526 call_all(dev, core, log_status);
1527 cx2341x_log_status(&dev->mpeg_params, name);
1528 printk(KERN_INFO
1529 "%s/2: ============= END LOG STATUS =============\n",
1530 dev->name);
1531 return 0;
1532}
1533
1534static int vidioc_querymenu(struct file *file, void *priv,
1535 struct v4l2_querymenu *a)
1536{
1537 struct cx23885_fh *fh = priv;
1538 struct cx23885_dev *dev = fh->dev;
1539
1540 return cx23885_querymenu(dev, a);
1541}
1542
1543static int vidioc_queryctrl(struct file *file, void *priv,
1544 struct v4l2_queryctrl *c)
1545{
1546 struct cx23885_fh *fh = priv;
1547 struct cx23885_dev *dev = fh->dev;
1548
1549 return cx23885_queryctrl(dev, c);
1550}
1551
1552static int mpeg_open(struct file *file)
1553{
1554 struct cx23885_dev *dev = video_drvdata(file);
1555 struct cx23885_fh *fh;
1556
1557 dprintk(2, "%s()\n", __func__);
1558
1559 /* allocate + initialize per filehandle data */
1560 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1561 if (!fh)
1562 return -ENOMEM;
1563
1564 file->private_data = fh;
1565 fh->dev = dev;
1566
1567 videobuf_queue_sg_init(&fh->mpegq, &cx23885_qops,
1568 &dev->pci->dev, &dev->ts1.slock,
1569 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1570 V4L2_FIELD_INTERLACED,
1571 sizeof(struct cx23885_buffer),
1572 fh, NULL);
1573 return 0;
1574}
1575
1576static int mpeg_release(struct file *file)
1577{
1578 struct cx23885_fh *fh = file->private_data;
1579 struct cx23885_dev *dev = fh->dev;
1580
1581 dprintk(2, "%s()\n", __func__);
1582
1583 /* FIXME: Review this crap */
1584 /* Shut device down on last close */
1585 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1586 if (atomic_dec_return(&dev->v4l_reader_count) == 0) {
1587 /* stop mpeg capture */
1588 cx23885_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
1589 CX23885_END_NOW, CX23885_MPEG_CAPTURE,
1590 CX23885_RAW_BITS_NONE);
1591
1592 msleep(500);
1593 cx23885_417_check_encoder(dev);
1594
1595 cx23885_cancel_buffers(&fh->dev->ts1);
1596 }
1597 }
1598
1599 if (fh->mpegq.streaming)
1600 videobuf_streamoff(&fh->mpegq);
1601 if (fh->mpegq.reading)
1602 videobuf_read_stop(&fh->mpegq);
1603
1604 videobuf_mmap_free(&fh->mpegq);
1605 file->private_data = NULL;
1606 kfree(fh);
1607
1608 return 0;
1609}
1610
1611static ssize_t mpeg_read(struct file *file, char __user *data,
1612 size_t count, loff_t *ppos)
1613{
1614 struct cx23885_fh *fh = file->private_data;
1615 struct cx23885_dev *dev = fh->dev;
1616
1617 dprintk(2, "%s()\n", __func__);
1618
1619 /* Deal w/ A/V decoder * and mpeg encoder sync issues. */
1620 /* Start mpeg encoder on first read. */
1621 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1622 if (atomic_inc_return(&dev->v4l_reader_count) == 1) {
1623 if (cx23885_initialize_codec(dev, 1) < 0)
1624 return -EINVAL;
1625 }
1626 }
1627
1628 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1629 file->f_flags & O_NONBLOCK);
1630}
1631
1632static unsigned int mpeg_poll(struct file *file,
1633 struct poll_table_struct *wait)
1634{
1635 struct cx23885_fh *fh = file->private_data;
1636 struct cx23885_dev *dev = fh->dev;
1637
1638 dprintk(2, "%s\n", __func__);
1639
1640 return videobuf_poll_stream(file, &fh->mpegq, wait);
1641}
1642
1643static int mpeg_mmap(struct file *file, struct vm_area_struct *vma)
1644{
1645 struct cx23885_fh *fh = file->private_data;
1646 struct cx23885_dev *dev = fh->dev;
1647
1648 dprintk(2, "%s()\n", __func__);
1649
1650 return videobuf_mmap_mapper(&fh->mpegq, vma);
1651}
1652
1653static struct v4l2_file_operations mpeg_fops = {
1654 .owner = THIS_MODULE,
1655 .open = mpeg_open,
1656 .release = mpeg_release,
1657 .read = mpeg_read,
1658 .poll = mpeg_poll,
1659 .mmap = mpeg_mmap,
1660 .ioctl = video_ioctl2,
1661};
1662
1663static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1664 .vidioc_querystd = vidioc_g_std,
1665 .vidioc_g_std = vidioc_g_std,
1666 .vidioc_s_std = vidioc_s_std,
1667 .vidioc_enum_input = vidioc_enum_input,
1668 .vidioc_g_input = vidioc_g_input,
1669 .vidioc_s_input = vidioc_s_input,
1670 .vidioc_g_tuner = vidioc_g_tuner,
1671 .vidioc_s_tuner = vidioc_s_tuner,
1672 .vidioc_g_frequency = vidioc_g_frequency,
1673 .vidioc_s_frequency = vidioc_s_frequency,
1674 .vidioc_s_ctrl = vidioc_s_ctrl,
1675 .vidioc_g_ctrl = vidioc_g_ctrl,
1676 .vidioc_querycap = vidioc_querycap,
1677 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1678 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1679 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1680 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1681 .vidioc_reqbufs = vidioc_reqbufs,
1682 .vidioc_querybuf = vidioc_querybuf,
1683 .vidioc_qbuf = vidioc_qbuf,
1684 .vidioc_dqbuf = vidioc_dqbuf,
1685 .vidioc_streamon = vidioc_streamon,
1686 .vidioc_streamoff = vidioc_streamoff,
1687 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1688 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1689 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1690 .vidioc_log_status = vidioc_log_status,
1691 .vidioc_querymenu = vidioc_querymenu,
1692 .vidioc_queryctrl = vidioc_queryctrl,
1693 .vidioc_g_chip_ident = cx23885_g_chip_ident,
1694#ifdef CONFIG_VIDEO_ADV_DEBUG
1695 .vidioc_g_register = cx23885_g_register,
1696 .vidioc_s_register = cx23885_s_register,
1697#endif
1698};
1699
1700static struct video_device cx23885_mpeg_template = {
1701 .name = "cx23885",
1702 .fops = &mpeg_fops,
1703 .ioctl_ops = &mpeg_ioctl_ops,
1704 .tvnorms = CX23885_NORMS,
1705 .current_norm = V4L2_STD_NTSC_M,
1706};
1707
1708void cx23885_417_unregister(struct cx23885_dev *dev)
1709{
1710 dprintk(1, "%s()\n", __func__);
1711
1712 if (dev->v4l_device) {
1713 if (video_is_registered(dev->v4l_device))
1714 video_unregister_device(dev->v4l_device);
1715 else
1716 video_device_release(dev->v4l_device);
1717 dev->v4l_device = NULL;
1718 }
1719}
1720
1721static struct video_device *cx23885_video_dev_alloc(
1722 struct cx23885_tsport *tsport,
1723 struct pci_dev *pci,
1724 struct video_device *template,
1725 char *type)
1726{
1727 struct video_device *vfd;
1728 struct cx23885_dev *dev = tsport->dev;
1729
1730 dprintk(1, "%s()\n", __func__);
1731
1732 vfd = video_device_alloc();
1733 if (NULL == vfd)
1734 return NULL;
1735 *vfd = *template;
1736 snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
1737 cx23885_boards[tsport->dev->board].name, type);
1738 vfd->parent = &pci->dev;
1739 vfd->release = video_device_release;
1740 return vfd;
1741}
1742
1743int cx23885_417_register(struct cx23885_dev *dev)
1744{
1745 /* FIXME: Port1 hardcoded here */
1746 int err = -ENODEV;
1747 struct cx23885_tsport *tsport = &dev->ts1;
1748
1749 dprintk(1, "%s()\n", __func__);
1750
1751 if (cx23885_boards[dev->board].portb != CX23885_MPEG_ENCODER)
1752 return err;
1753
1754 /* Set default TV standard */
1755 dev->encodernorm = cx23885_tvnorms[0];
1756
1757 if (dev->encodernorm.id & V4L2_STD_525_60)
1758 tsport->height = 480;
1759 else
1760 tsport->height = 576;
1761
1762 tsport->width = 720;
1763 cx2341x_fill_defaults(&dev->mpeg_params);
1764
1765 dev->mpeg_params.port = CX2341X_PORT_SERIAL;
1766
1767 /* Allocate and initialize V4L video device */
1768 dev->v4l_device = cx23885_video_dev_alloc(tsport,
1769 dev->pci, &cx23885_mpeg_template, "mpeg");
1770 video_set_drvdata(dev->v4l_device, dev);
1771 err = video_register_device(dev->v4l_device,
1772 VFL_TYPE_GRABBER, -1);
1773 if (err < 0) {
1774 printk(KERN_INFO "%s: can't register mpeg device\n", dev->name);
1775 return err;
1776 }
1777
1778 printk(KERN_INFO "%s: registered device %s [mpeg]\n",
1779 dev->name, video_device_node_name(dev->v4l_device));
1780
1781 /* ST: Configure the encoder paramaters, but don't begin
1782 * encoding, this resolves an issue where the first time the
1783 * encoder is started video can be choppy.
1784 */
1785 cx23885_initialize_codec(dev, 0);
1786
1787 return 0;
1788}
1789
1790MODULE_FIRMWARE(CX23885_FIRM_IMAGE_NAME);
diff --git a/drivers/media/pci/cx23885/cx23885-alsa.c b/drivers/media/pci/cx23885/cx23885-alsa.c
new file mode 100644
index 000000000000..795169237e70
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-alsa.c
@@ -0,0 +1,535 @@
1/*
2 *
3 * Support for CX23885 analog audio capture
4 *
5 * (c) 2008 Mijhail Moreyra <mijhail.moreyra@gmail.com>
6 * Adapted from cx88-alsa.c
7 * (c) 2009 Steven Toth <stoth@kernellabs.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/device.h>
27#include <linux/interrupt.h>
28#include <linux/vmalloc.h>
29#include <linux/dma-mapping.h>
30#include <linux/pci.h>
31
32#include <asm/delay.h>
33
34#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/control.h>
38#include <sound/initval.h>
39
40#include <sound/tlv.h>
41
42
43#include "cx23885.h"
44#include "cx23885-reg.h"
45
46#define AUDIO_SRAM_CHANNEL SRAM_CH07
47
48#define dprintk(level, fmt, arg...) if (audio_debug >= level) \
49 printk(KERN_INFO "%s: " fmt, chip->dev->name , ## arg)
50
51#define dprintk_core(level, fmt, arg...) if (audio_debug >= level) \
52 printk(KERN_DEBUG "%s: " fmt, chip->dev->name , ## arg)
53
54/****************************************************************************
55 Module global static vars
56 ****************************************************************************/
57
58static unsigned int disable_analog_audio;
59module_param(disable_analog_audio, int, 0644);
60MODULE_PARM_DESC(disable_analog_audio, "disable analog audio ALSA driver");
61
62static unsigned int audio_debug;
63module_param(audio_debug, int, 0644);
64MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]");
65
66/****************************************************************************
67 Board specific funtions
68 ****************************************************************************/
69
70/* Constants taken from cx88-reg.h */
71#define AUD_INT_DN_RISCI1 (1 << 0)
72#define AUD_INT_UP_RISCI1 (1 << 1)
73#define AUD_INT_RDS_DN_RISCI1 (1 << 2)
74#define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
75#define AUD_INT_UP_RISCI2 (1 << 5)
76#define AUD_INT_RDS_DN_RISCI2 (1 << 6)
77#define AUD_INT_DN_SYNC (1 << 12)
78#define AUD_INT_UP_SYNC (1 << 13)
79#define AUD_INT_RDS_DN_SYNC (1 << 14)
80#define AUD_INT_OPC_ERR (1 << 16)
81#define AUD_INT_BER_IRQ (1 << 20)
82#define AUD_INT_MCHG_IRQ (1 << 21)
83#define GP_COUNT_CONTROL_RESET 0x3
84
85/*
86 * BOARD Specific: Sets audio DMA
87 */
88
89static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
90{
91 struct cx23885_audio_buffer *buf = chip->buf;
92 struct cx23885_dev *dev = chip->dev;
93 struct sram_channel *audio_ch =
94 &dev->sram_channels[AUDIO_SRAM_CHANNEL];
95
96 dprintk(1, "%s()\n", __func__);
97
98 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
99 cx_clear(AUD_INT_DMA_CTL, 0x11);
100
101 /* setup fifo + format - out channel */
102 cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
103 buf->risc.dma);
104
105 /* sets bpl size */
106 cx_write(AUD_INT_A_LNGTH, buf->bpl);
107
108 /* This is required to get good audio (1 seems to be ok) */
109 cx_write(AUD_INT_A_MODE, 1);
110
111 /* reset counter */
112 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
113 atomic_set(&chip->count, 0);
114
115 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
116 "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start+12)>>1,
117 chip->num_periods, buf->bpl * chip->num_periods);
118
119 /* Enables corresponding bits at AUD_INT_STAT */
120 cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
121 AUD_INT_DN_RISCI1);
122
123 /* Clean any pending interrupt bits already set */
124 cx_write(AUDIO_INT_INT_STAT, ~0);
125
126 /* enable audio irqs */
127 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
128
129 /* start dma */
130 cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
131 cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and
132 RISC enable */
133 if (audio_debug)
134 cx23885_sram_channel_dump(chip->dev, audio_ch);
135
136 return 0;
137}
138
139/*
140 * BOARD Specific: Resets audio DMA
141 */
142static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
143{
144 struct cx23885_dev *dev = chip->dev;
145 dprintk(1, "Stopping audio DMA\n");
146
147 /* stop dma */
148 cx_clear(AUD_INT_DMA_CTL, 0x11);
149
150 /* disable irqs */
151 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
152 cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
153 AUD_INT_DN_RISCI1);
154
155 if (audio_debug)
156 cx23885_sram_channel_dump(chip->dev,
157 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
158
159 return 0;
160}
161
162/*
163 * BOARD Specific: Handles audio IRQ
164 */
165int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
166{
167 struct cx23885_audio_dev *chip = dev->audio_dev;
168
169 if (0 == (status & mask))
170 return 0;
171
172 cx_write(AUDIO_INT_INT_STAT, status);
173
174 /* risc op code error */
175 if (status & AUD_INT_OPC_ERR) {
176 printk(KERN_WARNING "%s/1: Audio risc op code error\n",
177 dev->name);
178 cx_clear(AUD_INT_DMA_CTL, 0x11);
179 cx23885_sram_channel_dump(dev,
180 &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
181 }
182 if (status & AUD_INT_DN_SYNC) {
183 dprintk(1, "Downstream sync error\n");
184 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
185 return 1;
186 }
187 /* risc1 downstream */
188 if (status & AUD_INT_DN_RISCI1) {
189 atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT));
190 snd_pcm_period_elapsed(chip->substream);
191 }
192 /* FIXME: Any other status should deserve a special handling? */
193
194 return 1;
195}
196
197static int dsp_buffer_free(struct cx23885_audio_dev *chip)
198{
199 BUG_ON(!chip->dma_size);
200
201 dprintk(2, "Freeing buffer\n");
202 videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
203 videobuf_dma_free(chip->dma_risc);
204 btcx_riscmem_free(chip->pci, &chip->buf->risc);
205 kfree(chip->buf);
206
207 chip->dma_risc = NULL;
208 chip->dma_size = 0;
209
210 return 0;
211}
212
213/****************************************************************************
214 ALSA PCM Interface
215 ****************************************************************************/
216
217/*
218 * Digital hardware definition
219 */
220#define DEFAULT_FIFO_SIZE 4096
221
222static struct snd_pcm_hardware snd_cx23885_digital_hw = {
223 .info = SNDRV_PCM_INFO_MMAP |
224 SNDRV_PCM_INFO_INTERLEAVED |
225 SNDRV_PCM_INFO_BLOCK_TRANSFER |
226 SNDRV_PCM_INFO_MMAP_VALID,
227 .formats = SNDRV_PCM_FMTBIT_S16_LE,
228
229 .rates = SNDRV_PCM_RATE_48000,
230 .rate_min = 48000,
231 .rate_max = 48000,
232 .channels_min = 2,
233 .channels_max = 2,
234 /* Analog audio output will be full of clicks and pops if there
235 are not exactly four lines in the SRAM FIFO buffer. */
236 .period_bytes_min = DEFAULT_FIFO_SIZE/4,
237 .period_bytes_max = DEFAULT_FIFO_SIZE/4,
238 .periods_min = 1,
239 .periods_max = 1024,
240 .buffer_bytes_max = (1024*1024),
241};
242
243/*
244 * audio pcm capture open callback
245 */
246static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream)
247{
248 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
249 struct snd_pcm_runtime *runtime = substream->runtime;
250 int err;
251
252 if (!chip) {
253 printk(KERN_ERR "BUG: cx23885 can't find device struct."
254 " Can't proceed with open\n");
255 return -ENODEV;
256 }
257
258 err = snd_pcm_hw_constraint_pow2(runtime, 0,
259 SNDRV_PCM_HW_PARAM_PERIODS);
260 if (err < 0)
261 goto _error;
262
263 chip->substream = substream;
264
265 runtime->hw = snd_cx23885_digital_hw;
266
267 if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
268 DEFAULT_FIFO_SIZE) {
269 unsigned int bpl = chip->dev->
270 sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4;
271 bpl &= ~7; /* must be multiple of 8 */
272 runtime->hw.period_bytes_min = bpl;
273 runtime->hw.period_bytes_max = bpl;
274 }
275
276 return 0;
277_error:
278 dprintk(1, "Error opening PCM!\n");
279 return err;
280}
281
282/*
283 * audio close callback
284 */
285static int snd_cx23885_close(struct snd_pcm_substream *substream)
286{
287 return 0;
288}
289
290/*
291 * hw_params callback
292 */
293static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
294 struct snd_pcm_hw_params *hw_params)
295{
296 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
297 struct videobuf_dmabuf *dma;
298
299 struct cx23885_audio_buffer *buf;
300 int ret;
301
302 if (substream->runtime->dma_area) {
303 dsp_buffer_free(chip);
304 substream->runtime->dma_area = NULL;
305 }
306
307 chip->period_size = params_period_bytes(hw_params);
308 chip->num_periods = params_periods(hw_params);
309 chip->dma_size = chip->period_size * params_periods(hw_params);
310
311 BUG_ON(!chip->dma_size);
312 BUG_ON(chip->num_periods & (chip->num_periods-1));
313
314 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
315 if (NULL == buf)
316 return -ENOMEM;
317
318 buf->bpl = chip->period_size;
319
320 dma = &buf->dma;
321 videobuf_dma_init(dma);
322 ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
323 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
324 if (ret < 0)
325 goto error;
326
327 ret = videobuf_dma_map(&chip->pci->dev, dma);
328 if (ret < 0)
329 goto error;
330
331 ret = cx23885_risc_databuffer(chip->pci, &buf->risc, dma->sglist,
332 chip->period_size, chip->num_periods, 1);
333 if (ret < 0)
334 goto error;
335
336 /* Loop back to start of program */
337 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
338 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
339 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
340
341 chip->buf = buf;
342 chip->dma_risc = dma;
343
344 substream->runtime->dma_area = chip->dma_risc->vaddr;
345 substream->runtime->dma_bytes = chip->dma_size;
346 substream->runtime->dma_addr = 0;
347
348 return 0;
349
350error:
351 kfree(buf);
352 return ret;
353}
354
355/*
356 * hw free callback
357 */
358static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
359{
360
361 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
362
363 if (substream->runtime->dma_area) {
364 dsp_buffer_free(chip);
365 substream->runtime->dma_area = NULL;
366 }
367
368 return 0;
369}
370
371/*
372 * prepare callback
373 */
374static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
375{
376 return 0;
377}
378
379/*
380 * trigger callback
381 */
382static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
383 int cmd)
384{
385 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
386 int err;
387
388 /* Local interrupts are already disabled by ALSA */
389 spin_lock(&chip->lock);
390
391 switch (cmd) {
392 case SNDRV_PCM_TRIGGER_START:
393 err = cx23885_start_audio_dma(chip);
394 break;
395 case SNDRV_PCM_TRIGGER_STOP:
396 err = cx23885_stop_audio_dma(chip);
397 break;
398 default:
399 err = -EINVAL;
400 break;
401 }
402
403 spin_unlock(&chip->lock);
404
405 return err;
406}
407
408/*
409 * pointer callback
410 */
411static snd_pcm_uframes_t snd_cx23885_pointer(
412 struct snd_pcm_substream *substream)
413{
414 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
415 struct snd_pcm_runtime *runtime = substream->runtime;
416 u16 count;
417
418 count = atomic_read(&chip->count);
419
420 return runtime->period_size * (count & (runtime->periods-1));
421}
422
423/*
424 * page callback (needed for mmap)
425 */
426static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
427 unsigned long offset)
428{
429 void *pageptr = substream->runtime->dma_area + offset;
430 return vmalloc_to_page(pageptr);
431}
432
433/*
434 * operators
435 */
436static struct snd_pcm_ops snd_cx23885_pcm_ops = {
437 .open = snd_cx23885_pcm_open,
438 .close = snd_cx23885_close,
439 .ioctl = snd_pcm_lib_ioctl,
440 .hw_params = snd_cx23885_hw_params,
441 .hw_free = snd_cx23885_hw_free,
442 .prepare = snd_cx23885_prepare,
443 .trigger = snd_cx23885_card_trigger,
444 .pointer = snd_cx23885_pointer,
445 .page = snd_cx23885_page,
446};
447
448/*
449 * create a PCM device
450 */
451static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
452 char *name)
453{
454 int err;
455 struct snd_pcm *pcm;
456
457 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
458 if (err < 0)
459 return err;
460 pcm->private_data = chip;
461 strcpy(pcm->name, name);
462 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
463
464 return 0;
465}
466
467/****************************************************************************
468 Basic Flow for Sound Devices
469 ****************************************************************************/
470
471/*
472 * Alsa Constructor - Component probe
473 */
474
475struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
476{
477 struct snd_card *card;
478 struct cx23885_audio_dev *chip;
479 int err;
480
481 if (disable_analog_audio)
482 return NULL;
483
484 if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
485 printk(KERN_WARNING "%s(): Missing SRAM channel configuration "
486 "for analog TV Audio\n", __func__);
487 return NULL;
488 }
489
490 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
491 THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
492 if (err < 0)
493 goto error;
494
495 chip = (struct cx23885_audio_dev *) card->private_data;
496 chip->dev = dev;
497 chip->pci = dev->pci;
498 chip->card = card;
499 spin_lock_init(&chip->lock);
500
501 snd_card_set_dev(card, &dev->pci->dev);
502
503 err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
504 if (err < 0)
505 goto error;
506
507 strcpy(card->driver, "CX23885");
508 sprintf(card->shortname, "Conexant CX23885");
509 sprintf(card->longname, "%s at %s", card->shortname, dev->name);
510
511 err = snd_card_register(card);
512 if (err < 0)
513 goto error;
514
515 dprintk(0, "registered ALSA audio device\n");
516
517 return chip;
518
519error:
520 snd_card_free(card);
521 printk(KERN_ERR "%s(): Failed to register analog "
522 "audio adapter\n", __func__);
523
524 return NULL;
525}
526
527/*
528 * ALSA destructor
529 */
530void cx23885_audio_unregister(struct cx23885_dev *dev)
531{
532 struct cx23885_audio_dev *chip = dev->audio_dev;
533
534 snd_card_free(chip->card);
535}
diff --git a/drivers/media/pci/cx23885/cx23885-av.c b/drivers/media/pci/cx23885/cx23885-av.c
new file mode 100644
index 000000000000..134ebddd860f
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-av.c
@@ -0,0 +1,35 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * AV device support routines - non-input, non-vl42_subdev routines
5 *
6 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#include "cx23885.h"
25
26void cx23885_av_work_handler(struct work_struct *work)
27{
28 struct cx23885_dev *dev =
29 container_of(work, struct cx23885_dev, cx25840_work);
30 bool handled;
31
32 v4l2_subdev_call(dev->sd_cx25840, core, interrupt_service_routine,
33 PCI_MSK_AV_CORE, &handled);
34 cx23885_irq_enable(dev, PCI_MSK_AV_CORE);
35}
diff --git a/drivers/media/pci/cx23885/cx23885-av.h b/drivers/media/pci/cx23885/cx23885-av.h
new file mode 100644
index 000000000000..d2915c3e53a2
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-av.h
@@ -0,0 +1,27 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * AV device support routines - non-input, non-vl42_subdev routines
5 *
6 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#ifndef _CX23885_AV_H_
25#define _CX23885_AV_H_
26void cx23885_av_work_handler(struct work_struct *work);
27#endif
diff --git a/drivers/media/pci/cx23885/cx23885-cards.c b/drivers/media/pci/cx23885/cx23885-cards.c
new file mode 100644
index 000000000000..d365e9a8efc4
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-cards.c
@@ -0,0 +1,1684 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/pci.h>
25#include <linux/delay.h>
26#include <media/cx25840.h>
27#include <linux/firmware.h>
28#include <misc/altera.h>
29
30#include "cx23885.h"
31#include "tuner-xc2028.h"
32#include "netup-eeprom.h"
33#include "netup-init.h"
34#include "altera-ci.h"
35#include "xc4000.h"
36#include "xc5000.h"
37#include "cx23888-ir.h"
38
39static unsigned int netup_card_rev = 1;
40module_param(netup_card_rev, int, 0644);
41MODULE_PARM_DESC(netup_card_rev,
42 "NetUP Dual DVB-T/C CI card revision");
43static unsigned int enable_885_ir;
44module_param(enable_885_ir, int, 0644);
45MODULE_PARM_DESC(enable_885_ir,
46 "Enable integrated IR controller for supported\n"
47 "\t\t CX2388[57] boards that are wired for it:\n"
48 "\t\t\tHVR-1250 (reported safe)\n"
49 "\t\t\tTerraTec Cinergy T PCIe Dual (not well tested, appears to be safe)\n"
50 "\t\t\tTeVii S470 (reported unsafe)\n"
51 "\t\t This can cause an interrupt storm with some cards.\n"
52 "\t\t Default: 0 [Disabled]");
53
54/* ------------------------------------------------------------------ */
55/* board config info */
56
57struct cx23885_board cx23885_boards[] = {
58 [CX23885_BOARD_UNKNOWN] = {
59 .name = "UNKNOWN/GENERIC",
60 /* Ensure safe default for unknown boards */
61 .clk_freq = 0,
62 .input = {{
63 .type = CX23885_VMUX_COMPOSITE1,
64 .vmux = 0,
65 }, {
66 .type = CX23885_VMUX_COMPOSITE2,
67 .vmux = 1,
68 }, {
69 .type = CX23885_VMUX_COMPOSITE3,
70 .vmux = 2,
71 }, {
72 .type = CX23885_VMUX_COMPOSITE4,
73 .vmux = 3,
74 } },
75 },
76 [CX23885_BOARD_HAUPPAUGE_HVR1800lp] = {
77 .name = "Hauppauge WinTV-HVR1800lp",
78 .portc = CX23885_MPEG_DVB,
79 .input = {{
80 .type = CX23885_VMUX_TELEVISION,
81 .vmux = 0,
82 .gpio0 = 0xff00,
83 }, {
84 .type = CX23885_VMUX_DEBUG,
85 .vmux = 0,
86 .gpio0 = 0xff01,
87 }, {
88 .type = CX23885_VMUX_COMPOSITE1,
89 .vmux = 1,
90 .gpio0 = 0xff02,
91 }, {
92 .type = CX23885_VMUX_SVIDEO,
93 .vmux = 2,
94 .gpio0 = 0xff02,
95 } },
96 },
97 [CX23885_BOARD_HAUPPAUGE_HVR1800] = {
98 .name = "Hauppauge WinTV-HVR1800",
99 .porta = CX23885_ANALOG_VIDEO,
100 .portb = CX23885_MPEG_ENCODER,
101 .portc = CX23885_MPEG_DVB,
102 .tuner_type = TUNER_PHILIPS_TDA8290,
103 .tuner_addr = 0x42, /* 0x84 >> 1 */
104 .tuner_bus = 1,
105 .input = {{
106 .type = CX23885_VMUX_TELEVISION,
107 .vmux = CX25840_VIN7_CH3 |
108 CX25840_VIN5_CH2 |
109 CX25840_VIN2_CH1,
110 .amux = CX25840_AUDIO8,
111 .gpio0 = 0,
112 }, {
113 .type = CX23885_VMUX_COMPOSITE1,
114 .vmux = CX25840_VIN7_CH3 |
115 CX25840_VIN4_CH2 |
116 CX25840_VIN6_CH1,
117 .amux = CX25840_AUDIO7,
118 .gpio0 = 0,
119 }, {
120 .type = CX23885_VMUX_SVIDEO,
121 .vmux = CX25840_VIN7_CH3 |
122 CX25840_VIN4_CH2 |
123 CX25840_VIN8_CH1 |
124 CX25840_SVIDEO_ON,
125 .amux = CX25840_AUDIO7,
126 .gpio0 = 0,
127 } },
128 },
129 [CX23885_BOARD_HAUPPAUGE_HVR1250] = {
130 .name = "Hauppauge WinTV-HVR1250",
131 .porta = CX23885_ANALOG_VIDEO,
132 .portc = CX23885_MPEG_DVB,
133#ifdef MT2131_NO_ANALOG_SUPPORT_YET
134 .tuner_type = TUNER_PHILIPS_TDA8290,
135 .tuner_addr = 0x42, /* 0x84 >> 1 */
136 .tuner_bus = 1,
137#endif
138 .force_bff = 1,
139 .input = {{
140#ifdef MT2131_NO_ANALOG_SUPPORT_YET
141 .type = CX23885_VMUX_TELEVISION,
142 .vmux = CX25840_VIN7_CH3 |
143 CX25840_VIN5_CH2 |
144 CX25840_VIN2_CH1,
145 .amux = CX25840_AUDIO8,
146 .gpio0 = 0xff00,
147 }, {
148#endif
149 .type = CX23885_VMUX_COMPOSITE1,
150 .vmux = CX25840_VIN7_CH3 |
151 CX25840_VIN4_CH2 |
152 CX25840_VIN6_CH1,
153 .amux = CX25840_AUDIO7,
154 .gpio0 = 0xff02,
155 }, {
156 .type = CX23885_VMUX_SVIDEO,
157 .vmux = CX25840_VIN7_CH3 |
158 CX25840_VIN4_CH2 |
159 CX25840_VIN8_CH1 |
160 CX25840_SVIDEO_ON,
161 .amux = CX25840_AUDIO7,
162 .gpio0 = 0xff02,
163 } },
164 },
165 [CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP] = {
166 .name = "DViCO FusionHDTV5 Express",
167 .portb = CX23885_MPEG_DVB,
168 },
169 [CX23885_BOARD_HAUPPAUGE_HVR1500Q] = {
170 .name = "Hauppauge WinTV-HVR1500Q",
171 .portc = CX23885_MPEG_DVB,
172 },
173 [CX23885_BOARD_HAUPPAUGE_HVR1500] = {
174 .name = "Hauppauge WinTV-HVR1500",
175 .porta = CX23885_ANALOG_VIDEO,
176 .portc = CX23885_MPEG_DVB,
177 .tuner_type = TUNER_XC2028,
178 .tuner_addr = 0x61, /* 0xc2 >> 1 */
179 .input = {{
180 .type = CX23885_VMUX_TELEVISION,
181 .vmux = CX25840_VIN7_CH3 |
182 CX25840_VIN5_CH2 |
183 CX25840_VIN2_CH1,
184 .gpio0 = 0,
185 }, {
186 .type = CX23885_VMUX_COMPOSITE1,
187 .vmux = CX25840_VIN7_CH3 |
188 CX25840_VIN4_CH2 |
189 CX25840_VIN6_CH1,
190 .gpio0 = 0,
191 }, {
192 .type = CX23885_VMUX_SVIDEO,
193 .vmux = CX25840_VIN7_CH3 |
194 CX25840_VIN4_CH2 |
195 CX25840_VIN8_CH1 |
196 CX25840_SVIDEO_ON,
197 .gpio0 = 0,
198 } },
199 },
200 [CX23885_BOARD_HAUPPAUGE_HVR1200] = {
201 .name = "Hauppauge WinTV-HVR1200",
202 .portc = CX23885_MPEG_DVB,
203 },
204 [CX23885_BOARD_HAUPPAUGE_HVR1700] = {
205 .name = "Hauppauge WinTV-HVR1700",
206 .portc = CX23885_MPEG_DVB,
207 },
208 [CX23885_BOARD_HAUPPAUGE_HVR1400] = {
209 .name = "Hauppauge WinTV-HVR1400",
210 .portc = CX23885_MPEG_DVB,
211 },
212 [CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP] = {
213 .name = "DViCO FusionHDTV7 Dual Express",
214 .portb = CX23885_MPEG_DVB,
215 .portc = CX23885_MPEG_DVB,
216 },
217 [CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP] = {
218 .name = "DViCO FusionHDTV DVB-T Dual Express",
219 .portb = CX23885_MPEG_DVB,
220 .portc = CX23885_MPEG_DVB,
221 },
222 [CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H] = {
223 .name = "Leadtek Winfast PxDVR3200 H",
224 .portc = CX23885_MPEG_DVB,
225 },
226 [CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000] = {
227 .name = "Leadtek Winfast PxDVR3200 H XC4000",
228 .porta = CX23885_ANALOG_VIDEO,
229 .portc = CX23885_MPEG_DVB,
230 .tuner_type = TUNER_XC4000,
231 .tuner_addr = 0x61,
232 .radio_type = UNSET,
233 .radio_addr = ADDR_UNSET,
234 .input = {{
235 .type = CX23885_VMUX_TELEVISION,
236 .vmux = CX25840_VIN2_CH1 |
237 CX25840_VIN5_CH2 |
238 CX25840_NONE0_CH3,
239 }, {
240 .type = CX23885_VMUX_COMPOSITE1,
241 .vmux = CX25840_COMPOSITE1,
242 }, {
243 .type = CX23885_VMUX_SVIDEO,
244 .vmux = CX25840_SVIDEO_LUMA3 |
245 CX25840_SVIDEO_CHROMA4,
246 }, {
247 .type = CX23885_VMUX_COMPONENT,
248 .vmux = CX25840_VIN7_CH1 |
249 CX25840_VIN6_CH2 |
250 CX25840_VIN8_CH3 |
251 CX25840_COMPONENT_ON,
252 } },
253 },
254 [CX23885_BOARD_COMPRO_VIDEOMATE_E650F] = {
255 .name = "Compro VideoMate E650F",
256 .portc = CX23885_MPEG_DVB,
257 },
258 [CX23885_BOARD_TBS_6920] = {
259 .name = "TurboSight TBS 6920",
260 .portb = CX23885_MPEG_DVB,
261 },
262 [CX23885_BOARD_TEVII_S470] = {
263 .name = "TeVii S470",
264 .portb = CX23885_MPEG_DVB,
265 },
266 [CX23885_BOARD_DVBWORLD_2005] = {
267 .name = "DVBWorld DVB-S2 2005",
268 .portb = CX23885_MPEG_DVB,
269 },
270 [CX23885_BOARD_NETUP_DUAL_DVBS2_CI] = {
271 .ci_type = 1,
272 .name = "NetUP Dual DVB-S2 CI",
273 .portb = CX23885_MPEG_DVB,
274 .portc = CX23885_MPEG_DVB,
275 },
276 [CX23885_BOARD_HAUPPAUGE_HVR1270] = {
277 .name = "Hauppauge WinTV-HVR1270",
278 .portc = CX23885_MPEG_DVB,
279 },
280 [CX23885_BOARD_HAUPPAUGE_HVR1275] = {
281 .name = "Hauppauge WinTV-HVR1275",
282 .portc = CX23885_MPEG_DVB,
283 },
284 [CX23885_BOARD_HAUPPAUGE_HVR1255] = {
285 .name = "Hauppauge WinTV-HVR1255",
286 .porta = CX23885_ANALOG_VIDEO,
287 .portc = CX23885_MPEG_DVB,
288 .tuner_type = TUNER_ABSENT,
289 .tuner_addr = 0x42, /* 0x84 >> 1 */
290 .force_bff = 1,
291 .input = {{
292 .type = CX23885_VMUX_TELEVISION,
293 .vmux = CX25840_VIN7_CH3 |
294 CX25840_VIN5_CH2 |
295 CX25840_VIN2_CH1 |
296 CX25840_DIF_ON,
297 .amux = CX25840_AUDIO8,
298 }, {
299 .type = CX23885_VMUX_COMPOSITE1,
300 .vmux = CX25840_VIN7_CH3 |
301 CX25840_VIN4_CH2 |
302 CX25840_VIN6_CH1,
303 .amux = CX25840_AUDIO7,
304 }, {
305 .type = CX23885_VMUX_SVIDEO,
306 .vmux = CX25840_VIN7_CH3 |
307 CX25840_VIN4_CH2 |
308 CX25840_VIN8_CH1 |
309 CX25840_SVIDEO_ON,
310 .amux = CX25840_AUDIO7,
311 } },
312 },
313 [CX23885_BOARD_HAUPPAUGE_HVR1255_22111] = {
314 .name = "Hauppauge WinTV-HVR1255",
315 .porta = CX23885_ANALOG_VIDEO,
316 .portc = CX23885_MPEG_DVB,
317 .tuner_type = TUNER_ABSENT,
318 .tuner_addr = 0x42, /* 0x84 >> 1 */
319 .force_bff = 1,
320 .input = {{
321 .type = CX23885_VMUX_TELEVISION,
322 .vmux = CX25840_VIN7_CH3 |
323 CX25840_VIN5_CH2 |
324 CX25840_VIN2_CH1 |
325 CX25840_DIF_ON,
326 .amux = CX25840_AUDIO8,
327 }, {
328 .type = CX23885_VMUX_SVIDEO,
329 .vmux = CX25840_VIN7_CH3 |
330 CX25840_VIN4_CH2 |
331 CX25840_VIN8_CH1 |
332 CX25840_SVIDEO_ON,
333 .amux = CX25840_AUDIO7,
334 } },
335 },
336 [CX23885_BOARD_HAUPPAUGE_HVR1210] = {
337 .name = "Hauppauge WinTV-HVR1210",
338 .portc = CX23885_MPEG_DVB,
339 },
340 [CX23885_BOARD_MYGICA_X8506] = {
341 .name = "Mygica X8506 DMB-TH",
342 .tuner_type = TUNER_XC5000,
343 .tuner_addr = 0x61,
344 .tuner_bus = 1,
345 .porta = CX23885_ANALOG_VIDEO,
346 .portb = CX23885_MPEG_DVB,
347 .input = {
348 {
349 .type = CX23885_VMUX_TELEVISION,
350 .vmux = CX25840_COMPOSITE2,
351 },
352 {
353 .type = CX23885_VMUX_COMPOSITE1,
354 .vmux = CX25840_COMPOSITE8,
355 },
356 {
357 .type = CX23885_VMUX_SVIDEO,
358 .vmux = CX25840_SVIDEO_LUMA3 |
359 CX25840_SVIDEO_CHROMA4,
360 },
361 {
362 .type = CX23885_VMUX_COMPONENT,
363 .vmux = CX25840_COMPONENT_ON |
364 CX25840_VIN1_CH1 |
365 CX25840_VIN6_CH2 |
366 CX25840_VIN7_CH3,
367 },
368 },
369 },
370 [CX23885_BOARD_MAGICPRO_PROHDTVE2] = {
371 .name = "Magic-Pro ProHDTV Extreme 2",
372 .tuner_type = TUNER_XC5000,
373 .tuner_addr = 0x61,
374 .tuner_bus = 1,
375 .porta = CX23885_ANALOG_VIDEO,
376 .portb = CX23885_MPEG_DVB,
377 .input = {
378 {
379 .type = CX23885_VMUX_TELEVISION,
380 .vmux = CX25840_COMPOSITE2,
381 },
382 {
383 .type = CX23885_VMUX_COMPOSITE1,
384 .vmux = CX25840_COMPOSITE8,
385 },
386 {
387 .type = CX23885_VMUX_SVIDEO,
388 .vmux = CX25840_SVIDEO_LUMA3 |
389 CX25840_SVIDEO_CHROMA4,
390 },
391 {
392 .type = CX23885_VMUX_COMPONENT,
393 .vmux = CX25840_COMPONENT_ON |
394 CX25840_VIN1_CH1 |
395 CX25840_VIN6_CH2 |
396 CX25840_VIN7_CH3,
397 },
398 },
399 },
400 [CX23885_BOARD_HAUPPAUGE_HVR1850] = {
401 .name = "Hauppauge WinTV-HVR1850",
402 .porta = CX23885_ANALOG_VIDEO,
403 .portb = CX23885_MPEG_ENCODER,
404 .portc = CX23885_MPEG_DVB,
405 .tuner_type = TUNER_ABSENT,
406 .tuner_addr = 0x42, /* 0x84 >> 1 */
407 .force_bff = 1,
408 .input = {{
409 .type = CX23885_VMUX_TELEVISION,
410 .vmux = CX25840_VIN7_CH3 |
411 CX25840_VIN5_CH2 |
412 CX25840_VIN2_CH1 |
413 CX25840_DIF_ON,
414 .amux = CX25840_AUDIO8,
415 }, {
416 .type = CX23885_VMUX_COMPOSITE1,
417 .vmux = CX25840_VIN7_CH3 |
418 CX25840_VIN4_CH2 |
419 CX25840_VIN6_CH1,
420 .amux = CX25840_AUDIO7,
421 }, {
422 .type = CX23885_VMUX_SVIDEO,
423 .vmux = CX25840_VIN7_CH3 |
424 CX25840_VIN4_CH2 |
425 CX25840_VIN8_CH1 |
426 CX25840_SVIDEO_ON,
427 .amux = CX25840_AUDIO7,
428 } },
429 },
430 [CX23885_BOARD_COMPRO_VIDEOMATE_E800] = {
431 .name = "Compro VideoMate E800",
432 .portc = CX23885_MPEG_DVB,
433 },
434 [CX23885_BOARD_HAUPPAUGE_HVR1290] = {
435 .name = "Hauppauge WinTV-HVR1290",
436 .portc = CX23885_MPEG_DVB,
437 },
438 [CX23885_BOARD_MYGICA_X8558PRO] = {
439 .name = "Mygica X8558 PRO DMB-TH",
440 .portb = CX23885_MPEG_DVB,
441 .portc = CX23885_MPEG_DVB,
442 },
443 [CX23885_BOARD_LEADTEK_WINFAST_PXTV1200] = {
444 .name = "LEADTEK WinFast PxTV1200",
445 .porta = CX23885_ANALOG_VIDEO,
446 .tuner_type = TUNER_XC2028,
447 .tuner_addr = 0x61,
448 .tuner_bus = 1,
449 .input = {{
450 .type = CX23885_VMUX_TELEVISION,
451 .vmux = CX25840_VIN2_CH1 |
452 CX25840_VIN5_CH2 |
453 CX25840_NONE0_CH3,
454 }, {
455 .type = CX23885_VMUX_COMPOSITE1,
456 .vmux = CX25840_COMPOSITE1,
457 }, {
458 .type = CX23885_VMUX_SVIDEO,
459 .vmux = CX25840_SVIDEO_LUMA3 |
460 CX25840_SVIDEO_CHROMA4,
461 }, {
462 .type = CX23885_VMUX_COMPONENT,
463 .vmux = CX25840_VIN7_CH1 |
464 CX25840_VIN6_CH2 |
465 CX25840_VIN8_CH3 |
466 CX25840_COMPONENT_ON,
467 } },
468 },
469 [CX23885_BOARD_GOTVIEW_X5_3D_HYBRID] = {
470 .name = "GoTView X5 3D Hybrid",
471 .tuner_type = TUNER_XC5000,
472 .tuner_addr = 0x64,
473 .tuner_bus = 1,
474 .porta = CX23885_ANALOG_VIDEO,
475 .portb = CX23885_MPEG_DVB,
476 .input = {{
477 .type = CX23885_VMUX_TELEVISION,
478 .vmux = CX25840_VIN2_CH1 |
479 CX25840_VIN5_CH2,
480 .gpio0 = 0x02,
481 }, {
482 .type = CX23885_VMUX_COMPOSITE1,
483 .vmux = CX23885_VMUX_COMPOSITE1,
484 }, {
485 .type = CX23885_VMUX_SVIDEO,
486 .vmux = CX25840_SVIDEO_LUMA3 |
487 CX25840_SVIDEO_CHROMA4,
488 } },
489 },
490 [CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF] = {
491 .ci_type = 2,
492 .name = "NetUP Dual DVB-T/C-CI RF",
493 .porta = CX23885_ANALOG_VIDEO,
494 .portb = CX23885_MPEG_DVB,
495 .portc = CX23885_MPEG_DVB,
496 .num_fds_portb = 2,
497 .num_fds_portc = 2,
498 .tuner_type = TUNER_XC5000,
499 .tuner_addr = 0x64,
500 .input = { {
501 .type = CX23885_VMUX_TELEVISION,
502 .vmux = CX25840_COMPOSITE1,
503 } },
504 },
505 [CX23885_BOARD_MPX885] = {
506 .name = "MPX-885",
507 .porta = CX23885_ANALOG_VIDEO,
508 .input = {{
509 .type = CX23885_VMUX_COMPOSITE1,
510 .vmux = CX25840_COMPOSITE1,
511 .amux = CX25840_AUDIO6,
512 .gpio0 = 0,
513 }, {
514 .type = CX23885_VMUX_COMPOSITE2,
515 .vmux = CX25840_COMPOSITE2,
516 .amux = CX25840_AUDIO6,
517 .gpio0 = 0,
518 }, {
519 .type = CX23885_VMUX_COMPOSITE3,
520 .vmux = CX25840_COMPOSITE3,
521 .amux = CX25840_AUDIO7,
522 .gpio0 = 0,
523 }, {
524 .type = CX23885_VMUX_COMPOSITE4,
525 .vmux = CX25840_COMPOSITE4,
526 .amux = CX25840_AUDIO7,
527 .gpio0 = 0,
528 } },
529 },
530 [CX23885_BOARD_MYGICA_X8507] = {
531 .name = "Mygica X8507",
532 .tuner_type = TUNER_XC5000,
533 .tuner_addr = 0x61,
534 .tuner_bus = 1,
535 .porta = CX23885_ANALOG_VIDEO,
536 .input = {
537 {
538 .type = CX23885_VMUX_TELEVISION,
539 .vmux = CX25840_COMPOSITE2,
540 .amux = CX25840_AUDIO8,
541 },
542 {
543 .type = CX23885_VMUX_COMPOSITE1,
544 .vmux = CX25840_COMPOSITE8,
545 },
546 {
547 .type = CX23885_VMUX_SVIDEO,
548 .vmux = CX25840_SVIDEO_LUMA3 |
549 CX25840_SVIDEO_CHROMA4,
550 },
551 {
552 .type = CX23885_VMUX_COMPONENT,
553 .vmux = CX25840_COMPONENT_ON |
554 CX25840_VIN1_CH1 |
555 CX25840_VIN6_CH2 |
556 CX25840_VIN7_CH3,
557 },
558 },
559 },
560 [CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL] = {
561 .name = "TerraTec Cinergy T PCIe Dual",
562 .portb = CX23885_MPEG_DVB,
563 .portc = CX23885_MPEG_DVB,
564 },
565 [CX23885_BOARD_TEVII_S471] = {
566 .name = "TeVii S471",
567 .portb = CX23885_MPEG_DVB,
568 }
569};
570const unsigned int cx23885_bcount = ARRAY_SIZE(cx23885_boards);
571
572/* ------------------------------------------------------------------ */
573/* PCI subsystem IDs */
574
575struct cx23885_subid cx23885_subids[] = {
576 {
577 .subvendor = 0x0070,
578 .subdevice = 0x3400,
579 .card = CX23885_BOARD_UNKNOWN,
580 }, {
581 .subvendor = 0x0070,
582 .subdevice = 0x7600,
583 .card = CX23885_BOARD_HAUPPAUGE_HVR1800lp,
584 }, {
585 .subvendor = 0x0070,
586 .subdevice = 0x7800,
587 .card = CX23885_BOARD_HAUPPAUGE_HVR1800,
588 }, {
589 .subvendor = 0x0070,
590 .subdevice = 0x7801,
591 .card = CX23885_BOARD_HAUPPAUGE_HVR1800,
592 }, {
593 .subvendor = 0x0070,
594 .subdevice = 0x7809,
595 .card = CX23885_BOARD_HAUPPAUGE_HVR1800,
596 }, {
597 .subvendor = 0x0070,
598 .subdevice = 0x7911,
599 .card = CX23885_BOARD_HAUPPAUGE_HVR1250,
600 }, {
601 .subvendor = 0x18ac,
602 .subdevice = 0xd500,
603 .card = CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP,
604 }, {
605 .subvendor = 0x0070,
606 .subdevice = 0x7790,
607 .card = CX23885_BOARD_HAUPPAUGE_HVR1500Q,
608 }, {
609 .subvendor = 0x0070,
610 .subdevice = 0x7797,
611 .card = CX23885_BOARD_HAUPPAUGE_HVR1500Q,
612 }, {
613 .subvendor = 0x0070,
614 .subdevice = 0x7710,
615 .card = CX23885_BOARD_HAUPPAUGE_HVR1500,
616 }, {
617 .subvendor = 0x0070,
618 .subdevice = 0x7717,
619 .card = CX23885_BOARD_HAUPPAUGE_HVR1500,
620 }, {
621 .subvendor = 0x0070,
622 .subdevice = 0x71d1,
623 .card = CX23885_BOARD_HAUPPAUGE_HVR1200,
624 }, {
625 .subvendor = 0x0070,
626 .subdevice = 0x71d3,
627 .card = CX23885_BOARD_HAUPPAUGE_HVR1200,
628 }, {
629 .subvendor = 0x0070,
630 .subdevice = 0x8101,
631 .card = CX23885_BOARD_HAUPPAUGE_HVR1700,
632 }, {
633 .subvendor = 0x0070,
634 .subdevice = 0x8010,
635 .card = CX23885_BOARD_HAUPPAUGE_HVR1400,
636 }, {
637 .subvendor = 0x18ac,
638 .subdevice = 0xd618,
639 .card = CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP,
640 }, {
641 .subvendor = 0x18ac,
642 .subdevice = 0xdb78,
643 .card = CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP,
644 }, {
645 .subvendor = 0x107d,
646 .subdevice = 0x6681,
647 .card = CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H,
648 }, {
649 .subvendor = 0x107d,
650 .subdevice = 0x6f39,
651 .card = CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000,
652 }, {
653 .subvendor = 0x185b,
654 .subdevice = 0xe800,
655 .card = CX23885_BOARD_COMPRO_VIDEOMATE_E650F,
656 }, {
657 .subvendor = 0x6920,
658 .subdevice = 0x8888,
659 .card = CX23885_BOARD_TBS_6920,
660 }, {
661 .subvendor = 0xd470,
662 .subdevice = 0x9022,
663 .card = CX23885_BOARD_TEVII_S470,
664 }, {
665 .subvendor = 0x0001,
666 .subdevice = 0x2005,
667 .card = CX23885_BOARD_DVBWORLD_2005,
668 }, {
669 .subvendor = 0x1b55,
670 .subdevice = 0x2a2c,
671 .card = CX23885_BOARD_NETUP_DUAL_DVBS2_CI,
672 }, {
673 .subvendor = 0x0070,
674 .subdevice = 0x2211,
675 .card = CX23885_BOARD_HAUPPAUGE_HVR1270,
676 }, {
677 .subvendor = 0x0070,
678 .subdevice = 0x2215,
679 .card = CX23885_BOARD_HAUPPAUGE_HVR1275,
680 }, {
681 .subvendor = 0x0070,
682 .subdevice = 0x221d,
683 .card = CX23885_BOARD_HAUPPAUGE_HVR1275,
684 }, {
685 .subvendor = 0x0070,
686 .subdevice = 0x2251,
687 .card = CX23885_BOARD_HAUPPAUGE_HVR1255,
688 }, {
689 .subvendor = 0x0070,
690 .subdevice = 0x2259,
691 .card = CX23885_BOARD_HAUPPAUGE_HVR1255_22111,
692 }, {
693 .subvendor = 0x0070,
694 .subdevice = 0x2291,
695 .card = CX23885_BOARD_HAUPPAUGE_HVR1210,
696 }, {
697 .subvendor = 0x0070,
698 .subdevice = 0x2295,
699 .card = CX23885_BOARD_HAUPPAUGE_HVR1210,
700 }, {
701 .subvendor = 0x0070,
702 .subdevice = 0x2299,
703 .card = CX23885_BOARD_HAUPPAUGE_HVR1210,
704 }, {
705 .subvendor = 0x0070,
706 .subdevice = 0x229d,
707 .card = CX23885_BOARD_HAUPPAUGE_HVR1210, /* HVR1215 */
708 }, {
709 .subvendor = 0x0070,
710 .subdevice = 0x22f0,
711 .card = CX23885_BOARD_HAUPPAUGE_HVR1210,
712 }, {
713 .subvendor = 0x0070,
714 .subdevice = 0x22f1,
715 .card = CX23885_BOARD_HAUPPAUGE_HVR1255,
716 }, {
717 .subvendor = 0x0070,
718 .subdevice = 0x22f2,
719 .card = CX23885_BOARD_HAUPPAUGE_HVR1275,
720 }, {
721 .subvendor = 0x0070,
722 .subdevice = 0x22f3,
723 .card = CX23885_BOARD_HAUPPAUGE_HVR1210, /* HVR1215 */
724 }, {
725 .subvendor = 0x0070,
726 .subdevice = 0x22f4,
727 .card = CX23885_BOARD_HAUPPAUGE_HVR1210,
728 }, {
729 .subvendor = 0x0070,
730 .subdevice = 0x22f5,
731 .card = CX23885_BOARD_HAUPPAUGE_HVR1210, /* HVR1215 */
732 }, {
733 .subvendor = 0x14f1,
734 .subdevice = 0x8651,
735 .card = CX23885_BOARD_MYGICA_X8506,
736 }, {
737 .subvendor = 0x14f1,
738 .subdevice = 0x8657,
739 .card = CX23885_BOARD_MAGICPRO_PROHDTVE2,
740 }, {
741 .subvendor = 0x0070,
742 .subdevice = 0x8541,
743 .card = CX23885_BOARD_HAUPPAUGE_HVR1850,
744 }, {
745 .subvendor = 0x1858,
746 .subdevice = 0xe800,
747 .card = CX23885_BOARD_COMPRO_VIDEOMATE_E800,
748 }, {
749 .subvendor = 0x0070,
750 .subdevice = 0x8551,
751 .card = CX23885_BOARD_HAUPPAUGE_HVR1290,
752 }, {
753 .subvendor = 0x14f1,
754 .subdevice = 0x8578,
755 .card = CX23885_BOARD_MYGICA_X8558PRO,
756 }, {
757 .subvendor = 0x107d,
758 .subdevice = 0x6f22,
759 .card = CX23885_BOARD_LEADTEK_WINFAST_PXTV1200,
760 }, {
761 .subvendor = 0x5654,
762 .subdevice = 0x2390,
763 .card = CX23885_BOARD_GOTVIEW_X5_3D_HYBRID,
764 }, {
765 .subvendor = 0x1b55,
766 .subdevice = 0xe2e4,
767 .card = CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF,
768 }, {
769 .subvendor = 0x14f1,
770 .subdevice = 0x8502,
771 .card = CX23885_BOARD_MYGICA_X8507,
772 }, {
773 .subvendor = 0x153b,
774 .subdevice = 0x117e,
775 .card = CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL,
776 }, {
777 .subvendor = 0xd471,
778 .subdevice = 0x9022,
779 .card = CX23885_BOARD_TEVII_S471,
780 },
781};
782const unsigned int cx23885_idcount = ARRAY_SIZE(cx23885_subids);
783
784void cx23885_card_list(struct cx23885_dev *dev)
785{
786 int i;
787
788 if (0 == dev->pci->subsystem_vendor &&
789 0 == dev->pci->subsystem_device) {
790 printk(KERN_INFO
791 "%s: Board has no valid PCIe Subsystem ID and can't\n"
792 "%s: be autodetected. Pass card=<n> insmod option\n"
793 "%s: to workaround that. Redirect complaints to the\n"
794 "%s: vendor of the TV card. Best regards,\n"
795 "%s: -- tux\n",
796 dev->name, dev->name, dev->name, dev->name, dev->name);
797 } else {
798 printk(KERN_INFO
799 "%s: Your board isn't known (yet) to the driver.\n"
800 "%s: Try to pick one of the existing card configs via\n"
801 "%s: card=<n> insmod option. Updating to the latest\n"
802 "%s: version might help as well.\n",
803 dev->name, dev->name, dev->name, dev->name);
804 }
805 printk(KERN_INFO "%s: Here is a list of valid choices for the card=<n> insmod option:\n",
806 dev->name);
807 for (i = 0; i < cx23885_bcount; i++)
808 printk(KERN_INFO "%s: card=%d -> %s\n",
809 dev->name, i, cx23885_boards[i].name);
810}
811
812static void hauppauge_eeprom(struct cx23885_dev *dev, u8 *eeprom_data)
813{
814 struct tveeprom tv;
815
816 tveeprom_hauppauge_analog(&dev->i2c_bus[0].i2c_client, &tv,
817 eeprom_data);
818
819 /* Make sure we support the board model */
820 switch (tv.model) {
821 case 22001:
822 /* WinTV-HVR1270 (PCIe, Retail, half height)
823 * ATSC/QAM and basic analog, IR Blast */
824 case 22009:
825 /* WinTV-HVR1210 (PCIe, Retail, half height)
826 * DVB-T and basic analog, IR Blast */
827 case 22011:
828 /* WinTV-HVR1270 (PCIe, Retail, half height)
829 * ATSC/QAM and basic analog, IR Recv */
830 case 22019:
831 /* WinTV-HVR1210 (PCIe, Retail, half height)
832 * DVB-T and basic analog, IR Recv */
833 case 22021:
834 /* WinTV-HVR1275 (PCIe, Retail, half height)
835 * ATSC/QAM and basic analog, IR Recv */
836 case 22029:
837 /* WinTV-HVR1210 (PCIe, Retail, half height)
838 * DVB-T and basic analog, IR Recv */
839 case 22101:
840 /* WinTV-HVR1270 (PCIe, Retail, full height)
841 * ATSC/QAM and basic analog, IR Blast */
842 case 22109:
843 /* WinTV-HVR1210 (PCIe, Retail, full height)
844 * DVB-T and basic analog, IR Blast */
845 case 22111:
846 /* WinTV-HVR1270 (PCIe, Retail, full height)
847 * ATSC/QAM and basic analog, IR Recv */
848 case 22119:
849 /* WinTV-HVR1210 (PCIe, Retail, full height)
850 * DVB-T and basic analog, IR Recv */
851 case 22121:
852 /* WinTV-HVR1275 (PCIe, Retail, full height)
853 * ATSC/QAM and basic analog, IR Recv */
854 case 22129:
855 /* WinTV-HVR1210 (PCIe, Retail, full height)
856 * DVB-T and basic analog, IR Recv */
857 case 71009:
858 /* WinTV-HVR1200 (PCIe, Retail, full height)
859 * DVB-T and basic analog */
860 case 71359:
861 /* WinTV-HVR1200 (PCIe, OEM, half height)
862 * DVB-T and basic analog */
863 case 71439:
864 /* WinTV-HVR1200 (PCIe, OEM, half height)
865 * DVB-T and basic analog */
866 case 71449:
867 /* WinTV-HVR1200 (PCIe, OEM, full height)
868 * DVB-T and basic analog */
869 case 71939:
870 /* WinTV-HVR1200 (PCIe, OEM, half height)
871 * DVB-T and basic analog */
872 case 71949:
873 /* WinTV-HVR1200 (PCIe, OEM, full height)
874 * DVB-T and basic analog */
875 case 71959:
876 /* WinTV-HVR1200 (PCIe, OEM, full height)
877 * DVB-T and basic analog */
878 case 71979:
879 /* WinTV-HVR1200 (PCIe, OEM, half height)
880 * DVB-T and basic analog */
881 case 71999:
882 /* WinTV-HVR1200 (PCIe, OEM, full height)
883 * DVB-T and basic analog */
884 case 76601:
885 /* WinTV-HVR1800lp (PCIe, Retail, No IR, Dual
886 channel ATSC and MPEG2 HW Encoder */
887 case 77001:
888 /* WinTV-HVR1500 (Express Card, OEM, No IR, ATSC
889 and Basic analog */
890 case 77011:
891 /* WinTV-HVR1500 (Express Card, Retail, No IR, ATSC
892 and Basic analog */
893 case 77041:
894 /* WinTV-HVR1500Q (Express Card, OEM, No IR, ATSC/QAM
895 and Basic analog */
896 case 77051:
897 /* WinTV-HVR1500Q (Express Card, Retail, No IR, ATSC/QAM
898 and Basic analog */
899 case 78011:
900 /* WinTV-HVR1800 (PCIe, Retail, 3.5mm in, IR, No FM,
901 Dual channel ATSC and MPEG2 HW Encoder */
902 case 78501:
903 /* WinTV-HVR1800 (PCIe, OEM, RCA in, No IR, FM,
904 Dual channel ATSC and MPEG2 HW Encoder */
905 case 78521:
906 /* WinTV-HVR1800 (PCIe, OEM, RCA in, No IR, FM,
907 Dual channel ATSC and MPEG2 HW Encoder */
908 case 78531:
909 /* WinTV-HVR1800 (PCIe, OEM, RCA in, No IR, No FM,
910 Dual channel ATSC and MPEG2 HW Encoder */
911 case 78631:
912 /* WinTV-HVR1800 (PCIe, OEM, No IR, No FM,
913 Dual channel ATSC and MPEG2 HW Encoder */
914 case 79001:
915 /* WinTV-HVR1250 (PCIe, Retail, IR, full height,
916 ATSC and Basic analog */
917 case 79101:
918 /* WinTV-HVR1250 (PCIe, Retail, IR, half height,
919 ATSC and Basic analog */
920 case 79501:
921 /* WinTV-HVR1250 (PCIe, No IR, half height,
922 ATSC [at least] and Basic analog) */
923 case 79561:
924 /* WinTV-HVR1250 (PCIe, OEM, No IR, half height,
925 ATSC and Basic analog */
926 case 79571:
927 /* WinTV-HVR1250 (PCIe, OEM, No IR, full height,
928 ATSC and Basic analog */
929 case 79671:
930 /* WinTV-HVR1250 (PCIe, OEM, No IR, half height,
931 ATSC and Basic analog */
932 case 80019:
933 /* WinTV-HVR1400 (Express Card, Retail, IR,
934 * DVB-T and Basic analog */
935 case 81509:
936 /* WinTV-HVR1700 (PCIe, OEM, No IR, half height)
937 * DVB-T and MPEG2 HW Encoder */
938 case 81519:
939 /* WinTV-HVR1700 (PCIe, OEM, No IR, full height)
940 * DVB-T and MPEG2 HW Encoder */
941 break;
942 case 85021:
943 /* WinTV-HVR1850 (PCIe, Retail, 3.5mm in, IR, FM,
944 Dual channel ATSC and MPEG2 HW Encoder */
945 break;
946 case 85721:
947 /* WinTV-HVR1290 (PCIe, OEM, RCA in, IR,
948 Dual channel ATSC and Basic analog */
949 break;
950 default:
951 printk(KERN_WARNING "%s: warning: "
952 "unknown hauppauge model #%d\n",
953 dev->name, tv.model);
954 break;
955 }
956
957 printk(KERN_INFO "%s: hauppauge eeprom: model=%d\n",
958 dev->name, tv.model);
959}
960
961int cx23885_tuner_callback(void *priv, int component, int command, int arg)
962{
963 struct cx23885_tsport *port = priv;
964 struct cx23885_dev *dev = port->dev;
965 u32 bitmask = 0;
966
967 if ((command == XC2028_RESET_CLK) || (command == XC2028_I2C_FLUSH))
968 return 0;
969
970 if (command != 0) {
971 printk(KERN_ERR "%s(): Unknown command 0x%x.\n",
972 __func__, command);
973 return -EINVAL;
974 }
975
976 switch (dev->board) {
977 case CX23885_BOARD_HAUPPAUGE_HVR1400:
978 case CX23885_BOARD_HAUPPAUGE_HVR1500:
979 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
980 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
981 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000:
982 case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
983 case CX23885_BOARD_COMPRO_VIDEOMATE_E800:
984 case CX23885_BOARD_LEADTEK_WINFAST_PXTV1200:
985 /* Tuner Reset Command */
986 bitmask = 0x04;
987 break;
988 case CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP:
989 case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP:
990 /* Two identical tuners on two different i2c buses,
991 * we need to reset the correct gpio. */
992 if (port->nr == 1)
993 bitmask = 0x01;
994 else if (port->nr == 2)
995 bitmask = 0x04;
996 break;
997 case CX23885_BOARD_GOTVIEW_X5_3D_HYBRID:
998 /* Tuner Reset Command */
999 bitmask = 0x02;
1000 break;
1001 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
1002 altera_ci_tuner_reset(dev, port->nr);
1003 break;
1004 }
1005
1006 if (bitmask) {
1007 /* Drive the tuner into reset and back out */
1008 cx_clear(GP0_IO, bitmask);
1009 mdelay(200);
1010 cx_set(GP0_IO, bitmask);
1011 }
1012
1013 return 0;
1014}
1015
1016void cx23885_gpio_setup(struct cx23885_dev *dev)
1017{
1018 switch (dev->board) {
1019 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1020 /* GPIO-0 cx24227 demodulator reset */
1021 cx_set(GP0_IO, 0x00010001); /* Bring the part out of reset */
1022 break;
1023 case CX23885_BOARD_HAUPPAUGE_HVR1500:
1024 /* GPIO-0 cx24227 demodulator */
1025 /* GPIO-2 xc3028 tuner */
1026
1027 /* Put the parts into reset */
1028 cx_set(GP0_IO, 0x00050000);
1029 cx_clear(GP0_IO, 0x00000005);
1030 msleep(5);
1031
1032 /* Bring the parts out of reset */
1033 cx_set(GP0_IO, 0x00050005);
1034 break;
1035 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
1036 /* GPIO-0 cx24227 demodulator reset */
1037 /* GPIO-2 xc5000 tuner reset */
1038 cx_set(GP0_IO, 0x00050005); /* Bring the part out of reset */
1039 break;
1040 case CX23885_BOARD_HAUPPAUGE_HVR1800:
1041 /* GPIO-0 656_CLK */
1042 /* GPIO-1 656_D0 */
1043 /* GPIO-2 8295A Reset */
1044 /* GPIO-3-10 cx23417 data0-7 */
1045 /* GPIO-11-14 cx23417 addr0-3 */
1046 /* GPIO-15-18 cx23417 READY, CS, RD, WR */
1047 /* GPIO-19 IR_RX */
1048
1049 /* CX23417 GPIO's */
1050 /* EIO15 Zilog Reset */
1051 /* EIO14 S5H1409/CX24227 Reset */
1052 mc417_gpio_enable(dev, GPIO_15 | GPIO_14, 1);
1053
1054 /* Put the demod into reset and protect the eeprom */
1055 mc417_gpio_clear(dev, GPIO_15 | GPIO_14);
1056 mdelay(100);
1057
1058 /* Bring the demod and blaster out of reset */
1059 mc417_gpio_set(dev, GPIO_15 | GPIO_14);
1060 mdelay(100);
1061
1062 /* Force the TDA8295A into reset and back */
1063 cx23885_gpio_enable(dev, GPIO_2, 1);
1064 cx23885_gpio_set(dev, GPIO_2);
1065 mdelay(20);
1066 cx23885_gpio_clear(dev, GPIO_2);
1067 mdelay(20);
1068 cx23885_gpio_set(dev, GPIO_2);
1069 mdelay(20);
1070 break;
1071 case CX23885_BOARD_HAUPPAUGE_HVR1200:
1072 /* GPIO-0 tda10048 demodulator reset */
1073 /* GPIO-2 tda18271 tuner reset */
1074
1075 /* Put the parts into reset and back */
1076 cx_set(GP0_IO, 0x00050000);
1077 mdelay(20);
1078 cx_clear(GP0_IO, 0x00000005);
1079 mdelay(20);
1080 cx_set(GP0_IO, 0x00050005);
1081 break;
1082 case CX23885_BOARD_HAUPPAUGE_HVR1700:
1083 /* GPIO-0 TDA10048 demodulator reset */
1084 /* GPIO-2 TDA8295A Reset */
1085 /* GPIO-3-10 cx23417 data0-7 */
1086 /* GPIO-11-14 cx23417 addr0-3 */
1087 /* GPIO-15-18 cx23417 READY, CS, RD, WR */
1088
1089 /* The following GPIO's are on the interna AVCore (cx25840) */
1090 /* GPIO-19 IR_RX */
1091 /* GPIO-20 IR_TX 416/DVBT Select */
1092 /* GPIO-21 IIS DAT */
1093 /* GPIO-22 IIS WCLK */
1094 /* GPIO-23 IIS BCLK */
1095
1096 /* Put the parts into reset and back */
1097 cx_set(GP0_IO, 0x00050000);
1098 mdelay(20);
1099 cx_clear(GP0_IO, 0x00000005);
1100 mdelay(20);
1101 cx_set(GP0_IO, 0x00050005);
1102 break;
1103 case CX23885_BOARD_HAUPPAUGE_HVR1400:
1104 /* GPIO-0 Dibcom7000p demodulator reset */
1105 /* GPIO-2 xc3028L tuner reset */
1106 /* GPIO-13 LED */
1107
1108 /* Put the parts into reset and back */
1109 cx_set(GP0_IO, 0x00050000);
1110 mdelay(20);
1111 cx_clear(GP0_IO, 0x00000005);
1112 mdelay(20);
1113 cx_set(GP0_IO, 0x00050005);
1114 break;
1115 case CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP:
1116 /* GPIO-0 xc5000 tuner reset i2c bus 0 */
1117 /* GPIO-1 s5h1409 demod reset i2c bus 0 */
1118 /* GPIO-2 xc5000 tuner reset i2c bus 1 */
1119 /* GPIO-3 s5h1409 demod reset i2c bus 0 */
1120
1121 /* Put the parts into reset and back */
1122 cx_set(GP0_IO, 0x000f0000);
1123 mdelay(20);
1124 cx_clear(GP0_IO, 0x0000000f);
1125 mdelay(20);
1126 cx_set(GP0_IO, 0x000f000f);
1127 break;
1128 case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP:
1129 /* GPIO-0 portb xc3028 reset */
1130 /* GPIO-1 portb zl10353 reset */
1131 /* GPIO-2 portc xc3028 reset */
1132 /* GPIO-3 portc zl10353 reset */
1133
1134 /* Put the parts into reset and back */
1135 cx_set(GP0_IO, 0x000f0000);
1136 mdelay(20);
1137 cx_clear(GP0_IO, 0x0000000f);
1138 mdelay(20);
1139 cx_set(GP0_IO, 0x000f000f);
1140 break;
1141 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
1142 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000:
1143 case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
1144 case CX23885_BOARD_COMPRO_VIDEOMATE_E800:
1145 case CX23885_BOARD_LEADTEK_WINFAST_PXTV1200:
1146 /* GPIO-2 xc3028 tuner reset */
1147
1148 /* The following GPIO's are on the internal AVCore (cx25840) */
1149 /* GPIO-? zl10353 demod reset */
1150
1151 /* Put the parts into reset and back */
1152 cx_set(GP0_IO, 0x00040000);
1153 mdelay(20);
1154 cx_clear(GP0_IO, 0x00000004);
1155 mdelay(20);
1156 cx_set(GP0_IO, 0x00040004);
1157 break;
1158 case CX23885_BOARD_TBS_6920:
1159 cx_write(MC417_CTL, 0x00000036);
1160 cx_write(MC417_OEN, 0x00001000);
1161 cx_set(MC417_RWD, 0x00000002);
1162 mdelay(200);
1163 cx_clear(MC417_RWD, 0x00000800);
1164 mdelay(200);
1165 cx_set(MC417_RWD, 0x00000800);
1166 mdelay(200);
1167 break;
1168 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
1169 /* GPIO-0 INTA from CiMax1
1170 GPIO-1 INTB from CiMax2
1171 GPIO-2 reset chips
1172 GPIO-3 to GPIO-10 data/addr for CA
1173 GPIO-11 ~CS0 to CiMax1
1174 GPIO-12 ~CS1 to CiMax2
1175 GPIO-13 ADL0 load LSB addr
1176 GPIO-14 ADL1 load MSB addr
1177 GPIO-15 ~RDY from CiMax
1178 GPIO-17 ~RD to CiMax
1179 GPIO-18 ~WR to CiMax
1180 */
1181 cx_set(GP0_IO, 0x00040000); /* GPIO as out */
1182 /* GPIO1 and GPIO2 as INTA and INTB from CiMaxes, reset low */
1183 cx_clear(GP0_IO, 0x00030004);
1184 mdelay(100);/* reset delay */
1185 cx_set(GP0_IO, 0x00040004); /* GPIO as out, reset high */
1186 cx_write(MC417_CTL, 0x00000037);/* enable GPIO3-18 pins */
1187 /* GPIO-15 IN as ~ACK, rest as OUT */
1188 cx_write(MC417_OEN, 0x00001000);
1189 /* ~RD, ~WR high; ADL0, ADL1 low; ~CS0, ~CS1 high */
1190 cx_write(MC417_RWD, 0x0000c300);
1191 /* enable irq */
1192 cx_write(GPIO_ISM, 0x00000000);/* INTERRUPTS active low*/
1193 break;
1194 case CX23885_BOARD_HAUPPAUGE_HVR1270:
1195 case CX23885_BOARD_HAUPPAUGE_HVR1275:
1196 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1197 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1198 case CX23885_BOARD_HAUPPAUGE_HVR1210:
1199 /* GPIO-5 RF Control: 0 = RF1 Terrestrial, 1 = RF2 Cable */
1200 /* GPIO-6 I2C Gate which can isolate the demod from the bus */
1201 /* GPIO-9 Demod reset */
1202
1203 /* Put the parts into reset and back */
1204 cx23885_gpio_enable(dev, GPIO_9 | GPIO_6 | GPIO_5, 1);
1205 cx23885_gpio_set(dev, GPIO_9 | GPIO_6 | GPIO_5);
1206 cx23885_gpio_clear(dev, GPIO_9);
1207 mdelay(20);
1208 cx23885_gpio_set(dev, GPIO_9);
1209 break;
1210 case CX23885_BOARD_MYGICA_X8506:
1211 case CX23885_BOARD_MAGICPRO_PROHDTVE2:
1212 case CX23885_BOARD_MYGICA_X8507:
1213 /* GPIO-0 (0)Analog / (1)Digital TV */
1214 /* GPIO-1 reset XC5000 */
1215 /* GPIO-2 reset LGS8GL5 / LGS8G75 */
1216 cx23885_gpio_enable(dev, GPIO_0 | GPIO_1 | GPIO_2, 1);
1217 cx23885_gpio_clear(dev, GPIO_1 | GPIO_2);
1218 mdelay(100);
1219 cx23885_gpio_set(dev, GPIO_0 | GPIO_1 | GPIO_2);
1220 mdelay(100);
1221 break;
1222 case CX23885_BOARD_MYGICA_X8558PRO:
1223 /* GPIO-0 reset first ATBM8830 */
1224 /* GPIO-1 reset second ATBM8830 */
1225 cx23885_gpio_enable(dev, GPIO_0 | GPIO_1, 1);
1226 cx23885_gpio_clear(dev, GPIO_0 | GPIO_1);
1227 mdelay(100);
1228 cx23885_gpio_set(dev, GPIO_0 | GPIO_1);
1229 mdelay(100);
1230 break;
1231 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1232 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1233 /* GPIO-0 656_CLK */
1234 /* GPIO-1 656_D0 */
1235 /* GPIO-2 Wake# */
1236 /* GPIO-3-10 cx23417 data0-7 */
1237 /* GPIO-11-14 cx23417 addr0-3 */
1238 /* GPIO-15-18 cx23417 READY, CS, RD, WR */
1239 /* GPIO-19 IR_RX */
1240 /* GPIO-20 C_IR_TX */
1241 /* GPIO-21 I2S DAT */
1242 /* GPIO-22 I2S WCLK */
1243 /* GPIO-23 I2S BCLK */
1244 /* ALT GPIO: EXP GPIO LATCH */
1245
1246 /* CX23417 GPIO's */
1247 /* GPIO-14 S5H1411/CX24228 Reset */
1248 /* GPIO-13 EEPROM write protect */
1249 mc417_gpio_enable(dev, GPIO_14 | GPIO_13, 1);
1250
1251 /* Put the demod into reset and protect the eeprom */
1252 mc417_gpio_clear(dev, GPIO_14 | GPIO_13);
1253 mdelay(100);
1254
1255 /* Bring the demod out of reset */
1256 mc417_gpio_set(dev, GPIO_14);
1257 mdelay(100);
1258
1259 /* CX24228 GPIO */
1260 /* Connected to IF / Mux */
1261 break;
1262 case CX23885_BOARD_GOTVIEW_X5_3D_HYBRID:
1263 cx_set(GP0_IO, 0x00010001); /* Bring the part out of reset */
1264 break;
1265 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
1266 /* GPIO-0 ~INT in
1267 GPIO-1 TMS out
1268 GPIO-2 ~reset chips out
1269 GPIO-3 to GPIO-10 data/addr for CA in/out
1270 GPIO-11 ~CS out
1271 GPIO-12 ADDR out
1272 GPIO-13 ~WR out
1273 GPIO-14 ~RD out
1274 GPIO-15 ~RDY in
1275 GPIO-16 TCK out
1276 GPIO-17 TDO in
1277 GPIO-18 TDI out
1278 */
1279 cx_set(GP0_IO, 0x00060000); /* GPIO-1,2 as out */
1280 /* GPIO-0 as INT, reset & TMS low */
1281 cx_clear(GP0_IO, 0x00010006);
1282 mdelay(100);/* reset delay */
1283 cx_set(GP0_IO, 0x00000004); /* reset high */
1284 cx_write(MC417_CTL, 0x00000037);/* enable GPIO-3..18 pins */
1285 /* GPIO-17 is TDO in, GPIO-15 is ~RDY in, rest is out */
1286 cx_write(MC417_OEN, 0x00005000);
1287 /* ~RD, ~WR high; ADDR low; ~CS high */
1288 cx_write(MC417_RWD, 0x00000d00);
1289 /* enable irq */
1290 cx_write(GPIO_ISM, 0x00000000);/* INTERRUPTS active low*/
1291 break;
1292 }
1293}
1294
1295int cx23885_ir_init(struct cx23885_dev *dev)
1296{
1297 static struct v4l2_subdev_io_pin_config ir_rxtx_pin_cfg[] = {
1298 {
1299 .flags = V4L2_SUBDEV_IO_PIN_INPUT,
1300 .pin = CX23885_PIN_IR_RX_GPIO19,
1301 .function = CX23885_PAD_IR_RX,
1302 .value = 0,
1303 .strength = CX25840_PIN_DRIVE_MEDIUM,
1304 }, {
1305 .flags = V4L2_SUBDEV_IO_PIN_OUTPUT,
1306 .pin = CX23885_PIN_IR_TX_GPIO20,
1307 .function = CX23885_PAD_IR_TX,
1308 .value = 0,
1309 .strength = CX25840_PIN_DRIVE_MEDIUM,
1310 }
1311 };
1312 const size_t ir_rxtx_pin_cfg_count = ARRAY_SIZE(ir_rxtx_pin_cfg);
1313
1314 static struct v4l2_subdev_io_pin_config ir_rx_pin_cfg[] = {
1315 {
1316 .flags = V4L2_SUBDEV_IO_PIN_INPUT,
1317 .pin = CX23885_PIN_IR_RX_GPIO19,
1318 .function = CX23885_PAD_IR_RX,
1319 .value = 0,
1320 .strength = CX25840_PIN_DRIVE_MEDIUM,
1321 }
1322 };
1323 const size_t ir_rx_pin_cfg_count = ARRAY_SIZE(ir_rx_pin_cfg);
1324
1325 struct v4l2_subdev_ir_parameters params;
1326 int ret = 0;
1327 switch (dev->board) {
1328 case CX23885_BOARD_HAUPPAUGE_HVR1500:
1329 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
1330 case CX23885_BOARD_HAUPPAUGE_HVR1800:
1331 case CX23885_BOARD_HAUPPAUGE_HVR1200:
1332 case CX23885_BOARD_HAUPPAUGE_HVR1400:
1333 case CX23885_BOARD_HAUPPAUGE_HVR1275:
1334 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1335 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1336 case CX23885_BOARD_HAUPPAUGE_HVR1210:
1337 /* FIXME: Implement me */
1338 break;
1339 case CX23885_BOARD_HAUPPAUGE_HVR1270:
1340 ret = cx23888_ir_probe(dev);
1341 if (ret)
1342 break;
1343 dev->sd_ir = cx23885_find_hw(dev, CX23885_HW_888_IR);
1344 v4l2_subdev_call(dev->sd_cx25840, core, s_io_pin_config,
1345 ir_rx_pin_cfg_count, ir_rx_pin_cfg);
1346 break;
1347 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1348 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1349 ret = cx23888_ir_probe(dev);
1350 if (ret)
1351 break;
1352 dev->sd_ir = cx23885_find_hw(dev, CX23885_HW_888_IR);
1353 v4l2_subdev_call(dev->sd_cx25840, core, s_io_pin_config,
1354 ir_rxtx_pin_cfg_count, ir_rxtx_pin_cfg);
1355 /*
1356 * For these boards we need to invert the Tx output via the
1357 * IR controller to have the LED off while idle
1358 */
1359 v4l2_subdev_call(dev->sd_ir, ir, tx_g_parameters, &params);
1360 params.enable = false;
1361 params.shutdown = false;
1362 params.invert_level = true;
1363 v4l2_subdev_call(dev->sd_ir, ir, tx_s_parameters, &params);
1364 params.shutdown = true;
1365 v4l2_subdev_call(dev->sd_ir, ir, tx_s_parameters, &params);
1366 break;
1367 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
1368 case CX23885_BOARD_TEVII_S470:
1369 if (!enable_885_ir)
1370 break;
1371 dev->sd_ir = cx23885_find_hw(dev, CX23885_HW_AV_CORE);
1372 if (dev->sd_ir == NULL) {
1373 ret = -ENODEV;
1374 break;
1375 }
1376 v4l2_subdev_call(dev->sd_cx25840, core, s_io_pin_config,
1377 ir_rx_pin_cfg_count, ir_rx_pin_cfg);
1378 break;
1379 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1380 if (!enable_885_ir)
1381 break;
1382 dev->sd_ir = cx23885_find_hw(dev, CX23885_HW_AV_CORE);
1383 if (dev->sd_ir == NULL) {
1384 ret = -ENODEV;
1385 break;
1386 }
1387 v4l2_subdev_call(dev->sd_cx25840, core, s_io_pin_config,
1388 ir_rxtx_pin_cfg_count, ir_rxtx_pin_cfg);
1389 break;
1390 case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP:
1391 request_module("ir-kbd-i2c");
1392 break;
1393 }
1394
1395 return ret;
1396}
1397
1398void cx23885_ir_fini(struct cx23885_dev *dev)
1399{
1400 switch (dev->board) {
1401 case CX23885_BOARD_HAUPPAUGE_HVR1270:
1402 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1403 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1404 cx23885_irq_remove(dev, PCI_MSK_IR);
1405 cx23888_ir_remove(dev);
1406 dev->sd_ir = NULL;
1407 break;
1408 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
1409 case CX23885_BOARD_TEVII_S470:
1410 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1411 cx23885_irq_remove(dev, PCI_MSK_AV_CORE);
1412 /* sd_ir is a duplicate pointer to the AV Core, just clear it */
1413 dev->sd_ir = NULL;
1414 break;
1415 }
1416}
1417
1418int netup_jtag_io(void *device, int tms, int tdi, int read_tdo)
1419{
1420 int data;
1421 int tdo = 0;
1422 struct cx23885_dev *dev = (struct cx23885_dev *)device;
1423 /*TMS*/
1424 data = ((cx_read(GP0_IO)) & (~0x00000002));
1425 data |= (tms ? 0x00020002 : 0x00020000);
1426 cx_write(GP0_IO, data);
1427
1428 /*TDI*/
1429 data = ((cx_read(MC417_RWD)) & (~0x0000a000));
1430 data |= (tdi ? 0x00008000 : 0);
1431 cx_write(MC417_RWD, data);
1432 if (read_tdo)
1433 tdo = (data & 0x00004000) ? 1 : 0; /*TDO*/
1434
1435 cx_write(MC417_RWD, data | 0x00002000);
1436 udelay(1);
1437 /*TCK*/
1438 cx_write(MC417_RWD, data);
1439
1440 return tdo;
1441}
1442
1443void cx23885_ir_pci_int_enable(struct cx23885_dev *dev)
1444{
1445 switch (dev->board) {
1446 case CX23885_BOARD_HAUPPAUGE_HVR1270:
1447 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1448 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1449 if (dev->sd_ir)
1450 cx23885_irq_add_enable(dev, PCI_MSK_IR);
1451 break;
1452 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
1453 case CX23885_BOARD_TEVII_S470:
1454 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1455 if (dev->sd_ir)
1456 cx23885_irq_add_enable(dev, PCI_MSK_AV_CORE);
1457 break;
1458 }
1459}
1460
1461void cx23885_card_setup(struct cx23885_dev *dev)
1462{
1463 struct cx23885_tsport *ts1 = &dev->ts1;
1464 struct cx23885_tsport *ts2 = &dev->ts2;
1465
1466 static u8 eeprom[256];
1467
1468 if (dev->i2c_bus[0].i2c_rc == 0) {
1469 dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
1470 tveeprom_read(&dev->i2c_bus[0].i2c_client,
1471 eeprom, sizeof(eeprom));
1472 }
1473
1474 switch (dev->board) {
1475 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1476 if (dev->i2c_bus[0].i2c_rc == 0) {
1477 if (eeprom[0x80] != 0x84)
1478 hauppauge_eeprom(dev, eeprom+0xc0);
1479 else
1480 hauppauge_eeprom(dev, eeprom+0x80);
1481 }
1482 break;
1483 case CX23885_BOARD_HAUPPAUGE_HVR1500:
1484 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
1485 case CX23885_BOARD_HAUPPAUGE_HVR1400:
1486 if (dev->i2c_bus[0].i2c_rc == 0)
1487 hauppauge_eeprom(dev, eeprom+0x80);
1488 break;
1489 case CX23885_BOARD_HAUPPAUGE_HVR1800:
1490 case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
1491 case CX23885_BOARD_HAUPPAUGE_HVR1200:
1492 case CX23885_BOARD_HAUPPAUGE_HVR1700:
1493 case CX23885_BOARD_HAUPPAUGE_HVR1270:
1494 case CX23885_BOARD_HAUPPAUGE_HVR1275:
1495 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1496 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1497 case CX23885_BOARD_HAUPPAUGE_HVR1210:
1498 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1499 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1500 if (dev->i2c_bus[0].i2c_rc == 0)
1501 hauppauge_eeprom(dev, eeprom+0xc0);
1502 break;
1503 }
1504
1505 switch (dev->board) {
1506 case CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP:
1507 case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP:
1508 ts2->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
1509 ts2->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1510 ts2->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1511 /* break omitted intentionally */
1512 case CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP:
1513 ts1->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
1514 ts1->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1515 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1516 break;
1517 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1518 case CX23885_BOARD_HAUPPAUGE_HVR1800:
1519 /* Defaults for VID B - Analog encoder */
1520 /* DREQ_POL, SMODE, PUNC_CLK, MCLK_POL Serial bus + punc clk */
1521 ts1->gen_ctrl_val = 0x10e;
1522 ts1->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1523 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1524
1525 /* APB_TSVALERR_POL (active low)*/
1526 ts1->vld_misc_val = 0x2000;
1527 ts1->hw_sop_ctrl_val = (0x47 << 16 | 188 << 4 | 0xc);
1528 cx_write(0x130184, 0xc);
1529
1530 /* Defaults for VID C */
1531 ts2->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
1532 ts2->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1533 ts2->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1534 break;
1535 case CX23885_BOARD_TBS_6920:
1536 ts1->gen_ctrl_val = 0x4; /* Parallel */
1537 ts1->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1538 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1539 break;
1540 case CX23885_BOARD_TEVII_S470:
1541 case CX23885_BOARD_TEVII_S471:
1542 case CX23885_BOARD_DVBWORLD_2005:
1543 ts1->gen_ctrl_val = 0x5; /* Parallel */
1544 ts1->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1545 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1546 break;
1547 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
1548 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
1549 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
1550 ts1->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
1551 ts1->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1552 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1553 ts2->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
1554 ts2->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1555 ts2->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1556 break;
1557 case CX23885_BOARD_MYGICA_X8506:
1558 case CX23885_BOARD_MAGICPRO_PROHDTVE2:
1559 ts1->gen_ctrl_val = 0x5; /* Parallel */
1560 ts1->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1561 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1562 break;
1563 case CX23885_BOARD_MYGICA_X8558PRO:
1564 ts1->gen_ctrl_val = 0x5; /* Parallel */
1565 ts1->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1566 ts1->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1567 ts2->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
1568 ts2->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1569 ts2->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1570 break;
1571 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1572 case CX23885_BOARD_HAUPPAUGE_HVR1500:
1573 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
1574 case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
1575 case CX23885_BOARD_HAUPPAUGE_HVR1200:
1576 case CX23885_BOARD_HAUPPAUGE_HVR1700:
1577 case CX23885_BOARD_HAUPPAUGE_HVR1400:
1578 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
1579 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000:
1580 case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
1581 case CX23885_BOARD_HAUPPAUGE_HVR1270:
1582 case CX23885_BOARD_HAUPPAUGE_HVR1275:
1583 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1584 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1585 case CX23885_BOARD_HAUPPAUGE_HVR1210:
1586 case CX23885_BOARD_COMPRO_VIDEOMATE_E800:
1587 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1588 case CX23885_BOARD_GOTVIEW_X5_3D_HYBRID:
1589 default:
1590 ts2->gen_ctrl_val = 0xc; /* Serial bus + punctured clock */
1591 ts2->ts_clk_en_val = 0x1; /* Enable TS_CLK */
1592 ts2->src_sel_val = CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO;
1593 }
1594
1595 /* Certain boards support analog, or require the avcore to be
1596 * loaded, ensure this happens.
1597 */
1598 switch (dev->board) {
1599 case CX23885_BOARD_TEVII_S470:
1600 /* Currently only enabled for the integrated IR controller */
1601 if (!enable_885_ir)
1602 break;
1603 case CX23885_BOARD_HAUPPAUGE_HVR1250:
1604 case CX23885_BOARD_HAUPPAUGE_HVR1800:
1605 case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
1606 case CX23885_BOARD_HAUPPAUGE_HVR1700:
1607 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
1608 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000:
1609 case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
1610 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
1611 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
1612 case CX23885_BOARD_COMPRO_VIDEOMATE_E800:
1613 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1614 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1615 case CX23885_BOARD_HAUPPAUGE_HVR1270:
1616 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1617 case CX23885_BOARD_MYGICA_X8506:
1618 case CX23885_BOARD_MAGICPRO_PROHDTVE2:
1619 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1620 case CX23885_BOARD_LEADTEK_WINFAST_PXTV1200:
1621 case CX23885_BOARD_GOTVIEW_X5_3D_HYBRID:
1622 case CX23885_BOARD_HAUPPAUGE_HVR1500:
1623 case CX23885_BOARD_MPX885:
1624 case CX23885_BOARD_MYGICA_X8507:
1625 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
1626 dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1627 &dev->i2c_bus[2].i2c_adap,
1628 "cx25840", 0x88 >> 1, NULL);
1629 if (dev->sd_cx25840) {
1630 dev->sd_cx25840->grp_id = CX23885_HW_AV_CORE;
1631 v4l2_subdev_call(dev->sd_cx25840, core, load_fw);
1632 }
1633 break;
1634 }
1635
1636 /* AUX-PLL 27MHz CLK */
1637 switch (dev->board) {
1638 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
1639 netup_initialize(dev);
1640 break;
1641 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF: {
1642 int ret;
1643 const struct firmware *fw;
1644 const char *filename = "dvb-netup-altera-01.fw";
1645 char *action = "configure";
1646 static struct netup_card_info cinfo;
1647 struct altera_config netup_config = {
1648 .dev = dev,
1649 .action = action,
1650 .jtag_io = netup_jtag_io,
1651 };
1652
1653 netup_initialize(dev);
1654
1655 netup_get_card_info(&dev->i2c_bus[0].i2c_adap, &cinfo);
1656 if (netup_card_rev)
1657 cinfo.rev = netup_card_rev;
1658
1659 switch (cinfo.rev) {
1660 case 0x4:
1661 filename = "dvb-netup-altera-04.fw";
1662 break;
1663 default:
1664 filename = "dvb-netup-altera-01.fw";
1665 break;
1666 }
1667 printk(KERN_INFO "NetUP card rev=0x%x fw_filename=%s\n",
1668 cinfo.rev, filename);
1669
1670 ret = request_firmware(&fw, filename, &dev->pci->dev);
1671 if (ret != 0)
1672 printk(KERN_ERR "did not find the firmware file. (%s) "
1673 "Please see linux/Documentation/dvb/ for more details "
1674 "on firmware-problems.", filename);
1675 else
1676 altera_init(&netup_config, fw);
1677
1678 release_firmware(fw);
1679 break;
1680 }
1681 }
1682}
1683
1684/* ------------------------------------------------------------------ */
diff --git a/drivers/media/pci/cx23885/cx23885-core.c b/drivers/media/pci/cx23885/cx23885-core.c
new file mode 100644
index 000000000000..697728f09430
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-core.c
@@ -0,0 +1,2234 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/init.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kmod.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30#include <linux/delay.h>
31#include <asm/div64.h>
32#include <linux/firmware.h>
33
34#include "cx23885.h"
35#include "cimax2.h"
36#include "altera-ci.h"
37#include "cx23888-ir.h"
38#include "cx23885-ir.h"
39#include "cx23885-av.h"
40#include "cx23885-input.h"
41
42MODULE_DESCRIPTION("Driver for cx23885 based TV cards");
43MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
44MODULE_LICENSE("GPL");
45MODULE_VERSION(CX23885_VERSION);
46
47static unsigned int debug;
48module_param(debug, int, 0644);
49MODULE_PARM_DESC(debug, "enable debug messages");
50
51static unsigned int card[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
52module_param_array(card, int, NULL, 0444);
53MODULE_PARM_DESC(card, "card type");
54
55#define dprintk(level, fmt, arg...)\
56 do { if (debug >= level)\
57 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
58 } while (0)
59
60static unsigned int cx23885_devcount;
61
62#define NO_SYNC_LINE (-1U)
63
64/* FIXME, these allocations will change when
65 * analog arrives. The be reviewed.
66 * CX23887 Assumptions
67 * 1 line = 16 bytes of CDT
68 * cmds size = 80
69 * cdt size = 16 * linesize
70 * iqsize = 64
71 * maxlines = 6
72 *
73 * Address Space:
74 * 0x00000000 0x00008fff FIFO clusters
75 * 0x00010000 0x000104af Channel Management Data Structures
76 * 0x000104b0 0x000104ff Free
77 * 0x00010500 0x000108bf 15 channels * iqsize
78 * 0x000108c0 0x000108ff Free
79 * 0x00010900 0x00010e9f IQ's + Cluster Descriptor Tables
80 * 15 channels * (iqsize + (maxlines * linesize))
81 * 0x00010ea0 0x00010xxx Free
82 */
83
84static struct sram_channel cx23885_sram_channels[] = {
85 [SRAM_CH01] = {
86 .name = "VID A",
87 .cmds_start = 0x10000,
88 .ctrl_start = 0x10380,
89 .cdt = 0x104c0,
90 .fifo_start = 0x40,
91 .fifo_size = 0x2800,
92 .ptr1_reg = DMA1_PTR1,
93 .ptr2_reg = DMA1_PTR2,
94 .cnt1_reg = DMA1_CNT1,
95 .cnt2_reg = DMA1_CNT2,
96 },
97 [SRAM_CH02] = {
98 .name = "ch2",
99 .cmds_start = 0x0,
100 .ctrl_start = 0x0,
101 .cdt = 0x0,
102 .fifo_start = 0x0,
103 .fifo_size = 0x0,
104 .ptr1_reg = DMA2_PTR1,
105 .ptr2_reg = DMA2_PTR2,
106 .cnt1_reg = DMA2_CNT1,
107 .cnt2_reg = DMA2_CNT2,
108 },
109 [SRAM_CH03] = {
110 .name = "TS1 B",
111 .cmds_start = 0x100A0,
112 .ctrl_start = 0x10400,
113 .cdt = 0x10580,
114 .fifo_start = 0x5000,
115 .fifo_size = 0x1000,
116 .ptr1_reg = DMA3_PTR1,
117 .ptr2_reg = DMA3_PTR2,
118 .cnt1_reg = DMA3_CNT1,
119 .cnt2_reg = DMA3_CNT2,
120 },
121 [SRAM_CH04] = {
122 .name = "ch4",
123 .cmds_start = 0x0,
124 .ctrl_start = 0x0,
125 .cdt = 0x0,
126 .fifo_start = 0x0,
127 .fifo_size = 0x0,
128 .ptr1_reg = DMA4_PTR1,
129 .ptr2_reg = DMA4_PTR2,
130 .cnt1_reg = DMA4_CNT1,
131 .cnt2_reg = DMA4_CNT2,
132 },
133 [SRAM_CH05] = {
134 .name = "ch5",
135 .cmds_start = 0x0,
136 .ctrl_start = 0x0,
137 .cdt = 0x0,
138 .fifo_start = 0x0,
139 .fifo_size = 0x0,
140 .ptr1_reg = DMA5_PTR1,
141 .ptr2_reg = DMA5_PTR2,
142 .cnt1_reg = DMA5_CNT1,
143 .cnt2_reg = DMA5_CNT2,
144 },
145 [SRAM_CH06] = {
146 .name = "TS2 C",
147 .cmds_start = 0x10140,
148 .ctrl_start = 0x10440,
149 .cdt = 0x105e0,
150 .fifo_start = 0x6000,
151 .fifo_size = 0x1000,
152 .ptr1_reg = DMA5_PTR1,
153 .ptr2_reg = DMA5_PTR2,
154 .cnt1_reg = DMA5_CNT1,
155 .cnt2_reg = DMA5_CNT2,
156 },
157 [SRAM_CH07] = {
158 .name = "TV Audio",
159 .cmds_start = 0x10190,
160 .ctrl_start = 0x10480,
161 .cdt = 0x10a00,
162 .fifo_start = 0x7000,
163 .fifo_size = 0x1000,
164 .ptr1_reg = DMA6_PTR1,
165 .ptr2_reg = DMA6_PTR2,
166 .cnt1_reg = DMA6_CNT1,
167 .cnt2_reg = DMA6_CNT2,
168 },
169 [SRAM_CH08] = {
170 .name = "ch8",
171 .cmds_start = 0x0,
172 .ctrl_start = 0x0,
173 .cdt = 0x0,
174 .fifo_start = 0x0,
175 .fifo_size = 0x0,
176 .ptr1_reg = DMA7_PTR1,
177 .ptr2_reg = DMA7_PTR2,
178 .cnt1_reg = DMA7_CNT1,
179 .cnt2_reg = DMA7_CNT2,
180 },
181 [SRAM_CH09] = {
182 .name = "ch9",
183 .cmds_start = 0x0,
184 .ctrl_start = 0x0,
185 .cdt = 0x0,
186 .fifo_start = 0x0,
187 .fifo_size = 0x0,
188 .ptr1_reg = DMA8_PTR1,
189 .ptr2_reg = DMA8_PTR2,
190 .cnt1_reg = DMA8_CNT1,
191 .cnt2_reg = DMA8_CNT2,
192 },
193};
194
195static struct sram_channel cx23887_sram_channels[] = {
196 [SRAM_CH01] = {
197 .name = "VID A",
198 .cmds_start = 0x10000,
199 .ctrl_start = 0x105b0,
200 .cdt = 0x107b0,
201 .fifo_start = 0x40,
202 .fifo_size = 0x2800,
203 .ptr1_reg = DMA1_PTR1,
204 .ptr2_reg = DMA1_PTR2,
205 .cnt1_reg = DMA1_CNT1,
206 .cnt2_reg = DMA1_CNT2,
207 },
208 [SRAM_CH02] = {
209 .name = "VID A (VBI)",
210 .cmds_start = 0x10050,
211 .ctrl_start = 0x105F0,
212 .cdt = 0x10810,
213 .fifo_start = 0x3000,
214 .fifo_size = 0x1000,
215 .ptr1_reg = DMA2_PTR1,
216 .ptr2_reg = DMA2_PTR2,
217 .cnt1_reg = DMA2_CNT1,
218 .cnt2_reg = DMA2_CNT2,
219 },
220 [SRAM_CH03] = {
221 .name = "TS1 B",
222 .cmds_start = 0x100A0,
223 .ctrl_start = 0x10630,
224 .cdt = 0x10870,
225 .fifo_start = 0x5000,
226 .fifo_size = 0x1000,
227 .ptr1_reg = DMA3_PTR1,
228 .ptr2_reg = DMA3_PTR2,
229 .cnt1_reg = DMA3_CNT1,
230 .cnt2_reg = DMA3_CNT2,
231 },
232 [SRAM_CH04] = {
233 .name = "ch4",
234 .cmds_start = 0x0,
235 .ctrl_start = 0x0,
236 .cdt = 0x0,
237 .fifo_start = 0x0,
238 .fifo_size = 0x0,
239 .ptr1_reg = DMA4_PTR1,
240 .ptr2_reg = DMA4_PTR2,
241 .cnt1_reg = DMA4_CNT1,
242 .cnt2_reg = DMA4_CNT2,
243 },
244 [SRAM_CH05] = {
245 .name = "ch5",
246 .cmds_start = 0x0,
247 .ctrl_start = 0x0,
248 .cdt = 0x0,
249 .fifo_start = 0x0,
250 .fifo_size = 0x0,
251 .ptr1_reg = DMA5_PTR1,
252 .ptr2_reg = DMA5_PTR2,
253 .cnt1_reg = DMA5_CNT1,
254 .cnt2_reg = DMA5_CNT2,
255 },
256 [SRAM_CH06] = {
257 .name = "TS2 C",
258 .cmds_start = 0x10140,
259 .ctrl_start = 0x10670,
260 .cdt = 0x108d0,
261 .fifo_start = 0x6000,
262 .fifo_size = 0x1000,
263 .ptr1_reg = DMA5_PTR1,
264 .ptr2_reg = DMA5_PTR2,
265 .cnt1_reg = DMA5_CNT1,
266 .cnt2_reg = DMA5_CNT2,
267 },
268 [SRAM_CH07] = {
269 .name = "TV Audio",
270 .cmds_start = 0x10190,
271 .ctrl_start = 0x106B0,
272 .cdt = 0x10930,
273 .fifo_start = 0x7000,
274 .fifo_size = 0x1000,
275 .ptr1_reg = DMA6_PTR1,
276 .ptr2_reg = DMA6_PTR2,
277 .cnt1_reg = DMA6_CNT1,
278 .cnt2_reg = DMA6_CNT2,
279 },
280 [SRAM_CH08] = {
281 .name = "ch8",
282 .cmds_start = 0x0,
283 .ctrl_start = 0x0,
284 .cdt = 0x0,
285 .fifo_start = 0x0,
286 .fifo_size = 0x0,
287 .ptr1_reg = DMA7_PTR1,
288 .ptr2_reg = DMA7_PTR2,
289 .cnt1_reg = DMA7_CNT1,
290 .cnt2_reg = DMA7_CNT2,
291 },
292 [SRAM_CH09] = {
293 .name = "ch9",
294 .cmds_start = 0x0,
295 .ctrl_start = 0x0,
296 .cdt = 0x0,
297 .fifo_start = 0x0,
298 .fifo_size = 0x0,
299 .ptr1_reg = DMA8_PTR1,
300 .ptr2_reg = DMA8_PTR2,
301 .cnt1_reg = DMA8_CNT1,
302 .cnt2_reg = DMA8_CNT2,
303 },
304};
305
306void cx23885_irq_add(struct cx23885_dev *dev, u32 mask)
307{
308 unsigned long flags;
309 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
310
311 dev->pci_irqmask |= mask;
312
313 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
314}
315
316void cx23885_irq_add_enable(struct cx23885_dev *dev, u32 mask)
317{
318 unsigned long flags;
319 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
320
321 dev->pci_irqmask |= mask;
322 cx_set(PCI_INT_MSK, mask);
323
324 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
325}
326
327void cx23885_irq_enable(struct cx23885_dev *dev, u32 mask)
328{
329 u32 v;
330 unsigned long flags;
331 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
332
333 v = mask & dev->pci_irqmask;
334 if (v)
335 cx_set(PCI_INT_MSK, v);
336
337 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
338}
339
340static inline void cx23885_irq_enable_all(struct cx23885_dev *dev)
341{
342 cx23885_irq_enable(dev, 0xffffffff);
343}
344
345void cx23885_irq_disable(struct cx23885_dev *dev, u32 mask)
346{
347 unsigned long flags;
348 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
349
350 cx_clear(PCI_INT_MSK, mask);
351
352 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
353}
354
355static inline void cx23885_irq_disable_all(struct cx23885_dev *dev)
356{
357 cx23885_irq_disable(dev, 0xffffffff);
358}
359
360void cx23885_irq_remove(struct cx23885_dev *dev, u32 mask)
361{
362 unsigned long flags;
363 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
364
365 dev->pci_irqmask &= ~mask;
366 cx_clear(PCI_INT_MSK, mask);
367
368 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
369}
370
371static u32 cx23885_irq_get_mask(struct cx23885_dev *dev)
372{
373 u32 v;
374 unsigned long flags;
375 spin_lock_irqsave(&dev->pci_irqmask_lock, flags);
376
377 v = cx_read(PCI_INT_MSK);
378
379 spin_unlock_irqrestore(&dev->pci_irqmask_lock, flags);
380 return v;
381}
382
383static int cx23885_risc_decode(u32 risc)
384{
385 static char *instr[16] = {
386 [RISC_SYNC >> 28] = "sync",
387 [RISC_WRITE >> 28] = "write",
388 [RISC_WRITEC >> 28] = "writec",
389 [RISC_READ >> 28] = "read",
390 [RISC_READC >> 28] = "readc",
391 [RISC_JUMP >> 28] = "jump",
392 [RISC_SKIP >> 28] = "skip",
393 [RISC_WRITERM >> 28] = "writerm",
394 [RISC_WRITECM >> 28] = "writecm",
395 [RISC_WRITECR >> 28] = "writecr",
396 };
397 static int incr[16] = {
398 [RISC_WRITE >> 28] = 3,
399 [RISC_JUMP >> 28] = 3,
400 [RISC_SKIP >> 28] = 1,
401 [RISC_SYNC >> 28] = 1,
402 [RISC_WRITERM >> 28] = 3,
403 [RISC_WRITECM >> 28] = 3,
404 [RISC_WRITECR >> 28] = 4,
405 };
406 static char *bits[] = {
407 "12", "13", "14", "resync",
408 "cnt0", "cnt1", "18", "19",
409 "20", "21", "22", "23",
410 "irq1", "irq2", "eol", "sol",
411 };
412 int i;
413
414 printk("0x%08x [ %s", risc,
415 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
416 for (i = ARRAY_SIZE(bits) - 1; i >= 0; i--)
417 if (risc & (1 << (i + 12)))
418 printk(" %s", bits[i]);
419 printk(" count=%d ]\n", risc & 0xfff);
420 return incr[risc >> 28] ? incr[risc >> 28] : 1;
421}
422
423void cx23885_wakeup(struct cx23885_tsport *port,
424 struct cx23885_dmaqueue *q, u32 count)
425{
426 struct cx23885_dev *dev = port->dev;
427 struct cx23885_buffer *buf;
428 int bc;
429
430 for (bc = 0;; bc++) {
431 if (list_empty(&q->active))
432 break;
433 buf = list_entry(q->active.next,
434 struct cx23885_buffer, vb.queue);
435
436 /* count comes from the hw and is is 16bit wide --
437 * this trick handles wrap-arounds correctly for
438 * up to 32767 buffers in flight... */
439 if ((s16) (count - buf->count) < 0)
440 break;
441
442 do_gettimeofday(&buf->vb.ts);
443 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
444 count, buf->count);
445 buf->vb.state = VIDEOBUF_DONE;
446 list_del(&buf->vb.queue);
447 wake_up(&buf->vb.done);
448 }
449 if (list_empty(&q->active))
450 del_timer(&q->timeout);
451 else
452 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
453 if (bc != 1)
454 printk(KERN_WARNING "%s: %d buffers handled (should be 1)\n",
455 __func__, bc);
456}
457
458int cx23885_sram_channel_setup(struct cx23885_dev *dev,
459 struct sram_channel *ch,
460 unsigned int bpl, u32 risc)
461{
462 unsigned int i, lines;
463 u32 cdt;
464
465 if (ch->cmds_start == 0) {
466 dprintk(1, "%s() Erasing channel [%s]\n", __func__,
467 ch->name);
468 cx_write(ch->ptr1_reg, 0);
469 cx_write(ch->ptr2_reg, 0);
470 cx_write(ch->cnt2_reg, 0);
471 cx_write(ch->cnt1_reg, 0);
472 return 0;
473 } else {
474 dprintk(1, "%s() Configuring channel [%s]\n", __func__,
475 ch->name);
476 }
477
478 bpl = (bpl + 7) & ~7; /* alignment */
479 cdt = ch->cdt;
480 lines = ch->fifo_size / bpl;
481 if (lines > 6)
482 lines = 6;
483 BUG_ON(lines < 2);
484
485 cx_write(8 + 0, RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
486 cx_write(8 + 4, 8);
487 cx_write(8 + 8, 0);
488
489 /* write CDT */
490 for (i = 0; i < lines; i++) {
491 dprintk(2, "%s() 0x%08x <- 0x%08x\n", __func__, cdt + 16*i,
492 ch->fifo_start + bpl*i);
493 cx_write(cdt + 16*i, ch->fifo_start + bpl*i);
494 cx_write(cdt + 16*i + 4, 0);
495 cx_write(cdt + 16*i + 8, 0);
496 cx_write(cdt + 16*i + 12, 0);
497 }
498
499 /* write CMDS */
500 if (ch->jumponly)
501 cx_write(ch->cmds_start + 0, 8);
502 else
503 cx_write(ch->cmds_start + 0, risc);
504 cx_write(ch->cmds_start + 4, 0); /* 64 bits 63-32 */
505 cx_write(ch->cmds_start + 8, cdt);
506 cx_write(ch->cmds_start + 12, (lines*16) >> 3);
507 cx_write(ch->cmds_start + 16, ch->ctrl_start);
508 if (ch->jumponly)
509 cx_write(ch->cmds_start + 20, 0x80000000 | (64 >> 2));
510 else
511 cx_write(ch->cmds_start + 20, 64 >> 2);
512 for (i = 24; i < 80; i += 4)
513 cx_write(ch->cmds_start + i, 0);
514
515 /* fill registers */
516 cx_write(ch->ptr1_reg, ch->fifo_start);
517 cx_write(ch->ptr2_reg, cdt);
518 cx_write(ch->cnt2_reg, (lines*16) >> 3);
519 cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
520
521 dprintk(2, "[bridge %d] sram setup %s: bpl=%d lines=%d\n",
522 dev->bridge,
523 ch->name,
524 bpl,
525 lines);
526
527 return 0;
528}
529
530void cx23885_sram_channel_dump(struct cx23885_dev *dev,
531 struct sram_channel *ch)
532{
533 static char *name[] = {
534 "init risc lo",
535 "init risc hi",
536 "cdt base",
537 "cdt size",
538 "iq base",
539 "iq size",
540 "risc pc lo",
541 "risc pc hi",
542 "iq wr ptr",
543 "iq rd ptr",
544 "cdt current",
545 "pci target lo",
546 "pci target hi",
547 "line / byte",
548 };
549 u32 risc;
550 unsigned int i, j, n;
551
552 printk(KERN_WARNING "%s: %s - dma channel status dump\n",
553 dev->name, ch->name);
554 for (i = 0; i < ARRAY_SIZE(name); i++)
555 printk(KERN_WARNING "%s: cmds: %-15s: 0x%08x\n",
556 dev->name, name[i],
557 cx_read(ch->cmds_start + 4*i));
558
559 for (i = 0; i < 4; i++) {
560 risc = cx_read(ch->cmds_start + 4 * (i + 14));
561 printk(KERN_WARNING "%s: risc%d: ", dev->name, i);
562 cx23885_risc_decode(risc);
563 }
564 for (i = 0; i < (64 >> 2); i += n) {
565 risc = cx_read(ch->ctrl_start + 4 * i);
566 /* No consideration for bits 63-32 */
567
568 printk(KERN_WARNING "%s: (0x%08x) iq %x: ", dev->name,
569 ch->ctrl_start + 4 * i, i);
570 n = cx23885_risc_decode(risc);
571 for (j = 1; j < n; j++) {
572 risc = cx_read(ch->ctrl_start + 4 * (i + j));
573 printk(KERN_WARNING "%s: iq %x: 0x%08x [ arg #%d ]\n",
574 dev->name, i+j, risc, j);
575 }
576 }
577
578 printk(KERN_WARNING "%s: fifo: 0x%08x -> 0x%x\n",
579 dev->name, ch->fifo_start, ch->fifo_start+ch->fifo_size);
580 printk(KERN_WARNING "%s: ctrl: 0x%08x -> 0x%x\n",
581 dev->name, ch->ctrl_start, ch->ctrl_start + 6*16);
582 printk(KERN_WARNING "%s: ptr1_reg: 0x%08x\n",
583 dev->name, cx_read(ch->ptr1_reg));
584 printk(KERN_WARNING "%s: ptr2_reg: 0x%08x\n",
585 dev->name, cx_read(ch->ptr2_reg));
586 printk(KERN_WARNING "%s: cnt1_reg: 0x%08x\n",
587 dev->name, cx_read(ch->cnt1_reg));
588 printk(KERN_WARNING "%s: cnt2_reg: 0x%08x\n",
589 dev->name, cx_read(ch->cnt2_reg));
590}
591
592static void cx23885_risc_disasm(struct cx23885_tsport *port,
593 struct btcx_riscmem *risc)
594{
595 struct cx23885_dev *dev = port->dev;
596 unsigned int i, j, n;
597
598 printk(KERN_INFO "%s: risc disasm: %p [dma=0x%08lx]\n",
599 dev->name, risc->cpu, (unsigned long)risc->dma);
600 for (i = 0; i < (risc->size >> 2); i += n) {
601 printk(KERN_INFO "%s: %04d: ", dev->name, i);
602 n = cx23885_risc_decode(le32_to_cpu(risc->cpu[i]));
603 for (j = 1; j < n; j++)
604 printk(KERN_INFO "%s: %04d: 0x%08x [ arg #%d ]\n",
605 dev->name, i + j, risc->cpu[i + j], j);
606 if (risc->cpu[i] == cpu_to_le32(RISC_JUMP))
607 break;
608 }
609}
610
611static void cx23885_shutdown(struct cx23885_dev *dev)
612{
613 /* disable RISC controller */
614 cx_write(DEV_CNTRL2, 0);
615
616 /* Disable all IR activity */
617 cx_write(IR_CNTRL_REG, 0);
618
619 /* Disable Video A/B activity */
620 cx_write(VID_A_DMA_CTL, 0);
621 cx_write(VID_B_DMA_CTL, 0);
622 cx_write(VID_C_DMA_CTL, 0);
623
624 /* Disable Audio activity */
625 cx_write(AUD_INT_DMA_CTL, 0);
626 cx_write(AUD_EXT_DMA_CTL, 0);
627
628 /* Disable Serial port */
629 cx_write(UART_CTL, 0);
630
631 /* Disable Interrupts */
632 cx23885_irq_disable_all(dev);
633 cx_write(VID_A_INT_MSK, 0);
634 cx_write(VID_B_INT_MSK, 0);
635 cx_write(VID_C_INT_MSK, 0);
636 cx_write(AUDIO_INT_INT_MSK, 0);
637 cx_write(AUDIO_EXT_INT_MSK, 0);
638
639}
640
641static void cx23885_reset(struct cx23885_dev *dev)
642{
643 dprintk(1, "%s()\n", __func__);
644
645 cx23885_shutdown(dev);
646
647 cx_write(PCI_INT_STAT, 0xffffffff);
648 cx_write(VID_A_INT_STAT, 0xffffffff);
649 cx_write(VID_B_INT_STAT, 0xffffffff);
650 cx_write(VID_C_INT_STAT, 0xffffffff);
651 cx_write(AUDIO_INT_INT_STAT, 0xffffffff);
652 cx_write(AUDIO_EXT_INT_STAT, 0xffffffff);
653 cx_write(CLK_DELAY, cx_read(CLK_DELAY) & 0x80000000);
654 cx_write(PAD_CTRL, 0x00500300);
655
656 mdelay(100);
657
658 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
659 720*4, 0);
660 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02], 128, 0);
661 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH03],
662 188*4, 0);
663 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH04], 128, 0);
664 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH05], 128, 0);
665 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH06],
666 188*4, 0);
667 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH07], 128, 0);
668 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH08], 128, 0);
669 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH09], 128, 0);
670
671 cx23885_gpio_setup(dev);
672}
673
674
675static int cx23885_pci_quirks(struct cx23885_dev *dev)
676{
677 dprintk(1, "%s()\n", __func__);
678
679 /* The cx23885 bridge has a weird bug which causes NMI to be asserted
680 * when DMA begins if RDR_TLCTL0 bit4 is not cleared. It does not
681 * occur on the cx23887 bridge.
682 */
683 if (dev->bridge == CX23885_BRIDGE_885)
684 cx_clear(RDR_TLCTL0, 1 << 4);
685
686 return 0;
687}
688
689static int get_resources(struct cx23885_dev *dev)
690{
691 if (request_mem_region(pci_resource_start(dev->pci, 0),
692 pci_resource_len(dev->pci, 0),
693 dev->name))
694 return 0;
695
696 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx\n",
697 dev->name, (unsigned long long)pci_resource_start(dev->pci, 0));
698
699 return -EBUSY;
700}
701
702static void cx23885_timeout(unsigned long data);
703int cx23885_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
704 u32 reg, u32 mask, u32 value);
705
706static int cx23885_init_tsport(struct cx23885_dev *dev,
707 struct cx23885_tsport *port, int portno)
708{
709 dprintk(1, "%s(portno=%d)\n", __func__, portno);
710
711 /* Transport bus init dma queue - Common settings */
712 port->dma_ctl_val = 0x11; /* Enable RISC controller and Fifo */
713 port->ts_int_msk_val = 0x1111; /* TS port bits for RISC */
714 port->vld_misc_val = 0x0;
715 port->hw_sop_ctrl_val = (0x47 << 16 | 188 << 4);
716
717 spin_lock_init(&port->slock);
718 port->dev = dev;
719 port->nr = portno;
720
721 INIT_LIST_HEAD(&port->mpegq.active);
722 INIT_LIST_HEAD(&port->mpegq.queued);
723 port->mpegq.timeout.function = cx23885_timeout;
724 port->mpegq.timeout.data = (unsigned long)port;
725 init_timer(&port->mpegq.timeout);
726
727 mutex_init(&port->frontends.lock);
728 INIT_LIST_HEAD(&port->frontends.felist);
729 port->frontends.active_fe_id = 0;
730
731 /* This should be hardcoded allow a single frontend
732 * attachment to this tsport, keeping the -dvb.c
733 * code clean and safe.
734 */
735 if (!port->num_frontends)
736 port->num_frontends = 1;
737
738 switch (portno) {
739 case 1:
740 port->reg_gpcnt = VID_B_GPCNT;
741 port->reg_gpcnt_ctl = VID_B_GPCNT_CTL;
742 port->reg_dma_ctl = VID_B_DMA_CTL;
743 port->reg_lngth = VID_B_LNGTH;
744 port->reg_hw_sop_ctrl = VID_B_HW_SOP_CTL;
745 port->reg_gen_ctrl = VID_B_GEN_CTL;
746 port->reg_bd_pkt_status = VID_B_BD_PKT_STATUS;
747 port->reg_sop_status = VID_B_SOP_STATUS;
748 port->reg_fifo_ovfl_stat = VID_B_FIFO_OVFL_STAT;
749 port->reg_vld_misc = VID_B_VLD_MISC;
750 port->reg_ts_clk_en = VID_B_TS_CLK_EN;
751 port->reg_src_sel = VID_B_SRC_SEL;
752 port->reg_ts_int_msk = VID_B_INT_MSK;
753 port->reg_ts_int_stat = VID_B_INT_STAT;
754 port->sram_chno = SRAM_CH03; /* VID_B */
755 port->pci_irqmask = 0x02; /* VID_B bit1 */
756 break;
757 case 2:
758 port->reg_gpcnt = VID_C_GPCNT;
759 port->reg_gpcnt_ctl = VID_C_GPCNT_CTL;
760 port->reg_dma_ctl = VID_C_DMA_CTL;
761 port->reg_lngth = VID_C_LNGTH;
762 port->reg_hw_sop_ctrl = VID_C_HW_SOP_CTL;
763 port->reg_gen_ctrl = VID_C_GEN_CTL;
764 port->reg_bd_pkt_status = VID_C_BD_PKT_STATUS;
765 port->reg_sop_status = VID_C_SOP_STATUS;
766 port->reg_fifo_ovfl_stat = VID_C_FIFO_OVFL_STAT;
767 port->reg_vld_misc = VID_C_VLD_MISC;
768 port->reg_ts_clk_en = VID_C_TS_CLK_EN;
769 port->reg_src_sel = 0;
770 port->reg_ts_int_msk = VID_C_INT_MSK;
771 port->reg_ts_int_stat = VID_C_INT_STAT;
772 port->sram_chno = SRAM_CH06; /* VID_C */
773 port->pci_irqmask = 0x04; /* VID_C bit2 */
774 break;
775 default:
776 BUG();
777 }
778
779 cx23885_risc_stopper(dev->pci, &port->mpegq.stopper,
780 port->reg_dma_ctl, port->dma_ctl_val, 0x00);
781
782 return 0;
783}
784
785static void cx23885_dev_checkrevision(struct cx23885_dev *dev)
786{
787 switch (cx_read(RDR_CFG2) & 0xff) {
788 case 0x00:
789 /* cx23885 */
790 dev->hwrevision = 0xa0;
791 break;
792 case 0x01:
793 /* CX23885-12Z */
794 dev->hwrevision = 0xa1;
795 break;
796 case 0x02:
797 /* CX23885-13Z/14Z */
798 dev->hwrevision = 0xb0;
799 break;
800 case 0x03:
801 if (dev->pci->device == 0x8880) {
802 /* CX23888-21Z/22Z */
803 dev->hwrevision = 0xc0;
804 } else {
805 /* CX23885-14Z */
806 dev->hwrevision = 0xa4;
807 }
808 break;
809 case 0x04:
810 if (dev->pci->device == 0x8880) {
811 /* CX23888-31Z */
812 dev->hwrevision = 0xd0;
813 } else {
814 /* CX23885-15Z, CX23888-31Z */
815 dev->hwrevision = 0xa5;
816 }
817 break;
818 case 0x0e:
819 /* CX23887-15Z */
820 dev->hwrevision = 0xc0;
821 break;
822 case 0x0f:
823 /* CX23887-14Z */
824 dev->hwrevision = 0xb1;
825 break;
826 default:
827 printk(KERN_ERR "%s() New hardware revision found 0x%x\n",
828 __func__, dev->hwrevision);
829 }
830 if (dev->hwrevision)
831 printk(KERN_INFO "%s() Hardware revision = 0x%02x\n",
832 __func__, dev->hwrevision);
833 else
834 printk(KERN_ERR "%s() Hardware revision unknown 0x%x\n",
835 __func__, dev->hwrevision);
836}
837
838/* Find the first v4l2_subdev member of the group id in hw */
839struct v4l2_subdev *cx23885_find_hw(struct cx23885_dev *dev, u32 hw)
840{
841 struct v4l2_subdev *result = NULL;
842 struct v4l2_subdev *sd;
843
844 spin_lock(&dev->v4l2_dev.lock);
845 v4l2_device_for_each_subdev(sd, &dev->v4l2_dev) {
846 if (sd->grp_id == hw) {
847 result = sd;
848 break;
849 }
850 }
851 spin_unlock(&dev->v4l2_dev.lock);
852 return result;
853}
854
855static int cx23885_dev_setup(struct cx23885_dev *dev)
856{
857 int i;
858
859 spin_lock_init(&dev->pci_irqmask_lock);
860
861 mutex_init(&dev->lock);
862 mutex_init(&dev->gpio_lock);
863
864 atomic_inc(&dev->refcount);
865
866 dev->nr = cx23885_devcount++;
867 sprintf(dev->name, "cx23885[%d]", dev->nr);
868
869 /* Configure the internal memory */
870 if (dev->pci->device == 0x8880) {
871 /* Could be 887 or 888, assume a default */
872 dev->bridge = CX23885_BRIDGE_887;
873 /* Apply a sensible clock frequency for the PCIe bridge */
874 dev->clk_freq = 25000000;
875 dev->sram_channels = cx23887_sram_channels;
876 } else
877 if (dev->pci->device == 0x8852) {
878 dev->bridge = CX23885_BRIDGE_885;
879 /* Apply a sensible clock frequency for the PCIe bridge */
880 dev->clk_freq = 28000000;
881 dev->sram_channels = cx23885_sram_channels;
882 } else
883 BUG();
884
885 dprintk(1, "%s() Memory configured for PCIe bridge type %d\n",
886 __func__, dev->bridge);
887
888 /* board config */
889 dev->board = UNSET;
890 if (card[dev->nr] < cx23885_bcount)
891 dev->board = card[dev->nr];
892 for (i = 0; UNSET == dev->board && i < cx23885_idcount; i++)
893 if (dev->pci->subsystem_vendor == cx23885_subids[i].subvendor &&
894 dev->pci->subsystem_device == cx23885_subids[i].subdevice)
895 dev->board = cx23885_subids[i].card;
896 if (UNSET == dev->board) {
897 dev->board = CX23885_BOARD_UNKNOWN;
898 cx23885_card_list(dev);
899 }
900
901 /* If the user specific a clk freq override, apply it */
902 if (cx23885_boards[dev->board].clk_freq > 0)
903 dev->clk_freq = cx23885_boards[dev->board].clk_freq;
904
905 dev->pci_bus = dev->pci->bus->number;
906 dev->pci_slot = PCI_SLOT(dev->pci->devfn);
907 cx23885_irq_add(dev, 0x001f00);
908
909 /* External Master 1 Bus */
910 dev->i2c_bus[0].nr = 0;
911 dev->i2c_bus[0].dev = dev;
912 dev->i2c_bus[0].reg_stat = I2C1_STAT;
913 dev->i2c_bus[0].reg_ctrl = I2C1_CTRL;
914 dev->i2c_bus[0].reg_addr = I2C1_ADDR;
915 dev->i2c_bus[0].reg_rdata = I2C1_RDATA;
916 dev->i2c_bus[0].reg_wdata = I2C1_WDATA;
917 dev->i2c_bus[0].i2c_period = (0x9d << 24); /* 100kHz */
918
919 /* External Master 2 Bus */
920 dev->i2c_bus[1].nr = 1;
921 dev->i2c_bus[1].dev = dev;
922 dev->i2c_bus[1].reg_stat = I2C2_STAT;
923 dev->i2c_bus[1].reg_ctrl = I2C2_CTRL;
924 dev->i2c_bus[1].reg_addr = I2C2_ADDR;
925 dev->i2c_bus[1].reg_rdata = I2C2_RDATA;
926 dev->i2c_bus[1].reg_wdata = I2C2_WDATA;
927 dev->i2c_bus[1].i2c_period = (0x9d << 24); /* 100kHz */
928
929 /* Internal Master 3 Bus */
930 dev->i2c_bus[2].nr = 2;
931 dev->i2c_bus[2].dev = dev;
932 dev->i2c_bus[2].reg_stat = I2C3_STAT;
933 dev->i2c_bus[2].reg_ctrl = I2C3_CTRL;
934 dev->i2c_bus[2].reg_addr = I2C3_ADDR;
935 dev->i2c_bus[2].reg_rdata = I2C3_RDATA;
936 dev->i2c_bus[2].reg_wdata = I2C3_WDATA;
937 dev->i2c_bus[2].i2c_period = (0x07 << 24); /* 1.95MHz */
938
939 if ((cx23885_boards[dev->board].portb == CX23885_MPEG_DVB) ||
940 (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER))
941 cx23885_init_tsport(dev, &dev->ts1, 1);
942
943 if ((cx23885_boards[dev->board].portc == CX23885_MPEG_DVB) ||
944 (cx23885_boards[dev->board].portc == CX23885_MPEG_ENCODER))
945 cx23885_init_tsport(dev, &dev->ts2, 2);
946
947 if (get_resources(dev) < 0) {
948 printk(KERN_ERR "CORE %s No more PCIe resources for "
949 "subsystem: %04x:%04x\n",
950 dev->name, dev->pci->subsystem_vendor,
951 dev->pci->subsystem_device);
952
953 cx23885_devcount--;
954 return -ENODEV;
955 }
956
957 /* PCIe stuff */
958 dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
959 pci_resource_len(dev->pci, 0));
960
961 dev->bmmio = (u8 __iomem *)dev->lmmio;
962
963 printk(KERN_INFO "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
964 dev->name, dev->pci->subsystem_vendor,
965 dev->pci->subsystem_device, cx23885_boards[dev->board].name,
966 dev->board, card[dev->nr] == dev->board ?
967 "insmod option" : "autodetected");
968
969 cx23885_pci_quirks(dev);
970
971 /* Assume some sensible defaults */
972 dev->tuner_type = cx23885_boards[dev->board].tuner_type;
973 dev->tuner_addr = cx23885_boards[dev->board].tuner_addr;
974 dev->tuner_bus = cx23885_boards[dev->board].tuner_bus;
975 dev->radio_type = cx23885_boards[dev->board].radio_type;
976 dev->radio_addr = cx23885_boards[dev->board].radio_addr;
977
978 dprintk(1, "%s() tuner_type = 0x%x tuner_addr = 0x%x tuner_bus = %d\n",
979 __func__, dev->tuner_type, dev->tuner_addr, dev->tuner_bus);
980 dprintk(1, "%s() radio_type = 0x%x radio_addr = 0x%x\n",
981 __func__, dev->radio_type, dev->radio_addr);
982
983 /* The cx23417 encoder has GPIO's that need to be initialised
984 * before DVB, so that demodulators and tuners are out of
985 * reset before DVB uses them.
986 */
987 if ((cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER) ||
988 (cx23885_boards[dev->board].portc == CX23885_MPEG_ENCODER))
989 cx23885_mc417_init(dev);
990
991 /* init hardware */
992 cx23885_reset(dev);
993
994 cx23885_i2c_register(&dev->i2c_bus[0]);
995 cx23885_i2c_register(&dev->i2c_bus[1]);
996 cx23885_i2c_register(&dev->i2c_bus[2]);
997 cx23885_card_setup(dev);
998 call_all(dev, core, s_power, 0);
999 cx23885_ir_init(dev);
1000
1001 if (cx23885_boards[dev->board].porta == CX23885_ANALOG_VIDEO) {
1002 if (cx23885_video_register(dev) < 0) {
1003 printk(KERN_ERR "%s() Failed to register analog "
1004 "video adapters on VID_A\n", __func__);
1005 }
1006 }
1007
1008 if (cx23885_boards[dev->board].portb == CX23885_MPEG_DVB) {
1009 if (cx23885_boards[dev->board].num_fds_portb)
1010 dev->ts1.num_frontends =
1011 cx23885_boards[dev->board].num_fds_portb;
1012 if (cx23885_dvb_register(&dev->ts1) < 0) {
1013 printk(KERN_ERR "%s() Failed to register dvb adapters on VID_B\n",
1014 __func__);
1015 }
1016 } else
1017 if (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER) {
1018 if (cx23885_417_register(dev) < 0) {
1019 printk(KERN_ERR
1020 "%s() Failed to register 417 on VID_B\n",
1021 __func__);
1022 }
1023 }
1024
1025 if (cx23885_boards[dev->board].portc == CX23885_MPEG_DVB) {
1026 if (cx23885_boards[dev->board].num_fds_portc)
1027 dev->ts2.num_frontends =
1028 cx23885_boards[dev->board].num_fds_portc;
1029 if (cx23885_dvb_register(&dev->ts2) < 0) {
1030 printk(KERN_ERR
1031 "%s() Failed to register dvb on VID_C\n",
1032 __func__);
1033 }
1034 } else
1035 if (cx23885_boards[dev->board].portc == CX23885_MPEG_ENCODER) {
1036 if (cx23885_417_register(dev) < 0) {
1037 printk(KERN_ERR
1038 "%s() Failed to register 417 on VID_C\n",
1039 __func__);
1040 }
1041 }
1042
1043 cx23885_dev_checkrevision(dev);
1044
1045 /* disable MSI for NetUP cards, otherwise CI is not working */
1046 if (cx23885_boards[dev->board].ci_type > 0)
1047 cx_clear(RDR_RDRCTL1, 1 << 8);
1048
1049 switch (dev->board) {
1050 case CX23885_BOARD_TEVII_S470:
1051 case CX23885_BOARD_TEVII_S471:
1052 cx_clear(RDR_RDRCTL1, 1 << 8);
1053 break;
1054 }
1055
1056 return 0;
1057}
1058
1059static void cx23885_dev_unregister(struct cx23885_dev *dev)
1060{
1061 release_mem_region(pci_resource_start(dev->pci, 0),
1062 pci_resource_len(dev->pci, 0));
1063
1064 if (!atomic_dec_and_test(&dev->refcount))
1065 return;
1066
1067 if (cx23885_boards[dev->board].porta == CX23885_ANALOG_VIDEO)
1068 cx23885_video_unregister(dev);
1069
1070 if (cx23885_boards[dev->board].portb == CX23885_MPEG_DVB)
1071 cx23885_dvb_unregister(&dev->ts1);
1072
1073 if (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER)
1074 cx23885_417_unregister(dev);
1075
1076 if (cx23885_boards[dev->board].portc == CX23885_MPEG_DVB)
1077 cx23885_dvb_unregister(&dev->ts2);
1078
1079 if (cx23885_boards[dev->board].portc == CX23885_MPEG_ENCODER)
1080 cx23885_417_unregister(dev);
1081
1082 cx23885_i2c_unregister(&dev->i2c_bus[2]);
1083 cx23885_i2c_unregister(&dev->i2c_bus[1]);
1084 cx23885_i2c_unregister(&dev->i2c_bus[0]);
1085
1086 iounmap(dev->lmmio);
1087}
1088
1089static __le32 *cx23885_risc_field(__le32 *rp, struct scatterlist *sglist,
1090 unsigned int offset, u32 sync_line,
1091 unsigned int bpl, unsigned int padding,
1092 unsigned int lines, unsigned int lpi)
1093{
1094 struct scatterlist *sg;
1095 unsigned int line, todo, sol;
1096
1097 /* sync instruction */
1098 if (sync_line != NO_SYNC_LINE)
1099 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
1100
1101 /* scan lines */
1102 sg = sglist;
1103 for (line = 0; line < lines; line++) {
1104 while (offset && offset >= sg_dma_len(sg)) {
1105 offset -= sg_dma_len(sg);
1106 sg++;
1107 }
1108
1109 if (lpi && line > 0 && !(line % lpi))
1110 sol = RISC_SOL | RISC_IRQ1 | RISC_CNT_INC;
1111 else
1112 sol = RISC_SOL;
1113
1114 if (bpl <= sg_dma_len(sg)-offset) {
1115 /* fits into current chunk */
1116 *(rp++) = cpu_to_le32(RISC_WRITE|sol|RISC_EOL|bpl);
1117 *(rp++) = cpu_to_le32(sg_dma_address(sg)+offset);
1118 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1119 offset += bpl;
1120 } else {
1121 /* scanline needs to be split */
1122 todo = bpl;
1123 *(rp++) = cpu_to_le32(RISC_WRITE|sol|
1124 (sg_dma_len(sg)-offset));
1125 *(rp++) = cpu_to_le32(sg_dma_address(sg)+offset);
1126 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1127 todo -= (sg_dma_len(sg)-offset);
1128 offset = 0;
1129 sg++;
1130 while (todo > sg_dma_len(sg)) {
1131 *(rp++) = cpu_to_le32(RISC_WRITE|
1132 sg_dma_len(sg));
1133 *(rp++) = cpu_to_le32(sg_dma_address(sg));
1134 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1135 todo -= sg_dma_len(sg);
1136 sg++;
1137 }
1138 *(rp++) = cpu_to_le32(RISC_WRITE|RISC_EOL|todo);
1139 *(rp++) = cpu_to_le32(sg_dma_address(sg));
1140 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1141 offset += todo;
1142 }
1143 offset += padding;
1144 }
1145
1146 return rp;
1147}
1148
1149int cx23885_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
1150 struct scatterlist *sglist, unsigned int top_offset,
1151 unsigned int bottom_offset, unsigned int bpl,
1152 unsigned int padding, unsigned int lines)
1153{
1154 u32 instructions, fields;
1155 __le32 *rp;
1156 int rc;
1157
1158 fields = 0;
1159 if (UNSET != top_offset)
1160 fields++;
1161 if (UNSET != bottom_offset)
1162 fields++;
1163
1164 /* estimate risc mem: worst case is one write per page border +
1165 one write per scan line + syncs + jump (all 2 dwords). Padding
1166 can cause next bpl to start close to a page border. First DMA
1167 region may be smaller than PAGE_SIZE */
1168 /* write and jump need and extra dword */
1169 instructions = fields * (1 + ((bpl + padding) * lines)
1170 / PAGE_SIZE + lines);
1171 instructions += 2;
1172 rc = btcx_riscmem_alloc(pci, risc, instructions*12);
1173 if (rc < 0)
1174 return rc;
1175
1176 /* write risc instructions */
1177 rp = risc->cpu;
1178 if (UNSET != top_offset)
1179 rp = cx23885_risc_field(rp, sglist, top_offset, 0,
1180 bpl, padding, lines, 0);
1181 if (UNSET != bottom_offset)
1182 rp = cx23885_risc_field(rp, sglist, bottom_offset, 0x200,
1183 bpl, padding, lines, 0);
1184
1185 /* save pointer to jmp instruction address */
1186 risc->jmp = rp;
1187 BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
1188 return 0;
1189}
1190
1191int cx23885_risc_databuffer(struct pci_dev *pci,
1192 struct btcx_riscmem *risc,
1193 struct scatterlist *sglist,
1194 unsigned int bpl,
1195 unsigned int lines, unsigned int lpi)
1196{
1197 u32 instructions;
1198 __le32 *rp;
1199 int rc;
1200
1201 /* estimate risc mem: worst case is one write per page border +
1202 one write per scan line + syncs + jump (all 2 dwords). Here
1203 there is no padding and no sync. First DMA region may be smaller
1204 than PAGE_SIZE */
1205 /* Jump and write need an extra dword */
1206 instructions = 1 + (bpl * lines) / PAGE_SIZE + lines;
1207 instructions += 1;
1208
1209 rc = btcx_riscmem_alloc(pci, risc, instructions*12);
1210 if (rc < 0)
1211 return rc;
1212
1213 /* write risc instructions */
1214 rp = risc->cpu;
1215 rp = cx23885_risc_field(rp, sglist, 0, NO_SYNC_LINE,
1216 bpl, 0, lines, lpi);
1217
1218 /* save pointer to jmp instruction address */
1219 risc->jmp = rp;
1220 BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
1221 return 0;
1222}
1223
1224int cx23885_risc_vbibuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
1225 struct scatterlist *sglist, unsigned int top_offset,
1226 unsigned int bottom_offset, unsigned int bpl,
1227 unsigned int padding, unsigned int lines)
1228{
1229 u32 instructions, fields;
1230 __le32 *rp;
1231 int rc;
1232
1233 fields = 0;
1234 if (UNSET != top_offset)
1235 fields++;
1236 if (UNSET != bottom_offset)
1237 fields++;
1238
1239 /* estimate risc mem: worst case is one write per page border +
1240 one write per scan line + syncs + jump (all 2 dwords). Padding
1241 can cause next bpl to start close to a page border. First DMA
1242 region may be smaller than PAGE_SIZE */
1243 /* write and jump need and extra dword */
1244 instructions = fields * (1 + ((bpl + padding) * lines)
1245 / PAGE_SIZE + lines);
1246 instructions += 2;
1247 rc = btcx_riscmem_alloc(pci, risc, instructions*12);
1248 if (rc < 0)
1249 return rc;
1250 /* write risc instructions */
1251 rp = risc->cpu;
1252
1253 /* Sync to line 6, so US CC line 21 will appear in line '12'
1254 * in the userland vbi payload */
1255 if (UNSET != top_offset)
1256 rp = cx23885_risc_field(rp, sglist, top_offset, 6,
1257 bpl, padding, lines, 0);
1258
1259 if (UNSET != bottom_offset)
1260 rp = cx23885_risc_field(rp, sglist, bottom_offset, 0x207,
1261 bpl, padding, lines, 0);
1262
1263
1264
1265 /* save pointer to jmp instruction address */
1266 risc->jmp = rp;
1267 BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
1268 return 0;
1269}
1270
1271
1272int cx23885_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
1273 u32 reg, u32 mask, u32 value)
1274{
1275 __le32 *rp;
1276 int rc;
1277
1278 rc = btcx_riscmem_alloc(pci, risc, 4*16);
1279 if (rc < 0)
1280 return rc;
1281
1282 /* write risc instructions */
1283 rp = risc->cpu;
1284 *(rp++) = cpu_to_le32(RISC_WRITECR | RISC_IRQ2);
1285 *(rp++) = cpu_to_le32(reg);
1286 *(rp++) = cpu_to_le32(value);
1287 *(rp++) = cpu_to_le32(mask);
1288 *(rp++) = cpu_to_le32(RISC_JUMP);
1289 *(rp++) = cpu_to_le32(risc->dma);
1290 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1291 return 0;
1292}
1293
1294void cx23885_free_buffer(struct videobuf_queue *q, struct cx23885_buffer *buf)
1295{
1296 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
1297
1298 BUG_ON(in_interrupt());
1299 videobuf_waiton(q, &buf->vb, 0, 0);
1300 videobuf_dma_unmap(q->dev, dma);
1301 videobuf_dma_free(dma);
1302 btcx_riscmem_free(to_pci_dev(q->dev), &buf->risc);
1303 buf->vb.state = VIDEOBUF_NEEDS_INIT;
1304}
1305
1306static void cx23885_tsport_reg_dump(struct cx23885_tsport *port)
1307{
1308 struct cx23885_dev *dev = port->dev;
1309
1310 dprintk(1, "%s() Register Dump\n", __func__);
1311 dprintk(1, "%s() DEV_CNTRL2 0x%08X\n", __func__,
1312 cx_read(DEV_CNTRL2));
1313 dprintk(1, "%s() PCI_INT_MSK 0x%08X\n", __func__,
1314 cx23885_irq_get_mask(dev));
1315 dprintk(1, "%s() AUD_INT_INT_MSK 0x%08X\n", __func__,
1316 cx_read(AUDIO_INT_INT_MSK));
1317 dprintk(1, "%s() AUD_INT_DMA_CTL 0x%08X\n", __func__,
1318 cx_read(AUD_INT_DMA_CTL));
1319 dprintk(1, "%s() AUD_EXT_INT_MSK 0x%08X\n", __func__,
1320 cx_read(AUDIO_EXT_INT_MSK));
1321 dprintk(1, "%s() AUD_EXT_DMA_CTL 0x%08X\n", __func__,
1322 cx_read(AUD_EXT_DMA_CTL));
1323 dprintk(1, "%s() PAD_CTRL 0x%08X\n", __func__,
1324 cx_read(PAD_CTRL));
1325 dprintk(1, "%s() ALT_PIN_OUT_SEL 0x%08X\n", __func__,
1326 cx_read(ALT_PIN_OUT_SEL));
1327 dprintk(1, "%s() GPIO2 0x%08X\n", __func__,
1328 cx_read(GPIO2));
1329 dprintk(1, "%s() gpcnt(0x%08X) 0x%08X\n", __func__,
1330 port->reg_gpcnt, cx_read(port->reg_gpcnt));
1331 dprintk(1, "%s() gpcnt_ctl(0x%08X) 0x%08x\n", __func__,
1332 port->reg_gpcnt_ctl, cx_read(port->reg_gpcnt_ctl));
1333 dprintk(1, "%s() dma_ctl(0x%08X) 0x%08x\n", __func__,
1334 port->reg_dma_ctl, cx_read(port->reg_dma_ctl));
1335 if (port->reg_src_sel)
1336 dprintk(1, "%s() src_sel(0x%08X) 0x%08x\n", __func__,
1337 port->reg_src_sel, cx_read(port->reg_src_sel));
1338 dprintk(1, "%s() lngth(0x%08X) 0x%08x\n", __func__,
1339 port->reg_lngth, cx_read(port->reg_lngth));
1340 dprintk(1, "%s() hw_sop_ctrl(0x%08X) 0x%08x\n", __func__,
1341 port->reg_hw_sop_ctrl, cx_read(port->reg_hw_sop_ctrl));
1342 dprintk(1, "%s() gen_ctrl(0x%08X) 0x%08x\n", __func__,
1343 port->reg_gen_ctrl, cx_read(port->reg_gen_ctrl));
1344 dprintk(1, "%s() bd_pkt_status(0x%08X) 0x%08x\n", __func__,
1345 port->reg_bd_pkt_status, cx_read(port->reg_bd_pkt_status));
1346 dprintk(1, "%s() sop_status(0x%08X) 0x%08x\n", __func__,
1347 port->reg_sop_status, cx_read(port->reg_sop_status));
1348 dprintk(1, "%s() fifo_ovfl_stat(0x%08X) 0x%08x\n", __func__,
1349 port->reg_fifo_ovfl_stat, cx_read(port->reg_fifo_ovfl_stat));
1350 dprintk(1, "%s() vld_misc(0x%08X) 0x%08x\n", __func__,
1351 port->reg_vld_misc, cx_read(port->reg_vld_misc));
1352 dprintk(1, "%s() ts_clk_en(0x%08X) 0x%08x\n", __func__,
1353 port->reg_ts_clk_en, cx_read(port->reg_ts_clk_en));
1354 dprintk(1, "%s() ts_int_msk(0x%08X) 0x%08x\n", __func__,
1355 port->reg_ts_int_msk, cx_read(port->reg_ts_int_msk));
1356}
1357
1358static int cx23885_start_dma(struct cx23885_tsport *port,
1359 struct cx23885_dmaqueue *q,
1360 struct cx23885_buffer *buf)
1361{
1362 struct cx23885_dev *dev = port->dev;
1363 u32 reg;
1364
1365 dprintk(1, "%s() w: %d, h: %d, f: %d\n", __func__,
1366 buf->vb.width, buf->vb.height, buf->vb.field);
1367
1368 /* Stop the fifo and risc engine for this port */
1369 cx_clear(port->reg_dma_ctl, port->dma_ctl_val);
1370
1371 /* setup fifo + format */
1372 cx23885_sram_channel_setup(dev,
1373 &dev->sram_channels[port->sram_chno],
1374 port->ts_packet_size, buf->risc.dma);
1375 if (debug > 5) {
1376 cx23885_sram_channel_dump(dev,
1377 &dev->sram_channels[port->sram_chno]);
1378 cx23885_risc_disasm(port, &buf->risc);
1379 }
1380
1381 /* write TS length to chip */
1382 cx_write(port->reg_lngth, buf->vb.width);
1383
1384 if ((!(cx23885_boards[dev->board].portb & CX23885_MPEG_DVB)) &&
1385 (!(cx23885_boards[dev->board].portc & CX23885_MPEG_DVB))) {
1386 printk("%s() Unsupported .portb/c (0x%08x)/(0x%08x)\n",
1387 __func__,
1388 cx23885_boards[dev->board].portb,
1389 cx23885_boards[dev->board].portc);
1390 return -EINVAL;
1391 }
1392
1393 if (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER)
1394 cx23885_av_clk(dev, 0);
1395
1396 udelay(100);
1397
1398 /* If the port supports SRC SELECT, configure it */
1399 if (port->reg_src_sel)
1400 cx_write(port->reg_src_sel, port->src_sel_val);
1401
1402 cx_write(port->reg_hw_sop_ctrl, port->hw_sop_ctrl_val);
1403 cx_write(port->reg_ts_clk_en, port->ts_clk_en_val);
1404 cx_write(port->reg_vld_misc, port->vld_misc_val);
1405 cx_write(port->reg_gen_ctrl, port->gen_ctrl_val);
1406 udelay(100);
1407
1408 /* NOTE: this is 2 (reserved) for portb, does it matter? */
1409 /* reset counter to zero */
1410 cx_write(port->reg_gpcnt_ctl, 3);
1411 q->count = 1;
1412
1413 /* Set VIDB pins to input */
1414 if (cx23885_boards[dev->board].portb == CX23885_MPEG_DVB) {
1415 reg = cx_read(PAD_CTRL);
1416 reg &= ~0x3; /* Clear TS1_OE & TS1_SOP_OE */
1417 cx_write(PAD_CTRL, reg);
1418 }
1419
1420 /* Set VIDC pins to input */
1421 if (cx23885_boards[dev->board].portc == CX23885_MPEG_DVB) {
1422 reg = cx_read(PAD_CTRL);
1423 reg &= ~0x4; /* Clear TS2_SOP_OE */
1424 cx_write(PAD_CTRL, reg);
1425 }
1426
1427 if (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER) {
1428
1429 reg = cx_read(PAD_CTRL);
1430 reg = reg & ~0x1; /* Clear TS1_OE */
1431
1432 /* FIXME, bit 2 writing here is questionable */
1433 /* set TS1_SOP_OE and TS1_OE_HI */
1434 reg = reg | 0xa;
1435 cx_write(PAD_CTRL, reg);
1436
1437 /* FIXME and these two registers should be documented. */
1438 cx_write(CLK_DELAY, cx_read(CLK_DELAY) | 0x80000011);
1439 cx_write(ALT_PIN_OUT_SEL, 0x10100045);
1440 }
1441
1442 switch (dev->bridge) {
1443 case CX23885_BRIDGE_885:
1444 case CX23885_BRIDGE_887:
1445 case CX23885_BRIDGE_888:
1446 /* enable irqs */
1447 dprintk(1, "%s() enabling TS int's and DMA\n", __func__);
1448 cx_set(port->reg_ts_int_msk, port->ts_int_msk_val);
1449 cx_set(port->reg_dma_ctl, port->dma_ctl_val);
1450 cx23885_irq_add(dev, port->pci_irqmask);
1451 cx23885_irq_enable_all(dev);
1452 break;
1453 default:
1454 BUG();
1455 }
1456
1457 cx_set(DEV_CNTRL2, (1<<5)); /* Enable RISC controller */
1458
1459 if (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER)
1460 cx23885_av_clk(dev, 1);
1461
1462 if (debug > 4)
1463 cx23885_tsport_reg_dump(port);
1464
1465 return 0;
1466}
1467
1468static int cx23885_stop_dma(struct cx23885_tsport *port)
1469{
1470 struct cx23885_dev *dev = port->dev;
1471 u32 reg;
1472
1473 dprintk(1, "%s()\n", __func__);
1474
1475 /* Stop interrupts and DMA */
1476 cx_clear(port->reg_ts_int_msk, port->ts_int_msk_val);
1477 cx_clear(port->reg_dma_ctl, port->dma_ctl_val);
1478
1479 if (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER) {
1480
1481 reg = cx_read(PAD_CTRL);
1482
1483 /* Set TS1_OE */
1484 reg = reg | 0x1;
1485
1486 /* clear TS1_SOP_OE and TS1_OE_HI */
1487 reg = reg & ~0xa;
1488 cx_write(PAD_CTRL, reg);
1489 cx_write(port->reg_src_sel, 0);
1490 cx_write(port->reg_gen_ctrl, 8);
1491
1492 }
1493
1494 if (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER)
1495 cx23885_av_clk(dev, 0);
1496
1497 return 0;
1498}
1499
1500int cx23885_restart_queue(struct cx23885_tsport *port,
1501 struct cx23885_dmaqueue *q)
1502{
1503 struct cx23885_dev *dev = port->dev;
1504 struct cx23885_buffer *buf;
1505
1506 dprintk(5, "%s()\n", __func__);
1507 if (list_empty(&q->active)) {
1508 struct cx23885_buffer *prev;
1509 prev = NULL;
1510
1511 dprintk(5, "%s() queue is empty\n", __func__);
1512
1513 for (;;) {
1514 if (list_empty(&q->queued))
1515 return 0;
1516 buf = list_entry(q->queued.next, struct cx23885_buffer,
1517 vb.queue);
1518 if (NULL == prev) {
1519 list_del(&buf->vb.queue);
1520 list_add_tail(&buf->vb.queue, &q->active);
1521 cx23885_start_dma(port, q, buf);
1522 buf->vb.state = VIDEOBUF_ACTIVE;
1523 buf->count = q->count++;
1524 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
1525 dprintk(5, "[%p/%d] restart_queue - f/active\n",
1526 buf, buf->vb.i);
1527
1528 } else if (prev->vb.width == buf->vb.width &&
1529 prev->vb.height == buf->vb.height &&
1530 prev->fmt == buf->fmt) {
1531 list_del(&buf->vb.queue);
1532 list_add_tail(&buf->vb.queue, &q->active);
1533 buf->vb.state = VIDEOBUF_ACTIVE;
1534 buf->count = q->count++;
1535 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
1536 /* 64 bit bits 63-32 */
1537 prev->risc.jmp[2] = cpu_to_le32(0);
1538 dprintk(5, "[%p/%d] restart_queue - m/active\n",
1539 buf, buf->vb.i);
1540 } else {
1541 return 0;
1542 }
1543 prev = buf;
1544 }
1545 return 0;
1546 }
1547
1548 buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
1549 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
1550 buf, buf->vb.i);
1551 cx23885_start_dma(port, q, buf);
1552 list_for_each_entry(buf, &q->active, vb.queue)
1553 buf->count = q->count++;
1554 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
1555 return 0;
1556}
1557
1558/* ------------------------------------------------------------------ */
1559
1560int cx23885_buf_prepare(struct videobuf_queue *q, struct cx23885_tsport *port,
1561 struct cx23885_buffer *buf, enum v4l2_field field)
1562{
1563 struct cx23885_dev *dev = port->dev;
1564 int size = port->ts_packet_size * port->ts_packet_count;
1565 int rc;
1566
1567 dprintk(1, "%s: %p\n", __func__, buf);
1568 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1569 return -EINVAL;
1570
1571 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1572 buf->vb.width = port->ts_packet_size;
1573 buf->vb.height = port->ts_packet_count;
1574 buf->vb.size = size;
1575 buf->vb.field = field /*V4L2_FIELD_TOP*/;
1576
1577 rc = videobuf_iolock(q, &buf->vb, NULL);
1578 if (0 != rc)
1579 goto fail;
1580 cx23885_risc_databuffer(dev->pci, &buf->risc,
1581 videobuf_to_dma(&buf->vb)->sglist,
1582 buf->vb.width, buf->vb.height, 0);
1583 }
1584 buf->vb.state = VIDEOBUF_PREPARED;
1585 return 0;
1586
1587 fail:
1588 cx23885_free_buffer(q, buf);
1589 return rc;
1590}
1591
1592void cx23885_buf_queue(struct cx23885_tsport *port, struct cx23885_buffer *buf)
1593{
1594 struct cx23885_buffer *prev;
1595 struct cx23885_dev *dev = port->dev;
1596 struct cx23885_dmaqueue *cx88q = &port->mpegq;
1597
1598 /* add jump to stopper */
1599 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
1600 buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
1601 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
1602
1603 if (list_empty(&cx88q->active)) {
1604 dprintk(1, "queue is empty - first active\n");
1605 list_add_tail(&buf->vb.queue, &cx88q->active);
1606 cx23885_start_dma(port, cx88q, buf);
1607 buf->vb.state = VIDEOBUF_ACTIVE;
1608 buf->count = cx88q->count++;
1609 mod_timer(&cx88q->timeout, jiffies + BUFFER_TIMEOUT);
1610 dprintk(1, "[%p/%d] %s - first active\n",
1611 buf, buf->vb.i, __func__);
1612 } else {
1613 dprintk(1, "queue is not empty - append to active\n");
1614 prev = list_entry(cx88q->active.prev, struct cx23885_buffer,
1615 vb.queue);
1616 list_add_tail(&buf->vb.queue, &cx88q->active);
1617 buf->vb.state = VIDEOBUF_ACTIVE;
1618 buf->count = cx88q->count++;
1619 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
1620 prev->risc.jmp[2] = cpu_to_le32(0); /* 64 bit bits 63-32 */
1621 dprintk(1, "[%p/%d] %s - append to active\n",
1622 buf, buf->vb.i, __func__);
1623 }
1624}
1625
1626/* ----------------------------------------------------------- */
1627
1628static void do_cancel_buffers(struct cx23885_tsport *port, char *reason,
1629 int restart)
1630{
1631 struct cx23885_dev *dev = port->dev;
1632 struct cx23885_dmaqueue *q = &port->mpegq;
1633 struct cx23885_buffer *buf;
1634 unsigned long flags;
1635
1636 spin_lock_irqsave(&port->slock, flags);
1637 while (!list_empty(&q->active)) {
1638 buf = list_entry(q->active.next, struct cx23885_buffer,
1639 vb.queue);
1640 list_del(&buf->vb.queue);
1641 buf->vb.state = VIDEOBUF_ERROR;
1642 wake_up(&buf->vb.done);
1643 dprintk(1, "[%p/%d] %s - dma=0x%08lx\n",
1644 buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
1645 }
1646 if (restart) {
1647 dprintk(1, "restarting queue\n");
1648 cx23885_restart_queue(port, q);
1649 }
1650 spin_unlock_irqrestore(&port->slock, flags);
1651}
1652
1653void cx23885_cancel_buffers(struct cx23885_tsport *port)
1654{
1655 struct cx23885_dev *dev = port->dev;
1656 struct cx23885_dmaqueue *q = &port->mpegq;
1657
1658 dprintk(1, "%s()\n", __func__);
1659 del_timer_sync(&q->timeout);
1660 cx23885_stop_dma(port);
1661 do_cancel_buffers(port, "cancel", 0);
1662}
1663
1664static void cx23885_timeout(unsigned long data)
1665{
1666 struct cx23885_tsport *port = (struct cx23885_tsport *)data;
1667 struct cx23885_dev *dev = port->dev;
1668
1669 dprintk(1, "%s()\n", __func__);
1670
1671 if (debug > 5)
1672 cx23885_sram_channel_dump(dev,
1673 &dev->sram_channels[port->sram_chno]);
1674
1675 cx23885_stop_dma(port);
1676 do_cancel_buffers(port, "timeout", 1);
1677}
1678
1679int cx23885_irq_417(struct cx23885_dev *dev, u32 status)
1680{
1681 /* FIXME: port1 assumption here. */
1682 struct cx23885_tsport *port = &dev->ts1;
1683 int count = 0;
1684 int handled = 0;
1685
1686 if (status == 0)
1687 return handled;
1688
1689 count = cx_read(port->reg_gpcnt);
1690 dprintk(7, "status: 0x%08x mask: 0x%08x count: 0x%x\n",
1691 status, cx_read(port->reg_ts_int_msk), count);
1692
1693 if ((status & VID_B_MSK_BAD_PKT) ||
1694 (status & VID_B_MSK_OPC_ERR) ||
1695 (status & VID_B_MSK_VBI_OPC_ERR) ||
1696 (status & VID_B_MSK_SYNC) ||
1697 (status & VID_B_MSK_VBI_SYNC) ||
1698 (status & VID_B_MSK_OF) ||
1699 (status & VID_B_MSK_VBI_OF)) {
1700 printk(KERN_ERR "%s: V4L mpeg risc op code error, status "
1701 "= 0x%x\n", dev->name, status);
1702 if (status & VID_B_MSK_BAD_PKT)
1703 dprintk(1, " VID_B_MSK_BAD_PKT\n");
1704 if (status & VID_B_MSK_OPC_ERR)
1705 dprintk(1, " VID_B_MSK_OPC_ERR\n");
1706 if (status & VID_B_MSK_VBI_OPC_ERR)
1707 dprintk(1, " VID_B_MSK_VBI_OPC_ERR\n");
1708 if (status & VID_B_MSK_SYNC)
1709 dprintk(1, " VID_B_MSK_SYNC\n");
1710 if (status & VID_B_MSK_VBI_SYNC)
1711 dprintk(1, " VID_B_MSK_VBI_SYNC\n");
1712 if (status & VID_B_MSK_OF)
1713 dprintk(1, " VID_B_MSK_OF\n");
1714 if (status & VID_B_MSK_VBI_OF)
1715 dprintk(1, " VID_B_MSK_VBI_OF\n");
1716
1717 cx_clear(port->reg_dma_ctl, port->dma_ctl_val);
1718 cx23885_sram_channel_dump(dev,
1719 &dev->sram_channels[port->sram_chno]);
1720 cx23885_417_check_encoder(dev);
1721 } else if (status & VID_B_MSK_RISCI1) {
1722 dprintk(7, " VID_B_MSK_RISCI1\n");
1723 spin_lock(&port->slock);
1724 cx23885_wakeup(port, &port->mpegq, count);
1725 spin_unlock(&port->slock);
1726 } else if (status & VID_B_MSK_RISCI2) {
1727 dprintk(7, " VID_B_MSK_RISCI2\n");
1728 spin_lock(&port->slock);
1729 cx23885_restart_queue(port, &port->mpegq);
1730 spin_unlock(&port->slock);
1731 }
1732 if (status) {
1733 cx_write(port->reg_ts_int_stat, status);
1734 handled = 1;
1735 }
1736
1737 return handled;
1738}
1739
1740static int cx23885_irq_ts(struct cx23885_tsport *port, u32 status)
1741{
1742 struct cx23885_dev *dev = port->dev;
1743 int handled = 0;
1744 u32 count;
1745
1746 if ((status & VID_BC_MSK_OPC_ERR) ||
1747 (status & VID_BC_MSK_BAD_PKT) ||
1748 (status & VID_BC_MSK_SYNC) ||
1749 (status & VID_BC_MSK_OF)) {
1750
1751 if (status & VID_BC_MSK_OPC_ERR)
1752 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1753 VID_BC_MSK_OPC_ERR);
1754
1755 if (status & VID_BC_MSK_BAD_PKT)
1756 dprintk(7, " (VID_BC_MSK_BAD_PKT 0x%08x)\n",
1757 VID_BC_MSK_BAD_PKT);
1758
1759 if (status & VID_BC_MSK_SYNC)
1760 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x)\n",
1761 VID_BC_MSK_SYNC);
1762
1763 if (status & VID_BC_MSK_OF)
1764 dprintk(7, " (VID_BC_MSK_OF 0x%08x)\n",
1765 VID_BC_MSK_OF);
1766
1767 printk(KERN_ERR "%s: mpeg risc op code error\n", dev->name);
1768
1769 cx_clear(port->reg_dma_ctl, port->dma_ctl_val);
1770 cx23885_sram_channel_dump(dev,
1771 &dev->sram_channels[port->sram_chno]);
1772
1773 } else if (status & VID_BC_MSK_RISCI1) {
1774
1775 dprintk(7, " (RISCI1 0x%08x)\n", VID_BC_MSK_RISCI1);
1776
1777 spin_lock(&port->slock);
1778 count = cx_read(port->reg_gpcnt);
1779 cx23885_wakeup(port, &port->mpegq, count);
1780 spin_unlock(&port->slock);
1781
1782 } else if (status & VID_BC_MSK_RISCI2) {
1783
1784 dprintk(7, " (RISCI2 0x%08x)\n", VID_BC_MSK_RISCI2);
1785
1786 spin_lock(&port->slock);
1787 cx23885_restart_queue(port, &port->mpegq);
1788 spin_unlock(&port->slock);
1789
1790 }
1791 if (status) {
1792 cx_write(port->reg_ts_int_stat, status);
1793 handled = 1;
1794 }
1795
1796 return handled;
1797}
1798
1799static irqreturn_t cx23885_irq(int irq, void *dev_id)
1800{
1801 struct cx23885_dev *dev = dev_id;
1802 struct cx23885_tsport *ts1 = &dev->ts1;
1803 struct cx23885_tsport *ts2 = &dev->ts2;
1804 u32 pci_status, pci_mask;
1805 u32 vida_status, vida_mask;
1806 u32 audint_status, audint_mask;
1807 u32 ts1_status, ts1_mask;
1808 u32 ts2_status, ts2_mask;
1809 int vida_count = 0, ts1_count = 0, ts2_count = 0, handled = 0;
1810 int audint_count = 0;
1811 bool subdev_handled;
1812
1813 pci_status = cx_read(PCI_INT_STAT);
1814 pci_mask = cx23885_irq_get_mask(dev);
1815 vida_status = cx_read(VID_A_INT_STAT);
1816 vida_mask = cx_read(VID_A_INT_MSK);
1817 audint_status = cx_read(AUDIO_INT_INT_STAT);
1818 audint_mask = cx_read(AUDIO_INT_INT_MSK);
1819 ts1_status = cx_read(VID_B_INT_STAT);
1820 ts1_mask = cx_read(VID_B_INT_MSK);
1821 ts2_status = cx_read(VID_C_INT_STAT);
1822 ts2_mask = cx_read(VID_C_INT_MSK);
1823
1824 if ((pci_status == 0) && (ts2_status == 0) && (ts1_status == 0))
1825 goto out;
1826
1827 vida_count = cx_read(VID_A_GPCNT);
1828 audint_count = cx_read(AUD_INT_A_GPCNT);
1829 ts1_count = cx_read(ts1->reg_gpcnt);
1830 ts2_count = cx_read(ts2->reg_gpcnt);
1831 dprintk(7, "pci_status: 0x%08x pci_mask: 0x%08x\n",
1832 pci_status, pci_mask);
1833 dprintk(7, "vida_status: 0x%08x vida_mask: 0x%08x count: 0x%x\n",
1834 vida_status, vida_mask, vida_count);
1835 dprintk(7, "audint_status: 0x%08x audint_mask: 0x%08x count: 0x%x\n",
1836 audint_status, audint_mask, audint_count);
1837 dprintk(7, "ts1_status: 0x%08x ts1_mask: 0x%08x count: 0x%x\n",
1838 ts1_status, ts1_mask, ts1_count);
1839 dprintk(7, "ts2_status: 0x%08x ts2_mask: 0x%08x count: 0x%x\n",
1840 ts2_status, ts2_mask, ts2_count);
1841
1842 if (pci_status & (PCI_MSK_RISC_RD | PCI_MSK_RISC_WR |
1843 PCI_MSK_AL_RD | PCI_MSK_AL_WR | PCI_MSK_APB_DMA |
1844 PCI_MSK_VID_C | PCI_MSK_VID_B | PCI_MSK_VID_A |
1845 PCI_MSK_AUD_INT | PCI_MSK_AUD_EXT |
1846 PCI_MSK_GPIO0 | PCI_MSK_GPIO1 |
1847 PCI_MSK_AV_CORE | PCI_MSK_IR)) {
1848
1849 if (pci_status & PCI_MSK_RISC_RD)
1850 dprintk(7, " (PCI_MSK_RISC_RD 0x%08x)\n",
1851 PCI_MSK_RISC_RD);
1852
1853 if (pci_status & PCI_MSK_RISC_WR)
1854 dprintk(7, " (PCI_MSK_RISC_WR 0x%08x)\n",
1855 PCI_MSK_RISC_WR);
1856
1857 if (pci_status & PCI_MSK_AL_RD)
1858 dprintk(7, " (PCI_MSK_AL_RD 0x%08x)\n",
1859 PCI_MSK_AL_RD);
1860
1861 if (pci_status & PCI_MSK_AL_WR)
1862 dprintk(7, " (PCI_MSK_AL_WR 0x%08x)\n",
1863 PCI_MSK_AL_WR);
1864
1865 if (pci_status & PCI_MSK_APB_DMA)
1866 dprintk(7, " (PCI_MSK_APB_DMA 0x%08x)\n",
1867 PCI_MSK_APB_DMA);
1868
1869 if (pci_status & PCI_MSK_VID_C)
1870 dprintk(7, " (PCI_MSK_VID_C 0x%08x)\n",
1871 PCI_MSK_VID_C);
1872
1873 if (pci_status & PCI_MSK_VID_B)
1874 dprintk(7, " (PCI_MSK_VID_B 0x%08x)\n",
1875 PCI_MSK_VID_B);
1876
1877 if (pci_status & PCI_MSK_VID_A)
1878 dprintk(7, " (PCI_MSK_VID_A 0x%08x)\n",
1879 PCI_MSK_VID_A);
1880
1881 if (pci_status & PCI_MSK_AUD_INT)
1882 dprintk(7, " (PCI_MSK_AUD_INT 0x%08x)\n",
1883 PCI_MSK_AUD_INT);
1884
1885 if (pci_status & PCI_MSK_AUD_EXT)
1886 dprintk(7, " (PCI_MSK_AUD_EXT 0x%08x)\n",
1887 PCI_MSK_AUD_EXT);
1888
1889 if (pci_status & PCI_MSK_GPIO0)
1890 dprintk(7, " (PCI_MSK_GPIO0 0x%08x)\n",
1891 PCI_MSK_GPIO0);
1892
1893 if (pci_status & PCI_MSK_GPIO1)
1894 dprintk(7, " (PCI_MSK_GPIO1 0x%08x)\n",
1895 PCI_MSK_GPIO1);
1896
1897 if (pci_status & PCI_MSK_AV_CORE)
1898 dprintk(7, " (PCI_MSK_AV_CORE 0x%08x)\n",
1899 PCI_MSK_AV_CORE);
1900
1901 if (pci_status & PCI_MSK_IR)
1902 dprintk(7, " (PCI_MSK_IR 0x%08x)\n",
1903 PCI_MSK_IR);
1904 }
1905
1906 if (cx23885_boards[dev->board].ci_type == 1 &&
1907 (pci_status & (PCI_MSK_GPIO1 | PCI_MSK_GPIO0)))
1908 handled += netup_ci_slot_status(dev, pci_status);
1909
1910 if (cx23885_boards[dev->board].ci_type == 2 &&
1911 (pci_status & PCI_MSK_GPIO0))
1912 handled += altera_ci_irq(dev);
1913
1914 if (ts1_status) {
1915 if (cx23885_boards[dev->board].portb == CX23885_MPEG_DVB)
1916 handled += cx23885_irq_ts(ts1, ts1_status);
1917 else
1918 if (cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER)
1919 handled += cx23885_irq_417(dev, ts1_status);
1920 }
1921
1922 if (ts2_status) {
1923 if (cx23885_boards[dev->board].portc == CX23885_MPEG_DVB)
1924 handled += cx23885_irq_ts(ts2, ts2_status);
1925 else
1926 if (cx23885_boards[dev->board].portc == CX23885_MPEG_ENCODER)
1927 handled += cx23885_irq_417(dev, ts2_status);
1928 }
1929
1930 if (vida_status)
1931 handled += cx23885_video_irq(dev, vida_status);
1932
1933 if (audint_status)
1934 handled += cx23885_audio_irq(dev, audint_status, audint_mask);
1935
1936 if (pci_status & PCI_MSK_IR) {
1937 subdev_handled = false;
1938 v4l2_subdev_call(dev->sd_ir, core, interrupt_service_routine,
1939 pci_status, &subdev_handled);
1940 if (subdev_handled)
1941 handled++;
1942 }
1943
1944 if ((pci_status & pci_mask) & PCI_MSK_AV_CORE) {
1945 cx23885_irq_disable(dev, PCI_MSK_AV_CORE);
1946 if (!schedule_work(&dev->cx25840_work))
1947 printk(KERN_ERR "%s: failed to set up deferred work for"
1948 " AV Core/IR interrupt. Interrupt is disabled"
1949 " and won't be re-enabled\n", dev->name);
1950 handled++;
1951 }
1952
1953 if (handled)
1954 cx_write(PCI_INT_STAT, pci_status);
1955out:
1956 return IRQ_RETVAL(handled);
1957}
1958
1959static void cx23885_v4l2_dev_notify(struct v4l2_subdev *sd,
1960 unsigned int notification, void *arg)
1961{
1962 struct cx23885_dev *dev;
1963
1964 if (sd == NULL)
1965 return;
1966
1967 dev = to_cx23885(sd->v4l2_dev);
1968
1969 switch (notification) {
1970 case V4L2_SUBDEV_IR_RX_NOTIFY: /* Possibly called in an IRQ context */
1971 if (sd == dev->sd_ir)
1972 cx23885_ir_rx_v4l2_dev_notify(sd, *(u32 *)arg);
1973 break;
1974 case V4L2_SUBDEV_IR_TX_NOTIFY: /* Possibly called in an IRQ context */
1975 if (sd == dev->sd_ir)
1976 cx23885_ir_tx_v4l2_dev_notify(sd, *(u32 *)arg);
1977 break;
1978 }
1979}
1980
1981static void cx23885_v4l2_dev_notify_init(struct cx23885_dev *dev)
1982{
1983 INIT_WORK(&dev->cx25840_work, cx23885_av_work_handler);
1984 INIT_WORK(&dev->ir_rx_work, cx23885_ir_rx_work_handler);
1985 INIT_WORK(&dev->ir_tx_work, cx23885_ir_tx_work_handler);
1986 dev->v4l2_dev.notify = cx23885_v4l2_dev_notify;
1987}
1988
1989static inline int encoder_on_portb(struct cx23885_dev *dev)
1990{
1991 return cx23885_boards[dev->board].portb == CX23885_MPEG_ENCODER;
1992}
1993
1994static inline int encoder_on_portc(struct cx23885_dev *dev)
1995{
1996 return cx23885_boards[dev->board].portc == CX23885_MPEG_ENCODER;
1997}
1998
1999/* Mask represents 32 different GPIOs, GPIO's are split into multiple
2000 * registers depending on the board configuration (and whether the
2001 * 417 encoder (wi it's own GPIO's) are present. Each GPIO bit will
2002 * be pushed into the correct hardware register, regardless of the
2003 * physical location. Certain registers are shared so we sanity check
2004 * and report errors if we think we're tampering with a GPIo that might
2005 * be assigned to the encoder (and used for the host bus).
2006 *
2007 * GPIO 2 thru 0 - On the cx23885 bridge
2008 * GPIO 18 thru 3 - On the cx23417 host bus interface
2009 * GPIO 23 thru 19 - On the cx25840 a/v core
2010 */
2011void cx23885_gpio_set(struct cx23885_dev *dev, u32 mask)
2012{
2013 if (mask & 0x7)
2014 cx_set(GP0_IO, mask & 0x7);
2015
2016 if (mask & 0x0007fff8) {
2017 if (encoder_on_portb(dev) || encoder_on_portc(dev))
2018 printk(KERN_ERR
2019 "%s: Setting GPIO on encoder ports\n",
2020 dev->name);
2021 cx_set(MC417_RWD, (mask & 0x0007fff8) >> 3);
2022 }
2023
2024 /* TODO: 23-19 */
2025 if (mask & 0x00f80000)
2026 printk(KERN_INFO "%s: Unsupported\n", dev->name);
2027}
2028
2029void cx23885_gpio_clear(struct cx23885_dev *dev, u32 mask)
2030{
2031 if (mask & 0x00000007)
2032 cx_clear(GP0_IO, mask & 0x7);
2033
2034 if (mask & 0x0007fff8) {
2035 if (encoder_on_portb(dev) || encoder_on_portc(dev))
2036 printk(KERN_ERR
2037 "%s: Clearing GPIO moving on encoder ports\n",
2038 dev->name);
2039 cx_clear(MC417_RWD, (mask & 0x7fff8) >> 3);
2040 }
2041
2042 /* TODO: 23-19 */
2043 if (mask & 0x00f80000)
2044 printk(KERN_INFO "%s: Unsupported\n", dev->name);
2045}
2046
2047u32 cx23885_gpio_get(struct cx23885_dev *dev, u32 mask)
2048{
2049 if (mask & 0x00000007)
2050 return (cx_read(GP0_IO) >> 8) & mask & 0x7;
2051
2052 if (mask & 0x0007fff8) {
2053 if (encoder_on_portb(dev) || encoder_on_portc(dev))
2054 printk(KERN_ERR
2055 "%s: Reading GPIO moving on encoder ports\n",
2056 dev->name);
2057 return (cx_read(MC417_RWD) & ((mask & 0x7fff8) >> 3)) << 3;
2058 }
2059
2060 /* TODO: 23-19 */
2061 if (mask & 0x00f80000)
2062 printk(KERN_INFO "%s: Unsupported\n", dev->name);
2063
2064 return 0;
2065}
2066
2067void cx23885_gpio_enable(struct cx23885_dev *dev, u32 mask, int asoutput)
2068{
2069 if ((mask & 0x00000007) && asoutput)
2070 cx_set(GP0_IO, (mask & 0x7) << 16);
2071 else if ((mask & 0x00000007) && !asoutput)
2072 cx_clear(GP0_IO, (mask & 0x7) << 16);
2073
2074 if (mask & 0x0007fff8) {
2075 if (encoder_on_portb(dev) || encoder_on_portc(dev))
2076 printk(KERN_ERR
2077 "%s: Enabling GPIO on encoder ports\n",
2078 dev->name);
2079 }
2080
2081 /* MC417_OEN is active low for output, write 1 for an input */
2082 if ((mask & 0x0007fff8) && asoutput)
2083 cx_clear(MC417_OEN, (mask & 0x7fff8) >> 3);
2084
2085 else if ((mask & 0x0007fff8) && !asoutput)
2086 cx_set(MC417_OEN, (mask & 0x7fff8) >> 3);
2087
2088 /* TODO: 23-19 */
2089}
2090
2091static int __devinit cx23885_initdev(struct pci_dev *pci_dev,
2092 const struct pci_device_id *pci_id)
2093{
2094 struct cx23885_dev *dev;
2095 int err;
2096
2097 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2098 if (NULL == dev)
2099 return -ENOMEM;
2100
2101 err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
2102 if (err < 0)
2103 goto fail_free;
2104
2105 /* Prepare to handle notifications from subdevices */
2106 cx23885_v4l2_dev_notify_init(dev);
2107
2108 /* pci init */
2109 dev->pci = pci_dev;
2110 if (pci_enable_device(pci_dev)) {
2111 err = -EIO;
2112 goto fail_unreg;
2113 }
2114
2115 if (cx23885_dev_setup(dev) < 0) {
2116 err = -EINVAL;
2117 goto fail_unreg;
2118 }
2119
2120 /* print pci info */
2121 dev->pci_rev = pci_dev->revision;
2122 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
2123 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
2124 "latency: %d, mmio: 0x%llx\n", dev->name,
2125 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
2126 dev->pci_lat,
2127 (unsigned long long)pci_resource_start(pci_dev, 0));
2128
2129 pci_set_master(pci_dev);
2130 if (!pci_dma_supported(pci_dev, 0xffffffff)) {
2131 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
2132 err = -EIO;
2133 goto fail_irq;
2134 }
2135
2136 err = request_irq(pci_dev->irq, cx23885_irq,
2137 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
2138 if (err < 0) {
2139 printk(KERN_ERR "%s: can't get IRQ %d\n",
2140 dev->name, pci_dev->irq);
2141 goto fail_irq;
2142 }
2143
2144 switch (dev->board) {
2145 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
2146 cx23885_irq_add_enable(dev, PCI_MSK_GPIO1 | PCI_MSK_GPIO0);
2147 break;
2148 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
2149 cx23885_irq_add_enable(dev, PCI_MSK_GPIO0);
2150 break;
2151 }
2152
2153 /*
2154 * The CX2388[58] IR controller can start firing interrupts when
2155 * enabled, so these have to take place after the cx23885_irq() handler
2156 * is hooked up by the call to request_irq() above.
2157 */
2158 cx23885_ir_pci_int_enable(dev);
2159 cx23885_input_init(dev);
2160
2161 return 0;
2162
2163fail_irq:
2164 cx23885_dev_unregister(dev);
2165fail_unreg:
2166 v4l2_device_unregister(&dev->v4l2_dev);
2167fail_free:
2168 kfree(dev);
2169 return err;
2170}
2171
2172static void __devexit cx23885_finidev(struct pci_dev *pci_dev)
2173{
2174 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
2175 struct cx23885_dev *dev = to_cx23885(v4l2_dev);
2176
2177 cx23885_input_fini(dev);
2178 cx23885_ir_fini(dev);
2179
2180 cx23885_shutdown(dev);
2181
2182 pci_disable_device(pci_dev);
2183
2184 /* unregister stuff */
2185 free_irq(pci_dev->irq, dev);
2186
2187 cx23885_dev_unregister(dev);
2188 v4l2_device_unregister(v4l2_dev);
2189 kfree(dev);
2190}
2191
2192static struct pci_device_id cx23885_pci_tbl[] = {
2193 {
2194 /* CX23885 */
2195 .vendor = 0x14f1,
2196 .device = 0x8852,
2197 .subvendor = PCI_ANY_ID,
2198 .subdevice = PCI_ANY_ID,
2199 }, {
2200 /* CX23887 Rev 2 */
2201 .vendor = 0x14f1,
2202 .device = 0x8880,
2203 .subvendor = PCI_ANY_ID,
2204 .subdevice = PCI_ANY_ID,
2205 }, {
2206 /* --- end of list --- */
2207 }
2208};
2209MODULE_DEVICE_TABLE(pci, cx23885_pci_tbl);
2210
2211static struct pci_driver cx23885_pci_driver = {
2212 .name = "cx23885",
2213 .id_table = cx23885_pci_tbl,
2214 .probe = cx23885_initdev,
2215 .remove = __devexit_p(cx23885_finidev),
2216 /* TODO */
2217 .suspend = NULL,
2218 .resume = NULL,
2219};
2220
2221static int __init cx23885_init(void)
2222{
2223 printk(KERN_INFO "cx23885 driver version %s loaded\n",
2224 CX23885_VERSION);
2225 return pci_register_driver(&cx23885_pci_driver);
2226}
2227
2228static void __exit cx23885_fini(void)
2229{
2230 pci_unregister_driver(&cx23885_pci_driver);
2231}
2232
2233module_init(cx23885_init);
2234module_exit(cx23885_fini);
diff --git a/drivers/media/pci/cx23885/cx23885-dvb.c b/drivers/media/pci/cx23885/cx23885-dvb.c
new file mode 100644
index 000000000000..f3202a52d535
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-dvb.c
@@ -0,0 +1,1356 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/device.h>
25#include <linux/fs.h>
26#include <linux/kthread.h>
27#include <linux/file.h>
28#include <linux/suspend.h>
29
30#include "cx23885.h"
31#include <media/v4l2-common.h>
32
33#include "dvb_ca_en50221.h"
34#include "s5h1409.h"
35#include "s5h1411.h"
36#include "mt2131.h"
37#include "tda8290.h"
38#include "tda18271.h"
39#include "lgdt330x.h"
40#include "xc4000.h"
41#include "xc5000.h"
42#include "max2165.h"
43#include "tda10048.h"
44#include "tuner-xc2028.h"
45#include "tuner-simple.h"
46#include "dib7000p.h"
47#include "dibx000_common.h"
48#include "zl10353.h"
49#include "stv0900.h"
50#include "stv0900_reg.h"
51#include "stv6110.h"
52#include "lnbh24.h"
53#include "cx24116.h"
54#include "cimax2.h"
55#include "lgs8gxx.h"
56#include "netup-eeprom.h"
57#include "netup-init.h"
58#include "lgdt3305.h"
59#include "atbm8830.h"
60#include "ds3000.h"
61#include "cx23885-f300.h"
62#include "altera-ci.h"
63#include "stv0367.h"
64#include "drxk.h"
65#include "mt2063.h"
66
67static unsigned int debug;
68
69#define dprintk(level, fmt, arg...)\
70 do { if (debug >= level)\
71 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
72 } while (0)
73
74/* ------------------------------------------------------------------ */
75
76static unsigned int alt_tuner;
77module_param(alt_tuner, int, 0644);
78MODULE_PARM_DESC(alt_tuner, "Enable alternate tuner configuration");
79
80DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
81
82/* ------------------------------------------------------------------ */
83
84static int dvb_buf_setup(struct videobuf_queue *q,
85 unsigned int *count, unsigned int *size)
86{
87 struct cx23885_tsport *port = q->priv_data;
88
89 port->ts_packet_size = 188 * 4;
90 port->ts_packet_count = 32;
91
92 *size = port->ts_packet_size * port->ts_packet_count;
93 *count = 32;
94 return 0;
95}
96
97static int dvb_buf_prepare(struct videobuf_queue *q,
98 struct videobuf_buffer *vb, enum v4l2_field field)
99{
100 struct cx23885_tsport *port = q->priv_data;
101 return cx23885_buf_prepare(q, port, (struct cx23885_buffer *)vb, field);
102}
103
104static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
105{
106 struct cx23885_tsport *port = q->priv_data;
107 cx23885_buf_queue(port, (struct cx23885_buffer *)vb);
108}
109
110static void dvb_buf_release(struct videobuf_queue *q,
111 struct videobuf_buffer *vb)
112{
113 cx23885_free_buffer(q, (struct cx23885_buffer *)vb);
114}
115
116static int cx23885_dvb_set_frontend(struct dvb_frontend *fe);
117
118static void cx23885_dvb_gate_ctrl(struct cx23885_tsport *port, int open)
119{
120 struct videobuf_dvb_frontends *f;
121 struct videobuf_dvb_frontend *fe;
122
123 f = &port->frontends;
124
125 if (f->gate <= 1) /* undefined or fe0 */
126 fe = videobuf_dvb_get_frontend(f, 1);
127 else
128 fe = videobuf_dvb_get_frontend(f, f->gate);
129
130 if (fe && fe->dvb.frontend && fe->dvb.frontend->ops.i2c_gate_ctrl)
131 fe->dvb.frontend->ops.i2c_gate_ctrl(fe->dvb.frontend, open);
132
133 /*
134 * FIXME: Improve this path to avoid calling the
135 * cx23885_dvb_set_frontend() every time it passes here.
136 */
137 cx23885_dvb_set_frontend(fe->dvb.frontend);
138}
139
140static struct videobuf_queue_ops dvb_qops = {
141 .buf_setup = dvb_buf_setup,
142 .buf_prepare = dvb_buf_prepare,
143 .buf_queue = dvb_buf_queue,
144 .buf_release = dvb_buf_release,
145};
146
147static struct s5h1409_config hauppauge_generic_config = {
148 .demod_address = 0x32 >> 1,
149 .output_mode = S5H1409_SERIAL_OUTPUT,
150 .gpio = S5H1409_GPIO_ON,
151 .qam_if = 44000,
152 .inversion = S5H1409_INVERSION_OFF,
153 .status_mode = S5H1409_DEMODLOCKING,
154 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
155};
156
157static struct tda10048_config hauppauge_hvr1200_config = {
158 .demod_address = 0x10 >> 1,
159 .output_mode = TDA10048_SERIAL_OUTPUT,
160 .fwbulkwritelen = TDA10048_BULKWRITE_200,
161 .inversion = TDA10048_INVERSION_ON,
162 .dtv6_if_freq_khz = TDA10048_IF_3300,
163 .dtv7_if_freq_khz = TDA10048_IF_3800,
164 .dtv8_if_freq_khz = TDA10048_IF_4300,
165 .clk_freq_khz = TDA10048_CLK_16000,
166};
167
168static struct tda10048_config hauppauge_hvr1210_config = {
169 .demod_address = 0x10 >> 1,
170 .output_mode = TDA10048_SERIAL_OUTPUT,
171 .fwbulkwritelen = TDA10048_BULKWRITE_200,
172 .inversion = TDA10048_INVERSION_ON,
173 .dtv6_if_freq_khz = TDA10048_IF_3300,
174 .dtv7_if_freq_khz = TDA10048_IF_3500,
175 .dtv8_if_freq_khz = TDA10048_IF_4000,
176 .clk_freq_khz = TDA10048_CLK_16000,
177};
178
179static struct s5h1409_config hauppauge_ezqam_config = {
180 .demod_address = 0x32 >> 1,
181 .output_mode = S5H1409_SERIAL_OUTPUT,
182 .gpio = S5H1409_GPIO_OFF,
183 .qam_if = 4000,
184 .inversion = S5H1409_INVERSION_ON,
185 .status_mode = S5H1409_DEMODLOCKING,
186 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
187};
188
189static struct s5h1409_config hauppauge_hvr1800lp_config = {
190 .demod_address = 0x32 >> 1,
191 .output_mode = S5H1409_SERIAL_OUTPUT,
192 .gpio = S5H1409_GPIO_OFF,
193 .qam_if = 44000,
194 .inversion = S5H1409_INVERSION_OFF,
195 .status_mode = S5H1409_DEMODLOCKING,
196 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
197};
198
199static struct s5h1409_config hauppauge_hvr1500_config = {
200 .demod_address = 0x32 >> 1,
201 .output_mode = S5H1409_SERIAL_OUTPUT,
202 .gpio = S5H1409_GPIO_OFF,
203 .inversion = S5H1409_INVERSION_OFF,
204 .status_mode = S5H1409_DEMODLOCKING,
205 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
206};
207
208static struct mt2131_config hauppauge_generic_tunerconfig = {
209 0x61
210};
211
212static struct lgdt330x_config fusionhdtv_5_express = {
213 .demod_address = 0x0e,
214 .demod_chip = LGDT3303,
215 .serial_mpeg = 0x40,
216};
217
218static struct s5h1409_config hauppauge_hvr1500q_config = {
219 .demod_address = 0x32 >> 1,
220 .output_mode = S5H1409_SERIAL_OUTPUT,
221 .gpio = S5H1409_GPIO_ON,
222 .qam_if = 44000,
223 .inversion = S5H1409_INVERSION_OFF,
224 .status_mode = S5H1409_DEMODLOCKING,
225 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
226};
227
228static struct s5h1409_config dvico_s5h1409_config = {
229 .demod_address = 0x32 >> 1,
230 .output_mode = S5H1409_SERIAL_OUTPUT,
231 .gpio = S5H1409_GPIO_ON,
232 .qam_if = 44000,
233 .inversion = S5H1409_INVERSION_OFF,
234 .status_mode = S5H1409_DEMODLOCKING,
235 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
236};
237
238static struct s5h1411_config dvico_s5h1411_config = {
239 .output_mode = S5H1411_SERIAL_OUTPUT,
240 .gpio = S5H1411_GPIO_ON,
241 .qam_if = S5H1411_IF_44000,
242 .vsb_if = S5H1411_IF_44000,
243 .inversion = S5H1411_INVERSION_OFF,
244 .status_mode = S5H1411_DEMODLOCKING,
245 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
246};
247
248static struct s5h1411_config hcw_s5h1411_config = {
249 .output_mode = S5H1411_SERIAL_OUTPUT,
250 .gpio = S5H1411_GPIO_OFF,
251 .vsb_if = S5H1411_IF_44000,
252 .qam_if = S5H1411_IF_4000,
253 .inversion = S5H1411_INVERSION_ON,
254 .status_mode = S5H1411_DEMODLOCKING,
255 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
256};
257
258static struct xc5000_config hauppauge_hvr1500q_tunerconfig = {
259 .i2c_address = 0x61,
260 .if_khz = 5380,
261};
262
263static struct xc5000_config dvico_xc5000_tunerconfig = {
264 .i2c_address = 0x64,
265 .if_khz = 5380,
266};
267
268static struct tda829x_config tda829x_no_probe = {
269 .probe_tuner = TDA829X_DONT_PROBE,
270};
271
272static struct tda18271_std_map hauppauge_tda18271_std_map = {
273 .atsc_6 = { .if_freq = 5380, .agc_mode = 3, .std = 3,
274 .if_lvl = 6, .rfagc_top = 0x37 },
275 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0,
276 .if_lvl = 6, .rfagc_top = 0x37 },
277};
278
279static struct tda18271_std_map hauppauge_hvr1200_tda18271_std_map = {
280 .dvbt_6 = { .if_freq = 3300, .agc_mode = 3, .std = 4,
281 .if_lvl = 1, .rfagc_top = 0x37, },
282 .dvbt_7 = { .if_freq = 3800, .agc_mode = 3, .std = 5,
283 .if_lvl = 1, .rfagc_top = 0x37, },
284 .dvbt_8 = { .if_freq = 4300, .agc_mode = 3, .std = 6,
285 .if_lvl = 1, .rfagc_top = 0x37, },
286};
287
288static struct tda18271_config hauppauge_tda18271_config = {
289 .std_map = &hauppauge_tda18271_std_map,
290 .gate = TDA18271_GATE_ANALOG,
291 .output_opt = TDA18271_OUTPUT_LT_OFF,
292};
293
294static struct tda18271_config hauppauge_hvr1200_tuner_config = {
295 .std_map = &hauppauge_hvr1200_tda18271_std_map,
296 .gate = TDA18271_GATE_ANALOG,
297 .output_opt = TDA18271_OUTPUT_LT_OFF,
298};
299
300static struct tda18271_config hauppauge_hvr1210_tuner_config = {
301 .gate = TDA18271_GATE_DIGITAL,
302 .output_opt = TDA18271_OUTPUT_LT_OFF,
303};
304
305static struct tda18271_std_map hauppauge_hvr127x_std_map = {
306 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 4,
307 .if_lvl = 1, .rfagc_top = 0x58 },
308 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 5,
309 .if_lvl = 1, .rfagc_top = 0x58 },
310};
311
312static struct tda18271_config hauppauge_hvr127x_config = {
313 .std_map = &hauppauge_hvr127x_std_map,
314 .output_opt = TDA18271_OUTPUT_LT_OFF,
315};
316
317static struct lgdt3305_config hauppauge_lgdt3305_config = {
318 .i2c_addr = 0x0e,
319 .mpeg_mode = LGDT3305_MPEG_SERIAL,
320 .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
321 .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
322 .deny_i2c_rptr = 1,
323 .spectral_inversion = 1,
324 .qam_if_khz = 4000,
325 .vsb_if_khz = 3250,
326};
327
328static struct dibx000_agc_config xc3028_agc_config = {
329 BAND_VHF | BAND_UHF, /* band_caps */
330
331 /* P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=0,
332 * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0,
333 * P_agc_inh_dc_rv_est=0, P_agc_time_est=3, P_agc_freeze=0,
334 * P_agc_nb_est=2, P_agc_write=0
335 */
336 (0 << 15) | (0 << 14) | (0 << 11) | (0 << 10) | (0 << 9) | (0 << 8) |
337 (3 << 5) | (0 << 4) | (2 << 1) | (0 << 0), /* setup */
338
339 712, /* inv_gain */
340 21, /* time_stabiliz */
341
342 0, /* alpha_level */
343 118, /* thlock */
344
345 0, /* wbd_inv */
346 2867, /* wbd_ref */
347 0, /* wbd_sel */
348 2, /* wbd_alpha */
349
350 0, /* agc1_max */
351 0, /* agc1_min */
352 39718, /* agc2_max */
353 9930, /* agc2_min */
354 0, /* agc1_pt1 */
355 0, /* agc1_pt2 */
356 0, /* agc1_pt3 */
357 0, /* agc1_slope1 */
358 0, /* agc1_slope2 */
359 0, /* agc2_pt1 */
360 128, /* agc2_pt2 */
361 29, /* agc2_slope1 */
362 29, /* agc2_slope2 */
363
364 17, /* alpha_mant */
365 27, /* alpha_exp */
366 23, /* beta_mant */
367 51, /* beta_exp */
368
369 1, /* perform_agc_softsplit */
370};
371
372/* PLL Configuration for COFDM BW_MHz = 8.000000
373 * With external clock = 30.000000 */
374static struct dibx000_bandwidth_config xc3028_bw_config = {
375 60000, /* internal */
376 30000, /* sampling */
377 1, /* pll_cfg: prediv */
378 8, /* pll_cfg: ratio */
379 3, /* pll_cfg: range */
380 1, /* pll_cfg: reset */
381 0, /* pll_cfg: bypass */
382 0, /* misc: refdiv */
383 0, /* misc: bypclk_div */
384 1, /* misc: IO_CLK_en_core */
385 1, /* misc: ADClkSrc */
386 0, /* misc: modulo */
387 (3 << 14) | (1 << 12) | (524 << 0), /* sad_cfg: refsel, sel, freq_15k */
388 (1 << 25) | 5816102, /* ifreq = 5.200000 MHz */
389 20452225, /* timf */
390 30000000 /* xtal_hz */
391};
392
393static struct dib7000p_config hauppauge_hvr1400_dib7000_config = {
394 .output_mpeg2_in_188_bytes = 1,
395 .hostbus_diversity = 1,
396 .tuner_is_baseband = 0,
397 .update_lna = NULL,
398
399 .agc_config_count = 1,
400 .agc = &xc3028_agc_config,
401 .bw = &xc3028_bw_config,
402
403 .gpio_dir = DIB7000P_GPIO_DEFAULT_DIRECTIONS,
404 .gpio_val = DIB7000P_GPIO_DEFAULT_VALUES,
405 .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
406
407 .pwm_freq_div = 0,
408 .agc_control = NULL,
409 .spur_protect = 0,
410
411 .output_mode = OUTMODE_MPEG2_SERIAL,
412};
413
414static struct zl10353_config dvico_fusionhdtv_xc3028 = {
415 .demod_address = 0x0f,
416 .if2 = 45600,
417 .no_tuner = 1,
418 .disable_i2c_gate_ctrl = 1,
419};
420
421static struct stv0900_reg stv0900_ts_regs[] = {
422 { R0900_TSGENERAL, 0x00 },
423 { R0900_P1_TSSPEED, 0x40 },
424 { R0900_P2_TSSPEED, 0x40 },
425 { R0900_P1_TSCFGM, 0xc0 },
426 { R0900_P2_TSCFGM, 0xc0 },
427 { R0900_P1_TSCFGH, 0xe0 },
428 { R0900_P2_TSCFGH, 0xe0 },
429 { R0900_P1_TSCFGL, 0x20 },
430 { R0900_P2_TSCFGL, 0x20 },
431 { 0xffff, 0xff }, /* terminate */
432};
433
434static struct stv0900_config netup_stv0900_config = {
435 .demod_address = 0x68,
436 .demod_mode = 1, /* dual */
437 .xtal = 8000000,
438 .clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
439 .diseqc_mode = 2,/* 2/3 PWM */
440 .ts_config_regs = stv0900_ts_regs,
441 .tun1_maddress = 0,/* 0x60 */
442 .tun2_maddress = 3,/* 0x63 */
443 .tun1_adc = 1,/* 1 Vpp */
444 .tun2_adc = 1,/* 1 Vpp */
445};
446
447static struct stv6110_config netup_stv6110_tunerconfig_a = {
448 .i2c_address = 0x60,
449 .mclk = 16000000,
450 .clk_div = 1,
451 .gain = 8, /* +16 dB - maximum gain */
452};
453
454static struct stv6110_config netup_stv6110_tunerconfig_b = {
455 .i2c_address = 0x63,
456 .mclk = 16000000,
457 .clk_div = 1,
458 .gain = 8, /* +16 dB - maximum gain */
459};
460
461static struct cx24116_config tbs_cx24116_config = {
462 .demod_address = 0x55,
463};
464
465static struct ds3000_config tevii_ds3000_config = {
466 .demod_address = 0x68,
467};
468
469static struct cx24116_config dvbworld_cx24116_config = {
470 .demod_address = 0x05,
471};
472
473static struct lgs8gxx_config mygica_x8506_lgs8gl5_config = {
474 .prod = LGS8GXX_PROD_LGS8GL5,
475 .demod_address = 0x19,
476 .serial_ts = 0,
477 .ts_clk_pol = 1,
478 .ts_clk_gated = 1,
479 .if_clk_freq = 30400, /* 30.4 MHz */
480 .if_freq = 5380, /* 5.38 MHz */
481 .if_neg_center = 1,
482 .ext_adc = 0,
483 .adc_signed = 0,
484 .if_neg_edge = 0,
485};
486
487static struct xc5000_config mygica_x8506_xc5000_config = {
488 .i2c_address = 0x61,
489 .if_khz = 5380,
490};
491
492static int cx23885_dvb_set_frontend(struct dvb_frontend *fe)
493{
494 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
495 struct cx23885_tsport *port = fe->dvb->priv;
496 struct cx23885_dev *dev = port->dev;
497
498 switch (dev->board) {
499 case CX23885_BOARD_HAUPPAUGE_HVR1275:
500 switch (p->modulation) {
501 case VSB_8:
502 cx23885_gpio_clear(dev, GPIO_5);
503 break;
504 case QAM_64:
505 case QAM_256:
506 default:
507 cx23885_gpio_set(dev, GPIO_5);
508 break;
509 }
510 break;
511 case CX23885_BOARD_MYGICA_X8506:
512 case CX23885_BOARD_MAGICPRO_PROHDTVE2:
513 /* Select Digital TV */
514 cx23885_gpio_set(dev, GPIO_0);
515 break;
516 }
517 return 0;
518}
519
520static struct lgs8gxx_config magicpro_prohdtve2_lgs8g75_config = {
521 .prod = LGS8GXX_PROD_LGS8G75,
522 .demod_address = 0x19,
523 .serial_ts = 0,
524 .ts_clk_pol = 1,
525 .ts_clk_gated = 1,
526 .if_clk_freq = 30400, /* 30.4 MHz */
527 .if_freq = 6500, /* 6.50 MHz */
528 .if_neg_center = 1,
529 .ext_adc = 0,
530 .adc_signed = 1,
531 .adc_vpp = 2, /* 1.6 Vpp */
532 .if_neg_edge = 1,
533};
534
535static struct xc5000_config magicpro_prohdtve2_xc5000_config = {
536 .i2c_address = 0x61,
537 .if_khz = 6500,
538};
539
540static struct atbm8830_config mygica_x8558pro_atbm8830_cfg1 = {
541 .prod = ATBM8830_PROD_8830,
542 .demod_address = 0x44,
543 .serial_ts = 0,
544 .ts_sampling_edge = 1,
545 .ts_clk_gated = 0,
546 .osc_clk_freq = 30400, /* in kHz */
547 .if_freq = 0, /* zero IF */
548 .zif_swap_iq = 1,
549 .agc_min = 0x2E,
550 .agc_max = 0xFF,
551 .agc_hold_loop = 0,
552};
553
554static struct max2165_config mygic_x8558pro_max2165_cfg1 = {
555 .i2c_address = 0x60,
556 .osc_clk = 20
557};
558
559static struct atbm8830_config mygica_x8558pro_atbm8830_cfg2 = {
560 .prod = ATBM8830_PROD_8830,
561 .demod_address = 0x44,
562 .serial_ts = 1,
563 .ts_sampling_edge = 1,
564 .ts_clk_gated = 0,
565 .osc_clk_freq = 30400, /* in kHz */
566 .if_freq = 0, /* zero IF */
567 .zif_swap_iq = 1,
568 .agc_min = 0x2E,
569 .agc_max = 0xFF,
570 .agc_hold_loop = 0,
571};
572
573static struct max2165_config mygic_x8558pro_max2165_cfg2 = {
574 .i2c_address = 0x60,
575 .osc_clk = 20
576};
577static struct stv0367_config netup_stv0367_config[] = {
578 {
579 .demod_address = 0x1c,
580 .xtal = 27000000,
581 .if_khz = 4500,
582 .if_iq_mode = 0,
583 .ts_mode = 1,
584 .clk_pol = 0,
585 }, {
586 .demod_address = 0x1d,
587 .xtal = 27000000,
588 .if_khz = 4500,
589 .if_iq_mode = 0,
590 .ts_mode = 1,
591 .clk_pol = 0,
592 },
593};
594
595static struct xc5000_config netup_xc5000_config[] = {
596 {
597 .i2c_address = 0x61,
598 .if_khz = 4500,
599 }, {
600 .i2c_address = 0x64,
601 .if_khz = 4500,
602 },
603};
604
605static struct drxk_config terratec_drxk_config[] = {
606 {
607 .adr = 0x29,
608 .no_i2c_bridge = 1,
609 }, {
610 .adr = 0x2a,
611 .no_i2c_bridge = 1,
612 },
613};
614
615static struct mt2063_config terratec_mt2063_config[] = {
616 {
617 .tuner_address = 0x60,
618 }, {
619 .tuner_address = 0x67,
620 },
621};
622
623int netup_altera_fpga_rw(void *device, int flag, int data, int read)
624{
625 struct cx23885_dev *dev = (struct cx23885_dev *)device;
626 unsigned long timeout = jiffies + msecs_to_jiffies(1);
627 uint32_t mem = 0;
628
629 mem = cx_read(MC417_RWD);
630 if (read)
631 cx_set(MC417_OEN, ALT_DATA);
632 else {
633 cx_clear(MC417_OEN, ALT_DATA);/* D0-D7 out */
634 mem &= ~ALT_DATA;
635 mem |= (data & ALT_DATA);
636 }
637
638 if (flag)
639 mem |= ALT_AD_RG;
640 else
641 mem &= ~ALT_AD_RG;
642
643 mem &= ~ALT_CS;
644 if (read)
645 mem = (mem & ~ALT_RD) | ALT_WR;
646 else
647 mem = (mem & ~ALT_WR) | ALT_RD;
648
649 cx_write(MC417_RWD, mem); /* start RW cycle */
650
651 for (;;) {
652 mem = cx_read(MC417_RWD);
653 if ((mem & ALT_RDY) == 0)
654 break;
655 if (time_after(jiffies, timeout))
656 break;
657 udelay(1);
658 }
659
660 cx_set(MC417_RWD, ALT_RD | ALT_WR | ALT_CS);
661 if (read)
662 return mem & ALT_DATA;
663
664 return 0;
665};
666
667static int dvb_register(struct cx23885_tsport *port)
668{
669 struct cx23885_dev *dev = port->dev;
670 struct cx23885_i2c *i2c_bus = NULL, *i2c_bus2 = NULL;
671 struct videobuf_dvb_frontend *fe0, *fe1 = NULL;
672 int mfe_shared = 0; /* bus not shared by default */
673 int ret;
674
675 /* Get the first frontend */
676 fe0 = videobuf_dvb_get_frontend(&port->frontends, 1);
677 if (!fe0)
678 return -EINVAL;
679
680 /* init struct videobuf_dvb */
681 fe0->dvb.name = dev->name;
682
683 /* multi-frontend gate control is undefined or defaults to fe0 */
684 port->frontends.gate = 0;
685
686 /* Sets the gate control callback to be used by i2c command calls */
687 port->gate_ctrl = cx23885_dvb_gate_ctrl;
688
689 /* init frontend */
690 switch (dev->board) {
691 case CX23885_BOARD_HAUPPAUGE_HVR1250:
692 i2c_bus = &dev->i2c_bus[0];
693 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
694 &hauppauge_generic_config,
695 &i2c_bus->i2c_adap);
696 if (fe0->dvb.frontend != NULL) {
697 dvb_attach(mt2131_attach, fe0->dvb.frontend,
698 &i2c_bus->i2c_adap,
699 &hauppauge_generic_tunerconfig, 0);
700 }
701 break;
702 case CX23885_BOARD_HAUPPAUGE_HVR1270:
703 case CX23885_BOARD_HAUPPAUGE_HVR1275:
704 i2c_bus = &dev->i2c_bus[0];
705 fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
706 &hauppauge_lgdt3305_config,
707 &i2c_bus->i2c_adap);
708 if (fe0->dvb.frontend != NULL) {
709 dvb_attach(tda18271_attach, fe0->dvb.frontend,
710 0x60, &dev->i2c_bus[1].i2c_adap,
711 &hauppauge_hvr127x_config);
712 }
713 break;
714 case CX23885_BOARD_HAUPPAUGE_HVR1255:
715 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
716 i2c_bus = &dev->i2c_bus[0];
717 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
718 &hcw_s5h1411_config,
719 &i2c_bus->i2c_adap);
720 if (fe0->dvb.frontend != NULL) {
721 dvb_attach(tda18271_attach, fe0->dvb.frontend,
722 0x60, &dev->i2c_bus[1].i2c_adap,
723 &hauppauge_tda18271_config);
724 }
725
726 tda18271_attach(&dev->ts1.analog_fe,
727 0x60, &dev->i2c_bus[1].i2c_adap,
728 &hauppauge_tda18271_config);
729
730 break;
731 case CX23885_BOARD_HAUPPAUGE_HVR1800:
732 i2c_bus = &dev->i2c_bus[0];
733 switch (alt_tuner) {
734 case 1:
735 fe0->dvb.frontend =
736 dvb_attach(s5h1409_attach,
737 &hauppauge_ezqam_config,
738 &i2c_bus->i2c_adap);
739 if (fe0->dvb.frontend != NULL) {
740 dvb_attach(tda829x_attach, fe0->dvb.frontend,
741 &dev->i2c_bus[1].i2c_adap, 0x42,
742 &tda829x_no_probe);
743 dvb_attach(tda18271_attach, fe0->dvb.frontend,
744 0x60, &dev->i2c_bus[1].i2c_adap,
745 &hauppauge_tda18271_config);
746 }
747 break;
748 case 0:
749 default:
750 fe0->dvb.frontend =
751 dvb_attach(s5h1409_attach,
752 &hauppauge_generic_config,
753 &i2c_bus->i2c_adap);
754 if (fe0->dvb.frontend != NULL)
755 dvb_attach(mt2131_attach, fe0->dvb.frontend,
756 &i2c_bus->i2c_adap,
757 &hauppauge_generic_tunerconfig, 0);
758 break;
759 }
760 break;
761 case CX23885_BOARD_HAUPPAUGE_HVR1800lp:
762 i2c_bus = &dev->i2c_bus[0];
763 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
764 &hauppauge_hvr1800lp_config,
765 &i2c_bus->i2c_adap);
766 if (fe0->dvb.frontend != NULL) {
767 dvb_attach(mt2131_attach, fe0->dvb.frontend,
768 &i2c_bus->i2c_adap,
769 &hauppauge_generic_tunerconfig, 0);
770 }
771 break;
772 case CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP:
773 i2c_bus = &dev->i2c_bus[0];
774 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
775 &fusionhdtv_5_express,
776 &i2c_bus->i2c_adap);
777 if (fe0->dvb.frontend != NULL) {
778 dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
779 &i2c_bus->i2c_adap, 0x61,
780 TUNER_LG_TDVS_H06XF);
781 }
782 break;
783 case CX23885_BOARD_HAUPPAUGE_HVR1500Q:
784 i2c_bus = &dev->i2c_bus[1];
785 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
786 &hauppauge_hvr1500q_config,
787 &dev->i2c_bus[0].i2c_adap);
788 if (fe0->dvb.frontend != NULL)
789 dvb_attach(xc5000_attach, fe0->dvb.frontend,
790 &i2c_bus->i2c_adap,
791 &hauppauge_hvr1500q_tunerconfig);
792 break;
793 case CX23885_BOARD_HAUPPAUGE_HVR1500:
794 i2c_bus = &dev->i2c_bus[1];
795 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
796 &hauppauge_hvr1500_config,
797 &dev->i2c_bus[0].i2c_adap);
798 if (fe0->dvb.frontend != NULL) {
799 struct dvb_frontend *fe;
800 struct xc2028_config cfg = {
801 .i2c_adap = &i2c_bus->i2c_adap,
802 .i2c_addr = 0x61,
803 };
804 static struct xc2028_ctrl ctl = {
805 .fname = XC2028_DEFAULT_FIRMWARE,
806 .max_len = 64,
807 .demod = XC3028_FE_OREN538,
808 };
809
810 fe = dvb_attach(xc2028_attach,
811 fe0->dvb.frontend, &cfg);
812 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
813 fe->ops.tuner_ops.set_config(fe, &ctl);
814 }
815 break;
816 case CX23885_BOARD_HAUPPAUGE_HVR1200:
817 case CX23885_BOARD_HAUPPAUGE_HVR1700:
818 i2c_bus = &dev->i2c_bus[0];
819 fe0->dvb.frontend = dvb_attach(tda10048_attach,
820 &hauppauge_hvr1200_config,
821 &i2c_bus->i2c_adap);
822 if (fe0->dvb.frontend != NULL) {
823 dvb_attach(tda829x_attach, fe0->dvb.frontend,
824 &dev->i2c_bus[1].i2c_adap, 0x42,
825 &tda829x_no_probe);
826 dvb_attach(tda18271_attach, fe0->dvb.frontend,
827 0x60, &dev->i2c_bus[1].i2c_adap,
828 &hauppauge_hvr1200_tuner_config);
829 }
830 break;
831 case CX23885_BOARD_HAUPPAUGE_HVR1210:
832 i2c_bus = &dev->i2c_bus[0];
833 fe0->dvb.frontend = dvb_attach(tda10048_attach,
834 &hauppauge_hvr1210_config,
835 &i2c_bus->i2c_adap);
836 if (fe0->dvb.frontend != NULL) {
837 dvb_attach(tda18271_attach, fe0->dvb.frontend,
838 0x60, &dev->i2c_bus[1].i2c_adap,
839 &hauppauge_hvr1210_tuner_config);
840 }
841 break;
842 case CX23885_BOARD_HAUPPAUGE_HVR1400:
843 i2c_bus = &dev->i2c_bus[0];
844 fe0->dvb.frontend = dvb_attach(dib7000p_attach,
845 &i2c_bus->i2c_adap,
846 0x12, &hauppauge_hvr1400_dib7000_config);
847 if (fe0->dvb.frontend != NULL) {
848 struct dvb_frontend *fe;
849 struct xc2028_config cfg = {
850 .i2c_adap = &dev->i2c_bus[1].i2c_adap,
851 .i2c_addr = 0x64,
852 };
853 static struct xc2028_ctrl ctl = {
854 .fname = XC3028L_DEFAULT_FIRMWARE,
855 .max_len = 64,
856 .demod = XC3028_FE_DIBCOM52,
857 /* This is true for all demods with
858 v36 firmware? */
859 .type = XC2028_D2633,
860 };
861
862 fe = dvb_attach(xc2028_attach,
863 fe0->dvb.frontend, &cfg);
864 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
865 fe->ops.tuner_ops.set_config(fe, &ctl);
866 }
867 break;
868 case CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP:
869 i2c_bus = &dev->i2c_bus[port->nr - 1];
870
871 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
872 &dvico_s5h1409_config,
873 &i2c_bus->i2c_adap);
874 if (fe0->dvb.frontend == NULL)
875 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
876 &dvico_s5h1411_config,
877 &i2c_bus->i2c_adap);
878 if (fe0->dvb.frontend != NULL)
879 dvb_attach(xc5000_attach, fe0->dvb.frontend,
880 &i2c_bus->i2c_adap,
881 &dvico_xc5000_tunerconfig);
882 break;
883 case CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP: {
884 i2c_bus = &dev->i2c_bus[port->nr - 1];
885
886 fe0->dvb.frontend = dvb_attach(zl10353_attach,
887 &dvico_fusionhdtv_xc3028,
888 &i2c_bus->i2c_adap);
889 if (fe0->dvb.frontend != NULL) {
890 struct dvb_frontend *fe;
891 struct xc2028_config cfg = {
892 .i2c_adap = &i2c_bus->i2c_adap,
893 .i2c_addr = 0x61,
894 };
895 static struct xc2028_ctrl ctl = {
896 .fname = XC2028_DEFAULT_FIRMWARE,
897 .max_len = 64,
898 .demod = XC3028_FE_ZARLINK456,
899 };
900
901 fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
902 &cfg);
903 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
904 fe->ops.tuner_ops.set_config(fe, &ctl);
905 }
906 break;
907 }
908 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H:
909 case CX23885_BOARD_COMPRO_VIDEOMATE_E650F:
910 case CX23885_BOARD_COMPRO_VIDEOMATE_E800:
911 i2c_bus = &dev->i2c_bus[0];
912
913 fe0->dvb.frontend = dvb_attach(zl10353_attach,
914 &dvico_fusionhdtv_xc3028,
915 &i2c_bus->i2c_adap);
916 if (fe0->dvb.frontend != NULL) {
917 struct dvb_frontend *fe;
918 struct xc2028_config cfg = {
919 .i2c_adap = &dev->i2c_bus[1].i2c_adap,
920 .i2c_addr = 0x61,
921 };
922 static struct xc2028_ctrl ctl = {
923 .fname = XC2028_DEFAULT_FIRMWARE,
924 .max_len = 64,
925 .demod = XC3028_FE_ZARLINK456,
926 };
927
928 fe = dvb_attach(xc2028_attach, fe0->dvb.frontend,
929 &cfg);
930 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
931 fe->ops.tuner_ops.set_config(fe, &ctl);
932 }
933 break;
934 case CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000:
935 i2c_bus = &dev->i2c_bus[0];
936
937 fe0->dvb.frontend = dvb_attach(zl10353_attach,
938 &dvico_fusionhdtv_xc3028,
939 &i2c_bus->i2c_adap);
940 if (fe0->dvb.frontend != NULL) {
941 struct dvb_frontend *fe;
942 struct xc4000_config cfg = {
943 .i2c_address = 0x61,
944 .default_pm = 0,
945 .dvb_amplitude = 134,
946 .set_smoothedcvbs = 1,
947 .if_khz = 4560
948 };
949
950 fe = dvb_attach(xc4000_attach, fe0->dvb.frontend,
951 &dev->i2c_bus[1].i2c_adap, &cfg);
952 if (!fe) {
953 printk(KERN_ERR "%s/2: xc4000 attach failed\n",
954 dev->name);
955 goto frontend_detach;
956 }
957 }
958 break;
959 case CX23885_BOARD_TBS_6920:
960 i2c_bus = &dev->i2c_bus[1];
961
962 fe0->dvb.frontend = dvb_attach(cx24116_attach,
963 &tbs_cx24116_config,
964 &i2c_bus->i2c_adap);
965 if (fe0->dvb.frontend != NULL)
966 fe0->dvb.frontend->ops.set_voltage = f300_set_voltage;
967
968 break;
969 case CX23885_BOARD_TEVII_S470:
970 i2c_bus = &dev->i2c_bus[1];
971
972 fe0->dvb.frontend = dvb_attach(ds3000_attach,
973 &tevii_ds3000_config,
974 &i2c_bus->i2c_adap);
975 if (fe0->dvb.frontend != NULL)
976 fe0->dvb.frontend->ops.set_voltage = f300_set_voltage;
977
978 break;
979 case CX23885_BOARD_DVBWORLD_2005:
980 i2c_bus = &dev->i2c_bus[1];
981
982 fe0->dvb.frontend = dvb_attach(cx24116_attach,
983 &dvbworld_cx24116_config,
984 &i2c_bus->i2c_adap);
985 break;
986 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
987 i2c_bus = &dev->i2c_bus[0];
988 switch (port->nr) {
989 /* port B */
990 case 1:
991 fe0->dvb.frontend = dvb_attach(stv0900_attach,
992 &netup_stv0900_config,
993 &i2c_bus->i2c_adap, 0);
994 if (fe0->dvb.frontend != NULL) {
995 if (dvb_attach(stv6110_attach,
996 fe0->dvb.frontend,
997 &netup_stv6110_tunerconfig_a,
998 &i2c_bus->i2c_adap)) {
999 if (!dvb_attach(lnbh24_attach,
1000 fe0->dvb.frontend,
1001 &i2c_bus->i2c_adap,
1002 LNBH24_PCL | LNBH24_TTX,
1003 LNBH24_TEN, 0x09))
1004 printk(KERN_ERR
1005 "No LNBH24 found!\n");
1006
1007 }
1008 }
1009 break;
1010 /* port C */
1011 case 2:
1012 fe0->dvb.frontend = dvb_attach(stv0900_attach,
1013 &netup_stv0900_config,
1014 &i2c_bus->i2c_adap, 1);
1015 if (fe0->dvb.frontend != NULL) {
1016 if (dvb_attach(stv6110_attach,
1017 fe0->dvb.frontend,
1018 &netup_stv6110_tunerconfig_b,
1019 &i2c_bus->i2c_adap)) {
1020 if (!dvb_attach(lnbh24_attach,
1021 fe0->dvb.frontend,
1022 &i2c_bus->i2c_adap,
1023 LNBH24_PCL | LNBH24_TTX,
1024 LNBH24_TEN, 0x0a))
1025 printk(KERN_ERR
1026 "No LNBH24 found!\n");
1027
1028 }
1029 }
1030 break;
1031 }
1032 break;
1033 case CX23885_BOARD_MYGICA_X8506:
1034 i2c_bus = &dev->i2c_bus[0];
1035 i2c_bus2 = &dev->i2c_bus[1];
1036 fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1037 &mygica_x8506_lgs8gl5_config,
1038 &i2c_bus->i2c_adap);
1039 if (fe0->dvb.frontend != NULL) {
1040 dvb_attach(xc5000_attach,
1041 fe0->dvb.frontend,
1042 &i2c_bus2->i2c_adap,
1043 &mygica_x8506_xc5000_config);
1044 }
1045 break;
1046 case CX23885_BOARD_MAGICPRO_PROHDTVE2:
1047 i2c_bus = &dev->i2c_bus[0];
1048 i2c_bus2 = &dev->i2c_bus[1];
1049 fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1050 &magicpro_prohdtve2_lgs8g75_config,
1051 &i2c_bus->i2c_adap);
1052 if (fe0->dvb.frontend != NULL) {
1053 dvb_attach(xc5000_attach,
1054 fe0->dvb.frontend,
1055 &i2c_bus2->i2c_adap,
1056 &magicpro_prohdtve2_xc5000_config);
1057 }
1058 break;
1059 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1060 i2c_bus = &dev->i2c_bus[0];
1061 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1062 &hcw_s5h1411_config,
1063 &i2c_bus->i2c_adap);
1064 if (fe0->dvb.frontend != NULL)
1065 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1066 0x60, &dev->i2c_bus[0].i2c_adap,
1067 &hauppauge_tda18271_config);
1068
1069 tda18271_attach(&dev->ts1.analog_fe,
1070 0x60, &dev->i2c_bus[1].i2c_adap,
1071 &hauppauge_tda18271_config);
1072
1073 break;
1074 case CX23885_BOARD_HAUPPAUGE_HVR1290:
1075 i2c_bus = &dev->i2c_bus[0];
1076 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1077 &hcw_s5h1411_config,
1078 &i2c_bus->i2c_adap);
1079 if (fe0->dvb.frontend != NULL)
1080 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1081 0x60, &dev->i2c_bus[0].i2c_adap,
1082 &hauppauge_tda18271_config);
1083 break;
1084 case CX23885_BOARD_MYGICA_X8558PRO:
1085 switch (port->nr) {
1086 /* port B */
1087 case 1:
1088 i2c_bus = &dev->i2c_bus[0];
1089 fe0->dvb.frontend = dvb_attach(atbm8830_attach,
1090 &mygica_x8558pro_atbm8830_cfg1,
1091 &i2c_bus->i2c_adap);
1092 if (fe0->dvb.frontend != NULL) {
1093 dvb_attach(max2165_attach,
1094 fe0->dvb.frontend,
1095 &i2c_bus->i2c_adap,
1096 &mygic_x8558pro_max2165_cfg1);
1097 }
1098 break;
1099 /* port C */
1100 case 2:
1101 i2c_bus = &dev->i2c_bus[1];
1102 fe0->dvb.frontend = dvb_attach(atbm8830_attach,
1103 &mygica_x8558pro_atbm8830_cfg2,
1104 &i2c_bus->i2c_adap);
1105 if (fe0->dvb.frontend != NULL) {
1106 dvb_attach(max2165_attach,
1107 fe0->dvb.frontend,
1108 &i2c_bus->i2c_adap,
1109 &mygic_x8558pro_max2165_cfg2);
1110 }
1111 break;
1112 }
1113 break;
1114 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
1115 i2c_bus = &dev->i2c_bus[0];
1116 mfe_shared = 1;/* MFE */
1117 port->frontends.gate = 0;/* not clear for me yet */
1118 /* ports B, C */
1119 /* MFE frontend 1 DVB-T */
1120 fe0->dvb.frontend = dvb_attach(stv0367ter_attach,
1121 &netup_stv0367_config[port->nr - 1],
1122 &i2c_bus->i2c_adap);
1123 if (fe0->dvb.frontend != NULL) {
1124 if (NULL == dvb_attach(xc5000_attach,
1125 fe0->dvb.frontend,
1126 &i2c_bus->i2c_adap,
1127 &netup_xc5000_config[port->nr - 1]))
1128 goto frontend_detach;
1129 /* load xc5000 firmware */
1130 fe0->dvb.frontend->ops.tuner_ops.init(fe0->dvb.frontend);
1131 }
1132 /* MFE frontend 2 */
1133 fe1 = videobuf_dvb_get_frontend(&port->frontends, 2);
1134 if (fe1 == NULL)
1135 goto frontend_detach;
1136 /* DVB-C init */
1137 fe1->dvb.frontend = dvb_attach(stv0367cab_attach,
1138 &netup_stv0367_config[port->nr - 1],
1139 &i2c_bus->i2c_adap);
1140 if (fe1->dvb.frontend != NULL) {
1141 fe1->dvb.frontend->id = 1;
1142 if (NULL == dvb_attach(xc5000_attach,
1143 fe1->dvb.frontend,
1144 &i2c_bus->i2c_adap,
1145 &netup_xc5000_config[port->nr - 1]))
1146 goto frontend_detach;
1147 }
1148 break;
1149 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
1150 i2c_bus = &dev->i2c_bus[0];
1151 i2c_bus2 = &dev->i2c_bus[1];
1152
1153 switch (port->nr) {
1154 /* port b */
1155 case 1:
1156 fe0->dvb.frontend = dvb_attach(drxk_attach,
1157 &terratec_drxk_config[0],
1158 &i2c_bus->i2c_adap);
1159 if (fe0->dvb.frontend != NULL) {
1160 if (!dvb_attach(mt2063_attach,
1161 fe0->dvb.frontend,
1162 &terratec_mt2063_config[0],
1163 &i2c_bus2->i2c_adap))
1164 goto frontend_detach;
1165 }
1166 break;
1167 /* port c */
1168 case 2:
1169 fe0->dvb.frontend = dvb_attach(drxk_attach,
1170 &terratec_drxk_config[1],
1171 &i2c_bus->i2c_adap);
1172 if (fe0->dvb.frontend != NULL) {
1173 if (!dvb_attach(mt2063_attach,
1174 fe0->dvb.frontend,
1175 &terratec_mt2063_config[1],
1176 &i2c_bus2->i2c_adap))
1177 goto frontend_detach;
1178 }
1179 break;
1180 }
1181 break;
1182 case CX23885_BOARD_TEVII_S471:
1183 i2c_bus = &dev->i2c_bus[1];
1184
1185 fe0->dvb.frontend = dvb_attach(ds3000_attach,
1186 &tevii_ds3000_config,
1187 &i2c_bus->i2c_adap);
1188 break;
1189 default:
1190 printk(KERN_INFO "%s: The frontend of your DVB/ATSC card "
1191 " isn't supported yet\n",
1192 dev->name);
1193 break;
1194 }
1195
1196 if ((NULL == fe0->dvb.frontend) || (fe1 && NULL == fe1->dvb.frontend)) {
1197 printk(KERN_ERR "%s: frontend initialization failed\n",
1198 dev->name);
1199 goto frontend_detach;
1200 }
1201
1202 /* define general-purpose callback pointer */
1203 fe0->dvb.frontend->callback = cx23885_tuner_callback;
1204 if (fe1)
1205 fe1->dvb.frontend->callback = cx23885_tuner_callback;
1206#if 0
1207 /* Ensure all frontends negotiate bus access */
1208 fe0->dvb.frontend->ops.ts_bus_ctrl = cx23885_dvb_bus_ctrl;
1209 if (fe1)
1210 fe1->dvb.frontend->ops.ts_bus_ctrl = cx23885_dvb_bus_ctrl;
1211#endif
1212
1213 /* Put the analog decoder in standby to keep it quiet */
1214 call_all(dev, core, s_power, 0);
1215
1216 if (fe0->dvb.frontend->ops.analog_ops.standby)
1217 fe0->dvb.frontend->ops.analog_ops.standby(fe0->dvb.frontend);
1218
1219 /* register everything */
1220 ret = videobuf_dvb_register_bus(&port->frontends, THIS_MODULE, port,
1221 &dev->pci->dev, adapter_nr, mfe_shared);
1222 if (ret)
1223 goto frontend_detach;
1224
1225 /* init CI & MAC */
1226 switch (dev->board) {
1227 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI: {
1228 static struct netup_card_info cinfo;
1229
1230 netup_get_card_info(&dev->i2c_bus[0].i2c_adap, &cinfo);
1231 memcpy(port->frontends.adapter.proposed_mac,
1232 cinfo.port[port->nr - 1].mac, 6);
1233 printk(KERN_INFO "NetUP Dual DVB-S2 CI card port%d MAC=%pM\n",
1234 port->nr, port->frontends.adapter.proposed_mac);
1235
1236 netup_ci_init(port);
1237 break;
1238 }
1239 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF: {
1240 struct altera_ci_config netup_ci_cfg = {
1241 .dev = dev,/* magic number to identify*/
1242 .adapter = &port->frontends.adapter,/* for CI */
1243 .demux = &fe0->dvb.demux,/* for hw pid filter */
1244 .fpga_rw = netup_altera_fpga_rw,
1245 };
1246
1247 altera_ci_init(&netup_ci_cfg, port->nr);
1248 break;
1249 }
1250 case CX23885_BOARD_TEVII_S470: {
1251 u8 eeprom[256]; /* 24C02 i2c eeprom */
1252
1253 if (port->nr != 1)
1254 break;
1255
1256 /* Read entire EEPROM */
1257 dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
1258 tveeprom_read(&dev->i2c_bus[0].i2c_client, eeprom, sizeof(eeprom));
1259 printk(KERN_INFO "TeVii S470 MAC= %pM\n", eeprom + 0xa0);
1260 memcpy(port->frontends.adapter.proposed_mac, eeprom + 0xa0, 6);
1261 break;
1262 }
1263 }
1264
1265 return ret;
1266
1267frontend_detach:
1268 port->gate_ctrl = NULL;
1269 videobuf_dvb_dealloc_frontends(&port->frontends);
1270 return -EINVAL;
1271}
1272
1273int cx23885_dvb_register(struct cx23885_tsport *port)
1274{
1275
1276 struct videobuf_dvb_frontend *fe0;
1277 struct cx23885_dev *dev = port->dev;
1278 int err, i;
1279
1280 /* Here we need to allocate the correct number of frontends,
1281 * as reflected in the cards struct. The reality is that currently
1282 * no cx23885 boards support this - yet. But, if we don't modify this
1283 * code then the second frontend would never be allocated (later)
1284 * and fail with error before the attach in dvb_register().
1285 * Without these changes we risk an OOPS later. The changes here
1286 * are for safety, and should provide a good foundation for the
1287 * future addition of any multi-frontend cx23885 based boards.
1288 */
1289 printk(KERN_INFO "%s() allocating %d frontend(s)\n", __func__,
1290 port->num_frontends);
1291
1292 for (i = 1; i <= port->num_frontends; i++) {
1293 if (videobuf_dvb_alloc_frontend(
1294 &port->frontends, i) == NULL) {
1295 printk(KERN_ERR "%s() failed to alloc\n", __func__);
1296 return -ENOMEM;
1297 }
1298
1299 fe0 = videobuf_dvb_get_frontend(&port->frontends, i);
1300 if (!fe0)
1301 err = -EINVAL;
1302
1303 dprintk(1, "%s\n", __func__);
1304 dprintk(1, " ->probed by Card=%d Name=%s, PCI %02x:%02x\n",
1305 dev->board,
1306 dev->name,
1307 dev->pci_bus,
1308 dev->pci_slot);
1309
1310 err = -ENODEV;
1311
1312 /* dvb stuff */
1313 /* We have to init the queue for each frontend on a port. */
1314 printk(KERN_INFO "%s: cx23885 based dvb card\n", dev->name);
1315 videobuf_queue_sg_init(&fe0->dvb.dvbq, &dvb_qops,
1316 &dev->pci->dev, &port->slock,
1317 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_TOP,
1318 sizeof(struct cx23885_buffer), port, NULL);
1319 }
1320 err = dvb_register(port);
1321 if (err != 0)
1322 printk(KERN_ERR "%s() dvb_register failed err = %d\n",
1323 __func__, err);
1324
1325 return err;
1326}
1327
1328int cx23885_dvb_unregister(struct cx23885_tsport *port)
1329{
1330 struct videobuf_dvb_frontend *fe0;
1331
1332 /* FIXME: in an error condition where the we have
1333 * an expected number of frontends (attach problem)
1334 * then this might not clean up correctly, if 1
1335 * is invalid.
1336 * This comment only applies to future boards IF they
1337 * implement MFE support.
1338 */
1339 fe0 = videobuf_dvb_get_frontend(&port->frontends, 1);
1340 if (fe0 && fe0->dvb.frontend)
1341 videobuf_dvb_unregister_bus(&port->frontends);
1342
1343 switch (port->dev->board) {
1344 case CX23885_BOARD_NETUP_DUAL_DVBS2_CI:
1345 netup_ci_exit(port);
1346 break;
1347 case CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF:
1348 altera_ci_release(port->dev, port->nr);
1349 break;
1350 }
1351
1352 port->gate_ctrl = NULL;
1353
1354 return 0;
1355}
1356
diff --git a/drivers/media/pci/cx23885/cx23885-f300.c b/drivers/media/pci/cx23885/cx23885-f300.c
new file mode 100644
index 000000000000..93998f220986
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-f300.c
@@ -0,0 +1,177 @@
1/*
2 * Driver for Silicon Labs C8051F300 microcontroller.
3 *
4 * It is used for LNB power control in TeVii S470,
5 * TBS 6920 PCIe DVB-S2 cards.
6 *
7 * Microcontroller connected to cx23885 GPIO pins:
8 * GPIO0 - data - P0.3 F300
9 * GPIO1 - reset - P0.2 F300
10 * GPIO2 - clk - P0.1 F300
11 * GPIO3 - busy - P0.0 F300
12 *
13 * Copyright (C) 2009 Igor M. Liplianin <liplianin@me.by>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 *
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31#include "cx23885.h"
32
33#define F300_DATA GPIO_0
34#define F300_RESET GPIO_1
35#define F300_CLK GPIO_2
36#define F300_BUSY GPIO_3
37
38static void f300_set_line(struct cx23885_dev *dev, u32 line, u8 lvl)
39{
40 cx23885_gpio_enable(dev, line, 1);
41 if (lvl == 1)
42 cx23885_gpio_set(dev, line);
43 else
44 cx23885_gpio_clear(dev, line);
45}
46
47static u8 f300_get_line(struct cx23885_dev *dev, u32 line)
48{
49 cx23885_gpio_enable(dev, line, 0);
50
51 return cx23885_gpio_get(dev, line);
52}
53
54static void f300_send_byte(struct cx23885_dev *dev, u8 dta)
55{
56 u8 i;
57
58 for (i = 0; i < 8; i++) {
59 f300_set_line(dev, F300_CLK, 0);
60 udelay(30);
61 f300_set_line(dev, F300_DATA, (dta & 0x80) >> 7);/* msb first */
62 udelay(30);
63 dta <<= 1;
64 f300_set_line(dev, F300_CLK, 1);
65 udelay(30);
66 }
67}
68
69static u8 f300_get_byte(struct cx23885_dev *dev)
70{
71 u8 i, dta = 0;
72
73 for (i = 0; i < 8; i++) {
74 f300_set_line(dev, F300_CLK, 0);
75 udelay(30);
76 dta <<= 1;
77 f300_set_line(dev, F300_CLK, 1);
78 udelay(30);
79 dta |= f300_get_line(dev, F300_DATA);/* msb first */
80
81 }
82
83 return dta;
84}
85
86static u8 f300_xfer(struct dvb_frontend *fe, u8 *buf)
87{
88 struct cx23885_tsport *port = fe->dvb->priv;
89 struct cx23885_dev *dev = port->dev;
90 u8 i, temp, ret = 0;
91
92 temp = buf[0];
93 for (i = 0; i < buf[0]; i++)
94 temp += buf[i + 1];
95 temp = (~temp + 1);/* get check sum */
96 buf[1 + buf[0]] = temp;
97
98 f300_set_line(dev, F300_RESET, 1);
99 f300_set_line(dev, F300_CLK, 1);
100 udelay(30);
101 f300_set_line(dev, F300_DATA, 1);
102 msleep(1);
103
104 /* question: */
105 f300_set_line(dev, F300_RESET, 0);/* begin to send data */
106 msleep(1);
107
108 f300_send_byte(dev, 0xe0);/* the slave address is 0xe0, write */
109 msleep(1);
110
111 temp = buf[0];
112 temp += 2;
113 for (i = 0; i < temp; i++)
114 f300_send_byte(dev, buf[i]);
115
116 f300_set_line(dev, F300_RESET, 1);/* sent data over */
117 f300_set_line(dev, F300_DATA, 1);
118
119 /* answer: */
120 temp = 0;
121 for (i = 0; ((i < 8) & (temp == 0)); i++) {
122 msleep(1);
123 if (f300_get_line(dev, F300_BUSY) == 0)
124 temp = 1;
125 }
126
127 if (i > 7) {
128 printk(KERN_ERR "%s: timeout, the slave no response\n",
129 __func__);
130 ret = 1; /* timeout, the slave no response */
131 } else { /* the slave not busy, prepare for getting data */
132 f300_set_line(dev, F300_RESET, 0);/*ready...*/
133 msleep(1);
134 f300_send_byte(dev, 0xe1);/* 0xe1 is Read */
135 msleep(1);
136 temp = f300_get_byte(dev);/*get the data length */
137 if (temp > 14)
138 temp = 14;
139
140 for (i = 0; i < (temp + 1); i++)
141 f300_get_byte(dev);/* get data to empty buffer */
142
143 f300_set_line(dev, F300_RESET, 1);/* received data over */
144 f300_set_line(dev, F300_DATA, 1);
145 }
146
147 return ret;
148}
149
150int f300_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
151{
152 u8 buf[16];
153
154 buf[0] = 0x05;
155 buf[1] = 0x38;/* write port */
156 buf[2] = 0x01;/* A port, lnb power */
157
158 switch (voltage) {
159 case SEC_VOLTAGE_13:
160 buf[3] = 0x01;/* power on */
161 buf[4] = 0x02;/* B port, H/V */
162 buf[5] = 0x00;/*13V v*/
163 break;
164 case SEC_VOLTAGE_18:
165 buf[3] = 0x01;
166 buf[4] = 0x02;
167 buf[5] = 0x01;/* 18V h*/
168 break;
169 case SEC_VOLTAGE_OFF:
170 buf[3] = 0x00;/* power off */
171 buf[4] = 0x00;
172 buf[5] = 0x00;
173 break;
174 }
175
176 return f300_xfer(fe, buf);
177}
diff --git a/drivers/media/pci/cx23885/cx23885-f300.h b/drivers/media/pci/cx23885/cx23885-f300.h
new file mode 100644
index 000000000000..e73344c94963
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-f300.h
@@ -0,0 +1,2 @@
1extern int f300_set_voltage(struct dvb_frontend *fe,
2 fe_sec_voltage_t voltage);
diff --git a/drivers/media/pci/cx23885/cx23885-i2c.c b/drivers/media/pci/cx23885/cx23885-i2c.c
new file mode 100644
index 000000000000..4887314339cb
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-i2c.c
@@ -0,0 +1,396 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <asm/io.h>
27
28#include "cx23885.h"
29
30#include <media/v4l2-common.h>
31
32static unsigned int i2c_debug;
33module_param(i2c_debug, int, 0644);
34MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
35
36static unsigned int i2c_scan;
37module_param(i2c_scan, int, 0444);
38MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
39
40#define dprintk(level, fmt, arg...)\
41 do { if (i2c_debug >= level)\
42 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
43 } while (0)
44
45#define I2C_WAIT_DELAY 32
46#define I2C_WAIT_RETRY 64
47
48#define I2C_EXTEND (1 << 3)
49#define I2C_NOSTOP (1 << 4)
50
51static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
52{
53 struct cx23885_i2c *bus = i2c_adap->algo_data;
54 struct cx23885_dev *dev = bus->dev;
55 return cx_read(bus->reg_stat) & 0x01;
56}
57
58static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
59{
60 struct cx23885_i2c *bus = i2c_adap->algo_data;
61 struct cx23885_dev *dev = bus->dev;
62 return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
63}
64
65static int i2c_wait_done(struct i2c_adapter *i2c_adap)
66{
67 int count;
68
69 for (count = 0; count < I2C_WAIT_RETRY; count++) {
70 if (!i2c_is_busy(i2c_adap))
71 break;
72 udelay(I2C_WAIT_DELAY);
73 }
74
75 if (I2C_WAIT_RETRY == count)
76 return 0;
77
78 return 1;
79}
80
81static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
82 const struct i2c_msg *msg, int joined_rlen)
83{
84 struct cx23885_i2c *bus = i2c_adap->algo_data;
85 struct cx23885_dev *dev = bus->dev;
86 u32 wdata, addr, ctrl;
87 int retval, cnt;
88
89 if (joined_rlen)
90 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
91 msg->len, joined_rlen);
92 else
93 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
94
95 /* Deal with i2c probe functions with zero payload */
96 if (msg->len == 0) {
97 cx_write(bus->reg_addr, msg->addr << 25);
98 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
99 if (!i2c_wait_done(i2c_adap))
100 return -EIO;
101 if (!i2c_slave_did_ack(i2c_adap))
102 return -ENXIO;
103
104 dprintk(1, "%s() returns 0\n", __func__);
105 return 0;
106 }
107
108
109 /* dev, reg + first byte */
110 addr = (msg->addr << 25) | msg->buf[0];
111 wdata = msg->buf[0];
112 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
113
114 if (msg->len > 1)
115 ctrl |= I2C_NOSTOP | I2C_EXTEND;
116 else if (joined_rlen)
117 ctrl |= I2C_NOSTOP;
118
119 cx_write(bus->reg_addr, addr);
120 cx_write(bus->reg_wdata, wdata);
121 cx_write(bus->reg_ctrl, ctrl);
122
123 if (!i2c_wait_done(i2c_adap))
124 goto eio;
125 if (i2c_debug) {
126 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
127 if (!(ctrl & I2C_NOSTOP))
128 printk(" >\n");
129 }
130
131 for (cnt = 1; cnt < msg->len; cnt++) {
132 /* following bytes */
133 wdata = msg->buf[cnt];
134 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
135
136 if (cnt < msg->len - 1)
137 ctrl |= I2C_NOSTOP | I2C_EXTEND;
138 else if (joined_rlen)
139 ctrl |= I2C_NOSTOP;
140
141 cx_write(bus->reg_addr, addr);
142 cx_write(bus->reg_wdata, wdata);
143 cx_write(bus->reg_ctrl, ctrl);
144
145 if (!i2c_wait_done(i2c_adap))
146 goto eio;
147 if (i2c_debug) {
148 dprintk(1, " %02x", msg->buf[cnt]);
149 if (!(ctrl & I2C_NOSTOP))
150 dprintk(1, " >\n");
151 }
152 }
153 return msg->len;
154
155 eio:
156 retval = -EIO;
157 if (i2c_debug)
158 printk(KERN_ERR " ERR: %d\n", retval);
159 return retval;
160}
161
162static int i2c_readbytes(struct i2c_adapter *i2c_adap,
163 const struct i2c_msg *msg, int joined)
164{
165 struct cx23885_i2c *bus = i2c_adap->algo_data;
166 struct cx23885_dev *dev = bus->dev;
167 u32 ctrl, cnt;
168 int retval;
169
170
171 if (i2c_debug && !joined)
172 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
173
174 /* Deal with i2c probe functions with zero payload */
175 if (msg->len == 0) {
176 cx_write(bus->reg_addr, msg->addr << 25);
177 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
178 if (!i2c_wait_done(i2c_adap))
179 return -EIO;
180 if (!i2c_slave_did_ack(i2c_adap))
181 return -ENXIO;
182
183
184 dprintk(1, "%s() returns 0\n", __func__);
185 return 0;
186 }
187
188 if (i2c_debug) {
189 if (joined)
190 dprintk(1, " R");
191 else
192 dprintk(1, " <R %02x", (msg->addr << 1) + 1);
193 }
194
195 for (cnt = 0; cnt < msg->len; cnt++) {
196
197 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
198
199 if (cnt < msg->len - 1)
200 ctrl |= I2C_NOSTOP | I2C_EXTEND;
201
202 cx_write(bus->reg_addr, msg->addr << 25);
203 cx_write(bus->reg_ctrl, ctrl);
204
205 if (!i2c_wait_done(i2c_adap))
206 goto eio;
207 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
208 if (i2c_debug) {
209 dprintk(1, " %02x", msg->buf[cnt]);
210 if (!(ctrl & I2C_NOSTOP))
211 dprintk(1, " >\n");
212 }
213 }
214 return msg->len;
215
216 eio:
217 retval = -EIO;
218 if (i2c_debug)
219 printk(KERN_ERR " ERR: %d\n", retval);
220 return retval;
221}
222
223static int i2c_xfer(struct i2c_adapter *i2c_adap,
224 struct i2c_msg *msgs, int num)
225{
226 struct cx23885_i2c *bus = i2c_adap->algo_data;
227 struct cx23885_dev *dev = bus->dev;
228 int i, retval = 0;
229
230 dprintk(1, "%s(num = %d)\n", __func__, num);
231
232 for (i = 0 ; i < num; i++) {
233 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
234 __func__, num, msgs[i].addr, msgs[i].len);
235 if (msgs[i].flags & I2C_M_RD) {
236 /* read */
237 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
238 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
239 msgs[i].addr == msgs[i + 1].addr) {
240 /* write then read from same address */
241 retval = i2c_sendbytes(i2c_adap, &msgs[i],
242 msgs[i + 1].len);
243 if (retval < 0)
244 goto err;
245 i++;
246 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
247 } else {
248 /* write */
249 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
250 }
251 if (retval < 0)
252 goto err;
253 }
254 return num;
255
256 err:
257 return retval;
258}
259
260static u32 cx23885_functionality(struct i2c_adapter *adap)
261{
262 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
263}
264
265static struct i2c_algorithm cx23885_i2c_algo_template = {
266 .master_xfer = i2c_xfer,
267 .functionality = cx23885_functionality,
268};
269
270/* ----------------------------------------------------------------------- */
271
272static struct i2c_adapter cx23885_i2c_adap_template = {
273 .name = "cx23885",
274 .owner = THIS_MODULE,
275 .algo = &cx23885_i2c_algo_template,
276};
277
278static struct i2c_client cx23885_i2c_client_template = {
279 .name = "cx23885 internal",
280};
281
282static char *i2c_devs[128] = {
283 [0x10 >> 1] = "tda10048",
284 [0x12 >> 1] = "dib7000pc",
285 [0x1c >> 1] = "lgdt3303",
286 [0x86 >> 1] = "tda9887",
287 [0x32 >> 1] = "cx24227",
288 [0x88 >> 1] = "cx25837",
289 [0x84 >> 1] = "tda8295",
290 [0x98 >> 1] = "flatiron",
291 [0xa0 >> 1] = "eeprom",
292 [0xc0 >> 1] = "tuner/mt2131/tda8275",
293 [0xc2 >> 1] = "tuner/mt2131/tda8275/xc5000/xc3028",
294 [0xc8 >> 1] = "tuner/xc3028L",
295};
296
297static void do_i2c_scan(char *name, struct i2c_client *c)
298{
299 unsigned char buf;
300 int i, rc;
301
302 for (i = 0; i < 128; i++) {
303 c->addr = i;
304 rc = i2c_master_recv(c, &buf, 0);
305 if (rc < 0)
306 continue;
307 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
308 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
309 }
310}
311
312/* init + register i2c adapter */
313int cx23885_i2c_register(struct cx23885_i2c *bus)
314{
315 struct cx23885_dev *dev = bus->dev;
316
317 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
318
319 bus->i2c_adap = cx23885_i2c_adap_template;
320 bus->i2c_client = cx23885_i2c_client_template;
321 bus->i2c_adap.dev.parent = &dev->pci->dev;
322
323 strlcpy(bus->i2c_adap.name, bus->dev->name,
324 sizeof(bus->i2c_adap.name));
325
326 bus->i2c_adap.algo_data = bus;
327 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
328 i2c_add_adapter(&bus->i2c_adap);
329
330 bus->i2c_client.adapter = &bus->i2c_adap;
331
332 if (0 == bus->i2c_rc) {
333 dprintk(1, "%s: i2c bus %d registered\n", dev->name, bus->nr);
334 if (i2c_scan) {
335 printk(KERN_INFO "%s: scan bus %d:\n",
336 dev->name, bus->nr);
337 do_i2c_scan(dev->name, &bus->i2c_client);
338 }
339 } else
340 printk(KERN_WARNING "%s: i2c bus %d register FAILED\n",
341 dev->name, bus->nr);
342
343 /* Instantiate the IR receiver device, if present */
344 if (0 == bus->i2c_rc) {
345 struct i2c_board_info info;
346 const unsigned short addr_list[] = {
347 0x6b, I2C_CLIENT_END
348 };
349
350 memset(&info, 0, sizeof(struct i2c_board_info));
351 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
352 /* Use quick read command for probe, some IR chips don't
353 * support writes */
354 i2c_new_probed_device(&bus->i2c_adap, &info, addr_list,
355 i2c_probe_func_quick_read);
356 }
357
358 return bus->i2c_rc;
359}
360
361int cx23885_i2c_unregister(struct cx23885_i2c *bus)
362{
363 i2c_del_adapter(&bus->i2c_adap);
364 return 0;
365}
366
367void cx23885_av_clk(struct cx23885_dev *dev, int enable)
368{
369 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
370 char buffer[3];
371 struct i2c_msg msg;
372 dprintk(1, "%s(enabled = %d)\n", __func__, enable);
373
374 /* Register 0x144 */
375 buffer[0] = 0x01;
376 buffer[1] = 0x44;
377 if (enable == 1)
378 buffer[2] = 0x05;
379 else
380 buffer[2] = 0x00;
381
382 msg.addr = 0x44;
383 msg.flags = I2C_M_TEN;
384 msg.len = 3;
385 msg.buf = buffer;
386
387 i2c_xfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
388}
389
390/* ----------------------------------------------------------------------- */
391
392/*
393 * Local variables:
394 * c-basic-offset: 8
395 * End:
396 */
diff --git a/drivers/media/pci/cx23885/cx23885-input.c b/drivers/media/pci/cx23885/cx23885-input.c
new file mode 100644
index 000000000000..56066721edc1
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-input.c
@@ -0,0 +1,365 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * Infrared remote control input device
5 *
6 * Most of this file is
7 *
8 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
9 *
10 * However, the cx23885_input_{init,fini} functions contained herein are
11 * derived from Linux kernel files linux/media/video/.../...-input.c marked as:
12 *
13 * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
14 * Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
15 * Markus Rechberger <mrechberger@gmail.com>
16 * Mauro Carvalho Chehab <mchehab@infradead.org>
17 * Sascha Sommer <saschasommer@freenet.de>
18 * Copyright (C) 2004, 2005 Chris Pascoe
19 * Copyright (C) 2003, 2004 Gerd Knorr
20 * Copyright (C) 2003 Pavel Machek
21 *
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version 2
25 * of the License, or (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 * 02110-1301, USA.
36 */
37
38#include <linux/slab.h>
39#include <media/rc-core.h>
40#include <media/v4l2-subdev.h>
41
42#include "cx23885.h"
43
44#define MODULE_NAME "cx23885"
45
46static void cx23885_input_process_measurements(struct cx23885_dev *dev,
47 bool overrun)
48{
49 struct cx23885_kernel_ir *kernel_ir = dev->kernel_ir;
50
51 ssize_t num;
52 int count, i;
53 bool handle = false;
54 struct ir_raw_event ir_core_event[64];
55
56 do {
57 num = 0;
58 v4l2_subdev_call(dev->sd_ir, ir, rx_read, (u8 *) ir_core_event,
59 sizeof(ir_core_event), &num);
60
61 count = num / sizeof(struct ir_raw_event);
62
63 for (i = 0; i < count; i++) {
64 ir_raw_event_store(kernel_ir->rc,
65 &ir_core_event[i]);
66 handle = true;
67 }
68 } while (num != 0);
69
70 if (overrun)
71 ir_raw_event_reset(kernel_ir->rc);
72 else if (handle)
73 ir_raw_event_handle(kernel_ir->rc);
74}
75
76void cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events)
77{
78 struct v4l2_subdev_ir_parameters params;
79 int overrun, data_available;
80
81 if (dev->sd_ir == NULL || events == 0)
82 return;
83
84 switch (dev->board) {
85 case CX23885_BOARD_HAUPPAUGE_HVR1270:
86 case CX23885_BOARD_HAUPPAUGE_HVR1850:
87 case CX23885_BOARD_HAUPPAUGE_HVR1290:
88 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
89 case CX23885_BOARD_TEVII_S470:
90 case CX23885_BOARD_HAUPPAUGE_HVR1250:
91 /*
92 * The only boards we handle right now. However other boards
93 * using the CX2388x integrated IR controller should be similar
94 */
95 break;
96 default:
97 return;
98 }
99
100 overrun = events & (V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN |
101 V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN);
102
103 data_available = events & (V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED |
104 V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ);
105
106 if (overrun) {
107 /* If there was a FIFO overrun, stop the device */
108 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
109 params.enable = false;
110 /* Mitigate race with cx23885_input_ir_stop() */
111 params.shutdown = atomic_read(&dev->ir_input_stopping);
112 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
113 }
114
115 if (data_available)
116 cx23885_input_process_measurements(dev, overrun);
117
118 if (overrun) {
119 /* If there was a FIFO overrun, clear & restart the device */
120 params.enable = true;
121 /* Mitigate race with cx23885_input_ir_stop() */
122 params.shutdown = atomic_read(&dev->ir_input_stopping);
123 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
124 }
125}
126
127static int cx23885_input_ir_start(struct cx23885_dev *dev)
128{
129 struct v4l2_subdev_ir_parameters params;
130
131 if (dev->sd_ir == NULL)
132 return -ENODEV;
133
134 atomic_set(&dev->ir_input_stopping, 0);
135
136 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
137 switch (dev->board) {
138 case CX23885_BOARD_HAUPPAUGE_HVR1270:
139 case CX23885_BOARD_HAUPPAUGE_HVR1850:
140 case CX23885_BOARD_HAUPPAUGE_HVR1290:
141 case CX23885_BOARD_HAUPPAUGE_HVR1250:
142 /*
143 * The IR controller on this board only returns pulse widths.
144 * Any other mode setting will fail to set up the device.
145 */
146 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
147 params.enable = true;
148 params.interrupt_enable = true;
149 params.shutdown = false;
150
151 /* Setup for baseband compatible with both RC-5 and RC-6A */
152 params.modulation = false;
153 /* RC-5: 2,222,222 ns = 1/36 kHz * 32 cycles * 2 marks * 1.25*/
154 /* RC-6A: 3,333,333 ns = 1/36 kHz * 16 cycles * 6 marks * 1.25*/
155 params.max_pulse_width = 3333333; /* ns */
156 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
157 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
158 params.noise_filter_min_width = 333333; /* ns */
159 /*
160 * This board has inverted receive sense:
161 * mark is received as low logic level;
162 * falling edges are detected as rising edges; etc.
163 */
164 params.invert_level = true;
165 break;
166 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
167 case CX23885_BOARD_TEVII_S470:
168 /*
169 * The IR controller on this board only returns pulse widths.
170 * Any other mode setting will fail to set up the device.
171 */
172 params.mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
173 params.enable = true;
174 params.interrupt_enable = true;
175 params.shutdown = false;
176
177 /* Setup for a standard NEC protocol */
178 params.carrier_freq = 37917; /* Hz, 455 kHz/12 for NEC */
179 params.carrier_range_lower = 33000; /* Hz */
180 params.carrier_range_upper = 43000; /* Hz */
181 params.duty_cycle = 33; /* percent, 33 percent for NEC */
182
183 /*
184 * NEC max pulse width: (64/3)/(455 kHz/12) * 16 nec_units
185 * (64/3)/(455 kHz/12) * 16 nec_units * 1.375 = 12378022 ns
186 */
187 params.max_pulse_width = 12378022; /* ns */
188
189 /*
190 * NEC noise filter min width: (64/3)/(455 kHz/12) * 1 nec_unit
191 * (64/3)/(455 kHz/12) * 1 nec_units * 0.625 = 351648 ns
192 */
193 params.noise_filter_min_width = 351648; /* ns */
194
195 params.modulation = false;
196 params.invert_level = true;
197 break;
198 }
199 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
200 return 0;
201}
202
203static int cx23885_input_ir_open(struct rc_dev *rc)
204{
205 struct cx23885_kernel_ir *kernel_ir = rc->priv;
206
207 if (kernel_ir->cx == NULL)
208 return -ENODEV;
209
210 return cx23885_input_ir_start(kernel_ir->cx);
211}
212
213static void cx23885_input_ir_stop(struct cx23885_dev *dev)
214{
215 struct v4l2_subdev_ir_parameters params;
216
217 if (dev->sd_ir == NULL)
218 return;
219
220 /*
221 * Stop the sd_ir subdevice from generating notifications and
222 * scheduling work.
223 * It is shutdown this way in order to mitigate a race with
224 * cx23885_input_rx_work_handler() in the overrun case, which could
225 * re-enable the subdevice.
226 */
227 atomic_set(&dev->ir_input_stopping, 1);
228 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
229 while (params.shutdown == false) {
230 params.enable = false;
231 params.interrupt_enable = false;
232 params.shutdown = true;
233 v4l2_subdev_call(dev->sd_ir, ir, rx_s_parameters, &params);
234 v4l2_subdev_call(dev->sd_ir, ir, rx_g_parameters, &params);
235 }
236 flush_work_sync(&dev->cx25840_work);
237 flush_work_sync(&dev->ir_rx_work);
238 flush_work_sync(&dev->ir_tx_work);
239}
240
241static void cx23885_input_ir_close(struct rc_dev *rc)
242{
243 struct cx23885_kernel_ir *kernel_ir = rc->priv;
244
245 if (kernel_ir->cx != NULL)
246 cx23885_input_ir_stop(kernel_ir->cx);
247}
248
249int cx23885_input_init(struct cx23885_dev *dev)
250{
251 struct cx23885_kernel_ir *kernel_ir;
252 struct rc_dev *rc;
253 char *rc_map;
254 enum rc_driver_type driver_type;
255 unsigned long allowed_protos;
256
257 int ret;
258
259 /*
260 * If the IR device (hardware registers, chip, GPIO lines, etc.) isn't
261 * encapsulated in a v4l2_subdev, then I'm not going to deal with it.
262 */
263 if (dev->sd_ir == NULL)
264 return -ENODEV;
265
266 switch (dev->board) {
267 case CX23885_BOARD_HAUPPAUGE_HVR1270:
268 case CX23885_BOARD_HAUPPAUGE_HVR1850:
269 case CX23885_BOARD_HAUPPAUGE_HVR1290:
270 case CX23885_BOARD_HAUPPAUGE_HVR1250:
271 /* Integrated CX2388[58] IR controller */
272 driver_type = RC_DRIVER_IR_RAW;
273 allowed_protos = RC_TYPE_ALL;
274 /* The grey Hauppauge RC-5 remote */
275 rc_map = RC_MAP_HAUPPAUGE;
276 break;
277 case CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL:
278 /* Integrated CX23885 IR controller */
279 driver_type = RC_DRIVER_IR_RAW;
280 allowed_protos = RC_TYPE_NEC;
281 /* The grey Terratec remote with orange buttons */
282 rc_map = RC_MAP_NEC_TERRATEC_CINERGY_XS;
283 break;
284 case CX23885_BOARD_TEVII_S470:
285 /* Integrated CX23885 IR controller */
286 driver_type = RC_DRIVER_IR_RAW;
287 allowed_protos = RC_TYPE_ALL;
288 /* A guess at the remote */
289 rc_map = RC_MAP_TEVII_NEC;
290 break;
291 default:
292 return -ENODEV;
293 }
294
295 /* cx23885 board instance kernel IR state */
296 kernel_ir = kzalloc(sizeof(struct cx23885_kernel_ir), GFP_KERNEL);
297 if (kernel_ir == NULL)
298 return -ENOMEM;
299
300 kernel_ir->cx = dev;
301 kernel_ir->name = kasprintf(GFP_KERNEL, "cx23885 IR (%s)",
302 cx23885_boards[dev->board].name);
303 kernel_ir->phys = kasprintf(GFP_KERNEL, "pci-%s/ir0",
304 pci_name(dev->pci));
305
306 /* input device */
307 rc = rc_allocate_device();
308 if (!rc) {
309 ret = -ENOMEM;
310 goto err_out_free;
311 }
312
313 kernel_ir->rc = rc;
314 rc->input_name = kernel_ir->name;
315 rc->input_phys = kernel_ir->phys;
316 rc->input_id.bustype = BUS_PCI;
317 rc->input_id.version = 1;
318 if (dev->pci->subsystem_vendor) {
319 rc->input_id.vendor = dev->pci->subsystem_vendor;
320 rc->input_id.product = dev->pci->subsystem_device;
321 } else {
322 rc->input_id.vendor = dev->pci->vendor;
323 rc->input_id.product = dev->pci->device;
324 }
325 rc->dev.parent = &dev->pci->dev;
326 rc->driver_type = driver_type;
327 rc->allowed_protos = allowed_protos;
328 rc->priv = kernel_ir;
329 rc->open = cx23885_input_ir_open;
330 rc->close = cx23885_input_ir_close;
331 rc->map_name = rc_map;
332 rc->driver_name = MODULE_NAME;
333
334 /* Go */
335 dev->kernel_ir = kernel_ir;
336 ret = rc_register_device(rc);
337 if (ret)
338 goto err_out_stop;
339
340 return 0;
341
342err_out_stop:
343 cx23885_input_ir_stop(dev);
344 dev->kernel_ir = NULL;
345 rc_free_device(rc);
346err_out_free:
347 kfree(kernel_ir->phys);
348 kfree(kernel_ir->name);
349 kfree(kernel_ir);
350 return ret;
351}
352
353void cx23885_input_fini(struct cx23885_dev *dev)
354{
355 /* Always stop the IR hardware from generating interrupts */
356 cx23885_input_ir_stop(dev);
357
358 if (dev->kernel_ir == NULL)
359 return;
360 rc_unregister_device(dev->kernel_ir->rc);
361 kfree(dev->kernel_ir->phys);
362 kfree(dev->kernel_ir->name);
363 kfree(dev->kernel_ir);
364 dev->kernel_ir = NULL;
365}
diff --git a/drivers/media/pci/cx23885/cx23885-input.h b/drivers/media/pci/cx23885/cx23885-input.h
new file mode 100644
index 000000000000..75ef15d3f523
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-input.h
@@ -0,0 +1,30 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * Infrared remote control input device
5 *
6 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#ifndef _CX23885_INPUT_H_
25#define _CX23885_INPUT_H_
26int cx23885_input_rx_work_handler(struct cx23885_dev *dev, u32 events);
27
28int cx23885_input_init(struct cx23885_dev *dev);
29void cx23885_input_fini(struct cx23885_dev *dev);
30#endif
diff --git a/drivers/media/pci/cx23885/cx23885-ioctl.c b/drivers/media/pci/cx23885/cx23885-ioctl.c
new file mode 100644
index 000000000000..44812ca78899
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-ioctl.c
@@ -0,0 +1,208 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * Various common ioctl() support functions
5 *
6 * Copyright (c) 2009 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include "cx23885.h"
25#include <media/v4l2-chip-ident.h>
26
27int cx23885_g_chip_ident(struct file *file, void *fh,
28 struct v4l2_dbg_chip_ident *chip)
29{
30 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
31 int err = 0;
32 u8 rev;
33
34 chip->ident = V4L2_IDENT_NONE;
35 chip->revision = 0;
36 switch (chip->match.type) {
37 case V4L2_CHIP_MATCH_HOST:
38 switch (chip->match.addr) {
39 case 0:
40 rev = cx_read(RDR_CFG2) & 0xff;
41 switch (dev->pci->device) {
42 case 0x8852:
43 /* rev 0x04 could be '885 or '888. Pick '888. */
44 if (rev == 0x04)
45 chip->ident = V4L2_IDENT_CX23888;
46 else
47 chip->ident = V4L2_IDENT_CX23885;
48 break;
49 case 0x8880:
50 if (rev == 0x0e || rev == 0x0f)
51 chip->ident = V4L2_IDENT_CX23887;
52 else
53 chip->ident = V4L2_IDENT_CX23888;
54 break;
55 default:
56 chip->ident = V4L2_IDENT_UNKNOWN;
57 break;
58 }
59 chip->revision = (dev->pci->device << 16) | (rev << 8) |
60 (dev->hwrevision & 0xff);
61 break;
62 case 1:
63 if (dev->v4l_device != NULL) {
64 chip->ident = V4L2_IDENT_CX23417;
65 chip->revision = 0;
66 }
67 break;
68 case 2:
69 /*
70 * The integrated IR controller on the CX23888 is
71 * host chip 2. It may not be used/initialized or sd_ir
72 * may be pointing at the cx25840 subdevice for the
73 * IR controller on the CX23885. Thus we find it
74 * without using the dev->sd_ir pointer.
75 */
76 call_hw(dev, CX23885_HW_888_IR, core, g_chip_ident,
77 chip);
78 break;
79 default:
80 err = -EINVAL; /* per V4L2 spec */
81 break;
82 }
83 break;
84 case V4L2_CHIP_MATCH_I2C_DRIVER:
85 /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
86 call_all(dev, core, g_chip_ident, chip);
87 break;
88 case V4L2_CHIP_MATCH_I2C_ADDR:
89 /*
90 * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
91 * to look if a chip is at the address with no driver. That's a
92 * dangerous thing to do with EEPROMs anyway.
93 */
94 call_all(dev, core, g_chip_ident, chip);
95 break;
96 default:
97 err = -EINVAL;
98 break;
99 }
100 return err;
101}
102
103#ifdef CONFIG_VIDEO_ADV_DEBUG
104static int cx23885_g_host_register(struct cx23885_dev *dev,
105 struct v4l2_dbg_register *reg)
106{
107 if ((reg->reg & 0x3) != 0 || reg->reg >= pci_resource_len(dev->pci, 0))
108 return -EINVAL;
109
110 reg->size = 4;
111 reg->val = cx_read(reg->reg);
112 return 0;
113}
114
115static int cx23417_g_register(struct cx23885_dev *dev,
116 struct v4l2_dbg_register *reg)
117{
118 u32 value;
119
120 if (dev->v4l_device == NULL)
121 return -EINVAL;
122
123 if ((reg->reg & 0x3) != 0 || reg->reg >= 0x10000)
124 return -EINVAL;
125
126 if (mc417_register_read(dev, (u16) reg->reg, &value))
127 return -EINVAL; /* V4L2 spec, but -EREMOTEIO really */
128
129 reg->size = 4;
130 reg->val = value;
131 return 0;
132}
133
134int cx23885_g_register(struct file *file, void *fh,
135 struct v4l2_dbg_register *reg)
136{
137 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
138
139 if (!capable(CAP_SYS_ADMIN))
140 return -EPERM;
141
142 if (reg->match.type == V4L2_CHIP_MATCH_HOST) {
143 switch (reg->match.addr) {
144 case 0:
145 return cx23885_g_host_register(dev, reg);
146 case 1:
147 return cx23417_g_register(dev, reg);
148 default:
149 break;
150 }
151 }
152
153 /* FIXME - any error returns should not be ignored */
154 call_all(dev, core, g_register, reg);
155 return 0;
156}
157
158static int cx23885_s_host_register(struct cx23885_dev *dev,
159 struct v4l2_dbg_register *reg)
160{
161 if ((reg->reg & 0x3) != 0 || reg->reg >= pci_resource_len(dev->pci, 0))
162 return -EINVAL;
163
164 reg->size = 4;
165 cx_write(reg->reg, reg->val);
166 return 0;
167}
168
169static int cx23417_s_register(struct cx23885_dev *dev,
170 struct v4l2_dbg_register *reg)
171{
172 if (dev->v4l_device == NULL)
173 return -EINVAL;
174
175 if ((reg->reg & 0x3) != 0 || reg->reg >= 0x10000)
176 return -EINVAL;
177
178 if (mc417_register_write(dev, (u16) reg->reg, (u32) reg->val))
179 return -EINVAL; /* V4L2 spec, but -EREMOTEIO really */
180
181 reg->size = 4;
182 return 0;
183}
184
185int cx23885_s_register(struct file *file, void *fh,
186 struct v4l2_dbg_register *reg)
187{
188 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
189
190 if (!capable(CAP_SYS_ADMIN))
191 return -EPERM;
192
193 if (reg->match.type == V4L2_CHIP_MATCH_HOST) {
194 switch (reg->match.addr) {
195 case 0:
196 return cx23885_s_host_register(dev, reg);
197 case 1:
198 return cx23417_s_register(dev, reg);
199 default:
200 break;
201 }
202 }
203
204 /* FIXME - any error returns should not be ignored */
205 call_all(dev, core, s_register, reg);
206 return 0;
207}
208#endif
diff --git a/drivers/media/pci/cx23885/cx23885-ioctl.h b/drivers/media/pci/cx23885/cx23885-ioctl.h
new file mode 100644
index 000000000000..315be0ca5a04
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-ioctl.h
@@ -0,0 +1,39 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * Various common ioctl() support functions
5 *
6 * Copyright (c) 2009 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#ifndef _CX23885_IOCTL_H_
25#define _CX23885_IOCTL_H_
26
27int cx23885_g_chip_ident(struct file *file, void *fh,
28 struct v4l2_dbg_chip_ident *chip);
29
30#ifdef CONFIG_VIDEO_ADV_DEBUG
31int cx23885_g_register(struct file *file, void *fh,
32 struct v4l2_dbg_register *reg);
33
34
35int cx23885_s_register(struct file *file, void *fh,
36 struct v4l2_dbg_register *reg);
37
38#endif
39#endif
diff --git a/drivers/media/pci/cx23885/cx23885-ir.c b/drivers/media/pci/cx23885/cx23885-ir.c
new file mode 100644
index 000000000000..7125247dd255
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-ir.c
@@ -0,0 +1,117 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * Infrared device support routines - non-input, non-vl42_subdev routines
5 *
6 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#include <media/v4l2-device.h>
25
26#include "cx23885.h"
27#include "cx23885-input.h"
28
29#define CX23885_IR_RX_FIFO_SERVICE_REQ 0
30#define CX23885_IR_RX_END_OF_RX_DETECTED 1
31#define CX23885_IR_RX_HW_FIFO_OVERRUN 2
32#define CX23885_IR_RX_SW_FIFO_OVERRUN 3
33
34#define CX23885_IR_TX_FIFO_SERVICE_REQ 0
35
36
37void cx23885_ir_rx_work_handler(struct work_struct *work)
38{
39 struct cx23885_dev *dev =
40 container_of(work, struct cx23885_dev, ir_rx_work);
41 u32 events = 0;
42 unsigned long *notifications = &dev->ir_rx_notifications;
43
44 if (test_and_clear_bit(CX23885_IR_RX_SW_FIFO_OVERRUN, notifications))
45 events |= V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN;
46 if (test_and_clear_bit(CX23885_IR_RX_HW_FIFO_OVERRUN, notifications))
47 events |= V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN;
48 if (test_and_clear_bit(CX23885_IR_RX_END_OF_RX_DETECTED, notifications))
49 events |= V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED;
50 if (test_and_clear_bit(CX23885_IR_RX_FIFO_SERVICE_REQ, notifications))
51 events |= V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ;
52
53 if (events == 0)
54 return;
55
56 if (dev->kernel_ir)
57 cx23885_input_rx_work_handler(dev, events);
58}
59
60void cx23885_ir_tx_work_handler(struct work_struct *work)
61{
62 struct cx23885_dev *dev =
63 container_of(work, struct cx23885_dev, ir_tx_work);
64 u32 events = 0;
65 unsigned long *notifications = &dev->ir_tx_notifications;
66
67 if (test_and_clear_bit(CX23885_IR_TX_FIFO_SERVICE_REQ, notifications))
68 events |= V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ;
69
70 if (events == 0)
71 return;
72
73}
74
75/* Possibly called in an IRQ context */
76void cx23885_ir_rx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events)
77{
78 struct cx23885_dev *dev = to_cx23885(sd->v4l2_dev);
79 unsigned long *notifications = &dev->ir_rx_notifications;
80
81 if (events & V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ)
82 set_bit(CX23885_IR_RX_FIFO_SERVICE_REQ, notifications);
83 if (events & V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED)
84 set_bit(CX23885_IR_RX_END_OF_RX_DETECTED, notifications);
85 if (events & V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN)
86 set_bit(CX23885_IR_RX_HW_FIFO_OVERRUN, notifications);
87 if (events & V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN)
88 set_bit(CX23885_IR_RX_SW_FIFO_OVERRUN, notifications);
89
90 /*
91 * For the integrated AV core, we are already in a workqueue context.
92 * For the CX23888 integrated IR, we are in an interrupt context.
93 */
94 if (sd == dev->sd_cx25840)
95 cx23885_ir_rx_work_handler(&dev->ir_rx_work);
96 else
97 schedule_work(&dev->ir_rx_work);
98}
99
100/* Possibly called in an IRQ context */
101void cx23885_ir_tx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events)
102{
103 struct cx23885_dev *dev = to_cx23885(sd->v4l2_dev);
104 unsigned long *notifications = &dev->ir_tx_notifications;
105
106 if (events & V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ)
107 set_bit(CX23885_IR_TX_FIFO_SERVICE_REQ, notifications);
108
109 /*
110 * For the integrated AV core, we are already in a workqueue context.
111 * For the CX23888 integrated IR, we are in an interrupt context.
112 */
113 if (sd == dev->sd_cx25840)
114 cx23885_ir_tx_work_handler(&dev->ir_tx_work);
115 else
116 schedule_work(&dev->ir_tx_work);
117}
diff --git a/drivers/media/pci/cx23885/cx23885-ir.h b/drivers/media/pci/cx23885/cx23885-ir.h
new file mode 100644
index 000000000000..0c9d8bda9e28
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-ir.h
@@ -0,0 +1,31 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * Infrared device support routines - non-input, non-vl42_subdev routines
5 *
6 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#ifndef _CX23885_IR_H_
25#define _CX23885_IR_H_
26void cx23885_ir_rx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events);
27void cx23885_ir_tx_v4l2_dev_notify(struct v4l2_subdev *sd, u32 events);
28
29void cx23885_ir_rx_work_handler(struct work_struct *work);
30void cx23885_ir_tx_work_handler(struct work_struct *work);
31#endif
diff --git a/drivers/media/pci/cx23885/cx23885-reg.h b/drivers/media/pci/cx23885/cx23885-reg.h
new file mode 100644
index 000000000000..a99936e0cbc2
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-reg.h
@@ -0,0 +1,452 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#ifndef _CX23885_REG_H_
23#define _CX23885_REG_H_
24
25/*
26Address Map
270x00000000 -> 0x00009000 TX SRAM (Fifos)
280x00010000 -> 0x00013c00 RX SRAM CMDS + CDT
29
30EACH CMDS struct is 0x80 bytes long
31
32DMAx_PTR1 = 0x03040 address of first cluster
33DMAx_PTR2 = 0x10600 address of the CDT
34DMAx_CNT1 = cluster size in (bytes >> 4) -1
35DMAx_CNT2 = total cdt size for all entries >> 3
36
37Cluster Descriptor entry = 4 DWORDS
38 DWORD 0 -> ptr to cluster
39 DWORD 1 Reserved
40 DWORD 2 Reserved
41 DWORD 3 Reserved
42
43Channel manager Data Structure entry = 20 DWORD
44 0 IntialProgramCounterLow
45 1 IntialProgramCounterHigh
46 2 ClusterDescriptorTableBase
47 3 ClusterDescriptorTableSize
48 4 InstructionQueueBase
49 5 InstructionQueueSize
50... Reserved
51 19 Reserved
52*/
53
54/* Risc Instructions */
55#define RISC_CNT_INC 0x00010000
56#define RISC_CNT_RESET 0x00030000
57#define RISC_IRQ1 0x01000000
58#define RISC_IRQ2 0x02000000
59#define RISC_EOL 0x04000000
60#define RISC_SOL 0x08000000
61#define RISC_WRITE 0x10000000
62#define RISC_SKIP 0x20000000
63#define RISC_JUMP 0x70000000
64#define RISC_SYNC 0x80000000
65#define RISC_RESYNC 0x80008000
66#define RISC_READ 0x90000000
67#define RISC_WRITERM 0xB0000000
68#define RISC_WRITECM 0xC0000000
69#define RISC_WRITECR 0xD0000000
70#define RISC_WRITEC 0x50000000
71#define RISC_READC 0xA0000000
72
73
74/* Audio and Video Core */
75#define HOST_REG1 0x00000000
76#define HOST_REG2 0x00000001
77#define HOST_REG3 0x00000002
78
79/* Chip Configuration Registers */
80#define CHIP_CTRL 0x00000100
81#define AFE_CTRL 0x00000104
82#define VID_PLL_INT_POST 0x00000108
83#define VID_PLL_FRAC 0x0000010C
84#define AUX_PLL_INT_POST 0x00000110
85#define AUX_PLL_FRAC 0x00000114
86#define SYS_PLL_INT_POST 0x00000118
87#define SYS_PLL_FRAC 0x0000011C
88#define PIN_CTRL 0x00000120
89#define AUD_IO_CTRL 0x00000124
90#define AUD_LOCK1 0x00000128
91#define AUD_LOCK2 0x0000012C
92#define POWER_CTRL 0x00000130
93#define AFE_DIAG_CTRL1 0x00000134
94#define AFE_DIAG_CTRL3 0x0000013C
95#define PLL_DIAG_CTRL 0x00000140
96#define AFE_CLK_OUT_CTRL 0x00000144
97#define DLL1_DIAG_CTRL 0x0000015C
98
99/* GPIO[23:19] Output Enable */
100#define GPIO2_OUT_EN_REG 0x00000160
101/* GPIO[23:19] Data Registers */
102#define GPIO2 0x00000164
103
104#define IFADC_CTRL 0x00000180
105
106/* Infrared Remote Registers */
107#define IR_CNTRL_REG 0x00000200
108#define IR_TXCLK_REG 0x00000204
109#define IR_RXCLK_REG 0x00000208
110#define IR_CDUTY_REG 0x0000020C
111#define IR_STAT_REG 0x00000210
112#define IR_IRQEN_REG 0x00000214
113#define IR_FILTR_REG 0x00000218
114#define IR_FIFO_REG 0x0000023C
115
116/* Video Decoder Registers */
117#define MODE_CTRL 0x00000400
118#define OUT_CTRL1 0x00000404
119#define OUT_CTRL2 0x00000408
120#define GEN_STAT 0x0000040C
121#define INT_STAT_MASK 0x00000410
122#define LUMA_CTRL 0x00000414
123#define HSCALE_CTRL 0x00000418
124#define VSCALE_CTRL 0x0000041C
125#define CHROMA_CTRL 0x00000420
126#define VBI_LINE_CTRL1 0x00000424
127#define VBI_LINE_CTRL2 0x00000428
128#define VBI_LINE_CTRL3 0x0000042C
129#define VBI_LINE_CTRL4 0x00000430
130#define VBI_LINE_CTRL5 0x00000434
131#define VBI_FC_CFG 0x00000438
132#define VBI_MISC_CFG1 0x0000043C
133#define VBI_MISC_CFG2 0x00000440
134#define VBI_PAY1 0x00000444
135#define VBI_PAY2 0x00000448
136#define VBI_CUST1_CFG1 0x0000044C
137#define VBI_CUST1_CFG2 0x00000450
138#define VBI_CUST1_CFG3 0x00000454
139#define VBI_CUST2_CFG1 0x00000458
140#define VBI_CUST2_CFG2 0x0000045C
141#define VBI_CUST2_CFG3 0x00000460
142#define VBI_CUST3_CFG1 0x00000464
143#define VBI_CUST3_CFG2 0x00000468
144#define VBI_CUST3_CFG3 0x0000046C
145#define HORIZ_TIM_CTRL 0x00000470
146#define VERT_TIM_CTRL 0x00000474
147#define SRC_COMB_CFG 0x00000478
148#define CHROMA_VBIOFF_CFG 0x0000047C
149#define FIELD_COUNT 0x00000480
150#define MISC_TIM_CTRL 0x00000484
151#define DFE_CTRL1 0x00000488
152#define DFE_CTRL2 0x0000048C
153#define DFE_CTRL3 0x00000490
154#define PLL_CTRL 0x00000494
155#define HTL_CTRL 0x00000498
156#define COMB_CTRL 0x0000049C
157#define CRUSH_CTRL 0x000004A0
158#define SOFT_RST_CTRL 0x000004A4
159#define CX885_VERSION 0x000004B4
160#define VBI_PASS_CTRL 0x000004BC
161
162/* Audio Decoder Registers */
163/* 8051 Configuration */
164#define DL_CTL 0x00000800
165#define STD_DET_STATUS 0x00000804
166#define STD_DET_CTL 0x00000808
167#define DW8051_INT 0x0000080C
168#define GENERAL_CTL 0x00000810
169#define AAGC_CTL 0x00000814
170#define DEMATRIX_CTL 0x000008CC
171#define PATH1_CTL1 0x000008D0
172#define PATH1_VOL_CTL 0x000008D4
173#define PATH1_EQ_CTL 0x000008D8
174#define PATH1_SC_CTL 0x000008DC
175#define PATH2_CTL1 0x000008E0
176#define PATH2_VOL_CTL 0x000008E4
177#define PATH2_EQ_CTL 0x000008E8
178#define PATH2_SC_CTL 0x000008EC
179
180/* Sample Rate Converter */
181#define SRC_CTL 0x000008F0
182#define SRC_LF_COEF 0x000008F4
183#define SRC1_CTL 0x000008F8
184#define SRC2_CTL 0x000008FC
185#define SRC3_CTL 0x00000900
186#define SRC4_CTL 0x00000904
187#define SRC5_CTL 0x00000908
188#define SRC6_CTL 0x0000090C
189#define BAND_OUT_SEL 0x00000910
190#define I2S_N_CTL 0x00000914
191#define I2S_OUT_CTL 0x00000918
192#define AUTOCONFIG_REG 0x000009C4
193
194/* Audio ADC Registers */
195#define DSM_CTRL1 0x00000000
196#define DSM_CTRL2 0x00000001
197#define CHP_EN_CTRL 0x00000002
198#define CHP_CLK_CTRL1 0x00000004
199#define CHP_CLK_CTRL2 0x00000005
200#define BG_REF_CTRL 0x00000006
201#define SD2_SW_CTRL1 0x00000008
202#define SD2_SW_CTRL2 0x00000009
203#define SD2_BIAS_CTRL 0x0000000A
204#define AMP_BIAS_CTRL 0x0000000C
205#define CH_PWR_CTRL1 0x0000000E
206#define FLD_CH_SEL (1 << 3)
207#define CH_PWR_CTRL2 0x0000000F
208#define DSM_STATUS1 0x00000010
209#define DSM_STATUS2 0x00000011
210#define DIG_CTL1 0x00000012
211#define DIG_CTL2 0x00000013
212#define I2S_TX_CFG 0x0000001A
213
214#define DEV_CNTRL2 0x00040000
215
216#define PCI_MSK_IR (1 << 28)
217#define PCI_MSK_AV_CORE (1 << 27)
218#define PCI_MSK_GPIO1 (1 << 24)
219#define PCI_MSK_GPIO0 (1 << 23)
220#define PCI_MSK_APB_DMA (1 << 12)
221#define PCI_MSK_AL_WR (1 << 11)
222#define PCI_MSK_AL_RD (1 << 10)
223#define PCI_MSK_RISC_WR (1 << 9)
224#define PCI_MSK_RISC_RD (1 << 8)
225#define PCI_MSK_AUD_EXT (1 << 4)
226#define PCI_MSK_AUD_INT (1 << 3)
227#define PCI_MSK_VID_C (1 << 2)
228#define PCI_MSK_VID_B (1 << 1)
229#define PCI_MSK_VID_A 1
230#define PCI_INT_MSK 0x00040010
231
232#define PCI_INT_STAT 0x00040014
233#define PCI_INT_MSTAT 0x00040018
234
235#define VID_A_INT_MSK 0x00040020
236#define VID_A_INT_STAT 0x00040024
237#define VID_A_INT_MSTAT 0x00040028
238#define VID_A_INT_SSTAT 0x0004002C
239
240#define VID_B_INT_MSK 0x00040030
241#define VID_B_MSK_BAD_PKT (1 << 20)
242#define VID_B_MSK_VBI_OPC_ERR (1 << 17)
243#define VID_B_MSK_OPC_ERR (1 << 16)
244#define VID_B_MSK_VBI_SYNC (1 << 13)
245#define VID_B_MSK_SYNC (1 << 12)
246#define VID_B_MSK_VBI_OF (1 << 9)
247#define VID_B_MSK_OF (1 << 8)
248#define VID_B_MSK_VBI_RISCI2 (1 << 5)
249#define VID_B_MSK_RISCI2 (1 << 4)
250#define VID_B_MSK_VBI_RISCI1 (1 << 1)
251#define VID_B_MSK_RISCI1 1
252#define VID_B_INT_STAT 0x00040034
253#define VID_B_INT_MSTAT 0x00040038
254#define VID_B_INT_SSTAT 0x0004003C
255
256#define VID_B_MSK_BAD_PKT (1 << 20)
257#define VID_B_MSK_OPC_ERR (1 << 16)
258#define VID_B_MSK_SYNC (1 << 12)
259#define VID_B_MSK_OF (1 << 8)
260#define VID_B_MSK_RISCI2 (1 << 4)
261#define VID_B_MSK_RISCI1 1
262
263#define VID_C_MSK_BAD_PKT (1 << 20)
264#define VID_C_MSK_OPC_ERR (1 << 16)
265#define VID_C_MSK_SYNC (1 << 12)
266#define VID_C_MSK_OF (1 << 8)
267#define VID_C_MSK_RISCI2 (1 << 4)
268#define VID_C_MSK_RISCI1 1
269
270/* A superset for testing purposes */
271#define VID_BC_MSK_BAD_PKT (1 << 20)
272#define VID_BC_MSK_OPC_ERR (1 << 16)
273#define VID_BC_MSK_SYNC (1 << 12)
274#define VID_BC_MSK_OF (1 << 8)
275#define VID_BC_MSK_VBI_RISCI2 (1 << 5)
276#define VID_BC_MSK_RISCI2 (1 << 4)
277#define VID_BC_MSK_VBI_RISCI1 (1 << 1)
278#define VID_BC_MSK_RISCI1 1
279
280#define VID_C_INT_MSK 0x00040040
281#define VID_C_INT_STAT 0x00040044
282#define VID_C_INT_MSTAT 0x00040048
283#define VID_C_INT_SSTAT 0x0004004C
284
285#define AUDIO_INT_INT_MSK 0x00040050
286#define AUDIO_INT_INT_STAT 0x00040054
287#define AUDIO_INT_INT_MSTAT 0x00040058
288#define AUDIO_INT_INT_SSTAT 0x0004005C
289
290#define AUDIO_EXT_INT_MSK 0x00040060
291#define AUDIO_EXT_INT_STAT 0x00040064
292#define AUDIO_EXT_INT_MSTAT 0x00040068
293#define AUDIO_EXT_INT_SSTAT 0x0004006C
294
295#define RDR_CFG0 0x00050000
296#define RDR_CFG1 0x00050004
297#define RDR_CFG2 0x00050008
298#define RDR_RDRCTL1 0x0005030c
299#define RDR_TLCTL0 0x00050318
300
301/* APB DMAC Current Buffer Pointer */
302#define DMA1_PTR1 0x00100000
303#define DMA2_PTR1 0x00100004
304#define DMA3_PTR1 0x00100008
305#define DMA4_PTR1 0x0010000C
306#define DMA5_PTR1 0x00100010
307#define DMA6_PTR1 0x00100014
308#define DMA7_PTR1 0x00100018
309#define DMA8_PTR1 0x0010001C
310
311/* APB DMAC Current Table Pointer */
312#define DMA1_PTR2 0x00100040
313#define DMA2_PTR2 0x00100044
314#define DMA3_PTR2 0x00100048
315#define DMA4_PTR2 0x0010004C
316#define DMA5_PTR2 0x00100050
317#define DMA6_PTR2 0x00100054
318#define DMA7_PTR2 0x00100058
319#define DMA8_PTR2 0x0010005C
320
321/* APB DMAC Buffer Limit */
322#define DMA1_CNT1 0x00100080
323#define DMA2_CNT1 0x00100084
324#define DMA3_CNT1 0x00100088
325#define DMA4_CNT1 0x0010008C
326#define DMA5_CNT1 0x00100090
327#define DMA6_CNT1 0x00100094
328#define DMA7_CNT1 0x00100098
329#define DMA8_CNT1 0x0010009C
330
331/* APB DMAC Table Size */
332#define DMA1_CNT2 0x001000C0
333#define DMA2_CNT2 0x001000C4
334#define DMA3_CNT2 0x001000C8
335#define DMA4_CNT2 0x001000CC
336#define DMA5_CNT2 0x001000D0
337#define DMA6_CNT2 0x001000D4
338#define DMA7_CNT2 0x001000D8
339#define DMA8_CNT2 0x001000DC
340
341/* Timer Counters */
342#define TM_CNT_LDW 0x00110000
343#define TM_CNT_UW 0x00110004
344#define TM_LMT_LDW 0x00110008
345#define TM_LMT_UW 0x0011000C
346
347/* GPIO */
348#define GP0_IO 0x00110010
349#define GPIO_ISM 0x00110014
350#define SOFT_RESET 0x0011001C
351
352/* GPIO (417 Microsoftcontroller) RW Data */
353#define MC417_RWD 0x00110020
354
355/* GPIO (417 Microsoftcontroller) Output Enable, Low Active */
356#define MC417_OEN 0x00110024
357#define MC417_CTL 0x00110028
358#define ALT_PIN_OUT_SEL 0x0011002C
359#define CLK_DELAY 0x00110048
360#define PAD_CTRL 0x0011004C
361
362/* Video A Interface */
363#define VID_A_GPCNT 0x00130020
364#define VBI_A_GPCNT 0x00130024
365#define VID_A_GPCNT_CTL 0x00130030
366#define VBI_A_GPCNT_CTL 0x00130034
367#define VID_A_DMA_CTL 0x00130040
368#define VID_A_VIP_CTRL 0x00130080
369#define VID_A_PIXEL_FRMT 0x00130084
370#define VID_A_VBI_CTRL 0x00130088
371
372/* Video B Interface */
373#define VID_B_DMA 0x00130100
374#define VBI_B_DMA 0x00130108
375#define VID_B_GPCNT 0x00130120
376#define VBI_B_GPCNT 0x00130124
377#define VID_B_GPCNT_CTL 0x00130134
378#define VBI_B_GPCNT_CTL 0x00130138
379#define VID_B_DMA_CTL 0x00130140
380#define VID_B_SRC_SEL 0x00130144
381#define VID_B_LNGTH 0x00130150
382#define VID_B_HW_SOP_CTL 0x00130154
383#define VID_B_GEN_CTL 0x00130158
384#define VID_B_BD_PKT_STATUS 0x0013015C
385#define VID_B_SOP_STATUS 0x00130160
386#define VID_B_FIFO_OVFL_STAT 0x00130164
387#define VID_B_VLD_MISC 0x00130168
388#define VID_B_TS_CLK_EN 0x0013016C
389#define VID_B_VIP_CTRL 0x00130180
390#define VID_B_PIXEL_FRMT 0x00130184
391
392/* Video C Interface */
393#define VID_C_GPCNT 0x00130220
394#define VID_C_GPCNT_CTL 0x00130230
395#define VBI_C_GPCNT_CTL 0x00130234
396#define VID_C_DMA_CTL 0x00130240
397#define VID_C_LNGTH 0x00130250
398#define VID_C_HW_SOP_CTL 0x00130254
399#define VID_C_GEN_CTL 0x00130258
400#define VID_C_BD_PKT_STATUS 0x0013025C
401#define VID_C_SOP_STATUS 0x00130260
402#define VID_C_FIFO_OVFL_STAT 0x00130264
403#define VID_C_VLD_MISC 0x00130268
404#define VID_C_TS_CLK_EN 0x0013026C
405
406/* Internal Audio Interface */
407#define AUD_INT_A_GPCNT 0x00140020
408#define AUD_INT_B_GPCNT 0x00140024
409#define AUD_INT_A_GPCNT_CTL 0x00140030
410#define AUD_INT_B_GPCNT_CTL 0x00140034
411#define AUD_INT_DMA_CTL 0x00140040
412#define AUD_INT_A_LNGTH 0x00140050
413#define AUD_INT_B_LNGTH 0x00140054
414#define AUD_INT_A_MODE 0x00140058
415#define AUD_INT_B_MODE 0x0014005C
416
417/* External Audio Interface */
418#define AUD_EXT_DMA 0x00140100
419#define AUD_EXT_GPCNT 0x00140120
420#define AUD_EXT_GPCNT_CTL 0x00140130
421#define AUD_EXT_DMA_CTL 0x00140140
422#define AUD_EXT_LNGTH 0x00140150
423#define AUD_EXT_A_MODE 0x00140158
424
425/* I2C Bus 1 */
426#define I2C1_ADDR 0x00180000
427#define I2C1_WDATA 0x00180004
428#define I2C1_CTRL 0x00180008
429#define I2C1_RDATA 0x0018000C
430#define I2C1_STAT 0x00180010
431
432/* I2C Bus 2 */
433#define I2C2_ADDR 0x00190000
434#define I2C2_WDATA 0x00190004
435#define I2C2_CTRL 0x00190008
436#define I2C2_RDATA 0x0019000C
437#define I2C2_STAT 0x00190010
438
439/* I2C Bus 3 */
440#define I2C3_ADDR 0x001A0000
441#define I2C3_WDATA 0x001A0004
442#define I2C3_CTRL 0x001A0008
443#define I2C3_RDATA 0x001A000C
444#define I2C3_STAT 0x001A0010
445
446/* UART */
447#define UART_CTL 0x001B0000
448#define UART_BRD 0x001B0004
449#define UART_ISR 0x001B000C
450#define UART_CNT 0x001B0010
451
452#endif /* _CX23885_REG_H_ */
diff --git a/drivers/media/pci/cx23885/cx23885-vbi.c b/drivers/media/pci/cx23885/cx23885-vbi.c
new file mode 100644
index 000000000000..a1154f035bc1
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-vbi.c
@@ -0,0 +1,295 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/init.h>
26
27#include "cx23885.h"
28
29static unsigned int vbibufs = 4;
30module_param(vbibufs, int, 0644);
31MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
32
33static unsigned int vbi_debug;
34module_param(vbi_debug, int, 0644);
35MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
36
37#define dprintk(level, fmt, arg...)\
38 do { if (vbi_debug >= level)\
39 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
40 } while (0)
41
42/* ------------------------------------------------------------------ */
43
44#define VBI_LINE_LENGTH 1440
45#define NTSC_VBI_START_LINE 10 /* line 10 - 21 */
46#define NTSC_VBI_END_LINE 21
47#define NTSC_VBI_LINES (NTSC_VBI_END_LINE - NTSC_VBI_START_LINE + 1)
48
49
50int cx23885_vbi_fmt(struct file *file, void *priv,
51 struct v4l2_format *f)
52{
53 struct cx23885_fh *fh = priv;
54 struct cx23885_dev *dev = fh->dev;
55
56 if (dev->tvnorm & V4L2_STD_525_60) {
57 /* ntsc */
58 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
59 f->fmt.vbi.sampling_rate = 27000000;
60 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
61 f->fmt.vbi.offset = 0;
62 f->fmt.vbi.flags = 0;
63 f->fmt.vbi.start[0] = 10;
64 f->fmt.vbi.count[0] = 17;
65 f->fmt.vbi.start[1] = 263 + 10 + 1;
66 f->fmt.vbi.count[1] = 17;
67 } else if (dev->tvnorm & V4L2_STD_625_50) {
68 /* pal */
69 f->fmt.vbi.sampling_rate = 35468950;
70 f->fmt.vbi.start[0] = 7 - 1;
71 f->fmt.vbi.start[1] = 319 - 1;
72 }
73
74 return 0;
75}
76
77/* We're given the Video Interrupt status register.
78 * The cx23885_video_irq() func has already validated
79 * the potential error bits, we just need to
80 * deal with vbi payload and return indication if
81 * we actually processed any payload.
82 */
83int cx23885_vbi_irq(struct cx23885_dev *dev, u32 status)
84{
85 u32 count;
86 int handled = 0;
87
88 if (status & VID_BC_MSK_VBI_RISCI1) {
89 dprintk(1, "%s() VID_BC_MSK_VBI_RISCI1\n", __func__);
90 spin_lock(&dev->slock);
91 count = cx_read(VID_A_GPCNT);
92 cx23885_video_wakeup(dev, &dev->vbiq, count);
93 spin_unlock(&dev->slock);
94 handled++;
95 }
96
97 if (status & VID_BC_MSK_VBI_RISCI2) {
98 dprintk(1, "%s() VID_BC_MSK_VBI_RISCI2\n", __func__);
99 dprintk(2, "stopper vbi\n");
100 spin_lock(&dev->slock);
101 cx23885_restart_vbi_queue(dev, &dev->vbiq);
102 spin_unlock(&dev->slock);
103 handled++;
104 }
105
106 return handled;
107}
108
109static int cx23885_start_vbi_dma(struct cx23885_dev *dev,
110 struct cx23885_dmaqueue *q,
111 struct cx23885_buffer *buf)
112{
113 dprintk(1, "%s()\n", __func__);
114
115 /* setup fifo + format */
116 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH02],
117 buf->vb.width, buf->risc.dma);
118
119 /* reset counter */
120 cx_write(VID_A_GPCNT_CTL, 3);
121 cx_write(VID_A_VBI_CTRL, 3);
122 cx_write(VBI_A_GPCNT_CTL, 3);
123 q->count = 1;
124
125 /* enable irq */
126 cx23885_irq_add_enable(dev, 0x01);
127 cx_set(VID_A_INT_MSK, 0x000022);
128
129 /* start dma */
130 cx_set(DEV_CNTRL2, (1<<5));
131 cx_set(VID_A_DMA_CTL, 0x22); /* FIFO and RISC enable */
132
133 return 0;
134}
135
136
137int cx23885_restart_vbi_queue(struct cx23885_dev *dev,
138 struct cx23885_dmaqueue *q)
139{
140 struct cx23885_buffer *buf;
141 struct list_head *item;
142
143 if (list_empty(&q->active))
144 return 0;
145
146 buf = list_entry(q->active.next, struct cx23885_buffer, vb.queue);
147 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
148 buf, buf->vb.i);
149 cx23885_start_vbi_dma(dev, q, buf);
150 list_for_each(item, &q->active) {
151 buf = list_entry(item, struct cx23885_buffer, vb.queue);
152 buf->count = q->count++;
153 }
154 mod_timer(&q->timeout, jiffies + (BUFFER_TIMEOUT / 30));
155 return 0;
156}
157
158void cx23885_vbi_timeout(unsigned long data)
159{
160 struct cx23885_dev *dev = (struct cx23885_dev *)data;
161 struct cx23885_dmaqueue *q = &dev->vbiq;
162 struct cx23885_buffer *buf;
163 unsigned long flags;
164
165 /* Stop the VBI engine */
166 cx_clear(VID_A_DMA_CTL, 0x22);
167
168 spin_lock_irqsave(&dev->slock, flags);
169 while (!list_empty(&q->active)) {
170 buf = list_entry(q->active.next, struct cx23885_buffer,
171 vb.queue);
172 list_del(&buf->vb.queue);
173 buf->vb.state = VIDEOBUF_ERROR;
174 wake_up(&buf->vb.done);
175 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
176 buf, buf->vb.i, (unsigned long)buf->risc.dma);
177 }
178 cx23885_restart_vbi_queue(dev, q);
179 spin_unlock_irqrestore(&dev->slock, flags);
180}
181
182/* ------------------------------------------------------------------ */
183#define VBI_LINE_LENGTH 1440
184#define VBI_LINE_COUNT 17
185
186static int
187vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
188{
189 *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
190 if (0 == *count)
191 *count = vbibufs;
192 if (*count < 2)
193 *count = 2;
194 if (*count > 32)
195 *count = 32;
196 return 0;
197}
198
199static int
200vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
201 enum v4l2_field field)
202{
203 struct cx23885_fh *fh = q->priv_data;
204 struct cx23885_dev *dev = fh->dev;
205 struct cx23885_buffer *buf = container_of(vb,
206 struct cx23885_buffer, vb);
207 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
208 unsigned int size;
209 int rc;
210
211 size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
212 if (0 != buf->vb.baddr && buf->vb.bsize < size)
213 return -EINVAL;
214
215 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
216 buf->vb.width = VBI_LINE_LENGTH;
217 buf->vb.height = VBI_LINE_COUNT;
218 buf->vb.size = size;
219 buf->vb.field = V4L2_FIELD_SEQ_TB;
220
221 rc = videobuf_iolock(q, &buf->vb, NULL);
222 if (0 != rc)
223 goto fail;
224 cx23885_risc_vbibuffer(dev->pci, &buf->risc,
225 dma->sglist,
226 0, buf->vb.width * buf->vb.height,
227 buf->vb.width, 0,
228 buf->vb.height);
229 }
230 buf->vb.state = VIDEOBUF_PREPARED;
231 return 0;
232
233 fail:
234 cx23885_free_buffer(q, buf);
235 return rc;
236}
237
238static void
239vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
240{
241 struct cx23885_buffer *buf =
242 container_of(vb, struct cx23885_buffer, vb);
243 struct cx23885_buffer *prev;
244 struct cx23885_fh *fh = vq->priv_data;
245 struct cx23885_dev *dev = fh->dev;
246 struct cx23885_dmaqueue *q = &dev->vbiq;
247
248 /* add jump to stopper */
249 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
250 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
251 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
252
253 if (list_empty(&q->active)) {
254 list_add_tail(&buf->vb.queue, &q->active);
255 cx23885_start_vbi_dma(dev, q, buf);
256 buf->vb.state = VIDEOBUF_ACTIVE;
257 buf->count = q->count++;
258 mod_timer(&q->timeout, jiffies + (BUFFER_TIMEOUT / 30));
259 dprintk(2, "[%p/%d] vbi_queue - first active\n",
260 buf, buf->vb.i);
261
262 } else {
263 prev = list_entry(q->active.prev, struct cx23885_buffer,
264 vb.queue);
265 list_add_tail(&buf->vb.queue, &q->active);
266 buf->vb.state = VIDEOBUF_ACTIVE;
267 buf->count = q->count++;
268 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
269 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63-32 */
270 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
271 buf, buf->vb.i);
272 }
273}
274
275static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
276{
277 struct cx23885_buffer *buf =
278 container_of(vb, struct cx23885_buffer, vb);
279
280 cx23885_free_buffer(q, buf);
281}
282
283struct videobuf_queue_ops cx23885_vbi_qops = {
284 .buf_setup = vbi_setup,
285 .buf_prepare = vbi_prepare,
286 .buf_queue = vbi_queue,
287 .buf_release = vbi_release,
288};
289
290/* ------------------------------------------------------------------ */
291/*
292 * Local variables:
293 * c-basic-offset: 8
294 * End:
295 */
diff --git a/drivers/media/pci/cx23885/cx23885-video.c b/drivers/media/pci/cx23885/cx23885-video.c
new file mode 100644
index 000000000000..22f8e7fbd665
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885-video.c
@@ -0,0 +1,1926 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/init.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kmod.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30#include <linux/delay.h>
31#include <linux/kthread.h>
32#include <asm/div64.h>
33
34#include "cx23885.h"
35#include <media/v4l2-common.h>
36#include <media/v4l2-ioctl.h>
37#include "cx23885-ioctl.h"
38#include "tuner-xc2028.h"
39
40#include <media/cx25840.h>
41
42MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
43MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
44MODULE_LICENSE("GPL");
45
46/* ------------------------------------------------------------------ */
47
48static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
51
52module_param_array(video_nr, int, NULL, 0444);
53module_param_array(vbi_nr, int, NULL, 0444);
54module_param_array(radio_nr, int, NULL, 0444);
55
56MODULE_PARM_DESC(video_nr, "video device numbers");
57MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
58MODULE_PARM_DESC(radio_nr, "radio device numbers");
59
60static unsigned int video_debug;
61module_param(video_debug, int, 0644);
62MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
63
64static unsigned int irq_debug;
65module_param(irq_debug, int, 0644);
66MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
67
68static unsigned int vid_limit = 16;
69module_param(vid_limit, int, 0644);
70MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
71
72#define dprintk(level, fmt, arg...)\
73 do { if (video_debug >= level)\
74 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
75 } while (0)
76
77/* ------------------------------------------------------------------- */
78/* static data */
79
80#define FORMAT_FLAGS_PACKED 0x01
81#if 0
82static struct cx23885_fmt formats[] = {
83 {
84 .name = "8 bpp, gray",
85 .fourcc = V4L2_PIX_FMT_GREY,
86 .depth = 8,
87 .flags = FORMAT_FLAGS_PACKED,
88 }, {
89 .name = "15 bpp RGB, le",
90 .fourcc = V4L2_PIX_FMT_RGB555,
91 .depth = 16,
92 .flags = FORMAT_FLAGS_PACKED,
93 }, {
94 .name = "15 bpp RGB, be",
95 .fourcc = V4L2_PIX_FMT_RGB555X,
96 .depth = 16,
97 .flags = FORMAT_FLAGS_PACKED,
98 }, {
99 .name = "16 bpp RGB, le",
100 .fourcc = V4L2_PIX_FMT_RGB565,
101 .depth = 16,
102 .flags = FORMAT_FLAGS_PACKED,
103 }, {
104 .name = "16 bpp RGB, be",
105 .fourcc = V4L2_PIX_FMT_RGB565X,
106 .depth = 16,
107 .flags = FORMAT_FLAGS_PACKED,
108 }, {
109 .name = "24 bpp RGB, le",
110 .fourcc = V4L2_PIX_FMT_BGR24,
111 .depth = 24,
112 .flags = FORMAT_FLAGS_PACKED,
113 }, {
114 .name = "32 bpp RGB, le",
115 .fourcc = V4L2_PIX_FMT_BGR32,
116 .depth = 32,
117 .flags = FORMAT_FLAGS_PACKED,
118 }, {
119 .name = "32 bpp RGB, be",
120 .fourcc = V4L2_PIX_FMT_RGB32,
121 .depth = 32,
122 .flags = FORMAT_FLAGS_PACKED,
123 }, {
124 .name = "4:2:2, packed, YUYV",
125 .fourcc = V4L2_PIX_FMT_YUYV,
126 .depth = 16,
127 .flags = FORMAT_FLAGS_PACKED,
128 }, {
129 .name = "4:2:2, packed, UYVY",
130 .fourcc = V4L2_PIX_FMT_UYVY,
131 .depth = 16,
132 .flags = FORMAT_FLAGS_PACKED,
133 },
134};
135#else
136static struct cx23885_fmt formats[] = {
137 {
138#if 0
139 .name = "4:2:2, packed, UYVY",
140 .fourcc = V4L2_PIX_FMT_UYVY,
141 .depth = 16,
142 .flags = FORMAT_FLAGS_PACKED,
143 }, {
144#endif
145 .name = "4:2:2, packed, YUYV",
146 .fourcc = V4L2_PIX_FMT_YUYV,
147 .depth = 16,
148 .flags = FORMAT_FLAGS_PACKED,
149 }
150};
151#endif
152
153static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
154{
155 unsigned int i;
156
157 for (i = 0; i < ARRAY_SIZE(formats); i++)
158 if (formats[i].fourcc == fourcc)
159 return formats+i;
160
161 printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__,
162 (fourcc & 0xff),
163 ((fourcc >> 8) & 0xff),
164 ((fourcc >> 16) & 0xff),
165 ((fourcc >> 24) & 0xff)
166 );
167 return NULL;
168}
169
170/* ------------------------------------------------------------------- */
171
172static const struct v4l2_queryctrl no_ctl = {
173 .name = "42",
174 .flags = V4L2_CTRL_FLAG_DISABLED,
175};
176
177static struct cx23885_ctrl cx23885_ctls[] = {
178 /* --- video --- */
179 {
180 .v = {
181 .id = V4L2_CID_BRIGHTNESS,
182 .name = "Brightness",
183 .minimum = 0x00,
184 .maximum = 0xff,
185 .step = 1,
186 .default_value = 0x7f,
187 .type = V4L2_CTRL_TYPE_INTEGER,
188 },
189 .off = 128,
190 .reg = LUMA_CTRL,
191 .mask = 0x00ff,
192 .shift = 0,
193 }, {
194 .v = {
195 .id = V4L2_CID_CONTRAST,
196 .name = "Contrast",
197 .minimum = 0,
198 .maximum = 0x7f,
199 .step = 1,
200 .default_value = 0x3f,
201 .type = V4L2_CTRL_TYPE_INTEGER,
202 },
203 .off = 0,
204 .reg = LUMA_CTRL,
205 .mask = 0xff00,
206 .shift = 8,
207 }, {
208 .v = {
209 .id = V4L2_CID_HUE,
210 .name = "Hue",
211 .minimum = -127,
212 .maximum = 128,
213 .step = 1,
214 .default_value = 0x0,
215 .type = V4L2_CTRL_TYPE_INTEGER,
216 },
217 .off = 128,
218 .reg = CHROMA_CTRL,
219 .mask = 0xff0000,
220 .shift = 16,
221 }, {
222 /* strictly, this only describes only U saturation.
223 * V saturation is handled specially through code.
224 */
225 .v = {
226 .id = V4L2_CID_SATURATION,
227 .name = "Saturation",
228 .minimum = 0,
229 .maximum = 0x7f,
230 .step = 1,
231 .default_value = 0x3f,
232 .type = V4L2_CTRL_TYPE_INTEGER,
233 },
234 .off = 0,
235 .reg = CHROMA_CTRL,
236 .mask = 0x00ff,
237 .shift = 0,
238 }, {
239 /* --- audio --- */
240 .v = {
241 .id = V4L2_CID_AUDIO_MUTE,
242 .name = "Mute",
243 .minimum = 0,
244 .maximum = 1,
245 .default_value = 1,
246 .type = V4L2_CTRL_TYPE_BOOLEAN,
247 },
248 .reg = PATH1_CTL1,
249 .mask = (0x1f << 24),
250 .shift = 24,
251 }, {
252 .v = {
253 .id = V4L2_CID_AUDIO_VOLUME,
254 .name = "Volume",
255 .minimum = 0,
256 .maximum = 65535,
257 .step = 65535 / 100,
258 .default_value = 65535,
259 .type = V4L2_CTRL_TYPE_INTEGER,
260 },
261 .reg = PATH1_VOL_CTL,
262 .mask = 0xff,
263 .shift = 0,
264 }
265};
266static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
267
268/* Must be sorted from low to high control ID! */
269static const u32 cx23885_user_ctrls[] = {
270 V4L2_CID_USER_CLASS,
271 V4L2_CID_BRIGHTNESS,
272 V4L2_CID_CONTRAST,
273 V4L2_CID_SATURATION,
274 V4L2_CID_HUE,
275 V4L2_CID_AUDIO_VOLUME,
276 V4L2_CID_AUDIO_MUTE,
277 0
278};
279
280static const u32 *ctrl_classes[] = {
281 cx23885_user_ctrls,
282 NULL
283};
284
285void cx23885_video_wakeup(struct cx23885_dev *dev,
286 struct cx23885_dmaqueue *q, u32 count)
287{
288 struct cx23885_buffer *buf;
289 int bc;
290
291 for (bc = 0;; bc++) {
292 if (list_empty(&q->active))
293 break;
294 buf = list_entry(q->active.next,
295 struct cx23885_buffer, vb.queue);
296
297 /* count comes from the hw and is is 16bit wide --
298 * this trick handles wrap-arounds correctly for
299 * up to 32767 buffers in flight... */
300 if ((s16) (count - buf->count) < 0)
301 break;
302
303 do_gettimeofday(&buf->vb.ts);
304 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
305 count, buf->count);
306 buf->vb.state = VIDEOBUF_DONE;
307 list_del(&buf->vb.queue);
308 wake_up(&buf->vb.done);
309 }
310 if (list_empty(&q->active))
311 del_timer(&q->timeout);
312 else
313 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
314 if (bc != 1)
315 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
316 __func__, bc);
317}
318
319int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
320{
321 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
322 __func__,
323 (unsigned int)norm,
324 v4l2_norm_to_name(norm));
325
326 dev->tvnorm = norm;
327
328 call_all(dev, core, s_std, norm);
329
330 return 0;
331}
332
333static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
334 struct pci_dev *pci,
335 struct video_device *template,
336 char *type)
337{
338 struct video_device *vfd;
339 dprintk(1, "%s()\n", __func__);
340
341 vfd = video_device_alloc();
342 if (NULL == vfd)
343 return NULL;
344 *vfd = *template;
345 vfd->v4l2_dev = &dev->v4l2_dev;
346 vfd->release = video_device_release;
347 snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
348 cx23885_boards[dev->board].name, type);
349 video_set_drvdata(vfd, dev);
350 return vfd;
351}
352
353static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
354{
355 int i;
356
357 if (qctrl->id < V4L2_CID_BASE ||
358 qctrl->id >= V4L2_CID_LASTP1)
359 return -EINVAL;
360 for (i = 0; i < CX23885_CTLS; i++)
361 if (cx23885_ctls[i].v.id == qctrl->id)
362 break;
363 if (i == CX23885_CTLS) {
364 *qctrl = no_ctl;
365 return 0;
366 }
367 *qctrl = cx23885_ctls[i].v;
368 return 0;
369}
370
371/* ------------------------------------------------------------------- */
372/* resource management */
373
374static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
375 unsigned int bit)
376{
377 dprintk(1, "%s()\n", __func__);
378 if (fh->resources & bit)
379 /* have it already allocated */
380 return 1;
381
382 /* is it free? */
383 mutex_lock(&dev->lock);
384 if (dev->resources & bit) {
385 /* no, someone else uses it */
386 mutex_unlock(&dev->lock);
387 return 0;
388 }
389 /* it's free, grab it */
390 fh->resources |= bit;
391 dev->resources |= bit;
392 dprintk(1, "res: get %d\n", bit);
393 mutex_unlock(&dev->lock);
394 return 1;
395}
396
397static int res_check(struct cx23885_fh *fh, unsigned int bit)
398{
399 return fh->resources & bit;
400}
401
402static int res_locked(struct cx23885_dev *dev, unsigned int bit)
403{
404 return dev->resources & bit;
405}
406
407static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
408 unsigned int bits)
409{
410 BUG_ON((fh->resources & bits) != bits);
411 dprintk(1, "%s()\n", __func__);
412
413 mutex_lock(&dev->lock);
414 fh->resources &= ~bits;
415 dev->resources &= ~bits;
416 dprintk(1, "res: put %d\n", bits);
417 mutex_unlock(&dev->lock);
418}
419
420static int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
421{
422 /* 8 bit registers, 8 bit values */
423 u8 buf[] = { reg, data };
424
425 struct i2c_msg msg = { .addr = 0x98 >> 1,
426 .flags = 0, .buf = buf, .len = 2 };
427
428 return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
429}
430
431static u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
432{
433 /* 8 bit registers, 8 bit values */
434 int ret;
435 u8 b0[] = { reg };
436 u8 b1[] = { 0 };
437
438 struct i2c_msg msg[] = {
439 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
440 { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
441 };
442
443 ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
444 if (ret != 2)
445 printk(KERN_ERR "%s() error\n", __func__);
446
447 return b1[0];
448}
449
450static void cx23885_flatiron_dump(struct cx23885_dev *dev)
451{
452 int i;
453 dprintk(1, "Flatiron dump\n");
454 for (i = 0; i < 0x24; i++) {
455 dprintk(1, "FI[%02x] = %02x\n", i,
456 cx23885_flatiron_read(dev, i));
457 }
458}
459
460static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
461{
462 u8 val;
463 dprintk(1, "%s(input = %d)\n", __func__, input);
464
465 if (input == 1)
466 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
467 else if (input == 2)
468 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
469 else
470 return -EINVAL;
471
472 val |= 0x20; /* Enable clock to delta-sigma and dec filter */
473
474 cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
475
476 /* Wake up */
477 cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
478
479 if (video_debug)
480 cx23885_flatiron_dump(dev);
481
482 return 0;
483}
484
485static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
486{
487 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
488 __func__,
489 input, INPUT(input)->vmux,
490 INPUT(input)->gpio0, INPUT(input)->gpio1,
491 INPUT(input)->gpio2, INPUT(input)->gpio3);
492 dev->input = input;
493
494 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
495 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
496 dev->board == CX23885_BOARD_MYGICA_X8507) {
497 /* Select Analog TV */
498 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
499 cx23885_gpio_clear(dev, GPIO_0);
500 }
501
502 /* Tell the internal A/V decoder */
503 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
504 INPUT(input)->vmux, 0, 0);
505
506 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
507 (dev->board == CX23885_BOARD_MPX885) ||
508 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
509 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
510 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
511 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850)) {
512 /* Configure audio routing */
513 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
514 INPUT(input)->amux, 0, 0);
515
516 if (INPUT(input)->amux == CX25840_AUDIO7)
517 cx23885_flatiron_mux(dev, 1);
518 else if (INPUT(input)->amux == CX25840_AUDIO6)
519 cx23885_flatiron_mux(dev, 2);
520 }
521
522 return 0;
523}
524
525static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
526{
527 dprintk(1, "%s(input=%d)\n", __func__, input);
528
529 /* The baseband video core of the cx23885 has two audio inputs.
530 * LR1 and LR2. In almost every single case so far only HVR1xxx
531 * cards we've only ever supported LR1. Time to support LR2,
532 * which is available via the optional white breakout header on
533 * the board.
534 * We'll use a could of existing enums in the card struct to allow
535 * devs to specify which baseband input they need, or just default
536 * to what we've always used.
537 */
538 if (INPUT(input)->amux == CX25840_AUDIO7)
539 cx23885_flatiron_mux(dev, 1);
540 else if (INPUT(input)->amux == CX25840_AUDIO6)
541 cx23885_flatiron_mux(dev, 2);
542 else {
543 /* Not specifically defined, assume the default. */
544 cx23885_flatiron_mux(dev, 1);
545 }
546
547 return 0;
548}
549
550/* ------------------------------------------------------------------ */
551static int cx23885_start_video_dma(struct cx23885_dev *dev,
552 struct cx23885_dmaqueue *q,
553 struct cx23885_buffer *buf)
554{
555 dprintk(1, "%s()\n", __func__);
556
557 /* Stop the dma/fifo before we tamper with it's risc programs */
558 cx_clear(VID_A_DMA_CTL, 0x11);
559
560 /* setup fifo + format */
561 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
562 buf->bpl, buf->risc.dma);
563
564 /* reset counter */
565 cx_write(VID_A_GPCNT_CTL, 3);
566 q->count = 1;
567
568 /* enable irq */
569 cx23885_irq_add_enable(dev, 0x01);
570 cx_set(VID_A_INT_MSK, 0x000011);
571
572 /* start dma */
573 cx_set(DEV_CNTRL2, (1<<5));
574 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
575
576 return 0;
577}
578
579
580static int cx23885_restart_video_queue(struct cx23885_dev *dev,
581 struct cx23885_dmaqueue *q)
582{
583 struct cx23885_buffer *buf, *prev;
584 struct list_head *item;
585 dprintk(1, "%s()\n", __func__);
586
587 if (!list_empty(&q->active)) {
588 buf = list_entry(q->active.next, struct cx23885_buffer,
589 vb.queue);
590 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
591 buf, buf->vb.i);
592 cx23885_start_video_dma(dev, q, buf);
593 list_for_each(item, &q->active) {
594 buf = list_entry(item, struct cx23885_buffer,
595 vb.queue);
596 buf->count = q->count++;
597 }
598 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
599 return 0;
600 }
601
602 prev = NULL;
603 for (;;) {
604 if (list_empty(&q->queued))
605 return 0;
606 buf = list_entry(q->queued.next, struct cx23885_buffer,
607 vb.queue);
608 if (NULL == prev) {
609 list_move_tail(&buf->vb.queue, &q->active);
610 cx23885_start_video_dma(dev, q, buf);
611 buf->vb.state = VIDEOBUF_ACTIVE;
612 buf->count = q->count++;
613 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
614 dprintk(2, "[%p/%d] restart_queue - first active\n",
615 buf, buf->vb.i);
616
617 } else if (prev->vb.width == buf->vb.width &&
618 prev->vb.height == buf->vb.height &&
619 prev->fmt == buf->fmt) {
620 list_move_tail(&buf->vb.queue, &q->active);
621 buf->vb.state = VIDEOBUF_ACTIVE;
622 buf->count = q->count++;
623 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
624 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
625 dprintk(2, "[%p/%d] restart_queue - move to active\n",
626 buf, buf->vb.i);
627 } else {
628 return 0;
629 }
630 prev = buf;
631 }
632}
633
634static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
635 unsigned int *size)
636{
637 struct cx23885_fh *fh = q->priv_data;
638
639 *size = fh->fmt->depth*fh->width*fh->height >> 3;
640 if (0 == *count)
641 *count = 32;
642 if (*size * *count > vid_limit * 1024 * 1024)
643 *count = (vid_limit * 1024 * 1024) / *size;
644 return 0;
645}
646
647static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
648 enum v4l2_field field)
649{
650 struct cx23885_fh *fh = q->priv_data;
651 struct cx23885_dev *dev = fh->dev;
652 struct cx23885_buffer *buf =
653 container_of(vb, struct cx23885_buffer, vb);
654 int rc, init_buffer = 0;
655 u32 line0_offset, line1_offset;
656 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
657 int field_tff;
658
659 BUG_ON(NULL == fh->fmt);
660 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
661 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
662 return -EINVAL;
663 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
664 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
665 return -EINVAL;
666
667 if (buf->fmt != fh->fmt ||
668 buf->vb.width != fh->width ||
669 buf->vb.height != fh->height ||
670 buf->vb.field != field) {
671 buf->fmt = fh->fmt;
672 buf->vb.width = fh->width;
673 buf->vb.height = fh->height;
674 buf->vb.field = field;
675 init_buffer = 1;
676 }
677
678 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
679 init_buffer = 1;
680 rc = videobuf_iolock(q, &buf->vb, NULL);
681 if (0 != rc)
682 goto fail;
683 }
684
685 if (init_buffer) {
686 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
687 switch (buf->vb.field) {
688 case V4L2_FIELD_TOP:
689 cx23885_risc_buffer(dev->pci, &buf->risc,
690 dma->sglist, 0, UNSET,
691 buf->bpl, 0, buf->vb.height);
692 break;
693 case V4L2_FIELD_BOTTOM:
694 cx23885_risc_buffer(dev->pci, &buf->risc,
695 dma->sglist, UNSET, 0,
696 buf->bpl, 0, buf->vb.height);
697 break;
698 case V4L2_FIELD_INTERLACED:
699 if (dev->tvnorm & V4L2_STD_NTSC)
700 /* NTSC or */
701 field_tff = 1;
702 else
703 field_tff = 0;
704
705 if (cx23885_boards[dev->board].force_bff)
706 /* PAL / SECAM OR 888 in NTSC MODE */
707 field_tff = 0;
708
709 if (field_tff) {
710 /* cx25840 transmits NTSC bottom field first */
711 dprintk(1, "%s() Creating TFF/NTSC risc\n",
712 __func__);
713 line0_offset = buf->bpl;
714 line1_offset = 0;
715 } else {
716 /* All other formats are top field first */
717 dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
718 __func__);
719 line0_offset = 0;
720 line1_offset = buf->bpl;
721 }
722 cx23885_risc_buffer(dev->pci, &buf->risc,
723 dma->sglist, line0_offset,
724 line1_offset,
725 buf->bpl, buf->bpl,
726 buf->vb.height >> 1);
727 break;
728 case V4L2_FIELD_SEQ_TB:
729 cx23885_risc_buffer(dev->pci, &buf->risc,
730 dma->sglist,
731 0, buf->bpl * (buf->vb.height >> 1),
732 buf->bpl, 0,
733 buf->vb.height >> 1);
734 break;
735 case V4L2_FIELD_SEQ_BT:
736 cx23885_risc_buffer(dev->pci, &buf->risc,
737 dma->sglist,
738 buf->bpl * (buf->vb.height >> 1), 0,
739 buf->bpl, 0,
740 buf->vb.height >> 1);
741 break;
742 default:
743 BUG();
744 }
745 }
746 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
747 buf, buf->vb.i,
748 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
749 (unsigned long)buf->risc.dma);
750
751 buf->vb.state = VIDEOBUF_PREPARED;
752 return 0;
753
754 fail:
755 cx23885_free_buffer(q, buf);
756 return rc;
757}
758
759static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
760{
761 struct cx23885_buffer *buf = container_of(vb,
762 struct cx23885_buffer, vb);
763 struct cx23885_buffer *prev;
764 struct cx23885_fh *fh = vq->priv_data;
765 struct cx23885_dev *dev = fh->dev;
766 struct cx23885_dmaqueue *q = &dev->vidq;
767
768 /* add jump to stopper */
769 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
770 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
771 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
772
773 if (!list_empty(&q->queued)) {
774 list_add_tail(&buf->vb.queue, &q->queued);
775 buf->vb.state = VIDEOBUF_QUEUED;
776 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
777 buf, buf->vb.i);
778
779 } else if (list_empty(&q->active)) {
780 list_add_tail(&buf->vb.queue, &q->active);
781 cx23885_start_video_dma(dev, q, buf);
782 buf->vb.state = VIDEOBUF_ACTIVE;
783 buf->count = q->count++;
784 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
785 dprintk(2, "[%p/%d] buffer_queue - first active\n",
786 buf, buf->vb.i);
787
788 } else {
789 prev = list_entry(q->active.prev, struct cx23885_buffer,
790 vb.queue);
791 if (prev->vb.width == buf->vb.width &&
792 prev->vb.height == buf->vb.height &&
793 prev->fmt == buf->fmt) {
794 list_add_tail(&buf->vb.queue, &q->active);
795 buf->vb.state = VIDEOBUF_ACTIVE;
796 buf->count = q->count++;
797 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
798 /* 64 bit bits 63-32 */
799 prev->risc.jmp[2] = cpu_to_le32(0);
800 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
801 buf, buf->vb.i);
802
803 } else {
804 list_add_tail(&buf->vb.queue, &q->queued);
805 buf->vb.state = VIDEOBUF_QUEUED;
806 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
807 buf, buf->vb.i);
808 }
809 }
810}
811
812static void buffer_release(struct videobuf_queue *q,
813 struct videobuf_buffer *vb)
814{
815 struct cx23885_buffer *buf = container_of(vb,
816 struct cx23885_buffer, vb);
817
818 cx23885_free_buffer(q, buf);
819}
820
821static struct videobuf_queue_ops cx23885_video_qops = {
822 .buf_setup = buffer_setup,
823 .buf_prepare = buffer_prepare,
824 .buf_queue = buffer_queue,
825 .buf_release = buffer_release,
826};
827
828static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
829{
830 switch (fh->type) {
831 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
832 return &fh->vidq;
833 case V4L2_BUF_TYPE_VBI_CAPTURE:
834 return &fh->vbiq;
835 default:
836 BUG();
837 return NULL;
838 }
839}
840
841static int get_resource(struct cx23885_fh *fh)
842{
843 switch (fh->type) {
844 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
845 return RESOURCE_VIDEO;
846 case V4L2_BUF_TYPE_VBI_CAPTURE:
847 return RESOURCE_VBI;
848 default:
849 BUG();
850 return 0;
851 }
852}
853
854static int video_open(struct file *file)
855{
856 struct video_device *vdev = video_devdata(file);
857 struct cx23885_dev *dev = video_drvdata(file);
858 struct cx23885_fh *fh;
859 enum v4l2_buf_type type = 0;
860 int radio = 0;
861
862 switch (vdev->vfl_type) {
863 case VFL_TYPE_GRABBER:
864 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
865 break;
866 case VFL_TYPE_VBI:
867 type = V4L2_BUF_TYPE_VBI_CAPTURE;
868 break;
869 case VFL_TYPE_RADIO:
870 radio = 1;
871 break;
872 }
873
874 dprintk(1, "open dev=%s radio=%d type=%s\n",
875 video_device_node_name(vdev), radio, v4l2_type_names[type]);
876
877 /* allocate + initialize per filehandle data */
878 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
879 if (NULL == fh)
880 return -ENOMEM;
881
882 file->private_data = fh;
883 fh->dev = dev;
884 fh->radio = radio;
885 fh->type = type;
886 fh->width = 320;
887 fh->height = 240;
888 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
889
890 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
891 &dev->pci->dev, &dev->slock,
892 V4L2_BUF_TYPE_VIDEO_CAPTURE,
893 V4L2_FIELD_INTERLACED,
894 sizeof(struct cx23885_buffer),
895 fh, NULL);
896
897 videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
898 &dev->pci->dev, &dev->slock,
899 V4L2_BUF_TYPE_VBI_CAPTURE,
900 V4L2_FIELD_SEQ_TB,
901 sizeof(struct cx23885_buffer),
902 fh, NULL);
903
904
905 dprintk(1, "post videobuf_queue_init()\n");
906
907 return 0;
908}
909
910static ssize_t video_read(struct file *file, char __user *data,
911 size_t count, loff_t *ppos)
912{
913 struct cx23885_fh *fh = file->private_data;
914
915 switch (fh->type) {
916 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
917 if (res_locked(fh->dev, RESOURCE_VIDEO))
918 return -EBUSY;
919 return videobuf_read_one(&fh->vidq, data, count, ppos,
920 file->f_flags & O_NONBLOCK);
921 case V4L2_BUF_TYPE_VBI_CAPTURE:
922 if (!res_get(fh->dev, fh, RESOURCE_VBI))
923 return -EBUSY;
924 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
925 file->f_flags & O_NONBLOCK);
926 default:
927 BUG();
928 return 0;
929 }
930}
931
932static unsigned int video_poll(struct file *file,
933 struct poll_table_struct *wait)
934{
935 struct cx23885_fh *fh = file->private_data;
936 struct cx23885_buffer *buf;
937 unsigned int rc = POLLERR;
938
939 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
940 if (!res_get(fh->dev, fh, RESOURCE_VBI))
941 return POLLERR;
942 return videobuf_poll_stream(file, &fh->vbiq, wait);
943 }
944
945 mutex_lock(&fh->vidq.vb_lock);
946 if (res_check(fh, RESOURCE_VIDEO)) {
947 /* streaming capture */
948 if (list_empty(&fh->vidq.stream))
949 goto done;
950 buf = list_entry(fh->vidq.stream.next,
951 struct cx23885_buffer, vb.stream);
952 } else {
953 /* read() capture */
954 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
955 if (NULL == buf)
956 goto done;
957 }
958 poll_wait(file, &buf->vb.done, wait);
959 if (buf->vb.state == VIDEOBUF_DONE ||
960 buf->vb.state == VIDEOBUF_ERROR)
961 rc = POLLIN|POLLRDNORM;
962 else
963 rc = 0;
964done:
965 mutex_unlock(&fh->vidq.vb_lock);
966 return rc;
967}
968
969static int video_release(struct file *file)
970{
971 struct cx23885_fh *fh = file->private_data;
972 struct cx23885_dev *dev = fh->dev;
973
974 /* turn off overlay */
975 if (res_check(fh, RESOURCE_OVERLAY)) {
976 /* FIXME */
977 res_free(dev, fh, RESOURCE_OVERLAY);
978 }
979
980 /* stop video capture */
981 if (res_check(fh, RESOURCE_VIDEO)) {
982 videobuf_queue_cancel(&fh->vidq);
983 res_free(dev, fh, RESOURCE_VIDEO);
984 }
985 if (fh->vidq.read_buf) {
986 buffer_release(&fh->vidq, fh->vidq.read_buf);
987 kfree(fh->vidq.read_buf);
988 }
989
990 /* stop vbi capture */
991 if (res_check(fh, RESOURCE_VBI)) {
992 if (fh->vbiq.streaming)
993 videobuf_streamoff(&fh->vbiq);
994 if (fh->vbiq.reading)
995 videobuf_read_stop(&fh->vbiq);
996 res_free(dev, fh, RESOURCE_VBI);
997 }
998
999 videobuf_mmap_free(&fh->vidq);
1000 videobuf_mmap_free(&fh->vbiq);
1001
1002 file->private_data = NULL;
1003 kfree(fh);
1004
1005 /* We are not putting the tuner to sleep here on exit, because
1006 * we want to use the mpeg encoder in another session to capture
1007 * tuner video. Closing this will result in no video to the encoder.
1008 */
1009
1010 return 0;
1011}
1012
1013static int video_mmap(struct file *file, struct vm_area_struct *vma)
1014{
1015 struct cx23885_fh *fh = file->private_data;
1016
1017 return videobuf_mmap_mapper(get_queue(fh), vma);
1018}
1019
1020/* ------------------------------------------------------------------ */
1021/* VIDEO CTRL IOCTLS */
1022
1023int cx23885_get_control(struct cx23885_dev *dev,
1024 struct v4l2_control *ctl)
1025{
1026 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
1027 call_all(dev, core, g_ctrl, ctl);
1028 return 0;
1029}
1030
1031int cx23885_set_control(struct cx23885_dev *dev,
1032 struct v4l2_control *ctl)
1033{
1034 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
1035 call_all(dev, core, s_ctrl, ctl);
1036
1037 return 0;
1038}
1039
1040static void init_controls(struct cx23885_dev *dev)
1041{
1042 struct v4l2_control ctrl;
1043 int i;
1044
1045 for (i = 0; i < CX23885_CTLS; i++) {
1046 ctrl.id = cx23885_ctls[i].v.id;
1047 ctrl.value = cx23885_ctls[i].v.default_value;
1048
1049 cx23885_set_control(dev, &ctrl);
1050 }
1051}
1052
1053/* ------------------------------------------------------------------ */
1054/* VIDEO IOCTLS */
1055
1056static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1057 struct v4l2_format *f)
1058{
1059 struct cx23885_fh *fh = priv;
1060
1061 f->fmt.pix.width = fh->width;
1062 f->fmt.pix.height = fh->height;
1063 f->fmt.pix.field = fh->vidq.field;
1064 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1065 f->fmt.pix.bytesperline =
1066 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1067 f->fmt.pix.sizeimage =
1068 f->fmt.pix.height * f->fmt.pix.bytesperline;
1069
1070 return 0;
1071}
1072
1073static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1074 struct v4l2_format *f)
1075{
1076 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1077 struct cx23885_fmt *fmt;
1078 enum v4l2_field field;
1079 unsigned int maxw, maxh;
1080
1081 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1082 if (NULL == fmt)
1083 return -EINVAL;
1084
1085 field = f->fmt.pix.field;
1086 maxw = norm_maxw(dev->tvnorm);
1087 maxh = norm_maxh(dev->tvnorm);
1088
1089 if (V4L2_FIELD_ANY == field) {
1090 field = (f->fmt.pix.height > maxh/2)
1091 ? V4L2_FIELD_INTERLACED
1092 : V4L2_FIELD_BOTTOM;
1093 }
1094
1095 switch (field) {
1096 case V4L2_FIELD_TOP:
1097 case V4L2_FIELD_BOTTOM:
1098 maxh = maxh / 2;
1099 break;
1100 case V4L2_FIELD_INTERLACED:
1101 break;
1102 default:
1103 return -EINVAL;
1104 }
1105
1106 f->fmt.pix.field = field;
1107 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1108 &f->fmt.pix.height, 32, maxh, 0, 0);
1109 f->fmt.pix.bytesperline =
1110 (f->fmt.pix.width * fmt->depth) >> 3;
1111 f->fmt.pix.sizeimage =
1112 f->fmt.pix.height * f->fmt.pix.bytesperline;
1113
1114 return 0;
1115}
1116
1117static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1118 struct v4l2_format *f)
1119{
1120 struct cx23885_fh *fh = priv;
1121 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1122 struct v4l2_mbus_framefmt mbus_fmt;
1123 int err;
1124
1125 dprintk(2, "%s()\n", __func__);
1126 err = vidioc_try_fmt_vid_cap(file, priv, f);
1127
1128 if (0 != err)
1129 return err;
1130 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1131 fh->width = f->fmt.pix.width;
1132 fh->height = f->fmt.pix.height;
1133 fh->vidq.field = f->fmt.pix.field;
1134 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
1135 fh->width, fh->height, fh->vidq.field);
1136 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1137 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1138 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
1139 return 0;
1140}
1141
1142static int vidioc_querycap(struct file *file, void *priv,
1143 struct v4l2_capability *cap)
1144{
1145 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1146
1147 strcpy(cap->driver, "cx23885");
1148 strlcpy(cap->card, cx23885_boards[dev->board].name,
1149 sizeof(cap->card));
1150 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1151 cap->capabilities =
1152 V4L2_CAP_VIDEO_CAPTURE |
1153 V4L2_CAP_READWRITE |
1154 V4L2_CAP_STREAMING |
1155 V4L2_CAP_VBI_CAPTURE;
1156 if (UNSET != dev->tuner_type)
1157 cap->capabilities |= V4L2_CAP_TUNER;
1158 return 0;
1159}
1160
1161static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1162 struct v4l2_fmtdesc *f)
1163{
1164 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1165 return -EINVAL;
1166
1167 strlcpy(f->description, formats[f->index].name,
1168 sizeof(f->description));
1169 f->pixelformat = formats[f->index].fourcc;
1170
1171 return 0;
1172}
1173
1174static int vidioc_reqbufs(struct file *file, void *priv,
1175 struct v4l2_requestbuffers *p)
1176{
1177 struct cx23885_fh *fh = priv;
1178 return videobuf_reqbufs(get_queue(fh), p);
1179}
1180
1181static int vidioc_querybuf(struct file *file, void *priv,
1182 struct v4l2_buffer *p)
1183{
1184 struct cx23885_fh *fh = priv;
1185 return videobuf_querybuf(get_queue(fh), p);
1186}
1187
1188static int vidioc_qbuf(struct file *file, void *priv,
1189 struct v4l2_buffer *p)
1190{
1191 struct cx23885_fh *fh = priv;
1192 return videobuf_qbuf(get_queue(fh), p);
1193}
1194
1195static int vidioc_dqbuf(struct file *file, void *priv,
1196 struct v4l2_buffer *p)
1197{
1198 struct cx23885_fh *fh = priv;
1199 return videobuf_dqbuf(get_queue(fh), p,
1200 file->f_flags & O_NONBLOCK);
1201}
1202
1203static int vidioc_streamon(struct file *file, void *priv,
1204 enum v4l2_buf_type i)
1205{
1206 struct cx23885_fh *fh = priv;
1207 struct cx23885_dev *dev = fh->dev;
1208 dprintk(1, "%s()\n", __func__);
1209
1210 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1211 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1212 return -EINVAL;
1213 if (unlikely(i != fh->type))
1214 return -EINVAL;
1215
1216 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1217 return -EBUSY;
1218
1219 /* Don't start VBI streaming unless vida streaming
1220 * has already started.
1221 */
1222 if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
1223 ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
1224 return -EINVAL;
1225
1226 return videobuf_streamon(get_queue(fh));
1227}
1228
1229static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1230{
1231 struct cx23885_fh *fh = priv;
1232 struct cx23885_dev *dev = fh->dev;
1233 int err, res;
1234 dprintk(1, "%s()\n", __func__);
1235
1236 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1237 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1238 return -EINVAL;
1239 if (i != fh->type)
1240 return -EINVAL;
1241
1242 res = get_resource(fh);
1243 err = videobuf_streamoff(get_queue(fh));
1244 if (err < 0)
1245 return err;
1246 res_free(dev, fh, res);
1247 return 0;
1248}
1249
1250static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1251{
1252 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1253 dprintk(1, "%s()\n", __func__);
1254
1255 call_all(dev, core, g_std, id);
1256
1257 return 0;
1258}
1259
1260static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1261{
1262 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1263 dprintk(1, "%s()\n", __func__);
1264
1265 mutex_lock(&dev->lock);
1266 cx23885_set_tvnorm(dev, *tvnorms);
1267 mutex_unlock(&dev->lock);
1268
1269 return 0;
1270}
1271
1272int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1273{
1274 static const char *iname[] = {
1275 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1276 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1277 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1278 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1279 [CX23885_VMUX_SVIDEO] = "S-Video",
1280 [CX23885_VMUX_COMPONENT] = "Component",
1281 [CX23885_VMUX_TELEVISION] = "Television",
1282 [CX23885_VMUX_CABLE] = "Cable TV",
1283 [CX23885_VMUX_DVB] = "DVB",
1284 [CX23885_VMUX_DEBUG] = "for debug only",
1285 };
1286 unsigned int n;
1287 dprintk(1, "%s()\n", __func__);
1288
1289 n = i->index;
1290 if (n >= MAX_CX23885_INPUT)
1291 return -EINVAL;
1292
1293 if (0 == INPUT(n)->type)
1294 return -EINVAL;
1295
1296 i->index = n;
1297 i->type = V4L2_INPUT_TYPE_CAMERA;
1298 strcpy(i->name, iname[INPUT(n)->type]);
1299 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1300 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
1301 i->type = V4L2_INPUT_TYPE_TUNER;
1302 i->std = CX23885_NORMS;
1303 }
1304
1305 /* Two selectable audio inputs for non-tv inputs */
1306 if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
1307 i->audioset = 0x3;
1308
1309 if (dev->input == n) {
1310 /* enum'd input matches our configured input.
1311 * Ask the video decoder to process the call
1312 * and give it an oppertunity to update the
1313 * status field.
1314 */
1315 call_all(dev, video, g_input_status, &i->status);
1316 }
1317
1318 return 0;
1319}
1320
1321static int vidioc_enum_input(struct file *file, void *priv,
1322 struct v4l2_input *i)
1323{
1324 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1325 dprintk(1, "%s()\n", __func__);
1326 return cx23885_enum_input(dev, i);
1327}
1328
1329int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
1330{
1331 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1332
1333 *i = dev->input;
1334 dprintk(1, "%s() returns %d\n", __func__, *i);
1335 return 0;
1336}
1337
1338static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1339{
1340 return cx23885_get_input(file, priv, i);
1341}
1342
1343int cx23885_set_input(struct file *file, void *priv, unsigned int i)
1344{
1345 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1346
1347 dprintk(1, "%s(%d)\n", __func__, i);
1348
1349 if (i >= MAX_CX23885_INPUT) {
1350 dprintk(1, "%s() -EINVAL\n", __func__);
1351 return -EINVAL;
1352 }
1353
1354 if (INPUT(i)->type == 0)
1355 return -EINVAL;
1356
1357 mutex_lock(&dev->lock);
1358 cx23885_video_mux(dev, i);
1359
1360 /* By default establish the default audio input for the card also */
1361 /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
1362 cx23885_audio_mux(dev, i);
1363 mutex_unlock(&dev->lock);
1364 return 0;
1365}
1366
1367static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1368{
1369 return cx23885_set_input(file, priv, i);
1370}
1371
1372static int vidioc_log_status(struct file *file, void *priv)
1373{
1374 struct cx23885_fh *fh = priv;
1375 struct cx23885_dev *dev = fh->dev;
1376
1377 printk(KERN_INFO
1378 "%s/0: ============ START LOG STATUS ============\n",
1379 dev->name);
1380 call_all(dev, core, log_status);
1381 printk(KERN_INFO
1382 "%s/0: ============= END LOG STATUS =============\n",
1383 dev->name);
1384 return 0;
1385}
1386
1387static int cx23885_query_audinput(struct file *file, void *priv,
1388 struct v4l2_audio *i)
1389{
1390 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1391 static const char *iname[] = {
1392 [0] = "Baseband L/R 1",
1393 [1] = "Baseband L/R 2",
1394 };
1395 unsigned int n;
1396 dprintk(1, "%s()\n", __func__);
1397
1398 n = i->index;
1399 if (n >= 2)
1400 return -EINVAL;
1401
1402 memset(i, 0, sizeof(*i));
1403 i->index = n;
1404 strcpy(i->name, iname[n]);
1405 i->capability = V4L2_AUDCAP_STEREO;
1406 i->mode = V4L2_AUDMODE_AVL;
1407 return 0;
1408
1409}
1410
1411static int vidioc_enum_audinput(struct file *file, void *priv,
1412 struct v4l2_audio *i)
1413{
1414 return cx23885_query_audinput(file, priv, i);
1415}
1416
1417static int vidioc_g_audinput(struct file *file, void *priv,
1418 struct v4l2_audio *i)
1419{
1420 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1421
1422 i->index = dev->audinput;
1423 dprintk(1, "%s(input=%d)\n", __func__, i->index);
1424
1425 return cx23885_query_audinput(file, priv, i);
1426}
1427
1428static int vidioc_s_audinput(struct file *file, void *priv,
1429 struct v4l2_audio *i)
1430{
1431 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1432 if (i->index >= 2)
1433 return -EINVAL;
1434
1435 dprintk(1, "%s(%d)\n", __func__, i->index);
1436
1437 dev->audinput = i->index;
1438
1439 /* Skip the audio defaults from the cards struct, caller wants
1440 * directly touch the audio mux hardware. */
1441 cx23885_flatiron_mux(dev, dev->audinput + 1);
1442 return 0;
1443}
1444
1445static int vidioc_queryctrl(struct file *file, void *priv,
1446 struct v4l2_queryctrl *qctrl)
1447{
1448 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1449 if (unlikely(qctrl->id == 0))
1450 return -EINVAL;
1451 return cx23885_ctrl_query(qctrl);
1452}
1453
1454static int vidioc_g_ctrl(struct file *file, void *priv,
1455 struct v4l2_control *ctl)
1456{
1457 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1458
1459 return cx23885_get_control(dev, ctl);
1460}
1461
1462static int vidioc_s_ctrl(struct file *file, void *priv,
1463 struct v4l2_control *ctl)
1464{
1465 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1466
1467 return cx23885_set_control(dev, ctl);
1468}
1469
1470static int vidioc_g_tuner(struct file *file, void *priv,
1471 struct v4l2_tuner *t)
1472{
1473 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1474
1475 if (unlikely(UNSET == dev->tuner_type))
1476 return -EINVAL;
1477 if (0 != t->index)
1478 return -EINVAL;
1479
1480 strcpy(t->name, "Television");
1481
1482 call_all(dev, tuner, g_tuner, t);
1483 return 0;
1484}
1485
1486static int vidioc_s_tuner(struct file *file, void *priv,
1487 struct v4l2_tuner *t)
1488{
1489 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1490
1491 if (UNSET == dev->tuner_type)
1492 return -EINVAL;
1493 if (0 != t->index)
1494 return -EINVAL;
1495 /* Update the A/V core */
1496 call_all(dev, tuner, s_tuner, t);
1497
1498 return 0;
1499}
1500
1501static int vidioc_g_frequency(struct file *file, void *priv,
1502 struct v4l2_frequency *f)
1503{
1504 struct cx23885_fh *fh = priv;
1505 struct cx23885_dev *dev = fh->dev;
1506
1507 if (unlikely(UNSET == dev->tuner_type))
1508 return -EINVAL;
1509
1510 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1511 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1512 f->frequency = dev->freq;
1513
1514 call_all(dev, tuner, g_frequency, f);
1515
1516 return 0;
1517}
1518
1519static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1520{
1521 struct v4l2_control ctrl;
1522
1523 if (unlikely(UNSET == dev->tuner_type))
1524 return -EINVAL;
1525 if (unlikely(f->tuner != 0))
1526 return -EINVAL;
1527
1528 mutex_lock(&dev->lock);
1529 dev->freq = f->frequency;
1530
1531 /* I need to mute audio here */
1532 ctrl.id = V4L2_CID_AUDIO_MUTE;
1533 ctrl.value = 1;
1534 cx23885_set_control(dev, &ctrl);
1535
1536 call_all(dev, tuner, s_frequency, f);
1537
1538 /* When changing channels it is required to reset TVAUDIO */
1539 msleep(100);
1540
1541 /* I need to unmute audio here */
1542 ctrl.value = 0;
1543 cx23885_set_control(dev, &ctrl);
1544
1545 mutex_unlock(&dev->lock);
1546
1547 return 0;
1548}
1549
1550static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
1551 struct v4l2_frequency *f)
1552{
1553 struct v4l2_control ctrl;
1554 struct videobuf_dvb_frontend *vfe;
1555 struct dvb_frontend *fe;
1556
1557 struct analog_parameters params = {
1558 .mode = V4L2_TUNER_ANALOG_TV,
1559 .audmode = V4L2_TUNER_MODE_STEREO,
1560 .std = dev->tvnorm,
1561 .frequency = f->frequency
1562 };
1563
1564 mutex_lock(&dev->lock);
1565 dev->freq = f->frequency;
1566
1567 /* I need to mute audio here */
1568 ctrl.id = V4L2_CID_AUDIO_MUTE;
1569 ctrl.value = 1;
1570 cx23885_set_control(dev, &ctrl);
1571
1572 /* If HVR1850 */
1573 dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1574 params.frequency, f->tuner, params.std);
1575
1576 vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
1577 if (!vfe) {
1578 mutex_unlock(&dev->lock);
1579 return -EINVAL;
1580 }
1581
1582 fe = vfe->dvb.frontend;
1583
1584 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
1585 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
1586 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
1587 fe = &dev->ts1.analog_fe;
1588
1589 if (fe && fe->ops.tuner_ops.set_analog_params) {
1590 call_all(dev, core, s_std, dev->tvnorm);
1591 fe->ops.tuner_ops.set_analog_params(fe, &params);
1592 }
1593 else
1594 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
1595
1596 /* When changing channels it is required to reset TVAUDIO */
1597 msleep(100);
1598
1599 /* I need to unmute audio here */
1600 ctrl.value = 0;
1601 cx23885_set_control(dev, &ctrl);
1602
1603 mutex_unlock(&dev->lock);
1604
1605 return 0;
1606}
1607
1608int cx23885_set_frequency(struct file *file, void *priv,
1609 struct v4l2_frequency *f)
1610{
1611 struct cx23885_fh *fh = priv;
1612 struct cx23885_dev *dev = fh->dev;
1613 int ret;
1614
1615 switch (dev->board) {
1616 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1617 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1618 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1619 ret = cx23885_set_freq_via_ops(dev, f);
1620 break;
1621 default:
1622 ret = cx23885_set_freq(dev, f);
1623 }
1624
1625 return ret;
1626}
1627
1628static int vidioc_s_frequency(struct file *file, void *priv,
1629 struct v4l2_frequency *f)
1630{
1631 return cx23885_set_frequency(file, priv, f);
1632}
1633
1634/* ----------------------------------------------------------- */
1635
1636static void cx23885_vid_timeout(unsigned long data)
1637{
1638 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1639 struct cx23885_dmaqueue *q = &dev->vidq;
1640 struct cx23885_buffer *buf;
1641 unsigned long flags;
1642
1643 spin_lock_irqsave(&dev->slock, flags);
1644 while (!list_empty(&q->active)) {
1645 buf = list_entry(q->active.next,
1646 struct cx23885_buffer, vb.queue);
1647 list_del(&buf->vb.queue);
1648 buf->vb.state = VIDEOBUF_ERROR;
1649 wake_up(&buf->vb.done);
1650 printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
1651 dev->name, buf, buf->vb.i,
1652 (unsigned long)buf->risc.dma);
1653 }
1654 cx23885_restart_video_queue(dev, q);
1655 spin_unlock_irqrestore(&dev->slock, flags);
1656}
1657
1658int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1659{
1660 u32 mask, count;
1661 int handled = 0;
1662
1663 mask = cx_read(VID_A_INT_MSK);
1664 if (0 == (status & mask))
1665 return handled;
1666
1667 cx_write(VID_A_INT_STAT, status);
1668
1669 /* risc op code error, fifo overflow or line sync detection error */
1670 if ((status & VID_BC_MSK_OPC_ERR) ||
1671 (status & VID_BC_MSK_SYNC) ||
1672 (status & VID_BC_MSK_OF)) {
1673
1674 if (status & VID_BC_MSK_OPC_ERR) {
1675 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1676 VID_BC_MSK_OPC_ERR);
1677 printk(KERN_WARNING "%s: video risc op code error\n",
1678 dev->name);
1679 cx23885_sram_channel_dump(dev,
1680 &dev->sram_channels[SRAM_CH01]);
1681 }
1682
1683 if (status & VID_BC_MSK_SYNC)
1684 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1685 "video lines miss-match\n",
1686 VID_BC_MSK_SYNC);
1687
1688 if (status & VID_BC_MSK_OF)
1689 dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1690 VID_BC_MSK_OF);
1691
1692 }
1693
1694 /* Video */
1695 if (status & VID_BC_MSK_RISCI1) {
1696 spin_lock(&dev->slock);
1697 count = cx_read(VID_A_GPCNT);
1698 cx23885_video_wakeup(dev, &dev->vidq, count);
1699 spin_unlock(&dev->slock);
1700 handled++;
1701 }
1702 if (status & VID_BC_MSK_RISCI2) {
1703 dprintk(2, "stopper video\n");
1704 spin_lock(&dev->slock);
1705 cx23885_restart_video_queue(dev, &dev->vidq);
1706 spin_unlock(&dev->slock);
1707 handled++;
1708 }
1709
1710 /* Allow the VBI framework to process it's payload */
1711 handled += cx23885_vbi_irq(dev, status);
1712
1713 return handled;
1714}
1715
1716/* ----------------------------------------------------------- */
1717/* exported stuff */
1718
1719static const struct v4l2_file_operations video_fops = {
1720 .owner = THIS_MODULE,
1721 .open = video_open,
1722 .release = video_release,
1723 .read = video_read,
1724 .poll = video_poll,
1725 .mmap = video_mmap,
1726 .ioctl = video_ioctl2,
1727};
1728
1729static const struct v4l2_ioctl_ops video_ioctl_ops = {
1730 .vidioc_querycap = vidioc_querycap,
1731 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1732 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1733 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1734 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1735 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1736 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1737 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
1738 .vidioc_reqbufs = vidioc_reqbufs,
1739 .vidioc_querybuf = vidioc_querybuf,
1740 .vidioc_qbuf = vidioc_qbuf,
1741 .vidioc_dqbuf = vidioc_dqbuf,
1742 .vidioc_s_std = vidioc_s_std,
1743 .vidioc_g_std = vidioc_g_std,
1744 .vidioc_querystd = vidioc_g_std,
1745 .vidioc_enum_input = vidioc_enum_input,
1746 .vidioc_g_input = vidioc_g_input,
1747 .vidioc_s_input = vidioc_s_input,
1748 .vidioc_log_status = vidioc_log_status,
1749 .vidioc_queryctrl = vidioc_queryctrl,
1750 .vidioc_g_ctrl = vidioc_g_ctrl,
1751 .vidioc_s_ctrl = vidioc_s_ctrl,
1752 .vidioc_streamon = vidioc_streamon,
1753 .vidioc_streamoff = vidioc_streamoff,
1754 .vidioc_g_tuner = vidioc_g_tuner,
1755 .vidioc_s_tuner = vidioc_s_tuner,
1756 .vidioc_g_frequency = vidioc_g_frequency,
1757 .vidioc_s_frequency = vidioc_s_frequency,
1758 .vidioc_g_chip_ident = cx23885_g_chip_ident,
1759#ifdef CONFIG_VIDEO_ADV_DEBUG
1760 .vidioc_g_register = cx23885_g_register,
1761 .vidioc_s_register = cx23885_s_register,
1762#endif
1763 .vidioc_enumaudio = vidioc_enum_audinput,
1764 .vidioc_g_audio = vidioc_g_audinput,
1765 .vidioc_s_audio = vidioc_s_audinput,
1766};
1767
1768static struct video_device cx23885_vbi_template;
1769static struct video_device cx23885_video_template = {
1770 .name = "cx23885-video",
1771 .fops = &video_fops,
1772 .ioctl_ops = &video_ioctl_ops,
1773 .tvnorms = CX23885_NORMS,
1774 .current_norm = V4L2_STD_NTSC_M,
1775};
1776
1777static const struct v4l2_file_operations radio_fops = {
1778 .owner = THIS_MODULE,
1779 .open = video_open,
1780 .release = video_release,
1781 .ioctl = video_ioctl2,
1782};
1783
1784
1785void cx23885_video_unregister(struct cx23885_dev *dev)
1786{
1787 dprintk(1, "%s()\n", __func__);
1788 cx23885_irq_remove(dev, 0x01);
1789
1790 if (dev->vbi_dev) {
1791 if (video_is_registered(dev->vbi_dev))
1792 video_unregister_device(dev->vbi_dev);
1793 else
1794 video_device_release(dev->vbi_dev);
1795 dev->vbi_dev = NULL;
1796 btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
1797 }
1798 if (dev->video_dev) {
1799 if (video_is_registered(dev->video_dev))
1800 video_unregister_device(dev->video_dev);
1801 else
1802 video_device_release(dev->video_dev);
1803 dev->video_dev = NULL;
1804
1805 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1806 }
1807
1808 if (dev->audio_dev)
1809 cx23885_audio_unregister(dev);
1810}
1811
1812int cx23885_video_register(struct cx23885_dev *dev)
1813{
1814 int err;
1815
1816 dprintk(1, "%s()\n", __func__);
1817 spin_lock_init(&dev->slock);
1818
1819 /* Initialize VBI template */
1820 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1821 sizeof(cx23885_vbi_template));
1822 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1823
1824 dev->tvnorm = cx23885_video_template.current_norm;
1825
1826 /* init video dma queues */
1827 INIT_LIST_HEAD(&dev->vidq.active);
1828 INIT_LIST_HEAD(&dev->vidq.queued);
1829 dev->vidq.timeout.function = cx23885_vid_timeout;
1830 dev->vidq.timeout.data = (unsigned long)dev;
1831 init_timer(&dev->vidq.timeout);
1832 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1833 VID_A_DMA_CTL, 0x11, 0x00);
1834
1835 /* init vbi dma queues */
1836 INIT_LIST_HEAD(&dev->vbiq.active);
1837 INIT_LIST_HEAD(&dev->vbiq.queued);
1838 dev->vbiq.timeout.function = cx23885_vbi_timeout;
1839 dev->vbiq.timeout.data = (unsigned long)dev;
1840 init_timer(&dev->vbiq.timeout);
1841 cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
1842 VID_A_DMA_CTL, 0x22, 0x00);
1843
1844 cx23885_irq_add_enable(dev, 0x01);
1845
1846 if ((TUNER_ABSENT != dev->tuner_type) &&
1847 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1848 struct v4l2_subdev *sd = NULL;
1849
1850 if (dev->tuner_addr)
1851 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1852 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1853 "tuner", dev->tuner_addr, NULL);
1854 else
1855 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1856 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1857 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1858 if (sd) {
1859 struct tuner_setup tun_setup;
1860
1861 memset(&tun_setup, 0, sizeof(tun_setup));
1862 tun_setup.mode_mask = T_ANALOG_TV;
1863 tun_setup.type = dev->tuner_type;
1864 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1865 tun_setup.tuner_callback = cx23885_tuner_callback;
1866
1867 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1868
1869 if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1870 struct xc2028_ctrl ctrl = {
1871 .fname = XC2028_DEFAULT_FIRMWARE,
1872 .max_len = 64
1873 };
1874 struct v4l2_priv_tun_config cfg = {
1875 .tuner = dev->tuner_type,
1876 .priv = &ctrl
1877 };
1878 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1879 }
1880 }
1881 }
1882
1883 /* register Video device */
1884 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1885 &cx23885_video_template, "video");
1886 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1887 video_nr[dev->nr]);
1888 if (err < 0) {
1889 printk(KERN_INFO "%s: can't register video device\n",
1890 dev->name);
1891 goto fail_unreg;
1892 }
1893 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1894 dev->name, video_device_node_name(dev->video_dev));
1895
1896 /* register VBI device */
1897 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1898 &cx23885_vbi_template, "vbi");
1899 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1900 vbi_nr[dev->nr]);
1901 if (err < 0) {
1902 printk(KERN_INFO "%s: can't register vbi device\n",
1903 dev->name);
1904 goto fail_unreg;
1905 }
1906 printk(KERN_INFO "%s: registered device %s\n",
1907 dev->name, video_device_node_name(dev->vbi_dev));
1908
1909 /* Register ALSA audio device */
1910 dev->audio_dev = cx23885_audio_register(dev);
1911
1912 /* initial device configuration */
1913 mutex_lock(&dev->lock);
1914 cx23885_set_tvnorm(dev, dev->tvnorm);
1915 init_controls(dev);
1916 cx23885_video_mux(dev, 0);
1917 cx23885_audio_mux(dev, 0);
1918 mutex_unlock(&dev->lock);
1919
1920 return 0;
1921
1922fail_unreg:
1923 cx23885_video_unregister(dev);
1924 return err;
1925}
1926
diff --git a/drivers/media/pci/cx23885/cx23885.h b/drivers/media/pci/cx23885/cx23885.h
new file mode 100644
index 000000000000..5d560c747e09
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23885.h
@@ -0,0 +1,653 @@
1/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/pci.h>
23#include <linux/i2c.h>
24#include <linux/kdev_t.h>
25#include <linux/slab.h>
26
27#include <media/v4l2-device.h>
28#include <media/tuner.h>
29#include <media/tveeprom.h>
30#include <media/videobuf-dma-sg.h>
31#include <media/videobuf-dvb.h>
32#include <media/rc-core.h>
33
34#include "btcx-risc.h"
35#include "cx23885-reg.h"
36#include "media/cx2341x.h"
37
38#include <linux/mutex.h>
39
40#define CX23885_VERSION "0.0.3"
41
42#define UNSET (-1U)
43
44#define CX23885_MAXBOARDS 8
45
46/* Max number of inputs by card */
47#define MAX_CX23885_INPUT 8
48#define INPUT(nr) (&cx23885_boards[dev->board].input[nr])
49#define RESOURCE_OVERLAY 1
50#define RESOURCE_VIDEO 2
51#define RESOURCE_VBI 4
52
53#define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
54
55#define CX23885_BOARD_NOAUTO UNSET
56#define CX23885_BOARD_UNKNOWN 0
57#define CX23885_BOARD_HAUPPAUGE_HVR1800lp 1
58#define CX23885_BOARD_HAUPPAUGE_HVR1800 2
59#define CX23885_BOARD_HAUPPAUGE_HVR1250 3
60#define CX23885_BOARD_DVICO_FUSIONHDTV_5_EXP 4
61#define CX23885_BOARD_HAUPPAUGE_HVR1500Q 5
62#define CX23885_BOARD_HAUPPAUGE_HVR1500 6
63#define CX23885_BOARD_HAUPPAUGE_HVR1200 7
64#define CX23885_BOARD_HAUPPAUGE_HVR1700 8
65#define CX23885_BOARD_HAUPPAUGE_HVR1400 9
66#define CX23885_BOARD_DVICO_FUSIONHDTV_7_DUAL_EXP 10
67#define CX23885_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL_EXP 11
68#define CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H 12
69#define CX23885_BOARD_COMPRO_VIDEOMATE_E650F 13
70#define CX23885_BOARD_TBS_6920 14
71#define CX23885_BOARD_TEVII_S470 15
72#define CX23885_BOARD_DVBWORLD_2005 16
73#define CX23885_BOARD_NETUP_DUAL_DVBS2_CI 17
74#define CX23885_BOARD_HAUPPAUGE_HVR1270 18
75#define CX23885_BOARD_HAUPPAUGE_HVR1275 19
76#define CX23885_BOARD_HAUPPAUGE_HVR1255 20
77#define CX23885_BOARD_HAUPPAUGE_HVR1210 21
78#define CX23885_BOARD_MYGICA_X8506 22
79#define CX23885_BOARD_MAGICPRO_PROHDTVE2 23
80#define CX23885_BOARD_HAUPPAUGE_HVR1850 24
81#define CX23885_BOARD_COMPRO_VIDEOMATE_E800 25
82#define CX23885_BOARD_HAUPPAUGE_HVR1290 26
83#define CX23885_BOARD_MYGICA_X8558PRO 27
84#define CX23885_BOARD_LEADTEK_WINFAST_PXTV1200 28
85#define CX23885_BOARD_GOTVIEW_X5_3D_HYBRID 29
86#define CX23885_BOARD_NETUP_DUAL_DVB_T_C_CI_RF 30
87#define CX23885_BOARD_LEADTEK_WINFAST_PXDVR3200_H_XC4000 31
88#define CX23885_BOARD_MPX885 32
89#define CX23885_BOARD_MYGICA_X8507 33
90#define CX23885_BOARD_TERRATEC_CINERGY_T_PCIE_DUAL 34
91#define CX23885_BOARD_TEVII_S471 35
92#define CX23885_BOARD_HAUPPAUGE_HVR1255_22111 36
93
94#define GPIO_0 0x00000001
95#define GPIO_1 0x00000002
96#define GPIO_2 0x00000004
97#define GPIO_3 0x00000008
98#define GPIO_4 0x00000010
99#define GPIO_5 0x00000020
100#define GPIO_6 0x00000040
101#define GPIO_7 0x00000080
102#define GPIO_8 0x00000100
103#define GPIO_9 0x00000200
104#define GPIO_10 0x00000400
105#define GPIO_11 0x00000800
106#define GPIO_12 0x00001000
107#define GPIO_13 0x00002000
108#define GPIO_14 0x00004000
109#define GPIO_15 0x00008000
110
111/* Currently unsupported by the driver: PAL/H, NTSC/Kr, SECAM B/G/H/LC */
112#define CX23885_NORMS (\
113 V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_443 | \
114 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
115 V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_Nc | \
116 V4L2_STD_PAL_60 | V4L2_STD_SECAM_L | V4L2_STD_SECAM_DK)
117
118struct cx23885_fmt {
119 char *name;
120 u32 fourcc; /* v4l2 format id */
121 int depth;
122 int flags;
123 u32 cxformat;
124};
125
126struct cx23885_ctrl {
127 struct v4l2_queryctrl v;
128 u32 off;
129 u32 reg;
130 u32 mask;
131 u32 shift;
132};
133
134struct cx23885_tvnorm {
135 char *name;
136 v4l2_std_id id;
137 u32 cxiformat;
138 u32 cxoformat;
139};
140
141struct cx23885_fh {
142 struct cx23885_dev *dev;
143 enum v4l2_buf_type type;
144 int radio;
145 u32 resources;
146
147 /* video overlay */
148 struct v4l2_window win;
149 struct v4l2_clip *clips;
150 unsigned int nclips;
151
152 /* video capture */
153 struct cx23885_fmt *fmt;
154 unsigned int width, height;
155
156 /* vbi capture */
157 struct videobuf_queue vidq;
158 struct videobuf_queue vbiq;
159
160 /* MPEG Encoder specifics ONLY */
161 struct videobuf_queue mpegq;
162 atomic_t v4l_reading;
163};
164
165enum cx23885_itype {
166 CX23885_VMUX_COMPOSITE1 = 1,
167 CX23885_VMUX_COMPOSITE2,
168 CX23885_VMUX_COMPOSITE3,
169 CX23885_VMUX_COMPOSITE4,
170 CX23885_VMUX_SVIDEO,
171 CX23885_VMUX_COMPONENT,
172 CX23885_VMUX_TELEVISION,
173 CX23885_VMUX_CABLE,
174 CX23885_VMUX_DVB,
175 CX23885_VMUX_DEBUG,
176 CX23885_RADIO,
177};
178
179enum cx23885_src_sel_type {
180 CX23885_SRC_SEL_EXT_656_VIDEO = 0,
181 CX23885_SRC_SEL_PARALLEL_MPEG_VIDEO
182};
183
184/* buffer for one video frame */
185struct cx23885_buffer {
186 /* common v4l buffer stuff -- must be first */
187 struct videobuf_buffer vb;
188
189 /* cx23885 specific */
190 unsigned int bpl;
191 struct btcx_riscmem risc;
192 struct cx23885_fmt *fmt;
193 u32 count;
194};
195
196struct cx23885_input {
197 enum cx23885_itype type;
198 unsigned int vmux;
199 unsigned int amux;
200 u32 gpio0, gpio1, gpio2, gpio3;
201};
202
203typedef enum {
204 CX23885_MPEG_UNDEFINED = 0,
205 CX23885_MPEG_DVB,
206 CX23885_ANALOG_VIDEO,
207 CX23885_MPEG_ENCODER,
208} port_t;
209
210struct cx23885_board {
211 char *name;
212 port_t porta, portb, portc;
213 int num_fds_portb, num_fds_portc;
214 unsigned int tuner_type;
215 unsigned int radio_type;
216 unsigned char tuner_addr;
217 unsigned char radio_addr;
218 unsigned int tuner_bus;
219
220 /* Vendors can and do run the PCIe bridge at different
221 * clock rates, driven physically by crystals on the PCBs.
222 * The core has to accommodate this. This allows the user
223 * to add new boards with new frequencys. The value is
224 * expressed in Hz.
225 *
226 * The core framework will default this value based on
227 * current designs, but it can vary.
228 */
229 u32 clk_freq;
230 struct cx23885_input input[MAX_CX23885_INPUT];
231 int ci_type; /* for NetUP */
232 /* Force bottom field first during DMA (888 workaround) */
233 u32 force_bff;
234};
235
236struct cx23885_subid {
237 u16 subvendor;
238 u16 subdevice;
239 u32 card;
240};
241
242struct cx23885_i2c {
243 struct cx23885_dev *dev;
244
245 int nr;
246
247 /* i2c i/o */
248 struct i2c_adapter i2c_adap;
249 struct i2c_client i2c_client;
250 u32 i2c_rc;
251
252 /* 885 registers used for raw addess */
253 u32 i2c_period;
254 u32 reg_ctrl;
255 u32 reg_stat;
256 u32 reg_addr;
257 u32 reg_rdata;
258 u32 reg_wdata;
259};
260
261struct cx23885_dmaqueue {
262 struct list_head active;
263 struct list_head queued;
264 struct timer_list timeout;
265 struct btcx_riscmem stopper;
266 u32 count;
267};
268
269struct cx23885_tsport {
270 struct cx23885_dev *dev;
271
272 int nr;
273 int sram_chno;
274
275 struct videobuf_dvb_frontends frontends;
276
277 /* dma queues */
278 struct cx23885_dmaqueue mpegq;
279 u32 ts_packet_size;
280 u32 ts_packet_count;
281
282 int width;
283 int height;
284
285 spinlock_t slock;
286
287 /* registers */
288 u32 reg_gpcnt;
289 u32 reg_gpcnt_ctl;
290 u32 reg_dma_ctl;
291 u32 reg_lngth;
292 u32 reg_hw_sop_ctrl;
293 u32 reg_gen_ctrl;
294 u32 reg_bd_pkt_status;
295 u32 reg_sop_status;
296 u32 reg_fifo_ovfl_stat;
297 u32 reg_vld_misc;
298 u32 reg_ts_clk_en;
299 u32 reg_ts_int_msk;
300 u32 reg_ts_int_stat;
301 u32 reg_src_sel;
302
303 /* Default register vals */
304 int pci_irqmask;
305 u32 dma_ctl_val;
306 u32 ts_int_msk_val;
307 u32 gen_ctrl_val;
308 u32 ts_clk_en_val;
309 u32 src_sel_val;
310 u32 vld_misc_val;
311 u32 hw_sop_ctrl_val;
312
313 /* Allow a single tsport to have multiple frontends */
314 u32 num_frontends;
315 void (*gate_ctrl)(struct cx23885_tsport *port, int open);
316 void *port_priv;
317
318 /* Workaround for a temp dvb_frontend that the tuner can attached to */
319 struct dvb_frontend analog_fe;
320};
321
322struct cx23885_kernel_ir {
323 struct cx23885_dev *cx;
324 char *name;
325 char *phys;
326
327 struct rc_dev *rc;
328};
329
330struct cx23885_audio_buffer {
331 unsigned int bpl;
332 struct btcx_riscmem risc;
333 struct videobuf_dmabuf dma;
334};
335
336struct cx23885_audio_dev {
337 struct cx23885_dev *dev;
338
339 struct pci_dev *pci;
340
341 struct snd_card *card;
342
343 spinlock_t lock;
344
345 atomic_t count;
346
347 unsigned int dma_size;
348 unsigned int period_size;
349 unsigned int num_periods;
350
351 struct videobuf_dmabuf *dma_risc;
352
353 struct cx23885_audio_buffer *buf;
354
355 struct snd_pcm_substream *substream;
356};
357
358struct cx23885_dev {
359 atomic_t refcount;
360 struct v4l2_device v4l2_dev;
361
362 /* pci stuff */
363 struct pci_dev *pci;
364 unsigned char pci_rev, pci_lat;
365 int pci_bus, pci_slot;
366 u32 __iomem *lmmio;
367 u8 __iomem *bmmio;
368 int pci_irqmask;
369 spinlock_t pci_irqmask_lock; /* protects mask reg too */
370 int hwrevision;
371
372 /* This valud is board specific and is used to configure the
373 * AV core so we see nice clean and stable video and audio. */
374 u32 clk_freq;
375
376 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
377 struct cx23885_i2c i2c_bus[3];
378
379 int nr;
380 struct mutex lock;
381 struct mutex gpio_lock;
382
383 /* board details */
384 unsigned int board;
385 char name[32];
386
387 struct cx23885_tsport ts1, ts2;
388
389 /* sram configuration */
390 struct sram_channel *sram_channels;
391
392 enum {
393 CX23885_BRIDGE_UNDEFINED = 0,
394 CX23885_BRIDGE_885 = 885,
395 CX23885_BRIDGE_887 = 887,
396 CX23885_BRIDGE_888 = 888,
397 } bridge;
398
399 /* Analog video */
400 u32 resources;
401 unsigned int input;
402 unsigned int audinput; /* Selectable audio input */
403 u32 tvaudio;
404 v4l2_std_id tvnorm;
405 unsigned int tuner_type;
406 unsigned char tuner_addr;
407 unsigned int tuner_bus;
408 unsigned int radio_type;
409 unsigned char radio_addr;
410 unsigned int has_radio;
411 struct v4l2_subdev *sd_cx25840;
412 struct work_struct cx25840_work;
413
414 /* Infrared */
415 struct v4l2_subdev *sd_ir;
416 struct work_struct ir_rx_work;
417 unsigned long ir_rx_notifications;
418 struct work_struct ir_tx_work;
419 unsigned long ir_tx_notifications;
420
421 struct cx23885_kernel_ir *kernel_ir;
422 atomic_t ir_input_stopping;
423
424 /* V4l */
425 u32 freq;
426 struct video_device *video_dev;
427 struct video_device *vbi_dev;
428 struct video_device *radio_dev;
429
430 struct cx23885_dmaqueue vidq;
431 struct cx23885_dmaqueue vbiq;
432 spinlock_t slock;
433
434 /* MPEG Encoder ONLY settings */
435 u32 cx23417_mailbox;
436 struct cx2341x_mpeg_params mpeg_params;
437 struct video_device *v4l_device;
438 atomic_t v4l_reader_count;
439 struct cx23885_tvnorm encodernorm;
440
441 /* Analog raw audio */
442 struct cx23885_audio_dev *audio_dev;
443
444};
445
446static inline struct cx23885_dev *to_cx23885(struct v4l2_device *v4l2_dev)
447{
448 return container_of(v4l2_dev, struct cx23885_dev, v4l2_dev);
449}
450
451#define call_all(dev, o, f, args...) \
452 v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
453
454#define CX23885_HW_888_IR (1 << 0)
455#define CX23885_HW_AV_CORE (1 << 1)
456
457#define call_hw(dev, grpid, o, f, args...) \
458 v4l2_device_call_all(&dev->v4l2_dev, grpid, o, f, ##args)
459
460extern struct v4l2_subdev *cx23885_find_hw(struct cx23885_dev *dev, u32 hw);
461
462#define SRAM_CH01 0 /* Video A */
463#define SRAM_CH02 1 /* VBI A */
464#define SRAM_CH03 2 /* Video B */
465#define SRAM_CH04 3 /* Transport via B */
466#define SRAM_CH05 4 /* VBI B */
467#define SRAM_CH06 5 /* Video C */
468#define SRAM_CH07 6 /* Transport via C */
469#define SRAM_CH08 7 /* Audio Internal A */
470#define SRAM_CH09 8 /* Audio Internal B */
471#define SRAM_CH10 9 /* Audio External */
472#define SRAM_CH11 10 /* COMB_3D_N */
473#define SRAM_CH12 11 /* Comb 3D N1 */
474#define SRAM_CH13 12 /* Comb 3D N2 */
475#define SRAM_CH14 13 /* MOE Vid */
476#define SRAM_CH15 14 /* MOE RSLT */
477
478struct sram_channel {
479 char *name;
480 u32 cmds_start;
481 u32 ctrl_start;
482 u32 cdt;
483 u32 fifo_start;
484 u32 fifo_size;
485 u32 ptr1_reg;
486 u32 ptr2_reg;
487 u32 cnt1_reg;
488 u32 cnt2_reg;
489 u32 jumponly;
490};
491
492/* ----------------------------------------------------------- */
493
494#define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
495#define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
496
497#define cx_andor(reg, mask, value) \
498 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
499 ((value) & (mask)), dev->lmmio+((reg)>>2))
500
501#define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
502#define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
503
504/* ----------------------------------------------------------- */
505/* cx23885-core.c */
506
507extern int cx23885_sram_channel_setup(struct cx23885_dev *dev,
508 struct sram_channel *ch,
509 unsigned int bpl, u32 risc);
510
511extern void cx23885_sram_channel_dump(struct cx23885_dev *dev,
512 struct sram_channel *ch);
513
514extern int cx23885_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
515 u32 reg, u32 mask, u32 value);
516
517extern int cx23885_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
518 struct scatterlist *sglist,
519 unsigned int top_offset, unsigned int bottom_offset,
520 unsigned int bpl, unsigned int padding, unsigned int lines);
521
522extern int cx23885_risc_vbibuffer(struct pci_dev *pci,
523 struct btcx_riscmem *risc, struct scatterlist *sglist,
524 unsigned int top_offset, unsigned int bottom_offset,
525 unsigned int bpl, unsigned int padding, unsigned int lines);
526
527void cx23885_cancel_buffers(struct cx23885_tsport *port);
528
529extern int cx23885_restart_queue(struct cx23885_tsport *port,
530 struct cx23885_dmaqueue *q);
531
532extern void cx23885_wakeup(struct cx23885_tsport *port,
533 struct cx23885_dmaqueue *q, u32 count);
534
535extern void cx23885_gpio_set(struct cx23885_dev *dev, u32 mask);
536extern void cx23885_gpio_clear(struct cx23885_dev *dev, u32 mask);
537extern u32 cx23885_gpio_get(struct cx23885_dev *dev, u32 mask);
538extern void cx23885_gpio_enable(struct cx23885_dev *dev, u32 mask,
539 int asoutput);
540
541extern void cx23885_irq_add_enable(struct cx23885_dev *dev, u32 mask);
542extern void cx23885_irq_enable(struct cx23885_dev *dev, u32 mask);
543extern void cx23885_irq_disable(struct cx23885_dev *dev, u32 mask);
544extern void cx23885_irq_remove(struct cx23885_dev *dev, u32 mask);
545
546/* ----------------------------------------------------------- */
547/* cx23885-cards.c */
548extern struct cx23885_board cx23885_boards[];
549extern const unsigned int cx23885_bcount;
550
551extern struct cx23885_subid cx23885_subids[];
552extern const unsigned int cx23885_idcount;
553
554extern int cx23885_tuner_callback(void *priv, int component,
555 int command, int arg);
556extern void cx23885_card_list(struct cx23885_dev *dev);
557extern int cx23885_ir_init(struct cx23885_dev *dev);
558extern void cx23885_ir_pci_int_enable(struct cx23885_dev *dev);
559extern void cx23885_ir_fini(struct cx23885_dev *dev);
560extern void cx23885_gpio_setup(struct cx23885_dev *dev);
561extern void cx23885_card_setup(struct cx23885_dev *dev);
562extern void cx23885_card_setup_pre_i2c(struct cx23885_dev *dev);
563
564extern int cx23885_dvb_register(struct cx23885_tsport *port);
565extern int cx23885_dvb_unregister(struct cx23885_tsport *port);
566
567extern int cx23885_buf_prepare(struct videobuf_queue *q,
568 struct cx23885_tsport *port,
569 struct cx23885_buffer *buf,
570 enum v4l2_field field);
571extern void cx23885_buf_queue(struct cx23885_tsport *port,
572 struct cx23885_buffer *buf);
573extern void cx23885_free_buffer(struct videobuf_queue *q,
574 struct cx23885_buffer *buf);
575
576/* ----------------------------------------------------------- */
577/* cx23885-video.c */
578/* Video */
579extern int cx23885_video_register(struct cx23885_dev *dev);
580extern void cx23885_video_unregister(struct cx23885_dev *dev);
581extern int cx23885_video_irq(struct cx23885_dev *dev, u32 status);
582extern void cx23885_video_wakeup(struct cx23885_dev *dev,
583 struct cx23885_dmaqueue *q, u32 count);
584int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i);
585int cx23885_set_input(struct file *file, void *priv, unsigned int i);
586int cx23885_get_input(struct file *file, void *priv, unsigned int *i);
587int cx23885_set_frequency(struct file *file, void *priv, struct v4l2_frequency *f);
588int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl);
589int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl);
590int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm);
591
592/* ----------------------------------------------------------- */
593/* cx23885-vbi.c */
594extern int cx23885_vbi_fmt(struct file *file, void *priv,
595 struct v4l2_format *f);
596extern void cx23885_vbi_timeout(unsigned long data);
597extern struct videobuf_queue_ops cx23885_vbi_qops;
598extern int cx23885_restart_vbi_queue(struct cx23885_dev *dev,
599 struct cx23885_dmaqueue *q);
600extern int cx23885_vbi_irq(struct cx23885_dev *dev, u32 status);
601
602/* cx23885-i2c.c */
603extern int cx23885_i2c_register(struct cx23885_i2c *bus);
604extern int cx23885_i2c_unregister(struct cx23885_i2c *bus);
605extern void cx23885_av_clk(struct cx23885_dev *dev, int enable);
606
607/* ----------------------------------------------------------- */
608/* cx23885-417.c */
609extern int cx23885_417_register(struct cx23885_dev *dev);
610extern void cx23885_417_unregister(struct cx23885_dev *dev);
611extern int cx23885_irq_417(struct cx23885_dev *dev, u32 status);
612extern void cx23885_417_check_encoder(struct cx23885_dev *dev);
613extern void cx23885_mc417_init(struct cx23885_dev *dev);
614extern int mc417_memory_read(struct cx23885_dev *dev, u32 address, u32 *value);
615extern int mc417_memory_write(struct cx23885_dev *dev, u32 address, u32 value);
616extern int mc417_register_read(struct cx23885_dev *dev,
617 u16 address, u32 *value);
618extern int mc417_register_write(struct cx23885_dev *dev,
619 u16 address, u32 value);
620extern void mc417_gpio_set(struct cx23885_dev *dev, u32 mask);
621extern void mc417_gpio_clear(struct cx23885_dev *dev, u32 mask);
622extern void mc417_gpio_enable(struct cx23885_dev *dev, u32 mask, int asoutput);
623
624/* ----------------------------------------------------------- */
625/* cx23885-alsa.c */
626extern struct cx23885_audio_dev *cx23885_audio_register(
627 struct cx23885_dev *dev);
628extern void cx23885_audio_unregister(struct cx23885_dev *dev);
629extern int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask);
630extern int cx23885_risc_databuffer(struct pci_dev *pci,
631 struct btcx_riscmem *risc,
632 struct scatterlist *sglist,
633 unsigned int bpl,
634 unsigned int lines,
635 unsigned int lpi);
636
637/* ----------------------------------------------------------- */
638/* tv norms */
639
640static inline unsigned int norm_maxw(v4l2_std_id norm)
641{
642 return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 720 : 768;
643}
644
645static inline unsigned int norm_maxh(v4l2_std_id norm)
646{
647 return (norm & V4L2_STD_625_50) ? 576 : 480;
648}
649
650static inline unsigned int norm_swidth(v4l2_std_id norm)
651{
652 return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 754 : 922;
653}
diff --git a/drivers/media/pci/cx23885/cx23888-ir.c b/drivers/media/pci/cx23885/cx23888-ir.c
new file mode 100644
index 000000000000..c2bc39c58f82
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23888-ir.c
@@ -0,0 +1,1271 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * CX23888 Integrated Consumer Infrared Controller
5 *
6 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#include <linux/kfifo.h>
25#include <linux/slab.h>
26
27#include <media/v4l2-device.h>
28#include <media/v4l2-chip-ident.h>
29#include <media/rc-core.h>
30
31#include "cx23885.h"
32
33static unsigned int ir_888_debug;
34module_param(ir_888_debug, int, 0644);
35MODULE_PARM_DESC(ir_888_debug, "enable debug messages [CX23888 IR controller]");
36
37#define CX23888_IR_REG_BASE 0x170000
38/*
39 * These CX23888 register offsets have a straightforward one to one mapping
40 * to the CX23885 register offsets of 0x200 through 0x218
41 */
42#define CX23888_IR_CNTRL_REG 0x170000
43#define CNTRL_WIN_3_3 0x00000000
44#define CNTRL_WIN_4_3 0x00000001
45#define CNTRL_WIN_3_4 0x00000002
46#define CNTRL_WIN_4_4 0x00000003
47#define CNTRL_WIN 0x00000003
48#define CNTRL_EDG_NONE 0x00000000
49#define CNTRL_EDG_FALL 0x00000004
50#define CNTRL_EDG_RISE 0x00000008
51#define CNTRL_EDG_BOTH 0x0000000C
52#define CNTRL_EDG 0x0000000C
53#define CNTRL_DMD 0x00000010
54#define CNTRL_MOD 0x00000020
55#define CNTRL_RFE 0x00000040
56#define CNTRL_TFE 0x00000080
57#define CNTRL_RXE 0x00000100
58#define CNTRL_TXE 0x00000200
59#define CNTRL_RIC 0x00000400
60#define CNTRL_TIC 0x00000800
61#define CNTRL_CPL 0x00001000
62#define CNTRL_LBM 0x00002000
63#define CNTRL_R 0x00004000
64/* CX23888 specific control flag */
65#define CNTRL_IVO 0x00008000
66
67#define CX23888_IR_TXCLK_REG 0x170004
68#define TXCLK_TCD 0x0000FFFF
69
70#define CX23888_IR_RXCLK_REG 0x170008
71#define RXCLK_RCD 0x0000FFFF
72
73#define CX23888_IR_CDUTY_REG 0x17000C
74#define CDUTY_CDC 0x0000000F
75
76#define CX23888_IR_STATS_REG 0x170010
77#define STATS_RTO 0x00000001
78#define STATS_ROR 0x00000002
79#define STATS_RBY 0x00000004
80#define STATS_TBY 0x00000008
81#define STATS_RSR 0x00000010
82#define STATS_TSR 0x00000020
83
84#define CX23888_IR_IRQEN_REG 0x170014
85#define IRQEN_RTE 0x00000001
86#define IRQEN_ROE 0x00000002
87#define IRQEN_RSE 0x00000010
88#define IRQEN_TSE 0x00000020
89
90#define CX23888_IR_FILTR_REG 0x170018
91#define FILTR_LPF 0x0000FFFF
92
93/* This register doesn't follow the pattern; it's 0x23C on a CX23885 */
94#define CX23888_IR_FIFO_REG 0x170040
95#define FIFO_RXTX 0x0000FFFF
96#define FIFO_RXTX_LVL 0x00010000
97#define FIFO_RXTX_RTO 0x0001FFFF
98#define FIFO_RX_NDV 0x00020000
99#define FIFO_RX_DEPTH 8
100#define FIFO_TX_DEPTH 8
101
102/* CX23888 unique registers */
103#define CX23888_IR_SEEDP_REG 0x17001C
104#define CX23888_IR_TIMOL_REG 0x170020
105#define CX23888_IR_WAKE0_REG 0x170024
106#define CX23888_IR_WAKE1_REG 0x170028
107#define CX23888_IR_WAKE2_REG 0x17002C
108#define CX23888_IR_MASK0_REG 0x170030
109#define CX23888_IR_MASK1_REG 0x170034
110#define CX23888_IR_MAKS2_REG 0x170038
111#define CX23888_IR_DPIPG_REG 0x17003C
112#define CX23888_IR_LEARN_REG 0x170044
113
114#define CX23888_VIDCLK_FREQ 108000000 /* 108 MHz, BT.656 */
115#define CX23888_IR_REFCLK_FREQ (CX23888_VIDCLK_FREQ / 2)
116
117/*
118 * We use this union internally for convenience, but callers to tx_write
119 * and rx_read will be expecting records of type struct ir_raw_event.
120 * Always ensure the size of this union is dictated by struct ir_raw_event.
121 */
122union cx23888_ir_fifo_rec {
123 u32 hw_fifo_data;
124 struct ir_raw_event ir_core_data;
125};
126
127#define CX23888_IR_RX_KFIFO_SIZE (256 * sizeof(union cx23888_ir_fifo_rec))
128#define CX23888_IR_TX_KFIFO_SIZE (256 * sizeof(union cx23888_ir_fifo_rec))
129
130struct cx23888_ir_state {
131 struct v4l2_subdev sd;
132 struct cx23885_dev *dev;
133 u32 id;
134 u32 rev;
135
136 struct v4l2_subdev_ir_parameters rx_params;
137 struct mutex rx_params_lock;
138 atomic_t rxclk_divider;
139 atomic_t rx_invert;
140
141 struct kfifo rx_kfifo;
142 spinlock_t rx_kfifo_lock;
143
144 struct v4l2_subdev_ir_parameters tx_params;
145 struct mutex tx_params_lock;
146 atomic_t txclk_divider;
147};
148
149static inline struct cx23888_ir_state *to_state(struct v4l2_subdev *sd)
150{
151 return v4l2_get_subdevdata(sd);
152}
153
154/*
155 * IR register block read and write functions
156 */
157static
158inline int cx23888_ir_write4(struct cx23885_dev *dev, u32 addr, u32 value)
159{
160 cx_write(addr, value);
161 return 0;
162}
163
164static inline u32 cx23888_ir_read4(struct cx23885_dev *dev, u32 addr)
165{
166 return cx_read(addr);
167}
168
169static inline int cx23888_ir_and_or4(struct cx23885_dev *dev, u32 addr,
170 u32 and_mask, u32 or_value)
171{
172 cx_andor(addr, ~and_mask, or_value);
173 return 0;
174}
175
176/*
177 * Rx and Tx Clock Divider register computations
178 *
179 * Note the largest clock divider value of 0xffff corresponds to:
180 * (0xffff + 1) * 1000 / 108/2 MHz = 1,213,629.629... ns
181 * which fits in 21 bits, so we'll use unsigned int for time arguments.
182 */
183static inline u16 count_to_clock_divider(unsigned int d)
184{
185 if (d > RXCLK_RCD + 1)
186 d = RXCLK_RCD;
187 else if (d < 2)
188 d = 1;
189 else
190 d--;
191 return (u16) d;
192}
193
194static inline u16 ns_to_clock_divider(unsigned int ns)
195{
196 return count_to_clock_divider(
197 DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ / 1000000 * ns, 1000));
198}
199
200static inline unsigned int clock_divider_to_ns(unsigned int divider)
201{
202 /* Period of the Rx or Tx clock in ns */
203 return DIV_ROUND_CLOSEST((divider + 1) * 1000,
204 CX23888_IR_REFCLK_FREQ / 1000000);
205}
206
207static inline u16 carrier_freq_to_clock_divider(unsigned int freq)
208{
209 return count_to_clock_divider(
210 DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, freq * 16));
211}
212
213static inline unsigned int clock_divider_to_carrier_freq(unsigned int divider)
214{
215 return DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, (divider + 1) * 16);
216}
217
218static inline u16 freq_to_clock_divider(unsigned int freq,
219 unsigned int rollovers)
220{
221 return count_to_clock_divider(
222 DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ, freq * rollovers));
223}
224
225static inline unsigned int clock_divider_to_freq(unsigned int divider,
226 unsigned int rollovers)
227{
228 return DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ,
229 (divider + 1) * rollovers);
230}
231
232/*
233 * Low Pass Filter register calculations
234 *
235 * Note the largest count value of 0xffff corresponds to:
236 * 0xffff * 1000 / 108/2 MHz = 1,213,611.11... ns
237 * which fits in 21 bits, so we'll use unsigned int for time arguments.
238 */
239static inline u16 count_to_lpf_count(unsigned int d)
240{
241 if (d > FILTR_LPF)
242 d = FILTR_LPF;
243 else if (d < 4)
244 d = 0;
245 return (u16) d;
246}
247
248static inline u16 ns_to_lpf_count(unsigned int ns)
249{
250 return count_to_lpf_count(
251 DIV_ROUND_CLOSEST(CX23888_IR_REFCLK_FREQ / 1000000 * ns, 1000));
252}
253
254static inline unsigned int lpf_count_to_ns(unsigned int count)
255{
256 /* Duration of the Low Pass Filter rejection window in ns */
257 return DIV_ROUND_CLOSEST(count * 1000,
258 CX23888_IR_REFCLK_FREQ / 1000000);
259}
260
261static inline unsigned int lpf_count_to_us(unsigned int count)
262{
263 /* Duration of the Low Pass Filter rejection window in us */
264 return DIV_ROUND_CLOSEST(count, CX23888_IR_REFCLK_FREQ / 1000000);
265}
266
267/*
268 * FIFO register pulse width count compuations
269 */
270static u32 clock_divider_to_resolution(u16 divider)
271{
272 /*
273 * Resolution is the duration of 1 tick of the readable portion of
274 * of the pulse width counter as read from the FIFO. The two lsb's are
275 * not readable, hence the << 2. This function returns ns.
276 */
277 return DIV_ROUND_CLOSEST((1 << 2) * ((u32) divider + 1) * 1000,
278 CX23888_IR_REFCLK_FREQ / 1000000);
279}
280
281static u64 pulse_width_count_to_ns(u16 count, u16 divider)
282{
283 u64 n;
284 u32 rem;
285
286 /*
287 * The 2 lsb's of the pulse width timer count are not readable, hence
288 * the (count << 2) | 0x3
289 */
290 n = (((u64) count << 2) | 0x3) * (divider + 1) * 1000; /* millicycles */
291 rem = do_div(n, CX23888_IR_REFCLK_FREQ / 1000000); /* / MHz => ns */
292 if (rem >= CX23888_IR_REFCLK_FREQ / 1000000 / 2)
293 n++;
294 return n;
295}
296
297static unsigned int pulse_width_count_to_us(u16 count, u16 divider)
298{
299 u64 n;
300 u32 rem;
301
302 /*
303 * The 2 lsb's of the pulse width timer count are not readable, hence
304 * the (count << 2) | 0x3
305 */
306 n = (((u64) count << 2) | 0x3) * (divider + 1); /* cycles */
307 rem = do_div(n, CX23888_IR_REFCLK_FREQ / 1000000); /* / MHz => us */
308 if (rem >= CX23888_IR_REFCLK_FREQ / 1000000 / 2)
309 n++;
310 return (unsigned int) n;
311}
312
313/*
314 * Pulse Clocks computations: Combined Pulse Width Count & Rx Clock Counts
315 *
316 * The total pulse clock count is an 18 bit pulse width timer count as the most
317 * significant part and (up to) 16 bit clock divider count as a modulus.
318 * When the Rx clock divider ticks down to 0, it increments the 18 bit pulse
319 * width timer count's least significant bit.
320 */
321static u64 ns_to_pulse_clocks(u32 ns)
322{
323 u64 clocks;
324 u32 rem;
325 clocks = CX23888_IR_REFCLK_FREQ / 1000000 * (u64) ns; /* millicycles */
326 rem = do_div(clocks, 1000); /* /1000 = cycles */
327 if (rem >= 1000 / 2)
328 clocks++;
329 return clocks;
330}
331
332static u16 pulse_clocks_to_clock_divider(u64 count)
333{
334 do_div(count, (FIFO_RXTX << 2) | 0x3);
335
336 /* net result needs to be rounded down and decremented by 1 */
337 if (count > RXCLK_RCD + 1)
338 count = RXCLK_RCD;
339 else if (count < 2)
340 count = 1;
341 else
342 count--;
343 return (u16) count;
344}
345
346/*
347 * IR Control Register helpers
348 */
349enum tx_fifo_watermark {
350 TX_FIFO_HALF_EMPTY = 0,
351 TX_FIFO_EMPTY = CNTRL_TIC,
352};
353
354enum rx_fifo_watermark {
355 RX_FIFO_HALF_FULL = 0,
356 RX_FIFO_NOT_EMPTY = CNTRL_RIC,
357};
358
359static inline void control_tx_irq_watermark(struct cx23885_dev *dev,
360 enum tx_fifo_watermark level)
361{
362 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_TIC, level);
363}
364
365static inline void control_rx_irq_watermark(struct cx23885_dev *dev,
366 enum rx_fifo_watermark level)
367{
368 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_RIC, level);
369}
370
371static inline void control_tx_enable(struct cx23885_dev *dev, bool enable)
372{
373 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~(CNTRL_TXE | CNTRL_TFE),
374 enable ? (CNTRL_TXE | CNTRL_TFE) : 0);
375}
376
377static inline void control_rx_enable(struct cx23885_dev *dev, bool enable)
378{
379 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~(CNTRL_RXE | CNTRL_RFE),
380 enable ? (CNTRL_RXE | CNTRL_RFE) : 0);
381}
382
383static inline void control_tx_modulation_enable(struct cx23885_dev *dev,
384 bool enable)
385{
386 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_MOD,
387 enable ? CNTRL_MOD : 0);
388}
389
390static inline void control_rx_demodulation_enable(struct cx23885_dev *dev,
391 bool enable)
392{
393 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_DMD,
394 enable ? CNTRL_DMD : 0);
395}
396
397static inline void control_rx_s_edge_detection(struct cx23885_dev *dev,
398 u32 edge_types)
399{
400 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_EDG_BOTH,
401 edge_types & CNTRL_EDG_BOTH);
402}
403
404static void control_rx_s_carrier_window(struct cx23885_dev *dev,
405 unsigned int carrier,
406 unsigned int *carrier_range_low,
407 unsigned int *carrier_range_high)
408{
409 u32 v;
410 unsigned int c16 = carrier * 16;
411
412 if (*carrier_range_low < DIV_ROUND_CLOSEST(c16, 16 + 3)) {
413 v = CNTRL_WIN_3_4;
414 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 4);
415 } else {
416 v = CNTRL_WIN_3_3;
417 *carrier_range_low = DIV_ROUND_CLOSEST(c16, 16 + 3);
418 }
419
420 if (*carrier_range_high > DIV_ROUND_CLOSEST(c16, 16 - 3)) {
421 v |= CNTRL_WIN_4_3;
422 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 4);
423 } else {
424 v |= CNTRL_WIN_3_3;
425 *carrier_range_high = DIV_ROUND_CLOSEST(c16, 16 - 3);
426 }
427 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_WIN, v);
428}
429
430static inline void control_tx_polarity_invert(struct cx23885_dev *dev,
431 bool invert)
432{
433 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_CPL,
434 invert ? CNTRL_CPL : 0);
435}
436
437static inline void control_tx_level_invert(struct cx23885_dev *dev,
438 bool invert)
439{
440 cx23888_ir_and_or4(dev, CX23888_IR_CNTRL_REG, ~CNTRL_IVO,
441 invert ? CNTRL_IVO : 0);
442}
443
444/*
445 * IR Rx & Tx Clock Register helpers
446 */
447static unsigned int txclk_tx_s_carrier(struct cx23885_dev *dev,
448 unsigned int freq,
449 u16 *divider)
450{
451 *divider = carrier_freq_to_clock_divider(freq);
452 cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider);
453 return clock_divider_to_carrier_freq(*divider);
454}
455
456static unsigned int rxclk_rx_s_carrier(struct cx23885_dev *dev,
457 unsigned int freq,
458 u16 *divider)
459{
460 *divider = carrier_freq_to_clock_divider(freq);
461 cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider);
462 return clock_divider_to_carrier_freq(*divider);
463}
464
465static u32 txclk_tx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,
466 u16 *divider)
467{
468 u64 pulse_clocks;
469
470 if (ns > IR_MAX_DURATION)
471 ns = IR_MAX_DURATION;
472 pulse_clocks = ns_to_pulse_clocks(ns);
473 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
474 cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, *divider);
475 return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
476}
477
478static u32 rxclk_rx_s_max_pulse_width(struct cx23885_dev *dev, u32 ns,
479 u16 *divider)
480{
481 u64 pulse_clocks;
482
483 if (ns > IR_MAX_DURATION)
484 ns = IR_MAX_DURATION;
485 pulse_clocks = ns_to_pulse_clocks(ns);
486 *divider = pulse_clocks_to_clock_divider(pulse_clocks);
487 cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, *divider);
488 return (u32) pulse_width_count_to_ns(FIFO_RXTX, *divider);
489}
490
491/*
492 * IR Tx Carrier Duty Cycle register helpers
493 */
494static unsigned int cduty_tx_s_duty_cycle(struct cx23885_dev *dev,
495 unsigned int duty_cycle)
496{
497 u32 n;
498 n = DIV_ROUND_CLOSEST(duty_cycle * 100, 625); /* 16ths of 100% */
499 if (n != 0)
500 n--;
501 if (n > 15)
502 n = 15;
503 cx23888_ir_write4(dev, CX23888_IR_CDUTY_REG, n);
504 return DIV_ROUND_CLOSEST((n + 1) * 100, 16);
505}
506
507/*
508 * IR Filter Register helpers
509 */
510static u32 filter_rx_s_min_width(struct cx23885_dev *dev, u32 min_width_ns)
511{
512 u32 count = ns_to_lpf_count(min_width_ns);
513 cx23888_ir_write4(dev, CX23888_IR_FILTR_REG, count);
514 return lpf_count_to_ns(count);
515}
516
517/*
518 * IR IRQ Enable Register helpers
519 */
520static inline void irqenable_rx(struct cx23885_dev *dev, u32 mask)
521{
522 mask &= (IRQEN_RTE | IRQEN_ROE | IRQEN_RSE);
523 cx23888_ir_and_or4(dev, CX23888_IR_IRQEN_REG,
524 ~(IRQEN_RTE | IRQEN_ROE | IRQEN_RSE), mask);
525}
526
527static inline void irqenable_tx(struct cx23885_dev *dev, u32 mask)
528{
529 mask &= IRQEN_TSE;
530 cx23888_ir_and_or4(dev, CX23888_IR_IRQEN_REG, ~IRQEN_TSE, mask);
531}
532
533/*
534 * V4L2 Subdevice IR Ops
535 */
536static int cx23888_ir_irq_handler(struct v4l2_subdev *sd, u32 status,
537 bool *handled)
538{
539 struct cx23888_ir_state *state = to_state(sd);
540 struct cx23885_dev *dev = state->dev;
541 unsigned long flags;
542
543 u32 cntrl = cx23888_ir_read4(dev, CX23888_IR_CNTRL_REG);
544 u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);
545 u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);
546
547 union cx23888_ir_fifo_rec rx_data[FIFO_RX_DEPTH];
548 unsigned int i, j, k;
549 u32 events, v;
550 int tsr, rsr, rto, ror, tse, rse, rte, roe, kror;
551
552 tsr = stats & STATS_TSR; /* Tx FIFO Service Request */
553 rsr = stats & STATS_RSR; /* Rx FIFO Service Request */
554 rto = stats & STATS_RTO; /* Rx Pulse Width Timer Time Out */
555 ror = stats & STATS_ROR; /* Rx FIFO Over Run */
556
557 tse = irqen & IRQEN_TSE; /* Tx FIFO Service Request IRQ Enable */
558 rse = irqen & IRQEN_RSE; /* Rx FIFO Service Reuqest IRQ Enable */
559 rte = irqen & IRQEN_RTE; /* Rx Pulse Width Timer Time Out IRQ Enable */
560 roe = irqen & IRQEN_ROE; /* Rx FIFO Over Run IRQ Enable */
561
562 *handled = false;
563 v4l2_dbg(2, ir_888_debug, sd, "IRQ Status: %s %s %s %s %s %s\n",
564 tsr ? "tsr" : " ", rsr ? "rsr" : " ",
565 rto ? "rto" : " ", ror ? "ror" : " ",
566 stats & STATS_TBY ? "tby" : " ",
567 stats & STATS_RBY ? "rby" : " ");
568
569 v4l2_dbg(2, ir_888_debug, sd, "IRQ Enables: %s %s %s %s\n",
570 tse ? "tse" : " ", rse ? "rse" : " ",
571 rte ? "rte" : " ", roe ? "roe" : " ");
572
573 /*
574 * Transmitter interrupt service
575 */
576 if (tse && tsr) {
577 /*
578 * TODO:
579 * Check the watermark threshold setting
580 * Pull FIFO_TX_DEPTH or FIFO_TX_DEPTH/2 entries from tx_kfifo
581 * Push the data to the hardware FIFO.
582 * If there was nothing more to send in the tx_kfifo, disable
583 * the TSR IRQ and notify the v4l2_device.
584 * If there was something in the tx_kfifo, check the tx_kfifo
585 * level and notify the v4l2_device, if it is low.
586 */
587 /* For now, inhibit TSR interrupt until Tx is implemented */
588 irqenable_tx(dev, 0);
589 events = V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ;
590 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_TX_NOTIFY, &events);
591 *handled = true;
592 }
593
594 /*
595 * Receiver interrupt service
596 */
597 kror = 0;
598 if ((rse && rsr) || (rte && rto)) {
599 /*
600 * Receive data on RSR to clear the STATS_RSR.
601 * Receive data on RTO, since we may not have yet hit the RSR
602 * watermark when we receive the RTO.
603 */
604 for (i = 0, v = FIFO_RX_NDV;
605 (v & FIFO_RX_NDV) && !kror; i = 0) {
606 for (j = 0;
607 (v & FIFO_RX_NDV) && j < FIFO_RX_DEPTH; j++) {
608 v = cx23888_ir_read4(dev, CX23888_IR_FIFO_REG);
609 rx_data[i].hw_fifo_data = v & ~FIFO_RX_NDV;
610 i++;
611 }
612 if (i == 0)
613 break;
614 j = i * sizeof(union cx23888_ir_fifo_rec);
615 k = kfifo_in_locked(&state->rx_kfifo,
616 (unsigned char *) rx_data, j,
617 &state->rx_kfifo_lock);
618 if (k != j)
619 kror++; /* rx_kfifo over run */
620 }
621 *handled = true;
622 }
623
624 events = 0;
625 v = 0;
626 if (kror) {
627 events |= V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN;
628 v4l2_err(sd, "IR receiver software FIFO overrun\n");
629 }
630 if (roe && ror) {
631 /*
632 * The RX FIFO Enable (CNTRL_RFE) must be toggled to clear
633 * the Rx FIFO Over Run status (STATS_ROR)
634 */
635 v |= CNTRL_RFE;
636 events |= V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN;
637 v4l2_err(sd, "IR receiver hardware FIFO overrun\n");
638 }
639 if (rte && rto) {
640 /*
641 * The IR Receiver Enable (CNTRL_RXE) must be toggled to clear
642 * the Rx Pulse Width Timer Time Out (STATS_RTO)
643 */
644 v |= CNTRL_RXE;
645 events |= V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED;
646 }
647 if (v) {
648 /* Clear STATS_ROR & STATS_RTO as needed by reseting hardware */
649 cx23888_ir_write4(dev, CX23888_IR_CNTRL_REG, cntrl & ~v);
650 cx23888_ir_write4(dev, CX23888_IR_CNTRL_REG, cntrl);
651 *handled = true;
652 }
653
654 spin_lock_irqsave(&state->rx_kfifo_lock, flags);
655 if (kfifo_len(&state->rx_kfifo) >= CX23888_IR_RX_KFIFO_SIZE / 2)
656 events |= V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ;
657 spin_unlock_irqrestore(&state->rx_kfifo_lock, flags);
658
659 if (events)
660 v4l2_subdev_notify(sd, V4L2_SUBDEV_IR_RX_NOTIFY, &events);
661 return 0;
662}
663
664/* Receiver */
665static int cx23888_ir_rx_read(struct v4l2_subdev *sd, u8 *buf, size_t count,
666 ssize_t *num)
667{
668 struct cx23888_ir_state *state = to_state(sd);
669 bool invert = (bool) atomic_read(&state->rx_invert);
670 u16 divider = (u16) atomic_read(&state->rxclk_divider);
671
672 unsigned int i, n;
673 union cx23888_ir_fifo_rec *p;
674 unsigned u, v, w;
675
676 n = count / sizeof(union cx23888_ir_fifo_rec)
677 * sizeof(union cx23888_ir_fifo_rec);
678 if (n == 0) {
679 *num = 0;
680 return 0;
681 }
682
683 n = kfifo_out_locked(&state->rx_kfifo, buf, n, &state->rx_kfifo_lock);
684
685 n /= sizeof(union cx23888_ir_fifo_rec);
686 *num = n * sizeof(union cx23888_ir_fifo_rec);
687
688 for (p = (union cx23888_ir_fifo_rec *) buf, i = 0; i < n; p++, i++) {
689
690 if ((p->hw_fifo_data & FIFO_RXTX_RTO) == FIFO_RXTX_RTO) {
691 /* Assume RTO was because of no IR light input */
692 u = 0;
693 w = 1;
694 } else {
695 u = (p->hw_fifo_data & FIFO_RXTX_LVL) ? 1 : 0;
696 if (invert)
697 u = u ? 0 : 1;
698 w = 0;
699 }
700
701 v = (unsigned) pulse_width_count_to_ns(
702 (u16) (p->hw_fifo_data & FIFO_RXTX), divider);
703 if (v > IR_MAX_DURATION)
704 v = IR_MAX_DURATION;
705
706 init_ir_raw_event(&p->ir_core_data);
707 p->ir_core_data.pulse = u;
708 p->ir_core_data.duration = v;
709 p->ir_core_data.timeout = w;
710
711 v4l2_dbg(2, ir_888_debug, sd, "rx read: %10u ns %s %s\n",
712 v, u ? "mark" : "space", w ? "(timed out)" : "");
713 if (w)
714 v4l2_dbg(2, ir_888_debug, sd, "rx read: end of rx\n");
715 }
716 return 0;
717}
718
719static int cx23888_ir_rx_g_parameters(struct v4l2_subdev *sd,
720 struct v4l2_subdev_ir_parameters *p)
721{
722 struct cx23888_ir_state *state = to_state(sd);
723 mutex_lock(&state->rx_params_lock);
724 memcpy(p, &state->rx_params, sizeof(struct v4l2_subdev_ir_parameters));
725 mutex_unlock(&state->rx_params_lock);
726 return 0;
727}
728
729static int cx23888_ir_rx_shutdown(struct v4l2_subdev *sd)
730{
731 struct cx23888_ir_state *state = to_state(sd);
732 struct cx23885_dev *dev = state->dev;
733
734 mutex_lock(&state->rx_params_lock);
735
736 /* Disable or slow down all IR Rx circuits and counters */
737 irqenable_rx(dev, 0);
738 control_rx_enable(dev, false);
739 control_rx_demodulation_enable(dev, false);
740 control_rx_s_edge_detection(dev, CNTRL_EDG_NONE);
741 filter_rx_s_min_width(dev, 0);
742 cx23888_ir_write4(dev, CX23888_IR_RXCLK_REG, RXCLK_RCD);
743
744 state->rx_params.shutdown = true;
745
746 mutex_unlock(&state->rx_params_lock);
747 return 0;
748}
749
750static int cx23888_ir_rx_s_parameters(struct v4l2_subdev *sd,
751 struct v4l2_subdev_ir_parameters *p)
752{
753 struct cx23888_ir_state *state = to_state(sd);
754 struct cx23885_dev *dev = state->dev;
755 struct v4l2_subdev_ir_parameters *o = &state->rx_params;
756 u16 rxclk_divider;
757
758 if (p->shutdown)
759 return cx23888_ir_rx_shutdown(sd);
760
761 if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
762 return -ENOSYS;
763
764 mutex_lock(&state->rx_params_lock);
765
766 o->shutdown = p->shutdown;
767
768 o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
769
770 o->bytes_per_data_element = p->bytes_per_data_element
771 = sizeof(union cx23888_ir_fifo_rec);
772
773 /* Before we tweak the hardware, we have to disable the receiver */
774 irqenable_rx(dev, 0);
775 control_rx_enable(dev, false);
776
777 control_rx_demodulation_enable(dev, p->modulation);
778 o->modulation = p->modulation;
779
780 if (p->modulation) {
781 p->carrier_freq = rxclk_rx_s_carrier(dev, p->carrier_freq,
782 &rxclk_divider);
783
784 o->carrier_freq = p->carrier_freq;
785
786 o->duty_cycle = p->duty_cycle = 50;
787
788 control_rx_s_carrier_window(dev, p->carrier_freq,
789 &p->carrier_range_lower,
790 &p->carrier_range_upper);
791 o->carrier_range_lower = p->carrier_range_lower;
792 o->carrier_range_upper = p->carrier_range_upper;
793
794 p->max_pulse_width =
795 (u32) pulse_width_count_to_ns(FIFO_RXTX, rxclk_divider);
796 } else {
797 p->max_pulse_width =
798 rxclk_rx_s_max_pulse_width(dev, p->max_pulse_width,
799 &rxclk_divider);
800 }
801 o->max_pulse_width = p->max_pulse_width;
802 atomic_set(&state->rxclk_divider, rxclk_divider);
803
804 p->noise_filter_min_width =
805 filter_rx_s_min_width(dev, p->noise_filter_min_width);
806 o->noise_filter_min_width = p->noise_filter_min_width;
807
808 p->resolution = clock_divider_to_resolution(rxclk_divider);
809 o->resolution = p->resolution;
810
811 /* FIXME - make this dependent on resolution for better performance */
812 control_rx_irq_watermark(dev, RX_FIFO_HALF_FULL);
813
814 control_rx_s_edge_detection(dev, CNTRL_EDG_BOTH);
815
816 o->invert_level = p->invert_level;
817 atomic_set(&state->rx_invert, p->invert_level);
818
819 o->interrupt_enable = p->interrupt_enable;
820 o->enable = p->enable;
821 if (p->enable) {
822 unsigned long flags;
823
824 spin_lock_irqsave(&state->rx_kfifo_lock, flags);
825 kfifo_reset(&state->rx_kfifo);
826 /* reset tx_fifo too if there is one... */
827 spin_unlock_irqrestore(&state->rx_kfifo_lock, flags);
828 if (p->interrupt_enable)
829 irqenable_rx(dev, IRQEN_RSE | IRQEN_RTE | IRQEN_ROE);
830 control_rx_enable(dev, p->enable);
831 }
832
833 mutex_unlock(&state->rx_params_lock);
834 return 0;
835}
836
837/* Transmitter */
838static int cx23888_ir_tx_write(struct v4l2_subdev *sd, u8 *buf, size_t count,
839 ssize_t *num)
840{
841 struct cx23888_ir_state *state = to_state(sd);
842 struct cx23885_dev *dev = state->dev;
843 /* For now enable the Tx FIFO Service interrupt & pretend we did work */
844 irqenable_tx(dev, IRQEN_TSE);
845 *num = count;
846 return 0;
847}
848
849static int cx23888_ir_tx_g_parameters(struct v4l2_subdev *sd,
850 struct v4l2_subdev_ir_parameters *p)
851{
852 struct cx23888_ir_state *state = to_state(sd);
853 mutex_lock(&state->tx_params_lock);
854 memcpy(p, &state->tx_params, sizeof(struct v4l2_subdev_ir_parameters));
855 mutex_unlock(&state->tx_params_lock);
856 return 0;
857}
858
859static int cx23888_ir_tx_shutdown(struct v4l2_subdev *sd)
860{
861 struct cx23888_ir_state *state = to_state(sd);
862 struct cx23885_dev *dev = state->dev;
863
864 mutex_lock(&state->tx_params_lock);
865
866 /* Disable or slow down all IR Tx circuits and counters */
867 irqenable_tx(dev, 0);
868 control_tx_enable(dev, false);
869 control_tx_modulation_enable(dev, false);
870 cx23888_ir_write4(dev, CX23888_IR_TXCLK_REG, TXCLK_TCD);
871
872 state->tx_params.shutdown = true;
873
874 mutex_unlock(&state->tx_params_lock);
875 return 0;
876}
877
878static int cx23888_ir_tx_s_parameters(struct v4l2_subdev *sd,
879 struct v4l2_subdev_ir_parameters *p)
880{
881 struct cx23888_ir_state *state = to_state(sd);
882 struct cx23885_dev *dev = state->dev;
883 struct v4l2_subdev_ir_parameters *o = &state->tx_params;
884 u16 txclk_divider;
885
886 if (p->shutdown)
887 return cx23888_ir_tx_shutdown(sd);
888
889 if (p->mode != V4L2_SUBDEV_IR_MODE_PULSE_WIDTH)
890 return -ENOSYS;
891
892 mutex_lock(&state->tx_params_lock);
893
894 o->shutdown = p->shutdown;
895
896 o->mode = p->mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH;
897
898 o->bytes_per_data_element = p->bytes_per_data_element
899 = sizeof(union cx23888_ir_fifo_rec);
900
901 /* Before we tweak the hardware, we have to disable the transmitter */
902 irqenable_tx(dev, 0);
903 control_tx_enable(dev, false);
904
905 control_tx_modulation_enable(dev, p->modulation);
906 o->modulation = p->modulation;
907
908 if (p->modulation) {
909 p->carrier_freq = txclk_tx_s_carrier(dev, p->carrier_freq,
910 &txclk_divider);
911 o->carrier_freq = p->carrier_freq;
912
913 p->duty_cycle = cduty_tx_s_duty_cycle(dev, p->duty_cycle);
914 o->duty_cycle = p->duty_cycle;
915
916 p->max_pulse_width =
917 (u32) pulse_width_count_to_ns(FIFO_RXTX, txclk_divider);
918 } else {
919 p->max_pulse_width =
920 txclk_tx_s_max_pulse_width(dev, p->max_pulse_width,
921 &txclk_divider);
922 }
923 o->max_pulse_width = p->max_pulse_width;
924 atomic_set(&state->txclk_divider, txclk_divider);
925
926 p->resolution = clock_divider_to_resolution(txclk_divider);
927 o->resolution = p->resolution;
928
929 /* FIXME - make this dependent on resolution for better performance */
930 control_tx_irq_watermark(dev, TX_FIFO_HALF_EMPTY);
931
932 control_tx_polarity_invert(dev, p->invert_carrier_sense);
933 o->invert_carrier_sense = p->invert_carrier_sense;
934
935 control_tx_level_invert(dev, p->invert_level);
936 o->invert_level = p->invert_level;
937
938 o->interrupt_enable = p->interrupt_enable;
939 o->enable = p->enable;
940 if (p->enable) {
941 if (p->interrupt_enable)
942 irqenable_tx(dev, IRQEN_TSE);
943 control_tx_enable(dev, p->enable);
944 }
945
946 mutex_unlock(&state->tx_params_lock);
947 return 0;
948}
949
950
951/*
952 * V4L2 Subdevice Core Ops
953 */
954static int cx23888_ir_log_status(struct v4l2_subdev *sd)
955{
956 struct cx23888_ir_state *state = to_state(sd);
957 struct cx23885_dev *dev = state->dev;
958 char *s;
959 int i, j;
960
961 u32 cntrl = cx23888_ir_read4(dev, CX23888_IR_CNTRL_REG);
962 u32 txclk = cx23888_ir_read4(dev, CX23888_IR_TXCLK_REG) & TXCLK_TCD;
963 u32 rxclk = cx23888_ir_read4(dev, CX23888_IR_RXCLK_REG) & RXCLK_RCD;
964 u32 cduty = cx23888_ir_read4(dev, CX23888_IR_CDUTY_REG) & CDUTY_CDC;
965 u32 stats = cx23888_ir_read4(dev, CX23888_IR_STATS_REG);
966 u32 irqen = cx23888_ir_read4(dev, CX23888_IR_IRQEN_REG);
967 u32 filtr = cx23888_ir_read4(dev, CX23888_IR_FILTR_REG) & FILTR_LPF;
968
969 v4l2_info(sd, "IR Receiver:\n");
970 v4l2_info(sd, "\tEnabled: %s\n",
971 cntrl & CNTRL_RXE ? "yes" : "no");
972 v4l2_info(sd, "\tDemodulation from a carrier: %s\n",
973 cntrl & CNTRL_DMD ? "enabled" : "disabled");
974 v4l2_info(sd, "\tFIFO: %s\n",
975 cntrl & CNTRL_RFE ? "enabled" : "disabled");
976 switch (cntrl & CNTRL_EDG) {
977 case CNTRL_EDG_NONE:
978 s = "disabled";
979 break;
980 case CNTRL_EDG_FALL:
981 s = "falling edge";
982 break;
983 case CNTRL_EDG_RISE:
984 s = "rising edge";
985 break;
986 case CNTRL_EDG_BOTH:
987 s = "rising & falling edges";
988 break;
989 default:
990 s = "??? edge";
991 break;
992 }
993 v4l2_info(sd, "\tPulse timers' start/stop trigger: %s\n", s);
994 v4l2_info(sd, "\tFIFO data on pulse timer overflow: %s\n",
995 cntrl & CNTRL_R ? "not loaded" : "overflow marker");
996 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
997 cntrl & CNTRL_RIC ? "not empty" : "half full or greater");
998 v4l2_info(sd, "\tLoopback mode: %s\n",
999 cntrl & CNTRL_LBM ? "loopback active" : "normal receive");
1000 if (cntrl & CNTRL_DMD) {
1001 v4l2_info(sd, "\tExpected carrier (16 clocks): %u Hz\n",
1002 clock_divider_to_carrier_freq(rxclk));
1003 switch (cntrl & CNTRL_WIN) {
1004 case CNTRL_WIN_3_3:
1005 i = 3;
1006 j = 3;
1007 break;
1008 case CNTRL_WIN_4_3:
1009 i = 4;
1010 j = 3;
1011 break;
1012 case CNTRL_WIN_3_4:
1013 i = 3;
1014 j = 4;
1015 break;
1016 case CNTRL_WIN_4_4:
1017 i = 4;
1018 j = 4;
1019 break;
1020 default:
1021 i = 0;
1022 j = 0;
1023 break;
1024 }
1025 v4l2_info(sd, "\tNext carrier edge window: 16 clocks "
1026 "-%1d/+%1d, %u to %u Hz\n", i, j,
1027 clock_divider_to_freq(rxclk, 16 + j),
1028 clock_divider_to_freq(rxclk, 16 - i));
1029 }
1030 v4l2_info(sd, "\tMax measurable pulse width: %u us, %llu ns\n",
1031 pulse_width_count_to_us(FIFO_RXTX, rxclk),
1032 pulse_width_count_to_ns(FIFO_RXTX, rxclk));
1033 v4l2_info(sd, "\tLow pass filter: %s\n",
1034 filtr ? "enabled" : "disabled");
1035 if (filtr)
1036 v4l2_info(sd, "\tMin acceptable pulse width (LPF): %u us, "
1037 "%u ns\n",
1038 lpf_count_to_us(filtr),
1039 lpf_count_to_ns(filtr));
1040 v4l2_info(sd, "\tPulse width timer timed-out: %s\n",
1041 stats & STATS_RTO ? "yes" : "no");
1042 v4l2_info(sd, "\tPulse width timer time-out intr: %s\n",
1043 irqen & IRQEN_RTE ? "enabled" : "disabled");
1044 v4l2_info(sd, "\tFIFO overrun: %s\n",
1045 stats & STATS_ROR ? "yes" : "no");
1046 v4l2_info(sd, "\tFIFO overrun interrupt: %s\n",
1047 irqen & IRQEN_ROE ? "enabled" : "disabled");
1048 v4l2_info(sd, "\tBusy: %s\n",
1049 stats & STATS_RBY ? "yes" : "no");
1050 v4l2_info(sd, "\tFIFO service requested: %s\n",
1051 stats & STATS_RSR ? "yes" : "no");
1052 v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
1053 irqen & IRQEN_RSE ? "enabled" : "disabled");
1054
1055 v4l2_info(sd, "IR Transmitter:\n");
1056 v4l2_info(sd, "\tEnabled: %s\n",
1057 cntrl & CNTRL_TXE ? "yes" : "no");
1058 v4l2_info(sd, "\tModulation onto a carrier: %s\n",
1059 cntrl & CNTRL_MOD ? "enabled" : "disabled");
1060 v4l2_info(sd, "\tFIFO: %s\n",
1061 cntrl & CNTRL_TFE ? "enabled" : "disabled");
1062 v4l2_info(sd, "\tFIFO interrupt watermark: %s\n",
1063 cntrl & CNTRL_TIC ? "not empty" : "half full or less");
1064 v4l2_info(sd, "\tOutput pin level inversion %s\n",
1065 cntrl & CNTRL_IVO ? "yes" : "no");
1066 v4l2_info(sd, "\tCarrier polarity: %s\n",
1067 cntrl & CNTRL_CPL ? "space:burst mark:noburst"
1068 : "space:noburst mark:burst");
1069 if (cntrl & CNTRL_MOD) {
1070 v4l2_info(sd, "\tCarrier (16 clocks): %u Hz\n",
1071 clock_divider_to_carrier_freq(txclk));
1072 v4l2_info(sd, "\tCarrier duty cycle: %2u/16\n",
1073 cduty + 1);
1074 }
1075 v4l2_info(sd, "\tMax pulse width: %u us, %llu ns\n",
1076 pulse_width_count_to_us(FIFO_RXTX, txclk),
1077 pulse_width_count_to_ns(FIFO_RXTX, txclk));
1078 v4l2_info(sd, "\tBusy: %s\n",
1079 stats & STATS_TBY ? "yes" : "no");
1080 v4l2_info(sd, "\tFIFO service requested: %s\n",
1081 stats & STATS_TSR ? "yes" : "no");
1082 v4l2_info(sd, "\tFIFO service request interrupt: %s\n",
1083 irqen & IRQEN_TSE ? "enabled" : "disabled");
1084
1085 return 0;
1086}
1087
1088static inline int cx23888_ir_dbg_match(const struct v4l2_dbg_match *match)
1089{
1090 return match->type == V4L2_CHIP_MATCH_HOST && match->addr == 2;
1091}
1092
1093static int cx23888_ir_g_chip_ident(struct v4l2_subdev *sd,
1094 struct v4l2_dbg_chip_ident *chip)
1095{
1096 struct cx23888_ir_state *state = to_state(sd);
1097
1098 if (cx23888_ir_dbg_match(&chip->match)) {
1099 chip->ident = state->id;
1100 chip->revision = state->rev;
1101 }
1102 return 0;
1103}
1104
1105#ifdef CONFIG_VIDEO_ADV_DEBUG
1106static int cx23888_ir_g_register(struct v4l2_subdev *sd,
1107 struct v4l2_dbg_register *reg)
1108{
1109 struct cx23888_ir_state *state = to_state(sd);
1110 u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;
1111
1112 if (!cx23888_ir_dbg_match(&reg->match))
1113 return -EINVAL;
1114 if ((addr & 0x3) != 0)
1115 return -EINVAL;
1116 if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
1117 return -EINVAL;
1118 if (!capable(CAP_SYS_ADMIN))
1119 return -EPERM;
1120 reg->size = 4;
1121 reg->val = cx23888_ir_read4(state->dev, addr);
1122 return 0;
1123}
1124
1125static int cx23888_ir_s_register(struct v4l2_subdev *sd,
1126 struct v4l2_dbg_register *reg)
1127{
1128 struct cx23888_ir_state *state = to_state(sd);
1129 u32 addr = CX23888_IR_REG_BASE + (u32) reg->reg;
1130
1131 if (!cx23888_ir_dbg_match(&reg->match))
1132 return -EINVAL;
1133 if ((addr & 0x3) != 0)
1134 return -EINVAL;
1135 if (addr < CX23888_IR_CNTRL_REG || addr > CX23888_IR_LEARN_REG)
1136 return -EINVAL;
1137 if (!capable(CAP_SYS_ADMIN))
1138 return -EPERM;
1139 cx23888_ir_write4(state->dev, addr, reg->val);
1140 return 0;
1141}
1142#endif
1143
1144static const struct v4l2_subdev_core_ops cx23888_ir_core_ops = {
1145 .g_chip_ident = cx23888_ir_g_chip_ident,
1146 .log_status = cx23888_ir_log_status,
1147#ifdef CONFIG_VIDEO_ADV_DEBUG
1148 .g_register = cx23888_ir_g_register,
1149 .s_register = cx23888_ir_s_register,
1150#endif
1151 .interrupt_service_routine = cx23888_ir_irq_handler,
1152};
1153
1154static const struct v4l2_subdev_ir_ops cx23888_ir_ir_ops = {
1155 .rx_read = cx23888_ir_rx_read,
1156 .rx_g_parameters = cx23888_ir_rx_g_parameters,
1157 .rx_s_parameters = cx23888_ir_rx_s_parameters,
1158
1159 .tx_write = cx23888_ir_tx_write,
1160 .tx_g_parameters = cx23888_ir_tx_g_parameters,
1161 .tx_s_parameters = cx23888_ir_tx_s_parameters,
1162};
1163
1164static const struct v4l2_subdev_ops cx23888_ir_controller_ops = {
1165 .core = &cx23888_ir_core_ops,
1166 .ir = &cx23888_ir_ir_ops,
1167};
1168
1169static const struct v4l2_subdev_ir_parameters default_rx_params = {
1170 .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
1171 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1172
1173 .enable = false,
1174 .interrupt_enable = false,
1175 .shutdown = true,
1176
1177 .modulation = true,
1178 .carrier_freq = 36000, /* 36 kHz - RC-5, RC-6, and RC-6A carrier */
1179
1180 /* RC-5: 666,667 ns = 1/36 kHz * 32 cycles * 1 mark * 0.75 */
1181 /* RC-6A: 333,333 ns = 1/36 kHz * 16 cycles * 1 mark * 0.75 */
1182 .noise_filter_min_width = 333333, /* ns */
1183 .carrier_range_lower = 35000,
1184 .carrier_range_upper = 37000,
1185 .invert_level = false,
1186};
1187
1188static const struct v4l2_subdev_ir_parameters default_tx_params = {
1189 .bytes_per_data_element = sizeof(union cx23888_ir_fifo_rec),
1190 .mode = V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
1191
1192 .enable = false,
1193 .interrupt_enable = false,
1194 .shutdown = true,
1195
1196 .modulation = true,
1197 .carrier_freq = 36000, /* 36 kHz - RC-5 carrier */
1198 .duty_cycle = 25, /* 25 % - RC-5 carrier */
1199 .invert_level = false,
1200 .invert_carrier_sense = false,
1201};
1202
1203int cx23888_ir_probe(struct cx23885_dev *dev)
1204{
1205 struct cx23888_ir_state *state;
1206 struct v4l2_subdev *sd;
1207 struct v4l2_subdev_ir_parameters default_params;
1208 int ret;
1209
1210 state = kzalloc(sizeof(struct cx23888_ir_state), GFP_KERNEL);
1211 if (state == NULL)
1212 return -ENOMEM;
1213
1214 spin_lock_init(&state->rx_kfifo_lock);
1215 if (kfifo_alloc(&state->rx_kfifo, CX23888_IR_RX_KFIFO_SIZE, GFP_KERNEL))
1216 return -ENOMEM;
1217
1218 state->dev = dev;
1219 state->id = V4L2_IDENT_CX23888_IR;
1220 state->rev = 0;
1221 sd = &state->sd;
1222
1223 v4l2_subdev_init(sd, &cx23888_ir_controller_ops);
1224 v4l2_set_subdevdata(sd, state);
1225 /* FIXME - fix the formatting of dev->v4l2_dev.name and use it */
1226 snprintf(sd->name, sizeof(sd->name), "%s/888-ir", dev->name);
1227 sd->grp_id = CX23885_HW_888_IR;
1228
1229 ret = v4l2_device_register_subdev(&dev->v4l2_dev, sd);
1230 if (ret == 0) {
1231 /*
1232 * Ensure no interrupts arrive from '888 specific conditions,
1233 * since we ignore them in this driver to have commonality with
1234 * similar IR controller cores.
1235 */
1236 cx23888_ir_write4(dev, CX23888_IR_IRQEN_REG, 0);
1237
1238 mutex_init(&state->rx_params_lock);
1239 memcpy(&default_params, &default_rx_params,
1240 sizeof(struct v4l2_subdev_ir_parameters));
1241 v4l2_subdev_call(sd, ir, rx_s_parameters, &default_params);
1242
1243 mutex_init(&state->tx_params_lock);
1244 memcpy(&default_params, &default_tx_params,
1245 sizeof(struct v4l2_subdev_ir_parameters));
1246 v4l2_subdev_call(sd, ir, tx_s_parameters, &default_params);
1247 } else {
1248 kfifo_free(&state->rx_kfifo);
1249 }
1250 return ret;
1251}
1252
1253int cx23888_ir_remove(struct cx23885_dev *dev)
1254{
1255 struct v4l2_subdev *sd;
1256 struct cx23888_ir_state *state;
1257
1258 sd = cx23885_find_hw(dev, CX23885_HW_888_IR);
1259 if (sd == NULL)
1260 return -ENODEV;
1261
1262 cx23888_ir_rx_shutdown(sd);
1263 cx23888_ir_tx_shutdown(sd);
1264
1265 state = to_state(sd);
1266 v4l2_device_unregister_subdev(sd);
1267 kfifo_free(&state->rx_kfifo);
1268 kfree(state);
1269 /* Nothing more to free() as state held the actual v4l2_subdev object */
1270 return 0;
1271}
diff --git a/drivers/media/pci/cx23885/cx23888-ir.h b/drivers/media/pci/cx23885/cx23888-ir.h
new file mode 100644
index 000000000000..d2de41caaf1d
--- /dev/null
+++ b/drivers/media/pci/cx23885/cx23888-ir.h
@@ -0,0 +1,28 @@
1/*
2 * Driver for the Conexant CX23885/7/8 PCIe bridge
3 *
4 * CX23888 Integrated Consumer Infrared Controller
5 *
6 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24#ifndef _CX23888_IR_H_
25#define _CX23888_IR_H_
26int cx23888_ir_probe(struct cx23885_dev *dev);
27int cx23888_ir_remove(struct cx23885_dev *dev);
28#endif
diff --git a/drivers/media/pci/cx23885/netup-eeprom.c b/drivers/media/pci/cx23885/netup-eeprom.c
new file mode 100644
index 000000000000..98a48f500684
--- /dev/null
+++ b/drivers/media/pci/cx23885/netup-eeprom.c
@@ -0,0 +1,107 @@
1
2/*
3 * netup-eeprom.c
4 *
5 * 24LC02 EEPROM driver in conjunction with NetUP Dual DVB-S2 CI card
6 *
7 * Copyright (C) 2009 NetUP Inc.
8 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 *
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#
27#include "cx23885.h"
28#include "netup-eeprom.h"
29
30#define EEPROM_I2C_ADDR 0x50
31
32int netup_eeprom_read(struct i2c_adapter *i2c_adap, u8 addr)
33{
34 int ret;
35 unsigned char buf[2];
36
37 /* Read from EEPROM */
38 struct i2c_msg msg[] = {
39 {
40 .addr = EEPROM_I2C_ADDR,
41 .flags = 0,
42 .buf = &buf[0],
43 .len = 1
44 }, {
45 .addr = EEPROM_I2C_ADDR,
46 .flags = I2C_M_RD,
47 .buf = &buf[1],
48 .len = 1
49 }
50
51 };
52
53 buf[0] = addr;
54 buf[1] = 0x0;
55
56 ret = i2c_transfer(i2c_adap, msg, 2);
57
58 if (ret != 2) {
59 printk(KERN_ERR "eeprom i2c read error, status=%d\n", ret);
60 return -1;
61 }
62
63 return buf[1];
64};
65
66int netup_eeprom_write(struct i2c_adapter *i2c_adap, u8 addr, u8 data)
67{
68 int ret;
69 unsigned char bufw[2];
70
71 /* Write into EEPROM */
72 struct i2c_msg msg[] = {
73 {
74 .addr = EEPROM_I2C_ADDR,
75 .flags = 0,
76 .buf = &bufw[0],
77 .len = 2
78 }
79 };
80
81 bufw[0] = addr;
82 bufw[1] = data;
83
84 ret = i2c_transfer(i2c_adap, msg, 1);
85
86 if (ret != 1) {
87 printk(KERN_ERR "eeprom i2c write error, status=%d\n", ret);
88 return -1;
89 }
90
91 mdelay(10); /* prophylactic delay, datasheet write cycle time = 5 ms */
92 return 0;
93};
94
95void netup_get_card_info(struct i2c_adapter *i2c_adap,
96 struct netup_card_info *cinfo)
97{
98 int i, j;
99
100 cinfo->rev = netup_eeprom_read(i2c_adap, 63);
101
102 for (i = 64, j = 0; i < 70; i++, j++)
103 cinfo->port[0].mac[j] = netup_eeprom_read(i2c_adap, i);
104
105 for (i = 70, j = 0; i < 76; i++, j++)
106 cinfo->port[1].mac[j] = netup_eeprom_read(i2c_adap, i);
107};
diff --git a/drivers/media/pci/cx23885/netup-eeprom.h b/drivers/media/pci/cx23885/netup-eeprom.h
new file mode 100644
index 000000000000..13926e18feba
--- /dev/null
+++ b/drivers/media/pci/cx23885/netup-eeprom.h
@@ -0,0 +1,42 @@
1/*
2 * netup-eeprom.h
3 *
4 * 24LC02 EEPROM driver in conjunction with NetUP Dual DVB-S2 CI card
5 *
6 * Copyright (C) 2009 NetUP Inc.
7 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 *
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#ifndef NETUP_EEPROM_H
26#define NETUP_EEPROM_H
27
28struct netup_port_info {
29 u8 mac[6];/* card MAC address */
30};
31
32struct netup_card_info {
33 struct netup_port_info port[2];/* ports - 1,2 */
34 u8 rev;/* card revision */
35};
36
37extern int netup_eeprom_read(struct i2c_adapter *i2c_adap, u8 addr);
38extern int netup_eeprom_write(struct i2c_adapter *i2c_adap, u8 addr, u8 data);
39extern void netup_get_card_info(struct i2c_adapter *i2c_adap,
40 struct netup_card_info *cinfo);
41
42#endif
diff --git a/drivers/media/pci/cx23885/netup-init.c b/drivers/media/pci/cx23885/netup-init.c
new file mode 100644
index 000000000000..f4893e69cd89
--- /dev/null
+++ b/drivers/media/pci/cx23885/netup-init.c
@@ -0,0 +1,125 @@
1/*
2 * netup-init.c
3 *
4 * NetUP Dual DVB-S2 CI driver
5 *
6 * Copyright (C) 2009 NetUP Inc.
7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
8 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 *
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include "cx23885.h"
27
28static void i2c_av_write(struct i2c_adapter *i2c, u16 reg, u8 val)
29{
30 int ret;
31 u8 buf[3];
32 struct i2c_msg msg = {
33 .addr = 0x88 >> 1,
34 .flags = 0,
35 .buf = buf,
36 .len = 3
37 };
38
39 buf[0] = reg >> 8;
40 buf[1] = reg & 0xff;
41 buf[2] = val;
42
43 ret = i2c_transfer(i2c, &msg, 1);
44
45 if (ret != 1)
46 printk(KERN_ERR "%s: i2c write error!\n", __func__);
47}
48
49static void i2c_av_write4(struct i2c_adapter *i2c, u16 reg, u32 val)
50{
51 int ret;
52 u8 buf[6];
53 struct i2c_msg msg = {
54 .addr = 0x88 >> 1,
55 .flags = 0,
56 .buf = buf,
57 .len = 6
58 };
59
60 buf[0] = reg >> 8;
61 buf[1] = reg & 0xff;
62 buf[2] = val & 0xff;
63 buf[3] = (val >> 8) & 0xff;
64 buf[4] = (val >> 16) & 0xff;
65 buf[5] = val >> 24;
66
67 ret = i2c_transfer(i2c, &msg, 1);
68
69 if (ret != 1)
70 printk(KERN_ERR "%s: i2c write error!\n", __func__);
71}
72
73static u8 i2c_av_read(struct i2c_adapter *i2c, u16 reg)
74{
75 int ret;
76 u8 buf[2];
77 struct i2c_msg msg = {
78 .addr = 0x88 >> 1,
79 .flags = 0,
80 .buf = buf,
81 .len = 2
82 };
83
84 buf[0] = reg >> 8;
85 buf[1] = reg & 0xff;
86
87 ret = i2c_transfer(i2c, &msg, 1);
88
89 if (ret != 1)
90 printk(KERN_ERR "%s: i2c write error!\n", __func__);
91
92 msg.flags = I2C_M_RD;
93 msg.len = 1;
94
95 ret = i2c_transfer(i2c, &msg, 1);
96
97 if (ret != 1)
98 printk(KERN_ERR "%s: i2c read error!\n", __func__);
99
100 return buf[0];
101}
102
103static void i2c_av_and_or(struct i2c_adapter *i2c, u16 reg, unsigned and_mask,
104 u8 or_value)
105{
106 i2c_av_write(i2c, reg, (i2c_av_read(i2c, reg) & and_mask) | or_value);
107}
108/* set 27MHz on AUX_CLK */
109void netup_initialize(struct cx23885_dev *dev)
110{
111 struct cx23885_i2c *i2c_bus = &dev->i2c_bus[2];
112 struct i2c_adapter *i2c = &i2c_bus->i2c_adap;
113
114 /* Stop microcontroller */
115 i2c_av_and_or(i2c, 0x803, ~0x10, 0x00);
116
117 /* Aux PLL frac for 27 MHz */
118 i2c_av_write4(i2c, 0x114, 0xea0eb3);
119
120 /* Aux PLL int for 27 MHz */
121 i2c_av_write4(i2c, 0x110, 0x090319);
122
123 /* start microcontroller */
124 i2c_av_and_or(i2c, 0x803, ~0x10, 0x10);
125}
diff --git a/drivers/media/pci/cx23885/netup-init.h b/drivers/media/pci/cx23885/netup-init.h
new file mode 100644
index 000000000000..d26ae4b1590e
--- /dev/null
+++ b/drivers/media/pci/cx23885/netup-init.h
@@ -0,0 +1,25 @@
1/*
2 * netup-init.h
3 *
4 * NetUP Dual DVB-S2 CI driver
5 *
6 * Copyright (C) 2009 NetUP Inc.
7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
8 * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 *
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25extern void netup_initialize(struct cx23885_dev *dev);
diff --git a/drivers/media/pci/cx25821/Kconfig b/drivers/media/pci/cx25821/Kconfig
new file mode 100644
index 000000000000..5f6b54213713
--- /dev/null
+++ b/drivers/media/pci/cx25821/Kconfig
@@ -0,0 +1,34 @@
1config VIDEO_CX25821
2 tristate "Conexant cx25821 support"
3 depends on DVB_CORE && VIDEO_DEV && PCI && I2C
4 select I2C_ALGOBIT
5 select VIDEO_BTCX
6 select VIDEO_TVEEPROM
7 depends on RC_CORE
8 select VIDEOBUF_DVB
9 select VIDEOBUF_DMA_SG
10 select VIDEO_CX25840
11 select VIDEO_CX2341X
12 ---help---
13 This is a video4linux driver for Conexant 25821 based
14 TV cards.
15
16 To compile this driver as a module, choose M here: the
17 module will be called cx25821
18
19config VIDEO_CX25821_ALSA
20 tristate "Conexant 25821 DMA audio support"
21 depends on VIDEO_CX25821 && SND && EXPERIMENTAL
22 select SND_PCM
23 ---help---
24 This is a video4linux driver for direct (DMA) audio on
25 Conexant 25821 based capture cards using ALSA.
26
27 It only works with boards with function 01 enabled.
28 To check if your board supports, use lspci -n.
29 If supported, you should see 14f1:8801 or 14f1:8811
30 PCI device.
31
32 To compile this driver as a module, choose M here: the
33 module will be called cx25821-alsa.
34
diff --git a/drivers/media/pci/cx25821/Makefile b/drivers/media/pci/cx25821/Makefile
new file mode 100644
index 000000000000..1434e8094803
--- /dev/null
+++ b/drivers/media/pci/cx25821/Makefile
@@ -0,0 +1,13 @@
1cx25821-y := cx25821-core.o cx25821-cards.o cx25821-i2c.o \
2 cx25821-gpio.o cx25821-medusa-video.o \
3 cx25821-video.o cx25821-video-upstream.o \
4 cx25821-video-upstream-ch2.o \
5 cx25821-audio-upstream.o
6
7obj-$(CONFIG_VIDEO_CX25821) += cx25821.o
8obj-$(CONFIG_VIDEO_CX25821_ALSA) += cx25821-alsa.o
9
10ccflags-y := -Idrivers/media/video
11ccflags-y += -Idrivers/media/tuners
12ccflags-y += -Idrivers/media/dvb-core
13ccflags-y += -Idrivers/media/dvb-frontends
diff --git a/drivers/media/pci/cx25821/cx25821-alsa.c b/drivers/media/pci/cx25821/cx25821-alsa.c
new file mode 100644
index 000000000000..1858a45dd081
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-alsa.c
@@ -0,0 +1,784 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on SAA713x ALSA driver and CX88 driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, version 2
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/device.h>
28#include <linux/interrupt.h>
29#include <linux/vmalloc.h>
30#include <linux/dma-mapping.h>
31#include <linux/pci.h>
32#include <linux/slab.h>
33
34#include <linux/delay.h>
35#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/control.h>
39#include <sound/initval.h>
40#include <sound/tlv.h>
41
42#include "cx25821.h"
43#include "cx25821-reg.h"
44
45#define AUDIO_SRAM_CHANNEL SRAM_CH08
46
47#define dprintk(level, fmt, arg...) \
48do { \
49 if (debug >= level) \
50 pr_info("%s/1: " fmt, chip->dev->name, ##arg); \
51} while (0)
52#define dprintk_core(level, fmt, arg...) \
53do { \
54 if (debug >= level) \
55 printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
56} while (0)
57
58/****************************************************************************
59 Data type declarations - Can be moded to a header file later
60 ****************************************************************************/
61
62static struct snd_card *snd_cx25821_cards[SNDRV_CARDS];
63static int devno;
64
65struct cx25821_audio_buffer {
66 unsigned int bpl;
67 struct btcx_riscmem risc;
68 struct videobuf_dmabuf dma;
69};
70
71struct cx25821_audio_dev {
72 struct cx25821_dev *dev;
73 struct cx25821_dmaqueue q;
74
75 /* pci i/o */
76 struct pci_dev *pci;
77
78 /* audio controls */
79 int irq;
80
81 struct snd_card *card;
82
83 unsigned long iobase;
84 spinlock_t reg_lock;
85 atomic_t count;
86
87 unsigned int dma_size;
88 unsigned int period_size;
89 unsigned int num_periods;
90
91 struct videobuf_dmabuf *dma_risc;
92
93 struct cx25821_audio_buffer *buf;
94
95 struct snd_pcm_substream *substream;
96};
97
98
99/****************************************************************************
100 Module global static vars
101 ****************************************************************************/
102
103static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
104static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
105static bool enable[SNDRV_CARDS] = { 1, [1 ... (SNDRV_CARDS - 1)] = 1 };
106
107module_param_array(enable, bool, NULL, 0444);
108MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
109
110module_param_array(index, int, NULL, 0444);
111MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
112
113/****************************************************************************
114 Module macros
115 ****************************************************************************/
116
117MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
118MODULE_AUTHOR("Hiep Huynh");
119MODULE_LICENSE("GPL");
120MODULE_SUPPORTED_DEVICE("{{Conexant,25821}"); /* "{{Conexant,23881}," */
121
122static unsigned int debug;
123module_param(debug, int, 0644);
124MODULE_PARM_DESC(debug, "enable debug messages");
125
126/****************************************************************************
127 Module specific funtions
128 ****************************************************************************/
129/* Constants taken from cx88-reg.h */
130#define AUD_INT_DN_RISCI1 (1 << 0)
131#define AUD_INT_UP_RISCI1 (1 << 1)
132#define AUD_INT_RDS_DN_RISCI1 (1 << 2)
133#define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
134#define AUD_INT_UP_RISCI2 (1 << 5)
135#define AUD_INT_RDS_DN_RISCI2 (1 << 6)
136#define AUD_INT_DN_SYNC (1 << 12)
137#define AUD_INT_UP_SYNC (1 << 13)
138#define AUD_INT_RDS_DN_SYNC (1 << 14)
139#define AUD_INT_OPC_ERR (1 << 16)
140#define AUD_INT_BER_IRQ (1 << 20)
141#define AUD_INT_MCHG_IRQ (1 << 21)
142#define GP_COUNT_CONTROL_RESET 0x3
143
144#define PCI_MSK_AUD_EXT (1 << 4)
145#define PCI_MSK_AUD_INT (1 << 3)
146/*
147 * BOARD Specific: Sets audio DMA
148 */
149
150static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
151{
152 struct cx25821_audio_buffer *buf = chip->buf;
153 struct cx25821_dev *dev = chip->dev;
154 struct sram_channel *audio_ch =
155 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
156 u32 tmp = 0;
157
158 /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
159 cx25821_set_gpiopin_direction(chip->dev, 0, 0);
160
161 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
162 cx_clear(AUD_INT_DMA_CTL,
163 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
164
165 /* setup fifo + format - out channel */
166 cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
167 buf->risc.dma);
168
169 /* sets bpl size */
170 cx_write(AUD_A_LNGTH, buf->bpl);
171
172 /* reset counter */
173 /* GP_COUNT_CONTROL_RESET = 0x3 */
174 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
175 atomic_set(&chip->count, 0);
176
177 /* Set the input mode to 16-bit */
178 tmp = cx_read(AUD_A_CFG);
179 cx_write(AUD_A_CFG, tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
180 FLD_AUD_CLK_ENABLE);
181
182 /*
183 pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
184 buf->bpl, audio_ch->cmds_start,
185 cx_read(audio_ch->cmds_start + 12)>>1,
186 chip->num_periods, buf->bpl * chip->num_periods);
187 */
188
189 /* Enables corresponding bits at AUD_INT_STAT */
190 cx_write(AUD_A_INT_MSK, FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF |
191 FLD_AUD_DST_SYNC | FLD_AUD_DST_OPC_ERR);
192
193 /* Clean any pending interrupt bits already set */
194 cx_write(AUD_A_INT_STAT, ~0);
195
196 /* enable audio irqs */
197 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
198
199 /* Turn on audio downstream fifo and risc enable 0x101 */
200 tmp = cx_read(AUD_INT_DMA_CTL);
201 cx_set(AUD_INT_DMA_CTL, tmp |
202 (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
203
204 mdelay(100);
205 return 0;
206}
207
208/*
209 * BOARD Specific: Resets audio DMA
210 */
211static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
212{
213 struct cx25821_dev *dev = chip->dev;
214
215 /* stop dma */
216 cx_clear(AUD_INT_DMA_CTL,
217 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
218
219 /* disable irqs */
220 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
221 cx_clear(AUD_A_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
222 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
223
224 return 0;
225}
226
227#define MAX_IRQ_LOOP 50
228
229/*
230 * BOARD Specific: IRQ dma bits
231 */
232static char *cx25821_aud_irqs[32] = {
233 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
234 NULL, /* reserved */
235 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
236 NULL, /* reserved */
237 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
238 NULL, /* reserved */
239 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
240 NULL, /* reserved */
241 "opc_err", "par_err", "rip_err", /* 16-18 */
242 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
243};
244
245/*
246 * BOARD Specific: Threats IRQ audio specific calls
247 */
248static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
249 u32 mask)
250{
251 struct cx25821_dev *dev = chip->dev;
252
253 if (0 == (status & mask))
254 return;
255
256 cx_write(AUD_A_INT_STAT, status);
257 if (debug > 1 || (status & mask & ~0xff))
258 cx25821_print_irqbits(dev->name, "irq aud", cx25821_aud_irqs,
259 ARRAY_SIZE(cx25821_aud_irqs), status, mask);
260
261 /* risc op code error */
262 if (status & AUD_INT_OPC_ERR) {
263 pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
264
265 cx_clear(AUD_INT_DMA_CTL,
266 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
267 cx25821_sram_channel_dump_audio(dev,
268 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL]);
269 }
270 if (status & AUD_INT_DN_SYNC) {
271 pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
272 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
273 return;
274 }
275
276 /* risc1 downstream */
277 if (status & AUD_INT_DN_RISCI1) {
278 atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
279 snd_pcm_period_elapsed(chip->substream);
280 }
281}
282
283/*
284 * BOARD Specific: Handles IRQ calls
285 */
286static irqreturn_t cx25821_irq(int irq, void *dev_id)
287{
288 struct cx25821_audio_dev *chip = dev_id;
289 struct cx25821_dev *dev = chip->dev;
290 u32 status, pci_status;
291 u32 audint_status, audint_mask;
292 int loop, handled = 0;
293
294 audint_status = cx_read(AUD_A_INT_STAT);
295 audint_mask = cx_read(AUD_A_INT_MSK);
296 status = cx_read(PCI_INT_STAT);
297
298 for (loop = 0; loop < 1; loop++) {
299 status = cx_read(PCI_INT_STAT);
300 if (0 == status) {
301 status = cx_read(PCI_INT_STAT);
302 audint_status = cx_read(AUD_A_INT_STAT);
303 audint_mask = cx_read(AUD_A_INT_MSK);
304
305 if (status) {
306 handled = 1;
307 cx_write(PCI_INT_STAT, status);
308
309 cx25821_aud_irq(chip, audint_status,
310 audint_mask);
311 break;
312 } else {
313 goto out;
314 }
315 }
316
317 handled = 1;
318 cx_write(PCI_INT_STAT, status);
319
320 cx25821_aud_irq(chip, audint_status, audint_mask);
321 }
322
323 pci_status = cx_read(PCI_INT_STAT);
324
325 if (handled)
326 cx_write(PCI_INT_STAT, pci_status);
327
328out:
329 return IRQ_RETVAL(handled);
330}
331
332static int dsp_buffer_free(struct cx25821_audio_dev *chip)
333{
334 BUG_ON(!chip->dma_size);
335
336 dprintk(2, "Freeing buffer\n");
337 videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
338 videobuf_dma_free(chip->dma_risc);
339 btcx_riscmem_free(chip->pci, &chip->buf->risc);
340 kfree(chip->buf);
341
342 chip->dma_risc = NULL;
343 chip->dma_size = 0;
344
345 return 0;
346}
347
348/****************************************************************************
349 ALSA PCM Interface
350 ****************************************************************************/
351
352/*
353 * Digital hardware definition
354 */
355#define DEFAULT_FIFO_SIZE 384
356static struct snd_pcm_hardware snd_cx25821_digital_hw = {
357 .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
358 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
359 .formats = SNDRV_PCM_FMTBIT_S16_LE,
360
361 .rates = SNDRV_PCM_RATE_48000,
362 .rate_min = 48000,
363 .rate_max = 48000,
364 .channels_min = 2,
365 .channels_max = 2,
366 /* Analog audio output will be full of clicks and pops if there
367 are not exactly four lines in the SRAM FIFO buffer. */
368 .period_bytes_min = DEFAULT_FIFO_SIZE / 3,
369 .period_bytes_max = DEFAULT_FIFO_SIZE / 3,
370 .periods_min = 1,
371 .periods_max = AUDIO_LINE_SIZE,
372 /* 128 * 128 = 16384 = 1024 * 16 */
373 .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
374};
375
376/*
377 * audio pcm capture open callback
378 */
379static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
380{
381 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
382 struct snd_pcm_runtime *runtime = substream->runtime;
383 int err;
384 unsigned int bpl = 0;
385
386 if (!chip) {
387 pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
388 return -ENODEV;
389 }
390
391 err = snd_pcm_hw_constraint_pow2(runtime, 0,
392 SNDRV_PCM_HW_PARAM_PERIODS);
393 if (err < 0)
394 goto _error;
395
396 chip->substream = substream;
397
398 runtime->hw = snd_cx25821_digital_hw;
399
400 if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
401 DEFAULT_FIFO_SIZE) {
402 /* since there are 3 audio Clusters */
403 bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
404 bpl &= ~7; /* must be multiple of 8 */
405
406 if (bpl > AUDIO_LINE_SIZE)
407 bpl = AUDIO_LINE_SIZE;
408
409 runtime->hw.period_bytes_min = bpl;
410 runtime->hw.period_bytes_max = bpl;
411 }
412
413 return 0;
414_error:
415 dprintk(1, "Error opening PCM!\n");
416 return err;
417}
418
419/*
420 * audio close callback
421 */
422static int snd_cx25821_close(struct snd_pcm_substream *substream)
423{
424 return 0;
425}
426
427/*
428 * hw_params callback
429 */
430static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
431 struct snd_pcm_hw_params *hw_params)
432{
433 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
434 struct videobuf_dmabuf *dma;
435
436 struct cx25821_audio_buffer *buf;
437 int ret;
438
439 if (substream->runtime->dma_area) {
440 dsp_buffer_free(chip);
441 substream->runtime->dma_area = NULL;
442 }
443
444 chip->period_size = params_period_bytes(hw_params);
445 chip->num_periods = params_periods(hw_params);
446 chip->dma_size = chip->period_size * params_periods(hw_params);
447
448 BUG_ON(!chip->dma_size);
449 BUG_ON(chip->num_periods & (chip->num_periods - 1));
450
451 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
452 if (NULL == buf)
453 return -ENOMEM;
454
455 if (chip->period_size > AUDIO_LINE_SIZE)
456 chip->period_size = AUDIO_LINE_SIZE;
457
458 buf->bpl = chip->period_size;
459
460 dma = &buf->dma;
461 videobuf_dma_init(dma);
462 ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
463 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
464 if (ret < 0)
465 goto error;
466
467 ret = videobuf_dma_map(&chip->pci->dev, dma);
468 if (ret < 0)
469 goto error;
470
471 ret = cx25821_risc_databuffer_audio(chip->pci, &buf->risc, dma->sglist,
472 chip->period_size, chip->num_periods, 1);
473 if (ret < 0) {
474 pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
475 goto error;
476 }
477
478 /* Loop back to start of program */
479 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
480 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
481 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
482
483 chip->buf = buf;
484 chip->dma_risc = dma;
485
486 substream->runtime->dma_area = chip->dma_risc->vaddr;
487 substream->runtime->dma_bytes = chip->dma_size;
488 substream->runtime->dma_addr = 0;
489
490 return 0;
491
492error:
493 kfree(buf);
494 return ret;
495}
496
497/*
498 * hw free callback
499 */
500static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
501{
502 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
503
504 if (substream->runtime->dma_area) {
505 dsp_buffer_free(chip);
506 substream->runtime->dma_area = NULL;
507 }
508
509 return 0;
510}
511
512/*
513 * prepare callback
514 */
515static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
516{
517 return 0;
518}
519
520/*
521 * trigger callback
522 */
523static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
524 int cmd)
525{
526 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
527 int err = 0;
528
529 /* Local interrupts are already disabled by ALSA */
530 spin_lock(&chip->reg_lock);
531
532 switch (cmd) {
533 case SNDRV_PCM_TRIGGER_START:
534 err = _cx25821_start_audio_dma(chip);
535 break;
536 case SNDRV_PCM_TRIGGER_STOP:
537 err = _cx25821_stop_audio_dma(chip);
538 break;
539 default:
540 err = -EINVAL;
541 break;
542 }
543
544 spin_unlock(&chip->reg_lock);
545
546 return err;
547}
548
549/*
550 * pointer callback
551 */
552static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
553 *substream)
554{
555 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
556 struct snd_pcm_runtime *runtime = substream->runtime;
557 u16 count;
558
559 count = atomic_read(&chip->count);
560
561 return runtime->period_size * (count & (runtime->periods - 1));
562}
563
564/*
565 * page callback (needed for mmap)
566 */
567static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
568 unsigned long offset)
569{
570 void *pageptr = substream->runtime->dma_area + offset;
571
572 return vmalloc_to_page(pageptr);
573}
574
575/*
576 * operators
577 */
578static struct snd_pcm_ops snd_cx25821_pcm_ops = {
579 .open = snd_cx25821_pcm_open,
580 .close = snd_cx25821_close,
581 .ioctl = snd_pcm_lib_ioctl,
582 .hw_params = snd_cx25821_hw_params,
583 .hw_free = snd_cx25821_hw_free,
584 .prepare = snd_cx25821_prepare,
585 .trigger = snd_cx25821_card_trigger,
586 .pointer = snd_cx25821_pointer,
587 .page = snd_cx25821_page,
588};
589
590/*
591 * ALSA create a PCM device: Called when initializing the board.
592 * Sets up the name and hooks up the callbacks
593 */
594static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
595 char *name)
596{
597 struct snd_pcm *pcm;
598 int err;
599
600 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
601 if (err < 0) {
602 pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
603 return err;
604 }
605 pcm->private_data = chip;
606 pcm->info_flags = 0;
607 strcpy(pcm->name, name);
608 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
609
610 return 0;
611}
612
613/****************************************************************************
614 Basic Flow for Sound Devices
615 ****************************************************************************/
616
617/*
618 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
619 * Only boards with eeprom and byte 1 at eeprom=1 have it
620 */
621
622static DEFINE_PCI_DEVICE_TABLE(cx25821_audio_pci_tbl) = {
623 {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
624 {0,}
625};
626
627MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
628
629/*
630 * Not used in the function snd_cx25821_dev_free so removing
631 * from the file.
632 */
633/*
634static int snd_cx25821_free(struct cx25821_audio_dev *chip)
635{
636 if (chip->irq >= 0)
637 free_irq(chip->irq, chip);
638
639 cx25821_dev_unregister(chip->dev);
640 pci_disable_device(chip->pci);
641
642 return 0;
643}
644*/
645
646/*
647 * Component Destructor
648 */
649static void snd_cx25821_dev_free(struct snd_card *card)
650{
651 struct cx25821_audio_dev *chip = card->private_data;
652
653 /* snd_cx25821_free(chip); */
654 snd_card_free(chip->card);
655}
656
657/*
658 * Alsa Constructor - Component probe
659 */
660static int cx25821_audio_initdev(struct cx25821_dev *dev)
661{
662 struct snd_card *card;
663 struct cx25821_audio_dev *chip;
664 int err;
665
666 if (devno >= SNDRV_CARDS) {
667 pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
668 return -ENODEV;
669 }
670
671 if (!enable[devno]) {
672 ++devno;
673 pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
674 return -ENOENT;
675 }
676
677 err = snd_card_create(index[devno], id[devno], THIS_MODULE,
678 sizeof(struct cx25821_audio_dev), &card);
679 if (err < 0) {
680 pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
681 __func__);
682 return err;
683 }
684
685 strcpy(card->driver, "cx25821");
686
687 /* Card "creation" */
688 card->private_free = snd_cx25821_dev_free;
689 chip = card->private_data;
690 spin_lock_init(&chip->reg_lock);
691
692 chip->dev = dev;
693 chip->card = card;
694 chip->pci = dev->pci;
695 chip->iobase = pci_resource_start(dev->pci, 0);
696
697 chip->irq = dev->pci->irq;
698
699 err = request_irq(dev->pci->irq, cx25821_irq,
700 IRQF_SHARED, chip->dev->name, chip);
701
702 if (err < 0) {
703 pr_err("ERROR %s: can't get IRQ %d for ALSA\n", chip->dev->name,
704 dev->pci->irq);
705 goto error;
706 }
707
708 err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
709 if (err < 0) {
710 pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
711 __func__);
712 goto error;
713 }
714
715 snd_card_set_dev(card, &chip->pci->dev);
716
717 strcpy(card->shortname, "cx25821");
718 sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
719 chip->iobase, chip->irq);
720 strcpy(card->mixername, "CX25821");
721
722 pr_info("%s/%i: ALSA support for cx25821 boards\n", card->driver,
723 devno);
724
725 err = snd_card_register(card);
726 if (err < 0) {
727 pr_info("DEBUG ERROR: cannot register sound card %s\n",
728 __func__);
729 goto error;
730 }
731
732 snd_cx25821_cards[devno] = card;
733
734 devno++;
735 return 0;
736
737error:
738 snd_card_free(card);
739 return err;
740}
741
742/****************************************************************************
743 LINUX MODULE INIT
744 ****************************************************************************/
745static void cx25821_audio_fini(void)
746{
747 snd_card_free(snd_cx25821_cards[0]);
748}
749
750/*
751 * Module initializer
752 *
753 * Loops through present saa7134 cards, and assigns an ALSA device
754 * to each one
755 *
756 */
757static int cx25821_alsa_init(void)
758{
759 struct cx25821_dev *dev = NULL;
760 struct list_head *list;
761
762 mutex_lock(&cx25821_devlist_mutex);
763 list_for_each(list, &cx25821_devlist) {
764 dev = list_entry(list, struct cx25821_dev, devlist);
765 cx25821_audio_initdev(dev);
766 }
767 mutex_unlock(&cx25821_devlist_mutex);
768
769 if (dev == NULL)
770 pr_info("ERROR ALSA: no cx25821 cards found\n");
771
772 return 0;
773
774}
775
776late_initcall(cx25821_alsa_init);
777module_exit(cx25821_audio_fini);
778
779/* ----------------------------------------------------------- */
780/*
781 * Local variables:
782 * c-basic-offset: 8
783 * End:
784 */
diff --git a/drivers/media/pci/cx25821/cx25821-audio-upstream.c b/drivers/media/pci/cx25821/cx25821-audio-upstream.c
new file mode 100644
index 000000000000..8b2a99975c23
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-audio-upstream.c
@@ -0,0 +1,778 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include "cx25821-video.h"
26#include "cx25821-audio-upstream.h"
27
28#include <linux/fs.h>
29#include <linux/errno.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/module.h>
33#include <linux/syscalls.h>
34#include <linux/file.h>
35#include <linux/fcntl.h>
36#include <linux/delay.h>
37#include <linux/slab.h>
38#include <linux/uaccess.h>
39
40MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
41MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
42MODULE_LICENSE("GPL");
43
44static int _intr_msk = FLD_AUD_SRC_RISCI1 | FLD_AUD_SRC_OF |
45 FLD_AUD_SRC_SYNC | FLD_AUD_SRC_OPC_ERR;
46
47int cx25821_sram_channel_setup_upstream_audio(struct cx25821_dev *dev,
48 struct sram_channel *ch,
49 unsigned int bpl, u32 risc)
50{
51 unsigned int i, lines;
52 u32 cdt;
53
54 if (ch->cmds_start == 0) {
55 cx_write(ch->ptr1_reg, 0);
56 cx_write(ch->ptr2_reg, 0);
57 cx_write(ch->cnt2_reg, 0);
58 cx_write(ch->cnt1_reg, 0);
59 return 0;
60 }
61
62 bpl = (bpl + 7) & ~7; /* alignment */
63 cdt = ch->cdt;
64 lines = ch->fifo_size / bpl;
65
66 if (lines > 3)
67 lines = 3;
68
69 BUG_ON(lines < 2);
70
71 /* write CDT */
72 for (i = 0; i < lines; i++) {
73 cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
74 cx_write(cdt + 16 * i + 4, 0);
75 cx_write(cdt + 16 * i + 8, 0);
76 cx_write(cdt + 16 * i + 12, 0);
77 }
78
79 /* write CMDS */
80 cx_write(ch->cmds_start + 0, risc);
81
82 cx_write(ch->cmds_start + 4, 0);
83 cx_write(ch->cmds_start + 8, cdt);
84 cx_write(ch->cmds_start + 12, AUDIO_CDT_SIZE_QW);
85 cx_write(ch->cmds_start + 16, ch->ctrl_start);
86
87 /* IQ size */
88 cx_write(ch->cmds_start + 20, AUDIO_IQ_SIZE_DW);
89
90 for (i = 24; i < 80; i += 4)
91 cx_write(ch->cmds_start + i, 0);
92
93 /* fill registers */
94 cx_write(ch->ptr1_reg, ch->fifo_start);
95 cx_write(ch->ptr2_reg, cdt);
96 cx_write(ch->cnt2_reg, AUDIO_CDT_SIZE_QW);
97 cx_write(ch->cnt1_reg, AUDIO_CLUSTER_SIZE_QW - 1);
98
99 return 0;
100}
101
102static __le32 *cx25821_risc_field_upstream_audio(struct cx25821_dev *dev,
103 __le32 *rp,
104 dma_addr_t databuf_phys_addr,
105 unsigned int bpl,
106 int fifo_enable)
107{
108 unsigned int line;
109 struct sram_channel *sram_ch =
110 dev->channels[dev->_audio_upstream_channel].sram_channels;
111 int offset = 0;
112
113 /* scan lines */
114 for (line = 0; line < LINES_PER_AUDIO_BUFFER; line++) {
115 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
116 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
117 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
118
119 /* Check if we need to enable the FIFO
120 * after the first 3 lines.
121 * For the upstream audio channel,
122 * the risc engine will enable the FIFO */
123 if (fifo_enable && line == 2) {
124 *(rp++) = RISC_WRITECR;
125 *(rp++) = sram_ch->dma_ctl;
126 *(rp++) = sram_ch->fld_aud_fifo_en;
127 *(rp++) = 0x00000020;
128 }
129
130 offset += AUDIO_LINE_SIZE;
131 }
132
133 return rp;
134}
135
136int cx25821_risc_buffer_upstream_audio(struct cx25821_dev *dev,
137 struct pci_dev *pci,
138 unsigned int bpl, unsigned int lines)
139{
140 __le32 *rp;
141 int fifo_enable = 0;
142 int frame = 0, i = 0;
143 int frame_size = AUDIO_DATA_BUF_SZ;
144 int databuf_offset = 0;
145 int risc_flag = RISC_CNT_INC;
146 dma_addr_t risc_phys_jump_addr;
147
148 /* Virtual address of Risc buffer program */
149 rp = dev->_risc_virt_addr;
150
151 /* sync instruction */
152 *(rp++) = cpu_to_le32(RISC_RESYNC | AUDIO_SYNC_LINE);
153
154 for (frame = 0; frame < NUM_AUDIO_FRAMES; frame++) {
155 databuf_offset = frame_size * frame;
156
157 if (frame == 0) {
158 fifo_enable = 1;
159 risc_flag = RISC_CNT_RESET;
160 } else {
161 fifo_enable = 0;
162 risc_flag = RISC_CNT_INC;
163 }
164
165 /* Calculate physical jump address */
166 if ((frame + 1) == NUM_AUDIO_FRAMES) {
167 risc_phys_jump_addr =
168 dev->_risc_phys_start_addr +
169 RISC_SYNC_INSTRUCTION_SIZE;
170 } else {
171 risc_phys_jump_addr =
172 dev->_risc_phys_start_addr +
173 RISC_SYNC_INSTRUCTION_SIZE +
174 AUDIO_RISC_DMA_BUF_SIZE * (frame + 1);
175 }
176
177 rp = cx25821_risc_field_upstream_audio(dev, rp,
178 dev->_audiodata_buf_phys_addr + databuf_offset,
179 bpl, fifo_enable);
180
181 if (USE_RISC_NOOP_AUDIO) {
182 for (i = 0; i < NUM_NO_OPS; i++)
183 *(rp++) = cpu_to_le32(RISC_NOOP);
184 }
185
186 /* Loop to (Nth)FrameRISC or to Start of Risc program &
187 * generate IRQ */
188 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
189 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
190 *(rp++) = cpu_to_le32(0);
191
192 /* Recalculate virtual address based on frame index */
193 rp = dev->_risc_virt_addr + RISC_SYNC_INSTRUCTION_SIZE / 4 +
194 (AUDIO_RISC_DMA_BUF_SIZE * (frame + 1) / 4);
195 }
196
197 return 0;
198}
199
200void cx25821_free_memory_audio(struct cx25821_dev *dev)
201{
202 if (dev->_risc_virt_addr) {
203 pci_free_consistent(dev->pci, dev->_audiorisc_size,
204 dev->_risc_virt_addr, dev->_risc_phys_addr);
205 dev->_risc_virt_addr = NULL;
206 }
207
208 if (dev->_audiodata_buf_virt_addr) {
209 pci_free_consistent(dev->pci, dev->_audiodata_buf_size,
210 dev->_audiodata_buf_virt_addr,
211 dev->_audiodata_buf_phys_addr);
212 dev->_audiodata_buf_virt_addr = NULL;
213 }
214}
215
216void cx25821_stop_upstream_audio(struct cx25821_dev *dev)
217{
218 struct sram_channel *sram_ch =
219 dev->channels[AUDIO_UPSTREAM_SRAM_CHANNEL_B].sram_channels;
220 u32 tmp = 0;
221
222 if (!dev->_audio_is_running) {
223 printk(KERN_DEBUG
224 pr_fmt("No audio file is currently running so return!\n"));
225 return;
226 }
227 /* Disable RISC interrupts */
228 cx_write(sram_ch->int_msk, 0);
229
230 /* Turn OFF risc and fifo enable in AUD_DMA_CNTRL */
231 tmp = cx_read(sram_ch->dma_ctl);
232 cx_write(sram_ch->dma_ctl,
233 tmp & ~(sram_ch->fld_aud_fifo_en | sram_ch->fld_aud_risc_en));
234
235 /* Clear data buffer memory */
236 if (dev->_audiodata_buf_virt_addr)
237 memset(dev->_audiodata_buf_virt_addr, 0,
238 dev->_audiodata_buf_size);
239
240 dev->_audio_is_running = 0;
241 dev->_is_first_audio_frame = 0;
242 dev->_audioframe_count = 0;
243 dev->_audiofile_status = END_OF_FILE;
244
245 kfree(dev->_irq_audio_queues);
246 dev->_irq_audio_queues = NULL;
247
248 kfree(dev->_audiofilename);
249}
250
251void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev)
252{
253 if (dev->_audio_is_running)
254 cx25821_stop_upstream_audio(dev);
255
256 cx25821_free_memory_audio(dev);
257}
258
259int cx25821_get_audio_data(struct cx25821_dev *dev,
260 struct sram_channel *sram_ch)
261{
262 struct file *myfile;
263 int frame_index_temp = dev->_audioframe_index;
264 int i = 0;
265 int line_size = AUDIO_LINE_SIZE;
266 int frame_size = AUDIO_DATA_BUF_SZ;
267 int frame_offset = frame_size * frame_index_temp;
268 ssize_t vfs_read_retval = 0;
269 char mybuf[line_size];
270 loff_t file_offset = dev->_audioframe_count * frame_size;
271 loff_t pos;
272 mm_segment_t old_fs;
273
274 if (dev->_audiofile_status == END_OF_FILE)
275 return 0;
276
277 myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
278
279 if (IS_ERR(myfile)) {
280 const int open_errno = -PTR_ERR(myfile);
281 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
282 __func__, dev->_audiofilename, open_errno);
283 return PTR_ERR(myfile);
284 } else {
285 if (!(myfile->f_op)) {
286 pr_err("%s(): File has no file operations registered!\n",
287 __func__);
288 filp_close(myfile, NULL);
289 return -EIO;
290 }
291
292 if (!myfile->f_op->read) {
293 pr_err("%s(): File has no READ operations registered!\n",
294 __func__);
295 filp_close(myfile, NULL);
296 return -EIO;
297 }
298
299 pos = myfile->f_pos;
300 old_fs = get_fs();
301 set_fs(KERNEL_DS);
302
303 for (i = 0; i < dev->_audio_lines_count; i++) {
304 pos = file_offset;
305
306 vfs_read_retval = vfs_read(myfile, mybuf, line_size,
307 &pos);
308
309 if (vfs_read_retval > 0 && vfs_read_retval == line_size
310 && dev->_audiodata_buf_virt_addr != NULL) {
311 memcpy((void *)(dev->_audiodata_buf_virt_addr +
312 frame_offset / 4), mybuf,
313 vfs_read_retval);
314 }
315
316 file_offset += vfs_read_retval;
317 frame_offset += vfs_read_retval;
318
319 if (vfs_read_retval < line_size) {
320 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
321 __func__);
322 break;
323 }
324 }
325
326 if (i > 0)
327 dev->_audioframe_count++;
328
329 dev->_audiofile_status = (vfs_read_retval == line_size) ?
330 IN_PROGRESS : END_OF_FILE;
331
332 set_fs(old_fs);
333 filp_close(myfile, NULL);
334 }
335
336 return 0;
337}
338
339static void cx25821_audioups_handler(struct work_struct *work)
340{
341 struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
342 _audio_work_entry);
343
344 if (!dev) {
345 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
346 __func__);
347 return;
348 }
349
350 cx25821_get_audio_data(dev, dev->channels[dev->_audio_upstream_channel].
351 sram_channels);
352}
353
354int cx25821_openfile_audio(struct cx25821_dev *dev,
355 struct sram_channel *sram_ch)
356{
357 struct file *myfile;
358 int i = 0, j = 0;
359 int line_size = AUDIO_LINE_SIZE;
360 ssize_t vfs_read_retval = 0;
361 char mybuf[line_size];
362 loff_t pos;
363 loff_t offset = (unsigned long)0;
364 mm_segment_t old_fs;
365
366 myfile = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
367
368 if (IS_ERR(myfile)) {
369 const int open_errno = -PTR_ERR(myfile);
370 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
371 __func__, dev->_audiofilename, open_errno);
372 return PTR_ERR(myfile);
373 } else {
374 if (!(myfile->f_op)) {
375 pr_err("%s(): File has no file operations registered!\n",
376 __func__);
377 filp_close(myfile, NULL);
378 return -EIO;
379 }
380
381 if (!myfile->f_op->read) {
382 pr_err("%s(): File has no READ operations registered!\n",
383 __func__);
384 filp_close(myfile, NULL);
385 return -EIO;
386 }
387
388 pos = myfile->f_pos;
389 old_fs = get_fs();
390 set_fs(KERNEL_DS);
391
392 for (j = 0; j < NUM_AUDIO_FRAMES; j++) {
393 for (i = 0; i < dev->_audio_lines_count; i++) {
394 pos = offset;
395
396 vfs_read_retval = vfs_read(myfile, mybuf,
397 line_size, &pos);
398
399 if (vfs_read_retval > 0 &&
400 vfs_read_retval == line_size &&
401 dev->_audiodata_buf_virt_addr != NULL) {
402 memcpy((void *)(dev->
403 _audiodata_buf_virt_addr
404 + offset / 4), mybuf,
405 vfs_read_retval);
406 }
407
408 offset += vfs_read_retval;
409
410 if (vfs_read_retval < line_size) {
411 pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
412 __func__);
413 break;
414 }
415 }
416
417 if (i > 0)
418 dev->_audioframe_count++;
419
420 if (vfs_read_retval < line_size)
421 break;
422 }
423
424 dev->_audiofile_status = (vfs_read_retval == line_size) ?
425 IN_PROGRESS : END_OF_FILE;
426
427 set_fs(old_fs);
428 myfile->f_pos = 0;
429 filp_close(myfile, NULL);
430 }
431
432 return 0;
433}
434
435static int cx25821_audio_upstream_buffer_prepare(struct cx25821_dev *dev,
436 struct sram_channel *sram_ch,
437 int bpl)
438{
439 int ret = 0;
440 dma_addr_t dma_addr;
441 dma_addr_t data_dma_addr;
442
443 cx25821_free_memory_audio(dev);
444
445 dev->_risc_virt_addr = pci_alloc_consistent(dev->pci,
446 dev->audio_upstream_riscbuf_size, &dma_addr);
447 dev->_risc_virt_start_addr = dev->_risc_virt_addr;
448 dev->_risc_phys_start_addr = dma_addr;
449 dev->_risc_phys_addr = dma_addr;
450 dev->_audiorisc_size = dev->audio_upstream_riscbuf_size;
451
452 if (!dev->_risc_virt_addr) {
453 printk(KERN_DEBUG
454 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for RISC program! Returning\n"));
455 return -ENOMEM;
456 }
457 /* Clear out memory at address */
458 memset(dev->_risc_virt_addr, 0, dev->_audiorisc_size);
459
460 /* For Audio Data buffer allocation */
461 dev->_audiodata_buf_virt_addr = pci_alloc_consistent(dev->pci,
462 dev->audio_upstream_databuf_size, &data_dma_addr);
463 dev->_audiodata_buf_phys_addr = data_dma_addr;
464 dev->_audiodata_buf_size = dev->audio_upstream_databuf_size;
465
466 if (!dev->_audiodata_buf_virt_addr) {
467 printk(KERN_DEBUG
468 pr_fmt("ERROR: pci_alloc_consistent() FAILED to allocate memory for data buffer! Returning\n"));
469 return -ENOMEM;
470 }
471 /* Clear out memory at address */
472 memset(dev->_audiodata_buf_virt_addr, 0, dev->_audiodata_buf_size);
473
474 ret = cx25821_openfile_audio(dev, sram_ch);
475 if (ret < 0)
476 return ret;
477
478 /* Creating RISC programs */
479 ret = cx25821_risc_buffer_upstream_audio(dev, dev->pci, bpl,
480 dev->_audio_lines_count);
481 if (ret < 0) {
482 printk(KERN_DEBUG
483 pr_fmt("ERROR creating audio upstream RISC programs!\n"));
484 goto error;
485 }
486
487 return 0;
488
489error:
490 return ret;
491}
492
493int cx25821_audio_upstream_irq(struct cx25821_dev *dev, int chan_num,
494 u32 status)
495{
496 int i = 0;
497 u32 int_msk_tmp;
498 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
499 dma_addr_t risc_phys_jump_addr;
500 __le32 *rp;
501
502 if (status & FLD_AUD_SRC_RISCI1) {
503 /* Get interrupt_index of the program that interrupted */
504 u32 prog_cnt = cx_read(channel->gpcnt);
505
506 /* Since we've identified our IRQ, clear our bits from the
507 * interrupt mask and interrupt status registers */
508 cx_write(channel->int_msk, 0);
509 cx_write(channel->int_stat, cx_read(channel->int_stat));
510
511 spin_lock(&dev->slock);
512
513 while (prog_cnt != dev->_last_index_irq) {
514 /* Update _last_index_irq */
515 if (dev->_last_index_irq < (NUMBER_OF_PROGRAMS - 1))
516 dev->_last_index_irq++;
517 else
518 dev->_last_index_irq = 0;
519
520 dev->_audioframe_index = dev->_last_index_irq;
521
522 queue_work(dev->_irq_audio_queues,
523 &dev->_audio_work_entry);
524 }
525
526 if (dev->_is_first_audio_frame) {
527 dev->_is_first_audio_frame = 0;
528
529 if (dev->_risc_virt_start_addr != NULL) {
530 risc_phys_jump_addr =
531 dev->_risc_phys_start_addr +
532 RISC_SYNC_INSTRUCTION_SIZE +
533 AUDIO_RISC_DMA_BUF_SIZE;
534
535 rp = cx25821_risc_field_upstream_audio(dev,
536 dev->_risc_virt_start_addr + 1,
537 dev->_audiodata_buf_phys_addr,
538 AUDIO_LINE_SIZE, FIFO_DISABLE);
539
540 if (USE_RISC_NOOP_AUDIO) {
541 for (i = 0; i < NUM_NO_OPS; i++) {
542 *(rp++) =
543 cpu_to_le32(RISC_NOOP);
544 }
545 }
546 /* Jump to 2nd Audio Frame */
547 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 |
548 RISC_CNT_RESET);
549 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
550 *(rp++) = cpu_to_le32(0);
551 }
552 }
553
554 spin_unlock(&dev->slock);
555 } else {
556 if (status & FLD_AUD_SRC_OF)
557 pr_warn("%s(): Audio Received Overflow Error Interrupt!\n",
558 __func__);
559
560 if (status & FLD_AUD_SRC_SYNC)
561 pr_warn("%s(): Audio Received Sync Error Interrupt!\n",
562 __func__);
563
564 if (status & FLD_AUD_SRC_OPC_ERR)
565 pr_warn("%s(): Audio Received OpCode Error Interrupt!\n",
566 __func__);
567
568 /* Read and write back the interrupt status register to clear
569 * our bits */
570 cx_write(channel->int_stat, cx_read(channel->int_stat));
571 }
572
573 if (dev->_audiofile_status == END_OF_FILE) {
574 pr_warn("EOF Channel Audio Framecount = %d\n",
575 dev->_audioframe_count);
576 return -1;
577 }
578 /* ElSE, set the interrupt mask register, re-enable irq. */
579 int_msk_tmp = cx_read(channel->int_msk);
580 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
581
582 return 0;
583}
584
585static irqreturn_t cx25821_upstream_irq_audio(int irq, void *dev_id)
586{
587 struct cx25821_dev *dev = dev_id;
588 u32 audio_status;
589 int handled = 0;
590 struct sram_channel *sram_ch;
591
592 if (!dev)
593 return -1;
594
595 sram_ch = dev->channels[dev->_audio_upstream_channel].sram_channels;
596
597 audio_status = cx_read(sram_ch->int_stat);
598
599 /* Only deal with our interrupt */
600 if (audio_status) {
601 handled = cx25821_audio_upstream_irq(dev,
602 dev->_audio_upstream_channel, audio_status);
603 }
604
605 if (handled < 0)
606 cx25821_stop_upstream_audio(dev);
607 else
608 handled += handled;
609
610 return IRQ_RETVAL(handled);
611}
612
613static void cx25821_wait_fifo_enable(struct cx25821_dev *dev,
614 struct sram_channel *sram_ch)
615{
616 int count = 0;
617 u32 tmp;
618
619 do {
620 /* Wait 10 microsecond before checking to see if the FIFO is
621 * turned ON. */
622 udelay(10);
623
624 tmp = cx_read(sram_ch->dma_ctl);
625
626 /* 10 millisecond timeout */
627 if (count++ > 1000) {
628 pr_err("ERROR: %s() fifo is NOT turned on. Timeout!\n",
629 __func__);
630 return;
631 }
632
633 } while (!(tmp & sram_ch->fld_aud_fifo_en));
634
635}
636
637int cx25821_start_audio_dma_upstream(struct cx25821_dev *dev,
638 struct sram_channel *sram_ch)
639{
640 u32 tmp = 0;
641 int err = 0;
642
643 /* Set the physical start address of the RISC program in the initial
644 * program counter(IPC) member of the CMDS. */
645 cx_write(sram_ch->cmds_start + 0, dev->_risc_phys_addr);
646 /* Risc IPC High 64 bits 63-32 */
647 cx_write(sram_ch->cmds_start + 4, 0);
648
649 /* reset counter */
650 cx_write(sram_ch->gpcnt_ctl, 3);
651
652 /* Set the line length (It looks like we do not need to set the
653 * line length) */
654 cx_write(sram_ch->aud_length, AUDIO_LINE_SIZE & FLD_AUD_DST_LN_LNGTH);
655
656 /* Set the input mode to 16-bit */
657 tmp = cx_read(sram_ch->aud_cfg);
658 tmp |= FLD_AUD_SRC_ENABLE | FLD_AUD_DST_PK_MODE | FLD_AUD_CLK_ENABLE |
659 FLD_AUD_MASTER_MODE | FLD_AUD_CLK_SELECT_PLL_D |
660 FLD_AUD_SONY_MODE;
661 cx_write(sram_ch->aud_cfg, tmp);
662
663 /* Read and write back the interrupt status register to clear it */
664 tmp = cx_read(sram_ch->int_stat);
665 cx_write(sram_ch->int_stat, tmp);
666
667 /* Clear our bits from the interrupt status register. */
668 cx_write(sram_ch->int_stat, _intr_msk);
669
670 /* Set the interrupt mask register, enable irq. */
671 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
672 tmp = cx_read(sram_ch->int_msk);
673 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
674
675 err = request_irq(dev->pci->irq, cx25821_upstream_irq_audio,
676 IRQF_SHARED, dev->name, dev);
677 if (err < 0) {
678 pr_err("%s: can't get upstream IRQ %d\n", dev->name,
679 dev->pci->irq);
680 goto fail_irq;
681 }
682
683 /* Start the DMA engine */
684 tmp = cx_read(sram_ch->dma_ctl);
685 cx_set(sram_ch->dma_ctl, tmp | sram_ch->fld_aud_risc_en);
686
687 dev->_audio_is_running = 1;
688 dev->_is_first_audio_frame = 1;
689
690 /* The fifo_en bit turns on by the first Risc program */
691 cx25821_wait_fifo_enable(dev, sram_ch);
692
693 return 0;
694
695fail_irq:
696 cx25821_dev_unregister(dev);
697 return err;
698}
699
700int cx25821_audio_upstream_init(struct cx25821_dev *dev, int channel_select)
701{
702 struct sram_channel *sram_ch;
703 int retval = 0;
704 int err = 0;
705 int str_length = 0;
706
707 if (dev->_audio_is_running) {
708 pr_warn("Audio Channel is still running so return!\n");
709 return 0;
710 }
711
712 dev->_audio_upstream_channel = channel_select;
713 sram_ch = dev->channels[channel_select].sram_channels;
714
715 /* Work queue */
716 INIT_WORK(&dev->_audio_work_entry, cx25821_audioups_handler);
717 dev->_irq_audio_queues =
718 create_singlethread_workqueue("cx25821_audioworkqueue");
719
720 if (!dev->_irq_audio_queues) {
721 printk(KERN_DEBUG
722 pr_fmt("ERROR: create_singlethread_workqueue() for Audio FAILED!\n"));
723 return -ENOMEM;
724 }
725
726 dev->_last_index_irq = 0;
727 dev->_audio_is_running = 0;
728 dev->_audioframe_count = 0;
729 dev->_audiofile_status = RESET_STATUS;
730 dev->_audio_lines_count = LINES_PER_AUDIO_BUFFER;
731 _line_size = AUDIO_LINE_SIZE;
732
733 if (dev->input_audiofilename) {
734 str_length = strlen(dev->input_audiofilename);
735 dev->_audiofilename = kmemdup(dev->input_audiofilename,
736 str_length + 1, GFP_KERNEL);
737
738 if (!dev->_audiofilename)
739 goto error;
740
741 /* Default if filename is empty string */
742 if (strcmp(dev->input_audiofilename, "") == 0)
743 dev->_audiofilename = "/root/audioGOOD.wav";
744 } else {
745 str_length = strlen(_defaultAudioName);
746 dev->_audiofilename = kmemdup(_defaultAudioName,
747 str_length + 1, GFP_KERNEL);
748
749 if (!dev->_audiofilename)
750 goto error;
751 }
752
753 retval = cx25821_sram_channel_setup_upstream_audio(dev, sram_ch,
754 _line_size, 0);
755
756 dev->audio_upstream_riscbuf_size =
757 AUDIO_RISC_DMA_BUF_SIZE * NUM_AUDIO_PROGS +
758 RISC_SYNC_INSTRUCTION_SIZE;
759 dev->audio_upstream_databuf_size = AUDIO_DATA_BUF_SZ * NUM_AUDIO_PROGS;
760
761 /* Allocating buffers and prepare RISC program */
762 retval = cx25821_audio_upstream_buffer_prepare(dev, sram_ch,
763 _line_size);
764 if (retval < 0) {
765 pr_err("%s: Failed to set up Audio upstream buffers!\n",
766 dev->name);
767 goto error;
768 }
769 /* Start RISC engine */
770 cx25821_start_audio_dma_upstream(dev, sram_ch);
771
772 return 0;
773
774error:
775 cx25821_dev_unregister(dev);
776
777 return err;
778}
diff --git a/drivers/media/pci/cx25821/cx25821-audio-upstream.h b/drivers/media/pci/cx25821/cx25821-audio-upstream.h
new file mode 100644
index 000000000000..af2ae7c5815a
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-audio-upstream.h
@@ -0,0 +1,62 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/mutex.h>
24#include <linux/workqueue.h>
25
26#define NUM_AUDIO_PROGS 8
27#define NUM_AUDIO_FRAMES 8
28#define END_OF_FILE 0
29#define IN_PROGRESS 1
30#define RESET_STATUS -1
31#define FIFO_DISABLE 0
32#define FIFO_ENABLE 1
33#define NUM_NO_OPS 4
34
35#define RISC_READ_INSTRUCTION_SIZE 12
36#define RISC_JUMP_INSTRUCTION_SIZE 12
37#define RISC_WRITECR_INSTRUCTION_SIZE 16
38#define RISC_SYNC_INSTRUCTION_SIZE 4
39#define DWORD_SIZE 4
40#define AUDIO_SYNC_LINE 4
41
42#define LINES_PER_AUDIO_BUFFER 15
43#define AUDIO_LINE_SIZE 128
44#define AUDIO_DATA_BUF_SZ (AUDIO_LINE_SIZE * LINES_PER_AUDIO_BUFFER)
45
46#define USE_RISC_NOOP_AUDIO 1
47
48#ifdef USE_RISC_NOOP_AUDIO
49#define AUDIO_RISC_DMA_BUF_SIZE \
50 (LINES_PER_AUDIO_BUFFER * RISC_READ_INSTRUCTION_SIZE + \
51 RISC_WRITECR_INSTRUCTION_SIZE + NUM_NO_OPS * DWORD_SIZE + \
52 RISC_JUMP_INSTRUCTION_SIZE)
53#endif
54
55#ifndef USE_RISC_NOOP_AUDIO
56#define AUDIO_RISC_DMA_BUF_SIZE \
57 (LINES_PER_AUDIO_BUFFER * RISC_READ_INSTRUCTION_SIZE + \
58 RISC_WRITECR_INSTRUCTION_SIZE + RISC_JUMP_INSTRUCTION_SIZE)
59#endif
60
61static int _line_size;
62char *_defaultAudioName = "/root/audioGOOD.wav";
diff --git a/drivers/media/pci/cx25821/cx25821-audio.h b/drivers/media/pci/cx25821/cx25821-audio.h
new file mode 100644
index 000000000000..1fc2d24f5110
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-audio.h
@@ -0,0 +1,62 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#ifndef __CX25821_AUDIO_H__
24#define __CX25821_AUDIO_H__
25
26#define USE_RISC_NOOP 1
27#define LINES_PER_BUFFER 15
28#define AUDIO_LINE_SIZE 128
29
30/* Number of buffer programs to use at once. */
31#define NUMBER_OF_PROGRAMS 8
32
33/*
34 * Max size of the RISC program for a buffer. - worst case is 2 writes per line
35 * Space is also added for the 4 no-op instructions added on the end.
36 */
37#ifndef USE_RISC_NOOP
38#define MAX_BUFFER_PROGRAM_SIZE \
39 (2 * LINES_PER_BUFFER * RISC_WRITE_INSTRUCTION_SIZE + \
40 RISC_WRITECR_INSTRUCTION_SIZE * 4)
41#endif
42
43/* MAE 12 July 2005 Try to use NOOP RISC instruction instead */
44#ifdef USE_RISC_NOOP
45#define MAX_BUFFER_PROGRAM_SIZE \
46 (2 * LINES_PER_BUFFER * RISC_WRITE_INSTRUCTION_SIZE + \
47 RISC_NOOP_INSTRUCTION_SIZE * 4)
48#endif
49
50/* Sizes of various instructions in bytes. Used when adding instructions. */
51#define RISC_WRITE_INSTRUCTION_SIZE 12
52#define RISC_JUMP_INSTRUCTION_SIZE 12
53#define RISC_SKIP_INSTRUCTION_SIZE 4
54#define RISC_SYNC_INSTRUCTION_SIZE 4
55#define RISC_WRITECR_INSTRUCTION_SIZE 16
56#define RISC_NOOP_INSTRUCTION_SIZE 4
57
58#define MAX_AUDIO_DMA_BUFFER_SIZE \
59 (MAX_BUFFER_PROGRAM_SIZE * NUMBER_OF_PROGRAMS + \
60 RISC_SYNC_INSTRUCTION_SIZE)
61
62#endif
diff --git a/drivers/media/pci/cx25821/cx25821-biffuncs.h b/drivers/media/pci/cx25821/cx25821-biffuncs.h
new file mode 100644
index 000000000000..9326a7c729ec
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-biffuncs.h
@@ -0,0 +1,45 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#ifndef _BITFUNCS_H
24#define _BITFUNCS_H
25
26#define SetBit(Bit) (1 << Bit)
27
28inline u8 getBit(u32 sample, u8 index)
29{
30 return (u8) ((sample >> index) & 1);
31}
32
33inline u32 clearBitAtPos(u32 value, u8 bit)
34{
35 return value & ~(1 << bit);
36}
37
38inline u32 setBitAtPos(u32 sample, u8 bit)
39{
40 sample |= (1 << bit);
41 return sample;
42
43}
44
45#endif
diff --git a/drivers/media/pci/cx25821/cx25821-cards.c b/drivers/media/pci/cx25821/cx25821-cards.c
new file mode 100644
index 000000000000..99988c988095
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-cards.c
@@ -0,0 +1,72 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/pci.h>
29#include <linux/delay.h>
30#include <media/cx25840.h>
31
32#include "cx25821.h"
33#include "tuner-xc2028.h"
34
35/* board config info */
36
37struct cx25821_board cx25821_boards[] = {
38 [UNKNOWN_BOARD] = {
39 .name = "UNKNOWN/GENERIC",
40 /* Ensure safe default for unknown boards */
41 .clk_freq = 0,
42 },
43
44 [CX25821_BOARD] = {
45 .name = "CX25821",
46 .portb = CX25821_RAW,
47 .portc = CX25821_264,
48 .input[0].type = CX25821_VMUX_COMPOSITE,
49 },
50
51};
52
53const unsigned int cx25821_bcount = ARRAY_SIZE(cx25821_boards);
54
55struct cx25821_subid cx25821_subids[] = {
56 {
57 .subvendor = 0x14f1,
58 .subdevice = 0x0920,
59 .card = CX25821_BOARD,
60 },
61};
62
63void cx25821_card_setup(struct cx25821_dev *dev)
64{
65 static u8 eeprom[256];
66
67 if (dev->i2c_bus[0].i2c_rc == 0) {
68 dev->i2c_bus[0].i2c_client.addr = 0xa0 >> 1;
69 tveeprom_read(&dev->i2c_bus[0].i2c_client, eeprom,
70 sizeof(eeprom));
71 }
72}
diff --git a/drivers/media/pci/cx25821/cx25821-core.c b/drivers/media/pci/cx25821/cx25821-core.c
new file mode 100644
index 000000000000..f11f6f07e915
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-core.c
@@ -0,0 +1,1502 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
26#include <linux/i2c.h>
27#include <linux/slab.h>
28#include "cx25821.h"
29#include "cx25821-sram.h"
30#include "cx25821-video.h"
31
32MODULE_DESCRIPTION("Driver for Athena cards");
33MODULE_AUTHOR("Shu Lin - Hiep Huynh");
34MODULE_LICENSE("GPL");
35
36static unsigned int debug;
37module_param(debug, int, 0644);
38MODULE_PARM_DESC(debug, "enable debug messages");
39
40static unsigned int card[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
41module_param_array(card, int, NULL, 0444);
42MODULE_PARM_DESC(card, "card type");
43
44static unsigned int cx25821_devcount;
45
46DEFINE_MUTEX(cx25821_devlist_mutex);
47EXPORT_SYMBOL(cx25821_devlist_mutex);
48LIST_HEAD(cx25821_devlist);
49EXPORT_SYMBOL(cx25821_devlist);
50
51struct sram_channel cx25821_sram_channels[] = {
52 [SRAM_CH00] = {
53 .i = SRAM_CH00,
54 .name = "VID A",
55 .cmds_start = VID_A_DOWN_CMDS,
56 .ctrl_start = VID_A_IQ,
57 .cdt = VID_A_CDT,
58 .fifo_start = VID_A_DOWN_CLUSTER_1,
59 .fifo_size = (VID_CLUSTER_SIZE << 2),
60 .ptr1_reg = DMA1_PTR1,
61 .ptr2_reg = DMA1_PTR2,
62 .cnt1_reg = DMA1_CNT1,
63 .cnt2_reg = DMA1_CNT2,
64 .int_msk = VID_A_INT_MSK,
65 .int_stat = VID_A_INT_STAT,
66 .int_mstat = VID_A_INT_MSTAT,
67 .dma_ctl = VID_DST_A_DMA_CTL,
68 .gpcnt_ctl = VID_DST_A_GPCNT_CTL,
69 .gpcnt = VID_DST_A_GPCNT,
70 .vip_ctl = VID_DST_A_VIP_CTL,
71 .pix_frmt = VID_DST_A_PIX_FRMT,
72 },
73
74 [SRAM_CH01] = {
75 .i = SRAM_CH01,
76 .name = "VID B",
77 .cmds_start = VID_B_DOWN_CMDS,
78 .ctrl_start = VID_B_IQ,
79 .cdt = VID_B_CDT,
80 .fifo_start = VID_B_DOWN_CLUSTER_1,
81 .fifo_size = (VID_CLUSTER_SIZE << 2),
82 .ptr1_reg = DMA2_PTR1,
83 .ptr2_reg = DMA2_PTR2,
84 .cnt1_reg = DMA2_CNT1,
85 .cnt2_reg = DMA2_CNT2,
86 .int_msk = VID_B_INT_MSK,
87 .int_stat = VID_B_INT_STAT,
88 .int_mstat = VID_B_INT_MSTAT,
89 .dma_ctl = VID_DST_B_DMA_CTL,
90 .gpcnt_ctl = VID_DST_B_GPCNT_CTL,
91 .gpcnt = VID_DST_B_GPCNT,
92 .vip_ctl = VID_DST_B_VIP_CTL,
93 .pix_frmt = VID_DST_B_PIX_FRMT,
94 },
95
96 [SRAM_CH02] = {
97 .i = SRAM_CH02,
98 .name = "VID C",
99 .cmds_start = VID_C_DOWN_CMDS,
100 .ctrl_start = VID_C_IQ,
101 .cdt = VID_C_CDT,
102 .fifo_start = VID_C_DOWN_CLUSTER_1,
103 .fifo_size = (VID_CLUSTER_SIZE << 2),
104 .ptr1_reg = DMA3_PTR1,
105 .ptr2_reg = DMA3_PTR2,
106 .cnt1_reg = DMA3_CNT1,
107 .cnt2_reg = DMA3_CNT2,
108 .int_msk = VID_C_INT_MSK,
109 .int_stat = VID_C_INT_STAT,
110 .int_mstat = VID_C_INT_MSTAT,
111 .dma_ctl = VID_DST_C_DMA_CTL,
112 .gpcnt_ctl = VID_DST_C_GPCNT_CTL,
113 .gpcnt = VID_DST_C_GPCNT,
114 .vip_ctl = VID_DST_C_VIP_CTL,
115 .pix_frmt = VID_DST_C_PIX_FRMT,
116 },
117
118 [SRAM_CH03] = {
119 .i = SRAM_CH03,
120 .name = "VID D",
121 .cmds_start = VID_D_DOWN_CMDS,
122 .ctrl_start = VID_D_IQ,
123 .cdt = VID_D_CDT,
124 .fifo_start = VID_D_DOWN_CLUSTER_1,
125 .fifo_size = (VID_CLUSTER_SIZE << 2),
126 .ptr1_reg = DMA4_PTR1,
127 .ptr2_reg = DMA4_PTR2,
128 .cnt1_reg = DMA4_CNT1,
129 .cnt2_reg = DMA4_CNT2,
130 .int_msk = VID_D_INT_MSK,
131 .int_stat = VID_D_INT_STAT,
132 .int_mstat = VID_D_INT_MSTAT,
133 .dma_ctl = VID_DST_D_DMA_CTL,
134 .gpcnt_ctl = VID_DST_D_GPCNT_CTL,
135 .gpcnt = VID_DST_D_GPCNT,
136 .vip_ctl = VID_DST_D_VIP_CTL,
137 .pix_frmt = VID_DST_D_PIX_FRMT,
138 },
139
140 [SRAM_CH04] = {
141 .i = SRAM_CH04,
142 .name = "VID E",
143 .cmds_start = VID_E_DOWN_CMDS,
144 .ctrl_start = VID_E_IQ,
145 .cdt = VID_E_CDT,
146 .fifo_start = VID_E_DOWN_CLUSTER_1,
147 .fifo_size = (VID_CLUSTER_SIZE << 2),
148 .ptr1_reg = DMA5_PTR1,
149 .ptr2_reg = DMA5_PTR2,
150 .cnt1_reg = DMA5_CNT1,
151 .cnt2_reg = DMA5_CNT2,
152 .int_msk = VID_E_INT_MSK,
153 .int_stat = VID_E_INT_STAT,
154 .int_mstat = VID_E_INT_MSTAT,
155 .dma_ctl = VID_DST_E_DMA_CTL,
156 .gpcnt_ctl = VID_DST_E_GPCNT_CTL,
157 .gpcnt = VID_DST_E_GPCNT,
158 .vip_ctl = VID_DST_E_VIP_CTL,
159 .pix_frmt = VID_DST_E_PIX_FRMT,
160 },
161
162 [SRAM_CH05] = {
163 .i = SRAM_CH05,
164 .name = "VID F",
165 .cmds_start = VID_F_DOWN_CMDS,
166 .ctrl_start = VID_F_IQ,
167 .cdt = VID_F_CDT,
168 .fifo_start = VID_F_DOWN_CLUSTER_1,
169 .fifo_size = (VID_CLUSTER_SIZE << 2),
170 .ptr1_reg = DMA6_PTR1,
171 .ptr2_reg = DMA6_PTR2,
172 .cnt1_reg = DMA6_CNT1,
173 .cnt2_reg = DMA6_CNT2,
174 .int_msk = VID_F_INT_MSK,
175 .int_stat = VID_F_INT_STAT,
176 .int_mstat = VID_F_INT_MSTAT,
177 .dma_ctl = VID_DST_F_DMA_CTL,
178 .gpcnt_ctl = VID_DST_F_GPCNT_CTL,
179 .gpcnt = VID_DST_F_GPCNT,
180 .vip_ctl = VID_DST_F_VIP_CTL,
181 .pix_frmt = VID_DST_F_PIX_FRMT,
182 },
183
184 [SRAM_CH06] = {
185 .i = SRAM_CH06,
186 .name = "VID G",
187 .cmds_start = VID_G_DOWN_CMDS,
188 .ctrl_start = VID_G_IQ,
189 .cdt = VID_G_CDT,
190 .fifo_start = VID_G_DOWN_CLUSTER_1,
191 .fifo_size = (VID_CLUSTER_SIZE << 2),
192 .ptr1_reg = DMA7_PTR1,
193 .ptr2_reg = DMA7_PTR2,
194 .cnt1_reg = DMA7_CNT1,
195 .cnt2_reg = DMA7_CNT2,
196 .int_msk = VID_G_INT_MSK,
197 .int_stat = VID_G_INT_STAT,
198 .int_mstat = VID_G_INT_MSTAT,
199 .dma_ctl = VID_DST_G_DMA_CTL,
200 .gpcnt_ctl = VID_DST_G_GPCNT_CTL,
201 .gpcnt = VID_DST_G_GPCNT,
202 .vip_ctl = VID_DST_G_VIP_CTL,
203 .pix_frmt = VID_DST_G_PIX_FRMT,
204 },
205
206 [SRAM_CH07] = {
207 .i = SRAM_CH07,
208 .name = "VID H",
209 .cmds_start = VID_H_DOWN_CMDS,
210 .ctrl_start = VID_H_IQ,
211 .cdt = VID_H_CDT,
212 .fifo_start = VID_H_DOWN_CLUSTER_1,
213 .fifo_size = (VID_CLUSTER_SIZE << 2),
214 .ptr1_reg = DMA8_PTR1,
215 .ptr2_reg = DMA8_PTR2,
216 .cnt1_reg = DMA8_CNT1,
217 .cnt2_reg = DMA8_CNT2,
218 .int_msk = VID_H_INT_MSK,
219 .int_stat = VID_H_INT_STAT,
220 .int_mstat = VID_H_INT_MSTAT,
221 .dma_ctl = VID_DST_H_DMA_CTL,
222 .gpcnt_ctl = VID_DST_H_GPCNT_CTL,
223 .gpcnt = VID_DST_H_GPCNT,
224 .vip_ctl = VID_DST_H_VIP_CTL,
225 .pix_frmt = VID_DST_H_PIX_FRMT,
226 },
227
228 [SRAM_CH08] = {
229 .name = "audio from",
230 .cmds_start = AUD_A_DOWN_CMDS,
231 .ctrl_start = AUD_A_IQ,
232 .cdt = AUD_A_CDT,
233 .fifo_start = AUD_A_DOWN_CLUSTER_1,
234 .fifo_size = AUDIO_CLUSTER_SIZE * 3,
235 .ptr1_reg = DMA17_PTR1,
236 .ptr2_reg = DMA17_PTR2,
237 .cnt1_reg = DMA17_CNT1,
238 .cnt2_reg = DMA17_CNT2,
239 },
240
241 [SRAM_CH09] = {
242 .i = SRAM_CH09,
243 .name = "VID Upstream I",
244 .cmds_start = VID_I_UP_CMDS,
245 .ctrl_start = VID_I_IQ,
246 .cdt = VID_I_CDT,
247 .fifo_start = VID_I_UP_CLUSTER_1,
248 .fifo_size = (VID_CLUSTER_SIZE << 2),
249 .ptr1_reg = DMA15_PTR1,
250 .ptr2_reg = DMA15_PTR2,
251 .cnt1_reg = DMA15_CNT1,
252 .cnt2_reg = DMA15_CNT2,
253 .int_msk = VID_I_INT_MSK,
254 .int_stat = VID_I_INT_STAT,
255 .int_mstat = VID_I_INT_MSTAT,
256 .dma_ctl = VID_SRC_I_DMA_CTL,
257 .gpcnt_ctl = VID_SRC_I_GPCNT_CTL,
258 .gpcnt = VID_SRC_I_GPCNT,
259
260 .vid_fmt_ctl = VID_SRC_I_FMT_CTL,
261 .vid_active_ctl1 = VID_SRC_I_ACTIVE_CTL1,
262 .vid_active_ctl2 = VID_SRC_I_ACTIVE_CTL2,
263 .vid_cdt_size = VID_SRC_I_CDT_SZ,
264 .irq_bit = 8,
265 },
266
267 [SRAM_CH10] = {
268 .i = SRAM_CH10,
269 .name = "VID Upstream J",
270 .cmds_start = VID_J_UP_CMDS,
271 .ctrl_start = VID_J_IQ,
272 .cdt = VID_J_CDT,
273 .fifo_start = VID_J_UP_CLUSTER_1,
274 .fifo_size = (VID_CLUSTER_SIZE << 2),
275 .ptr1_reg = DMA16_PTR1,
276 .ptr2_reg = DMA16_PTR2,
277 .cnt1_reg = DMA16_CNT1,
278 .cnt2_reg = DMA16_CNT2,
279 .int_msk = VID_J_INT_MSK,
280 .int_stat = VID_J_INT_STAT,
281 .int_mstat = VID_J_INT_MSTAT,
282 .dma_ctl = VID_SRC_J_DMA_CTL,
283 .gpcnt_ctl = VID_SRC_J_GPCNT_CTL,
284 .gpcnt = VID_SRC_J_GPCNT,
285
286 .vid_fmt_ctl = VID_SRC_J_FMT_CTL,
287 .vid_active_ctl1 = VID_SRC_J_ACTIVE_CTL1,
288 .vid_active_ctl2 = VID_SRC_J_ACTIVE_CTL2,
289 .vid_cdt_size = VID_SRC_J_CDT_SZ,
290 .irq_bit = 9,
291 },
292
293 [SRAM_CH11] = {
294 .i = SRAM_CH11,
295 .name = "Audio Upstream Channel B",
296 .cmds_start = AUD_B_UP_CMDS,
297 .ctrl_start = AUD_B_IQ,
298 .cdt = AUD_B_CDT,
299 .fifo_start = AUD_B_UP_CLUSTER_1,
300 .fifo_size = (AUDIO_CLUSTER_SIZE * 3),
301 .ptr1_reg = DMA22_PTR1,
302 .ptr2_reg = DMA22_PTR2,
303 .cnt1_reg = DMA22_CNT1,
304 .cnt2_reg = DMA22_CNT2,
305 .int_msk = AUD_B_INT_MSK,
306 .int_stat = AUD_B_INT_STAT,
307 .int_mstat = AUD_B_INT_MSTAT,
308 .dma_ctl = AUD_INT_DMA_CTL,
309 .gpcnt_ctl = AUD_B_GPCNT_CTL,
310 .gpcnt = AUD_B_GPCNT,
311 .aud_length = AUD_B_LNGTH,
312 .aud_cfg = AUD_B_CFG,
313 .fld_aud_fifo_en = FLD_AUD_SRC_B_FIFO_EN,
314 .fld_aud_risc_en = FLD_AUD_SRC_B_RISC_EN,
315 .irq_bit = 11,
316 },
317};
318EXPORT_SYMBOL(cx25821_sram_channels);
319
320struct sram_channel *channel0 = &cx25821_sram_channels[SRAM_CH00];
321struct sram_channel *channel1 = &cx25821_sram_channels[SRAM_CH01];
322struct sram_channel *channel2 = &cx25821_sram_channels[SRAM_CH02];
323struct sram_channel *channel3 = &cx25821_sram_channels[SRAM_CH03];
324struct sram_channel *channel4 = &cx25821_sram_channels[SRAM_CH04];
325struct sram_channel *channel5 = &cx25821_sram_channels[SRAM_CH05];
326struct sram_channel *channel6 = &cx25821_sram_channels[SRAM_CH06];
327struct sram_channel *channel7 = &cx25821_sram_channels[SRAM_CH07];
328struct sram_channel *channel9 = &cx25821_sram_channels[SRAM_CH09];
329struct sram_channel *channel10 = &cx25821_sram_channels[SRAM_CH10];
330struct sram_channel *channel11 = &cx25821_sram_channels[SRAM_CH11];
331
332struct cx25821_dmaqueue mpegq;
333
334static int cx25821_risc_decode(u32 risc)
335{
336 static const char * const instr[16] = {
337 [RISC_SYNC >> 28] = "sync",
338 [RISC_WRITE >> 28] = "write",
339 [RISC_WRITEC >> 28] = "writec",
340 [RISC_READ >> 28] = "read",
341 [RISC_READC >> 28] = "readc",
342 [RISC_JUMP >> 28] = "jump",
343 [RISC_SKIP >> 28] = "skip",
344 [RISC_WRITERM >> 28] = "writerm",
345 [RISC_WRITECM >> 28] = "writecm",
346 [RISC_WRITECR >> 28] = "writecr",
347 };
348 static const int incr[16] = {
349 [RISC_WRITE >> 28] = 3,
350 [RISC_JUMP >> 28] = 3,
351 [RISC_SKIP >> 28] = 1,
352 [RISC_SYNC >> 28] = 1,
353 [RISC_WRITERM >> 28] = 3,
354 [RISC_WRITECM >> 28] = 3,
355 [RISC_WRITECR >> 28] = 4,
356 };
357 static const char * const bits[] = {
358 "12", "13", "14", "resync",
359 "cnt0", "cnt1", "18", "19",
360 "20", "21", "22", "23",
361 "irq1", "irq2", "eol", "sol",
362 };
363 int i;
364
365 pr_cont("0x%08x [ %s",
366 risc, instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
367 for (i = ARRAY_SIZE(bits) - 1; i >= 0; i--) {
368 if (risc & (1 << (i + 12)))
369 pr_cont(" %s", bits[i]);
370 }
371 pr_cont(" count=%d ]\n", risc & 0xfff);
372 return incr[risc >> 28] ? incr[risc >> 28] : 1;
373}
374
375static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
376{
377 struct cx25821_i2c *bus = i2c_adap->algo_data;
378 struct cx25821_dev *dev = bus->dev;
379 return cx_read(bus->reg_stat) & 0x01;
380}
381
382static void cx25821_registers_init(struct cx25821_dev *dev)
383{
384 u32 tmp;
385
386 /* enable RUN_RISC in Pecos */
387 cx_write(DEV_CNTRL2, 0x20);
388
389 /* Set the master PCI interrupt masks to enable video, audio, MBIF,
390 * and GPIO interrupts
391 * I2C interrupt masking is handled by the I2C objects themselves. */
392 cx_write(PCI_INT_MSK, 0x2001FFFF);
393
394 tmp = cx_read(RDR_TLCTL0);
395 tmp &= ~FLD_CFG_RCB_CK_EN; /* Clear the RCB_CK_EN bit */
396 cx_write(RDR_TLCTL0, tmp);
397
398 /* PLL-A setting for the Audio Master Clock */
399 cx_write(PLL_A_INT_FRAC, 0x9807A58B);
400
401 /* PLL_A_POST = 0x1C, PLL_A_OUT_TO_PIN = 0x1 */
402 cx_write(PLL_A_POST_STAT_BIST, 0x8000019C);
403
404 /* clear reset bit [31] */
405 tmp = cx_read(PLL_A_INT_FRAC);
406 cx_write(PLL_A_INT_FRAC, tmp & 0x7FFFFFFF);
407
408 /* PLL-B setting for Mobilygen Host Bus Interface */
409 cx_write(PLL_B_INT_FRAC, 0x9883A86F);
410
411 /* PLL_B_POST = 0xD, PLL_B_OUT_TO_PIN = 0x0 */
412 cx_write(PLL_B_POST_STAT_BIST, 0x8000018D);
413
414 /* clear reset bit [31] */
415 tmp = cx_read(PLL_B_INT_FRAC);
416 cx_write(PLL_B_INT_FRAC, tmp & 0x7FFFFFFF);
417
418 /* PLL-C setting for video upstream channel */
419 cx_write(PLL_C_INT_FRAC, 0x96A0EA3F);
420
421 /* PLL_C_POST = 0x3, PLL_C_OUT_TO_PIN = 0x0 */
422 cx_write(PLL_C_POST_STAT_BIST, 0x80000103);
423
424 /* clear reset bit [31] */
425 tmp = cx_read(PLL_C_INT_FRAC);
426 cx_write(PLL_C_INT_FRAC, tmp & 0x7FFFFFFF);
427
428 /* PLL-D setting for audio upstream channel */
429 cx_write(PLL_D_INT_FRAC, 0x98757F5B);
430
431 /* PLL_D_POST = 0x13, PLL_D_OUT_TO_PIN = 0x0 */
432 cx_write(PLL_D_POST_STAT_BIST, 0x80000113);
433
434 /* clear reset bit [31] */
435 tmp = cx_read(PLL_D_INT_FRAC);
436 cx_write(PLL_D_INT_FRAC, tmp & 0x7FFFFFFF);
437
438 /* This selects the PLL C clock source for the video upstream channel
439 * I and J */
440 tmp = cx_read(VID_CH_CLK_SEL);
441 cx_write(VID_CH_CLK_SEL, (tmp & 0x00FFFFFF) | 0x24000000);
442
443 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
444 * channel A-C
445 * select 656/VIP DST for downstream Channel A - C */
446 tmp = cx_read(VID_CH_MODE_SEL);
447 /* cx_write( VID_CH_MODE_SEL, tmp | 0x1B0001FF); */
448 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
449
450 /* enables 656 port I and J as output */
451 tmp = cx_read(CLK_RST);
452 /* use external ALT_PLL_REF pin as its reference clock instead */
453 tmp |= FLD_USE_ALT_PLL_REF;
454 cx_write(CLK_RST, tmp & ~(FLD_VID_I_CLK_NOE | FLD_VID_J_CLK_NOE));
455
456 mdelay(100);
457}
458
459int cx25821_sram_channel_setup(struct cx25821_dev *dev,
460 struct sram_channel *ch,
461 unsigned int bpl, u32 risc)
462{
463 unsigned int i, lines;
464 u32 cdt;
465
466 if (ch->cmds_start == 0) {
467 cx_write(ch->ptr1_reg, 0);
468 cx_write(ch->ptr2_reg, 0);
469 cx_write(ch->cnt2_reg, 0);
470 cx_write(ch->cnt1_reg, 0);
471 return 0;
472 }
473
474 bpl = (bpl + 7) & ~7; /* alignment */
475 cdt = ch->cdt;
476 lines = ch->fifo_size / bpl;
477
478 if (lines > 4)
479 lines = 4;
480
481 BUG_ON(lines < 2);
482
483 cx_write(8 + 0, RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
484 cx_write(8 + 4, 8);
485 cx_write(8 + 8, 0);
486
487 /* write CDT */
488 for (i = 0; i < lines; i++) {
489 cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
490 cx_write(cdt + 16 * i + 4, 0);
491 cx_write(cdt + 16 * i + 8, 0);
492 cx_write(cdt + 16 * i + 12, 0);
493 }
494
495 /* init the first cdt buffer */
496 for (i = 0; i < 128; i++)
497 cx_write(ch->fifo_start + 4 * i, i);
498
499 /* write CMDS */
500 if (ch->jumponly)
501 cx_write(ch->cmds_start + 0, 8);
502 else
503 cx_write(ch->cmds_start + 0, risc);
504
505 cx_write(ch->cmds_start + 4, 0); /* 64 bits 63-32 */
506 cx_write(ch->cmds_start + 8, cdt);
507 cx_write(ch->cmds_start + 12, (lines * 16) >> 3);
508 cx_write(ch->cmds_start + 16, ch->ctrl_start);
509
510 if (ch->jumponly)
511 cx_write(ch->cmds_start + 20, 0x80000000 | (64 >> 2));
512 else
513 cx_write(ch->cmds_start + 20, 64 >> 2);
514
515 for (i = 24; i < 80; i += 4)
516 cx_write(ch->cmds_start + i, 0);
517
518 /* fill registers */
519 cx_write(ch->ptr1_reg, ch->fifo_start);
520 cx_write(ch->ptr2_reg, cdt);
521 cx_write(ch->cnt2_reg, (lines * 16) >> 3);
522 cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
523
524 return 0;
525}
526EXPORT_SYMBOL(cx25821_sram_channel_setup);
527
528int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
529 struct sram_channel *ch,
530 unsigned int bpl, u32 risc)
531{
532 unsigned int i, lines;
533 u32 cdt;
534
535 if (ch->cmds_start == 0) {
536 cx_write(ch->ptr1_reg, 0);
537 cx_write(ch->ptr2_reg, 0);
538 cx_write(ch->cnt2_reg, 0);
539 cx_write(ch->cnt1_reg, 0);
540 return 0;
541 }
542
543 bpl = (bpl + 7) & ~7; /* alignment */
544 cdt = ch->cdt;
545 lines = ch->fifo_size / bpl;
546
547 if (lines > 3)
548 lines = 3; /* for AUDIO */
549
550 BUG_ON(lines < 2);
551
552 cx_write(8 + 0, RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
553 cx_write(8 + 4, 8);
554 cx_write(8 + 8, 0);
555
556 /* write CDT */
557 for (i = 0; i < lines; i++) {
558 cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
559 cx_write(cdt + 16 * i + 4, 0);
560 cx_write(cdt + 16 * i + 8, 0);
561 cx_write(cdt + 16 * i + 12, 0);
562 }
563
564 /* write CMDS */
565 if (ch->jumponly)
566 cx_write(ch->cmds_start + 0, 8);
567 else
568 cx_write(ch->cmds_start + 0, risc);
569
570 cx_write(ch->cmds_start + 4, 0); /* 64 bits 63-32 */
571 cx_write(ch->cmds_start + 8, cdt);
572 cx_write(ch->cmds_start + 12, (lines * 16) >> 3);
573 cx_write(ch->cmds_start + 16, ch->ctrl_start);
574
575 /* IQ size */
576 if (ch->jumponly)
577 cx_write(ch->cmds_start + 20, 0x80000000 | (64 >> 2));
578 else
579 cx_write(ch->cmds_start + 20, 64 >> 2);
580
581 /* zero out */
582 for (i = 24; i < 80; i += 4)
583 cx_write(ch->cmds_start + i, 0);
584
585 /* fill registers */
586 cx_write(ch->ptr1_reg, ch->fifo_start);
587 cx_write(ch->ptr2_reg, cdt);
588 cx_write(ch->cnt2_reg, (lines * 16) >> 3);
589 cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
590
591 return 0;
592}
593EXPORT_SYMBOL(cx25821_sram_channel_setup_audio);
594
595void cx25821_sram_channel_dump(struct cx25821_dev *dev, struct sram_channel *ch)
596{
597 static char *name[] = {
598 "init risc lo",
599 "init risc hi",
600 "cdt base",
601 "cdt size",
602 "iq base",
603 "iq size",
604 "risc pc lo",
605 "risc pc hi",
606 "iq wr ptr",
607 "iq rd ptr",
608 "cdt current",
609 "pci target lo",
610 "pci target hi",
611 "line / byte",
612 };
613 u32 risc;
614 unsigned int i, j, n;
615
616 pr_warn("%s: %s - dma channel status dump\n", dev->name, ch->name);
617 for (i = 0; i < ARRAY_SIZE(name); i++)
618 pr_warn("cmds + 0x%2x: %-15s: 0x%08x\n",
619 i * 4, name[i], cx_read(ch->cmds_start + 4 * i));
620
621 j = i * 4;
622 for (i = 0; i < 4;) {
623 risc = cx_read(ch->cmds_start + 4 * (i + 14));
624 pr_warn("cmds + 0x%2x: risc%d: ", j + i * 4, i);
625 i += cx25821_risc_decode(risc);
626 }
627
628 for (i = 0; i < (64 >> 2); i += n) {
629 risc = cx_read(ch->ctrl_start + 4 * i);
630 /* No consideration for bits 63-32 */
631
632 pr_warn("ctrl + 0x%2x (0x%08x): iq %x: ",
633 i * 4, ch->ctrl_start + 4 * i, i);
634 n = cx25821_risc_decode(risc);
635 for (j = 1; j < n; j++) {
636 risc = cx_read(ch->ctrl_start + 4 * (i + j));
637 pr_warn("ctrl + 0x%2x : iq %x: 0x%08x [ arg #%d ]\n",
638 4 * (i + j), i + j, risc, j);
639 }
640 }
641
642 pr_warn(" : fifo: 0x%08x -> 0x%x\n",
643 ch->fifo_start, ch->fifo_start + ch->fifo_size);
644 pr_warn(" : ctrl: 0x%08x -> 0x%x\n",
645 ch->ctrl_start, ch->ctrl_start + 6 * 16);
646 pr_warn(" : ptr1_reg: 0x%08x\n",
647 cx_read(ch->ptr1_reg));
648 pr_warn(" : ptr2_reg: 0x%08x\n",
649 cx_read(ch->ptr2_reg));
650 pr_warn(" : cnt1_reg: 0x%08x\n",
651 cx_read(ch->cnt1_reg));
652 pr_warn(" : cnt2_reg: 0x%08x\n",
653 cx_read(ch->cnt2_reg));
654}
655EXPORT_SYMBOL(cx25821_sram_channel_dump);
656
657void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
658 struct sram_channel *ch)
659{
660 static const char * const name[] = {
661 "init risc lo",
662 "init risc hi",
663 "cdt base",
664 "cdt size",
665 "iq base",
666 "iq size",
667 "risc pc lo",
668 "risc pc hi",
669 "iq wr ptr",
670 "iq rd ptr",
671 "cdt current",
672 "pci target lo",
673 "pci target hi",
674 "line / byte",
675 };
676
677 u32 risc, value, tmp;
678 unsigned int i, j, n;
679
680 pr_info("\n%s: %s - dma Audio channel status dump\n",
681 dev->name, ch->name);
682
683 for (i = 0; i < ARRAY_SIZE(name); i++)
684 pr_info("%s: cmds + 0x%2x: %-15s: 0x%08x\n",
685 dev->name, i * 4, name[i],
686 cx_read(ch->cmds_start + 4 * i));
687
688 j = i * 4;
689 for (i = 0; i < 4;) {
690 risc = cx_read(ch->cmds_start + 4 * (i + 14));
691 pr_warn("cmds + 0x%2x: risc%d: ", j + i * 4, i);
692 i += cx25821_risc_decode(risc);
693 }
694
695 for (i = 0; i < (64 >> 2); i += n) {
696 risc = cx_read(ch->ctrl_start + 4 * i);
697 /* No consideration for bits 63-32 */
698
699 pr_warn("ctrl + 0x%2x (0x%08x): iq %x: ",
700 i * 4, ch->ctrl_start + 4 * i, i);
701 n = cx25821_risc_decode(risc);
702
703 for (j = 1; j < n; j++) {
704 risc = cx_read(ch->ctrl_start + 4 * (i + j));
705 pr_warn("ctrl + 0x%2x : iq %x: 0x%08x [ arg #%d ]\n",
706 4 * (i + j), i + j, risc, j);
707 }
708 }
709
710 pr_warn(" : fifo: 0x%08x -> 0x%x\n",
711 ch->fifo_start, ch->fifo_start + ch->fifo_size);
712 pr_warn(" : ctrl: 0x%08x -> 0x%x\n",
713 ch->ctrl_start, ch->ctrl_start + 6 * 16);
714 pr_warn(" : ptr1_reg: 0x%08x\n",
715 cx_read(ch->ptr1_reg));
716 pr_warn(" : ptr2_reg: 0x%08x\n",
717 cx_read(ch->ptr2_reg));
718 pr_warn(" : cnt1_reg: 0x%08x\n",
719 cx_read(ch->cnt1_reg));
720 pr_warn(" : cnt2_reg: 0x%08x\n",
721 cx_read(ch->cnt2_reg));
722
723 for (i = 0; i < 4; i++) {
724 risc = cx_read(ch->cmds_start + 56 + (i * 4));
725 pr_warn("instruction %d = 0x%x\n", i, risc);
726 }
727
728 /* read data from the first cdt buffer */
729 risc = cx_read(AUD_A_CDT);
730 pr_warn("\nread cdt loc=0x%x\n", risc);
731 for (i = 0; i < 8; i++) {
732 n = cx_read(risc + i * 4);
733 pr_cont("0x%x ", n);
734 }
735 pr_cont("\n\n");
736
737 value = cx_read(CLK_RST);
738 CX25821_INFO(" CLK_RST = 0x%x\n\n", value);
739
740 value = cx_read(PLL_A_POST_STAT_BIST);
741 CX25821_INFO(" PLL_A_POST_STAT_BIST = 0x%x\n\n", value);
742 value = cx_read(PLL_A_INT_FRAC);
743 CX25821_INFO(" PLL_A_INT_FRAC = 0x%x\n\n", value);
744
745 value = cx_read(PLL_B_POST_STAT_BIST);
746 CX25821_INFO(" PLL_B_POST_STAT_BIST = 0x%x\n\n", value);
747 value = cx_read(PLL_B_INT_FRAC);
748 CX25821_INFO(" PLL_B_INT_FRAC = 0x%x\n\n", value);
749
750 value = cx_read(PLL_C_POST_STAT_BIST);
751 CX25821_INFO(" PLL_C_POST_STAT_BIST = 0x%x\n\n", value);
752 value = cx_read(PLL_C_INT_FRAC);
753 CX25821_INFO(" PLL_C_INT_FRAC = 0x%x\n\n", value);
754
755 value = cx_read(PLL_D_POST_STAT_BIST);
756 CX25821_INFO(" PLL_D_POST_STAT_BIST = 0x%x\n\n", value);
757 value = cx_read(PLL_D_INT_FRAC);
758 CX25821_INFO(" PLL_D_INT_FRAC = 0x%x\n\n", value);
759
760 value = cx25821_i2c_read(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL, &tmp);
761 CX25821_INFO(" AFE_AB_DIAG_CTRL (0x10900090) = 0x%x\n\n", value);
762}
763EXPORT_SYMBOL(cx25821_sram_channel_dump_audio);
764
765static void cx25821_shutdown(struct cx25821_dev *dev)
766{
767 int i;
768
769 /* disable RISC controller */
770 cx_write(DEV_CNTRL2, 0);
771
772 /* Disable Video A/B activity */
773 for (i = 0; i < VID_CHANNEL_NUM; i++) {
774 cx_write(dev->channels[i].sram_channels->dma_ctl, 0);
775 cx_write(dev->channels[i].sram_channels->int_msk, 0);
776 }
777
778 for (i = VID_UPSTREAM_SRAM_CHANNEL_I;
779 i <= VID_UPSTREAM_SRAM_CHANNEL_J; i++) {
780 cx_write(dev->channels[i].sram_channels->dma_ctl, 0);
781 cx_write(dev->channels[i].sram_channels->int_msk, 0);
782 }
783
784 /* Disable Audio activity */
785 cx_write(AUD_INT_DMA_CTL, 0);
786
787 /* Disable Serial port */
788 cx_write(UART_CTL, 0);
789
790 /* Disable Interrupts */
791 cx_write(PCI_INT_MSK, 0);
792 cx_write(AUD_A_INT_MSK, 0);
793}
794
795void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel_select,
796 u32 format)
797{
798 if (channel_select <= 7 && channel_select >= 0) {
799 cx_write(dev->channels[channel_select].sram_channels->pix_frmt,
800 format);
801 dev->channels[channel_select].pixel_formats = format;
802 }
803}
804
805static void cx25821_set_vip_mode(struct cx25821_dev *dev,
806 struct sram_channel *ch)
807{
808 cx_write(ch->pix_frmt, PIXEL_FRMT_422);
809 cx_write(ch->vip_ctl, PIXEL_ENGINE_VIP1);
810}
811
812static void cx25821_initialize(struct cx25821_dev *dev)
813{
814 int i;
815
816 dprintk(1, "%s()\n", __func__);
817
818 cx25821_shutdown(dev);
819 cx_write(PCI_INT_STAT, 0xffffffff);
820
821 for (i = 0; i < VID_CHANNEL_NUM; i++)
822 cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
823
824 cx_write(AUD_A_INT_STAT, 0xffffffff);
825 cx_write(AUD_B_INT_STAT, 0xffffffff);
826 cx_write(AUD_C_INT_STAT, 0xffffffff);
827 cx_write(AUD_D_INT_STAT, 0xffffffff);
828 cx_write(AUD_E_INT_STAT, 0xffffffff);
829
830 cx_write(CLK_DELAY, cx_read(CLK_DELAY) & 0x80000000);
831 cx_write(PAD_CTRL, 0x12); /* for I2C */
832 cx25821_registers_init(dev); /* init Pecos registers */
833 mdelay(100);
834
835 for (i = 0; i < VID_CHANNEL_NUM; i++) {
836 cx25821_set_vip_mode(dev, dev->channels[i].sram_channels);
837 cx25821_sram_channel_setup(dev, dev->channels[i].sram_channels,
838 1440, 0);
839 dev->channels[i].pixel_formats = PIXEL_FRMT_422;
840 dev->channels[i].use_cif_resolution = FALSE;
841 }
842
843 /* Probably only affect Downstream */
844 for (i = VID_UPSTREAM_SRAM_CHANNEL_I;
845 i <= VID_UPSTREAM_SRAM_CHANNEL_J; i++) {
846 cx25821_set_vip_mode(dev, dev->channels[i].sram_channels);
847 }
848
849 cx25821_sram_channel_setup_audio(dev,
850 dev->channels[SRAM_CH08].sram_channels, 128, 0);
851
852 cx25821_gpio_init(dev);
853}
854
855static int cx25821_get_resources(struct cx25821_dev *dev)
856{
857 if (request_mem_region(pci_resource_start(dev->pci, 0),
858 pci_resource_len(dev->pci, 0), dev->name))
859 return 0;
860
861 pr_err("%s: can't get MMIO memory @ 0x%llx\n",
862 dev->name, (unsigned long long)pci_resource_start(dev->pci, 0));
863
864 return -EBUSY;
865}
866
867static void cx25821_dev_checkrevision(struct cx25821_dev *dev)
868{
869 dev->hwrevision = cx_read(RDR_CFG2) & 0xff;
870
871 pr_info("%s(): Hardware revision = 0x%02x\n",
872 __func__, dev->hwrevision);
873}
874
875static void cx25821_iounmap(struct cx25821_dev *dev)
876{
877 if (dev == NULL)
878 return;
879
880 /* Releasing IO memory */
881 if (dev->lmmio != NULL) {
882 CX25821_INFO("Releasing lmmio.\n");
883 iounmap(dev->lmmio);
884 dev->lmmio = NULL;
885 }
886}
887
888static int cx25821_dev_setup(struct cx25821_dev *dev)
889{
890 int i;
891
892 pr_info("\n***********************************\n");
893 pr_info("cx25821 set up\n");
894 pr_info("***********************************\n\n");
895
896 mutex_init(&dev->lock);
897
898 atomic_inc(&dev->refcount);
899
900 dev->nr = ++cx25821_devcount;
901 sprintf(dev->name, "cx25821[%d]", dev->nr);
902
903 mutex_lock(&cx25821_devlist_mutex);
904 list_add_tail(&dev->devlist, &cx25821_devlist);
905 mutex_unlock(&cx25821_devlist_mutex);
906
907 if (dev->pci->device != 0x8210) {
908 pr_info("%s(): Exiting. Incorrect Hardware device = 0x%02x\n",
909 __func__, dev->pci->device);
910 return -1;
911 } else {
912 pr_info("Athena Hardware device = 0x%02x\n", dev->pci->device);
913 }
914
915 /* Apply a sensible clock frequency for the PCIe bridge */
916 dev->clk_freq = 28000000;
917 for (i = 0; i < MAX_VID_CHANNEL_NUM; i++)
918 dev->channels[i].sram_channels = &cx25821_sram_channels[i];
919
920 if (dev->nr > 1)
921 CX25821_INFO("dev->nr > 1!");
922
923 /* board config */
924 dev->board = 1; /* card[dev->nr]; */
925 dev->_max_num_decoders = MAX_DECODERS;
926
927 dev->pci_bus = dev->pci->bus->number;
928 dev->pci_slot = PCI_SLOT(dev->pci->devfn);
929 dev->pci_irqmask = 0x001f00;
930
931 /* External Master 1 Bus */
932 dev->i2c_bus[0].nr = 0;
933 dev->i2c_bus[0].dev = dev;
934 dev->i2c_bus[0].reg_stat = I2C1_STAT;
935 dev->i2c_bus[0].reg_ctrl = I2C1_CTRL;
936 dev->i2c_bus[0].reg_addr = I2C1_ADDR;
937 dev->i2c_bus[0].reg_rdata = I2C1_RDATA;
938 dev->i2c_bus[0].reg_wdata = I2C1_WDATA;
939 dev->i2c_bus[0].i2c_period = (0x07 << 24); /* 1.95MHz */
940
941 if (cx25821_get_resources(dev) < 0) {
942 pr_err("%s: No more PCIe resources for subsystem: %04x:%04x\n",
943 dev->name, dev->pci->subsystem_vendor,
944 dev->pci->subsystem_device);
945
946 cx25821_devcount--;
947 return -EBUSY;
948 }
949
950 /* PCIe stuff */
951 dev->base_io_addr = pci_resource_start(dev->pci, 0);
952
953 if (!dev->base_io_addr) {
954 CX25821_ERR("No PCI Memory resources, exiting!\n");
955 return -ENODEV;
956 }
957
958 dev->lmmio = ioremap(dev->base_io_addr, pci_resource_len(dev->pci, 0));
959
960 if (!dev->lmmio) {
961 CX25821_ERR("ioremap failed, maybe increasing __VMALLOC_RESERVE in page.h\n");
962 cx25821_iounmap(dev);
963 return -ENOMEM;
964 }
965
966 dev->bmmio = (u8 __iomem *) dev->lmmio;
967
968 pr_info("%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
969 dev->name, dev->pci->subsystem_vendor,
970 dev->pci->subsystem_device, cx25821_boards[dev->board].name,
971 dev->board, card[dev->nr] == dev->board ?
972 "insmod option" : "autodetected");
973
974 /* init hardware */
975 cx25821_initialize(dev);
976
977 cx25821_i2c_register(&dev->i2c_bus[0]);
978/* cx25821_i2c_register(&dev->i2c_bus[1]);
979 * cx25821_i2c_register(&dev->i2c_bus[2]); */
980
981 CX25821_INFO("i2c register! bus->i2c_rc = %d\n",
982 dev->i2c_bus[0].i2c_rc);
983
984 cx25821_card_setup(dev);
985
986 if (medusa_video_init(dev) < 0)
987 CX25821_ERR("%s(): Failed to initialize medusa!\n", __func__);
988
989 cx25821_video_register(dev);
990
991 /* register IOCTL device */
992 dev->ioctl_dev = cx25821_vdev_init(dev, dev->pci,
993 &cx25821_videoioctl_template, "video");
994
995 if (video_register_device
996 (dev->ioctl_dev, VFL_TYPE_GRABBER, VIDEO_IOCTL_CH) < 0) {
997 cx25821_videoioctl_unregister(dev);
998 pr_err("%s(): Failed to register video adapter for IOCTL, so unregistering videoioctl device\n",
999 __func__);
1000 }
1001
1002 cx25821_dev_checkrevision(dev);
1003 CX25821_INFO("setup done!\n");
1004
1005 return 0;
1006}
1007
1008void cx25821_start_upstream_video_ch1(struct cx25821_dev *dev,
1009 struct upstream_user_struct *up_data)
1010{
1011 dev->_isNTSC = !strcmp(dev->vid_stdname, "NTSC") ? 1 : 0;
1012
1013 dev->tvnorm = !dev->_isNTSC ? V4L2_STD_PAL_BG : V4L2_STD_NTSC_M;
1014 medusa_set_videostandard(dev);
1015
1016 cx25821_vidupstream_init_ch1(dev, dev->channel_select,
1017 dev->pixel_format);
1018}
1019
1020void cx25821_start_upstream_video_ch2(struct cx25821_dev *dev,
1021 struct upstream_user_struct *up_data)
1022{
1023 dev->_isNTSC_ch2 = !strcmp(dev->vid_stdname_ch2, "NTSC") ? 1 : 0;
1024
1025 dev->tvnorm = !dev->_isNTSC_ch2 ? V4L2_STD_PAL_BG : V4L2_STD_NTSC_M;
1026 medusa_set_videostandard(dev);
1027
1028 cx25821_vidupstream_init_ch2(dev, dev->channel_select_ch2,
1029 dev->pixel_format_ch2);
1030}
1031
1032void cx25821_start_upstream_audio(struct cx25821_dev *dev,
1033 struct upstream_user_struct *up_data)
1034{
1035 cx25821_audio_upstream_init(dev, AUDIO_UPSTREAM_SRAM_CHANNEL_B);
1036}
1037
1038void cx25821_dev_unregister(struct cx25821_dev *dev)
1039{
1040 int i;
1041
1042 if (!dev->base_io_addr)
1043 return;
1044
1045 cx25821_free_mem_upstream_ch1(dev);
1046 cx25821_free_mem_upstream_ch2(dev);
1047 cx25821_free_mem_upstream_audio(dev);
1048
1049 release_mem_region(dev->base_io_addr, pci_resource_len(dev->pci, 0));
1050
1051 if (!atomic_dec_and_test(&dev->refcount))
1052 return;
1053
1054 for (i = 0; i < VID_CHANNEL_NUM; i++)
1055 cx25821_video_unregister(dev, i);
1056
1057 for (i = VID_UPSTREAM_SRAM_CHANNEL_I;
1058 i <= AUDIO_UPSTREAM_SRAM_CHANNEL_B; i++) {
1059 cx25821_video_unregister(dev, i);
1060 }
1061
1062 cx25821_videoioctl_unregister(dev);
1063
1064 cx25821_i2c_unregister(&dev->i2c_bus[0]);
1065 cx25821_iounmap(dev);
1066}
1067EXPORT_SYMBOL(cx25821_dev_unregister);
1068
1069static __le32 *cx25821_risc_field(__le32 * rp, struct scatterlist *sglist,
1070 unsigned int offset, u32 sync_line,
1071 unsigned int bpl, unsigned int padding,
1072 unsigned int lines)
1073{
1074 struct scatterlist *sg;
1075 unsigned int line, todo;
1076
1077 /* sync instruction */
1078 if (sync_line != NO_SYNC_LINE)
1079 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
1080
1081 /* scan lines */
1082 sg = sglist;
1083 for (line = 0; line < lines; line++) {
1084 while (offset && offset >= sg_dma_len(sg)) {
1085 offset -= sg_dma_len(sg);
1086 sg++;
1087 }
1088 if (bpl <= sg_dma_len(sg) - offset) {
1089 /* fits into current chunk */
1090 *(rp++) = cpu_to_le32(RISC_WRITE | RISC_SOL | RISC_EOL |
1091 bpl);
1092 *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
1093 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1094 offset += bpl;
1095 } else {
1096 /* scanline needs to be split */
1097 todo = bpl;
1098 *(rp++) = cpu_to_le32(RISC_WRITE | RISC_SOL |
1099 (sg_dma_len(sg) - offset));
1100 *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
1101 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1102 todo -= (sg_dma_len(sg) - offset);
1103 offset = 0;
1104 sg++;
1105 while (todo > sg_dma_len(sg)) {
1106 *(rp++) = cpu_to_le32(RISC_WRITE |
1107 sg_dma_len(sg));
1108 *(rp++) = cpu_to_le32(sg_dma_address(sg));
1109 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1110 todo -= sg_dma_len(sg);
1111 sg++;
1112 }
1113 *(rp++) = cpu_to_le32(RISC_WRITE | RISC_EOL | todo);
1114 *(rp++) = cpu_to_le32(sg_dma_address(sg));
1115 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1116 offset += todo;
1117 }
1118
1119 offset += padding;
1120 }
1121
1122 return rp;
1123}
1124
1125int cx25821_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
1126 struct scatterlist *sglist, unsigned int top_offset,
1127 unsigned int bottom_offset, unsigned int bpl,
1128 unsigned int padding, unsigned int lines)
1129{
1130 u32 instructions;
1131 u32 fields;
1132 __le32 *rp;
1133 int rc;
1134
1135 fields = 0;
1136 if (UNSET != top_offset)
1137 fields++;
1138 if (UNSET != bottom_offset)
1139 fields++;
1140
1141 /* estimate risc mem: worst case is one write per page border +
1142 one write per scan line + syncs + jump (all 2 dwords). Padding
1143 can cause next bpl to start close to a page border. First DMA
1144 region may be smaller than PAGE_SIZE */
1145 /* write and jump need and extra dword */
1146 instructions = fields * (1 + ((bpl + padding) * lines) / PAGE_SIZE +
1147 lines);
1148 instructions += 2;
1149 rc = btcx_riscmem_alloc(pci, risc, instructions * 12);
1150
1151 if (rc < 0)
1152 return rc;
1153
1154 /* write risc instructions */
1155 rp = risc->cpu;
1156
1157 if (UNSET != top_offset) {
1158 rp = cx25821_risc_field(rp, sglist, top_offset, 0, bpl, padding,
1159 lines);
1160 }
1161
1162 if (UNSET != bottom_offset) {
1163 rp = cx25821_risc_field(rp, sglist, bottom_offset, 0x200, bpl,
1164 padding, lines);
1165 }
1166
1167 /* save pointer to jmp instruction address */
1168 risc->jmp = rp;
1169 BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
1170
1171 return 0;
1172}
1173
1174static __le32 *cx25821_risc_field_audio(__le32 * rp, struct scatterlist *sglist,
1175 unsigned int offset, u32 sync_line,
1176 unsigned int bpl, unsigned int padding,
1177 unsigned int lines, unsigned int lpi)
1178{
1179 struct scatterlist *sg;
1180 unsigned int line, todo, sol;
1181
1182 /* sync instruction */
1183 if (sync_line != NO_SYNC_LINE)
1184 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
1185
1186 /* scan lines */
1187 sg = sglist;
1188 for (line = 0; line < lines; line++) {
1189 while (offset && offset >= sg_dma_len(sg)) {
1190 offset -= sg_dma_len(sg);
1191 sg++;
1192 }
1193
1194 if (lpi && line > 0 && !(line % lpi))
1195 sol = RISC_SOL | RISC_IRQ1 | RISC_CNT_INC;
1196 else
1197 sol = RISC_SOL;
1198
1199 if (bpl <= sg_dma_len(sg) - offset) {
1200 /* fits into current chunk */
1201 *(rp++) = cpu_to_le32(RISC_WRITE | sol | RISC_EOL |
1202 bpl);
1203 *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
1204 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1205 offset += bpl;
1206 } else {
1207 /* scanline needs to be split */
1208 todo = bpl;
1209 *(rp++) = cpu_to_le32(RISC_WRITE | sol |
1210 (sg_dma_len(sg) - offset));
1211 *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
1212 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1213 todo -= (sg_dma_len(sg) - offset);
1214 offset = 0;
1215 sg++;
1216 while (todo > sg_dma_len(sg)) {
1217 *(rp++) = cpu_to_le32(RISC_WRITE |
1218 sg_dma_len(sg));
1219 *(rp++) = cpu_to_le32(sg_dma_address(sg));
1220 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1221 todo -= sg_dma_len(sg);
1222 sg++;
1223 }
1224 *(rp++) = cpu_to_le32(RISC_WRITE | RISC_EOL | todo);
1225 *(rp++) = cpu_to_le32(sg_dma_address(sg));
1226 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1227 offset += todo;
1228 }
1229 offset += padding;
1230 }
1231
1232 return rp;
1233}
1234
1235int cx25821_risc_databuffer_audio(struct pci_dev *pci,
1236 struct btcx_riscmem *risc,
1237 struct scatterlist *sglist,
1238 unsigned int bpl,
1239 unsigned int lines, unsigned int lpi)
1240{
1241 u32 instructions;
1242 __le32 *rp;
1243 int rc;
1244
1245 /* estimate risc mem: worst case is one write per page border +
1246 one write per scan line + syncs + jump (all 2 dwords). Here
1247 there is no padding and no sync. First DMA region may be smaller
1248 than PAGE_SIZE */
1249 /* Jump and write need an extra dword */
1250 instructions = 1 + (bpl * lines) / PAGE_SIZE + lines;
1251 instructions += 1;
1252
1253 rc = btcx_riscmem_alloc(pci, risc, instructions * 12);
1254 if (rc < 0)
1255 return rc;
1256
1257 /* write risc instructions */
1258 rp = risc->cpu;
1259 rp = cx25821_risc_field_audio(rp, sglist, 0, NO_SYNC_LINE, bpl, 0,
1260 lines, lpi);
1261
1262 /* save pointer to jmp instruction address */
1263 risc->jmp = rp;
1264 BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
1265 return 0;
1266}
1267EXPORT_SYMBOL(cx25821_risc_databuffer_audio);
1268
1269int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
1270 u32 reg, u32 mask, u32 value)
1271{
1272 __le32 *rp;
1273 int rc;
1274
1275 rc = btcx_riscmem_alloc(pci, risc, 4 * 16);
1276
1277 if (rc < 0)
1278 return rc;
1279
1280 /* write risc instructions */
1281 rp = risc->cpu;
1282
1283 *(rp++) = cpu_to_le32(RISC_WRITECR | RISC_IRQ1);
1284 *(rp++) = cpu_to_le32(reg);
1285 *(rp++) = cpu_to_le32(value);
1286 *(rp++) = cpu_to_le32(mask);
1287 *(rp++) = cpu_to_le32(RISC_JUMP);
1288 *(rp++) = cpu_to_le32(risc->dma);
1289 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
1290 return 0;
1291}
1292
1293void cx25821_free_buffer(struct videobuf_queue *q, struct cx25821_buffer *buf)
1294{
1295 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
1296
1297 BUG_ON(in_interrupt());
1298 videobuf_waiton(q, &buf->vb, 0, 0);
1299 videobuf_dma_unmap(q->dev, dma);
1300 videobuf_dma_free(dma);
1301 btcx_riscmem_free(to_pci_dev(q->dev), &buf->risc);
1302 buf->vb.state = VIDEOBUF_NEEDS_INIT;
1303}
1304
1305static irqreturn_t cx25821_irq(int irq, void *dev_id)
1306{
1307 struct cx25821_dev *dev = dev_id;
1308 u32 pci_status;
1309 u32 vid_status;
1310 int i, handled = 0;
1311 u32 mask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
1312
1313 pci_status = cx_read(PCI_INT_STAT);
1314
1315 if (pci_status == 0)
1316 goto out;
1317
1318 for (i = 0; i < VID_CHANNEL_NUM; i++) {
1319 if (pci_status & mask[i]) {
1320 vid_status = cx_read(dev->channels[i].
1321 sram_channels->int_stat);
1322
1323 if (vid_status)
1324 handled += cx25821_video_irq(dev, i,
1325 vid_status);
1326
1327 cx_write(PCI_INT_STAT, mask[i]);
1328 }
1329 }
1330
1331out:
1332 return IRQ_RETVAL(handled);
1333}
1334
1335void cx25821_print_irqbits(char *name, char *tag, char **strings,
1336 int len, u32 bits, u32 mask)
1337{
1338 unsigned int i;
1339
1340 printk(KERN_DEBUG pr_fmt("%s: %s [0x%x]"), name, tag, bits);
1341
1342 for (i = 0; i < len; i++) {
1343 if (!(bits & (1 << i)))
1344 continue;
1345 if (strings[i])
1346 pr_cont(" %s", strings[i]);
1347 else
1348 pr_cont(" %d", i);
1349 if (!(mask & (1 << i)))
1350 continue;
1351 pr_cont("*");
1352 }
1353 pr_cont("\n");
1354}
1355EXPORT_SYMBOL(cx25821_print_irqbits);
1356
1357struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci)
1358{
1359 struct cx25821_dev *dev = pci_get_drvdata(pci);
1360 return dev;
1361}
1362EXPORT_SYMBOL(cx25821_dev_get);
1363
1364static int __devinit cx25821_initdev(struct pci_dev *pci_dev,
1365 const struct pci_device_id *pci_id)
1366{
1367 struct cx25821_dev *dev;
1368 int err = 0;
1369
1370 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1371 if (NULL == dev)
1372 return -ENOMEM;
1373
1374 err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
1375 if (err < 0)
1376 goto fail_free;
1377
1378 /* pci init */
1379 dev->pci = pci_dev;
1380 if (pci_enable_device(pci_dev)) {
1381 err = -EIO;
1382
1383 pr_info("pci enable failed!\n");
1384
1385 goto fail_unregister_device;
1386 }
1387
1388 pr_info("Athena pci enable !\n");
1389
1390 err = cx25821_dev_setup(dev);
1391 if (err) {
1392 if (err == -EBUSY)
1393 goto fail_unregister_device;
1394 else
1395 goto fail_unregister_pci;
1396 }
1397
1398 /* print pci info */
1399 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1400 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1401 pr_info("%s/0: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
1402 dev->name, pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1403 dev->pci_lat, (unsigned long long)dev->base_io_addr);
1404
1405 pci_set_master(pci_dev);
1406 if (!pci_dma_supported(pci_dev, 0xffffffff)) {
1407 pr_err("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
1408 err = -EIO;
1409 goto fail_irq;
1410 }
1411
1412 err = request_irq(pci_dev->irq, cx25821_irq,
1413 IRQF_SHARED, dev->name, dev);
1414
1415 if (err < 0) {
1416 pr_err("%s: can't get IRQ %d\n", dev->name, pci_dev->irq);
1417 goto fail_irq;
1418 }
1419
1420 return 0;
1421
1422fail_irq:
1423 pr_info("cx25821_initdev() can't get IRQ !\n");
1424 cx25821_dev_unregister(dev);
1425
1426fail_unregister_pci:
1427 pci_disable_device(pci_dev);
1428fail_unregister_device:
1429 v4l2_device_unregister(&dev->v4l2_dev);
1430
1431fail_free:
1432 kfree(dev);
1433 return err;
1434}
1435
1436static void __devexit cx25821_finidev(struct pci_dev *pci_dev)
1437{
1438 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1439 struct cx25821_dev *dev = get_cx25821(v4l2_dev);
1440
1441 cx25821_shutdown(dev);
1442 pci_disable_device(pci_dev);
1443
1444 /* unregister stuff */
1445 if (pci_dev->irq)
1446 free_irq(pci_dev->irq, dev);
1447
1448 mutex_lock(&cx25821_devlist_mutex);
1449 list_del(&dev->devlist);
1450 mutex_unlock(&cx25821_devlist_mutex);
1451
1452 cx25821_dev_unregister(dev);
1453 v4l2_device_unregister(v4l2_dev);
1454 kfree(dev);
1455}
1456
1457static DEFINE_PCI_DEVICE_TABLE(cx25821_pci_tbl) = {
1458 {
1459 /* CX25821 Athena */
1460 .vendor = 0x14f1,
1461 .device = 0x8210,
1462 .subvendor = 0x14f1,
1463 .subdevice = 0x0920,
1464 }, {
1465 /* CX25821 No Brand */
1466 .vendor = 0x14f1,
1467 .device = 0x8210,
1468 .subvendor = 0x0000,
1469 .subdevice = 0x0000,
1470 }, {
1471 /* --- end of list --- */
1472 }
1473};
1474
1475MODULE_DEVICE_TABLE(pci, cx25821_pci_tbl);
1476
1477static struct pci_driver cx25821_pci_driver = {
1478 .name = "cx25821",
1479 .id_table = cx25821_pci_tbl,
1480 .probe = cx25821_initdev,
1481 .remove = __devexit_p(cx25821_finidev),
1482 /* TODO */
1483 .suspend = NULL,
1484 .resume = NULL,
1485};
1486
1487static int __init cx25821_init(void)
1488{
1489 pr_info("driver version %d.%d.%d loaded\n",
1490 (CX25821_VERSION_CODE >> 16) & 0xff,
1491 (CX25821_VERSION_CODE >> 8) & 0xff,
1492 CX25821_VERSION_CODE & 0xff);
1493 return pci_register_driver(&cx25821_pci_driver);
1494}
1495
1496static void __exit cx25821_fini(void)
1497{
1498 pci_unregister_driver(&cx25821_pci_driver);
1499}
1500
1501module_init(cx25821_init);
1502module_exit(cx25821_fini);
diff --git a/drivers/media/pci/cx25821/cx25821-gpio.c b/drivers/media/pci/cx25821/cx25821-gpio.c
new file mode 100644
index 000000000000..29e43b03c85e
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-gpio.c
@@ -0,0 +1,98 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "cx25821.h"
24
25/********************* GPIO stuffs *********************/
26void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
27 int pin_number, int pin_logic_value)
28{
29 int bit = pin_number;
30 u32 gpio_oe_reg = GPIO_LO_OE;
31 u32 gpio_register = 0;
32 u32 value = 0;
33
34 /* Check for valid pinNumber */
35 if (pin_number >= 47)
36 return;
37
38 if (pin_number > 31) {
39 bit = pin_number - 31;
40 gpio_oe_reg = GPIO_HI_OE;
41 }
42 /* Here we will make sure that the GPIOs 0 and 1 are output. keep the
43 * rest as is */
44 gpio_register = cx_read(gpio_oe_reg);
45
46 if (pin_logic_value == 1)
47 value = gpio_register | Set_GPIO_Bit(bit);
48 else
49 value = gpio_register & Clear_GPIO_Bit(bit);
50
51 cx_write(gpio_oe_reg, value);
52}
53EXPORT_SYMBOL(cx25821_set_gpiopin_direction);
54
55static void cx25821_set_gpiopin_logicvalue(struct cx25821_dev *dev,
56 int pin_number, int pin_logic_value)
57{
58 int bit = pin_number;
59 u32 gpio_reg = GPIO_LO;
60 u32 value = 0;
61
62 /* Check for valid pinNumber */
63 if (pin_number >= 47)
64 return;
65
66 /* change to output direction */
67 cx25821_set_gpiopin_direction(dev, pin_number, 0);
68
69 if (pin_number > 31) {
70 bit = pin_number - 31;
71 gpio_reg = GPIO_HI;
72 }
73
74 value = cx_read(gpio_reg);
75
76 if (pin_logic_value == 0)
77 value &= Clear_GPIO_Bit(bit);
78 else
79 value |= Set_GPIO_Bit(bit);
80
81 cx_write(gpio_reg, value);
82}
83
84void cx25821_gpio_init(struct cx25821_dev *dev)
85{
86 if (dev == NULL)
87 return;
88
89 switch (dev->board) {
90 case CX25821_BOARD_CONEXANT_ATHENA10:
91 default:
92 /* set GPIO 5 to select the path for Medusa/Athena */
93 cx25821_set_gpiopin_logicvalue(dev, 5, 1);
94 mdelay(20);
95 break;
96 }
97
98}
diff --git a/drivers/media/pci/cx25821/cx25821-i2c.c b/drivers/media/pci/cx25821/cx25821-i2c.c
new file mode 100644
index 000000000000..9844549764c9
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-i2c.c
@@ -0,0 +1,416 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
26#include "cx25821.h"
27#include <linux/i2c.h>
28
29static unsigned int i2c_debug;
30module_param(i2c_debug, int, 0644);
31MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
32
33static unsigned int i2c_scan;
34module_param(i2c_scan, int, 0444);
35MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
36
37#define dprintk(level, fmt, arg...) \
38do { \
39 if (i2c_debug >= level) \
40 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ##arg); \
41} while (0)
42
43#define I2C_WAIT_DELAY 32
44#define I2C_WAIT_RETRY 64
45
46#define I2C_EXTEND (1 << 3)
47#define I2C_NOSTOP (1 << 4)
48
49static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
50{
51 struct cx25821_i2c *bus = i2c_adap->algo_data;
52 struct cx25821_dev *dev = bus->dev;
53 return cx_read(bus->reg_stat) & 0x01;
54}
55
56static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
57{
58 struct cx25821_i2c *bus = i2c_adap->algo_data;
59 struct cx25821_dev *dev = bus->dev;
60 return cx_read(bus->reg_stat) & 0x02 ? 1 : 0;
61}
62
63static int i2c_wait_done(struct i2c_adapter *i2c_adap)
64{
65 int count;
66
67 for (count = 0; count < I2C_WAIT_RETRY; count++) {
68 if (!i2c_is_busy(i2c_adap))
69 break;
70 udelay(I2C_WAIT_DELAY);
71 }
72
73 if (I2C_WAIT_RETRY == count)
74 return 0;
75
76 return 1;
77}
78
79static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
80 const struct i2c_msg *msg, int joined_rlen)
81{
82 struct cx25821_i2c *bus = i2c_adap->algo_data;
83 struct cx25821_dev *dev = bus->dev;
84 u32 wdata, addr, ctrl;
85 int retval, cnt;
86
87 if (joined_rlen)
88 dprintk(1, "%s(msg->wlen=%d, nextmsg->rlen=%d)\n", __func__,
89 msg->len, joined_rlen);
90 else
91 dprintk(1, "%s(msg->len=%d)\n", __func__, msg->len);
92
93 /* Deal with i2c probe functions with zero payload */
94 if (msg->len == 0) {
95 cx_write(bus->reg_addr, msg->addr << 25);
96 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2));
97
98 if (!i2c_wait_done(i2c_adap))
99 return -EIO;
100
101 if (!i2c_slave_did_ack(i2c_adap))
102 return -EIO;
103
104 dprintk(1, "%s(): returns 0\n", __func__);
105 return 0;
106 }
107
108 /* dev, reg + first byte */
109 addr = (msg->addr << 25) | msg->buf[0];
110 wdata = msg->buf[0];
111
112 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
113
114 if (msg->len > 1)
115 ctrl |= I2C_NOSTOP | I2C_EXTEND;
116 else if (joined_rlen)
117 ctrl |= I2C_NOSTOP;
118
119 cx_write(bus->reg_addr, addr);
120 cx_write(bus->reg_wdata, wdata);
121 cx_write(bus->reg_ctrl, ctrl);
122
123 retval = i2c_wait_done(i2c_adap);
124 if (retval < 0)
125 goto err;
126
127 if (retval == 0)
128 goto eio;
129
130 if (i2c_debug) {
131 if (!(ctrl & I2C_NOSTOP))
132 printk(" >\n");
133 }
134
135 for (cnt = 1; cnt < msg->len; cnt++) {
136 /* following bytes */
137 wdata = msg->buf[cnt];
138 ctrl = bus->i2c_period | (1 << 12) | (1 << 2);
139
140 if (cnt < msg->len - 1)
141 ctrl |= I2C_NOSTOP | I2C_EXTEND;
142 else if (joined_rlen)
143 ctrl |= I2C_NOSTOP;
144
145 cx_write(bus->reg_addr, addr);
146 cx_write(bus->reg_wdata, wdata);
147 cx_write(bus->reg_ctrl, ctrl);
148
149 retval = i2c_wait_done(i2c_adap);
150 if (retval < 0)
151 goto err;
152
153 if (retval == 0)
154 goto eio;
155
156 if (i2c_debug) {
157 dprintk(1, " %02x", msg->buf[cnt]);
158 if (!(ctrl & I2C_NOSTOP))
159 dprintk(1, " >\n");
160 }
161 }
162
163 return msg->len;
164
165eio:
166 retval = -EIO;
167err:
168 if (i2c_debug)
169 pr_err(" ERR: %d\n", retval);
170 return retval;
171}
172
173static int i2c_readbytes(struct i2c_adapter *i2c_adap,
174 const struct i2c_msg *msg, int joined)
175{
176 struct cx25821_i2c *bus = i2c_adap->algo_data;
177 struct cx25821_dev *dev = bus->dev;
178 u32 ctrl, cnt;
179 int retval;
180
181 if (i2c_debug && !joined)
182 dprintk(1, "6-%s(msg->len=%d)\n", __func__, msg->len);
183
184 /* Deal with i2c probe functions with zero payload */
185 if (msg->len == 0) {
186 cx_write(bus->reg_addr, msg->addr << 25);
187 cx_write(bus->reg_ctrl, bus->i2c_period | (1 << 2) | 1);
188 if (!i2c_wait_done(i2c_adap))
189 return -EIO;
190 if (!i2c_slave_did_ack(i2c_adap))
191 return -EIO;
192
193 dprintk(1, "%s(): returns 0\n", __func__);
194 return 0;
195 }
196
197 if (i2c_debug) {
198 if (joined)
199 dprintk(1, " R");
200 else
201 dprintk(1, " <R %02x", (msg->addr << 1) + 1);
202 }
203
204 for (cnt = 0; cnt < msg->len; cnt++) {
205
206 ctrl = bus->i2c_period | (1 << 12) | (1 << 2) | 1;
207
208 if (cnt < msg->len - 1)
209 ctrl |= I2C_NOSTOP | I2C_EXTEND;
210
211 cx_write(bus->reg_addr, msg->addr << 25);
212 cx_write(bus->reg_ctrl, ctrl);
213
214 retval = i2c_wait_done(i2c_adap);
215 if (retval < 0)
216 goto err;
217 if (retval == 0)
218 goto eio;
219 msg->buf[cnt] = cx_read(bus->reg_rdata) & 0xff;
220
221 if (i2c_debug) {
222 dprintk(1, " %02x", msg->buf[cnt]);
223 if (!(ctrl & I2C_NOSTOP))
224 dprintk(1, " >\n");
225 }
226 }
227
228 return msg->len;
229eio:
230 retval = -EIO;
231err:
232 if (i2c_debug)
233 pr_err(" ERR: %d\n", retval);
234 return retval;
235}
236
237static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
238{
239 struct cx25821_i2c *bus = i2c_adap->algo_data;
240 struct cx25821_dev *dev = bus->dev;
241 int i, retval = 0;
242
243 dprintk(1, "%s(num = %d)\n", __func__, num);
244
245 for (i = 0; i < num; i++) {
246 dprintk(1, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
247 __func__, num, msgs[i].addr, msgs[i].len);
248
249 if (msgs[i].flags & I2C_M_RD) {
250 /* read */
251 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
252 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
253 msgs[i].addr == msgs[i + 1].addr) {
254 /* write then read from same address */
255 retval = i2c_sendbytes(i2c_adap, &msgs[i],
256 msgs[i + 1].len);
257
258 if (retval < 0)
259 goto err;
260 i++;
261 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
262 } else {
263 /* write */
264 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
265 }
266
267 if (retval < 0)
268 goto err;
269 }
270 return num;
271
272err:
273 return retval;
274}
275
276
277static u32 cx25821_functionality(struct i2c_adapter *adap)
278{
279 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C | I2C_FUNC_SMBUS_WORD_DATA |
280 I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
281}
282
283static struct i2c_algorithm cx25821_i2c_algo_template = {
284 .master_xfer = i2c_xfer,
285 .functionality = cx25821_functionality,
286#ifdef NEED_ALGO_CONTROL
287 .algo_control = dummy_algo_control,
288#endif
289};
290
291static struct i2c_adapter cx25821_i2c_adap_template = {
292 .name = "cx25821",
293 .owner = THIS_MODULE,
294 .algo = &cx25821_i2c_algo_template,
295};
296
297static struct i2c_client cx25821_i2c_client_template = {
298 .name = "cx25821 internal",
299};
300
301/* init + register i2c adapter */
302int cx25821_i2c_register(struct cx25821_i2c *bus)
303{
304 struct cx25821_dev *dev = bus->dev;
305
306 dprintk(1, "%s(bus = %d)\n", __func__, bus->nr);
307
308 bus->i2c_adap = cx25821_i2c_adap_template;
309 bus->i2c_client = cx25821_i2c_client_template;
310 bus->i2c_adap.dev.parent = &dev->pci->dev;
311
312 strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name));
313
314 bus->i2c_adap.algo_data = bus;
315 i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev);
316 i2c_add_adapter(&bus->i2c_adap);
317
318 bus->i2c_client.adapter = &bus->i2c_adap;
319
320 /* set up the I2c */
321 bus->i2c_client.addr = (0x88 >> 1);
322
323 return bus->i2c_rc;
324}
325
326int cx25821_i2c_unregister(struct cx25821_i2c *bus)
327{
328 i2c_del_adapter(&bus->i2c_adap);
329 return 0;
330}
331
332void cx25821_av_clk(struct cx25821_dev *dev, int enable)
333{
334 /* write 0 to bus 2 addr 0x144 via i2x_xfer() */
335 char buffer[3];
336 struct i2c_msg msg;
337 dprintk(1, "%s(enabled = %d)\n", __func__, enable);
338
339 /* Register 0x144 */
340 buffer[0] = 0x01;
341 buffer[1] = 0x44;
342 if (enable == 1)
343 buffer[2] = 0x05;
344 else
345 buffer[2] = 0x00;
346
347 msg.addr = 0x44;
348 msg.flags = I2C_M_TEN;
349 msg.len = 3;
350 msg.buf = buffer;
351
352 i2c_xfer(&dev->i2c_bus[0].i2c_adap, &msg, 1);
353}
354
355int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value)
356{
357 struct i2c_client *client = &bus->i2c_client;
358 int v = 0;
359 u8 addr[2] = { 0, 0 };
360 u8 buf[4] = { 0, 0, 0, 0 };
361
362 struct i2c_msg msgs[2] = {
363 {
364 .addr = client->addr,
365 .flags = 0,
366 .len = 2,
367 .buf = addr,
368 }, {
369 .addr = client->addr,
370 .flags = I2C_M_RD,
371 .len = 4,
372 .buf = buf,
373 }
374 };
375
376 addr[0] = (reg_addr >> 8);
377 addr[1] = (reg_addr & 0xff);
378 msgs[0].addr = 0x44;
379 msgs[1].addr = 0x44;
380
381 i2c_xfer(client->adapter, msgs, 2);
382
383 v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
384 *value = v;
385
386 return v;
387}
388
389int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value)
390{
391 struct i2c_client *client = &bus->i2c_client;
392 int retval = 0;
393 u8 buf[6] = { 0, 0, 0, 0, 0, 0 };
394
395 struct i2c_msg msgs[1] = {
396 {
397 .addr = client->addr,
398 .flags = 0,
399 .len = 6,
400 .buf = buf,
401 }
402 };
403
404 buf[0] = reg_addr >> 8;
405 buf[1] = reg_addr & 0xff;
406 buf[5] = (value >> 24) & 0xff;
407 buf[4] = (value >> 16) & 0xff;
408 buf[3] = (value >> 8) & 0xff;
409 buf[2] = value & 0xff;
410 client->flags = 0;
411 msgs[0].addr = 0x44;
412
413 retval = i2c_xfer(client->adapter, msgs, 1);
414
415 return retval;
416}
diff --git a/drivers/media/pci/cx25821/cx25821-medusa-defines.h b/drivers/media/pci/cx25821/cx25821-medusa-defines.h
new file mode 100644
index 000000000000..7a9e6470ba22
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-medusa-defines.h
@@ -0,0 +1,42 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#ifndef _MEDUSA_DEF_H_
24#define _MEDUSA_DEF_H_
25
26/* Video decoder that we supported */
27#define VDEC_A 0
28#define VDEC_B 1
29#define VDEC_C 2
30#define VDEC_D 3
31#define VDEC_E 4
32#define VDEC_F 5
33#define VDEC_G 6
34#define VDEC_H 7
35
36/* end of display sequence */
37#define END_OF_SEQ 0xF;
38
39/* registry string size */
40#define MAX_REGISTRY_SZ 40;
41
42#endif
diff --git a/drivers/media/pci/cx25821/cx25821-medusa-reg.h b/drivers/media/pci/cx25821/cx25821-medusa-reg.h
new file mode 100644
index 000000000000..c98ac946b277
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-medusa-reg.h
@@ -0,0 +1,455 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#ifndef __MEDUSA_REGISTERS__
24#define __MEDUSA_REGISTERS__
25
26/* Serial Slave Registers */
27#define HOST_REGISTER1 0x0000
28#define HOST_REGISTER2 0x0001
29
30/* Chip Configuration Registers */
31#define CHIP_CTRL 0x0100
32#define AFE_AB_CTRL 0x0104
33#define AFE_CD_CTRL 0x0108
34#define AFE_EF_CTRL 0x010C
35#define AFE_GH_CTRL 0x0110
36#define DENC_AB_CTRL 0x0114
37#define BYP_AB_CTRL 0x0118
38#define MON_A_CTRL 0x011C
39#define DISP_SEQ_A 0x0120
40#define DISP_SEQ_B 0x0124
41#define DISP_AB_CNT 0x0128
42#define DISP_CD_CNT 0x012C
43#define DISP_EF_CNT 0x0130
44#define DISP_GH_CNT 0x0134
45#define DISP_IJ_CNT 0x0138
46#define PIN_OE_CTRL 0x013C
47#define PIN_SPD_CTRL 0x0140
48#define PIN_SPD_CTRL2 0x0144
49#define IRQ_STAT_CTRL 0x0148
50#define POWER_CTRL_AB 0x014C
51#define POWER_CTRL_CD 0x0150
52#define POWER_CTRL_EF 0x0154
53#define POWER_CTRL_GH 0x0158
54#define TUNE_CTRL 0x015C
55#define BIAS_CTRL 0x0160
56#define AFE_AB_DIAG_CTRL 0x0164
57#define AFE_CD_DIAG_CTRL 0x0168
58#define AFE_EF_DIAG_CTRL 0x016C
59#define AFE_GH_DIAG_CTRL 0x0170
60#define PLL_AB_DIAG_CTRL 0x0174
61#define PLL_CD_DIAG_CTRL 0x0178
62#define PLL_EF_DIAG_CTRL 0x017C
63#define PLL_GH_DIAG_CTRL 0x0180
64#define TEST_CTRL 0x0184
65#define BIST_STAT 0x0188
66#define BIST_STAT2 0x018C
67#define BIST_VID_PLL_AB_STAT 0x0190
68#define BIST_VID_PLL_CD_STAT 0x0194
69#define BIST_VID_PLL_EF_STAT 0x0198
70#define BIST_VID_PLL_GH_STAT 0x019C
71#define DLL_DIAG_CTRL 0x01A0
72#define DEV_CH_ID_CTRL 0x01A4
73#define ABIST_CTRL_STATUS 0x01A8
74#define ABIST_FREQ 0x01AC
75#define ABIST_GOERT_SHIFT 0x01B0
76#define ABIST_COEF12 0x01B4
77#define ABIST_COEF34 0x01B8
78#define ABIST_COEF56 0x01BC
79#define ABIST_COEF7_SNR 0x01C0
80#define ABIST_ADC_CAL 0x01C4
81#define ABIST_BIN1_VGA0 0x01C8
82#define ABIST_BIN2_VGA1 0x01CC
83#define ABIST_BIN3_VGA2 0x01D0
84#define ABIST_BIN4_VGA3 0x01D4
85#define ABIST_BIN5_VGA4 0x01D8
86#define ABIST_BIN6_VGA5 0x01DC
87#define ABIST_BIN7_VGA6 0x0x1E0
88#define ABIST_CLAMP_A 0x0x1E4
89#define ABIST_CLAMP_B 0x0x1E8
90#define ABIST_CLAMP_C 0x01EC
91#define ABIST_CLAMP_D 0x01F0
92#define ABIST_CLAMP_E 0x01F4
93#define ABIST_CLAMP_F 0x01F8
94
95/* Digital Video Encoder A Registers */
96#define DENC_A_REG_1 0x0200
97#define DENC_A_REG_2 0x0204
98#define DENC_A_REG_3 0x0208
99#define DENC_A_REG_4 0x020C
100#define DENC_A_REG_5 0x0210
101#define DENC_A_REG_6 0x0214
102#define DENC_A_REG_7 0x0218
103#define DENC_A_REG_8 0x021C
104
105/* Digital Video Encoder B Registers */
106#define DENC_B_REG_1 0x0300
107#define DENC_B_REG_2 0x0304
108#define DENC_B_REG_3 0x0308
109#define DENC_B_REG_4 0x030C
110#define DENC_B_REG_5 0x0310
111#define DENC_B_REG_6 0x0314
112#define DENC_B_REG_7 0x0318
113#define DENC_B_REG_8 0x031C
114
115/* Video Decoder A Registers */
116#define MODE_CTRL 0x1000
117#define OUT_CTRL1 0x1004
118#define OUT_CTRL_NS 0x1008
119#define GEN_STAT 0x100C
120#define INT_STAT_MASK 0x1010
121#define LUMA_CTRL 0x1014
122#define CHROMA_CTRL 0x1018
123#define CRUSH_CTRL 0x101C
124#define HORIZ_TIM_CTRL 0x1020
125#define VERT_TIM_CTRL 0x1024
126#define MISC_TIM_CTRL 0x1028
127#define FIELD_COUNT 0x102C
128#define HSCALE_CTRL 0x1030
129#define VSCALE_CTRL 0x1034
130#define MAN_VGA_CTRL 0x1038
131#define MAN_AGC_CTRL 0x103C
132#define DFE_CTRL1 0x1040
133#define DFE_CTRL2 0x1044
134#define DFE_CTRL3 0x1048
135#define PLL_CTRL 0x104C
136#define PLL_CTRL_FAST 0x1050
137#define HTL_CTRL 0x1054
138#define SRC_CFG 0x1058
139#define SC_STEP_SIZE 0x105C
140#define SC_CONVERGE_CTRL 0x1060
141#define SC_LOOP_CTRL 0x1064
142#define COMB_2D_HFS_CFG 0x1068
143#define COMB_2D_HFD_CFG 0x106C
144#define COMB_2D_LF_CFG 0x1070
145#define COMB_2D_BLEND 0x1074
146#define COMB_MISC_CTRL 0x1078
147#define COMB_FLAT_THRESH_CTRL 0x107C
148#define COMB_TEST 0x1080
149#define BP_MISC_CTRL 0x1084
150#define VCR_DET_CTRL 0x1088
151#define NOISE_DET_CTRL 0x108C
152#define COMB_FLAT_NOISE_CTRL 0x1090
153#define VERSION 0x11F8
154#define SOFT_RST_CTRL 0x11FC
155
156/* Video Decoder B Registers */
157#define VDEC_B_MODE_CTRL 0x1200
158#define VDEC_B_OUT_CTRL1 0x1204
159#define VDEC_B_OUT_CTRL_NS 0x1208
160#define VDEC_B_GEN_STAT 0x120C
161#define VDEC_B_INT_STAT_MASK 0x1210
162#define VDEC_B_LUMA_CTRL 0x1214
163#define VDEC_B_CHROMA_CTRL 0x1218
164#define VDEC_B_CRUSH_CTRL 0x121C
165#define VDEC_B_HORIZ_TIM_CTRL 0x1220
166#define VDEC_B_VERT_TIM_CTRL 0x1224
167#define VDEC_B_MISC_TIM_CTRL 0x1228
168#define VDEC_B_FIELD_COUNT 0x122C
169#define VDEC_B_HSCALE_CTRL 0x1230
170#define VDEC_B_VSCALE_CTRL 0x1234
171#define VDEC_B_MAN_VGA_CTRL 0x1238
172#define VDEC_B_MAN_AGC_CTRL 0x123C
173#define VDEC_B_DFE_CTRL1 0x1240
174#define VDEC_B_DFE_CTRL2 0x1244
175#define VDEC_B_DFE_CTRL3 0x1248
176#define VDEC_B_PLL_CTRL 0x124C
177#define VDEC_B_PLL_CTRL_FAST 0x1250
178#define VDEC_B_HTL_CTRL 0x1254
179#define VDEC_B_SRC_CFG 0x1258
180#define VDEC_B_SC_STEP_SIZE 0x125C
181#define VDEC_B_SC_CONVERGE_CTRL 0x1260
182#define VDEC_B_SC_LOOP_CTRL 0x1264
183#define VDEC_B_COMB_2D_HFS_CFG 0x1268
184#define VDEC_B_COMB_2D_HFD_CFG 0x126C
185#define VDEC_B_COMB_2D_LF_CFG 0x1270
186#define VDEC_B_COMB_2D_BLEND 0x1274
187#define VDEC_B_COMB_MISC_CTRL 0x1278
188#define VDEC_B_COMB_FLAT_THRESH_CTRL 0x127C
189#define VDEC_B_COMB_TEST 0x1280
190#define VDEC_B_BP_MISC_CTRL 0x1284
191#define VDEC_B_VCR_DET_CTRL 0x1288
192#define VDEC_B_NOISE_DET_CTRL 0x128C
193#define VDEC_B_COMB_FLAT_NOISE_CTRL 0x1290
194#define VDEC_B_VERSION 0x13F8
195#define VDEC_B_SOFT_RST_CTRL 0x13FC
196
197/* Video Decoder C Registers */
198#define VDEC_C_MODE_CTRL 0x1400
199#define VDEC_C_OUT_CTRL1 0x1404
200#define VDEC_C_OUT_CTRL_NS 0x1408
201#define VDEC_C_GEN_STAT 0x140C
202#define VDEC_C_INT_STAT_MASK 0x1410
203#define VDEC_C_LUMA_CTRL 0x1414
204#define VDEC_C_CHROMA_CTRL 0x1418
205#define VDEC_C_CRUSH_CTRL 0x141C
206#define VDEC_C_HORIZ_TIM_CTRL 0x1420
207#define VDEC_C_VERT_TIM_CTRL 0x1424
208#define VDEC_C_MISC_TIM_CTRL 0x1428
209#define VDEC_C_FIELD_COUNT 0x142C
210#define VDEC_C_HSCALE_CTRL 0x1430
211#define VDEC_C_VSCALE_CTRL 0x1434
212#define VDEC_C_MAN_VGA_CTRL 0x1438
213#define VDEC_C_MAN_AGC_CTRL 0x143C
214#define VDEC_C_DFE_CTRL1 0x1440
215#define VDEC_C_DFE_CTRL2 0x1444
216#define VDEC_C_DFE_CTRL3 0x1448
217#define VDEC_C_PLL_CTRL 0x144C
218#define VDEC_C_PLL_CTRL_FAST 0x1450
219#define VDEC_C_HTL_CTRL 0x1454
220#define VDEC_C_SRC_CFG 0x1458
221#define VDEC_C_SC_STEP_SIZE 0x145C
222#define VDEC_C_SC_CONVERGE_CTRL 0x1460
223#define VDEC_C_SC_LOOP_CTRL 0x1464
224#define VDEC_C_COMB_2D_HFS_CFG 0x1468
225#define VDEC_C_COMB_2D_HFD_CFG 0x146C
226#define VDEC_C_COMB_2D_LF_CFG 0x1470
227#define VDEC_C_COMB_2D_BLEND 0x1474
228#define VDEC_C_COMB_MISC_CTRL 0x1478
229#define VDEC_C_COMB_FLAT_THRESH_CTRL 0x147C
230#define VDEC_C_COMB_TEST 0x1480
231#define VDEC_C_BP_MISC_CTRL 0x1484
232#define VDEC_C_VCR_DET_CTRL 0x1488
233#define VDEC_C_NOISE_DET_CTRL 0x148C
234#define VDEC_C_COMB_FLAT_NOISE_CTRL 0x1490
235#define VDEC_C_VERSION 0x15F8
236#define VDEC_C_SOFT_RST_CTRL 0x15FC
237
238/* Video Decoder D Registers */
239#define VDEC_D_MODE_CTRL 0x1600
240#define VDEC_D_OUT_CTRL1 0x1604
241#define VDEC_D_OUT_CTRL_NS 0x1608
242#define VDEC_D_GEN_STAT 0x160C
243#define VDEC_D_INT_STAT_MASK 0x1610
244#define VDEC_D_LUMA_CTRL 0x1614
245#define VDEC_D_CHROMA_CTRL 0x1618
246#define VDEC_D_CRUSH_CTRL 0x161C
247#define VDEC_D_HORIZ_TIM_CTRL 0x1620
248#define VDEC_D_VERT_TIM_CTRL 0x1624
249#define VDEC_D_MISC_TIM_CTRL 0x1628
250#define VDEC_D_FIELD_COUNT 0x162C
251#define VDEC_D_HSCALE_CTRL 0x1630
252#define VDEC_D_VSCALE_CTRL 0x1634
253#define VDEC_D_MAN_VGA_CTRL 0x1638
254#define VDEC_D_MAN_AGC_CTRL 0x163C
255#define VDEC_D_DFE_CTRL1 0x1640
256#define VDEC_D_DFE_CTRL2 0x1644
257#define VDEC_D_DFE_CTRL3 0x1648
258#define VDEC_D_PLL_CTRL 0x164C
259#define VDEC_D_PLL_CTRL_FAST 0x1650
260#define VDEC_D_HTL_CTRL 0x1654
261#define VDEC_D_SRC_CFG 0x1658
262#define VDEC_D_SC_STEP_SIZE 0x165C
263#define VDEC_D_SC_CONVERGE_CTRL 0x1660
264#define VDEC_D_SC_LOOP_CTRL 0x1664
265#define VDEC_D_COMB_2D_HFS_CFG 0x1668
266#define VDEC_D_COMB_2D_HFD_CFG 0x166C
267#define VDEC_D_COMB_2D_LF_CFG 0x1670
268#define VDEC_D_COMB_2D_BLEND 0x1674
269#define VDEC_D_COMB_MISC_CTRL 0x1678
270#define VDEC_D_COMB_FLAT_THRESH_CTRL 0x167C
271#define VDEC_D_COMB_TEST 0x1680
272#define VDEC_D_BP_MISC_CTRL 0x1684
273#define VDEC_D_VCR_DET_CTRL 0x1688
274#define VDEC_D_NOISE_DET_CTRL 0x168C
275#define VDEC_D_COMB_FLAT_NOISE_CTRL 0x1690
276#define VDEC_D_VERSION 0x17F8
277#define VDEC_D_SOFT_RST_CTRL 0x17FC
278
279/* Video Decoder E Registers */
280#define VDEC_E_MODE_CTRL 0x1800
281#define VDEC_E_OUT_CTRL1 0x1804
282#define VDEC_E_OUT_CTRL_NS 0x1808
283#define VDEC_E_GEN_STAT 0x180C
284#define VDEC_E_INT_STAT_MASK 0x1810
285#define VDEC_E_LUMA_CTRL 0x1814
286#define VDEC_E_CHROMA_CTRL 0x1818
287#define VDEC_E_CRUSH_CTRL 0x181C
288#define VDEC_E_HORIZ_TIM_CTRL 0x1820
289#define VDEC_E_VERT_TIM_CTRL 0x1824
290#define VDEC_E_MISC_TIM_CTRL 0x1828
291#define VDEC_E_FIELD_COUNT 0x182C
292#define VDEC_E_HSCALE_CTRL 0x1830
293#define VDEC_E_VSCALE_CTRL 0x1834
294#define VDEC_E_MAN_VGA_CTRL 0x1838
295#define VDEC_E_MAN_AGC_CTRL 0x183C
296#define VDEC_E_DFE_CTRL1 0x1840
297#define VDEC_E_DFE_CTRL2 0x1844
298#define VDEC_E_DFE_CTRL3 0x1848
299#define VDEC_E_PLL_CTRL 0x184C
300#define VDEC_E_PLL_CTRL_FAST 0x1850
301#define VDEC_E_HTL_CTRL 0x1854
302#define VDEC_E_SRC_CFG 0x1858
303#define VDEC_E_SC_STEP_SIZE 0x185C
304#define VDEC_E_SC_CONVERGE_CTRL 0x1860
305#define VDEC_E_SC_LOOP_CTRL 0x1864
306#define VDEC_E_COMB_2D_HFS_CFG 0x1868
307#define VDEC_E_COMB_2D_HFD_CFG 0x186C
308#define VDEC_E_COMB_2D_LF_CFG 0x1870
309#define VDEC_E_COMB_2D_BLEND 0x1874
310#define VDEC_E_COMB_MISC_CTRL 0x1878
311#define VDEC_E_COMB_FLAT_THRESH_CTRL 0x187C
312#define VDEC_E_COMB_TEST 0x1880
313#define VDEC_E_BP_MISC_CTRL 0x1884
314#define VDEC_E_VCR_DET_CTRL 0x1888
315#define VDEC_E_NOISE_DET_CTRL 0x188C
316#define VDEC_E_COMB_FLAT_NOISE_CTRL 0x1890
317#define VDEC_E_VERSION 0x19F8
318#define VDEC_E_SOFT_RST_CTRL 0x19FC
319
320/* Video Decoder F Registers */
321#define VDEC_F_MODE_CTRL 0x1A00
322#define VDEC_F_OUT_CTRL1 0x1A04
323#define VDEC_F_OUT_CTRL_NS 0x1A08
324#define VDEC_F_GEN_STAT 0x1A0C
325#define VDEC_F_INT_STAT_MASK 0x1A10
326#define VDEC_F_LUMA_CTRL 0x1A14
327#define VDEC_F_CHROMA_CTRL 0x1A18
328#define VDEC_F_CRUSH_CTRL 0x1A1C
329#define VDEC_F_HORIZ_TIM_CTRL 0x1A20
330#define VDEC_F_VERT_TIM_CTRL 0x1A24
331#define VDEC_F_MISC_TIM_CTRL 0x1A28
332#define VDEC_F_FIELD_COUNT 0x1A2C
333#define VDEC_F_HSCALE_CTRL 0x1A30
334#define VDEC_F_VSCALE_CTRL 0x1A34
335#define VDEC_F_MAN_VGA_CTRL 0x1A38
336#define VDEC_F_MAN_AGC_CTRL 0x1A3C
337#define VDEC_F_DFE_CTRL1 0x1A40
338#define VDEC_F_DFE_CTRL2 0x1A44
339#define VDEC_F_DFE_CTRL3 0x1A48
340#define VDEC_F_PLL_CTRL 0x1A4C
341#define VDEC_F_PLL_CTRL_FAST 0x1A50
342#define VDEC_F_HTL_CTRL 0x1A54
343#define VDEC_F_SRC_CFG 0x1A58
344#define VDEC_F_SC_STEP_SIZE 0x1A5C
345#define VDEC_F_SC_CONVERGE_CTRL 0x1A60
346#define VDEC_F_SC_LOOP_CTRL 0x1A64
347#define VDEC_F_COMB_2D_HFS_CFG 0x1A68
348#define VDEC_F_COMB_2D_HFD_CFG 0x1A6C
349#define VDEC_F_COMB_2D_LF_CFG 0x1A70
350#define VDEC_F_COMB_2D_BLEND 0x1A74
351#define VDEC_F_COMB_MISC_CTRL 0x1A78
352#define VDEC_F_COMB_FLAT_THRESH_CTRL 0x1A7C
353#define VDEC_F_COMB_TEST 0x1A80
354#define VDEC_F_BP_MISC_CTRL 0x1A84
355#define VDEC_F_VCR_DET_CTRL 0x1A88
356#define VDEC_F_NOISE_DET_CTRL 0x1A8C
357#define VDEC_F_COMB_FLAT_NOISE_CTRL 0x1A90
358#define VDEC_F_VERSION 0x1BF8
359#define VDEC_F_SOFT_RST_CTRL 0x1BFC
360
361/* Video Decoder G Registers */
362#define VDEC_G_MODE_CTRL 0x1C00
363#define VDEC_G_OUT_CTRL1 0x1C04
364#define VDEC_G_OUT_CTRL_NS 0x1C08
365#define VDEC_G_GEN_STAT 0x1C0C
366#define VDEC_G_INT_STAT_MASK 0x1C10
367#define VDEC_G_LUMA_CTRL 0x1C14
368#define VDEC_G_CHROMA_CTRL 0x1C18
369#define VDEC_G_CRUSH_CTRL 0x1C1C
370#define VDEC_G_HORIZ_TIM_CTRL 0x1C20
371#define VDEC_G_VERT_TIM_CTRL 0x1C24
372#define VDEC_G_MISC_TIM_CTRL 0x1C28
373#define VDEC_G_FIELD_COUNT 0x1C2C
374#define VDEC_G_HSCALE_CTRL 0x1C30
375#define VDEC_G_VSCALE_CTRL 0x1C34
376#define VDEC_G_MAN_VGA_CTRL 0x1C38
377#define VDEC_G_MAN_AGC_CTRL 0x1C3C
378#define VDEC_G_DFE_CTRL1 0x1C40
379#define VDEC_G_DFE_CTRL2 0x1C44
380#define VDEC_G_DFE_CTRL3 0x1C48
381#define VDEC_G_PLL_CTRL 0x1C4C
382#define VDEC_G_PLL_CTRL_FAST 0x1C50
383#define VDEC_G_HTL_CTRL 0x1C54
384#define VDEC_G_SRC_CFG 0x1C58
385#define VDEC_G_SC_STEP_SIZE 0x1C5C
386#define VDEC_G_SC_CONVERGE_CTRL 0x1C60
387#define VDEC_G_SC_LOOP_CTRL 0x1C64
388#define VDEC_G_COMB_2D_HFS_CFG 0x1C68
389#define VDEC_G_COMB_2D_HFD_CFG 0x1C6C
390#define VDEC_G_COMB_2D_LF_CFG 0x1C70
391#define VDEC_G_COMB_2D_BLEND 0x1C74
392#define VDEC_G_COMB_MISC_CTRL 0x1C78
393#define VDEC_G_COMB_FLAT_THRESH_CTRL 0x1C7C
394#define VDEC_G_COMB_TEST 0x1C80
395#define VDEC_G_BP_MISC_CTRL 0x1C84
396#define VDEC_G_VCR_DET_CTRL 0x1C88
397#define VDEC_G_NOISE_DET_CTRL 0x1C8C
398#define VDEC_G_COMB_FLAT_NOISE_CTRL 0x1C90
399#define VDEC_G_VERSION 0x1DF8
400#define VDEC_G_SOFT_RST_CTRL 0x1DFC
401
402/* Video Decoder H Registers */
403#define VDEC_H_MODE_CTRL 0x1E00
404#define VDEC_H_OUT_CTRL1 0x1E04
405#define VDEC_H_OUT_CTRL_NS 0x1E08
406#define VDEC_H_GEN_STAT 0x1E0C
407#define VDEC_H_INT_STAT_MASK 0x1E1E
408#define VDEC_H_LUMA_CTRL 0x1E14
409#define VDEC_H_CHROMA_CTRL 0x1E18
410#define VDEC_H_CRUSH_CTRL 0x1E1C
411#define VDEC_H_HORIZ_TIM_CTRL 0x1E20
412#define VDEC_H_VERT_TIM_CTRL 0x1E24
413#define VDEC_H_MISC_TIM_CTRL 0x1E28
414#define VDEC_H_FIELD_COUNT 0x1E2C
415#define VDEC_H_HSCALE_CTRL 0x1E30
416#define VDEC_H_VSCALE_CTRL 0x1E34
417#define VDEC_H_MAN_VGA_CTRL 0x1E38
418#define VDEC_H_MAN_AGC_CTRL 0x1E3C
419#define VDEC_H_DFE_CTRL1 0x1E40
420#define VDEC_H_DFE_CTRL2 0x1E44
421#define VDEC_H_DFE_CTRL3 0x1E48
422#define VDEC_H_PLL_CTRL 0x1E4C
423#define VDEC_H_PLL_CTRL_FAST 0x1E50
424#define VDEC_H_HTL_CTRL 0x1E54
425#define VDEC_H_SRC_CFG 0x1E58
426#define VDEC_H_SC_STEP_SIZE 0x1E5C
427#define VDEC_H_SC_CONVERGE_CTRL 0x1E60
428#define VDEC_H_SC_LOOP_CTRL 0x1E64
429#define VDEC_H_COMB_2D_HFS_CFG 0x1E68
430#define VDEC_H_COMB_2D_HFD_CFG 0x1E6C
431#define VDEC_H_COMB_2D_LF_CFG 0x1E70
432#define VDEC_H_COMB_2D_BLEND 0x1E74
433#define VDEC_H_COMB_MISC_CTRL 0x1E78
434#define VDEC_H_COMB_FLAT_THRESH_CTRL 0x1E7C
435#define VDEC_H_COMB_TEST 0x1E80
436#define VDEC_H_BP_MISC_CTRL 0x1E84
437#define VDEC_H_VCR_DET_CTRL 0x1E88
438#define VDEC_H_NOISE_DET_CTRL 0x1E8C
439#define VDEC_H_COMB_FLAT_NOISE_CTRL 0x1E90
440#define VDEC_H_VERSION 0x1FF8
441#define VDEC_H_SOFT_RST_CTRL 0x1FFC
442
443/*****************************************************************************/
444/* LUMA_CTRL register fields */
445#define VDEC_A_BRITE_CTRL 0x1014
446#define VDEC_A_CNTRST_CTRL 0x1015
447#define VDEC_A_PEAK_SEL 0x1016
448
449/*****************************************************************************/
450/* CHROMA_CTRL register fields */
451#define VDEC_A_USAT_CTRL 0x1018
452#define VDEC_A_VSAT_CTRL 0x1019
453#define VDEC_A_HUE_CTRL 0x101A
454
455#endif
diff --git a/drivers/media/pci/cx25821/cx25821-medusa-video.c b/drivers/media/pci/cx25821/cx25821-medusa-video.c
new file mode 100644
index 000000000000..6a92e5c70c2a
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-medusa-video.c
@@ -0,0 +1,787 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include "cx25821.h"
26#include "cx25821-medusa-video.h"
27#include "cx25821-biffuncs.h"
28
29/*
30 * medusa_enable_bluefield_output()
31 *
32 * Enable the generation of blue filed output if no video
33 *
34 */
35static void medusa_enable_bluefield_output(struct cx25821_dev *dev, int channel,
36 int enable)
37{
38 u32 value = 0;
39 u32 tmp = 0;
40 int out_ctrl = OUT_CTRL1;
41 int out_ctrl_ns = OUT_CTRL_NS;
42
43 switch (channel) {
44 default:
45 case VDEC_A:
46 break;
47 case VDEC_B:
48 out_ctrl = VDEC_B_OUT_CTRL1;
49 out_ctrl_ns = VDEC_B_OUT_CTRL_NS;
50 break;
51 case VDEC_C:
52 out_ctrl = VDEC_C_OUT_CTRL1;
53 out_ctrl_ns = VDEC_C_OUT_CTRL_NS;
54 break;
55 case VDEC_D:
56 out_ctrl = VDEC_D_OUT_CTRL1;
57 out_ctrl_ns = VDEC_D_OUT_CTRL_NS;
58 break;
59 case VDEC_E:
60 out_ctrl = VDEC_E_OUT_CTRL1;
61 out_ctrl_ns = VDEC_E_OUT_CTRL_NS;
62 return;
63 case VDEC_F:
64 out_ctrl = VDEC_F_OUT_CTRL1;
65 out_ctrl_ns = VDEC_F_OUT_CTRL_NS;
66 return;
67 case VDEC_G:
68 out_ctrl = VDEC_G_OUT_CTRL1;
69 out_ctrl_ns = VDEC_G_OUT_CTRL_NS;
70 return;
71 case VDEC_H:
72 out_ctrl = VDEC_H_OUT_CTRL1;
73 out_ctrl_ns = VDEC_H_OUT_CTRL_NS;
74 return;
75 }
76
77 value = cx25821_i2c_read(&dev->i2c_bus[0], out_ctrl, &tmp);
78 value &= 0xFFFFFF7F; /* clear BLUE_FIELD_EN */
79 if (enable)
80 value |= 0x00000080; /* set BLUE_FIELD_EN */
81 cx25821_i2c_write(&dev->i2c_bus[0], out_ctrl, value);
82
83 value = cx25821_i2c_read(&dev->i2c_bus[0], out_ctrl_ns, &tmp);
84 value &= 0xFFFFFF7F;
85 if (enable)
86 value |= 0x00000080; /* set BLUE_FIELD_EN */
87 cx25821_i2c_write(&dev->i2c_bus[0], out_ctrl_ns, value);
88}
89
90static int medusa_initialize_ntsc(struct cx25821_dev *dev)
91{
92 int ret_val = 0;
93 int i = 0;
94 u32 value = 0;
95 u32 tmp = 0;
96
97 mutex_lock(&dev->lock);
98
99 for (i = 0; i < MAX_DECODERS; i++) {
100 /* set video format NTSC-M */
101 value = cx25821_i2c_read(&dev->i2c_bus[0],
102 MODE_CTRL + (0x200 * i), &tmp);
103 value &= 0xFFFFFFF0;
104 /* enable the fast locking mode bit[16] */
105 value |= 0x10001;
106 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
107 MODE_CTRL + (0x200 * i), value);
108
109 /* resolution NTSC 720x480 */
110 value = cx25821_i2c_read(&dev->i2c_bus[0],
111 HORIZ_TIM_CTRL + (0x200 * i), &tmp);
112 value &= 0x00C00C00;
113 value |= 0x612D0074;
114 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
115 HORIZ_TIM_CTRL + (0x200 * i), value);
116
117 value = cx25821_i2c_read(&dev->i2c_bus[0],
118 VERT_TIM_CTRL + (0x200 * i), &tmp);
119 value &= 0x00C00C00;
120 value |= 0x1C1E001A; /* vblank_cnt + 2 to get camera ID */
121 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
122 VERT_TIM_CTRL + (0x200 * i), value);
123
124 /* chroma subcarrier step size */
125 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
126 SC_STEP_SIZE + (0x200 * i), 0x43E00000);
127
128 /* enable VIP optional active */
129 value = cx25821_i2c_read(&dev->i2c_bus[0],
130 OUT_CTRL_NS + (0x200 * i), &tmp);
131 value &= 0xFFFBFFFF;
132 value |= 0x00040000;
133 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
134 OUT_CTRL_NS + (0x200 * i), value);
135
136 /* enable VIP optional active (VIP_OPT_AL) for direct output. */
137 value = cx25821_i2c_read(&dev->i2c_bus[0],
138 OUT_CTRL1 + (0x200 * i), &tmp);
139 value &= 0xFFFBFFFF;
140 value |= 0x00040000;
141 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
142 OUT_CTRL1 + (0x200 * i), value);
143
144 /*
145 * clear VPRES_VERT_EN bit, fixes the chroma run away problem
146 * when the input switching rate < 16 fields
147 */
148 value = cx25821_i2c_read(&dev->i2c_bus[0],
149 MISC_TIM_CTRL + (0x200 * i), &tmp);
150 /* disable special play detection */
151 value = setBitAtPos(value, 14);
152 value = clearBitAtPos(value, 15);
153 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
154 MISC_TIM_CTRL + (0x200 * i), value);
155
156 /* set vbi_gate_en to 0 */
157 value = cx25821_i2c_read(&dev->i2c_bus[0],
158 DFE_CTRL1 + (0x200 * i), &tmp);
159 value = clearBitAtPos(value, 29);
160 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
161 DFE_CTRL1 + (0x200 * i), value);
162
163 /* Enable the generation of blue field output if no video */
164 medusa_enable_bluefield_output(dev, i, 1);
165 }
166
167 for (i = 0; i < MAX_ENCODERS; i++) {
168 /* NTSC hclock */
169 value = cx25821_i2c_read(&dev->i2c_bus[0],
170 DENC_A_REG_1 + (0x100 * i), &tmp);
171 value &= 0xF000FC00;
172 value |= 0x06B402D0;
173 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
174 DENC_A_REG_1 + (0x100 * i), value);
175
176 /* burst begin and burst end */
177 value = cx25821_i2c_read(&dev->i2c_bus[0],
178 DENC_A_REG_2 + (0x100 * i), &tmp);
179 value &= 0xFF000000;
180 value |= 0x007E9054;
181 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
182 DENC_A_REG_2 + (0x100 * i), value);
183
184 value = cx25821_i2c_read(&dev->i2c_bus[0],
185 DENC_A_REG_3 + (0x100 * i), &tmp);
186 value &= 0xFC00FE00;
187 value |= 0x00EC00F0;
188 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
189 DENC_A_REG_3 + (0x100 * i), value);
190
191 /* set NTSC vblank, no phase alternation, 7.5 IRE pedestal */
192 value = cx25821_i2c_read(&dev->i2c_bus[0],
193 DENC_A_REG_4 + (0x100 * i), &tmp);
194 value &= 0x00FCFFFF;
195 value |= 0x13020000;
196 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
197 DENC_A_REG_4 + (0x100 * i), value);
198
199 value = cx25821_i2c_read(&dev->i2c_bus[0],
200 DENC_A_REG_5 + (0x100 * i), &tmp);
201 value &= 0xFFFF0000;
202 value |= 0x0000E575;
203 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
204 DENC_A_REG_5 + (0x100 * i), value);
205
206 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
207 DENC_A_REG_6 + (0x100 * i), 0x009A89C1);
208
209 /* Subcarrier Increment */
210 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
211 DENC_A_REG_7 + (0x100 * i), 0x21F07C1F);
212 }
213
214 /* set picture resolutions */
215 /* 0 - 720 */
216 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], HSCALE_CTRL, 0x0);
217 /* 0 - 480 */
218 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], VSCALE_CTRL, 0x0);
219
220 /* set Bypass input format to NTSC 525 lines */
221 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
222 value |= 0x00080200;
223 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
224
225 mutex_unlock(&dev->lock);
226
227 return ret_val;
228}
229
230static int medusa_PALCombInit(struct cx25821_dev *dev, int dec)
231{
232 int ret_val = -1;
233 u32 value = 0, tmp = 0;
234
235 /* Setup for 2D threshold */
236 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
237 COMB_2D_HFS_CFG + (0x200 * dec), 0x20002861);
238 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
239 COMB_2D_HFD_CFG + (0x200 * dec), 0x20002861);
240 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
241 COMB_2D_LF_CFG + (0x200 * dec), 0x200A1023);
242
243 /* Setup flat chroma and luma thresholds */
244 value = cx25821_i2c_read(&dev->i2c_bus[0],
245 COMB_FLAT_THRESH_CTRL + (0x200 * dec), &tmp);
246 value &= 0x06230000;
247 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
248 COMB_FLAT_THRESH_CTRL + (0x200 * dec), value);
249
250 /* set comb 2D blend */
251 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
252 COMB_2D_BLEND + (0x200 * dec), 0x210F0F0F);
253
254 /* COMB MISC CONTROL */
255 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
256 COMB_MISC_CTRL + (0x200 * dec), 0x41120A7F);
257
258 return ret_val;
259}
260
261static int medusa_initialize_pal(struct cx25821_dev *dev)
262{
263 int ret_val = 0;
264 int i = 0;
265 u32 value = 0;
266 u32 tmp = 0;
267
268 mutex_lock(&dev->lock);
269
270 for (i = 0; i < MAX_DECODERS; i++) {
271 /* set video format PAL-BDGHI */
272 value = cx25821_i2c_read(&dev->i2c_bus[0],
273 MODE_CTRL + (0x200 * i), &tmp);
274 value &= 0xFFFFFFF0;
275 /* enable the fast locking mode bit[16] */
276 value |= 0x10004;
277 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
278 MODE_CTRL + (0x200 * i), value);
279
280 /* resolution PAL 720x576 */
281 value = cx25821_i2c_read(&dev->i2c_bus[0],
282 HORIZ_TIM_CTRL + (0x200 * i), &tmp);
283 value &= 0x00C00C00;
284 value |= 0x632D007D;
285 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
286 HORIZ_TIM_CTRL + (0x200 * i), value);
287
288 /* vblank656_cnt=x26, vactive_cnt=240h, vblank_cnt=x24 */
289 value = cx25821_i2c_read(&dev->i2c_bus[0],
290 VERT_TIM_CTRL + (0x200 * i), &tmp);
291 value &= 0x00C00C00;
292 value |= 0x28240026; /* vblank_cnt + 2 to get camera ID */
293 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
294 VERT_TIM_CTRL + (0x200 * i), value);
295
296 /* chroma subcarrier step size */
297 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
298 SC_STEP_SIZE + (0x200 * i), 0x5411E2D0);
299
300 /* enable VIP optional active */
301 value = cx25821_i2c_read(&dev->i2c_bus[0],
302 OUT_CTRL_NS + (0x200 * i), &tmp);
303 value &= 0xFFFBFFFF;
304 value |= 0x00040000;
305 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
306 OUT_CTRL_NS + (0x200 * i), value);
307
308 /* enable VIP optional active (VIP_OPT_AL) for direct output. */
309 value = cx25821_i2c_read(&dev->i2c_bus[0],
310 OUT_CTRL1 + (0x200 * i), &tmp);
311 value &= 0xFFFBFFFF;
312 value |= 0x00040000;
313 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
314 OUT_CTRL1 + (0x200 * i), value);
315
316 /*
317 * clear VPRES_VERT_EN bit, fixes the chroma run away problem
318 * when the input switching rate < 16 fields
319 */
320 value = cx25821_i2c_read(&dev->i2c_bus[0],
321 MISC_TIM_CTRL + (0x200 * i), &tmp);
322 /* disable special play detection */
323 value = setBitAtPos(value, 14);
324 value = clearBitAtPos(value, 15);
325 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
326 MISC_TIM_CTRL + (0x200 * i), value);
327
328 /* set vbi_gate_en to 0 */
329 value = cx25821_i2c_read(&dev->i2c_bus[0],
330 DFE_CTRL1 + (0x200 * i), &tmp);
331 value = clearBitAtPos(value, 29);
332 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
333 DFE_CTRL1 + (0x200 * i), value);
334
335 medusa_PALCombInit(dev, i);
336
337 /* Enable the generation of blue field output if no video */
338 medusa_enable_bluefield_output(dev, i, 1);
339 }
340
341 for (i = 0; i < MAX_ENCODERS; i++) {
342 /* PAL hclock */
343 value = cx25821_i2c_read(&dev->i2c_bus[0],
344 DENC_A_REG_1 + (0x100 * i), &tmp);
345 value &= 0xF000FC00;
346 value |= 0x06C002D0;
347 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
348 DENC_A_REG_1 + (0x100 * i), value);
349
350 /* burst begin and burst end */
351 value = cx25821_i2c_read(&dev->i2c_bus[0],
352 DENC_A_REG_2 + (0x100 * i), &tmp);
353 value &= 0xFF000000;
354 value |= 0x007E9754;
355 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
356 DENC_A_REG_2 + (0x100 * i), value);
357
358 /* hblank and vactive */
359 value = cx25821_i2c_read(&dev->i2c_bus[0],
360 DENC_A_REG_3 + (0x100 * i), &tmp);
361 value &= 0xFC00FE00;
362 value |= 0x00FC0120;
363 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
364 DENC_A_REG_3 + (0x100 * i), value);
365
366 /* set PAL vblank, phase alternation, 0 IRE pedestal */
367 value = cx25821_i2c_read(&dev->i2c_bus[0],
368 DENC_A_REG_4 + (0x100 * i), &tmp);
369 value &= 0x00FCFFFF;
370 value |= 0x14010000;
371 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
372 DENC_A_REG_4 + (0x100 * i), value);
373
374 value = cx25821_i2c_read(&dev->i2c_bus[0],
375 DENC_A_REG_5 + (0x100 * i), &tmp);
376 value &= 0xFFFF0000;
377 value |= 0x0000F078;
378 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
379 DENC_A_REG_5 + (0x100 * i), value);
380
381 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
382 DENC_A_REG_6 + (0x100 * i), 0x00A493CF);
383
384 /* Subcarrier Increment */
385 ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
386 DENC_A_REG_7 + (0x100 * i), 0x2A098ACB);
387 }
388
389 /* set picture resolutions */
390 /* 0 - 720 */
391 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], HSCALE_CTRL, 0x0);
392 /* 0 - 576 */
393 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], VSCALE_CTRL, 0x0);
394
395 /* set Bypass input format to PAL 625 lines */
396 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
397 value &= 0xFFF7FDFF;
398 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
399
400 mutex_unlock(&dev->lock);
401
402 return ret_val;
403}
404
405int medusa_set_videostandard(struct cx25821_dev *dev)
406{
407 int status = STATUS_SUCCESS;
408 u32 value = 0, tmp = 0;
409
410 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
411 status = medusa_initialize_pal(dev);
412 else
413 status = medusa_initialize_ntsc(dev);
414
415 /* Enable DENC_A output */
416 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_A_REG_4, &tmp);
417 value = setBitAtPos(value, 4);
418 status = cx25821_i2c_write(&dev->i2c_bus[0], DENC_A_REG_4, value);
419
420 /* Enable DENC_B output */
421 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_B_REG_4, &tmp);
422 value = setBitAtPos(value, 4);
423 status = cx25821_i2c_write(&dev->i2c_bus[0], DENC_B_REG_4, value);
424
425 return status;
426}
427
428void medusa_set_resolution(struct cx25821_dev *dev, int width,
429 int decoder_select)
430{
431 int decoder = 0;
432 int decoder_count = 0;
433 u32 hscale = 0x0;
434 u32 vscale = 0x0;
435 const int MAX_WIDTH = 720;
436
437 mutex_lock(&dev->lock);
438
439 /* validate the width */
440 if (width > MAX_WIDTH) {
441 pr_info("%s(): width %d > MAX_WIDTH %d ! resetting to MAX_WIDTH\n",
442 __func__, width, MAX_WIDTH);
443 width = MAX_WIDTH;
444 }
445
446 if (decoder_select <= 7 && decoder_select >= 0) {
447 decoder = decoder_select;
448 decoder_count = decoder_select + 1;
449 } else {
450 decoder = 0;
451 decoder_count = _num_decoders;
452 }
453
454 switch (width) {
455 case 320:
456 hscale = 0x13E34B;
457 vscale = 0x0;
458 break;
459
460 case 352:
461 hscale = 0x10A273;
462 vscale = 0x0;
463 break;
464
465 case 176:
466 hscale = 0x3115B2;
467 vscale = 0x1E00;
468 break;
469
470 case 160:
471 hscale = 0x378D84;
472 vscale = 0x1E00;
473 break;
474
475 default: /* 720 */
476 hscale = 0x0;
477 vscale = 0x0;
478 break;
479 }
480
481 for (; decoder < decoder_count; decoder++) {
482 /* write scaling values for each decoder */
483 cx25821_i2c_write(&dev->i2c_bus[0],
484 HSCALE_CTRL + (0x200 * decoder), hscale);
485 cx25821_i2c_write(&dev->i2c_bus[0],
486 VSCALE_CTRL + (0x200 * decoder), vscale);
487 }
488
489 mutex_unlock(&dev->lock);
490}
491
492static void medusa_set_decoderduration(struct cx25821_dev *dev, int decoder,
493 int duration)
494{
495 u32 fld_cnt = 0;
496 u32 tmp = 0;
497 u32 disp_cnt_reg = DISP_AB_CNT;
498
499 mutex_lock(&dev->lock);
500
501 /* no support */
502 if (decoder < VDEC_A || decoder > VDEC_H) {
503 mutex_unlock(&dev->lock);
504 return;
505 }
506
507 switch (decoder) {
508 default:
509 break;
510 case VDEC_C:
511 case VDEC_D:
512 disp_cnt_reg = DISP_CD_CNT;
513 break;
514 case VDEC_E:
515 case VDEC_F:
516 disp_cnt_reg = DISP_EF_CNT;
517 break;
518 case VDEC_G:
519 case VDEC_H:
520 disp_cnt_reg = DISP_GH_CNT;
521 break;
522 }
523
524 _display_field_cnt[decoder] = duration;
525
526 /* update hardware */
527 fld_cnt = cx25821_i2c_read(&dev->i2c_bus[0], disp_cnt_reg, &tmp);
528
529 if (!(decoder % 2)) { /* EVEN decoder */
530 fld_cnt &= 0xFFFF0000;
531 fld_cnt |= duration;
532 } else {
533 fld_cnt &= 0x0000FFFF;
534 fld_cnt |= ((u32) duration) << 16;
535 }
536
537 cx25821_i2c_write(&dev->i2c_bus[0], disp_cnt_reg, fld_cnt);
538
539 mutex_unlock(&dev->lock);
540}
541
542/* Map to Medusa register setting */
543static int mapM(int srcMin, int srcMax, int srcVal, int dstMin, int dstMax,
544 int *dstVal)
545{
546 int numerator;
547 int denominator;
548 int quotient;
549
550 if ((srcMin == srcMax) || (srcVal < srcMin) || (srcVal > srcMax))
551 return -1;
552 /*
553 * This is the overall expression used:
554 * *dstVal =
555 * (srcVal - srcMin)*(dstMax - dstMin) / (srcMax - srcMin) + dstMin;
556 * but we need to account for rounding so below we use the modulus
557 * operator to find the remainder and increment if necessary.
558 */
559 numerator = (srcVal - srcMin) * (dstMax - dstMin);
560 denominator = srcMax - srcMin;
561 quotient = numerator / denominator;
562
563 if (2 * (numerator % denominator) >= denominator)
564 quotient++;
565
566 *dstVal = quotient + dstMin;
567
568 return 0;
569}
570
571static unsigned long convert_to_twos(long numeric, unsigned long bits_len)
572{
573 unsigned char temp;
574
575 if (numeric >= 0)
576 return numeric;
577 else {
578 temp = ~(abs(numeric) & 0xFF);
579 temp += 1;
580 return temp;
581 }
582}
583
584int medusa_set_brightness(struct cx25821_dev *dev, int brightness, int decoder)
585{
586 int ret_val = 0;
587 int value = 0;
588 u32 val = 0, tmp = 0;
589
590 mutex_lock(&dev->lock);
591 if ((brightness > VIDEO_PROCAMP_MAX) ||
592 (brightness < VIDEO_PROCAMP_MIN)) {
593 mutex_unlock(&dev->lock);
594 return -1;
595 }
596 ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, brightness,
597 SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
598 value = convert_to_twos(value, 8);
599 val = cx25821_i2c_read(&dev->i2c_bus[0],
600 VDEC_A_BRITE_CTRL + (0x200 * decoder), &tmp);
601 val &= 0xFFFFFF00;
602 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
603 VDEC_A_BRITE_CTRL + (0x200 * decoder), val | value);
604 mutex_unlock(&dev->lock);
605 return ret_val;
606}
607
608int medusa_set_contrast(struct cx25821_dev *dev, int contrast, int decoder)
609{
610 int ret_val = 0;
611 int value = 0;
612 u32 val = 0, tmp = 0;
613
614 mutex_lock(&dev->lock);
615
616 if ((contrast > VIDEO_PROCAMP_MAX) || (contrast < VIDEO_PROCAMP_MIN)) {
617 mutex_unlock(&dev->lock);
618 return -1;
619 }
620
621 ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, contrast,
622 UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
623 val = cx25821_i2c_read(&dev->i2c_bus[0],
624 VDEC_A_CNTRST_CTRL + (0x200 * decoder), &tmp);
625 val &= 0xFFFFFF00;
626 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
627 VDEC_A_CNTRST_CTRL + (0x200 * decoder), val | value);
628
629 mutex_unlock(&dev->lock);
630 return ret_val;
631}
632
633int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder)
634{
635 int ret_val = 0;
636 int value = 0;
637 u32 val = 0, tmp = 0;
638
639 mutex_lock(&dev->lock);
640
641 if ((hue > VIDEO_PROCAMP_MAX) || (hue < VIDEO_PROCAMP_MIN)) {
642 mutex_unlock(&dev->lock);
643 return -1;
644 }
645
646 ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, hue,
647 SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
648
649 value = convert_to_twos(value, 8);
650 val = cx25821_i2c_read(&dev->i2c_bus[0],
651 VDEC_A_HUE_CTRL + (0x200 * decoder), &tmp);
652 val &= 0xFFFFFF00;
653
654 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
655 VDEC_A_HUE_CTRL + (0x200 * decoder), val | value);
656
657 mutex_unlock(&dev->lock);
658 return ret_val;
659}
660
661int medusa_set_saturation(struct cx25821_dev *dev, int saturation, int decoder)
662{
663 int ret_val = 0;
664 int value = 0;
665 u32 val = 0, tmp = 0;
666
667 mutex_lock(&dev->lock);
668
669 if ((saturation > VIDEO_PROCAMP_MAX) ||
670 (saturation < VIDEO_PROCAMP_MIN)) {
671 mutex_unlock(&dev->lock);
672 return -1;
673 }
674
675 ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, saturation,
676 UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
677
678 val = cx25821_i2c_read(&dev->i2c_bus[0],
679 VDEC_A_USAT_CTRL + (0x200 * decoder), &tmp);
680 val &= 0xFFFFFF00;
681 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
682 VDEC_A_USAT_CTRL + (0x200 * decoder), val | value);
683
684 val = cx25821_i2c_read(&dev->i2c_bus[0],
685 VDEC_A_VSAT_CTRL + (0x200 * decoder), &tmp);
686 val &= 0xFFFFFF00;
687 ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
688 VDEC_A_VSAT_CTRL + (0x200 * decoder), val | value);
689
690 mutex_unlock(&dev->lock);
691 return ret_val;
692}
693
694/* Program the display sequence and monitor output. */
695
696int medusa_video_init(struct cx25821_dev *dev)
697{
698 u32 value = 0, tmp = 0;
699 int ret_val = 0;
700 int i = 0;
701
702 mutex_lock(&dev->lock);
703
704 _num_decoders = dev->_max_num_decoders;
705
706 /* disable Auto source selection on all video decoders */
707 value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
708 value &= 0xFFFFF0FF;
709 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
710
711 if (ret_val < 0)
712 goto error;
713
714 /* Turn off Master source switch enable */
715 value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
716 value &= 0xFFFFFFDF;
717 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
718
719 if (ret_val < 0)
720 goto error;
721
722 mutex_unlock(&dev->lock);
723
724 for (i = 0; i < _num_decoders; i++)
725 medusa_set_decoderduration(dev, i, _display_field_cnt[i]);
726
727 mutex_lock(&dev->lock);
728
729 /* Select monitor as DENC A input, power up the DAC */
730 value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_AB_CTRL, &tmp);
731 value &= 0xFF70FF70;
732 value |= 0x00090008; /* set en_active */
733 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], DENC_AB_CTRL, value);
734
735 if (ret_val < 0)
736 goto error;
737
738 /* enable input is VIP/656 */
739 value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
740 value |= 0x00040100; /* enable VIP */
741 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
742
743 if (ret_val < 0)
744 goto error;
745
746 /* select AFE clock to output mode */
747 value = cx25821_i2c_read(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL, &tmp);
748 value &= 0x83FFFFFF;
749 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL,
750 value | 0x10000000);
751
752 if (ret_val < 0)
753 goto error;
754
755 /* Turn on all of the data out and control output pins. */
756 value = cx25821_i2c_read(&dev->i2c_bus[0], PIN_OE_CTRL, &tmp);
757 value &= 0xFEF0FE00;
758 if (_num_decoders == MAX_DECODERS) {
759 /*
760 * Note: The octal board does not support control pins(bit16-19)
761 * These bits are ignored in the octal board.
762 *
763 * disable VDEC A-C port, default to Mobilygen Interface
764 */
765 value |= 0x010001F8;
766 } else {
767 /* disable VDEC A-C port, default to Mobilygen Interface */
768 value |= 0x010F0108;
769 }
770
771 value |= 7;
772 ret_val = cx25821_i2c_write(&dev->i2c_bus[0], PIN_OE_CTRL, value);
773
774 if (ret_val < 0)
775 goto error;
776
777
778 mutex_unlock(&dev->lock);
779
780 ret_val = medusa_set_videostandard(dev);
781
782 return ret_val;
783
784error:
785 mutex_unlock(&dev->lock);
786 return ret_val;
787}
diff --git a/drivers/media/pci/cx25821/cx25821-medusa-video.h b/drivers/media/pci/cx25821/cx25821-medusa-video.h
new file mode 100644
index 000000000000..6175e0961855
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-medusa-video.h
@@ -0,0 +1,49 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#ifndef _MEDUSA_VIDEO_H
24#define _MEDUSA_VIDEO_H
25
26#include "cx25821-medusa-defines.h"
27
28/* Color control constants */
29#define VIDEO_PROCAMP_MIN 0
30#define VIDEO_PROCAMP_MAX 10000
31#define UNSIGNED_BYTE_MIN 0
32#define UNSIGNED_BYTE_MAX 0xFF
33#define SIGNED_BYTE_MIN -128
34#define SIGNED_BYTE_MAX 127
35
36/* Default video color settings */
37#define SHARPNESS_DEFAULT 50
38#define SATURATION_DEFAULT 5000
39#define BRIGHTNESS_DEFAULT 6200
40#define CONTRAST_DEFAULT 5000
41#define HUE_DEFAULT 5000
42
43unsigned short _num_decoders;
44unsigned short _num_cameras;
45
46unsigned int _video_standard;
47int _display_field_cnt[MAX_DECODERS];
48
49#endif
diff --git a/drivers/media/pci/cx25821/cx25821-reg.h b/drivers/media/pci/cx25821/cx25821-reg.h
new file mode 100644
index 000000000000..a3fc25a4dc0b
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-reg.h
@@ -0,0 +1,1592 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#ifndef __CX25821_REGISTERS__
24#define __CX25821_REGISTERS__
25
26/* Risc Instructions */
27#define RISC_CNT_INC 0x00010000
28#define RISC_CNT_RESET 0x00030000
29#define RISC_IRQ1 0x01000000
30#define RISC_IRQ2 0x02000000
31#define RISC_EOL 0x04000000
32#define RISC_SOL 0x08000000
33#define RISC_WRITE 0x10000000
34#define RISC_SKIP 0x20000000
35#define RISC_JUMP 0x70000000
36#define RISC_SYNC 0x80000000
37#define RISC_RESYNC 0x80008000
38#define RISC_READ 0x90000000
39#define RISC_WRITERM 0xB0000000
40#define RISC_WRITECM 0xC0000000
41#define RISC_WRITECR 0xD0000000
42#define RISC_WRITEC 0x50000000
43#define RISC_READC 0xA0000000
44
45#define RISC_SYNC_ODD 0x00000000
46#define RISC_SYNC_EVEN 0x00000200
47#define RISC_SYNC_ODD_VBI 0x00000006
48#define RISC_SYNC_EVEN_VBI 0x00000207
49#define RISC_NOOP 0xF0000000
50
51/*****************************************************************************
52* ASB SRAM
53 *****************************************************************************/
54#define TX_SRAM 0x000000 /* Transmit SRAM */
55
56/*****************************************************************************/
57#define RX_RAM 0x010000 /* Receive SRAM */
58
59/*****************************************************************************
60* Application Layer (AL)
61 *****************************************************************************/
62#define DEV_CNTRL2 0x040000 /* Device control */
63#define FLD_RUN_RISC 0x00000020
64
65/* ***************************************************************************** */
66#define PCI_INT_MSK 0x040010 /* PCI interrupt mask */
67#define PCI_INT_STAT 0x040014 /* PCI interrupt status */
68#define PCI_INT_MSTAT 0x040018 /* PCI interrupt masked status */
69#define FLD_HAMMERHEAD_INT (1 << 27)
70#define FLD_UART_INT (1 << 26)
71#define FLD_IRQN_INT (1 << 25)
72#define FLD_TM_INT (1 << 28)
73#define FLD_I2C_3_RACK (1 << 27)
74#define FLD_I2C_3_INT (1 << 26)
75#define FLD_I2C_2_RACK (1 << 25)
76#define FLD_I2C_2_INT (1 << 24)
77#define FLD_I2C_1_RACK (1 << 23)
78#define FLD_I2C_1_INT (1 << 22)
79
80#define FLD_APB_DMA_BERR_INT (1 << 21)
81#define FLD_AL_WR_BERR_INT (1 << 20)
82#define FLD_AL_RD_BERR_INT (1 << 19)
83#define FLD_RISC_WR_BERR_INT (1 << 18)
84#define FLD_RISC_RD_BERR_INT (1 << 17)
85
86#define FLD_VID_I_INT (1 << 8)
87#define FLD_VID_H_INT (1 << 7)
88#define FLD_VID_G_INT (1 << 6)
89#define FLD_VID_F_INT (1 << 5)
90#define FLD_VID_E_INT (1 << 4)
91#define FLD_VID_D_INT (1 << 3)
92#define FLD_VID_C_INT (1 << 2)
93#define FLD_VID_B_INT (1 << 1)
94#define FLD_VID_A_INT (1 << 0)
95
96/* ***************************************************************************** */
97#define VID_A_INT_MSK 0x040020 /* Video A interrupt mask */
98#define VID_A_INT_STAT 0x040024 /* Video A interrupt status */
99#define VID_A_INT_MSTAT 0x040028 /* Video A interrupt masked status */
100#define VID_A_INT_SSTAT 0x04002C /* Video A interrupt set status */
101
102/* ***************************************************************************** */
103#define VID_B_INT_MSK 0x040030 /* Video B interrupt mask */
104#define VID_B_INT_STAT 0x040034 /* Video B interrupt status */
105#define VID_B_INT_MSTAT 0x040038 /* Video B interrupt masked status */
106#define VID_B_INT_SSTAT 0x04003C /* Video B interrupt set status */
107
108/* ***************************************************************************** */
109#define VID_C_INT_MSK 0x040040 /* Video C interrupt mask */
110#define VID_C_INT_STAT 0x040044 /* Video C interrupt status */
111#define VID_C_INT_MSTAT 0x040048 /* Video C interrupt masked status */
112#define VID_C_INT_SSTAT 0x04004C /* Video C interrupt set status */
113
114/* ***************************************************************************** */
115#define VID_D_INT_MSK 0x040050 /* Video D interrupt mask */
116#define VID_D_INT_STAT 0x040054 /* Video D interrupt status */
117#define VID_D_INT_MSTAT 0x040058 /* Video D interrupt masked status */
118#define VID_D_INT_SSTAT 0x04005C /* Video D interrupt set status */
119
120/* ***************************************************************************** */
121#define VID_E_INT_MSK 0x040060 /* Video E interrupt mask */
122#define VID_E_INT_STAT 0x040064 /* Video E interrupt status */
123#define VID_E_INT_MSTAT 0x040068 /* Video E interrupt masked status */
124#define VID_E_INT_SSTAT 0x04006C /* Video E interrupt set status */
125
126/* ***************************************************************************** */
127#define VID_F_INT_MSK 0x040070 /* Video F interrupt mask */
128#define VID_F_INT_STAT 0x040074 /* Video F interrupt status */
129#define VID_F_INT_MSTAT 0x040078 /* Video F interrupt masked status */
130#define VID_F_INT_SSTAT 0x04007C /* Video F interrupt set status */
131
132/* ***************************************************************************** */
133#define VID_G_INT_MSK 0x040080 /* Video G interrupt mask */
134#define VID_G_INT_STAT 0x040084 /* Video G interrupt status */
135#define VID_G_INT_MSTAT 0x040088 /* Video G interrupt masked status */
136#define VID_G_INT_SSTAT 0x04008C /* Video G interrupt set status */
137
138/* ***************************************************************************** */
139#define VID_H_INT_MSK 0x040090 /* Video H interrupt mask */
140#define VID_H_INT_STAT 0x040094 /* Video H interrupt status */
141#define VID_H_INT_MSTAT 0x040098 /* Video H interrupt masked status */
142#define VID_H_INT_SSTAT 0x04009C /* Video H interrupt set status */
143
144/* ***************************************************************************** */
145#define VID_I_INT_MSK 0x0400A0 /* Video I interrupt mask */
146#define VID_I_INT_STAT 0x0400A4 /* Video I interrupt status */
147#define VID_I_INT_MSTAT 0x0400A8 /* Video I interrupt masked status */
148#define VID_I_INT_SSTAT 0x0400AC /* Video I interrupt set status */
149
150/* ***************************************************************************** */
151#define VID_J_INT_MSK 0x0400B0 /* Video J interrupt mask */
152#define VID_J_INT_STAT 0x0400B4 /* Video J interrupt status */
153#define VID_J_INT_MSTAT 0x0400B8 /* Video J interrupt masked status */
154#define VID_J_INT_SSTAT 0x0400BC /* Video J interrupt set status */
155
156#define FLD_VID_SRC_OPC_ERR 0x00020000
157#define FLD_VID_DST_OPC_ERR 0x00010000
158#define FLD_VID_SRC_SYNC 0x00002000
159#define FLD_VID_DST_SYNC 0x00001000
160#define FLD_VID_SRC_UF 0x00000200
161#define FLD_VID_DST_OF 0x00000100
162#define FLD_VID_SRC_RISC2 0x00000020
163#define FLD_VID_DST_RISC2 0x00000010
164#define FLD_VID_SRC_RISC1 0x00000002
165#define FLD_VID_DST_RISC1 0x00000001
166#define FLD_VID_SRC_ERRORS (FLD_VID_SRC_OPC_ERR | FLD_VID_SRC_SYNC | FLD_VID_SRC_UF)
167#define FLD_VID_DST_ERRORS (FLD_VID_DST_OPC_ERR | FLD_VID_DST_SYNC | FLD_VID_DST_OF)
168
169/* ***************************************************************************** */
170#define AUD_A_INT_MSK 0x0400C0 /* Audio Int interrupt mask */
171#define AUD_A_INT_STAT 0x0400C4 /* Audio Int interrupt status */
172#define AUD_A_INT_MSTAT 0x0400C8 /* Audio Int interrupt masked status */
173#define AUD_A_INT_SSTAT 0x0400CC /* Audio Int interrupt set status */
174
175/* ***************************************************************************** */
176#define AUD_B_INT_MSK 0x0400D0 /* Audio Int interrupt mask */
177#define AUD_B_INT_STAT 0x0400D4 /* Audio Int interrupt status */
178#define AUD_B_INT_MSTAT 0x0400D8 /* Audio Int interrupt masked status */
179#define AUD_B_INT_SSTAT 0x0400DC /* Audio Int interrupt set status */
180
181/* ***************************************************************************** */
182#define AUD_C_INT_MSK 0x0400E0 /* Audio Int interrupt mask */
183#define AUD_C_INT_STAT 0x0400E4 /* Audio Int interrupt status */
184#define AUD_C_INT_MSTAT 0x0400E8 /* Audio Int interrupt masked status */
185#define AUD_C_INT_SSTAT 0x0400EC /* Audio Int interrupt set status */
186
187/* ***************************************************************************** */
188#define AUD_D_INT_MSK 0x0400F0 /* Audio Int interrupt mask */
189#define AUD_D_INT_STAT 0x0400F4 /* Audio Int interrupt status */
190#define AUD_D_INT_MSTAT 0x0400F8 /* Audio Int interrupt masked status */
191#define AUD_D_INT_SSTAT 0x0400FC /* Audio Int interrupt set status */
192
193/* ***************************************************************************** */
194#define AUD_E_INT_MSK 0x040100 /* Audio Int interrupt mask */
195#define AUD_E_INT_STAT 0x040104 /* Audio Int interrupt status */
196#define AUD_E_INT_MSTAT 0x040108 /* Audio Int interrupt masked status */
197#define AUD_E_INT_SSTAT 0x04010C /* Audio Int interrupt set status */
198
199#define FLD_AUD_SRC_OPC_ERR 0x00020000
200#define FLD_AUD_DST_OPC_ERR 0x00010000
201#define FLD_AUD_SRC_SYNC 0x00002000
202#define FLD_AUD_DST_SYNC 0x00001000
203#define FLD_AUD_SRC_OF 0x00000200
204#define FLD_AUD_DST_OF 0x00000100
205#define FLD_AUD_SRC_RISCI2 0x00000020
206#define FLD_AUD_DST_RISCI2 0x00000010
207#define FLD_AUD_SRC_RISCI1 0x00000002
208#define FLD_AUD_DST_RISCI1 0x00000001
209
210/* ***************************************************************************** */
211#define MBIF_A_INT_MSK 0x040110 /* MBIF Int interrupt mask */
212#define MBIF_A_INT_STAT 0x040114 /* MBIF Int interrupt status */
213#define MBIF_A_INT_MSTAT 0x040118 /* MBIF Int interrupt masked status */
214#define MBIF_A_INT_SSTAT 0x04011C /* MBIF Int interrupt set status */
215
216/* ***************************************************************************** */
217#define MBIF_B_INT_MSK 0x040120 /* MBIF Int interrupt mask */
218#define MBIF_B_INT_STAT 0x040124 /* MBIF Int interrupt status */
219#define MBIF_B_INT_MSTAT 0x040128 /* MBIF Int interrupt masked status */
220#define MBIF_B_INT_SSTAT 0x04012C /* MBIF Int interrupt set status */
221
222#define FLD_MBIF_DST_OPC_ERR 0x00010000
223#define FLD_MBIF_DST_SYNC 0x00001000
224#define FLD_MBIF_DST_OF 0x00000100
225#define FLD_MBIF_DST_RISCI2 0x00000010
226#define FLD_MBIF_DST_RISCI1 0x00000001
227
228/* ***************************************************************************** */
229#define AUD_EXT_INT_MSK 0x040060 /* Audio Ext interrupt mask */
230#define AUD_EXT_INT_STAT 0x040064 /* Audio Ext interrupt status */
231#define AUD_EXT_INT_MSTAT 0x040068 /* Audio Ext interrupt masked status */
232#define AUD_EXT_INT_SSTAT 0x04006C /* Audio Ext interrupt set status */
233#define FLD_AUD_EXT_OPC_ERR 0x00010000
234#define FLD_AUD_EXT_SYNC 0x00001000
235#define FLD_AUD_EXT_OF 0x00000100
236#define FLD_AUD_EXT_RISCI2 0x00000010
237#define FLD_AUD_EXT_RISCI1 0x00000001
238
239/* ***************************************************************************** */
240#define GPIO_LO 0x110010 /* Lower of GPIO pins [31:0] */
241#define GPIO_HI 0x110014 /* Upper WORD of GPIO pins [47:31] */
242
243#define GPIO_LO_OE 0x110018 /* Lower of GPIO output enable [31:0] */
244#define GPIO_HI_OE 0x11001C /* Upper word of GPIO output enable [47:32] */
245
246#define GPIO_LO_INT_MSK 0x11003C /* GPIO interrupt mask */
247#define GPIO_LO_INT_STAT 0x110044 /* GPIO interrupt status */
248#define GPIO_LO_INT_MSTAT 0x11004C /* GPIO interrupt masked status */
249#define GPIO_LO_ISM_SNS 0x110054 /* GPIO interrupt sensitivity */
250#define GPIO_LO_ISM_POL 0x11005C /* GPIO interrupt polarity */
251
252#define GPIO_HI_INT_MSK 0x110040 /* GPIO interrupt mask */
253#define GPIO_HI_INT_STAT 0x110048 /* GPIO interrupt status */
254#define GPIO_HI_INT_MSTAT 0x110050 /* GPIO interrupt masked status */
255#define GPIO_HI_ISM_SNS 0x110058 /* GPIO interrupt sensitivity */
256#define GPIO_HI_ISM_POL 0x110060 /* GPIO interrupt polarity */
257
258#define FLD_GPIO43_INT (1 << 11)
259#define FLD_GPIO42_INT (1 << 10)
260#define FLD_GPIO41_INT (1 << 9)
261#define FLD_GPIO40_INT (1 << 8)
262
263#define FLD_GPIO9_INT (1 << 9)
264#define FLD_GPIO8_INT (1 << 8)
265#define FLD_GPIO7_INT (1 << 7)
266#define FLD_GPIO6_INT (1 << 6)
267#define FLD_GPIO5_INT (1 << 5)
268#define FLD_GPIO4_INT (1 << 4)
269#define FLD_GPIO3_INT (1 << 3)
270#define FLD_GPIO2_INT (1 << 2)
271#define FLD_GPIO1_INT (1 << 1)
272#define FLD_GPIO0_INT (1 << 0)
273
274/* ***************************************************************************** */
275#define TC_REQ 0x040090 /* Rider PCI Express traFFic class request */
276
277/* ***************************************************************************** */
278#define TC_REQ_SET 0x040094 /* Rider PCI Express traFFic class request set */
279
280/* ***************************************************************************** */
281/* Rider */
282/* ***************************************************************************** */
283
284/* PCI Compatible Header */
285/* ***************************************************************************** */
286#define RDR_CFG0 0x050000
287#define RDR_VENDOR_DEVICE_ID_CFG 0x050000
288
289/* ***************************************************************************** */
290#define RDR_CFG1 0x050004
291
292/* ***************************************************************************** */
293#define RDR_CFG2 0x050008
294
295/* ***************************************************************************** */
296#define RDR_CFG3 0x05000C
297
298/* ***************************************************************************** */
299#define RDR_CFG4 0x050010
300
301/* ***************************************************************************** */
302#define RDR_CFG5 0x050014
303
304/* ***************************************************************************** */
305#define RDR_CFG6 0x050018
306
307/* ***************************************************************************** */
308#define RDR_CFG7 0x05001C
309
310/* ***************************************************************************** */
311#define RDR_CFG8 0x050020
312
313/* ***************************************************************************** */
314#define RDR_CFG9 0x050024
315
316/* ***************************************************************************** */
317#define RDR_CFGA 0x050028
318
319/* ***************************************************************************** */
320#define RDR_CFGB 0x05002C
321#define RDR_SUSSYSTEM_ID_CFG 0x05002C
322
323/* ***************************************************************************** */
324#define RDR_CFGC 0x050030
325
326/* ***************************************************************************** */
327#define RDR_CFGD 0x050034
328
329/* ***************************************************************************** */
330#define RDR_CFGE 0x050038
331
332/* ***************************************************************************** */
333#define RDR_CFGF 0x05003C
334
335/* ***************************************************************************** */
336/* PCI-Express Capabilities */
337/* ***************************************************************************** */
338#define RDR_PECAP 0x050040
339
340/* ***************************************************************************** */
341#define RDR_PEDEVCAP 0x050044
342
343/* ***************************************************************************** */
344#define RDR_PEDEVSC 0x050048
345
346/* ***************************************************************************** */
347#define RDR_PELINKCAP 0x05004C
348
349/* ***************************************************************************** */
350#define RDR_PELINKSC 0x050050
351
352/* ***************************************************************************** */
353#define RDR_PMICAP 0x050080
354
355/* ***************************************************************************** */
356#define RDR_PMCSR 0x050084
357
358/* ***************************************************************************** */
359#define RDR_VPDCAP 0x050090
360
361/* ***************************************************************************** */
362#define RDR_VPDDATA 0x050094
363
364/* ***************************************************************************** */
365#define RDR_MSICAP 0x0500A0
366
367/* ***************************************************************************** */
368#define RDR_MSIARL 0x0500A4
369
370/* ***************************************************************************** */
371#define RDR_MSIARU 0x0500A8
372
373/* ***************************************************************************** */
374#define RDR_MSIDATA 0x0500AC
375
376/* ***************************************************************************** */
377/* PCI Express Extended Capabilities */
378/* ***************************************************************************** */
379#define RDR_AERXCAP 0x050100
380
381/* ***************************************************************************** */
382#define RDR_AERUESTA 0x050104
383
384/* ***************************************************************************** */
385#define RDR_AERUEMSK 0x050108
386
387/* ***************************************************************************** */
388#define RDR_AERUESEV 0x05010C
389
390/* ***************************************************************************** */
391#define RDR_AERCESTA 0x050110
392
393/* ***************************************************************************** */
394#define RDR_AERCEMSK 0x050114
395
396/* ***************************************************************************** */
397#define RDR_AERCC 0x050118
398
399/* ***************************************************************************** */
400#define RDR_AERHL0 0x05011C
401
402/* ***************************************************************************** */
403#define RDR_AERHL1 0x050120
404
405/* ***************************************************************************** */
406#define RDR_AERHL2 0x050124
407
408/* ***************************************************************************** */
409#define RDR_AERHL3 0x050128
410
411/* ***************************************************************************** */
412#define RDR_VCXCAP 0x050200
413
414/* ***************************************************************************** */
415#define RDR_VCCAP1 0x050204
416
417/* ***************************************************************************** */
418#define RDR_VCCAP2 0x050208
419
420/* ***************************************************************************** */
421#define RDR_VCSC 0x05020C
422
423/* ***************************************************************************** */
424#define RDR_VCR0_CAP 0x050210
425
426/* ***************************************************************************** */
427#define RDR_VCR0_CTRL 0x050214
428
429/* ***************************************************************************** */
430#define RDR_VCR0_STAT 0x050218
431
432/* ***************************************************************************** */
433#define RDR_VCR1_CAP 0x05021C
434
435/* ***************************************************************************** */
436#define RDR_VCR1_CTRL 0x050220
437
438/* ***************************************************************************** */
439#define RDR_VCR1_STAT 0x050224
440
441/* ***************************************************************************** */
442#define RDR_VCR2_CAP 0x050228
443
444/* ***************************************************************************** */
445#define RDR_VCR2_CTRL 0x05022C
446
447/* ***************************************************************************** */
448#define RDR_VCR2_STAT 0x050230
449
450/* ***************************************************************************** */
451#define RDR_VCR3_CAP 0x050234
452
453/* ***************************************************************************** */
454#define RDR_VCR3_CTRL 0x050238
455
456/* ***************************************************************************** */
457#define RDR_VCR3_STAT 0x05023C
458
459/* ***************************************************************************** */
460#define RDR_VCARB0 0x050240
461
462/* ***************************************************************************** */
463#define RDR_VCARB1 0x050244
464
465/* ***************************************************************************** */
466#define RDR_VCARB2 0x050248
467
468/* ***************************************************************************** */
469#define RDR_VCARB3 0x05024C
470
471/* ***************************************************************************** */
472#define RDR_VCARB4 0x050250
473
474/* ***************************************************************************** */
475#define RDR_VCARB5 0x050254
476
477/* ***************************************************************************** */
478#define RDR_VCARB6 0x050258
479
480/* ***************************************************************************** */
481#define RDR_VCARB7 0x05025C
482
483/* ***************************************************************************** */
484#define RDR_RDRSTAT0 0x050300
485
486/* ***************************************************************************** */
487#define RDR_RDRSTAT1 0x050304
488
489/* ***************************************************************************** */
490#define RDR_RDRCTL0 0x050308
491
492/* ***************************************************************************** */
493#define RDR_RDRCTL1 0x05030C
494
495/* ***************************************************************************** */
496/* Transaction Layer Registers */
497/* ***************************************************************************** */
498#define RDR_TLSTAT0 0x050310
499
500/* ***************************************************************************** */
501#define RDR_TLSTAT1 0x050314
502
503/* ***************************************************************************** */
504#define RDR_TLCTL0 0x050318
505#define FLD_CFG_UR_CPL_MODE 0x00000040
506#define FLD_CFG_CORR_ERR_QUITE 0x00000020
507#define FLD_CFG_RCB_CK_EN 0x00000010
508#define FLD_CFG_BNDRY_CK_EN 0x00000008
509#define FLD_CFG_BYTE_EN_CK_EN 0x00000004
510#define FLD_CFG_RELAX_ORDER_MSK 0x00000002
511#define FLD_CFG_TAG_ORDER_EN 0x00000001
512
513/* ***************************************************************************** */
514#define RDR_TLCTL1 0x05031C
515
516/* ***************************************************************************** */
517#define RDR_REQRCAL 0x050320
518
519/* ***************************************************************************** */
520#define RDR_REQRCAU 0x050324
521
522/* ***************************************************************************** */
523#define RDR_REQEPA 0x050328
524
525/* ***************************************************************************** */
526#define RDR_REQCTRL 0x05032C
527
528/* ***************************************************************************** */
529#define RDR_REQSTAT 0x050330
530
531/* ***************************************************************************** */
532#define RDR_TL_TEST 0x050334
533
534/* ***************************************************************************** */
535#define RDR_VCR01_CTL 0x050348
536
537/* ***************************************************************************** */
538#define RDR_VCR23_CTL 0x05034C
539
540/* ***************************************************************************** */
541#define RDR_RX_VCR0_FC 0x050350
542
543/* ***************************************************************************** */
544#define RDR_RX_VCR1_FC 0x050354
545
546/* ***************************************************************************** */
547#define RDR_RX_VCR2_FC 0x050358
548
549/* ***************************************************************************** */
550#define RDR_RX_VCR3_FC 0x05035C
551
552/* ***************************************************************************** */
553/* Data Link Layer Registers */
554/* ***************************************************************************** */
555#define RDR_DLLSTAT 0x050360
556
557/* ***************************************************************************** */
558#define RDR_DLLCTRL 0x050364
559
560/* ***************************************************************************** */
561#define RDR_REPLAYTO 0x050368
562
563/* ***************************************************************************** */
564#define RDR_ACKLATTO 0x05036C
565
566/* ***************************************************************************** */
567/* MAC Layer Registers */
568/* ***************************************************************************** */
569#define RDR_MACSTAT0 0x050380
570
571/* ***************************************************************************** */
572#define RDR_MACSTAT1 0x050384
573
574/* ***************************************************************************** */
575#define RDR_MACCTRL0 0x050388
576
577/* ***************************************************************************** */
578#define RDR_MACCTRL1 0x05038C
579
580/* ***************************************************************************** */
581#define RDR_MACCTRL2 0x050390
582
583/* ***************************************************************************** */
584#define RDR_MAC_LB_DATA 0x050394
585
586/* ***************************************************************************** */
587#define RDR_L0S_EXIT_LAT 0x050398
588
589/* ***************************************************************************** */
590/* DMAC */
591/* ***************************************************************************** */
592#define DMA1_PTR1 0x100000 /* DMA Current Ptr : Ch#1 */
593
594/* ***************************************************************************** */
595#define DMA2_PTR1 0x100004 /* DMA Current Ptr : Ch#2 */
596
597/* ***************************************************************************** */
598#define DMA3_PTR1 0x100008 /* DMA Current Ptr : Ch#3 */
599
600/* ***************************************************************************** */
601#define DMA4_PTR1 0x10000C /* DMA Current Ptr : Ch#4 */
602
603/* ***************************************************************************** */
604#define DMA5_PTR1 0x100010 /* DMA Current Ptr : Ch#5 */
605
606/* ***************************************************************************** */
607#define DMA6_PTR1 0x100014 /* DMA Current Ptr : Ch#6 */
608
609/* ***************************************************************************** */
610#define DMA7_PTR1 0x100018 /* DMA Current Ptr : Ch#7 */
611
612/* ***************************************************************************** */
613#define DMA8_PTR1 0x10001C /* DMA Current Ptr : Ch#8 */
614
615/* ***************************************************************************** */
616#define DMA9_PTR1 0x100020 /* DMA Current Ptr : Ch#9 */
617
618/* ***************************************************************************** */
619#define DMA10_PTR1 0x100024 /* DMA Current Ptr : Ch#10 */
620
621/* ***************************************************************************** */
622#define DMA11_PTR1 0x100028 /* DMA Current Ptr : Ch#11 */
623
624/* ***************************************************************************** */
625#define DMA12_PTR1 0x10002C /* DMA Current Ptr : Ch#12 */
626
627/* ***************************************************************************** */
628#define DMA13_PTR1 0x100030 /* DMA Current Ptr : Ch#13 */
629
630/* ***************************************************************************** */
631#define DMA14_PTR1 0x100034 /* DMA Current Ptr : Ch#14 */
632
633/* ***************************************************************************** */
634#define DMA15_PTR1 0x100038 /* DMA Current Ptr : Ch#15 */
635
636/* ***************************************************************************** */
637#define DMA16_PTR1 0x10003C /* DMA Current Ptr : Ch#16 */
638
639/* ***************************************************************************** */
640#define DMA17_PTR1 0x100040 /* DMA Current Ptr : Ch#17 */
641
642/* ***************************************************************************** */
643#define DMA18_PTR1 0x100044 /* DMA Current Ptr : Ch#18 */
644
645/* ***************************************************************************** */
646#define DMA19_PTR1 0x100048 /* DMA Current Ptr : Ch#19 */
647
648/* ***************************************************************************** */
649#define DMA20_PTR1 0x10004C /* DMA Current Ptr : Ch#20 */
650
651/* ***************************************************************************** */
652#define DMA21_PTR1 0x100050 /* DMA Current Ptr : Ch#21 */
653
654/* ***************************************************************************** */
655#define DMA22_PTR1 0x100054 /* DMA Current Ptr : Ch#22 */
656
657/* ***************************************************************************** */
658#define DMA23_PTR1 0x100058 /* DMA Current Ptr : Ch#23 */
659
660/* ***************************************************************************** */
661#define DMA24_PTR1 0x10005C /* DMA Current Ptr : Ch#24 */
662
663/* ***************************************************************************** */
664#define DMA25_PTR1 0x100060 /* DMA Current Ptr : Ch#25 */
665
666/* ***************************************************************************** */
667#define DMA26_PTR1 0x100064 /* DMA Current Ptr : Ch#26 */
668
669/* ***************************************************************************** */
670#define DMA1_PTR2 0x100080 /* DMA Tab Ptr : Ch#1 */
671
672/* ***************************************************************************** */
673#define DMA2_PTR2 0x100084 /* DMA Tab Ptr : Ch#2 */
674
675/* ***************************************************************************** */
676#define DMA3_PTR2 0x100088 /* DMA Tab Ptr : Ch#3 */
677
678/* ***************************************************************************** */
679#define DMA4_PTR2 0x10008C /* DMA Tab Ptr : Ch#4 */
680
681/* ***************************************************************************** */
682#define DMA5_PTR2 0x100090 /* DMA Tab Ptr : Ch#5 */
683
684/* ***************************************************************************** */
685#define DMA6_PTR2 0x100094 /* DMA Tab Ptr : Ch#6 */
686
687/* ***************************************************************************** */
688#define DMA7_PTR2 0x100098 /* DMA Tab Ptr : Ch#7 */
689
690/* ***************************************************************************** */
691#define DMA8_PTR2 0x10009C /* DMA Tab Ptr : Ch#8 */
692
693/* ***************************************************************************** */
694#define DMA9_PTR2 0x1000A0 /* DMA Tab Ptr : Ch#9 */
695
696/* ***************************************************************************** */
697#define DMA10_PTR2 0x1000A4 /* DMA Tab Ptr : Ch#10 */
698
699/* ***************************************************************************** */
700#define DMA11_PTR2 0x1000A8 /* DMA Tab Ptr : Ch#11 */
701
702/* ***************************************************************************** */
703#define DMA12_PTR2 0x1000AC /* DMA Tab Ptr : Ch#12 */
704
705/* ***************************************************************************** */
706#define DMA13_PTR2 0x1000B0 /* DMA Tab Ptr : Ch#13 */
707
708/* ***************************************************************************** */
709#define DMA14_PTR2 0x1000B4 /* DMA Tab Ptr : Ch#14 */
710
711/* ***************************************************************************** */
712#define DMA15_PTR2 0x1000B8 /* DMA Tab Ptr : Ch#15 */
713
714/* ***************************************************************************** */
715#define DMA16_PTR2 0x1000BC /* DMA Tab Ptr : Ch#16 */
716
717/* ***************************************************************************** */
718#define DMA17_PTR2 0x1000C0 /* DMA Tab Ptr : Ch#17 */
719
720/* ***************************************************************************** */
721#define DMA18_PTR2 0x1000C4 /* DMA Tab Ptr : Ch#18 */
722
723/* ***************************************************************************** */
724#define DMA19_PTR2 0x1000C8 /* DMA Tab Ptr : Ch#19 */
725
726/* ***************************************************************************** */
727#define DMA20_PTR2 0x1000CC /* DMA Tab Ptr : Ch#20 */
728
729/* ***************************************************************************** */
730#define DMA21_PTR2 0x1000D0 /* DMA Tab Ptr : Ch#21 */
731
732/* ***************************************************************************** */
733#define DMA22_PTR2 0x1000D4 /* DMA Tab Ptr : Ch#22 */
734
735/* ***************************************************************************** */
736#define DMA23_PTR2 0x1000D8 /* DMA Tab Ptr : Ch#23 */
737
738/* ***************************************************************************** */
739#define DMA24_PTR2 0x1000DC /* DMA Tab Ptr : Ch#24 */
740
741/* ***************************************************************************** */
742#define DMA25_PTR2 0x1000E0 /* DMA Tab Ptr : Ch#25 */
743
744/* ***************************************************************************** */
745#define DMA26_PTR2 0x1000E4 /* DMA Tab Ptr : Ch#26 */
746
747/* ***************************************************************************** */
748#define DMA1_CNT1 0x100100 /* DMA BuFFer Size : Ch#1 */
749
750/* ***************************************************************************** */
751#define DMA2_CNT1 0x100104 /* DMA BuFFer Size : Ch#2 */
752
753/* ***************************************************************************** */
754#define DMA3_CNT1 0x100108 /* DMA BuFFer Size : Ch#3 */
755
756/* ***************************************************************************** */
757#define DMA4_CNT1 0x10010C /* DMA BuFFer Size : Ch#4 */
758
759/* ***************************************************************************** */
760#define DMA5_CNT1 0x100110 /* DMA BuFFer Size : Ch#5 */
761
762/* ***************************************************************************** */
763#define DMA6_CNT1 0x100114 /* DMA BuFFer Size : Ch#6 */
764
765/* ***************************************************************************** */
766#define DMA7_CNT1 0x100118 /* DMA BuFFer Size : Ch#7 */
767
768/* ***************************************************************************** */
769#define DMA8_CNT1 0x10011C /* DMA BuFFer Size : Ch#8 */
770
771/* ***************************************************************************** */
772#define DMA9_CNT1 0x100120 /* DMA BuFFer Size : Ch#9 */
773
774/* ***************************************************************************** */
775#define DMA10_CNT1 0x100124 /* DMA BuFFer Size : Ch#10 */
776
777/* ***************************************************************************** */
778#define DMA11_CNT1 0x100128 /* DMA BuFFer Size : Ch#11 */
779
780/* ***************************************************************************** */
781#define DMA12_CNT1 0x10012C /* DMA BuFFer Size : Ch#12 */
782
783/* ***************************************************************************** */
784#define DMA13_CNT1 0x100130 /* DMA BuFFer Size : Ch#13 */
785
786/* ***************************************************************************** */
787#define DMA14_CNT1 0x100134 /* DMA BuFFer Size : Ch#14 */
788
789/* ***************************************************************************** */
790#define DMA15_CNT1 0x100138 /* DMA BuFFer Size : Ch#15 */
791
792/* ***************************************************************************** */
793#define DMA16_CNT1 0x10013C /* DMA BuFFer Size : Ch#16 */
794
795/* ***************************************************************************** */
796#define DMA17_CNT1 0x100140 /* DMA BuFFer Size : Ch#17 */
797
798/* ***************************************************************************** */
799#define DMA18_CNT1 0x100144 /* DMA BuFFer Size : Ch#18 */
800
801/* ***************************************************************************** */
802#define DMA19_CNT1 0x100148 /* DMA BuFFer Size : Ch#19 */
803
804/* ***************************************************************************** */
805#define DMA20_CNT1 0x10014C /* DMA BuFFer Size : Ch#20 */
806
807/* ***************************************************************************** */
808#define DMA21_CNT1 0x100150 /* DMA BuFFer Size : Ch#21 */
809
810/* ***************************************************************************** */
811#define DMA22_CNT1 0x100154 /* DMA BuFFer Size : Ch#22 */
812
813/* ***************************************************************************** */
814#define DMA23_CNT1 0x100158 /* DMA BuFFer Size : Ch#23 */
815
816/* ***************************************************************************** */
817#define DMA24_CNT1 0x10015C /* DMA BuFFer Size : Ch#24 */
818
819/* ***************************************************************************** */
820#define DMA25_CNT1 0x100160 /* DMA BuFFer Size : Ch#25 */
821
822/* ***************************************************************************** */
823#define DMA26_CNT1 0x100164 /* DMA BuFFer Size : Ch#26 */
824
825/* ***************************************************************************** */
826#define DMA1_CNT2 0x100180 /* DMA Table Size : Ch#1 */
827
828/* ***************************************************************************** */
829#define DMA2_CNT2 0x100184 /* DMA Table Size : Ch#2 */
830
831/* ***************************************************************************** */
832#define DMA3_CNT2 0x100188 /* DMA Table Size : Ch#3 */
833
834/* ***************************************************************************** */
835#define DMA4_CNT2 0x10018C /* DMA Table Size : Ch#4 */
836
837/* ***************************************************************************** */
838#define DMA5_CNT2 0x100190 /* DMA Table Size : Ch#5 */
839
840/* ***************************************************************************** */
841#define DMA6_CNT2 0x100194 /* DMA Table Size : Ch#6 */
842
843/* ***************************************************************************** */
844#define DMA7_CNT2 0x100198 /* DMA Table Size : Ch#7 */
845
846/* ***************************************************************************** */
847#define DMA8_CNT2 0x10019C /* DMA Table Size : Ch#8 */
848
849/* ***************************************************************************** */
850#define DMA9_CNT2 0x1001A0 /* DMA Table Size : Ch#9 */
851
852/* ***************************************************************************** */
853#define DMA10_CNT2 0x1001A4 /* DMA Table Size : Ch#10 */
854
855/* ***************************************************************************** */
856#define DMA11_CNT2 0x1001A8 /* DMA Table Size : Ch#11 */
857
858/* ***************************************************************************** */
859#define DMA12_CNT2 0x1001AC /* DMA Table Size : Ch#12 */
860
861/* ***************************************************************************** */
862#define DMA13_CNT2 0x1001B0 /* DMA Table Size : Ch#13 */
863
864/* ***************************************************************************** */
865#define DMA14_CNT2 0x1001B4 /* DMA Table Size : Ch#14 */
866
867/* ***************************************************************************** */
868#define DMA15_CNT2 0x1001B8 /* DMA Table Size : Ch#15 */
869
870/* ***************************************************************************** */
871#define DMA16_CNT2 0x1001BC /* DMA Table Size : Ch#16 */
872
873/* ***************************************************************************** */
874#define DMA17_CNT2 0x1001C0 /* DMA Table Size : Ch#17 */
875
876/* ***************************************************************************** */
877#define DMA18_CNT2 0x1001C4 /* DMA Table Size : Ch#18 */
878
879/* ***************************************************************************** */
880#define DMA19_CNT2 0x1001C8 /* DMA Table Size : Ch#19 */
881
882/* ***************************************************************************** */
883#define DMA20_CNT2 0x1001CC /* DMA Table Size : Ch#20 */
884
885/* ***************************************************************************** */
886#define DMA21_CNT2 0x1001D0 /* DMA Table Size : Ch#21 */
887
888/* ***************************************************************************** */
889#define DMA22_CNT2 0x1001D4 /* DMA Table Size : Ch#22 */
890
891/* ***************************************************************************** */
892#define DMA23_CNT2 0x1001D8 /* DMA Table Size : Ch#23 */
893
894/* ***************************************************************************** */
895#define DMA24_CNT2 0x1001DC /* DMA Table Size : Ch#24 */
896
897/* ***************************************************************************** */
898#define DMA25_CNT2 0x1001E0 /* DMA Table Size : Ch#25 */
899
900/* ***************************************************************************** */
901#define DMA26_CNT2 0x1001E4 /* DMA Table Size : Ch#26 */
902
903/* ***************************************************************************** */
904 /* ITG */
905/* ***************************************************************************** */
906#define TM_CNT_LDW 0x110000 /* Timer : Counter low */
907
908/* ***************************************************************************** */
909#define TM_CNT_UW 0x110004 /* Timer : Counter high word */
910
911/* ***************************************************************************** */
912#define TM_LMT_LDW 0x110008 /* Timer : Limit low */
913
914/* ***************************************************************************** */
915#define TM_LMT_UW 0x11000C /* Timer : Limit high word */
916
917/* ***************************************************************************** */
918#define GP0_IO 0x110010 /* GPIO output enables data I/O */
919#define FLD_GP_OE 0x00FF0000 /* GPIO: GP_OE output enable */
920#define FLD_GP_IN 0x0000FF00 /* GPIO: GP_IN status */
921#define FLD_GP_OUT 0x000000FF /* GPIO: GP_OUT control */
922
923/* ***************************************************************************** */
924#define GPIO_ISM 0x110014 /* GPIO interrupt sensitivity mode */
925#define FLD_GP_ISM_SNS 0x00000070
926#define FLD_GP_ISM_POL 0x00000007
927
928/* ***************************************************************************** */
929#define SOFT_RESET 0x11001C /* Output system reset reg */
930#define FLD_PECOS_SOFT_RESET 0x00000001
931
932/* ***************************************************************************** */
933#define MC416_RWD 0x110020 /* MC416 GPIO[18:3] pin */
934#define MC416_OEN 0x110024 /* Output enable of GPIO[18:3] */
935#define MC416_CTL 0x110028
936
937/* ***************************************************************************** */
938#define ALT_PIN_OUT_SEL 0x11002C /* Alternate GPIO output select */
939
940#define FLD_ALT_GPIO_OUT_SEL 0xF0000000
941/* 0 Disabled <-- default */
942/* 1 GPIO[0] */
943/* 2 GPIO[10] */
944/* 3 VIP_656_DATA_VAL */
945/* 4 VIP_656_DATA[0] */
946/* 5 VIP_656_CLK */
947/* 6 VIP_656_DATA_EXT[1] */
948/* 7 VIP_656_DATA_EXT[0] */
949/* 8 ATT_IF */
950
951#define FLD_AUX_PLL_CLK_ALT_SEL 0x0F000000
952/* 0 AUX_PLL_CLK<-- default */
953/* 1 GPIO[2] */
954/* 2 GPIO[10] */
955/* 3 VIP_656_DATA_VAL */
956/* 4 VIP_656_DATA[0] */
957/* 5 VIP_656_CLK */
958/* 6 VIP_656_DATA_EXT[1] */
959/* 7 VIP_656_DATA_EXT[0] */
960
961#define FLD_IR_TX_ALT_SEL 0x00F00000
962/* 0 IR_TX <-- default */
963/* 1 GPIO[1] */
964/* 2 GPIO[10] */
965/* 3 VIP_656_DATA_VAL */
966/* 4 VIP_656_DATA[0] */
967/* 5 VIP_656_CLK */
968/* 6 VIP_656_DATA_EXT[1] */
969/* 7 VIP_656_DATA_EXT[0] */
970
971#define FLD_IR_RX_ALT_SEL 0x000F0000
972/* 0 IR_RX <-- default */
973/* 1 GPIO[0] */
974/* 2 GPIO[10] */
975/* 3 VIP_656_DATA_VAL */
976/* 4 VIP_656_DATA[0] */
977/* 5 VIP_656_CLK */
978/* 6 VIP_656_DATA_EXT[1] */
979/* 7 VIP_656_DATA_EXT[0] */
980
981#define FLD_GPIO10_ALT_SEL 0x0000F000
982/* 0 GPIO[10] <-- default */
983/* 1 GPIO[0] */
984/* 2 GPIO[10] */
985/* 3 VIP_656_DATA_VAL */
986/* 4 VIP_656_DATA[0] */
987/* 5 VIP_656_CLK */
988/* 6 VIP_656_DATA_EXT[1] */
989/* 7 VIP_656_DATA_EXT[0] */
990
991#define FLD_GPIO2_ALT_SEL 0x00000F00
992/* 0 GPIO[2] <-- default */
993/* 1 GPIO[1] */
994/* 2 GPIO[10] */
995/* 3 VIP_656_DATA_VAL */
996/* 4 VIP_656_DATA[0] */
997/* 5 VIP_656_CLK */
998/* 6 VIP_656_DATA_EXT[1] */
999/* 7 VIP_656_DATA_EXT[0] */
1000
1001#define FLD_GPIO1_ALT_SEL 0x000000F0
1002/* 0 GPIO[1] <-- default */
1003/* 1 GPIO[0] */
1004/* 2 GPIO[10] */
1005/* 3 VIP_656_DATA_VAL */
1006/* 4 VIP_656_DATA[0] */
1007/* 5 VIP_656_CLK */
1008/* 6 VIP_656_DATA_EXT[1] */
1009/* 7 VIP_656_DATA_EXT[0] */
1010
1011#define FLD_GPIO0_ALT_SEL 0x0000000F
1012/* 0 GPIO[0] <-- default */
1013/* 1 GPIO[1] */
1014/* 2 GPIO[10] */
1015/* 3 VIP_656_DATA_VAL */
1016/* 4 VIP_656_DATA[0] */
1017/* 5 VIP_656_CLK */
1018/* 6 VIP_656_DATA_EXT[1] */
1019/* 7 VIP_656_DATA_EXT[0] */
1020
1021#define ALT_PIN_IN_SEL 0x110030 /* Alternate GPIO input select */
1022
1023#define FLD_GPIO10_ALT_IN_SEL 0x0000F000
1024/* 0 GPIO[10] <-- default */
1025/* 1 IR_RX */
1026/* 2 IR_TX */
1027/* 3 AUX_PLL_CLK */
1028/* 4 IF_ATT_SEL */
1029/* 5 GPIO[0] */
1030/* 6 GPIO[1] */
1031/* 7 GPIO[2] */
1032
1033#define FLD_GPIO2_ALT_IN_SEL 0x00000F00
1034/* 0 GPIO[2] <-- default */
1035/* 1 IR_RX */
1036/* 2 IR_TX */
1037/* 3 AUX_PLL_CLK */
1038/* 4 IF_ATT_SEL */
1039
1040#define FLD_GPIO1_ALT_IN_SEL 0x000000F0
1041/* 0 GPIO[1] <-- default */
1042/* 1 IR_RX */
1043/* 2 IR_TX */
1044/* 3 AUX_PLL_CLK */
1045/* 4 IF_ATT_SEL */
1046
1047#define FLD_GPIO0_ALT_IN_SEL 0x0000000F
1048/* 0 GPIO[0] <-- default */
1049/* 1 IR_RX */
1050/* 2 IR_TX */
1051/* 3 AUX_PLL_CLK */
1052/* 4 IF_ATT_SEL */
1053
1054/* ***************************************************************************** */
1055#define TEST_BUS_CTL1 0x110040 /* Test bus control register #1 */
1056
1057/* ***************************************************************************** */
1058#define TEST_BUS_CTL2 0x110044 /* Test bus control register #2 */
1059
1060/* ***************************************************************************** */
1061#define CLK_DELAY 0x110048 /* Clock delay */
1062#define FLD_MOE_CLK_DIS 0x80000000 /* Disable MoE clock */
1063
1064/* ***************************************************************************** */
1065#define PAD_CTRL 0x110068 /* Pad drive strength control */
1066
1067/* ***************************************************************************** */
1068#define MBIST_CTRL 0x110050 /* SRAM memory built-in self test control */
1069
1070/* ***************************************************************************** */
1071#define MBIST_STAT 0x110054 /* SRAM memory built-in self test status */
1072
1073/* ***************************************************************************** */
1074/* PLL registers */
1075/* ***************************************************************************** */
1076#define PLL_A_INT_FRAC 0x110088
1077#define PLL_A_POST_STAT_BIST 0x11008C
1078#define PLL_B_INT_FRAC 0x110090
1079#define PLL_B_POST_STAT_BIST 0x110094
1080#define PLL_C_INT_FRAC 0x110098
1081#define PLL_C_POST_STAT_BIST 0x11009C
1082#define PLL_D_INT_FRAC 0x1100A0
1083#define PLL_D_POST_STAT_BIST 0x1100A4
1084
1085#define CLK_RST 0x11002C
1086#define FLD_VID_I_CLK_NOE 0x00001000
1087#define FLD_VID_J_CLK_NOE 0x00002000
1088#define FLD_USE_ALT_PLL_REF 0x00004000
1089
1090#define VID_CH_MODE_SEL 0x110078
1091#define VID_CH_CLK_SEL 0x11007C
1092
1093/* ***************************************************************************** */
1094#define VBI_A_DMA 0x130008 /* VBI A DMA data port */
1095
1096/* ***************************************************************************** */
1097#define VID_A_VIP_CTL 0x130080 /* Video A VIP format control */
1098#define FLD_VIP_MODE 0x00000001
1099
1100/* ***************************************************************************** */
1101#define VID_A_PIXEL_FRMT 0x130084 /* Video A pixel format */
1102#define FLD_VID_A_GAMMA_DIS 0x00000008
1103#define FLD_VID_A_FORMAT 0x00000007
1104#define FLD_VID_A_GAMMA_FACTOR 0x00000010
1105
1106/* ***************************************************************************** */
1107#define VID_A_VBI_CTL 0x130088 /* Video A VBI miscellaneous control */
1108#define FLD_VID_A_VIP_EXT 0x00000003
1109
1110/* ***************************************************************************** */
1111#define VID_B_DMA 0x130100 /* Video B DMA data port */
1112
1113/* ***************************************************************************** */
1114#define VBI_B_DMA 0x130108 /* VBI B DMA data port */
1115
1116/* ***************************************************************************** */
1117#define VID_B_SRC_SEL 0x130144 /* Video B source select */
1118#define FLD_VID_B_SRC_SEL 0x00000000
1119
1120/* ***************************************************************************** */
1121#define VID_B_LNGTH 0x130150 /* Video B line length */
1122#define FLD_VID_B_LN_LNGTH 0x00000FFF
1123
1124/* ***************************************************************************** */
1125#define VID_B_VIP_CTL 0x130180 /* Video B VIP format control */
1126
1127/* ***************************************************************************** */
1128#define VID_B_PIXEL_FRMT 0x130184 /* Video B pixel format */
1129#define FLD_VID_B_GAMMA_DIS 0x00000008
1130#define FLD_VID_B_FORMAT 0x00000007
1131#define FLD_VID_B_GAMMA_FACTOR 0x00000010
1132
1133/* ***************************************************************************** */
1134#define VID_C_DMA 0x130200 /* Video C DMA data port */
1135
1136/* ***************************************************************************** */
1137#define VID_C_LNGTH 0x130250 /* Video C line length */
1138#define FLD_VID_C_LN_LNGTH 0x00000FFF
1139
1140/* ***************************************************************************** */
1141/* Video Destination Channels */
1142/* ***************************************************************************** */
1143
1144#define VID_DST_A_GPCNT 0x130020 /* Video A general purpose counter */
1145#define VID_DST_B_GPCNT 0x130120 /* Video B general purpose counter */
1146#define VID_DST_C_GPCNT 0x130220 /* Video C general purpose counter */
1147#define VID_DST_D_GPCNT 0x130320 /* Video D general purpose counter */
1148#define VID_DST_E_GPCNT 0x130420 /* Video E general purpose counter */
1149#define VID_DST_F_GPCNT 0x130520 /* Video F general purpose counter */
1150#define VID_DST_G_GPCNT 0x130620 /* Video G general purpose counter */
1151#define VID_DST_H_GPCNT 0x130720 /* Video H general purpose counter */
1152
1153/* ***************************************************************************** */
1154
1155#define VID_DST_A_GPCNT_CTL 0x130030 /* Video A general purpose control */
1156#define VID_DST_B_GPCNT_CTL 0x130130 /* Video B general purpose control */
1157#define VID_DST_C_GPCNT_CTL 0x130230 /* Video C general purpose control */
1158#define VID_DST_D_GPCNT_CTL 0x130330 /* Video D general purpose control */
1159#define VID_DST_E_GPCNT_CTL 0x130430 /* Video E general purpose control */
1160#define VID_DST_F_GPCNT_CTL 0x130530 /* Video F general purpose control */
1161#define VID_DST_G_GPCNT_CTL 0x130630 /* Video G general purpose control */
1162#define VID_DST_H_GPCNT_CTL 0x130730 /* Video H general purpose control */
1163
1164/* ***************************************************************************** */
1165
1166#define VID_DST_A_DMA_CTL 0x130040 /* Video A DMA control */
1167#define VID_DST_B_DMA_CTL 0x130140 /* Video B DMA control */
1168#define VID_DST_C_DMA_CTL 0x130240 /* Video C DMA control */
1169#define VID_DST_D_DMA_CTL 0x130340 /* Video D DMA control */
1170#define VID_DST_E_DMA_CTL 0x130440 /* Video E DMA control */
1171#define VID_DST_F_DMA_CTL 0x130540 /* Video F DMA control */
1172#define VID_DST_G_DMA_CTL 0x130640 /* Video G DMA control */
1173#define VID_DST_H_DMA_CTL 0x130740 /* Video H DMA control */
1174
1175#define FLD_VID_RISC_EN 0x00000010
1176#define FLD_VID_FIFO_EN 0x00000001
1177
1178/* ***************************************************************************** */
1179
1180#define VID_DST_A_VIP_CTL 0x130080 /* Video A VIP control */
1181#define VID_DST_B_VIP_CTL 0x130180 /* Video B VIP control */
1182#define VID_DST_C_VIP_CTL 0x130280 /* Video C VIP control */
1183#define VID_DST_D_VIP_CTL 0x130380 /* Video D VIP control */
1184#define VID_DST_E_VIP_CTL 0x130480 /* Video E VIP control */
1185#define VID_DST_F_VIP_CTL 0x130580 /* Video F VIP control */
1186#define VID_DST_G_VIP_CTL 0x130680 /* Video G VIP control */
1187#define VID_DST_H_VIP_CTL 0x130780 /* Video H VIP control */
1188
1189/* ***************************************************************************** */
1190
1191#define VID_DST_A_PIX_FRMT 0x130084 /* Video A Pixel format */
1192#define VID_DST_B_PIX_FRMT 0x130184 /* Video B Pixel format */
1193#define VID_DST_C_PIX_FRMT 0x130284 /* Video C Pixel format */
1194#define VID_DST_D_PIX_FRMT 0x130384 /* Video D Pixel format */
1195#define VID_DST_E_PIX_FRMT 0x130484 /* Video E Pixel format */
1196#define VID_DST_F_PIX_FRMT 0x130584 /* Video F Pixel format */
1197#define VID_DST_G_PIX_FRMT 0x130684 /* Video G Pixel format */
1198#define VID_DST_H_PIX_FRMT 0x130784 /* Video H Pixel format */
1199
1200/* ***************************************************************************** */
1201/* Video Source Channels */
1202/* ***************************************************************************** */
1203
1204#define VID_SRC_A_GPCNT_CTL 0x130804 /* Video A general purpose control */
1205#define VID_SRC_B_GPCNT_CTL 0x130904 /* Video B general purpose control */
1206#define VID_SRC_C_GPCNT_CTL 0x130A04 /* Video C general purpose control */
1207#define VID_SRC_D_GPCNT_CTL 0x130B04 /* Video D general purpose control */
1208#define VID_SRC_E_GPCNT_CTL 0x130C04 /* Video E general purpose control */
1209#define VID_SRC_F_GPCNT_CTL 0x130D04 /* Video F general purpose control */
1210#define VID_SRC_I_GPCNT_CTL 0x130E04 /* Video I general purpose control */
1211#define VID_SRC_J_GPCNT_CTL 0x130F04 /* Video J general purpose control */
1212
1213/* ***************************************************************************** */
1214
1215#define VID_SRC_A_GPCNT 0x130808 /* Video A general purpose counter */
1216#define VID_SRC_B_GPCNT 0x130908 /* Video B general purpose counter */
1217#define VID_SRC_C_GPCNT 0x130A08 /* Video C general purpose counter */
1218#define VID_SRC_D_GPCNT 0x130B08 /* Video D general purpose counter */
1219#define VID_SRC_E_GPCNT 0x130C08 /* Video E general purpose counter */
1220#define VID_SRC_F_GPCNT 0x130D08 /* Video F general purpose counter */
1221#define VID_SRC_I_GPCNT 0x130E08 /* Video I general purpose counter */
1222#define VID_SRC_J_GPCNT 0x130F08 /* Video J general purpose counter */
1223
1224/* ***************************************************************************** */
1225
1226#define VID_SRC_A_DMA_CTL 0x13080C /* Video A DMA control */
1227#define VID_SRC_B_DMA_CTL 0x13090C /* Video B DMA control */
1228#define VID_SRC_C_DMA_CTL 0x130A0C /* Video C DMA control */
1229#define VID_SRC_D_DMA_CTL 0x130B0C /* Video D DMA control */
1230#define VID_SRC_E_DMA_CTL 0x130C0C /* Video E DMA control */
1231#define VID_SRC_F_DMA_CTL 0x130D0C /* Video F DMA control */
1232#define VID_SRC_I_DMA_CTL 0x130E0C /* Video I DMA control */
1233#define VID_SRC_J_DMA_CTL 0x130F0C /* Video J DMA control */
1234
1235#define FLD_APB_RISC_EN 0x00000010
1236#define FLD_APB_FIFO_EN 0x00000001
1237
1238/* ***************************************************************************** */
1239
1240#define VID_SRC_A_FMT_CTL 0x130810 /* Video A format control */
1241#define VID_SRC_B_FMT_CTL 0x130910 /* Video B format control */
1242#define VID_SRC_C_FMT_CTL 0x130A10 /* Video C format control */
1243#define VID_SRC_D_FMT_CTL 0x130B10 /* Video D format control */
1244#define VID_SRC_E_FMT_CTL 0x130C10 /* Video E format control */
1245#define VID_SRC_F_FMT_CTL 0x130D10 /* Video F format control */
1246#define VID_SRC_I_FMT_CTL 0x130E10 /* Video I format control */
1247#define VID_SRC_J_FMT_CTL 0x130F10 /* Video J format control */
1248
1249/* ***************************************************************************** */
1250
1251#define VID_SRC_A_ACTIVE_CTL1 0x130814 /* Video A active control 1 */
1252#define VID_SRC_B_ACTIVE_CTL1 0x130914 /* Video B active control 1 */
1253#define VID_SRC_C_ACTIVE_CTL1 0x130A14 /* Video C active control 1 */
1254#define VID_SRC_D_ACTIVE_CTL1 0x130B14 /* Video D active control 1 */
1255#define VID_SRC_E_ACTIVE_CTL1 0x130C14 /* Video E active control 1 */
1256#define VID_SRC_F_ACTIVE_CTL1 0x130D14 /* Video F active control 1 */
1257#define VID_SRC_I_ACTIVE_CTL1 0x130E14 /* Video I active control 1 */
1258#define VID_SRC_J_ACTIVE_CTL1 0x130F14 /* Video J active control 1 */
1259
1260/* ***************************************************************************** */
1261
1262#define VID_SRC_A_ACTIVE_CTL2 0x130818 /* Video A active control 2 */
1263#define VID_SRC_B_ACTIVE_CTL2 0x130918 /* Video B active control 2 */
1264#define VID_SRC_C_ACTIVE_CTL2 0x130A18 /* Video C active control 2 */
1265#define VID_SRC_D_ACTIVE_CTL2 0x130B18 /* Video D active control 2 */
1266#define VID_SRC_E_ACTIVE_CTL2 0x130C18 /* Video E active control 2 */
1267#define VID_SRC_F_ACTIVE_CTL2 0x130D18 /* Video F active control 2 */
1268#define VID_SRC_I_ACTIVE_CTL2 0x130E18 /* Video I active control 2 */
1269#define VID_SRC_J_ACTIVE_CTL2 0x130F18 /* Video J active control 2 */
1270
1271/* ***************************************************************************** */
1272
1273#define VID_SRC_A_CDT_SZ 0x13081C /* Video A CDT size */
1274#define VID_SRC_B_CDT_SZ 0x13091C /* Video B CDT size */
1275#define VID_SRC_C_CDT_SZ 0x130A1C /* Video C CDT size */
1276#define VID_SRC_D_CDT_SZ 0x130B1C /* Video D CDT size */
1277#define VID_SRC_E_CDT_SZ 0x130C1C /* Video E CDT size */
1278#define VID_SRC_F_CDT_SZ 0x130D1C /* Video F CDT size */
1279#define VID_SRC_I_CDT_SZ 0x130E1C /* Video I CDT size */
1280#define VID_SRC_J_CDT_SZ 0x130F1C /* Video J CDT size */
1281
1282/* ***************************************************************************** */
1283/* Audio I/F */
1284/* ***************************************************************************** */
1285#define AUD_DST_A_DMA 0x140000 /* Audio Int A DMA data port */
1286#define AUD_SRC_A_DMA 0x140008 /* Audio Int A DMA data port */
1287
1288#define AUD_A_GPCNT 0x140010 /* Audio Int A gp counter */
1289#define FLD_AUD_A_GP_CNT 0x0000FFFF
1290
1291#define AUD_A_GPCNT_CTL 0x140014 /* Audio Int A gp control */
1292
1293#define AUD_A_LNGTH 0x140018 /* Audio Int A line length */
1294
1295#define AUD_A_CFG 0x14001C /* Audio Int A configuration */
1296
1297/* ***************************************************************************** */
1298#define AUD_DST_B_DMA 0x140100 /* Audio Int B DMA data port */
1299#define AUD_SRC_B_DMA 0x140108 /* Audio Int B DMA data port */
1300
1301#define AUD_B_GPCNT 0x140110 /* Audio Int B gp counter */
1302#define FLD_AUD_B_GP_CNT 0x0000FFFF
1303
1304#define AUD_B_GPCNT_CTL 0x140114 /* Audio Int B gp control */
1305
1306#define AUD_B_LNGTH 0x140118 /* Audio Int B line length */
1307
1308#define AUD_B_CFG 0x14011C /* Audio Int B configuration */
1309
1310/* ***************************************************************************** */
1311#define AUD_DST_C_DMA 0x140200 /* Audio Int C DMA data port */
1312#define AUD_SRC_C_DMA 0x140208 /* Audio Int C DMA data port */
1313
1314#define AUD_C_GPCNT 0x140210 /* Audio Int C gp counter */
1315#define FLD_AUD_C_GP_CNT 0x0000FFFF
1316
1317#define AUD_C_GPCNT_CTL 0x140214 /* Audio Int C gp control */
1318
1319#define AUD_C_LNGTH 0x140218 /* Audio Int C line length */
1320
1321#define AUD_C_CFG 0x14021C /* Audio Int C configuration */
1322
1323/* ***************************************************************************** */
1324#define AUD_DST_D_DMA 0x140300 /* Audio Int D DMA data port */
1325#define AUD_SRC_D_DMA 0x140308 /* Audio Int D DMA data port */
1326
1327#define AUD_D_GPCNT 0x140310 /* Audio Int D gp counter */
1328#define FLD_AUD_D_GP_CNT 0x0000FFFF
1329
1330#define AUD_D_GPCNT_CTL 0x140314 /* Audio Int D gp control */
1331
1332#define AUD_D_LNGTH 0x140318 /* Audio Int D line length */
1333
1334#define AUD_D_CFG 0x14031C /* Audio Int D configuration */
1335
1336/* ***************************************************************************** */
1337#define AUD_SRC_E_DMA 0x140400 /* Audio Int E DMA data port */
1338
1339#define AUD_E_GPCNT 0x140410 /* Audio Int E gp counter */
1340#define FLD_AUD_E_GP_CNT 0x0000FFFF
1341
1342#define AUD_E_GPCNT_CTL 0x140414 /* Audio Int E gp control */
1343
1344#define AUD_E_CFG 0x14041C /* Audio Int E configuration */
1345
1346/* ***************************************************************************** */
1347
1348#define FLD_AUD_DST_LN_LNGTH 0x00000FFF
1349
1350#define FLD_AUD_DST_PK_MODE 0x00004000
1351
1352#define FLD_AUD_CLK_ENABLE 0x00000200
1353
1354#define FLD_AUD_MASTER_MODE 0x00000002
1355
1356#define FLD_AUD_SONY_MODE 0x00000001
1357
1358#define FLD_AUD_CLK_SELECT_PLL_D 0x00001800
1359
1360#define FLD_AUD_DST_ENABLE 0x00020000
1361
1362#define FLD_AUD_SRC_ENABLE 0x00010000
1363
1364/* ***************************************************************************** */
1365#define AUD_INT_DMA_CTL 0x140500 /* Audio Int DMA control */
1366
1367#define FLD_AUD_SRC_E_RISC_EN 0x00008000
1368#define FLD_AUD_SRC_C_RISC_EN 0x00004000
1369#define FLD_AUD_SRC_B_RISC_EN 0x00002000
1370#define FLD_AUD_SRC_A_RISC_EN 0x00001000
1371
1372#define FLD_AUD_DST_D_RISC_EN 0x00000800
1373#define FLD_AUD_DST_C_RISC_EN 0x00000400
1374#define FLD_AUD_DST_B_RISC_EN 0x00000200
1375#define FLD_AUD_DST_A_RISC_EN 0x00000100
1376
1377#define FLD_AUD_SRC_E_FIFO_EN 0x00000080
1378#define FLD_AUD_SRC_C_FIFO_EN 0x00000040
1379#define FLD_AUD_SRC_B_FIFO_EN 0x00000020
1380#define FLD_AUD_SRC_A_FIFO_EN 0x00000010
1381
1382#define FLD_AUD_DST_D_FIFO_EN 0x00000008
1383#define FLD_AUD_DST_C_FIFO_EN 0x00000004
1384#define FLD_AUD_DST_B_FIFO_EN 0x00000002
1385#define FLD_AUD_DST_A_FIFO_EN 0x00000001
1386
1387/* ***************************************************************************** */
1388/* */
1389/* Mobilygen Interface Registers */
1390/* */
1391/* ***************************************************************************** */
1392/* Mobilygen Interface A */
1393/* ***************************************************************************** */
1394#define MB_IF_A_DMA 0x150000 /* MBIF A DMA data port */
1395#define MB_IF_A_GPCN 0x150008 /* MBIF A GP counter */
1396#define MB_IF_A_GPCN_CTRL 0x15000C
1397#define MB_IF_A_DMA_CTRL 0x150010
1398#define MB_IF_A_LENGTH 0x150014
1399#define MB_IF_A_HDMA_XFER_SZ 0x150018
1400#define MB_IF_A_HCMD 0x15001C
1401#define MB_IF_A_HCONFIG 0x150020
1402#define MB_IF_A_DATA_STRUCT_0 0x150024
1403#define MB_IF_A_DATA_STRUCT_1 0x150028
1404#define MB_IF_A_DATA_STRUCT_2 0x15002C
1405#define MB_IF_A_DATA_STRUCT_3 0x150030
1406#define MB_IF_A_DATA_STRUCT_4 0x150034
1407#define MB_IF_A_DATA_STRUCT_5 0x150038
1408#define MB_IF_A_DATA_STRUCT_6 0x15003C
1409#define MB_IF_A_DATA_STRUCT_7 0x150040
1410#define MB_IF_A_DATA_STRUCT_8 0x150044
1411#define MB_IF_A_DATA_STRUCT_9 0x150048
1412#define MB_IF_A_DATA_STRUCT_A 0x15004C
1413#define MB_IF_A_DATA_STRUCT_B 0x150050
1414#define MB_IF_A_DATA_STRUCT_C 0x150054
1415#define MB_IF_A_DATA_STRUCT_D 0x150058
1416#define MB_IF_A_DATA_STRUCT_E 0x15005C
1417#define MB_IF_A_DATA_STRUCT_F 0x150060
1418/* ***************************************************************************** */
1419/* Mobilygen Interface B */
1420/* ***************************************************************************** */
1421#define MB_IF_B_DMA 0x160000 /* MBIF A DMA data port */
1422#define MB_IF_B_GPCN 0x160008 /* MBIF A GP counter */
1423#define MB_IF_B_GPCN_CTRL 0x16000C
1424#define MB_IF_B_DMA_CTRL 0x160010
1425#define MB_IF_B_LENGTH 0x160014
1426#define MB_IF_B_HDMA_XFER_SZ 0x160018
1427#define MB_IF_B_HCMD 0x16001C
1428#define MB_IF_B_HCONFIG 0x160020
1429#define MB_IF_B_DATA_STRUCT_0 0x160024
1430#define MB_IF_B_DATA_STRUCT_1 0x160028
1431#define MB_IF_B_DATA_STRUCT_2 0x16002C
1432#define MB_IF_B_DATA_STRUCT_3 0x160030
1433#define MB_IF_B_DATA_STRUCT_4 0x160034
1434#define MB_IF_B_DATA_STRUCT_5 0x160038
1435#define MB_IF_B_DATA_STRUCT_6 0x16003C
1436#define MB_IF_B_DATA_STRUCT_7 0x160040
1437#define MB_IF_B_DATA_STRUCT_8 0x160044
1438#define MB_IF_B_DATA_STRUCT_9 0x160048
1439#define MB_IF_B_DATA_STRUCT_A 0x16004C
1440#define MB_IF_B_DATA_STRUCT_B 0x160050
1441#define MB_IF_B_DATA_STRUCT_C 0x160054
1442#define MB_IF_B_DATA_STRUCT_D 0x160058
1443#define MB_IF_B_DATA_STRUCT_E 0x16005C
1444#define MB_IF_B_DATA_STRUCT_F 0x160060
1445
1446/* MB_DMA_CTRL */
1447#define FLD_MB_IF_RISC_EN 0x00000010
1448#define FLD_MB_IF_FIFO_EN 0x00000001
1449
1450/* MB_LENGTH */
1451#define FLD_MB_IF_LN_LNGTH 0x00000FFF
1452
1453/* MB_HCMD register */
1454#define FLD_MB_HCMD_H_GO 0x80000000
1455#define FLD_MB_HCMD_H_BUSY 0x40000000
1456#define FLD_MB_HCMD_H_DMA_HOLD 0x10000000
1457#define FLD_MB_HCMD_H_DMA_BUSY 0x08000000
1458#define FLD_MB_HCMD_H_DMA_TYPE 0x04000000
1459#define FLD_MB_HCMD_H_DMA_XACT 0x02000000
1460#define FLD_MB_HCMD_H_RW_N 0x01000000
1461#define FLD_MB_HCMD_H_ADDR 0x00FF0000
1462#define FLD_MB_HCMD_H_DATA 0x0000FFFF
1463
1464/* ***************************************************************************** */
1465/* I2C #1 */
1466/* ***************************************************************************** */
1467#define I2C1_ADDR 0x180000 /* I2C #1 address */
1468#define FLD_I2C_DADDR 0xfe000000 /* RW [31:25] I2C Device Address */
1469 /* RO [24] reserved */
1470/* ***************************************************************************** */
1471#define FLD_I2C_SADDR 0x00FFFFFF /* RW [23:0] I2C Sub-address */
1472
1473/* ***************************************************************************** */
1474#define I2C1_WDATA 0x180004 /* I2C #1 write data */
1475#define FLD_I2C_WDATA 0xFFFFFFFF /* RW [31:0] */
1476
1477/* ***************************************************************************** */
1478#define I2C1_CTRL 0x180008 /* I2C #1 control */
1479#define FLD_I2C_PERIOD 0xFF000000 /* RW [31:24] */
1480#define FLD_I2C_SCL_IN 0x00200000 /* RW [21] */
1481#define FLD_I2C_SDA_IN 0x00100000 /* RW [20] */
1482 /* RO [19:18] reserved */
1483#define FLD_I2C_SCL_OUT 0x00020000 /* RW [17] */
1484#define FLD_I2C_SDA_OUT 0x00010000 /* RW [16] */
1485 /* RO [15] reserved */
1486#define FLD_I2C_DATA_LEN 0x00007000 /* RW [14:12] */
1487#define FLD_I2C_SADDR_INC 0x00000800 /* RW [11] */
1488 /* RO [10:9] reserved */
1489#define FLD_I2C_SADDR_LEN 0x00000300 /* RW [9:8] */
1490 /* RO [7:6] reserved */
1491#define FLD_I2C_SOFT 0x00000020 /* RW [5] */
1492#define FLD_I2C_NOSTOP 0x00000010 /* RW [4] */
1493#define FLD_I2C_EXTEND 0x00000008 /* RW [3] */
1494#define FLD_I2C_SYNC 0x00000004 /* RW [2] */
1495#define FLD_I2C_READ_SA 0x00000002 /* RW [1] */
1496#define FLD_I2C_READ_WRN 0x00000001 /* RW [0] */
1497
1498/* ***************************************************************************** */
1499#define I2C1_RDATA 0x18000C /* I2C #1 read data */
1500#define FLD_I2C_RDATA 0xFFFFFFFF /* RO [31:0] */
1501
1502/* ***************************************************************************** */
1503#define I2C1_STAT 0x180010 /* I2C #1 status */
1504#define FLD_I2C_XFER_IN_PROG 0x00000002 /* RO [1] */
1505#define FLD_I2C_RACK 0x00000001 /* RO [0] */
1506
1507/* ***************************************************************************** */
1508/* I2C #2 */
1509/* ***************************************************************************** */
1510#define I2C2_ADDR 0x190000 /* I2C #2 address */
1511
1512/* ***************************************************************************** */
1513#define I2C2_WDATA 0x190004 /* I2C #2 write data */
1514
1515/* ***************************************************************************** */
1516#define I2C2_CTRL 0x190008 /* I2C #2 control */
1517
1518/* ***************************************************************************** */
1519#define I2C2_RDATA 0x19000C /* I2C #2 read data */
1520
1521/* ***************************************************************************** */
1522#define I2C2_STAT 0x190010 /* I2C #2 status */
1523
1524/* ***************************************************************************** */
1525/* I2C #3 */
1526/* ***************************************************************************** */
1527#define I2C3_ADDR 0x1A0000 /* I2C #3 address */
1528
1529/* ***************************************************************************** */
1530#define I2C3_WDATA 0x1A0004 /* I2C #3 write data */
1531
1532/* ***************************************************************************** */
1533#define I2C3_CTRL 0x1A0008 /* I2C #3 control */
1534
1535/* ***************************************************************************** */
1536#define I2C3_RDATA 0x1A000C /* I2C #3 read data */
1537
1538/* ***************************************************************************** */
1539#define I2C3_STAT 0x1A0010 /* I2C #3 status */
1540
1541/* ***************************************************************************** */
1542/* UART */
1543/* ***************************************************************************** */
1544#define UART_CTL 0x1B0000 /* UART Control Register */
1545#define FLD_LOOP_BACK_EN (1 << 7) /* RW field - default 0 */
1546#define FLD_RX_TRG_SZ (3 << 2) /* RW field - default 0 */
1547#define FLD_RX_EN (1 << 1) /* RW field - default 0 */
1548#define FLD_TX_EN (1 << 0) /* RW field - default 0 */
1549
1550/* ***************************************************************************** */
1551#define UART_BRD 0x1B0004 /* UART Baud Rate Divisor */
1552#define FLD_BRD 0x0000FFFF /* RW field - default 0x197 */
1553
1554/* ***************************************************************************** */
1555#define UART_DBUF 0x1B0008 /* UART Tx/Rx Data BuFFer */
1556#define FLD_DB 0xFFFFFFFF /* RW field - default 0 */
1557
1558/* ***************************************************************************** */
1559#define UART_ISR 0x1B000C /* UART Interrupt Status */
1560#define FLD_RXD_TIMEOUT_EN (1 << 7) /* RW field - default 0 */
1561#define FLD_FRM_ERR_EN (1 << 6) /* RW field - default 0 */
1562#define FLD_RXD_RDY_EN (1 << 5) /* RW field - default 0 */
1563#define FLD_TXD_EMPTY_EN (1 << 4) /* RW field - default 0 */
1564#define FLD_RXD_OVERFLOW (1 << 3) /* RW field - default 0 */
1565#define FLD_FRM_ERR (1 << 2) /* RW field - default 0 */
1566#define FLD_RXD_RDY (1 << 1) /* RW field - default 0 */
1567#define FLD_TXD_EMPTY (1 << 0) /* RW field - default 0 */
1568
1569/* ***************************************************************************** */
1570#define UART_CNT 0x1B0010 /* UART Tx/Rx FIFO Byte Count */
1571#define FLD_TXD_CNT (0x1F << 8) /* RW field - default 0 */
1572#define FLD_RXD_CNT (0x1F << 0) /* RW field - default 0 */
1573
1574/* ***************************************************************************** */
1575/* Motion Detection */
1576#define MD_CH0_GRID_BLOCK_YCNT 0x170014
1577#define MD_CH1_GRID_BLOCK_YCNT 0x170094
1578#define MD_CH2_GRID_BLOCK_YCNT 0x170114
1579#define MD_CH3_GRID_BLOCK_YCNT 0x170194
1580#define MD_CH4_GRID_BLOCK_YCNT 0x170214
1581#define MD_CH5_GRID_BLOCK_YCNT 0x170294
1582#define MD_CH6_GRID_BLOCK_YCNT 0x170314
1583#define MD_CH7_GRID_BLOCK_YCNT 0x170394
1584
1585#define PIXEL_FRMT_422 4
1586#define PIXEL_FRMT_411 5
1587#define PIXEL_FRMT_Y8 6
1588
1589#define PIXEL_ENGINE_VIP1 0
1590#define PIXEL_ENGINE_VIP2 1
1591
1592#endif /* Athena_REGISTERS */
diff --git a/drivers/media/pci/cx25821/cx25821-sram.h b/drivers/media/pci/cx25821/cx25821-sram.h
new file mode 100644
index 000000000000..5f05d153bc4d
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-sram.h
@@ -0,0 +1,261 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#ifndef __ATHENA_SRAM_H__
24#define __ATHENA_SRAM_H__
25
26/* #define RX_SRAM_START_SIZE = 0; // Start of reserved SRAM */
27#define VID_CMDS_SIZE 80 /* Video CMDS size in bytes */
28#define AUDIO_CMDS_SIZE 80 /* AUDIO CMDS size in bytes */
29#define MBIF_CMDS_SIZE 80 /* MBIF CMDS size in bytes */
30
31/* #define RX_SRAM_POOL_START_SIZE = 0; // Start of useable RX SRAM for buffers */
32#define VID_IQ_SIZE 64 /* VID instruction queue size in bytes */
33#define MBIF_IQ_SIZE 64
34#define AUDIO_IQ_SIZE 64 /* AUD instruction queue size in bytes */
35
36#define VID_CDT_SIZE 64 /* VID cluster descriptor table size in bytes */
37#define MBIF_CDT_SIZE 64 /* MBIF/HBI cluster descriptor table size in bytes */
38#define AUDIO_CDT_SIZE 48 /* AUD cluster descriptor table size in bytes */
39
40/* #define RX_SRAM_POOL_FREE_SIZE = 16; // Start of available RX SRAM */
41/* #define RX_SRAM_END_SIZE = 0; // End of RX SRAM */
42
43/* #define TX_SRAM_POOL_START_SIZE = 0; // Start of transmit pool SRAM */
44/* #define MSI_DATA_SIZE = 64; // Reserved (MSI Data, RISC working stora */
45
46#define VID_CLUSTER_SIZE 1440 /* VID cluster data line */
47#define AUDIO_CLUSTER_SIZE 128 /* AUDIO cluster data line */
48#define MBIF_CLUSTER_SIZE 1440 /* MBIF/HBI cluster data line */
49
50/* #define TX_SRAM_POOL_FREE_SIZE = 704; // Start of available TX SRAM */
51/* #define TX_SRAM_END_SIZE = 0; // End of TX SRAM */
52
53/* Receive SRAM */
54#define RX_SRAM_START 0x10000
55#define VID_A_DOWN_CMDS 0x10000
56#define VID_B_DOWN_CMDS 0x10050
57#define VID_C_DOWN_CMDS 0x100A0
58#define VID_D_DOWN_CMDS 0x100F0
59#define VID_E_DOWN_CMDS 0x10140
60#define VID_F_DOWN_CMDS 0x10190
61#define VID_G_DOWN_CMDS 0x101E0
62#define VID_H_DOWN_CMDS 0x10230
63#define VID_A_UP_CMDS 0x10280
64#define VID_B_UP_CMDS 0x102D0
65#define VID_C_UP_CMDS 0x10320
66#define VID_D_UP_CMDS 0x10370
67#define VID_E_UP_CMDS 0x103C0
68#define VID_F_UP_CMDS 0x10410
69#define VID_I_UP_CMDS 0x10460
70#define VID_J_UP_CMDS 0x104B0
71#define AUD_A_DOWN_CMDS 0x10500
72#define AUD_B_DOWN_CMDS 0x10550
73#define AUD_C_DOWN_CMDS 0x105A0
74#define AUD_D_DOWN_CMDS 0x105F0
75#define AUD_A_UP_CMDS 0x10640
76#define AUD_B_UP_CMDS 0x10690
77#define AUD_C_UP_CMDS 0x106E0
78#define AUD_E_UP_CMDS 0x10730
79#define MBIF_A_DOWN_CMDS 0x10780
80#define MBIF_B_DOWN_CMDS 0x107D0
81#define DMA_SCRATCH_PAD 0x10820 /* Scratch pad area from 0x10820 to 0x10B40 */
82
83/* #define RX_SRAM_POOL_START = 0x105B0; */
84
85#define VID_A_IQ 0x11000
86#define VID_B_IQ 0x11040
87#define VID_C_IQ 0x11080
88#define VID_D_IQ 0x110C0
89#define VID_E_IQ 0x11100
90#define VID_F_IQ 0x11140
91#define VID_G_IQ 0x11180
92#define VID_H_IQ 0x111C0
93#define VID_I_IQ 0x11200
94#define VID_J_IQ 0x11240
95#define AUD_A_IQ 0x11280
96#define AUD_B_IQ 0x112C0
97#define AUD_C_IQ 0x11300
98#define AUD_D_IQ 0x11340
99#define AUD_E_IQ 0x11380
100#define MBIF_A_IQ 0x11000
101#define MBIF_B_IQ 0x110C0
102
103#define VID_A_CDT 0x10C00
104#define VID_B_CDT 0x10C40
105#define VID_C_CDT 0x10C80
106#define VID_D_CDT 0x10CC0
107#define VID_E_CDT 0x10D00
108#define VID_F_CDT 0x10D40
109#define VID_G_CDT 0x10D80
110#define VID_H_CDT 0x10DC0
111#define VID_I_CDT 0x10E00
112#define VID_J_CDT 0x10E40
113#define AUD_A_CDT 0x10E80
114#define AUD_B_CDT 0x10EB0
115#define AUD_C_CDT 0x10EE0
116#define AUD_D_CDT 0x10F10
117#define AUD_E_CDT 0x10F40
118#define MBIF_A_CDT 0x10C00
119#define MBIF_B_CDT 0x10CC0
120
121/* Cluster Buffer for RX */
122#define VID_A_UP_CLUSTER_1 0x11400
123#define VID_A_UP_CLUSTER_2 0x119A0
124#define VID_A_UP_CLUSTER_3 0x11F40
125#define VID_A_UP_CLUSTER_4 0x124E0
126
127#define VID_B_UP_CLUSTER_1 0x12A80
128#define VID_B_UP_CLUSTER_2 0x13020
129#define VID_B_UP_CLUSTER_3 0x135C0
130#define VID_B_UP_CLUSTER_4 0x13B60
131
132#define VID_C_UP_CLUSTER_1 0x14100
133#define VID_C_UP_CLUSTER_2 0x146A0
134#define VID_C_UP_CLUSTER_3 0x14C40
135#define VID_C_UP_CLUSTER_4 0x151E0
136
137#define VID_D_UP_CLUSTER_1 0x15780
138#define VID_D_UP_CLUSTER_2 0x15D20
139#define VID_D_UP_CLUSTER_3 0x162C0
140#define VID_D_UP_CLUSTER_4 0x16860
141
142#define VID_E_UP_CLUSTER_1 0x16E00
143#define VID_E_UP_CLUSTER_2 0x173A0
144#define VID_E_UP_CLUSTER_3 0x17940
145#define VID_E_UP_CLUSTER_4 0x17EE0
146
147#define VID_F_UP_CLUSTER_1 0x18480
148#define VID_F_UP_CLUSTER_2 0x18A20
149#define VID_F_UP_CLUSTER_3 0x18FC0
150#define VID_F_UP_CLUSTER_4 0x19560
151
152#define VID_I_UP_CLUSTER_1 0x19B00
153#define VID_I_UP_CLUSTER_2 0x1A0A0
154#define VID_I_UP_CLUSTER_3 0x1A640
155#define VID_I_UP_CLUSTER_4 0x1ABE0
156
157#define VID_J_UP_CLUSTER_1 0x1B180
158#define VID_J_UP_CLUSTER_2 0x1B720
159#define VID_J_UP_CLUSTER_3 0x1BCC0
160#define VID_J_UP_CLUSTER_4 0x1C260
161
162#define AUD_A_UP_CLUSTER_1 0x1C800
163#define AUD_A_UP_CLUSTER_2 0x1C880
164#define AUD_A_UP_CLUSTER_3 0x1C900
165
166#define AUD_B_UP_CLUSTER_1 0x1C980
167#define AUD_B_UP_CLUSTER_2 0x1CA00
168#define AUD_B_UP_CLUSTER_3 0x1CA80
169
170#define AUD_C_UP_CLUSTER_1 0x1CB00
171#define AUD_C_UP_CLUSTER_2 0x1CB80
172#define AUD_C_UP_CLUSTER_3 0x1CC00
173
174#define AUD_E_UP_CLUSTER_1 0x1CC80
175#define AUD_E_UP_CLUSTER_2 0x1CD00
176#define AUD_E_UP_CLUSTER_3 0x1CD80
177
178#define RX_SRAM_POOL_FREE 0x1CE00
179#define RX_SRAM_END 0x1D000
180
181/* Free Receive SRAM 144 Bytes */
182
183/* Transmit SRAM */
184#define TX_SRAM_POOL_START 0x00000
185
186#define VID_A_DOWN_CLUSTER_1 0x00040
187#define VID_A_DOWN_CLUSTER_2 0x005E0
188#define VID_A_DOWN_CLUSTER_3 0x00B80
189#define VID_A_DOWN_CLUSTER_4 0x01120
190
191#define VID_B_DOWN_CLUSTER_1 0x016C0
192#define VID_B_DOWN_CLUSTER_2 0x01C60
193#define VID_B_DOWN_CLUSTER_3 0x02200
194#define VID_B_DOWN_CLUSTER_4 0x027A0
195
196#define VID_C_DOWN_CLUSTER_1 0x02D40
197#define VID_C_DOWN_CLUSTER_2 0x032E0
198#define VID_C_DOWN_CLUSTER_3 0x03880
199#define VID_C_DOWN_CLUSTER_4 0x03E20
200
201#define VID_D_DOWN_CLUSTER_1 0x043C0
202#define VID_D_DOWN_CLUSTER_2 0x04960
203#define VID_D_DOWN_CLUSTER_3 0x04F00
204#define VID_D_DOWN_CLUSTER_4 0x054A0
205
206#define VID_E_DOWN_CLUSTER_1 0x05a40
207#define VID_E_DOWN_CLUSTER_2 0x05FE0
208#define VID_E_DOWN_CLUSTER_3 0x06580
209#define VID_E_DOWN_CLUSTER_4 0x06B20
210
211#define VID_F_DOWN_CLUSTER_1 0x070C0
212#define VID_F_DOWN_CLUSTER_2 0x07660
213#define VID_F_DOWN_CLUSTER_3 0x07C00
214#define VID_F_DOWN_CLUSTER_4 0x081A0
215
216#define VID_G_DOWN_CLUSTER_1 0x08740
217#define VID_G_DOWN_CLUSTER_2 0x08CE0
218#define VID_G_DOWN_CLUSTER_3 0x09280
219#define VID_G_DOWN_CLUSTER_4 0x09820
220
221#define VID_H_DOWN_CLUSTER_1 0x09DC0
222#define VID_H_DOWN_CLUSTER_2 0x0A360
223#define VID_H_DOWN_CLUSTER_3 0x0A900
224#define VID_H_DOWN_CLUSTER_4 0x0AEA0
225
226#define AUD_A_DOWN_CLUSTER_1 0x0B500
227#define AUD_A_DOWN_CLUSTER_2 0x0B580
228#define AUD_A_DOWN_CLUSTER_3 0x0B600
229
230#define AUD_B_DOWN_CLUSTER_1 0x0B680
231#define AUD_B_DOWN_CLUSTER_2 0x0B700
232#define AUD_B_DOWN_CLUSTER_3 0x0B780
233
234#define AUD_C_DOWN_CLUSTER_1 0x0B800
235#define AUD_C_DOWN_CLUSTER_2 0x0B880
236#define AUD_C_DOWN_CLUSTER_3 0x0B900
237
238#define AUD_D_DOWN_CLUSTER_1 0x0B980
239#define AUD_D_DOWN_CLUSTER_2 0x0BA00
240#define AUD_D_DOWN_CLUSTER_3 0x0BA80
241
242#define TX_SRAM_POOL_FREE 0x0BB00
243#define TX_SRAM_END 0x0C000
244
245#define BYTES_TO_DWORDS(bcount) ((bcount) >> 2)
246#define BYTES_TO_QWORDS(bcount) ((bcount) >> 3)
247#define BYTES_TO_OWORDS(bcount) ((bcount) >> 4)
248
249#define VID_IQ_SIZE_DW BYTES_TO_DWORDS(VID_IQ_SIZE)
250#define VID_CDT_SIZE_QW BYTES_TO_QWORDS(VID_CDT_SIZE)
251#define VID_CLUSTER_SIZE_OW BYTES_TO_OWORDS(VID_CLUSTER_SIZE)
252
253#define AUDIO_IQ_SIZE_DW BYTES_TO_DWORDS(AUDIO_IQ_SIZE)
254#define AUDIO_CDT_SIZE_QW BYTES_TO_QWORDS(AUDIO_CDT_SIZE)
255#define AUDIO_CLUSTER_SIZE_QW BYTES_TO_QWORDS(AUDIO_CLUSTER_SIZE)
256
257#define MBIF_IQ_SIZE_DW BYTES_TO_DWORDS(MBIF_IQ_SIZE)
258#define MBIF_CDT_SIZE_QW BYTES_TO_QWORDS(MBIF_CDT_SIZE)
259#define MBIF_CLUSTER_SIZE_OW BYTES_TO_OWORDS(MBIF_CLUSTER_SIZE)
260
261#endif
diff --git a/drivers/media/pci/cx25821/cx25821-video-upstream-ch2.c b/drivers/media/pci/cx25821/cx25821-video-upstream-ch2.c
new file mode 100644
index 000000000000..c8c94fbf5d8d
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-video-upstream-ch2.c
@@ -0,0 +1,802 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include "cx25821-video.h"
26#include "cx25821-video-upstream-ch2.h"
27
28#include <linux/fs.h>
29#include <linux/errno.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/module.h>
33#include <linux/syscalls.h>
34#include <linux/file.h>
35#include <linux/fcntl.h>
36#include <linux/slab.h>
37#include <linux/uaccess.h>
38
39MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
40MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
41MODULE_LICENSE("GPL");
42
43static int _intr_msk = FLD_VID_SRC_RISC1 | FLD_VID_SRC_UF | FLD_VID_SRC_SYNC |
44 FLD_VID_SRC_OPC_ERR;
45
46static __le32 *cx25821_update_riscprogram_ch2(struct cx25821_dev *dev,
47 __le32 *rp, unsigned int offset,
48 unsigned int bpl, u32 sync_line,
49 unsigned int lines,
50 int fifo_enable, int field_type)
51{
52 unsigned int line, i;
53 int dist_betwn_starts = bpl * 2;
54
55 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
56
57 if (USE_RISC_NOOP_VIDEO) {
58 for (i = 0; i < NUM_NO_OPS; i++)
59 *(rp++) = cpu_to_le32(RISC_NOOP);
60 }
61
62 /* scan lines */
63 for (line = 0; line < lines; line++) {
64 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
65 *(rp++) = cpu_to_le32(dev->_data_buf_phys_addr_ch2 + offset);
66 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
67
68 if ((lines <= NTSC_FIELD_HEIGHT) ||
69 (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC_ch2)) {
70 offset += dist_betwn_starts;
71 }
72 }
73
74 return rp;
75}
76
77static __le32 *cx25821_risc_field_upstream_ch2(struct cx25821_dev *dev,
78 __le32 *rp,
79 dma_addr_t databuf_phys_addr,
80 unsigned int offset,
81 u32 sync_line, unsigned int bpl,
82 unsigned int lines,
83 int fifo_enable, int field_type)
84{
85 unsigned int line, i;
86 struct sram_channel *sram_ch =
87 dev->channels[dev->_channel2_upstream_select].sram_channels;
88 int dist_betwn_starts = bpl * 2;
89
90 /* sync instruction */
91 if (sync_line != NO_SYNC_LINE)
92 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
93
94 if (USE_RISC_NOOP_VIDEO) {
95 for (i = 0; i < NUM_NO_OPS; i++)
96 *(rp++) = cpu_to_le32(RISC_NOOP);
97 }
98
99 /* scan lines */
100 for (line = 0; line < lines; line++) {
101 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
102 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
103 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
104
105 if ((lines <= NTSC_FIELD_HEIGHT) ||
106 (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC_ch2)) {
107 offset += dist_betwn_starts;
108 }
109
110 /*
111 check if we need to enable the FIFO after the first 4 lines
112 For the upstream video channel, the risc engine will enable
113 the FIFO.
114 */
115 if (fifo_enable && line == 3) {
116 *(rp++) = RISC_WRITECR;
117 *(rp++) = sram_ch->dma_ctl;
118 *(rp++) = FLD_VID_FIFO_EN;
119 *(rp++) = 0x00000001;
120 }
121 }
122
123 return rp;
124}
125
126int cx25821_risc_buffer_upstream_ch2(struct cx25821_dev *dev,
127 struct pci_dev *pci,
128 unsigned int top_offset, unsigned int bpl,
129 unsigned int lines)
130{
131 __le32 *rp;
132 int fifo_enable = 0;
133 int singlefield_lines = lines >> 1; /*get line count for single field */
134 int odd_num_lines = singlefield_lines;
135 int frame = 0;
136 int frame_size = 0;
137 int databuf_offset = 0;
138 int risc_program_size = 0;
139 int risc_flag = RISC_CNT_RESET;
140 unsigned int bottom_offset = bpl;
141 dma_addr_t risc_phys_jump_addr;
142
143 if (dev->_isNTSC_ch2) {
144 odd_num_lines = singlefield_lines + 1;
145 risc_program_size = FRAME1_VID_PROG_SIZE;
146 if (bpl == Y411_LINE_SZ)
147 frame_size = FRAME_SIZE_NTSC_Y411;
148 else
149 frame_size = FRAME_SIZE_NTSC_Y422;
150 } else {
151 risc_program_size = PAL_VID_PROG_SIZE;
152 if (bpl == Y411_LINE_SZ)
153 frame_size = FRAME_SIZE_PAL_Y411;
154 else
155 frame_size = FRAME_SIZE_PAL_Y422;
156 }
157
158 /* Virtual address of Risc buffer program */
159 rp = dev->_dma_virt_addr_ch2;
160
161 for (frame = 0; frame < NUM_FRAMES; frame++) {
162 databuf_offset = frame_size * frame;
163
164 if (UNSET != top_offset) {
165 fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
166 rp = cx25821_risc_field_upstream_ch2(dev, rp,
167 dev->_data_buf_phys_addr_ch2 + databuf_offset,
168 top_offset, 0, bpl, odd_num_lines, fifo_enable,
169 ODD_FIELD);
170 }
171
172 fifo_enable = FIFO_DISABLE;
173
174 /* Even field */
175 rp = cx25821_risc_field_upstream_ch2(dev, rp,
176 dev->_data_buf_phys_addr_ch2 + databuf_offset,
177 bottom_offset, 0x200, bpl, singlefield_lines,
178 fifo_enable, EVEN_FIELD);
179
180 if (frame == 0) {
181 risc_flag = RISC_CNT_RESET;
182 risc_phys_jump_addr = dev->_dma_phys_start_addr_ch2 +
183 risc_program_size;
184 } else {
185 risc_flag = RISC_CNT_INC;
186 risc_phys_jump_addr = dev->_dma_phys_start_addr_ch2;
187 }
188
189 /*
190 * Loop to 2ndFrameRISC or to Start of
191 * Risc program & generate IRQ
192 */
193 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
194 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
195 *(rp++) = cpu_to_le32(0);
196 }
197
198 return 0;
199}
200
201void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev)
202{
203 struct sram_channel *sram_ch =
204 dev->channels[VID_UPSTREAM_SRAM_CHANNEL_J].sram_channels;
205 u32 tmp = 0;
206
207 if (!dev->_is_running_ch2) {
208 pr_info("No video file is currently running so return!\n");
209 return;
210 }
211 /* Disable RISC interrupts */
212 tmp = cx_read(sram_ch->int_msk);
213 cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
214
215 /* Turn OFF risc and fifo */
216 tmp = cx_read(sram_ch->dma_ctl);
217 cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
218
219 /* Clear data buffer memory */
220 if (dev->_data_buf_virt_addr_ch2)
221 memset(dev->_data_buf_virt_addr_ch2, 0,
222 dev->_data_buf_size_ch2);
223
224 dev->_is_running_ch2 = 0;
225 dev->_is_first_frame_ch2 = 0;
226 dev->_frame_count_ch2 = 0;
227 dev->_file_status_ch2 = END_OF_FILE;
228
229 kfree(dev->_irq_queues_ch2);
230 dev->_irq_queues_ch2 = NULL;
231
232 kfree(dev->_filename_ch2);
233
234 tmp = cx_read(VID_CH_MODE_SEL);
235 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
236}
237
238void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev)
239{
240 if (dev->_is_running_ch2)
241 cx25821_stop_upstream_video_ch2(dev);
242
243 if (dev->_dma_virt_addr_ch2) {
244 pci_free_consistent(dev->pci, dev->_risc_size_ch2,
245 dev->_dma_virt_addr_ch2,
246 dev->_dma_phys_addr_ch2);
247 dev->_dma_virt_addr_ch2 = NULL;
248 }
249
250 if (dev->_data_buf_virt_addr_ch2) {
251 pci_free_consistent(dev->pci, dev->_data_buf_size_ch2,
252 dev->_data_buf_virt_addr_ch2,
253 dev->_data_buf_phys_addr_ch2);
254 dev->_data_buf_virt_addr_ch2 = NULL;
255 }
256}
257
258int cx25821_get_frame_ch2(struct cx25821_dev *dev, struct sram_channel *sram_ch)
259{
260 struct file *myfile;
261 int frame_index_temp = dev->_frame_index_ch2;
262 int i = 0;
263 int line_size = (dev->_pixel_format_ch2 == PIXEL_FRMT_411) ?
264 Y411_LINE_SZ : Y422_LINE_SZ;
265 int frame_size = 0;
266 int frame_offset = 0;
267 ssize_t vfs_read_retval = 0;
268 char mybuf[line_size];
269 loff_t file_offset;
270 loff_t pos;
271 mm_segment_t old_fs;
272
273 if (dev->_file_status_ch2 == END_OF_FILE)
274 return 0;
275
276 if (dev->_isNTSC_ch2) {
277 frame_size = (line_size == Y411_LINE_SZ) ?
278 FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
279 } else {
280 frame_size = (line_size == Y411_LINE_SZ) ?
281 FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
282 }
283
284 frame_offset = (frame_index_temp > 0) ? frame_size : 0;
285 file_offset = dev->_frame_count_ch2 * frame_size;
286
287 myfile = filp_open(dev->_filename_ch2, O_RDONLY | O_LARGEFILE, 0);
288 if (IS_ERR(myfile)) {
289 const int open_errno = -PTR_ERR(myfile);
290 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
291 __func__, dev->_filename_ch2, open_errno);
292 return PTR_ERR(myfile);
293 } else {
294 if (!(myfile->f_op)) {
295 pr_err("%s(): File has no file operations registered!\n",
296 __func__);
297 filp_close(myfile, NULL);
298 return -EIO;
299 }
300
301 if (!myfile->f_op->read) {
302 pr_err("%s(): File has no READ operations registered!\n",
303 __func__);
304 filp_close(myfile, NULL);
305 return -EIO;
306 }
307
308 pos = myfile->f_pos;
309 old_fs = get_fs();
310 set_fs(KERNEL_DS);
311
312 for (i = 0; i < dev->_lines_count_ch2; i++) {
313 pos = file_offset;
314
315 vfs_read_retval = vfs_read(myfile, mybuf, line_size,
316 &pos);
317
318 if (vfs_read_retval > 0 && vfs_read_retval == line_size
319 && dev->_data_buf_virt_addr_ch2 != NULL) {
320 memcpy((void *)(dev->_data_buf_virt_addr_ch2 +
321 frame_offset / 4), mybuf,
322 vfs_read_retval);
323 }
324
325 file_offset += vfs_read_retval;
326 frame_offset += vfs_read_retval;
327
328 if (vfs_read_retval < line_size) {
329 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
330 __func__);
331 break;
332 }
333 }
334
335 if (i > 0)
336 dev->_frame_count_ch2++;
337
338 dev->_file_status_ch2 = (vfs_read_retval == line_size) ?
339 IN_PROGRESS : END_OF_FILE;
340
341 set_fs(old_fs);
342 filp_close(myfile, NULL);
343 }
344
345 return 0;
346}
347
348static void cx25821_vidups_handler_ch2(struct work_struct *work)
349{
350 struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
351 _irq_work_entry_ch2);
352
353 if (!dev) {
354 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
355 __func__);
356 return;
357 }
358
359 cx25821_get_frame_ch2(dev, dev->channels[dev->
360 _channel2_upstream_select].sram_channels);
361}
362
363int cx25821_openfile_ch2(struct cx25821_dev *dev, struct sram_channel *sram_ch)
364{
365 struct file *myfile;
366 int i = 0, j = 0;
367 int line_size = (dev->_pixel_format_ch2 == PIXEL_FRMT_411) ?
368 Y411_LINE_SZ : Y422_LINE_SZ;
369 ssize_t vfs_read_retval = 0;
370 char mybuf[line_size];
371 loff_t pos;
372 loff_t offset = (unsigned long)0;
373 mm_segment_t old_fs;
374
375 myfile = filp_open(dev->_filename_ch2, O_RDONLY | O_LARGEFILE, 0);
376
377 if (IS_ERR(myfile)) {
378 const int open_errno = -PTR_ERR(myfile);
379 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
380 __func__, dev->_filename_ch2, open_errno);
381 return PTR_ERR(myfile);
382 } else {
383 if (!(myfile->f_op)) {
384 pr_err("%s(): File has no file operations registered!\n",
385 __func__);
386 filp_close(myfile, NULL);
387 return -EIO;
388 }
389
390 if (!myfile->f_op->read) {
391 pr_err("%s(): File has no READ operations registered! Returning\n",
392 __func__);
393 filp_close(myfile, NULL);
394 return -EIO;
395 }
396
397 pos = myfile->f_pos;
398 old_fs = get_fs();
399 set_fs(KERNEL_DS);
400
401 for (j = 0; j < NUM_FRAMES; j++) {
402 for (i = 0; i < dev->_lines_count_ch2; i++) {
403 pos = offset;
404
405 vfs_read_retval = vfs_read(myfile, mybuf,
406 line_size, &pos);
407
408 if (vfs_read_retval > 0 &&
409 vfs_read_retval == line_size &&
410 dev->_data_buf_virt_addr_ch2 != NULL) {
411 memcpy((void *)(dev->
412 _data_buf_virt_addr_ch2
413 + offset / 4), mybuf,
414 vfs_read_retval);
415 }
416
417 offset += vfs_read_retval;
418
419 if (vfs_read_retval < line_size) {
420 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
421 __func__);
422 break;
423 }
424 }
425
426 if (i > 0)
427 dev->_frame_count_ch2++;
428
429 if (vfs_read_retval < line_size)
430 break;
431 }
432
433 dev->_file_status_ch2 = (vfs_read_retval == line_size) ?
434 IN_PROGRESS : END_OF_FILE;
435
436 set_fs(old_fs);
437 myfile->f_pos = 0;
438 filp_close(myfile, NULL);
439 }
440
441 return 0;
442}
443
444static int cx25821_upstream_buffer_prepare_ch2(struct cx25821_dev *dev,
445 struct sram_channel *sram_ch,
446 int bpl)
447{
448 int ret = 0;
449 dma_addr_t dma_addr;
450 dma_addr_t data_dma_addr;
451
452 if (dev->_dma_virt_addr_ch2 != NULL) {
453 pci_free_consistent(dev->pci, dev->upstream_riscbuf_size_ch2,
454 dev->_dma_virt_addr_ch2,
455 dev->_dma_phys_addr_ch2);
456 }
457
458 dev->_dma_virt_addr_ch2 = pci_alloc_consistent(dev->pci,
459 dev->upstream_riscbuf_size_ch2, &dma_addr);
460 dev->_dma_virt_start_addr_ch2 = dev->_dma_virt_addr_ch2;
461 dev->_dma_phys_start_addr_ch2 = dma_addr;
462 dev->_dma_phys_addr_ch2 = dma_addr;
463 dev->_risc_size_ch2 = dev->upstream_riscbuf_size_ch2;
464
465 if (!dev->_dma_virt_addr_ch2) {
466 pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
467 return -ENOMEM;
468 }
469
470 /* Iniitize at this address until n bytes to 0 */
471 memset(dev->_dma_virt_addr_ch2, 0, dev->_risc_size_ch2);
472
473 if (dev->_data_buf_virt_addr_ch2 != NULL) {
474 pci_free_consistent(dev->pci, dev->upstream_databuf_size_ch2,
475 dev->_data_buf_virt_addr_ch2,
476 dev->_data_buf_phys_addr_ch2);
477 }
478 /* For Video Data buffer allocation */
479 dev->_data_buf_virt_addr_ch2 = pci_alloc_consistent(dev->pci,
480 dev->upstream_databuf_size_ch2, &data_dma_addr);
481 dev->_data_buf_phys_addr_ch2 = data_dma_addr;
482 dev->_data_buf_size_ch2 = dev->upstream_databuf_size_ch2;
483
484 if (!dev->_data_buf_virt_addr_ch2) {
485 pr_err("FAILED to allocate memory for data buffer! Returning\n");
486 return -ENOMEM;
487 }
488
489 /* Initialize at this address until n bytes to 0 */
490 memset(dev->_data_buf_virt_addr_ch2, 0, dev->_data_buf_size_ch2);
491
492 ret = cx25821_openfile_ch2(dev, sram_ch);
493 if (ret < 0)
494 return ret;
495
496 /* Creating RISC programs */
497 ret = cx25821_risc_buffer_upstream_ch2(dev, dev->pci, 0, bpl,
498 dev->_lines_count_ch2);
499 if (ret < 0) {
500 pr_info("Failed creating Video Upstream Risc programs!\n");
501 goto error;
502 }
503
504 return 0;
505
506error:
507 return ret;
508}
509
510int cx25821_video_upstream_irq_ch2(struct cx25821_dev *dev, int chan_num,
511 u32 status)
512{
513 u32 int_msk_tmp;
514 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
515 int singlefield_lines = NTSC_FIELD_HEIGHT;
516 int line_size_in_bytes = Y422_LINE_SZ;
517 int odd_risc_prog_size = 0;
518 dma_addr_t risc_phys_jump_addr;
519 __le32 *rp;
520
521 if (status & FLD_VID_SRC_RISC1) {
522 /* We should only process one program per call */
523 u32 prog_cnt = cx_read(channel->gpcnt);
524
525 /*
526 * Since we've identified our IRQ, clear our bits from the
527 * interrupt mask and interrupt status registers
528 */
529 int_msk_tmp = cx_read(channel->int_msk);
530 cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
531 cx_write(channel->int_stat, _intr_msk);
532
533 spin_lock(&dev->slock);
534
535 dev->_frame_index_ch2 = prog_cnt;
536
537 queue_work(dev->_irq_queues_ch2, &dev->_irq_work_entry_ch2);
538
539 if (dev->_is_first_frame_ch2) {
540 dev->_is_first_frame_ch2 = 0;
541
542 if (dev->_isNTSC_ch2) {
543 singlefield_lines += 1;
544 odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
545 } else {
546 singlefield_lines = PAL_FIELD_HEIGHT;
547 odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
548 }
549
550 if (dev->_dma_virt_start_addr_ch2 != NULL) {
551 if (dev->_pixel_format_ch2 == PIXEL_FRMT_411)
552 line_size_in_bytes = Y411_LINE_SZ;
553 else
554 line_size_in_bytes = Y422_LINE_SZ;
555 risc_phys_jump_addr =
556 dev->_dma_phys_start_addr_ch2 +
557 odd_risc_prog_size;
558
559 rp = cx25821_update_riscprogram_ch2(dev,
560 dev->_dma_virt_start_addr_ch2,
561 TOP_OFFSET, line_size_in_bytes,
562 0x0, singlefield_lines,
563 FIFO_DISABLE, ODD_FIELD);
564
565 /* Jump to Even Risc program of 1st Frame */
566 *(rp++) = cpu_to_le32(RISC_JUMP);
567 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
568 *(rp++) = cpu_to_le32(0);
569 }
570 }
571
572 spin_unlock(&dev->slock);
573 }
574
575 if (dev->_file_status_ch2 == END_OF_FILE) {
576 pr_info("EOF Channel 2 Framecount = %d\n",
577 dev->_frame_count_ch2);
578 return -1;
579 }
580 /* ElSE, set the interrupt mask register, re-enable irq. */
581 int_msk_tmp = cx_read(channel->int_msk);
582 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
583
584 return 0;
585}
586
587static irqreturn_t cx25821_upstream_irq_ch2(int irq, void *dev_id)
588{
589 struct cx25821_dev *dev = dev_id;
590 u32 vid_status;
591 int handled = 0;
592 int channel_num = 0;
593 struct sram_channel *sram_ch;
594
595 if (!dev)
596 return -1;
597
598 channel_num = VID_UPSTREAM_SRAM_CHANNEL_J;
599 sram_ch = dev->channels[channel_num].sram_channels;
600
601 vid_status = cx_read(sram_ch->int_stat);
602
603 /* Only deal with our interrupt */
604 if (vid_status)
605 handled = cx25821_video_upstream_irq_ch2(dev, channel_num,
606 vid_status);
607
608 if (handled < 0)
609 cx25821_stop_upstream_video_ch2(dev);
610 else
611 handled += handled;
612
613 return IRQ_RETVAL(handled);
614}
615
616static void cx25821_set_pixelengine_ch2(struct cx25821_dev *dev,
617 struct sram_channel *ch, int pix_format)
618{
619 int width = WIDTH_D1;
620 int height = dev->_lines_count_ch2;
621 int num_lines, odd_num_lines;
622 u32 value;
623 int vip_mode = PIXEL_ENGINE_VIP1;
624
625 value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
626 value &= 0xFFFFFFEF;
627 value |= dev->_isNTSC_ch2 ? 0 : 0x10;
628 cx_write(ch->vid_fmt_ctl, value);
629
630 /*
631 * set number of active pixels in each line. Default is 720
632 * pixels in both NTSC and PAL format
633 */
634 cx_write(ch->vid_active_ctl1, width);
635
636 num_lines = (height / 2) & 0x3FF;
637 odd_num_lines = num_lines;
638
639 if (dev->_isNTSC_ch2)
640 odd_num_lines += 1;
641
642 value = (num_lines << 16) | odd_num_lines;
643
644 /* set number of active lines in field 0 (top) and field 1 (bottom) */
645 cx_write(ch->vid_active_ctl2, value);
646
647 cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
648}
649
650int cx25821_start_video_dma_upstream_ch2(struct cx25821_dev *dev,
651 struct sram_channel *sram_ch)
652{
653 u32 tmp = 0;
654 int err = 0;
655
656 /*
657 * 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface
658 * for channel A-C
659 */
660 tmp = cx_read(VID_CH_MODE_SEL);
661 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
662
663 /*
664 * Set the physical start address of the RISC program in the initial
665 * program counter(IPC) member of the cmds.
666 */
667 cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr_ch2);
668 cx_write(sram_ch->cmds_start + 4, 0); /* Risc IPC High 64 bits 63-32 */
669
670 /* reset counter */
671 cx_write(sram_ch->gpcnt_ctl, 3);
672
673 /* Clear our bits from the interrupt status register. */
674 cx_write(sram_ch->int_stat, _intr_msk);
675
676 /* Set the interrupt mask register, enable irq. */
677 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
678 tmp = cx_read(sram_ch->int_msk);
679 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
680
681 err = request_irq(dev->pci->irq, cx25821_upstream_irq_ch2,
682 IRQF_SHARED, dev->name, dev);
683 if (err < 0) {
684 pr_err("%s: can't get upstream IRQ %d\n",
685 dev->name, dev->pci->irq);
686 goto fail_irq;
687 }
688 /* Start the DMA engine */
689 tmp = cx_read(sram_ch->dma_ctl);
690 cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
691
692 dev->_is_running_ch2 = 1;
693 dev->_is_first_frame_ch2 = 1;
694
695 return 0;
696
697fail_irq:
698 cx25821_dev_unregister(dev);
699 return err;
700}
701
702int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev, int channel_select,
703 int pixel_format)
704{
705 struct sram_channel *sram_ch;
706 u32 tmp;
707 int retval = 0;
708 int err = 0;
709 int data_frame_size = 0;
710 int risc_buffer_size = 0;
711 int str_length = 0;
712
713 if (dev->_is_running_ch2) {
714 pr_info("Video Channel is still running so return!\n");
715 return 0;
716 }
717
718 dev->_channel2_upstream_select = channel_select;
719 sram_ch = dev->channels[channel_select].sram_channels;
720
721 INIT_WORK(&dev->_irq_work_entry_ch2, cx25821_vidups_handler_ch2);
722 dev->_irq_queues_ch2 =
723 create_singlethread_workqueue("cx25821_workqueue2");
724
725 if (!dev->_irq_queues_ch2) {
726 pr_err("create_singlethread_workqueue() for Video FAILED!\n");
727 return -ENOMEM;
728 }
729 /*
730 * 656/VIP SRC Upstream Channel I & J and 7 -
731 * Host Bus Interface for channel A-C
732 */
733 tmp = cx_read(VID_CH_MODE_SEL);
734 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
735
736 dev->_is_running_ch2 = 0;
737 dev->_frame_count_ch2 = 0;
738 dev->_file_status_ch2 = RESET_STATUS;
739 dev->_lines_count_ch2 = dev->_isNTSC_ch2 ? 480 : 576;
740 dev->_pixel_format_ch2 = pixel_format;
741 dev->_line_size_ch2 = (dev->_pixel_format_ch2 == PIXEL_FRMT_422) ?
742 (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
743 data_frame_size = dev->_isNTSC_ch2 ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
744 risc_buffer_size = dev->_isNTSC_ch2 ?
745 NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
746
747 if (dev->input_filename_ch2) {
748 str_length = strlen(dev->input_filename_ch2);
749 dev->_filename_ch2 = kmemdup(dev->input_filename_ch2,
750 str_length + 1, GFP_KERNEL);
751
752 if (!dev->_filename_ch2)
753 goto error;
754 } else {
755 str_length = strlen(dev->_defaultname_ch2);
756 dev->_filename_ch2 = kmemdup(dev->_defaultname_ch2,
757 str_length + 1, GFP_KERNEL);
758
759 if (!dev->_filename_ch2)
760 goto error;
761 }
762
763 /* Default if filename is empty string */
764 if (strcmp(dev->input_filename_ch2, "") == 0) {
765 if (dev->_isNTSC_ch2) {
766 dev->_filename_ch2 = (dev->_pixel_format_ch2 ==
767 PIXEL_FRMT_411) ? "/root/vid411.yuv" :
768 "/root/vidtest.yuv";
769 } else {
770 dev->_filename_ch2 = (dev->_pixel_format_ch2 ==
771 PIXEL_FRMT_411) ? "/root/pal411.yuv" :
772 "/root/pal422.yuv";
773 }
774 }
775
776 retval = cx25821_sram_channel_setup_upstream(dev, sram_ch,
777 dev->_line_size_ch2, 0);
778
779 /* setup fifo + format */
780 cx25821_set_pixelengine_ch2(dev, sram_ch, dev->_pixel_format_ch2);
781
782 dev->upstream_riscbuf_size_ch2 = risc_buffer_size * 2;
783 dev->upstream_databuf_size_ch2 = data_frame_size * 2;
784
785 /* Allocating buffers and prepare RISC program */
786 retval = cx25821_upstream_buffer_prepare_ch2(dev, sram_ch,
787 dev->_line_size_ch2);
788 if (retval < 0) {
789 pr_err("%s: Failed to set up Video upstream buffers!\n",
790 dev->name);
791 goto error;
792 }
793
794 cx25821_start_video_dma_upstream_ch2(dev, sram_ch);
795
796 return 0;
797
798error:
799 cx25821_dev_unregister(dev);
800
801 return err;
802}
diff --git a/drivers/media/pci/cx25821/cx25821-video-upstream-ch2.h b/drivers/media/pci/cx25821/cx25821-video-upstream-ch2.h
new file mode 100644
index 000000000000..d42dab59b663
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-video-upstream-ch2.h
@@ -0,0 +1,138 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/mutex.h>
24#include <linux/workqueue.h>
25
26#define OPEN_FILE_1 0
27#define NUM_PROGS 8
28#define NUM_FRAMES 2
29#define ODD_FIELD 0
30#define EVEN_FIELD 1
31#define TOP_OFFSET 0
32#define FIFO_DISABLE 0
33#define FIFO_ENABLE 1
34#define TEST_FRAMES 5
35#define END_OF_FILE 0
36#define IN_PROGRESS 1
37#define RESET_STATUS -1
38#define NUM_NO_OPS 5
39
40/* PAL and NTSC line sizes and number of lines. */
41#define WIDTH_D1 720
42#define NTSC_LINES_PER_FRAME 480
43#define PAL_LINES_PER_FRAME 576
44#define PAL_LINE_SZ 1440
45#define Y422_LINE_SZ 1440
46#define Y411_LINE_SZ 1080
47#define NTSC_FIELD_HEIGHT 240
48#define NTSC_ODD_FLD_LINES 241
49#define PAL_FIELD_HEIGHT 288
50
51#define FRAME_SIZE_NTSC_Y422 (NTSC_LINES_PER_FRAME * Y422_LINE_SZ)
52#define FRAME_SIZE_NTSC_Y411 (NTSC_LINES_PER_FRAME * Y411_LINE_SZ)
53#define FRAME_SIZE_PAL_Y422 (PAL_LINES_PER_FRAME * Y422_LINE_SZ)
54#define FRAME_SIZE_PAL_Y411 (PAL_LINES_PER_FRAME * Y411_LINE_SZ)
55
56#define NTSC_DATA_BUF_SZ (Y422_LINE_SZ * NTSC_LINES_PER_FRAME)
57#define PAL_DATA_BUF_SZ (Y422_LINE_SZ * PAL_LINES_PER_FRAME)
58
59#define RISC_WRITECR_INSTRUCTION_SIZE 16
60#define RISC_SYNC_INSTRUCTION_SIZE 4
61#define JUMP_INSTRUCTION_SIZE 12
62#define MAXSIZE_NO_OPS 36
63#define DWORD_SIZE 4
64
65#define USE_RISC_NOOP_VIDEO 1
66
67#ifdef USE_RISC_NOOP_VIDEO
68#define PAL_US_VID_PROG_SIZE \
69 (PAL_FIELD_HEIGHT * 3 * DWORD_SIZE + \
70 RISC_WRITECR_INSTRUCTION_SIZE + RISC_SYNC_INSTRUCTION_SIZE + \
71 NUM_NO_OPS * DWORD_SIZE)
72
73#define PAL_RISC_BUF_SIZE (2 * PAL_US_VID_PROG_SIZE)
74
75#define PAL_VID_PROG_SIZE \
76 ((PAL_FIELD_HEIGHT * 2) * 3 * DWORD_SIZE + \
77 2 * RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
78 JUMP_INSTRUCTION_SIZE + 2 * NUM_NO_OPS * DWORD_SIZE)
79
80#define ODD_FLD_PAL_PROG_SIZE \
81 (PAL_FIELD_HEIGHT * 3 * DWORD_SIZE + \
82 RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
83 NUM_NO_OPS * DWORD_SIZE)
84
85#define NTSC_US_VID_PROG_SIZE \
86 ((NTSC_ODD_FLD_LINES + 1) * 3 * DWORD_SIZE + \
87 RISC_WRITECR_INSTRUCTION_SIZE + JUMP_INSTRUCTION_SIZE + \
88 NUM_NO_OPS * DWORD_SIZE)
89
90#define NTSC_RISC_BUF_SIZE \
91 (2 * (RISC_SYNC_INSTRUCTION_SIZE + NTSC_US_VID_PROG_SIZE))
92
93#define FRAME1_VID_PROG_SIZE \
94 ((NTSC_ODD_FLD_LINES + NTSC_FIELD_HEIGHT) * \
95 3 * DWORD_SIZE + 2 * RISC_SYNC_INSTRUCTION_SIZE + \
96 RISC_WRITECR_INSTRUCTION_SIZE + JUMP_INSTRUCTION_SIZE + \
97 2 * NUM_NO_OPS * DWORD_SIZE)
98
99#define ODD_FLD_NTSC_PROG_SIZE \
100 (NTSC_ODD_FLD_LINES * 3 * DWORD_SIZE + \
101 RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
102 NUM_NO_OPS * DWORD_SIZE)
103#endif
104
105#ifndef USE_RISC_NOOP_VIDEO
106#define PAL_US_VID_PROG_SIZE \
107 ((PAL_FIELD_HEIGHT + 1) * 3 * DWORD_SIZE + \
108 RISC_WRITECR_INSTRUCTION_SIZE)
109
110#define PAL_RISC_BUF_SIZE \
111 (2 * (RISC_SYNC_INSTRUCTION_SIZE + PAL_US_VID_PROG_SIZE))
112
113#define PAL_VID_PROG_SIZE \
114 ((PAL_FIELD_HEIGHT * 2) * 3 * DWORD_SIZE + \
115 2 * RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
116 JUMP_INSTRUCTION_SIZE)
117
118#define ODD_FLD_PAL_PROG_SIZE \
119 (PAL_FIELD_HEIGHT * 3 * DWORD_SIZE + \
120 RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE)
121
122#define ODD_FLD_NTSC_PROG_SIZE \
123 (NTSC_ODD_FLD_LINES * 3 * DWORD_SIZE + \
124 RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE)
125
126#define NTSC_US_VID_PROG_SIZE \
127 ((NTSC_ODD_FLD_LINES + 1) * 3 * DWORD_SIZE + \
128 RISC_WRITECR_INSTRUCTION_SIZE + JUMP_INSTRUCTION_SIZE)
129
130#define NTSC_RISC_BUF_SIZE \
131 (2 * (RISC_SYNC_INSTRUCTION_SIZE + NTSC_US_VID_PROG_SIZE))
132
133#define FRAME1_VID_PROG_SIZE \
134 ((NTSC_ODD_FLD_LINES + NTSC_FIELD_HEIGHT) * \
135 3 * DWORD_SIZE + 2 * RISC_SYNC_INSTRUCTION_SIZE + \
136 RISC_WRITECR_INSTRUCTION_SIZE + JUMP_INSTRUCTION_SIZE)
137
138#endif
diff --git a/drivers/media/pci/cx25821/cx25821-video-upstream.c b/drivers/media/pci/cx25821/cx25821-video-upstream.c
new file mode 100644
index 000000000000..52c13e0b6492
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-video-upstream.c
@@ -0,0 +1,856 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include "cx25821-video.h"
26#include "cx25821-video-upstream.h"
27
28#include <linux/fs.h>
29#include <linux/errno.h>
30#include <linux/kernel.h>
31#include <linux/init.h>
32#include <linux/module.h>
33#include <linux/syscalls.h>
34#include <linux/file.h>
35#include <linux/fcntl.h>
36#include <linux/slab.h>
37#include <linux/uaccess.h>
38
39MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
40MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
41MODULE_LICENSE("GPL");
42
43static int _intr_msk = FLD_VID_SRC_RISC1 | FLD_VID_SRC_UF | FLD_VID_SRC_SYNC |
44 FLD_VID_SRC_OPC_ERR;
45
46int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
47 struct sram_channel *ch,
48 unsigned int bpl, u32 risc)
49{
50 unsigned int i, lines;
51 u32 cdt;
52
53 if (ch->cmds_start == 0) {
54 cx_write(ch->ptr1_reg, 0);
55 cx_write(ch->ptr2_reg, 0);
56 cx_write(ch->cnt2_reg, 0);
57 cx_write(ch->cnt1_reg, 0);
58 return 0;
59 }
60
61 bpl = (bpl + 7) & ~7; /* alignment */
62 cdt = ch->cdt;
63 lines = ch->fifo_size / bpl;
64
65 if (lines > 4)
66 lines = 4;
67
68 BUG_ON(lines < 2);
69
70 /* write CDT */
71 for (i = 0; i < lines; i++) {
72 cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
73 cx_write(cdt + 16 * i + 4, 0);
74 cx_write(cdt + 16 * i + 8, 0);
75 cx_write(cdt + 16 * i + 12, 0);
76 }
77
78 /* write CMDS */
79 cx_write(ch->cmds_start + 0, risc);
80
81 cx_write(ch->cmds_start + 4, 0);
82 cx_write(ch->cmds_start + 8, cdt);
83 cx_write(ch->cmds_start + 12, (lines * 16) >> 3);
84 cx_write(ch->cmds_start + 16, ch->ctrl_start);
85
86 cx_write(ch->cmds_start + 20, VID_IQ_SIZE_DW);
87
88 for (i = 24; i < 80; i += 4)
89 cx_write(ch->cmds_start + i, 0);
90
91 /* fill registers */
92 cx_write(ch->ptr1_reg, ch->fifo_start);
93 cx_write(ch->ptr2_reg, cdt);
94 cx_write(ch->cnt2_reg, (lines * 16) >> 3);
95 cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
96
97 return 0;
98}
99
100static __le32 *cx25821_update_riscprogram(struct cx25821_dev *dev,
101 __le32 *rp, unsigned int offset,
102 unsigned int bpl, u32 sync_line,
103 unsigned int lines, int fifo_enable,
104 int field_type)
105{
106 unsigned int line, i;
107 int dist_betwn_starts = bpl * 2;
108
109 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
110
111 if (USE_RISC_NOOP_VIDEO) {
112 for (i = 0; i < NUM_NO_OPS; i++)
113 *(rp++) = cpu_to_le32(RISC_NOOP);
114 }
115
116 /* scan lines */
117 for (line = 0; line < lines; line++) {
118 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
119 *(rp++) = cpu_to_le32(dev->_data_buf_phys_addr + offset);
120 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
121
122 if ((lines <= NTSC_FIELD_HEIGHT)
123 || (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC)) {
124 offset += dist_betwn_starts;
125 }
126 }
127
128 return rp;
129}
130
131static __le32 *cx25821_risc_field_upstream(struct cx25821_dev *dev, __le32 * rp,
132 dma_addr_t databuf_phys_addr,
133 unsigned int offset, u32 sync_line,
134 unsigned int bpl, unsigned int lines,
135 int fifo_enable, int field_type)
136{
137 unsigned int line, i;
138 struct sram_channel *sram_ch =
139 dev->channels[dev->_channel_upstream_select].sram_channels;
140 int dist_betwn_starts = bpl * 2;
141
142 /* sync instruction */
143 if (sync_line != NO_SYNC_LINE)
144 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
145
146 if (USE_RISC_NOOP_VIDEO) {
147 for (i = 0; i < NUM_NO_OPS; i++)
148 *(rp++) = cpu_to_le32(RISC_NOOP);
149 }
150
151 /* scan lines */
152 for (line = 0; line < lines; line++) {
153 *(rp++) = cpu_to_le32(RISC_READ | RISC_SOL | RISC_EOL | bpl);
154 *(rp++) = cpu_to_le32(databuf_phys_addr + offset);
155 *(rp++) = cpu_to_le32(0); /* bits 63-32 */
156
157 if ((lines <= NTSC_FIELD_HEIGHT)
158 || (line < (NTSC_FIELD_HEIGHT - 1)) || !(dev->_isNTSC))
159 /* to skip the other field line */
160 offset += dist_betwn_starts;
161
162 /* check if we need to enable the FIFO after the first 4 lines
163 * For the upstream video channel, the risc engine will enable
164 * the FIFO. */
165 if (fifo_enable && line == 3) {
166 *(rp++) = RISC_WRITECR;
167 *(rp++) = sram_ch->dma_ctl;
168 *(rp++) = FLD_VID_FIFO_EN;
169 *(rp++) = 0x00000001;
170 }
171 }
172
173 return rp;
174}
175
176int cx25821_risc_buffer_upstream(struct cx25821_dev *dev,
177 struct pci_dev *pci,
178 unsigned int top_offset,
179 unsigned int bpl, unsigned int lines)
180{
181 __le32 *rp;
182 int fifo_enable = 0;
183 /* get line count for single field */
184 int singlefield_lines = lines >> 1;
185 int odd_num_lines = singlefield_lines;
186 int frame = 0;
187 int frame_size = 0;
188 int databuf_offset = 0;
189 int risc_program_size = 0;
190 int risc_flag = RISC_CNT_RESET;
191 unsigned int bottom_offset = bpl;
192 dma_addr_t risc_phys_jump_addr;
193
194 if (dev->_isNTSC) {
195 odd_num_lines = singlefield_lines + 1;
196 risc_program_size = FRAME1_VID_PROG_SIZE;
197 frame_size = (bpl == Y411_LINE_SZ) ?
198 FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
199 } else {
200 risc_program_size = PAL_VID_PROG_SIZE;
201 frame_size = (bpl == Y411_LINE_SZ) ?
202 FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
203 }
204
205 /* Virtual address of Risc buffer program */
206 rp = dev->_dma_virt_addr;
207
208 for (frame = 0; frame < NUM_FRAMES; frame++) {
209 databuf_offset = frame_size * frame;
210
211 if (UNSET != top_offset) {
212 fifo_enable = (frame == 0) ? FIFO_ENABLE : FIFO_DISABLE;
213 rp = cx25821_risc_field_upstream(dev, rp,
214 dev->_data_buf_phys_addr +
215 databuf_offset, top_offset, 0, bpl,
216 odd_num_lines, fifo_enable, ODD_FIELD);
217 }
218
219 fifo_enable = FIFO_DISABLE;
220
221 /* Even Field */
222 rp = cx25821_risc_field_upstream(dev, rp,
223 dev->_data_buf_phys_addr +
224 databuf_offset, bottom_offset,
225 0x200, bpl, singlefield_lines,
226 fifo_enable, EVEN_FIELD);
227
228 if (frame == 0) {
229 risc_flag = RISC_CNT_RESET;
230 risc_phys_jump_addr = dev->_dma_phys_start_addr +
231 risc_program_size;
232 } else {
233 risc_phys_jump_addr = dev->_dma_phys_start_addr;
234 risc_flag = RISC_CNT_INC;
235 }
236
237 /* Loop to 2ndFrameRISC or to Start of Risc
238 * program & generate IRQ
239 */
240 *(rp++) = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | risc_flag);
241 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
242 *(rp++) = cpu_to_le32(0);
243 }
244
245 return 0;
246}
247
248void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev)
249{
250 struct sram_channel *sram_ch =
251 dev->channels[VID_UPSTREAM_SRAM_CHANNEL_I].sram_channels;
252 u32 tmp = 0;
253
254 if (!dev->_is_running) {
255 pr_info("No video file is currently running so return!\n");
256 return;
257 }
258 /* Disable RISC interrupts */
259 tmp = cx_read(sram_ch->int_msk);
260 cx_write(sram_ch->int_msk, tmp & ~_intr_msk);
261
262 /* Turn OFF risc and fifo enable */
263 tmp = cx_read(sram_ch->dma_ctl);
264 cx_write(sram_ch->dma_ctl, tmp & ~(FLD_VID_FIFO_EN | FLD_VID_RISC_EN));
265
266 /* Clear data buffer memory */
267 if (dev->_data_buf_virt_addr)
268 memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
269
270 dev->_is_running = 0;
271 dev->_is_first_frame = 0;
272 dev->_frame_count = 0;
273 dev->_file_status = END_OF_FILE;
274
275 kfree(dev->_irq_queues);
276 dev->_irq_queues = NULL;
277
278 kfree(dev->_filename);
279
280 tmp = cx_read(VID_CH_MODE_SEL);
281 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
282}
283
284void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev)
285{
286 if (dev->_is_running)
287 cx25821_stop_upstream_video_ch1(dev);
288
289 if (dev->_dma_virt_addr) {
290 pci_free_consistent(dev->pci, dev->_risc_size,
291 dev->_dma_virt_addr, dev->_dma_phys_addr);
292 dev->_dma_virt_addr = NULL;
293 }
294
295 if (dev->_data_buf_virt_addr) {
296 pci_free_consistent(dev->pci, dev->_data_buf_size,
297 dev->_data_buf_virt_addr,
298 dev->_data_buf_phys_addr);
299 dev->_data_buf_virt_addr = NULL;
300 }
301}
302
303int cx25821_get_frame(struct cx25821_dev *dev, struct sram_channel *sram_ch)
304{
305 struct file *myfile;
306 int frame_index_temp = dev->_frame_index;
307 int i = 0;
308 int line_size = (dev->_pixel_format == PIXEL_FRMT_411) ?
309 Y411_LINE_SZ : Y422_LINE_SZ;
310 int frame_size = 0;
311 int frame_offset = 0;
312 ssize_t vfs_read_retval = 0;
313 char mybuf[line_size];
314 loff_t file_offset;
315 loff_t pos;
316 mm_segment_t old_fs;
317
318 if (dev->_file_status == END_OF_FILE)
319 return 0;
320
321 if (dev->_isNTSC)
322 frame_size = (line_size == Y411_LINE_SZ) ?
323 FRAME_SIZE_NTSC_Y411 : FRAME_SIZE_NTSC_Y422;
324 else
325 frame_size = (line_size == Y411_LINE_SZ) ?
326 FRAME_SIZE_PAL_Y411 : FRAME_SIZE_PAL_Y422;
327
328 frame_offset = (frame_index_temp > 0) ? frame_size : 0;
329 file_offset = dev->_frame_count * frame_size;
330
331 myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
332
333 if (IS_ERR(myfile)) {
334 const int open_errno = -PTR_ERR(myfile);
335 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
336 __func__, dev->_filename, open_errno);
337 return PTR_ERR(myfile);
338 } else {
339 if (!(myfile->f_op)) {
340 pr_err("%s(): File has no file operations registered!\n",
341 __func__);
342 filp_close(myfile, NULL);
343 return -EIO;
344 }
345
346 if (!myfile->f_op->read) {
347 pr_err("%s(): File has no READ operations registered!\n",
348 __func__);
349 filp_close(myfile, NULL);
350 return -EIO;
351 }
352
353 pos = myfile->f_pos;
354 old_fs = get_fs();
355 set_fs(KERNEL_DS);
356
357 for (i = 0; i < dev->_lines_count; i++) {
358 pos = file_offset;
359
360 vfs_read_retval = vfs_read(myfile, mybuf, line_size,
361 &pos);
362
363 if (vfs_read_retval > 0 && vfs_read_retval == line_size
364 && dev->_data_buf_virt_addr != NULL) {
365 memcpy((void *)(dev->_data_buf_virt_addr +
366 frame_offset / 4), mybuf,
367 vfs_read_retval);
368 }
369
370 file_offset += vfs_read_retval;
371 frame_offset += vfs_read_retval;
372
373 if (vfs_read_retval < line_size) {
374 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
375 __func__);
376 break;
377 }
378 }
379
380 if (i > 0)
381 dev->_frame_count++;
382
383 dev->_file_status = (vfs_read_retval == line_size) ?
384 IN_PROGRESS : END_OF_FILE;
385
386 set_fs(old_fs);
387 filp_close(myfile, NULL);
388 }
389
390 return 0;
391}
392
393static void cx25821_vidups_handler(struct work_struct *work)
394{
395 struct cx25821_dev *dev = container_of(work, struct cx25821_dev,
396 _irq_work_entry);
397
398 if (!dev) {
399 pr_err("ERROR %s(): since container_of(work_struct) FAILED!\n",
400 __func__);
401 return;
402 }
403
404 cx25821_get_frame(dev, dev->channels[dev->_channel_upstream_select].
405 sram_channels);
406}
407
408int cx25821_openfile(struct cx25821_dev *dev, struct sram_channel *sram_ch)
409{
410 struct file *myfile;
411 int i = 0, j = 0;
412 int line_size = (dev->_pixel_format == PIXEL_FRMT_411) ?
413 Y411_LINE_SZ : Y422_LINE_SZ;
414 ssize_t vfs_read_retval = 0;
415 char mybuf[line_size];
416 loff_t pos;
417 loff_t offset = (unsigned long)0;
418 mm_segment_t old_fs;
419
420 myfile = filp_open(dev->_filename, O_RDONLY | O_LARGEFILE, 0);
421
422 if (IS_ERR(myfile)) {
423 const int open_errno = -PTR_ERR(myfile);
424 pr_err("%s(): ERROR opening file(%s) with errno = %d!\n",
425 __func__, dev->_filename, open_errno);
426 return PTR_ERR(myfile);
427 } else {
428 if (!(myfile->f_op)) {
429 pr_err("%s(): File has no file operations registered!\n",
430 __func__);
431 filp_close(myfile, NULL);
432 return -EIO;
433 }
434
435 if (!myfile->f_op->read) {
436 pr_err("%s(): File has no READ operations registered! Returning\n",
437 __func__);
438 filp_close(myfile, NULL);
439 return -EIO;
440 }
441
442 pos = myfile->f_pos;
443 old_fs = get_fs();
444 set_fs(KERNEL_DS);
445
446 for (j = 0; j < NUM_FRAMES; j++) {
447 for (i = 0; i < dev->_lines_count; i++) {
448 pos = offset;
449
450 vfs_read_retval = vfs_read(myfile, mybuf,
451 line_size, &pos);
452
453 if (vfs_read_retval > 0
454 && vfs_read_retval == line_size
455 && dev->_data_buf_virt_addr != NULL) {
456 memcpy((void *)(dev->
457 _data_buf_virt_addr +
458 offset / 4), mybuf,
459 vfs_read_retval);
460 }
461
462 offset += vfs_read_retval;
463
464 if (vfs_read_retval < line_size) {
465 pr_info("Done: exit %s() since no more bytes to read from Video file\n",
466 __func__);
467 break;
468 }
469 }
470
471 if (i > 0)
472 dev->_frame_count++;
473
474 if (vfs_read_retval < line_size)
475 break;
476 }
477
478 dev->_file_status = (vfs_read_retval == line_size) ?
479 IN_PROGRESS : END_OF_FILE;
480
481 set_fs(old_fs);
482 myfile->f_pos = 0;
483 filp_close(myfile, NULL);
484 }
485
486 return 0;
487}
488
489int cx25821_upstream_buffer_prepare(struct cx25821_dev *dev,
490 struct sram_channel *sram_ch, int bpl)
491{
492 int ret = 0;
493 dma_addr_t dma_addr;
494 dma_addr_t data_dma_addr;
495
496 if (dev->_dma_virt_addr != NULL)
497 pci_free_consistent(dev->pci, dev->upstream_riscbuf_size,
498 dev->_dma_virt_addr, dev->_dma_phys_addr);
499
500 dev->_dma_virt_addr = pci_alloc_consistent(dev->pci,
501 dev->upstream_riscbuf_size, &dma_addr);
502 dev->_dma_virt_start_addr = dev->_dma_virt_addr;
503 dev->_dma_phys_start_addr = dma_addr;
504 dev->_dma_phys_addr = dma_addr;
505 dev->_risc_size = dev->upstream_riscbuf_size;
506
507 if (!dev->_dma_virt_addr) {
508 pr_err("FAILED to allocate memory for Risc buffer! Returning\n");
509 return -ENOMEM;
510 }
511
512 /* Clear memory at address */
513 memset(dev->_dma_virt_addr, 0, dev->_risc_size);
514
515 if (dev->_data_buf_virt_addr != NULL)
516 pci_free_consistent(dev->pci, dev->upstream_databuf_size,
517 dev->_data_buf_virt_addr,
518 dev->_data_buf_phys_addr);
519 /* For Video Data buffer allocation */
520 dev->_data_buf_virt_addr = pci_alloc_consistent(dev->pci,
521 dev->upstream_databuf_size, &data_dma_addr);
522 dev->_data_buf_phys_addr = data_dma_addr;
523 dev->_data_buf_size = dev->upstream_databuf_size;
524
525 if (!dev->_data_buf_virt_addr) {
526 pr_err("FAILED to allocate memory for data buffer! Returning\n");
527 return -ENOMEM;
528 }
529
530 /* Clear memory at address */
531 memset(dev->_data_buf_virt_addr, 0, dev->_data_buf_size);
532
533 ret = cx25821_openfile(dev, sram_ch);
534 if (ret < 0)
535 return ret;
536
537 /* Create RISC programs */
538 ret = cx25821_risc_buffer_upstream(dev, dev->pci, 0, bpl,
539 dev->_lines_count);
540 if (ret < 0) {
541 pr_info("Failed creating Video Upstream Risc programs!\n");
542 goto error;
543 }
544
545 return 0;
546
547error:
548 return ret;
549}
550
551int cx25821_video_upstream_irq(struct cx25821_dev *dev, int chan_num,
552 u32 status)
553{
554 u32 int_msk_tmp;
555 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
556 int singlefield_lines = NTSC_FIELD_HEIGHT;
557 int line_size_in_bytes = Y422_LINE_SZ;
558 int odd_risc_prog_size = 0;
559 dma_addr_t risc_phys_jump_addr;
560 __le32 *rp;
561
562 if (status & FLD_VID_SRC_RISC1) {
563 /* We should only process one program per call */
564 u32 prog_cnt = cx_read(channel->gpcnt);
565
566 /* Since we've identified our IRQ, clear our bits from the
567 * interrupt mask and interrupt status registers */
568 int_msk_tmp = cx_read(channel->int_msk);
569 cx_write(channel->int_msk, int_msk_tmp & ~_intr_msk);
570 cx_write(channel->int_stat, _intr_msk);
571
572 spin_lock(&dev->slock);
573
574 dev->_frame_index = prog_cnt;
575
576 queue_work(dev->_irq_queues, &dev->_irq_work_entry);
577
578 if (dev->_is_first_frame) {
579 dev->_is_first_frame = 0;
580
581 if (dev->_isNTSC) {
582 singlefield_lines += 1;
583 odd_risc_prog_size = ODD_FLD_NTSC_PROG_SIZE;
584 } else {
585 singlefield_lines = PAL_FIELD_HEIGHT;
586 odd_risc_prog_size = ODD_FLD_PAL_PROG_SIZE;
587 }
588
589 if (dev->_dma_virt_start_addr != NULL) {
590 line_size_in_bytes =
591 (dev->_pixel_format ==
592 PIXEL_FRMT_411) ? Y411_LINE_SZ :
593 Y422_LINE_SZ;
594 risc_phys_jump_addr =
595 dev->_dma_phys_start_addr +
596 odd_risc_prog_size;
597
598 rp = cx25821_update_riscprogram(dev,
599 dev->_dma_virt_start_addr, TOP_OFFSET,
600 line_size_in_bytes, 0x0,
601 singlefield_lines, FIFO_DISABLE,
602 ODD_FIELD);
603
604 /* Jump to Even Risc program of 1st Frame */
605 *(rp++) = cpu_to_le32(RISC_JUMP);
606 *(rp++) = cpu_to_le32(risc_phys_jump_addr);
607 *(rp++) = cpu_to_le32(0);
608 }
609 }
610
611 spin_unlock(&dev->slock);
612 } else {
613 if (status & FLD_VID_SRC_UF)
614 pr_err("%s(): Video Received Underflow Error Interrupt!\n",
615 __func__);
616
617 if (status & FLD_VID_SRC_SYNC)
618 pr_err("%s(): Video Received Sync Error Interrupt!\n",
619 __func__);
620
621 if (status & FLD_VID_SRC_OPC_ERR)
622 pr_err("%s(): Video Received OpCode Error Interrupt!\n",
623 __func__);
624 }
625
626 if (dev->_file_status == END_OF_FILE) {
627 pr_err("EOF Channel 1 Framecount = %d\n", dev->_frame_count);
628 return -1;
629 }
630 /* ElSE, set the interrupt mask register, re-enable irq. */
631 int_msk_tmp = cx_read(channel->int_msk);
632 cx_write(channel->int_msk, int_msk_tmp |= _intr_msk);
633
634 return 0;
635}
636
637static irqreturn_t cx25821_upstream_irq(int irq, void *dev_id)
638{
639 struct cx25821_dev *dev = dev_id;
640 u32 vid_status;
641 int handled = 0;
642 int channel_num = 0;
643 struct sram_channel *sram_ch;
644
645 if (!dev)
646 return -1;
647
648 channel_num = VID_UPSTREAM_SRAM_CHANNEL_I;
649
650 sram_ch = dev->channels[channel_num].sram_channels;
651
652 vid_status = cx_read(sram_ch->int_stat);
653
654 /* Only deal with our interrupt */
655 if (vid_status)
656 handled = cx25821_video_upstream_irq(dev, channel_num,
657 vid_status);
658
659 if (handled < 0)
660 cx25821_stop_upstream_video_ch1(dev);
661 else
662 handled += handled;
663
664 return IRQ_RETVAL(handled);
665}
666
667void cx25821_set_pixelengine(struct cx25821_dev *dev, struct sram_channel *ch,
668 int pix_format)
669{
670 int width = WIDTH_D1;
671 int height = dev->_lines_count;
672 int num_lines, odd_num_lines;
673 u32 value;
674 int vip_mode = OUTPUT_FRMT_656;
675
676 value = ((pix_format & 0x3) << 12) | (vip_mode & 0x7);
677 value &= 0xFFFFFFEF;
678 value |= dev->_isNTSC ? 0 : 0x10;
679 cx_write(ch->vid_fmt_ctl, value);
680
681 /* set number of active pixels in each line.
682 * Default is 720 pixels in both NTSC and PAL format */
683 cx_write(ch->vid_active_ctl1, width);
684
685 num_lines = (height / 2) & 0x3FF;
686 odd_num_lines = num_lines;
687
688 if (dev->_isNTSC)
689 odd_num_lines += 1;
690
691 value = (num_lines << 16) | odd_num_lines;
692
693 /* set number of active lines in field 0 (top) and field 1 (bottom) */
694 cx_write(ch->vid_active_ctl2, value);
695
696 cx_write(ch->vid_cdt_size, VID_CDT_SIZE >> 3);
697}
698
699int cx25821_start_video_dma_upstream(struct cx25821_dev *dev,
700 struct sram_channel *sram_ch)
701{
702 u32 tmp = 0;
703 int err = 0;
704
705 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
706 * channel A-C
707 */
708 tmp = cx_read(VID_CH_MODE_SEL);
709 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
710
711 /* Set the physical start address of the RISC program in the initial
712 * program counter(IPC) member of the cmds.
713 */
714 cx_write(sram_ch->cmds_start + 0, dev->_dma_phys_addr);
715 /* Risc IPC High 64 bits 63-32 */
716 cx_write(sram_ch->cmds_start + 4, 0);
717
718 /* reset counter */
719 cx_write(sram_ch->gpcnt_ctl, 3);
720
721 /* Clear our bits from the interrupt status register. */
722 cx_write(sram_ch->int_stat, _intr_msk);
723
724 /* Set the interrupt mask register, enable irq. */
725 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << sram_ch->irq_bit));
726 tmp = cx_read(sram_ch->int_msk);
727 cx_write(sram_ch->int_msk, tmp |= _intr_msk);
728
729 err = request_irq(dev->pci->irq, cx25821_upstream_irq,
730 IRQF_SHARED, dev->name, dev);
731 if (err < 0) {
732 pr_err("%s: can't get upstream IRQ %d\n",
733 dev->name, dev->pci->irq);
734 goto fail_irq;
735 }
736
737 /* Start the DMA engine */
738 tmp = cx_read(sram_ch->dma_ctl);
739 cx_set(sram_ch->dma_ctl, tmp | FLD_VID_RISC_EN);
740
741 dev->_is_running = 1;
742 dev->_is_first_frame = 1;
743
744 return 0;
745
746fail_irq:
747 cx25821_dev_unregister(dev);
748 return err;
749}
750
751int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev, int channel_select,
752 int pixel_format)
753{
754 struct sram_channel *sram_ch;
755 u32 tmp;
756 int retval = 0;
757 int err = 0;
758 int data_frame_size = 0;
759 int risc_buffer_size = 0;
760 int str_length = 0;
761
762 if (dev->_is_running) {
763 pr_info("Video Channel is still running so return!\n");
764 return 0;
765 }
766
767 dev->_channel_upstream_select = channel_select;
768 sram_ch = dev->channels[channel_select].sram_channels;
769
770 INIT_WORK(&dev->_irq_work_entry, cx25821_vidups_handler);
771 dev->_irq_queues = create_singlethread_workqueue("cx25821_workqueue");
772
773 if (!dev->_irq_queues) {
774 pr_err("create_singlethread_workqueue() for Video FAILED!\n");
775 return -ENOMEM;
776 }
777 /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
778 * channel A-C
779 */
780 tmp = cx_read(VID_CH_MODE_SEL);
781 cx_write(VID_CH_MODE_SEL, tmp | 0x1B0001FF);
782
783 dev->_is_running = 0;
784 dev->_frame_count = 0;
785 dev->_file_status = RESET_STATUS;
786 dev->_lines_count = dev->_isNTSC ? 480 : 576;
787 dev->_pixel_format = pixel_format;
788 dev->_line_size = (dev->_pixel_format == PIXEL_FRMT_422) ?
789 (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
790 data_frame_size = dev->_isNTSC ? NTSC_DATA_BUF_SZ : PAL_DATA_BUF_SZ;
791 risc_buffer_size = dev->_isNTSC ?
792 NTSC_RISC_BUF_SIZE : PAL_RISC_BUF_SIZE;
793
794 if (dev->input_filename) {
795 str_length = strlen(dev->input_filename);
796 dev->_filename = kmemdup(dev->input_filename, str_length + 1,
797 GFP_KERNEL);
798
799 if (!dev->_filename)
800 goto error;
801 } else {
802 str_length = strlen(dev->_defaultname);
803 dev->_filename = kmemdup(dev->_defaultname, str_length + 1,
804 GFP_KERNEL);
805
806 if (!dev->_filename)
807 goto error;
808 }
809
810 /* Default if filename is empty string */
811 if (strcmp(dev->input_filename, "") == 0) {
812 if (dev->_isNTSC) {
813 dev->_filename =
814 (dev->_pixel_format == PIXEL_FRMT_411) ?
815 "/root/vid411.yuv" : "/root/vidtest.yuv";
816 } else {
817 dev->_filename =
818 (dev->_pixel_format == PIXEL_FRMT_411) ?
819 "/root/pal411.yuv" : "/root/pal422.yuv";
820 }
821 }
822
823 dev->_is_running = 0;
824 dev->_frame_count = 0;
825 dev->_file_status = RESET_STATUS;
826 dev->_lines_count = dev->_isNTSC ? 480 : 576;
827 dev->_pixel_format = pixel_format;
828 dev->_line_size = (dev->_pixel_format == PIXEL_FRMT_422) ?
829 (WIDTH_D1 * 2) : (WIDTH_D1 * 3) / 2;
830
831 retval = cx25821_sram_channel_setup_upstream(dev, sram_ch,
832 dev->_line_size, 0);
833
834 /* setup fifo + format */
835 cx25821_set_pixelengine(dev, sram_ch, dev->_pixel_format);
836
837 dev->upstream_riscbuf_size = risc_buffer_size * 2;
838 dev->upstream_databuf_size = data_frame_size * 2;
839
840 /* Allocating buffers and prepare RISC program */
841 retval = cx25821_upstream_buffer_prepare(dev, sram_ch, dev->_line_size);
842 if (retval < 0) {
843 pr_err("%s: Failed to set up Video upstream buffers!\n",
844 dev->name);
845 goto error;
846 }
847
848 cx25821_start_video_dma_upstream(dev, sram_ch);
849
850 return 0;
851
852error:
853 cx25821_dev_unregister(dev);
854
855 return err;
856}
diff --git a/drivers/media/pci/cx25821/cx25821-video-upstream.h b/drivers/media/pci/cx25821/cx25821-video-upstream.h
new file mode 100644
index 000000000000..268ec8aa6a61
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-video-upstream.h
@@ -0,0 +1,139 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <hiep.huynh@conexant.com>, <shu.lin@conexant.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/mutex.h>
24#include <linux/workqueue.h>
25
26#define OUTPUT_FRMT_656 0
27#define OPEN_FILE_1 0
28#define NUM_PROGS 8
29#define NUM_FRAMES 2
30#define ODD_FIELD 0
31#define EVEN_FIELD 1
32#define TOP_OFFSET 0
33#define FIFO_DISABLE 0
34#define FIFO_ENABLE 1
35#define TEST_FRAMES 5
36#define END_OF_FILE 0
37#define IN_PROGRESS 1
38#define RESET_STATUS -1
39#define NUM_NO_OPS 5
40
41/* PAL and NTSC line sizes and number of lines. */
42#define WIDTH_D1 720
43#define NTSC_LINES_PER_FRAME 480
44#define PAL_LINES_PER_FRAME 576
45#define PAL_LINE_SZ 1440
46#define Y422_LINE_SZ 1440
47#define Y411_LINE_SZ 1080
48#define NTSC_FIELD_HEIGHT 240
49#define NTSC_ODD_FLD_LINES 241
50#define PAL_FIELD_HEIGHT 288
51
52#define FRAME_SIZE_NTSC_Y422 (NTSC_LINES_PER_FRAME * Y422_LINE_SZ)
53#define FRAME_SIZE_NTSC_Y411 (NTSC_LINES_PER_FRAME * Y411_LINE_SZ)
54#define FRAME_SIZE_PAL_Y422 (PAL_LINES_PER_FRAME * Y422_LINE_SZ)
55#define FRAME_SIZE_PAL_Y411 (PAL_LINES_PER_FRAME * Y411_LINE_SZ)
56
57#define NTSC_DATA_BUF_SZ (Y422_LINE_SZ * NTSC_LINES_PER_FRAME)
58#define PAL_DATA_BUF_SZ (Y422_LINE_SZ * PAL_LINES_PER_FRAME)
59
60#define RISC_WRITECR_INSTRUCTION_SIZE 16
61#define RISC_SYNC_INSTRUCTION_SIZE 4
62#define JUMP_INSTRUCTION_SIZE 12
63#define MAXSIZE_NO_OPS 36
64#define DWORD_SIZE 4
65
66#define USE_RISC_NOOP_VIDEO 1
67
68#ifdef USE_RISC_NOOP_VIDEO
69#define PAL_US_VID_PROG_SIZE \
70 (PAL_FIELD_HEIGHT * 3 * DWORD_SIZE + \
71 RISC_WRITECR_INSTRUCTION_SIZE + RISC_SYNC_INSTRUCTION_SIZE + \
72 NUM_NO_OPS * DWORD_SIZE)
73
74#define PAL_RISC_BUF_SIZE (2 * PAL_US_VID_PROG_SIZE)
75
76#define PAL_VID_PROG_SIZE \
77 ((PAL_FIELD_HEIGHT * 2) * 3 * DWORD_SIZE + \
78 2 * RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
79 JUMP_INSTRUCTION_SIZE + 2 * NUM_NO_OPS * DWORD_SIZE)
80
81#define ODD_FLD_PAL_PROG_SIZE \
82 (PAL_FIELD_HEIGHT * 3 * DWORD_SIZE + \
83 RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
84 NUM_NO_OPS * DWORD_SIZE)
85
86#define ODD_FLD_NTSC_PROG_SIZE \
87 (NTSC_ODD_FLD_LINES * 3 * DWORD_SIZE + \
88 RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
89 NUM_NO_OPS * DWORD_SIZE)
90
91#define NTSC_US_VID_PROG_SIZE \
92 ((NTSC_ODD_FLD_LINES + 1) * 3 * DWORD_SIZE + \
93 RISC_WRITECR_INSTRUCTION_SIZE + JUMP_INSTRUCTION_SIZE + \
94 NUM_NO_OPS * DWORD_SIZE)
95
96#define NTSC_RISC_BUF_SIZE \
97 (2 * (RISC_SYNC_INSTRUCTION_SIZE + NTSC_US_VID_PROG_SIZE))
98
99#define FRAME1_VID_PROG_SIZE \
100 ((NTSC_ODD_FLD_LINES + NTSC_FIELD_HEIGHT) * 3 * DWORD_SIZE + \
101 2 * RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
102 JUMP_INSTRUCTION_SIZE + 2 * NUM_NO_OPS * DWORD_SIZE)
103
104#endif
105
106#ifndef USE_RISC_NOOP_VIDEO
107#define PAL_US_VID_PROG_SIZE \
108 (PAL_FIELD_HEIGHT * 3 * DWORD_SIZE + \
109 RISC_WRITECR_INSTRUCTION_SIZE + RISC_SYNC_INSTRUCTION_SIZE + \
110 JUMP_INSTRUCTION_SIZE)
111
112#define PAL_RISC_BUF_SIZE (2 * PAL_US_VID_PROG_SIZE)
113
114#define PAL_VID_PROG_SIZE \
115 ((PAL_FIELD_HEIGHT * 2) * 3 * DWORD_SIZE + \
116 2 * RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
117 JUMP_INSTRUCTION_SIZE)
118
119#define ODD_FLD_PAL_PROG_SIZE \
120 (PAL_FIELD_HEIGHT * 3 * DWORD_SIZE + \
121 RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE)
122
123#define ODD_FLD_NTSC_PROG_SIZE \
124 (NTSC_ODD_FLD_LINES * 3 * DWORD_SIZE + \
125 RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE)
126
127#define NTSC_US_VID_PROG_SIZE \
128 ((NTSC_ODD_FLD_LINES + 1) * 3 * DWORD_SIZE + \
129 RISC_WRITECR_INSTRUCTION_SIZE + JUMP_INSTRUCTION_SIZE)
130
131#define NTSC_RISC_BUF_SIZE \
132 (2 * (RISC_SYNC_INSTRUCTION_SIZE + NTSC_US_VID_PROG_SIZE))
133
134#define FRAME1_VID_PROG_SIZE \
135 ((NTSC_ODD_FLD_LINES + NTSC_FIELD_HEIGHT) * 3 * DWORD_SIZE + \
136 2 * RISC_SYNC_INSTRUCTION_SIZE + RISC_WRITECR_INSTRUCTION_SIZE + \
137 JUMP_INSTRUCTION_SIZE)
138
139#endif
diff --git a/drivers/media/pci/cx25821/cx25821-video.c b/drivers/media/pci/cx25821/cx25821-video.c
new file mode 100644
index 000000000000..b38d4379cc36
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-video.c
@@ -0,0 +1,1990 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 * Parts adapted/taken from Eduardo Moscoso Rubino
8 * Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 *
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29#include "cx25821-video.h"
30
31MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
32MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
33MODULE_LICENSE("GPL");
34
35static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
36static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
37
38module_param_array(video_nr, int, NULL, 0444);
39module_param_array(radio_nr, int, NULL, 0444);
40
41MODULE_PARM_DESC(video_nr, "video device numbers");
42MODULE_PARM_DESC(radio_nr, "radio device numbers");
43
44static unsigned int video_debug = VIDEO_DEBUG;
45module_param(video_debug, int, 0644);
46MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
47
48static unsigned int irq_debug;
49module_param(irq_debug, int, 0644);
50MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
51
52unsigned int vid_limit = 16;
53module_param(vid_limit, int, 0644);
54MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
55
56static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
57
58static const struct v4l2_file_operations video_fops;
59static const struct v4l2_ioctl_ops video_ioctl_ops;
60
61#define FORMAT_FLAGS_PACKED 0x01
62
63struct cx25821_fmt formats[] = {
64 {
65 .name = "8 bpp, gray",
66 .fourcc = V4L2_PIX_FMT_GREY,
67 .depth = 8,
68 .flags = FORMAT_FLAGS_PACKED,
69 }, {
70 .name = "4:1:1, packed, Y41P",
71 .fourcc = V4L2_PIX_FMT_Y41P,
72 .depth = 12,
73 .flags = FORMAT_FLAGS_PACKED,
74 }, {
75 .name = "4:2:2, packed, YUYV",
76 .fourcc = V4L2_PIX_FMT_YUYV,
77 .depth = 16,
78 .flags = FORMAT_FLAGS_PACKED,
79 }, {
80 .name = "4:2:2, packed, UYVY",
81 .fourcc = V4L2_PIX_FMT_UYVY,
82 .depth = 16,
83 .flags = FORMAT_FLAGS_PACKED,
84 }, {
85 .name = "4:2:0, YUV",
86 .fourcc = V4L2_PIX_FMT_YUV420,
87 .depth = 12,
88 .flags = FORMAT_FLAGS_PACKED,
89 },
90};
91
92int cx25821_get_format_size(void)
93{
94 return ARRAY_SIZE(formats);
95}
96
97struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
98{
99 unsigned int i;
100
101 if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
102 return formats + 1;
103
104 for (i = 0; i < ARRAY_SIZE(formats); i++)
105 if (formats[i].fourcc == fourcc)
106 return formats + i;
107
108 pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
109 return NULL;
110}
111
112void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
113 u32 count)
114{
115 struct cx25821_buffer *buf;
116 int bc;
117
118 for (bc = 0;; bc++) {
119 if (list_empty(&q->active)) {
120 dprintk(1, "bc=%d (=0: active empty)\n", bc);
121 break;
122 }
123
124 buf = list_entry(q->active.next, struct cx25821_buffer,
125 vb.queue);
126
127 /* count comes from the hw and it is 16bit wide --
128 * this trick handles wrap-arounds correctly for
129 * up to 32767 buffers in flight... */
130 if ((s16) (count - buf->count) < 0)
131 break;
132
133 do_gettimeofday(&buf->vb.ts);
134 buf->vb.state = VIDEOBUF_DONE;
135 list_del(&buf->vb.queue);
136 wake_up(&buf->vb.done);
137 }
138
139 if (list_empty(&q->active))
140 del_timer(&q->timeout);
141 else
142 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
143 if (bc != 1)
144 pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
145}
146
147#ifdef TUNER_FLAG
148int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
149{
150 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
151 __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
152
153 dev->tvnorm = norm;
154
155 /* Tell the internal A/V decoder */
156 cx25821_call_all(dev, core, s_std, norm);
157
158 return 0;
159}
160#endif
161
162struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
163 struct pci_dev *pci,
164 struct video_device *template,
165 char *type)
166{
167 struct video_device *vfd;
168 dprintk(1, "%s()\n", __func__);
169
170 vfd = video_device_alloc();
171 if (NULL == vfd)
172 return NULL;
173 *vfd = *template;
174 vfd->v4l2_dev = &dev->v4l2_dev;
175 vfd->release = video_device_release;
176 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
177 cx25821_boards[dev->board].name);
178 video_set_drvdata(vfd, dev);
179 return vfd;
180}
181
182/*
183static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
184{
185 int i;
186
187 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
188 return -EINVAL;
189 for (i = 0; i < CX25821_CTLS; i++)
190 if (cx25821_ctls[i].v.id == qctrl->id)
191 break;
192 if (i == CX25821_CTLS) {
193 *qctrl = no_ctl;
194 return 0;
195 }
196 *qctrl = cx25821_ctls[i].v;
197 return 0;
198}
199*/
200
201/* resource management */
202int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
203 unsigned int bit)
204{
205 dprintk(1, "%s()\n", __func__);
206 if (fh->resources & bit)
207 /* have it already allocated */
208 return 1;
209
210 /* is it free? */
211 mutex_lock(&dev->lock);
212 if (dev->channels[fh->channel_id].resources & bit) {
213 /* no, someone else uses it */
214 mutex_unlock(&dev->lock);
215 return 0;
216 }
217 /* it's free, grab it */
218 fh->resources |= bit;
219 dev->channels[fh->channel_id].resources |= bit;
220 dprintk(1, "res: get %d\n", bit);
221 mutex_unlock(&dev->lock);
222 return 1;
223}
224
225int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
226{
227 return fh->resources & bit;
228}
229
230int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
231{
232 return fh->dev->channels[fh->channel_id].resources & bit;
233}
234
235void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
236 unsigned int bits)
237{
238 BUG_ON((fh->resources & bits) != bits);
239 dprintk(1, "%s()\n", __func__);
240
241 mutex_lock(&dev->lock);
242 fh->resources &= ~bits;
243 dev->channels[fh->channel_id].resources &= ~bits;
244 dprintk(1, "res: put %d\n", bits);
245 mutex_unlock(&dev->lock);
246}
247
248int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
249{
250 struct v4l2_routing route;
251 memset(&route, 0, sizeof(route));
252
253 dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
254 __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
255 INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
256 dev->input = input;
257
258 route.input = INPUT(input)->vmux;
259
260 /* Tell the internal A/V decoder */
261 cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
262
263 return 0;
264}
265
266int cx25821_start_video_dma(struct cx25821_dev *dev,
267 struct cx25821_dmaqueue *q,
268 struct cx25821_buffer *buf,
269 struct sram_channel *channel)
270{
271 int tmp = 0;
272
273 /* setup fifo + format */
274 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
275
276 /* reset counter */
277 cx_write(channel->gpcnt_ctl, 3);
278 q->count = 1;
279
280 /* enable irq */
281 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
282 cx_set(channel->int_msk, 0x11);
283
284 /* start dma */
285 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
286
287 /* make sure upstream setting if any is reversed */
288 tmp = cx_read(VID_CH_MODE_SEL);
289 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
290
291 return 0;
292}
293
294int cx25821_restart_video_queue(struct cx25821_dev *dev,
295 struct cx25821_dmaqueue *q,
296 struct sram_channel *channel)
297{
298 struct cx25821_buffer *buf, *prev;
299 struct list_head *item;
300
301 if (!list_empty(&q->active)) {
302 buf = list_entry(q->active.next, struct cx25821_buffer,
303 vb.queue);
304
305 cx25821_start_video_dma(dev, q, buf, channel);
306
307 list_for_each(item, &q->active) {
308 buf = list_entry(item, struct cx25821_buffer, vb.queue);
309 buf->count = q->count++;
310 }
311
312 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
313 return 0;
314 }
315
316 prev = NULL;
317 for (;;) {
318 if (list_empty(&q->queued))
319 return 0;
320
321 buf = list_entry(q->queued.next, struct cx25821_buffer,
322 vb.queue);
323
324 if (NULL == prev) {
325 list_move_tail(&buf->vb.queue, &q->active);
326 cx25821_start_video_dma(dev, q, buf, channel);
327 buf->vb.state = VIDEOBUF_ACTIVE;
328 buf->count = q->count++;
329 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
330 } else if (prev->vb.width == buf->vb.width &&
331 prev->vb.height == buf->vb.height &&
332 prev->fmt == buf->fmt) {
333 list_move_tail(&buf->vb.queue, &q->active);
334 buf->vb.state = VIDEOBUF_ACTIVE;
335 buf->count = q->count++;
336 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
337 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
338 } else {
339 return 0;
340 }
341 prev = buf;
342 }
343}
344
345void cx25821_vid_timeout(unsigned long data)
346{
347 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
348 struct cx25821_dev *dev = timeout_data->dev;
349 struct sram_channel *channel = timeout_data->channel;
350 struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
351 struct cx25821_buffer *buf;
352 unsigned long flags;
353
354 /* cx25821_sram_channel_dump(dev, channel); */
355 cx_clear(channel->dma_ctl, 0x11);
356
357 spin_lock_irqsave(&dev->slock, flags);
358 while (!list_empty(&q->active)) {
359 buf = list_entry(q->active.next, struct cx25821_buffer,
360 vb.queue);
361 list_del(&buf->vb.queue);
362
363 buf->vb.state = VIDEOBUF_ERROR;
364 wake_up(&buf->vb.done);
365 }
366
367 cx25821_restart_video_queue(dev, q, channel);
368 spin_unlock_irqrestore(&dev->slock, flags);
369}
370
371int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
372{
373 u32 count = 0;
374 int handled = 0;
375 u32 mask;
376 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
377
378 mask = cx_read(channel->int_msk);
379 if (0 == (status & mask))
380 return handled;
381
382 cx_write(channel->int_stat, status);
383
384 /* risc op code error */
385 if (status & (1 << 16)) {
386 pr_warn("%s, %s: video risc op code error\n",
387 dev->name, channel->name);
388 cx_clear(channel->dma_ctl, 0x11);
389 cx25821_sram_channel_dump(dev, channel);
390 }
391
392 /* risc1 y */
393 if (status & FLD_VID_DST_RISC1) {
394 spin_lock(&dev->slock);
395 count = cx_read(channel->gpcnt);
396 cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
397 count);
398 spin_unlock(&dev->slock);
399 handled++;
400 }
401
402 /* risc2 y */
403 if (status & 0x10) {
404 dprintk(2, "stopper video\n");
405 spin_lock(&dev->slock);
406 cx25821_restart_video_queue(dev,
407 &dev->channels[channel->i].vidq, channel);
408 spin_unlock(&dev->slock);
409 handled++;
410 }
411 return handled;
412}
413
414void cx25821_videoioctl_unregister(struct cx25821_dev *dev)
415{
416 if (dev->ioctl_dev) {
417 if (video_is_registered(dev->ioctl_dev))
418 video_unregister_device(dev->ioctl_dev);
419 else
420 video_device_release(dev->ioctl_dev);
421
422 dev->ioctl_dev = NULL;
423 }
424}
425
426void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
427{
428 cx_clear(PCI_INT_MSK, 1);
429
430 if (dev->channels[chan_num].video_dev) {
431 if (video_is_registered(dev->channels[chan_num].video_dev))
432 video_unregister_device(
433 dev->channels[chan_num].video_dev);
434 else
435 video_device_release(
436 dev->channels[chan_num].video_dev);
437
438 dev->channels[chan_num].video_dev = NULL;
439
440 btcx_riscmem_free(dev->pci,
441 &dev->channels[chan_num].vidq.stopper);
442
443 pr_warn("device %d released!\n", chan_num);
444 }
445
446}
447
448int cx25821_video_register(struct cx25821_dev *dev)
449{
450 int err;
451 int i;
452
453 struct video_device cx25821_video_device = {
454 .name = "cx25821-video",
455 .fops = &video_fops,
456 .minor = -1,
457 .ioctl_ops = &video_ioctl_ops,
458 .tvnorms = CX25821_NORMS,
459 .current_norm = V4L2_STD_NTSC_M,
460 };
461
462 spin_lock_init(&dev->slock);
463
464 for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
465 cx25821_init_controls(dev, i);
466
467 cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
468 dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
469
470 dev->channels[i].sram_channels = &cx25821_sram_channels[i];
471 dev->channels[i].video_dev = NULL;
472 dev->channels[i].resources = 0;
473
474 cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
475
476 INIT_LIST_HEAD(&dev->channels[i].vidq.active);
477 INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
478
479 dev->channels[i].timeout_data.dev = dev;
480 dev->channels[i].timeout_data.channel =
481 &cx25821_sram_channels[i];
482 dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
483 dev->channels[i].vidq.timeout.data =
484 (unsigned long)&dev->channels[i].timeout_data;
485 init_timer(&dev->channels[i].vidq.timeout);
486
487 /* register v4l devices */
488 dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
489 &cx25821_video_device, "video");
490
491 err = video_register_device(dev->channels[i].video_dev,
492 VFL_TYPE_GRABBER, video_nr[dev->nr]);
493
494 if (err < 0)
495 goto fail_unreg;
496
497 }
498
499 /* set PCI interrupt */
500 cx_set(PCI_INT_MSK, 0xff);
501
502 /* initial device configuration */
503 mutex_lock(&dev->lock);
504#ifdef TUNER_FLAG
505 dev->tvnorm = cx25821_video_device.current_norm;
506 cx25821_set_tvnorm(dev, dev->tvnorm);
507#endif
508 mutex_unlock(&dev->lock);
509
510 return 0;
511
512fail_unreg:
513 cx25821_video_unregister(dev, i);
514 return err;
515}
516
517int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
518 unsigned int *size)
519{
520 struct cx25821_fh *fh = q->priv_data;
521
522 *size = fh->fmt->depth * fh->width * fh->height >> 3;
523
524 if (0 == *count)
525 *count = 32;
526
527 if (*size * *count > vid_limit * 1024 * 1024)
528 *count = (vid_limit * 1024 * 1024) / *size;
529
530 return 0;
531}
532
533int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
534 enum v4l2_field field)
535{
536 struct cx25821_fh *fh = q->priv_data;
537 struct cx25821_dev *dev = fh->dev;
538 struct cx25821_buffer *buf =
539 container_of(vb, struct cx25821_buffer, vb);
540 int rc, init_buffer = 0;
541 u32 line0_offset;
542 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
543 int bpl_local = LINE_SIZE_D1;
544 int channel_opened = fh->channel_id;
545
546 BUG_ON(NULL == fh->fmt);
547 if (fh->width < 48 || fh->width > 720 ||
548 fh->height < 32 || fh->height > 576)
549 return -EINVAL;
550
551 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
552
553 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
554 return -EINVAL;
555
556 if (buf->fmt != fh->fmt ||
557 buf->vb.width != fh->width ||
558 buf->vb.height != fh->height || buf->vb.field != field) {
559 buf->fmt = fh->fmt;
560 buf->vb.width = fh->width;
561 buf->vb.height = fh->height;
562 buf->vb.field = field;
563 init_buffer = 1;
564 }
565
566 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
567 init_buffer = 1;
568 rc = videobuf_iolock(q, &buf->vb, NULL);
569 if (0 != rc) {
570 printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
571 goto fail;
572 }
573 }
574
575 dprintk(1, "init_buffer=%d\n", init_buffer);
576
577 if (init_buffer) {
578
579 channel_opened = dev->channel_opened;
580 if (channel_opened < 0 || channel_opened > 7)
581 channel_opened = 7;
582
583 if (dev->channels[channel_opened].pixel_formats ==
584 PIXEL_FRMT_411)
585 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
586 else
587 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
588
589 if (dev->channels[channel_opened].pixel_formats ==
590 PIXEL_FRMT_411) {
591 bpl_local = buf->bpl;
592 } else {
593 bpl_local = buf->bpl; /* Default */
594
595 if (channel_opened >= 0 && channel_opened <= 7) {
596 if (dev->channels[channel_opened]
597 .use_cif_resolution) {
598 if (dev->tvnorm & V4L2_STD_PAL_BG ||
599 dev->tvnorm & V4L2_STD_PAL_DK)
600 bpl_local = 352 << 1;
601 else
602 bpl_local = dev->channels[
603 channel_opened].
604 cif_width << 1;
605 }
606 }
607 }
608
609 switch (buf->vb.field) {
610 case V4L2_FIELD_TOP:
611 cx25821_risc_buffer(dev->pci, &buf->risc,
612 dma->sglist, 0, UNSET,
613 buf->bpl, 0, buf->vb.height);
614 break;
615 case V4L2_FIELD_BOTTOM:
616 cx25821_risc_buffer(dev->pci, &buf->risc,
617 dma->sglist, UNSET, 0,
618 buf->bpl, 0, buf->vb.height);
619 break;
620 case V4L2_FIELD_INTERLACED:
621 /* All other formats are top field first */
622 line0_offset = 0;
623 dprintk(1, "top field first\n");
624
625 cx25821_risc_buffer(dev->pci, &buf->risc,
626 dma->sglist, line0_offset,
627 bpl_local, bpl_local, bpl_local,
628 buf->vb.height >> 1);
629 break;
630 case V4L2_FIELD_SEQ_TB:
631 cx25821_risc_buffer(dev->pci, &buf->risc,
632 dma->sglist,
633 0, buf->bpl * (buf->vb.height >> 1),
634 buf->bpl, 0, buf->vb.height >> 1);
635 break;
636 case V4L2_FIELD_SEQ_BT:
637 cx25821_risc_buffer(dev->pci, &buf->risc,
638 dma->sglist,
639 buf->bpl * (buf->vb.height >> 1), 0,
640 buf->bpl, 0, buf->vb.height >> 1);
641 break;
642 default:
643 BUG();
644 }
645 }
646
647 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
648 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
649 fh->fmt->name, (unsigned long)buf->risc.dma);
650
651 buf->vb.state = VIDEOBUF_PREPARED;
652
653 return 0;
654
655fail:
656 cx25821_free_buffer(q, buf);
657 return rc;
658}
659
660void cx25821_buffer_release(struct videobuf_queue *q,
661 struct videobuf_buffer *vb)
662{
663 struct cx25821_buffer *buf =
664 container_of(vb, struct cx25821_buffer, vb);
665
666 cx25821_free_buffer(q, buf);
667}
668
669struct videobuf_queue *get_queue(struct cx25821_fh *fh)
670{
671 switch (fh->type) {
672 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
673 return &fh->vidq;
674 default:
675 BUG();
676 return NULL;
677 }
678}
679
680int cx25821_get_resource(struct cx25821_fh *fh, int resource)
681{
682 switch (fh->type) {
683 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
684 return resource;
685 default:
686 BUG();
687 return 0;
688 }
689}
690
691int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
692{
693 struct cx25821_fh *fh = file->private_data;
694
695 return videobuf_mmap_mapper(get_queue(fh), vma);
696}
697
698
699static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
700{
701 struct cx25821_buffer *buf =
702 container_of(vb, struct cx25821_buffer, vb);
703 struct cx25821_buffer *prev;
704 struct cx25821_fh *fh = vq->priv_data;
705 struct cx25821_dev *dev = fh->dev;
706 struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
707
708 /* add jump to stopper */
709 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
710 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
711 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
712
713 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
714
715 if (!list_empty(&q->queued)) {
716 list_add_tail(&buf->vb.queue, &q->queued);
717 buf->vb.state = VIDEOBUF_QUEUED;
718 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
719 buf->vb.i);
720
721 } else if (list_empty(&q->active)) {
722 list_add_tail(&buf->vb.queue, &q->active);
723 cx25821_start_video_dma(dev, q, buf,
724 dev->channels[fh->channel_id].sram_channels);
725 buf->vb.state = VIDEOBUF_ACTIVE;
726 buf->count = q->count++;
727 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
728 dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
729 buf, buf->vb.i, buf->count, q->count);
730 } else {
731 prev = list_entry(q->active.prev, struct cx25821_buffer,
732 vb.queue);
733 if (prev->vb.width == buf->vb.width
734 && prev->vb.height == buf->vb.height
735 && prev->fmt == buf->fmt) {
736 list_add_tail(&buf->vb.queue, &q->active);
737 buf->vb.state = VIDEOBUF_ACTIVE;
738 buf->count = q->count++;
739 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
740
741 /* 64 bit bits 63-32 */
742 prev->risc.jmp[2] = cpu_to_le32(0);
743 dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
744 buf, buf->vb.i, buf->count);
745
746 } else {
747 list_add_tail(&buf->vb.queue, &q->queued);
748 buf->vb.state = VIDEOBUF_QUEUED;
749 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
750 buf->vb.i);
751 }
752 }
753
754 if (list_empty(&q->active))
755 dprintk(2, "active queue empty!\n");
756}
757
758static struct videobuf_queue_ops cx25821_video_qops = {
759 .buf_setup = cx25821_buffer_setup,
760 .buf_prepare = cx25821_buffer_prepare,
761 .buf_queue = buffer_queue,
762 .buf_release = cx25821_buffer_release,
763};
764
765static int video_open(struct file *file)
766{
767 struct video_device *vdev = video_devdata(file);
768 struct cx25821_dev *h, *dev = video_drvdata(file);
769 struct cx25821_fh *fh;
770 struct list_head *list;
771 int minor = video_devdata(file)->minor;
772 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
773 u32 pix_format;
774 int ch_id = 0;
775 int i;
776
777 dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
778 v4l2_type_names[type]);
779
780 /* allocate + initialize per filehandle data */
781 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
782 if (NULL == fh)
783 return -ENOMEM;
784
785 mutex_lock(&cx25821_devlist_mutex);
786
787 list_for_each(list, &cx25821_devlist)
788 {
789 h = list_entry(list, struct cx25821_dev, devlist);
790
791 for (i = 0; i < MAX_VID_CHANNEL_NUM; i++) {
792 if (h->channels[i].video_dev &&
793 h->channels[i].video_dev->minor == minor) {
794 dev = h;
795 ch_id = i;
796 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
797 }
798 }
799 }
800
801 if (NULL == dev) {
802 mutex_unlock(&cx25821_devlist_mutex);
803 kfree(fh);
804 return -ENODEV;
805 }
806
807 file->private_data = fh;
808 fh->dev = dev;
809 fh->type = type;
810 fh->width = 720;
811 fh->channel_id = ch_id;
812
813 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
814 fh->height = 576;
815 else
816 fh->height = 480;
817
818 dev->channel_opened = fh->channel_id;
819 if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
820 pix_format = V4L2_PIX_FMT_Y41P;
821 else
822 pix_format = V4L2_PIX_FMT_YUYV;
823 fh->fmt = cx25821_format_by_fourcc(pix_format);
824
825 v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
826
827 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
828 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
829 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
830 fh, NULL);
831
832 dprintk(1, "post videobuf_queue_init()\n");
833 mutex_unlock(&cx25821_devlist_mutex);
834
835 return 0;
836}
837
838static ssize_t video_read(struct file *file, char __user * data, size_t count,
839 loff_t *ppos)
840{
841 struct cx25821_fh *fh = file->private_data;
842
843 switch (fh->type) {
844 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
845 if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
846 return -EBUSY;
847
848 return videobuf_read_one(&fh->vidq, data, count, ppos,
849 file->f_flags & O_NONBLOCK);
850
851 default:
852 BUG();
853 return 0;
854 }
855}
856
857static unsigned int video_poll(struct file *file,
858 struct poll_table_struct *wait)
859{
860 struct cx25821_fh *fh = file->private_data;
861 struct cx25821_buffer *buf;
862
863 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
864 /* streaming capture */
865 if (list_empty(&fh->vidq.stream))
866 return POLLERR;
867 buf = list_entry(fh->vidq.stream.next,
868 struct cx25821_buffer, vb.stream);
869 } else {
870 /* read() capture */
871 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
872 if (NULL == buf)
873 return POLLERR;
874 }
875
876 poll_wait(file, &buf->vb.done, wait);
877 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
878 if (buf->vb.state == VIDEOBUF_DONE) {
879 struct cx25821_dev *dev = fh->dev;
880
881 if (dev && dev->channels[fh->channel_id]
882 .use_cif_resolution) {
883 u8 cam_id = *((char *)buf->vb.baddr + 3);
884 memcpy((char *)buf->vb.baddr,
885 (char *)buf->vb.baddr + (fh->width * 2),
886 (fh->width * 2));
887 *((char *)buf->vb.baddr + 3) = cam_id;
888 }
889 }
890
891 return POLLIN | POLLRDNORM;
892 }
893
894 return 0;
895}
896
897static int video_release(struct file *file)
898{
899 struct cx25821_fh *fh = file->private_data;
900 struct cx25821_dev *dev = fh->dev;
901
902 /* stop the risc engine and fifo */
903 cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
904
905 /* stop video capture */
906 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
907 videobuf_queue_cancel(&fh->vidq);
908 cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
909 }
910
911 if (fh->vidq.read_buf) {
912 cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
913 kfree(fh->vidq.read_buf);
914 }
915
916 videobuf_mmap_free(&fh->vidq);
917
918 v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
919 file->private_data = NULL;
920 kfree(fh);
921
922 return 0;
923}
924
925static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
926{
927 struct cx25821_fh *fh = priv;
928 struct cx25821_dev *dev = fh->dev;
929
930 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
931 return -EINVAL;
932
933 if (unlikely(i != fh->type))
934 return -EINVAL;
935
936 if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
937 RESOURCE_VIDEO0))))
938 return -EBUSY;
939
940 return videobuf_streamon(get_queue(fh));
941}
942
943static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
944{
945 struct cx25821_fh *fh = priv;
946 struct cx25821_dev *dev = fh->dev;
947 int err, res;
948
949 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
950 return -EINVAL;
951 if (i != fh->type)
952 return -EINVAL;
953
954 res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
955 err = videobuf_streamoff(get_queue(fh));
956 if (err < 0)
957 return err;
958 cx25821_res_free(dev, fh, res);
959 return 0;
960}
961
962static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
963 struct v4l2_format *f)
964{
965 struct cx25821_fh *fh = priv;
966 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
967 struct v4l2_mbus_framefmt mbus_fmt;
968 int err;
969 int pix_format = PIXEL_FRMT_422;
970
971 if (fh) {
972 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
973 fh->prio);
974 if (0 != err)
975 return err;
976 }
977
978 dprintk(2, "%s()\n", __func__);
979 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
980
981 if (0 != err)
982 return err;
983
984 fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
985 fh->vidq.field = f->fmt.pix.field;
986
987 /* check if width and height is valid based on set standard */
988 if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
989 fh->width = f->fmt.pix.width;
990
991 if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
992 fh->height = f->fmt.pix.height;
993
994 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
995 pix_format = PIXEL_FRMT_411;
996 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
997 pix_format = PIXEL_FRMT_422;
998 else
999 return -EINVAL;
1000
1001 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
1002
1003 /* check if cif resolution */
1004 if (fh->width == 320 || fh->width == 352)
1005 dev->channels[fh->channel_id].use_cif_resolution = 1;
1006 else
1007 dev->channels[fh->channel_id].use_cif_resolution = 0;
1008
1009 dev->channels[fh->channel_id].cif_width = fh->width;
1010 medusa_set_resolution(dev, fh->width, SRAM_CH00);
1011
1012 dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
1013 fh->height, fh->vidq.field);
1014 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1015 cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1016
1017 return 0;
1018}
1019
1020static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1021{
1022 int ret_val = 0;
1023 struct cx25821_fh *fh = priv;
1024 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1025
1026 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
1027
1028 p->sequence = dev->channels[fh->channel_id].vidq.count;
1029
1030 return ret_val;
1031}
1032
1033static int vidioc_log_status(struct file *file, void *priv)
1034{
1035 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1036 struct cx25821_fh *fh = priv;
1037 char name[32 + 2];
1038
1039 struct sram_channel *sram_ch = dev->channels[fh->channel_id]
1040 .sram_channels;
1041 u32 tmp = 0;
1042
1043 snprintf(name, sizeof(name), "%s/2", dev->name);
1044 pr_info("%s/2: ============ START LOG STATUS ============\n",
1045 dev->name);
1046 cx25821_call_all(dev, core, log_status);
1047 tmp = cx_read(sram_ch->dma_ctl);
1048 pr_info("Video input 0 is %s\n",
1049 (tmp & 0x11) ? "streaming" : "stopped");
1050 pr_info("%s/2: ============= END LOG STATUS =============\n",
1051 dev->name);
1052 return 0;
1053}
1054
1055static int vidioc_s_ctrl(struct file *file, void *priv,
1056 struct v4l2_control *ctl)
1057{
1058 struct cx25821_fh *fh = priv;
1059 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1060 int err;
1061
1062 if (fh) {
1063 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1064 fh->prio);
1065 if (0 != err)
1066 return err;
1067 }
1068
1069 return cx25821_set_control(dev, ctl, fh->channel_id);
1070}
1071
1072/* VIDEO IOCTLS */
1073int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1074 struct v4l2_format *f)
1075{
1076 struct cx25821_fh *fh = priv;
1077
1078 f->fmt.pix.width = fh->width;
1079 f->fmt.pix.height = fh->height;
1080 f->fmt.pix.field = fh->vidq.field;
1081 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1082 f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
1083 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1084
1085 return 0;
1086}
1087
1088int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1089 struct v4l2_format *f)
1090{
1091 struct cx25821_fmt *fmt;
1092 enum v4l2_field field;
1093 unsigned int maxw, maxh;
1094
1095 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
1096 if (NULL == fmt)
1097 return -EINVAL;
1098
1099 field = f->fmt.pix.field;
1100 maxw = 720;
1101 maxh = 576;
1102
1103 if (V4L2_FIELD_ANY == field) {
1104 if (f->fmt.pix.height > maxh / 2)
1105 field = V4L2_FIELD_INTERLACED;
1106 else
1107 field = V4L2_FIELD_TOP;
1108 }
1109
1110 switch (field) {
1111 case V4L2_FIELD_TOP:
1112 case V4L2_FIELD_BOTTOM:
1113 maxh = maxh / 2;
1114 break;
1115 case V4L2_FIELD_INTERLACED:
1116 break;
1117 default:
1118 return -EINVAL;
1119 }
1120
1121 f->fmt.pix.field = field;
1122 if (f->fmt.pix.height < 32)
1123 f->fmt.pix.height = 32;
1124 if (f->fmt.pix.height > maxh)
1125 f->fmt.pix.height = maxh;
1126 if (f->fmt.pix.width < 48)
1127 f->fmt.pix.width = 48;
1128 if (f->fmt.pix.width > maxw)
1129 f->fmt.pix.width = maxw;
1130 f->fmt.pix.width &= ~0x03;
1131 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
1132 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1133
1134 return 0;
1135}
1136
1137int cx25821_vidioc_querycap(struct file *file, void *priv,
1138 struct v4l2_capability *cap)
1139{
1140 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1141
1142 strcpy(cap->driver, "cx25821");
1143 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1144 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1145 cap->version = CX25821_VERSION_CODE;
1146 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1147 V4L2_CAP_STREAMING;
1148 if (UNSET != dev->tuner_type)
1149 cap->capabilities |= V4L2_CAP_TUNER;
1150 return 0;
1151}
1152
1153int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1154 struct v4l2_fmtdesc *f)
1155{
1156 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1157 return -EINVAL;
1158
1159 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1160 f->pixelformat = formats[f->index].fourcc;
1161
1162 return 0;
1163}
1164
1165int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1166 struct v4l2_requestbuffers *p)
1167{
1168 struct cx25821_fh *fh = priv;
1169 return videobuf_reqbufs(get_queue(fh), p);
1170}
1171
1172int cx25821_vidioc_querybuf(struct file *file, void *priv,
1173 struct v4l2_buffer *p)
1174{
1175 struct cx25821_fh *fh = priv;
1176 return videobuf_querybuf(get_queue(fh), p);
1177}
1178
1179int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1180{
1181 struct cx25821_fh *fh = priv;
1182 return videobuf_qbuf(get_queue(fh), p);
1183}
1184
1185int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1186{
1187 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1188 struct cx25821_fh *fh = f;
1189
1190 *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1191
1192 return 0;
1193}
1194
1195int cx25821_vidioc_s_priority(struct file *file, void *f,
1196 enum v4l2_priority prio)
1197{
1198 struct cx25821_fh *fh = f;
1199 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1200
1201 return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1202 prio);
1203}
1204
1205#ifdef TUNER_FLAG
1206int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id * tvnorms)
1207{
1208 struct cx25821_fh *fh = priv;
1209 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1210 int err;
1211
1212 dprintk(1, "%s()\n", __func__);
1213
1214 if (fh) {
1215 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1216 fh->prio);
1217 if (0 != err)
1218 return err;
1219 }
1220
1221 if (dev->tvnorm == *tvnorms)
1222 return 0;
1223
1224 mutex_lock(&dev->lock);
1225 cx25821_set_tvnorm(dev, *tvnorms);
1226 mutex_unlock(&dev->lock);
1227
1228 medusa_set_videostandard(dev);
1229
1230 return 0;
1231}
1232#endif
1233
1234int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
1235{
1236 static const char * const iname[] = {
1237 [CX25821_VMUX_COMPOSITE] = "Composite",
1238 [CX25821_VMUX_SVIDEO] = "S-Video",
1239 [CX25821_VMUX_DEBUG] = "for debug only",
1240 };
1241 unsigned int n;
1242 dprintk(1, "%s()\n", __func__);
1243
1244 n = i->index;
1245 if (n >= 2)
1246 return -EINVAL;
1247
1248 if (0 == INPUT(n)->type)
1249 return -EINVAL;
1250
1251 i->type = V4L2_INPUT_TYPE_CAMERA;
1252 strcpy(i->name, iname[INPUT(n)->type]);
1253
1254 i->std = CX25821_NORMS;
1255 return 0;
1256}
1257
1258int cx25821_vidioc_enum_input(struct file *file, void *priv,
1259 struct v4l2_input *i)
1260{
1261 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1262 dprintk(1, "%s()\n", __func__);
1263 return cx25821_enum_input(dev, i);
1264}
1265
1266int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1267{
1268 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1269
1270 *i = dev->input;
1271 dprintk(1, "%s(): returns %d\n", __func__, *i);
1272 return 0;
1273}
1274
1275int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1276{
1277 struct cx25821_fh *fh = priv;
1278 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1279 int err;
1280
1281 dprintk(1, "%s(%d)\n", __func__, i);
1282
1283 if (fh) {
1284 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1285 fh->prio);
1286 if (0 != err)
1287 return err;
1288 }
1289
1290 if (i >= CX25821_NR_INPUT) {
1291 dprintk(1, "%s(): -EINVAL\n", __func__);
1292 return -EINVAL;
1293 }
1294
1295 mutex_lock(&dev->lock);
1296 cx25821_video_mux(dev, i);
1297 mutex_unlock(&dev->lock);
1298 return 0;
1299}
1300
1301#ifdef TUNER_FLAG
1302int cx25821_vidioc_g_frequency(struct file *file, void *priv,
1303 struct v4l2_frequency *f)
1304{
1305 struct cx25821_fh *fh = priv;
1306 struct cx25821_dev *dev = fh->dev;
1307
1308 f->frequency = dev->freq;
1309
1310 cx25821_call_all(dev, tuner, g_frequency, f);
1311
1312 return 0;
1313}
1314
1315int cx25821_set_freq(struct cx25821_dev *dev, struct v4l2_frequency *f)
1316{
1317 mutex_lock(&dev->lock);
1318 dev->freq = f->frequency;
1319
1320 cx25821_call_all(dev, tuner, s_frequency, f);
1321
1322 /* When changing channels it is required to reset TVAUDIO */
1323 msleep(10);
1324
1325 mutex_unlock(&dev->lock);
1326
1327 return 0;
1328}
1329
1330int cx25821_vidioc_s_frequency(struct file *file, void *priv,
1331 struct v4l2_frequency *f)
1332{
1333 struct cx25821_fh *fh = priv;
1334 struct cx25821_dev *dev;
1335 int err;
1336
1337 if (fh) {
1338 dev = fh->dev;
1339 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1340 fh->prio);
1341 if (0 != err)
1342 return err;
1343 } else {
1344 pr_err("Invalid fh pointer!\n");
1345 return -EINVAL;
1346 }
1347
1348 return cx25821_set_freq(dev, f);
1349}
1350#endif
1351
1352#ifdef CONFIG_VIDEO_ADV_DEBUG
1353int cx25821_vidioc_g_register(struct file *file, void *fh,
1354 struct v4l2_dbg_register *reg)
1355{
1356 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1357
1358 if (!v4l2_chip_match_host(&reg->match))
1359 return -EINVAL;
1360
1361 cx25821_call_all(dev, core, g_register, reg);
1362
1363 return 0;
1364}
1365
1366int cx25821_vidioc_s_register(struct file *file, void *fh,
1367 struct v4l2_dbg_register *reg)
1368{
1369 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1370
1371 if (!v4l2_chip_match_host(&reg->match))
1372 return -EINVAL;
1373
1374 cx25821_call_all(dev, core, s_register, reg);
1375
1376 return 0;
1377}
1378
1379#endif
1380
1381#ifdef TUNER_FLAG
1382int cx25821_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1383{
1384 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1385
1386 if (unlikely(UNSET == dev->tuner_type))
1387 return -EINVAL;
1388 if (0 != t->index)
1389 return -EINVAL;
1390
1391 strcpy(t->name, "Television");
1392 t->type = V4L2_TUNER_ANALOG_TV;
1393 t->capability = V4L2_TUNER_CAP_NORM;
1394 t->rangehigh = 0xffffffffUL;
1395
1396 t->signal = 0xffff; /* LOCKED */
1397 return 0;
1398}
1399
1400int cx25821_vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1401{
1402 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1403 struct cx25821_fh *fh = priv;
1404 int err;
1405
1406 if (fh) {
1407 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1408 fh->prio);
1409 if (0 != err)
1410 return err;
1411 }
1412
1413 dprintk(1, "%s()\n", __func__);
1414 if (UNSET == dev->tuner_type)
1415 return -EINVAL;
1416 if (0 != t->index)
1417 return -EINVAL;
1418
1419 return 0;
1420}
1421
1422#endif
1423/*****************************************************************************/
1424static const struct v4l2_queryctrl no_ctl = {
1425 .name = "42",
1426 .flags = V4L2_CTRL_FLAG_DISABLED,
1427};
1428
1429static struct v4l2_queryctrl cx25821_ctls[] = {
1430 /* --- video --- */
1431 {
1432 .id = V4L2_CID_BRIGHTNESS,
1433 .name = "Brightness",
1434 .minimum = 0,
1435 .maximum = 10000,
1436 .step = 1,
1437 .default_value = 6200,
1438 .type = V4L2_CTRL_TYPE_INTEGER,
1439 }, {
1440 .id = V4L2_CID_CONTRAST,
1441 .name = "Contrast",
1442 .minimum = 0,
1443 .maximum = 10000,
1444 .step = 1,
1445 .default_value = 5000,
1446 .type = V4L2_CTRL_TYPE_INTEGER,
1447 }, {
1448 .id = V4L2_CID_SATURATION,
1449 .name = "Saturation",
1450 .minimum = 0,
1451 .maximum = 10000,
1452 .step = 1,
1453 .default_value = 5000,
1454 .type = V4L2_CTRL_TYPE_INTEGER,
1455 }, {
1456 .id = V4L2_CID_HUE,
1457 .name = "Hue",
1458 .minimum = 0,
1459 .maximum = 10000,
1460 .step = 1,
1461 .default_value = 5000,
1462 .type = V4L2_CTRL_TYPE_INTEGER,
1463 }
1464};
1465static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1466
1467static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1468{
1469 int i;
1470
1471 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1472 return -EINVAL;
1473 for (i = 0; i < CX25821_CTLS; i++)
1474 if (cx25821_ctls[i].id == qctrl->id)
1475 break;
1476 if (i == CX25821_CTLS) {
1477 *qctrl = no_ctl;
1478 return 0;
1479 }
1480 *qctrl = cx25821_ctls[i];
1481 return 0;
1482}
1483
1484int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1485 struct v4l2_queryctrl *qctrl)
1486{
1487 return cx25821_ctrl_query(qctrl);
1488}
1489
1490/* ------------------------------------------------------------------ */
1491/* VIDEO CTRL IOCTLS */
1492
1493static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1494{
1495 unsigned int i;
1496
1497 for (i = 0; i < CX25821_CTLS; i++)
1498 if (cx25821_ctls[i].id == id)
1499 return cx25821_ctls + i;
1500 return NULL;
1501}
1502
1503int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
1504 struct v4l2_control *ctl)
1505{
1506 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1507 struct cx25821_fh *fh = priv;
1508
1509 const struct v4l2_queryctrl *ctrl;
1510
1511 ctrl = ctrl_by_id(ctl->id);
1512
1513 if (NULL == ctrl)
1514 return -EINVAL;
1515 switch (ctl->id) {
1516 case V4L2_CID_BRIGHTNESS:
1517 ctl->value = dev->channels[fh->channel_id].ctl_bright;
1518 break;
1519 case V4L2_CID_HUE:
1520 ctl->value = dev->channels[fh->channel_id].ctl_hue;
1521 break;
1522 case V4L2_CID_CONTRAST:
1523 ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1524 break;
1525 case V4L2_CID_SATURATION:
1526 ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1527 break;
1528 }
1529 return 0;
1530}
1531
1532int cx25821_set_control(struct cx25821_dev *dev,
1533 struct v4l2_control *ctl, int chan_num)
1534{
1535 int err;
1536 const struct v4l2_queryctrl *ctrl;
1537
1538 err = -EINVAL;
1539
1540 ctrl = ctrl_by_id(ctl->id);
1541
1542 if (NULL == ctrl)
1543 return err;
1544
1545 switch (ctrl->type) {
1546 case V4L2_CTRL_TYPE_BOOLEAN:
1547 case V4L2_CTRL_TYPE_MENU:
1548 case V4L2_CTRL_TYPE_INTEGER:
1549 if (ctl->value < ctrl->minimum)
1550 ctl->value = ctrl->minimum;
1551 if (ctl->value > ctrl->maximum)
1552 ctl->value = ctrl->maximum;
1553 break;
1554 default:
1555 /* nothing */ ;
1556 }
1557
1558 switch (ctl->id) {
1559 case V4L2_CID_BRIGHTNESS:
1560 dev->channels[chan_num].ctl_bright = ctl->value;
1561 medusa_set_brightness(dev, ctl->value, chan_num);
1562 break;
1563 case V4L2_CID_HUE:
1564 dev->channels[chan_num].ctl_hue = ctl->value;
1565 medusa_set_hue(dev, ctl->value, chan_num);
1566 break;
1567 case V4L2_CID_CONTRAST:
1568 dev->channels[chan_num].ctl_contrast = ctl->value;
1569 medusa_set_contrast(dev, ctl->value, chan_num);
1570 break;
1571 case V4L2_CID_SATURATION:
1572 dev->channels[chan_num].ctl_saturation = ctl->value;
1573 medusa_set_saturation(dev, ctl->value, chan_num);
1574 break;
1575 }
1576
1577 err = 0;
1578
1579 return err;
1580}
1581
1582static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1583{
1584 struct v4l2_control ctrl;
1585 int i;
1586 for (i = 0; i < CX25821_CTLS; i++) {
1587 ctrl.id = cx25821_ctls[i].id;
1588 ctrl.value = cx25821_ctls[i].default_value;
1589
1590 cx25821_set_control(dev, &ctrl, chan_num);
1591 }
1592}
1593
1594int cx25821_vidioc_cropcap(struct file *file, void *priv,
1595 struct v4l2_cropcap *cropcap)
1596{
1597 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1598
1599 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1600 return -EINVAL;
1601 cropcap->bounds.top = 0;
1602 cropcap->bounds.left = 0;
1603 cropcap->bounds.width = 720;
1604 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1605 cropcap->pixelaspect.numerator =
1606 dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1607 cropcap->pixelaspect.denominator =
1608 dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1609 cropcap->defrect = cropcap->bounds;
1610 return 0;
1611}
1612
1613int cx25821_vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1614{
1615 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1616 struct cx25821_fh *fh = priv;
1617 int err;
1618
1619 if (fh) {
1620 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1621 fh->prio);
1622 if (0 != err)
1623 return err;
1624 }
1625 /* cx25821_vidioc_s_crop not supported */
1626 return -EINVAL;
1627}
1628
1629int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1630{
1631 /* cx25821_vidioc_g_crop not supported */
1632 return -EINVAL;
1633}
1634
1635int cx25821_vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm)
1636{
1637 /* medusa does not support video standard sensing of current input */
1638 *norm = CX25821_NORMS;
1639
1640 return 0;
1641}
1642
1643int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1644{
1645 if (tvnorm == V4L2_STD_PAL_BG) {
1646 if (width == 352 || width == 720)
1647 return 1;
1648 else
1649 return 0;
1650 }
1651
1652 if (tvnorm == V4L2_STD_NTSC_M) {
1653 if (width == 320 || width == 352 || width == 720)
1654 return 1;
1655 else
1656 return 0;
1657 }
1658 return 0;
1659}
1660
1661int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1662{
1663 if (tvnorm == V4L2_STD_PAL_BG) {
1664 if (height == 576 || height == 288)
1665 return 1;
1666 else
1667 return 0;
1668 }
1669
1670 if (tvnorm == V4L2_STD_NTSC_M) {
1671 if (height == 480 || height == 240)
1672 return 1;
1673 else
1674 return 0;
1675 }
1676
1677 return 0;
1678}
1679
1680static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1681 unsigned long arg)
1682{
1683 struct cx25821_fh *fh = file->private_data;
1684 struct cx25821_dev *dev = fh->dev;
1685 int command = 0;
1686 struct upstream_user_struct *data_from_user;
1687
1688 data_from_user = (struct upstream_user_struct *)arg;
1689
1690 if (!data_from_user) {
1691 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1692 return 0;
1693 }
1694
1695 command = data_from_user->command;
1696
1697 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1698 return 0;
1699
1700 dev->input_filename = data_from_user->input_filename;
1701 dev->input_audiofilename = data_from_user->input_filename;
1702 dev->vid_stdname = data_from_user->vid_stdname;
1703 dev->pixel_format = data_from_user->pixel_format;
1704 dev->channel_select = data_from_user->channel_select;
1705 dev->command = data_from_user->command;
1706
1707 switch (command) {
1708 case UPSTREAM_START_VIDEO:
1709 cx25821_start_upstream_video_ch1(dev, data_from_user);
1710 break;
1711
1712 case UPSTREAM_STOP_VIDEO:
1713 cx25821_stop_upstream_video_ch1(dev);
1714 break;
1715 }
1716
1717 return 0;
1718}
1719
1720static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1721 unsigned long arg)
1722{
1723 struct cx25821_fh *fh = file->private_data;
1724 struct cx25821_dev *dev = fh->dev;
1725 int command = 0;
1726 struct upstream_user_struct *data_from_user;
1727
1728 data_from_user = (struct upstream_user_struct *)arg;
1729
1730 if (!data_from_user) {
1731 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1732 return 0;
1733 }
1734
1735 command = data_from_user->command;
1736
1737 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1738 return 0;
1739
1740 dev->input_filename_ch2 = data_from_user->input_filename;
1741 dev->input_audiofilename = data_from_user->input_filename;
1742 dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1743 dev->pixel_format_ch2 = data_from_user->pixel_format;
1744 dev->channel_select_ch2 = data_from_user->channel_select;
1745 dev->command_ch2 = data_from_user->command;
1746
1747 switch (command) {
1748 case UPSTREAM_START_VIDEO:
1749 cx25821_start_upstream_video_ch2(dev, data_from_user);
1750 break;
1751
1752 case UPSTREAM_STOP_VIDEO:
1753 cx25821_stop_upstream_video_ch2(dev);
1754 break;
1755 }
1756
1757 return 0;
1758}
1759
1760static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1761 unsigned long arg)
1762{
1763 struct cx25821_fh *fh = file->private_data;
1764 struct cx25821_dev *dev = fh->dev;
1765 int command = 0;
1766 struct upstream_user_struct *data_from_user;
1767
1768 data_from_user = (struct upstream_user_struct *)arg;
1769
1770 if (!data_from_user) {
1771 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1772 return 0;
1773 }
1774
1775 command = data_from_user->command;
1776
1777 if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1778 return 0;
1779
1780 dev->input_filename = data_from_user->input_filename;
1781 dev->input_audiofilename = data_from_user->input_filename;
1782 dev->vid_stdname = data_from_user->vid_stdname;
1783 dev->pixel_format = data_from_user->pixel_format;
1784 dev->channel_select = data_from_user->channel_select;
1785 dev->command = data_from_user->command;
1786
1787 switch (command) {
1788 case UPSTREAM_START_AUDIO:
1789 cx25821_start_upstream_audio(dev, data_from_user);
1790 break;
1791
1792 case UPSTREAM_STOP_AUDIO:
1793 cx25821_stop_upstream_audio(dev);
1794 break;
1795 }
1796
1797 return 0;
1798}
1799
1800static long video_ioctl_set(struct file *file, unsigned int cmd,
1801 unsigned long arg)
1802{
1803 struct cx25821_fh *fh = file->private_data;
1804 struct cx25821_dev *dev = fh->dev;
1805 struct downstream_user_struct *data_from_user;
1806 int command;
1807 int width = 720;
1808 int selected_channel = 0;
1809 int pix_format = 0;
1810 int i = 0;
1811 int cif_enable = 0;
1812 int cif_width = 0;
1813
1814 data_from_user = (struct downstream_user_struct *)arg;
1815
1816 if (!data_from_user) {
1817 pr_err("%s(): User data is INVALID. Returning\n", __func__);
1818 return 0;
1819 }
1820
1821 command = data_from_user->command;
1822
1823 if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1824 && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1825 && command != REG_WRITE && command != MEDUSA_READ
1826 && command != MEDUSA_WRITE) {
1827 return 0;
1828 }
1829
1830 switch (command) {
1831 case SET_VIDEO_STD:
1832 if (!strcmp(data_from_user->vid_stdname, "PAL"))
1833 dev->tvnorm = V4L2_STD_PAL_BG;
1834 else
1835 dev->tvnorm = V4L2_STD_NTSC_M;
1836 medusa_set_videostandard(dev);
1837 break;
1838
1839 case SET_PIXEL_FORMAT:
1840 selected_channel = data_from_user->decoder_select;
1841 pix_format = data_from_user->pixel_format;
1842
1843 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1844 selected_channel -= 4;
1845 selected_channel = selected_channel % 8;
1846 }
1847
1848 if (selected_channel >= 0)
1849 cx25821_set_pixel_format(dev, selected_channel,
1850 pix_format);
1851
1852 break;
1853
1854 case ENABLE_CIF_RESOLUTION:
1855 selected_channel = data_from_user->decoder_select;
1856 cif_enable = data_from_user->cif_resolution_enable;
1857 cif_width = data_from_user->cif_width;
1858
1859 if (cif_enable) {
1860 if (dev->tvnorm & V4L2_STD_PAL_BG
1861 || dev->tvnorm & V4L2_STD_PAL_DK) {
1862 width = 352;
1863 } else {
1864 width = cif_width;
1865 if (cif_width != 320 && cif_width != 352)
1866 width = 320;
1867 }
1868 }
1869
1870 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1871 selected_channel -= 4;
1872 selected_channel = selected_channel % 8;
1873 }
1874
1875 if (selected_channel <= 7 && selected_channel >= 0) {
1876 dev->channels[selected_channel].use_cif_resolution =
1877 cif_enable;
1878 dev->channels[selected_channel].cif_width = width;
1879 } else {
1880 for (i = 0; i < VID_CHANNEL_NUM; i++) {
1881 dev->channels[i].use_cif_resolution =
1882 cif_enable;
1883 dev->channels[i].cif_width = width;
1884 }
1885 }
1886
1887 medusa_set_resolution(dev, width, selected_channel);
1888 break;
1889 case REG_READ:
1890 data_from_user->reg_data = cx_read(data_from_user->reg_address);
1891 break;
1892 case REG_WRITE:
1893 cx_write(data_from_user->reg_address, data_from_user->reg_data);
1894 break;
1895 case MEDUSA_READ:
1896 cx25821_i2c_read(&dev->i2c_bus[0],
1897 (u16) data_from_user->reg_address,
1898 &data_from_user->reg_data);
1899 break;
1900 case MEDUSA_WRITE:
1901 cx25821_i2c_write(&dev->i2c_bus[0],
1902 (u16) data_from_user->reg_address,
1903 data_from_user->reg_data);
1904 break;
1905 }
1906
1907 return 0;
1908}
1909
1910static long cx25821_video_ioctl(struct file *file,
1911 unsigned int cmd, unsigned long arg)
1912{
1913 int ret = 0;
1914
1915 struct cx25821_fh *fh = file->private_data;
1916
1917 /* check to see if it's the video upstream */
1918 if (fh->channel_id == SRAM_CH09) {
1919 ret = video_ioctl_upstream9(file, cmd, arg);
1920 return ret;
1921 } else if (fh->channel_id == SRAM_CH10) {
1922 ret = video_ioctl_upstream10(file, cmd, arg);
1923 return ret;
1924 } else if (fh->channel_id == SRAM_CH11) {
1925 ret = video_ioctl_upstream11(file, cmd, arg);
1926 ret = video_ioctl_set(file, cmd, arg);
1927 return ret;
1928 }
1929
1930 return video_ioctl2(file, cmd, arg);
1931}
1932
1933/* exported stuff */
1934static const struct v4l2_file_operations video_fops = {
1935 .owner = THIS_MODULE,
1936 .open = video_open,
1937 .release = video_release,
1938 .read = video_read,
1939 .poll = video_poll,
1940 .mmap = cx25821_video_mmap,
1941 .ioctl = cx25821_video_ioctl,
1942};
1943
1944static const struct v4l2_ioctl_ops video_ioctl_ops = {
1945 .vidioc_querycap = cx25821_vidioc_querycap,
1946 .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1947 .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1948 .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1949 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1950 .vidioc_reqbufs = cx25821_vidioc_reqbufs,
1951 .vidioc_querybuf = cx25821_vidioc_querybuf,
1952 .vidioc_qbuf = cx25821_vidioc_qbuf,
1953 .vidioc_dqbuf = vidioc_dqbuf,
1954#ifdef TUNER_FLAG
1955 .vidioc_s_std = cx25821_vidioc_s_std,
1956 .vidioc_querystd = cx25821_vidioc_querystd,
1957#endif
1958 .vidioc_cropcap = cx25821_vidioc_cropcap,
1959 .vidioc_s_crop = cx25821_vidioc_s_crop,
1960 .vidioc_g_crop = cx25821_vidioc_g_crop,
1961 .vidioc_enum_input = cx25821_vidioc_enum_input,
1962 .vidioc_g_input = cx25821_vidioc_g_input,
1963 .vidioc_s_input = cx25821_vidioc_s_input,
1964 .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1965 .vidioc_s_ctrl = vidioc_s_ctrl,
1966 .vidioc_queryctrl = cx25821_vidioc_queryctrl,
1967 .vidioc_streamon = vidioc_streamon,
1968 .vidioc_streamoff = vidioc_streamoff,
1969 .vidioc_log_status = vidioc_log_status,
1970 .vidioc_g_priority = cx25821_vidioc_g_priority,
1971 .vidioc_s_priority = cx25821_vidioc_s_priority,
1972#ifdef TUNER_FLAG
1973 .vidioc_g_tuner = cx25821_vidioc_g_tuner,
1974 .vidioc_s_tuner = cx25821_vidioc_s_tuner,
1975 .vidioc_g_frequency = cx25821_vidioc_g_frequency,
1976 .vidioc_s_frequency = cx25821_vidioc_s_frequency,
1977#endif
1978#ifdef CONFIG_VIDEO_ADV_DEBUG
1979 .vidioc_g_register = cx25821_vidioc_g_register,
1980 .vidioc_s_register = cx25821_vidioc_s_register,
1981#endif
1982};
1983
1984struct video_device cx25821_videoioctl_template = {
1985 .name = "cx25821-videoioctl",
1986 .fops = &video_fops,
1987 .ioctl_ops = &video_ioctl_ops,
1988 .tvnorms = CX25821_NORMS,
1989 .current_norm = V4L2_STD_NTSC_M,
1990};
diff --git a/drivers/media/pci/cx25821/cx25821-video.h b/drivers/media/pci/cx25821/cx25821-video.h
new file mode 100644
index 000000000000..9652a5e35ba2
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821-video.h
@@ -0,0 +1,186 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#ifndef CX25821_VIDEO_H_
25#define CX25821_VIDEO_H_
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
35#include <linux/delay.h>
36#include <linux/kthread.h>
37#include <asm/div64.h>
38
39#include "cx25821.h"
40#include <media/v4l2-common.h>
41#include <media/v4l2-ioctl.h>
42
43#define TUNER_FLAG
44
45#define VIDEO_DEBUG 0
46
47#define dprintk(level, fmt, arg...) \
48do { \
49 if (VIDEO_DEBUG >= level) \
50 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ##arg); \
51} while (0)
52
53/* For IOCTL to identify running upstream */
54#define UPSTREAM_START_VIDEO 700
55#define UPSTREAM_STOP_VIDEO 701
56#define UPSTREAM_START_AUDIO 702
57#define UPSTREAM_STOP_AUDIO 703
58#define UPSTREAM_DUMP_REGISTERS 702
59#define SET_VIDEO_STD 800
60#define SET_PIXEL_FORMAT 1000
61#define ENABLE_CIF_RESOLUTION 1001
62
63#define REG_READ 900
64#define REG_WRITE 901
65#define MEDUSA_READ 910
66#define MEDUSA_WRITE 911
67
68extern struct sram_channel *channel0;
69extern struct sram_channel *channel1;
70extern struct sram_channel *channel2;
71extern struct sram_channel *channel3;
72extern struct sram_channel *channel4;
73extern struct sram_channel *channel5;
74extern struct sram_channel *channel6;
75extern struct sram_channel *channel7;
76extern struct sram_channel *channel9;
77extern struct sram_channel *channel10;
78extern struct sram_channel *channel11;
79extern struct video_device cx25821_videoioctl_template;
80/* extern const u32 *ctrl_classes[]; */
81
82extern unsigned int vid_limit;
83
84#define FORMAT_FLAGS_PACKED 0x01
85extern struct cx25821_fmt formats[];
86extern struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc);
87extern struct cx25821_data timeout_data[MAX_VID_CHANNEL_NUM];
88
89extern void cx25821_video_wakeup(struct cx25821_dev *dev,
90 struct cx25821_dmaqueue *q, u32 count);
91
92#ifdef TUNER_FLAG
93extern int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm);
94#endif
95
96extern int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
97 unsigned int bit);
98extern int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit);
99extern int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit);
100extern void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
101 unsigned int bits);
102extern int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input);
103extern int cx25821_start_video_dma(struct cx25821_dev *dev,
104 struct cx25821_dmaqueue *q,
105 struct cx25821_buffer *buf,
106 struct sram_channel *channel);
107
108extern int cx25821_set_scale(struct cx25821_dev *dev, unsigned int width,
109 unsigned int height, enum v4l2_field field);
110extern int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status);
111extern void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num);
112extern int cx25821_video_register(struct cx25821_dev *dev);
113extern int cx25821_get_format_size(void);
114
115extern int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
116 unsigned int *size);
117extern int cx25821_buffer_prepare(struct videobuf_queue *q,
118 struct videobuf_buffer *vb,
119 enum v4l2_field field);
120extern void cx25821_buffer_release(struct videobuf_queue *q,
121 struct videobuf_buffer *vb);
122extern struct videobuf_queue *get_queue(struct cx25821_fh *fh);
123extern int cx25821_get_resource(struct cx25821_fh *fh, int resource);
124extern int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma);
125extern int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
126 struct v4l2_format *f);
127extern int cx25821_vidioc_querycap(struct file *file, void *priv,
128 struct v4l2_capability *cap);
129extern int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
130 struct v4l2_fmtdesc *f);
131extern int cx25821_vidioc_reqbufs(struct file *file, void *priv,
132 struct v4l2_requestbuffers *p);
133extern int cx25821_vidioc_querybuf(struct file *file, void *priv,
134 struct v4l2_buffer *p);
135extern int cx25821_vidioc_qbuf(struct file *file, void *priv,
136 struct v4l2_buffer *p);
137extern int cx25821_vidioc_s_std(struct file *file, void *priv,
138 v4l2_std_id *tvnorms);
139extern int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i);
140extern int cx25821_vidioc_enum_input(struct file *file, void *priv,
141 struct v4l2_input *i);
142extern int cx25821_vidioc_g_input(struct file *file, void *priv,
143 unsigned int *i);
144extern int cx25821_vidioc_s_input(struct file *file, void *priv,
145 unsigned int i);
146extern int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
147 struct v4l2_control *ctl);
148extern int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
149 struct v4l2_format *f);
150extern int cx25821_vidioc_g_frequency(struct file *file, void *priv,
151 struct v4l2_frequency *f);
152extern int cx25821_set_freq(struct cx25821_dev *dev, struct v4l2_frequency *f);
153extern int cx25821_vidioc_s_frequency(struct file *file, void *priv,
154 struct v4l2_frequency *f);
155extern int cx25821_vidioc_g_register(struct file *file, void *fh,
156 struct v4l2_dbg_register *reg);
157extern int cx25821_vidioc_s_register(struct file *file, void *fh,
158 struct v4l2_dbg_register *reg);
159extern int cx25821_vidioc_g_tuner(struct file *file, void *priv,
160 struct v4l2_tuner *t);
161extern int cx25821_vidioc_s_tuner(struct file *file, void *priv,
162 struct v4l2_tuner *t);
163
164extern int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm);
165extern int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm);
166
167extern int cx25821_vidioc_g_priority(struct file *file, void *f,
168 enum v4l2_priority *p);
169extern int cx25821_vidioc_s_priority(struct file *file, void *f,
170 enum v4l2_priority prio);
171
172extern int cx25821_vidioc_queryctrl(struct file *file, void *priv,
173 struct v4l2_queryctrl *qctrl);
174extern int cx25821_set_control(struct cx25821_dev *dev,
175 struct v4l2_control *ctrl, int chan_num);
176
177extern int cx25821_vidioc_cropcap(struct file *file, void *fh,
178 struct v4l2_cropcap *cropcap);
179extern int cx25821_vidioc_s_crop(struct file *file, void *priv,
180 struct v4l2_crop *crop);
181extern int cx25821_vidioc_g_crop(struct file *file, void *priv,
182 struct v4l2_crop *crop);
183
184extern int cx25821_vidioc_querystd(struct file *file, void *priv,
185 v4l2_std_id *norm);
186#endif
diff --git a/drivers/media/pci/cx25821/cx25821.h b/drivers/media/pci/cx25821/cx25821.h
new file mode 100644
index 000000000000..8a9c0c869412
--- /dev/null
+++ b/drivers/media/pci/cx25821/cx25821.h
@@ -0,0 +1,615 @@
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#ifndef CX25821_H_
25#define CX25821_H_
26
27#include <linux/pci.h>
28#include <linux/i2c.h>
29#include <linux/interrupt.h>
30#include <linux/delay.h>
31#include <linux/sched.h>
32#include <linux/kdev_t.h>
33
34#include <media/v4l2-common.h>
35#include <media/v4l2-device.h>
36#include <media/tuner.h>
37#include <media/tveeprom.h>
38#include <media/videobuf-dma-sg.h>
39#include <media/videobuf-dvb.h>
40
41#include "btcx-risc.h"
42#include "cx25821-reg.h"
43#include "cx25821-medusa-reg.h"
44#include "cx25821-sram.h"
45#include "cx25821-audio.h"
46#include "media/cx2341x.h"
47
48#include <linux/version.h>
49#include <linux/mutex.h>
50
51#define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
52
53#define UNSET (-1U)
54#define NO_SYNC_LINE (-1U)
55
56#define CX25821_MAXBOARDS 2
57
58#define TRUE 1
59#define FALSE 0
60#define LINE_SIZE_D1 1440
61
62/* Number of decoders and encoders */
63#define MAX_DECODERS 8
64#define MAX_ENCODERS 2
65#define QUAD_DECODERS 4
66#define MAX_CAMERAS 16
67
68/* Max number of inputs by card */
69#define MAX_CX25821_INPUT 8
70#define INPUT(nr) (&cx25821_boards[dev->board].input[nr])
71#define RESOURCE_VIDEO0 1
72#define RESOURCE_VIDEO1 2
73#define RESOURCE_VIDEO2 4
74#define RESOURCE_VIDEO3 8
75#define RESOURCE_VIDEO4 16
76#define RESOURCE_VIDEO5 32
77#define RESOURCE_VIDEO6 64
78#define RESOURCE_VIDEO7 128
79#define RESOURCE_VIDEO8 256
80#define RESOURCE_VIDEO9 512
81#define RESOURCE_VIDEO10 1024
82#define RESOURCE_VIDEO11 2048
83#define RESOURCE_VIDEO_IOCTL 4096
84
85#define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
86
87#define UNKNOWN_BOARD 0
88#define CX25821_BOARD 1
89
90/* Currently supported by the driver */
91#define CX25821_NORMS (\
92 V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
93 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
94 V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_H | \
95 V4L2_STD_PAL_Nc)
96
97#define CX25821_BOARD_CONEXANT_ATHENA10 1
98#define MAX_VID_CHANNEL_NUM 12
99#define VID_CHANNEL_NUM 8
100#define CX25821_NR_INPUT 2
101
102struct cx25821_fmt {
103 char *name;
104 u32 fourcc; /* v4l2 format id */
105 int depth;
106 int flags;
107 u32 cxformat;
108};
109
110struct cx25821_ctrl {
111 struct v4l2_queryctrl v;
112 u32 off;
113 u32 reg;
114 u32 mask;
115 u32 shift;
116};
117
118struct cx25821_tvnorm {
119 char *name;
120 v4l2_std_id id;
121 u32 cxiformat;
122 u32 cxoformat;
123};
124
125struct cx25821_fh {
126 struct cx25821_dev *dev;
127 enum v4l2_buf_type type;
128 int radio;
129 u32 resources;
130
131 enum v4l2_priority prio;
132
133 /* video overlay */
134 struct v4l2_window win;
135 struct v4l2_clip *clips;
136 unsigned int nclips;
137
138 /* video capture */
139 struct cx25821_fmt *fmt;
140 unsigned int width, height;
141 int channel_id;
142
143 /* vbi capture */
144 struct videobuf_queue vidq;
145 struct videobuf_queue vbiq;
146
147 /* H264 Encoder specifics ONLY */
148 struct videobuf_queue mpegq;
149 atomic_t v4l_reading;
150};
151
152enum cx25821_itype {
153 CX25821_VMUX_COMPOSITE = 1,
154 CX25821_VMUX_SVIDEO,
155 CX25821_VMUX_DEBUG,
156 CX25821_RADIO,
157};
158
159enum cx25821_src_sel_type {
160 CX25821_SRC_SEL_EXT_656_VIDEO = 0,
161 CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
162};
163
164/* buffer for one video frame */
165struct cx25821_buffer {
166 /* common v4l buffer stuff -- must be first */
167 struct videobuf_buffer vb;
168
169 /* cx25821 specific */
170 unsigned int bpl;
171 struct btcx_riscmem risc;
172 struct cx25821_fmt *fmt;
173 u32 count;
174};
175
176struct cx25821_input {
177 enum cx25821_itype type;
178 unsigned int vmux;
179 u32 gpio0, gpio1, gpio2, gpio3;
180};
181
182enum port {
183 CX25821_UNDEFINED = 0,
184 CX25821_RAW,
185 CX25821_264
186};
187
188struct cx25821_board {
189 const char *name;
190 enum port porta;
191 enum port portb;
192 enum port portc;
193 unsigned int tuner_type;
194 unsigned int radio_type;
195 unsigned char tuner_addr;
196 unsigned char radio_addr;
197
198 u32 clk_freq;
199 struct cx25821_input input[CX25821_NR_INPUT];
200};
201
202struct cx25821_subid {
203 u16 subvendor;
204 u16 subdevice;
205 u32 card;
206};
207
208struct cx25821_i2c {
209 struct cx25821_dev *dev;
210
211 int nr;
212
213 /* i2c i/o */
214 struct i2c_adapter i2c_adap;
215 struct i2c_client i2c_client;
216 u32 i2c_rc;
217
218 /* cx25821 registers used for raw addess */
219 u32 i2c_period;
220 u32 reg_ctrl;
221 u32 reg_stat;
222 u32 reg_addr;
223 u32 reg_rdata;
224 u32 reg_wdata;
225};
226
227struct cx25821_dmaqueue {
228 struct list_head active;
229 struct list_head queued;
230 struct timer_list timeout;
231 struct btcx_riscmem stopper;
232 u32 count;
233};
234
235struct cx25821_data {
236 struct cx25821_dev *dev;
237 struct sram_channel *channel;
238};
239
240struct cx25821_channel {
241 struct v4l2_prio_state prio;
242
243 int ctl_bright;
244 int ctl_contrast;
245 int ctl_hue;
246 int ctl_saturation;
247 struct cx25821_data timeout_data;
248
249 struct video_device *video_dev;
250 struct cx25821_dmaqueue vidq;
251
252 struct sram_channel *sram_channels;
253
254 struct mutex lock;
255 int resources;
256
257 int pixel_formats;
258 int use_cif_resolution;
259 int cif_width;
260};
261
262struct cx25821_dev {
263 struct list_head devlist;
264 atomic_t refcount;
265 struct v4l2_device v4l2_dev;
266
267 /* pci stuff */
268 struct pci_dev *pci;
269 unsigned char pci_rev, pci_lat;
270 int pci_bus, pci_slot;
271 u32 base_io_addr;
272 u32 __iomem *lmmio;
273 u8 __iomem *bmmio;
274 int pci_irqmask;
275 int hwrevision;
276
277 u32 clk_freq;
278
279 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
280 struct cx25821_i2c i2c_bus[3];
281
282 int nr;
283 struct mutex lock;
284
285 struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
286
287 /* board details */
288 unsigned int board;
289 char name[32];
290
291 /* Analog video */
292 u32 resources;
293 unsigned int input;
294 u32 tvaudio;
295 v4l2_std_id tvnorm;
296 unsigned int tuner_type;
297 unsigned char tuner_addr;
298 unsigned int radio_type;
299 unsigned char radio_addr;
300 unsigned int has_radio;
301 unsigned int videc_type;
302 unsigned char videc_addr;
303 unsigned short _max_num_decoders;
304
305 /* Analog Audio Upstream */
306 int _audio_is_running;
307 int _audiopixel_format;
308 int _is_first_audio_frame;
309 int _audiofile_status;
310 int _audio_lines_count;
311 int _audioframe_count;
312 int _audio_upstream_channel;
313 int _last_index_irq; /* The last interrupt index processed. */
314
315 __le32 *_risc_audio_jmp_addr;
316 __le32 *_risc_virt_start_addr;
317 __le32 *_risc_virt_addr;
318 dma_addr_t _risc_phys_addr;
319 dma_addr_t _risc_phys_start_addr;
320
321 unsigned int _audiorisc_size;
322 unsigned int _audiodata_buf_size;
323 __le32 *_audiodata_buf_virt_addr;
324 dma_addr_t _audiodata_buf_phys_addr;
325 char *_audiofilename;
326
327 /* V4l */
328 u32 freq;
329 struct video_device *vbi_dev;
330 struct video_device *radio_dev;
331 struct video_device *ioctl_dev;
332
333 spinlock_t slock;
334
335 /* Video Upstream */
336 int _line_size;
337 int _prog_cnt;
338 int _pixel_format;
339 int _is_first_frame;
340 int _is_running;
341 int _file_status;
342 int _lines_count;
343 int _frame_count;
344 int _channel_upstream_select;
345 unsigned int _risc_size;
346
347 __le32 *_dma_virt_start_addr;
348 __le32 *_dma_virt_addr;
349 dma_addr_t _dma_phys_addr;
350 dma_addr_t _dma_phys_start_addr;
351
352 unsigned int _data_buf_size;
353 __le32 *_data_buf_virt_addr;
354 dma_addr_t _data_buf_phys_addr;
355 char *_filename;
356 char *_defaultname;
357
358 int _line_size_ch2;
359 int _prog_cnt_ch2;
360 int _pixel_format_ch2;
361 int _is_first_frame_ch2;
362 int _is_running_ch2;
363 int _file_status_ch2;
364 int _lines_count_ch2;
365 int _frame_count_ch2;
366 int _channel2_upstream_select;
367 unsigned int _risc_size_ch2;
368
369 __le32 *_dma_virt_start_addr_ch2;
370 __le32 *_dma_virt_addr_ch2;
371 dma_addr_t _dma_phys_addr_ch2;
372 dma_addr_t _dma_phys_start_addr_ch2;
373
374 unsigned int _data_buf_size_ch2;
375 __le32 *_data_buf_virt_addr_ch2;
376 dma_addr_t _data_buf_phys_addr_ch2;
377 char *_filename_ch2;
378 char *_defaultname_ch2;
379
380 /* MPEG Encoder ONLY settings */
381 u32 cx23417_mailbox;
382 struct cx2341x_mpeg_params mpeg_params;
383 struct video_device *v4l_device;
384 atomic_t v4l_reader_count;
385 struct cx25821_tvnorm encodernorm;
386
387 u32 upstream_riscbuf_size;
388 u32 upstream_databuf_size;
389 u32 upstream_riscbuf_size_ch2;
390 u32 upstream_databuf_size_ch2;
391 u32 audio_upstream_riscbuf_size;
392 u32 audio_upstream_databuf_size;
393 int _isNTSC;
394 int _frame_index;
395 int _audioframe_index;
396 struct workqueue_struct *_irq_queues;
397 struct work_struct _irq_work_entry;
398 struct workqueue_struct *_irq_queues_ch2;
399 struct work_struct _irq_work_entry_ch2;
400 struct workqueue_struct *_irq_audio_queues;
401 struct work_struct _audio_work_entry;
402 char *input_filename;
403 char *input_filename_ch2;
404 int _frame_index_ch2;
405 int _isNTSC_ch2;
406 char *vid_stdname_ch2;
407 int pixel_format_ch2;
408 int channel_select_ch2;
409 int command_ch2;
410 char *input_audiofilename;
411 char *vid_stdname;
412 int pixel_format;
413 int channel_select;
414 int command;
415 int channel_opened;
416};
417
418struct upstream_user_struct {
419 char *input_filename;
420 char *vid_stdname;
421 int pixel_format;
422 int channel_select;
423 int command;
424};
425
426struct downstream_user_struct {
427 char *vid_stdname;
428 int pixel_format;
429 int cif_resolution_enable;
430 int cif_width;
431 int decoder_select;
432 int command;
433 int reg_address;
434 int reg_data;
435};
436
437extern struct upstream_user_struct *up_data;
438
439static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
440{
441 return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
442}
443
444#define cx25821_call_all(dev, o, f, args...) \
445 v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
446
447extern struct list_head cx25821_devlist;
448extern struct mutex cx25821_devlist_mutex;
449
450extern struct cx25821_board cx25821_boards[];
451extern struct cx25821_subid cx25821_subids[];
452
453#define SRAM_CH00 0 /* Video A */
454#define SRAM_CH01 1 /* Video B */
455#define SRAM_CH02 2 /* Video C */
456#define SRAM_CH03 3 /* Video D */
457#define SRAM_CH04 4 /* Video E */
458#define SRAM_CH05 5 /* Video F */
459#define SRAM_CH06 6 /* Video G */
460#define SRAM_CH07 7 /* Video H */
461
462#define SRAM_CH08 8 /* Audio A */
463#define SRAM_CH09 9 /* Video Upstream I */
464#define SRAM_CH10 10 /* Video Upstream J */
465#define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
466
467#define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
468#define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
469#define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
470#define VIDEO_IOCTL_CH 11
471
472struct sram_channel {
473 char *name;
474 u32 i;
475 u32 cmds_start;
476 u32 ctrl_start;
477 u32 cdt;
478 u32 fifo_start;
479 u32 fifo_size;
480 u32 ptr1_reg;
481 u32 ptr2_reg;
482 u32 cnt1_reg;
483 u32 cnt2_reg;
484 u32 int_msk;
485 u32 int_stat;
486 u32 int_mstat;
487 u32 dma_ctl;
488 u32 gpcnt_ctl;
489 u32 gpcnt;
490 u32 aud_length;
491 u32 aud_cfg;
492 u32 fld_aud_fifo_en;
493 u32 fld_aud_risc_en;
494
495 /* For Upstream Video */
496 u32 vid_fmt_ctl;
497 u32 vid_active_ctl1;
498 u32 vid_active_ctl2;
499 u32 vid_cdt_size;
500
501 u32 vip_ctl;
502 u32 pix_frmt;
503 u32 jumponly;
504 u32 irq_bit;
505};
506extern struct sram_channel cx25821_sram_channels[];
507
508#define STATUS_SUCCESS 0
509#define STATUS_UNSUCCESSFUL -1
510
511#define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
512#define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
513
514#define cx_andor(reg, mask, value) \
515 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
516 ((value) & (mask)), dev->lmmio+((reg)>>2))
517
518#define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
519#define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
520
521#define Set_GPIO_Bit(Bit) (1 << Bit)
522#define Clear_GPIO_Bit(Bit) (~(1 << Bit))
523
524#define CX25821_ERR(fmt, args...) \
525 pr_err("(%d): " fmt, dev->board, ##args)
526#define CX25821_WARN(fmt, args...) \
527 pr_warn("(%d): " fmt, dev->board, ##args)
528#define CX25821_INFO(fmt, args...) \
529 pr_info("(%d): " fmt, dev->board, ##args)
530
531extern int cx25821_i2c_register(struct cx25821_i2c *bus);
532extern void cx25821_card_setup(struct cx25821_dev *dev);
533extern int cx25821_ir_init(struct cx25821_dev *dev);
534extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
535extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
536extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
537extern void cx25821_gpio_init(struct cx25821_dev *dev);
538extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
539 int pin_number, int pin_logic_value);
540
541extern int medusa_video_init(struct cx25821_dev *dev);
542extern int medusa_set_videostandard(struct cx25821_dev *dev);
543extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
544 int decoder_select);
545extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
546 int decoder);
547extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
548 int decoder);
549extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
550extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
551 int decoder);
552
553extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
554 struct sram_channel *ch, unsigned int bpl,
555 u32 risc);
556
557extern int cx25821_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
558 struct scatterlist *sglist,
559 unsigned int top_offset,
560 unsigned int bottom_offset,
561 unsigned int bpl,
562 unsigned int padding, unsigned int lines);
563extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
564 struct btcx_riscmem *risc,
565 struct scatterlist *sglist,
566 unsigned int bpl,
567 unsigned int lines, unsigned int lpi);
568extern void cx25821_free_buffer(struct videobuf_queue *q,
569 struct cx25821_buffer *buf);
570extern int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
571 u32 reg, u32 mask, u32 value);
572extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
573 struct sram_channel *ch);
574extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
575 struct sram_channel *ch);
576
577extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
578extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
579 int len, u32 bits, u32 mask);
580extern void cx25821_dev_unregister(struct cx25821_dev *dev);
581extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
582 struct sram_channel *ch,
583 unsigned int bpl, u32 risc);
584
585extern int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev,
586 int channel_select, int pixel_format);
587extern int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev,
588 int channel_select, int pixel_format);
589extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
590 int channel_select);
591extern void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev);
592extern void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev);
593extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
594extern void cx25821_start_upstream_video_ch1(struct cx25821_dev *dev,
595 struct upstream_user_struct
596 *up_data);
597extern void cx25821_start_upstream_video_ch2(struct cx25821_dev *dev,
598 struct upstream_user_struct
599 *up_data);
600extern void cx25821_start_upstream_audio(struct cx25821_dev *dev,
601 struct upstream_user_struct *up_data);
602extern void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev);
603extern void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev);
604extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
605extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
606 struct sram_channel *ch,
607 unsigned int bpl, u32 risc);
608extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
609 u32 format);
610extern void cx25821_videoioctl_unregister(struct cx25821_dev *dev);
611extern struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
612 struct pci_dev *pci,
613 struct video_device *template,
614 char *type);
615#endif
diff --git a/drivers/media/pci/cx88/Kconfig b/drivers/media/pci/cx88/Kconfig
new file mode 100644
index 000000000000..3598dc087b08
--- /dev/null
+++ b/drivers/media/pci/cx88/Kconfig
@@ -0,0 +1,86 @@
1config VIDEO_CX88
2 tristate "Conexant 2388x (bt878 successor) support"
3 depends on VIDEO_DEV && PCI && I2C && RC_CORE
4 select I2C_ALGOBIT
5 select VIDEO_BTCX
6 select VIDEOBUF_DMA_SG
7 select VIDEO_TUNER
8 select VIDEO_TVEEPROM
9 select VIDEO_WM8775 if VIDEO_HELPER_CHIPS_AUTO
10 ---help---
11 This is a video4linux driver for Conexant 2388x based
12 TV cards.
13
14 To compile this driver as a module, choose M here: the
15 module will be called cx8800
16
17config VIDEO_CX88_ALSA
18 tristate "Conexant 2388x DMA audio support"
19 depends on VIDEO_CX88 && SND
20 select SND_PCM
21 ---help---
22 This is a video4linux driver for direct (DMA) audio on
23 Conexant 2388x based TV cards using ALSA.
24
25 It only works with boards with function 01 enabled.
26 To check if your board supports, use lspci -n.
27 If supported, you should see 14f1:8801 or 14f1:8811
28 PCI device.
29
30 To compile this driver as a module, choose M here: the
31 module will be called cx88-alsa.
32
33config VIDEO_CX88_BLACKBIRD
34 tristate "Blackbird MPEG encoder support (cx2388x + cx23416)"
35 depends on VIDEO_CX88
36 select VIDEO_CX2341X
37 ---help---
38 This adds support for MPEG encoder cards based on the
39 Blackbird reference design, using the Conexant 2388x
40 and 23416 chips.
41
42 To compile this driver as a module, choose M here: the
43 module will be called cx88-blackbird.
44
45config VIDEO_CX88_DVB
46 tristate "DVB/ATSC Support for cx2388x based TV cards"
47 depends on VIDEO_CX88 && DVB_CORE
48 select VIDEOBUF_DVB
49 select DVB_PLL if !DVB_FE_CUSTOMISE
50 select DVB_MT352 if !DVB_FE_CUSTOMISE
51 select DVB_ZL10353 if !DVB_FE_CUSTOMISE
52 select DVB_OR51132 if !DVB_FE_CUSTOMISE
53 select DVB_CX22702 if !DVB_FE_CUSTOMISE
54 select DVB_LGDT330X if !DVB_FE_CUSTOMISE
55 select DVB_NXT200X if !DVB_FE_CUSTOMISE
56 select DVB_CX24123 if !DVB_FE_CUSTOMISE
57 select DVB_ISL6421 if !DVB_FE_CUSTOMISE
58 select DVB_S5H1411 if !DVB_FE_CUSTOMISE
59 select DVB_CX24116 if !DVB_FE_CUSTOMISE
60 select DVB_STV0299 if !DVB_FE_CUSTOMISE
61 select DVB_STV0288 if !DVB_FE_CUSTOMISE
62 select DVB_STB6000 if !DVB_FE_CUSTOMISE
63 select DVB_STV0900 if !DVB_FE_CUSTOMISE
64 select DVB_STB6100 if !DVB_FE_CUSTOMISE
65 select MEDIA_TUNER_SIMPLE if !MEDIA_TUNER_CUSTOMISE
66 ---help---
67 This adds support for DVB/ATSC cards based on the
68 Conexant 2388x chip.
69
70 To compile this driver as a module, choose M here: the
71 module will be called cx88-dvb.
72
73config VIDEO_CX88_VP3054
74 tristate "VP-3054 Secondary I2C Bus Support"
75 default m
76 depends on VIDEO_CX88_DVB && DVB_MT352
77 ---help---
78 This adds DVB-T support for cards based on the
79 Conexant 2388x chip and the MT352 demodulator,
80 which also require support for the VP-3054
81 Secondary I2C bus, such at DNTV Live! DVB-T Pro.
82
83config VIDEO_CX88_MPEG
84 tristate
85 depends on VIDEO_CX88_DVB || VIDEO_CX88_BLACKBIRD
86 default y
diff --git a/drivers/media/pci/cx88/Makefile b/drivers/media/pci/cx88/Makefile
new file mode 100644
index 000000000000..884b4cdd8ff0
--- /dev/null
+++ b/drivers/media/pci/cx88/Makefile
@@ -0,0 +1,16 @@
1cx88xx-objs := cx88-cards.o cx88-core.o cx88-i2c.o cx88-tvaudio.o \
2 cx88-dsp.o cx88-input.o
3cx8800-objs := cx88-video.o cx88-vbi.o
4cx8802-objs := cx88-mpeg.o
5
6obj-$(CONFIG_VIDEO_CX88) += cx88xx.o cx8800.o
7obj-$(CONFIG_VIDEO_CX88_MPEG) += cx8802.o
8obj-$(CONFIG_VIDEO_CX88_ALSA) += cx88-alsa.o
9obj-$(CONFIG_VIDEO_CX88_BLACKBIRD) += cx88-blackbird.o
10obj-$(CONFIG_VIDEO_CX88_DVB) += cx88-dvb.o
11obj-$(CONFIG_VIDEO_CX88_VP3054) += cx88-vp3054-i2c.o
12
13ccflags-y += -Idrivers/media/video
14ccflags-y += -Idrivers/media/tuners
15ccflags-y += -Idrivers/media/dvb-core
16ccflags-y += -Idrivers/media/dvb-frontends
diff --git a/drivers/media/pci/cx88/cx88-alsa.c b/drivers/media/pci/cx88/cx88-alsa.c
new file mode 100644
index 000000000000..3aa6856ead3b
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-alsa.c
@@ -0,0 +1,975 @@
1/*
2 *
3 * Support for audio capture
4 * PCI function #1 of the cx2388x.
5 *
6 * (c) 2007 Trent Piepho <xyzzy@speakeasy.org>
7 * (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org>
8 * (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org>
10 * Based on dummy.c by Jaroslav Kysela <perex@perex.cz>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/device.h>
30#include <linux/interrupt.h>
31#include <linux/vmalloc.h>
32#include <linux/dma-mapping.h>
33#include <linux/pci.h>
34#include <linux/slab.h>
35
36#include <asm/delay.h>
37#include <sound/core.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/control.h>
41#include <sound/initval.h>
42#include <sound/tlv.h>
43#include <media/wm8775.h>
44
45#include "cx88.h"
46#include "cx88-reg.h"
47
48#define dprintk(level,fmt, arg...) if (debug >= level) \
49 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg)
50
51#define dprintk_core(level,fmt, arg...) if (debug >= level) \
52 printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg)
53
54/****************************************************************************
55 Data type declarations - Can be moded to a header file later
56 ****************************************************************************/
57
58struct cx88_audio_buffer {
59 unsigned int bpl;
60 struct btcx_riscmem risc;
61 struct videobuf_dmabuf dma;
62};
63
64struct cx88_audio_dev {
65 struct cx88_core *core;
66 struct cx88_dmaqueue q;
67
68 /* pci i/o */
69 struct pci_dev *pci;
70
71 /* audio controls */
72 int irq;
73
74 struct snd_card *card;
75
76 spinlock_t reg_lock;
77 atomic_t count;
78
79 unsigned int dma_size;
80 unsigned int period_size;
81 unsigned int num_periods;
82
83 struct videobuf_dmabuf *dma_risc;
84
85 struct cx88_audio_buffer *buf;
86
87 struct snd_pcm_substream *substream;
88};
89typedef struct cx88_audio_dev snd_cx88_card_t;
90
91
92
93/****************************************************************************
94 Module global static vars
95 ****************************************************************************/
96
97static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
98static const char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
99static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
100
101module_param_array(enable, bool, NULL, 0444);
102MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
103
104module_param_array(index, int, NULL, 0444);
105MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s).");
106
107
108/****************************************************************************
109 Module macros
110 ****************************************************************************/
111
112MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards");
113MODULE_AUTHOR("Ricardo Cerqueira");
114MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
115MODULE_LICENSE("GPL");
116MODULE_VERSION(CX88_VERSION);
117
118MODULE_SUPPORTED_DEVICE("{{Conexant,23881},"
119 "{{Conexant,23882},"
120 "{{Conexant,23883}");
121static unsigned int debug;
122module_param(debug,int,0644);
123MODULE_PARM_DESC(debug,"enable debug messages");
124
125/****************************************************************************
126 Module specific funtions
127 ****************************************************************************/
128
129/*
130 * BOARD Specific: Sets audio DMA
131 */
132
133static int _cx88_start_audio_dma(snd_cx88_card_t *chip)
134{
135 struct cx88_audio_buffer *buf = chip->buf;
136 struct cx88_core *core=chip->core;
137 const struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25];
138
139 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
140 cx_clear(MO_AUD_DMACNTRL, 0x11);
141
142 /* setup fifo + format - out channel */
143 cx88_sram_channel_setup(chip->core, audio_ch, buf->bpl, buf->risc.dma);
144
145 /* sets bpl size */
146 cx_write(MO_AUDD_LNGTH, buf->bpl);
147
148 /* reset counter */
149 cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
150 atomic_set(&chip->count, 0);
151
152 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
153 "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start + 8)>>1,
154 chip->num_periods, buf->bpl * chip->num_periods);
155
156 /* Enables corresponding bits at AUD_INT_STAT */
157 cx_write(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
158 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
159
160 /* Clean any pending interrupt bits already set */
161 cx_write(MO_AUD_INTSTAT, ~0);
162
163 /* enable audio irqs */
164 cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT);
165
166 /* start dma */
167 cx_set(MO_DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
168 cx_set(MO_AUD_DMACNTRL, 0x11); /* audio downstream FIFO and RISC enable */
169
170 if (debug)
171 cx88_sram_channel_dump(chip->core, audio_ch);
172
173 return 0;
174}
175
176/*
177 * BOARD Specific: Resets audio DMA
178 */
179static int _cx88_stop_audio_dma(snd_cx88_card_t *chip)
180{
181 struct cx88_core *core=chip->core;
182 dprintk(1, "Stopping audio DMA\n");
183
184 /* stop dma */
185 cx_clear(MO_AUD_DMACNTRL, 0x11);
186
187 /* disable irqs */
188 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
189 cx_clear(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
190 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
191
192 if (debug)
193 cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]);
194
195 return 0;
196}
197
198#define MAX_IRQ_LOOP 50
199
200/*
201 * BOARD Specific: IRQ dma bits
202 */
203static const char *cx88_aud_irqs[32] = {
204 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
205 NULL, /* reserved */
206 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
207 NULL, /* reserved */
208 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
209 NULL, /* reserved */
210 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
211 NULL, /* reserved */
212 "opc_err", "par_err", "rip_err", /* 16-18 */
213 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
214};
215
216/*
217 * BOARD Specific: Threats IRQ audio specific calls
218 */
219static void cx8801_aud_irq(snd_cx88_card_t *chip)
220{
221 struct cx88_core *core = chip->core;
222 u32 status, mask;
223
224 status = cx_read(MO_AUD_INTSTAT);
225 mask = cx_read(MO_AUD_INTMSK);
226 if (0 == (status & mask))
227 return;
228 cx_write(MO_AUD_INTSTAT, status);
229 if (debug > 1 || (status & mask & ~0xff))
230 cx88_print_irqbits(core->name, "irq aud",
231 cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs),
232 status, mask);
233 /* risc op code error */
234 if (status & AUD_INT_OPC_ERR) {
235 printk(KERN_WARNING "%s/1: Audio risc op code error\n",core->name);
236 cx_clear(MO_AUD_DMACNTRL, 0x11);
237 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
238 }
239 if (status & AUD_INT_DN_SYNC) {
240 dprintk(1, "Downstream sync error\n");
241 cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
242 return;
243 }
244 /* risc1 downstream */
245 if (status & AUD_INT_DN_RISCI1) {
246 atomic_set(&chip->count, cx_read(MO_AUDD_GPCNT));
247 snd_pcm_period_elapsed(chip->substream);
248 }
249 /* FIXME: Any other status should deserve a special handling? */
250}
251
252/*
253 * BOARD Specific: Handles IRQ calls
254 */
255static irqreturn_t cx8801_irq(int irq, void *dev_id)
256{
257 snd_cx88_card_t *chip = dev_id;
258 struct cx88_core *core = chip->core;
259 u32 status;
260 int loop, handled = 0;
261
262 for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
263 status = cx_read(MO_PCI_INTSTAT) &
264 (core->pci_irqmask | PCI_INT_AUDINT);
265 if (0 == status)
266 goto out;
267 dprintk(3, "cx8801_irq loop %d/%d, status %x\n",
268 loop, MAX_IRQ_LOOP, status);
269 handled = 1;
270 cx_write(MO_PCI_INTSTAT, status);
271
272 if (status & core->pci_irqmask)
273 cx88_core_irq(core, status);
274 if (status & PCI_INT_AUDINT)
275 cx8801_aud_irq(chip);
276 }
277
278 if (MAX_IRQ_LOOP == loop) {
279 printk(KERN_ERR
280 "%s/1: IRQ loop detected, disabling interrupts\n",
281 core->name);
282 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
283 }
284
285 out:
286 return IRQ_RETVAL(handled);
287}
288
289
290static int dsp_buffer_free(snd_cx88_card_t *chip)
291{
292 BUG_ON(!chip->dma_size);
293
294 dprintk(2,"Freeing buffer\n");
295 videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
296 videobuf_dma_free(chip->dma_risc);
297 btcx_riscmem_free(chip->pci,&chip->buf->risc);
298 kfree(chip->buf);
299
300 chip->dma_risc = NULL;
301 chip->dma_size = 0;
302
303 return 0;
304}
305
306/****************************************************************************
307 ALSA PCM Interface
308 ****************************************************************************/
309
310/*
311 * Digital hardware definition
312 */
313#define DEFAULT_FIFO_SIZE 4096
314static const struct snd_pcm_hardware snd_cx88_digital_hw = {
315 .info = SNDRV_PCM_INFO_MMAP |
316 SNDRV_PCM_INFO_INTERLEAVED |
317 SNDRV_PCM_INFO_BLOCK_TRANSFER |
318 SNDRV_PCM_INFO_MMAP_VALID,
319 .formats = SNDRV_PCM_FMTBIT_S16_LE,
320
321 .rates = SNDRV_PCM_RATE_48000,
322 .rate_min = 48000,
323 .rate_max = 48000,
324 .channels_min = 2,
325 .channels_max = 2,
326 /* Analog audio output will be full of clicks and pops if there
327 are not exactly four lines in the SRAM FIFO buffer. */
328 .period_bytes_min = DEFAULT_FIFO_SIZE/4,
329 .period_bytes_max = DEFAULT_FIFO_SIZE/4,
330 .periods_min = 1,
331 .periods_max = 1024,
332 .buffer_bytes_max = (1024*1024),
333};
334
335/*
336 * audio pcm capture open callback
337 */
338static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
339{
340 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
341 struct snd_pcm_runtime *runtime = substream->runtime;
342 int err;
343
344 if (!chip) {
345 printk(KERN_ERR "BUG: cx88 can't find device struct."
346 " Can't proceed with open\n");
347 return -ENODEV;
348 }
349
350 err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
351 if (err < 0)
352 goto _error;
353
354 chip->substream = substream;
355
356 runtime->hw = snd_cx88_digital_hw;
357
358 if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) {
359 unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4;
360 bpl &= ~7; /* must be multiple of 8 */
361 runtime->hw.period_bytes_min = bpl;
362 runtime->hw.period_bytes_max = bpl;
363 }
364
365 return 0;
366_error:
367 dprintk(1,"Error opening PCM!\n");
368 return err;
369}
370
371/*
372 * audio close callback
373 */
374static int snd_cx88_close(struct snd_pcm_substream *substream)
375{
376 return 0;
377}
378
379/*
380 * hw_params callback
381 */
382static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
383 struct snd_pcm_hw_params * hw_params)
384{
385 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
386 struct videobuf_dmabuf *dma;
387
388 struct cx88_audio_buffer *buf;
389 int ret;
390
391 if (substream->runtime->dma_area) {
392 dsp_buffer_free(chip);
393 substream->runtime->dma_area = NULL;
394 }
395
396 chip->period_size = params_period_bytes(hw_params);
397 chip->num_periods = params_periods(hw_params);
398 chip->dma_size = chip->period_size * params_periods(hw_params);
399
400 BUG_ON(!chip->dma_size);
401 BUG_ON(chip->num_periods & (chip->num_periods-1));
402
403 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
404 if (NULL == buf)
405 return -ENOMEM;
406
407 buf->bpl = chip->period_size;
408
409 dma = &buf->dma;
410 videobuf_dma_init(dma);
411 ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
412 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
413 if (ret < 0)
414 goto error;
415
416 ret = videobuf_dma_map(&chip->pci->dev, dma);
417 if (ret < 0)
418 goto error;
419
420 ret = cx88_risc_databuffer(chip->pci, &buf->risc, dma->sglist,
421 chip->period_size, chip->num_periods, 1);
422 if (ret < 0)
423 goto error;
424
425 /* Loop back to start of program */
426 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
427 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
428
429 chip->buf = buf;
430 chip->dma_risc = dma;
431
432 substream->runtime->dma_area = chip->dma_risc->vaddr;
433 substream->runtime->dma_bytes = chip->dma_size;
434 substream->runtime->dma_addr = 0;
435 return 0;
436
437error:
438 kfree(buf);
439 return ret;
440}
441
442/*
443 * hw free callback
444 */
445static int snd_cx88_hw_free(struct snd_pcm_substream * substream)
446{
447
448 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
449
450 if (substream->runtime->dma_area) {
451 dsp_buffer_free(chip);
452 substream->runtime->dma_area = NULL;
453 }
454
455 return 0;
456}
457
458/*
459 * prepare callback
460 */
461static int snd_cx88_prepare(struct snd_pcm_substream *substream)
462{
463 return 0;
464}
465
466/*
467 * trigger callback
468 */
469static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
470{
471 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
472 int err;
473
474 /* Local interrupts are already disabled by ALSA */
475 spin_lock(&chip->reg_lock);
476
477 switch (cmd) {
478 case SNDRV_PCM_TRIGGER_START:
479 err=_cx88_start_audio_dma(chip);
480 break;
481 case SNDRV_PCM_TRIGGER_STOP:
482 err=_cx88_stop_audio_dma(chip);
483 break;
484 default:
485 err=-EINVAL;
486 break;
487 }
488
489 spin_unlock(&chip->reg_lock);
490
491 return err;
492}
493
494/*
495 * pointer callback
496 */
497static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
498{
499 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
500 struct snd_pcm_runtime *runtime = substream->runtime;
501 u16 count;
502
503 count = atomic_read(&chip->count);
504
505// dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __func__,
506// count, new, count & (runtime->periods-1),
507// runtime->period_size * (count & (runtime->periods-1)));
508 return runtime->period_size * (count & (runtime->periods-1));
509}
510
511/*
512 * page callback (needed for mmap)
513 */
514static struct page *snd_cx88_page(struct snd_pcm_substream *substream,
515 unsigned long offset)
516{
517 void *pageptr = substream->runtime->dma_area + offset;
518 return vmalloc_to_page(pageptr);
519}
520
521/*
522 * operators
523 */
524static struct snd_pcm_ops snd_cx88_pcm_ops = {
525 .open = snd_cx88_pcm_open,
526 .close = snd_cx88_close,
527 .ioctl = snd_pcm_lib_ioctl,
528 .hw_params = snd_cx88_hw_params,
529 .hw_free = snd_cx88_hw_free,
530 .prepare = snd_cx88_prepare,
531 .trigger = snd_cx88_card_trigger,
532 .pointer = snd_cx88_pointer,
533 .page = snd_cx88_page,
534};
535
536/*
537 * create a PCM device
538 */
539static int __devinit snd_cx88_pcm(snd_cx88_card_t *chip, int device, const char *name)
540{
541 int err;
542 struct snd_pcm *pcm;
543
544 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
545 if (err < 0)
546 return err;
547 pcm->private_data = chip;
548 strcpy(pcm->name, name);
549 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
550
551 return 0;
552}
553
554/****************************************************************************
555 CONTROL INTERFACE
556 ****************************************************************************/
557static int snd_cx88_volume_info(struct snd_kcontrol *kcontrol,
558 struct snd_ctl_elem_info *info)
559{
560 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
561 info->count = 2;
562 info->value.integer.min = 0;
563 info->value.integer.max = 0x3f;
564
565 return 0;
566}
567
568static int snd_cx88_volume_get(struct snd_kcontrol *kcontrol,
569 struct snd_ctl_elem_value *value)
570{
571 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
572 struct cx88_core *core=chip->core;
573 int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f),
574 bal = cx_read(AUD_BAL_CTL);
575
576 value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol;
577 vol -= (bal & 0x3f);
578 value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol;
579
580 return 0;
581}
582
583static void snd_cx88_wm8775_volume_put(struct snd_kcontrol *kcontrol,
584 struct snd_ctl_elem_value *value)
585{
586 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
587 struct cx88_core *core = chip->core;
588 int left = value->value.integer.value[0];
589 int right = value->value.integer.value[1];
590 int v, b;
591
592 /* Pass volume & balance onto any WM8775 */
593 if (left >= right) {
594 v = left << 10;
595 b = left ? (0x8000 * right) / left : 0x8000;
596 } else {
597 v = right << 10;
598 b = right ? 0xffff - (0x8000 * left) / right : 0x8000;
599 }
600 wm8775_s_ctrl(core, V4L2_CID_AUDIO_VOLUME, v);
601 wm8775_s_ctrl(core, V4L2_CID_AUDIO_BALANCE, b);
602}
603
604/* OK - TODO: test it */
605static int snd_cx88_volume_put(struct snd_kcontrol *kcontrol,
606 struct snd_ctl_elem_value *value)
607{
608 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
609 struct cx88_core *core=chip->core;
610 int left, right, v, b;
611 int changed = 0;
612 u32 old;
613
614 if (core->board.audio_chip == V4L2_IDENT_WM8775)
615 snd_cx88_wm8775_volume_put(kcontrol, value);
616
617 left = value->value.integer.value[0] & 0x3f;
618 right = value->value.integer.value[1] & 0x3f;
619 b = right - left;
620 if (b < 0) {
621 v = 0x3f - left;
622 b = (-b) | 0x40;
623 } else {
624 v = 0x3f - right;
625 }
626 /* Do we really know this will always be called with IRQs on? */
627 spin_lock_irq(&chip->reg_lock);
628 old = cx_read(AUD_VOL_CTL);
629 if (v != (old & 0x3f)) {
630 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, (old & ~0x3f) | v);
631 changed = 1;
632 }
633 if ((cx_read(AUD_BAL_CTL) & 0x7f) != b) {
634 cx_write(AUD_BAL_CTL, b);
635 changed = 1;
636 }
637 spin_unlock_irq(&chip->reg_lock);
638
639 return changed;
640}
641
642static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0);
643
644static const struct snd_kcontrol_new snd_cx88_volume = {
645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
646 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
647 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
648 .name = "Analog-TV Volume",
649 .info = snd_cx88_volume_info,
650 .get = snd_cx88_volume_get,
651 .put = snd_cx88_volume_put,
652 .tlv.p = snd_cx88_db_scale,
653};
654
655static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol,
656 struct snd_ctl_elem_value *value)
657{
658 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
659 struct cx88_core *core = chip->core;
660 u32 bit = kcontrol->private_value;
661
662 value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit);
663 return 0;
664}
665
666static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol,
667 struct snd_ctl_elem_value *value)
668{
669 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
670 struct cx88_core *core = chip->core;
671 u32 bit = kcontrol->private_value;
672 int ret = 0;
673 u32 vol;
674
675 spin_lock_irq(&chip->reg_lock);
676 vol = cx_read(AUD_VOL_CTL);
677 if (value->value.integer.value[0] != !(vol & bit)) {
678 vol ^= bit;
679 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, vol);
680 /* Pass mute onto any WM8775 */
681 if ((core->board.audio_chip == V4L2_IDENT_WM8775) &&
682 ((1<<6) == bit))
683 wm8775_s_ctrl(core, V4L2_CID_AUDIO_MUTE, 0 != (vol & bit));
684 ret = 1;
685 }
686 spin_unlock_irq(&chip->reg_lock);
687 return ret;
688}
689
690static const struct snd_kcontrol_new snd_cx88_dac_switch = {
691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
692 .name = "Audio-Out Switch",
693 .info = snd_ctl_boolean_mono_info,
694 .get = snd_cx88_switch_get,
695 .put = snd_cx88_switch_put,
696 .private_value = (1<<8),
697};
698
699static const struct snd_kcontrol_new snd_cx88_source_switch = {
700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
701 .name = "Analog-TV Switch",
702 .info = snd_ctl_boolean_mono_info,
703 .get = snd_cx88_switch_get,
704 .put = snd_cx88_switch_put,
705 .private_value = (1<<6),
706};
707
708static int snd_cx88_alc_get(struct snd_kcontrol *kcontrol,
709 struct snd_ctl_elem_value *value)
710{
711 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
712 struct cx88_core *core = chip->core;
713 s32 val;
714
715 val = wm8775_g_ctrl(core, V4L2_CID_AUDIO_LOUDNESS);
716 value->value.integer.value[0] = val ? 1 : 0;
717 return 0;
718}
719
720static int snd_cx88_alc_put(struct snd_kcontrol *kcontrol,
721 struct snd_ctl_elem_value *value)
722{
723 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
724 struct cx88_core *core = chip->core;
725 struct v4l2_control client_ctl;
726
727 memset(&client_ctl, 0, sizeof(client_ctl));
728 client_ctl.value = 0 != value->value.integer.value[0];
729 client_ctl.id = V4L2_CID_AUDIO_LOUDNESS;
730 call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
731
732 return 0;
733}
734
735static struct snd_kcontrol_new snd_cx88_alc_switch = {
736 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
737 .name = "Line-In ALC Switch",
738 .info = snd_ctl_boolean_mono_info,
739 .get = snd_cx88_alc_get,
740 .put = snd_cx88_alc_put,
741};
742
743/****************************************************************************
744 Basic Flow for Sound Devices
745 ****************************************************************************/
746
747/*
748 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
749 * Only boards with eeprom and byte 1 at eeprom=1 have it
750 */
751
752static const struct pci_device_id cx88_audio_pci_tbl[] __devinitdata = {
753 {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
754 {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
755 {0, }
756};
757MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
758
759/*
760 * Chip-specific destructor
761 */
762
763static int snd_cx88_free(snd_cx88_card_t *chip)
764{
765
766 if (chip->irq >= 0)
767 free_irq(chip->irq, chip);
768
769 cx88_core_put(chip->core,chip->pci);
770
771 pci_disable_device(chip->pci);
772 return 0;
773}
774
775/*
776 * Component Destructor
777 */
778static void snd_cx88_dev_free(struct snd_card * card)
779{
780 snd_cx88_card_t *chip = card->private_data;
781
782 snd_cx88_free(chip);
783}
784
785
786/*
787 * Alsa Constructor - Component probe
788 */
789
790static int devno;
791static int __devinit snd_cx88_create(struct snd_card *card,
792 struct pci_dev *pci,
793 snd_cx88_card_t **rchip,
794 struct cx88_core **core_ptr)
795{
796 snd_cx88_card_t *chip;
797 struct cx88_core *core;
798 int err;
799 unsigned char pci_lat;
800
801 *rchip = NULL;
802
803 err = pci_enable_device(pci);
804 if (err < 0)
805 return err;
806
807 pci_set_master(pci);
808
809 chip = card->private_data;
810
811 core = cx88_core_get(pci);
812 if (NULL == core) {
813 err = -EINVAL;
814 return err;
815 }
816
817 if (!pci_dma_supported(pci,DMA_BIT_MASK(32))) {
818 dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
819 err = -EIO;
820 cx88_core_put(core, pci);
821 return err;
822 }
823
824
825 /* pci init */
826 chip->card = card;
827 chip->pci = pci;
828 chip->irq = -1;
829 spin_lock_init(&chip->reg_lock);
830
831 chip->core = core;
832
833 /* get irq */
834 err = request_irq(chip->pci->irq, cx8801_irq,
835 IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
836 if (err < 0) {
837 dprintk(0, "%s: can't get IRQ %d\n",
838 chip->core->name, chip->pci->irq);
839 return err;
840 }
841
842 /* print pci info */
843 pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
844
845 dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
846 "latency: %d, mmio: 0x%llx\n", core->name, devno,
847 pci_name(pci), pci->revision, pci->irq,
848 pci_lat, (unsigned long long)pci_resource_start(pci,0));
849
850 chip->irq = pci->irq;
851 synchronize_irq(chip->irq);
852
853 snd_card_set_dev(card, &pci->dev);
854
855 *rchip = chip;
856 *core_ptr = core;
857
858 return 0;
859}
860
861static int __devinit cx88_audio_initdev(struct pci_dev *pci,
862 const struct pci_device_id *pci_id)
863{
864 struct snd_card *card;
865 snd_cx88_card_t *chip;
866 struct cx88_core *core = NULL;
867 int err;
868
869 if (devno >= SNDRV_CARDS)
870 return (-ENODEV);
871
872 if (!enable[devno]) {
873 ++devno;
874 return (-ENOENT);
875 }
876
877 err = snd_card_create(index[devno], id[devno], THIS_MODULE,
878 sizeof(snd_cx88_card_t), &card);
879 if (err < 0)
880 return err;
881
882 card->private_free = snd_cx88_dev_free;
883
884 err = snd_cx88_create(card, pci, &chip, &core);
885 if (err < 0)
886 goto error;
887
888 err = snd_cx88_pcm(chip, 0, "CX88 Digital");
889 if (err < 0)
890 goto error;
891
892 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip));
893 if (err < 0)
894 goto error;
895 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip));
896 if (err < 0)
897 goto error;
898 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip));
899 if (err < 0)
900 goto error;
901
902 /* If there's a wm8775 then add a Line-In ALC switch */
903 if (core->board.audio_chip == V4L2_IDENT_WM8775)
904 snd_ctl_add(card, snd_ctl_new1(&snd_cx88_alc_switch, chip));
905
906 strcpy (card->driver, "CX88x");
907 sprintf(card->shortname, "Conexant CX%x", pci->device);
908 sprintf(card->longname, "%s at %#llx",
909 card->shortname,(unsigned long long)pci_resource_start(pci, 0));
910 strcpy (card->mixername, "CX88");
911
912 dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
913 card->driver,devno);
914
915 err = snd_card_register(card);
916 if (err < 0)
917 goto error;
918 pci_set_drvdata(pci,card);
919
920 devno++;
921 return 0;
922
923error:
924 snd_card_free(card);
925 return err;
926}
927/*
928 * ALSA destructor
929 */
930static void __devexit cx88_audio_finidev(struct pci_dev *pci)
931{
932 struct cx88_audio_dev *card = pci_get_drvdata(pci);
933
934 snd_card_free((void *)card);
935
936 pci_set_drvdata(pci, NULL);
937
938 devno--;
939}
940
941/*
942 * PCI driver definition
943 */
944
945static struct pci_driver cx88_audio_pci_driver = {
946 .name = "cx88_audio",
947 .id_table = cx88_audio_pci_tbl,
948 .probe = cx88_audio_initdev,
949 .remove = __devexit_p(cx88_audio_finidev),
950};
951
952/****************************************************************************
953 LINUX MODULE INIT
954 ****************************************************************************/
955
956/*
957 * module init
958 */
959static int __init cx88_audio_init(void)
960{
961 printk(KERN_INFO "cx2388x alsa driver version %s loaded\n",
962 CX88_VERSION);
963 return pci_register_driver(&cx88_audio_pci_driver);
964}
965
966/*
967 * module remove
968 */
969static void __exit cx88_audio_fini(void)
970{
971 pci_unregister_driver(&cx88_audio_pci_driver);
972}
973
974module_init(cx88_audio_init);
975module_exit(cx88_audio_fini);
diff --git a/drivers/media/pci/cx88/cx88-blackbird.c b/drivers/media/pci/cx88/cx88-blackbird.c
new file mode 100644
index 000000000000..843ffd9e533b
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-blackbird.c
@@ -0,0 +1,1299 @@
1/*
2 *
3 * Support for a cx23416 mpeg encoder via cx2388x host port.
4 * "blackbird" reference design.
5 *
6 * (c) 2004 Jelle Foks <jelle@foks.us>
7 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
8 *
9 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10 * - video_ioctl2 conversion
11 *
12 * Includes parts from the ivtv driver <http://sourceforge.net/projects/ivtv/>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/fs.h>
33#include <linux/delay.h>
34#include <linux/device.h>
35#include <linux/firmware.h>
36#include <media/v4l2-common.h>
37#include <media/v4l2-ioctl.h>
38#include <media/v4l2-event.h>
39#include <media/cx2341x.h>
40
41#include "cx88.h"
42
43MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
44MODULE_AUTHOR("Jelle Foks <jelle@foks.us>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
45MODULE_LICENSE("GPL");
46MODULE_VERSION(CX88_VERSION);
47
48static unsigned int mpegbufs = 32;
49module_param(mpegbufs,int,0644);
50MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
51
52static unsigned int debug;
53module_param(debug,int,0644);
54MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
55
56#define dprintk(level,fmt, arg...) if (debug >= level) \
57 printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
58
59
60/* ------------------------------------------------------------------ */
61
62#define BLACKBIRD_FIRM_IMAGE_SIZE 376836
63
64/* defines below are from ivtv-driver.h */
65
66#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
67
68/* Firmware API commands */
69#define IVTV_API_STD_TIMEOUT 500
70
71enum blackbird_capture_type {
72 BLACKBIRD_MPEG_CAPTURE,
73 BLACKBIRD_RAW_CAPTURE,
74 BLACKBIRD_RAW_PASSTHRU_CAPTURE
75};
76enum blackbird_capture_bits {
77 BLACKBIRD_RAW_BITS_NONE = 0x00,
78 BLACKBIRD_RAW_BITS_YUV_CAPTURE = 0x01,
79 BLACKBIRD_RAW_BITS_PCM_CAPTURE = 0x02,
80 BLACKBIRD_RAW_BITS_VBI_CAPTURE = 0x04,
81 BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
82 BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE = 0x10
83};
84enum blackbird_capture_end {
85 BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
86 BLACKBIRD_END_NOW, /* stop immediately, no irq */
87};
88enum blackbird_framerate {
89 BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
90 BLACKBIRD_FRAMERATE_PAL_25 /* PAL: 25fps */
91};
92enum blackbird_stream_port {
93 BLACKBIRD_OUTPUT_PORT_MEMORY,
94 BLACKBIRD_OUTPUT_PORT_STREAMING,
95 BLACKBIRD_OUTPUT_PORT_SERIAL
96};
97enum blackbird_data_xfer_status {
98 BLACKBIRD_MORE_BUFFERS_FOLLOW,
99 BLACKBIRD_LAST_BUFFER,
100};
101enum blackbird_picture_mask {
102 BLACKBIRD_PICTURE_MASK_NONE,
103 BLACKBIRD_PICTURE_MASK_I_FRAMES,
104 BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
105 BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
106};
107enum blackbird_vbi_mode_bits {
108 BLACKBIRD_VBI_BITS_SLICED,
109 BLACKBIRD_VBI_BITS_RAW,
110};
111enum blackbird_vbi_insertion_bits {
112 BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
113 BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
114 BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
115 BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
116 BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
117};
118enum blackbird_dma_unit {
119 BLACKBIRD_DMA_BYTES,
120 BLACKBIRD_DMA_FRAMES,
121};
122enum blackbird_dma_transfer_status_bits {
123 BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
124 BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
125 BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
126};
127enum blackbird_pause {
128 BLACKBIRD_PAUSE_ENCODING,
129 BLACKBIRD_RESUME_ENCODING,
130};
131enum blackbird_copyright {
132 BLACKBIRD_COPYRIGHT_OFF,
133 BLACKBIRD_COPYRIGHT_ON,
134};
135enum blackbird_notification_type {
136 BLACKBIRD_NOTIFICATION_REFRESH,
137};
138enum blackbird_notification_status {
139 BLACKBIRD_NOTIFICATION_OFF,
140 BLACKBIRD_NOTIFICATION_ON,
141};
142enum blackbird_notification_mailbox {
143 BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
144};
145enum blackbird_field1_lines {
146 BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
147 BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
148 BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
149};
150enum blackbird_field2_lines {
151 BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
152 BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
153 BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
154};
155enum blackbird_custom_data_type {
156 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
157 BLACKBIRD_CUSTOM_PRIVATE_PACKET,
158};
159enum blackbird_mute {
160 BLACKBIRD_UNMUTE,
161 BLACKBIRD_MUTE,
162};
163enum blackbird_mute_video_mask {
164 BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
165 BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
166 BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
167};
168enum blackbird_mute_video_shift {
169 BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
170 BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
171 BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
172};
173
174/* Registers */
175#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
176#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
177#define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
178#define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
179#define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
180#define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
181
182/* ------------------------------------------------------------------ */
183
184static void host_setup(struct cx88_core *core)
185{
186 /* toggle reset of the host */
187 cx_write(MO_GPHST_SOFT_RST, 1);
188 udelay(100);
189 cx_write(MO_GPHST_SOFT_RST, 0);
190 udelay(100);
191
192 /* host port setup */
193 cx_write(MO_GPHST_WSC, 0x44444444U);
194 cx_write(MO_GPHST_XFR, 0);
195 cx_write(MO_GPHST_WDTH, 15);
196 cx_write(MO_GPHST_HDSHK, 0);
197 cx_write(MO_GPHST_MUX16, 0x44448888U);
198 cx_write(MO_GPHST_MODE, 0);
199}
200
201/* ------------------------------------------------------------------ */
202
203#define P1_MDATA0 0x390000
204#define P1_MDATA1 0x390001
205#define P1_MDATA2 0x390002
206#define P1_MDATA3 0x390003
207#define P1_MADDR2 0x390004
208#define P1_MADDR1 0x390005
209#define P1_MADDR0 0x390006
210#define P1_RDATA0 0x390008
211#define P1_RDATA1 0x390009
212#define P1_RDATA2 0x39000A
213#define P1_RDATA3 0x39000B
214#define P1_RADDR0 0x39000C
215#define P1_RADDR1 0x39000D
216#define P1_RRDWR 0x39000E
217
218static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
219{
220 unsigned long timeout = jiffies + msecs_to_jiffies(1);
221 u32 gpio0,need;
222
223 need = state ? 2 : 0;
224 for (;;) {
225 gpio0 = cx_read(MO_GP0_IO) & 2;
226 if (need == gpio0)
227 return 0;
228 if (time_after(jiffies,timeout))
229 return -1;
230 udelay(1);
231 }
232}
233
234static int memory_write(struct cx88_core *core, u32 address, u32 value)
235{
236 /* Warning: address is dword address (4 bytes) */
237 cx_writeb(P1_MDATA0, (unsigned int)value);
238 cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
239 cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
240 cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
241 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
242 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
243 cx_writeb(P1_MADDR0, (unsigned int)address);
244 cx_read(P1_MDATA0);
245 cx_read(P1_MADDR0);
246
247 return wait_ready_gpio0_bit1(core,1);
248}
249
250static int memory_read(struct cx88_core *core, u32 address, u32 *value)
251{
252 int retval;
253 u32 val;
254
255 /* Warning: address is dword address (4 bytes) */
256 cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
257 cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
258 cx_writeb(P1_MADDR0, (unsigned int)address);
259 cx_read(P1_MADDR0);
260
261 retval = wait_ready_gpio0_bit1(core,1);
262
263 cx_writeb(P1_MDATA3, 0);
264 val = (unsigned char)cx_read(P1_MDATA3) << 24;
265 cx_writeb(P1_MDATA2, 0);
266 val |= (unsigned char)cx_read(P1_MDATA2) << 16;
267 cx_writeb(P1_MDATA1, 0);
268 val |= (unsigned char)cx_read(P1_MDATA1) << 8;
269 cx_writeb(P1_MDATA0, 0);
270 val |= (unsigned char)cx_read(P1_MDATA0);
271
272 *value = val;
273 return retval;
274}
275
276static int register_write(struct cx88_core *core, u32 address, u32 value)
277{
278 cx_writeb(P1_RDATA0, (unsigned int)value);
279 cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
280 cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
281 cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
282 cx_writeb(P1_RADDR0, (unsigned int)address);
283 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
284 cx_writeb(P1_RRDWR, 1);
285 cx_read(P1_RDATA0);
286 cx_read(P1_RADDR0);
287
288 return wait_ready_gpio0_bit1(core,1);
289}
290
291
292static int register_read(struct cx88_core *core, u32 address, u32 *value)
293{
294 int retval;
295 u32 val;
296
297 cx_writeb(P1_RADDR0, (unsigned int)address);
298 cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
299 cx_writeb(P1_RRDWR, 0);
300 cx_read(P1_RADDR0);
301
302 retval = wait_ready_gpio0_bit1(core,1);
303 val = (unsigned char)cx_read(P1_RDATA0);
304 val |= (unsigned char)cx_read(P1_RDATA1) << 8;
305 val |= (unsigned char)cx_read(P1_RDATA2) << 16;
306 val |= (unsigned char)cx_read(P1_RDATA3) << 24;
307
308 *value = val;
309 return retval;
310}
311
312/* ------------------------------------------------------------------ */
313
314static int blackbird_mbox_func(void *priv, u32 command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
315{
316 struct cx8802_dev *dev = priv;
317 unsigned long timeout;
318 u32 value, flag, retval;
319 int i;
320
321 dprintk(1,"%s: 0x%X\n", __func__, command);
322
323 /* this may not be 100% safe if we can't read any memory location
324 without side effects */
325 memory_read(dev->core, dev->mailbox - 4, &value);
326 if (value != 0x12345678) {
327 dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
328 return -1;
329 }
330
331 memory_read(dev->core, dev->mailbox, &flag);
332 if (flag) {
333 dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
334 return -1;
335 }
336
337 flag |= 1; /* tell 'em we're working on it */
338 memory_write(dev->core, dev->mailbox, flag);
339
340 /* write command + args + fill remaining with zeros */
341 memory_write(dev->core, dev->mailbox + 1, command); /* command code */
342 memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
343 for (i = 0; i < in; i++) {
344 memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
345 dprintk(1, "API Input %d = %d\n", i, data[i]);
346 }
347 for (; i < CX2341X_MBOX_MAX_DATA; i++)
348 memory_write(dev->core, dev->mailbox + 4 + i, 0);
349
350 flag |= 3; /* tell 'em we're done writing */
351 memory_write(dev->core, dev->mailbox, flag);
352
353 /* wait for firmware to handle the API command */
354 timeout = jiffies + msecs_to_jiffies(10);
355 for (;;) {
356 memory_read(dev->core, dev->mailbox, &flag);
357 if (0 != (flag & 4))
358 break;
359 if (time_after(jiffies,timeout)) {
360 dprintk(0, "ERROR: API Mailbox timeout\n");
361 return -1;
362 }
363 udelay(10);
364 }
365
366 /* read output values */
367 for (i = 0; i < out; i++) {
368 memory_read(dev->core, dev->mailbox + 4 + i, data + i);
369 dprintk(1, "API Output %d = %d\n", i, data[i]);
370 }
371
372 memory_read(dev->core, dev->mailbox + 2, &retval);
373 dprintk(1, "API result = %d\n",retval);
374
375 flag = 0;
376 memory_write(dev->core, dev->mailbox, flag);
377 return retval;
378}
379/* ------------------------------------------------------------------ */
380
381/* We don't need to call the API often, so using just one mailbox will probably suffice */
382static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
383 u32 inputcnt, u32 outputcnt, ...)
384{
385 u32 data[CX2341X_MBOX_MAX_DATA];
386 va_list vargs;
387 int i, err;
388
389 va_start(vargs, outputcnt);
390
391 for (i = 0; i < inputcnt; i++) {
392 data[i] = va_arg(vargs, int);
393 }
394 err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
395 for (i = 0; i < outputcnt; i++) {
396 int *vptr = va_arg(vargs, int *);
397 *vptr = data[i];
398 }
399 va_end(vargs);
400 return err;
401}
402
403static int blackbird_find_mailbox(struct cx8802_dev *dev)
404{
405 u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
406 int signaturecnt=0;
407 u32 value;
408 int i;
409
410 for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
411 memory_read(dev->core, i, &value);
412 if (value == signature[signaturecnt])
413 signaturecnt++;
414 else
415 signaturecnt = 0;
416 if (4 == signaturecnt) {
417 dprintk(1, "Mailbox signature found\n");
418 return i+1;
419 }
420 }
421 dprintk(0, "Mailbox signature values not found!\n");
422 return -1;
423}
424
425static int blackbird_load_firmware(struct cx8802_dev *dev)
426{
427 static const unsigned char magic[8] = {
428 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
429 };
430 const struct firmware *firmware;
431 int i, retval = 0;
432 u32 value = 0;
433 u32 checksum = 0;
434 u32 *dataptr;
435
436 retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
437 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
438 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
439 retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
440 msleep(1);
441 retval |= register_write(dev->core, IVTV_REG_APU, 0);
442
443 if (retval < 0)
444 dprintk(0, "Error with register_write\n");
445
446 retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
447 &dev->pci->dev);
448
449
450 if (retval != 0) {
451 dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
452 CX2341X_FIRM_ENC_FILENAME);
453 dprintk(0, "Please fix your hotplug setup, the board will "
454 "not work without firmware loaded!\n");
455 return -1;
456 }
457
458 if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
459 dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
460 firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
461 release_firmware(firmware);
462 return -1;
463 }
464
465 if (0 != memcmp(firmware->data, magic, 8)) {
466 dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
467 release_firmware(firmware);
468 return -1;
469 }
470
471 /* transfer to the chip */
472 dprintk(1,"Loading firmware ...\n");
473 dataptr = (u32*)firmware->data;
474 for (i = 0; i < (firmware->size >> 2); i++) {
475 value = le32_to_cpu(*dataptr);
476 checksum += ~value;
477 memory_write(dev->core, i, value);
478 dataptr++;
479 }
480
481 /* read back to verify with the checksum */
482 for (i--; i >= 0; i--) {
483 memory_read(dev->core, i, &value);
484 checksum -= ~value;
485 }
486 if (checksum) {
487 dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
488 release_firmware(firmware);
489 return -1;
490 }
491 release_firmware(firmware);
492 dprintk(0, "Firmware upload successful.\n");
493
494 retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
495 retval |= register_read(dev->core, IVTV_REG_SPU, &value);
496 retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
497 msleep(1);
498
499 retval |= register_read(dev->core, IVTV_REG_VPU, &value);
500 retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
501
502 if (retval < 0)
503 dprintk(0, "Error with register_write\n");
504 return 0;
505}
506
507/**
508 Settings used by the windows tv app for PVR2000:
509=================================================================================================================
510Profile | Codec | Resolution | CBR/VBR | Video Qlty | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
511-----------------------------------------------------------------------------------------------------------------
512MPEG-1 | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 2000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
513MPEG-2 | MPEG2 | 720x576PAL | VBR | 600 :Good | 4000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
514VCD | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 1150 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
515DVD | MPEG2 | 720x576PAL | VBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
516DB* DVD | MPEG2 | 720x576PAL | CBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
517=================================================================================================================
518*DB: "DirectBurn"
519*/
520
521static void blackbird_codec_settings(struct cx8802_dev *dev)
522{
523 /* assign frame size */
524 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
525 dev->height, dev->width);
526
527 dev->cxhdl.width = dev->width;
528 dev->cxhdl.height = dev->height;
529 cx2341x_handler_set_50hz(&dev->cxhdl, dev->core->tvnorm & V4L2_STD_625_50);
530 cx2341x_handler_setup(&dev->cxhdl);
531}
532
533static int blackbird_initialize_codec(struct cx8802_dev *dev)
534{
535 struct cx88_core *core = dev->core;
536 int version;
537 int retval;
538
539 dprintk(1,"Initialize codec\n");
540 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
541 if (retval < 0) {
542
543 dev->mpeg_active = 0;
544
545 /* ping was not successful, reset and upload firmware */
546 cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
547 cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
548 retval = blackbird_load_firmware(dev);
549 if (retval < 0)
550 return retval;
551
552 retval = blackbird_find_mailbox(dev);
553 if (retval < 0)
554 return -1;
555
556 dev->mailbox = retval;
557
558 retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
559 if (retval < 0) {
560 dprintk(0, "ERROR: Firmware ping failed!\n");
561 return -1;
562 }
563
564 retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
565 if (retval < 0) {
566 dprintk(0, "ERROR: Firmware get encoder version failed!\n");
567 return -1;
568 }
569 dprintk(0, "Firmware version is 0x%08x\n", version);
570 }
571
572 cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
573 cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
574 cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
575 cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
576
577 blackbird_codec_settings(dev);
578
579 blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
580 BLACKBIRD_FIELD1_SAA7115,
581 BLACKBIRD_FIELD2_SAA7115
582 );
583
584 blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
585 BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
587
588 return 0;
589}
590
591static int blackbird_start_codec(struct file *file, void *priv)
592{
593 struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev;
594 struct cx88_core *core = dev->core;
595 /* start capturing to the host interface */
596 u32 reg;
597
598 int i;
599 int lastchange = -1;
600 int lastval = 0;
601
602 for (i = 0; (i < 10) && (i < (lastchange + 4)); i++) {
603 reg = cx_read(AUD_STATUS);
604
605 dprintk(1, "AUD_STATUS:%dL: 0x%x\n", i, reg);
606 if ((reg & 0x0F) != lastval) {
607 lastval = reg & 0x0F;
608 lastchange = i;
609 }
610 msleep(100);
611 }
612
613 /* unmute audio source */
614 cx_clear(AUD_VOL_CTL, (1 << 6));
615
616 blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0, 0);
617
618 /* initialize the video input */
619 blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
620
621 cx2341x_handler_set_busy(&dev->cxhdl, 1);
622
623 /* start capturing to the host interface */
624 blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
625 BLACKBIRD_MPEG_CAPTURE,
626 BLACKBIRD_RAW_BITS_NONE
627 );
628
629 dev->mpeg_active = 1;
630 return 0;
631}
632
633static int blackbird_stop_codec(struct cx8802_dev *dev)
634{
635 blackbird_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
636 BLACKBIRD_END_NOW,
637 BLACKBIRD_MPEG_CAPTURE,
638 BLACKBIRD_RAW_BITS_NONE
639 );
640
641 cx2341x_handler_set_busy(&dev->cxhdl, 0);
642
643 dev->mpeg_active = 0;
644 return 0;
645}
646
647/* ------------------------------------------------------------------ */
648
649static int bb_buf_setup(struct videobuf_queue *q,
650 unsigned int *count, unsigned int *size)
651{
652 struct cx8802_fh *fh = q->priv_data;
653
654 fh->dev->ts_packet_size = 188 * 4; /* was: 512 */
655 fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
656
657 *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
658 *count = fh->dev->ts_packet_count;
659 return 0;
660}
661
662static int
663bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
664 enum v4l2_field field)
665{
666 struct cx8802_fh *fh = q->priv_data;
667 return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
668}
669
670static void
671bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
672{
673 struct cx8802_fh *fh = q->priv_data;
674 cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
675}
676
677static void
678bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
679{
680 cx88_free_buffer(q, (struct cx88_buffer*)vb);
681}
682
683static struct videobuf_queue_ops blackbird_qops = {
684 .buf_setup = bb_buf_setup,
685 .buf_prepare = bb_buf_prepare,
686 .buf_queue = bb_buf_queue,
687 .buf_release = bb_buf_release,
688};
689
690/* ------------------------------------------------------------------ */
691
692static int vidioc_querycap(struct file *file, void *priv,
693 struct v4l2_capability *cap)
694{
695 struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev;
696 struct cx88_core *core = dev->core;
697
698 strcpy(cap->driver, "cx88_blackbird");
699 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
700 cx88_querycap(file, core, cap);
701 return 0;
702}
703
704static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
705 struct v4l2_fmtdesc *f)
706{
707 if (f->index != 0)
708 return -EINVAL;
709
710 strlcpy(f->description, "MPEG", sizeof(f->description));
711 f->pixelformat = V4L2_PIX_FMT_MPEG;
712 f->flags = V4L2_FMT_FLAG_COMPRESSED;
713 return 0;
714}
715
716static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
717 struct v4l2_format *f)
718{
719 struct cx8802_fh *fh = priv;
720 struct cx8802_dev *dev = fh->dev;
721
722 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
723 f->fmt.pix.bytesperline = 0;
724 f->fmt.pix.sizeimage = 188 * 4 * mpegbufs; /* 188 * 4 * 1024; */;
725 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
726 f->fmt.pix.width = dev->width;
727 f->fmt.pix.height = dev->height;
728 f->fmt.pix.field = fh->mpegq.field;
729 dprintk(1, "VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
730 dev->width, dev->height, fh->mpegq.field );
731 return 0;
732}
733
734static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
735 struct v4l2_format *f)
736{
737 struct cx8802_fh *fh = priv;
738 struct cx8802_dev *dev = fh->dev;
739
740 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
741 f->fmt.pix.bytesperline = 0;
742 f->fmt.pix.sizeimage = 188 * 4 * mpegbufs; /* 188 * 4 * 1024; */;
743 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
744 dprintk(1, "VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
745 dev->width, dev->height, fh->mpegq.field );
746 return 0;
747}
748
749static int vidioc_s_fmt_vid_cap (struct file *file, void *priv,
750 struct v4l2_format *f)
751{
752 struct cx8802_fh *fh = priv;
753 struct cx8802_dev *dev = fh->dev;
754 struct cx88_core *core = dev->core;
755
756 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
757 f->fmt.pix.bytesperline = 0;
758 f->fmt.pix.sizeimage = 188 * 4 * mpegbufs; /* 188 * 4 * 1024; */;
759 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
760 dev->width = f->fmt.pix.width;
761 dev->height = f->fmt.pix.height;
762 fh->mpegq.field = f->fmt.pix.field;
763 cx88_set_scale(core, f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
764 blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
765 f->fmt.pix.height, f->fmt.pix.width);
766 dprintk(1, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
767 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
768 return 0;
769}
770
771static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
772{
773 struct cx8802_fh *fh = priv;
774 return (videobuf_reqbufs(&fh->mpegq, p));
775}
776
777static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
778{
779 struct cx8802_fh *fh = priv;
780 return (videobuf_querybuf(&fh->mpegq, p));
781}
782
783static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
784{
785 struct cx8802_fh *fh = priv;
786 return (videobuf_qbuf(&fh->mpegq, p));
787}
788
789static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
790{
791 struct cx8802_fh *fh = priv;
792 return (videobuf_dqbuf(&fh->mpegq, p,
793 file->f_flags & O_NONBLOCK));
794}
795
796static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
797{
798 struct cx8802_fh *fh = priv;
799 struct cx8802_dev *dev = fh->dev;
800
801 if (!dev->mpeg_active)
802 blackbird_start_codec(file, fh);
803 return videobuf_streamon(&fh->mpegq);
804}
805
806static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
807{
808 struct cx8802_fh *fh = priv;
809 struct cx8802_dev *dev = fh->dev;
810
811 if (dev->mpeg_active)
812 blackbird_stop_codec(dev);
813 return videobuf_streamoff(&fh->mpegq);
814}
815
816static int vidioc_s_frequency (struct file *file, void *priv,
817 struct v4l2_frequency *f)
818{
819 struct cx8802_fh *fh = priv;
820 struct cx8802_dev *dev = fh->dev;
821 struct cx88_core *core = dev->core;
822
823 if (unlikely(UNSET == core->board.tuner_type))
824 return -EINVAL;
825 if (unlikely(f->tuner != 0))
826 return -EINVAL;
827 if (dev->mpeg_active)
828 blackbird_stop_codec(dev);
829
830 cx88_set_freq (core,f);
831 blackbird_initialize_codec(dev);
832 cx88_set_scale(dev->core, dev->width, dev->height,
833 fh->mpegq.field);
834 return 0;
835}
836
837static int vidioc_log_status (struct file *file, void *priv)
838{
839 struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev;
840 struct cx88_core *core = dev->core;
841 char name[32 + 2];
842
843 snprintf(name, sizeof(name), "%s/2", core->name);
844 call_all(core, core, log_status);
845 v4l2_ctrl_handler_log_status(&dev->cxhdl.hdl, name);
846 return 0;
847}
848
849static int vidioc_enum_input (struct file *file, void *priv,
850 struct v4l2_input *i)
851{
852 struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core;
853 return cx88_enum_input (core,i);
854}
855
856static int vidioc_g_frequency (struct file *file, void *priv,
857 struct v4l2_frequency *f)
858{
859 struct cx8802_fh *fh = priv;
860 struct cx88_core *core = fh->dev->core;
861
862 if (unlikely(UNSET == core->board.tuner_type))
863 return -EINVAL;
864 if (unlikely(f->tuner != 0))
865 return -EINVAL;
866
867 f->frequency = core->freq;
868 call_all(core, tuner, g_frequency, f);
869
870 return 0;
871}
872
873static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
874{
875 struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core;
876
877 *i = core->input;
878 return 0;
879}
880
881static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
882{
883 struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core;
884
885 if (i >= 4)
886 return -EINVAL;
887 if (0 == INPUT(i).type)
888 return -EINVAL;
889
890 mutex_lock(&core->lock);
891 cx88_newstation(core);
892 cx88_video_mux(core,i);
893 mutex_unlock(&core->lock);
894 return 0;
895}
896
897static int vidioc_g_tuner (struct file *file, void *priv,
898 struct v4l2_tuner *t)
899{
900 struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core;
901 u32 reg;
902
903 if (unlikely(UNSET == core->board.tuner_type))
904 return -EINVAL;
905 if (0 != t->index)
906 return -EINVAL;
907
908 strcpy(t->name, "Television");
909 t->capability = V4L2_TUNER_CAP_NORM;
910 t->rangehigh = 0xffffffffUL;
911 call_all(core, tuner, g_tuner, t);
912
913 cx88_get_stereo(core ,t);
914 reg = cx_read(MO_DEVICE_STATUS);
915 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
916 return 0;
917}
918
919static int vidioc_s_tuner (struct file *file, void *priv,
920 struct v4l2_tuner *t)
921{
922 struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core;
923
924 if (UNSET == core->board.tuner_type)
925 return -EINVAL;
926 if (0 != t->index)
927 return -EINVAL;
928
929 cx88_set_stereo(core, t->audmode, 1);
930 return 0;
931}
932
933static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
934{
935 struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core;
936
937 *tvnorm = core->tvnorm;
938 return 0;
939}
940
941static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
942{
943 struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core;
944
945 mutex_lock(&core->lock);
946 cx88_set_tvnorm(core,*id);
947 mutex_unlock(&core->lock);
948 return 0;
949}
950
951/* FIXME: cx88_ioctl_hook not implemented */
952
953static int mpeg_open(struct file *file)
954{
955 struct video_device *vdev = video_devdata(file);
956 struct cx8802_dev *dev = video_drvdata(file);
957 struct cx8802_fh *fh;
958 struct cx8802_driver *drv = NULL;
959 int err;
960
961 dprintk( 1, "%s\n", __func__);
962
963 mutex_lock(&dev->core->lock);
964
965 /* Make sure we can acquire the hardware */
966 drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
967 if (!drv) {
968 dprintk(1, "%s: blackbird driver is not loaded\n", __func__);
969 mutex_unlock(&dev->core->lock);
970 return -ENODEV;
971 }
972
973 err = drv->request_acquire(drv);
974 if (err != 0) {
975 dprintk(1,"%s: Unable to acquire hardware, %d\n", __func__, err);
976 mutex_unlock(&dev->core->lock);
977 return err;
978 }
979
980 if (!dev->core->mpeg_users && blackbird_initialize_codec(dev) < 0) {
981 drv->request_release(drv);
982 mutex_unlock(&dev->core->lock);
983 return -EINVAL;
984 }
985 dprintk(1, "open dev=%s\n", video_device_node_name(vdev));
986
987 /* allocate + initialize per filehandle data */
988 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
989 if (NULL == fh) {
990 drv->request_release(drv);
991 mutex_unlock(&dev->core->lock);
992 return -ENOMEM;
993 }
994 v4l2_fh_init(&fh->fh, vdev);
995 file->private_data = fh;
996 fh->dev = dev;
997
998 videobuf_queue_sg_init(&fh->mpegq, &blackbird_qops,
999 &dev->pci->dev, &dev->slock,
1000 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1001 V4L2_FIELD_INTERLACED,
1002 sizeof(struct cx88_buffer),
1003 fh, NULL);
1004
1005 /* FIXME: locking against other video device */
1006 cx88_set_scale(dev->core, dev->width, dev->height,
1007 fh->mpegq.field);
1008
1009 dev->core->mpeg_users++;
1010 mutex_unlock(&dev->core->lock);
1011 v4l2_fh_add(&fh->fh);
1012 return 0;
1013}
1014
1015static int mpeg_release(struct file *file)
1016{
1017 struct cx8802_fh *fh = file->private_data;
1018 struct cx8802_dev *dev = fh->dev;
1019 struct cx8802_driver *drv = NULL;
1020
1021 mutex_lock(&dev->core->lock);
1022
1023 if (dev->mpeg_active && dev->core->mpeg_users == 1)
1024 blackbird_stop_codec(dev);
1025
1026 cx8802_cancel_buffers(fh->dev);
1027 /* stop mpeg capture */
1028 videobuf_stop(&fh->mpegq);
1029
1030 videobuf_mmap_free(&fh->mpegq);
1031
1032 v4l2_fh_del(&fh->fh);
1033 v4l2_fh_exit(&fh->fh);
1034 file->private_data = NULL;
1035 kfree(fh);
1036
1037 /* Make sure we release the hardware */
1038 drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
1039 WARN_ON(!drv);
1040 if (drv)
1041 drv->request_release(drv);
1042
1043 dev->core->mpeg_users--;
1044
1045 mutex_unlock(&dev->core->lock);
1046
1047 return 0;
1048}
1049
1050static ssize_t
1051mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1052{
1053 struct cx8802_fh *fh = file->private_data;
1054 struct cx8802_dev *dev = fh->dev;
1055
1056 if (!dev->mpeg_active)
1057 blackbird_start_codec(file, fh);
1058
1059 return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
1060 file->f_flags & O_NONBLOCK);
1061}
1062
1063static unsigned int
1064mpeg_poll(struct file *file, struct poll_table_struct *wait)
1065{
1066 unsigned long req_events = poll_requested_events(wait);
1067 struct cx8802_fh *fh = file->private_data;
1068 struct cx8802_dev *dev = fh->dev;
1069
1070 if (!dev->mpeg_active && (req_events & (POLLIN | POLLRDNORM)))
1071 blackbird_start_codec(file, fh);
1072
1073 return v4l2_ctrl_poll(file, wait) | videobuf_poll_stream(file, &fh->mpegq, wait);
1074}
1075
1076static int
1077mpeg_mmap(struct file *file, struct vm_area_struct * vma)
1078{
1079 struct cx8802_fh *fh = file->private_data;
1080
1081 return videobuf_mmap_mapper(&fh->mpegq, vma);
1082}
1083
1084static const struct v4l2_file_operations mpeg_fops =
1085{
1086 .owner = THIS_MODULE,
1087 .open = mpeg_open,
1088 .release = mpeg_release,
1089 .read = mpeg_read,
1090 .poll = mpeg_poll,
1091 .mmap = mpeg_mmap,
1092 .unlocked_ioctl = video_ioctl2,
1093};
1094
1095static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1096 .vidioc_querycap = vidioc_querycap,
1097 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1098 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1099 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1100 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1101 .vidioc_reqbufs = vidioc_reqbufs,
1102 .vidioc_querybuf = vidioc_querybuf,
1103 .vidioc_qbuf = vidioc_qbuf,
1104 .vidioc_dqbuf = vidioc_dqbuf,
1105 .vidioc_streamon = vidioc_streamon,
1106 .vidioc_streamoff = vidioc_streamoff,
1107 .vidioc_s_frequency = vidioc_s_frequency,
1108 .vidioc_log_status = vidioc_log_status,
1109 .vidioc_enum_input = vidioc_enum_input,
1110 .vidioc_g_frequency = vidioc_g_frequency,
1111 .vidioc_g_input = vidioc_g_input,
1112 .vidioc_s_input = vidioc_s_input,
1113 .vidioc_g_tuner = vidioc_g_tuner,
1114 .vidioc_s_tuner = vidioc_s_tuner,
1115 .vidioc_g_std = vidioc_g_std,
1116 .vidioc_s_std = vidioc_s_std,
1117 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1118 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1119};
1120
1121static struct video_device cx8802_mpeg_template = {
1122 .name = "cx8802",
1123 .fops = &mpeg_fops,
1124 .ioctl_ops = &mpeg_ioctl_ops,
1125 .tvnorms = CX88_NORMS,
1126};
1127
1128/* ------------------------------------------------------------------ */
1129
1130/* The CX8802 MPEG API will call this when we can use the hardware */
1131static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
1132{
1133 struct cx88_core *core = drv->core;
1134 int err = 0;
1135
1136 switch (core->boardnr) {
1137 case CX88_BOARD_HAUPPAUGE_HVR1300:
1138 /* By default, core setup will leave the cx22702 out of reset, on the bus.
1139 * We left the hardware on power up with the cx22702 active.
1140 * We're being given access to re-arrange the GPIOs.
1141 * Take the bus off the cx22702 and put the cx23416 on it.
1142 */
1143 /* Toggle reset on cx22702 leaving i2c active */
1144 cx_set(MO_GP0_IO, 0x00000080);
1145 udelay(1000);
1146 cx_clear(MO_GP0_IO, 0x00000080);
1147 udelay(50);
1148 cx_set(MO_GP0_IO, 0x00000080);
1149 udelay(1000);
1150 /* tri-state the cx22702 pins */
1151 cx_set(MO_GP0_IO, 0x00000004);
1152 udelay(1000);
1153 break;
1154 default:
1155 err = -ENODEV;
1156 }
1157 return err;
1158}
1159
1160/* The CX8802 MPEG API will call this when we need to release the hardware */
1161static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
1162{
1163 struct cx88_core *core = drv->core;
1164 int err = 0;
1165
1166 switch (core->boardnr) {
1167 case CX88_BOARD_HAUPPAUGE_HVR1300:
1168 /* Exit leaving the cx23416 on the bus */
1169 break;
1170 default:
1171 err = -ENODEV;
1172 }
1173 return err;
1174}
1175
1176static void blackbird_unregister_video(struct cx8802_dev *dev)
1177{
1178 if (dev->mpeg_dev) {
1179 if (video_is_registered(dev->mpeg_dev))
1180 video_unregister_device(dev->mpeg_dev);
1181 else
1182 video_device_release(dev->mpeg_dev);
1183 dev->mpeg_dev = NULL;
1184 }
1185}
1186
1187static int blackbird_register_video(struct cx8802_dev *dev)
1188{
1189 int err;
1190
1191 dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
1192 &cx8802_mpeg_template,"mpeg");
1193 dev->mpeg_dev->ctrl_handler = &dev->cxhdl.hdl;
1194 video_set_drvdata(dev->mpeg_dev, dev);
1195 err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
1196 if (err < 0) {
1197 printk(KERN_INFO "%s/2: can't register mpeg device\n",
1198 dev->core->name);
1199 return err;
1200 }
1201 printk(KERN_INFO "%s/2: registered device %s [mpeg]\n",
1202 dev->core->name, video_device_node_name(dev->mpeg_dev));
1203 return 0;
1204}
1205
1206/* ----------------------------------------------------------- */
1207
1208static int cx8802_blackbird_probe(struct cx8802_driver *drv)
1209{
1210 struct cx88_core *core = drv->core;
1211 struct cx8802_dev *dev = core->dvbdev;
1212 int err;
1213
1214 dprintk( 1, "%s\n", __func__);
1215 dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1216 core->boardnr,
1217 core->name,
1218 core->pci_bus,
1219 core->pci_slot);
1220
1221 err = -ENODEV;
1222 if (!(core->board.mpeg & CX88_MPEG_BLACKBIRD))
1223 goto fail_core;
1224
1225 dev->width = 720;
1226 if (core->tvnorm & V4L2_STD_525_60) {
1227 dev->height = 480;
1228 } else {
1229 dev->height = 576;
1230 }
1231 dev->cxhdl.port = CX2341X_PORT_STREAMING;
1232 dev->cxhdl.width = dev->width;
1233 dev->cxhdl.height = dev->height;
1234 dev->cxhdl.func = blackbird_mbox_func;
1235 dev->cxhdl.priv = dev;
1236 err = cx2341x_handler_init(&dev->cxhdl, 36);
1237 if (err)
1238 goto fail_core;
1239 v4l2_ctrl_add_handler(&dev->cxhdl.hdl, &core->video_hdl);
1240
1241 /* blackbird stuff */
1242 printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1243 core->name);
1244 host_setup(dev->core);
1245
1246 blackbird_initialize_codec(dev);
1247
1248 /* initial device configuration: needed ? */
1249// init_controls(core);
1250 cx88_set_tvnorm(core,core->tvnorm);
1251 cx88_video_mux(core,0);
1252 cx2341x_handler_set_50hz(&dev->cxhdl, dev->height == 576);
1253 cx2341x_handler_setup(&dev->cxhdl);
1254 blackbird_register_video(dev);
1255
1256 return 0;
1257
1258 fail_core:
1259 return err;
1260}
1261
1262static int cx8802_blackbird_remove(struct cx8802_driver *drv)
1263{
1264 struct cx88_core *core = drv->core;
1265 struct cx8802_dev *dev = core->dvbdev;
1266
1267 /* blackbird */
1268 blackbird_unregister_video(drv->core->dvbdev);
1269 v4l2_ctrl_handler_free(&dev->cxhdl.hdl);
1270
1271 return 0;
1272}
1273
1274static struct cx8802_driver cx8802_blackbird_driver = {
1275 .type_id = CX88_MPEG_BLACKBIRD,
1276 .hw_access = CX8802_DRVCTL_SHARED,
1277 .probe = cx8802_blackbird_probe,
1278 .remove = cx8802_blackbird_remove,
1279 .advise_acquire = cx8802_blackbird_advise_acquire,
1280 .advise_release = cx8802_blackbird_advise_release,
1281};
1282
1283static int __init blackbird_init(void)
1284{
1285 printk(KERN_INFO "cx2388x blackbird driver version %s loaded\n",
1286 CX88_VERSION);
1287 return cx8802_register_driver(&cx8802_blackbird_driver);
1288}
1289
1290static void __exit blackbird_fini(void)
1291{
1292 cx8802_unregister_driver(&cx8802_blackbird_driver);
1293}
1294
1295module_init(blackbird_init);
1296module_exit(blackbird_fini);
1297
1298module_param_named(video_debug,cx8802_mpeg_template.debug, int, 0644);
1299MODULE_PARM_DESC(debug,"enable debug messages [video]");
diff --git a/drivers/media/pci/cx88/cx88-cards.c b/drivers/media/pci/cx88/cx88-cards.c
new file mode 100644
index 000000000000..4e9d4f722960
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-cards.c
@@ -0,0 +1,3811 @@
1/*
2 *
3 * device driver for Conexant 2388x based TV cards
4 * card-specific stuff.
5 *
6 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/pci.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28
29#include "cx88.h"
30#include "tea5767.h"
31#include "xc4000.h"
32
33static unsigned int tuner[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
34static unsigned int radio[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
35static unsigned int card[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
36
37module_param_array(tuner, int, NULL, 0444);
38module_param_array(radio, int, NULL, 0444);
39module_param_array(card, int, NULL, 0444);
40
41MODULE_PARM_DESC(tuner,"tuner type");
42MODULE_PARM_DESC(radio,"radio tuner type");
43MODULE_PARM_DESC(card,"card type");
44
45static unsigned int latency = UNSET;
46module_param(latency,int,0444);
47MODULE_PARM_DESC(latency,"pci latency timer");
48
49static int disable_ir;
50module_param(disable_ir, int, 0444);
51MODULE_PARM_DESC(disable_ir, "Disable IR support");
52
53#define info_printk(core, fmt, arg...) \
54 printk(KERN_INFO "%s: " fmt, core->name , ## arg)
55
56#define warn_printk(core, fmt, arg...) \
57 printk(KERN_WARNING "%s: " fmt, core->name , ## arg)
58
59#define err_printk(core, fmt, arg...) \
60 printk(KERN_ERR "%s: " fmt, core->name , ## arg)
61
62
63/* ------------------------------------------------------------------ */
64/* board config info */
65
66/* If radio_type !=UNSET, radio_addr should be specified
67 */
68
69static const struct cx88_board cx88_boards[] = {
70 [CX88_BOARD_UNKNOWN] = {
71 .name = "UNKNOWN/GENERIC",
72 .tuner_type = UNSET,
73 .radio_type = UNSET,
74 .tuner_addr = ADDR_UNSET,
75 .radio_addr = ADDR_UNSET,
76 .input = {{
77 .type = CX88_VMUX_COMPOSITE1,
78 .vmux = 0,
79 },{
80 .type = CX88_VMUX_COMPOSITE2,
81 .vmux = 1,
82 },{
83 .type = CX88_VMUX_COMPOSITE3,
84 .vmux = 2,
85 },{
86 .type = CX88_VMUX_COMPOSITE4,
87 .vmux = 3,
88 }},
89 },
90 [CX88_BOARD_HAUPPAUGE] = {
91 .name = "Hauppauge WinTV 34xxx models",
92 .tuner_type = UNSET,
93 .radio_type = UNSET,
94 .tuner_addr = ADDR_UNSET,
95 .radio_addr = ADDR_UNSET,
96 .tda9887_conf = TDA9887_PRESENT,
97 .input = {{
98 .type = CX88_VMUX_TELEVISION,
99 .vmux = 0,
100 .gpio0 = 0xff00, // internal decoder
101 },{
102 .type = CX88_VMUX_DEBUG,
103 .vmux = 0,
104 .gpio0 = 0xff01, // mono from tuner chip
105 },{
106 .type = CX88_VMUX_COMPOSITE1,
107 .vmux = 1,
108 .gpio0 = 0xff02,
109 },{
110 .type = CX88_VMUX_SVIDEO,
111 .vmux = 2,
112 .gpio0 = 0xff02,
113 }},
114 .radio = {
115 .type = CX88_RADIO,
116 .gpio0 = 0xff01,
117 },
118 },
119 [CX88_BOARD_GDI] = {
120 .name = "GDI Black Gold",
121 .tuner_type = UNSET,
122 .radio_type = UNSET,
123 .tuner_addr = ADDR_UNSET,
124 .radio_addr = ADDR_UNSET,
125 .input = {{
126 .type = CX88_VMUX_TELEVISION,
127 .vmux = 0,
128 },{
129 .type = CX88_VMUX_SVIDEO,
130 .vmux = 2,
131 }},
132 },
133 [CX88_BOARD_PIXELVIEW] = {
134 .name = "PixelView",
135 .tuner_type = TUNER_PHILIPS_PAL,
136 .radio_type = UNSET,
137 .tuner_addr = ADDR_UNSET,
138 .radio_addr = ADDR_UNSET,
139 .input = {{
140 .type = CX88_VMUX_TELEVISION,
141 .vmux = 0,
142 .gpio0 = 0xff00, // internal decoder
143 },{
144 .type = CX88_VMUX_COMPOSITE1,
145 .vmux = 1,
146 },{
147 .type = CX88_VMUX_SVIDEO,
148 .vmux = 2,
149 }},
150 .radio = {
151 .type = CX88_RADIO,
152 .gpio0 = 0xff10,
153 },
154 },
155 [CX88_BOARD_ATI_WONDER_PRO] = {
156 .name = "ATI TV Wonder Pro",
157 .tuner_type = TUNER_PHILIPS_4IN1,
158 .radio_type = UNSET,
159 .tuner_addr = ADDR_UNSET,
160 .radio_addr = ADDR_UNSET,
161 .tda9887_conf = TDA9887_PRESENT | TDA9887_INTERCARRIER,
162 .input = {{
163 .type = CX88_VMUX_TELEVISION,
164 .vmux = 0,
165 .gpio0 = 0x03ff,
166 },{
167 .type = CX88_VMUX_COMPOSITE1,
168 .vmux = 1,
169 .gpio0 = 0x03fe,
170 },{
171 .type = CX88_VMUX_SVIDEO,
172 .vmux = 2,
173 .gpio0 = 0x03fe,
174 }},
175 },
176 [CX88_BOARD_WINFAST2000XP_EXPERT] = {
177 .name = "Leadtek Winfast 2000XP Expert",
178 .tuner_type = TUNER_PHILIPS_4IN1,
179 .radio_type = UNSET,
180 .tuner_addr = ADDR_UNSET,
181 .radio_addr = ADDR_UNSET,
182 .tda9887_conf = TDA9887_PRESENT,
183 .input = {{
184 .type = CX88_VMUX_TELEVISION,
185 .vmux = 0,
186 .gpio0 = 0x00F5e700,
187 .gpio1 = 0x00003004,
188 .gpio2 = 0x00F5e700,
189 .gpio3 = 0x02000000,
190 },{
191 .type = CX88_VMUX_COMPOSITE1,
192 .vmux = 1,
193 .gpio0 = 0x00F5c700,
194 .gpio1 = 0x00003004,
195 .gpio2 = 0x00F5c700,
196 .gpio3 = 0x02000000,
197 },{
198 .type = CX88_VMUX_SVIDEO,
199 .vmux = 2,
200 .gpio0 = 0x00F5c700,
201 .gpio1 = 0x00003004,
202 .gpio2 = 0x00F5c700,
203 .gpio3 = 0x02000000,
204 }},
205 .radio = {
206 .type = CX88_RADIO,
207 .gpio0 = 0x00F5d700,
208 .gpio1 = 0x00003004,
209 .gpio2 = 0x00F5d700,
210 .gpio3 = 0x02000000,
211 },
212 },
213 [CX88_BOARD_AVERTV_STUDIO_303] = {
214 .name = "AverTV Studio 303 (M126)",
215 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
216 .radio_type = UNSET,
217 .tuner_addr = ADDR_UNSET,
218 .radio_addr = ADDR_UNSET,
219 .tda9887_conf = TDA9887_PRESENT,
220 .input = {{
221 .type = CX88_VMUX_TELEVISION,
222 .vmux = 0,
223 .gpio1 = 0xe09f,
224 },{
225 .type = CX88_VMUX_COMPOSITE1,
226 .vmux = 1,
227 .gpio1 = 0xe05f,
228 },{
229 .type = CX88_VMUX_SVIDEO,
230 .vmux = 2,
231 .gpio1 = 0xe05f,
232 }},
233 .radio = {
234 .gpio1 = 0xe0df,
235 .type = CX88_RADIO,
236 },
237 },
238 [CX88_BOARD_MSI_TVANYWHERE_MASTER] = {
239 // added gpio values thanks to Michal
240 // values for PAL from DScaler
241 .name = "MSI TV-@nywhere Master",
242 .tuner_type = TUNER_MT2032,
243 .radio_type = UNSET,
244 .tuner_addr = ADDR_UNSET,
245 .radio_addr = ADDR_UNSET,
246 .tda9887_conf = TDA9887_PRESENT | TDA9887_INTERCARRIER_NTSC,
247 .input = {{
248 .type = CX88_VMUX_TELEVISION,
249 .vmux = 0,
250 .gpio0 = 0x000040bf,
251 .gpio1 = 0x000080c0,
252 .gpio2 = 0x0000ff40,
253 },{
254 .type = CX88_VMUX_COMPOSITE1,
255 .vmux = 1,
256 .gpio0 = 0x000040bf,
257 .gpio1 = 0x000080c0,
258 .gpio2 = 0x0000ff40,
259 },{
260 .type = CX88_VMUX_SVIDEO,
261 .vmux = 2,
262 .gpio0 = 0x000040bf,
263 .gpio1 = 0x000080c0,
264 .gpio2 = 0x0000ff40,
265 }},
266 .radio = {
267 .type = CX88_RADIO,
268 .vmux = 3,
269 .gpio0 = 0x000040bf,
270 .gpio1 = 0x000080c0,
271 .gpio2 = 0x0000ff20,
272 },
273 },
274 [CX88_BOARD_WINFAST_DV2000] = {
275 .name = "Leadtek Winfast DV2000",
276 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
277 .radio_type = UNSET,
278 .tuner_addr = ADDR_UNSET,
279 .radio_addr = ADDR_UNSET,
280 .tda9887_conf = TDA9887_PRESENT,
281 .input = {{
282 .type = CX88_VMUX_TELEVISION,
283 .vmux = 0,
284 .gpio0 = 0x0035e700,
285 .gpio1 = 0x00003004,
286 .gpio2 = 0x0035e700,
287 .gpio3 = 0x02000000,
288 },{
289
290 .type = CX88_VMUX_COMPOSITE1,
291 .vmux = 1,
292 .gpio0 = 0x0035c700,
293 .gpio1 = 0x00003004,
294 .gpio2 = 0x0035c700,
295 .gpio3 = 0x02000000,
296 },{
297 .type = CX88_VMUX_SVIDEO,
298 .vmux = 2,
299 .gpio0 = 0x0035c700,
300 .gpio1 = 0x0035c700,
301 .gpio2 = 0x02000000,
302 .gpio3 = 0x02000000,
303 }},
304 .radio = {
305 .type = CX88_RADIO,
306 .gpio0 = 0x0035d700,
307 .gpio1 = 0x00007004,
308 .gpio2 = 0x0035d700,
309 .gpio3 = 0x02000000,
310 },
311 },
312 [CX88_BOARD_LEADTEK_PVR2000] = {
313 // gpio values for PAL version from regspy by DScaler
314 .name = "Leadtek PVR 2000",
315 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
316 .radio_type = UNSET,
317 .tuner_addr = ADDR_UNSET,
318 .radio_addr = ADDR_UNSET,
319 .tda9887_conf = TDA9887_PRESENT,
320 .input = {{
321 .type = CX88_VMUX_TELEVISION,
322 .vmux = 0,
323 .gpio0 = 0x0000bde2,
324 .audioroute = 1,
325 },{
326 .type = CX88_VMUX_COMPOSITE1,
327 .vmux = 1,
328 .gpio0 = 0x0000bde6,
329 .audioroute = 1,
330 },{
331 .type = CX88_VMUX_SVIDEO,
332 .vmux = 2,
333 .gpio0 = 0x0000bde6,
334 .audioroute = 1,
335 }},
336 .radio = {
337 .type = CX88_RADIO,
338 .gpio0 = 0x0000bd62,
339 .audioroute = 1,
340 },
341 .mpeg = CX88_MPEG_BLACKBIRD,
342 },
343 [CX88_BOARD_IODATA_GVVCP3PCI] = {
344 .name = "IODATA GV-VCP3/PCI",
345 .tuner_type = TUNER_ABSENT,
346 .radio_type = UNSET,
347 .tuner_addr = ADDR_UNSET,
348 .radio_addr = ADDR_UNSET,
349 .input = {{
350 .type = CX88_VMUX_COMPOSITE1,
351 .vmux = 0,
352 },{
353 .type = CX88_VMUX_COMPOSITE2,
354 .vmux = 1,
355 },{
356 .type = CX88_VMUX_SVIDEO,
357 .vmux = 2,
358 }},
359 },
360 [CX88_BOARD_PROLINK_PLAYTVPVR] = {
361 .name = "Prolink PlayTV PVR",
362 .tuner_type = TUNER_PHILIPS_FM1236_MK3,
363 .radio_type = UNSET,
364 .tuner_addr = ADDR_UNSET,
365 .radio_addr = ADDR_UNSET,
366 .tda9887_conf = TDA9887_PRESENT,
367 .input = {{
368 .type = CX88_VMUX_TELEVISION,
369 .vmux = 0,
370 .gpio0 = 0xbff0,
371 },{
372 .type = CX88_VMUX_COMPOSITE1,
373 .vmux = 1,
374 .gpio0 = 0xbff3,
375 },{
376 .type = CX88_VMUX_SVIDEO,
377 .vmux = 2,
378 .gpio0 = 0xbff3,
379 }},
380 .radio = {
381 .type = CX88_RADIO,
382 .gpio0 = 0xbff0,
383 },
384 },
385 [CX88_BOARD_ASUS_PVR_416] = {
386 .name = "ASUS PVR-416",
387 .tuner_type = TUNER_PHILIPS_FM1236_MK3,
388 .radio_type = UNSET,
389 .tuner_addr = ADDR_UNSET,
390 .radio_addr = ADDR_UNSET,
391 .tda9887_conf = TDA9887_PRESENT,
392 .input = {{
393 .type = CX88_VMUX_TELEVISION,
394 .vmux = 0,
395 .gpio0 = 0x0000fde6,
396 },{
397 .type = CX88_VMUX_SVIDEO,
398 .vmux = 2,
399 .gpio0 = 0x0000fde6, // 0x0000fda6 L,R RCA audio in?
400 .audioroute = 1,
401 }},
402 .radio = {
403 .type = CX88_RADIO,
404 .gpio0 = 0x0000fde2,
405 },
406 .mpeg = CX88_MPEG_BLACKBIRD,
407 },
408 [CX88_BOARD_MSI_TVANYWHERE] = {
409 .name = "MSI TV-@nywhere",
410 .tuner_type = TUNER_MT2032,
411 .radio_type = UNSET,
412 .tuner_addr = ADDR_UNSET,
413 .radio_addr = ADDR_UNSET,
414 .tda9887_conf = TDA9887_PRESENT,
415 .input = {{
416 .type = CX88_VMUX_TELEVISION,
417 .vmux = 0,
418 .gpio0 = 0x00000fbf,
419 .gpio2 = 0x0000fc08,
420 },{
421 .type = CX88_VMUX_COMPOSITE1,
422 .vmux = 1,
423 .gpio0 = 0x00000fbf,
424 .gpio2 = 0x0000fc68,
425 },{
426 .type = CX88_VMUX_SVIDEO,
427 .vmux = 2,
428 .gpio0 = 0x00000fbf,
429 .gpio2 = 0x0000fc68,
430 }},
431 },
432 [CX88_BOARD_KWORLD_DVB_T] = {
433 .name = "KWorld/VStream XPert DVB-T",
434 .tuner_type = TUNER_ABSENT,
435 .radio_type = UNSET,
436 .tuner_addr = ADDR_UNSET,
437 .radio_addr = ADDR_UNSET,
438 .input = {{
439 .type = CX88_VMUX_COMPOSITE1,
440 .vmux = 1,
441 .gpio0 = 0x0700,
442 .gpio2 = 0x0101,
443 },{
444 .type = CX88_VMUX_SVIDEO,
445 .vmux = 2,
446 .gpio0 = 0x0700,
447 .gpio2 = 0x0101,
448 }},
449 .mpeg = CX88_MPEG_DVB,
450 },
451 [CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1] = {
452 .name = "DViCO FusionHDTV DVB-T1",
453 .tuner_type = TUNER_ABSENT, /* No analog tuner */
454 .radio_type = UNSET,
455 .tuner_addr = ADDR_UNSET,
456 .radio_addr = ADDR_UNSET,
457 .input = {{
458 .type = CX88_VMUX_COMPOSITE1,
459 .vmux = 1,
460 .gpio0 = 0x000027df,
461 },{
462 .type = CX88_VMUX_SVIDEO,
463 .vmux = 2,
464 .gpio0 = 0x000027df,
465 }},
466 .mpeg = CX88_MPEG_DVB,
467 },
468 [CX88_BOARD_KWORLD_LTV883] = {
469 .name = "KWorld LTV883RF",
470 .tuner_type = TUNER_TNF_8831BGFF,
471 .radio_type = UNSET,
472 .tuner_addr = ADDR_UNSET,
473 .radio_addr = ADDR_UNSET,
474 .input = {{
475 .type = CX88_VMUX_TELEVISION,
476 .vmux = 0,
477 .gpio0 = 0x07f8,
478 },{
479 .type = CX88_VMUX_DEBUG,
480 .vmux = 0,
481 .gpio0 = 0x07f9, // mono from tuner chip
482 },{
483 .type = CX88_VMUX_COMPOSITE1,
484 .vmux = 1,
485 .gpio0 = 0x000007fa,
486 },{
487 .type = CX88_VMUX_SVIDEO,
488 .vmux = 2,
489 .gpio0 = 0x000007fa,
490 }},
491 .radio = {
492 .type = CX88_RADIO,
493 .gpio0 = 0x000007f8,
494 },
495 },
496 [CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q] = {
497 .name = "DViCO FusionHDTV 3 Gold-Q",
498 .tuner_type = TUNER_MICROTUNE_4042FI5,
499 .radio_type = UNSET,
500 .tuner_addr = ADDR_UNSET,
501 .radio_addr = ADDR_UNSET,
502 /*
503 GPIO[0] resets DT3302 DTV receiver
504 0 - reset asserted
505 1 - normal operation
506 GPIO[1] mutes analog audio output connector
507 0 - enable selected source
508 1 - mute
509 GPIO[2] selects source for analog audio output connector
510 0 - analog audio input connector on tab
511 1 - analog DAC output from CX23881 chip
512 GPIO[3] selects RF input connector on tuner module
513 0 - RF connector labeled CABLE
514 1 - RF connector labeled ANT
515 GPIO[4] selects high RF for QAM256 mode
516 0 - normal RF
517 1 - high RF
518 */
519 .input = {{
520 .type = CX88_VMUX_TELEVISION,
521 .vmux = 0,
522 .gpio0 = 0x0f0d,
523 },{
524 .type = CX88_VMUX_CABLE,
525 .vmux = 0,
526 .gpio0 = 0x0f05,
527 },{
528 .type = CX88_VMUX_COMPOSITE1,
529 .vmux = 1,
530 .gpio0 = 0x0f00,
531 },{
532 .type = CX88_VMUX_SVIDEO,
533 .vmux = 2,
534 .gpio0 = 0x0f00,
535 }},
536 .mpeg = CX88_MPEG_DVB,
537 },
538 [CX88_BOARD_HAUPPAUGE_DVB_T1] = {
539 .name = "Hauppauge Nova-T DVB-T",
540 .tuner_type = TUNER_ABSENT,
541 .radio_type = UNSET,
542 .tuner_addr = ADDR_UNSET,
543 .radio_addr = ADDR_UNSET,
544 .input = {{
545 .type = CX88_VMUX_DVB,
546 .vmux = 0,
547 }},
548 .mpeg = CX88_MPEG_DVB,
549 },
550 [CX88_BOARD_CONEXANT_DVB_T1] = {
551 .name = "Conexant DVB-T reference design",
552 .tuner_type = TUNER_ABSENT,
553 .radio_type = UNSET,
554 .tuner_addr = ADDR_UNSET,
555 .radio_addr = ADDR_UNSET,
556 .input = {{
557 .type = CX88_VMUX_DVB,
558 .vmux = 0,
559 }},
560 .mpeg = CX88_MPEG_DVB,
561 },
562 [CX88_BOARD_PROVIDEO_PV259] = {
563 .name = "Provideo PV259",
564 .tuner_type = TUNER_PHILIPS_FQ1216ME,
565 .radio_type = UNSET,
566 .tuner_addr = ADDR_UNSET,
567 .radio_addr = ADDR_UNSET,
568 .input = {{
569 .type = CX88_VMUX_TELEVISION,
570 .vmux = 0,
571 .audioroute = 1,
572 }},
573 .mpeg = CX88_MPEG_BLACKBIRD,
574 },
575 [CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS] = {
576 .name = "DViCO FusionHDTV DVB-T Plus",
577 .tuner_type = TUNER_ABSENT, /* No analog tuner */
578 .radio_type = UNSET,
579 .tuner_addr = ADDR_UNSET,
580 .radio_addr = ADDR_UNSET,
581 .input = {{
582 .type = CX88_VMUX_COMPOSITE1,
583 .vmux = 1,
584 .gpio0 = 0x000027df,
585 },{
586 .type = CX88_VMUX_SVIDEO,
587 .vmux = 2,
588 .gpio0 = 0x000027df,
589 }},
590 .mpeg = CX88_MPEG_DVB,
591 },
592 [CX88_BOARD_DNTV_LIVE_DVB_T] = {
593 .name = "digitalnow DNTV Live! DVB-T",
594 .tuner_type = TUNER_ABSENT,
595 .radio_type = UNSET,
596 .tuner_addr = ADDR_UNSET,
597 .radio_addr = ADDR_UNSET,
598 .input = {{
599 .type = CX88_VMUX_COMPOSITE1,
600 .vmux = 1,
601 .gpio0 = 0x00000700,
602 .gpio2 = 0x00000101,
603 },{
604 .type = CX88_VMUX_SVIDEO,
605 .vmux = 2,
606 .gpio0 = 0x00000700,
607 .gpio2 = 0x00000101,
608 }},
609 .mpeg = CX88_MPEG_DVB,
610 },
611 [CX88_BOARD_PCHDTV_HD3000] = {
612 .name = "pcHDTV HD3000 HDTV",
613 .tuner_type = TUNER_THOMSON_DTT761X,
614 .radio_type = UNSET,
615 .tuner_addr = ADDR_UNSET,
616 .radio_addr = ADDR_UNSET,
617 .tda9887_conf = TDA9887_PRESENT,
618 /* GPIO[2] = audio source for analog audio out connector
619 * 0 = analog audio input connector
620 * 1 = CX88 audio DACs
621 *
622 * GPIO[7] = input to CX88's audio/chroma ADC
623 * 0 = FM 10.7 MHz IF
624 * 1 = Sound 4.5 MHz IF
625 *
626 * GPIO[1,5,6] = Oren 51132 pins 27,35,28 respectively
627 *
628 * GPIO[16] = Remote control input
629 */
630 .input = {{
631 .type = CX88_VMUX_TELEVISION,
632 .vmux = 0,
633 .gpio0 = 0x00008484,
634 },{
635 .type = CX88_VMUX_COMPOSITE1,
636 .vmux = 1,
637 .gpio0 = 0x00008400,
638 },{
639 .type = CX88_VMUX_SVIDEO,
640 .vmux = 2,
641 .gpio0 = 0x00008400,
642 }},
643 .radio = {
644 .type = CX88_RADIO,
645 .gpio0 = 0x00008404,
646 },
647 .mpeg = CX88_MPEG_DVB,
648 },
649 [CX88_BOARD_HAUPPAUGE_ROSLYN] = {
650 // entry added by Kaustubh D. Bhalerao <bhalerao.1@osu.edu>
651 // GPIO values obtained from regspy, courtesy Sean Covel
652 .name = "Hauppauge WinTV 28xxx (Roslyn) models",
653 .tuner_type = UNSET,
654 .radio_type = UNSET,
655 .tuner_addr = ADDR_UNSET,
656 .radio_addr = ADDR_UNSET,
657 .input = {{
658 .type = CX88_VMUX_TELEVISION,
659 .vmux = 0,
660 .gpio0 = 0xed1a,
661 .gpio2 = 0x00ff,
662 },{
663 .type = CX88_VMUX_DEBUG,
664 .vmux = 0,
665 .gpio0 = 0xff01,
666 },{
667 .type = CX88_VMUX_COMPOSITE1,
668 .vmux = 1,
669 .gpio0 = 0xff02,
670 },{
671 .type = CX88_VMUX_SVIDEO,
672 .vmux = 2,
673 .gpio0 = 0xed92,
674 .gpio2 = 0x00ff,
675 }},
676 .radio = {
677 .type = CX88_RADIO,
678 .gpio0 = 0xed96,
679 .gpio2 = 0x00ff,
680 },
681 .mpeg = CX88_MPEG_BLACKBIRD,
682 },
683 [CX88_BOARD_DIGITALLOGIC_MEC] = {
684 .name = "Digital-Logic MICROSPACE Entertainment Center (MEC)",
685 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
686 .radio_type = UNSET,
687 .tuner_addr = ADDR_UNSET,
688 .radio_addr = ADDR_UNSET,
689 .tda9887_conf = TDA9887_PRESENT,
690 .input = {{
691 .type = CX88_VMUX_TELEVISION,
692 .vmux = 0,
693 .gpio0 = 0x00009d80,
694 .audioroute = 1,
695 },{
696 .type = CX88_VMUX_COMPOSITE1,
697 .vmux = 1,
698 .gpio0 = 0x00009d76,
699 .audioroute = 1,
700 },{
701 .type = CX88_VMUX_SVIDEO,
702 .vmux = 2,
703 .gpio0 = 0x00009d76,
704 .audioroute = 1,
705 }},
706 .radio = {
707 .type = CX88_RADIO,
708 .gpio0 = 0x00009d00,
709 .audioroute = 1,
710 },
711 .mpeg = CX88_MPEG_BLACKBIRD,
712 },
713 [CX88_BOARD_IODATA_GVBCTV7E] = {
714 .name = "IODATA GV/BCTV7E",
715 .tuner_type = TUNER_PHILIPS_FQ1286,
716 .radio_type = UNSET,
717 .tuner_addr = ADDR_UNSET,
718 .radio_addr = ADDR_UNSET,
719 .tda9887_conf = TDA9887_PRESENT,
720 .input = {{
721 .type = CX88_VMUX_TELEVISION,
722 .vmux = 1,
723 .gpio1 = 0x0000e03f,
724 },{
725 .type = CX88_VMUX_COMPOSITE1,
726 .vmux = 2,
727 .gpio1 = 0x0000e07f,
728 },{
729 .type = CX88_VMUX_SVIDEO,
730 .vmux = 3,
731 .gpio1 = 0x0000e07f,
732 }}
733 },
734 [CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO] = {
735 .name = "PixelView PlayTV Ultra Pro (Stereo)",
736 /* May be also TUNER_YMEC_TVF_5533MF for NTSC/M or PAL/M */
737 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
738 .radio_type = UNSET,
739 .tuner_addr = ADDR_UNSET,
740 .radio_addr = ADDR_UNSET,
741 /* Some variants use a tda9874 and so need the tvaudio module. */
742 .audio_chip = V4L2_IDENT_TVAUDIO,
743 .input = {{
744 .type = CX88_VMUX_TELEVISION,
745 .vmux = 0,
746 .gpio0 = 0xbf61, /* internal decoder */
747 },{
748 .type = CX88_VMUX_COMPOSITE1,
749 .vmux = 1,
750 .gpio0 = 0xbf63,
751 },{
752 .type = CX88_VMUX_SVIDEO,
753 .vmux = 2,
754 .gpio0 = 0xbf63,
755 }},
756 .radio = {
757 .type = CX88_RADIO,
758 .gpio0 = 0xbf60,
759 },
760 },
761 [CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T] = {
762 .name = "DViCO FusionHDTV 3 Gold-T",
763 .tuner_type = TUNER_THOMSON_DTT761X,
764 .radio_type = UNSET,
765 .tuner_addr = ADDR_UNSET,
766 .radio_addr = ADDR_UNSET,
767 .tda9887_conf = TDA9887_PRESENT,
768 .input = {{
769 .type = CX88_VMUX_TELEVISION,
770 .vmux = 0,
771 .gpio0 = 0x97ed,
772 },{
773 .type = CX88_VMUX_COMPOSITE1,
774 .vmux = 1,
775 .gpio0 = 0x97e9,
776 },{
777 .type = CX88_VMUX_SVIDEO,
778 .vmux = 2,
779 .gpio0 = 0x97e9,
780 }},
781 .mpeg = CX88_MPEG_DVB,
782 },
783 [CX88_BOARD_ADSTECH_DVB_T_PCI] = {
784 .name = "ADS Tech Instant TV DVB-T PCI",
785 .tuner_type = TUNER_ABSENT,
786 .radio_type = UNSET,
787 .tuner_addr = ADDR_UNSET,
788 .radio_addr = ADDR_UNSET,
789 .input = {{
790 .type = CX88_VMUX_COMPOSITE1,
791 .vmux = 1,
792 .gpio0 = 0x0700,
793 .gpio2 = 0x0101,
794 },{
795 .type = CX88_VMUX_SVIDEO,
796 .vmux = 2,
797 .gpio0 = 0x0700,
798 .gpio2 = 0x0101,
799 }},
800 .mpeg = CX88_MPEG_DVB,
801 },
802 [CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1] = {
803 .name = "TerraTec Cinergy 1400 DVB-T",
804 .tuner_type = TUNER_ABSENT,
805 .input = {{
806 .type = CX88_VMUX_DVB,
807 .vmux = 0,
808 },{
809 .type = CX88_VMUX_COMPOSITE1,
810 .vmux = 2,
811 },{
812 .type = CX88_VMUX_SVIDEO,
813 .vmux = 2,
814 }},
815 .mpeg = CX88_MPEG_DVB,
816 },
817 [CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD] = {
818 .name = "DViCO FusionHDTV 5 Gold",
819 .tuner_type = TUNER_LG_TDVS_H06XF, /* TDVS-H062F */
820 .radio_type = UNSET,
821 .tuner_addr = ADDR_UNSET,
822 .radio_addr = ADDR_UNSET,
823 .tda9887_conf = TDA9887_PRESENT,
824 .input = {{
825 .type = CX88_VMUX_TELEVISION,
826 .vmux = 0,
827 .gpio0 = 0x87fd,
828 },{
829 .type = CX88_VMUX_COMPOSITE1,
830 .vmux = 1,
831 .gpio0 = 0x87f9,
832 },{
833 .type = CX88_VMUX_SVIDEO,
834 .vmux = 2,
835 .gpio0 = 0x87f9,
836 }},
837 .mpeg = CX88_MPEG_DVB,
838 },
839 [CX88_BOARD_AVERMEDIA_ULTRATV_MC_550] = {
840 .name = "AverMedia UltraTV Media Center PCI 550",
841 .tuner_type = TUNER_PHILIPS_FM1236_MK3,
842 .radio_type = UNSET,
843 .tuner_addr = ADDR_UNSET,
844 .radio_addr = ADDR_UNSET,
845 .tda9887_conf = TDA9887_PRESENT,
846 .input = {{
847 .type = CX88_VMUX_COMPOSITE1,
848 .vmux = 0,
849 .gpio0 = 0x0000cd73,
850 .audioroute = 1,
851 },{
852 .type = CX88_VMUX_SVIDEO,
853 .vmux = 1,
854 .gpio0 = 0x0000cd73,
855 .audioroute = 1,
856 },{
857 .type = CX88_VMUX_TELEVISION,
858 .vmux = 3,
859 .gpio0 = 0x0000cdb3,
860 .audioroute = 1,
861 }},
862 .radio = {
863 .type = CX88_RADIO,
864 .vmux = 2,
865 .gpio0 = 0x0000cdf3,
866 .audioroute = 1,
867 },
868 .mpeg = CX88_MPEG_BLACKBIRD,
869 },
870 [CX88_BOARD_KWORLD_VSTREAM_EXPERT_DVD] = {
871 /* Alexander Wold <awold@bigfoot.com> */
872 .name = "Kworld V-Stream Xpert DVD",
873 .tuner_type = UNSET,
874 .input = {{
875 .type = CX88_VMUX_COMPOSITE1,
876 .vmux = 1,
877 .gpio0 = 0x03000000,
878 .gpio1 = 0x01000000,
879 .gpio2 = 0x02000000,
880 .gpio3 = 0x00100000,
881 },{
882 .type = CX88_VMUX_SVIDEO,
883 .vmux = 2,
884 .gpio0 = 0x03000000,
885 .gpio1 = 0x01000000,
886 .gpio2 = 0x02000000,
887 .gpio3 = 0x00100000,
888 }},
889 },
890 [CX88_BOARD_ATI_HDTVWONDER] = {
891 .name = "ATI HDTV Wonder",
892 .tuner_type = TUNER_PHILIPS_TUV1236D,
893 .radio_type = UNSET,
894 .tuner_addr = ADDR_UNSET,
895 .radio_addr = ADDR_UNSET,
896 .input = {{
897 .type = CX88_VMUX_TELEVISION,
898 .vmux = 0,
899 .gpio0 = 0x00000ff7,
900 .gpio1 = 0x000000ff,
901 .gpio2 = 0x00000001,
902 .gpio3 = 0x00000000,
903 },{
904 .type = CX88_VMUX_COMPOSITE1,
905 .vmux = 1,
906 .gpio0 = 0x00000ffe,
907 .gpio1 = 0x000000ff,
908 .gpio2 = 0x00000001,
909 .gpio3 = 0x00000000,
910 },{
911 .type = CX88_VMUX_SVIDEO,
912 .vmux = 2,
913 .gpio0 = 0x00000ffe,
914 .gpio1 = 0x000000ff,
915 .gpio2 = 0x00000001,
916 .gpio3 = 0x00000000,
917 }},
918 .mpeg = CX88_MPEG_DVB,
919 },
920 [CX88_BOARD_WINFAST_DTV1000] = {
921 .name = "WinFast DTV1000-T",
922 .tuner_type = TUNER_ABSENT,
923 .radio_type = UNSET,
924 .tuner_addr = ADDR_UNSET,
925 .radio_addr = ADDR_UNSET,
926 .input = {{
927 .type = CX88_VMUX_DVB,
928 .vmux = 0,
929 },{
930 .type = CX88_VMUX_COMPOSITE1,
931 .vmux = 1,
932 },{
933 .type = CX88_VMUX_SVIDEO,
934 .vmux = 2,
935 }},
936 .mpeg = CX88_MPEG_DVB,
937 },
938 [CX88_BOARD_AVERTV_303] = {
939 .name = "AVerTV 303 (M126)",
940 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
941 .radio_type = UNSET,
942 .tuner_addr = ADDR_UNSET,
943 .radio_addr = ADDR_UNSET,
944 .tda9887_conf = TDA9887_PRESENT,
945 .input = {{
946 .type = CX88_VMUX_TELEVISION,
947 .vmux = 0,
948 .gpio0 = 0x00ff,
949 .gpio1 = 0xe09f,
950 .gpio2 = 0x0010,
951 .gpio3 = 0x0000,
952 },{
953 .type = CX88_VMUX_COMPOSITE1,
954 .vmux = 1,
955 .gpio0 = 0x00ff,
956 .gpio1 = 0xe05f,
957 .gpio2 = 0x0010,
958 .gpio3 = 0x0000,
959 },{
960 .type = CX88_VMUX_SVIDEO,
961 .vmux = 2,
962 .gpio0 = 0x00ff,
963 .gpio1 = 0xe05f,
964 .gpio2 = 0x0010,
965 .gpio3 = 0x0000,
966 }},
967 },
968 [CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1] = {
969 .name = "Hauppauge Nova-S-Plus DVB-S",
970 .tuner_type = TUNER_ABSENT,
971 .radio_type = UNSET,
972 .tuner_addr = ADDR_UNSET,
973 .radio_addr = ADDR_UNSET,
974 .audio_chip = V4L2_IDENT_WM8775,
975 .i2sinputcntl = 2,
976 .input = {{
977 .type = CX88_VMUX_DVB,
978 .vmux = 0,
979 /* 2: Line-In */
980 .audioroute = 2,
981 },{
982 .type = CX88_VMUX_COMPOSITE1,
983 .vmux = 1,
984 /* 2: Line-In */
985 .audioroute = 2,
986 },{
987 .type = CX88_VMUX_SVIDEO,
988 .vmux = 2,
989 /* 2: Line-In */
990 .audioroute = 2,
991 }},
992 .mpeg = CX88_MPEG_DVB,
993 },
994 [CX88_BOARD_HAUPPAUGE_NOVASE2_S1] = {
995 .name = "Hauppauge Nova-SE2 DVB-S",
996 .tuner_type = TUNER_ABSENT,
997 .radio_type = UNSET,
998 .tuner_addr = ADDR_UNSET,
999 .radio_addr = ADDR_UNSET,
1000 .input = {{
1001 .type = CX88_VMUX_DVB,
1002 .vmux = 0,
1003 }},
1004 .mpeg = CX88_MPEG_DVB,
1005 },
1006 [CX88_BOARD_KWORLD_DVBS_100] = {
1007 .name = "KWorld DVB-S 100",
1008 .tuner_type = TUNER_ABSENT,
1009 .radio_type = UNSET,
1010 .tuner_addr = ADDR_UNSET,
1011 .radio_addr = ADDR_UNSET,
1012 .audio_chip = V4L2_IDENT_WM8775,
1013 .input = {{
1014 .type = CX88_VMUX_DVB,
1015 .vmux = 0,
1016 /* 2: Line-In */
1017 .audioroute = 2,
1018 },{
1019 .type = CX88_VMUX_COMPOSITE1,
1020 .vmux = 1,
1021 /* 2: Line-In */
1022 .audioroute = 2,
1023 },{
1024 .type = CX88_VMUX_SVIDEO,
1025 .vmux = 2,
1026 /* 2: Line-In */
1027 .audioroute = 2,
1028 }},
1029 .mpeg = CX88_MPEG_DVB,
1030 },
1031 [CX88_BOARD_HAUPPAUGE_HVR1100] = {
1032 .name = "Hauppauge WinTV-HVR1100 DVB-T/Hybrid",
1033 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
1034 .radio_type = UNSET,
1035 .tuner_addr = ADDR_UNSET,
1036 .radio_addr = ADDR_UNSET,
1037 .tda9887_conf = TDA9887_PRESENT,
1038 .input = {{
1039 .type = CX88_VMUX_TELEVISION,
1040 .vmux = 0,
1041 },{
1042 .type = CX88_VMUX_COMPOSITE1,
1043 .vmux = 1,
1044 },{
1045 .type = CX88_VMUX_SVIDEO,
1046 .vmux = 2,
1047 }},
1048 /* fixme: Add radio support */
1049 .mpeg = CX88_MPEG_DVB,
1050 },
1051 [CX88_BOARD_HAUPPAUGE_HVR1100LP] = {
1052 .name = "Hauppauge WinTV-HVR1100 DVB-T/Hybrid (Low Profile)",
1053 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
1054 .radio_type = UNSET,
1055 .tuner_addr = ADDR_UNSET,
1056 .radio_addr = ADDR_UNSET,
1057 .tda9887_conf = TDA9887_PRESENT,
1058 .input = {{
1059 .type = CX88_VMUX_TELEVISION,
1060 .vmux = 0,
1061 },{
1062 .type = CX88_VMUX_COMPOSITE1,
1063 .vmux = 1,
1064 }},
1065 /* fixme: Add radio support */
1066 .mpeg = CX88_MPEG_DVB,
1067 },
1068 [CX88_BOARD_DNTV_LIVE_DVB_T_PRO] = {
1069 .name = "digitalnow DNTV Live! DVB-T Pro",
1070 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
1071 .radio_type = UNSET,
1072 .tuner_addr = ADDR_UNSET,
1073 .radio_addr = ADDR_UNSET,
1074 .tda9887_conf = TDA9887_PRESENT | TDA9887_PORT1_ACTIVE |
1075 TDA9887_PORT2_ACTIVE,
1076 .input = {{
1077 .type = CX88_VMUX_TELEVISION,
1078 .vmux = 0,
1079 .gpio0 = 0xf80808,
1080 },{
1081 .type = CX88_VMUX_COMPOSITE1,
1082 .vmux = 1,
1083 .gpio0 = 0xf80808,
1084 },{
1085 .type = CX88_VMUX_SVIDEO,
1086 .vmux = 2,
1087 .gpio0 = 0xf80808,
1088 }},
1089 .radio = {
1090 .type = CX88_RADIO,
1091 .gpio0 = 0xf80808,
1092 },
1093 .mpeg = CX88_MPEG_DVB,
1094 },
1095 [CX88_BOARD_KWORLD_DVB_T_CX22702] = {
1096 /* Kworld V-stream Xpert DVB-T with Thomson tuner */
1097 /* DTT 7579 Conexant CX22702-19 Conexant CX2388x */
1098 /* Manenti Marco <marco_manenti@colman.it> */
1099 .name = "KWorld/VStream XPert DVB-T with cx22702",
1100 .tuner_type = TUNER_ABSENT,
1101 .radio_type = UNSET,
1102 .tuner_addr = ADDR_UNSET,
1103 .radio_addr = ADDR_UNSET,
1104 .input = {{
1105 .type = CX88_VMUX_COMPOSITE1,
1106 .vmux = 1,
1107 .gpio0 = 0x0700,
1108 .gpio2 = 0x0101,
1109 },{
1110 .type = CX88_VMUX_SVIDEO,
1111 .vmux = 2,
1112 .gpio0 = 0x0700,
1113 .gpio2 = 0x0101,
1114 }},
1115 .mpeg = CX88_MPEG_DVB,
1116 },
1117 [CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL] = {
1118 .name = "DViCO FusionHDTV DVB-T Dual Digital",
1119 .tuner_type = TUNER_ABSENT, /* No analog tuner */
1120 .radio_type = UNSET,
1121 .tuner_addr = ADDR_UNSET,
1122 .radio_addr = ADDR_UNSET,
1123 .input = {{
1124 .type = CX88_VMUX_COMPOSITE1,
1125 .vmux = 1,
1126 .gpio0 = 0x000067df,
1127 },{
1128 .type = CX88_VMUX_SVIDEO,
1129 .vmux = 2,
1130 .gpio0 = 0x000067df,
1131 }},
1132 .mpeg = CX88_MPEG_DVB,
1133 },
1134 [CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT] = {
1135 .name = "KWorld HardwareMpegTV XPert",
1136 .tuner_type = TUNER_PHILIPS_TDA8290,
1137 .radio_type = UNSET,
1138 .tuner_addr = ADDR_UNSET,
1139 .radio_addr = ADDR_UNSET,
1140 .input = {{
1141 .type = CX88_VMUX_TELEVISION,
1142 .vmux = 0,
1143 .gpio0 = 0x3de2,
1144 .gpio2 = 0x00ff,
1145 },{
1146 .type = CX88_VMUX_COMPOSITE1,
1147 .vmux = 1,
1148 .gpio0 = 0x3de6,
1149 .audioroute = 1,
1150 },{
1151 .type = CX88_VMUX_SVIDEO,
1152 .vmux = 2,
1153 .gpio0 = 0x3de6,
1154 .audioroute = 1,
1155 }},
1156 .radio = {
1157 .type = CX88_RADIO,
1158 .gpio0 = 0x3de6,
1159 .gpio2 = 0x00ff,
1160 },
1161 .mpeg = CX88_MPEG_BLACKBIRD,
1162 },
1163 [CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID] = {
1164 .name = "DViCO FusionHDTV DVB-T Hybrid",
1165 .tuner_type = TUNER_THOMSON_FE6600,
1166 .radio_type = UNSET,
1167 .tuner_addr = ADDR_UNSET,
1168 .radio_addr = ADDR_UNSET,
1169 .input = {{
1170 .type = CX88_VMUX_TELEVISION,
1171 .vmux = 0,
1172 .gpio0 = 0x0000a75f,
1173 },{
1174 .type = CX88_VMUX_COMPOSITE1,
1175 .vmux = 1,
1176 .gpio0 = 0x0000a75b,
1177 },{
1178 .type = CX88_VMUX_SVIDEO,
1179 .vmux = 2,
1180 .gpio0 = 0x0000a75b,
1181 }},
1182 .mpeg = CX88_MPEG_DVB,
1183 },
1184 [CX88_BOARD_PCHDTV_HD5500] = {
1185 .name = "pcHDTV HD5500 HDTV",
1186 .tuner_type = TUNER_LG_TDVS_H06XF, /* TDVS-H064F */
1187 .radio_type = UNSET,
1188 .tuner_addr = ADDR_UNSET,
1189 .radio_addr = ADDR_UNSET,
1190 .tda9887_conf = TDA9887_PRESENT,
1191 .input = {{
1192 .type = CX88_VMUX_TELEVISION,
1193 .vmux = 0,
1194 .gpio0 = 0x87fd,
1195 },{
1196 .type = CX88_VMUX_COMPOSITE1,
1197 .vmux = 1,
1198 .gpio0 = 0x87f9,
1199 },{
1200 .type = CX88_VMUX_SVIDEO,
1201 .vmux = 2,
1202 .gpio0 = 0x87f9,
1203 }},
1204 .mpeg = CX88_MPEG_DVB,
1205 },
1206 [CX88_BOARD_KWORLD_MCE200_DELUXE] = {
1207 /* FIXME: tested TV input only, disabled composite,
1208 svideo and radio until they can be tested also. */
1209 .name = "Kworld MCE 200 Deluxe",
1210 .tuner_type = TUNER_TENA_9533_DI,
1211 .radio_type = UNSET,
1212 .tda9887_conf = TDA9887_PRESENT,
1213 .tuner_addr = ADDR_UNSET,
1214 .radio_addr = ADDR_UNSET,
1215 .input = {{
1216 .type = CX88_VMUX_TELEVISION,
1217 .vmux = 0,
1218 .gpio0 = 0x0000BDE6
1219 }},
1220 .mpeg = CX88_MPEG_BLACKBIRD,
1221 },
1222 [CX88_BOARD_PIXELVIEW_PLAYTV_P7000] = {
1223 /* FIXME: SVideo, Composite and FM inputs are untested */
1224 .name = "PixelView PlayTV P7000",
1225 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
1226 .radio_type = UNSET,
1227 .tuner_addr = ADDR_UNSET,
1228 .radio_addr = ADDR_UNSET,
1229 .tda9887_conf = TDA9887_PRESENT | TDA9887_PORT1_ACTIVE |
1230 TDA9887_PORT2_ACTIVE,
1231 .input = {{
1232 .type = CX88_VMUX_TELEVISION,
1233 .vmux = 0,
1234 .gpio0 = 0x5da6,
1235 }},
1236 .mpeg = CX88_MPEG_BLACKBIRD,
1237 },
1238 [CX88_BOARD_NPGTECH_REALTV_TOP10FM] = {
1239 .name = "NPG Tech Real TV FM Top 10",
1240 .tuner_type = TUNER_TNF_5335MF, /* Actually a TNF9535 */
1241 .radio_type = UNSET,
1242 .tuner_addr = ADDR_UNSET,
1243 .radio_addr = ADDR_UNSET,
1244 .input = {{
1245 .type = CX88_VMUX_TELEVISION,
1246 .vmux = 0,
1247 .gpio0 = 0x0788,
1248 },{
1249 .type = CX88_VMUX_COMPOSITE1,
1250 .vmux = 1,
1251 .gpio0 = 0x078b,
1252 },{
1253 .type = CX88_VMUX_SVIDEO,
1254 .vmux = 2,
1255 .gpio0 = 0x078b,
1256 }},
1257 .radio = {
1258 .type = CX88_RADIO,
1259 .gpio0 = 0x074a,
1260 },
1261 },
1262 [CX88_BOARD_WINFAST_DTV2000H] = {
1263 .name = "WinFast DTV2000 H",
1264 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
1265 .radio_type = UNSET,
1266 .tuner_addr = ADDR_UNSET,
1267 .radio_addr = ADDR_UNSET,
1268 .tda9887_conf = TDA9887_PRESENT,
1269 .input = {{
1270 .type = CX88_VMUX_TELEVISION,
1271 .vmux = 0,
1272 .gpio0 = 0x00017304,
1273 .gpio1 = 0x00008203,
1274 .gpio2 = 0x00017304,
1275 .gpio3 = 0x02000000,
1276 }, {
1277 .type = CX88_VMUX_COMPOSITE1,
1278 .vmux = 1,
1279 .gpio0 = 0x0001d701,
1280 .gpio1 = 0x0000b207,
1281 .gpio2 = 0x0001d701,
1282 .gpio3 = 0x02000000,
1283 }, {
1284 .type = CX88_VMUX_COMPOSITE2,
1285 .vmux = 2,
1286 .gpio0 = 0x0001d503,
1287 .gpio1 = 0x0000b207,
1288 .gpio2 = 0x0001d503,
1289 .gpio3 = 0x02000000,
1290 }, {
1291 .type = CX88_VMUX_SVIDEO,
1292 .vmux = 3,
1293 .gpio0 = 0x0001d701,
1294 .gpio1 = 0x0000b207,
1295 .gpio2 = 0x0001d701,
1296 .gpio3 = 0x02000000,
1297 }},
1298 .radio = {
1299 .type = CX88_RADIO,
1300 .gpio0 = 0x00015702,
1301 .gpio1 = 0x0000f207,
1302 .gpio2 = 0x00015702,
1303 .gpio3 = 0x02000000,
1304 },
1305 .mpeg = CX88_MPEG_DVB,
1306 },
1307 [CX88_BOARD_WINFAST_DTV2000H_J] = {
1308 .name = "WinFast DTV2000 H rev. J",
1309 .tuner_type = TUNER_PHILIPS_FMD1216MEX_MK3,
1310 .radio_type = UNSET,
1311 .tuner_addr = ADDR_UNSET,
1312 .radio_addr = ADDR_UNSET,
1313 .tda9887_conf = TDA9887_PRESENT,
1314 .input = {{
1315 .type = CX88_VMUX_TELEVISION,
1316 .vmux = 0,
1317 .gpio0 = 0x00017300,
1318 .gpio1 = 0x00008207,
1319 .gpio2 = 0x00000000,
1320 .gpio3 = 0x02000000,
1321 },{
1322 .type = CX88_VMUX_TELEVISION,
1323 .vmux = 0,
1324 .gpio0 = 0x00018300,
1325 .gpio1 = 0x0000f207,
1326 .gpio2 = 0x00017304,
1327 .gpio3 = 0x02000000,
1328 },{
1329 .type = CX88_VMUX_COMPOSITE1,
1330 .vmux = 1,
1331 .gpio0 = 0x00018301,
1332 .gpio1 = 0x0000f207,
1333 .gpio2 = 0x00017304,
1334 .gpio3 = 0x02000000,
1335 },{
1336 .type = CX88_VMUX_SVIDEO,
1337 .vmux = 2,
1338 .gpio0 = 0x00018301,
1339 .gpio1 = 0x0000f207,
1340 .gpio2 = 0x00017304,
1341 .gpio3 = 0x02000000,
1342 }},
1343 .radio = {
1344 .type = CX88_RADIO,
1345 .gpio0 = 0x00015702,
1346 .gpio1 = 0x0000f207,
1347 .gpio2 = 0x00015702,
1348 .gpio3 = 0x02000000,
1349 },
1350 .mpeg = CX88_MPEG_DVB,
1351 },
1352 [CX88_BOARD_GENIATECH_DVBS] = {
1353 .name = "Geniatech DVB-S",
1354 .tuner_type = TUNER_ABSENT,
1355 .radio_type = UNSET,
1356 .tuner_addr = ADDR_UNSET,
1357 .radio_addr = ADDR_UNSET,
1358 .input = {{
1359 .type = CX88_VMUX_DVB,
1360 .vmux = 0,
1361 },{
1362 .type = CX88_VMUX_COMPOSITE1,
1363 .vmux = 1,
1364 }},
1365 .mpeg = CX88_MPEG_DVB,
1366 },
1367 [CX88_BOARD_HAUPPAUGE_HVR3000] = {
1368 .name = "Hauppauge WinTV-HVR3000 TriMode Analog/DVB-S/DVB-T",
1369 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
1370 .radio_type = UNSET,
1371 .tuner_addr = ADDR_UNSET,
1372 .radio_addr = ADDR_UNSET,
1373 .tda9887_conf = TDA9887_PRESENT,
1374 .audio_chip = V4L2_IDENT_WM8775,
1375 .input = {{
1376 .type = CX88_VMUX_TELEVISION,
1377 .vmux = 0,
1378 .gpio0 = 0x84bf,
1379 /* 1: TV Audio / FM Mono */
1380 .audioroute = 1,
1381 },{
1382 .type = CX88_VMUX_COMPOSITE1,
1383 .vmux = 1,
1384 .gpio0 = 0x84bf,
1385 /* 2: Line-In */
1386 .audioroute = 2,
1387 },{
1388 .type = CX88_VMUX_SVIDEO,
1389 .vmux = 2,
1390 .gpio0 = 0x84bf,
1391 /* 2: Line-In */
1392 .audioroute = 2,
1393 }},
1394 .radio = {
1395 .type = CX88_RADIO,
1396 .gpio0 = 0x84bf,
1397 /* 4: FM Stereo (untested) */
1398 .audioroute = 8,
1399 },
1400 .mpeg = CX88_MPEG_DVB,
1401 .num_frontends = 2,
1402 },
1403 [CX88_BOARD_NORWOOD_MICRO] = {
1404 .name = "Norwood Micro TV Tuner",
1405 .tuner_type = TUNER_TNF_5335MF,
1406 .radio_type = UNSET,
1407 .tuner_addr = ADDR_UNSET,
1408 .radio_addr = ADDR_UNSET,
1409 .input = {{
1410 .type = CX88_VMUX_TELEVISION,
1411 .vmux = 0,
1412 .gpio0 = 0x0709,
1413 },{
1414 .type = CX88_VMUX_COMPOSITE1,
1415 .vmux = 1,
1416 .gpio0 = 0x070b,
1417 },{
1418 .type = CX88_VMUX_SVIDEO,
1419 .vmux = 2,
1420 .gpio0 = 0x070b,
1421 }},
1422 },
1423 [CX88_BOARD_TE_DTV_250_OEM_SWANN] = {
1424 .name = "Shenzhen Tungsten Ages Tech TE-DTV-250 / Swann OEM",
1425 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
1426 .radio_type = UNSET,
1427 .tuner_addr = ADDR_UNSET,
1428 .radio_addr = ADDR_UNSET,
1429 .input = {{
1430 .type = CX88_VMUX_TELEVISION,
1431 .vmux = 0,
1432 .gpio0 = 0x003fffff,
1433 .gpio1 = 0x00e00000,
1434 .gpio2 = 0x003fffff,
1435 .gpio3 = 0x02000000,
1436 },{
1437 .type = CX88_VMUX_COMPOSITE1,
1438 .vmux = 1,
1439 .gpio0 = 0x003fffff,
1440 .gpio1 = 0x00e00000,
1441 .gpio2 = 0x003fffff,
1442 .gpio3 = 0x02000000,
1443 },{
1444 .type = CX88_VMUX_SVIDEO,
1445 .vmux = 2,
1446 .gpio0 = 0x003fffff,
1447 .gpio1 = 0x00e00000,
1448 .gpio2 = 0x003fffff,
1449 .gpio3 = 0x02000000,
1450 }},
1451 },
1452 [CX88_BOARD_HAUPPAUGE_HVR1300] = {
1453 .name = "Hauppauge WinTV-HVR1300 DVB-T/Hybrid MPEG Encoder",
1454 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
1455 .radio_type = UNSET,
1456 .tuner_addr = ADDR_UNSET,
1457 .radio_addr = ADDR_UNSET,
1458 .tda9887_conf = TDA9887_PRESENT,
1459 .audio_chip = V4L2_IDENT_WM8775,
1460 /*
1461 * gpio0 as reported by Mike Crash <mike AT mikecrash.com>
1462 */
1463 .input = {{
1464 .type = CX88_VMUX_TELEVISION,
1465 .vmux = 0,
1466 .gpio0 = 0xef88,
1467 /* 1: TV Audio / FM Mono */
1468 .audioroute = 1,
1469 },{
1470 .type = CX88_VMUX_COMPOSITE1,
1471 .vmux = 1,
1472 .gpio0 = 0xef88,
1473 /* 2: Line-In */
1474 .audioroute = 2,
1475 },{
1476 .type = CX88_VMUX_SVIDEO,
1477 .vmux = 2,
1478 .gpio0 = 0xef88,
1479 /* 2: Line-In */
1480 .audioroute = 2,
1481 }},
1482 .mpeg = CX88_MPEG_DVB | CX88_MPEG_BLACKBIRD,
1483 .radio = {
1484 .type = CX88_RADIO,
1485 .gpio0 = 0xef88,
1486 /* 4: FM Stereo (untested) */
1487 .audioroute = 8,
1488 },
1489 },
1490 [CX88_BOARD_SAMSUNG_SMT_7020] = {
1491 .name = "Samsung SMT 7020 DVB-S",
1492 .tuner_type = TUNER_ABSENT,
1493 .radio_type = UNSET,
1494 .tuner_addr = ADDR_UNSET,
1495 .radio_addr = ADDR_UNSET,
1496 .input = { {
1497 .type = CX88_VMUX_DVB,
1498 .vmux = 0,
1499 } },
1500 .mpeg = CX88_MPEG_DVB,
1501 },
1502 [CX88_BOARD_ADSTECH_PTV_390] = {
1503 .name = "ADS Tech Instant Video PCI",
1504 .tuner_type = TUNER_ABSENT,
1505 .radio_type = UNSET,
1506 .tuner_addr = ADDR_UNSET,
1507 .radio_addr = ADDR_UNSET,
1508 .input = {{
1509 .type = CX88_VMUX_DEBUG,
1510 .vmux = 3,
1511 .gpio0 = 0x04ff,
1512 },{
1513 .type = CX88_VMUX_COMPOSITE1,
1514 .vmux = 1,
1515 .gpio0 = 0x07fa,
1516 },{
1517 .type = CX88_VMUX_SVIDEO,
1518 .vmux = 2,
1519 .gpio0 = 0x07fa,
1520 }},
1521 },
1522 [CX88_BOARD_PINNACLE_PCTV_HD_800i] = {
1523 .name = "Pinnacle PCTV HD 800i",
1524 .tuner_type = TUNER_XC5000,
1525 .radio_type = UNSET,
1526 .tuner_addr = ADDR_UNSET,
1527 .radio_addr = ADDR_UNSET,
1528 .input = {{
1529 .type = CX88_VMUX_TELEVISION,
1530 .vmux = 0,
1531 .gpio0 = 0x04fb,
1532 .gpio1 = 0x10ff,
1533 },{
1534 .type = CX88_VMUX_COMPOSITE1,
1535 .vmux = 1,
1536 .gpio0 = 0x04fb,
1537 .gpio1 = 0x10ef,
1538 .audioroute = 1,
1539 },{
1540 .type = CX88_VMUX_SVIDEO,
1541 .vmux = 2,
1542 .gpio0 = 0x04fb,
1543 .gpio1 = 0x10ef,
1544 .audioroute = 1,
1545 }},
1546 .mpeg = CX88_MPEG_DVB,
1547 },
1548 [CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO] = {
1549 .name = "DViCO FusionHDTV 5 PCI nano",
1550 /* xc3008 tuner, digital only for now */
1551 .tuner_type = TUNER_ABSENT,
1552 .radio_type = UNSET,
1553 .tuner_addr = ADDR_UNSET,
1554 .radio_addr = ADDR_UNSET,
1555 .input = {{
1556 .type = CX88_VMUX_TELEVISION,
1557 .vmux = 0,
1558 .gpio0 = 0x000027df, /* Unconfirmed */
1559 }, {
1560 .type = CX88_VMUX_COMPOSITE1,
1561 .vmux = 1,
1562 .gpio0 = 0x000027df, /* Unconfirmed */
1563 .audioroute = 1,
1564 }, {
1565 .type = CX88_VMUX_SVIDEO,
1566 .vmux = 2,
1567 .gpio0 = 0x000027df, /* Unconfirmed */
1568 .audioroute = 1,
1569 } },
1570 .mpeg = CX88_MPEG_DVB,
1571 },
1572 [CX88_BOARD_PINNACLE_HYBRID_PCTV] = {
1573 .name = "Pinnacle Hybrid PCTV",
1574 .tuner_type = TUNER_XC2028,
1575 .tuner_addr = 0x61,
1576 .radio_type = UNSET,
1577 .radio_addr = ADDR_UNSET,
1578 .input = { {
1579 .type = CX88_VMUX_TELEVISION,
1580 .vmux = 0,
1581 .gpio0 = 0x004ff,
1582 .gpio1 = 0x010ff,
1583 .gpio2 = 0x00001,
1584 }, {
1585 .type = CX88_VMUX_COMPOSITE1,
1586 .vmux = 1,
1587 .gpio0 = 0x004fb,
1588 .gpio1 = 0x010ef,
1589 .audioroute = 1,
1590 }, {
1591 .type = CX88_VMUX_SVIDEO,
1592 .vmux = 2,
1593 .gpio0 = 0x004fb,
1594 .gpio1 = 0x010ef,
1595 .audioroute = 1,
1596 } },
1597 .radio = {
1598 .type = CX88_RADIO,
1599 .gpio0 = 0x004ff,
1600 .gpio1 = 0x010ff,
1601 .gpio2 = 0x0ff,
1602 },
1603 .mpeg = CX88_MPEG_DVB,
1604 },
1605 /* Terry Wu <terrywu2009@gmail.com> */
1606 /* TV Audio : set GPIO 2, 18, 19 value to 0, 1, 0 */
1607 /* FM Audio : set GPIO 2, 18, 19 value to 0, 0, 0 */
1608 /* Line-in Audio : set GPIO 2, 18, 19 value to 0, 1, 1 */
1609 /* Mute Audio : set GPIO 2 value to 1 */
1610 [CX88_BOARD_WINFAST_TV2000_XP_GLOBAL] = {
1611 .name = "Leadtek TV2000 XP Global",
1612 .tuner_type = TUNER_XC2028,
1613 .tuner_addr = 0x61,
1614 .radio_type = UNSET,
1615 .radio_addr = ADDR_UNSET,
1616 .input = { {
1617 .type = CX88_VMUX_TELEVISION,
1618 .vmux = 0,
1619 .gpio0 = 0x0400, /* pin 2 = 0 */
1620 .gpio1 = 0x0000,
1621 .gpio2 = 0x0C04, /* pin 18 = 1, pin 19 = 0 */
1622 .gpio3 = 0x0000,
1623 }, {
1624 .type = CX88_VMUX_COMPOSITE1,
1625 .vmux = 1,
1626 .gpio0 = 0x0400, /* pin 2 = 0 */
1627 .gpio1 = 0x0000,
1628 .gpio2 = 0x0C0C, /* pin 18 = 1, pin 19 = 1 */
1629 .gpio3 = 0x0000,
1630 }, {
1631 .type = CX88_VMUX_SVIDEO,
1632 .vmux = 2,
1633 .gpio0 = 0x0400, /* pin 2 = 0 */
1634 .gpio1 = 0x0000,
1635 .gpio2 = 0x0C0C, /* pin 18 = 1, pin 19 = 1 */
1636 .gpio3 = 0x0000,
1637 } },
1638 .radio = {
1639 .type = CX88_RADIO,
1640 .gpio0 = 0x0400, /* pin 2 = 0 */
1641 .gpio1 = 0x0000,
1642 .gpio2 = 0x0C00, /* pin 18 = 0, pin 19 = 0 */
1643 .gpio3 = 0x0000,
1644 },
1645 },
1646 [CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36] = {
1647 .name = "Leadtek TV2000 XP Global (SC4100)",
1648 .tuner_type = TUNER_XC4000,
1649 .tuner_addr = 0x61,
1650 .radio_type = UNSET,
1651 .radio_addr = ADDR_UNSET,
1652 .input = { {
1653 .type = CX88_VMUX_TELEVISION,
1654 .vmux = 0,
1655 .gpio0 = 0x0400, /* pin 2 = 0 */
1656 .gpio1 = 0x0000,
1657 .gpio2 = 0x0C04, /* pin 18 = 1, pin 19 = 0 */
1658 .gpio3 = 0x0000,
1659 }, {
1660 .type = CX88_VMUX_COMPOSITE1,
1661 .vmux = 1,
1662 .gpio0 = 0x0400, /* pin 2 = 0 */
1663 .gpio1 = 0x0000,
1664 .gpio2 = 0x0C0C, /* pin 18 = 1, pin 19 = 1 */
1665 .gpio3 = 0x0000,
1666 }, {
1667 .type = CX88_VMUX_SVIDEO,
1668 .vmux = 2,
1669 .gpio0 = 0x0400, /* pin 2 = 0 */
1670 .gpio1 = 0x0000,
1671 .gpio2 = 0x0C0C, /* pin 18 = 1, pin 19 = 1 */
1672 .gpio3 = 0x0000,
1673 } },
1674 .radio = {
1675 .type = CX88_RADIO,
1676 .gpio0 = 0x0400, /* pin 2 = 0 */
1677 .gpio1 = 0x0000,
1678 .gpio2 = 0x0C00, /* pin 18 = 0, pin 19 = 0 */
1679 .gpio3 = 0x0000,
1680 },
1681 },
1682 [CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43] = {
1683 .name = "Leadtek TV2000 XP Global (XC4100)",
1684 .tuner_type = TUNER_XC4000,
1685 .tuner_addr = 0x61,
1686 .radio_type = UNSET,
1687 .radio_addr = ADDR_UNSET,
1688 .input = { {
1689 .type = CX88_VMUX_TELEVISION,
1690 .vmux = 0,
1691 .gpio0 = 0x0400, /* pin 2 = 0 */
1692 .gpio1 = 0x6040, /* pin 14 = 1, pin 13 = 0 */
1693 .gpio2 = 0x0000,
1694 .gpio3 = 0x0000,
1695 }, {
1696 .type = CX88_VMUX_COMPOSITE1,
1697 .vmux = 1,
1698 .gpio0 = 0x0400, /* pin 2 = 0 */
1699 .gpio1 = 0x6060, /* pin 14 = 1, pin 13 = 1 */
1700 .gpio2 = 0x0000,
1701 .gpio3 = 0x0000,
1702 }, {
1703 .type = CX88_VMUX_SVIDEO,
1704 .vmux = 2,
1705 .gpio0 = 0x0400, /* pin 2 = 0 */
1706 .gpio1 = 0x6060, /* pin 14 = 1, pin 13 = 1 */
1707 .gpio2 = 0x0000,
1708 .gpio3 = 0x0000,
1709 } },
1710 .radio = {
1711 .type = CX88_RADIO,
1712 .gpio0 = 0x0400, /* pin 2 = 0 */
1713 .gpio1 = 0x6000, /* pin 14 = 1, pin 13 = 0 */
1714 .gpio2 = 0x0000,
1715 .gpio3 = 0x0000,
1716 },
1717 },
1718 [CX88_BOARD_POWERCOLOR_REAL_ANGEL] = {
1719 .name = "PowerColor RA330", /* Long names may confuse LIRC. */
1720 .tuner_type = TUNER_XC2028,
1721 .tuner_addr = 0x61,
1722 .input = { {
1723 .type = CX88_VMUX_DEBUG,
1724 .vmux = 3, /* Due to the way the cx88 driver is written, */
1725 .gpio0 = 0x00ff, /* there is no way to deactivate audio pass- */
1726 .gpio1 = 0xf39d, /* through without this entry. Furthermore, if */
1727 .gpio3 = 0x0000, /* the TV mux entry is first, you get audio */
1728 }, { /* from the tuner on boot for a little while. */
1729 .type = CX88_VMUX_TELEVISION,
1730 .vmux = 0,
1731 .gpio0 = 0x00ff,
1732 .gpio1 = 0xf35d,
1733 .gpio3 = 0x0000,
1734 }, {
1735 .type = CX88_VMUX_COMPOSITE1,
1736 .vmux = 1,
1737 .gpio0 = 0x00ff,
1738 .gpio1 = 0xf37d,
1739 .gpio3 = 0x0000,
1740 }, {
1741 .type = CX88_VMUX_SVIDEO,
1742 .vmux = 2,
1743 .gpio0 = 0x000ff,
1744 .gpio1 = 0x0f37d,
1745 .gpio3 = 0x00000,
1746 } },
1747 .radio = {
1748 .type = CX88_RADIO,
1749 .gpio0 = 0x000ff,
1750 .gpio1 = 0x0f35d,
1751 .gpio3 = 0x00000,
1752 },
1753 },
1754 [CX88_BOARD_GENIATECH_X8000_MT] = {
1755 /* Also PowerColor Real Angel 330 and Geniatech X800 OEM */
1756 .name = "Geniatech X8000-MT DVBT",
1757 .tuner_type = TUNER_XC2028,
1758 .tuner_addr = 0x61,
1759 .input = { {
1760 .type = CX88_VMUX_TELEVISION,
1761 .vmux = 0,
1762 .gpio0 = 0x00000000,
1763 .gpio1 = 0x00e3e341,
1764 .gpio2 = 0x00000000,
1765 .gpio3 = 0x00000000,
1766 }, {
1767 .type = CX88_VMUX_COMPOSITE1,
1768 .vmux = 1,
1769 .gpio0 = 0x00000000,
1770 .gpio1 = 0x00e3e361,
1771 .gpio2 = 0x00000000,
1772 .gpio3 = 0x00000000,
1773 }, {
1774 .type = CX88_VMUX_SVIDEO,
1775 .vmux = 2,
1776 .gpio0 = 0x00000000,
1777 .gpio1 = 0x00e3e361,
1778 .gpio2 = 0x00000000,
1779 .gpio3 = 0x00000000,
1780 } },
1781 .radio = {
1782 .type = CX88_RADIO,
1783 .gpio0 = 0x00000000,
1784 .gpio1 = 0x00e3e341,
1785 .gpio2 = 0x00000000,
1786 .gpio3 = 0x00000000,
1787 },
1788 .mpeg = CX88_MPEG_DVB,
1789 },
1790 [CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO] = {
1791 .name = "DViCO FusionHDTV DVB-T PRO",
1792 .tuner_type = TUNER_XC2028,
1793 .tuner_addr = 0x61,
1794 .radio_type = UNSET,
1795 .radio_addr = ADDR_UNSET,
1796 .input = { {
1797 .type = CX88_VMUX_COMPOSITE1,
1798 .vmux = 1,
1799 .gpio0 = 0x000067df,
1800 }, {
1801 .type = CX88_VMUX_SVIDEO,
1802 .vmux = 2,
1803 .gpio0 = 0x000067df,
1804 } },
1805 .mpeg = CX88_MPEG_DVB,
1806 },
1807 [CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD] = {
1808 .name = "DViCO FusionHDTV 7 Gold",
1809 .tuner_type = TUNER_XC5000,
1810 .radio_type = UNSET,
1811 .tuner_addr = ADDR_UNSET,
1812 .radio_addr = ADDR_UNSET,
1813 .input = {{
1814 .type = CX88_VMUX_TELEVISION,
1815 .vmux = 0,
1816 .gpio0 = 0x10df,
1817 },{
1818 .type = CX88_VMUX_COMPOSITE1,
1819 .vmux = 1,
1820 .gpio0 = 0x16d9,
1821 },{
1822 .type = CX88_VMUX_SVIDEO,
1823 .vmux = 2,
1824 .gpio0 = 0x16d9,
1825 }},
1826 .mpeg = CX88_MPEG_DVB,
1827 },
1828 [CX88_BOARD_PROLINK_PV_8000GT] = {
1829 .name = "Prolink Pixelview MPEG 8000GT",
1830 .tuner_type = TUNER_XC2028,
1831 .tuner_addr = 0x61,
1832 .input = { {
1833 .type = CX88_VMUX_TELEVISION,
1834 .vmux = 0,
1835 .gpio0 = 0x0ff,
1836 .gpio2 = 0x0cfb,
1837 }, {
1838 .type = CX88_VMUX_COMPOSITE1,
1839 .vmux = 1,
1840 .gpio2 = 0x0cfb,
1841 }, {
1842 .type = CX88_VMUX_SVIDEO,
1843 .vmux = 2,
1844 .gpio2 = 0x0cfb,
1845 } },
1846 .radio = {
1847 .type = CX88_RADIO,
1848 .gpio2 = 0x0cfb,
1849 },
1850 },
1851 [CX88_BOARD_PROLINK_PV_GLOBAL_XTREME] = {
1852 .name = "Prolink Pixelview Global Extreme",
1853 .tuner_type = TUNER_XC2028,
1854 .tuner_addr = 0x61,
1855 .input = { {
1856 .type = CX88_VMUX_TELEVISION,
1857 .vmux = 0,
1858 .gpio0 = 0x04fb,
1859 .gpio1 = 0x04080,
1860 .gpio2 = 0x0cf7,
1861 }, {
1862 .type = CX88_VMUX_COMPOSITE1,
1863 .vmux = 1,
1864 .gpio0 = 0x04fb,
1865 .gpio1 = 0x04080,
1866 .gpio2 = 0x0cfb,
1867 }, {
1868 .type = CX88_VMUX_SVIDEO,
1869 .vmux = 2,
1870 .gpio0 = 0x04fb,
1871 .gpio1 = 0x04080,
1872 .gpio2 = 0x0cfb,
1873 } },
1874 .radio = {
1875 .type = CX88_RADIO,
1876 .gpio0 = 0x04ff,
1877 .gpio1 = 0x04080,
1878 .gpio2 = 0x0cf7,
1879 },
1880 },
1881 /* Both radio, analog and ATSC work with this board.
1882 However, for analog to work, s5h1409 gate should be open,
1883 otherwise, tuner-xc3028 won't be detected.
1884 A proper fix require using the newer i2c methods to add
1885 tuner-xc3028 without doing an i2c probe.
1886 */
1887 [CX88_BOARD_KWORLD_ATSC_120] = {
1888 .name = "Kworld PlusTV HD PCI 120 (ATSC 120)",
1889 .tuner_type = TUNER_XC2028,
1890 .radio_type = UNSET,
1891 .tuner_addr = ADDR_UNSET,
1892 .radio_addr = ADDR_UNSET,
1893 .input = { {
1894 .type = CX88_VMUX_TELEVISION,
1895 .vmux = 0,
1896 .gpio0 = 0x000000ff,
1897 .gpio1 = 0x0000f35d,
1898 .gpio2 = 0x00000000,
1899 }, {
1900 .type = CX88_VMUX_COMPOSITE1,
1901 .vmux = 1,
1902 .gpio0 = 0x000000ff,
1903 .gpio1 = 0x0000f37e,
1904 .gpio2 = 0x00000000,
1905 }, {
1906 .type = CX88_VMUX_SVIDEO,
1907 .vmux = 2,
1908 .gpio0 = 0x000000ff,
1909 .gpio1 = 0x0000f37e,
1910 .gpio2 = 0x00000000,
1911 } },
1912 .radio = {
1913 .type = CX88_RADIO,
1914 .gpio0 = 0x000000ff,
1915 .gpio1 = 0x0000f35d,
1916 .gpio2 = 0x00000000,
1917 },
1918 .mpeg = CX88_MPEG_DVB,
1919 },
1920 [CX88_BOARD_HAUPPAUGE_HVR4000] = {
1921 .name = "Hauppauge WinTV-HVR4000 DVB-S/S2/T/Hybrid",
1922 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
1923 .radio_type = UNSET,
1924 .tuner_addr = ADDR_UNSET,
1925 .radio_addr = ADDR_UNSET,
1926 .tda9887_conf = TDA9887_PRESENT,
1927 .audio_chip = V4L2_IDENT_WM8775,
1928 /*
1929 * GPIO0 (WINTV2000)
1930 *
1931 * Analogue SAT DVB-T
1932 * Antenna 0xc4bf 0xc4bb
1933 * Composite 0xc4bf 0xc4bb
1934 * S-Video 0xc4bf 0xc4bb
1935 * Composite1 0xc4ff 0xc4fb
1936 * S-Video1 0xc4ff 0xc4fb
1937 *
1938 * BIT VALUE FUNCTION GP{x}_IO
1939 * 0 1 I:?
1940 * 1 1 I:?
1941 * 2 1 O:MPEG PORT 0=DVB-T 1=DVB-S
1942 * 3 1 I:?
1943 * 4 1 I:?
1944 * 5 1 I:?
1945 * 6 0 O:INPUT SELECTOR 0=INTERNAL 1=EXPANSION
1946 * 7 1 O:DVB-T DEMOD RESET LOW
1947 *
1948 * BIT VALUE FUNCTION GP{x}_OE
1949 * 8 0 I
1950 * 9 0 I
1951 * a 1 O
1952 * b 0 I
1953 * c 0 I
1954 * d 0 I
1955 * e 1 O
1956 * f 1 O
1957 *
1958 * WM8775 ADC
1959 *
1960 * 1: TV Audio / FM Mono
1961 * 2: Line-In
1962 * 3: Line-In Expansion
1963 * 4: FM Stereo
1964 */
1965 .input = {{
1966 .type = CX88_VMUX_TELEVISION,
1967 .vmux = 0,
1968 .gpio0 = 0xc4bf,
1969 /* 1: TV Audio / FM Mono */
1970 .audioroute = 1,
1971 }, {
1972 .type = CX88_VMUX_COMPOSITE1,
1973 .vmux = 1,
1974 .gpio0 = 0xc4bf,
1975 /* 2: Line-In */
1976 .audioroute = 2,
1977 }, {
1978 .type = CX88_VMUX_SVIDEO,
1979 .vmux = 2,
1980 .gpio0 = 0xc4bf,
1981 /* 2: Line-In */
1982 .audioroute = 2,
1983 } },
1984 .radio = {
1985 .type = CX88_RADIO,
1986 .gpio0 = 0xc4bf,
1987 /* 4: FM Stereo */
1988 .audioroute = 8,
1989 },
1990 .mpeg = CX88_MPEG_DVB,
1991 .num_frontends = 2,
1992 },
1993 [CX88_BOARD_HAUPPAUGE_HVR4000LITE] = {
1994 .name = "Hauppauge WinTV-HVR4000(Lite) DVB-S/S2",
1995 .tuner_type = UNSET,
1996 .radio_type = UNSET,
1997 .tuner_addr = ADDR_UNSET,
1998 .radio_addr = ADDR_UNSET,
1999 .input = {{
2000 .type = CX88_VMUX_DVB,
2001 .vmux = 0,
2002 } },
2003 .mpeg = CX88_MPEG_DVB,
2004 },
2005 [CX88_BOARD_TEVII_S420] = {
2006 .name = "TeVii S420 DVB-S",
2007 .tuner_type = UNSET,
2008 .radio_type = UNSET,
2009 .tuner_addr = ADDR_UNSET,
2010 .radio_addr = ADDR_UNSET,
2011 .input = {{
2012 .type = CX88_VMUX_DVB,
2013 .vmux = 0,
2014 } },
2015 .mpeg = CX88_MPEG_DVB,
2016 },
2017 [CX88_BOARD_TEVII_S460] = {
2018 .name = "TeVii S460 DVB-S/S2",
2019 .tuner_type = UNSET,
2020 .radio_type = UNSET,
2021 .tuner_addr = ADDR_UNSET,
2022 .radio_addr = ADDR_UNSET,
2023 .input = {{
2024 .type = CX88_VMUX_DVB,
2025 .vmux = 0,
2026 } },
2027 .mpeg = CX88_MPEG_DVB,
2028 },
2029 [CX88_BOARD_TEVII_S464] = {
2030 .name = "TeVii S464 DVB-S/S2",
2031 .tuner_type = UNSET,
2032 .radio_type = UNSET,
2033 .tuner_addr = ADDR_UNSET,
2034 .radio_addr = ADDR_UNSET,
2035 .input = {{
2036 .type = CX88_VMUX_DVB,
2037 .vmux = 0,
2038 } },
2039 .mpeg = CX88_MPEG_DVB,
2040 },
2041 [CX88_BOARD_OMICOM_SS4_PCI] = {
2042 .name = "Omicom SS4 DVB-S/S2 PCI",
2043 .tuner_type = UNSET,
2044 .radio_type = UNSET,
2045 .tuner_addr = ADDR_UNSET,
2046 .radio_addr = ADDR_UNSET,
2047 .input = {{
2048 .type = CX88_VMUX_DVB,
2049 .vmux = 0,
2050 } },
2051 .mpeg = CX88_MPEG_DVB,
2052 },
2053 [CX88_BOARD_TBS_8910] = {
2054 .name = "TBS 8910 DVB-S",
2055 .tuner_type = UNSET,
2056 .radio_type = UNSET,
2057 .tuner_addr = ADDR_UNSET,
2058 .radio_addr = ADDR_UNSET,
2059 .input = {{
2060 .type = CX88_VMUX_DVB,
2061 .vmux = 0,
2062 } },
2063 .mpeg = CX88_MPEG_DVB,
2064 },
2065 [CX88_BOARD_TBS_8920] = {
2066 .name = "TBS 8920 DVB-S/S2",
2067 .tuner_type = TUNER_ABSENT,
2068 .radio_type = UNSET,
2069 .tuner_addr = ADDR_UNSET,
2070 .radio_addr = ADDR_UNSET,
2071 .input = {{
2072 .type = CX88_VMUX_DVB,
2073 .vmux = 0,
2074 .gpio0 = 0x8080,
2075 } },
2076 .mpeg = CX88_MPEG_DVB,
2077 },
2078 [CX88_BOARD_PROF_6200] = {
2079 .name = "Prof 6200 DVB-S",
2080 .tuner_type = UNSET,
2081 .radio_type = UNSET,
2082 .tuner_addr = ADDR_UNSET,
2083 .radio_addr = ADDR_UNSET,
2084 .input = {{
2085 .type = CX88_VMUX_DVB,
2086 .vmux = 0,
2087 } },
2088 .mpeg = CX88_MPEG_DVB,
2089 },
2090 [CX88_BOARD_PROF_7300] = {
2091 .name = "PROF 7300 DVB-S/S2",
2092 .tuner_type = UNSET,
2093 .radio_type = UNSET,
2094 .tuner_addr = ADDR_UNSET,
2095 .radio_addr = ADDR_UNSET,
2096 .input = {{
2097 .type = CX88_VMUX_DVB,
2098 .vmux = 0,
2099 } },
2100 .mpeg = CX88_MPEG_DVB,
2101 },
2102 [CX88_BOARD_SATTRADE_ST4200] = {
2103 .name = "SATTRADE ST4200 DVB-S/S2",
2104 .tuner_type = UNSET,
2105 .radio_type = UNSET,
2106 .tuner_addr = ADDR_UNSET,
2107 .radio_addr = ADDR_UNSET,
2108 .input = {{
2109 .type = CX88_VMUX_DVB,
2110 .vmux = 0,
2111 } },
2112 .mpeg = CX88_MPEG_DVB,
2113 },
2114 [CX88_BOARD_TERRATEC_CINERGY_HT_PCI_MKII] = {
2115 .name = "Terratec Cinergy HT PCI MKII",
2116 .tuner_type = TUNER_XC2028,
2117 .tuner_addr = 0x61,
2118 .radio_type = UNSET,
2119 .radio_addr = ADDR_UNSET,
2120 .input = { {
2121 .type = CX88_VMUX_TELEVISION,
2122 .vmux = 0,
2123 .gpio0 = 0x004ff,
2124 .gpio1 = 0x010ff,
2125 .gpio2 = 0x00001,
2126 }, {
2127 .type = CX88_VMUX_COMPOSITE1,
2128 .vmux = 1,
2129 .gpio0 = 0x004fb,
2130 .gpio1 = 0x010ef,
2131 .audioroute = 1,
2132 }, {
2133 .type = CX88_VMUX_SVIDEO,
2134 .vmux = 2,
2135 .gpio0 = 0x004fb,
2136 .gpio1 = 0x010ef,
2137 .audioroute = 1,
2138 } },
2139 .radio = {
2140 .type = CX88_RADIO,
2141 .gpio0 = 0x004ff,
2142 .gpio1 = 0x010ff,
2143 .gpio2 = 0x0ff,
2144 },
2145 .mpeg = CX88_MPEG_DVB,
2146 },
2147 [CX88_BOARD_HAUPPAUGE_IRONLY] = {
2148 .name = "Hauppauge WinTV-IR Only",
2149 .tuner_type = UNSET,
2150 .radio_type = UNSET,
2151 .tuner_addr = ADDR_UNSET,
2152 .radio_addr = ADDR_UNSET,
2153 },
2154 [CX88_BOARD_WINFAST_DTV1800H] = {
2155 .name = "Leadtek WinFast DTV1800 Hybrid",
2156 .tuner_type = TUNER_XC2028,
2157 .radio_type = UNSET,
2158 .tuner_addr = 0x61,
2159 .radio_addr = ADDR_UNSET,
2160 /*
2161 * GPIO setting
2162 *
2163 * 2: mute (0=off,1=on)
2164 * 12: tuner reset pin
2165 * 13: audio source (0=tuner audio,1=line in)
2166 * 14: FM (0=on,1=off ???)
2167 */
2168 .input = {{
2169 .type = CX88_VMUX_TELEVISION,
2170 .vmux = 0,
2171 .gpio0 = 0x0400, /* pin 2 = 0 */
2172 .gpio1 = 0x6040, /* pin 13 = 0, pin 14 = 1 */
2173 .gpio2 = 0x0000,
2174 }, {
2175 .type = CX88_VMUX_COMPOSITE1,
2176 .vmux = 1,
2177 .gpio0 = 0x0400, /* pin 2 = 0 */
2178 .gpio1 = 0x6060, /* pin 13 = 1, pin 14 = 1 */
2179 .gpio2 = 0x0000,
2180 }, {
2181 .type = CX88_VMUX_SVIDEO,
2182 .vmux = 2,
2183 .gpio0 = 0x0400, /* pin 2 = 0 */
2184 .gpio1 = 0x6060, /* pin 13 = 1, pin 14 = 1 */
2185 .gpio2 = 0x0000,
2186 } },
2187 .radio = {
2188 .type = CX88_RADIO,
2189 .gpio0 = 0x0400, /* pin 2 = 0 */
2190 .gpio1 = 0x6000, /* pin 13 = 0, pin 14 = 0 */
2191 .gpio2 = 0x0000,
2192 },
2193 .mpeg = CX88_MPEG_DVB,
2194 },
2195 [CX88_BOARD_WINFAST_DTV1800H_XC4000] = {
2196 .name = "Leadtek WinFast DTV1800 H (XC4000)",
2197 .tuner_type = TUNER_XC4000,
2198 .radio_type = UNSET,
2199 .tuner_addr = 0x61,
2200 .radio_addr = ADDR_UNSET,
2201 /*
2202 * GPIO setting
2203 *
2204 * 2: mute (0=off,1=on)
2205 * 12: tuner reset pin
2206 * 13: audio source (0=tuner audio,1=line in)
2207 * 14: FM (0=on,1=off ???)
2208 */
2209 .input = {{
2210 .type = CX88_VMUX_TELEVISION,
2211 .vmux = 0,
2212 .gpio0 = 0x0400, /* pin 2 = 0 */
2213 .gpio1 = 0x6040, /* pin 13 = 0, pin 14 = 1 */
2214 .gpio2 = 0x0000,
2215 }, {
2216 .type = CX88_VMUX_COMPOSITE1,
2217 .vmux = 1,
2218 .gpio0 = 0x0400, /* pin 2 = 0 */
2219 .gpio1 = 0x6060, /* pin 13 = 1, pin 14 = 1 */
2220 .gpio2 = 0x0000,
2221 }, {
2222 .type = CX88_VMUX_SVIDEO,
2223 .vmux = 2,
2224 .gpio0 = 0x0400, /* pin 2 = 0 */
2225 .gpio1 = 0x6060, /* pin 13 = 1, pin 14 = 1 */
2226 .gpio2 = 0x0000,
2227 }},
2228 .radio = {
2229 .type = CX88_RADIO,
2230 .gpio0 = 0x0400, /* pin 2 = 0 */
2231 .gpio1 = 0x6000, /* pin 13 = 0, pin 14 = 0 */
2232 .gpio2 = 0x0000,
2233 },
2234 .mpeg = CX88_MPEG_DVB,
2235 },
2236 [CX88_BOARD_WINFAST_DTV2000H_PLUS] = {
2237 .name = "Leadtek WinFast DTV2000 H PLUS",
2238 .tuner_type = TUNER_XC4000,
2239 .radio_type = UNSET,
2240 .tuner_addr = 0x61,
2241 .radio_addr = ADDR_UNSET,
2242 /*
2243 * GPIO
2244 * 2: 1: mute audio
2245 * 12: 0: reset XC4000
2246 * 13: 1: audio input is line in (0: tuner)
2247 * 14: 0: FM radio
2248 * 16: 0: RF input is cable
2249 */
2250 .input = {{
2251 .type = CX88_VMUX_TELEVISION,
2252 .vmux = 0,
2253 .gpio0 = 0x0403,
2254 .gpio1 = 0xF0D7,
2255 .gpio2 = 0x0101,
2256 .gpio3 = 0x0000,
2257 }, {
2258 .type = CX88_VMUX_CABLE,
2259 .vmux = 0,
2260 .gpio0 = 0x0403,
2261 .gpio1 = 0xF0D7,
2262 .gpio2 = 0x0100,
2263 .gpio3 = 0x0000,
2264 }, {
2265 .type = CX88_VMUX_COMPOSITE1,
2266 .vmux = 1,
2267 .gpio0 = 0x0403, /* was 0x0407 */
2268 .gpio1 = 0xF0F7,
2269 .gpio2 = 0x0101,
2270 .gpio3 = 0x0000,
2271 }, {
2272 .type = CX88_VMUX_SVIDEO,
2273 .vmux = 2,
2274 .gpio0 = 0x0403, /* was 0x0407 */
2275 .gpio1 = 0xF0F7,
2276 .gpio2 = 0x0101,
2277 .gpio3 = 0x0000,
2278 }},
2279 .radio = {
2280 .type = CX88_RADIO,
2281 .gpio0 = 0x0403,
2282 .gpio1 = 0xF097,
2283 .gpio2 = 0x0100,
2284 .gpio3 = 0x0000,
2285 },
2286 .mpeg = CX88_MPEG_DVB,
2287 },
2288 [CX88_BOARD_PROF_7301] = {
2289 .name = "Prof 7301 DVB-S/S2",
2290 .tuner_type = UNSET,
2291 .radio_type = UNSET,
2292 .tuner_addr = ADDR_UNSET,
2293 .radio_addr = ADDR_UNSET,
2294 .input = { {
2295 .type = CX88_VMUX_DVB,
2296 .vmux = 0,
2297 } },
2298 .mpeg = CX88_MPEG_DVB,
2299 },
2300 [CX88_BOARD_TWINHAN_VP1027_DVBS] = {
2301 .name = "Twinhan VP-1027 DVB-S",
2302 .tuner_type = TUNER_ABSENT,
2303 .radio_type = UNSET,
2304 .tuner_addr = ADDR_UNSET,
2305 .radio_addr = ADDR_UNSET,
2306 .input = {{
2307 .type = CX88_VMUX_DVB,
2308 .vmux = 0,
2309 } },
2310 .mpeg = CX88_MPEG_DVB,
2311 },
2312};
2313
2314/* ------------------------------------------------------------------ */
2315/* PCI subsystem IDs */
2316
2317static const struct cx88_subid cx88_subids[] = {
2318 {
2319 .subvendor = 0x0070,
2320 .subdevice = 0x3400,
2321 .card = CX88_BOARD_HAUPPAUGE,
2322 },{
2323 .subvendor = 0x0070,
2324 .subdevice = 0x3401,
2325 .card = CX88_BOARD_HAUPPAUGE,
2326 },{
2327 .subvendor = 0x14c7,
2328 .subdevice = 0x0106,
2329 .card = CX88_BOARD_GDI,
2330 },{
2331 .subvendor = 0x14c7,
2332 .subdevice = 0x0107, /* with mpeg encoder */
2333 .card = CX88_BOARD_GDI,
2334 },{
2335 .subvendor = PCI_VENDOR_ID_ATI,
2336 .subdevice = 0x00f8,
2337 .card = CX88_BOARD_ATI_WONDER_PRO,
2338 }, {
2339 .subvendor = PCI_VENDOR_ID_ATI,
2340 .subdevice = 0x00f9,
2341 .card = CX88_BOARD_ATI_WONDER_PRO,
2342 }, {
2343 .subvendor = 0x107d,
2344 .subdevice = 0x6611,
2345 .card = CX88_BOARD_WINFAST2000XP_EXPERT,
2346 },{
2347 .subvendor = 0x107d,
2348 .subdevice = 0x6613, /* NTSC */
2349 .card = CX88_BOARD_WINFAST2000XP_EXPERT,
2350 },{
2351 .subvendor = 0x107d,
2352 .subdevice = 0x6620,
2353 .card = CX88_BOARD_WINFAST_DV2000,
2354 },{
2355 .subvendor = 0x107d,
2356 .subdevice = 0x663b,
2357 .card = CX88_BOARD_LEADTEK_PVR2000,
2358 },{
2359 .subvendor = 0x107d,
2360 .subdevice = 0x663c,
2361 .card = CX88_BOARD_LEADTEK_PVR2000,
2362 },{
2363 .subvendor = 0x1461,
2364 .subdevice = 0x000b,
2365 .card = CX88_BOARD_AVERTV_STUDIO_303,
2366 },{
2367 .subvendor = 0x1462,
2368 .subdevice = 0x8606,
2369 .card = CX88_BOARD_MSI_TVANYWHERE_MASTER,
2370 },{
2371 .subvendor = 0x10fc,
2372 .subdevice = 0xd003,
2373 .card = CX88_BOARD_IODATA_GVVCP3PCI,
2374 },{
2375 .subvendor = 0x1043,
2376 .subdevice = 0x4823, /* with mpeg encoder */
2377 .card = CX88_BOARD_ASUS_PVR_416,
2378 },{
2379 .subvendor = 0x17de,
2380 .subdevice = 0x08a6,
2381 .card = CX88_BOARD_KWORLD_DVB_T,
2382 },{
2383 .subvendor = 0x18ac,
2384 .subdevice = 0xd810,
2385 .card = CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q,
2386 },{
2387 .subvendor = 0x18ac,
2388 .subdevice = 0xd820,
2389 .card = CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T,
2390 },{
2391 .subvendor = 0x18ac,
2392 .subdevice = 0xdb00,
2393 .card = CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1,
2394 },{
2395 .subvendor = 0x0070,
2396 .subdevice = 0x9002,
2397 .card = CX88_BOARD_HAUPPAUGE_DVB_T1,
2398 },{
2399 .subvendor = 0x14f1,
2400 .subdevice = 0x0187,
2401 .card = CX88_BOARD_CONEXANT_DVB_T1,
2402 },{
2403 .subvendor = 0x1540,
2404 .subdevice = 0x2580,
2405 .card = CX88_BOARD_PROVIDEO_PV259,
2406 },{
2407 .subvendor = 0x18ac,
2408 .subdevice = 0xdb10,
2409 .card = CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS,
2410 },{
2411 .subvendor = 0x1554,
2412 .subdevice = 0x4811,
2413 .card = CX88_BOARD_PIXELVIEW,
2414 },{
2415 .subvendor = 0x7063,
2416 .subdevice = 0x3000, /* HD-3000 card */
2417 .card = CX88_BOARD_PCHDTV_HD3000,
2418 },{
2419 .subvendor = 0x17de,
2420 .subdevice = 0xa8a6,
2421 .card = CX88_BOARD_DNTV_LIVE_DVB_T,
2422 },{
2423 .subvendor = 0x0070,
2424 .subdevice = 0x2801,
2425 .card = CX88_BOARD_HAUPPAUGE_ROSLYN,
2426 },{
2427 .subvendor = 0x14f1,
2428 .subdevice = 0x0342,
2429 .card = CX88_BOARD_DIGITALLOGIC_MEC,
2430 },{
2431 .subvendor = 0x10fc,
2432 .subdevice = 0xd035,
2433 .card = CX88_BOARD_IODATA_GVBCTV7E,
2434 },{
2435 .subvendor = 0x1421,
2436 .subdevice = 0x0334,
2437 .card = CX88_BOARD_ADSTECH_DVB_T_PCI,
2438 },{
2439 .subvendor = 0x153b,
2440 .subdevice = 0x1166,
2441 .card = CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1,
2442 },{
2443 .subvendor = 0x18ac,
2444 .subdevice = 0xd500,
2445 .card = CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD,
2446 },{
2447 .subvendor = 0x1461,
2448 .subdevice = 0x8011,
2449 .card = CX88_BOARD_AVERMEDIA_ULTRATV_MC_550,
2450 },{
2451 .subvendor = PCI_VENDOR_ID_ATI,
2452 .subdevice = 0xa101,
2453 .card = CX88_BOARD_ATI_HDTVWONDER,
2454 },{
2455 .subvendor = 0x107d,
2456 .subdevice = 0x665f,
2457 .card = CX88_BOARD_WINFAST_DTV1000,
2458 },{
2459 .subvendor = 0x1461,
2460 .subdevice = 0x000a,
2461 .card = CX88_BOARD_AVERTV_303,
2462 },{
2463 .subvendor = 0x0070,
2464 .subdevice = 0x9200,
2465 .card = CX88_BOARD_HAUPPAUGE_NOVASE2_S1,
2466 },{
2467 .subvendor = 0x0070,
2468 .subdevice = 0x9201,
2469 .card = CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1,
2470 },{
2471 .subvendor = 0x0070,
2472 .subdevice = 0x9202,
2473 .card = CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1,
2474 },{
2475 .subvendor = 0x17de,
2476 .subdevice = 0x08b2,
2477 .card = CX88_BOARD_KWORLD_DVBS_100,
2478 },{
2479 .subvendor = 0x0070,
2480 .subdevice = 0x9400,
2481 .card = CX88_BOARD_HAUPPAUGE_HVR1100,
2482 },{
2483 .subvendor = 0x0070,
2484 .subdevice = 0x9402,
2485 .card = CX88_BOARD_HAUPPAUGE_HVR1100,
2486 },{
2487 .subvendor = 0x0070,
2488 .subdevice = 0x9800,
2489 .card = CX88_BOARD_HAUPPAUGE_HVR1100LP,
2490 },{
2491 .subvendor = 0x0070,
2492 .subdevice = 0x9802,
2493 .card = CX88_BOARD_HAUPPAUGE_HVR1100LP,
2494 },{
2495 .subvendor = 0x0070,
2496 .subdevice = 0x9001,
2497 .card = CX88_BOARD_HAUPPAUGE_DVB_T1,
2498 },{
2499 .subvendor = 0x1822,
2500 .subdevice = 0x0025,
2501 .card = CX88_BOARD_DNTV_LIVE_DVB_T_PRO,
2502 },{
2503 .subvendor = 0x17de,
2504 .subdevice = 0x08a1,
2505 .card = CX88_BOARD_KWORLD_DVB_T_CX22702,
2506 },{
2507 .subvendor = 0x18ac,
2508 .subdevice = 0xdb50,
2509 .card = CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL,
2510 },{
2511 .subvendor = 0x18ac,
2512 .subdevice = 0xdb54,
2513 .card = CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL,
2514 /* Re-branded DViCO: DigitalNow DVB-T Dual */
2515 },{
2516 .subvendor = 0x18ac,
2517 .subdevice = 0xdb11,
2518 .card = CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS,
2519 /* Re-branded DViCO: UltraView DVB-T Plus */
2520 }, {
2521 .subvendor = 0x18ac,
2522 .subdevice = 0xdb30,
2523 .card = CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO,
2524 }, {
2525 .subvendor = 0x17de,
2526 .subdevice = 0x0840,
2527 .card = CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT,
2528 },{
2529 .subvendor = 0x1421,
2530 .subdevice = 0x0305,
2531 .card = CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT,
2532 },{
2533 .subvendor = 0x18ac,
2534 .subdevice = 0xdb40,
2535 .card = CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID,
2536 },{
2537 .subvendor = 0x18ac,
2538 .subdevice = 0xdb44,
2539 .card = CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID,
2540 },{
2541 .subvendor = 0x7063,
2542 .subdevice = 0x5500,
2543 .card = CX88_BOARD_PCHDTV_HD5500,
2544 },{
2545 .subvendor = 0x17de,
2546 .subdevice = 0x0841,
2547 .card = CX88_BOARD_KWORLD_MCE200_DELUXE,
2548 },{
2549 .subvendor = 0x1822,
2550 .subdevice = 0x0019,
2551 .card = CX88_BOARD_DNTV_LIVE_DVB_T_PRO,
2552 },{
2553 .subvendor = 0x1554,
2554 .subdevice = 0x4813,
2555 .card = CX88_BOARD_PIXELVIEW_PLAYTV_P7000,
2556 },{
2557 .subvendor = 0x14f1,
2558 .subdevice = 0x0842,
2559 .card = CX88_BOARD_NPGTECH_REALTV_TOP10FM,
2560 },{
2561 .subvendor = 0x107d,
2562 .subdevice = 0x665e,
2563 .card = CX88_BOARD_WINFAST_DTV2000H,
2564 },{
2565 .subvendor = 0x107d,
2566 .subdevice = 0x6f2b,
2567 .card = CX88_BOARD_WINFAST_DTV2000H_J,
2568 },{
2569 .subvendor = 0x18ac,
2570 .subdevice = 0xd800, /* FusionHDTV 3 Gold (original revision) */
2571 .card = CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q,
2572 },{
2573 .subvendor = 0x14f1,
2574 .subdevice = 0x0084,
2575 .card = CX88_BOARD_GENIATECH_DVBS,
2576 },{
2577 .subvendor = 0x0070,
2578 .subdevice = 0x1404,
2579 .card = CX88_BOARD_HAUPPAUGE_HVR3000,
2580 }, {
2581 .subvendor = 0x18ac,
2582 .subdevice = 0xdc00,
2583 .card = CX88_BOARD_SAMSUNG_SMT_7020,
2584 }, {
2585 .subvendor = 0x18ac,
2586 .subdevice = 0xdccd,
2587 .card = CX88_BOARD_SAMSUNG_SMT_7020,
2588 },{
2589 .subvendor = 0x1461,
2590 .subdevice = 0xc111, /* AverMedia M150-D */
2591 /* This board is known to work with the ASUS PVR416 config */
2592 .card = CX88_BOARD_ASUS_PVR_416,
2593 },{
2594 .subvendor = 0xc180,
2595 .subdevice = 0xc980,
2596 .card = CX88_BOARD_TE_DTV_250_OEM_SWANN,
2597 },{
2598 .subvendor = 0x0070,
2599 .subdevice = 0x9600,
2600 .card = CX88_BOARD_HAUPPAUGE_HVR1300,
2601 },{
2602 .subvendor = 0x0070,
2603 .subdevice = 0x9601,
2604 .card = CX88_BOARD_HAUPPAUGE_HVR1300,
2605 },{
2606 .subvendor = 0x0070,
2607 .subdevice = 0x9602,
2608 .card = CX88_BOARD_HAUPPAUGE_HVR1300,
2609 },{
2610 .subvendor = 0x107d,
2611 .subdevice = 0x6632,
2612 .card = CX88_BOARD_LEADTEK_PVR2000,
2613 },{
2614 .subvendor = 0x12ab,
2615 .subdevice = 0x2300, /* Club3D Zap TV2100 */
2616 .card = CX88_BOARD_KWORLD_DVB_T_CX22702,
2617 },{
2618 .subvendor = 0x0070,
2619 .subdevice = 0x9000,
2620 .card = CX88_BOARD_HAUPPAUGE_DVB_T1,
2621 },{
2622 .subvendor = 0x0070,
2623 .subdevice = 0x1400,
2624 .card = CX88_BOARD_HAUPPAUGE_HVR3000,
2625 },{
2626 .subvendor = 0x0070,
2627 .subdevice = 0x1401,
2628 .card = CX88_BOARD_HAUPPAUGE_HVR3000,
2629 },{
2630 .subvendor = 0x0070,
2631 .subdevice = 0x1402,
2632 .card = CX88_BOARD_HAUPPAUGE_HVR3000,
2633 },{
2634 .subvendor = 0x1421,
2635 .subdevice = 0x0341, /* ADS Tech InstantTV DVB-S */
2636 .card = CX88_BOARD_KWORLD_DVBS_100,
2637 },{
2638 .subvendor = 0x1421,
2639 .subdevice = 0x0390,
2640 .card = CX88_BOARD_ADSTECH_PTV_390,
2641 },{
2642 .subvendor = 0x11bd,
2643 .subdevice = 0x0051,
2644 .card = CX88_BOARD_PINNACLE_PCTV_HD_800i,
2645 }, {
2646 .subvendor = 0x18ac,
2647 .subdevice = 0xd530,
2648 .card = CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO,
2649 }, {
2650 .subvendor = 0x12ab,
2651 .subdevice = 0x1788,
2652 .card = CX88_BOARD_PINNACLE_HYBRID_PCTV,
2653 }, {
2654 .subvendor = 0x14f1,
2655 .subdevice = 0xea3d,
2656 .card = CX88_BOARD_POWERCOLOR_REAL_ANGEL,
2657 }, {
2658 .subvendor = 0x107d,
2659 .subdevice = 0x6f18,
2660 .card = CX88_BOARD_WINFAST_TV2000_XP_GLOBAL,
2661 }, {
2662 .subvendor = 0x14f1,
2663 .subdevice = 0x8852,
2664 .card = CX88_BOARD_GENIATECH_X8000_MT,
2665 }, {
2666 .subvendor = 0x18ac,
2667 .subdevice = 0xd610,
2668 .card = CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD,
2669 }, {
2670 .subvendor = 0x1554,
2671 .subdevice = 0x4935,
2672 .card = CX88_BOARD_PROLINK_PV_8000GT,
2673 }, {
2674 .subvendor = 0x1554,
2675 .subdevice = 0x4976,
2676 .card = CX88_BOARD_PROLINK_PV_GLOBAL_XTREME,
2677 }, {
2678 .subvendor = 0x17de,
2679 .subdevice = 0x08c1,
2680 .card = CX88_BOARD_KWORLD_ATSC_120,
2681 }, {
2682 .subvendor = 0x0070,
2683 .subdevice = 0x6900,
2684 .card = CX88_BOARD_HAUPPAUGE_HVR4000,
2685 }, {
2686 .subvendor = 0x0070,
2687 .subdevice = 0x6904,
2688 .card = CX88_BOARD_HAUPPAUGE_HVR4000,
2689 }, {
2690 .subvendor = 0x0070,
2691 .subdevice = 0x6902,
2692 .card = CX88_BOARD_HAUPPAUGE_HVR4000,
2693 }, {
2694 .subvendor = 0x0070,
2695 .subdevice = 0x6905,
2696 .card = CX88_BOARD_HAUPPAUGE_HVR4000LITE,
2697 }, {
2698 .subvendor = 0x0070,
2699 .subdevice = 0x6906,
2700 .card = CX88_BOARD_HAUPPAUGE_HVR4000LITE,
2701 }, {
2702 .subvendor = 0xd420,
2703 .subdevice = 0x9022,
2704 .card = CX88_BOARD_TEVII_S420,
2705 }, {
2706 .subvendor = 0xd460,
2707 .subdevice = 0x9022,
2708 .card = CX88_BOARD_TEVII_S460,
2709 }, {
2710 .subvendor = 0xd464,
2711 .subdevice = 0x9022,
2712 .card = CX88_BOARD_TEVII_S464,
2713 }, {
2714 .subvendor = 0xA044,
2715 .subdevice = 0x2011,
2716 .card = CX88_BOARD_OMICOM_SS4_PCI,
2717 }, {
2718 .subvendor = 0x8910,
2719 .subdevice = 0x8888,
2720 .card = CX88_BOARD_TBS_8910,
2721 }, {
2722 .subvendor = 0x8920,
2723 .subdevice = 0x8888,
2724 .card = CX88_BOARD_TBS_8920,
2725 }, {
2726 .subvendor = 0xb022,
2727 .subdevice = 0x3022,
2728 .card = CX88_BOARD_PROF_6200,
2729 }, {
2730 .subvendor = 0xB033,
2731 .subdevice = 0x3033,
2732 .card = CX88_BOARD_PROF_7300,
2733 }, {
2734 .subvendor = 0xb200,
2735 .subdevice = 0x4200,
2736 .card = CX88_BOARD_SATTRADE_ST4200,
2737 }, {
2738 .subvendor = 0x153b,
2739 .subdevice = 0x1177,
2740 .card = CX88_BOARD_TERRATEC_CINERGY_HT_PCI_MKII,
2741 }, {
2742 .subvendor = 0x0070,
2743 .subdevice = 0x9290,
2744 .card = CX88_BOARD_HAUPPAUGE_IRONLY,
2745 }, {
2746 .subvendor = 0x107d,
2747 .subdevice = 0x6654,
2748 .card = CX88_BOARD_WINFAST_DTV1800H,
2749 }, {
2750 /* WinFast DTV1800 H with XC4000 tuner */
2751 .subvendor = 0x107d,
2752 .subdevice = 0x6f38,
2753 .card = CX88_BOARD_WINFAST_DTV1800H_XC4000,
2754 }, {
2755 .subvendor = 0x107d,
2756 .subdevice = 0x6f42,
2757 .card = CX88_BOARD_WINFAST_DTV2000H_PLUS,
2758 }, {
2759 /* PVR2000 PAL Model [107d:6630] */
2760 .subvendor = 0x107d,
2761 .subdevice = 0x6630,
2762 .card = CX88_BOARD_LEADTEK_PVR2000,
2763 }, {
2764 /* PVR2000 PAL Model [107d:6638] */
2765 .subvendor = 0x107d,
2766 .subdevice = 0x6638,
2767 .card = CX88_BOARD_LEADTEK_PVR2000,
2768 }, {
2769 /* PVR2000 NTSC Model [107d:6631] */
2770 .subvendor = 0x107d,
2771 .subdevice = 0x6631,
2772 .card = CX88_BOARD_LEADTEK_PVR2000,
2773 }, {
2774 /* PVR2000 NTSC Model [107d:6637] */
2775 .subvendor = 0x107d,
2776 .subdevice = 0x6637,
2777 .card = CX88_BOARD_LEADTEK_PVR2000,
2778 }, {
2779 /* PVR2000 NTSC Model [107d:663d] */
2780 .subvendor = 0x107d,
2781 .subdevice = 0x663d,
2782 .card = CX88_BOARD_LEADTEK_PVR2000,
2783 }, {
2784 /* DV2000 NTSC Model [107d:6621] */
2785 .subvendor = 0x107d,
2786 .subdevice = 0x6621,
2787 .card = CX88_BOARD_WINFAST_DV2000,
2788 }, {
2789 /* TV2000 XP Global [107d:6618] */
2790 .subvendor = 0x107d,
2791 .subdevice = 0x6618,
2792 .card = CX88_BOARD_WINFAST_TV2000_XP_GLOBAL,
2793 }, {
2794 /* TV2000 XP Global [107d:6618] */
2795 .subvendor = 0x107d,
2796 .subdevice = 0x6619,
2797 .card = CX88_BOARD_WINFAST_TV2000_XP_GLOBAL,
2798 }, {
2799 /* WinFast TV2000 XP Global with XC4000 tuner */
2800 .subvendor = 0x107d,
2801 .subdevice = 0x6f36,
2802 .card = CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36,
2803 }, {
2804 /* WinFast TV2000 XP Global with XC4000 tuner and different GPIOs */
2805 .subvendor = 0x107d,
2806 .subdevice = 0x6f43,
2807 .card = CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43,
2808 }, {
2809 .subvendor = 0xb034,
2810 .subdevice = 0x3034,
2811 .card = CX88_BOARD_PROF_7301,
2812 }, {
2813 .subvendor = 0x1822,
2814 .subdevice = 0x0023,
2815 .card = CX88_BOARD_TWINHAN_VP1027_DVBS,
2816 },
2817};
2818
2819/* ----------------------------------------------------------------------- */
2820/* some leadtek specific stuff */
2821
2822static void leadtek_eeprom(struct cx88_core *core, u8 *eeprom_data)
2823{
2824 if (eeprom_data[4] != 0x7d ||
2825 eeprom_data[5] != 0x10 ||
2826 eeprom_data[7] != 0x66) {
2827 warn_printk(core, "Leadtek eeprom invalid.\n");
2828 return;
2829 }
2830
2831 /* Terry Wu <terrywu2009@gmail.com> */
2832 switch (eeprom_data[6]) {
2833 case 0x13: /* SSID 6613 for TV2000 XP Expert NTSC Model */
2834 case 0x21: /* SSID 6621 for DV2000 NTSC Model */
2835 case 0x31: /* SSID 6631 for PVR2000 NTSC Model */
2836 case 0x37: /* SSID 6637 for PVR2000 NTSC Model */
2837 case 0x3d: /* SSID 6637 for PVR2000 NTSC Model */
2838 core->board.tuner_type = TUNER_PHILIPS_FM1236_MK3;
2839 break;
2840 default:
2841 core->board.tuner_type = TUNER_PHILIPS_FM1216ME_MK3;
2842 break;
2843 }
2844
2845 info_printk(core, "Leadtek Winfast 2000XP Expert config: "
2846 "tuner=%d, eeprom[0]=0x%02x\n",
2847 core->board.tuner_type, eeprom_data[0]);
2848}
2849
2850static void hauppauge_eeprom(struct cx88_core *core, u8 *eeprom_data)
2851{
2852 struct tveeprom tv;
2853
2854 tveeprom_hauppauge_analog(&core->i2c_client, &tv, eeprom_data);
2855 core->board.tuner_type = tv.tuner_type;
2856 core->tuner_formats = tv.tuner_formats;
2857 core->board.radio.type = tv.has_radio ? CX88_RADIO : 0;
2858
2859 /* Make sure we support the board model */
2860 switch (tv.model)
2861 {
2862 case 14009: /* WinTV-HVR3000 (Retail, IR, b/panel video, 3.5mm audio in) */
2863 case 14019: /* WinTV-HVR3000 (Retail, IR Blaster, b/panel video, 3.5mm audio in) */
2864 case 14029: /* WinTV-HVR3000 (Retail, IR, b/panel video, 3.5mm audio in - 880 bridge) */
2865 case 14109: /* WinTV-HVR3000 (Retail, IR, b/panel video, 3.5mm audio in - low profile) */
2866 case 14129: /* WinTV-HVR3000 (Retail, IR, b/panel video, 3.5mm audio in - 880 bridge - LP) */
2867 case 14559: /* WinTV-HVR3000 (OEM, no IR, b/panel video, 3.5mm audio in) */
2868 case 14569: /* WinTV-HVR3000 (OEM, no IR, no back panel video) */
2869 case 14659: /* WinTV-HVR3000 (OEM, no IR, b/panel video, RCA audio in - Low profile) */
2870 case 14669: /* WinTV-HVR3000 (OEM, no IR, no b/panel video - Low profile) */
2871 case 28552: /* WinTV-PVR 'Roslyn' (No IR) */
2872 case 34519: /* WinTV-PCI-FM */
2873 case 69009:
2874 /* WinTV-HVR4000 (DVBS/S2/T, Video and IR, back panel inputs) */
2875 case 69100: /* WinTV-HVR4000LITE (DVBS/S2, IR) */
2876 case 69500: /* WinTV-HVR4000LITE (DVBS/S2, No IR) */
2877 case 69559:
2878 /* WinTV-HVR4000 (DVBS/S2/T, Video no IR, back panel inputs) */
2879 case 69569: /* WinTV-HVR4000 (DVBS/S2/T, Video no IR) */
2880 case 90002: /* Nova-T-PCI (9002) */
2881 case 92001: /* Nova-S-Plus (Video and IR) */
2882 case 92002: /* Nova-S-Plus (Video and IR) */
2883 case 90003: /* Nova-T-PCI (9002 No RF out) */
2884 case 90500: /* Nova-T-PCI (oem) */
2885 case 90501: /* Nova-T-PCI (oem/IR) */
2886 case 92000: /* Nova-SE2 (OEM, No Video or IR) */
2887 case 92900: /* WinTV-IROnly (No analog or digital Video inputs) */
2888 case 94009: /* WinTV-HVR1100 (Video and IR Retail) */
2889 case 94501: /* WinTV-HVR1100 (Video and IR OEM) */
2890 case 96009: /* WinTV-HVR1300 (PAL Video, MPEG Video and IR RX) */
2891 case 96019: /* WinTV-HVR1300 (PAL Video, MPEG Video and IR RX/TX) */
2892 case 96559: /* WinTV-HVR1300 (PAL Video, MPEG Video no IR) */
2893 case 96569: /* WinTV-HVR1300 () */
2894 case 96659: /* WinTV-HVR1300 () */
2895 case 98559: /* WinTV-HVR1100LP (Video no IR, Retail - Low Profile) */
2896 /* known */
2897 break;
2898 case CX88_BOARD_SAMSUNG_SMT_7020:
2899 cx_set(MO_GP0_IO, 0x008989FF);
2900 break;
2901 default:
2902 warn_printk(core, "warning: unknown hauppauge model #%d\n",
2903 tv.model);
2904 break;
2905 }
2906
2907 info_printk(core, "hauppauge eeprom: model=%d\n", tv.model);
2908}
2909
2910/* ----------------------------------------------------------------------- */
2911/* some GDI (was: Modular Technology) specific stuff */
2912
2913static const struct {
2914 int id;
2915 int fm;
2916 const char *name;
2917} gdi_tuner[] = {
2918 [ 0x01 ] = { .id = TUNER_ABSENT,
2919 .name = "NTSC_M" },
2920 [ 0x02 ] = { .id = TUNER_ABSENT,
2921 .name = "PAL_B" },
2922 [ 0x03 ] = { .id = TUNER_ABSENT,
2923 .name = "PAL_I" },
2924 [ 0x04 ] = { .id = TUNER_ABSENT,
2925 .name = "PAL_D" },
2926 [ 0x05 ] = { .id = TUNER_ABSENT,
2927 .name = "SECAM" },
2928
2929 [ 0x10 ] = { .id = TUNER_ABSENT,
2930 .fm = 1,
2931 .name = "TEMIC_4049" },
2932 [ 0x11 ] = { .id = TUNER_TEMIC_4136FY5,
2933 .name = "TEMIC_4136" },
2934 [ 0x12 ] = { .id = TUNER_ABSENT,
2935 .name = "TEMIC_4146" },
2936
2937 [ 0x20 ] = { .id = TUNER_PHILIPS_FQ1216ME,
2938 .fm = 1,
2939 .name = "PHILIPS_FQ1216_MK3" },
2940 [ 0x21 ] = { .id = TUNER_ABSENT, .fm = 1,
2941 .name = "PHILIPS_FQ1236_MK3" },
2942 [ 0x22 ] = { .id = TUNER_ABSENT,
2943 .name = "PHILIPS_FI1236_MK3" },
2944 [ 0x23 ] = { .id = TUNER_ABSENT,
2945 .name = "PHILIPS_FI1216_MK3" },
2946};
2947
2948static void gdi_eeprom(struct cx88_core *core, u8 *eeprom_data)
2949{
2950 const char *name = (eeprom_data[0x0d] < ARRAY_SIZE(gdi_tuner))
2951 ? gdi_tuner[eeprom_data[0x0d]].name : NULL;
2952
2953 info_printk(core, "GDI: tuner=%s\n", name ? name : "unknown");
2954 if (NULL == name)
2955 return;
2956 core->board.tuner_type = gdi_tuner[eeprom_data[0x0d]].id;
2957 core->board.radio.type = gdi_tuner[eeprom_data[0x0d]].fm ?
2958 CX88_RADIO : 0;
2959}
2960
2961/* ------------------------------------------------------------------- */
2962/* some Divco specific stuff */
2963static int cx88_dvico_xc2028_callback(struct cx88_core *core,
2964 int command, int arg)
2965{
2966 switch (command) {
2967 case XC2028_TUNER_RESET:
2968 switch (core->boardnr) {
2969 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
2970 /* GPIO-4 xc3028 tuner */
2971
2972 cx_set(MO_GP0_IO, 0x00001000);
2973 cx_clear(MO_GP0_IO, 0x00000010);
2974 msleep(100);
2975 cx_set(MO_GP0_IO, 0x00000010);
2976 msleep(100);
2977 break;
2978 default:
2979 cx_write(MO_GP0_IO, 0x101000);
2980 mdelay(5);
2981 cx_set(MO_GP0_IO, 0x101010);
2982 }
2983 break;
2984 default:
2985 return -EINVAL;
2986 }
2987
2988 return 0;
2989}
2990
2991
2992/* ----------------------------------------------------------------------- */
2993/* some Geniatech specific stuff */
2994
2995static int cx88_xc3028_geniatech_tuner_callback(struct cx88_core *core,
2996 int command, int mode)
2997{
2998 switch (command) {
2999 case XC2028_TUNER_RESET:
3000 switch (INPUT(core->input).type) {
3001 case CX88_RADIO:
3002 break;
3003 case CX88_VMUX_DVB:
3004 cx_write(MO_GP1_IO, 0x030302);
3005 mdelay(50);
3006 break;
3007 default:
3008 cx_write(MO_GP1_IO, 0x030301);
3009 mdelay(50);
3010 }
3011 cx_write(MO_GP1_IO, 0x101010);
3012 mdelay(50);
3013 cx_write(MO_GP1_IO, 0x101000);
3014 mdelay(50);
3015 cx_write(MO_GP1_IO, 0x101010);
3016 mdelay(50);
3017 return 0;
3018 }
3019 return -EINVAL;
3020}
3021
3022static int cx88_xc3028_winfast1800h_callback(struct cx88_core *core,
3023 int command, int arg)
3024{
3025 switch (command) {
3026 case XC2028_TUNER_RESET:
3027 /* GPIO 12 (xc3028 tuner reset) */
3028 cx_set(MO_GP1_IO, 0x1010);
3029 mdelay(50);
3030 cx_clear(MO_GP1_IO, 0x10);
3031 mdelay(50);
3032 cx_set(MO_GP1_IO, 0x10);
3033 mdelay(50);
3034 return 0;
3035 }
3036 return -EINVAL;
3037}
3038
3039static int cx88_xc4000_winfast2000h_plus_callback(struct cx88_core *core,
3040 int command, int arg)
3041{
3042 switch (command) {
3043 case XC4000_TUNER_RESET:
3044 /* GPIO 12 (xc4000 tuner reset) */
3045 cx_set(MO_GP1_IO, 0x1010);
3046 mdelay(50);
3047 cx_clear(MO_GP1_IO, 0x10);
3048 mdelay(75);
3049 cx_set(MO_GP1_IO, 0x10);
3050 mdelay(75);
3051 return 0;
3052 }
3053 return -EINVAL;
3054}
3055
3056/* ------------------------------------------------------------------- */
3057/* some Divco specific stuff */
3058static int cx88_pv_8000gt_callback(struct cx88_core *core,
3059 int command, int arg)
3060{
3061 switch (command) {
3062 case XC2028_TUNER_RESET:
3063 cx_write(MO_GP2_IO, 0xcf7);
3064 mdelay(50);
3065 cx_write(MO_GP2_IO, 0xef5);
3066 mdelay(50);
3067 cx_write(MO_GP2_IO, 0xcf7);
3068 break;
3069 default:
3070 return -EINVAL;
3071 }
3072
3073 return 0;
3074}
3075
3076/* ----------------------------------------------------------------------- */
3077/* some DViCO specific stuff */
3078
3079static void dvico_fusionhdtv_hybrid_init(struct cx88_core *core)
3080{
3081 struct i2c_msg msg = { .addr = 0x45, .flags = 0 };
3082 int i, err;
3083 static u8 init_bufs[13][5] = {
3084 { 0x10, 0x00, 0x20, 0x01, 0x03 },
3085 { 0x10, 0x10, 0x01, 0x00, 0x21 },
3086 { 0x10, 0x10, 0x10, 0x00, 0xCA },
3087 { 0x10, 0x10, 0x12, 0x00, 0x08 },
3088 { 0x10, 0x10, 0x13, 0x00, 0x0A },
3089 { 0x10, 0x10, 0x16, 0x01, 0xC0 },
3090 { 0x10, 0x10, 0x22, 0x01, 0x3D },
3091 { 0x10, 0x10, 0x73, 0x01, 0x2E },
3092 { 0x10, 0x10, 0x72, 0x00, 0xC5 },
3093 { 0x10, 0x10, 0x71, 0x01, 0x97 },
3094 { 0x10, 0x10, 0x70, 0x00, 0x0F },
3095 { 0x10, 0x10, 0xB0, 0x00, 0x01 },
3096 { 0x03, 0x0C },
3097 };
3098
3099 for (i = 0; i < ARRAY_SIZE(init_bufs); i++) {
3100 msg.buf = init_bufs[i];
3101 msg.len = (i != 12 ? 5 : 2);
3102 err = i2c_transfer(&core->i2c_adap, &msg, 1);
3103 if (err != 1) {
3104 warn_printk(core, "dvico_fusionhdtv_hybrid_init buf %d "
3105 "failed (err = %d)!\n", i, err);
3106 return;
3107 }
3108 }
3109}
3110
3111static int cx88_xc2028_tuner_callback(struct cx88_core *core,
3112 int command, int arg)
3113{
3114 /* Board-specific callbacks */
3115 switch (core->boardnr) {
3116 case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
3117 case CX88_BOARD_GENIATECH_X8000_MT:
3118 case CX88_BOARD_KWORLD_ATSC_120:
3119 return cx88_xc3028_geniatech_tuner_callback(core,
3120 command, arg);
3121 case CX88_BOARD_PROLINK_PV_8000GT:
3122 case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
3123 return cx88_pv_8000gt_callback(core, command, arg);
3124 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO:
3125 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
3126 return cx88_dvico_xc2028_callback(core, command, arg);
3127 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
3128 case CX88_BOARD_WINFAST_DTV1800H:
3129 return cx88_xc3028_winfast1800h_callback(core, command, arg);
3130 }
3131
3132 switch (command) {
3133 case XC2028_TUNER_RESET:
3134 switch (INPUT(core->input).type) {
3135 case CX88_RADIO:
3136 info_printk(core, "setting GPIO to radio!\n");
3137 cx_write(MO_GP0_IO, 0x4ff);
3138 mdelay(250);
3139 cx_write(MO_GP2_IO, 0xff);
3140 mdelay(250);
3141 break;
3142 case CX88_VMUX_DVB: /* Digital TV*/
3143 default: /* Analog TV */
3144 info_printk(core, "setting GPIO to TV!\n");
3145 break;
3146 }
3147 cx_write(MO_GP1_IO, 0x101010);
3148 mdelay(250);
3149 cx_write(MO_GP1_IO, 0x101000);
3150 mdelay(250);
3151 cx_write(MO_GP1_IO, 0x101010);
3152 mdelay(250);
3153 return 0;
3154 }
3155 return -EINVAL;
3156}
3157
3158static int cx88_xc4000_tuner_callback(struct cx88_core *core,
3159 int command, int arg)
3160{
3161 /* Board-specific callbacks */
3162 switch (core->boardnr) {
3163 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
3164 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
3165 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36:
3166 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43:
3167 return cx88_xc4000_winfast2000h_plus_callback(core,
3168 command, arg);
3169 }
3170 return -EINVAL;
3171}
3172
3173/* ----------------------------------------------------------------------- */
3174/* Tuner callback function. Currently only needed for the Pinnacle *
3175 * PCTV HD 800i with an xc5000 sillicon tuner. This is used for both *
3176 * analog tuner attach (tuner-core.c) and dvb tuner attach (cx88-dvb.c) */
3177
3178static int cx88_xc5000_tuner_callback(struct cx88_core *core,
3179 int command, int arg)
3180{
3181 switch (core->boardnr) {
3182 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
3183 if (command == 0) { /* This is the reset command from xc5000 */
3184
3185 /* djh - According to the engineer at PCTV Systems,
3186 the xc5000 reset pin is supposed to be on GPIO12.
3187 However, despite three nights of effort, pulling
3188 that GPIO low didn't reset the xc5000. While
3189 pulling MO_SRST_IO low does reset the xc5000, this
3190 also resets in the s5h1409 being reset as well.
3191 This causes tuning to always fail since the internal
3192 state of the s5h1409 does not match the driver's
3193 state. Given that the only two conditions in which
3194 the driver performs a reset is during firmware load
3195 and powering down the chip, I am taking out the
3196 reset. We know that the chip is being reset
3197 when the cx88 comes online, and not being able to
3198 do power management for this board is worse than
3199 not having any tuning at all. */
3200 return 0;
3201 } else {
3202 err_printk(core, "xc5000: unknown tuner "
3203 "callback command.\n");
3204 return -EINVAL;
3205 }
3206 break;
3207 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
3208 if (command == 0) { /* This is the reset command from xc5000 */
3209 cx_clear(MO_GP0_IO, 0x00000010);
3210 msleep(10);
3211 cx_set(MO_GP0_IO, 0x00000010);
3212 return 0;
3213 } else {
3214 printk(KERN_ERR
3215 "xc5000: unknown tuner callback command.\n");
3216 return -EINVAL;
3217 }
3218 break;
3219 }
3220 return 0; /* Should never be here */
3221}
3222
3223int cx88_tuner_callback(void *priv, int component, int command, int arg)
3224{
3225 struct i2c_algo_bit_data *i2c_algo = priv;
3226 struct cx88_core *core;
3227
3228 if (!i2c_algo) {
3229 printk(KERN_ERR "cx88: Error - i2c private data undefined.\n");
3230 return -EINVAL;
3231 }
3232
3233 core = i2c_algo->data;
3234
3235 if (!core) {
3236 printk(KERN_ERR "cx88: Error - device struct undefined.\n");
3237 return -EINVAL;
3238 }
3239
3240 if (component != DVB_FRONTEND_COMPONENT_TUNER)
3241 return -EINVAL;
3242
3243 switch (core->board.tuner_type) {
3244 case TUNER_XC2028:
3245 info_printk(core, "Calling XC2028/3028 callback\n");
3246 return cx88_xc2028_tuner_callback(core, command, arg);
3247 case TUNER_XC4000:
3248 info_printk(core, "Calling XC4000 callback\n");
3249 return cx88_xc4000_tuner_callback(core, command, arg);
3250 case TUNER_XC5000:
3251 info_printk(core, "Calling XC5000 callback\n");
3252 return cx88_xc5000_tuner_callback(core, command, arg);
3253 }
3254 err_printk(core, "Error: Calling callback for tuner %d\n",
3255 core->board.tuner_type);
3256 return -EINVAL;
3257}
3258EXPORT_SYMBOL(cx88_tuner_callback);
3259
3260/* ----------------------------------------------------------------------- */
3261
3262static void cx88_card_list(struct cx88_core *core, struct pci_dev *pci)
3263{
3264 int i;
3265
3266 if (0 == pci->subsystem_vendor &&
3267 0 == pci->subsystem_device) {
3268 printk(KERN_ERR
3269 "%s: Your board has no valid PCI Subsystem ID and thus can't\n"
3270 "%s: be autodetected. Please pass card=<n> insmod option to\n"
3271 "%s: workaround that. Redirect complaints to the vendor of\n"
3272 "%s: the TV card. Best regards,\n"
3273 "%s: -- tux\n",
3274 core->name,core->name,core->name,core->name,core->name);
3275 } else {
3276 printk(KERN_ERR
3277 "%s: Your board isn't known (yet) to the driver. You can\n"
3278 "%s: try to pick one of the existing card configs via\n"
3279 "%s: card=<n> insmod option. Updating to the latest\n"
3280 "%s: version might help as well.\n",
3281 core->name,core->name,core->name,core->name);
3282 }
3283 err_printk(core, "Here is a list of valid choices for the card=<n> "
3284 "insmod option:\n");
3285 for (i = 0; i < ARRAY_SIZE(cx88_boards); i++)
3286 printk(KERN_ERR "%s: card=%d -> %s\n",
3287 core->name, i, cx88_boards[i].name);
3288}
3289
3290static void cx88_card_setup_pre_i2c(struct cx88_core *core)
3291{
3292 switch (core->boardnr) {
3293 case CX88_BOARD_HAUPPAUGE_HVR1300:
3294 /*
3295 * Bring the 702 demod up before i2c scanning/attach or devices are hidden
3296 * We leave here with the 702 on the bus
3297 *
3298 * "reset the IR receiver on GPIO[3]"
3299 * Reported by Mike Crash <mike AT mikecrash.com>
3300 */
3301 cx_write(MO_GP0_IO, 0x0000ef88);
3302 udelay(1000);
3303 cx_clear(MO_GP0_IO, 0x00000088);
3304 udelay(50);
3305 cx_set(MO_GP0_IO, 0x00000088); /* 702 out of reset */
3306 udelay(1000);
3307 break;
3308
3309 case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
3310 case CX88_BOARD_PROLINK_PV_8000GT:
3311 cx_write(MO_GP2_IO, 0xcf7);
3312 mdelay(50);
3313 cx_write(MO_GP2_IO, 0xef5);
3314 mdelay(50);
3315 cx_write(MO_GP2_IO, 0xcf7);
3316 msleep(10);
3317 break;
3318
3319 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
3320 /* Enable the xc5000 tuner */
3321 cx_set(MO_GP0_IO, 0x00001010);
3322 break;
3323
3324 case CX88_BOARD_WINFAST_DTV2000H_J:
3325 case CX88_BOARD_HAUPPAUGE_HVR3000:
3326 case CX88_BOARD_HAUPPAUGE_HVR4000:
3327 /* Init GPIO */
3328 cx_write(MO_GP0_IO, core->board.input[0].gpio0);
3329 udelay(1000);
3330 cx_clear(MO_GP0_IO, 0x00000080);
3331 udelay(50);
3332 cx_set(MO_GP0_IO, 0x00000080); /* 702 out of reset */
3333 udelay(1000);
3334 break;
3335
3336 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
3337 case CX88_BOARD_WINFAST_DTV1800H:
3338 cx88_xc3028_winfast1800h_callback(core, XC2028_TUNER_RESET, 0);
3339 break;
3340
3341 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
3342 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
3343 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36:
3344 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43:
3345 cx88_xc4000_winfast2000h_plus_callback(core,
3346 XC4000_TUNER_RESET, 0);
3347 break;
3348
3349 case CX88_BOARD_TWINHAN_VP1027_DVBS:
3350 cx_write(MO_GP0_IO, 0x00003230);
3351 cx_write(MO_GP0_IO, 0x00003210);
3352 msleep(1);
3353 cx_write(MO_GP0_IO, 0x00001230);
3354 break;
3355 }
3356}
3357
3358/*
3359 * Sets board-dependent xc3028 configuration
3360 */
3361void cx88_setup_xc3028(struct cx88_core *core, struct xc2028_ctrl *ctl)
3362{
3363 memset(ctl, 0, sizeof(*ctl));
3364
3365 ctl->fname = XC2028_DEFAULT_FIRMWARE;
3366 ctl->max_len = 64;
3367
3368 switch (core->boardnr) {
3369 case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
3370 /* Now works with firmware version 2.7 */
3371 if (core->i2c_algo.udelay < 16)
3372 core->i2c_algo.udelay = 16;
3373 break;
3374 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO:
3375 case CX88_BOARD_WINFAST_DTV1800H:
3376 ctl->demod = XC3028_FE_ZARLINK456;
3377 break;
3378 case CX88_BOARD_KWORLD_ATSC_120:
3379 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
3380 ctl->demod = XC3028_FE_OREN538;
3381 break;
3382 case CX88_BOARD_GENIATECH_X8000_MT:
3383 /* FIXME: For this board, the xc3028 never recovers after being
3384 powered down (the reset GPIO probably is not set properly).
3385 We don't have access to the hardware so we cannot determine
3386 which GPIO is used for xc3028, so just disable power xc3028
3387 power management for now */
3388 ctl->disable_power_mgmt = 1;
3389 break;
3390 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
3391 case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
3392 case CX88_BOARD_PROLINK_PV_8000GT:
3393 /*
3394 * Those boards uses non-MTS firmware
3395 */
3396 break;
3397 case CX88_BOARD_PINNACLE_HYBRID_PCTV:
3398 case CX88_BOARD_TERRATEC_CINERGY_HT_PCI_MKII:
3399 ctl->demod = XC3028_FE_ZARLINK456;
3400 ctl->mts = 1;
3401 break;
3402 default:
3403 ctl->demod = XC3028_FE_OREN538;
3404 ctl->mts = 1;
3405 }
3406}
3407EXPORT_SYMBOL_GPL(cx88_setup_xc3028);
3408
3409static void cx88_card_setup(struct cx88_core *core)
3410{
3411 static u8 eeprom[256];
3412 struct tuner_setup tun_setup;
3413 unsigned int mode_mask = T_RADIO | T_ANALOG_TV;
3414
3415 memset(&tun_setup, 0, sizeof(tun_setup));
3416
3417 if (0 == core->i2c_rc) {
3418 core->i2c_client.addr = 0xa0 >> 1;
3419 tveeprom_read(&core->i2c_client, eeprom, sizeof(eeprom));
3420 }
3421
3422 switch (core->boardnr) {
3423 case CX88_BOARD_HAUPPAUGE:
3424 case CX88_BOARD_HAUPPAUGE_ROSLYN:
3425 if (0 == core->i2c_rc)
3426 hauppauge_eeprom(core, eeprom+8);
3427 break;
3428 case CX88_BOARD_GDI:
3429 if (0 == core->i2c_rc)
3430 gdi_eeprom(core, eeprom);
3431 break;
3432 case CX88_BOARD_LEADTEK_PVR2000:
3433 case CX88_BOARD_WINFAST_DV2000:
3434 case CX88_BOARD_WINFAST2000XP_EXPERT:
3435 if (0 == core->i2c_rc)
3436 leadtek_eeprom(core, eeprom);
3437 break;
3438 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
3439 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
3440 case CX88_BOARD_HAUPPAUGE_DVB_T1:
3441 case CX88_BOARD_HAUPPAUGE_HVR1100:
3442 case CX88_BOARD_HAUPPAUGE_HVR1100LP:
3443 case CX88_BOARD_HAUPPAUGE_HVR3000:
3444 case CX88_BOARD_HAUPPAUGE_HVR1300:
3445 case CX88_BOARD_HAUPPAUGE_HVR4000:
3446 case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
3447 case CX88_BOARD_HAUPPAUGE_IRONLY:
3448 if (0 == core->i2c_rc)
3449 hauppauge_eeprom(core, eeprom);
3450 break;
3451 case CX88_BOARD_KWORLD_DVBS_100:
3452 cx_write(MO_GP0_IO, 0x000007f8);
3453 cx_write(MO_GP1_IO, 0x00000001);
3454 break;
3455 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO:
3456 /* GPIO0:0 is hooked to demod reset */
3457 /* GPIO0:4 is hooked to xc3028 reset */
3458 cx_write(MO_GP0_IO, 0x00111100);
3459 msleep(1);
3460 cx_write(MO_GP0_IO, 0x00111111);
3461 break;
3462 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL:
3463 /* GPIO0:6 is hooked to FX2 reset pin */
3464 cx_set(MO_GP0_IO, 0x00004040);
3465 cx_clear(MO_GP0_IO, 0x00000040);
3466 msleep(1000);
3467 cx_set(MO_GP0_IO, 0x00004040);
3468 /* FALLTHROUGH */
3469 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
3470 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
3471 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID:
3472 /* GPIO0:0 is hooked to mt352 reset pin */
3473 cx_set(MO_GP0_IO, 0x00000101);
3474 cx_clear(MO_GP0_IO, 0x00000001);
3475 msleep(1);
3476 cx_set(MO_GP0_IO, 0x00000101);
3477 if (0 == core->i2c_rc &&
3478 core->boardnr == CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID)
3479 dvico_fusionhdtv_hybrid_init(core);
3480 break;
3481 case CX88_BOARD_KWORLD_DVB_T:
3482 case CX88_BOARD_DNTV_LIVE_DVB_T:
3483 cx_set(MO_GP0_IO, 0x00000707);
3484 cx_set(MO_GP2_IO, 0x00000101);
3485 cx_clear(MO_GP2_IO, 0x00000001);
3486 msleep(1);
3487 cx_clear(MO_GP0_IO, 0x00000007);
3488 cx_set(MO_GP2_IO, 0x00000101);
3489 break;
3490 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
3491 cx_write(MO_GP0_IO, 0x00080808);
3492 break;
3493 case CX88_BOARD_ATI_HDTVWONDER:
3494 if (0 == core->i2c_rc) {
3495 /* enable tuner */
3496 int i;
3497 static const u8 buffer [][2] = {
3498 {0x10,0x12},
3499 {0x13,0x04},
3500 {0x16,0x00},
3501 {0x14,0x04},
3502 {0x17,0x00}
3503 };
3504 core->i2c_client.addr = 0x0a;
3505
3506 for (i = 0; i < ARRAY_SIZE(buffer); i++)
3507 if (2 != i2c_master_send(&core->i2c_client,
3508 buffer[i],2))
3509 warn_printk(core, "Unable to enable "
3510 "tuner(%i).\n", i);
3511 }
3512 break;
3513 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
3514 {
3515 struct v4l2_priv_tun_config tea5767_cfg;
3516 struct tea5767_ctrl ctl;
3517
3518 memset(&ctl, 0, sizeof(ctl));
3519
3520 ctl.high_cut = 1;
3521 ctl.st_noise = 1;
3522 ctl.deemph_75 = 1;
3523 ctl.xtal_freq = TEA5767_HIGH_LO_13MHz;
3524
3525 tea5767_cfg.tuner = TUNER_TEA5767;
3526 tea5767_cfg.priv = &ctl;
3527
3528 call_all(core, tuner, s_config, &tea5767_cfg);
3529 break;
3530 }
3531 case CX88_BOARD_TEVII_S420:
3532 case CX88_BOARD_TEVII_S460:
3533 case CX88_BOARD_TEVII_S464:
3534 case CX88_BOARD_OMICOM_SS4_PCI:
3535 case CX88_BOARD_TBS_8910:
3536 case CX88_BOARD_TBS_8920:
3537 case CX88_BOARD_PROF_6200:
3538 case CX88_BOARD_PROF_7300:
3539 case CX88_BOARD_PROF_7301:
3540 case CX88_BOARD_SATTRADE_ST4200:
3541 cx_write(MO_GP0_IO, 0x8000);
3542 msleep(100);
3543 cx_write(MO_SRST_IO, 0);
3544 msleep(10);
3545 cx_write(MO_GP0_IO, 0x8080);
3546 msleep(100);
3547 cx_write(MO_SRST_IO, 1);
3548 msleep(100);
3549 break;
3550 } /*end switch() */
3551
3552
3553 /* Setup tuners */
3554 if ((core->board.radio_type != UNSET)) {
3555 tun_setup.mode_mask = T_RADIO;
3556 tun_setup.type = core->board.radio_type;
3557 tun_setup.addr = core->board.radio_addr;
3558 tun_setup.tuner_callback = cx88_tuner_callback;
3559 call_all(core, tuner, s_type_addr, &tun_setup);
3560 mode_mask &= ~T_RADIO;
3561 }
3562
3563 if (core->board.tuner_type != TUNER_ABSENT) {
3564 tun_setup.mode_mask = mode_mask;
3565 tun_setup.type = core->board.tuner_type;
3566 tun_setup.addr = core->board.tuner_addr;
3567 tun_setup.tuner_callback = cx88_tuner_callback;
3568
3569 call_all(core, tuner, s_type_addr, &tun_setup);
3570 }
3571
3572 if (core->board.tda9887_conf) {
3573 struct v4l2_priv_tun_config tda9887_cfg;
3574
3575 tda9887_cfg.tuner = TUNER_TDA9887;
3576 tda9887_cfg.priv = &core->board.tda9887_conf;
3577
3578 call_all(core, tuner, s_config, &tda9887_cfg);
3579 }
3580
3581 if (core->board.tuner_type == TUNER_XC2028) {
3582 struct v4l2_priv_tun_config xc2028_cfg;
3583 struct xc2028_ctrl ctl;
3584
3585 /* Fills device-dependent initialization parameters */
3586 cx88_setup_xc3028(core, &ctl);
3587
3588 /* Sends parameters to xc2028/3028 tuner */
3589 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
3590 xc2028_cfg.tuner = TUNER_XC2028;
3591 xc2028_cfg.priv = &ctl;
3592 info_printk(core, "Asking xc2028/3028 to load firmware %s\n",
3593 ctl.fname);
3594 call_all(core, tuner, s_config, &xc2028_cfg);
3595 }
3596 call_all(core, core, s_power, 0);
3597}
3598
3599/* ------------------------------------------------------------------ */
3600
3601static int cx88_pci_quirks(const char *name, struct pci_dev *pci)
3602{
3603 unsigned int lat = UNSET;
3604 u8 ctrl = 0;
3605 u8 value;
3606
3607 /* check pci quirks */
3608 if (pci_pci_problems & PCIPCI_TRITON) {
3609 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON -- set TBFX\n",
3610 name);
3611 ctrl |= CX88X_EN_TBFX;
3612 }
3613 if (pci_pci_problems & PCIPCI_NATOMA) {
3614 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA -- set TBFX\n",
3615 name);
3616 ctrl |= CX88X_EN_TBFX;
3617 }
3618 if (pci_pci_problems & PCIPCI_VIAETBF) {
3619 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF -- set TBFX\n",
3620 name);
3621 ctrl |= CX88X_EN_TBFX;
3622 }
3623 if (pci_pci_problems & PCIPCI_VSFX) {
3624 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX -- set VSFX\n",
3625 name);
3626 ctrl |= CX88X_EN_VSFX;
3627 }
3628#ifdef PCIPCI_ALIMAGIK
3629 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
3630 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
3631 name);
3632 lat = 0x0A;
3633 }
3634#endif
3635
3636 /* check insmod options */
3637 if (UNSET != latency)
3638 lat = latency;
3639
3640 /* apply stuff */
3641 if (ctrl) {
3642 pci_read_config_byte(pci, CX88X_DEVCTRL, &value);
3643 value |= ctrl;
3644 pci_write_config_byte(pci, CX88X_DEVCTRL, value);
3645 }
3646 if (UNSET != lat) {
3647 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
3648 name, latency);
3649 pci_write_config_byte(pci, PCI_LATENCY_TIMER, latency);
3650 }
3651 return 0;
3652}
3653
3654int cx88_get_resources(const struct cx88_core *core, struct pci_dev *pci)
3655{
3656 if (request_mem_region(pci_resource_start(pci,0),
3657 pci_resource_len(pci,0),
3658 core->name))
3659 return 0;
3660 printk(KERN_ERR
3661 "%s/%d: Can't get MMIO memory @ 0x%llx, subsystem: %04x:%04x\n",
3662 core->name, PCI_FUNC(pci->devfn),
3663 (unsigned long long)pci_resource_start(pci, 0),
3664 pci->subsystem_vendor, pci->subsystem_device);
3665 return -EBUSY;
3666}
3667
3668/* Allocate and initialize the cx88 core struct. One should hold the
3669 * devlist mutex before calling this. */
3670struct cx88_core *cx88_core_create(struct pci_dev *pci, int nr)
3671{
3672 struct cx88_core *core;
3673 int i;
3674
3675 core = kzalloc(sizeof(*core), GFP_KERNEL);
3676 if (core == NULL)
3677 return NULL;
3678
3679 atomic_inc(&core->refcount);
3680 core->pci_bus = pci->bus->number;
3681 core->pci_slot = PCI_SLOT(pci->devfn);
3682 core->pci_irqmask = PCI_INT_RISC_RD_BERRINT | PCI_INT_RISC_WR_BERRINT |
3683 PCI_INT_BRDG_BERRINT | PCI_INT_SRC_DMA_BERRINT |
3684 PCI_INT_DST_DMA_BERRINT | PCI_INT_IPB_DMA_BERRINT;
3685 mutex_init(&core->lock);
3686
3687 core->nr = nr;
3688 sprintf(core->name, "cx88[%d]", core->nr);
3689
3690 strcpy(core->v4l2_dev.name, core->name);
3691 if (v4l2_device_register(NULL, &core->v4l2_dev)) {
3692 kfree(core);
3693 return NULL;
3694 }
3695
3696 if (v4l2_ctrl_handler_init(&core->video_hdl, 13)) {
3697 v4l2_device_unregister(&core->v4l2_dev);
3698 kfree(core);
3699 return NULL;
3700 }
3701
3702 if (v4l2_ctrl_handler_init(&core->audio_hdl, 13)) {
3703 v4l2_ctrl_handler_free(&core->video_hdl);
3704 v4l2_device_unregister(&core->v4l2_dev);
3705 kfree(core);
3706 return NULL;
3707 }
3708
3709 if (0 != cx88_get_resources(core, pci)) {
3710 v4l2_ctrl_handler_free(&core->video_hdl);
3711 v4l2_ctrl_handler_free(&core->audio_hdl);
3712 v4l2_device_unregister(&core->v4l2_dev);
3713 kfree(core);
3714 return NULL;
3715 }
3716
3717 /* PCI stuff */
3718 cx88_pci_quirks(core->name, pci);
3719 core->lmmio = ioremap(pci_resource_start(pci, 0),
3720 pci_resource_len(pci, 0));
3721 core->bmmio = (u8 __iomem *)core->lmmio;
3722
3723 if (core->lmmio == NULL) {
3724 release_mem_region(pci_resource_start(pci, 0),
3725 pci_resource_len(pci, 0));
3726 v4l2_ctrl_handler_free(&core->video_hdl);
3727 v4l2_ctrl_handler_free(&core->audio_hdl);
3728 v4l2_device_unregister(&core->v4l2_dev);
3729 kfree(core);
3730 return NULL;
3731 }
3732
3733 /* board config */
3734 core->boardnr = UNSET;
3735 if (card[core->nr] < ARRAY_SIZE(cx88_boards))
3736 core->boardnr = card[core->nr];
3737 for (i = 0; UNSET == core->boardnr && i < ARRAY_SIZE(cx88_subids); i++)
3738 if (pci->subsystem_vendor == cx88_subids[i].subvendor &&
3739 pci->subsystem_device == cx88_subids[i].subdevice)
3740 core->boardnr = cx88_subids[i].card;
3741 if (UNSET == core->boardnr) {
3742 core->boardnr = CX88_BOARD_UNKNOWN;
3743 cx88_card_list(core, pci);
3744 }
3745
3746 memcpy(&core->board, &cx88_boards[core->boardnr], sizeof(core->board));
3747
3748 if (!core->board.num_frontends && (core->board.mpeg & CX88_MPEG_DVB))
3749 core->board.num_frontends = 1;
3750
3751 info_printk(core, "subsystem: %04x:%04x, board: %s [card=%d,%s], frontend(s): %d\n",
3752 pci->subsystem_vendor, pci->subsystem_device, core->board.name,
3753 core->boardnr, card[core->nr] == core->boardnr ?
3754 "insmod option" : "autodetected",
3755 core->board.num_frontends);
3756
3757 if (tuner[core->nr] != UNSET)
3758 core->board.tuner_type = tuner[core->nr];
3759 if (radio[core->nr] != UNSET)
3760 core->board.radio_type = radio[core->nr];
3761
3762 info_printk(core, "TV tuner type %d, Radio tuner type %d\n",
3763 core->board.tuner_type, core->board.radio_type);
3764
3765 /* init hardware */
3766 cx88_reset(core);
3767 cx88_card_setup_pre_i2c(core);
3768 cx88_i2c_init(core, pci);
3769
3770 /* load tuner module, if needed */
3771 if (TUNER_ABSENT != core->board.tuner_type) {
3772 /* Ignore 0x6b and 0x6f on cx88 boards.
3773 * FusionHDTV5 RT Gold has an ir receiver at 0x6b
3774 * and an RTC at 0x6f which can get corrupted if probed. */
3775 static const unsigned short tv_addrs[] = {
3776 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
3777 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
3778 0x68, 0x69, 0x6a, 0x6c, 0x6d, 0x6e,
3779 I2C_CLIENT_END
3780 };
3781 int has_demod = (core->board.tda9887_conf & TDA9887_PRESENT);
3782
3783 /* I don't trust the radio_type as is stored in the card
3784 definitions, so we just probe for it.
3785 The radio_type is sometimes missing, or set to UNSET but
3786 later code configures a tea5767.
3787 */
3788 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
3789 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_RADIO));
3790 if (has_demod)
3791 v4l2_i2c_new_subdev(&core->v4l2_dev,
3792 &core->i2c_adap, "tuner",
3793 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
3794 if (core->board.tuner_addr == ADDR_UNSET) {
3795 v4l2_i2c_new_subdev(&core->v4l2_dev,
3796 &core->i2c_adap, "tuner",
3797 0, has_demod ? tv_addrs + 4 : tv_addrs);
3798 } else {
3799 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
3800 "tuner", core->board.tuner_addr, NULL);
3801 }
3802 }
3803
3804 cx88_card_setup(core);
3805 if (!disable_ir) {
3806 cx88_i2c_init_ir(core);
3807 cx88_ir_init(core, pci);
3808 }
3809
3810 return core;
3811}
diff --git a/drivers/media/pci/cx88/cx88-core.c b/drivers/media/pci/cx88/cx88-core.c
new file mode 100644
index 000000000000..c97b174be3ab
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-core.c
@@ -0,0 +1,1131 @@
1/*
2 *
3 * device driver for Conexant 2388x based TV cards
4 * driver core
5 *
6 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * - Multituner support
10 * - video_ioctl2 conversion
11 * - PAL/M fixes
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/kmod.h>
34#include <linux/sound.h>
35#include <linux/interrupt.h>
36#include <linux/pci.h>
37#include <linux/delay.h>
38#include <linux/videodev2.h>
39#include <linux/mutex.h>
40
41#include "cx88.h"
42#include <media/v4l2-common.h>
43#include <media/v4l2-ioctl.h>
44
45MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
46MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
47MODULE_LICENSE("GPL");
48
49/* ------------------------------------------------------------------ */
50
51static unsigned int core_debug;
52module_param(core_debug,int,0644);
53MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
54
55static unsigned int nicam;
56module_param(nicam,int,0644);
57MODULE_PARM_DESC(nicam,"tv audio is nicam");
58
59static unsigned int nocomb;
60module_param(nocomb,int,0644);
61MODULE_PARM_DESC(nocomb,"disable comb filter");
62
63#define dprintk(level,fmt, arg...) if (core_debug >= level) \
64 printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)
65
66static unsigned int cx88_devcount;
67static LIST_HEAD(cx88_devlist);
68static DEFINE_MUTEX(devlist);
69
70#define NO_SYNC_LINE (-1U)
71
72/* @lpi: lines per IRQ, or 0 to not generate irqs. Note: IRQ to be
73 generated _after_ lpi lines are transferred. */
74static __le32* cx88_risc_field(__le32 *rp, struct scatterlist *sglist,
75 unsigned int offset, u32 sync_line,
76 unsigned int bpl, unsigned int padding,
77 unsigned int lines, unsigned int lpi)
78{
79 struct scatterlist *sg;
80 unsigned int line,todo,sol;
81
82 /* sync instruction */
83 if (sync_line != NO_SYNC_LINE)
84 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
85
86 /* scan lines */
87 sg = sglist;
88 for (line = 0; line < lines; line++) {
89 while (offset && offset >= sg_dma_len(sg)) {
90 offset -= sg_dma_len(sg);
91 sg++;
92 }
93 if (lpi && line>0 && !(line % lpi))
94 sol = RISC_SOL | RISC_IRQ1 | RISC_CNT_INC;
95 else
96 sol = RISC_SOL;
97 if (bpl <= sg_dma_len(sg)-offset) {
98 /* fits into current chunk */
99 *(rp++)=cpu_to_le32(RISC_WRITE|sol|RISC_EOL|bpl);
100 *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
101 offset+=bpl;
102 } else {
103 /* scanline needs to be split */
104 todo = bpl;
105 *(rp++)=cpu_to_le32(RISC_WRITE|sol|
106 (sg_dma_len(sg)-offset));
107 *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
108 todo -= (sg_dma_len(sg)-offset);
109 offset = 0;
110 sg++;
111 while (todo > sg_dma_len(sg)) {
112 *(rp++)=cpu_to_le32(RISC_WRITE|
113 sg_dma_len(sg));
114 *(rp++)=cpu_to_le32(sg_dma_address(sg));
115 todo -= sg_dma_len(sg);
116 sg++;
117 }
118 *(rp++)=cpu_to_le32(RISC_WRITE|RISC_EOL|todo);
119 *(rp++)=cpu_to_le32(sg_dma_address(sg));
120 offset += todo;
121 }
122 offset += padding;
123 }
124
125 return rp;
126}
127
128int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
129 struct scatterlist *sglist,
130 unsigned int top_offset, unsigned int bottom_offset,
131 unsigned int bpl, unsigned int padding, unsigned int lines)
132{
133 u32 instructions,fields;
134 __le32 *rp;
135 int rc;
136
137 fields = 0;
138 if (UNSET != top_offset)
139 fields++;
140 if (UNSET != bottom_offset)
141 fields++;
142
143 /* estimate risc mem: worst case is one write per page border +
144 one write per scan line + syncs + jump (all 2 dwords). Padding
145 can cause next bpl to start close to a page border. First DMA
146 region may be smaller than PAGE_SIZE */
147 instructions = fields * (1 + ((bpl + padding) * lines) / PAGE_SIZE + lines);
148 instructions += 2;
149 if ((rc = btcx_riscmem_alloc(pci,risc,instructions*8)) < 0)
150 return rc;
151
152 /* write risc instructions */
153 rp = risc->cpu;
154 if (UNSET != top_offset)
155 rp = cx88_risc_field(rp, sglist, top_offset, 0,
156 bpl, padding, lines, 0);
157 if (UNSET != bottom_offset)
158 rp = cx88_risc_field(rp, sglist, bottom_offset, 0x200,
159 bpl, padding, lines, 0);
160
161 /* save pointer to jmp instruction address */
162 risc->jmp = rp;
163 BUG_ON((risc->jmp - risc->cpu + 2) * sizeof (*risc->cpu) > risc->size);
164 return 0;
165}
166
167int cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
168 struct scatterlist *sglist, unsigned int bpl,
169 unsigned int lines, unsigned int lpi)
170{
171 u32 instructions;
172 __le32 *rp;
173 int rc;
174
175 /* estimate risc mem: worst case is one write per page border +
176 one write per scan line + syncs + jump (all 2 dwords). Here
177 there is no padding and no sync. First DMA region may be smaller
178 than PAGE_SIZE */
179 instructions = 1 + (bpl * lines) / PAGE_SIZE + lines;
180 instructions += 1;
181 if ((rc = btcx_riscmem_alloc(pci,risc,instructions*8)) < 0)
182 return rc;
183
184 /* write risc instructions */
185 rp = risc->cpu;
186 rp = cx88_risc_field(rp, sglist, 0, NO_SYNC_LINE, bpl, 0, lines, lpi);
187
188 /* save pointer to jmp instruction address */
189 risc->jmp = rp;
190 BUG_ON((risc->jmp - risc->cpu + 2) * sizeof (*risc->cpu) > risc->size);
191 return 0;
192}
193
194int cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
195 u32 reg, u32 mask, u32 value)
196{
197 __le32 *rp;
198 int rc;
199
200 if ((rc = btcx_riscmem_alloc(pci, risc, 4*16)) < 0)
201 return rc;
202
203 /* write risc instructions */
204 rp = risc->cpu;
205 *(rp++) = cpu_to_le32(RISC_WRITECR | RISC_IRQ2 | RISC_IMM);
206 *(rp++) = cpu_to_le32(reg);
207 *(rp++) = cpu_to_le32(value);
208 *(rp++) = cpu_to_le32(mask);
209 *(rp++) = cpu_to_le32(RISC_JUMP);
210 *(rp++) = cpu_to_le32(risc->dma);
211 return 0;
212}
213
214void
215cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf)
216{
217 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
218
219 BUG_ON(in_interrupt());
220 videobuf_waiton(q, &buf->vb, 0, 0);
221 videobuf_dma_unmap(q->dev, dma);
222 videobuf_dma_free(dma);
223 btcx_riscmem_free(to_pci_dev(q->dev), &buf->risc);
224 buf->vb.state = VIDEOBUF_NEEDS_INIT;
225}
226
227/* ------------------------------------------------------------------ */
228/* our SRAM memory layout */
229
230/* we are going to put all thr risc programs into host memory, so we
231 * can use the whole SDRAM for the DMA fifos. To simplify things, we
232 * use a static memory layout. That surely will waste memory in case
233 * we don't use all DMA channels at the same time (which will be the
234 * case most of the time). But that still gives us enough FIFO space
235 * to be able to deal with insane long pci latencies ...
236 *
237 * FIFO space allocations:
238 * channel 21 (y video) - 10.0k
239 * channel 22 (u video) - 2.0k
240 * channel 23 (v video) - 2.0k
241 * channel 24 (vbi) - 4.0k
242 * channels 25+26 (audio) - 4.0k
243 * channel 28 (mpeg) - 4.0k
244 * channel 27 (audio rds)- 3.0k
245 * TOTAL = 29.0k
246 *
247 * Every channel has 160 bytes control data (64 bytes instruction
248 * queue and 6 CDT entries), which is close to 2k total.
249 *
250 * Address layout:
251 * 0x0000 - 0x03ff CMDs / reserved
252 * 0x0400 - 0x0bff instruction queues + CDs
253 * 0x0c00 - FIFOs
254 */
255
256const struct sram_channel cx88_sram_channels[] = {
257 [SRAM_CH21] = {
258 .name = "video y / packed",
259 .cmds_start = 0x180040,
260 .ctrl_start = 0x180400,
261 .cdt = 0x180400 + 64,
262 .fifo_start = 0x180c00,
263 .fifo_size = 0x002800,
264 .ptr1_reg = MO_DMA21_PTR1,
265 .ptr2_reg = MO_DMA21_PTR2,
266 .cnt1_reg = MO_DMA21_CNT1,
267 .cnt2_reg = MO_DMA21_CNT2,
268 },
269 [SRAM_CH22] = {
270 .name = "video u",
271 .cmds_start = 0x180080,
272 .ctrl_start = 0x1804a0,
273 .cdt = 0x1804a0 + 64,
274 .fifo_start = 0x183400,
275 .fifo_size = 0x000800,
276 .ptr1_reg = MO_DMA22_PTR1,
277 .ptr2_reg = MO_DMA22_PTR2,
278 .cnt1_reg = MO_DMA22_CNT1,
279 .cnt2_reg = MO_DMA22_CNT2,
280 },
281 [SRAM_CH23] = {
282 .name = "video v",
283 .cmds_start = 0x1800c0,
284 .ctrl_start = 0x180540,
285 .cdt = 0x180540 + 64,
286 .fifo_start = 0x183c00,
287 .fifo_size = 0x000800,
288 .ptr1_reg = MO_DMA23_PTR1,
289 .ptr2_reg = MO_DMA23_PTR2,
290 .cnt1_reg = MO_DMA23_CNT1,
291 .cnt2_reg = MO_DMA23_CNT2,
292 },
293 [SRAM_CH24] = {
294 .name = "vbi",
295 .cmds_start = 0x180100,
296 .ctrl_start = 0x1805e0,
297 .cdt = 0x1805e0 + 64,
298 .fifo_start = 0x184400,
299 .fifo_size = 0x001000,
300 .ptr1_reg = MO_DMA24_PTR1,
301 .ptr2_reg = MO_DMA24_PTR2,
302 .cnt1_reg = MO_DMA24_CNT1,
303 .cnt2_reg = MO_DMA24_CNT2,
304 },
305 [SRAM_CH25] = {
306 .name = "audio from",
307 .cmds_start = 0x180140,
308 .ctrl_start = 0x180680,
309 .cdt = 0x180680 + 64,
310 .fifo_start = 0x185400,
311 .fifo_size = 0x001000,
312 .ptr1_reg = MO_DMA25_PTR1,
313 .ptr2_reg = MO_DMA25_PTR2,
314 .cnt1_reg = MO_DMA25_CNT1,
315 .cnt2_reg = MO_DMA25_CNT2,
316 },
317 [SRAM_CH26] = {
318 .name = "audio to",
319 .cmds_start = 0x180180,
320 .ctrl_start = 0x180720,
321 .cdt = 0x180680 + 64, /* same as audio IN */
322 .fifo_start = 0x185400, /* same as audio IN */
323 .fifo_size = 0x001000, /* same as audio IN */
324 .ptr1_reg = MO_DMA26_PTR1,
325 .ptr2_reg = MO_DMA26_PTR2,
326 .cnt1_reg = MO_DMA26_CNT1,
327 .cnt2_reg = MO_DMA26_CNT2,
328 },
329 [SRAM_CH28] = {
330 .name = "mpeg",
331 .cmds_start = 0x180200,
332 .ctrl_start = 0x1807C0,
333 .cdt = 0x1807C0 + 64,
334 .fifo_start = 0x186400,
335 .fifo_size = 0x001000,
336 .ptr1_reg = MO_DMA28_PTR1,
337 .ptr2_reg = MO_DMA28_PTR2,
338 .cnt1_reg = MO_DMA28_CNT1,
339 .cnt2_reg = MO_DMA28_CNT2,
340 },
341 [SRAM_CH27] = {
342 .name = "audio rds",
343 .cmds_start = 0x1801C0,
344 .ctrl_start = 0x180860,
345 .cdt = 0x180860 + 64,
346 .fifo_start = 0x187400,
347 .fifo_size = 0x000C00,
348 .ptr1_reg = MO_DMA27_PTR1,
349 .ptr2_reg = MO_DMA27_PTR2,
350 .cnt1_reg = MO_DMA27_CNT1,
351 .cnt2_reg = MO_DMA27_CNT2,
352 },
353};
354
355int cx88_sram_channel_setup(struct cx88_core *core,
356 const struct sram_channel *ch,
357 unsigned int bpl, u32 risc)
358{
359 unsigned int i,lines;
360 u32 cdt;
361
362 bpl = (bpl + 7) & ~7; /* alignment */
363 cdt = ch->cdt;
364 lines = ch->fifo_size / bpl;
365 if (lines > 6)
366 lines = 6;
367 BUG_ON(lines < 2);
368
369 /* write CDT */
370 for (i = 0; i < lines; i++)
371 cx_write(cdt + 16*i, ch->fifo_start + bpl*i);
372
373 /* write CMDS */
374 cx_write(ch->cmds_start + 0, risc);
375 cx_write(ch->cmds_start + 4, cdt);
376 cx_write(ch->cmds_start + 8, (lines*16) >> 3);
377 cx_write(ch->cmds_start + 12, ch->ctrl_start);
378 cx_write(ch->cmds_start + 16, 64 >> 2);
379 for (i = 20; i < 64; i += 4)
380 cx_write(ch->cmds_start + i, 0);
381
382 /* fill registers */
383 cx_write(ch->ptr1_reg, ch->fifo_start);
384 cx_write(ch->ptr2_reg, cdt);
385 cx_write(ch->cnt1_reg, (bpl >> 3) -1);
386 cx_write(ch->cnt2_reg, (lines*16) >> 3);
387
388 dprintk(2,"sram setup %s: bpl=%d lines=%d\n", ch->name, bpl, lines);
389 return 0;
390}
391
392/* ------------------------------------------------------------------ */
393/* debug helper code */
394
395static int cx88_risc_decode(u32 risc)
396{
397 static const char * const instr[16] = {
398 [ RISC_SYNC >> 28 ] = "sync",
399 [ RISC_WRITE >> 28 ] = "write",
400 [ RISC_WRITEC >> 28 ] = "writec",
401 [ RISC_READ >> 28 ] = "read",
402 [ RISC_READC >> 28 ] = "readc",
403 [ RISC_JUMP >> 28 ] = "jump",
404 [ RISC_SKIP >> 28 ] = "skip",
405 [ RISC_WRITERM >> 28 ] = "writerm",
406 [ RISC_WRITECM >> 28 ] = "writecm",
407 [ RISC_WRITECR >> 28 ] = "writecr",
408 };
409 static int const incr[16] = {
410 [ RISC_WRITE >> 28 ] = 2,
411 [ RISC_JUMP >> 28 ] = 2,
412 [ RISC_WRITERM >> 28 ] = 3,
413 [ RISC_WRITECM >> 28 ] = 3,
414 [ RISC_WRITECR >> 28 ] = 4,
415 };
416 static const char * const bits[] = {
417 "12", "13", "14", "resync",
418 "cnt0", "cnt1", "18", "19",
419 "20", "21", "22", "23",
420 "irq1", "irq2", "eol", "sol",
421 };
422 int i;
423
424 printk("0x%08x [ %s", risc,
425 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
426 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
427 if (risc & (1 << (i + 12)))
428 printk(" %s",bits[i]);
429 printk(" count=%d ]\n", risc & 0xfff);
430 return incr[risc >> 28] ? incr[risc >> 28] : 1;
431}
432
433
434void cx88_sram_channel_dump(struct cx88_core *core,
435 const struct sram_channel *ch)
436{
437 static const char * const name[] = {
438 "initial risc",
439 "cdt base",
440 "cdt size",
441 "iq base",
442 "iq size",
443 "risc pc",
444 "iq wr ptr",
445 "iq rd ptr",
446 "cdt current",
447 "pci target",
448 "line / byte",
449 };
450 u32 risc;
451 unsigned int i,j,n;
452
453 printk("%s: %s - dma channel status dump\n",
454 core->name,ch->name);
455 for (i = 0; i < ARRAY_SIZE(name); i++)
456 printk("%s: cmds: %-12s: 0x%08x\n",
457 core->name,name[i],
458 cx_read(ch->cmds_start + 4*i));
459 for (n = 1, i = 0; i < 4; i++) {
460 risc = cx_read(ch->cmds_start + 4 * (i+11));
461 printk("%s: risc%d: ", core->name, i);
462 if (--n)
463 printk("0x%08x [ arg #%d ]\n", risc, n);
464 else
465 n = cx88_risc_decode(risc);
466 }
467 for (i = 0; i < 16; i += n) {
468 risc = cx_read(ch->ctrl_start + 4 * i);
469 printk("%s: iq %x: ", core->name, i);
470 n = cx88_risc_decode(risc);
471 for (j = 1; j < n; j++) {
472 risc = cx_read(ch->ctrl_start + 4 * (i+j));
473 printk("%s: iq %x: 0x%08x [ arg #%d ]\n",
474 core->name, i+j, risc, j);
475 }
476 }
477
478 printk("%s: fifo: 0x%08x -> 0x%x\n",
479 core->name, ch->fifo_start, ch->fifo_start+ch->fifo_size);
480 printk("%s: ctrl: 0x%08x -> 0x%x\n",
481 core->name, ch->ctrl_start, ch->ctrl_start+6*16);
482 printk("%s: ptr1_reg: 0x%08x\n",
483 core->name,cx_read(ch->ptr1_reg));
484 printk("%s: ptr2_reg: 0x%08x\n",
485 core->name,cx_read(ch->ptr2_reg));
486 printk("%s: cnt1_reg: 0x%08x\n",
487 core->name,cx_read(ch->cnt1_reg));
488 printk("%s: cnt2_reg: 0x%08x\n",
489 core->name,cx_read(ch->cnt2_reg));
490}
491
492static const char *cx88_pci_irqs[32] = {
493 "vid", "aud", "ts", "vip", "hst", "5", "6", "tm1",
494 "src_dma", "dst_dma", "risc_rd_err", "risc_wr_err",
495 "brdg_err", "src_dma_err", "dst_dma_err", "ipb_dma_err",
496 "i2c", "i2c_rack", "ir_smp", "gpio0", "gpio1"
497};
498
499void cx88_print_irqbits(const char *name, const char *tag, const char *strings[],
500 int len, u32 bits, u32 mask)
501{
502 unsigned int i;
503
504 printk(KERN_DEBUG "%s: %s [0x%x]", name, tag, bits);
505 for (i = 0; i < len; i++) {
506 if (!(bits & (1 << i)))
507 continue;
508 if (strings[i])
509 printk(" %s", strings[i]);
510 else
511 printk(" %d", i);
512 if (!(mask & (1 << i)))
513 continue;
514 printk("*");
515 }
516 printk("\n");
517}
518
519/* ------------------------------------------------------------------ */
520
521int cx88_core_irq(struct cx88_core *core, u32 status)
522{
523 int handled = 0;
524
525 if (status & PCI_INT_IR_SMPINT) {
526 cx88_ir_irq(core);
527 handled++;
528 }
529 if (!handled)
530 cx88_print_irqbits(core->name, "irq pci",
531 cx88_pci_irqs, ARRAY_SIZE(cx88_pci_irqs),
532 status, core->pci_irqmask);
533 return handled;
534}
535
536void cx88_wakeup(struct cx88_core *core,
537 struct cx88_dmaqueue *q, u32 count)
538{
539 struct cx88_buffer *buf;
540 int bc;
541
542 for (bc = 0;; bc++) {
543 if (list_empty(&q->active))
544 break;
545 buf = list_entry(q->active.next,
546 struct cx88_buffer, vb.queue);
547 /* count comes from the hw and is is 16bit wide --
548 * this trick handles wrap-arounds correctly for
549 * up to 32767 buffers in flight... */
550 if ((s16) (count - buf->count) < 0)
551 break;
552 do_gettimeofday(&buf->vb.ts);
553 dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i,
554 count, buf->count);
555 buf->vb.state = VIDEOBUF_DONE;
556 list_del(&buf->vb.queue);
557 wake_up(&buf->vb.done);
558 }
559 if (list_empty(&q->active)) {
560 del_timer(&q->timeout);
561 } else {
562 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
563 }
564 if (bc != 1)
565 dprintk(2, "%s: %d buffers handled (should be 1)\n",
566 __func__, bc);
567}
568
569void cx88_shutdown(struct cx88_core *core)
570{
571 /* disable RISC controller + IRQs */
572 cx_write(MO_DEV_CNTRL2, 0);
573
574 /* stop dma transfers */
575 cx_write(MO_VID_DMACNTRL, 0x0);
576 cx_write(MO_AUD_DMACNTRL, 0x0);
577 cx_write(MO_TS_DMACNTRL, 0x0);
578 cx_write(MO_VIP_DMACNTRL, 0x0);
579 cx_write(MO_GPHST_DMACNTRL, 0x0);
580
581 /* stop interrupts */
582 cx_write(MO_PCI_INTMSK, 0x0);
583 cx_write(MO_VID_INTMSK, 0x0);
584 cx_write(MO_AUD_INTMSK, 0x0);
585 cx_write(MO_TS_INTMSK, 0x0);
586 cx_write(MO_VIP_INTMSK, 0x0);
587 cx_write(MO_GPHST_INTMSK, 0x0);
588
589 /* stop capturing */
590 cx_write(VID_CAPTURE_CONTROL, 0);
591}
592
593int cx88_reset(struct cx88_core *core)
594{
595 dprintk(1,"%s\n",__func__);
596 cx88_shutdown(core);
597
598 /* clear irq status */
599 cx_write(MO_VID_INTSTAT, 0xFFFFFFFF); // Clear PIV int
600 cx_write(MO_PCI_INTSTAT, 0xFFFFFFFF); // Clear PCI int
601 cx_write(MO_INT1_STAT, 0xFFFFFFFF); // Clear RISC int
602
603 /* wait a bit */
604 msleep(100);
605
606 /* init sram */
607 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21], 720*4, 0);
608 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH22], 128, 0);
609 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH23], 128, 0);
610 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH24], 128, 0);
611 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH25], 128, 0);
612 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH26], 128, 0);
613 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28], 188*4, 0);
614 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH27], 128, 0);
615
616 /* misc init ... */
617 cx_write(MO_INPUT_FORMAT, ((1 << 13) | // agc enable
618 (1 << 12) | // agc gain
619 (1 << 11) | // adaptibe agc
620 (0 << 10) | // chroma agc
621 (0 << 9) | // ckillen
622 (7)));
623
624 /* setup image format */
625 cx_andor(MO_COLOR_CTRL, 0x4000, 0x4000);
626
627 /* setup FIFO Thresholds */
628 cx_write(MO_PDMA_STHRSH, 0x0807);
629 cx_write(MO_PDMA_DTHRSH, 0x0807);
630
631 /* fixes flashing of image */
632 cx_write(MO_AGC_SYNC_TIP1, 0x0380000F);
633 cx_write(MO_AGC_BACK_VBI, 0x00E00555);
634
635 cx_write(MO_VID_INTSTAT, 0xFFFFFFFF); // Clear PIV int
636 cx_write(MO_PCI_INTSTAT, 0xFFFFFFFF); // Clear PCI int
637 cx_write(MO_INT1_STAT, 0xFFFFFFFF); // Clear RISC int
638
639 /* Reset on-board parts */
640 cx_write(MO_SRST_IO, 0);
641 msleep(10);
642 cx_write(MO_SRST_IO, 1);
643
644 return 0;
645}
646
647/* ------------------------------------------------------------------ */
648
649static unsigned int inline norm_swidth(v4l2_std_id norm)
650{
651 return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 754 : 922;
652}
653
654static unsigned int inline norm_hdelay(v4l2_std_id norm)
655{
656 return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 135 : 186;
657}
658
659static unsigned int inline norm_vdelay(v4l2_std_id norm)
660{
661 return (norm & V4L2_STD_625_50) ? 0x24 : 0x18;
662}
663
664static unsigned int inline norm_fsc8(v4l2_std_id norm)
665{
666 if (norm & V4L2_STD_PAL_M)
667 return 28604892; // 3.575611 MHz
668
669 if (norm & (V4L2_STD_PAL_Nc))
670 return 28656448; // 3.582056 MHz
671
672 if (norm & V4L2_STD_NTSC) // All NTSC/M and variants
673 return 28636360; // 3.57954545 MHz +/- 10 Hz
674
675 /* SECAM have also different sub carrier for chroma,
676 but step_db and step_dr, at cx88_set_tvnorm already handles that.
677
678 The same FSC applies to PAL/BGDKIH, PAL/60, NTSC/4.43 and PAL/N
679 */
680
681 return 35468950; // 4.43361875 MHz +/- 5 Hz
682}
683
684static unsigned int inline norm_htotal(v4l2_std_id norm)
685{
686
687 unsigned int fsc4=norm_fsc8(norm)/2;
688
689 /* returns 4*FSC / vtotal / frames per seconds */
690 return (norm & V4L2_STD_625_50) ?
691 ((fsc4+312)/625+12)/25 :
692 ((fsc4+262)/525*1001+15000)/30000;
693}
694
695static unsigned int inline norm_vbipack(v4l2_std_id norm)
696{
697 return (norm & V4L2_STD_625_50) ? 511 : 400;
698}
699
700int cx88_set_scale(struct cx88_core *core, unsigned int width, unsigned int height,
701 enum v4l2_field field)
702{
703 unsigned int swidth = norm_swidth(core->tvnorm);
704 unsigned int sheight = norm_maxh(core->tvnorm);
705 u32 value;
706
707 dprintk(1,"set_scale: %dx%d [%s%s,%s]\n", width, height,
708 V4L2_FIELD_HAS_TOP(field) ? "T" : "",
709 V4L2_FIELD_HAS_BOTTOM(field) ? "B" : "",
710 v4l2_norm_to_name(core->tvnorm));
711 if (!V4L2_FIELD_HAS_BOTH(field))
712 height *= 2;
713
714 // recalc H delay and scale registers
715 value = (width * norm_hdelay(core->tvnorm)) / swidth;
716 value &= 0x3fe;
717 cx_write(MO_HDELAY_EVEN, value);
718 cx_write(MO_HDELAY_ODD, value);
719 dprintk(1,"set_scale: hdelay 0x%04x (width %d)\n", value,swidth);
720
721 value = (swidth * 4096 / width) - 4096;
722 cx_write(MO_HSCALE_EVEN, value);
723 cx_write(MO_HSCALE_ODD, value);
724 dprintk(1,"set_scale: hscale 0x%04x\n", value);
725
726 cx_write(MO_HACTIVE_EVEN, width);
727 cx_write(MO_HACTIVE_ODD, width);
728 dprintk(1,"set_scale: hactive 0x%04x\n", width);
729
730 // recalc V scale Register (delay is constant)
731 cx_write(MO_VDELAY_EVEN, norm_vdelay(core->tvnorm));
732 cx_write(MO_VDELAY_ODD, norm_vdelay(core->tvnorm));
733 dprintk(1,"set_scale: vdelay 0x%04x\n", norm_vdelay(core->tvnorm));
734
735 value = (0x10000 - (sheight * 512 / height - 512)) & 0x1fff;
736 cx_write(MO_VSCALE_EVEN, value);
737 cx_write(MO_VSCALE_ODD, value);
738 dprintk(1,"set_scale: vscale 0x%04x\n", value);
739
740 cx_write(MO_VACTIVE_EVEN, sheight);
741 cx_write(MO_VACTIVE_ODD, sheight);
742 dprintk(1,"set_scale: vactive 0x%04x\n", sheight);
743
744 // setup filters
745 value = 0;
746 value |= (1 << 19); // CFILT (default)
747 if (core->tvnorm & V4L2_STD_SECAM) {
748 value |= (1 << 15);
749 value |= (1 << 16);
750 }
751 if (INPUT(core->input).type == CX88_VMUX_SVIDEO)
752 value |= (1 << 13) | (1 << 5);
753 if (V4L2_FIELD_INTERLACED == field)
754 value |= (1 << 3); // VINT (interlaced vertical scaling)
755 if (width < 385)
756 value |= (1 << 0); // 3-tap interpolation
757 if (width < 193)
758 value |= (1 << 1); // 5-tap interpolation
759 if (nocomb)
760 value |= (3 << 5); // disable comb filter
761
762 cx_andor(MO_FILTER_EVEN, 0x7ffc7f, value); /* preserve PEAKEN, PSEL */
763 cx_andor(MO_FILTER_ODD, 0x7ffc7f, value);
764 dprintk(1,"set_scale: filter 0x%04x\n", value);
765
766 return 0;
767}
768
769static const u32 xtal = 28636363;
770
771static int set_pll(struct cx88_core *core, int prescale, u32 ofreq)
772{
773 static const u32 pre[] = { 0, 0, 0, 3, 2, 1 };
774 u64 pll;
775 u32 reg;
776 int i;
777
778 if (prescale < 2)
779 prescale = 2;
780 if (prescale > 5)
781 prescale = 5;
782
783 pll = ofreq * 8 * prescale * (u64)(1 << 20);
784 do_div(pll,xtal);
785 reg = (pll & 0x3ffffff) | (pre[prescale] << 26);
786 if (((reg >> 20) & 0x3f) < 14) {
787 printk("%s/0: pll out of range\n",core->name);
788 return -1;
789 }
790
791 dprintk(1,"set_pll: MO_PLL_REG 0x%08x [old=0x%08x,freq=%d]\n",
792 reg, cx_read(MO_PLL_REG), ofreq);
793 cx_write(MO_PLL_REG, reg);
794 for (i = 0; i < 100; i++) {
795 reg = cx_read(MO_DEVICE_STATUS);
796 if (reg & (1<<2)) {
797 dprintk(1,"pll locked [pre=%d,ofreq=%d]\n",
798 prescale,ofreq);
799 return 0;
800 }
801 dprintk(1,"pll not locked yet, waiting ...\n");
802 msleep(10);
803 }
804 dprintk(1,"pll NOT locked [pre=%d,ofreq=%d]\n",prescale,ofreq);
805 return -1;
806}
807
808int cx88_start_audio_dma(struct cx88_core *core)
809{
810 /* constant 128 made buzz in analog Nicam-stereo for bigger fifo_size */
811 int bpl = cx88_sram_channels[SRAM_CH25].fifo_size/4;
812
813 int rds_bpl = cx88_sram_channels[SRAM_CH27].fifo_size/AUD_RDS_LINES;
814
815 /* If downstream RISC is enabled, bail out; ALSA is managing DMA */
816 if (cx_read(MO_AUD_DMACNTRL) & 0x10)
817 return 0;
818
819 /* setup fifo + format */
820 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH25], bpl, 0);
821 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH26], bpl, 0);
822 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH27],
823 rds_bpl, 0);
824
825 cx_write(MO_AUDD_LNGTH, bpl); /* fifo bpl size */
826 cx_write(MO_AUDR_LNGTH, rds_bpl); /* fifo bpl size */
827
828 /* enable Up, Down and Audio RDS fifo */
829 cx_write(MO_AUD_DMACNTRL, 0x0007);
830
831 return 0;
832}
833
834int cx88_stop_audio_dma(struct cx88_core *core)
835{
836 /* If downstream RISC is enabled, bail out; ALSA is managing DMA */
837 if (cx_read(MO_AUD_DMACNTRL) & 0x10)
838 return 0;
839
840 /* stop dma */
841 cx_write(MO_AUD_DMACNTRL, 0x0000);
842
843 return 0;
844}
845
846static int set_tvaudio(struct cx88_core *core)
847{
848 v4l2_std_id norm = core->tvnorm;
849
850 if (CX88_VMUX_TELEVISION != INPUT(core->input).type &&
851 CX88_VMUX_CABLE != INPUT(core->input).type)
852 return 0;
853
854 if (V4L2_STD_PAL_BG & norm) {
855 core->tvaudio = WW_BG;
856
857 } else if (V4L2_STD_PAL_DK & norm) {
858 core->tvaudio = WW_DK;
859
860 } else if (V4L2_STD_PAL_I & norm) {
861 core->tvaudio = WW_I;
862
863 } else if (V4L2_STD_SECAM_L & norm) {
864 core->tvaudio = WW_L;
865
866 } else if ((V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H) & norm) {
867 core->tvaudio = WW_BG;
868
869 } else if (V4L2_STD_SECAM_DK & norm) {
870 core->tvaudio = WW_DK;
871
872 } else if ((V4L2_STD_NTSC_M & norm) ||
873 (V4L2_STD_PAL_M & norm)) {
874 core->tvaudio = WW_BTSC;
875
876 } else if (V4L2_STD_NTSC_M_JP & norm) {
877 core->tvaudio = WW_EIAJ;
878
879 } else {
880 printk("%s/0: tvaudio support needs work for this tv norm [%s], sorry\n",
881 core->name, v4l2_norm_to_name(core->tvnorm));
882 core->tvaudio = WW_NONE;
883 return 0;
884 }
885
886 cx_andor(MO_AFECFG_IO, 0x1f, 0x0);
887 cx88_set_tvaudio(core);
888 /* cx88_set_stereo(dev,V4L2_TUNER_MODE_STEREO); */
889
890/*
891 This should be needed only on cx88-alsa. It seems that some cx88 chips have
892 bugs and does require DMA enabled for it to work.
893 */
894 cx88_start_audio_dma(core);
895 return 0;
896}
897
898
899
900int cx88_set_tvnorm(struct cx88_core *core, v4l2_std_id norm)
901{
902 u32 fsc8;
903 u32 adc_clock;
904 u32 vdec_clock;
905 u32 step_db,step_dr;
906 u64 tmp64;
907 u32 bdelay,agcdelay,htotal;
908 u32 cxiformat, cxoformat;
909
910 core->tvnorm = norm;
911 fsc8 = norm_fsc8(norm);
912 adc_clock = xtal;
913 vdec_clock = fsc8;
914 step_db = fsc8;
915 step_dr = fsc8;
916
917 if (norm & V4L2_STD_NTSC_M_JP) {
918 cxiformat = VideoFormatNTSCJapan;
919 cxoformat = 0x181f0008;
920 } else if (norm & V4L2_STD_NTSC_443) {
921 cxiformat = VideoFormatNTSC443;
922 cxoformat = 0x181f0008;
923 } else if (norm & V4L2_STD_PAL_M) {
924 cxiformat = VideoFormatPALM;
925 cxoformat = 0x1c1f0008;
926 } else if (norm & V4L2_STD_PAL_N) {
927 cxiformat = VideoFormatPALN;
928 cxoformat = 0x1c1f0008;
929 } else if (norm & V4L2_STD_PAL_Nc) {
930 cxiformat = VideoFormatPALNC;
931 cxoformat = 0x1c1f0008;
932 } else if (norm & V4L2_STD_PAL_60) {
933 cxiformat = VideoFormatPAL60;
934 cxoformat = 0x181f0008;
935 } else if (norm & V4L2_STD_NTSC) {
936 cxiformat = VideoFormatNTSC;
937 cxoformat = 0x181f0008;
938 } else if (norm & V4L2_STD_SECAM) {
939 step_db = 4250000 * 8;
940 step_dr = 4406250 * 8;
941
942 cxiformat = VideoFormatSECAM;
943 cxoformat = 0x181f0008;
944 } else { /* PAL */
945 cxiformat = VideoFormatPAL;
946 cxoformat = 0x181f0008;
947 }
948
949 dprintk(1,"set_tvnorm: \"%s\" fsc8=%d adc=%d vdec=%d db/dr=%d/%d\n",
950 v4l2_norm_to_name(core->tvnorm), fsc8, adc_clock, vdec_clock,
951 step_db, step_dr);
952 set_pll(core,2,vdec_clock);
953
954 dprintk(1,"set_tvnorm: MO_INPUT_FORMAT 0x%08x [old=0x%08x]\n",
955 cxiformat, cx_read(MO_INPUT_FORMAT) & 0x0f);
956 /* Chroma AGC must be disabled if SECAM is used, we enable it
957 by default on PAL and NTSC */
958 cx_andor(MO_INPUT_FORMAT, 0x40f,
959 norm & V4L2_STD_SECAM ? cxiformat : cxiformat | 0x400);
960
961 // FIXME: as-is from DScaler
962 dprintk(1,"set_tvnorm: MO_OUTPUT_FORMAT 0x%08x [old=0x%08x]\n",
963 cxoformat, cx_read(MO_OUTPUT_FORMAT));
964 cx_write(MO_OUTPUT_FORMAT, cxoformat);
965
966 // MO_SCONV_REG = adc clock / video dec clock * 2^17
967 tmp64 = adc_clock * (u64)(1 << 17);
968 do_div(tmp64, vdec_clock);
969 dprintk(1,"set_tvnorm: MO_SCONV_REG 0x%08x [old=0x%08x]\n",
970 (u32)tmp64, cx_read(MO_SCONV_REG));
971 cx_write(MO_SCONV_REG, (u32)tmp64);
972
973 // MO_SUB_STEP = 8 * fsc / video dec clock * 2^22
974 tmp64 = step_db * (u64)(1 << 22);
975 do_div(tmp64, vdec_clock);
976 dprintk(1,"set_tvnorm: MO_SUB_STEP 0x%08x [old=0x%08x]\n",
977 (u32)tmp64, cx_read(MO_SUB_STEP));
978 cx_write(MO_SUB_STEP, (u32)tmp64);
979
980 // MO_SUB_STEP_DR = 8 * 4406250 / video dec clock * 2^22
981 tmp64 = step_dr * (u64)(1 << 22);
982 do_div(tmp64, vdec_clock);
983 dprintk(1,"set_tvnorm: MO_SUB_STEP_DR 0x%08x [old=0x%08x]\n",
984 (u32)tmp64, cx_read(MO_SUB_STEP_DR));
985 cx_write(MO_SUB_STEP_DR, (u32)tmp64);
986
987 // bdelay + agcdelay
988 bdelay = vdec_clock * 65 / 20000000 + 21;
989 agcdelay = vdec_clock * 68 / 20000000 + 15;
990 dprintk(1,"set_tvnorm: MO_AGC_BURST 0x%08x [old=0x%08x,bdelay=%d,agcdelay=%d]\n",
991 (bdelay << 8) | agcdelay, cx_read(MO_AGC_BURST), bdelay, agcdelay);
992 cx_write(MO_AGC_BURST, (bdelay << 8) | agcdelay);
993
994 // htotal
995 tmp64 = norm_htotal(norm) * (u64)vdec_clock;
996 do_div(tmp64, fsc8);
997 htotal = (u32)tmp64;
998 dprintk(1,"set_tvnorm: MO_HTOTAL 0x%08x [old=0x%08x,htotal=%d]\n",
999 htotal, cx_read(MO_HTOTAL), (u32)tmp64);
1000 cx_andor(MO_HTOTAL, 0x07ff, htotal);
1001
1002 // vbi stuff, set vbi offset to 10 (for 20 Clk*2 pixels), this makes
1003 // the effective vbi offset ~244 samples, the same as the Bt8x8
1004 cx_write(MO_VBI_PACKET, (10<<11) | norm_vbipack(norm));
1005
1006 // this is needed as well to set all tvnorm parameter
1007 cx88_set_scale(core, 320, 240, V4L2_FIELD_INTERLACED);
1008
1009 // audio
1010 set_tvaudio(core);
1011
1012 // tell i2c chips
1013 call_all(core, core, s_std, norm);
1014
1015 /* The chroma_agc control should be inaccessible if the video format is SECAM */
1016 v4l2_ctrl_grab(core->chroma_agc, cxiformat == VideoFormatSECAM);
1017
1018 // done
1019 return 0;
1020}
1021
1022/* ------------------------------------------------------------------ */
1023
1024struct video_device *cx88_vdev_init(struct cx88_core *core,
1025 struct pci_dev *pci,
1026 const struct video_device *template_,
1027 const char *type)
1028{
1029 struct video_device *vfd;
1030
1031 vfd = video_device_alloc();
1032 if (NULL == vfd)
1033 return NULL;
1034 *vfd = *template_;
1035 vfd->v4l2_dev = &core->v4l2_dev;
1036 vfd->release = video_device_release;
1037 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
1038 core->name, type, core->board.name);
1039 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1040 return vfd;
1041}
1042
1043struct cx88_core* cx88_core_get(struct pci_dev *pci)
1044{
1045 struct cx88_core *core;
1046
1047 mutex_lock(&devlist);
1048 list_for_each_entry(core, &cx88_devlist, devlist) {
1049 if (pci->bus->number != core->pci_bus)
1050 continue;
1051 if (PCI_SLOT(pci->devfn) != core->pci_slot)
1052 continue;
1053
1054 if (0 != cx88_get_resources(core, pci)) {
1055 mutex_unlock(&devlist);
1056 return NULL;
1057 }
1058 atomic_inc(&core->refcount);
1059 mutex_unlock(&devlist);
1060 return core;
1061 }
1062
1063 core = cx88_core_create(pci, cx88_devcount);
1064 if (NULL != core) {
1065 cx88_devcount++;
1066 list_add_tail(&core->devlist, &cx88_devlist);
1067 }
1068
1069 mutex_unlock(&devlist);
1070 return core;
1071}
1072
1073void cx88_core_put(struct cx88_core *core, struct pci_dev *pci)
1074{
1075 release_mem_region(pci_resource_start(pci,0),
1076 pci_resource_len(pci,0));
1077
1078 if (!atomic_dec_and_test(&core->refcount))
1079 return;
1080
1081 mutex_lock(&devlist);
1082 cx88_ir_fini(core);
1083 if (0 == core->i2c_rc) {
1084 if (core->i2c_rtc)
1085 i2c_unregister_device(core->i2c_rtc);
1086 i2c_del_adapter(&core->i2c_adap);
1087 }
1088 list_del(&core->devlist);
1089 iounmap(core->lmmio);
1090 cx88_devcount--;
1091 mutex_unlock(&devlist);
1092 v4l2_ctrl_handler_free(&core->video_hdl);
1093 v4l2_ctrl_handler_free(&core->audio_hdl);
1094 v4l2_device_unregister(&core->v4l2_dev);
1095 kfree(core);
1096}
1097
1098/* ------------------------------------------------------------------ */
1099
1100EXPORT_SYMBOL(cx88_print_irqbits);
1101
1102EXPORT_SYMBOL(cx88_core_irq);
1103EXPORT_SYMBOL(cx88_wakeup);
1104EXPORT_SYMBOL(cx88_reset);
1105EXPORT_SYMBOL(cx88_shutdown);
1106
1107EXPORT_SYMBOL(cx88_risc_buffer);
1108EXPORT_SYMBOL(cx88_risc_databuffer);
1109EXPORT_SYMBOL(cx88_risc_stopper);
1110EXPORT_SYMBOL(cx88_free_buffer);
1111
1112EXPORT_SYMBOL(cx88_sram_channels);
1113EXPORT_SYMBOL(cx88_sram_channel_setup);
1114EXPORT_SYMBOL(cx88_sram_channel_dump);
1115
1116EXPORT_SYMBOL(cx88_set_tvnorm);
1117EXPORT_SYMBOL(cx88_set_scale);
1118
1119EXPORT_SYMBOL(cx88_vdev_init);
1120EXPORT_SYMBOL(cx88_core_get);
1121EXPORT_SYMBOL(cx88_core_put);
1122
1123EXPORT_SYMBOL(cx88_ir_start);
1124EXPORT_SYMBOL(cx88_ir_stop);
1125
1126/*
1127 * Local variables:
1128 * c-basic-offset: 8
1129 * End:
1130 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
1131 */
diff --git a/drivers/media/pci/cx88/cx88-dsp.c b/drivers/media/pci/cx88/cx88-dsp.c
new file mode 100644
index 000000000000..a9907265ff66
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-dsp.c
@@ -0,0 +1,322 @@
1/*
2 *
3 * Stereo and SAP detection for cx88
4 *
5 * Copyright (c) 2009 Marton Balint <cus@fazekas.hu>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/slab.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/jiffies.h>
26#include <asm/div64.h>
27
28#include "cx88.h"
29#include "cx88-reg.h"
30
31#define INT_PI ((s32)(3.141592653589 * 32768.0))
32
33#define compat_remainder(a, b) \
34 ((float)(((s32)((a)*100))%((s32)((b)*100)))/100.0)
35
36#define baseband_freq(carrier, srate, tone) ((s32)( \
37 (compat_remainder(carrier + tone, srate)) / srate * 2 * INT_PI))
38
39/* We calculate the baseband frequencies of the carrier and the pilot tones
40 * based on the the sampling rate of the audio rds fifo. */
41
42#define FREQ_A2_CARRIER baseband_freq(54687.5, 2689.36, 0.0)
43#define FREQ_A2_DUAL baseband_freq(54687.5, 2689.36, 274.1)
44#define FREQ_A2_STEREO baseband_freq(54687.5, 2689.36, 117.5)
45
46/* The frequencies below are from the reference driver. They probably need
47 * further adjustments, because they are not tested at all. You may even need
48 * to play a bit with the registers of the chip to select the proper signal
49 * for the input of the audio rds fifo, and measure it's sampling rate to
50 * calculate the proper baseband frequencies... */
51
52#define FREQ_A2M_CARRIER ((s32)(2.114516 * 32768.0))
53#define FREQ_A2M_DUAL ((s32)(2.754916 * 32768.0))
54#define FREQ_A2M_STEREO ((s32)(2.462326 * 32768.0))
55
56#define FREQ_EIAJ_CARRIER ((s32)(1.963495 * 32768.0)) /* 5pi/8 */
57#define FREQ_EIAJ_DUAL ((s32)(2.562118 * 32768.0))
58#define FREQ_EIAJ_STEREO ((s32)(2.601053 * 32768.0))
59
60#define FREQ_BTSC_DUAL ((s32)(1.963495 * 32768.0)) /* 5pi/8 */
61#define FREQ_BTSC_DUAL_REF ((s32)(1.374446 * 32768.0)) /* 7pi/16 */
62
63#define FREQ_BTSC_SAP ((s32)(2.471532 * 32768.0))
64#define FREQ_BTSC_SAP_REF ((s32)(1.730072 * 32768.0))
65
66/* The spectrum of the signal should be empty between these frequencies. */
67#define FREQ_NOISE_START ((s32)(0.100000 * 32768.0))
68#define FREQ_NOISE_END ((s32)(1.200000 * 32768.0))
69
70static unsigned int dsp_debug;
71module_param(dsp_debug, int, 0644);
72MODULE_PARM_DESC(dsp_debug, "enable audio dsp debug messages");
73
74#define dprintk(level, fmt, arg...) if (dsp_debug >= level) \
75 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
76
77static s32 int_cos(u32 x)
78{
79 u32 t2, t4, t6, t8;
80 s32 ret;
81 u16 period = x / INT_PI;
82 if (period % 2)
83 return -int_cos(x - INT_PI);
84 x = x % INT_PI;
85 if (x > INT_PI/2)
86 return -int_cos(INT_PI/2 - (x % (INT_PI/2)));
87 /* Now x is between 0 and INT_PI/2.
88 * To calculate cos(x) we use it's Taylor polinom. */
89 t2 = x*x/32768/2;
90 t4 = t2*x/32768*x/32768/3/4;
91 t6 = t4*x/32768*x/32768/5/6;
92 t8 = t6*x/32768*x/32768/7/8;
93 ret = 32768-t2+t4-t6+t8;
94 return ret;
95}
96
97static u32 int_goertzel(s16 x[], u32 N, u32 freq)
98{
99 /* We use the Goertzel algorithm to determine the power of the
100 * given frequency in the signal */
101 s32 s_prev = 0;
102 s32 s_prev2 = 0;
103 s32 coeff = 2*int_cos(freq);
104 u32 i;
105
106 u64 tmp;
107 u32 divisor;
108
109 for (i = 0; i < N; i++) {
110 s32 s = x[i] + ((s64)coeff*s_prev/32768) - s_prev2;
111 s_prev2 = s_prev;
112 s_prev = s;
113 }
114
115 tmp = (s64)s_prev2 * s_prev2 + (s64)s_prev * s_prev -
116 (s64)coeff * s_prev2 * s_prev / 32768;
117
118 /* XXX: N must be low enough so that N*N fits in s32.
119 * Else we need two divisions. */
120 divisor = N * N;
121 do_div(tmp, divisor);
122
123 return (u32) tmp;
124}
125
126static u32 freq_magnitude(s16 x[], u32 N, u32 freq)
127{
128 u32 sum = int_goertzel(x, N, freq);
129 return (u32)int_sqrt(sum);
130}
131
132static u32 noise_magnitude(s16 x[], u32 N, u32 freq_start, u32 freq_end)
133{
134 int i;
135 u32 sum = 0;
136 u32 freq_step;
137 int samples = 5;
138
139 if (N > 192) {
140 /* The last 192 samples are enough for noise detection */
141 x += (N-192);
142 N = 192;
143 }
144
145 freq_step = (freq_end - freq_start) / (samples - 1);
146
147 for (i = 0; i < samples; i++) {
148 sum += int_goertzel(x, N, freq_start);
149 freq_start += freq_step;
150 }
151
152 return (u32)int_sqrt(sum / samples);
153}
154
155static s32 detect_a2_a2m_eiaj(struct cx88_core *core, s16 x[], u32 N)
156{
157 s32 carrier, stereo, dual, noise;
158 s32 carrier_freq, stereo_freq, dual_freq;
159 s32 ret;
160
161 switch (core->tvaudio) {
162 case WW_BG:
163 case WW_DK:
164 carrier_freq = FREQ_A2_CARRIER;
165 stereo_freq = FREQ_A2_STEREO;
166 dual_freq = FREQ_A2_DUAL;
167 break;
168 case WW_M:
169 carrier_freq = FREQ_A2M_CARRIER;
170 stereo_freq = FREQ_A2M_STEREO;
171 dual_freq = FREQ_A2M_DUAL;
172 break;
173 case WW_EIAJ:
174 carrier_freq = FREQ_EIAJ_CARRIER;
175 stereo_freq = FREQ_EIAJ_STEREO;
176 dual_freq = FREQ_EIAJ_DUAL;
177 break;
178 default:
179 printk(KERN_WARNING "%s/0: unsupported audio mode %d for %s\n",
180 core->name, core->tvaudio, __func__);
181 return UNSET;
182 }
183
184 carrier = freq_magnitude(x, N, carrier_freq);
185 stereo = freq_magnitude(x, N, stereo_freq);
186 dual = freq_magnitude(x, N, dual_freq);
187 noise = noise_magnitude(x, N, FREQ_NOISE_START, FREQ_NOISE_END);
188
189 dprintk(1, "detect a2/a2m/eiaj: carrier=%d, stereo=%d, dual=%d, "
190 "noise=%d\n", carrier, stereo, dual, noise);
191
192 if (stereo > dual)
193 ret = V4L2_TUNER_SUB_STEREO;
194 else
195 ret = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
196
197 if (core->tvaudio == WW_EIAJ) {
198 /* EIAJ checks may need adjustments */
199 if ((carrier > max(stereo, dual)*2) &&
200 (carrier < max(stereo, dual)*6) &&
201 (carrier > 20 && carrier < 200) &&
202 (max(stereo, dual) > min(stereo, dual))) {
203 /* For EIAJ the carrier is always present,
204 so we probably don't need noise detection */
205 return ret;
206 }
207 } else {
208 if ((carrier > max(stereo, dual)*2) &&
209 (carrier < max(stereo, dual)*8) &&
210 (carrier > 20 && carrier < 200) &&
211 (noise < 10) &&
212 (max(stereo, dual) > min(stereo, dual)*2)) {
213 return ret;
214 }
215 }
216 return V4L2_TUNER_SUB_MONO;
217}
218
219static s32 detect_btsc(struct cx88_core *core, s16 x[], u32 N)
220{
221 s32 sap_ref = freq_magnitude(x, N, FREQ_BTSC_SAP_REF);
222 s32 sap = freq_magnitude(x, N, FREQ_BTSC_SAP);
223 s32 dual_ref = freq_magnitude(x, N, FREQ_BTSC_DUAL_REF);
224 s32 dual = freq_magnitude(x, N, FREQ_BTSC_DUAL);
225 dprintk(1, "detect btsc: dual_ref=%d, dual=%d, sap_ref=%d, sap=%d"
226 "\n", dual_ref, dual, sap_ref, sap);
227 /* FIXME: Currently not supported */
228 return UNSET;
229}
230
231static s16 *read_rds_samples(struct cx88_core *core, u32 *N)
232{
233 const struct sram_channel *srch = &cx88_sram_channels[SRAM_CH27];
234 s16 *samples;
235
236 unsigned int i;
237 unsigned int bpl = srch->fifo_size/AUD_RDS_LINES;
238 unsigned int spl = bpl/4;
239 unsigned int sample_count = spl*(AUD_RDS_LINES-1);
240
241 u32 current_address = cx_read(srch->ptr1_reg);
242 u32 offset = (current_address - srch->fifo_start + bpl);
243
244 dprintk(1, "read RDS samples: current_address=%08x (offset=%08x), "
245 "sample_count=%d, aud_intstat=%08x\n", current_address,
246 current_address - srch->fifo_start, sample_count,
247 cx_read(MO_AUD_INTSTAT));
248
249 samples = kmalloc(sizeof(s16)*sample_count, GFP_KERNEL);
250 if (!samples)
251 return NULL;
252
253 *N = sample_count;
254
255 for (i = 0; i < sample_count; i++) {
256 offset = offset % (AUD_RDS_LINES*bpl);
257 samples[i] = cx_read(srch->fifo_start + offset);
258 offset += 4;
259 }
260
261 if (dsp_debug >= 2) {
262 dprintk(2, "RDS samples dump: ");
263 for (i = 0; i < sample_count; i++)
264 printk("%hd ", samples[i]);
265 printk(".\n");
266 }
267
268 return samples;
269}
270
271s32 cx88_dsp_detect_stereo_sap(struct cx88_core *core)
272{
273 s16 *samples;
274 u32 N = 0;
275 s32 ret = UNSET;
276
277 /* If audio RDS fifo is disabled, we can't read the samples */
278 if (!(cx_read(MO_AUD_DMACNTRL) & 0x04))
279 return ret;
280 if (!(cx_read(AUD_CTL) & EN_FMRADIO_EN_RDS))
281 return ret;
282
283 /* Wait at least 500 ms after an audio standard change */
284 if (time_before(jiffies, core->last_change + msecs_to_jiffies(500)))
285 return ret;
286
287 samples = read_rds_samples(core, &N);
288
289 if (!samples)
290 return ret;
291
292 switch (core->tvaudio) {
293 case WW_BG:
294 case WW_DK:
295 case WW_EIAJ:
296 case WW_M:
297 ret = detect_a2_a2m_eiaj(core, samples, N);
298 break;
299 case WW_BTSC:
300 ret = detect_btsc(core, samples, N);
301 break;
302 case WW_NONE:
303 case WW_I:
304 case WW_L:
305 case WW_I2SPT:
306 case WW_FM:
307 case WW_I2SADC:
308 break;
309 }
310
311 kfree(samples);
312
313 if (UNSET != ret)
314 dprintk(1, "stereo/sap detection result:%s%s%s\n",
315 (ret & V4L2_TUNER_SUB_MONO) ? " mono" : "",
316 (ret & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
317 (ret & V4L2_TUNER_SUB_LANG2) ? " dual" : "");
318
319 return ret;
320}
321EXPORT_SYMBOL(cx88_dsp_detect_stereo_sap);
322
diff --git a/drivers/media/pci/cx88/cx88-dvb.c b/drivers/media/pci/cx88/cx88-dvb.c
new file mode 100644
index 000000000000..d803bba09525
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-dvb.c
@@ -0,0 +1,1778 @@
1/*
2 *
3 * device driver for Conexant 2388x based TV cards
4 * MPEG Transport Stream (DVB) routines
5 *
6 * (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
7 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/device.h>
27#include <linux/fs.h>
28#include <linux/kthread.h>
29#include <linux/file.h>
30#include <linux/suspend.h>
31
32#include "cx88.h"
33#include "dvb-pll.h"
34#include <media/v4l2-common.h>
35
36#include "mt352.h"
37#include "mt352_priv.h"
38#include "cx88-vp3054-i2c.h"
39#include "zl10353.h"
40#include "cx22702.h"
41#include "or51132.h"
42#include "lgdt330x.h"
43#include "s5h1409.h"
44#include "xc4000.h"
45#include "xc5000.h"
46#include "nxt200x.h"
47#include "cx24123.h"
48#include "isl6421.h"
49#include "tuner-simple.h"
50#include "tda9887.h"
51#include "s5h1411.h"
52#include "stv0299.h"
53#include "z0194a.h"
54#include "stv0288.h"
55#include "stb6000.h"
56#include "cx24116.h"
57#include "stv0900.h"
58#include "stb6100.h"
59#include "stb6100_proc.h"
60#include "mb86a16.h"
61#include "ds3000.h"
62
63MODULE_DESCRIPTION("driver for cx2388x based DVB cards");
64MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
65MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
66MODULE_LICENSE("GPL");
67MODULE_VERSION(CX88_VERSION);
68
69static unsigned int debug;
70module_param(debug, int, 0644);
71MODULE_PARM_DESC(debug,"enable debug messages [dvb]");
72
73static unsigned int dvb_buf_tscnt = 32;
74module_param(dvb_buf_tscnt, int, 0644);
75MODULE_PARM_DESC(dvb_buf_tscnt, "DVB Buffer TS count [dvb]");
76
77DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
78
79#define dprintk(level,fmt, arg...) if (debug >= level) \
80 printk(KERN_DEBUG "%s/2-dvb: " fmt, core->name, ## arg)
81
82/* ------------------------------------------------------------------ */
83
84static int dvb_buf_setup(struct videobuf_queue *q,
85 unsigned int *count, unsigned int *size)
86{
87 struct cx8802_dev *dev = q->priv_data;
88
89 dev->ts_packet_size = 188 * 4;
90 dev->ts_packet_count = dvb_buf_tscnt;
91
92 *size = dev->ts_packet_size * dev->ts_packet_count;
93 *count = dvb_buf_tscnt;
94 return 0;
95}
96
97static int dvb_buf_prepare(struct videobuf_queue *q,
98 struct videobuf_buffer *vb, enum v4l2_field field)
99{
100 struct cx8802_dev *dev = q->priv_data;
101 return cx8802_buf_prepare(q, dev, (struct cx88_buffer*)vb,field);
102}
103
104static void dvb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
105{
106 struct cx8802_dev *dev = q->priv_data;
107 cx8802_buf_queue(dev, (struct cx88_buffer*)vb);
108}
109
110static void dvb_buf_release(struct videobuf_queue *q,
111 struct videobuf_buffer *vb)
112{
113 cx88_free_buffer(q, (struct cx88_buffer*)vb);
114}
115
116static const struct videobuf_queue_ops dvb_qops = {
117 .buf_setup = dvb_buf_setup,
118 .buf_prepare = dvb_buf_prepare,
119 .buf_queue = dvb_buf_queue,
120 .buf_release = dvb_buf_release,
121};
122
123/* ------------------------------------------------------------------ */
124
125static int cx88_dvb_bus_ctrl(struct dvb_frontend* fe, int acquire)
126{
127 struct cx8802_dev *dev= fe->dvb->priv;
128 struct cx8802_driver *drv = NULL;
129 int ret = 0;
130 int fe_id;
131
132 fe_id = videobuf_dvb_find_frontend(&dev->frontends, fe);
133 if (!fe_id) {
134 printk(KERN_ERR "%s() No frontend found\n", __func__);
135 return -EINVAL;
136 }
137
138 mutex_lock(&dev->core->lock);
139 drv = cx8802_get_driver(dev, CX88_MPEG_DVB);
140 if (drv) {
141 if (acquire){
142 dev->frontends.active_fe_id = fe_id;
143 ret = drv->request_acquire(drv);
144 } else {
145 ret = drv->request_release(drv);
146 dev->frontends.active_fe_id = 0;
147 }
148 }
149 mutex_unlock(&dev->core->lock);
150
151 return ret;
152}
153
154static void cx88_dvb_gate_ctrl(struct cx88_core *core, int open)
155{
156 struct videobuf_dvb_frontends *f;
157 struct videobuf_dvb_frontend *fe;
158
159 if (!core->dvbdev)
160 return;
161
162 f = &core->dvbdev->frontends;
163
164 if (!f)
165 return;
166
167 if (f->gate <= 1) /* undefined or fe0 */
168 fe = videobuf_dvb_get_frontend(f, 1);
169 else
170 fe = videobuf_dvb_get_frontend(f, f->gate);
171
172 if (fe && fe->dvb.frontend && fe->dvb.frontend->ops.i2c_gate_ctrl)
173 fe->dvb.frontend->ops.i2c_gate_ctrl(fe->dvb.frontend, open);
174}
175
176/* ------------------------------------------------------------------ */
177
178static int dvico_fusionhdtv_demod_init(struct dvb_frontend* fe)
179{
180 static const u8 clock_config [] = { CLOCK_CTL, 0x38, 0x39 };
181 static const u8 reset [] = { RESET, 0x80 };
182 static const u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
183 static const u8 agc_cfg [] = { AGC_TARGET, 0x24, 0x20 };
184 static const u8 gpp_ctl_cfg [] = { GPP_CTL, 0x33 };
185 static const u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
186
187 mt352_write(fe, clock_config, sizeof(clock_config));
188 udelay(200);
189 mt352_write(fe, reset, sizeof(reset));
190 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
191
192 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
193 mt352_write(fe, gpp_ctl_cfg, sizeof(gpp_ctl_cfg));
194 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
195 return 0;
196}
197
198static int dvico_dual_demod_init(struct dvb_frontend *fe)
199{
200 static const u8 clock_config [] = { CLOCK_CTL, 0x38, 0x38 };
201 static const u8 reset [] = { RESET, 0x80 };
202 static const u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
203 static const u8 agc_cfg [] = { AGC_TARGET, 0x28, 0x20 };
204 static const u8 gpp_ctl_cfg [] = { GPP_CTL, 0x33 };
205 static const u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
206
207 mt352_write(fe, clock_config, sizeof(clock_config));
208 udelay(200);
209 mt352_write(fe, reset, sizeof(reset));
210 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
211
212 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
213 mt352_write(fe, gpp_ctl_cfg, sizeof(gpp_ctl_cfg));
214 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
215
216 return 0;
217}
218
219static int dntv_live_dvbt_demod_init(struct dvb_frontend* fe)
220{
221 static const u8 clock_config [] = { 0x89, 0x38, 0x39 };
222 static const u8 reset [] = { 0x50, 0x80 };
223 static const u8 adc_ctl_1_cfg [] = { 0x8E, 0x40 };
224 static const u8 agc_cfg [] = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
225 0x00, 0xFF, 0x00, 0x40, 0x40 };
226 static const u8 dntv_extra[] = { 0xB5, 0x7A };
227 static const u8 capt_range_cfg[] = { 0x75, 0x32 };
228
229 mt352_write(fe, clock_config, sizeof(clock_config));
230 udelay(2000);
231 mt352_write(fe, reset, sizeof(reset));
232 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
233
234 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
235 udelay(2000);
236 mt352_write(fe, dntv_extra, sizeof(dntv_extra));
237 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
238
239 return 0;
240}
241
242static const struct mt352_config dvico_fusionhdtv = {
243 .demod_address = 0x0f,
244 .demod_init = dvico_fusionhdtv_demod_init,
245};
246
247static const struct mt352_config dntv_live_dvbt_config = {
248 .demod_address = 0x0f,
249 .demod_init = dntv_live_dvbt_demod_init,
250};
251
252static const struct mt352_config dvico_fusionhdtv_dual = {
253 .demod_address = 0x0f,
254 .demod_init = dvico_dual_demod_init,
255};
256
257static const struct zl10353_config cx88_terratec_cinergy_ht_pci_mkii_config = {
258 .demod_address = (0x1e >> 1),
259 .no_tuner = 1,
260 .if2 = 45600,
261};
262
263static struct mb86a16_config twinhan_vp1027 = {
264 .demod_address = 0x08,
265};
266
267#if defined(CONFIG_VIDEO_CX88_VP3054) || (defined(CONFIG_VIDEO_CX88_VP3054_MODULE) && defined(MODULE))
268static int dntv_live_dvbt_pro_demod_init(struct dvb_frontend* fe)
269{
270 static const u8 clock_config [] = { 0x89, 0x38, 0x38 };
271 static const u8 reset [] = { 0x50, 0x80 };
272 static const u8 adc_ctl_1_cfg [] = { 0x8E, 0x40 };
273 static const u8 agc_cfg [] = { 0x67, 0x10, 0x20, 0x00, 0xFF, 0xFF,
274 0x00, 0xFF, 0x00, 0x40, 0x40 };
275 static const u8 dntv_extra[] = { 0xB5, 0x7A };
276 static const u8 capt_range_cfg[] = { 0x75, 0x32 };
277
278 mt352_write(fe, clock_config, sizeof(clock_config));
279 udelay(2000);
280 mt352_write(fe, reset, sizeof(reset));
281 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
282
283 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
284 udelay(2000);
285 mt352_write(fe, dntv_extra, sizeof(dntv_extra));
286 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
287
288 return 0;
289}
290
291static const struct mt352_config dntv_live_dvbt_pro_config = {
292 .demod_address = 0x0f,
293 .no_tuner = 1,
294 .demod_init = dntv_live_dvbt_pro_demod_init,
295};
296#endif
297
298static const struct zl10353_config dvico_fusionhdtv_hybrid = {
299 .demod_address = 0x0f,
300 .no_tuner = 1,
301};
302
303static const struct zl10353_config dvico_fusionhdtv_xc3028 = {
304 .demod_address = 0x0f,
305 .if2 = 45600,
306 .no_tuner = 1,
307};
308
309static const struct mt352_config dvico_fusionhdtv_mt352_xc3028 = {
310 .demod_address = 0x0f,
311 .if2 = 4560,
312 .no_tuner = 1,
313 .demod_init = dvico_fusionhdtv_demod_init,
314};
315
316static const struct zl10353_config dvico_fusionhdtv_plus_v1_1 = {
317 .demod_address = 0x0f,
318};
319
320static const struct cx22702_config connexant_refboard_config = {
321 .demod_address = 0x43,
322 .output_mode = CX22702_SERIAL_OUTPUT,
323};
324
325static const struct cx22702_config hauppauge_hvr_config = {
326 .demod_address = 0x63,
327 .output_mode = CX22702_SERIAL_OUTPUT,
328};
329
330static int or51132_set_ts_param(struct dvb_frontend* fe, int is_punctured)
331{
332 struct cx8802_dev *dev= fe->dvb->priv;
333 dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
334 return 0;
335}
336
337static const struct or51132_config pchdtv_hd3000 = {
338 .demod_address = 0x15,
339 .set_ts_params = or51132_set_ts_param,
340};
341
342static int lgdt330x_pll_rf_set(struct dvb_frontend* fe, int index)
343{
344 struct cx8802_dev *dev= fe->dvb->priv;
345 struct cx88_core *core = dev->core;
346
347 dprintk(1, "%s: index = %d\n", __func__, index);
348 if (index == 0)
349 cx_clear(MO_GP0_IO, 8);
350 else
351 cx_set(MO_GP0_IO, 8);
352 return 0;
353}
354
355static int lgdt330x_set_ts_param(struct dvb_frontend* fe, int is_punctured)
356{
357 struct cx8802_dev *dev= fe->dvb->priv;
358 if (is_punctured)
359 dev->ts_gen_cntrl |= 0x04;
360 else
361 dev->ts_gen_cntrl &= ~0x04;
362 return 0;
363}
364
365static struct lgdt330x_config fusionhdtv_3_gold = {
366 .demod_address = 0x0e,
367 .demod_chip = LGDT3302,
368 .serial_mpeg = 0x04, /* TPSERIAL for 3302 in TOP_CONTROL */
369 .set_ts_params = lgdt330x_set_ts_param,
370};
371
372static const struct lgdt330x_config fusionhdtv_5_gold = {
373 .demod_address = 0x0e,
374 .demod_chip = LGDT3303,
375 .serial_mpeg = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
376 .set_ts_params = lgdt330x_set_ts_param,
377};
378
379static const struct lgdt330x_config pchdtv_hd5500 = {
380 .demod_address = 0x59,
381 .demod_chip = LGDT3303,
382 .serial_mpeg = 0x40, /* TPSERIAL for 3303 in TOP_CONTROL */
383 .set_ts_params = lgdt330x_set_ts_param,
384};
385
386static int nxt200x_set_ts_param(struct dvb_frontend* fe, int is_punctured)
387{
388 struct cx8802_dev *dev= fe->dvb->priv;
389 dev->ts_gen_cntrl = is_punctured ? 0x04 : 0x00;
390 return 0;
391}
392
393static const struct nxt200x_config ati_hdtvwonder = {
394 .demod_address = 0x0a,
395 .set_ts_params = nxt200x_set_ts_param,
396};
397
398static int cx24123_set_ts_param(struct dvb_frontend* fe,
399 int is_punctured)
400{
401 struct cx8802_dev *dev= fe->dvb->priv;
402 dev->ts_gen_cntrl = 0x02;
403 return 0;
404}
405
406static int kworld_dvbs_100_set_voltage(struct dvb_frontend* fe,
407 fe_sec_voltage_t voltage)
408{
409 struct cx8802_dev *dev= fe->dvb->priv;
410 struct cx88_core *core = dev->core;
411
412 if (voltage == SEC_VOLTAGE_OFF)
413 cx_write(MO_GP0_IO, 0x000006fb);
414 else
415 cx_write(MO_GP0_IO, 0x000006f9);
416
417 if (core->prev_set_voltage)
418 return core->prev_set_voltage(fe, voltage);
419 return 0;
420}
421
422static int geniatech_dvbs_set_voltage(struct dvb_frontend *fe,
423 fe_sec_voltage_t voltage)
424{
425 struct cx8802_dev *dev= fe->dvb->priv;
426 struct cx88_core *core = dev->core;
427
428 if (voltage == SEC_VOLTAGE_OFF) {
429 dprintk(1,"LNB Voltage OFF\n");
430 cx_write(MO_GP0_IO, 0x0000efff);
431 }
432
433 if (core->prev_set_voltage)
434 return core->prev_set_voltage(fe, voltage);
435 return 0;
436}
437
438static int tevii_dvbs_set_voltage(struct dvb_frontend *fe,
439 fe_sec_voltage_t voltage)
440{
441 struct cx8802_dev *dev= fe->dvb->priv;
442 struct cx88_core *core = dev->core;
443
444 cx_set(MO_GP0_IO, 0x6040);
445 switch (voltage) {
446 case SEC_VOLTAGE_13:
447 cx_clear(MO_GP0_IO, 0x20);
448 break;
449 case SEC_VOLTAGE_18:
450 cx_set(MO_GP0_IO, 0x20);
451 break;
452 case SEC_VOLTAGE_OFF:
453 cx_clear(MO_GP0_IO, 0x20);
454 break;
455 }
456
457 if (core->prev_set_voltage)
458 return core->prev_set_voltage(fe, voltage);
459 return 0;
460}
461
462static int vp1027_set_voltage(struct dvb_frontend *fe,
463 fe_sec_voltage_t voltage)
464{
465 struct cx8802_dev *dev = fe->dvb->priv;
466 struct cx88_core *core = dev->core;
467
468 switch (voltage) {
469 case SEC_VOLTAGE_13:
470 dprintk(1, "LNB SEC Voltage=13\n");
471 cx_write(MO_GP0_IO, 0x00001220);
472 break;
473 case SEC_VOLTAGE_18:
474 dprintk(1, "LNB SEC Voltage=18\n");
475 cx_write(MO_GP0_IO, 0x00001222);
476 break;
477 case SEC_VOLTAGE_OFF:
478 dprintk(1, "LNB Voltage OFF\n");
479 cx_write(MO_GP0_IO, 0x00001230);
480 break;
481 }
482
483 if (core->prev_set_voltage)
484 return core->prev_set_voltage(fe, voltage);
485 return 0;
486}
487
488static const struct cx24123_config geniatech_dvbs_config = {
489 .demod_address = 0x55,
490 .set_ts_params = cx24123_set_ts_param,
491};
492
493static const struct cx24123_config hauppauge_novas_config = {
494 .demod_address = 0x55,
495 .set_ts_params = cx24123_set_ts_param,
496};
497
498static const struct cx24123_config kworld_dvbs_100_config = {
499 .demod_address = 0x15,
500 .set_ts_params = cx24123_set_ts_param,
501 .lnb_polarity = 1,
502};
503
504static const struct s5h1409_config pinnacle_pctv_hd_800i_config = {
505 .demod_address = 0x32 >> 1,
506 .output_mode = S5H1409_PARALLEL_OUTPUT,
507 .gpio = S5H1409_GPIO_ON,
508 .qam_if = 44000,
509 .inversion = S5H1409_INVERSION_OFF,
510 .status_mode = S5H1409_DEMODLOCKING,
511 .mpeg_timing = S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK,
512};
513
514static const struct s5h1409_config dvico_hdtv5_pci_nano_config = {
515 .demod_address = 0x32 >> 1,
516 .output_mode = S5H1409_SERIAL_OUTPUT,
517 .gpio = S5H1409_GPIO_OFF,
518 .inversion = S5H1409_INVERSION_OFF,
519 .status_mode = S5H1409_DEMODLOCKING,
520 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
521};
522
523static const struct s5h1409_config kworld_atsc_120_config = {
524 .demod_address = 0x32 >> 1,
525 .output_mode = S5H1409_SERIAL_OUTPUT,
526 .gpio = S5H1409_GPIO_OFF,
527 .inversion = S5H1409_INVERSION_OFF,
528 .status_mode = S5H1409_DEMODLOCKING,
529 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
530};
531
532static const struct xc5000_config pinnacle_pctv_hd_800i_tuner_config = {
533 .i2c_address = 0x64,
534 .if_khz = 5380,
535};
536
537static const struct zl10353_config cx88_pinnacle_hybrid_pctv = {
538 .demod_address = (0x1e >> 1),
539 .no_tuner = 1,
540 .if2 = 45600,
541};
542
543static const struct zl10353_config cx88_geniatech_x8000_mt = {
544 .demod_address = (0x1e >> 1),
545 .no_tuner = 1,
546 .disable_i2c_gate_ctrl = 1,
547};
548
549static const struct s5h1411_config dvico_fusionhdtv7_config = {
550 .output_mode = S5H1411_SERIAL_OUTPUT,
551 .gpio = S5H1411_GPIO_ON,
552 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
553 .qam_if = S5H1411_IF_44000,
554 .vsb_if = S5H1411_IF_44000,
555 .inversion = S5H1411_INVERSION_OFF,
556 .status_mode = S5H1411_DEMODLOCKING
557};
558
559static const struct xc5000_config dvico_fusionhdtv7_tuner_config = {
560 .i2c_address = 0xc2 >> 1,
561 .if_khz = 5380,
562};
563
564static int attach_xc3028(u8 addr, struct cx8802_dev *dev)
565{
566 struct dvb_frontend *fe;
567 struct videobuf_dvb_frontend *fe0 = NULL;
568 struct xc2028_ctrl ctl;
569 struct xc2028_config cfg = {
570 .i2c_adap = &dev->core->i2c_adap,
571 .i2c_addr = addr,
572 .ctrl = &ctl,
573 };
574
575 /* Get the first frontend */
576 fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
577 if (!fe0)
578 return -EINVAL;
579
580 if (!fe0->dvb.frontend) {
581 printk(KERN_ERR "%s/2: dvb frontend not attached. "
582 "Can't attach xc3028\n",
583 dev->core->name);
584 return -EINVAL;
585 }
586
587 /*
588 * Some xc3028 devices may be hidden by an I2C gate. This is known
589 * to happen with some s5h1409-based devices.
590 * Now that I2C gate is open, sets up xc3028 configuration
591 */
592 cx88_setup_xc3028(dev->core, &ctl);
593
594 fe = dvb_attach(xc2028_attach, fe0->dvb.frontend, &cfg);
595 if (!fe) {
596 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
597 dev->core->name);
598 dvb_frontend_detach(fe0->dvb.frontend);
599 dvb_unregister_frontend(fe0->dvb.frontend);
600 fe0->dvb.frontend = NULL;
601 return -EINVAL;
602 }
603
604 printk(KERN_INFO "%s/2: xc3028 attached\n",
605 dev->core->name);
606
607 return 0;
608}
609
610static int attach_xc4000(struct cx8802_dev *dev, struct xc4000_config *cfg)
611{
612 struct dvb_frontend *fe;
613 struct videobuf_dvb_frontend *fe0 = NULL;
614
615 /* Get the first frontend */
616 fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
617 if (!fe0)
618 return -EINVAL;
619
620 if (!fe0->dvb.frontend) {
621 printk(KERN_ERR "%s/2: dvb frontend not attached. "
622 "Can't attach xc4000\n",
623 dev->core->name);
624 return -EINVAL;
625 }
626
627 fe = dvb_attach(xc4000_attach, fe0->dvb.frontend, &dev->core->i2c_adap,
628 cfg);
629 if (!fe) {
630 printk(KERN_ERR "%s/2: xc4000 attach failed\n",
631 dev->core->name);
632 dvb_frontend_detach(fe0->dvb.frontend);
633 dvb_unregister_frontend(fe0->dvb.frontend);
634 fe0->dvb.frontend = NULL;
635 return -EINVAL;
636 }
637
638 printk(KERN_INFO "%s/2: xc4000 attached\n", dev->core->name);
639
640 return 0;
641}
642
643static int cx24116_set_ts_param(struct dvb_frontend *fe,
644 int is_punctured)
645{
646 struct cx8802_dev *dev = fe->dvb->priv;
647 dev->ts_gen_cntrl = 0x2;
648
649 return 0;
650}
651
652static int stv0900_set_ts_param(struct dvb_frontend *fe,
653 int is_punctured)
654{
655 struct cx8802_dev *dev = fe->dvb->priv;
656 dev->ts_gen_cntrl = 0;
657
658 return 0;
659}
660
661static int cx24116_reset_device(struct dvb_frontend *fe)
662{
663 struct cx8802_dev *dev = fe->dvb->priv;
664 struct cx88_core *core = dev->core;
665
666 /* Reset the part */
667 /* Put the cx24116 into reset */
668 cx_write(MO_SRST_IO, 0);
669 msleep(10);
670 /* Take the cx24116 out of reset */
671 cx_write(MO_SRST_IO, 1);
672 msleep(10);
673
674 return 0;
675}
676
677static const struct cx24116_config hauppauge_hvr4000_config = {
678 .demod_address = 0x05,
679 .set_ts_params = cx24116_set_ts_param,
680 .reset_device = cx24116_reset_device,
681};
682
683static const struct cx24116_config tevii_s460_config = {
684 .demod_address = 0x55,
685 .set_ts_params = cx24116_set_ts_param,
686 .reset_device = cx24116_reset_device,
687};
688
689static int ds3000_set_ts_param(struct dvb_frontend *fe,
690 int is_punctured)
691{
692 struct cx8802_dev *dev = fe->dvb->priv;
693 dev->ts_gen_cntrl = 4;
694
695 return 0;
696}
697
698static struct ds3000_config tevii_ds3000_config = {
699 .demod_address = 0x68,
700 .set_ts_params = ds3000_set_ts_param,
701};
702
703static const struct stv0900_config prof_7301_stv0900_config = {
704 .demod_address = 0x6a,
705/* demod_mode = 0,*/
706 .xtal = 27000000,
707 .clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
708 .diseqc_mode = 2,/* 2/3 PWM */
709 .tun1_maddress = 0,/* 0x60 */
710 .tun1_adc = 0,/* 2 Vpp */
711 .path1_mode = 3,
712 .set_ts_params = stv0900_set_ts_param,
713};
714
715static const struct stb6100_config prof_7301_stb6100_config = {
716 .tuner_address = 0x60,
717 .refclock = 27000000,
718};
719
720static const struct stv0299_config tevii_tuner_sharp_config = {
721 .demod_address = 0x68,
722 .inittab = sharp_z0194a_inittab,
723 .mclk = 88000000UL,
724 .invert = 1,
725 .skip_reinit = 0,
726 .lock_output = 1,
727 .volt13_op0_op1 = STV0299_VOLT13_OP1,
728 .min_delay_ms = 100,
729 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
730 .set_ts_params = cx24116_set_ts_param,
731};
732
733static const struct stv0288_config tevii_tuner_earda_config = {
734 .demod_address = 0x68,
735 .min_delay_ms = 100,
736 .set_ts_params = cx24116_set_ts_param,
737};
738
739static int cx8802_alloc_frontends(struct cx8802_dev *dev)
740{
741 struct cx88_core *core = dev->core;
742 struct videobuf_dvb_frontend *fe = NULL;
743 int i;
744
745 mutex_init(&dev->frontends.lock);
746 INIT_LIST_HEAD(&dev->frontends.felist);
747
748 if (!core->board.num_frontends)
749 return -ENODEV;
750
751 printk(KERN_INFO "%s() allocating %d frontend(s)\n", __func__,
752 core->board.num_frontends);
753 for (i = 1; i <= core->board.num_frontends; i++) {
754 fe = videobuf_dvb_alloc_frontend(&dev->frontends, i);
755 if (!fe) {
756 printk(KERN_ERR "%s() failed to alloc\n", __func__);
757 videobuf_dvb_dealloc_frontends(&dev->frontends);
758 return -ENOMEM;
759 }
760 }
761 return 0;
762}
763
764
765
766static const u8 samsung_smt_7020_inittab[] = {
767 0x01, 0x15,
768 0x02, 0x00,
769 0x03, 0x00,
770 0x04, 0x7D,
771 0x05, 0x0F,
772 0x06, 0x02,
773 0x07, 0x00,
774 0x08, 0x60,
775
776 0x0A, 0xC2,
777 0x0B, 0x00,
778 0x0C, 0x01,
779 0x0D, 0x81,
780 0x0E, 0x44,
781 0x0F, 0x09,
782 0x10, 0x3C,
783 0x11, 0x84,
784 0x12, 0xDA,
785 0x13, 0x99,
786 0x14, 0x8D,
787 0x15, 0xCE,
788 0x16, 0xE8,
789 0x17, 0x43,
790 0x18, 0x1C,
791 0x19, 0x1B,
792 0x1A, 0x1D,
793
794 0x1C, 0x12,
795 0x1D, 0x00,
796 0x1E, 0x00,
797 0x1F, 0x00,
798 0x20, 0x00,
799 0x21, 0x00,
800 0x22, 0x00,
801 0x23, 0x00,
802
803 0x28, 0x02,
804 0x29, 0x28,
805 0x2A, 0x14,
806 0x2B, 0x0F,
807 0x2C, 0x09,
808 0x2D, 0x05,
809
810 0x31, 0x1F,
811 0x32, 0x19,
812 0x33, 0xFC,
813 0x34, 0x13,
814 0xff, 0xff,
815};
816
817
818static int samsung_smt_7020_tuner_set_params(struct dvb_frontend *fe)
819{
820 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
821 struct cx8802_dev *dev = fe->dvb->priv;
822 u8 buf[4];
823 u32 div;
824 struct i2c_msg msg = {
825 .addr = 0x61,
826 .flags = 0,
827 .buf = buf,
828 .len = sizeof(buf) };
829
830 div = c->frequency / 125;
831
832 buf[0] = (div >> 8) & 0x7f;
833 buf[1] = div & 0xff;
834 buf[2] = 0x84; /* 0xC4 */
835 buf[3] = 0x00;
836
837 if (c->frequency < 1500000)
838 buf[3] |= 0x10;
839
840 if (fe->ops.i2c_gate_ctrl)
841 fe->ops.i2c_gate_ctrl(fe, 1);
842
843 if (i2c_transfer(&dev->core->i2c_adap, &msg, 1) != 1)
844 return -EIO;
845
846 return 0;
847}
848
849static int samsung_smt_7020_set_tone(struct dvb_frontend *fe,
850 fe_sec_tone_mode_t tone)
851{
852 struct cx8802_dev *dev = fe->dvb->priv;
853 struct cx88_core *core = dev->core;
854
855 cx_set(MO_GP0_IO, 0x0800);
856
857 switch (tone) {
858 case SEC_TONE_ON:
859 cx_set(MO_GP0_IO, 0x08);
860 break;
861 case SEC_TONE_OFF:
862 cx_clear(MO_GP0_IO, 0x08);
863 break;
864 default:
865 return -EINVAL;
866 }
867
868 return 0;
869}
870
871static int samsung_smt_7020_set_voltage(struct dvb_frontend *fe,
872 fe_sec_voltage_t voltage)
873{
874 struct cx8802_dev *dev = fe->dvb->priv;
875 struct cx88_core *core = dev->core;
876
877 u8 data;
878 struct i2c_msg msg = {
879 .addr = 8,
880 .flags = 0,
881 .buf = &data,
882 .len = sizeof(data) };
883
884 cx_set(MO_GP0_IO, 0x8000);
885
886 switch (voltage) {
887 case SEC_VOLTAGE_OFF:
888 break;
889 case SEC_VOLTAGE_13:
890 data = ISL6421_EN1 | ISL6421_LLC1;
891 cx_clear(MO_GP0_IO, 0x80);
892 break;
893 case SEC_VOLTAGE_18:
894 data = ISL6421_EN1 | ISL6421_LLC1 | ISL6421_VSEL1;
895 cx_clear(MO_GP0_IO, 0x80);
896 break;
897 default:
898 return -EINVAL;
899 };
900
901 return (i2c_transfer(&dev->core->i2c_adap, &msg, 1) == 1) ? 0 : -EIO;
902}
903
904static int samsung_smt_7020_stv0299_set_symbol_rate(struct dvb_frontend *fe,
905 u32 srate, u32 ratio)
906{
907 u8 aclk = 0;
908 u8 bclk = 0;
909
910 if (srate < 1500000) {
911 aclk = 0xb7;
912 bclk = 0x47;
913 } else if (srate < 3000000) {
914 aclk = 0xb7;
915 bclk = 0x4b;
916 } else if (srate < 7000000) {
917 aclk = 0xb7;
918 bclk = 0x4f;
919 } else if (srate < 14000000) {
920 aclk = 0xb7;
921 bclk = 0x53;
922 } else if (srate < 30000000) {
923 aclk = 0xb6;
924 bclk = 0x53;
925 } else if (srate < 45000000) {
926 aclk = 0xb4;
927 bclk = 0x51;
928 }
929
930 stv0299_writereg(fe, 0x13, aclk);
931 stv0299_writereg(fe, 0x14, bclk);
932 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
933 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
934 stv0299_writereg(fe, 0x21, ratio & 0xf0);
935
936 return 0;
937}
938
939
940static const struct stv0299_config samsung_stv0299_config = {
941 .demod_address = 0x68,
942 .inittab = samsung_smt_7020_inittab,
943 .mclk = 88000000UL,
944 .invert = 0,
945 .skip_reinit = 0,
946 .lock_output = STV0299_LOCKOUTPUT_LK,
947 .volt13_op0_op1 = STV0299_VOLT13_OP1,
948 .min_delay_ms = 100,
949 .set_symbol_rate = samsung_smt_7020_stv0299_set_symbol_rate,
950};
951
952static int dvb_register(struct cx8802_dev *dev)
953{
954 struct cx88_core *core = dev->core;
955 struct videobuf_dvb_frontend *fe0, *fe1 = NULL;
956 int mfe_shared = 0; /* bus not shared by default */
957 int res = -EINVAL;
958
959 if (0 != core->i2c_rc) {
960 printk(KERN_ERR "%s/2: no i2c-bus available, cannot attach dvb drivers\n", core->name);
961 goto frontend_detach;
962 }
963
964 /* Get the first frontend */
965 fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
966 if (!fe0)
967 goto frontend_detach;
968
969 /* multi-frontend gate control is undefined or defaults to fe0 */
970 dev->frontends.gate = 0;
971
972 /* Sets the gate control callback to be used by i2c command calls */
973 core->gate_ctrl = cx88_dvb_gate_ctrl;
974
975 /* init frontend(s) */
976 switch (core->boardnr) {
977 case CX88_BOARD_HAUPPAUGE_DVB_T1:
978 fe0->dvb.frontend = dvb_attach(cx22702_attach,
979 &connexant_refboard_config,
980 &core->i2c_adap);
981 if (fe0->dvb.frontend != NULL) {
982 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
983 0x61, &core->i2c_adap,
984 DVB_PLL_THOMSON_DTT759X))
985 goto frontend_detach;
986 }
987 break;
988 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
989 case CX88_BOARD_CONEXANT_DVB_T1:
990 case CX88_BOARD_KWORLD_DVB_T_CX22702:
991 case CX88_BOARD_WINFAST_DTV1000:
992 fe0->dvb.frontend = dvb_attach(cx22702_attach,
993 &connexant_refboard_config,
994 &core->i2c_adap);
995 if (fe0->dvb.frontend != NULL) {
996 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
997 0x60, &core->i2c_adap,
998 DVB_PLL_THOMSON_DTT7579))
999 goto frontend_detach;
1000 }
1001 break;
1002 case CX88_BOARD_WINFAST_DTV2000H:
1003 case CX88_BOARD_HAUPPAUGE_HVR1100:
1004 case CX88_BOARD_HAUPPAUGE_HVR1100LP:
1005 case CX88_BOARD_HAUPPAUGE_HVR1300:
1006 fe0->dvb.frontend = dvb_attach(cx22702_attach,
1007 &hauppauge_hvr_config,
1008 &core->i2c_adap);
1009 if (fe0->dvb.frontend != NULL) {
1010 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1011 &core->i2c_adap, 0x61,
1012 TUNER_PHILIPS_FMD1216ME_MK3))
1013 goto frontend_detach;
1014 }
1015 break;
1016 case CX88_BOARD_WINFAST_DTV2000H_J:
1017 fe0->dvb.frontend = dvb_attach(cx22702_attach,
1018 &hauppauge_hvr_config,
1019 &core->i2c_adap);
1020 if (fe0->dvb.frontend != NULL) {
1021 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1022 &core->i2c_adap, 0x61,
1023 TUNER_PHILIPS_FMD1216MEX_MK3))
1024 goto frontend_detach;
1025 }
1026 break;
1027 case CX88_BOARD_HAUPPAUGE_HVR3000:
1028 /* MFE frontend 1 */
1029 mfe_shared = 1;
1030 dev->frontends.gate = 2;
1031 /* DVB-S init */
1032 fe0->dvb.frontend = dvb_attach(cx24123_attach,
1033 &hauppauge_novas_config,
1034 &dev->core->i2c_adap);
1035 if (fe0->dvb.frontend) {
1036 if (!dvb_attach(isl6421_attach,
1037 fe0->dvb.frontend,
1038 &dev->core->i2c_adap,
1039 0x08, ISL6421_DCL, 0x00))
1040 goto frontend_detach;
1041 }
1042 /* MFE frontend 2 */
1043 fe1 = videobuf_dvb_get_frontend(&dev->frontends, 2);
1044 if (!fe1)
1045 goto frontend_detach;
1046 /* DVB-T init */
1047 fe1->dvb.frontend = dvb_attach(cx22702_attach,
1048 &hauppauge_hvr_config,
1049 &dev->core->i2c_adap);
1050 if (fe1->dvb.frontend) {
1051 fe1->dvb.frontend->id = 1;
1052 if (!dvb_attach(simple_tuner_attach,
1053 fe1->dvb.frontend,
1054 &dev->core->i2c_adap,
1055 0x61, TUNER_PHILIPS_FMD1216ME_MK3))
1056 goto frontend_detach;
1057 }
1058 break;
1059 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS:
1060 fe0->dvb.frontend = dvb_attach(mt352_attach,
1061 &dvico_fusionhdtv,
1062 &core->i2c_adap);
1063 if (fe0->dvb.frontend != NULL) {
1064 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
1065 0x60, NULL, DVB_PLL_THOMSON_DTT7579))
1066 goto frontend_detach;
1067 break;
1068 }
1069 /* ZL10353 replaces MT352 on later cards */
1070 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1071 &dvico_fusionhdtv_plus_v1_1,
1072 &core->i2c_adap);
1073 if (fe0->dvb.frontend != NULL) {
1074 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
1075 0x60, NULL, DVB_PLL_THOMSON_DTT7579))
1076 goto frontend_detach;
1077 }
1078 break;
1079 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL:
1080 /* The tin box says DEE1601, but it seems to be DTT7579
1081 * compatible, with a slightly different MT352 AGC gain. */
1082 fe0->dvb.frontend = dvb_attach(mt352_attach,
1083 &dvico_fusionhdtv_dual,
1084 &core->i2c_adap);
1085 if (fe0->dvb.frontend != NULL) {
1086 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
1087 0x61, NULL, DVB_PLL_THOMSON_DTT7579))
1088 goto frontend_detach;
1089 break;
1090 }
1091 /* ZL10353 replaces MT352 on later cards */
1092 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1093 &dvico_fusionhdtv_plus_v1_1,
1094 &core->i2c_adap);
1095 if (fe0->dvb.frontend != NULL) {
1096 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
1097 0x61, NULL, DVB_PLL_THOMSON_DTT7579))
1098 goto frontend_detach;
1099 }
1100 break;
1101 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1:
1102 fe0->dvb.frontend = dvb_attach(mt352_attach,
1103 &dvico_fusionhdtv,
1104 &core->i2c_adap);
1105 if (fe0->dvb.frontend != NULL) {
1106 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
1107 0x61, NULL, DVB_PLL_LG_Z201))
1108 goto frontend_detach;
1109 }
1110 break;
1111 case CX88_BOARD_KWORLD_DVB_T:
1112 case CX88_BOARD_DNTV_LIVE_DVB_T:
1113 case CX88_BOARD_ADSTECH_DVB_T_PCI:
1114 fe0->dvb.frontend = dvb_attach(mt352_attach,
1115 &dntv_live_dvbt_config,
1116 &core->i2c_adap);
1117 if (fe0->dvb.frontend != NULL) {
1118 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend,
1119 0x61, NULL, DVB_PLL_UNKNOWN_1))
1120 goto frontend_detach;
1121 }
1122 break;
1123 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
1124#if defined(CONFIG_VIDEO_CX88_VP3054) || (defined(CONFIG_VIDEO_CX88_VP3054_MODULE) && defined(MODULE))
1125 /* MT352 is on a secondary I2C bus made from some GPIO lines */
1126 fe0->dvb.frontend = dvb_attach(mt352_attach, &dntv_live_dvbt_pro_config,
1127 &dev->vp3054->adap);
1128 if (fe0->dvb.frontend != NULL) {
1129 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1130 &core->i2c_adap, 0x61,
1131 TUNER_PHILIPS_FMD1216ME_MK3))
1132 goto frontend_detach;
1133 }
1134#else
1135 printk(KERN_ERR "%s/2: built without vp3054 support\n",
1136 core->name);
1137#endif
1138 break;
1139 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID:
1140 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1141 &dvico_fusionhdtv_hybrid,
1142 &core->i2c_adap);
1143 if (fe0->dvb.frontend != NULL) {
1144 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1145 &core->i2c_adap, 0x61,
1146 TUNER_THOMSON_FE6600))
1147 goto frontend_detach;
1148 }
1149 break;
1150 case CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO:
1151 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1152 &dvico_fusionhdtv_xc3028,
1153 &core->i2c_adap);
1154 if (fe0->dvb.frontend == NULL)
1155 fe0->dvb.frontend = dvb_attach(mt352_attach,
1156 &dvico_fusionhdtv_mt352_xc3028,
1157 &core->i2c_adap);
1158 /*
1159 * On this board, the demod provides the I2C bus pullup.
1160 * We must not permit gate_ctrl to be performed, or
1161 * the xc3028 cannot communicate on the bus.
1162 */
1163 if (fe0->dvb.frontend)
1164 fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1165 if (attach_xc3028(0x61, dev) < 0)
1166 goto frontend_detach;
1167 break;
1168 case CX88_BOARD_PCHDTV_HD3000:
1169 fe0->dvb.frontend = dvb_attach(or51132_attach, &pchdtv_hd3000,
1170 &core->i2c_adap);
1171 if (fe0->dvb.frontend != NULL) {
1172 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1173 &core->i2c_adap, 0x61,
1174 TUNER_THOMSON_DTT761X))
1175 goto frontend_detach;
1176 }
1177 break;
1178 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
1179 dev->ts_gen_cntrl = 0x08;
1180
1181 /* Do a hardware reset of chip before using it. */
1182 cx_clear(MO_GP0_IO, 1);
1183 mdelay(100);
1184 cx_set(MO_GP0_IO, 1);
1185 mdelay(200);
1186
1187 /* Select RF connector callback */
1188 fusionhdtv_3_gold.pll_rf_set = lgdt330x_pll_rf_set;
1189 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1190 &fusionhdtv_3_gold,
1191 &core->i2c_adap);
1192 if (fe0->dvb.frontend != NULL) {
1193 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1194 &core->i2c_adap, 0x61,
1195 TUNER_MICROTUNE_4042FI5))
1196 goto frontend_detach;
1197 }
1198 break;
1199 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
1200 dev->ts_gen_cntrl = 0x08;
1201
1202 /* Do a hardware reset of chip before using it. */
1203 cx_clear(MO_GP0_IO, 1);
1204 mdelay(100);
1205 cx_set(MO_GP0_IO, 9);
1206 mdelay(200);
1207 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1208 &fusionhdtv_3_gold,
1209 &core->i2c_adap);
1210 if (fe0->dvb.frontend != NULL) {
1211 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1212 &core->i2c_adap, 0x61,
1213 TUNER_THOMSON_DTT761X))
1214 goto frontend_detach;
1215 }
1216 break;
1217 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1218 dev->ts_gen_cntrl = 0x08;
1219
1220 /* Do a hardware reset of chip before using it. */
1221 cx_clear(MO_GP0_IO, 1);
1222 mdelay(100);
1223 cx_set(MO_GP0_IO, 1);
1224 mdelay(200);
1225 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1226 &fusionhdtv_5_gold,
1227 &core->i2c_adap);
1228 if (fe0->dvb.frontend != NULL) {
1229 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1230 &core->i2c_adap, 0x61,
1231 TUNER_LG_TDVS_H06XF))
1232 goto frontend_detach;
1233 if (!dvb_attach(tda9887_attach, fe0->dvb.frontend,
1234 &core->i2c_adap, 0x43))
1235 goto frontend_detach;
1236 }
1237 break;
1238 case CX88_BOARD_PCHDTV_HD5500:
1239 dev->ts_gen_cntrl = 0x08;
1240
1241 /* Do a hardware reset of chip before using it. */
1242 cx_clear(MO_GP0_IO, 1);
1243 mdelay(100);
1244 cx_set(MO_GP0_IO, 1);
1245 mdelay(200);
1246 fe0->dvb.frontend = dvb_attach(lgdt330x_attach,
1247 &pchdtv_hd5500,
1248 &core->i2c_adap);
1249 if (fe0->dvb.frontend != NULL) {
1250 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1251 &core->i2c_adap, 0x61,
1252 TUNER_LG_TDVS_H06XF))
1253 goto frontend_detach;
1254 if (!dvb_attach(tda9887_attach, fe0->dvb.frontend,
1255 &core->i2c_adap, 0x43))
1256 goto frontend_detach;
1257 }
1258 break;
1259 case CX88_BOARD_ATI_HDTVWONDER:
1260 fe0->dvb.frontend = dvb_attach(nxt200x_attach,
1261 &ati_hdtvwonder,
1262 &core->i2c_adap);
1263 if (fe0->dvb.frontend != NULL) {
1264 if (!dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1265 &core->i2c_adap, 0x61,
1266 TUNER_PHILIPS_TUV1236D))
1267 goto frontend_detach;
1268 }
1269 break;
1270 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
1271 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
1272 fe0->dvb.frontend = dvb_attach(cx24123_attach,
1273 &hauppauge_novas_config,
1274 &core->i2c_adap);
1275 if (fe0->dvb.frontend) {
1276 if (!dvb_attach(isl6421_attach, fe0->dvb.frontend,
1277 &core->i2c_adap, 0x08, ISL6421_DCL, 0x00))
1278 goto frontend_detach;
1279 }
1280 break;
1281 case CX88_BOARD_KWORLD_DVBS_100:
1282 fe0->dvb.frontend = dvb_attach(cx24123_attach,
1283 &kworld_dvbs_100_config,
1284 &core->i2c_adap);
1285 if (fe0->dvb.frontend) {
1286 core->prev_set_voltage = fe0->dvb.frontend->ops.set_voltage;
1287 fe0->dvb.frontend->ops.set_voltage = kworld_dvbs_100_set_voltage;
1288 }
1289 break;
1290 case CX88_BOARD_GENIATECH_DVBS:
1291 fe0->dvb.frontend = dvb_attach(cx24123_attach,
1292 &geniatech_dvbs_config,
1293 &core->i2c_adap);
1294 if (fe0->dvb.frontend) {
1295 core->prev_set_voltage = fe0->dvb.frontend->ops.set_voltage;
1296 fe0->dvb.frontend->ops.set_voltage = geniatech_dvbs_set_voltage;
1297 }
1298 break;
1299 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
1300 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1301 &pinnacle_pctv_hd_800i_config,
1302 &core->i2c_adap);
1303 if (fe0->dvb.frontend != NULL) {
1304 if (!dvb_attach(xc5000_attach, fe0->dvb.frontend,
1305 &core->i2c_adap,
1306 &pinnacle_pctv_hd_800i_tuner_config))
1307 goto frontend_detach;
1308 }
1309 break;
1310 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1311 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1312 &dvico_hdtv5_pci_nano_config,
1313 &core->i2c_adap);
1314 if (fe0->dvb.frontend != NULL) {
1315 struct dvb_frontend *fe;
1316 struct xc2028_config cfg = {
1317 .i2c_adap = &core->i2c_adap,
1318 .i2c_addr = 0x61,
1319 };
1320 static struct xc2028_ctrl ctl = {
1321 .fname = XC2028_DEFAULT_FIRMWARE,
1322 .max_len = 64,
1323 .scode_table = XC3028_FE_OREN538,
1324 };
1325
1326 fe = dvb_attach(xc2028_attach,
1327 fe0->dvb.frontend, &cfg);
1328 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
1329 fe->ops.tuner_ops.set_config(fe, &ctl);
1330 }
1331 break;
1332 case CX88_BOARD_PINNACLE_HYBRID_PCTV:
1333 case CX88_BOARD_WINFAST_DTV1800H:
1334 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1335 &cx88_pinnacle_hybrid_pctv,
1336 &core->i2c_adap);
1337 if (fe0->dvb.frontend) {
1338 fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1339 if (attach_xc3028(0x61, dev) < 0)
1340 goto frontend_detach;
1341 }
1342 break;
1343 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
1344 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
1345 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1346 &cx88_pinnacle_hybrid_pctv,
1347 &core->i2c_adap);
1348 if (fe0->dvb.frontend) {
1349 struct xc4000_config cfg = {
1350 .i2c_address = 0x61,
1351 .default_pm = 0,
1352 .dvb_amplitude = 134,
1353 .set_smoothedcvbs = 1,
1354 .if_khz = 4560
1355 };
1356 fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1357 if (attach_xc4000(dev, &cfg) < 0)
1358 goto frontend_detach;
1359 }
1360 break;
1361 case CX88_BOARD_GENIATECH_X8000_MT:
1362 dev->ts_gen_cntrl = 0x00;
1363
1364 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1365 &cx88_geniatech_x8000_mt,
1366 &core->i2c_adap);
1367 if (attach_xc3028(0x61, dev) < 0)
1368 goto frontend_detach;
1369 break;
1370 case CX88_BOARD_KWORLD_ATSC_120:
1371 fe0->dvb.frontend = dvb_attach(s5h1409_attach,
1372 &kworld_atsc_120_config,
1373 &core->i2c_adap);
1374 if (attach_xc3028(0x61, dev) < 0)
1375 goto frontend_detach;
1376 break;
1377 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
1378 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1379 &dvico_fusionhdtv7_config,
1380 &core->i2c_adap);
1381 if (fe0->dvb.frontend != NULL) {
1382 if (!dvb_attach(xc5000_attach, fe0->dvb.frontend,
1383 &core->i2c_adap,
1384 &dvico_fusionhdtv7_tuner_config))
1385 goto frontend_detach;
1386 }
1387 break;
1388 case CX88_BOARD_HAUPPAUGE_HVR4000:
1389 /* MFE frontend 1 */
1390 mfe_shared = 1;
1391 dev->frontends.gate = 2;
1392 /* DVB-S/S2 Init */
1393 fe0->dvb.frontend = dvb_attach(cx24116_attach,
1394 &hauppauge_hvr4000_config,
1395 &dev->core->i2c_adap);
1396 if (fe0->dvb.frontend) {
1397 if (!dvb_attach(isl6421_attach,
1398 fe0->dvb.frontend,
1399 &dev->core->i2c_adap,
1400 0x08, ISL6421_DCL, 0x00))
1401 goto frontend_detach;
1402 }
1403 /* MFE frontend 2 */
1404 fe1 = videobuf_dvb_get_frontend(&dev->frontends, 2);
1405 if (!fe1)
1406 goto frontend_detach;
1407 /* DVB-T Init */
1408 fe1->dvb.frontend = dvb_attach(cx22702_attach,
1409 &hauppauge_hvr_config,
1410 &dev->core->i2c_adap);
1411 if (fe1->dvb.frontend) {
1412 fe1->dvb.frontend->id = 1;
1413 if (!dvb_attach(simple_tuner_attach,
1414 fe1->dvb.frontend,
1415 &dev->core->i2c_adap,
1416 0x61, TUNER_PHILIPS_FMD1216ME_MK3))
1417 goto frontend_detach;
1418 }
1419 break;
1420 case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
1421 fe0->dvb.frontend = dvb_attach(cx24116_attach,
1422 &hauppauge_hvr4000_config,
1423 &dev->core->i2c_adap);
1424 if (fe0->dvb.frontend) {
1425 if (!dvb_attach(isl6421_attach,
1426 fe0->dvb.frontend,
1427 &dev->core->i2c_adap,
1428 0x08, ISL6421_DCL, 0x00))
1429 goto frontend_detach;
1430 }
1431 break;
1432 case CX88_BOARD_PROF_6200:
1433 case CX88_BOARD_TBS_8910:
1434 case CX88_BOARD_TEVII_S420:
1435 fe0->dvb.frontend = dvb_attach(stv0299_attach,
1436 &tevii_tuner_sharp_config,
1437 &core->i2c_adap);
1438 if (fe0->dvb.frontend != NULL) {
1439 if (!dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x60,
1440 &core->i2c_adap, DVB_PLL_OPERA1))
1441 goto frontend_detach;
1442 core->prev_set_voltage = fe0->dvb.frontend->ops.set_voltage;
1443 fe0->dvb.frontend->ops.set_voltage = tevii_dvbs_set_voltage;
1444
1445 } else {
1446 fe0->dvb.frontend = dvb_attach(stv0288_attach,
1447 &tevii_tuner_earda_config,
1448 &core->i2c_adap);
1449 if (fe0->dvb.frontend != NULL) {
1450 if (!dvb_attach(stb6000_attach, fe0->dvb.frontend, 0x61,
1451 &core->i2c_adap))
1452 goto frontend_detach;
1453 core->prev_set_voltage = fe0->dvb.frontend->ops.set_voltage;
1454 fe0->dvb.frontend->ops.set_voltage = tevii_dvbs_set_voltage;
1455 }
1456 }
1457 break;
1458 case CX88_BOARD_TEVII_S460:
1459 fe0->dvb.frontend = dvb_attach(cx24116_attach,
1460 &tevii_s460_config,
1461 &core->i2c_adap);
1462 if (fe0->dvb.frontend != NULL)
1463 fe0->dvb.frontend->ops.set_voltage = tevii_dvbs_set_voltage;
1464 break;
1465 case CX88_BOARD_TEVII_S464:
1466 fe0->dvb.frontend = dvb_attach(ds3000_attach,
1467 &tevii_ds3000_config,
1468 &core->i2c_adap);
1469 if (fe0->dvb.frontend != NULL)
1470 fe0->dvb.frontend->ops.set_voltage =
1471 tevii_dvbs_set_voltage;
1472 break;
1473 case CX88_BOARD_OMICOM_SS4_PCI:
1474 case CX88_BOARD_TBS_8920:
1475 case CX88_BOARD_PROF_7300:
1476 case CX88_BOARD_SATTRADE_ST4200:
1477 fe0->dvb.frontend = dvb_attach(cx24116_attach,
1478 &hauppauge_hvr4000_config,
1479 &core->i2c_adap);
1480 if (fe0->dvb.frontend != NULL)
1481 fe0->dvb.frontend->ops.set_voltage = tevii_dvbs_set_voltage;
1482 break;
1483 case CX88_BOARD_TERRATEC_CINERGY_HT_PCI_MKII:
1484 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1485 &cx88_terratec_cinergy_ht_pci_mkii_config,
1486 &core->i2c_adap);
1487 if (fe0->dvb.frontend) {
1488 fe0->dvb.frontend->ops.i2c_gate_ctrl = NULL;
1489 if (attach_xc3028(0x61, dev) < 0)
1490 goto frontend_detach;
1491 }
1492 break;
1493 case CX88_BOARD_PROF_7301:{
1494 struct dvb_tuner_ops *tuner_ops = NULL;
1495
1496 fe0->dvb.frontend = dvb_attach(stv0900_attach,
1497 &prof_7301_stv0900_config,
1498 &core->i2c_adap, 0);
1499 if (fe0->dvb.frontend != NULL) {
1500 if (!dvb_attach(stb6100_attach, fe0->dvb.frontend,
1501 &prof_7301_stb6100_config,
1502 &core->i2c_adap))
1503 goto frontend_detach;
1504
1505 tuner_ops = &fe0->dvb.frontend->ops.tuner_ops;
1506 tuner_ops->set_frequency = stb6100_set_freq;
1507 tuner_ops->get_frequency = stb6100_get_freq;
1508 tuner_ops->set_bandwidth = stb6100_set_bandw;
1509 tuner_ops->get_bandwidth = stb6100_get_bandw;
1510
1511 core->prev_set_voltage =
1512 fe0->dvb.frontend->ops.set_voltage;
1513 fe0->dvb.frontend->ops.set_voltage =
1514 tevii_dvbs_set_voltage;
1515 }
1516 break;
1517 }
1518 case CX88_BOARD_SAMSUNG_SMT_7020:
1519 dev->ts_gen_cntrl = 0x08;
1520
1521 cx_set(MO_GP0_IO, 0x0101);
1522
1523 cx_clear(MO_GP0_IO, 0x01);
1524 mdelay(100);
1525 cx_set(MO_GP0_IO, 0x01);
1526 mdelay(200);
1527
1528 fe0->dvb.frontend = dvb_attach(stv0299_attach,
1529 &samsung_stv0299_config,
1530 &dev->core->i2c_adap);
1531 if (fe0->dvb.frontend) {
1532 fe0->dvb.frontend->ops.tuner_ops.set_params =
1533 samsung_smt_7020_tuner_set_params;
1534 fe0->dvb.frontend->tuner_priv =
1535 &dev->core->i2c_adap;
1536 fe0->dvb.frontend->ops.set_voltage =
1537 samsung_smt_7020_set_voltage;
1538 fe0->dvb.frontend->ops.set_tone =
1539 samsung_smt_7020_set_tone;
1540 }
1541
1542 break;
1543 case CX88_BOARD_TWINHAN_VP1027_DVBS:
1544 dev->ts_gen_cntrl = 0x00;
1545 fe0->dvb.frontend = dvb_attach(mb86a16_attach,
1546 &twinhan_vp1027,
1547 &core->i2c_adap);
1548 if (fe0->dvb.frontend) {
1549 core->prev_set_voltage =
1550 fe0->dvb.frontend->ops.set_voltage;
1551 fe0->dvb.frontend->ops.set_voltage =
1552 vp1027_set_voltage;
1553 }
1554 break;
1555
1556 default:
1557 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card isn't supported yet\n",
1558 core->name);
1559 break;
1560 }
1561
1562 if ( (NULL == fe0->dvb.frontend) || (fe1 && NULL == fe1->dvb.frontend) ) {
1563 printk(KERN_ERR
1564 "%s/2: frontend initialization failed\n",
1565 core->name);
1566 goto frontend_detach;
1567 }
1568 /* define general-purpose callback pointer */
1569 fe0->dvb.frontend->callback = cx88_tuner_callback;
1570
1571 /* Ensure all frontends negotiate bus access */
1572 fe0->dvb.frontend->ops.ts_bus_ctrl = cx88_dvb_bus_ctrl;
1573 if (fe1)
1574 fe1->dvb.frontend->ops.ts_bus_ctrl = cx88_dvb_bus_ctrl;
1575
1576 /* Put the analog decoder in standby to keep it quiet */
1577 call_all(core, core, s_power, 0);
1578
1579 /* register everything */
1580 res = videobuf_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1581 &dev->pci->dev, adapter_nr, mfe_shared);
1582 if (res)
1583 goto frontend_detach;
1584 return res;
1585
1586frontend_detach:
1587 core->gate_ctrl = NULL;
1588 videobuf_dvb_dealloc_frontends(&dev->frontends);
1589 return res;
1590}
1591
1592/* ----------------------------------------------------------- */
1593
1594/* CX8802 MPEG -> mini driver - We have been given the hardware */
1595static int cx8802_dvb_advise_acquire(struct cx8802_driver *drv)
1596{
1597 struct cx88_core *core = drv->core;
1598 int err = 0;
1599 dprintk( 1, "%s\n", __func__);
1600
1601 switch (core->boardnr) {
1602 case CX88_BOARD_HAUPPAUGE_HVR1300:
1603 /* We arrive here with either the cx23416 or the cx22702
1604 * on the bus. Take the bus from the cx23416 and enable the
1605 * cx22702 demod
1606 */
1607 /* Toggle reset on cx22702 leaving i2c active */
1608 cx_set(MO_GP0_IO, 0x00000080);
1609 udelay(1000);
1610 cx_clear(MO_GP0_IO, 0x00000080);
1611 udelay(50);
1612 cx_set(MO_GP0_IO, 0x00000080);
1613 udelay(1000);
1614 /* enable the cx22702 pins */
1615 cx_clear(MO_GP0_IO, 0x00000004);
1616 udelay(1000);
1617 break;
1618
1619 case CX88_BOARD_HAUPPAUGE_HVR3000:
1620 case CX88_BOARD_HAUPPAUGE_HVR4000:
1621 /* Toggle reset on cx22702 leaving i2c active */
1622 cx_set(MO_GP0_IO, 0x00000080);
1623 udelay(1000);
1624 cx_clear(MO_GP0_IO, 0x00000080);
1625 udelay(50);
1626 cx_set(MO_GP0_IO, 0x00000080);
1627 udelay(1000);
1628 switch (core->dvbdev->frontends.active_fe_id) {
1629 case 1: /* DVB-S/S2 Enabled */
1630 /* tri-state the cx22702 pins */
1631 cx_set(MO_GP0_IO, 0x00000004);
1632 /* Take the cx24116/cx24123 out of reset */
1633 cx_write(MO_SRST_IO, 1);
1634 core->dvbdev->ts_gen_cntrl = 0x02; /* Parallel IO */
1635 break;
1636 case 2: /* DVB-T Enabled */
1637 /* Put the cx24116/cx24123 into reset */
1638 cx_write(MO_SRST_IO, 0);
1639 /* enable the cx22702 pins */
1640 cx_clear(MO_GP0_IO, 0x00000004);
1641 core->dvbdev->ts_gen_cntrl = 0x0c; /* Serial IO */
1642 break;
1643 }
1644 udelay(1000);
1645 break;
1646
1647 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
1648 /* set RF input to AIR for DVB-T (GPIO 16) */
1649 cx_write(MO_GP2_IO, 0x0101);
1650 break;
1651
1652 default:
1653 err = -ENODEV;
1654 }
1655 return err;
1656}
1657
1658/* CX8802 MPEG -> mini driver - We no longer have the hardware */
1659static int cx8802_dvb_advise_release(struct cx8802_driver *drv)
1660{
1661 struct cx88_core *core = drv->core;
1662 int err = 0;
1663 dprintk( 1, "%s\n", __func__);
1664
1665 switch (core->boardnr) {
1666 case CX88_BOARD_HAUPPAUGE_HVR1300:
1667 /* Do Nothing, leave the cx22702 on the bus. */
1668 break;
1669 case CX88_BOARD_HAUPPAUGE_HVR3000:
1670 case CX88_BOARD_HAUPPAUGE_HVR4000:
1671 break;
1672 default:
1673 err = -ENODEV;
1674 }
1675 return err;
1676}
1677
1678static int cx8802_dvb_probe(struct cx8802_driver *drv)
1679{
1680 struct cx88_core *core = drv->core;
1681 struct cx8802_dev *dev = drv->core->dvbdev;
1682 int err;
1683 struct videobuf_dvb_frontend *fe;
1684 int i;
1685
1686 dprintk( 1, "%s\n", __func__);
1687 dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1688 core->boardnr,
1689 core->name,
1690 core->pci_bus,
1691 core->pci_slot);
1692
1693 err = -ENODEV;
1694 if (!(core->board.mpeg & CX88_MPEG_DVB))
1695 goto fail_core;
1696
1697 /* If vp3054 isn't enabled, a stub will just return 0 */
1698 err = vp3054_i2c_probe(dev);
1699 if (0 != err)
1700 goto fail_core;
1701
1702 /* dvb stuff */
1703 printk(KERN_INFO "%s/2: cx2388x based DVB/ATSC card\n", core->name);
1704 dev->ts_gen_cntrl = 0x0c;
1705
1706 err = cx8802_alloc_frontends(dev);
1707 if (err)
1708 goto fail_core;
1709
1710 err = -ENODEV;
1711 for (i = 1; i <= core->board.num_frontends; i++) {
1712 fe = videobuf_dvb_get_frontend(&core->dvbdev->frontends, i);
1713 if (fe == NULL) {
1714 printk(KERN_ERR "%s() failed to get frontend(%d)\n",
1715 __func__, i);
1716 goto fail_probe;
1717 }
1718 videobuf_queue_sg_init(&fe->dvb.dvbq, &dvb_qops,
1719 &dev->pci->dev, &dev->slock,
1720 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1721 V4L2_FIELD_TOP,
1722 sizeof(struct cx88_buffer),
1723 dev, NULL);
1724 /* init struct videobuf_dvb */
1725 fe->dvb.name = dev->core->name;
1726 }
1727
1728 err = dvb_register(dev);
1729 if (err)
1730 /* frontends/adapter de-allocated in dvb_register */
1731 printk(KERN_ERR "%s/2: dvb_register failed (err = %d)\n",
1732 core->name, err);
1733 return err;
1734fail_probe:
1735 videobuf_dvb_dealloc_frontends(&core->dvbdev->frontends);
1736fail_core:
1737 return err;
1738}
1739
1740static int cx8802_dvb_remove(struct cx8802_driver *drv)
1741{
1742 struct cx88_core *core = drv->core;
1743 struct cx8802_dev *dev = drv->core->dvbdev;
1744
1745 dprintk( 1, "%s\n", __func__);
1746
1747 videobuf_dvb_unregister_bus(&dev->frontends);
1748
1749 vp3054_i2c_remove(dev);
1750
1751 core->gate_ctrl = NULL;
1752
1753 return 0;
1754}
1755
1756static struct cx8802_driver cx8802_dvb_driver = {
1757 .type_id = CX88_MPEG_DVB,
1758 .hw_access = CX8802_DRVCTL_SHARED,
1759 .probe = cx8802_dvb_probe,
1760 .remove = cx8802_dvb_remove,
1761 .advise_acquire = cx8802_dvb_advise_acquire,
1762 .advise_release = cx8802_dvb_advise_release,
1763};
1764
1765static int __init dvb_init(void)
1766{
1767 printk(KERN_INFO "cx88/2: cx2388x dvb driver version %s loaded\n",
1768 CX88_VERSION);
1769 return cx8802_register_driver(&cx8802_dvb_driver);
1770}
1771
1772static void __exit dvb_fini(void)
1773{
1774 cx8802_unregister_driver(&cx8802_dvb_driver);
1775}
1776
1777module_init(dvb_init);
1778module_exit(dvb_fini);
diff --git a/drivers/media/pci/cx88/cx88-i2c.c b/drivers/media/pci/cx88/cx88-i2c.c
new file mode 100644
index 000000000000..de0f1af74e41
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-i2c.c
@@ -0,0 +1,184 @@
1
2/*
3
4 cx88-i2c.c -- all the i2c code is here
5
6 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
7 & Marcus Metzler (mocm@thp.uni-koeln.de)
8 (c) 2002 Yurij Sysoev <yurij@naturesoft.net>
9 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
10
11 (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
12 - Multituner support and i2c address binding
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28*/
29
30#include <linux/module.h>
31#include <linux/init.h>
32
33#include <asm/io.h>
34
35#include "cx88.h"
36#include <media/v4l2-common.h>
37
38static unsigned int i2c_debug;
39module_param(i2c_debug, int, 0644);
40MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
41
42static unsigned int i2c_scan;
43module_param(i2c_scan, int, 0444);
44MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
45
46static unsigned int i2c_udelay = 5;
47module_param(i2c_udelay, int, 0644);
48MODULE_PARM_DESC(i2c_udelay,"i2c delay at insmod time, in usecs "
49 "(should be 5 or higher). Lower value means higher bus speed.");
50
51#define dprintk(level,fmt, arg...) if (i2c_debug >= level) \
52 printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)
53
54/* ----------------------------------------------------------------------- */
55
56static void cx8800_bit_setscl(void *data, int state)
57{
58 struct cx88_core *core = data;
59
60 if (state)
61 core->i2c_state |= 0x02;
62 else
63 core->i2c_state &= ~0x02;
64 cx_write(MO_I2C, core->i2c_state);
65 cx_read(MO_I2C);
66}
67
68static void cx8800_bit_setsda(void *data, int state)
69{
70 struct cx88_core *core = data;
71
72 if (state)
73 core->i2c_state |= 0x01;
74 else
75 core->i2c_state &= ~0x01;
76 cx_write(MO_I2C, core->i2c_state);
77 cx_read(MO_I2C);
78}
79
80static int cx8800_bit_getscl(void *data)
81{
82 struct cx88_core *core = data;
83 u32 state;
84
85 state = cx_read(MO_I2C);
86 return state & 0x02 ? 1 : 0;
87}
88
89static int cx8800_bit_getsda(void *data)
90{
91 struct cx88_core *core = data;
92 u32 state;
93
94 state = cx_read(MO_I2C);
95 return state & 0x01;
96}
97
98/* ----------------------------------------------------------------------- */
99
100static const struct i2c_algo_bit_data cx8800_i2c_algo_template = {
101 .setsda = cx8800_bit_setsda,
102 .setscl = cx8800_bit_setscl,
103 .getsda = cx8800_bit_getsda,
104 .getscl = cx8800_bit_getscl,
105 .udelay = 16,
106 .timeout = 200,
107};
108
109/* ----------------------------------------------------------------------- */
110
111static const char * const i2c_devs[128] = {
112 [ 0x1c >> 1 ] = "lgdt330x",
113 [ 0x86 >> 1 ] = "tda9887/cx22702",
114 [ 0xa0 >> 1 ] = "eeprom",
115 [ 0xc0 >> 1 ] = "tuner (analog)",
116 [ 0xc2 >> 1 ] = "tuner (analog/dvb)",
117 [ 0xc8 >> 1 ] = "xc5000",
118};
119
120static void do_i2c_scan(const char *name, struct i2c_client *c)
121{
122 unsigned char buf;
123 int i,rc;
124
125 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
126 c->addr = i;
127 rc = i2c_master_recv(c,&buf,0);
128 if (rc < 0)
129 continue;
130 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
131 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
132 }
133}
134
135/* init + register i2c adapter */
136int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci)
137{
138 /* Prevents usage of invalid delay values */
139 if (i2c_udelay<5)
140 i2c_udelay=5;
141
142 memcpy(&core->i2c_algo, &cx8800_i2c_algo_template,
143 sizeof(core->i2c_algo));
144
145
146 core->i2c_adap.dev.parent = &pci->dev;
147 strlcpy(core->i2c_adap.name,core->name,sizeof(core->i2c_adap.name));
148 core->i2c_adap.owner = THIS_MODULE;
149 core->i2c_algo.udelay = i2c_udelay;
150 core->i2c_algo.data = core;
151 i2c_set_adapdata(&core->i2c_adap, &core->v4l2_dev);
152 core->i2c_adap.algo_data = &core->i2c_algo;
153 core->i2c_client.adapter = &core->i2c_adap;
154 strlcpy(core->i2c_client.name, "cx88xx internal", I2C_NAME_SIZE);
155
156 cx8800_bit_setscl(core,1);
157 cx8800_bit_setsda(core,1);
158
159 core->i2c_rc = i2c_bit_add_bus(&core->i2c_adap);
160 if (0 == core->i2c_rc) {
161 static u8 tuner_data[] =
162 { 0x0b, 0xdc, 0x86, 0x52 };
163 static struct i2c_msg tuner_msg =
164 { .flags = 0, .addr = 0xc2 >> 1, .buf = tuner_data, .len = 4 };
165
166 dprintk(1, "i2c register ok\n");
167 switch( core->boardnr ) {
168 case CX88_BOARD_HAUPPAUGE_HVR1300:
169 case CX88_BOARD_HAUPPAUGE_HVR3000:
170 case CX88_BOARD_HAUPPAUGE_HVR4000:
171 printk("%s: i2c init: enabling analog demod on HVR1300/3000/4000 tuner\n",
172 core->name);
173 i2c_transfer(core->i2c_client.adapter, &tuner_msg, 1);
174 break;
175 default:
176 break;
177 }
178 if (i2c_scan)
179 do_i2c_scan(core->name,&core->i2c_client);
180 } else
181 printk("%s: i2c register FAILED\n", core->name);
182
183 return core->i2c_rc;
184}
diff --git a/drivers/media/pci/cx88/cx88-input.c b/drivers/media/pci/cx88/cx88-input.c
new file mode 100644
index 000000000000..ebf448c48ca3
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-input.c
@@ -0,0 +1,635 @@
1/*
2 *
3 * Device driver for GPIO attached remote control interfaces
4 * on Conexant 2388x based TV/DVB cards.
5 *
6 * Copyright (c) 2003 Pavel Machek
7 * Copyright (c) 2004 Gerd Knorr
8 * Copyright (c) 2004, 2005 Chris Pascoe
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/init.h>
26#include <linux/hrtimer.h>
27#include <linux/pci.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30
31#include "cx88.h"
32#include <media/rc-core.h>
33
34#define MODULE_NAME "cx88xx"
35
36/* ---------------------------------------------------------------------- */
37
38struct cx88_IR {
39 struct cx88_core *core;
40 struct rc_dev *dev;
41
42 int users;
43
44 char name[32];
45 char phys[32];
46
47 /* sample from gpio pin 16 */
48 u32 sampling;
49
50 /* poll external decoder */
51 int polling;
52 struct hrtimer timer;
53 u32 gpio_addr;
54 u32 last_gpio;
55 u32 mask_keycode;
56 u32 mask_keydown;
57 u32 mask_keyup;
58};
59
60static unsigned ir_samplerate = 4;
61module_param(ir_samplerate, uint, 0444);
62MODULE_PARM_DESC(ir_samplerate, "IR samplerate in kHz, 1 - 20, default 4");
63
64static int ir_debug;
65module_param(ir_debug, int, 0644); /* debug level [IR] */
66MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
67
68#define ir_dprintk(fmt, arg...) if (ir_debug) \
69 printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
70
71#define dprintk(fmt, arg...) if (ir_debug) \
72 printk(KERN_DEBUG "cx88 IR: " fmt , ##arg)
73
74/* ---------------------------------------------------------------------- */
75
76static void cx88_ir_handle_key(struct cx88_IR *ir)
77{
78 struct cx88_core *core = ir->core;
79 u32 gpio, data, auxgpio;
80
81 /* read gpio value */
82 gpio = cx_read(ir->gpio_addr);
83 switch (core->boardnr) {
84 case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
85 /* This board apparently uses a combination of 2 GPIO
86 to represent the keys. Additionally, the second GPIO
87 can be used for parity.
88
89 Example:
90
91 for key "5"
92 gpio = 0x758, auxgpio = 0xe5 or 0xf5
93 for key "Power"
94 gpio = 0x758, auxgpio = 0xed or 0xfd
95 */
96
97 auxgpio = cx_read(MO_GP1_IO);
98 /* Take out the parity part */
99 gpio=(gpio & 0x7fd) + (auxgpio & 0xef);
100 break;
101 case CX88_BOARD_WINFAST_DTV1000:
102 case CX88_BOARD_WINFAST_DTV1800H:
103 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
104 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
105 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
106 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36:
107 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43:
108 gpio = (gpio & 0x6ff) | ((cx_read(MO_GP1_IO) << 8) & 0x900);
109 auxgpio = gpio;
110 break;
111 default:
112 auxgpio = gpio;
113 }
114 if (ir->polling) {
115 if (ir->last_gpio == auxgpio)
116 return;
117 ir->last_gpio = auxgpio;
118 }
119
120 /* extract data */
121 data = ir_extract_bits(gpio, ir->mask_keycode);
122 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
123 gpio, data,
124 ir->polling ? "poll" : "irq",
125 (gpio & ir->mask_keydown) ? " down" : "",
126 (gpio & ir->mask_keyup) ? " up" : "");
127
128 if (ir->core->boardnr == CX88_BOARD_NORWOOD_MICRO) {
129 u32 gpio_key = cx_read(MO_GP0_IO);
130
131 data = (data << 4) | ((gpio_key & 0xf0) >> 4);
132
133 rc_keydown(ir->dev, data, 0);
134
135 } else if (ir->mask_keydown) {
136 /* bit set on keydown */
137 if (gpio & ir->mask_keydown)
138 rc_keydown_notimeout(ir->dev, data, 0);
139 else
140 rc_keyup(ir->dev);
141
142 } else if (ir->mask_keyup) {
143 /* bit cleared on keydown */
144 if (0 == (gpio & ir->mask_keyup))
145 rc_keydown_notimeout(ir->dev, data, 0);
146 else
147 rc_keyup(ir->dev);
148
149 } else {
150 /* can't distinguish keydown/up :-/ */
151 rc_keydown_notimeout(ir->dev, data, 0);
152 rc_keyup(ir->dev);
153 }
154}
155
156static enum hrtimer_restart cx88_ir_work(struct hrtimer *timer)
157{
158 unsigned long missed;
159 struct cx88_IR *ir = container_of(timer, struct cx88_IR, timer);
160
161 cx88_ir_handle_key(ir);
162 missed = hrtimer_forward_now(&ir->timer,
163 ktime_set(0, ir->polling * 1000000));
164 if (missed > 1)
165 ir_dprintk("Missed ticks %ld\n", missed - 1);
166
167 return HRTIMER_RESTART;
168}
169
170static int __cx88_ir_start(void *priv)
171{
172 struct cx88_core *core = priv;
173 struct cx88_IR *ir;
174
175 if (!core || !core->ir)
176 return -EINVAL;
177
178 ir = core->ir;
179
180 if (ir->polling) {
181 hrtimer_init(&ir->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
182 ir->timer.function = cx88_ir_work;
183 hrtimer_start(&ir->timer,
184 ktime_set(0, ir->polling * 1000000),
185 HRTIMER_MODE_REL);
186 }
187 if (ir->sampling) {
188 core->pci_irqmask |= PCI_INT_IR_SMPINT;
189 cx_write(MO_DDS_IO, 0x33F286 * ir_samplerate); /* samplerate */
190 cx_write(MO_DDSCFG_IO, 0x5); /* enable */
191 }
192 return 0;
193}
194
195static void __cx88_ir_stop(void *priv)
196{
197 struct cx88_core *core = priv;
198 struct cx88_IR *ir;
199
200 if (!core || !core->ir)
201 return;
202
203 ir = core->ir;
204 if (ir->sampling) {
205 cx_write(MO_DDSCFG_IO, 0x0);
206 core->pci_irqmask &= ~PCI_INT_IR_SMPINT;
207 }
208
209 if (ir->polling)
210 hrtimer_cancel(&ir->timer);
211}
212
213int cx88_ir_start(struct cx88_core *core)
214{
215 if (core->ir->users)
216 return __cx88_ir_start(core);
217
218 return 0;
219}
220
221void cx88_ir_stop(struct cx88_core *core)
222{
223 if (core->ir->users)
224 __cx88_ir_stop(core);
225}
226
227static int cx88_ir_open(struct rc_dev *rc)
228{
229 struct cx88_core *core = rc->priv;
230
231 core->ir->users++;
232 return __cx88_ir_start(core);
233}
234
235static void cx88_ir_close(struct rc_dev *rc)
236{
237 struct cx88_core *core = rc->priv;
238
239 core->ir->users--;
240 if (!core->ir->users)
241 __cx88_ir_stop(core);
242}
243
244/* ---------------------------------------------------------------------- */
245
246int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
247{
248 struct cx88_IR *ir;
249 struct rc_dev *dev;
250 char *ir_codes = NULL;
251 u64 rc_type = RC_TYPE_OTHER;
252 int err = -ENOMEM;
253 u32 hardware_mask = 0; /* For devices with a hardware mask, when
254 * used with a full-code IR table
255 */
256
257 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
258 dev = rc_allocate_device();
259 if (!ir || !dev)
260 goto err_out_free;
261
262 ir->dev = dev;
263
264 /* detect & configure */
265 switch (core->boardnr) {
266 case CX88_BOARD_DNTV_LIVE_DVB_T:
267 case CX88_BOARD_KWORLD_DVB_T:
268 case CX88_BOARD_KWORLD_DVB_T_CX22702:
269 ir_codes = RC_MAP_DNTV_LIVE_DVB_T;
270 ir->gpio_addr = MO_GP1_IO;
271 ir->mask_keycode = 0x1f;
272 ir->mask_keyup = 0x60;
273 ir->polling = 50; /* ms */
274 break;
275 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
276 ir_codes = RC_MAP_CINERGY_1400;
277 ir->sampling = 0xeb04; /* address */
278 break;
279 case CX88_BOARD_HAUPPAUGE:
280 case CX88_BOARD_HAUPPAUGE_DVB_T1:
281 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
282 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
283 case CX88_BOARD_HAUPPAUGE_HVR1100:
284 case CX88_BOARD_HAUPPAUGE_HVR3000:
285 case CX88_BOARD_HAUPPAUGE_HVR4000:
286 case CX88_BOARD_HAUPPAUGE_HVR4000LITE:
287 case CX88_BOARD_PCHDTV_HD3000:
288 case CX88_BOARD_PCHDTV_HD5500:
289 case CX88_BOARD_HAUPPAUGE_IRONLY:
290 ir_codes = RC_MAP_HAUPPAUGE;
291 ir->sampling = 1;
292 break;
293 case CX88_BOARD_WINFAST_DTV2000H:
294 case CX88_BOARD_WINFAST_DTV2000H_J:
295 case CX88_BOARD_WINFAST_DTV1800H:
296 case CX88_BOARD_WINFAST_DTV1800H_XC4000:
297 case CX88_BOARD_WINFAST_DTV2000H_PLUS:
298 ir_codes = RC_MAP_WINFAST;
299 ir->gpio_addr = MO_GP0_IO;
300 ir->mask_keycode = 0x8f8;
301 ir->mask_keyup = 0x100;
302 ir->polling = 50; /* ms */
303 break;
304 case CX88_BOARD_WINFAST2000XP_EXPERT:
305 case CX88_BOARD_WINFAST_DTV1000:
306 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL:
307 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36:
308 case CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43:
309 ir_codes = RC_MAP_WINFAST;
310 ir->gpio_addr = MO_GP0_IO;
311 ir->mask_keycode = 0x8f8;
312 ir->mask_keyup = 0x100;
313 ir->polling = 1; /* ms */
314 break;
315 case CX88_BOARD_IODATA_GVBCTV7E:
316 ir_codes = RC_MAP_IODATA_BCTV7E;
317 ir->gpio_addr = MO_GP0_IO;
318 ir->mask_keycode = 0xfd;
319 ir->mask_keydown = 0x02;
320 ir->polling = 5; /* ms */
321 break;
322 case CX88_BOARD_PROLINK_PLAYTVPVR:
323 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
324 /*
325 * It seems that this hardware is paired with NEC extended
326 * address 0x866b. So, unfortunately, its usage with other
327 * IR's with different address won't work. Still, there are
328 * other IR's from the same manufacturer that works, like the
329 * 002-T mini RC, provided with newer PV hardware
330 */
331 ir_codes = RC_MAP_PIXELVIEW_MK12;
332 ir->gpio_addr = MO_GP1_IO;
333 ir->mask_keyup = 0x80;
334 ir->polling = 10; /* ms */
335 hardware_mask = 0x3f; /* Hardware returns only 6 bits from command part */
336 break;
337 case CX88_BOARD_PROLINK_PV_8000GT:
338 case CX88_BOARD_PROLINK_PV_GLOBAL_XTREME:
339 ir_codes = RC_MAP_PIXELVIEW_NEW;
340 ir->gpio_addr = MO_GP1_IO;
341 ir->mask_keycode = 0x3f;
342 ir->mask_keyup = 0x80;
343 ir->polling = 1; /* ms */
344 break;
345 case CX88_BOARD_KWORLD_LTV883:
346 ir_codes = RC_MAP_PIXELVIEW;
347 ir->gpio_addr = MO_GP1_IO;
348 ir->mask_keycode = 0x1f;
349 ir->mask_keyup = 0x60;
350 ir->polling = 1; /* ms */
351 break;
352 case CX88_BOARD_ADSTECH_DVB_T_PCI:
353 ir_codes = RC_MAP_ADSTECH_DVB_T_PCI;
354 ir->gpio_addr = MO_GP1_IO;
355 ir->mask_keycode = 0xbf;
356 ir->mask_keyup = 0x40;
357 ir->polling = 50; /* ms */
358 break;
359 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
360 ir_codes = RC_MAP_MSI_TVANYWHERE;
361 ir->gpio_addr = MO_GP1_IO;
362 ir->mask_keycode = 0x1f;
363 ir->mask_keyup = 0x40;
364 ir->polling = 1; /* ms */
365 break;
366 case CX88_BOARD_AVERTV_303:
367 case CX88_BOARD_AVERTV_STUDIO_303:
368 ir_codes = RC_MAP_AVERTV_303;
369 ir->gpio_addr = MO_GP2_IO;
370 ir->mask_keycode = 0xfb;
371 ir->mask_keydown = 0x02;
372 ir->polling = 50; /* ms */
373 break;
374 case CX88_BOARD_OMICOM_SS4_PCI:
375 case CX88_BOARD_SATTRADE_ST4200:
376 case CX88_BOARD_TBS_8920:
377 case CX88_BOARD_TBS_8910:
378 case CX88_BOARD_PROF_7300:
379 case CX88_BOARD_PROF_7301:
380 case CX88_BOARD_PROF_6200:
381 ir_codes = RC_MAP_TBS_NEC;
382 ir->sampling = 0xff00; /* address */
383 break;
384 case CX88_BOARD_TEVII_S464:
385 case CX88_BOARD_TEVII_S460:
386 case CX88_BOARD_TEVII_S420:
387 ir_codes = RC_MAP_TEVII_NEC;
388 ir->sampling = 0xff00; /* address */
389 break;
390 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
391 ir_codes = RC_MAP_DNTV_LIVE_DVBT_PRO;
392 ir->sampling = 0xff00; /* address */
393 break;
394 case CX88_BOARD_NORWOOD_MICRO:
395 ir_codes = RC_MAP_NORWOOD;
396 ir->gpio_addr = MO_GP1_IO;
397 ir->mask_keycode = 0x0e;
398 ir->mask_keyup = 0x80;
399 ir->polling = 50; /* ms */
400 break;
401 case CX88_BOARD_NPGTECH_REALTV_TOP10FM:
402 ir_codes = RC_MAP_NPGTECH;
403 ir->gpio_addr = MO_GP0_IO;
404 ir->mask_keycode = 0xfa;
405 ir->polling = 50; /* ms */
406 break;
407 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
408 ir_codes = RC_MAP_PINNACLE_PCTV_HD;
409 ir->sampling = 1;
410 break;
411 case CX88_BOARD_POWERCOLOR_REAL_ANGEL:
412 ir_codes = RC_MAP_POWERCOLOR_REAL_ANGEL;
413 ir->gpio_addr = MO_GP2_IO;
414 ir->mask_keycode = 0x7e;
415 ir->polling = 100; /* ms */
416 break;
417 case CX88_BOARD_TWINHAN_VP1027_DVBS:
418 ir_codes = RC_MAP_TWINHAN_VP1027_DVBS;
419 rc_type = RC_TYPE_NEC;
420 ir->sampling = 0xff00; /* address */
421 break;
422 }
423
424 if (!ir_codes) {
425 err = -ENODEV;
426 goto err_out_free;
427 }
428
429 /*
430 * The usage of mask_keycode were very convenient, due to several
431 * reasons. Among others, the scancode tables were using the scancode
432 * as the index elements. So, the less bits it was used, the smaller
433 * the table were stored. After the input changes, the better is to use
434 * the full scancodes, since it allows replacing the IR remote by
435 * another one. Unfortunately, there are still some hardware, like
436 * Pixelview Ultra Pro, where only part of the scancode is sent via
437 * GPIO. So, there's no way to get the full scancode. Due to that,
438 * hardware_mask were introduced here: it represents those hardware
439 * that has such limits.
440 */
441 if (hardware_mask && !ir->mask_keycode)
442 ir->mask_keycode = hardware_mask;
443
444 /* init input device */
445 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)", core->board.name);
446 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
447
448 dev->input_name = ir->name;
449 dev->input_phys = ir->phys;
450 dev->input_id.bustype = BUS_PCI;
451 dev->input_id.version = 1;
452 if (pci->subsystem_vendor) {
453 dev->input_id.vendor = pci->subsystem_vendor;
454 dev->input_id.product = pci->subsystem_device;
455 } else {
456 dev->input_id.vendor = pci->vendor;
457 dev->input_id.product = pci->device;
458 }
459 dev->dev.parent = &pci->dev;
460 dev->map_name = ir_codes;
461 dev->driver_name = MODULE_NAME;
462 dev->priv = core;
463 dev->open = cx88_ir_open;
464 dev->close = cx88_ir_close;
465 dev->scanmask = hardware_mask;
466
467 if (ir->sampling) {
468 dev->driver_type = RC_DRIVER_IR_RAW;
469 dev->timeout = 10 * 1000 * 1000; /* 10 ms */
470 } else {
471 dev->driver_type = RC_DRIVER_SCANCODE;
472 dev->allowed_protos = rc_type;
473 }
474
475 ir->core = core;
476 core->ir = ir;
477
478 /* all done */
479 err = rc_register_device(dev);
480 if (err)
481 goto err_out_free;
482
483 return 0;
484
485err_out_free:
486 rc_free_device(dev);
487 core->ir = NULL;
488 kfree(ir);
489 return err;
490}
491
492int cx88_ir_fini(struct cx88_core *core)
493{
494 struct cx88_IR *ir = core->ir;
495
496 /* skip detach on non attached boards */
497 if (NULL == ir)
498 return 0;
499
500 cx88_ir_stop(core);
501 rc_unregister_device(ir->dev);
502 kfree(ir);
503
504 /* done */
505 core->ir = NULL;
506 return 0;
507}
508
509/* ---------------------------------------------------------------------- */
510
511void cx88_ir_irq(struct cx88_core *core)
512{
513 struct cx88_IR *ir = core->ir;
514 u32 samples;
515 unsigned todo, bits;
516 struct ir_raw_event ev;
517
518 if (!ir || !ir->sampling)
519 return;
520
521 /*
522 * Samples are stored in a 32 bit register, oldest sample in
523 * the msb. A set bit represents space and an unset bit
524 * represents a pulse.
525 */
526 samples = cx_read(MO_SAMPLE_IO);
527
528 if (samples == 0xff && ir->dev->idle)
529 return;
530
531 init_ir_raw_event(&ev);
532 for (todo = 32; todo > 0; todo -= bits) {
533 ev.pulse = samples & 0x80000000 ? false : true;
534 bits = min(todo, 32U - fls(ev.pulse ? samples : ~samples));
535 ev.duration = (bits * (NSEC_PER_SEC / 1000)) / ir_samplerate;
536 ir_raw_event_store_with_filter(ir->dev, &ev);
537 samples <<= bits;
538 }
539 ir_raw_event_handle(ir->dev);
540}
541
542static int get_key_pvr2000(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
543{
544 int flags, code;
545
546 /* poll IR chip */
547 flags = i2c_smbus_read_byte_data(ir->c, 0x10);
548 if (flags < 0) {
549 dprintk("read error\n");
550 return 0;
551 }
552 /* key pressed ? */
553 if (0 == (flags & 0x80))
554 return 0;
555
556 /* read actual key code */
557 code = i2c_smbus_read_byte_data(ir->c, 0x00);
558 if (code < 0) {
559 dprintk("read error\n");
560 return 0;
561 }
562
563 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n",
564 code & 0xff, flags & 0xff);
565
566 *ir_key = code & 0xff;
567 *ir_raw = code;
568 return 1;
569}
570
571void cx88_i2c_init_ir(struct cx88_core *core)
572{
573 struct i2c_board_info info;
574 const unsigned short default_addr_list[] = {
575 0x18, 0x6b, 0x71,
576 I2C_CLIENT_END
577 };
578 const unsigned short pvr2000_addr_list[] = {
579 0x18, 0x1a,
580 I2C_CLIENT_END
581 };
582 const unsigned short *addr_list = default_addr_list;
583 const unsigned short *addrp;
584 /* Instantiate the IR receiver device, if present */
585 if (0 != core->i2c_rc)
586 return;
587
588 memset(&info, 0, sizeof(struct i2c_board_info));
589 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
590
591 switch (core->boardnr) {
592 case CX88_BOARD_LEADTEK_PVR2000:
593 addr_list = pvr2000_addr_list;
594 core->init_data.name = "cx88 Leadtek PVR 2000 remote";
595 core->init_data.type = RC_TYPE_UNKNOWN;
596 core->init_data.get_key = get_key_pvr2000;
597 core->init_data.ir_codes = RC_MAP_EMPTY;
598 break;
599 }
600
601 /*
602 * We can't call i2c_new_probed_device() because it uses
603 * quick writes for probing and at least some RC receiver
604 * devices only reply to reads.
605 * Also, Hauppauge XVR needs to be specified, as address 0x71
606 * conflicts with another remote type used with saa7134
607 */
608 for (addrp = addr_list; *addrp != I2C_CLIENT_END; addrp++) {
609 info.platform_data = NULL;
610 memset(&core->init_data, 0, sizeof(core->init_data));
611
612 if (*addrp == 0x71) {
613 /* Hauppauge XVR */
614 core->init_data.name = "cx88 Hauppauge XVR remote";
615 core->init_data.ir_codes = RC_MAP_HAUPPAUGE;
616 core->init_data.type = RC_TYPE_RC5;
617 core->init_data.internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
618
619 info.platform_data = &core->init_data;
620 }
621 if (i2c_smbus_xfer(&core->i2c_adap, *addrp, 0,
622 I2C_SMBUS_READ, 0,
623 I2C_SMBUS_QUICK, NULL) >= 0) {
624 info.addr = *addrp;
625 i2c_new_device(&core->i2c_adap, &info);
626 break;
627 }
628 }
629}
630
631/* ---------------------------------------------------------------------- */
632
633MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
634MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
635MODULE_LICENSE("GPL");
diff --git a/drivers/media/pci/cx88/cx88-mpeg.c b/drivers/media/pci/cx88/cx88-mpeg.c
new file mode 100644
index 000000000000..cd5386ee210c
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-mpeg.c
@@ -0,0 +1,929 @@
1/*
2 *
3 * Support for the mpeg transport stream transfers
4 * PCI function #2 of the cx2388x.
5 *
6 * (c) 2004 Jelle Foks <jelle@foks.us>
7 * (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/dma-mapping.h>
30#include <linux/interrupt.h>
31#include <asm/delay.h>
32
33#include "cx88.h"
34
35/* ------------------------------------------------------------------ */
36
37MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
38MODULE_AUTHOR("Jelle Foks <jelle@foks.us>");
39MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
40MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
41MODULE_LICENSE("GPL");
42MODULE_VERSION(CX88_VERSION);
43
44static unsigned int debug;
45module_param(debug,int,0644);
46MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");
47
48#define dprintk(level,fmt, arg...) if (debug >= level) \
49 printk(KERN_DEBUG "%s/2-mpeg: " fmt, dev->core->name, ## arg)
50
51#define mpeg_dbg(level,fmt, arg...) if (debug >= level) \
52 printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg)
53
54#if defined(CONFIG_MODULES) && defined(MODULE)
55static void request_module_async(struct work_struct *work)
56{
57 struct cx8802_dev *dev=container_of(work, struct cx8802_dev, request_module_wk);
58
59 if (dev->core->board.mpeg & CX88_MPEG_DVB)
60 request_module("cx88-dvb");
61 if (dev->core->board.mpeg & CX88_MPEG_BLACKBIRD)
62 request_module("cx88-blackbird");
63}
64
65static void request_modules(struct cx8802_dev *dev)
66{
67 INIT_WORK(&dev->request_module_wk, request_module_async);
68 schedule_work(&dev->request_module_wk);
69}
70
71static void flush_request_modules(struct cx8802_dev *dev)
72{
73 flush_work_sync(&dev->request_module_wk);
74}
75#else
76#define request_modules(dev)
77#define flush_request_modules(dev)
78#endif /* CONFIG_MODULES */
79
80
81static LIST_HEAD(cx8802_devlist);
82static DEFINE_MUTEX(cx8802_mutex);
83/* ------------------------------------------------------------------ */
84
85static int cx8802_start_dma(struct cx8802_dev *dev,
86 struct cx88_dmaqueue *q,
87 struct cx88_buffer *buf)
88{
89 struct cx88_core *core = dev->core;
90
91 dprintk(1, "cx8802_start_dma w: %d, h: %d, f: %d\n",
92 buf->vb.width, buf->vb.height, buf->vb.field);
93
94 /* setup fifo + format */
95 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
96 dev->ts_packet_size, buf->risc.dma);
97
98 /* write TS length to chip */
99 cx_write(MO_TS_LNGTH, buf->vb.width);
100
101 /* FIXME: this needs a review.
102 * also: move to cx88-blackbird + cx88-dvb source files? */
103
104 dprintk( 1, "core->active_type_id = 0x%08x\n", core->active_type_id);
105
106 if ( (core->active_type_id == CX88_MPEG_DVB) &&
107 (core->board.mpeg & CX88_MPEG_DVB) ) {
108
109 dprintk( 1, "cx8802_start_dma doing .dvb\n");
110 /* negedge driven & software reset */
111 cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
112 udelay(100);
113 cx_write(MO_PINMUX_IO, 0x00);
114 cx_write(TS_HW_SOP_CNTRL, 0x47<<16|188<<4|0x01);
115 switch (core->boardnr) {
116 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
117 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
118 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
119 case CX88_BOARD_PCHDTV_HD5500:
120 cx_write(TS_SOP_STAT, 1<<13);
121 break;
122 case CX88_BOARD_SAMSUNG_SMT_7020:
123 cx_write(TS_SOP_STAT, 0x00);
124 break;
125 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
126 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
127 cx_write(MO_PINMUX_IO, 0x88); /* Enable MPEG parallel IO and video signal pins */
128 udelay(100);
129 break;
130 case CX88_BOARD_HAUPPAUGE_HVR1300:
131 /* Enable MPEG parallel IO and video signal pins */
132 cx_write(MO_PINMUX_IO, 0x88);
133 cx_write(TS_SOP_STAT, 0);
134 cx_write(TS_VALERR_CNTRL, 0);
135 break;
136 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
137 /* Enable MPEG parallel IO and video signal pins */
138 cx_write(MO_PINMUX_IO, 0x88);
139 cx_write(TS_HW_SOP_CNTRL, (0x47 << 16) | (188 << 4));
140 dev->ts_gen_cntrl = 5;
141 cx_write(TS_SOP_STAT, 0);
142 cx_write(TS_VALERR_CNTRL, 0);
143 udelay(100);
144 break;
145 default:
146 cx_write(TS_SOP_STAT, 0x00);
147 break;
148 }
149 cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
150 udelay(100);
151 } else if ( (core->active_type_id == CX88_MPEG_BLACKBIRD) &&
152 (core->board.mpeg & CX88_MPEG_BLACKBIRD) ) {
153 dprintk( 1, "cx8802_start_dma doing .blackbird\n");
154 cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
155
156 cx_write(TS_GEN_CNTRL, 0x46); /* punctured clock TS & posedge driven & software reset */
157 udelay(100);
158
159 cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
160 cx_write(TS_VALERR_CNTRL, 0x2000);
161
162 cx_write(TS_GEN_CNTRL, 0x06); /* punctured clock TS & posedge driven */
163 udelay(100);
164 } else {
165 printk( "%s() Failed. Unsupported value in .mpeg (0x%08x)\n", __func__,
166 core->board.mpeg );
167 return -EINVAL;
168 }
169
170 /* reset counter */
171 cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
172 q->count = 1;
173
174 /* enable irqs */
175 dprintk( 1, "setting the interrupt mask\n" );
176 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_TSINT);
177 cx_set(MO_TS_INTMSK, 0x1f0011);
178
179 /* start dma */
180 cx_set(MO_DEV_CNTRL2, (1<<5));
181 cx_set(MO_TS_DMACNTRL, 0x11);
182 return 0;
183}
184
185static int cx8802_stop_dma(struct cx8802_dev *dev)
186{
187 struct cx88_core *core = dev->core;
188 dprintk( 1, "cx8802_stop_dma\n" );
189
190 /* stop dma */
191 cx_clear(MO_TS_DMACNTRL, 0x11);
192
193 /* disable irqs */
194 cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
195 cx_clear(MO_TS_INTMSK, 0x1f0011);
196
197 /* Reset the controller */
198 cx_write(TS_GEN_CNTRL, 0xcd);
199 return 0;
200}
201
202static int cx8802_restart_queue(struct cx8802_dev *dev,
203 struct cx88_dmaqueue *q)
204{
205 struct cx88_buffer *buf;
206
207 dprintk( 1, "cx8802_restart_queue\n" );
208 if (list_empty(&q->active))
209 {
210 struct cx88_buffer *prev;
211 prev = NULL;
212
213 dprintk(1, "cx8802_restart_queue: queue is empty\n" );
214
215 for (;;) {
216 if (list_empty(&q->queued))
217 return 0;
218 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
219 if (NULL == prev) {
220 list_del(&buf->vb.queue);
221 list_add_tail(&buf->vb.queue,&q->active);
222 cx8802_start_dma(dev, q, buf);
223 buf->vb.state = VIDEOBUF_ACTIVE;
224 buf->count = q->count++;
225 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
226 dprintk(1,"[%p/%d] restart_queue - first active\n",
227 buf,buf->vb.i);
228
229 } else if (prev->vb.width == buf->vb.width &&
230 prev->vb.height == buf->vb.height &&
231 prev->fmt == buf->fmt) {
232 list_del(&buf->vb.queue);
233 list_add_tail(&buf->vb.queue,&q->active);
234 buf->vb.state = VIDEOBUF_ACTIVE;
235 buf->count = q->count++;
236 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
237 dprintk(1,"[%p/%d] restart_queue - move to active\n",
238 buf,buf->vb.i);
239 } else {
240 return 0;
241 }
242 prev = buf;
243 }
244 return 0;
245 }
246
247 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
248 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
249 buf, buf->vb.i);
250 cx8802_start_dma(dev, q, buf);
251 list_for_each_entry(buf, &q->active, vb.queue)
252 buf->count = q->count++;
253 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
254 return 0;
255}
256
257/* ------------------------------------------------------------------ */
258
259int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
260 struct cx88_buffer *buf, enum v4l2_field field)
261{
262 int size = dev->ts_packet_size * dev->ts_packet_count;
263 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
264 int rc;
265
266 dprintk(1, "%s: %p\n", __func__, buf);
267 if (0 != buf->vb.baddr && buf->vb.bsize < size)
268 return -EINVAL;
269
270 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
271 buf->vb.width = dev->ts_packet_size;
272 buf->vb.height = dev->ts_packet_count;
273 buf->vb.size = size;
274 buf->vb.field = field /*V4L2_FIELD_TOP*/;
275
276 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
277 goto fail;
278 cx88_risc_databuffer(dev->pci, &buf->risc,
279 dma->sglist,
280 buf->vb.width, buf->vb.height, 0);
281 }
282 buf->vb.state = VIDEOBUF_PREPARED;
283 return 0;
284
285 fail:
286 cx88_free_buffer(q,buf);
287 return rc;
288}
289
290void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
291{
292 struct cx88_buffer *prev;
293 struct cx88_dmaqueue *cx88q = &dev->mpegq;
294
295 dprintk( 1, "cx8802_buf_queue\n" );
296 /* add jump to stopper */
297 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
298 buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
299
300 if (list_empty(&cx88q->active)) {
301 dprintk( 1, "queue is empty - first active\n" );
302 list_add_tail(&buf->vb.queue,&cx88q->active);
303 cx8802_start_dma(dev, cx88q, buf);
304 buf->vb.state = VIDEOBUF_ACTIVE;
305 buf->count = cx88q->count++;
306 mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
307 dprintk(1,"[%p/%d] %s - first active\n",
308 buf, buf->vb.i, __func__);
309
310 } else {
311 dprintk( 1, "queue is not empty - append to active\n" );
312 prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
313 list_add_tail(&buf->vb.queue,&cx88q->active);
314 buf->vb.state = VIDEOBUF_ACTIVE;
315 buf->count = cx88q->count++;
316 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
317 dprintk( 1, "[%p/%d] %s - append to active\n",
318 buf, buf->vb.i, __func__);
319 }
320}
321
322/* ----------------------------------------------------------- */
323
324static void do_cancel_buffers(struct cx8802_dev *dev, const char *reason, int restart)
325{
326 struct cx88_dmaqueue *q = &dev->mpegq;
327 struct cx88_buffer *buf;
328 unsigned long flags;
329
330 spin_lock_irqsave(&dev->slock,flags);
331 while (!list_empty(&q->active)) {
332 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
333 list_del(&buf->vb.queue);
334 buf->vb.state = VIDEOBUF_ERROR;
335 wake_up(&buf->vb.done);
336 dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
337 buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
338 }
339 if (restart)
340 {
341 dprintk(1, "restarting queue\n" );
342 cx8802_restart_queue(dev,q);
343 }
344 spin_unlock_irqrestore(&dev->slock,flags);
345}
346
347void cx8802_cancel_buffers(struct cx8802_dev *dev)
348{
349 struct cx88_dmaqueue *q = &dev->mpegq;
350
351 dprintk( 1, "cx8802_cancel_buffers" );
352 del_timer_sync(&q->timeout);
353 cx8802_stop_dma(dev);
354 do_cancel_buffers(dev,"cancel",0);
355}
356
357static void cx8802_timeout(unsigned long data)
358{
359 struct cx8802_dev *dev = (struct cx8802_dev*)data;
360
361 dprintk(1, "%s\n",__func__);
362
363 if (debug)
364 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
365 cx8802_stop_dma(dev);
366 do_cancel_buffers(dev,"timeout",1);
367}
368
369static const char * cx88_mpeg_irqs[32] = {
370 "ts_risci1", NULL, NULL, NULL,
371 "ts_risci2", NULL, NULL, NULL,
372 "ts_oflow", NULL, NULL, NULL,
373 "ts_sync", NULL, NULL, NULL,
374 "opc_err", "par_err", "rip_err", "pci_abort",
375 "ts_err?",
376};
377
378static void cx8802_mpeg_irq(struct cx8802_dev *dev)
379{
380 struct cx88_core *core = dev->core;
381 u32 status, mask, count;
382
383 dprintk( 1, "cx8802_mpeg_irq\n" );
384 status = cx_read(MO_TS_INTSTAT);
385 mask = cx_read(MO_TS_INTMSK);
386 if (0 == (status & mask))
387 return;
388
389 cx_write(MO_TS_INTSTAT, status);
390
391 if (debug || (status & mask & ~0xff))
392 cx88_print_irqbits(core->name, "irq mpeg ",
393 cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
394 status, mask);
395
396 /* risc op code error */
397 if (status & (1 << 16)) {
398 printk(KERN_WARNING "%s: mpeg risc op code error\n",core->name);
399 cx_clear(MO_TS_DMACNTRL, 0x11);
400 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
401 }
402
403 /* risc1 y */
404 if (status & 0x01) {
405 dprintk( 1, "wake up\n" );
406 spin_lock(&dev->slock);
407 count = cx_read(MO_TS_GPCNT);
408 cx88_wakeup(dev->core, &dev->mpegq, count);
409 spin_unlock(&dev->slock);
410 }
411
412 /* risc2 y */
413 if (status & 0x10) {
414 spin_lock(&dev->slock);
415 cx8802_restart_queue(dev,&dev->mpegq);
416 spin_unlock(&dev->slock);
417 }
418
419 /* other general errors */
420 if (status & 0x1f0100) {
421 dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
422 spin_lock(&dev->slock);
423 cx8802_stop_dma(dev);
424 cx8802_restart_queue(dev,&dev->mpegq);
425 spin_unlock(&dev->slock);
426 }
427}
428
429#define MAX_IRQ_LOOP 10
430
431static irqreturn_t cx8802_irq(int irq, void *dev_id)
432{
433 struct cx8802_dev *dev = dev_id;
434 struct cx88_core *core = dev->core;
435 u32 status;
436 int loop, handled = 0;
437
438 for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
439 status = cx_read(MO_PCI_INTSTAT) &
440 (core->pci_irqmask | PCI_INT_TSINT);
441 if (0 == status)
442 goto out;
443 dprintk( 1, "cx8802_irq\n" );
444 dprintk( 1, " loop: %d/%d\n", loop, MAX_IRQ_LOOP );
445 dprintk( 1, " status: %d\n", status );
446 handled = 1;
447 cx_write(MO_PCI_INTSTAT, status);
448
449 if (status & core->pci_irqmask)
450 cx88_core_irq(core,status);
451 if (status & PCI_INT_TSINT)
452 cx8802_mpeg_irq(dev);
453 };
454 if (MAX_IRQ_LOOP == loop) {
455 dprintk( 0, "clearing mask\n" );
456 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
457 core->name);
458 cx_write(MO_PCI_INTMSK,0);
459 }
460
461 out:
462 return IRQ_RETVAL(handled);
463}
464
465static int cx8802_init_common(struct cx8802_dev *dev)
466{
467 struct cx88_core *core = dev->core;
468 int err;
469
470 /* pci init */
471 if (pci_enable_device(dev->pci))
472 return -EIO;
473 pci_set_master(dev->pci);
474 if (!pci_dma_supported(dev->pci,DMA_BIT_MASK(32))) {
475 printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
476 return -EIO;
477 }
478
479 dev->pci_rev = dev->pci->revision;
480 pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER, &dev->pci_lat);
481 printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
482 "latency: %d, mmio: 0x%llx\n", dev->core->name,
483 pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
484 dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
485
486 /* initialize driver struct */
487 spin_lock_init(&dev->slock);
488
489 /* init dma queue */
490 INIT_LIST_HEAD(&dev->mpegq.active);
491 INIT_LIST_HEAD(&dev->mpegq.queued);
492 dev->mpegq.timeout.function = cx8802_timeout;
493 dev->mpegq.timeout.data = (unsigned long)dev;
494 init_timer(&dev->mpegq.timeout);
495 cx88_risc_stopper(dev->pci,&dev->mpegq.stopper,
496 MO_TS_DMACNTRL,0x11,0x00);
497
498 /* get irq */
499 err = request_irq(dev->pci->irq, cx8802_irq,
500 IRQF_SHARED | IRQF_DISABLED, dev->core->name, dev);
501 if (err < 0) {
502 printk(KERN_ERR "%s: can't get IRQ %d\n",
503 dev->core->name, dev->pci->irq);
504 return err;
505 }
506 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
507
508 /* everything worked */
509 pci_set_drvdata(dev->pci,dev);
510 return 0;
511}
512
513static void cx8802_fini_common(struct cx8802_dev *dev)
514{
515 dprintk( 2, "cx8802_fini_common\n" );
516 cx8802_stop_dma(dev);
517 pci_disable_device(dev->pci);
518
519 /* unregister stuff */
520 free_irq(dev->pci->irq, dev);
521 pci_set_drvdata(dev->pci, NULL);
522
523 /* free memory */
524 btcx_riscmem_free(dev->pci,&dev->mpegq.stopper);
525}
526
527/* ----------------------------------------------------------- */
528
529static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
530{
531 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
532 struct cx88_core *core = dev->core;
533
534 /* stop mpeg dma */
535 spin_lock(&dev->slock);
536 if (!list_empty(&dev->mpegq.active)) {
537 dprintk( 2, "suspend\n" );
538 printk("%s: suspend mpeg\n", core->name);
539 cx8802_stop_dma(dev);
540 del_timer(&dev->mpegq.timeout);
541 }
542 spin_unlock(&dev->slock);
543
544 /* FIXME -- shutdown device */
545 cx88_shutdown(dev->core);
546
547 pci_save_state(pci_dev);
548 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
549 pci_disable_device(pci_dev);
550 dev->state.disabled = 1;
551 }
552 return 0;
553}
554
555static int cx8802_resume_common(struct pci_dev *pci_dev)
556{
557 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
558 struct cx88_core *core = dev->core;
559 int err;
560
561 if (dev->state.disabled) {
562 err=pci_enable_device(pci_dev);
563 if (err) {
564 printk(KERN_ERR "%s: can't enable device\n",
565 dev->core->name);
566 return err;
567 }
568 dev->state.disabled = 0;
569 }
570 err=pci_set_power_state(pci_dev, PCI_D0);
571 if (err) {
572 printk(KERN_ERR "%s: can't enable device\n",
573 dev->core->name);
574 pci_disable_device(pci_dev);
575 dev->state.disabled = 1;
576
577 return err;
578 }
579 pci_restore_state(pci_dev);
580
581 /* FIXME: re-initialize hardware */
582 cx88_reset(dev->core);
583
584 /* restart video+vbi capture */
585 spin_lock(&dev->slock);
586 if (!list_empty(&dev->mpegq.active)) {
587 printk("%s: resume mpeg\n", core->name);
588 cx8802_restart_queue(dev,&dev->mpegq);
589 }
590 spin_unlock(&dev->slock);
591
592 return 0;
593}
594
595struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
596{
597 struct cx8802_driver *d;
598
599 list_for_each_entry(d, &dev->drvlist, drvlist)
600 if (d->type_id == btype)
601 return d;
602
603 return NULL;
604}
605
606/* Driver asked for hardware access. */
607static int cx8802_request_acquire(struct cx8802_driver *drv)
608{
609 struct cx88_core *core = drv->core;
610 unsigned int i;
611
612 /* Fail a request for hardware if the device is busy. */
613 if (core->active_type_id != CX88_BOARD_NONE &&
614 core->active_type_id != drv->type_id)
615 return -EBUSY;
616
617 if (drv->type_id == CX88_MPEG_DVB) {
618 /* When switching to DVB, always set the input to the tuner */
619 core->last_analog_input = core->input;
620 core->input = 0;
621 for (i = 0;
622 i < (sizeof(core->board.input) / sizeof(struct cx88_input));
623 i++) {
624 if (core->board.input[i].type == CX88_VMUX_DVB) {
625 core->input = i;
626 break;
627 }
628 }
629 }
630
631 if (drv->advise_acquire)
632 {
633 core->active_ref++;
634 if (core->active_type_id == CX88_BOARD_NONE) {
635 core->active_type_id = drv->type_id;
636 drv->advise_acquire(drv);
637 }
638
639 mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
640 }
641
642 return 0;
643}
644
645/* Driver asked to release hardware. */
646static int cx8802_request_release(struct cx8802_driver *drv)
647{
648 struct cx88_core *core = drv->core;
649
650 if (drv->advise_release && --core->active_ref == 0)
651 {
652 if (drv->type_id == CX88_MPEG_DVB) {
653 /* If the DVB driver is releasing, reset the input
654 state to the last configured analog input */
655 core->input = core->last_analog_input;
656 }
657
658 drv->advise_release(drv);
659 core->active_type_id = CX88_BOARD_NONE;
660 mpeg_dbg(1,"%s() Post release GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
661 }
662
663 return 0;
664}
665
666static int cx8802_check_driver(struct cx8802_driver *drv)
667{
668 if (drv == NULL)
669 return -ENODEV;
670
671 if ((drv->type_id != CX88_MPEG_DVB) &&
672 (drv->type_id != CX88_MPEG_BLACKBIRD))
673 return -EINVAL;
674
675 if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
676 (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
677 return -EINVAL;
678
679 if ((drv->probe == NULL) ||
680 (drv->remove == NULL) ||
681 (drv->advise_acquire == NULL) ||
682 (drv->advise_release == NULL))
683 return -EINVAL;
684
685 return 0;
686}
687
688int cx8802_register_driver(struct cx8802_driver *drv)
689{
690 struct cx8802_dev *dev;
691 struct cx8802_driver *driver;
692 int err, i = 0;
693
694 printk(KERN_INFO
695 "cx88/2: registering cx8802 driver, type: %s access: %s\n",
696 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
697 drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
698
699 if ((err = cx8802_check_driver(drv)) != 0) {
700 printk(KERN_ERR "cx88/2: cx8802_driver is invalid\n");
701 return err;
702 }
703
704 mutex_lock(&cx8802_mutex);
705
706 list_for_each_entry(dev, &cx8802_devlist, devlist) {
707 printk(KERN_INFO
708 "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
709 dev->core->name, dev->pci->subsystem_vendor,
710 dev->pci->subsystem_device, dev->core->board.name,
711 dev->core->boardnr);
712
713 /* Bring up a new struct for each driver instance */
714 driver = kzalloc(sizeof(*drv),GFP_KERNEL);
715 if (driver == NULL) {
716 err = -ENOMEM;
717 goto out;
718 }
719
720 /* Snapshot of the driver registration data */
721 drv->core = dev->core;
722 drv->suspend = cx8802_suspend_common;
723 drv->resume = cx8802_resume_common;
724 drv->request_acquire = cx8802_request_acquire;
725 drv->request_release = cx8802_request_release;
726 memcpy(driver, drv, sizeof(*driver));
727
728 mutex_lock(&drv->core->lock);
729 err = drv->probe(driver);
730 if (err == 0) {
731 i++;
732 list_add_tail(&driver->drvlist, &dev->drvlist);
733 } else {
734 printk(KERN_ERR
735 "%s/2: cx8802 probe failed, err = %d\n",
736 dev->core->name, err);
737 }
738 mutex_unlock(&drv->core->lock);
739 }
740
741 err = i ? 0 : -ENODEV;
742out:
743 mutex_unlock(&cx8802_mutex);
744 return err;
745}
746
747int cx8802_unregister_driver(struct cx8802_driver *drv)
748{
749 struct cx8802_dev *dev;
750 struct cx8802_driver *d, *dtmp;
751 int err = 0;
752
753 printk(KERN_INFO
754 "cx88/2: unregistering cx8802 driver, type: %s access: %s\n",
755 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
756 drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
757
758 mutex_lock(&cx8802_mutex);
759
760 list_for_each_entry(dev, &cx8802_devlist, devlist) {
761 printk(KERN_INFO
762 "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
763 dev->core->name, dev->pci->subsystem_vendor,
764 dev->pci->subsystem_device, dev->core->board.name,
765 dev->core->boardnr);
766
767 mutex_lock(&dev->core->lock);
768
769 list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
770 /* only unregister the correct driver type */
771 if (d->type_id != drv->type_id)
772 continue;
773
774 err = d->remove(d);
775 if (err == 0) {
776 list_del(&d->drvlist);
777 kfree(d);
778 } else
779 printk(KERN_ERR "%s/2: cx8802 driver remove "
780 "failed (%d)\n", dev->core->name, err);
781 }
782
783 mutex_unlock(&dev->core->lock);
784 }
785
786 mutex_unlock(&cx8802_mutex);
787
788 return err;
789}
790
791/* ----------------------------------------------------------- */
792static int __devinit cx8802_probe(struct pci_dev *pci_dev,
793 const struct pci_device_id *pci_id)
794{
795 struct cx8802_dev *dev;
796 struct cx88_core *core;
797 int err;
798
799 /* general setup */
800 core = cx88_core_get(pci_dev);
801 if (NULL == core)
802 return -EINVAL;
803
804 printk("%s/2: cx2388x 8802 Driver Manager\n", core->name);
805
806 err = -ENODEV;
807 if (!core->board.mpeg)
808 goto fail_core;
809
810 err = -ENOMEM;
811 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
812 if (NULL == dev)
813 goto fail_core;
814 dev->pci = pci_dev;
815 dev->core = core;
816
817 /* Maintain a reference so cx88-video can query the 8802 device. */
818 core->dvbdev = dev;
819
820 err = cx8802_init_common(dev);
821 if (err != 0)
822 goto fail_free;
823
824 INIT_LIST_HEAD(&dev->drvlist);
825 mutex_lock(&cx8802_mutex);
826 list_add_tail(&dev->devlist,&cx8802_devlist);
827 mutex_unlock(&cx8802_mutex);
828
829 /* now autoload cx88-dvb or cx88-blackbird */
830 request_modules(dev);
831 return 0;
832
833 fail_free:
834 kfree(dev);
835 fail_core:
836 core->dvbdev = NULL;
837 cx88_core_put(core,pci_dev);
838 return err;
839}
840
841static void __devexit cx8802_remove(struct pci_dev *pci_dev)
842{
843 struct cx8802_dev *dev;
844
845 dev = pci_get_drvdata(pci_dev);
846
847 dprintk( 1, "%s\n", __func__);
848
849 flush_request_modules(dev);
850
851 mutex_lock(&dev->core->lock);
852
853 if (!list_empty(&dev->drvlist)) {
854 struct cx8802_driver *drv, *tmp;
855 int err;
856
857 printk(KERN_WARNING "%s/2: Trying to remove cx8802 driver "
858 "while cx8802 sub-drivers still loaded?!\n",
859 dev->core->name);
860
861 list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
862 err = drv->remove(drv);
863 if (err == 0) {
864 list_del(&drv->drvlist);
865 } else
866 printk(KERN_ERR "%s/2: cx8802 driver remove "
867 "failed (%d)\n", dev->core->name, err);
868 kfree(drv);
869 }
870 }
871
872 mutex_unlock(&dev->core->lock);
873
874 /* Destroy any 8802 reference. */
875 dev->core->dvbdev = NULL;
876
877 /* common */
878 cx8802_fini_common(dev);
879 cx88_core_put(dev->core,dev->pci);
880 kfree(dev);
881}
882
883static const struct pci_device_id cx8802_pci_tbl[] = {
884 {
885 .vendor = 0x14f1,
886 .device = 0x8802,
887 .subvendor = PCI_ANY_ID,
888 .subdevice = PCI_ANY_ID,
889 },{
890 /* --- end of list --- */
891 }
892};
893MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
894
895static struct pci_driver cx8802_pci_driver = {
896 .name = "cx88-mpeg driver manager",
897 .id_table = cx8802_pci_tbl,
898 .probe = cx8802_probe,
899 .remove = __devexit_p(cx8802_remove),
900};
901
902static int __init cx8802_init(void)
903{
904 printk(KERN_INFO "cx88/2: cx2388x MPEG-TS Driver Manager version %s loaded\n",
905 CX88_VERSION);
906 return pci_register_driver(&cx8802_pci_driver);
907}
908
909static void __exit cx8802_fini(void)
910{
911 pci_unregister_driver(&cx8802_pci_driver);
912}
913
914module_init(cx8802_init);
915module_exit(cx8802_fini);
916EXPORT_SYMBOL(cx8802_buf_prepare);
917EXPORT_SYMBOL(cx8802_buf_queue);
918EXPORT_SYMBOL(cx8802_cancel_buffers);
919
920EXPORT_SYMBOL(cx8802_register_driver);
921EXPORT_SYMBOL(cx8802_unregister_driver);
922EXPORT_SYMBOL(cx8802_get_driver);
923/* ----------------------------------------------------------- */
924/*
925 * Local variables:
926 * c-basic-offset: 8
927 * End:
928 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
929 */
diff --git a/drivers/media/pci/cx88/cx88-reg.h b/drivers/media/pci/cx88/cx88-reg.h
new file mode 100644
index 000000000000..2ec52d1cdea0
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-reg.h
@@ -0,0 +1,836 @@
1/*
2
3 cx88x-hw.h - CX2388x register offsets
4
5 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
6 2001 Michael Eskin
7 2002 Yurij Sysoev <yurij@naturesoft.net>
8 2003 Gerd Knorr <kraxel@bytesex.org>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23*/
24
25#ifndef _CX88_REG_H_
26#define _CX88_REG_H_
27
28/* ---------------------------------------------------------------------- */
29/* PCI IDs and config space */
30
31#ifndef PCI_VENDOR_ID_CONEXANT
32# define PCI_VENDOR_ID_CONEXANT 0x14F1
33#endif
34#ifndef PCI_DEVICE_ID_CX2300_VID
35# define PCI_DEVICE_ID_CX2300_VID 0x8800
36#endif
37
38#define CX88X_DEVCTRL 0x40
39#define CX88X_EN_TBFX 0x02
40#define CX88X_EN_VSFX 0x04
41
42/* ---------------------------------------------------------------------- */
43/* PCI controller registers */
44
45/* Command and Status Register */
46#define F0_CMD_STAT_MM 0x2f0004
47#define F1_CMD_STAT_MM 0x2f0104
48#define F2_CMD_STAT_MM 0x2f0204
49#define F3_CMD_STAT_MM 0x2f0304
50#define F4_CMD_STAT_MM 0x2f0404
51
52/* Device Control #1 */
53#define F0_DEV_CNTRL1_MM 0x2f0040
54#define F1_DEV_CNTRL1_MM 0x2f0140
55#define F2_DEV_CNTRL1_MM 0x2f0240
56#define F3_DEV_CNTRL1_MM 0x2f0340
57#define F4_DEV_CNTRL1_MM 0x2f0440
58
59/* Device Control #1 */
60#define F0_BAR0_MM 0x2f0010
61#define F1_BAR0_MM 0x2f0110
62#define F2_BAR0_MM 0x2f0210
63#define F3_BAR0_MM 0x2f0310
64#define F4_BAR0_MM 0x2f0410
65
66/* ---------------------------------------------------------------------- */
67/* DMA Controller registers */
68
69#define MO_PDMA_STHRSH 0x200000 // Source threshold
70#define MO_PDMA_STADRS 0x200004 // Source target address
71#define MO_PDMA_SIADRS 0x200008 // Source internal address
72#define MO_PDMA_SCNTRL 0x20000C // Source control
73#define MO_PDMA_DTHRSH 0x200010 // Destination threshold
74#define MO_PDMA_DTADRS 0x200014 // Destination target address
75#define MO_PDMA_DIADRS 0x200018 // Destination internal address
76#define MO_PDMA_DCNTRL 0x20001C // Destination control
77#define MO_LD_SSID 0x200030 // Load subsystem ID
78#define MO_DEV_CNTRL2 0x200034 // Device control
79#define MO_PCI_INTMSK 0x200040 // PCI interrupt mask
80#define MO_PCI_INTSTAT 0x200044 // PCI interrupt status
81#define MO_PCI_INTMSTAT 0x200048 // PCI interrupt masked status
82#define MO_VID_INTMSK 0x200050 // Video interrupt mask
83#define MO_VID_INTSTAT 0x200054 // Video interrupt status
84#define MO_VID_INTMSTAT 0x200058 // Video interrupt masked status
85#define MO_VID_INTSSTAT 0x20005C // Video interrupt set status
86#define MO_AUD_INTMSK 0x200060 // Audio interrupt mask
87#define MO_AUD_INTSTAT 0x200064 // Audio interrupt status
88#define MO_AUD_INTMSTAT 0x200068 // Audio interrupt masked status
89#define MO_AUD_INTSSTAT 0x20006C // Audio interrupt set status
90#define MO_TS_INTMSK 0x200070 // Transport stream interrupt mask
91#define MO_TS_INTSTAT 0x200074 // Transport stream interrupt status
92#define MO_TS_INTMSTAT 0x200078 // Transport stream interrupt mask status
93#define MO_TS_INTSSTAT 0x20007C // Transport stream interrupt set status
94#define MO_VIP_INTMSK 0x200080 // VIP interrupt mask
95#define MO_VIP_INTSTAT 0x200084 // VIP interrupt status
96#define MO_VIP_INTMSTAT 0x200088 // VIP interrupt masked status
97#define MO_VIP_INTSSTAT 0x20008C // VIP interrupt set status
98#define MO_GPHST_INTMSK 0x200090 // Host interrupt mask
99#define MO_GPHST_INTSTAT 0x200094 // Host interrupt status
100#define MO_GPHST_INTMSTAT 0x200098 // Host interrupt masked status
101#define MO_GPHST_INTSSTAT 0x20009C // Host interrupt set status
102
103// DMA Channels 1-6 belong to SPIPE
104#define MO_DMA7_PTR1 0x300018 // {24}RW* DMA Current Ptr : Ch#7
105#define MO_DMA8_PTR1 0x30001C // {24}RW* DMA Current Ptr : Ch#8
106
107// DMA Channels 9-20 belong to SPIPE
108#define MO_DMA21_PTR1 0x300080 // {24}R0* DMA Current Ptr : Ch#21
109#define MO_DMA22_PTR1 0x300084 // {24}R0* DMA Current Ptr : Ch#22
110#define MO_DMA23_PTR1 0x300088 // {24}R0* DMA Current Ptr : Ch#23
111#define MO_DMA24_PTR1 0x30008C // {24}R0* DMA Current Ptr : Ch#24
112#define MO_DMA25_PTR1 0x300090 // {24}R0* DMA Current Ptr : Ch#25
113#define MO_DMA26_PTR1 0x300094 // {24}R0* DMA Current Ptr : Ch#26
114#define MO_DMA27_PTR1 0x300098 // {24}R0* DMA Current Ptr : Ch#27
115#define MO_DMA28_PTR1 0x30009C // {24}R0* DMA Current Ptr : Ch#28
116#define MO_DMA29_PTR1 0x3000A0 // {24}R0* DMA Current Ptr : Ch#29
117#define MO_DMA30_PTR1 0x3000A4 // {24}R0* DMA Current Ptr : Ch#30
118#define MO_DMA31_PTR1 0x3000A8 // {24}R0* DMA Current Ptr : Ch#31
119#define MO_DMA32_PTR1 0x3000AC // {24}R0* DMA Current Ptr : Ch#32
120
121#define MO_DMA21_PTR2 0x3000C0 // {24}RW* DMA Tab Ptr : Ch#21
122#define MO_DMA22_PTR2 0x3000C4 // {24}RW* DMA Tab Ptr : Ch#22
123#define MO_DMA23_PTR2 0x3000C8 // {24}RW* DMA Tab Ptr : Ch#23
124#define MO_DMA24_PTR2 0x3000CC // {24}RW* DMA Tab Ptr : Ch#24
125#define MO_DMA25_PTR2 0x3000D0 // {24}RW* DMA Tab Ptr : Ch#25
126#define MO_DMA26_PTR2 0x3000D4 // {24}RW* DMA Tab Ptr : Ch#26
127#define MO_DMA27_PTR2 0x3000D8 // {24}RW* DMA Tab Ptr : Ch#27
128#define MO_DMA28_PTR2 0x3000DC // {24}RW* DMA Tab Ptr : Ch#28
129#define MO_DMA29_PTR2 0x3000E0 // {24}RW* DMA Tab Ptr : Ch#29
130#define MO_DMA30_PTR2 0x3000E4 // {24}RW* DMA Tab Ptr : Ch#30
131#define MO_DMA31_PTR2 0x3000E8 // {24}RW* DMA Tab Ptr : Ch#31
132#define MO_DMA32_PTR2 0x3000EC // {24}RW* DMA Tab Ptr : Ch#32
133
134#define MO_DMA21_CNT1 0x300100 // {11}RW* DMA Buffer Size : Ch#21
135#define MO_DMA22_CNT1 0x300104 // {11}RW* DMA Buffer Size : Ch#22
136#define MO_DMA23_CNT1 0x300108 // {11}RW* DMA Buffer Size : Ch#23
137#define MO_DMA24_CNT1 0x30010C // {11}RW* DMA Buffer Size : Ch#24
138#define MO_DMA25_CNT1 0x300110 // {11}RW* DMA Buffer Size : Ch#25
139#define MO_DMA26_CNT1 0x300114 // {11}RW* DMA Buffer Size : Ch#26
140#define MO_DMA27_CNT1 0x300118 // {11}RW* DMA Buffer Size : Ch#27
141#define MO_DMA28_CNT1 0x30011C // {11}RW* DMA Buffer Size : Ch#28
142#define MO_DMA29_CNT1 0x300120 // {11}RW* DMA Buffer Size : Ch#29
143#define MO_DMA30_CNT1 0x300124 // {11}RW* DMA Buffer Size : Ch#30
144#define MO_DMA31_CNT1 0x300128 // {11}RW* DMA Buffer Size : Ch#31
145#define MO_DMA32_CNT1 0x30012C // {11}RW* DMA Buffer Size : Ch#32
146
147#define MO_DMA21_CNT2 0x300140 // {11}RW* DMA Table Size : Ch#21
148#define MO_DMA22_CNT2 0x300144 // {11}RW* DMA Table Size : Ch#22
149#define MO_DMA23_CNT2 0x300148 // {11}RW* DMA Table Size : Ch#23
150#define MO_DMA24_CNT2 0x30014C // {11}RW* DMA Table Size : Ch#24
151#define MO_DMA25_CNT2 0x300150 // {11}RW* DMA Table Size : Ch#25
152#define MO_DMA26_CNT2 0x300154 // {11}RW* DMA Table Size : Ch#26
153#define MO_DMA27_CNT2 0x300158 // {11}RW* DMA Table Size : Ch#27
154#define MO_DMA28_CNT2 0x30015C // {11}RW* DMA Table Size : Ch#28
155#define MO_DMA29_CNT2 0x300160 // {11}RW* DMA Table Size : Ch#29
156#define MO_DMA30_CNT2 0x300164 // {11}RW* DMA Table Size : Ch#30
157#define MO_DMA31_CNT2 0x300168 // {11}RW* DMA Table Size : Ch#31
158#define MO_DMA32_CNT2 0x30016C // {11}RW* DMA Table Size : Ch#32
159
160
161/* ---------------------------------------------------------------------- */
162/* Video registers */
163
164#define MO_VIDY_DMA 0x310000 // {64}RWp Video Y
165#define MO_VIDU_DMA 0x310008 // {64}RWp Video U
166#define MO_VIDV_DMA 0x310010 // {64}RWp Video V
167#define MO_VBI_DMA 0x310018 // {64}RWp VBI (Vertical blanking interval)
168
169#define MO_DEVICE_STATUS 0x310100
170#define MO_INPUT_FORMAT 0x310104
171#define MO_AGC_BURST 0x31010c
172#define MO_CONTR_BRIGHT 0x310110
173#define MO_UV_SATURATION 0x310114
174#define MO_HUE 0x310118
175#define MO_HTOTAL 0x310120
176#define MO_HDELAY_EVEN 0x310124
177#define MO_HDELAY_ODD 0x310128
178#define MO_VDELAY_ODD 0x31012c
179#define MO_VDELAY_EVEN 0x310130
180#define MO_HACTIVE_EVEN 0x31013c
181#define MO_HACTIVE_ODD 0x310140
182#define MO_VACTIVE_EVEN 0x310144
183#define MO_VACTIVE_ODD 0x310148
184#define MO_HSCALE_EVEN 0x31014c
185#define MO_HSCALE_ODD 0x310150
186#define MO_VSCALE_EVEN 0x310154
187#define MO_FILTER_EVEN 0x31015c
188#define MO_VSCALE_ODD 0x310158
189#define MO_FILTER_ODD 0x310160
190#define MO_OUTPUT_FORMAT 0x310164
191
192#define MO_PLL_REG 0x310168 // PLL register
193#define MO_PLL_ADJ_CTRL 0x31016c // PLL adjust control register
194#define MO_SCONV_REG 0x310170 // sample rate conversion register
195#define MO_SCONV_FIFO 0x310174 // sample rate conversion fifo
196#define MO_SUB_STEP 0x310178 // subcarrier step size
197#define MO_SUB_STEP_DR 0x31017c // subcarrier step size for DR line
198
199#define MO_CAPTURE_CTRL 0x310180 // capture control
200#define MO_COLOR_CTRL 0x310184
201#define MO_VBI_PACKET 0x310188 // vbi packet size / delay
202#define MO_FIELD_COUNT 0x310190 // field counter
203#define MO_VIP_CONFIG 0x310194
204#define MO_VBOS_CONTROL 0x3101a8
205
206#define MO_AGC_BACK_VBI 0x310200
207#define MO_AGC_SYNC_TIP1 0x310208
208
209#define MO_VIDY_GPCNT 0x31C020 // {16}RO Video Y general purpose counter
210#define MO_VIDU_GPCNT 0x31C024 // {16}RO Video U general purpose counter
211#define MO_VIDV_GPCNT 0x31C028 // {16}RO Video V general purpose counter
212#define MO_VBI_GPCNT 0x31C02C // {16}RO VBI general purpose counter
213#define MO_VIDY_GPCNTRL 0x31C030 // {2}WO Video Y general purpose control
214#define MO_VIDU_GPCNTRL 0x31C034 // {2}WO Video U general purpose control
215#define MO_VIDV_GPCNTRL 0x31C038 // {2}WO Video V general purpose control
216#define MO_VBI_GPCNTRL 0x31C03C // {2}WO VBI general purpose counter
217#define MO_VID_DMACNTRL 0x31C040 // {8}RW Video DMA control
218#define MO_VID_XFR_STAT 0x31C044 // {1}RO Video transfer status
219
220
221/* ---------------------------------------------------------------------- */
222/* audio registers */
223
224#define MO_AUDD_DMA 0x320000 // {64}RWp Audio downstream
225#define MO_AUDU_DMA 0x320008 // {64}RWp Audio upstream
226#define MO_AUDR_DMA 0x320010 // {64}RWp Audio RDS (downstream)
227#define MO_AUDD_GPCNT 0x32C020 // {16}RO Audio down general purpose counter
228#define MO_AUDU_GPCNT 0x32C024 // {16}RO Audio up general purpose counter
229#define MO_AUDR_GPCNT 0x32C028 // {16}RO Audio RDS general purpose counter
230#define MO_AUDD_GPCNTRL 0x32C030 // {2}WO Audio down general purpose control
231#define MO_AUDU_GPCNTRL 0x32C034 // {2}WO Audio up general purpose control
232#define MO_AUDR_GPCNTRL 0x32C038 // {2}WO Audio RDS general purpose control
233#define MO_AUD_DMACNTRL 0x32C040 // {6}RW Audio DMA control
234#define MO_AUD_XFR_STAT 0x32C044 // {1}RO Audio transfer status
235#define MO_AUDD_LNGTH 0x32C048 // {12}RW Audio down line length
236#define MO_AUDR_LNGTH 0x32C04C // {12}RW Audio RDS line length
237
238#define AUD_INIT 0x320100
239#define AUD_INIT_LD 0x320104
240#define AUD_SOFT_RESET 0x320108
241#define AUD_I2SINPUTCNTL 0x320120
242#define AUD_BAUDRATE 0x320124
243#define AUD_I2SOUTPUTCNTL 0x320128
244#define AAGC_HYST 0x320134
245#define AAGC_GAIN 0x320138
246#define AAGC_DEF 0x32013c
247#define AUD_IIR1_0_SEL 0x320150
248#define AUD_IIR1_0_SHIFT 0x320154
249#define AUD_IIR1_1_SEL 0x320158
250#define AUD_IIR1_1_SHIFT 0x32015c
251#define AUD_IIR1_2_SEL 0x320160
252#define AUD_IIR1_2_SHIFT 0x320164
253#define AUD_IIR1_3_SEL 0x320168
254#define AUD_IIR1_3_SHIFT 0x32016c
255#define AUD_IIR1_4_SEL 0x320170
256#define AUD_IIR1_4_SHIFT 0x32017c
257#define AUD_IIR1_5_SEL 0x320180
258#define AUD_IIR1_5_SHIFT 0x320184
259#define AUD_IIR2_0_SEL 0x320190
260#define AUD_IIR2_0_SHIFT 0x320194
261#define AUD_IIR2_1_SEL 0x320198
262#define AUD_IIR2_1_SHIFT 0x32019c
263#define AUD_IIR2_2_SEL 0x3201a0
264#define AUD_IIR2_2_SHIFT 0x3201a4
265#define AUD_IIR2_3_SEL 0x3201a8
266#define AUD_IIR2_3_SHIFT 0x3201ac
267#define AUD_IIR3_0_SEL 0x3201c0
268#define AUD_IIR3_0_SHIFT 0x3201c4
269#define AUD_IIR3_1_SEL 0x3201c8
270#define AUD_IIR3_1_SHIFT 0x3201cc
271#define AUD_IIR3_2_SEL 0x3201d0
272#define AUD_IIR3_2_SHIFT 0x3201d4
273#define AUD_IIR4_0_SEL 0x3201e0
274#define AUD_IIR4_0_SHIFT 0x3201e4
275#define AUD_IIR4_1_SEL 0x3201e8
276#define AUD_IIR4_1_SHIFT 0x3201ec
277#define AUD_IIR4_2_SEL 0x3201f0
278#define AUD_IIR4_2_SHIFT 0x3201f4
279#define AUD_IIR4_0_CA0 0x320200
280#define AUD_IIR4_0_CA1 0x320204
281#define AUD_IIR4_0_CA2 0x320208
282#define AUD_IIR4_0_CB0 0x32020c
283#define AUD_IIR4_0_CB1 0x320210
284#define AUD_IIR4_1_CA0 0x320214
285#define AUD_IIR4_1_CA1 0x320218
286#define AUD_IIR4_1_CA2 0x32021c
287#define AUD_IIR4_1_CB0 0x320220
288#define AUD_IIR4_1_CB1 0x320224
289#define AUD_IIR4_2_CA0 0x320228
290#define AUD_IIR4_2_CA1 0x32022c
291#define AUD_IIR4_2_CA2 0x320230
292#define AUD_IIR4_2_CB0 0x320234
293#define AUD_IIR4_2_CB1 0x320238
294#define AUD_HP_MD_IIR4_1 0x320250
295#define AUD_HP_PROG_IIR4_1 0x320254
296#define AUD_FM_MODE_ENABLE 0x320258
297#define AUD_POLY0_DDS_CONSTANT 0x320270
298#define AUD_DN0_FREQ 0x320274
299#define AUD_DN1_FREQ 0x320278
300#define AUD_DN1_FREQ_SHIFT 0x32027c
301#define AUD_DN1_AFC 0x320280
302#define AUD_DN1_SRC_SEL 0x320284
303#define AUD_DN1_SHFT 0x320288
304#define AUD_DN2_FREQ 0x32028c
305#define AUD_DN2_FREQ_SHIFT 0x320290
306#define AUD_DN2_AFC 0x320294
307#define AUD_DN2_SRC_SEL 0x320298
308#define AUD_DN2_SHFT 0x32029c
309#define AUD_CRDC0_SRC_SEL 0x320300
310#define AUD_CRDC0_SHIFT 0x320304
311#define AUD_CORDIC_SHIFT_0 0x320308
312#define AUD_CRDC1_SRC_SEL 0x32030c
313#define AUD_CRDC1_SHIFT 0x320310
314#define AUD_CORDIC_SHIFT_1 0x320314
315#define AUD_DCOC_0_SRC 0x320320
316#define AUD_DCOC0_SHIFT 0x320324
317#define AUD_DCOC_0_SHIFT_IN0 0x320328
318#define AUD_DCOC_0_SHIFT_IN1 0x32032c
319#define AUD_DCOC_1_SRC 0x320330
320#define AUD_DCOC1_SHIFT 0x320334
321#define AUD_DCOC_1_SHIFT_IN0 0x320338
322#define AUD_DCOC_1_SHIFT_IN1 0x32033c
323#define AUD_DCOC_2_SRC 0x320340
324#define AUD_DCOC2_SHIFT 0x320344
325#define AUD_DCOC_2_SHIFT_IN0 0x320348
326#define AUD_DCOC_2_SHIFT_IN1 0x32034c
327#define AUD_DCOC_PASS_IN 0x320350
328#define AUD_PDET_SRC 0x320370
329#define AUD_PDET_SHIFT 0x320374
330#define AUD_PILOT_BQD_1_K0 0x320380
331#define AUD_PILOT_BQD_1_K1 0x320384
332#define AUD_PILOT_BQD_1_K2 0x320388
333#define AUD_PILOT_BQD_1_K3 0x32038c
334#define AUD_PILOT_BQD_1_K4 0x320390
335#define AUD_PILOT_BQD_2_K0 0x320394
336#define AUD_PILOT_BQD_2_K1 0x320398
337#define AUD_PILOT_BQD_2_K2 0x32039c
338#define AUD_PILOT_BQD_2_K3 0x3203a0
339#define AUD_PILOT_BQD_2_K4 0x3203a4
340#define AUD_THR_FR 0x3203c0
341#define AUD_X_PROG 0x3203c4
342#define AUD_Y_PROG 0x3203c8
343#define AUD_HARMONIC_MULT 0x3203cc
344#define AUD_C1_UP_THR 0x3203d0
345#define AUD_C1_LO_THR 0x3203d4
346#define AUD_C2_UP_THR 0x3203d8
347#define AUD_C2_LO_THR 0x3203dc
348#define AUD_PLL_EN 0x320400
349#define AUD_PLL_SRC 0x320404
350#define AUD_PLL_SHIFT 0x320408
351#define AUD_PLL_IF_SEL 0x32040c
352#define AUD_PLL_IF_SHIFT 0x320410
353#define AUD_BIQUAD_PLL_K0 0x320414
354#define AUD_BIQUAD_PLL_K1 0x320418
355#define AUD_BIQUAD_PLL_K2 0x32041c
356#define AUD_BIQUAD_PLL_K3 0x320420
357#define AUD_BIQUAD_PLL_K4 0x320424
358#define AUD_DEEMPH0_SRC_SEL 0x320440
359#define AUD_DEEMPH0_SHIFT 0x320444
360#define AUD_DEEMPH0_G0 0x320448
361#define AUD_DEEMPH0_A0 0x32044c
362#define AUD_DEEMPH0_B0 0x320450
363#define AUD_DEEMPH0_A1 0x320454
364#define AUD_DEEMPH0_B1 0x320458
365#define AUD_DEEMPH1_SRC_SEL 0x32045c
366#define AUD_DEEMPH1_SHIFT 0x320460
367#define AUD_DEEMPH1_G0 0x320464
368#define AUD_DEEMPH1_A0 0x320468
369#define AUD_DEEMPH1_B0 0x32046c
370#define AUD_DEEMPH1_A1 0x320470
371#define AUD_DEEMPH1_B1 0x320474
372#define AUD_OUT0_SEL 0x320490
373#define AUD_OUT0_SHIFT 0x320494
374#define AUD_OUT1_SEL 0x320498
375#define AUD_OUT1_SHIFT 0x32049c
376#define AUD_RDSI_SEL 0x3204a0
377#define AUD_RDSI_SHIFT 0x3204a4
378#define AUD_RDSQ_SEL 0x3204a8
379#define AUD_RDSQ_SHIFT 0x3204ac
380#define AUD_DBX_IN_GAIN 0x320500
381#define AUD_DBX_WBE_GAIN 0x320504
382#define AUD_DBX_SE_GAIN 0x320508
383#define AUD_DBX_RMS_WBE 0x32050c
384#define AUD_DBX_RMS_SE 0x320510
385#define AUD_DBX_SE_BYPASS 0x320514
386#define AUD_FAWDETCTL 0x320530
387#define AUD_FAWDETWINCTL 0x320534
388#define AUD_DEEMPHGAIN_R 0x320538
389#define AUD_DEEMPHNUMER1_R 0x32053c
390#define AUD_DEEMPHNUMER2_R 0x320540
391#define AUD_DEEMPHDENOM1_R 0x320544
392#define AUD_DEEMPHDENOM2_R 0x320548
393#define AUD_ERRLOGPERIOD_R 0x32054c
394#define AUD_ERRINTRPTTHSHLD1_R 0x320550
395#define AUD_ERRINTRPTTHSHLD2_R 0x320554
396#define AUD_ERRINTRPTTHSHLD3_R 0x320558
397#define AUD_NICAM_STATUS1 0x32055c
398#define AUD_NICAM_STATUS2 0x320560
399#define AUD_ERRLOG1 0x320564
400#define AUD_ERRLOG2 0x320568
401#define AUD_ERRLOG3 0x32056c
402#define AUD_DAC_BYPASS_L 0x320580
403#define AUD_DAC_BYPASS_R 0x320584
404#define AUD_DAC_BYPASS_CTL 0x320588
405#define AUD_CTL 0x32058c
406#define AUD_STATUS 0x320590
407#define AUD_VOL_CTL 0x320594
408#define AUD_BAL_CTL 0x320598
409#define AUD_START_TIMER 0x3205b0
410#define AUD_MODE_CHG_TIMER 0x3205b4
411#define AUD_POLYPH80SCALEFAC 0x3205b8
412#define AUD_DMD_RA_DDS 0x3205bc
413#define AUD_I2S_RA_DDS 0x3205c0
414#define AUD_RATE_THRES_DMD 0x3205d0
415#define AUD_RATE_THRES_I2S 0x3205d4
416#define AUD_RATE_ADJ1 0x3205d8
417#define AUD_RATE_ADJ2 0x3205dc
418#define AUD_RATE_ADJ3 0x3205e0
419#define AUD_RATE_ADJ4 0x3205e4
420#define AUD_RATE_ADJ5 0x3205e8
421#define AUD_APB_IN_RATE_ADJ 0x3205ec
422#define AUD_I2SCNTL 0x3205ec
423#define AUD_PHASE_FIX_CTL 0x3205f0
424#define AUD_PLL_PRESCALE 0x320600
425#define AUD_PLL_DDS 0x320604
426#define AUD_PLL_INT 0x320608
427#define AUD_PLL_FRAC 0x32060c
428#define AUD_PLL_JTAG 0x320620
429#define AUD_PLL_SPMP 0x320624
430#define AUD_AFE_12DB_EN 0x320628
431
432// Audio QAM Register Addresses
433#define AUD_PDF_DDS_CNST_BYTE2 0x320d01
434#define AUD_PDF_DDS_CNST_BYTE1 0x320d02
435#define AUD_PDF_DDS_CNST_BYTE0 0x320d03
436#define AUD_PHACC_FREQ_8MSB 0x320d2a
437#define AUD_PHACC_FREQ_8LSB 0x320d2b
438#define AUD_QAM_MODE 0x320d04
439
440
441/* ---------------------------------------------------------------------- */
442/* transport stream registers */
443
444#define MO_TS_DMA 0x330000 // {64}RWp Transport stream downstream
445#define MO_TS_GPCNT 0x33C020 // {16}RO TS general purpose counter
446#define MO_TS_GPCNTRL 0x33C030 // {2}WO TS general purpose control
447#define MO_TS_DMACNTRL 0x33C040 // {6}RW TS DMA control
448#define MO_TS_XFR_STAT 0x33C044 // {1}RO TS transfer status
449#define MO_TS_LNGTH 0x33C048 // {12}RW TS line length
450
451#define TS_HW_SOP_CNTRL 0x33C04C
452#define TS_GEN_CNTRL 0x33C050
453#define TS_BD_PKT_STAT 0x33C054
454#define TS_SOP_STAT 0x33C058
455#define TS_FIFO_OVFL_STAT 0x33C05C
456#define TS_VALERR_CNTRL 0x33C060
457
458
459/* ---------------------------------------------------------------------- */
460/* VIP registers */
461
462#define MO_VIPD_DMA 0x340000 // {64}RWp VIP downstream
463#define MO_VIPU_DMA 0x340008 // {64}RWp VIP upstream
464#define MO_VIPD_GPCNT 0x34C020 // {16}RO VIP down general purpose counter
465#define MO_VIPU_GPCNT 0x34C024 // {16}RO VIP up general purpose counter
466#define MO_VIPD_GPCNTRL 0x34C030 // {2}WO VIP down general purpose control
467#define MO_VIPU_GPCNTRL 0x34C034 // {2}WO VIP up general purpose control
468#define MO_VIP_DMACNTRL 0x34C040 // {6}RW VIP DMA control
469#define MO_VIP_XFR_STAT 0x34C044 // {1}RO VIP transfer status
470#define MO_VIP_CFG 0x340048 // VIP configuration
471#define MO_VIPU_CNTRL 0x34004C // VIP upstream control #1
472#define MO_VIPD_CNTRL 0x340050 // VIP downstream control #2
473#define MO_VIPD_LNGTH 0x340054 // VIP downstream line length
474#define MO_VIP_BRSTLN 0x340058 // VIP burst length
475#define MO_VIP_INTCNTRL 0x34C05C // VIP Interrupt Control
476#define MO_VIP_XFTERM 0x340060 // VIP transfer terminate
477
478
479/* ---------------------------------------------------------------------- */
480/* misc registers */
481
482#define MO_M2M_DMA 0x350000 // {64}RWp Mem2Mem DMA Bfr
483#define MO_GP0_IO 0x350010 // {32}RW* GPIOoutput enablesdata I/O
484#define MO_GP1_IO 0x350014 // {32}RW* GPIOoutput enablesdata I/O
485#define MO_GP2_IO 0x350018 // {32}RW* GPIOoutput enablesdata I/O
486#define MO_GP3_IO 0x35001C // {32}RW* GPIO Mode/Ctrloutput enables
487#define MO_GPIO 0x350020 // {32}RW* GPIO I2C Ctrldata I/O
488#define MO_GPOE 0x350024 // {32}RW GPIO I2C Ctrloutput enables
489#define MO_GP_ISM 0x350028 // {16}WO GPIO Intr Sens/Pol
490
491#define MO_PLL_B 0x35C008 // {32}RW* PLL Control for ASB bus clks
492#define MO_M2M_CNT 0x35C024 // {32}RW Mem2Mem DMA Cnt
493#define MO_M2M_XSUM 0x35C028 // {32}RO M2M XOR-Checksum
494#define MO_CRC 0x35C02C // {16}RW CRC16 init/result
495#define MO_CRC_D 0x35C030 // {32}WO CRC16 new data in
496#define MO_TM_CNT_LDW 0x35C034 // {32}RO Timer : Counter low dword
497#define MO_TM_CNT_UW 0x35C038 // {16}RO Timer : Counter high word
498#define MO_TM_LMT_LDW 0x35C03C // {32}RW Timer : Limit low dword
499#define MO_TM_LMT_UW 0x35C040 // {32}RW Timer : Limit high word
500#define MO_PINMUX_IO 0x35C044 // {8}RW Pin Mux Control
501#define MO_TSTSEL_IO 0x35C048 // {2}RW Pin Mux Control
502#define MO_AFECFG_IO 0x35C04C // AFE configuration reg
503#define MO_DDS_IO 0x35C050 // DDS Increment reg
504#define MO_DDSCFG_IO 0x35C054 // DDS Configuration reg
505#define MO_SAMPLE_IO 0x35C058 // IRIn sample reg
506#define MO_SRST_IO 0x35C05C // Output system reset reg
507
508#define MO_INT1_MSK 0x35C060 // DMA RISC interrupt mask
509#define MO_INT1_STAT 0x35C064 // DMA RISC interrupt status
510#define MO_INT1_MSTAT 0x35C068 // DMA RISC interrupt masked status
511
512
513/* ---------------------------------------------------------------------- */
514/* i2c bus registers */
515
516#define MO_I2C 0x368000 // I2C data/control
517#define MO_I2C_DIV (0xf<<4)
518#define MO_I2C_SYNC (1<<3)
519#define MO_I2C_W3B (1<<2)
520#define MO_I2C_SCL (1<<1)
521#define MO_I2C_SDA (1<<0)
522
523
524/* ---------------------------------------------------------------------- */
525/* general purpose host registers */
526/* FIXME: tyops? s/0x35/0x38/ ?? */
527
528#define MO_GPHSTD_DMA 0x350000 // {64}RWp Host downstream
529#define MO_GPHSTU_DMA 0x350008 // {64}RWp Host upstream
530#define MO_GPHSTU_CNTRL 0x380048 // Host upstream control #1
531#define MO_GPHSTD_CNTRL 0x38004C // Host downstream control #2
532#define MO_GPHSTD_LNGTH 0x380050 // Host downstream line length
533#define MO_GPHST_WSC 0x380054 // Host wait state control
534#define MO_GPHST_XFR 0x380058 // Host transfer control
535#define MO_GPHST_WDTH 0x38005C // Host interface width
536#define MO_GPHST_HDSHK 0x380060 // Host peripheral handshake
537#define MO_GPHST_MUX16 0x380064 // Host muxed 16-bit transfer parameters
538#define MO_GPHST_MODE 0x380068 // Host mode select
539
540#define MO_GPHSTD_GPCNT 0x35C020 // Host down general purpose counter
541#define MO_GPHSTU_GPCNT 0x35C024 // Host up general purpose counter
542#define MO_GPHSTD_GPCNTRL 0x38C030 // Host down general purpose control
543#define MO_GPHSTU_GPCNTRL 0x38C034 // Host up general purpose control
544#define MO_GPHST_DMACNTRL 0x38C040 // Host DMA control
545#define MO_GPHST_XFR_STAT 0x38C044 // Host transfer status
546#define MO_GPHST_SOFT_RST 0x38C06C // Host software reset
547
548
549/* ---------------------------------------------------------------------- */
550/* RISC instructions */
551
552#define RISC_SYNC 0x80000000
553#define RISC_SYNC_ODD 0x80000000
554#define RISC_SYNC_EVEN 0x80000200
555#define RISC_RESYNC 0x80008000
556#define RISC_RESYNC_ODD 0x80008000
557#define RISC_RESYNC_EVEN 0x80008200
558#define RISC_WRITE 0x10000000
559#define RISC_WRITEC 0x50000000
560#define RISC_READ 0x90000000
561#define RISC_READC 0xA0000000
562#define RISC_JUMP 0x70000000
563#define RISC_SKIP 0x20000000
564#define RISC_WRITERM 0xB0000000
565#define RISC_WRITECM 0xC0000000
566#define RISC_WRITECR 0xD0000000
567#define RISC_IMM 0x00000001
568
569#define RISC_SOL 0x08000000
570#define RISC_EOL 0x04000000
571
572#define RISC_IRQ2 0x02000000
573#define RISC_IRQ1 0x01000000
574
575#define RISC_CNT_NONE 0x00000000
576#define RISC_CNT_INC 0x00010000
577#define RISC_CNT_RSVR 0x00020000
578#define RISC_CNT_RESET 0x00030000
579#define RISC_JMP_SRP 0x01
580
581
582/* ---------------------------------------------------------------------- */
583/* various constants */
584
585// DMA
586/* Interrupt mask/status */
587#define PCI_INT_VIDINT (1 << 0)
588#define PCI_INT_AUDINT (1 << 1)
589#define PCI_INT_TSINT (1 << 2)
590#define PCI_INT_VIPINT (1 << 3)
591#define PCI_INT_HSTINT (1 << 4)
592#define PCI_INT_TM1INT (1 << 5)
593#define PCI_INT_SRCDMAINT (1 << 6)
594#define PCI_INT_DSTDMAINT (1 << 7)
595#define PCI_INT_RISC_RD_BERRINT (1 << 10)
596#define PCI_INT_RISC_WR_BERRINT (1 << 11)
597#define PCI_INT_BRDG_BERRINT (1 << 12)
598#define PCI_INT_SRC_DMA_BERRINT (1 << 13)
599#define PCI_INT_DST_DMA_BERRINT (1 << 14)
600#define PCI_INT_IPB_DMA_BERRINT (1 << 15)
601#define PCI_INT_I2CDONE (1 << 16)
602#define PCI_INT_I2CRACK (1 << 17)
603#define PCI_INT_IR_SMPINT (1 << 18)
604#define PCI_INT_GPIO_INT0 (1 << 19)
605#define PCI_INT_GPIO_INT1 (1 << 20)
606
607#define SEL_BTSC 0x01
608#define SEL_EIAJ 0x02
609#define SEL_A2 0x04
610#define SEL_SAP 0x08
611#define SEL_NICAM 0x10
612#define SEL_FMRADIO 0x20
613
614// AUD_CTL
615#define AUD_INT_DN_RISCI1 (1 << 0)
616#define AUD_INT_UP_RISCI1 (1 << 1)
617#define AUD_INT_RDS_DN_RISCI1 (1 << 2)
618#define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
619#define AUD_INT_UP_RISCI2 (1 << 5)
620#define AUD_INT_RDS_DN_RISCI2 (1 << 6)
621#define AUD_INT_DN_SYNC (1 << 12)
622#define AUD_INT_UP_SYNC (1 << 13)
623#define AUD_INT_RDS_DN_SYNC (1 << 14)
624#define AUD_INT_OPC_ERR (1 << 16)
625#define AUD_INT_BER_IRQ (1 << 20)
626#define AUD_INT_MCHG_IRQ (1 << 21)
627
628#define EN_BTSC_FORCE_MONO 0
629#define EN_BTSC_FORCE_STEREO 1
630#define EN_BTSC_FORCE_SAP 2
631#define EN_BTSC_AUTO_STEREO 3
632#define EN_BTSC_AUTO_SAP 4
633
634#define EN_A2_FORCE_MONO1 8
635#define EN_A2_FORCE_MONO2 9
636#define EN_A2_FORCE_STEREO 10
637#define EN_A2_AUTO_MONO2 11
638#define EN_A2_AUTO_STEREO 12
639
640#define EN_EIAJ_FORCE_MONO1 16
641#define EN_EIAJ_FORCE_MONO2 17
642#define EN_EIAJ_FORCE_STEREO 18
643#define EN_EIAJ_AUTO_MONO2 19
644#define EN_EIAJ_AUTO_STEREO 20
645
646#define EN_NICAM_FORCE_MONO1 32
647#define EN_NICAM_FORCE_MONO2 33
648#define EN_NICAM_FORCE_STEREO 34
649#define EN_NICAM_AUTO_MONO2 35
650#define EN_NICAM_AUTO_STEREO 36
651
652#define EN_FMRADIO_FORCE_MONO 24
653#define EN_FMRADIO_FORCE_STEREO 25
654#define EN_FMRADIO_AUTO_STEREO 26
655
656#define EN_NICAM_AUTO_FALLBACK 0x00000040
657#define EN_FMRADIO_EN_RDS 0x00000200
658#define EN_NICAM_TRY_AGAIN_BIT 0x00000400
659#define EN_DAC_ENABLE 0x00001000
660#define EN_I2SOUT_ENABLE 0x00002000
661#define EN_I2SIN_STR2DAC 0x00004000
662#define EN_I2SIN_ENABLE 0x00008000
663
664#define EN_DMTRX_SUMDIFF (0 << 7)
665#define EN_DMTRX_SUMR (1 << 7)
666#define EN_DMTRX_LR (2 << 7)
667#define EN_DMTRX_MONO (3 << 7)
668#define EN_DMTRX_BYPASS (1 << 11)
669
670// Video
671#define VID_CAPTURE_CONTROL 0x310180
672
673#define CX23880_CAP_CTL_CAPTURE_VBI_ODD (1<<3)
674#define CX23880_CAP_CTL_CAPTURE_VBI_EVEN (1<<2)
675#define CX23880_CAP_CTL_CAPTURE_ODD (1<<1)
676#define CX23880_CAP_CTL_CAPTURE_EVEN (1<<0)
677
678#define VideoInputMux0 0x0
679#define VideoInputMux1 0x1
680#define VideoInputMux2 0x2
681#define VideoInputMux3 0x3
682#define VideoInputTuner 0x0
683#define VideoInputComposite 0x1
684#define VideoInputSVideo 0x2
685#define VideoInputOther 0x3
686
687#define Xtal0 0x1
688#define Xtal1 0x2
689#define XtalAuto 0x3
690
691#define VideoFormatAuto 0x0
692#define VideoFormatNTSC 0x1
693#define VideoFormatNTSCJapan 0x2
694#define VideoFormatNTSC443 0x3
695#define VideoFormatPAL 0x4
696#define VideoFormatPALB 0x4
697#define VideoFormatPALD 0x4
698#define VideoFormatPALG 0x4
699#define VideoFormatPALH 0x4
700#define VideoFormatPALI 0x4
701#define VideoFormatPALBDGHI 0x4
702#define VideoFormatPALM 0x5
703#define VideoFormatPALN 0x6
704#define VideoFormatPALNC 0x7
705#define VideoFormatPAL60 0x8
706#define VideoFormatSECAM 0x9
707
708#define VideoFormatAuto27MHz 0x10
709#define VideoFormatNTSC27MHz 0x11
710#define VideoFormatNTSCJapan27MHz 0x12
711#define VideoFormatNTSC44327MHz 0x13
712#define VideoFormatPAL27MHz 0x14
713#define VideoFormatPALB27MHz 0x14
714#define VideoFormatPALD27MHz 0x14
715#define VideoFormatPALG27MHz 0x14
716#define VideoFormatPALH27MHz 0x14
717#define VideoFormatPALI27MHz 0x14
718#define VideoFormatPALBDGHI27MHz 0x14
719#define VideoFormatPALM27MHz 0x15
720#define VideoFormatPALN27MHz 0x16
721#define VideoFormatPALNC27MHz 0x17
722#define VideoFormatPAL6027MHz 0x18
723#define VideoFormatSECAM27MHz 0x19
724
725#define NominalUSECAM 0x87
726#define NominalVSECAM 0x85
727#define NominalUNTSC 0xFE
728#define NominalVNTSC 0xB4
729
730#define NominalContrast 0xD8
731
732#define HFilterAutoFormat 0x0
733#define HFilterCIF 0x1
734#define HFilterQCIF 0x2
735#define HFilterICON 0x3
736
737#define VFilter2TapInterpolate 0
738#define VFilter3TapInterpolate 1
739#define VFilter4TapInterpolate 2
740#define VFilter5TapInterpolate 3
741#define VFilter2TapNoInterpolate 4
742#define VFilter3TapNoInterpolate 5
743#define VFilter4TapNoInterpolate 6
744#define VFilter5TapNoInterpolate 7
745
746#define ColorFormatRGB32 0x0000
747#define ColorFormatRGB24 0x0011
748#define ColorFormatRGB16 0x0022
749#define ColorFormatRGB15 0x0033
750#define ColorFormatYUY2 0x0044
751#define ColorFormatBTYUV 0x0055
752#define ColorFormatY8 0x0066
753#define ColorFormatRGB8 0x0077
754#define ColorFormatPL422 0x0088
755#define ColorFormatPL411 0x0099
756#define ColorFormatYUV12 0x00AA
757#define ColorFormatYUV9 0x00BB
758#define ColorFormatRAW 0x00EE
759#define ColorFormatBSWAP 0x0300
760#define ColorFormatWSWAP 0x0c00
761#define ColorFormatEvenMask 0x050f
762#define ColorFormatOddMask 0x0af0
763#define ColorFormatGamma 0x1000
764
765#define Interlaced 0x1
766#define NonInterlaced 0x0
767
768#define FieldEven 0x1
769#define FieldOdd 0x0
770
771#define TGReadWriteMode 0x0
772#define TGEnableMode 0x1
773
774#define DV_CbAlign 0x0
775#define DV_Y0Align 0x1
776#define DV_CrAlign 0x2
777#define DV_Y1Align 0x3
778
779#define DVF_Analog 0x0
780#define DVF_CCIR656 0x1
781#define DVF_ByteStream 0x2
782#define DVF_ExtVSYNC 0x4
783#define DVF_ExtField 0x5
784
785#define CHANNEL_VID_Y 0x1
786#define CHANNEL_VID_U 0x2
787#define CHANNEL_VID_V 0x3
788#define CHANNEL_VID_VBI 0x4
789#define CHANNEL_AUD_DN 0x5
790#define CHANNEL_AUD_UP 0x6
791#define CHANNEL_AUD_RDS_DN 0x7
792#define CHANNEL_MPEG_DN 0x8
793#define CHANNEL_VIP_DN 0x9
794#define CHANNEL_VIP_UP 0xA
795#define CHANNEL_HOST_DN 0xB
796#define CHANNEL_HOST_UP 0xC
797#define CHANNEL_FIRST 0x1
798#define CHANNEL_LAST 0xC
799
800#define GP_COUNT_CONTROL_NONE 0x0
801#define GP_COUNT_CONTROL_INC 0x1
802#define GP_COUNT_CONTROL_RESERVED 0x2
803#define GP_COUNT_CONTROL_RESET 0x3
804
805#define PLL_PRESCALE_BY_2 2
806#define PLL_PRESCALE_BY_3 3
807#define PLL_PRESCALE_BY_4 4
808#define PLL_PRESCALE_BY_5 5
809
810#define HLNotchFilter4xFsc 0
811#define HLNotchFilterSquare 1
812#define HLNotchFilter135NTSC 2
813#define HLNotchFilter135PAL 3
814
815#define NTSC_8x_SUB_CARRIER 28.63636E6
816#define PAL_8x_SUB_CARRIER 35.46895E6
817
818// Default analog settings
819#define DEFAULT_HUE_NTSC 0x00
820#define DEFAULT_BRIGHTNESS_NTSC 0x00
821#define DEFAULT_CONTRAST_NTSC 0x39
822#define DEFAULT_SAT_U_NTSC 0x7F
823#define DEFAULT_SAT_V_NTSC 0x5A
824
825typedef enum
826{
827 SOURCE_TUNER = 0,
828 SOURCE_COMPOSITE,
829 SOURCE_SVIDEO,
830 SOURCE_OTHER1,
831 SOURCE_OTHER2,
832 SOURCE_COMPVIASVIDEO,
833 SOURCE_CCIR656
834} VIDEOSOURCETYPE;
835
836#endif /* _CX88_REG_H_ */
diff --git a/drivers/media/pci/cx88/cx88-tvaudio.c b/drivers/media/pci/cx88/cx88-tvaudio.c
new file mode 100644
index 000000000000..770ec05b5e9b
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-tvaudio.c
@@ -0,0 +1,1059 @@
1/*
2
3 cx88x-audio.c - Conexant CX23880/23881 audio downstream driver driver
4
5 (c) 2001 Michael Eskin, Tom Zakrajsek [Windows version]
6 (c) 2002 Yurij Sysoev <yurij@naturesoft.net>
7 (c) 2003 Gerd Knorr <kraxel@bytesex.org>
8
9 -----------------------------------------------------------------------
10
11 Lot of voodoo here. Even the data sheet doesn't help to
12 understand what is going on here, the documentation for the audio
13 part of the cx2388x chip is *very* bad.
14
15 Some of this comes from party done linux driver sources I got from
16 [undocumented].
17
18 Some comes from the dscaler sources, one of the dscaler driver guy works
19 for Conexant ...
20
21 -----------------------------------------------------------------------
22
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 2 of the License, or
26 (at your option) any later version.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36*/
37
38#include <linux/module.h>
39#include <linux/errno.h>
40#include <linux/freezer.h>
41#include <linux/kernel.h>
42#include <linux/mm.h>
43#include <linux/poll.h>
44#include <linux/signal.h>
45#include <linux/ioport.h>
46#include <linux/types.h>
47#include <linux/interrupt.h>
48#include <linux/vmalloc.h>
49#include <linux/init.h>
50#include <linux/delay.h>
51#include <linux/kthread.h>
52
53#include "cx88.h"
54
55static unsigned int audio_debug;
56module_param(audio_debug, int, 0644);
57MODULE_PARM_DESC(audio_debug, "enable debug messages [audio]");
58
59static unsigned int always_analog;
60module_param(always_analog,int,0644);
61MODULE_PARM_DESC(always_analog,"force analog audio out");
62
63static unsigned int radio_deemphasis;
64module_param(radio_deemphasis,int,0644);
65MODULE_PARM_DESC(radio_deemphasis, "Radio deemphasis time constant, "
66 "0=None, 1=50us (elsewhere), 2=75us (USA)");
67
68#define dprintk(fmt, arg...) if (audio_debug) \
69 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
70
71/* ----------------------------------------------------------- */
72
73static const char * const aud_ctl_names[64] = {
74 [EN_BTSC_FORCE_MONO] = "BTSC_FORCE_MONO",
75 [EN_BTSC_FORCE_STEREO] = "BTSC_FORCE_STEREO",
76 [EN_BTSC_FORCE_SAP] = "BTSC_FORCE_SAP",
77 [EN_BTSC_AUTO_STEREO] = "BTSC_AUTO_STEREO",
78 [EN_BTSC_AUTO_SAP] = "BTSC_AUTO_SAP",
79 [EN_A2_FORCE_MONO1] = "A2_FORCE_MONO1",
80 [EN_A2_FORCE_MONO2] = "A2_FORCE_MONO2",
81 [EN_A2_FORCE_STEREO] = "A2_FORCE_STEREO",
82 [EN_A2_AUTO_MONO2] = "A2_AUTO_MONO2",
83 [EN_A2_AUTO_STEREO] = "A2_AUTO_STEREO",
84 [EN_EIAJ_FORCE_MONO1] = "EIAJ_FORCE_MONO1",
85 [EN_EIAJ_FORCE_MONO2] = "EIAJ_FORCE_MONO2",
86 [EN_EIAJ_FORCE_STEREO] = "EIAJ_FORCE_STEREO",
87 [EN_EIAJ_AUTO_MONO2] = "EIAJ_AUTO_MONO2",
88 [EN_EIAJ_AUTO_STEREO] = "EIAJ_AUTO_STEREO",
89 [EN_NICAM_FORCE_MONO1] = "NICAM_FORCE_MONO1",
90 [EN_NICAM_FORCE_MONO2] = "NICAM_FORCE_MONO2",
91 [EN_NICAM_FORCE_STEREO] = "NICAM_FORCE_STEREO",
92 [EN_NICAM_AUTO_MONO2] = "NICAM_AUTO_MONO2",
93 [EN_NICAM_AUTO_STEREO] = "NICAM_AUTO_STEREO",
94 [EN_FMRADIO_FORCE_MONO] = "FMRADIO_FORCE_MONO",
95 [EN_FMRADIO_FORCE_STEREO] = "FMRADIO_FORCE_STEREO",
96 [EN_FMRADIO_AUTO_STEREO] = "FMRADIO_AUTO_STEREO",
97};
98
99struct rlist {
100 u32 reg;
101 u32 val;
102};
103
104static void set_audio_registers(struct cx88_core *core, const struct rlist *l)
105{
106 int i;
107
108 for (i = 0; l[i].reg; i++) {
109 switch (l[i].reg) {
110 case AUD_PDF_DDS_CNST_BYTE2:
111 case AUD_PDF_DDS_CNST_BYTE1:
112 case AUD_PDF_DDS_CNST_BYTE0:
113 case AUD_QAM_MODE:
114 case AUD_PHACC_FREQ_8MSB:
115 case AUD_PHACC_FREQ_8LSB:
116 cx_writeb(l[i].reg, l[i].val);
117 break;
118 default:
119 cx_write(l[i].reg, l[i].val);
120 break;
121 }
122 }
123}
124
125static void set_audio_start(struct cx88_core *core, u32 mode)
126{
127 /* mute */
128 cx_write(AUD_VOL_CTL, (1 << 6));
129
130 /* start programming */
131 cx_write(AUD_INIT, mode);
132 cx_write(AUD_INIT_LD, 0x0001);
133 cx_write(AUD_SOFT_RESET, 0x0001);
134}
135
136static void set_audio_finish(struct cx88_core *core, u32 ctl)
137{
138 u32 volume;
139
140 /* restart dma; This avoids buzz in NICAM and is good in others */
141 cx88_stop_audio_dma(core);
142 cx_write(AUD_RATE_THRES_DMD, 0x000000C0);
143 cx88_start_audio_dma(core);
144
145 if (core->board.mpeg & CX88_MPEG_BLACKBIRD) {
146 cx_write(AUD_I2SINPUTCNTL, 4);
147 cx_write(AUD_BAUDRATE, 1);
148 /* 'pass-thru mode': this enables the i2s output to the mpeg encoder */
149 cx_set(AUD_CTL, EN_I2SOUT_ENABLE);
150 cx_write(AUD_I2SOUTPUTCNTL, 1);
151 cx_write(AUD_I2SCNTL, 0);
152 /* cx_write(AUD_APB_IN_RATE_ADJ, 0); */
153 }
154 if ((always_analog) || (!(core->board.mpeg & CX88_MPEG_BLACKBIRD))) {
155 ctl |= EN_DAC_ENABLE;
156 cx_write(AUD_CTL, ctl);
157 }
158
159 /* finish programming */
160 cx_write(AUD_SOFT_RESET, 0x0000);
161
162 /* unmute */
163 volume = cx_sread(SHADOW_AUD_VOL_CTL);
164 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, volume);
165
166 core->last_change = jiffies;
167}
168
169/* ----------------------------------------------------------- */
170
171static void set_audio_standard_BTSC(struct cx88_core *core, unsigned int sap,
172 u32 mode)
173{
174 static const struct rlist btsc[] = {
175 {AUD_AFE_12DB_EN, 0x00000001},
176 {AUD_OUT1_SEL, 0x00000013},
177 {AUD_OUT1_SHIFT, 0x00000000},
178 {AUD_POLY0_DDS_CONSTANT, 0x0012010c},
179 {AUD_DMD_RA_DDS, 0x00c3e7aa},
180 {AUD_DBX_IN_GAIN, 0x00004734},
181 {AUD_DBX_WBE_GAIN, 0x00004640},
182 {AUD_DBX_SE_GAIN, 0x00008d31},
183 {AUD_DCOC_0_SRC, 0x0000001a},
184 {AUD_IIR1_4_SEL, 0x00000021},
185 {AUD_DCOC_PASS_IN, 0x00000003},
186 {AUD_DCOC_0_SHIFT_IN0, 0x0000000a},
187 {AUD_DCOC_0_SHIFT_IN1, 0x00000008},
188 {AUD_DCOC_1_SHIFT_IN0, 0x0000000a},
189 {AUD_DCOC_1_SHIFT_IN1, 0x00000008},
190 {AUD_DN0_FREQ, 0x0000283b},
191 {AUD_DN2_SRC_SEL, 0x00000008},
192 {AUD_DN2_FREQ, 0x00003000},
193 {AUD_DN2_AFC, 0x00000002},
194 {AUD_DN2_SHFT, 0x00000000},
195 {AUD_IIR2_2_SEL, 0x00000020},
196 {AUD_IIR2_2_SHIFT, 0x00000000},
197 {AUD_IIR2_3_SEL, 0x0000001f},
198 {AUD_IIR2_3_SHIFT, 0x00000000},
199 {AUD_CRDC1_SRC_SEL, 0x000003ce},
200 {AUD_CRDC1_SHIFT, 0x00000000},
201 {AUD_CORDIC_SHIFT_1, 0x00000007},
202 {AUD_DCOC_1_SRC, 0x0000001b},
203 {AUD_DCOC1_SHIFT, 0x00000000},
204 {AUD_RDSI_SEL, 0x00000008},
205 {AUD_RDSQ_SEL, 0x00000008},
206 {AUD_RDSI_SHIFT, 0x00000000},
207 {AUD_RDSQ_SHIFT, 0x00000000},
208 {AUD_POLYPH80SCALEFAC, 0x00000003},
209 { /* end of list */ },
210 };
211 static const struct rlist btsc_sap[] = {
212 {AUD_AFE_12DB_EN, 0x00000001},
213 {AUD_DBX_IN_GAIN, 0x00007200},
214 {AUD_DBX_WBE_GAIN, 0x00006200},
215 {AUD_DBX_SE_GAIN, 0x00006200},
216 {AUD_IIR1_1_SEL, 0x00000000},
217 {AUD_IIR1_3_SEL, 0x00000001},
218 {AUD_DN1_SRC_SEL, 0x00000007},
219 {AUD_IIR1_4_SHIFT, 0x00000006},
220 {AUD_IIR2_1_SHIFT, 0x00000000},
221 {AUD_IIR2_2_SHIFT, 0x00000000},
222 {AUD_IIR3_0_SHIFT, 0x00000000},
223 {AUD_IIR3_1_SHIFT, 0x00000000},
224 {AUD_IIR3_0_SEL, 0x0000000d},
225 {AUD_IIR3_1_SEL, 0x0000000e},
226 {AUD_DEEMPH1_SRC_SEL, 0x00000014},
227 {AUD_DEEMPH1_SHIFT, 0x00000000},
228 {AUD_DEEMPH1_G0, 0x00004000},
229 {AUD_DEEMPH1_A0, 0x00000000},
230 {AUD_DEEMPH1_B0, 0x00000000},
231 {AUD_DEEMPH1_A1, 0x00000000},
232 {AUD_DEEMPH1_B1, 0x00000000},
233 {AUD_OUT0_SEL, 0x0000003f},
234 {AUD_OUT1_SEL, 0x0000003f},
235 {AUD_DN1_AFC, 0x00000002},
236 {AUD_DCOC_0_SHIFT_IN0, 0x0000000a},
237 {AUD_DCOC_0_SHIFT_IN1, 0x00000008},
238 {AUD_DCOC_1_SHIFT_IN0, 0x0000000a},
239 {AUD_DCOC_1_SHIFT_IN1, 0x00000008},
240 {AUD_IIR1_0_SEL, 0x0000001d},
241 {AUD_IIR1_2_SEL, 0x0000001e},
242 {AUD_IIR2_1_SEL, 0x00000002},
243 {AUD_IIR2_2_SEL, 0x00000004},
244 {AUD_IIR3_2_SEL, 0x0000000f},
245 {AUD_DCOC2_SHIFT, 0x00000001},
246 {AUD_IIR3_2_SHIFT, 0x00000001},
247 {AUD_DEEMPH0_SRC_SEL, 0x00000014},
248 {AUD_CORDIC_SHIFT_1, 0x00000006},
249 {AUD_POLY0_DDS_CONSTANT, 0x000e4db2},
250 {AUD_DMD_RA_DDS, 0x00f696e6},
251 {AUD_IIR2_3_SEL, 0x00000025},
252 {AUD_IIR1_4_SEL, 0x00000021},
253 {AUD_DN1_FREQ, 0x0000c965},
254 {AUD_DCOC_PASS_IN, 0x00000003},
255 {AUD_DCOC_0_SRC, 0x0000001a},
256 {AUD_DCOC_1_SRC, 0x0000001b},
257 {AUD_DCOC1_SHIFT, 0x00000000},
258 {AUD_RDSI_SEL, 0x00000009},
259 {AUD_RDSQ_SEL, 0x00000009},
260 {AUD_RDSI_SHIFT, 0x00000000},
261 {AUD_RDSQ_SHIFT, 0x00000000},
262 {AUD_POLYPH80SCALEFAC, 0x00000003},
263 { /* end of list */ },
264 };
265
266 mode |= EN_FMRADIO_EN_RDS;
267
268 if (sap) {
269 dprintk("%s SAP (status: unknown)\n", __func__);
270 set_audio_start(core, SEL_SAP);
271 set_audio_registers(core, btsc_sap);
272 set_audio_finish(core, mode);
273 } else {
274 dprintk("%s (status: known-good)\n", __func__);
275 set_audio_start(core, SEL_BTSC);
276 set_audio_registers(core, btsc);
277 set_audio_finish(core, mode);
278 }
279}
280
281static void set_audio_standard_NICAM(struct cx88_core *core, u32 mode)
282{
283 static const struct rlist nicam_l[] = {
284 {AUD_AFE_12DB_EN, 0x00000001},
285 {AUD_RATE_ADJ1, 0x00000060},
286 {AUD_RATE_ADJ2, 0x000000F9},
287 {AUD_RATE_ADJ3, 0x000001CC},
288 {AUD_RATE_ADJ4, 0x000002B3},
289 {AUD_RATE_ADJ5, 0x00000726},
290 {AUD_DEEMPHDENOM1_R, 0x0000F3D0},
291 {AUD_DEEMPHDENOM2_R, 0x00000000},
292 {AUD_ERRLOGPERIOD_R, 0x00000064},
293 {AUD_ERRINTRPTTHSHLD1_R, 0x00000FFF},
294 {AUD_ERRINTRPTTHSHLD2_R, 0x0000001F},
295 {AUD_ERRINTRPTTHSHLD3_R, 0x0000000F},
296 {AUD_POLYPH80SCALEFAC, 0x00000003},
297 {AUD_DMD_RA_DDS, 0x00C00000},
298 {AUD_PLL_INT, 0x0000001E},
299 {AUD_PLL_DDS, 0x00000000},
300 {AUD_PLL_FRAC, 0x0000E542},
301 {AUD_START_TIMER, 0x00000000},
302 {AUD_DEEMPHNUMER1_R, 0x000353DE},
303 {AUD_DEEMPHNUMER2_R, 0x000001B1},
304 {AUD_PDF_DDS_CNST_BYTE2, 0x06},
305 {AUD_PDF_DDS_CNST_BYTE1, 0x82},
306 {AUD_PDF_DDS_CNST_BYTE0, 0x12},
307 {AUD_QAM_MODE, 0x05},
308 {AUD_PHACC_FREQ_8MSB, 0x34},
309 {AUD_PHACC_FREQ_8LSB, 0x4C},
310 {AUD_DEEMPHGAIN_R, 0x00006680},
311 {AUD_RATE_THRES_DMD, 0x000000C0},
312 { /* end of list */ },
313 };
314
315 static const struct rlist nicam_bgdki_common[] = {
316 {AUD_AFE_12DB_EN, 0x00000001},
317 {AUD_RATE_ADJ1, 0x00000010},
318 {AUD_RATE_ADJ2, 0x00000040},
319 {AUD_RATE_ADJ3, 0x00000100},
320 {AUD_RATE_ADJ4, 0x00000400},
321 {AUD_RATE_ADJ5, 0x00001000},
322 {AUD_ERRLOGPERIOD_R, 0x00000fff},
323 {AUD_ERRINTRPTTHSHLD1_R, 0x000003ff},
324 {AUD_ERRINTRPTTHSHLD2_R, 0x000000ff},
325 {AUD_ERRINTRPTTHSHLD3_R, 0x0000003f},
326 {AUD_POLYPH80SCALEFAC, 0x00000003},
327 {AUD_DEEMPHGAIN_R, 0x000023c2},
328 {AUD_DEEMPHNUMER1_R, 0x0002a7bc},
329 {AUD_DEEMPHNUMER2_R, 0x0003023e},
330 {AUD_DEEMPHDENOM1_R, 0x0000f3d0},
331 {AUD_DEEMPHDENOM2_R, 0x00000000},
332 {AUD_PDF_DDS_CNST_BYTE2, 0x06},
333 {AUD_PDF_DDS_CNST_BYTE1, 0x82},
334 {AUD_QAM_MODE, 0x05},
335 { /* end of list */ },
336 };
337
338 static const struct rlist nicam_i[] = {
339 {AUD_PDF_DDS_CNST_BYTE0, 0x12},
340 {AUD_PHACC_FREQ_8MSB, 0x3a},
341 {AUD_PHACC_FREQ_8LSB, 0x93},
342 { /* end of list */ },
343 };
344
345 static const struct rlist nicam_default[] = {
346 {AUD_PDF_DDS_CNST_BYTE0, 0x16},
347 {AUD_PHACC_FREQ_8MSB, 0x34},
348 {AUD_PHACC_FREQ_8LSB, 0x4c},
349 { /* end of list */ },
350 };
351
352 set_audio_start(core,SEL_NICAM);
353 switch (core->tvaudio) {
354 case WW_L:
355 dprintk("%s SECAM-L NICAM (status: devel)\n", __func__);
356 set_audio_registers(core, nicam_l);
357 break;
358 case WW_I:
359 dprintk("%s PAL-I NICAM (status: known-good)\n", __func__);
360 set_audio_registers(core, nicam_bgdki_common);
361 set_audio_registers(core, nicam_i);
362 break;
363 case WW_NONE:
364 case WW_BTSC:
365 case WW_BG:
366 case WW_DK:
367 case WW_EIAJ:
368 case WW_I2SPT:
369 case WW_FM:
370 case WW_I2SADC:
371 case WW_M:
372 dprintk("%s PAL-BGDK NICAM (status: known-good)\n", __func__);
373 set_audio_registers(core, nicam_bgdki_common);
374 set_audio_registers(core, nicam_default);
375 break;
376 };
377
378 mode |= EN_DMTRX_LR | EN_DMTRX_BYPASS;
379 set_audio_finish(core, mode);
380}
381
382static void set_audio_standard_A2(struct cx88_core *core, u32 mode)
383{
384 static const struct rlist a2_bgdk_common[] = {
385 {AUD_ERRLOGPERIOD_R, 0x00000064},
386 {AUD_ERRINTRPTTHSHLD1_R, 0x00000fff},
387 {AUD_ERRINTRPTTHSHLD2_R, 0x0000001f},
388 {AUD_ERRINTRPTTHSHLD3_R, 0x0000000f},
389 {AUD_PDF_DDS_CNST_BYTE2, 0x06},
390 {AUD_PDF_DDS_CNST_BYTE1, 0x82},
391 {AUD_PDF_DDS_CNST_BYTE0, 0x12},
392 {AUD_QAM_MODE, 0x05},
393 {AUD_PHACC_FREQ_8MSB, 0x34},
394 {AUD_PHACC_FREQ_8LSB, 0x4c},
395 {AUD_RATE_ADJ1, 0x00000100},
396 {AUD_RATE_ADJ2, 0x00000200},
397 {AUD_RATE_ADJ3, 0x00000300},
398 {AUD_RATE_ADJ4, 0x00000400},
399 {AUD_RATE_ADJ5, 0x00000500},
400 {AUD_THR_FR, 0x00000000},
401 {AAGC_HYST, 0x0000001a},
402 {AUD_PILOT_BQD_1_K0, 0x0000755b},
403 {AUD_PILOT_BQD_1_K1, 0x00551340},
404 {AUD_PILOT_BQD_1_K2, 0x006d30be},
405 {AUD_PILOT_BQD_1_K3, 0xffd394af},
406 {AUD_PILOT_BQD_1_K4, 0x00400000},
407 {AUD_PILOT_BQD_2_K0, 0x00040000},
408 {AUD_PILOT_BQD_2_K1, 0x002a4841},
409 {AUD_PILOT_BQD_2_K2, 0x00400000},
410 {AUD_PILOT_BQD_2_K3, 0x00000000},
411 {AUD_PILOT_BQD_2_K4, 0x00000000},
412 {AUD_MODE_CHG_TIMER, 0x00000040},
413 {AUD_AFE_12DB_EN, 0x00000001},
414 {AUD_CORDIC_SHIFT_0, 0x00000007},
415 {AUD_CORDIC_SHIFT_1, 0x00000007},
416 {AUD_DEEMPH0_G0, 0x00000380},
417 {AUD_DEEMPH1_G0, 0x00000380},
418 {AUD_DCOC_0_SRC, 0x0000001a},
419 {AUD_DCOC0_SHIFT, 0x00000000},
420 {AUD_DCOC_0_SHIFT_IN0, 0x0000000a},
421 {AUD_DCOC_0_SHIFT_IN1, 0x00000008},
422 {AUD_DCOC_PASS_IN, 0x00000003},
423 {AUD_IIR3_0_SEL, 0x00000021},
424 {AUD_DN2_AFC, 0x00000002},
425 {AUD_DCOC_1_SRC, 0x0000001b},
426 {AUD_DCOC1_SHIFT, 0x00000000},
427 {AUD_DCOC_1_SHIFT_IN0, 0x0000000a},
428 {AUD_DCOC_1_SHIFT_IN1, 0x00000008},
429 {AUD_IIR3_1_SEL, 0x00000023},
430 {AUD_RDSI_SEL, 0x00000017},
431 {AUD_RDSI_SHIFT, 0x00000000},
432 {AUD_RDSQ_SEL, 0x00000017},
433 {AUD_RDSQ_SHIFT, 0x00000000},
434 {AUD_PLL_INT, 0x0000001e},
435 {AUD_PLL_DDS, 0x00000000},
436 {AUD_PLL_FRAC, 0x0000e542},
437 {AUD_POLYPH80SCALEFAC, 0x00000001},
438 {AUD_START_TIMER, 0x00000000},
439 { /* end of list */ },
440 };
441
442 static const struct rlist a2_bg[] = {
443 {AUD_DMD_RA_DDS, 0x002a4f2f},
444 {AUD_C1_UP_THR, 0x00007000},
445 {AUD_C1_LO_THR, 0x00005400},
446 {AUD_C2_UP_THR, 0x00005400},
447 {AUD_C2_LO_THR, 0x00003000},
448 { /* end of list */ },
449 };
450
451 static const struct rlist a2_dk[] = {
452 {AUD_DMD_RA_DDS, 0x002a4f2f},
453 {AUD_C1_UP_THR, 0x00007000},
454 {AUD_C1_LO_THR, 0x00005400},
455 {AUD_C2_UP_THR, 0x00005400},
456 {AUD_C2_LO_THR, 0x00003000},
457 {AUD_DN0_FREQ, 0x00003a1c},
458 {AUD_DN2_FREQ, 0x0000d2e0},
459 { /* end of list */ },
460 };
461
462 static const struct rlist a1_i[] = {
463 {AUD_ERRLOGPERIOD_R, 0x00000064},
464 {AUD_ERRINTRPTTHSHLD1_R, 0x00000fff},
465 {AUD_ERRINTRPTTHSHLD2_R, 0x0000001f},
466 {AUD_ERRINTRPTTHSHLD3_R, 0x0000000f},
467 {AUD_PDF_DDS_CNST_BYTE2, 0x06},
468 {AUD_PDF_DDS_CNST_BYTE1, 0x82},
469 {AUD_PDF_DDS_CNST_BYTE0, 0x12},
470 {AUD_QAM_MODE, 0x05},
471 {AUD_PHACC_FREQ_8MSB, 0x3a},
472 {AUD_PHACC_FREQ_8LSB, 0x93},
473 {AUD_DMD_RA_DDS, 0x002a4f2f},
474 {AUD_PLL_INT, 0x0000001e},
475 {AUD_PLL_DDS, 0x00000004},
476 {AUD_PLL_FRAC, 0x0000e542},
477 {AUD_RATE_ADJ1, 0x00000100},
478 {AUD_RATE_ADJ2, 0x00000200},
479 {AUD_RATE_ADJ3, 0x00000300},
480 {AUD_RATE_ADJ4, 0x00000400},
481 {AUD_RATE_ADJ5, 0x00000500},
482 {AUD_THR_FR, 0x00000000},
483 {AUD_PILOT_BQD_1_K0, 0x0000755b},
484 {AUD_PILOT_BQD_1_K1, 0x00551340},
485 {AUD_PILOT_BQD_1_K2, 0x006d30be},
486 {AUD_PILOT_BQD_1_K3, 0xffd394af},
487 {AUD_PILOT_BQD_1_K4, 0x00400000},
488 {AUD_PILOT_BQD_2_K0, 0x00040000},
489 {AUD_PILOT_BQD_2_K1, 0x002a4841},
490 {AUD_PILOT_BQD_2_K2, 0x00400000},
491 {AUD_PILOT_BQD_2_K3, 0x00000000},
492 {AUD_PILOT_BQD_2_K4, 0x00000000},
493 {AUD_MODE_CHG_TIMER, 0x00000060},
494 {AUD_AFE_12DB_EN, 0x00000001},
495 {AAGC_HYST, 0x0000000a},
496 {AUD_CORDIC_SHIFT_0, 0x00000007},
497 {AUD_CORDIC_SHIFT_1, 0x00000007},
498 {AUD_C1_UP_THR, 0x00007000},
499 {AUD_C1_LO_THR, 0x00005400},
500 {AUD_C2_UP_THR, 0x00005400},
501 {AUD_C2_LO_THR, 0x00003000},
502 {AUD_DCOC_0_SRC, 0x0000001a},
503 {AUD_DCOC0_SHIFT, 0x00000000},
504 {AUD_DCOC_0_SHIFT_IN0, 0x0000000a},
505 {AUD_DCOC_0_SHIFT_IN1, 0x00000008},
506 {AUD_DCOC_PASS_IN, 0x00000003},
507 {AUD_IIR3_0_SEL, 0x00000021},
508 {AUD_DN2_AFC, 0x00000002},
509 {AUD_DCOC_1_SRC, 0x0000001b},
510 {AUD_DCOC1_SHIFT, 0x00000000},
511 {AUD_DCOC_1_SHIFT_IN0, 0x0000000a},
512 {AUD_DCOC_1_SHIFT_IN1, 0x00000008},
513 {AUD_IIR3_1_SEL, 0x00000023},
514 {AUD_DN0_FREQ, 0x000035a3},
515 {AUD_DN2_FREQ, 0x000029c7},
516 {AUD_CRDC0_SRC_SEL, 0x00000511},
517 {AUD_IIR1_0_SEL, 0x00000001},
518 {AUD_IIR1_1_SEL, 0x00000000},
519 {AUD_IIR3_2_SEL, 0x00000003},
520 {AUD_IIR3_2_SHIFT, 0x00000000},
521 {AUD_IIR3_0_SEL, 0x00000002},
522 {AUD_IIR2_0_SEL, 0x00000021},
523 {AUD_IIR2_0_SHIFT, 0x00000002},
524 {AUD_DEEMPH0_SRC_SEL, 0x0000000b},
525 {AUD_DEEMPH1_SRC_SEL, 0x0000000b},
526 {AUD_POLYPH80SCALEFAC, 0x00000001},
527 {AUD_START_TIMER, 0x00000000},
528 { /* end of list */ },
529 };
530
531 static const struct rlist am_l[] = {
532 {AUD_ERRLOGPERIOD_R, 0x00000064},
533 {AUD_ERRINTRPTTHSHLD1_R, 0x00000FFF},
534 {AUD_ERRINTRPTTHSHLD2_R, 0x0000001F},
535 {AUD_ERRINTRPTTHSHLD3_R, 0x0000000F},
536 {AUD_PDF_DDS_CNST_BYTE2, 0x48},
537 {AUD_PDF_DDS_CNST_BYTE1, 0x3D},
538 {AUD_QAM_MODE, 0x00},
539 {AUD_PDF_DDS_CNST_BYTE0, 0xf5},
540 {AUD_PHACC_FREQ_8MSB, 0x3a},
541 {AUD_PHACC_FREQ_8LSB, 0x4a},
542 {AUD_DEEMPHGAIN_R, 0x00006680},
543 {AUD_DEEMPHNUMER1_R, 0x000353DE},
544 {AUD_DEEMPHNUMER2_R, 0x000001B1},
545 {AUD_DEEMPHDENOM1_R, 0x0000F3D0},
546 {AUD_DEEMPHDENOM2_R, 0x00000000},
547 {AUD_FM_MODE_ENABLE, 0x00000007},
548 {AUD_POLYPH80SCALEFAC, 0x00000003},
549 {AUD_AFE_12DB_EN, 0x00000001},
550 {AAGC_GAIN, 0x00000000},
551 {AAGC_HYST, 0x00000018},
552 {AAGC_DEF, 0x00000020},
553 {AUD_DN0_FREQ, 0x00000000},
554 {AUD_POLY0_DDS_CONSTANT, 0x000E4DB2},
555 {AUD_DCOC_0_SRC, 0x00000021},
556 {AUD_IIR1_0_SEL, 0x00000000},
557 {AUD_IIR1_0_SHIFT, 0x00000007},
558 {AUD_IIR1_1_SEL, 0x00000002},
559 {AUD_IIR1_1_SHIFT, 0x00000000},
560 {AUD_DCOC_1_SRC, 0x00000003},
561 {AUD_DCOC1_SHIFT, 0x00000000},
562 {AUD_DCOC_PASS_IN, 0x00000000},
563 {AUD_IIR1_2_SEL, 0x00000023},
564 {AUD_IIR1_2_SHIFT, 0x00000000},
565 {AUD_IIR1_3_SEL, 0x00000004},
566 {AUD_IIR1_3_SHIFT, 0x00000007},
567 {AUD_IIR1_4_SEL, 0x00000005},
568 {AUD_IIR1_4_SHIFT, 0x00000007},
569 {AUD_IIR3_0_SEL, 0x00000007},
570 {AUD_IIR3_0_SHIFT, 0x00000000},
571 {AUD_DEEMPH0_SRC_SEL, 0x00000011},
572 {AUD_DEEMPH0_SHIFT, 0x00000000},
573 {AUD_DEEMPH0_G0, 0x00007000},
574 {AUD_DEEMPH0_A0, 0x00000000},
575 {AUD_DEEMPH0_B0, 0x00000000},
576 {AUD_DEEMPH0_A1, 0x00000000},
577 {AUD_DEEMPH0_B1, 0x00000000},
578 {AUD_DEEMPH1_SRC_SEL, 0x00000011},
579 {AUD_DEEMPH1_SHIFT, 0x00000000},
580 {AUD_DEEMPH1_G0, 0x00007000},
581 {AUD_DEEMPH1_A0, 0x00000000},
582 {AUD_DEEMPH1_B0, 0x00000000},
583 {AUD_DEEMPH1_A1, 0x00000000},
584 {AUD_DEEMPH1_B1, 0x00000000},
585 {AUD_OUT0_SEL, 0x0000003F},
586 {AUD_OUT1_SEL, 0x0000003F},
587 {AUD_DMD_RA_DDS, 0x00F5C285},
588 {AUD_PLL_INT, 0x0000001E},
589 {AUD_PLL_DDS, 0x00000000},
590 {AUD_PLL_FRAC, 0x0000E542},
591 {AUD_RATE_ADJ1, 0x00000100},
592 {AUD_RATE_ADJ2, 0x00000200},
593 {AUD_RATE_ADJ3, 0x00000300},
594 {AUD_RATE_ADJ4, 0x00000400},
595 {AUD_RATE_ADJ5, 0x00000500},
596 {AUD_RATE_THRES_DMD, 0x000000C0},
597 { /* end of list */ },
598 };
599
600 static const struct rlist a2_deemph50[] = {
601 {AUD_DEEMPH0_G0, 0x00000380},
602 {AUD_DEEMPH1_G0, 0x00000380},
603 {AUD_DEEMPHGAIN_R, 0x000011e1},
604 {AUD_DEEMPHNUMER1_R, 0x0002a7bc},
605 {AUD_DEEMPHNUMER2_R, 0x0003023c},
606 { /* end of list */ },
607 };
608
609 set_audio_start(core, SEL_A2);
610 switch (core->tvaudio) {
611 case WW_BG:
612 dprintk("%s PAL-BG A1/2 (status: known-good)\n", __func__);
613 set_audio_registers(core, a2_bgdk_common);
614 set_audio_registers(core, a2_bg);
615 set_audio_registers(core, a2_deemph50);
616 break;
617 case WW_DK:
618 dprintk("%s PAL-DK A1/2 (status: known-good)\n", __func__);
619 set_audio_registers(core, a2_bgdk_common);
620 set_audio_registers(core, a2_dk);
621 set_audio_registers(core, a2_deemph50);
622 break;
623 case WW_I:
624 dprintk("%s PAL-I A1 (status: known-good)\n", __func__);
625 set_audio_registers(core, a1_i);
626 set_audio_registers(core, a2_deemph50);
627 break;
628 case WW_L:
629 dprintk("%s AM-L (status: devel)\n", __func__);
630 set_audio_registers(core, am_l);
631 break;
632 case WW_NONE:
633 case WW_BTSC:
634 case WW_EIAJ:
635 case WW_I2SPT:
636 case WW_FM:
637 case WW_I2SADC:
638 case WW_M:
639 dprintk("%s Warning: wrong value\n", __func__);
640 return;
641 break;
642 };
643
644 mode |= EN_FMRADIO_EN_RDS | EN_DMTRX_SUMDIFF;
645 set_audio_finish(core, mode);
646}
647
648static void set_audio_standard_EIAJ(struct cx88_core *core)
649{
650 static const struct rlist eiaj[] = {
651 /* TODO: eiaj register settings are not there yet ... */
652
653 { /* end of list */ },
654 };
655 dprintk("%s (status: unknown)\n", __func__);
656
657 set_audio_start(core, SEL_EIAJ);
658 set_audio_registers(core, eiaj);
659 set_audio_finish(core, EN_EIAJ_AUTO_STEREO);
660}
661
662static void set_audio_standard_FM(struct cx88_core *core,
663 enum cx88_deemph_type deemph)
664{
665 static const struct rlist fm_deemph_50[] = {
666 {AUD_DEEMPH0_G0, 0x0C45},
667 {AUD_DEEMPH0_A0, 0x6262},
668 {AUD_DEEMPH0_B0, 0x1C29},
669 {AUD_DEEMPH0_A1, 0x3FC66},
670 {AUD_DEEMPH0_B1, 0x399A},
671
672 {AUD_DEEMPH1_G0, 0x0D80},
673 {AUD_DEEMPH1_A0, 0x6262},
674 {AUD_DEEMPH1_B0, 0x1C29},
675 {AUD_DEEMPH1_A1, 0x3FC66},
676 {AUD_DEEMPH1_B1, 0x399A},
677
678 {AUD_POLYPH80SCALEFAC, 0x0003},
679 { /* end of list */ },
680 };
681 static const struct rlist fm_deemph_75[] = {
682 {AUD_DEEMPH0_G0, 0x091B},
683 {AUD_DEEMPH0_A0, 0x6B68},
684 {AUD_DEEMPH0_B0, 0x11EC},
685 {AUD_DEEMPH0_A1, 0x3FC66},
686 {AUD_DEEMPH0_B1, 0x399A},
687
688 {AUD_DEEMPH1_G0, 0x0AA0},
689 {AUD_DEEMPH1_A0, 0x6B68},
690 {AUD_DEEMPH1_B0, 0x11EC},
691 {AUD_DEEMPH1_A1, 0x3FC66},
692 {AUD_DEEMPH1_B1, 0x399A},
693
694 {AUD_POLYPH80SCALEFAC, 0x0003},
695 { /* end of list */ },
696 };
697
698 /* It is enough to leave default values? */
699 /* No, it's not! The deemphasis registers are reset to the 75us
700 * values by default. Analyzing the spectrum of the decoded audio
701 * reveals that "no deemphasis" is the same as 75 us, while the 50 us
702 * setting results in less deemphasis. */
703 static const struct rlist fm_no_deemph[] = {
704
705 {AUD_POLYPH80SCALEFAC, 0x0003},
706 { /* end of list */ },
707 };
708
709 dprintk("%s (status: unknown)\n", __func__);
710 set_audio_start(core, SEL_FMRADIO);
711
712 switch (deemph) {
713 default:
714 case FM_NO_DEEMPH:
715 set_audio_registers(core, fm_no_deemph);
716 break;
717
718 case FM_DEEMPH_50:
719 set_audio_registers(core, fm_deemph_50);
720 break;
721
722 case FM_DEEMPH_75:
723 set_audio_registers(core, fm_deemph_75);
724 break;
725 }
726
727 set_audio_finish(core, EN_FMRADIO_AUTO_STEREO);
728}
729
730/* ----------------------------------------------------------- */
731
732static int cx88_detect_nicam(struct cx88_core *core)
733{
734 int i, j = 0;
735
736 dprintk("start nicam autodetect.\n");
737
738 for (i = 0; i < 6; i++) {
739 /* if bit1=1 then nicam is detected */
740 j += ((cx_read(AUD_NICAM_STATUS2) & 0x02) >> 1);
741
742 if (j == 1) {
743 dprintk("nicam is detected.\n");
744 return 1;
745 }
746
747 /* wait a little bit for next reading status */
748 msleep(10);
749 }
750
751 dprintk("nicam is not detected.\n");
752 return 0;
753}
754
755void cx88_set_tvaudio(struct cx88_core *core)
756{
757 switch (core->tvaudio) {
758 case WW_BTSC:
759 set_audio_standard_BTSC(core, 0, EN_BTSC_AUTO_STEREO);
760 break;
761 case WW_BG:
762 case WW_DK:
763 case WW_M:
764 case WW_I:
765 case WW_L:
766 /* prepare all dsp registers */
767 set_audio_standard_A2(core, EN_A2_FORCE_MONO1);
768
769 /* set nicam mode - otherwise
770 AUD_NICAM_STATUS2 contains wrong values */
771 set_audio_standard_NICAM(core, EN_NICAM_AUTO_STEREO);
772 if (0 == cx88_detect_nicam(core)) {
773 /* fall back to fm / am mono */
774 set_audio_standard_A2(core, EN_A2_FORCE_MONO1);
775 core->audiomode_current = V4L2_TUNER_MODE_MONO;
776 core->use_nicam = 0;
777 } else {
778 core->use_nicam = 1;
779 }
780 break;
781 case WW_EIAJ:
782 set_audio_standard_EIAJ(core);
783 break;
784 case WW_FM:
785 set_audio_standard_FM(core, radio_deemphasis);
786 break;
787 case WW_I2SADC:
788 set_audio_start(core, 0x01);
789 /*
790 * Slave/Philips/Autobaud
791 * NB on Nova-S bit1 NPhilipsSony appears to be inverted:
792 * 0= Sony, 1=Philips
793 */
794 cx_write(AUD_I2SINPUTCNTL, core->board.i2sinputcntl);
795 /* Switch to "I2S ADC mode" */
796 cx_write(AUD_I2SCNTL, 0x1);
797 set_audio_finish(core, EN_I2SIN_ENABLE);
798 break;
799 case WW_NONE:
800 case WW_I2SPT:
801 printk("%s/0: unknown tv audio mode [%d]\n",
802 core->name, core->tvaudio);
803 break;
804 }
805 return;
806}
807
808void cx88_newstation(struct cx88_core *core)
809{
810 core->audiomode_manual = UNSET;
811 core->last_change = jiffies;
812}
813
814void cx88_get_stereo(struct cx88_core *core, struct v4l2_tuner *t)
815{
816 static const char * const m[] = { "stereo", "dual mono", "mono", "sap" };
817 static const char * const p[] = { "no pilot", "pilot c1", "pilot c2", "?" };
818 u32 reg, mode, pilot;
819
820 reg = cx_read(AUD_STATUS);
821 mode = reg & 0x03;
822 pilot = (reg >> 2) & 0x03;
823
824 if (core->astat != reg)
825 dprintk("AUD_STATUS: 0x%x [%s/%s] ctl=%s\n",
826 reg, m[mode], p[pilot],
827 aud_ctl_names[cx_read(AUD_CTL) & 63]);
828 core->astat = reg;
829
830 t->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_SAP |
831 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
832 t->rxsubchans = UNSET;
833 t->audmode = V4L2_TUNER_MODE_MONO;
834
835 switch (mode) {
836 case 0:
837 t->audmode = V4L2_TUNER_MODE_STEREO;
838 break;
839 case 1:
840 t->audmode = V4L2_TUNER_MODE_LANG2;
841 break;
842 case 2:
843 t->audmode = V4L2_TUNER_MODE_MONO;
844 break;
845 case 3:
846 t->audmode = V4L2_TUNER_MODE_SAP;
847 break;
848 }
849
850 switch (core->tvaudio) {
851 case WW_BTSC:
852 case WW_BG:
853 case WW_DK:
854 case WW_M:
855 case WW_EIAJ:
856 if (!core->use_nicam) {
857 t->rxsubchans = cx88_dsp_detect_stereo_sap(core);
858 break;
859 }
860 break;
861 case WW_NONE:
862 case WW_I:
863 case WW_L:
864 case WW_I2SPT:
865 case WW_FM:
866 case WW_I2SADC:
867 /* nothing */
868 break;
869 }
870
871 /* If software stereo detection is not supported... */
872 if (UNSET == t->rxsubchans) {
873 t->rxsubchans = V4L2_TUNER_SUB_MONO;
874 /* If the hardware itself detected stereo, also return
875 stereo as an available subchannel */
876 if (V4L2_TUNER_MODE_STEREO == t->audmode)
877 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
878 }
879 return;
880}
881
882void cx88_set_stereo(struct cx88_core *core, u32 mode, int manual)
883{
884 u32 ctl = UNSET;
885 u32 mask = UNSET;
886
887 if (manual) {
888 core->audiomode_manual = mode;
889 } else {
890 if (UNSET != core->audiomode_manual)
891 return;
892 }
893 core->audiomode_current = mode;
894
895 switch (core->tvaudio) {
896 case WW_BTSC:
897 switch (mode) {
898 case V4L2_TUNER_MODE_MONO:
899 set_audio_standard_BTSC(core, 0, EN_BTSC_FORCE_MONO);
900 break;
901 case V4L2_TUNER_MODE_LANG1:
902 set_audio_standard_BTSC(core, 0, EN_BTSC_AUTO_STEREO);
903 break;
904 case V4L2_TUNER_MODE_LANG2:
905 set_audio_standard_BTSC(core, 1, EN_BTSC_FORCE_SAP);
906 break;
907 case V4L2_TUNER_MODE_STEREO:
908 case V4L2_TUNER_MODE_LANG1_LANG2:
909 set_audio_standard_BTSC(core, 0, EN_BTSC_FORCE_STEREO);
910 break;
911 }
912 break;
913 case WW_BG:
914 case WW_DK:
915 case WW_M:
916 case WW_I:
917 case WW_L:
918 if (1 == core->use_nicam) {
919 switch (mode) {
920 case V4L2_TUNER_MODE_MONO:
921 case V4L2_TUNER_MODE_LANG1:
922 set_audio_standard_NICAM(core,
923 EN_NICAM_FORCE_MONO1);
924 break;
925 case V4L2_TUNER_MODE_LANG2:
926 set_audio_standard_NICAM(core,
927 EN_NICAM_FORCE_MONO2);
928 break;
929 case V4L2_TUNER_MODE_STEREO:
930 case V4L2_TUNER_MODE_LANG1_LANG2:
931 set_audio_standard_NICAM(core,
932 EN_NICAM_FORCE_STEREO);
933 break;
934 }
935 } else {
936 if ((core->tvaudio == WW_I) || (core->tvaudio == WW_L)) {
937 /* fall back to fm / am mono */
938 set_audio_standard_A2(core, EN_A2_FORCE_MONO1);
939 } else {
940 /* TODO: Add A2 autodection */
941 mask = 0x3f;
942 switch (mode) {
943 case V4L2_TUNER_MODE_MONO:
944 case V4L2_TUNER_MODE_LANG1:
945 ctl = EN_A2_FORCE_MONO1;
946 break;
947 case V4L2_TUNER_MODE_LANG2:
948 ctl = EN_A2_FORCE_MONO2;
949 break;
950 case V4L2_TUNER_MODE_STEREO:
951 case V4L2_TUNER_MODE_LANG1_LANG2:
952 ctl = EN_A2_FORCE_STEREO;
953 break;
954 }
955 }
956 }
957 break;
958 case WW_FM:
959 switch (mode) {
960 case V4L2_TUNER_MODE_MONO:
961 ctl = EN_FMRADIO_FORCE_MONO;
962 mask = 0x3f;
963 break;
964 case V4L2_TUNER_MODE_STEREO:
965 ctl = EN_FMRADIO_AUTO_STEREO;
966 mask = 0x3f;
967 break;
968 }
969 break;
970 case WW_I2SADC:
971 case WW_NONE:
972 case WW_EIAJ:
973 case WW_I2SPT:
974 /* DO NOTHING */
975 break;
976 }
977
978 if (UNSET != ctl) {
979 dprintk("cx88_set_stereo: mask 0x%x, ctl 0x%x "
980 "[status=0x%x,ctl=0x%x,vol=0x%x]\n",
981 mask, ctl, cx_read(AUD_STATUS),
982 cx_read(AUD_CTL), cx_sread(SHADOW_AUD_VOL_CTL));
983 cx_andor(AUD_CTL, mask, ctl);
984 }
985 return;
986}
987
988int cx88_audio_thread(void *data)
989{
990 struct cx88_core *core = data;
991 struct v4l2_tuner t;
992 u32 mode = 0;
993
994 dprintk("cx88: tvaudio thread started\n");
995 set_freezable();
996 for (;;) {
997 msleep_interruptible(1000);
998 if (kthread_should_stop())
999 break;
1000 try_to_freeze();
1001
1002 switch (core->tvaudio) {
1003 case WW_BG:
1004 case WW_DK:
1005 case WW_M:
1006 case WW_I:
1007 case WW_L:
1008 if (core->use_nicam)
1009 goto hw_autodetect;
1010
1011 /* just monitor the audio status for now ... */
1012 memset(&t, 0, sizeof(t));
1013 cx88_get_stereo(core, &t);
1014
1015 if (UNSET != core->audiomode_manual)
1016 /* manually set, don't do anything. */
1017 continue;
1018
1019 /* monitor signal and set stereo if available */
1020 if (t.rxsubchans & V4L2_TUNER_SUB_STEREO)
1021 mode = V4L2_TUNER_MODE_STEREO;
1022 else
1023 mode = V4L2_TUNER_MODE_MONO;
1024 if (mode == core->audiomode_current)
1025 continue;
1026 /* automatically switch to best available mode */
1027 cx88_set_stereo(core, mode, 0);
1028 break;
1029 case WW_NONE:
1030 case WW_BTSC:
1031 case WW_EIAJ:
1032 case WW_I2SPT:
1033 case WW_FM:
1034 case WW_I2SADC:
1035hw_autodetect:
1036 /* stereo autodetection is supported by hardware so
1037 we don't need to do it manually. Do nothing. */
1038 break;
1039 }
1040 }
1041
1042 dprintk("cx88: tvaudio thread exiting\n");
1043 return 0;
1044}
1045
1046/* ----------------------------------------------------------- */
1047
1048EXPORT_SYMBOL(cx88_set_tvaudio);
1049EXPORT_SYMBOL(cx88_newstation);
1050EXPORT_SYMBOL(cx88_set_stereo);
1051EXPORT_SYMBOL(cx88_get_stereo);
1052EXPORT_SYMBOL(cx88_audio_thread);
1053
1054/*
1055 * Local variables:
1056 * c-basic-offset: 8
1057 * End:
1058 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
1059 */
diff --git a/drivers/media/pci/cx88/cx88-vbi.c b/drivers/media/pci/cx88/cx88-vbi.c
new file mode 100644
index 000000000000..f8f8389c0362
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-vbi.c
@@ -0,0 +1,245 @@
1/*
2 */
3#include <linux/kernel.h>
4#include <linux/module.h>
5#include <linux/init.h>
6
7#include "cx88.h"
8
9static unsigned int vbibufs = 4;
10module_param(vbibufs,int,0644);
11MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
12
13static unsigned int vbi_debug;
14module_param(vbi_debug,int,0644);
15MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
16
17#define dprintk(level,fmt, arg...) if (vbi_debug >= level) \
18 printk(KERN_DEBUG "%s: " fmt, dev->core->name , ## arg)
19
20/* ------------------------------------------------------------------ */
21
22int cx8800_vbi_fmt (struct file *file, void *priv,
23 struct v4l2_format *f)
24{
25 struct cx8800_fh *fh = priv;
26 struct cx8800_dev *dev = fh->dev;
27
28 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
29 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
30 f->fmt.vbi.offset = 244;
31 f->fmt.vbi.count[0] = VBI_LINE_COUNT;
32 f->fmt.vbi.count[1] = VBI_LINE_COUNT;
33
34 if (dev->core->tvnorm & V4L2_STD_525_60) {
35 /* ntsc */
36 f->fmt.vbi.sampling_rate = 28636363;
37 f->fmt.vbi.start[0] = 10;
38 f->fmt.vbi.start[1] = 273;
39
40 } else if (dev->core->tvnorm & V4L2_STD_625_50) {
41 /* pal */
42 f->fmt.vbi.sampling_rate = 35468950;
43 f->fmt.vbi.start[0] = 7 -1;
44 f->fmt.vbi.start[1] = 319 -1;
45 }
46 return 0;
47}
48
49static int cx8800_start_vbi_dma(struct cx8800_dev *dev,
50 struct cx88_dmaqueue *q,
51 struct cx88_buffer *buf)
52{
53 struct cx88_core *core = dev->core;
54
55 /* setup fifo + format */
56 cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH24],
57 buf->vb.width, buf->risc.dma);
58
59 cx_write(MO_VBOS_CONTROL, ( (1 << 18) | // comb filter delay fixup
60 (1 << 15) | // enable vbi capture
61 (1 << 11) ));
62
63 /* reset counter */
64 cx_write(MO_VBI_GPCNTRL, GP_COUNT_CONTROL_RESET);
65 q->count = 1;
66
67 /* enable irqs */
68 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
69 cx_set(MO_VID_INTMSK, 0x0f0088);
70
71 /* enable capture */
72 cx_set(VID_CAPTURE_CONTROL,0x18);
73
74 /* start dma */
75 cx_set(MO_DEV_CNTRL2, (1<<5));
76 cx_set(MO_VID_DMACNTRL, 0x88);
77
78 return 0;
79}
80
81int cx8800_stop_vbi_dma(struct cx8800_dev *dev)
82{
83 struct cx88_core *core = dev->core;
84
85 /* stop dma */
86 cx_clear(MO_VID_DMACNTRL, 0x88);
87
88 /* disable capture */
89 cx_clear(VID_CAPTURE_CONTROL,0x18);
90
91 /* disable irqs */
92 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
93 cx_clear(MO_VID_INTMSK, 0x0f0088);
94 return 0;
95}
96
97int cx8800_restart_vbi_queue(struct cx8800_dev *dev,
98 struct cx88_dmaqueue *q)
99{
100 struct cx88_buffer *buf;
101
102 if (list_empty(&q->active))
103 return 0;
104
105 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
106 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
107 buf, buf->vb.i);
108 cx8800_start_vbi_dma(dev, q, buf);
109 list_for_each_entry(buf, &q->active, vb.queue)
110 buf->count = q->count++;
111 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
112 return 0;
113}
114
115void cx8800_vbi_timeout(unsigned long data)
116{
117 struct cx8800_dev *dev = (struct cx8800_dev*)data;
118 struct cx88_core *core = dev->core;
119 struct cx88_dmaqueue *q = &dev->vbiq;
120 struct cx88_buffer *buf;
121 unsigned long flags;
122
123 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH24]);
124
125 cx_clear(MO_VID_DMACNTRL, 0x88);
126 cx_clear(VID_CAPTURE_CONTROL, 0x18);
127
128 spin_lock_irqsave(&dev->slock,flags);
129 while (!list_empty(&q->active)) {
130 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
131 list_del(&buf->vb.queue);
132 buf->vb.state = VIDEOBUF_ERROR;
133 wake_up(&buf->vb.done);
134 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", dev->core->name,
135 buf, buf->vb.i, (unsigned long)buf->risc.dma);
136 }
137 cx8800_restart_vbi_queue(dev,q);
138 spin_unlock_irqrestore(&dev->slock,flags);
139}
140
141/* ------------------------------------------------------------------ */
142
143static int
144vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
145{
146 *size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
147 if (0 == *count)
148 *count = vbibufs;
149 if (*count < 2)
150 *count = 2;
151 if (*count > 32)
152 *count = 32;
153 return 0;
154}
155
156static int
157vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
158 enum v4l2_field field)
159{
160 struct cx8800_fh *fh = q->priv_data;
161 struct cx8800_dev *dev = fh->dev;
162 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
163 unsigned int size;
164 int rc;
165
166 size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
167 if (0 != buf->vb.baddr && buf->vb.bsize < size)
168 return -EINVAL;
169
170 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
171 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
172 buf->vb.width = VBI_LINE_LENGTH;
173 buf->vb.height = VBI_LINE_COUNT;
174 buf->vb.size = size;
175 buf->vb.field = V4L2_FIELD_SEQ_TB;
176
177 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
178 goto fail;
179 cx88_risc_buffer(dev->pci, &buf->risc,
180 dma->sglist,
181 0, buf->vb.width * buf->vb.height,
182 buf->vb.width, 0,
183 buf->vb.height);
184 }
185 buf->vb.state = VIDEOBUF_PREPARED;
186 return 0;
187
188 fail:
189 cx88_free_buffer(q,buf);
190 return rc;
191}
192
193static void
194vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
195{
196 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
197 struct cx88_buffer *prev;
198 struct cx8800_fh *fh = vq->priv_data;
199 struct cx8800_dev *dev = fh->dev;
200 struct cx88_dmaqueue *q = &dev->vbiq;
201
202 /* add jump to stopper */
203 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
204 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
205
206 if (list_empty(&q->active)) {
207 list_add_tail(&buf->vb.queue,&q->active);
208 cx8800_start_vbi_dma(dev, q, buf);
209 buf->vb.state = VIDEOBUF_ACTIVE;
210 buf->count = q->count++;
211 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
212 dprintk(2,"[%p/%d] vbi_queue - first active\n",
213 buf, buf->vb.i);
214
215 } else {
216 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
217 list_add_tail(&buf->vb.queue,&q->active);
218 buf->vb.state = VIDEOBUF_ACTIVE;
219 buf->count = q->count++;
220 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
221 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
222 buf, buf->vb.i);
223 }
224}
225
226static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
227{
228 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
229
230 cx88_free_buffer(q,buf);
231}
232
233const struct videobuf_queue_ops cx8800_vbi_qops = {
234 .buf_setup = vbi_setup,
235 .buf_prepare = vbi_prepare,
236 .buf_queue = vbi_queue,
237 .buf_release = vbi_release,
238};
239
240/* ------------------------------------------------------------------ */
241/*
242 * Local variables:
243 * c-basic-offset: 8
244 * End:
245 */
diff --git a/drivers/media/pci/cx88/cx88-video.c b/drivers/media/pci/cx88/cx88-video.c
new file mode 100644
index 000000000000..f6fcc7e763ab
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-video.c
@@ -0,0 +1,2075 @@
1/*
2 *
3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
5 *
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9 * - Multituner support
10 * - video_ioctl2 conversion
11 * - PAL/M fixes
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kmod.h>
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/kthread.h>
38#include <asm/div64.h>
39
40#include "cx88.h"
41#include <media/v4l2-common.h>
42#include <media/v4l2-ioctl.h>
43#include <media/v4l2-event.h>
44#include <media/wm8775.h>
45
46MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
47MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
48MODULE_LICENSE("GPL");
49MODULE_VERSION(CX88_VERSION);
50
51/* ------------------------------------------------------------------ */
52
53static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
54static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56
57module_param_array(video_nr, int, NULL, 0444);
58module_param_array(vbi_nr, int, NULL, 0444);
59module_param_array(radio_nr, int, NULL, 0444);
60
61MODULE_PARM_DESC(video_nr,"video device numbers");
62MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
63MODULE_PARM_DESC(radio_nr,"radio device numbers");
64
65static unsigned int video_debug;
66module_param(video_debug,int,0644);
67MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
68
69static unsigned int irq_debug;
70module_param(irq_debug,int,0644);
71MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
72
73static unsigned int vid_limit = 16;
74module_param(vid_limit,int,0644);
75MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
76
77#define dprintk(level,fmt, arg...) if (video_debug >= level) \
78 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
79
80/* ------------------------------------------------------------------- */
81/* static data */
82
83static const struct cx8800_fmt formats[] = {
84 {
85 .name = "8 bpp, gray",
86 .fourcc = V4L2_PIX_FMT_GREY,
87 .cxformat = ColorFormatY8,
88 .depth = 8,
89 .flags = FORMAT_FLAGS_PACKED,
90 },{
91 .name = "15 bpp RGB, le",
92 .fourcc = V4L2_PIX_FMT_RGB555,
93 .cxformat = ColorFormatRGB15,
94 .depth = 16,
95 .flags = FORMAT_FLAGS_PACKED,
96 },{
97 .name = "15 bpp RGB, be",
98 .fourcc = V4L2_PIX_FMT_RGB555X,
99 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
100 .depth = 16,
101 .flags = FORMAT_FLAGS_PACKED,
102 },{
103 .name = "16 bpp RGB, le",
104 .fourcc = V4L2_PIX_FMT_RGB565,
105 .cxformat = ColorFormatRGB16,
106 .depth = 16,
107 .flags = FORMAT_FLAGS_PACKED,
108 },{
109 .name = "16 bpp RGB, be",
110 .fourcc = V4L2_PIX_FMT_RGB565X,
111 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
112 .depth = 16,
113 .flags = FORMAT_FLAGS_PACKED,
114 },{
115 .name = "24 bpp RGB, le",
116 .fourcc = V4L2_PIX_FMT_BGR24,
117 .cxformat = ColorFormatRGB24,
118 .depth = 24,
119 .flags = FORMAT_FLAGS_PACKED,
120 },{
121 .name = "32 bpp RGB, le",
122 .fourcc = V4L2_PIX_FMT_BGR32,
123 .cxformat = ColorFormatRGB32,
124 .depth = 32,
125 .flags = FORMAT_FLAGS_PACKED,
126 },{
127 .name = "32 bpp RGB, be",
128 .fourcc = V4L2_PIX_FMT_RGB32,
129 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
130 .depth = 32,
131 .flags = FORMAT_FLAGS_PACKED,
132 },{
133 .name = "4:2:2, packed, YUYV",
134 .fourcc = V4L2_PIX_FMT_YUYV,
135 .cxformat = ColorFormatYUY2,
136 .depth = 16,
137 .flags = FORMAT_FLAGS_PACKED,
138 },{
139 .name = "4:2:2, packed, UYVY",
140 .fourcc = V4L2_PIX_FMT_UYVY,
141 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
142 .depth = 16,
143 .flags = FORMAT_FLAGS_PACKED,
144 },
145};
146
147static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
148{
149 unsigned int i;
150
151 for (i = 0; i < ARRAY_SIZE(formats); i++)
152 if (formats[i].fourcc == fourcc)
153 return formats+i;
154 return NULL;
155}
156
157/* ------------------------------------------------------------------- */
158
159struct cx88_ctrl {
160 /* control information */
161 u32 id;
162 s32 minimum;
163 s32 maximum;
164 u32 step;
165 s32 default_value;
166
167 /* control register information */
168 u32 off;
169 u32 reg;
170 u32 sreg;
171 u32 mask;
172 u32 shift;
173};
174
175static const struct cx88_ctrl cx8800_vid_ctls[] = {
176 /* --- video --- */
177 {
178 .id = V4L2_CID_BRIGHTNESS,
179 .minimum = 0x00,
180 .maximum = 0xff,
181 .step = 1,
182 .default_value = 0x7f,
183 .off = 128,
184 .reg = MO_CONTR_BRIGHT,
185 .mask = 0x00ff,
186 .shift = 0,
187 },{
188 .id = V4L2_CID_CONTRAST,
189 .minimum = 0,
190 .maximum = 0xff,
191 .step = 1,
192 .default_value = 0x3f,
193 .off = 0,
194 .reg = MO_CONTR_BRIGHT,
195 .mask = 0xff00,
196 .shift = 8,
197 },{
198 .id = V4L2_CID_HUE,
199 .minimum = 0,
200 .maximum = 0xff,
201 .step = 1,
202 .default_value = 0x7f,
203 .off = 128,
204 .reg = MO_HUE,
205 .mask = 0x00ff,
206 .shift = 0,
207 },{
208 /* strictly, this only describes only U saturation.
209 * V saturation is handled specially through code.
210 */
211 .id = V4L2_CID_SATURATION,
212 .minimum = 0,
213 .maximum = 0xff,
214 .step = 1,
215 .default_value = 0x7f,
216 .off = 0,
217 .reg = MO_UV_SATURATION,
218 .mask = 0x00ff,
219 .shift = 0,
220 }, {
221 .id = V4L2_CID_SHARPNESS,
222 .minimum = 0,
223 .maximum = 4,
224 .step = 1,
225 .default_value = 0x0,
226 .off = 0,
227 /* NOTE: the value is converted and written to both even
228 and odd registers in the code */
229 .reg = MO_FILTER_ODD,
230 .mask = 7 << 7,
231 .shift = 7,
232 }, {
233 .id = V4L2_CID_CHROMA_AGC,
234 .minimum = 0,
235 .maximum = 1,
236 .default_value = 0x1,
237 .reg = MO_INPUT_FORMAT,
238 .mask = 1 << 10,
239 .shift = 10,
240 }, {
241 .id = V4L2_CID_COLOR_KILLER,
242 .minimum = 0,
243 .maximum = 1,
244 .default_value = 0x1,
245 .reg = MO_INPUT_FORMAT,
246 .mask = 1 << 9,
247 .shift = 9,
248 }, {
249 .id = V4L2_CID_BAND_STOP_FILTER,
250 .minimum = 0,
251 .maximum = 1,
252 .step = 1,
253 .default_value = 0x0,
254 .off = 0,
255 .reg = MO_HTOTAL,
256 .mask = 3 << 11,
257 .shift = 11,
258 }
259};
260
261static const struct cx88_ctrl cx8800_aud_ctls[] = {
262 {
263 /* --- audio --- */
264 .id = V4L2_CID_AUDIO_MUTE,
265 .minimum = 0,
266 .maximum = 1,
267 .default_value = 1,
268 .reg = AUD_VOL_CTL,
269 .sreg = SHADOW_AUD_VOL_CTL,
270 .mask = (1 << 6),
271 .shift = 6,
272 },{
273 .id = V4L2_CID_AUDIO_VOLUME,
274 .minimum = 0,
275 .maximum = 0x3f,
276 .step = 1,
277 .default_value = 0x3f,
278 .reg = AUD_VOL_CTL,
279 .sreg = SHADOW_AUD_VOL_CTL,
280 .mask = 0x3f,
281 .shift = 0,
282 },{
283 .id = V4L2_CID_AUDIO_BALANCE,
284 .minimum = 0,
285 .maximum = 0x7f,
286 .step = 1,
287 .default_value = 0x40,
288 .reg = AUD_BAL_CTL,
289 .sreg = SHADOW_AUD_BAL_CTL,
290 .mask = 0x7f,
291 .shift = 0,
292 }
293};
294
295enum {
296 CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
297 CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
298};
299
300/* ------------------------------------------------------------------- */
301/* resource management */
302
303static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
304{
305 struct cx88_core *core = dev->core;
306 if (fh->resources & bit)
307 /* have it already allocated */
308 return 1;
309
310 /* is it free? */
311 mutex_lock(&core->lock);
312 if (dev->resources & bit) {
313 /* no, someone else uses it */
314 mutex_unlock(&core->lock);
315 return 0;
316 }
317 /* it's free, grab it */
318 fh->resources |= bit;
319 dev->resources |= bit;
320 dprintk(1,"res: get %d\n",bit);
321 mutex_unlock(&core->lock);
322 return 1;
323}
324
325static
326int res_check(struct cx8800_fh *fh, unsigned int bit)
327{
328 return (fh->resources & bit);
329}
330
331static
332int res_locked(struct cx8800_dev *dev, unsigned int bit)
333{
334 return (dev->resources & bit);
335}
336
337static
338void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
339{
340 struct cx88_core *core = dev->core;
341 BUG_ON((fh->resources & bits) != bits);
342
343 mutex_lock(&core->lock);
344 fh->resources &= ~bits;
345 dev->resources &= ~bits;
346 dprintk(1,"res: put %d\n",bits);
347 mutex_unlock(&core->lock);
348}
349
350/* ------------------------------------------------------------------ */
351
352int cx88_video_mux(struct cx88_core *core, unsigned int input)
353{
354 /* struct cx88_core *core = dev->core; */
355
356 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
357 input, INPUT(input).vmux,
358 INPUT(input).gpio0,INPUT(input).gpio1,
359 INPUT(input).gpio2,INPUT(input).gpio3);
360 core->input = input;
361 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
362 cx_write(MO_GP3_IO, INPUT(input).gpio3);
363 cx_write(MO_GP0_IO, INPUT(input).gpio0);
364 cx_write(MO_GP1_IO, INPUT(input).gpio1);
365 cx_write(MO_GP2_IO, INPUT(input).gpio2);
366
367 switch (INPUT(input).type) {
368 case CX88_VMUX_SVIDEO:
369 cx_set(MO_AFECFG_IO, 0x00000001);
370 cx_set(MO_INPUT_FORMAT, 0x00010010);
371 cx_set(MO_FILTER_EVEN, 0x00002020);
372 cx_set(MO_FILTER_ODD, 0x00002020);
373 break;
374 default:
375 cx_clear(MO_AFECFG_IO, 0x00000001);
376 cx_clear(MO_INPUT_FORMAT, 0x00010010);
377 cx_clear(MO_FILTER_EVEN, 0x00002020);
378 cx_clear(MO_FILTER_ODD, 0x00002020);
379 break;
380 }
381
382 /* if there are audioroutes defined, we have an external
383 ADC to deal with audio */
384 if (INPUT(input).audioroute) {
385 /* The wm8775 module has the "2" route hardwired into
386 the initialization. Some boards may use different
387 routes for different inputs. HVR-1300 surely does */
388 if (core->board.audio_chip &&
389 core->board.audio_chip == V4L2_IDENT_WM8775) {
390 call_all(core, audio, s_routing,
391 INPUT(input).audioroute, 0, 0);
392 }
393 /* cx2388's C-ADC is connected to the tuner only.
394 When used with S-Video, that ADC is busy dealing with
395 chroma, so an external must be used for baseband audio */
396 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
397 INPUT(input).type != CX88_VMUX_CABLE) {
398 /* "I2S ADC mode" */
399 core->tvaudio = WW_I2SADC;
400 cx88_set_tvaudio(core);
401 } else {
402 /* Normal mode */
403 cx_write(AUD_I2SCNTL, 0x0);
404 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
405 }
406 }
407
408 return 0;
409}
410EXPORT_SYMBOL(cx88_video_mux);
411
412/* ------------------------------------------------------------------ */
413
414static int start_video_dma(struct cx8800_dev *dev,
415 struct cx88_dmaqueue *q,
416 struct cx88_buffer *buf)
417{
418 struct cx88_core *core = dev->core;
419
420 /* setup fifo + format */
421 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
422 buf->bpl, buf->risc.dma);
423 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
424 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
425
426 /* reset counter */
427 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
428 q->count = 1;
429
430 /* enable irqs */
431 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
432
433 /* Enables corresponding bits at PCI_INT_STAT:
434 bits 0 to 4: video, audio, transport stream, VIP, Host
435 bit 7: timer
436 bits 8 and 9: DMA complete for: SRC, DST
437 bits 10 and 11: BERR signal asserted for RISC: RD, WR
438 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
439 */
440 cx_set(MO_VID_INTMSK, 0x0f0011);
441
442 /* enable capture */
443 cx_set(VID_CAPTURE_CONTROL,0x06);
444
445 /* start dma */
446 cx_set(MO_DEV_CNTRL2, (1<<5));
447 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
448
449 return 0;
450}
451
452#ifdef CONFIG_PM
453static int stop_video_dma(struct cx8800_dev *dev)
454{
455 struct cx88_core *core = dev->core;
456
457 /* stop dma */
458 cx_clear(MO_VID_DMACNTRL, 0x11);
459
460 /* disable capture */
461 cx_clear(VID_CAPTURE_CONTROL,0x06);
462
463 /* disable irqs */
464 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
465 cx_clear(MO_VID_INTMSK, 0x0f0011);
466 return 0;
467}
468#endif
469
470static int restart_video_queue(struct cx8800_dev *dev,
471 struct cx88_dmaqueue *q)
472{
473 struct cx88_core *core = dev->core;
474 struct cx88_buffer *buf, *prev;
475
476 if (!list_empty(&q->active)) {
477 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
478 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
479 buf, buf->vb.i);
480 start_video_dma(dev, q, buf);
481 list_for_each_entry(buf, &q->active, vb.queue)
482 buf->count = q->count++;
483 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
484 return 0;
485 }
486
487 prev = NULL;
488 for (;;) {
489 if (list_empty(&q->queued))
490 return 0;
491 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
492 if (NULL == prev) {
493 list_move_tail(&buf->vb.queue, &q->active);
494 start_video_dma(dev, q, buf);
495 buf->vb.state = VIDEOBUF_ACTIVE;
496 buf->count = q->count++;
497 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
498 dprintk(2,"[%p/%d] restart_queue - first active\n",
499 buf,buf->vb.i);
500
501 } else if (prev->vb.width == buf->vb.width &&
502 prev->vb.height == buf->vb.height &&
503 prev->fmt == buf->fmt) {
504 list_move_tail(&buf->vb.queue, &q->active);
505 buf->vb.state = VIDEOBUF_ACTIVE;
506 buf->count = q->count++;
507 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
508 dprintk(2,"[%p/%d] restart_queue - move to active\n",
509 buf,buf->vb.i);
510 } else {
511 return 0;
512 }
513 prev = buf;
514 }
515}
516
517/* ------------------------------------------------------------------ */
518
519static int
520buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
521{
522 struct cx8800_fh *fh = q->priv_data;
523 struct cx8800_dev *dev = fh->dev;
524
525 *size = dev->fmt->depth * dev->width * dev->height >> 3;
526 if (0 == *count)
527 *count = 32;
528 if (*size * *count > vid_limit * 1024 * 1024)
529 *count = (vid_limit * 1024 * 1024) / *size;
530 return 0;
531}
532
533static int
534buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
535 enum v4l2_field field)
536{
537 struct cx8800_fh *fh = q->priv_data;
538 struct cx8800_dev *dev = fh->dev;
539 struct cx88_core *core = dev->core;
540 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
541 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
542 int rc, init_buffer = 0;
543
544 BUG_ON(NULL == dev->fmt);
545 if (dev->width < 48 || dev->width > norm_maxw(core->tvnorm) ||
546 dev->height < 32 || dev->height > norm_maxh(core->tvnorm))
547 return -EINVAL;
548 buf->vb.size = (dev->width * dev->height * dev->fmt->depth) >> 3;
549 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
550 return -EINVAL;
551
552 if (buf->fmt != dev->fmt ||
553 buf->vb.width != dev->width ||
554 buf->vb.height != dev->height ||
555 buf->vb.field != field) {
556 buf->fmt = dev->fmt;
557 buf->vb.width = dev->width;
558 buf->vb.height = dev->height;
559 buf->vb.field = field;
560 init_buffer = 1;
561 }
562
563 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
564 init_buffer = 1;
565 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
566 goto fail;
567 }
568
569 if (init_buffer) {
570 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
571 switch (buf->vb.field) {
572 case V4L2_FIELD_TOP:
573 cx88_risc_buffer(dev->pci, &buf->risc,
574 dma->sglist, 0, UNSET,
575 buf->bpl, 0, buf->vb.height);
576 break;
577 case V4L2_FIELD_BOTTOM:
578 cx88_risc_buffer(dev->pci, &buf->risc,
579 dma->sglist, UNSET, 0,
580 buf->bpl, 0, buf->vb.height);
581 break;
582 case V4L2_FIELD_INTERLACED:
583 cx88_risc_buffer(dev->pci, &buf->risc,
584 dma->sglist, 0, buf->bpl,
585 buf->bpl, buf->bpl,
586 buf->vb.height >> 1);
587 break;
588 case V4L2_FIELD_SEQ_TB:
589 cx88_risc_buffer(dev->pci, &buf->risc,
590 dma->sglist,
591 0, buf->bpl * (buf->vb.height >> 1),
592 buf->bpl, 0,
593 buf->vb.height >> 1);
594 break;
595 case V4L2_FIELD_SEQ_BT:
596 cx88_risc_buffer(dev->pci, &buf->risc,
597 dma->sglist,
598 buf->bpl * (buf->vb.height >> 1), 0,
599 buf->bpl, 0,
600 buf->vb.height >> 1);
601 break;
602 default:
603 BUG();
604 }
605 }
606 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
607 buf, buf->vb.i,
608 dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
609 (unsigned long)buf->risc.dma);
610
611 buf->vb.state = VIDEOBUF_PREPARED;
612 return 0;
613
614 fail:
615 cx88_free_buffer(q,buf);
616 return rc;
617}
618
619static void
620buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
621{
622 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
623 struct cx88_buffer *prev;
624 struct cx8800_fh *fh = vq->priv_data;
625 struct cx8800_dev *dev = fh->dev;
626 struct cx88_core *core = dev->core;
627 struct cx88_dmaqueue *q = &dev->vidq;
628
629 /* add jump to stopper */
630 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
631 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
632
633 if (!list_empty(&q->queued)) {
634 list_add_tail(&buf->vb.queue,&q->queued);
635 buf->vb.state = VIDEOBUF_QUEUED;
636 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
637 buf, buf->vb.i);
638
639 } else if (list_empty(&q->active)) {
640 list_add_tail(&buf->vb.queue,&q->active);
641 start_video_dma(dev, q, buf);
642 buf->vb.state = VIDEOBUF_ACTIVE;
643 buf->count = q->count++;
644 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
645 dprintk(2,"[%p/%d] buffer_queue - first active\n",
646 buf, buf->vb.i);
647
648 } else {
649 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
650 if (prev->vb.width == buf->vb.width &&
651 prev->vb.height == buf->vb.height &&
652 prev->fmt == buf->fmt) {
653 list_add_tail(&buf->vb.queue,&q->active);
654 buf->vb.state = VIDEOBUF_ACTIVE;
655 buf->count = q->count++;
656 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
657 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
658 buf, buf->vb.i);
659
660 } else {
661 list_add_tail(&buf->vb.queue,&q->queued);
662 buf->vb.state = VIDEOBUF_QUEUED;
663 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
664 buf, buf->vb.i);
665 }
666 }
667}
668
669static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
670{
671 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
672
673 cx88_free_buffer(q,buf);
674}
675
676static const struct videobuf_queue_ops cx8800_video_qops = {
677 .buf_setup = buffer_setup,
678 .buf_prepare = buffer_prepare,
679 .buf_queue = buffer_queue,
680 .buf_release = buffer_release,
681};
682
683/* ------------------------------------------------------------------ */
684
685
686/* ------------------------------------------------------------------ */
687
688static struct videobuf_queue *get_queue(struct file *file)
689{
690 struct video_device *vdev = video_devdata(file);
691 struct cx8800_fh *fh = file->private_data;
692
693 switch (vdev->vfl_type) {
694 case VFL_TYPE_GRABBER:
695 return &fh->vidq;
696 case VFL_TYPE_VBI:
697 return &fh->vbiq;
698 default:
699 BUG();
700 return NULL;
701 }
702}
703
704static int get_resource(struct file *file)
705{
706 struct video_device *vdev = video_devdata(file);
707
708 switch (vdev->vfl_type) {
709 case VFL_TYPE_GRABBER:
710 return RESOURCE_VIDEO;
711 case VFL_TYPE_VBI:
712 return RESOURCE_VBI;
713 default:
714 BUG();
715 return 0;
716 }
717}
718
719static int video_open(struct file *file)
720{
721 struct video_device *vdev = video_devdata(file);
722 struct cx8800_dev *dev = video_drvdata(file);
723 struct cx88_core *core = dev->core;
724 struct cx8800_fh *fh;
725 enum v4l2_buf_type type = 0;
726 int radio = 0;
727
728 switch (vdev->vfl_type) {
729 case VFL_TYPE_GRABBER:
730 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
731 break;
732 case VFL_TYPE_VBI:
733 type = V4L2_BUF_TYPE_VBI_CAPTURE;
734 break;
735 case VFL_TYPE_RADIO:
736 radio = 1;
737 break;
738 }
739
740 dprintk(1, "open dev=%s radio=%d type=%s\n",
741 video_device_node_name(vdev), radio, v4l2_type_names[type]);
742
743 /* allocate + initialize per filehandle data */
744 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
745 if (unlikely(!fh))
746 return -ENOMEM;
747
748 v4l2_fh_init(&fh->fh, vdev);
749 file->private_data = fh;
750 fh->dev = dev;
751
752 mutex_lock(&core->lock);
753
754 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
755 &dev->pci->dev, &dev->slock,
756 V4L2_BUF_TYPE_VIDEO_CAPTURE,
757 V4L2_FIELD_INTERLACED,
758 sizeof(struct cx88_buffer),
759 fh, NULL);
760 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
761 &dev->pci->dev, &dev->slock,
762 V4L2_BUF_TYPE_VBI_CAPTURE,
763 V4L2_FIELD_SEQ_TB,
764 sizeof(struct cx88_buffer),
765 fh, NULL);
766
767 if (vdev->vfl_type == VFL_TYPE_RADIO) {
768 dprintk(1,"video_open: setting radio device\n");
769 cx_write(MO_GP3_IO, core->board.radio.gpio3);
770 cx_write(MO_GP0_IO, core->board.radio.gpio0);
771 cx_write(MO_GP1_IO, core->board.radio.gpio1);
772 cx_write(MO_GP2_IO, core->board.radio.gpio2);
773 if (core->board.radio.audioroute) {
774 if(core->board.audio_chip &&
775 core->board.audio_chip == V4L2_IDENT_WM8775) {
776 call_all(core, audio, s_routing,
777 core->board.radio.audioroute, 0, 0);
778 }
779 /* "I2S ADC mode" */
780 core->tvaudio = WW_I2SADC;
781 cx88_set_tvaudio(core);
782 } else {
783 /* FM Mode */
784 core->tvaudio = WW_FM;
785 cx88_set_tvaudio(core);
786 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
787 }
788 call_all(core, tuner, s_radio);
789 }
790
791 core->users++;
792 mutex_unlock(&core->lock);
793 v4l2_fh_add(&fh->fh);
794
795 return 0;
796}
797
798static ssize_t
799video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
800{
801 struct video_device *vdev = video_devdata(file);
802 struct cx8800_fh *fh = file->private_data;
803
804 switch (vdev->vfl_type) {
805 case VFL_TYPE_GRABBER:
806 if (res_locked(fh->dev,RESOURCE_VIDEO))
807 return -EBUSY;
808 return videobuf_read_one(&fh->vidq, data, count, ppos,
809 file->f_flags & O_NONBLOCK);
810 case VFL_TYPE_VBI:
811 if (!res_get(fh->dev,fh,RESOURCE_VBI))
812 return -EBUSY;
813 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
814 file->f_flags & O_NONBLOCK);
815 default:
816 BUG();
817 return 0;
818 }
819}
820
821static unsigned int
822video_poll(struct file *file, struct poll_table_struct *wait)
823{
824 struct video_device *vdev = video_devdata(file);
825 struct cx8800_fh *fh = file->private_data;
826 struct cx88_buffer *buf;
827 unsigned int rc = v4l2_ctrl_poll(file, wait);
828
829 if (vdev->vfl_type == VFL_TYPE_VBI) {
830 if (!res_get(fh->dev,fh,RESOURCE_VBI))
831 return rc | POLLERR;
832 return rc | videobuf_poll_stream(file, &fh->vbiq, wait);
833 }
834 mutex_lock(&fh->vidq.vb_lock);
835 if (res_check(fh,RESOURCE_VIDEO)) {
836 /* streaming capture */
837 if (list_empty(&fh->vidq.stream))
838 goto done;
839 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
840 } else {
841 /* read() capture */
842 buf = (struct cx88_buffer*)fh->vidq.read_buf;
843 if (NULL == buf)
844 goto done;
845 }
846 poll_wait(file, &buf->vb.done, wait);
847 if (buf->vb.state == VIDEOBUF_DONE ||
848 buf->vb.state == VIDEOBUF_ERROR)
849 rc |= POLLIN|POLLRDNORM;
850done:
851 mutex_unlock(&fh->vidq.vb_lock);
852 return rc;
853}
854
855static int video_release(struct file *file)
856{
857 struct cx8800_fh *fh = file->private_data;
858 struct cx8800_dev *dev = fh->dev;
859
860 /* turn off overlay */
861 if (res_check(fh, RESOURCE_OVERLAY)) {
862 /* FIXME */
863 res_free(dev,fh,RESOURCE_OVERLAY);
864 }
865
866 /* stop video capture */
867 if (res_check(fh, RESOURCE_VIDEO)) {
868 videobuf_queue_cancel(&fh->vidq);
869 res_free(dev,fh,RESOURCE_VIDEO);
870 }
871 if (fh->vidq.read_buf) {
872 buffer_release(&fh->vidq,fh->vidq.read_buf);
873 kfree(fh->vidq.read_buf);
874 }
875
876 /* stop vbi capture */
877 if (res_check(fh, RESOURCE_VBI)) {
878 videobuf_stop(&fh->vbiq);
879 res_free(dev,fh,RESOURCE_VBI);
880 }
881
882 videobuf_mmap_free(&fh->vidq);
883 videobuf_mmap_free(&fh->vbiq);
884
885 mutex_lock(&dev->core->lock);
886 v4l2_fh_del(&fh->fh);
887 v4l2_fh_exit(&fh->fh);
888 file->private_data = NULL;
889 kfree(fh);
890
891 dev->core->users--;
892 if (!dev->core->users)
893 call_all(dev->core, core, s_power, 0);
894 mutex_unlock(&dev->core->lock);
895
896 return 0;
897}
898
899static int
900video_mmap(struct file *file, struct vm_area_struct * vma)
901{
902 return videobuf_mmap_mapper(get_queue(file), vma);
903}
904
905/* ------------------------------------------------------------------ */
906/* VIDEO CTRL IOCTLS */
907
908static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
909{
910 struct cx88_core *core =
911 container_of(ctrl->handler, struct cx88_core, video_hdl);
912 const struct cx88_ctrl *cc = ctrl->priv;
913 u32 value, mask;
914
915 mask = cc->mask;
916 switch (ctrl->id) {
917 case V4L2_CID_SATURATION:
918 /* special v_sat handling */
919
920 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
921
922 if (core->tvnorm & V4L2_STD_SECAM) {
923 /* For SECAM, both U and V sat should be equal */
924 value = value << 8 | value;
925 } else {
926 /* Keeps U Saturation proportional to V Sat */
927 value = (value * 0x5a) / 0x7f << 8 | value;
928 }
929 mask = 0xffff;
930 break;
931 case V4L2_CID_SHARPNESS:
932 /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
933 value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
934 /* needs to be set for both fields */
935 cx_andor(MO_FILTER_EVEN, mask, value);
936 break;
937 case V4L2_CID_CHROMA_AGC:
938 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
939 break;
940 default:
941 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
942 break;
943 }
944 dprintk(1, "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
945 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
946 mask, cc->sreg ? " [shadowed]" : "");
947 if (cc->sreg)
948 cx_sandor(cc->sreg, cc->reg, mask, value);
949 else
950 cx_andor(cc->reg, mask, value);
951 return 0;
952}
953
954static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
955{
956 struct cx88_core *core =
957 container_of(ctrl->handler, struct cx88_core, audio_hdl);
958 const struct cx88_ctrl *cc = ctrl->priv;
959 u32 value,mask;
960
961 /* Pass changes onto any WM8775 */
962 if (core->board.audio_chip == V4L2_IDENT_WM8775) {
963 switch (ctrl->id) {
964 case V4L2_CID_AUDIO_MUTE:
965 wm8775_s_ctrl(core, ctrl->id, ctrl->val);
966 break;
967 case V4L2_CID_AUDIO_VOLUME:
968 wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
969 (0x90 + ctrl->val) << 8 : 0);
970 break;
971 case V4L2_CID_AUDIO_BALANCE:
972 wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
973 break;
974 default:
975 break;
976 }
977 }
978
979 mask = cc->mask;
980 switch (ctrl->id) {
981 case V4L2_CID_AUDIO_BALANCE:
982 value = (ctrl->val < 0x40) ? (0x7f - ctrl->val) : (ctrl->val - 0x40);
983 break;
984 case V4L2_CID_AUDIO_VOLUME:
985 value = 0x3f - (ctrl->val & 0x3f);
986 break;
987 default:
988 value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
989 break;
990 }
991 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
992 ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
993 mask, cc->sreg ? " [shadowed]" : "");
994 if (cc->sreg)
995 cx_sandor(cc->sreg, cc->reg, mask, value);
996 else
997 cx_andor(cc->reg, mask, value);
998 return 0;
999}
1000
1001/* ------------------------------------------------------------------ */
1002/* VIDEO IOCTLS */
1003
1004static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1005 struct v4l2_format *f)
1006{
1007 struct cx8800_fh *fh = priv;
1008 struct cx8800_dev *dev = fh->dev;
1009
1010 f->fmt.pix.width = dev->width;
1011 f->fmt.pix.height = dev->height;
1012 f->fmt.pix.field = fh->vidq.field;
1013 f->fmt.pix.pixelformat = dev->fmt->fourcc;
1014 f->fmt.pix.bytesperline =
1015 (f->fmt.pix.width * dev->fmt->depth) >> 3;
1016 f->fmt.pix.sizeimage =
1017 f->fmt.pix.height * f->fmt.pix.bytesperline;
1018 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1019 return 0;
1020}
1021
1022static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1023 struct v4l2_format *f)
1024{
1025 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1026 const struct cx8800_fmt *fmt;
1027 enum v4l2_field field;
1028 unsigned int maxw, maxh;
1029
1030 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1031 if (NULL == fmt)
1032 return -EINVAL;
1033
1034 field = f->fmt.pix.field;
1035 maxw = norm_maxw(core->tvnorm);
1036 maxh = norm_maxh(core->tvnorm);
1037
1038 if (V4L2_FIELD_ANY == field) {
1039 field = (f->fmt.pix.height > maxh/2)
1040 ? V4L2_FIELD_INTERLACED
1041 : V4L2_FIELD_BOTTOM;
1042 }
1043
1044 switch (field) {
1045 case V4L2_FIELD_TOP:
1046 case V4L2_FIELD_BOTTOM:
1047 maxh = maxh / 2;
1048 break;
1049 case V4L2_FIELD_INTERLACED:
1050 break;
1051 default:
1052 return -EINVAL;
1053 }
1054
1055 f->fmt.pix.field = field;
1056 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1057 &f->fmt.pix.height, 32, maxh, 0, 0);
1058 f->fmt.pix.bytesperline =
1059 (f->fmt.pix.width * fmt->depth) >> 3;
1060 f->fmt.pix.sizeimage =
1061 f->fmt.pix.height * f->fmt.pix.bytesperline;
1062
1063 return 0;
1064}
1065
1066static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1067 struct v4l2_format *f)
1068{
1069 struct cx8800_fh *fh = priv;
1070 struct cx8800_dev *dev = fh->dev;
1071 int err = vidioc_try_fmt_vid_cap (file,priv,f);
1072
1073 if (0 != err)
1074 return err;
1075 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1076 dev->width = f->fmt.pix.width;
1077 dev->height = f->fmt.pix.height;
1078 fh->vidq.field = f->fmt.pix.field;
1079 return 0;
1080}
1081
1082void cx88_querycap(struct file *file, struct cx88_core *core,
1083 struct v4l2_capability *cap)
1084{
1085 struct video_device *vdev = video_devdata(file);
1086
1087 strlcpy(cap->card, core->board.name, sizeof(cap->card));
1088 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1089 if (UNSET != core->board.tuner_type)
1090 cap->device_caps |= V4L2_CAP_TUNER;
1091 switch (vdev->vfl_type) {
1092 case VFL_TYPE_RADIO:
1093 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
1094 break;
1095 case VFL_TYPE_GRABBER:
1096 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1097 break;
1098 case VFL_TYPE_VBI:
1099 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1100 break;
1101 }
1102 cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
1103 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
1104 if (core->board.radio.type == CX88_RADIO)
1105 cap->capabilities |= V4L2_CAP_RADIO;
1106}
1107EXPORT_SYMBOL(cx88_querycap);
1108
1109static int vidioc_querycap(struct file *file, void *priv,
1110 struct v4l2_capability *cap)
1111{
1112 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1113 struct cx88_core *core = dev->core;
1114
1115 strcpy(cap->driver, "cx8800");
1116 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1117 cx88_querycap(file, core, cap);
1118 return 0;
1119}
1120
1121static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
1122 struct v4l2_fmtdesc *f)
1123{
1124 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1125 return -EINVAL;
1126
1127 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1128 f->pixelformat = formats[f->index].fourcc;
1129
1130 return 0;
1131}
1132
1133static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1134{
1135 return videobuf_reqbufs(get_queue(file), p);
1136}
1137
1138static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1139{
1140 return videobuf_querybuf(get_queue(file), p);
1141}
1142
1143static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1144{
1145 return videobuf_qbuf(get_queue(file), p);
1146}
1147
1148static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1149{
1150 return videobuf_dqbuf(get_queue(file), p,
1151 file->f_flags & O_NONBLOCK);
1152}
1153
1154static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1155{
1156 struct video_device *vdev = video_devdata(file);
1157 struct cx8800_fh *fh = priv;
1158 struct cx8800_dev *dev = fh->dev;
1159
1160 if ((vdev->vfl_type == VFL_TYPE_GRABBER && i != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1161 (vdev->vfl_type == VFL_TYPE_VBI && i != V4L2_BUF_TYPE_VBI_CAPTURE))
1162 return -EINVAL;
1163
1164 if (unlikely(!res_get(dev, fh, get_resource(file))))
1165 return -EBUSY;
1166 return videobuf_streamon(get_queue(file));
1167}
1168
1169static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1170{
1171 struct video_device *vdev = video_devdata(file);
1172 struct cx8800_fh *fh = priv;
1173 struct cx8800_dev *dev = fh->dev;
1174 int err, res;
1175
1176 if ((vdev->vfl_type == VFL_TYPE_GRABBER && i != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1177 (vdev->vfl_type == VFL_TYPE_VBI && i != V4L2_BUF_TYPE_VBI_CAPTURE))
1178 return -EINVAL;
1179
1180 res = get_resource(file);
1181 err = videobuf_streamoff(get_queue(file));
1182 if (err < 0)
1183 return err;
1184 res_free(dev,fh,res);
1185 return 0;
1186}
1187
1188static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
1189{
1190 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1191
1192 *tvnorm = core->tvnorm;
1193 return 0;
1194}
1195
1196static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
1197{
1198 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1199
1200 mutex_lock(&core->lock);
1201 cx88_set_tvnorm(core,*tvnorms);
1202 mutex_unlock(&core->lock);
1203
1204 return 0;
1205}
1206
1207/* only one input in this sample driver */
1208int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
1209{
1210 static const char * const iname[] = {
1211 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1212 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1213 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1214 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1215 [ CX88_VMUX_SVIDEO ] = "S-Video",
1216 [ CX88_VMUX_TELEVISION ] = "Television",
1217 [ CX88_VMUX_CABLE ] = "Cable TV",
1218 [ CX88_VMUX_DVB ] = "DVB",
1219 [ CX88_VMUX_DEBUG ] = "for debug only",
1220 };
1221 unsigned int n = i->index;
1222
1223 if (n >= 4)
1224 return -EINVAL;
1225 if (0 == INPUT(n).type)
1226 return -EINVAL;
1227 i->type = V4L2_INPUT_TYPE_CAMERA;
1228 strcpy(i->name,iname[INPUT(n).type]);
1229 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1230 (CX88_VMUX_CABLE == INPUT(n).type)) {
1231 i->type = V4L2_INPUT_TYPE_TUNER;
1232 }
1233 i->std = CX88_NORMS;
1234 return 0;
1235}
1236EXPORT_SYMBOL(cx88_enum_input);
1237
1238static int vidioc_enum_input (struct file *file, void *priv,
1239 struct v4l2_input *i)
1240{
1241 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1242 return cx88_enum_input (core,i);
1243}
1244
1245static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1246{
1247 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1248
1249 *i = core->input;
1250 return 0;
1251}
1252
1253static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1254{
1255 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1256
1257 if (i >= 4)
1258 return -EINVAL;
1259 if (0 == INPUT(i).type)
1260 return -EINVAL;
1261
1262 mutex_lock(&core->lock);
1263 cx88_newstation(core);
1264 cx88_video_mux(core,i);
1265 mutex_unlock(&core->lock);
1266 return 0;
1267}
1268
1269static int vidioc_g_tuner (struct file *file, void *priv,
1270 struct v4l2_tuner *t)
1271{
1272 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1273 u32 reg;
1274
1275 if (unlikely(UNSET == core->board.tuner_type))
1276 return -EINVAL;
1277 if (0 != t->index)
1278 return -EINVAL;
1279
1280 strcpy(t->name, "Television");
1281 t->capability = V4L2_TUNER_CAP_NORM;
1282 t->rangehigh = 0xffffffffUL;
1283 call_all(core, tuner, g_tuner, t);
1284
1285 cx88_get_stereo(core ,t);
1286 reg = cx_read(MO_DEVICE_STATUS);
1287 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1288 return 0;
1289}
1290
1291static int vidioc_s_tuner (struct file *file, void *priv,
1292 struct v4l2_tuner *t)
1293{
1294 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1295
1296 if (UNSET == core->board.tuner_type)
1297 return -EINVAL;
1298 if (0 != t->index)
1299 return -EINVAL;
1300
1301 cx88_set_stereo(core, t->audmode, 1);
1302 return 0;
1303}
1304
1305static int vidioc_g_frequency (struct file *file, void *priv,
1306 struct v4l2_frequency *f)
1307{
1308 struct cx8800_fh *fh = priv;
1309 struct cx88_core *core = fh->dev->core;
1310
1311 if (unlikely(UNSET == core->board.tuner_type))
1312 return -EINVAL;
1313 if (f->tuner)
1314 return -EINVAL;
1315
1316 f->frequency = core->freq;
1317
1318 call_all(core, tuner, g_frequency, f);
1319
1320 return 0;
1321}
1322
1323int cx88_set_freq (struct cx88_core *core,
1324 struct v4l2_frequency *f)
1325{
1326 if (unlikely(UNSET == core->board.tuner_type))
1327 return -EINVAL;
1328 if (unlikely(f->tuner != 0))
1329 return -EINVAL;
1330
1331 mutex_lock(&core->lock);
1332 cx88_newstation(core);
1333 call_all(core, tuner, s_frequency, f);
1334 call_all(core, tuner, g_frequency, f);
1335 core->freq = f->frequency;
1336
1337 /* When changing channels it is required to reset TVAUDIO */
1338 msleep (10);
1339 cx88_set_tvaudio(core);
1340
1341 mutex_unlock(&core->lock);
1342
1343 return 0;
1344}
1345EXPORT_SYMBOL(cx88_set_freq);
1346
1347static int vidioc_s_frequency (struct file *file, void *priv,
1348 struct v4l2_frequency *f)
1349{
1350 struct cx8800_fh *fh = priv;
1351 struct cx88_core *core = fh->dev->core;
1352
1353 return cx88_set_freq(core, f);
1354}
1355
1356static int vidioc_g_chip_ident(struct file *file, void *priv,
1357 struct v4l2_dbg_chip_ident *chip)
1358{
1359 if (!v4l2_chip_match_host(&chip->match))
1360 return -EINVAL;
1361 chip->revision = 0;
1362 chip->ident = V4L2_IDENT_UNKNOWN;
1363 return 0;
1364}
1365
1366#ifdef CONFIG_VIDEO_ADV_DEBUG
1367static int vidioc_g_register (struct file *file, void *fh,
1368 struct v4l2_dbg_register *reg)
1369{
1370 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1371
1372 if (!v4l2_chip_match_host(&reg->match))
1373 return -EINVAL;
1374 /* cx2388x has a 24-bit register space */
1375 reg->val = cx_read(reg->reg & 0xffffff);
1376 reg->size = 4;
1377 return 0;
1378}
1379
1380static int vidioc_s_register (struct file *file, void *fh,
1381 struct v4l2_dbg_register *reg)
1382{
1383 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1384
1385 if (!v4l2_chip_match_host(&reg->match))
1386 return -EINVAL;
1387 cx_write(reg->reg & 0xffffff, reg->val);
1388 return 0;
1389}
1390#endif
1391
1392/* ----------------------------------------------------------- */
1393/* RADIO ESPECIFIC IOCTLS */
1394/* ----------------------------------------------------------- */
1395
1396static int radio_g_tuner (struct file *file, void *priv,
1397 struct v4l2_tuner *t)
1398{
1399 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1400
1401 if (unlikely(t->index > 0))
1402 return -EINVAL;
1403
1404 strcpy(t->name, "Radio");
1405
1406 call_all(core, tuner, g_tuner, t);
1407 return 0;
1408}
1409
1410/* FIXME: Should add a standard for radio */
1411
1412static int radio_s_tuner (struct file *file, void *priv,
1413 struct v4l2_tuner *t)
1414{
1415 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1416
1417 if (0 != t->index)
1418 return -EINVAL;
1419 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1420 t->audmode = V4L2_TUNER_MODE_STEREO;
1421
1422 call_all(core, tuner, s_tuner, t);
1423
1424 return 0;
1425}
1426
1427/* ----------------------------------------------------------- */
1428
1429static void cx8800_vid_timeout(unsigned long data)
1430{
1431 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1432 struct cx88_core *core = dev->core;
1433 struct cx88_dmaqueue *q = &dev->vidq;
1434 struct cx88_buffer *buf;
1435 unsigned long flags;
1436
1437 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1438
1439 cx_clear(MO_VID_DMACNTRL, 0x11);
1440 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1441
1442 spin_lock_irqsave(&dev->slock,flags);
1443 while (!list_empty(&q->active)) {
1444 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1445 list_del(&buf->vb.queue);
1446 buf->vb.state = VIDEOBUF_ERROR;
1447 wake_up(&buf->vb.done);
1448 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1449 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1450 }
1451 restart_video_queue(dev,q);
1452 spin_unlock_irqrestore(&dev->slock,flags);
1453}
1454
1455static const char *cx88_vid_irqs[32] = {
1456 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1457 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1458 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1459 "y_sync", "u_sync", "v_sync", "vbi_sync",
1460 "opc_err", "par_err", "rip_err", "pci_abort",
1461};
1462
1463static void cx8800_vid_irq(struct cx8800_dev *dev)
1464{
1465 struct cx88_core *core = dev->core;
1466 u32 status, mask, count;
1467
1468 status = cx_read(MO_VID_INTSTAT);
1469 mask = cx_read(MO_VID_INTMSK);
1470 if (0 == (status & mask))
1471 return;
1472 cx_write(MO_VID_INTSTAT, status);
1473 if (irq_debug || (status & mask & ~0xff))
1474 cx88_print_irqbits(core->name, "irq vid",
1475 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1476 status, mask);
1477
1478 /* risc op code error */
1479 if (status & (1 << 16)) {
1480 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1481 cx_clear(MO_VID_DMACNTRL, 0x11);
1482 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1483 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1484 }
1485
1486 /* risc1 y */
1487 if (status & 0x01) {
1488 spin_lock(&dev->slock);
1489 count = cx_read(MO_VIDY_GPCNT);
1490 cx88_wakeup(core, &dev->vidq, count);
1491 spin_unlock(&dev->slock);
1492 }
1493
1494 /* risc1 vbi */
1495 if (status & 0x08) {
1496 spin_lock(&dev->slock);
1497 count = cx_read(MO_VBI_GPCNT);
1498 cx88_wakeup(core, &dev->vbiq, count);
1499 spin_unlock(&dev->slock);
1500 }
1501
1502 /* risc2 y */
1503 if (status & 0x10) {
1504 dprintk(2,"stopper video\n");
1505 spin_lock(&dev->slock);
1506 restart_video_queue(dev,&dev->vidq);
1507 spin_unlock(&dev->slock);
1508 }
1509
1510 /* risc2 vbi */
1511 if (status & 0x80) {
1512 dprintk(2,"stopper vbi\n");
1513 spin_lock(&dev->slock);
1514 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1515 spin_unlock(&dev->slock);
1516 }
1517}
1518
1519static irqreturn_t cx8800_irq(int irq, void *dev_id)
1520{
1521 struct cx8800_dev *dev = dev_id;
1522 struct cx88_core *core = dev->core;
1523 u32 status;
1524 int loop, handled = 0;
1525
1526 for (loop = 0; loop < 10; loop++) {
1527 status = cx_read(MO_PCI_INTSTAT) &
1528 (core->pci_irqmask | PCI_INT_VIDINT);
1529 if (0 == status)
1530 goto out;
1531 cx_write(MO_PCI_INTSTAT, status);
1532 handled = 1;
1533
1534 if (status & core->pci_irqmask)
1535 cx88_core_irq(core,status);
1536 if (status & PCI_INT_VIDINT)
1537 cx8800_vid_irq(dev);
1538 };
1539 if (10 == loop) {
1540 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1541 core->name);
1542 cx_write(MO_PCI_INTMSK,0);
1543 }
1544
1545 out:
1546 return IRQ_RETVAL(handled);
1547}
1548
1549/* ----------------------------------------------------------- */
1550/* exported stuff */
1551
1552static const struct v4l2_file_operations video_fops =
1553{
1554 .owner = THIS_MODULE,
1555 .open = video_open,
1556 .release = video_release,
1557 .read = video_read,
1558 .poll = video_poll,
1559 .mmap = video_mmap,
1560 .unlocked_ioctl = video_ioctl2,
1561};
1562
1563static const struct v4l2_ioctl_ops video_ioctl_ops = {
1564 .vidioc_querycap = vidioc_querycap,
1565 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1566 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1567 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1568 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1569 .vidioc_reqbufs = vidioc_reqbufs,
1570 .vidioc_querybuf = vidioc_querybuf,
1571 .vidioc_qbuf = vidioc_qbuf,
1572 .vidioc_dqbuf = vidioc_dqbuf,
1573 .vidioc_g_std = vidioc_g_std,
1574 .vidioc_s_std = vidioc_s_std,
1575 .vidioc_enum_input = vidioc_enum_input,
1576 .vidioc_g_input = vidioc_g_input,
1577 .vidioc_s_input = vidioc_s_input,
1578 .vidioc_streamon = vidioc_streamon,
1579 .vidioc_streamoff = vidioc_streamoff,
1580 .vidioc_g_tuner = vidioc_g_tuner,
1581 .vidioc_s_tuner = vidioc_s_tuner,
1582 .vidioc_g_frequency = vidioc_g_frequency,
1583 .vidioc_s_frequency = vidioc_s_frequency,
1584 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1585 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1586 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1587#ifdef CONFIG_VIDEO_ADV_DEBUG
1588 .vidioc_g_register = vidioc_g_register,
1589 .vidioc_s_register = vidioc_s_register,
1590#endif
1591};
1592
1593static const struct video_device cx8800_video_template = {
1594 .name = "cx8800-video",
1595 .fops = &video_fops,
1596 .ioctl_ops = &video_ioctl_ops,
1597 .tvnorms = CX88_NORMS,
1598};
1599
1600static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1601 .vidioc_querycap = vidioc_querycap,
1602 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1603 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1604 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
1605 .vidioc_reqbufs = vidioc_reqbufs,
1606 .vidioc_querybuf = vidioc_querybuf,
1607 .vidioc_qbuf = vidioc_qbuf,
1608 .vidioc_dqbuf = vidioc_dqbuf,
1609 .vidioc_g_std = vidioc_g_std,
1610 .vidioc_s_std = vidioc_s_std,
1611 .vidioc_enum_input = vidioc_enum_input,
1612 .vidioc_g_input = vidioc_g_input,
1613 .vidioc_s_input = vidioc_s_input,
1614 .vidioc_streamon = vidioc_streamon,
1615 .vidioc_streamoff = vidioc_streamoff,
1616 .vidioc_g_tuner = vidioc_g_tuner,
1617 .vidioc_s_tuner = vidioc_s_tuner,
1618 .vidioc_g_frequency = vidioc_g_frequency,
1619 .vidioc_s_frequency = vidioc_s_frequency,
1620 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1621#ifdef CONFIG_VIDEO_ADV_DEBUG
1622 .vidioc_g_register = vidioc_g_register,
1623 .vidioc_s_register = vidioc_s_register,
1624#endif
1625};
1626
1627static const struct video_device cx8800_vbi_template = {
1628 .name = "cx8800-vbi",
1629 .fops = &video_fops,
1630 .ioctl_ops = &vbi_ioctl_ops,
1631 .tvnorms = CX88_NORMS,
1632};
1633
1634static const struct v4l2_file_operations radio_fops =
1635{
1636 .owner = THIS_MODULE,
1637 .open = video_open,
1638 .poll = v4l2_ctrl_poll,
1639 .release = video_release,
1640 .unlocked_ioctl = video_ioctl2,
1641};
1642
1643static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1644 .vidioc_querycap = vidioc_querycap,
1645 .vidioc_g_tuner = radio_g_tuner,
1646 .vidioc_s_tuner = radio_s_tuner,
1647 .vidioc_g_frequency = vidioc_g_frequency,
1648 .vidioc_s_frequency = vidioc_s_frequency,
1649 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1650 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1651 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1652#ifdef CONFIG_VIDEO_ADV_DEBUG
1653 .vidioc_g_register = vidioc_g_register,
1654 .vidioc_s_register = vidioc_s_register,
1655#endif
1656};
1657
1658static const struct video_device cx8800_radio_template = {
1659 .name = "cx8800-radio",
1660 .fops = &radio_fops,
1661 .ioctl_ops = &radio_ioctl_ops,
1662};
1663
1664static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
1665 .s_ctrl = cx8800_s_vid_ctrl,
1666};
1667
1668static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
1669 .s_ctrl = cx8800_s_aud_ctrl,
1670};
1671
1672/* ----------------------------------------------------------- */
1673
1674static void cx8800_unregister_video(struct cx8800_dev *dev)
1675{
1676 if (dev->radio_dev) {
1677 if (video_is_registered(dev->radio_dev))
1678 video_unregister_device(dev->radio_dev);
1679 else
1680 video_device_release(dev->radio_dev);
1681 dev->radio_dev = NULL;
1682 }
1683 if (dev->vbi_dev) {
1684 if (video_is_registered(dev->vbi_dev))
1685 video_unregister_device(dev->vbi_dev);
1686 else
1687 video_device_release(dev->vbi_dev);
1688 dev->vbi_dev = NULL;
1689 }
1690 if (dev->video_dev) {
1691 if (video_is_registered(dev->video_dev))
1692 video_unregister_device(dev->video_dev);
1693 else
1694 video_device_release(dev->video_dev);
1695 dev->video_dev = NULL;
1696 }
1697}
1698
1699static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1700 const struct pci_device_id *pci_id)
1701{
1702 struct cx8800_dev *dev;
1703 struct cx88_core *core;
1704 int err;
1705 int i;
1706
1707 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1708 if (NULL == dev)
1709 return -ENOMEM;
1710
1711 /* pci init */
1712 dev->pci = pci_dev;
1713 if (pci_enable_device(pci_dev)) {
1714 err = -EIO;
1715 goto fail_free;
1716 }
1717 core = cx88_core_get(dev->pci);
1718 if (NULL == core) {
1719 err = -EINVAL;
1720 goto fail_free;
1721 }
1722 dev->core = core;
1723
1724 /* print pci info */
1725 dev->pci_rev = pci_dev->revision;
1726 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1727 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1728 "latency: %d, mmio: 0x%llx\n", core->name,
1729 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1730 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1731
1732 pci_set_master(pci_dev);
1733 if (!pci_dma_supported(pci_dev,DMA_BIT_MASK(32))) {
1734 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1735 err = -EIO;
1736 goto fail_core;
1737 }
1738
1739 /* initialize driver struct */
1740 spin_lock_init(&dev->slock);
1741 core->tvnorm = V4L2_STD_NTSC_M;
1742
1743 /* init video dma queues */
1744 INIT_LIST_HEAD(&dev->vidq.active);
1745 INIT_LIST_HEAD(&dev->vidq.queued);
1746 dev->vidq.timeout.function = cx8800_vid_timeout;
1747 dev->vidq.timeout.data = (unsigned long)dev;
1748 init_timer(&dev->vidq.timeout);
1749 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1750 MO_VID_DMACNTRL,0x11,0x00);
1751
1752 /* init vbi dma queues */
1753 INIT_LIST_HEAD(&dev->vbiq.active);
1754 INIT_LIST_HEAD(&dev->vbiq.queued);
1755 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1756 dev->vbiq.timeout.data = (unsigned long)dev;
1757 init_timer(&dev->vbiq.timeout);
1758 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1759 MO_VID_DMACNTRL,0x88,0x00);
1760
1761 /* get irq */
1762 err = request_irq(pci_dev->irq, cx8800_irq,
1763 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1764 if (err < 0) {
1765 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
1766 core->name,pci_dev->irq);
1767 goto fail_core;
1768 }
1769 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1770
1771 for (i = 0; i < CX8800_AUD_CTLS; i++) {
1772 const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
1773 struct v4l2_ctrl *vc;
1774
1775 vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
1776 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1777 if (vc == NULL) {
1778 err = core->audio_hdl.error;
1779 goto fail_core;
1780 }
1781 vc->priv = (void *)cc;
1782 }
1783
1784 for (i = 0; i < CX8800_VID_CTLS; i++) {
1785 const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
1786 struct v4l2_ctrl *vc;
1787
1788 vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
1789 cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
1790 if (vc == NULL) {
1791 err = core->video_hdl.error;
1792 goto fail_core;
1793 }
1794 vc->priv = (void *)cc;
1795 if (vc->id == V4L2_CID_CHROMA_AGC)
1796 core->chroma_agc = vc;
1797 }
1798 v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl);
1799
1800 /* load and configure helper modules */
1801
1802 if (core->board.audio_chip == V4L2_IDENT_WM8775) {
1803 struct i2c_board_info wm8775_info = {
1804 .type = "wm8775",
1805 .addr = 0x36 >> 1,
1806 .platform_data = &core->wm8775_data,
1807 };
1808 struct v4l2_subdev *sd;
1809
1810 if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
1811 core->wm8775_data.is_nova_s = true;
1812 else
1813 core->wm8775_data.is_nova_s = false;
1814
1815 sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
1816 &wm8775_info, NULL);
1817 if (sd != NULL) {
1818 core->sd_wm8775 = sd;
1819 sd->grp_id = WM8775_GID;
1820 }
1821 }
1822
1823 if (core->board.audio_chip == V4L2_IDENT_TVAUDIO) {
1824 /* This probes for a tda9874 as is used on some
1825 Pixelview Ultra boards. */
1826 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
1827 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
1828 }
1829
1830 switch (core->boardnr) {
1831 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1832 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
1833 static const struct i2c_board_info rtc_info = {
1834 I2C_BOARD_INFO("isl1208", 0x6f)
1835 };
1836
1837 request_module("rtc-isl1208");
1838 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1839 }
1840 /* break intentionally omitted */
1841 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1842 request_module("ir-kbd-i2c");
1843 }
1844
1845 /* Sets device info at pci_dev */
1846 pci_set_drvdata(pci_dev, dev);
1847
1848 dev->width = 320;
1849 dev->height = 240;
1850 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1851
1852 /* initial device configuration */
1853 mutex_lock(&core->lock);
1854 cx88_set_tvnorm(core, core->tvnorm);
1855 v4l2_ctrl_handler_setup(&core->video_hdl);
1856 v4l2_ctrl_handler_setup(&core->audio_hdl);
1857 cx88_video_mux(core, 0);
1858
1859 /* register v4l devices */
1860 dev->video_dev = cx88_vdev_init(core,dev->pci,
1861 &cx8800_video_template,"video");
1862 video_set_drvdata(dev->video_dev, dev);
1863 dev->video_dev->ctrl_handler = &core->video_hdl;
1864 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1865 video_nr[core->nr]);
1866 if (err < 0) {
1867 printk(KERN_ERR "%s/0: can't register video device\n",
1868 core->name);
1869 goto fail_unreg;
1870 }
1871 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1872 core->name, video_device_node_name(dev->video_dev));
1873
1874 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1875 video_set_drvdata(dev->vbi_dev, dev);
1876 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1877 vbi_nr[core->nr]);
1878 if (err < 0) {
1879 printk(KERN_ERR "%s/0: can't register vbi device\n",
1880 core->name);
1881 goto fail_unreg;
1882 }
1883 printk(KERN_INFO "%s/0: registered device %s\n",
1884 core->name, video_device_node_name(dev->vbi_dev));
1885
1886 if (core->board.radio.type == CX88_RADIO) {
1887 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1888 &cx8800_radio_template,"radio");
1889 video_set_drvdata(dev->radio_dev, dev);
1890 dev->radio_dev->ctrl_handler = &core->audio_hdl;
1891 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1892 radio_nr[core->nr]);
1893 if (err < 0) {
1894 printk(KERN_ERR "%s/0: can't register radio device\n",
1895 core->name);
1896 goto fail_unreg;
1897 }
1898 printk(KERN_INFO "%s/0: registered device %s\n",
1899 core->name, video_device_node_name(dev->radio_dev));
1900 }
1901
1902 /* start tvaudio thread */
1903 if (core->board.tuner_type != TUNER_ABSENT) {
1904 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1905 if (IS_ERR(core->kthread)) {
1906 err = PTR_ERR(core->kthread);
1907 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1908 core->name, err);
1909 }
1910 }
1911 mutex_unlock(&core->lock);
1912
1913 return 0;
1914
1915fail_unreg:
1916 cx8800_unregister_video(dev);
1917 free_irq(pci_dev->irq, dev);
1918 mutex_unlock(&core->lock);
1919fail_core:
1920 cx88_core_put(core,dev->pci);
1921fail_free:
1922 kfree(dev);
1923 return err;
1924}
1925
1926static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1927{
1928 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1929 struct cx88_core *core = dev->core;
1930
1931 /* stop thread */
1932 if (core->kthread) {
1933 kthread_stop(core->kthread);
1934 core->kthread = NULL;
1935 }
1936
1937 if (core->ir)
1938 cx88_ir_stop(core);
1939
1940 cx88_shutdown(core); /* FIXME */
1941 pci_disable_device(pci_dev);
1942
1943 /* unregister stuff */
1944
1945 free_irq(pci_dev->irq, dev);
1946 cx8800_unregister_video(dev);
1947 pci_set_drvdata(pci_dev, NULL);
1948
1949 /* free memory */
1950 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1951 cx88_core_put(core,dev->pci);
1952 kfree(dev);
1953}
1954
1955#ifdef CONFIG_PM
1956static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1957{
1958 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1959 struct cx88_core *core = dev->core;
1960
1961 /* stop video+vbi capture */
1962 spin_lock(&dev->slock);
1963 if (!list_empty(&dev->vidq.active)) {
1964 printk("%s/0: suspend video\n", core->name);
1965 stop_video_dma(dev);
1966 del_timer(&dev->vidq.timeout);
1967 }
1968 if (!list_empty(&dev->vbiq.active)) {
1969 printk("%s/0: suspend vbi\n", core->name);
1970 cx8800_stop_vbi_dma(dev);
1971 del_timer(&dev->vbiq.timeout);
1972 }
1973 spin_unlock(&dev->slock);
1974
1975 if (core->ir)
1976 cx88_ir_stop(core);
1977 /* FIXME -- shutdown device */
1978 cx88_shutdown(core);
1979
1980 pci_save_state(pci_dev);
1981 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1982 pci_disable_device(pci_dev);
1983 dev->state.disabled = 1;
1984 }
1985 return 0;
1986}
1987
1988static int cx8800_resume(struct pci_dev *pci_dev)
1989{
1990 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1991 struct cx88_core *core = dev->core;
1992 int err;
1993
1994 if (dev->state.disabled) {
1995 err=pci_enable_device(pci_dev);
1996 if (err) {
1997 printk(KERN_ERR "%s/0: can't enable device\n",
1998 core->name);
1999 return err;
2000 }
2001
2002 dev->state.disabled = 0;
2003 }
2004 err= pci_set_power_state(pci_dev, PCI_D0);
2005 if (err) {
2006 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
2007 pci_disable_device(pci_dev);
2008 dev->state.disabled = 1;
2009
2010 return err;
2011 }
2012 pci_restore_state(pci_dev);
2013
2014 /* FIXME: re-initialize hardware */
2015 cx88_reset(core);
2016 if (core->ir)
2017 cx88_ir_start(core);
2018
2019 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
2020
2021 /* restart video+vbi capture */
2022 spin_lock(&dev->slock);
2023 if (!list_empty(&dev->vidq.active)) {
2024 printk("%s/0: resume video\n", core->name);
2025 restart_video_queue(dev,&dev->vidq);
2026 }
2027 if (!list_empty(&dev->vbiq.active)) {
2028 printk("%s/0: resume vbi\n", core->name);
2029 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2030 }
2031 spin_unlock(&dev->slock);
2032
2033 return 0;
2034}
2035#endif
2036
2037/* ----------------------------------------------------------- */
2038
2039static const struct pci_device_id cx8800_pci_tbl[] = {
2040 {
2041 .vendor = 0x14f1,
2042 .device = 0x8800,
2043 .subvendor = PCI_ANY_ID,
2044 .subdevice = PCI_ANY_ID,
2045 },{
2046 /* --- end of list --- */
2047 }
2048};
2049MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2050
2051static struct pci_driver cx8800_pci_driver = {
2052 .name = "cx8800",
2053 .id_table = cx8800_pci_tbl,
2054 .probe = cx8800_initdev,
2055 .remove = __devexit_p(cx8800_finidev),
2056#ifdef CONFIG_PM
2057 .suspend = cx8800_suspend,
2058 .resume = cx8800_resume,
2059#endif
2060};
2061
2062static int __init cx8800_init(void)
2063{
2064 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %s loaded\n",
2065 CX88_VERSION);
2066 return pci_register_driver(&cx8800_pci_driver);
2067}
2068
2069static void __exit cx8800_fini(void)
2070{
2071 pci_unregister_driver(&cx8800_pci_driver);
2072}
2073
2074module_init(cx8800_init);
2075module_exit(cx8800_fini);
diff --git a/drivers/media/pci/cx88/cx88-vp3054-i2c.c b/drivers/media/pci/cx88/cx88-vp3054-i2c.c
new file mode 100644
index 000000000000..d77f8ecab9d7
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-vp3054-i2c.c
@@ -0,0 +1,159 @@
1/*
2
3 cx88-vp3054-i2c.c -- support for the secondary I2C bus of the
4 DNTV Live! DVB-T Pro (VP-3054), wired as:
5 GPIO[0] -> SCL, GPIO[1] -> SDA
6
7 (c) 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23*/
24
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/init.h>
28
29#include <asm/io.h>
30
31#include "cx88.h"
32#include "cx88-vp3054-i2c.h"
33
34MODULE_DESCRIPTION("driver for cx2388x VP3054 design");
35MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
36MODULE_LICENSE("GPL");
37
38/* ----------------------------------------------------------------------- */
39
40static void vp3054_bit_setscl(void *data, int state)
41{
42 struct cx8802_dev *dev = data;
43 struct cx88_core *core = dev->core;
44 struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
45
46 if (state) {
47 vp3054_i2c->state |= 0x0001; /* SCL high */
48 vp3054_i2c->state &= ~0x0100; /* external pullup */
49 } else {
50 vp3054_i2c->state &= ~0x0001; /* SCL low */
51 vp3054_i2c->state |= 0x0100; /* drive pin */
52 }
53 cx_write(MO_GP0_IO, 0x010000 | vp3054_i2c->state);
54 cx_read(MO_GP0_IO);
55}
56
57static void vp3054_bit_setsda(void *data, int state)
58{
59 struct cx8802_dev *dev = data;
60 struct cx88_core *core = dev->core;
61 struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
62
63 if (state) {
64 vp3054_i2c->state |= 0x0002; /* SDA high */
65 vp3054_i2c->state &= ~0x0200; /* tristate pin */
66 } else {
67 vp3054_i2c->state &= ~0x0002; /* SDA low */
68 vp3054_i2c->state |= 0x0200; /* drive pin */
69 }
70 cx_write(MO_GP0_IO, 0x020000 | vp3054_i2c->state);
71 cx_read(MO_GP0_IO);
72}
73
74static int vp3054_bit_getscl(void *data)
75{
76 struct cx8802_dev *dev = data;
77 struct cx88_core *core = dev->core;
78 u32 state;
79
80 state = cx_read(MO_GP0_IO);
81 return (state & 0x01) ? 1 : 0;
82}
83
84static int vp3054_bit_getsda(void *data)
85{
86 struct cx8802_dev *dev = data;
87 struct cx88_core *core = dev->core;
88 u32 state;
89
90 state = cx_read(MO_GP0_IO);
91 return (state & 0x02) ? 1 : 0;
92}
93
94/* ----------------------------------------------------------------------- */
95
96static const struct i2c_algo_bit_data vp3054_i2c_algo_template = {
97 .setsda = vp3054_bit_setsda,
98 .setscl = vp3054_bit_setscl,
99 .getsda = vp3054_bit_getsda,
100 .getscl = vp3054_bit_getscl,
101 .udelay = 16,
102 .timeout = 200,
103};
104
105/* ----------------------------------------------------------------------- */
106
107int vp3054_i2c_probe(struct cx8802_dev *dev)
108{
109 struct cx88_core *core = dev->core;
110 struct vp3054_i2c_state *vp3054_i2c;
111 int rc;
112
113 if (core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
114 return 0;
115
116 vp3054_i2c = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL);
117 if (vp3054_i2c == NULL)
118 return -ENOMEM;
119 dev->vp3054 = vp3054_i2c;
120
121 memcpy(&vp3054_i2c->algo, &vp3054_i2c_algo_template,
122 sizeof(vp3054_i2c->algo));
123
124 vp3054_i2c->adap.dev.parent = &dev->pci->dev;
125 strlcpy(vp3054_i2c->adap.name, core->name,
126 sizeof(vp3054_i2c->adap.name));
127 vp3054_i2c->adap.owner = THIS_MODULE;
128 vp3054_i2c->algo.data = dev;
129 i2c_set_adapdata(&vp3054_i2c->adap, dev);
130 vp3054_i2c->adap.algo_data = &vp3054_i2c->algo;
131
132 vp3054_bit_setscl(dev,1);
133 vp3054_bit_setsda(dev,1);
134
135 rc = i2c_bit_add_bus(&vp3054_i2c->adap);
136 if (0 != rc) {
137 printk("%s: vp3054_i2c register FAILED\n", core->name);
138
139 kfree(dev->vp3054);
140 dev->vp3054 = NULL;
141 }
142
143 return rc;
144}
145
146void vp3054_i2c_remove(struct cx8802_dev *dev)
147{
148 struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
149
150 if (vp3054_i2c == NULL ||
151 dev->core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
152 return;
153
154 i2c_del_adapter(&vp3054_i2c->adap);
155 kfree(vp3054_i2c);
156}
157
158EXPORT_SYMBOL(vp3054_i2c_probe);
159EXPORT_SYMBOL(vp3054_i2c_remove);
diff --git a/drivers/media/pci/cx88/cx88-vp3054-i2c.h b/drivers/media/pci/cx88/cx88-vp3054-i2c.h
new file mode 100644
index 000000000000..be99c931dc3e
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88-vp3054-i2c.h
@@ -0,0 +1,41 @@
1/*
2
3 cx88-vp3054-i2c.h -- support for the secondary I2C bus of the
4 DNTV Live! DVB-T Pro (VP-3054), wired as:
5 GPIO[0] -> SCL, GPIO[1] -> SDA
6
7 (c) 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23*/
24
25/* ----------------------------------------------------------------------- */
26struct vp3054_i2c_state {
27 struct i2c_adapter adap;
28 struct i2c_algo_bit_data algo;
29 u32 state;
30};
31
32/* ----------------------------------------------------------------------- */
33#if defined(CONFIG_VIDEO_CX88_VP3054) || (defined(CONFIG_VIDEO_CX88_VP3054_MODULE) && defined(MODULE))
34int vp3054_i2c_probe(struct cx8802_dev *dev);
35void vp3054_i2c_remove(struct cx8802_dev *dev);
36#else
37static inline int vp3054_i2c_probe(struct cx8802_dev *dev)
38{ return 0; }
39static inline void vp3054_i2c_remove(struct cx8802_dev *dev)
40{ }
41#endif
diff --git a/drivers/media/pci/cx88/cx88.h b/drivers/media/pci/cx88/cx88.h
new file mode 100644
index 000000000000..44ffc8b3d45f
--- /dev/null
+++ b/drivers/media/pci/cx88/cx88.h
@@ -0,0 +1,748 @@
1/*
2 *
3 * v4l2 device driver for cx2388x based TV cards
4 *
5 * (c) 2003,04 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/pci.h>
23#include <linux/i2c.h>
24#include <linux/i2c-algo-bit.h>
25#include <linux/videodev2.h>
26#include <linux/kdev_t.h>
27
28#include <media/v4l2-device.h>
29#include <media/v4l2-fh.h>
30#include <media/tuner.h>
31#include <media/tveeprom.h>
32#include <media/videobuf-dma-sg.h>
33#include <media/v4l2-chip-ident.h>
34#include <media/cx2341x.h>
35#include <media/videobuf-dvb.h>
36#include <media/ir-kbd-i2c.h>
37#include <media/wm8775.h>
38
39#include "btcx-risc.h"
40#include "cx88-reg.h"
41#include "tuner-xc2028.h"
42
43#include <linux/mutex.h>
44
45#define CX88_VERSION "0.0.9"
46
47#define UNSET (-1U)
48
49#define CX88_MAXBOARDS 8
50
51/* Max number of inputs by card */
52#define MAX_CX88_INPUT 8
53
54/* ----------------------------------------------------------- */
55/* defines and enums */
56
57/* Currently unsupported by the driver: PAL/H, NTSC/Kr, SECAM/LC */
58#define CX88_NORMS (V4L2_STD_ALL \
59 & ~V4L2_STD_PAL_H \
60 & ~V4L2_STD_NTSC_M_KR \
61 & ~V4L2_STD_SECAM_LC)
62
63#define FORMAT_FLAGS_PACKED 0x01
64#define FORMAT_FLAGS_PLANAR 0x02
65
66#define VBI_LINE_COUNT 17
67#define VBI_LINE_LENGTH 2048
68
69#define AUD_RDS_LINES 4
70
71/* need "shadow" registers for some write-only ones ... */
72#define SHADOW_AUD_VOL_CTL 1
73#define SHADOW_AUD_BAL_CTL 2
74#define SHADOW_MAX 3
75
76/* FM Radio deemphasis type */
77enum cx88_deemph_type {
78 FM_NO_DEEMPH = 0,
79 FM_DEEMPH_50,
80 FM_DEEMPH_75
81};
82
83enum cx88_board_type {
84 CX88_BOARD_NONE = 0,
85 CX88_MPEG_DVB,
86 CX88_MPEG_BLACKBIRD
87};
88
89enum cx8802_board_access {
90 CX8802_DRVCTL_SHARED = 1,
91 CX8802_DRVCTL_EXCLUSIVE = 2,
92};
93
94/* ----------------------------------------------------------- */
95/* tv norms */
96
97static unsigned int inline norm_maxw(v4l2_std_id norm)
98{
99 return (norm & (V4L2_STD_MN & ~V4L2_STD_PAL_Nc)) ? 720 : 768;
100}
101
102
103static unsigned int inline norm_maxh(v4l2_std_id norm)
104{
105 return (norm & V4L2_STD_625_50) ? 576 : 480;
106}
107
108/* ----------------------------------------------------------- */
109/* static data */
110
111struct cx8800_fmt {
112 const char *name;
113 u32 fourcc; /* v4l2 format id */
114 int depth;
115 int flags;
116 u32 cxformat;
117};
118
119/* ----------------------------------------------------------- */
120/* SRAM memory management data (see cx88-core.c) */
121
122#define SRAM_CH21 0 /* video */
123#define SRAM_CH22 1
124#define SRAM_CH23 2
125#define SRAM_CH24 3 /* vbi */
126#define SRAM_CH25 4 /* audio */
127#define SRAM_CH26 5
128#define SRAM_CH28 6 /* mpeg */
129#define SRAM_CH27 7 /* audio rds */
130/* more */
131
132struct sram_channel {
133 const char *name;
134 u32 cmds_start;
135 u32 ctrl_start;
136 u32 cdt;
137 u32 fifo_start;
138 u32 fifo_size;
139 u32 ptr1_reg;
140 u32 ptr2_reg;
141 u32 cnt1_reg;
142 u32 cnt2_reg;
143};
144extern const struct sram_channel cx88_sram_channels[];
145
146/* ----------------------------------------------------------- */
147/* card configuration */
148
149#define CX88_BOARD_NOAUTO UNSET
150#define CX88_BOARD_UNKNOWN 0
151#define CX88_BOARD_HAUPPAUGE 1
152#define CX88_BOARD_GDI 2
153#define CX88_BOARD_PIXELVIEW 3
154#define CX88_BOARD_ATI_WONDER_PRO 4
155#define CX88_BOARD_WINFAST2000XP_EXPERT 5
156#define CX88_BOARD_AVERTV_STUDIO_303 6
157#define CX88_BOARD_MSI_TVANYWHERE_MASTER 7
158#define CX88_BOARD_WINFAST_DV2000 8
159#define CX88_BOARD_LEADTEK_PVR2000 9
160#define CX88_BOARD_IODATA_GVVCP3PCI 10
161#define CX88_BOARD_PROLINK_PLAYTVPVR 11
162#define CX88_BOARD_ASUS_PVR_416 12
163#define CX88_BOARD_MSI_TVANYWHERE 13
164#define CX88_BOARD_KWORLD_DVB_T 14
165#define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1 15
166#define CX88_BOARD_KWORLD_LTV883 16
167#define CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q 17
168#define CX88_BOARD_HAUPPAUGE_DVB_T1 18
169#define CX88_BOARD_CONEXANT_DVB_T1 19
170#define CX88_BOARD_PROVIDEO_PV259 20
171#define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS 21
172#define CX88_BOARD_PCHDTV_HD3000 22
173#define CX88_BOARD_DNTV_LIVE_DVB_T 23
174#define CX88_BOARD_HAUPPAUGE_ROSLYN 24
175#define CX88_BOARD_DIGITALLOGIC_MEC 25
176#define CX88_BOARD_IODATA_GVBCTV7E 26
177#define CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO 27
178#define CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T 28
179#define CX88_BOARD_ADSTECH_DVB_T_PCI 29
180#define CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1 30
181#define CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD 31
182#define CX88_BOARD_AVERMEDIA_ULTRATV_MC_550 32
183#define CX88_BOARD_KWORLD_VSTREAM_EXPERT_DVD 33
184#define CX88_BOARD_ATI_HDTVWONDER 34
185#define CX88_BOARD_WINFAST_DTV1000 35
186#define CX88_BOARD_AVERTV_303 36
187#define CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1 37
188#define CX88_BOARD_HAUPPAUGE_NOVASE2_S1 38
189#define CX88_BOARD_KWORLD_DVBS_100 39
190#define CX88_BOARD_HAUPPAUGE_HVR1100 40
191#define CX88_BOARD_HAUPPAUGE_HVR1100LP 41
192#define CX88_BOARD_DNTV_LIVE_DVB_T_PRO 42
193#define CX88_BOARD_KWORLD_DVB_T_CX22702 43
194#define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL 44
195#define CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT 45
196#define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID 46
197#define CX88_BOARD_PCHDTV_HD5500 47
198#define CX88_BOARD_KWORLD_MCE200_DELUXE 48
199#define CX88_BOARD_PIXELVIEW_PLAYTV_P7000 49
200#define CX88_BOARD_NPGTECH_REALTV_TOP10FM 50
201#define CX88_BOARD_WINFAST_DTV2000H 51
202#define CX88_BOARD_GENIATECH_DVBS 52
203#define CX88_BOARD_HAUPPAUGE_HVR3000 53
204#define CX88_BOARD_NORWOOD_MICRO 54
205#define CX88_BOARD_TE_DTV_250_OEM_SWANN 55
206#define CX88_BOARD_HAUPPAUGE_HVR1300 56
207#define CX88_BOARD_ADSTECH_PTV_390 57
208#define CX88_BOARD_PINNACLE_PCTV_HD_800i 58
209#define CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO 59
210#define CX88_BOARD_PINNACLE_HYBRID_PCTV 60
211#define CX88_BOARD_WINFAST_TV2000_XP_GLOBAL 61
212#define CX88_BOARD_POWERCOLOR_REAL_ANGEL 62
213#define CX88_BOARD_GENIATECH_X8000_MT 63
214#define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PRO 64
215#define CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD 65
216#define CX88_BOARD_PROLINK_PV_8000GT 66
217#define CX88_BOARD_KWORLD_ATSC_120 67
218#define CX88_BOARD_HAUPPAUGE_HVR4000 68
219#define CX88_BOARD_HAUPPAUGE_HVR4000LITE 69
220#define CX88_BOARD_TEVII_S460 70
221#define CX88_BOARD_OMICOM_SS4_PCI 71
222#define CX88_BOARD_TBS_8920 72
223#define CX88_BOARD_TEVII_S420 73
224#define CX88_BOARD_PROLINK_PV_GLOBAL_XTREME 74
225#define CX88_BOARD_PROF_7300 75
226#define CX88_BOARD_SATTRADE_ST4200 76
227#define CX88_BOARD_TBS_8910 77
228#define CX88_BOARD_PROF_6200 78
229#define CX88_BOARD_TERRATEC_CINERGY_HT_PCI_MKII 79
230#define CX88_BOARD_HAUPPAUGE_IRONLY 80
231#define CX88_BOARD_WINFAST_DTV1800H 81
232#define CX88_BOARD_WINFAST_DTV2000H_J 82
233#define CX88_BOARD_PROF_7301 83
234#define CX88_BOARD_SAMSUNG_SMT_7020 84
235#define CX88_BOARD_TWINHAN_VP1027_DVBS 85
236#define CX88_BOARD_TEVII_S464 86
237#define CX88_BOARD_WINFAST_DTV2000H_PLUS 87
238#define CX88_BOARD_WINFAST_DTV1800H_XC4000 88
239#define CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F36 89
240#define CX88_BOARD_WINFAST_TV2000_XP_GLOBAL_6F43 90
241
242enum cx88_itype {
243 CX88_VMUX_COMPOSITE1 = 1,
244 CX88_VMUX_COMPOSITE2,
245 CX88_VMUX_COMPOSITE3,
246 CX88_VMUX_COMPOSITE4,
247 CX88_VMUX_SVIDEO,
248 CX88_VMUX_TELEVISION,
249 CX88_VMUX_CABLE,
250 CX88_VMUX_DVB,
251 CX88_VMUX_DEBUG,
252 CX88_RADIO,
253};
254
255struct cx88_input {
256 enum cx88_itype type;
257 u32 gpio0, gpio1, gpio2, gpio3;
258 unsigned int vmux:2;
259 unsigned int audioroute:4;
260};
261
262struct cx88_board {
263 const char *name;
264 unsigned int tuner_type;
265 unsigned int radio_type;
266 unsigned char tuner_addr;
267 unsigned char radio_addr;
268 int tda9887_conf;
269 struct cx88_input input[MAX_CX88_INPUT];
270 struct cx88_input radio;
271 enum cx88_board_type mpeg;
272 unsigned int audio_chip;
273 int num_frontends;
274
275 /* Used for I2S devices */
276 int i2sinputcntl;
277};
278
279struct cx88_subid {
280 u16 subvendor;
281 u16 subdevice;
282 u32 card;
283};
284
285enum cx88_tvaudio {
286 WW_NONE = 1,
287 WW_BTSC,
288 WW_BG,
289 WW_DK,
290 WW_I,
291 WW_L,
292 WW_EIAJ,
293 WW_I2SPT,
294 WW_FM,
295 WW_I2SADC,
296 WW_M
297};
298
299#define INPUT(nr) (core->board.input[nr])
300
301/* ----------------------------------------------------------- */
302/* device / file handle status */
303
304#define RESOURCE_OVERLAY 1
305#define RESOURCE_VIDEO 2
306#define RESOURCE_VBI 4
307
308#define BUFFER_TIMEOUT msecs_to_jiffies(2000)
309
310/* buffer for one video frame */
311struct cx88_buffer {
312 /* common v4l buffer stuff -- must be first */
313 struct videobuf_buffer vb;
314
315 /* cx88 specific */
316 unsigned int bpl;
317 struct btcx_riscmem risc;
318 const struct cx8800_fmt *fmt;
319 u32 count;
320};
321
322struct cx88_dmaqueue {
323 struct list_head active;
324 struct list_head queued;
325 struct timer_list timeout;
326 struct btcx_riscmem stopper;
327 u32 count;
328};
329
330struct cx88_core {
331 struct list_head devlist;
332 atomic_t refcount;
333
334 /* board name */
335 int nr;
336 char name[32];
337
338 /* pci stuff */
339 int pci_bus;
340 int pci_slot;
341 u32 __iomem *lmmio;
342 u8 __iomem *bmmio;
343 u32 shadow[SHADOW_MAX];
344 int pci_irqmask;
345
346 /* i2c i/o */
347 struct i2c_adapter i2c_adap;
348 struct i2c_algo_bit_data i2c_algo;
349 struct i2c_client i2c_client;
350 u32 i2c_state, i2c_rc;
351
352 /* config info -- analog */
353 struct v4l2_device v4l2_dev;
354 struct v4l2_ctrl_handler video_hdl;
355 struct v4l2_ctrl *chroma_agc;
356 struct v4l2_ctrl_handler audio_hdl;
357 struct v4l2_subdev *sd_wm8775;
358 struct i2c_client *i2c_rtc;
359 unsigned int boardnr;
360 struct cx88_board board;
361
362 /* Supported V4L _STD_ tuner formats */
363 unsigned int tuner_formats;
364
365 /* config info -- dvb */
366#if defined(CONFIG_VIDEO_CX88_DVB) || defined(CONFIG_VIDEO_CX88_DVB_MODULE)
367 int (*prev_set_voltage)(struct dvb_frontend *fe, fe_sec_voltage_t voltage);
368#endif
369 void (*gate_ctrl)(struct cx88_core *core, int open);
370
371 /* state info */
372 struct task_struct *kthread;
373 v4l2_std_id tvnorm;
374 enum cx88_tvaudio tvaudio;
375 u32 audiomode_manual;
376 u32 audiomode_current;
377 u32 input;
378 u32 last_analog_input;
379 u32 astat;
380 u32 use_nicam;
381 unsigned long last_change;
382
383 /* IR remote control state */
384 struct cx88_IR *ir;
385
386 /* I2C remote data */
387 struct IR_i2c_init_data init_data;
388 struct wm8775_platform_data wm8775_data;
389
390 struct mutex lock;
391 /* various v4l controls */
392 u32 freq;
393 int users;
394 int mpeg_users;
395
396 /* cx88-video needs to access cx8802 for hybrid tuner pll access. */
397 struct cx8802_dev *dvbdev;
398 enum cx88_board_type active_type_id;
399 int active_ref;
400 int active_fe_id;
401};
402
403static inline struct cx88_core *to_core(struct v4l2_device *v4l2_dev)
404{
405 return container_of(v4l2_dev, struct cx88_core, v4l2_dev);
406}
407
408#define call_hw(core, grpid, o, f, args...) \
409 do { \
410 if (!core->i2c_rc) { \
411 if (core->gate_ctrl) \
412 core->gate_ctrl(core, 1); \
413 v4l2_device_call_all(&core->v4l2_dev, grpid, o, f, ##args); \
414 if (core->gate_ctrl) \
415 core->gate_ctrl(core, 0); \
416 } \
417 } while (0)
418
419#define call_all(core, o, f, args...) call_hw(core, 0, o, f, ##args)
420
421#define WM8775_GID (1 << 0)
422
423#define wm8775_s_ctrl(core, id, val) \
424 do { \
425 struct v4l2_ctrl *ctrl_ = \
426 v4l2_ctrl_find(core->sd_wm8775->ctrl_handler, id); \
427 if (ctrl_ && !core->i2c_rc) { \
428 if (core->gate_ctrl) \
429 core->gate_ctrl(core, 1); \
430 v4l2_ctrl_s_ctrl(ctrl_, val); \
431 if (core->gate_ctrl) \
432 core->gate_ctrl(core, 0); \
433 } \
434 } while (0)
435
436#define wm8775_g_ctrl(core, id) \
437 ({ \
438 struct v4l2_ctrl *ctrl_ = \
439 v4l2_ctrl_find(core->sd_wm8775->ctrl_handler, id); \
440 s32 val = 0; \
441 if (ctrl_ && !core->i2c_rc) { \
442 if (core->gate_ctrl) \
443 core->gate_ctrl(core, 1); \
444 val = v4l2_ctrl_g_ctrl(ctrl_); \
445 if (core->gate_ctrl) \
446 core->gate_ctrl(core, 0); \
447 } \
448 val; \
449 })
450
451struct cx8800_dev;
452struct cx8802_dev;
453
454/* ----------------------------------------------------------- */
455/* function 0: video stuff */
456
457struct cx8800_fh {
458 struct v4l2_fh fh;
459 struct cx8800_dev *dev;
460 unsigned int resources;
461
462 /* video capture */
463 struct videobuf_queue vidq;
464
465 /* vbi capture */
466 struct videobuf_queue vbiq;
467};
468
469struct cx8800_suspend_state {
470 int disabled;
471};
472
473struct cx8800_dev {
474 struct cx88_core *core;
475 spinlock_t slock;
476
477 /* various device info */
478 unsigned int resources;
479 struct video_device *video_dev;
480 struct video_device *vbi_dev;
481 struct video_device *radio_dev;
482
483 /* pci i/o */
484 struct pci_dev *pci;
485 unsigned char pci_rev,pci_lat;
486
487 const struct cx8800_fmt *fmt;
488 unsigned int width, height;
489
490 /* capture queues */
491 struct cx88_dmaqueue vidq;
492 struct cx88_dmaqueue vbiq;
493
494 /* various v4l controls */
495
496 /* other global state info */
497 struct cx8800_suspend_state state;
498};
499
500/* ----------------------------------------------------------- */
501/* function 1: audio/alsa stuff */
502/* =============> moved to cx88-alsa.c <====================== */
503
504
505/* ----------------------------------------------------------- */
506/* function 2: mpeg stuff */
507
508struct cx8802_fh {
509 struct v4l2_fh fh;
510 struct cx8802_dev *dev;
511 struct videobuf_queue mpegq;
512};
513
514struct cx8802_suspend_state {
515 int disabled;
516};
517
518struct cx8802_driver {
519 struct cx88_core *core;
520
521 /* List of drivers attached to device */
522 struct list_head drvlist;
523
524 /* Type of driver and access required */
525 enum cx88_board_type type_id;
526 enum cx8802_board_access hw_access;
527
528 /* MPEG 8802 internal only */
529 int (*suspend)(struct pci_dev *pci_dev, pm_message_t state);
530 int (*resume)(struct pci_dev *pci_dev);
531
532 /* Callers to the following functions must hold core->lock */
533
534 /* MPEG 8802 -> mini driver - Driver probe and configuration */
535 int (*probe)(struct cx8802_driver *drv);
536 int (*remove)(struct cx8802_driver *drv);
537
538 /* MPEG 8802 -> mini driver - Access for hardware control */
539 int (*advise_acquire)(struct cx8802_driver *drv);
540 int (*advise_release)(struct cx8802_driver *drv);
541
542 /* MPEG 8802 <- mini driver - Access for hardware control */
543 int (*request_acquire)(struct cx8802_driver *drv);
544 int (*request_release)(struct cx8802_driver *drv);
545};
546
547struct cx8802_dev {
548 struct cx88_core *core;
549 spinlock_t slock;
550
551 /* pci i/o */
552 struct pci_dev *pci;
553 unsigned char pci_rev,pci_lat;
554
555 /* dma queues */
556 struct cx88_dmaqueue mpegq;
557 u32 ts_packet_size;
558 u32 ts_packet_count;
559
560 /* other global state info */
561 struct cx8802_suspend_state state;
562
563 /* for blackbird only */
564 struct list_head devlist;
565#if defined(CONFIG_VIDEO_CX88_BLACKBIRD) || \
566 defined(CONFIG_VIDEO_CX88_BLACKBIRD_MODULE)
567 struct video_device *mpeg_dev;
568 u32 mailbox;
569 int width;
570 int height;
571 unsigned char mpeg_active; /* nonzero if mpeg encoder is active */
572
573 /* mpeg params */
574 struct cx2341x_handler cxhdl;
575#endif
576
577#if defined(CONFIG_VIDEO_CX88_DVB) || defined(CONFIG_VIDEO_CX88_DVB_MODULE)
578 /* for dvb only */
579 struct videobuf_dvb_frontends frontends;
580#endif
581
582#if defined(CONFIG_VIDEO_CX88_VP3054) || \
583 defined(CONFIG_VIDEO_CX88_VP3054_MODULE)
584 /* For VP3045 secondary I2C bus support */
585 struct vp3054_i2c_state *vp3054;
586#endif
587 /* for switching modulation types */
588 unsigned char ts_gen_cntrl;
589
590 /* List of attached drivers; must hold core->lock to access */
591 struct list_head drvlist;
592
593 struct work_struct request_module_wk;
594};
595
596/* ----------------------------------------------------------- */
597
598#define cx_read(reg) readl(core->lmmio + ((reg)>>2))
599#define cx_write(reg,value) writel((value), core->lmmio + ((reg)>>2))
600#define cx_writeb(reg,value) writeb((value), core->bmmio + (reg))
601
602#define cx_andor(reg,mask,value) \
603 writel((readl(core->lmmio+((reg)>>2)) & ~(mask)) |\
604 ((value) & (mask)), core->lmmio+((reg)>>2))
605#define cx_set(reg,bit) cx_andor((reg),(bit),(bit))
606#define cx_clear(reg,bit) cx_andor((reg),(bit),0)
607
608#define cx_wait(d) { if (need_resched()) schedule(); else udelay(d); }
609
610/* shadow registers */
611#define cx_sread(sreg) (core->shadow[sreg])
612#define cx_swrite(sreg,reg,value) \
613 (core->shadow[sreg] = value, \
614 writel(core->shadow[sreg], core->lmmio + ((reg)>>2)))
615#define cx_sandor(sreg,reg,mask,value) \
616 (core->shadow[sreg] = (core->shadow[sreg] & ~(mask)) | ((value) & (mask)), \
617 writel(core->shadow[sreg], core->lmmio + ((reg)>>2)))
618
619/* ----------------------------------------------------------- */
620/* cx88-core.c */
621
622extern void cx88_print_irqbits(const char *name, const char *tag, const char *strings[],
623 int len, u32 bits, u32 mask);
624
625extern int cx88_core_irq(struct cx88_core *core, u32 status);
626extern void cx88_wakeup(struct cx88_core *core,
627 struct cx88_dmaqueue *q, u32 count);
628extern void cx88_shutdown(struct cx88_core *core);
629extern int cx88_reset(struct cx88_core *core);
630
631extern int
632cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
633 struct scatterlist *sglist,
634 unsigned int top_offset, unsigned int bottom_offset,
635 unsigned int bpl, unsigned int padding, unsigned int lines);
636extern int
637cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
638 struct scatterlist *sglist, unsigned int bpl,
639 unsigned int lines, unsigned int lpi);
640extern int
641cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
642 u32 reg, u32 mask, u32 value);
643extern void
644cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf);
645
646extern void cx88_risc_disasm(struct cx88_core *core,
647 struct btcx_riscmem *risc);
648extern int cx88_sram_channel_setup(struct cx88_core *core,
649 const struct sram_channel *ch,
650 unsigned int bpl, u32 risc);
651extern void cx88_sram_channel_dump(struct cx88_core *core,
652 const struct sram_channel *ch);
653
654extern int cx88_set_scale(struct cx88_core *core, unsigned int width,
655 unsigned int height, enum v4l2_field field);
656extern int cx88_set_tvnorm(struct cx88_core *core, v4l2_std_id norm);
657
658extern struct video_device *cx88_vdev_init(struct cx88_core *core,
659 struct pci_dev *pci,
660 const struct video_device *template_,
661 const char *type);
662extern struct cx88_core* cx88_core_get(struct pci_dev *pci);
663extern void cx88_core_put(struct cx88_core *core,
664 struct pci_dev *pci);
665
666extern int cx88_start_audio_dma(struct cx88_core *core);
667extern int cx88_stop_audio_dma(struct cx88_core *core);
668
669
670/* ----------------------------------------------------------- */
671/* cx88-vbi.c */
672
673/* Can be used as g_vbi_fmt, try_vbi_fmt and s_vbi_fmt */
674int cx8800_vbi_fmt (struct file *file, void *priv,
675 struct v4l2_format *f);
676
677/*
678int cx8800_start_vbi_dma(struct cx8800_dev *dev,
679 struct cx88_dmaqueue *q,
680 struct cx88_buffer *buf);
681*/
682int cx8800_stop_vbi_dma(struct cx8800_dev *dev);
683int cx8800_restart_vbi_queue(struct cx8800_dev *dev,
684 struct cx88_dmaqueue *q);
685void cx8800_vbi_timeout(unsigned long data);
686
687extern const struct videobuf_queue_ops cx8800_vbi_qops;
688
689/* ----------------------------------------------------------- */
690/* cx88-i2c.c */
691
692extern int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci);
693
694
695/* ----------------------------------------------------------- */
696/* cx88-cards.c */
697
698extern int cx88_tuner_callback(void *dev, int component, int command, int arg);
699extern int cx88_get_resources(const struct cx88_core *core,
700 struct pci_dev *pci);
701extern struct cx88_core *cx88_core_create(struct pci_dev *pci, int nr);
702extern void cx88_setup_xc3028(struct cx88_core *core, struct xc2028_ctrl *ctl);
703
704/* ----------------------------------------------------------- */
705/* cx88-tvaudio.c */
706
707void cx88_set_tvaudio(struct cx88_core *core);
708void cx88_newstation(struct cx88_core *core);
709void cx88_get_stereo(struct cx88_core *core, struct v4l2_tuner *t);
710void cx88_set_stereo(struct cx88_core *core, u32 mode, int manual);
711int cx88_audio_thread(void *data);
712
713int cx8802_register_driver(struct cx8802_driver *drv);
714int cx8802_unregister_driver(struct cx8802_driver *drv);
715
716/* Caller must hold core->lock */
717struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype);
718
719/* ----------------------------------------------------------- */
720/* cx88-dsp.c */
721
722s32 cx88_dsp_detect_stereo_sap(struct cx88_core *core);
723
724/* ----------------------------------------------------------- */
725/* cx88-input.c */
726
727int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci);
728int cx88_ir_fini(struct cx88_core *core);
729void cx88_ir_irq(struct cx88_core *core);
730int cx88_ir_start(struct cx88_core *core);
731void cx88_ir_stop(struct cx88_core *core);
732extern void cx88_i2c_init_ir(struct cx88_core *core);
733
734/* ----------------------------------------------------------- */
735/* cx88-mpeg.c */
736
737int cx8802_buf_prepare(struct videobuf_queue *q,struct cx8802_dev *dev,
738 struct cx88_buffer *buf, enum v4l2_field field);
739void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf);
740void cx8802_cancel_buffers(struct cx8802_dev *dev);
741
742/* ----------------------------------------------------------- */
743/* cx88-video.c*/
744int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i);
745int cx88_set_freq (struct cx88_core *core,struct v4l2_frequency *f);
746int cx88_video_mux(struct cx88_core *core, unsigned int input);
747void cx88_querycap(struct file *file, struct cx88_core *core,
748 struct v4l2_capability *cap);
diff --git a/drivers/media/pci/ivtv/Kconfig b/drivers/media/pci/ivtv/Kconfig
new file mode 100644
index 000000000000..89f65914cc8e
--- /dev/null
+++ b/drivers/media/pci/ivtv/Kconfig
@@ -0,0 +1,45 @@
1config VIDEO_IVTV
2 tristate "Conexant cx23416/cx23415 MPEG encoder/decoder support"
3 depends on VIDEO_V4L2 && PCI && I2C
4 select I2C_ALGOBIT
5 depends on RC_CORE
6 select VIDEO_TUNER
7 select VIDEO_TVEEPROM
8 select VIDEO_CX2341X
9 select VIDEO_CX25840
10 select VIDEO_MSP3400
11 select VIDEO_SAA711X
12 select VIDEO_SAA717X
13 select VIDEO_SAA7127
14 select VIDEO_CS53L32A
15 select VIDEO_M52790
16 select VIDEO_WM8775
17 select VIDEO_WM8739
18 select VIDEO_VP27SMPX
19 select VIDEO_UPD64031A
20 select VIDEO_UPD64083
21 ---help---
22 This is a video4linux driver for Conexant cx23416 or cx23415 based
23 PCI personal video recorder devices.
24
25 This is used in devices such as the Hauppauge PVR-150/250/350/500
26 cards. There is a driver homepage at <http://www.ivtvdriver.org>.
27
28 To compile this driver as a module, choose M here: the
29 module will be called ivtv.
30
31config VIDEO_FB_IVTV
32 tristate "Conexant cx23415 framebuffer support"
33 depends on VIDEO_IVTV && FB
34 select FB_CFB_FILLRECT
35 select FB_CFB_COPYAREA
36 select FB_CFB_IMAGEBLIT
37 ---help---
38 This is a framebuffer driver for the Conexant cx23415 MPEG
39 encoder/decoder.
40
41 This is used in the Hauppauge PVR-350 card. There is a driver
42 homepage at <http://www.ivtvdriver.org>.
43
44 To compile this driver as a module, choose M here: the
45 module will be called ivtvfb.
diff --git a/drivers/media/pci/ivtv/Makefile b/drivers/media/pci/ivtv/Makefile
new file mode 100644
index 000000000000..80b4ec18475d
--- /dev/null
+++ b/drivers/media/pci/ivtv/Makefile
@@ -0,0 +1,14 @@
1ivtv-objs := ivtv-routing.o ivtv-cards.o ivtv-controls.o \
2 ivtv-driver.o ivtv-fileops.o ivtv-firmware.o \
3 ivtv-gpio.o ivtv-i2c.o ivtv-ioctl.o ivtv-irq.o \
4 ivtv-mailbox.o ivtv-queue.o ivtv-streams.o ivtv-udma.o \
5 ivtv-vbi.o ivtv-yuv.o
6
7obj-$(CONFIG_VIDEO_IVTV) += ivtv.o
8obj-$(CONFIG_VIDEO_FB_IVTV) += ivtvfb.o
9
10ccflags-y += -I$(srctree)/drivers/media/video
11ccflags-y += -I$(srctree)/drivers/media/tuners
12ccflags-y += -I$(srctree)/drivers/media/dvb-core
13ccflags-y += -I$(srctree)/drivers/media/dvb-frontends
14
diff --git a/drivers/media/pci/ivtv/ivtv-cards.c b/drivers/media/pci/ivtv/ivtv-cards.c
new file mode 100644
index 000000000000..145e4749a69d
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-cards.c
@@ -0,0 +1,1370 @@
1/*
2 Functions to query card hardware
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "ivtv-driver.h"
22#include "ivtv-cards.h"
23#include "ivtv-i2c.h"
24
25#include <media/msp3400.h>
26#include <media/m52790.h>
27#include <media/wm8775.h>
28#include <media/cs53l32a.h>
29#include <media/cx25840.h>
30#include <media/upd64031a.h>
31
32#define MSP_TUNER MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1, \
33 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER)
34#define MSP_SCART1 MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1, \
35 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART)
36#define MSP_SCART2 MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1, \
37 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART)
38#define MSP_SCART3 MSP_INPUT(MSP_IN_SCART3, MSP_IN_TUNER1, \
39 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART)
40#define MSP_MONO MSP_INPUT(MSP_IN_MONO, MSP_IN_TUNER1, \
41 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART)
42
43#define V4L2_STD_PAL_SECAM (V4L2_STD_PAL|V4L2_STD_SECAM)
44
45/* usual i2c tuner addresses to probe */
46static struct ivtv_card_tuner_i2c ivtv_i2c_std = {
47 .radio = { I2C_CLIENT_END },
48 .demod = { 0x43, I2C_CLIENT_END },
49 .tv = { 0x61, 0x60, I2C_CLIENT_END },
50};
51
52/* as above, but with possible radio tuner */
53static struct ivtv_card_tuner_i2c ivtv_i2c_radio = {
54 .radio = { 0x60, I2C_CLIENT_END },
55 .demod = { 0x43, I2C_CLIENT_END },
56 .tv = { 0x61, I2C_CLIENT_END },
57};
58
59/* using the tda8290+75a combo */
60static struct ivtv_card_tuner_i2c ivtv_i2c_tda8290 = {
61 .radio = { I2C_CLIENT_END },
62 .demod = { I2C_CLIENT_END },
63 .tv = { 0x4b, I2C_CLIENT_END },
64};
65
66/********************** card configuration *******************************/
67
68/* Please add new PCI IDs to: http://pci-ids.ucw.cz/
69 This keeps the PCI ID database up to date. Note that the entries
70 must be added under vendor 0x4444 (Conexant) as subsystem IDs.
71 New vendor IDs should still be added to the vendor ID list. */
72
73/* Hauppauge PVR-250 cards */
74
75/* Note: for Hauppauge cards the tveeprom information is used instead of PCI IDs */
76static const struct ivtv_card ivtv_card_pvr250 = {
77 .type = IVTV_CARD_PVR_250,
78 .name = "Hauppauge WinTV PVR-250",
79 .v4l2_capabilities = IVTV_CAP_ENCODER,
80 .hw_video = IVTV_HW_SAA7115,
81 .hw_audio = IVTV_HW_MSP34XX,
82 .hw_audio_ctrl = IVTV_HW_MSP34XX,
83 .hw_all = IVTV_HW_MSP34XX | IVTV_HW_SAA7115 |
84 IVTV_HW_TVEEPROM | IVTV_HW_TUNER,
85 .video_inputs = {
86 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 },
87 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
88 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE0 },
89 { IVTV_CARD_INPUT_SVIDEO2, 2, IVTV_SAA71XX_SVIDEO1 },
90 { IVTV_CARD_INPUT_COMPOSITE2, 2, IVTV_SAA71XX_COMPOSITE1 },
91 { IVTV_CARD_INPUT_COMPOSITE3, 1, IVTV_SAA71XX_COMPOSITE5 },
92 },
93 .audio_inputs = {
94 { IVTV_CARD_INPUT_AUD_TUNER, MSP_TUNER },
95 { IVTV_CARD_INPUT_LINE_IN1, MSP_SCART1 },
96 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 },
97 },
98 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 },
99 .i2c = &ivtv_i2c_std,
100};
101
102/* ------------------------------------------------------------------------- */
103
104/* Hauppauge PVR-350 cards */
105
106/* Outputs for Hauppauge PVR350 cards */
107static struct ivtv_card_output ivtv_pvr350_outputs[] = {
108 {
109 .name = "S-Video + Composite",
110 .video_output = 0,
111 }, {
112 .name = "Composite",
113 .video_output = 1,
114 }, {
115 .name = "S-Video",
116 .video_output = 2,
117 }, {
118 .name = "RGB",
119 .video_output = 3,
120 }, {
121 .name = "YUV C",
122 .video_output = 4,
123 }, {
124 .name = "YUV V",
125 .video_output = 5,
126 }
127};
128
129static const struct ivtv_card ivtv_card_pvr350 = {
130 .type = IVTV_CARD_PVR_350,
131 .name = "Hauppauge WinTV PVR-350",
132 .v4l2_capabilities = IVTV_CAP_ENCODER | IVTV_CAP_DECODER,
133 .video_outputs = ivtv_pvr350_outputs,
134 .nof_outputs = ARRAY_SIZE(ivtv_pvr350_outputs),
135 .hw_video = IVTV_HW_SAA7115,
136 .hw_audio = IVTV_HW_MSP34XX,
137 .hw_audio_ctrl = IVTV_HW_MSP34XX,
138 .hw_all = IVTV_HW_MSP34XX | IVTV_HW_SAA7115 |
139 IVTV_HW_SAA7127 | IVTV_HW_TVEEPROM | IVTV_HW_TUNER |
140 IVTV_HW_I2C_IR_RX_HAUP_EXT | IVTV_HW_I2C_IR_RX_HAUP_INT,
141 .video_inputs = {
142 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 },
143 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
144 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE0 },
145 { IVTV_CARD_INPUT_SVIDEO2, 2, IVTV_SAA71XX_SVIDEO1 },
146 { IVTV_CARD_INPUT_COMPOSITE2, 2, IVTV_SAA71XX_COMPOSITE1 },
147 { IVTV_CARD_INPUT_COMPOSITE3, 1, IVTV_SAA71XX_COMPOSITE5 },
148 },
149 .audio_inputs = {
150 { IVTV_CARD_INPUT_AUD_TUNER, MSP_TUNER },
151 { IVTV_CARD_INPUT_LINE_IN1, MSP_SCART1 },
152 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 },
153 },
154 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 },
155 .i2c = &ivtv_i2c_std,
156};
157
158/* PVR-350 V1 boards have a different audio tuner input and use a
159 saa7114 instead of a saa7115.
160 Note that the info below comes from a pre-production model so it may
161 not be correct. Especially the audio behaves strangely (mono only it seems) */
162static const struct ivtv_card ivtv_card_pvr350_v1 = {
163 .type = IVTV_CARD_PVR_350_V1,
164 .name = "Hauppauge WinTV PVR-350 (V1)",
165 .v4l2_capabilities = IVTV_CAP_ENCODER | IVTV_CAP_DECODER,
166 .video_outputs = ivtv_pvr350_outputs,
167 .nof_outputs = ARRAY_SIZE(ivtv_pvr350_outputs),
168 .hw_video = IVTV_HW_SAA7114,
169 .hw_audio = IVTV_HW_MSP34XX,
170 .hw_audio_ctrl = IVTV_HW_MSP34XX,
171 .hw_all = IVTV_HW_MSP34XX | IVTV_HW_SAA7114 |
172 IVTV_HW_SAA7127 | IVTV_HW_TVEEPROM | IVTV_HW_TUNER,
173 .video_inputs = {
174 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 },
175 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
176 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE0 },
177 { IVTV_CARD_INPUT_SVIDEO2, 2, IVTV_SAA71XX_SVIDEO1 },
178 { IVTV_CARD_INPUT_COMPOSITE2, 2, IVTV_SAA71XX_COMPOSITE1 },
179 { IVTV_CARD_INPUT_COMPOSITE3, 1, IVTV_SAA71XX_COMPOSITE5 },
180 },
181 .audio_inputs = {
182 { IVTV_CARD_INPUT_AUD_TUNER, MSP_MONO },
183 { IVTV_CARD_INPUT_LINE_IN1, MSP_SCART1 },
184 { IVTV_CARD_INPUT_LINE_IN2, MSP_SCART3 },
185 },
186 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, MSP_SCART2 },
187 .i2c = &ivtv_i2c_std,
188};
189
190/* ------------------------------------------------------------------------- */
191
192/* Hauppauge PVR-150/PVR-500 cards */
193
194static const struct ivtv_card ivtv_card_pvr150 = {
195 .type = IVTV_CARD_PVR_150,
196 .name = "Hauppauge WinTV PVR-150",
197 .v4l2_capabilities = IVTV_CAP_ENCODER,
198 .hw_video = IVTV_HW_CX25840,
199 .hw_audio = IVTV_HW_CX25840,
200 .hw_audio_ctrl = IVTV_HW_CX25840,
201 .hw_muxer = IVTV_HW_WM8775,
202 .hw_all = IVTV_HW_WM8775 | IVTV_HW_CX25840 |
203 IVTV_HW_TVEEPROM | IVTV_HW_TUNER |
204 IVTV_HW_I2C_IR_RX_HAUP_EXT | IVTV_HW_I2C_IR_RX_HAUP_INT |
205 IVTV_HW_Z8F0811_IR_HAUP,
206 .video_inputs = {
207 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE7 },
208 { IVTV_CARD_INPUT_SVIDEO1, 1, CX25840_SVIDEO1 },
209 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE3 },
210 { IVTV_CARD_INPUT_SVIDEO2, 2, CX25840_SVIDEO2 },
211 { IVTV_CARD_INPUT_COMPOSITE2, 2, CX25840_COMPOSITE4 },
212 },
213 .audio_inputs = {
214 { IVTV_CARD_INPUT_AUD_TUNER,
215 CX25840_AUDIO8, WM8775_AIN2 },
216 { IVTV_CARD_INPUT_LINE_IN1,
217 CX25840_AUDIO_SERIAL, WM8775_AIN2 },
218 { IVTV_CARD_INPUT_LINE_IN2,
219 CX25840_AUDIO_SERIAL, WM8775_AIN3 },
220 },
221 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER,
222 CX25840_AUDIO_SERIAL, WM8775_AIN4 },
223 /* apparently needed for the IR blaster */
224 .gpio_init = { .direction = 0x1f01, .initial_value = 0x26f3 },
225 .i2c = &ivtv_i2c_std,
226};
227
228/* ------------------------------------------------------------------------- */
229
230/* AVerMedia M179 cards */
231
232static const struct ivtv_card_pci_info ivtv_pci_m179[] = {
233 { PCI_DEVICE_ID_IVTV15, IVTV_PCI_ID_AVERMEDIA, 0xa3cf },
234 { PCI_DEVICE_ID_IVTV15, IVTV_PCI_ID_AVERMEDIA, 0xa3ce },
235 { 0, 0, 0 }
236};
237
238static const struct ivtv_card ivtv_card_m179 = {
239 .type = IVTV_CARD_M179,
240 .name = "AVerMedia M179",
241 .v4l2_capabilities = IVTV_CAP_ENCODER,
242 .hw_video = IVTV_HW_SAA7114,
243 .hw_audio = IVTV_HW_GPIO,
244 .hw_audio_ctrl = IVTV_HW_GPIO,
245 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA7114 | IVTV_HW_TUNER,
246 .video_inputs = {
247 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 },
248 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
249 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE3 },
250 },
251 .audio_inputs = {
252 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_GPIO_TUNER },
253 { IVTV_CARD_INPUT_LINE_IN1, IVTV_GPIO_LINE_IN },
254 },
255 .gpio_init = { .direction = 0xe380, .initial_value = 0x8290 },
256 .gpio_audio_input = { .mask = 0x8040, .tuner = 0x8000, .linein = 0x0000 },
257 .gpio_audio_mute = { .mask = 0x2000, .mute = 0x2000 },
258 .gpio_audio_mode = { .mask = 0x4300, .mono = 0x4000, .stereo = 0x0200,
259 .lang1 = 0x0200, .lang2 = 0x0100, .both = 0x0000 },
260 .gpio_audio_freq = { .mask = 0x0018, .f32000 = 0x0000,
261 .f44100 = 0x0008, .f48000 = 0x0010 },
262 .gpio_audio_detect = { .mask = 0x4000, .stereo = 0x0000 },
263 .tuners = {
264 /* As far as we know all M179 cards use this tuner */
265 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_NTSC },
266 },
267 .pci_list = ivtv_pci_m179,
268 .i2c = &ivtv_i2c_std,
269};
270
271/* ------------------------------------------------------------------------- */
272
273/* Yuan MPG600/Kuroutoshikou ITVC16-STVLP cards */
274
275static const struct ivtv_card_pci_info ivtv_pci_mpg600[] = {
276 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN1, 0xfff3 },
277 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN1, 0xffff },
278 { 0, 0, 0 }
279};
280
281static const struct ivtv_card ivtv_card_mpg600 = {
282 .type = IVTV_CARD_MPG600,
283 .name = "Yuan MPG600, Kuroutoshikou ITVC16-STVLP",
284 .v4l2_capabilities = IVTV_CAP_ENCODER,
285 .hw_video = IVTV_HW_SAA7115,
286 .hw_audio = IVTV_HW_GPIO,
287 .hw_audio_ctrl = IVTV_HW_GPIO,
288 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA7115 | IVTV_HW_TUNER,
289 .video_inputs = {
290 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 },
291 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
292 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE3 },
293 },
294 .audio_inputs = {
295 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_GPIO_TUNER },
296 { IVTV_CARD_INPUT_LINE_IN1, IVTV_GPIO_LINE_IN },
297 },
298 .gpio_init = { .direction = 0x3080, .initial_value = 0x0004 },
299 .gpio_audio_input = { .mask = 0x3000, .tuner = 0x0000, .linein = 0x2000 },
300 .gpio_audio_mute = { .mask = 0x0001, .mute = 0x0001 },
301 .gpio_audio_mode = { .mask = 0x000e, .mono = 0x0006, .stereo = 0x0004,
302 .lang1 = 0x0004, .lang2 = 0x0000, .both = 0x0008 },
303 .gpio_audio_detect = { .mask = 0x0900, .stereo = 0x0100 },
304 .tuners = {
305 /* The PAL tuner is confirmed */
306 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FQ1216ME },
307 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 },
308 },
309 .pci_list = ivtv_pci_mpg600,
310 .i2c = &ivtv_i2c_std,
311};
312
313/* ------------------------------------------------------------------------- */
314
315/* Yuan MPG160/Kuroutoshikou ITVC15-STVLP cards */
316
317static const struct ivtv_card_pci_info ivtv_pci_mpg160[] = {
318 { PCI_DEVICE_ID_IVTV15, IVTV_PCI_ID_YUAN1, 0 },
319 { PCI_DEVICE_ID_IVTV15, IVTV_PCI_ID_IODATA, 0x40a0 },
320 { 0, 0, 0 }
321};
322
323static const struct ivtv_card ivtv_card_mpg160 = {
324 .type = IVTV_CARD_MPG160,
325 .name = "YUAN MPG160, Kuroutoshikou ITVC15-STVLP, I/O Data GV-M2TV/PCI",
326 .v4l2_capabilities = IVTV_CAP_ENCODER,
327 .hw_video = IVTV_HW_SAA7114,
328 .hw_audio = IVTV_HW_GPIO,
329 .hw_audio_ctrl = IVTV_HW_GPIO,
330 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA7114 | IVTV_HW_TUNER,
331 .video_inputs = {
332 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 },
333 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
334 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE3 },
335 },
336 .audio_inputs = {
337 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_GPIO_TUNER },
338 { IVTV_CARD_INPUT_LINE_IN1, IVTV_GPIO_LINE_IN },
339 },
340 .gpio_init = { .direction = 0x7080, .initial_value = 0x400c },
341 .gpio_audio_input = { .mask = 0x3000, .tuner = 0x0000, .linein = 0x2000 },
342 .gpio_audio_mute = { .mask = 0x0001, .mute = 0x0001 },
343 .gpio_audio_mode = { .mask = 0x000e, .mono = 0x0006, .stereo = 0x0004,
344 .lang1 = 0x0004, .lang2 = 0x0000, .both = 0x0008 },
345 .gpio_audio_detect = { .mask = 0x0900, .stereo = 0x0100 },
346 .tuners = {
347 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FQ1216ME },
348 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 },
349 },
350 .pci_list = ivtv_pci_mpg160,
351 .i2c = &ivtv_i2c_std,
352};
353
354/* ------------------------------------------------------------------------- */
355
356/* Yuan PG600/Diamond PVR-550 cards */
357
358static const struct ivtv_card_pci_info ivtv_pci_pg600[] = {
359 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_DIAMONDMM, 0x0070 },
360 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN3, 0x0600 },
361 { 0, 0, 0 }
362};
363
364static const struct ivtv_card ivtv_card_pg600 = {
365 .type = IVTV_CARD_PG600,
366 .name = "Yuan PG600, Diamond PVR-550",
367 .v4l2_capabilities = IVTV_CAP_ENCODER,
368 .hw_video = IVTV_HW_CX25840,
369 .hw_audio = IVTV_HW_CX25840,
370 .hw_audio_ctrl = IVTV_HW_CX25840,
371 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER,
372 .video_inputs = {
373 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
374 { IVTV_CARD_INPUT_SVIDEO1, 1,
375 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
376 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
377 },
378 .audio_inputs = {
379 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
380 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL },
381 },
382 .tuners = {
383 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FQ1216ME },
384 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FQ1286 },
385 },
386 .pci_list = ivtv_pci_pg600,
387 .i2c = &ivtv_i2c_std,
388};
389
390/* ------------------------------------------------------------------------- */
391
392/* Adaptec VideOh! AVC-2410 card */
393
394static const struct ivtv_card_pci_info ivtv_pci_avc2410[] = {
395 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_ADAPTEC, 0x0093 },
396 { 0, 0, 0 }
397};
398
399static const struct ivtv_card ivtv_card_avc2410 = {
400 .type = IVTV_CARD_AVC2410,
401 .name = "Adaptec VideOh! AVC-2410",
402 .v4l2_capabilities = IVTV_CAP_ENCODER,
403 .hw_video = IVTV_HW_SAA7115,
404 .hw_audio = IVTV_HW_MSP34XX,
405 .hw_audio_ctrl = IVTV_HW_MSP34XX,
406 .hw_muxer = IVTV_HW_CS53L32A,
407 .hw_all = IVTV_HW_MSP34XX | IVTV_HW_CS53L32A |
408 IVTV_HW_SAA7115 | IVTV_HW_TUNER |
409 IVTV_HW_I2C_IR_RX_ADAPTEC,
410 .video_inputs = {
411 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 },
412 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
413 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE3 },
414 },
415 .audio_inputs = {
416 { IVTV_CARD_INPUT_AUD_TUNER,
417 MSP_TUNER, CS53L32A_IN0 },
418 { IVTV_CARD_INPUT_LINE_IN1,
419 MSP_SCART1, CS53L32A_IN2 },
420 },
421 /* This card has no eeprom and in fact the Windows driver relies
422 on the country/region setting of the user to decide which tuner
423 is available. */
424 .tuners = {
425 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
426 { .std = V4L2_STD_ALL - V4L2_STD_NTSC_M_JP,
427 .tuner = TUNER_PHILIPS_FM1236_MK3 },
428 { .std = V4L2_STD_NTSC_M_JP, .tuner = TUNER_PHILIPS_FQ1286 },
429 },
430 .pci_list = ivtv_pci_avc2410,
431 .i2c = &ivtv_i2c_std,
432};
433
434/* ------------------------------------------------------------------------- */
435
436/* Adaptec VideOh! AVC-2010 card */
437
438static const struct ivtv_card_pci_info ivtv_pci_avc2010[] = {
439 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_ADAPTEC, 0x0092 },
440 { 0, 0, 0 }
441};
442
443static const struct ivtv_card ivtv_card_avc2010 = {
444 .type = IVTV_CARD_AVC2010,
445 .name = "Adaptec VideOh! AVC-2010",
446 .v4l2_capabilities = IVTV_CAP_ENCODER,
447 .hw_video = IVTV_HW_SAA7115,
448 .hw_audio = IVTV_HW_CS53L32A,
449 .hw_audio_ctrl = IVTV_HW_CS53L32A,
450 .hw_all = IVTV_HW_CS53L32A | IVTV_HW_SAA7115,
451 .video_inputs = {
452 { IVTV_CARD_INPUT_SVIDEO1, 0, IVTV_SAA71XX_SVIDEO0 },
453 { IVTV_CARD_INPUT_COMPOSITE1, 0, IVTV_SAA71XX_COMPOSITE3 },
454 },
455 .audio_inputs = {
456 { IVTV_CARD_INPUT_LINE_IN1, CS53L32A_IN2 },
457 },
458 /* Does not have a tuner */
459 .pci_list = ivtv_pci_avc2010,
460};
461
462/* ------------------------------------------------------------------------- */
463
464/* Nagase Transgear 5000TV card */
465
466static const struct ivtv_card_pci_info ivtv_pci_tg5000tv[] = {
467 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xbfff },
468 { 0, 0, 0 }
469};
470
471static const struct ivtv_card ivtv_card_tg5000tv = {
472 .type = IVTV_CARD_TG5000TV,
473 .name = "Nagase Transgear 5000TV",
474 .v4l2_capabilities = IVTV_CAP_ENCODER,
475 .hw_video = IVTV_HW_SAA7114 | IVTV_HW_UPD64031A | IVTV_HW_UPD6408X |
476 IVTV_HW_GPIO,
477 .hw_audio = IVTV_HW_GPIO,
478 .hw_audio_ctrl = IVTV_HW_GPIO,
479 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA7114 | IVTV_HW_TUNER |
480 IVTV_HW_UPD64031A | IVTV_HW_UPD6408X,
481 .video_inputs = {
482 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_SVIDEO0 },
483 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO2 },
484 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_SVIDEO2 },
485 },
486 .audio_inputs = {
487 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_GPIO_TUNER },
488 { IVTV_CARD_INPUT_LINE_IN1, IVTV_GPIO_LINE_IN },
489 },
490 .gr_config = UPD64031A_VERTICAL_EXTERNAL,
491 .gpio_init = { .direction = 0xe080, .initial_value = 0x8000 },
492 .gpio_audio_input = { .mask = 0x8080, .tuner = 0x8000, .linein = 0x0080 },
493 .gpio_audio_mute = { .mask = 0x6000, .mute = 0x6000 },
494 .gpio_audio_mode = { .mask = 0x4300, .mono = 0x4000, .stereo = 0x0200,
495 .lang1 = 0x0300, .lang2 = 0x0000, .both = 0x0200 },
496 .gpio_video_input = { .mask = 0x0030, .tuner = 0x0000,
497 .composite = 0x0010, .svideo = 0x0020 },
498 .tuners = {
499 { .std = V4L2_STD_MN, .tuner = TUNER_PHILIPS_FQ1286 },
500 },
501 .pci_list = ivtv_pci_tg5000tv,
502 .i2c = &ivtv_i2c_std,
503};
504
505/* ------------------------------------------------------------------------- */
506
507/* AOpen VA2000MAX-SNT6 card */
508
509static const struct ivtv_card_pci_info ivtv_pci_va2000[] = {
510 { PCI_DEVICE_ID_IVTV16, 0, 0xff5f },
511 { 0, 0, 0 }
512};
513
514static const struct ivtv_card ivtv_card_va2000 = {
515 .type = IVTV_CARD_VA2000MAX_SNT6,
516 .name = "AOpen VA2000MAX-SNT6",
517 .v4l2_capabilities = IVTV_CAP_ENCODER,
518 .hw_video = IVTV_HW_SAA7115 | IVTV_HW_UPD6408X,
519 .hw_audio = IVTV_HW_MSP34XX,
520 .hw_audio_ctrl = IVTV_HW_MSP34XX,
521 .hw_all = IVTV_HW_MSP34XX | IVTV_HW_SAA7115 |
522 IVTV_HW_UPD6408X | IVTV_HW_TUNER,
523 .video_inputs = {
524 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_SVIDEO0 },
525 },
526 .audio_inputs = {
527 { IVTV_CARD_INPUT_AUD_TUNER, MSP_TUNER },
528 },
529 .tuners = {
530 { .std = V4L2_STD_MN, .tuner = TUNER_PHILIPS_FQ1286 },
531 },
532 .pci_list = ivtv_pci_va2000,
533 .i2c = &ivtv_i2c_std,
534};
535
536/* ------------------------------------------------------------------------- */
537
538/* Yuan MPG600GR/Kuroutoshikou CX23416GYC-STVLP cards */
539
540static const struct ivtv_card_pci_info ivtv_pci_cx23416gyc[] = {
541 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN1, 0x0600 },
542 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN4, 0x0600 },
543 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_MELCO, 0x0523 },
544 { 0, 0, 0 }
545};
546
547static const struct ivtv_card ivtv_card_cx23416gyc = {
548 .type = IVTV_CARD_CX23416GYC,
549 .name = "Yuan MPG600GR, Kuroutoshikou CX23416GYC-STVLP",
550 .v4l2_capabilities = IVTV_CAP_ENCODER,
551 .hw_video = IVTV_HW_SAA717X | IVTV_HW_GPIO |
552 IVTV_HW_UPD64031A | IVTV_HW_UPD6408X,
553 .hw_audio = IVTV_HW_SAA717X,
554 .hw_audio_ctrl = IVTV_HW_SAA717X,
555 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA717X | IVTV_HW_TUNER |
556 IVTV_HW_UPD64031A | IVTV_HW_UPD6408X,
557 .video_inputs = {
558 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_SVIDEO3 |
559 IVTV_SAA717X_TUNER_FLAG },
560 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
561 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_SVIDEO3 },
562 },
563 .audio_inputs = {
564 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_SAA717X_IN2 },
565 { IVTV_CARD_INPUT_LINE_IN1, IVTV_SAA717X_IN0 },
566 },
567 .gr_config = UPD64031A_VERTICAL_EXTERNAL,
568 .gpio_init = { .direction = 0xf880, .initial_value = 0x8800 },
569 .gpio_video_input = { .mask = 0x0020, .tuner = 0x0000,
570 .composite = 0x0020, .svideo = 0x0020 },
571 .gpio_audio_freq = { .mask = 0xc000, .f32000 = 0x0000,
572 .f44100 = 0x4000, .f48000 = 0x8000 },
573 .tuners = {
574 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
575 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 },
576 },
577 .pci_list = ivtv_pci_cx23416gyc,
578 .i2c = &ivtv_i2c_std,
579};
580
581static const struct ivtv_card ivtv_card_cx23416gyc_nogr = {
582 .type = IVTV_CARD_CX23416GYC_NOGR,
583 .name = "Yuan MPG600GR, Kuroutoshikou CX23416GYC-STVLP (no GR)",
584 .v4l2_capabilities = IVTV_CAP_ENCODER,
585 .hw_video = IVTV_HW_SAA717X | IVTV_HW_GPIO | IVTV_HW_UPD6408X,
586 .hw_audio = IVTV_HW_SAA717X,
587 .hw_audio_ctrl = IVTV_HW_SAA717X,
588 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA717X | IVTV_HW_TUNER |
589 IVTV_HW_UPD6408X,
590 .video_inputs = {
591 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 |
592 IVTV_SAA717X_TUNER_FLAG },
593 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
594 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE0 },
595 },
596 .audio_inputs = {
597 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_SAA717X_IN2 },
598 { IVTV_CARD_INPUT_LINE_IN1, IVTV_SAA717X_IN0 },
599 },
600 .gpio_init = { .direction = 0xf880, .initial_value = 0x8800 },
601 .gpio_video_input = { .mask = 0x0020, .tuner = 0x0000,
602 .composite = 0x0020, .svideo = 0x0020 },
603 .gpio_audio_freq = { .mask = 0xc000, .f32000 = 0x0000,
604 .f44100 = 0x4000, .f48000 = 0x8000 },
605 .tuners = {
606 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
607 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 },
608 },
609 .i2c = &ivtv_i2c_std,
610};
611
612static const struct ivtv_card ivtv_card_cx23416gyc_nogrycs = {
613 .type = IVTV_CARD_CX23416GYC_NOGRYCS,
614 .name = "Yuan MPG600GR, Kuroutoshikou CX23416GYC-STVLP (no GR/YCS)",
615 .v4l2_capabilities = IVTV_CAP_ENCODER,
616 .hw_video = IVTV_HW_SAA717X | IVTV_HW_GPIO,
617 .hw_audio = IVTV_HW_SAA717X,
618 .hw_audio_ctrl = IVTV_HW_SAA717X,
619 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA717X | IVTV_HW_TUNER,
620 .video_inputs = {
621 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 |
622 IVTV_SAA717X_TUNER_FLAG },
623 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
624 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE0 },
625 },
626 .audio_inputs = {
627 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_SAA717X_IN2 },
628 { IVTV_CARD_INPUT_LINE_IN1, IVTV_SAA717X_IN0 },
629 },
630 .gpio_init = { .direction = 0xf880, .initial_value = 0x8800 },
631 .gpio_video_input = { .mask = 0x0020, .tuner = 0x0000,
632 .composite = 0x0020, .svideo = 0x0020 },
633 .gpio_audio_freq = { .mask = 0xc000, .f32000 = 0x0000,
634 .f44100 = 0x4000, .f48000 = 0x8000 },
635 .tuners = {
636 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
637 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_FM1236_MK3 },
638 },
639 .i2c = &ivtv_i2c_std,
640};
641
642/* ------------------------------------------------------------------------- */
643
644/* I/O Data GV-MVP/RX & GV-MVP/RX2W (dual tuner) cards */
645
646static const struct ivtv_card_pci_info ivtv_pci_gv_mvprx[] = {
647 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_IODATA, 0xd01e },
648 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_IODATA, 0xd038 }, /* 2W unit #1 */
649 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_IODATA, 0xd039 }, /* 2W unit #2 */
650 { 0, 0, 0 }
651};
652
653static const struct ivtv_card ivtv_card_gv_mvprx = {
654 .type = IVTV_CARD_GV_MVPRX,
655 .name = "I/O Data GV-MVP/RX, GV-MVP/RX2W (dual tuner)",
656 .v4l2_capabilities = IVTV_CAP_ENCODER,
657 .hw_video = IVTV_HW_SAA7115 | IVTV_HW_UPD64031A | IVTV_HW_UPD6408X,
658 .hw_audio = IVTV_HW_GPIO,
659 .hw_audio_ctrl = IVTV_HW_WM8739,
660 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA7115 | IVTV_HW_VP27SMPX |
661 IVTV_HW_TUNER | IVTV_HW_WM8739 |
662 IVTV_HW_UPD64031A | IVTV_HW_UPD6408X,
663 .video_inputs = {
664 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_SVIDEO0 },
665 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO1 },
666 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_SVIDEO2 },
667 },
668 .audio_inputs = {
669 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_GPIO_TUNER },
670 { IVTV_CARD_INPUT_LINE_IN1, IVTV_GPIO_LINE_IN },
671 },
672 .gpio_init = { .direction = 0xc301, .initial_value = 0x0200 },
673 .gpio_audio_input = { .mask = 0xffff, .tuner = 0x0200, .linein = 0x0300 },
674 .tuners = {
675 /* This card has the Panasonic VP27 tuner */
676 { .std = V4L2_STD_MN, .tuner = TUNER_PANASONIC_VP27 },
677 },
678 .pci_list = ivtv_pci_gv_mvprx,
679 .i2c = &ivtv_i2c_std,
680};
681
682/* ------------------------------------------------------------------------- */
683
684/* I/O Data GV-MVP/RX2E card */
685
686static const struct ivtv_card_pci_info ivtv_pci_gv_mvprx2e[] = {
687 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_IODATA, 0xd025 },
688 {0, 0, 0}
689};
690
691static const struct ivtv_card ivtv_card_gv_mvprx2e = {
692 .type = IVTV_CARD_GV_MVPRX2E,
693 .name = "I/O Data GV-MVP/RX2E",
694 .v4l2_capabilities = IVTV_CAP_ENCODER,
695 .hw_video = IVTV_HW_SAA7115,
696 .hw_audio = IVTV_HW_GPIO,
697 .hw_audio_ctrl = IVTV_HW_WM8739,
698 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA7115 | IVTV_HW_TUNER |
699 IVTV_HW_VP27SMPX | IVTV_HW_WM8739,
700 .video_inputs = {
701 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE4 },
702 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 },
703 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE3 },
704 },
705 .audio_inputs = {
706 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_GPIO_TUNER },
707 { IVTV_CARD_INPUT_LINE_IN1, IVTV_GPIO_LINE_IN },
708 },
709 .gpio_init = { .direction = 0xc301, .initial_value = 0x0200 },
710 .gpio_audio_input = { .mask = 0xffff, .tuner = 0x0200, .linein = 0x0300 },
711 .tuners = {
712 /* This card has the Panasonic VP27 tuner */
713 { .std = V4L2_STD_MN, .tuner = TUNER_PANASONIC_VP27 },
714 },
715 .pci_list = ivtv_pci_gv_mvprx2e,
716 .i2c = &ivtv_i2c_std,
717};
718
719/* ------------------------------------------------------------------------- */
720
721/* GotVIEW PCI DVD card */
722
723static const struct ivtv_card_pci_info ivtv_pci_gotview_pci_dvd[] = {
724 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN1, 0x0600 },
725 { 0, 0, 0 }
726};
727
728static const struct ivtv_card ivtv_card_gotview_pci_dvd = {
729 .type = IVTV_CARD_GOTVIEW_PCI_DVD,
730 .name = "GotView PCI DVD",
731 .v4l2_capabilities = IVTV_CAP_ENCODER,
732 .hw_video = IVTV_HW_SAA717X,
733 .hw_audio = IVTV_HW_SAA717X,
734 .hw_audio_ctrl = IVTV_HW_SAA717X,
735 .hw_all = IVTV_HW_SAA717X | IVTV_HW_TUNER,
736 .video_inputs = {
737 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE1 }, /* pin 116 */
738 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO0 }, /* pin 114/109 */
739 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE3 }, /* pin 118 */
740 },
741 .audio_inputs = {
742 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_SAA717X_IN0 },
743 { IVTV_CARD_INPUT_LINE_IN1, IVTV_SAA717X_IN2 },
744 },
745 .gpio_init = { .direction = 0xf000, .initial_value = 0xA000 },
746 .tuners = {
747 /* This card has a Philips FQ1216ME MK3 tuner */
748 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
749 },
750 .pci_list = ivtv_pci_gotview_pci_dvd,
751 .i2c = &ivtv_i2c_std,
752};
753
754/* ------------------------------------------------------------------------- */
755
756/* GotVIEW PCI DVD2 Deluxe card */
757
758static const struct ivtv_card_pci_info ivtv_pci_gotview_pci_dvd2[] = {
759 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_GOTVIEW1, 0x0600 },
760 { 0, 0, 0 }
761};
762
763static const struct ivtv_card ivtv_card_gotview_pci_dvd2 = {
764 .type = IVTV_CARD_GOTVIEW_PCI_DVD2,
765 .name = "GotView PCI DVD2 Deluxe",
766 .v4l2_capabilities = IVTV_CAP_ENCODER,
767 .hw_video = IVTV_HW_CX25840,
768 .hw_audio = IVTV_HW_CX25840,
769 .hw_audio_ctrl = IVTV_HW_CX25840,
770 .hw_muxer = IVTV_HW_GPIO,
771 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER,
772 .video_inputs = {
773 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
774 { IVTV_CARD_INPUT_SVIDEO1, 1,
775 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
776 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
777 },
778 .audio_inputs = {
779 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5, 0 },
780 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 1 },
781 },
782 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO_SERIAL, 2 },
783 .gpio_init = { .direction = 0x0800, .initial_value = 0 },
784 .gpio_audio_input = { .mask = 0x0800, .tuner = 0, .linein = 0, .radio = 0x0800 },
785 .tuners = {
786 /* This card has a Philips FQ1216ME MK5 tuner */
787 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FM1216ME_MK3 },
788 },
789 .pci_list = ivtv_pci_gotview_pci_dvd2,
790 .i2c = &ivtv_i2c_std,
791};
792
793/* ------------------------------------------------------------------------- */
794
795/* Yuan MPC622 miniPCI card */
796
797static const struct ivtv_card_pci_info ivtv_pci_yuan_mpc622[] = {
798 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN2, 0xd998 },
799 { 0, 0, 0 }
800};
801
802static const struct ivtv_card ivtv_card_yuan_mpc622 = {
803 .type = IVTV_CARD_YUAN_MPC622,
804 .name = "Yuan MPC622",
805 .v4l2_capabilities = IVTV_CAP_ENCODER,
806 .hw_video = IVTV_HW_CX25840,
807 .hw_audio = IVTV_HW_CX25840,
808 .hw_audio_ctrl = IVTV_HW_CX25840,
809 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER,
810 .video_inputs = {
811 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
812 { IVTV_CARD_INPUT_SVIDEO1, 1,
813 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
814 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
815 },
816 .audio_inputs = {
817 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
818 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL },
819 },
820 .gpio_init = { .direction = 0x00ff, .initial_value = 0x0002 },
821 .tuners = {
822 /* This card has the TDA8290/TDA8275 tuner chips */
823 { .std = V4L2_STD_ALL, .tuner = TUNER_PHILIPS_TDA8290 },
824 },
825 .pci_list = ivtv_pci_yuan_mpc622,
826 .i2c = &ivtv_i2c_tda8290,
827};
828
829/* ------------------------------------------------------------------------- */
830
831/* DIGITAL COWBOY DCT-MTVP1 card */
832
833static const struct ivtv_card_pci_info ivtv_pci_dctmvtvp1[] = {
834 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xbfff },
835 { 0, 0, 0 }
836};
837
838static const struct ivtv_card ivtv_card_dctmvtvp1 = {
839 .type = IVTV_CARD_DCTMTVP1,
840 .name = "Digital Cowboy DCT-MTVP1",
841 .v4l2_capabilities = IVTV_CAP_ENCODER,
842 .hw_video = IVTV_HW_SAA7115 | IVTV_HW_UPD64031A | IVTV_HW_UPD6408X |
843 IVTV_HW_GPIO,
844 .hw_audio = IVTV_HW_GPIO,
845 .hw_audio_ctrl = IVTV_HW_GPIO,
846 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA7115 | IVTV_HW_TUNER |
847 IVTV_HW_UPD64031A | IVTV_HW_UPD6408X,
848 .video_inputs = {
849 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_SVIDEO0 },
850 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO2 },
851 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_SVIDEO2 },
852 },
853 .audio_inputs = {
854 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_GPIO_TUNER },
855 { IVTV_CARD_INPUT_LINE_IN1, IVTV_GPIO_LINE_IN },
856 },
857 .gpio_init = { .direction = 0xe080, .initial_value = 0x8000 },
858 .gpio_audio_input = { .mask = 0x8080, .tuner = 0x8000, .linein = 0x0080 },
859 .gpio_audio_mute = { .mask = 0x6000, .mute = 0x6000 },
860 .gpio_audio_mode = { .mask = 0x4300, .mono = 0x4000, .stereo = 0x0200,
861 .lang1 = 0x0300, .lang2 = 0x0000, .both = 0x0200 },
862 .gpio_video_input = { .mask = 0x0030, .tuner = 0x0000,
863 .composite = 0x0010, .svideo = 0x0020},
864 .tuners = {
865 { .std = V4L2_STD_MN, .tuner = TUNER_PHILIPS_FQ1286 },
866 },
867 .pci_list = ivtv_pci_dctmvtvp1,
868 .i2c = &ivtv_i2c_std,
869};
870
871/* ------------------------------------------------------------------------- */
872
873/* Yuan PG600-2/GotView PCI DVD Lite cards */
874
875static const struct ivtv_card_pci_info ivtv_pci_pg600v2[] = {
876 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN3, 0x0600 },
877 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_GOTVIEW2, 0x0600 },
878 { 0, 0, 0 }
879};
880
881static const struct ivtv_card ivtv_card_pg600v2 = {
882 .type = IVTV_CARD_PG600V2,
883 .name = "Yuan PG600-2, GotView PCI DVD Lite",
884 .v4l2_capabilities = IVTV_CAP_ENCODER,
885 .hw_video = IVTV_HW_CX25840,
886 .hw_audio = IVTV_HW_CX25840,
887 .hw_audio_ctrl = IVTV_HW_CX25840,
888 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER,
889 /* XC2028 support apparently works for the Yuan, it's still
890 uncertain whether it also works with the GotView. */
891 .video_inputs = {
892 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
893 { IVTV_CARD_INPUT_SVIDEO1, 1,
894 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
895 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
896 },
897 .audio_inputs = {
898 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
899 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL },
900 },
901 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
902 .xceive_pin = 12,
903 .tuners = {
904 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
905 },
906 .pci_list = ivtv_pci_pg600v2,
907 .i2c = &ivtv_i2c_std,
908};
909
910/* ------------------------------------------------------------------------- */
911
912/* Club3D ZAP-TV1x01 cards */
913
914static const struct ivtv_card_pci_info ivtv_pci_club3d[] = {
915 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_YUAN3, 0x0600 },
916 { 0, 0, 0 }
917};
918
919static const struct ivtv_card ivtv_card_club3d = {
920 .type = IVTV_CARD_CLUB3D,
921 .name = "Club3D ZAP-TV1x01",
922 .v4l2_capabilities = IVTV_CAP_ENCODER,
923 .hw_video = IVTV_HW_CX25840,
924 .hw_audio = IVTV_HW_CX25840,
925 .hw_audio_ctrl = IVTV_HW_CX25840,
926 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER,
927 .video_inputs = {
928 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
929 { IVTV_CARD_INPUT_SVIDEO1, 1,
930 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
931 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE3 },
932 },
933 .audio_inputs = {
934 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
935 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL },
936 },
937 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
938 .xceive_pin = 12,
939 .tuners = {
940 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
941 },
942 .pci_list = ivtv_pci_club3d,
943 .i2c = &ivtv_i2c_std,
944};
945
946/* ------------------------------------------------------------------------- */
947
948/* AVerTV MCE 116 Plus (M116) card */
949
950static const struct ivtv_card_pci_info ivtv_pci_avertv_mce116[] = {
951 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc439 },
952 { 0, 0, 0 }
953};
954
955static const struct ivtv_card ivtv_card_avertv_mce116 = {
956 .type = IVTV_CARD_AVERTV_MCE116,
957 .name = "AVerTV MCE 116 Plus",
958 .v4l2_capabilities = IVTV_CAP_ENCODER,
959 .hw_video = IVTV_HW_CX25840,
960 .hw_audio = IVTV_HW_CX25840,
961 .hw_audio_ctrl = IVTV_HW_CX25840,
962 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER | IVTV_HW_WM8739 |
963 IVTV_HW_I2C_IR_RX_AVER,
964 .video_inputs = {
965 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
966 { IVTV_CARD_INPUT_SVIDEO1, 1, CX25840_SVIDEO3 },
967 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
968 },
969 .audio_inputs = {
970 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
971 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 1 },
972 },
973 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
974 /* enable line-in */
975 .gpio_init = { .direction = 0xe000, .initial_value = 0x4000 },
976 .xceive_pin = 10,
977 .tuners = {
978 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
979 },
980 .pci_list = ivtv_pci_avertv_mce116,
981 .i2c = &ivtv_i2c_std,
982};
983
984/* ------------------------------------------------------------------------- */
985
986/* AVerMedia PVR-150 Plus / AVerTV M113 cards with a Daewoo/Partsnic Tuner */
987
988static const struct ivtv_card_pci_info ivtv_pci_aver_pvr150[] = {
989 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc034 }, /* NTSC */
990 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc035 }, /* NTSC FM */
991 { 0, 0, 0 }
992};
993
994static const struct ivtv_card ivtv_card_aver_pvr150 = {
995 .type = IVTV_CARD_AVER_PVR150PLUS,
996 .name = "AVerMedia PVR-150 Plus / AVerTV M113 Partsnic (Daewoo) Tuner",
997 .v4l2_capabilities = IVTV_CAP_ENCODER,
998 .hw_video = IVTV_HW_CX25840,
999 .hw_audio = IVTV_HW_CX25840,
1000 .hw_audio_ctrl = IVTV_HW_CX25840,
1001 .hw_muxer = IVTV_HW_GPIO,
1002 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER |
1003 IVTV_HW_WM8739 | IVTV_HW_GPIO,
1004 .video_inputs = {
1005 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
1006 { IVTV_CARD_INPUT_SVIDEO1, 1, CX25840_SVIDEO3 },
1007 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
1008 },
1009 .audio_inputs = {
1010 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5, 0 },
1011 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 1 },
1012 },
1013 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO_SERIAL, 2 },
1014 /* The 74HC4052 Dual 4:1 multiplexer is controlled by 2 GPIO lines */
1015 .gpio_init = { .direction = 0xc000, .initial_value = 0 },
1016 .gpio_audio_input = { .mask = 0xc000,
1017 .tuner = 0x0000,
1018 .linein = 0x4000,
1019 .radio = 0x8000 },
1020 .tuners = {
1021 /* Subsystem ID's 0xc03[45] have a Partsnic PTI-5NF05 tuner */
1022 { .std = V4L2_STD_MN, .tuner = TUNER_PARTSNIC_PTI_5NF05 },
1023 },
1024 .pci_list = ivtv_pci_aver_pvr150,
1025 /* Subsystem ID 0xc035 has a TEA5767(?) FM tuner, 0xc034 does not */
1026 .i2c = &ivtv_i2c_radio,
1027};
1028
1029/* ------------------------------------------------------------------------- */
1030
1031/* AVerMedia UltraTV 1500 MCE (newer non-cx88 version, M113 variant) card */
1032
1033static const struct ivtv_card_pci_info ivtv_pci_aver_ultra1500mce[] = {
1034 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc019 }, /* NTSC */
1035 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc01b }, /* PAL/SECAM */
1036 { 0, 0, 0 }
1037};
1038
1039static const struct ivtv_card ivtv_card_aver_ultra1500mce = {
1040 .type = IVTV_CARD_AVER_ULTRA1500MCE,
1041 .name = "AVerMedia UltraTV 1500 MCE / AVerTV M113 Philips Tuner",
1042 .comment = "For non-NTSC tuners, use the pal= or secam= module options",
1043 .v4l2_capabilities = IVTV_CAP_ENCODER,
1044 .hw_video = IVTV_HW_CX25840,
1045 .hw_audio = IVTV_HW_CX25840,
1046 .hw_audio_ctrl = IVTV_HW_CX25840,
1047 .hw_muxer = IVTV_HW_GPIO,
1048 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER |
1049 IVTV_HW_WM8739 | IVTV_HW_GPIO,
1050 .video_inputs = {
1051 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
1052 { IVTV_CARD_INPUT_SVIDEO1, 1, CX25840_SVIDEO3 },
1053 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
1054 },
1055 .audio_inputs = {
1056 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5, 0 },
1057 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 1 },
1058 },
1059 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO_SERIAL, 2 },
1060 /* The 74HC4052 Dual 4:1 multiplexer is controlled by 2 GPIO lines */
1061 .gpio_init = { .direction = 0xc000, .initial_value = 0 },
1062 .gpio_audio_input = { .mask = 0xc000,
1063 .tuner = 0x0000,
1064 .linein = 0x4000,
1065 .radio = 0x8000 },
1066 .tuners = {
1067 /* The UltraTV 1500 MCE has a Philips FM1236 MK5 TV/FM tuner */
1068 { .std = V4L2_STD_MN, .tuner = TUNER_PHILIPS_FM1236_MK3 },
1069 { .std = V4L2_STD_PAL_SECAM, .tuner = TUNER_PHILIPS_FM1216MK5 },
1070 },
1071 .pci_list = ivtv_pci_aver_ultra1500mce,
1072 .i2c = &ivtv_i2c_std,
1073};
1074
1075/* ------------------------------------------------------------------------- */
1076
1077/* AVerMedia EZMaker PCI Deluxe card */
1078
1079static const struct ivtv_card_pci_info ivtv_pci_aver_ezmaker[] = {
1080 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc03f },
1081 { 0, 0, 0 }
1082};
1083
1084static const struct ivtv_card ivtv_card_aver_ezmaker = {
1085 .type = IVTV_CARD_AVER_EZMAKER,
1086 .name = "AVerMedia EZMaker PCI Deluxe",
1087 .v4l2_capabilities = IVTV_CAP_ENCODER,
1088 .hw_video = IVTV_HW_CX25840,
1089 .hw_audio = IVTV_HW_CX25840,
1090 .hw_audio_ctrl = IVTV_HW_CX25840,
1091 .hw_all = IVTV_HW_CX25840 | IVTV_HW_WM8739,
1092 .video_inputs = {
1093 { IVTV_CARD_INPUT_SVIDEO1, 0, CX25840_SVIDEO3 },
1094 { IVTV_CARD_INPUT_COMPOSITE1, 0, CX25840_COMPOSITE1 },
1095 },
1096 .audio_inputs = {
1097 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 0 },
1098 },
1099 .gpio_init = { .direction = 0x4000, .initial_value = 0x4000 },
1100 /* Does not have a tuner */
1101 .pci_list = ivtv_pci_aver_ezmaker,
1102};
1103
1104/* ------------------------------------------------------------------------- */
1105
1106/* ASUS Falcon2 */
1107
1108static const struct ivtv_card_pci_info ivtv_pci_asus_falcon2[] = {
1109 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_ASUSTEK, 0x4b66 },
1110 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_ASUSTEK, 0x462e },
1111 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_ASUSTEK, 0x4b2e },
1112 { 0, 0, 0 }
1113};
1114
1115static const struct ivtv_card ivtv_card_asus_falcon2 = {
1116 .type = IVTV_CARD_ASUS_FALCON2,
1117 .name = "ASUS Falcon2",
1118 .v4l2_capabilities = IVTV_CAP_ENCODER,
1119 .hw_video = IVTV_HW_CX25840,
1120 .hw_audio = IVTV_HW_CX25840,
1121 .hw_audio_ctrl = IVTV_HW_CX25840,
1122 .hw_muxer = IVTV_HW_M52790,
1123 .hw_all = IVTV_HW_CX25840 | IVTV_HW_M52790 | IVTV_HW_TUNER,
1124 .video_inputs = {
1125 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
1126 { IVTV_CARD_INPUT_SVIDEO1, 1, CX25840_SVIDEO3 },
1127 { IVTV_CARD_INPUT_COMPOSITE1, 2, CX25840_COMPOSITE2 },
1128 },
1129 .audio_inputs = {
1130 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5, M52790_IN_TUNER },
1131 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL,
1132 M52790_IN_V2 | M52790_SW1_YCMIX | M52790_SW2_YCMIX },
1133 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, M52790_IN_V2 },
1134 },
1135 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO_SERIAL, M52790_IN_TUNER },
1136 .tuners = {
1137 { .std = V4L2_STD_MN, .tuner = TUNER_PHILIPS_FM1236_MK3 },
1138 },
1139 .pci_list = ivtv_pci_asus_falcon2,
1140 .i2c = &ivtv_i2c_std,
1141};
1142
1143/* ------------------------------------------------------------------------- */
1144
1145/* AVerMedia M104 miniPCI card */
1146
1147static const struct ivtv_card_pci_info ivtv_pci_aver_m104[] = {
1148 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_AVERMEDIA, 0xc136 },
1149 { 0, 0, 0 }
1150};
1151
1152static const struct ivtv_card ivtv_card_aver_m104 = {
1153 .type = IVTV_CARD_AVER_M104,
1154 .name = "AVerMedia M104",
1155 .comment = "Not yet supported!\n",
1156 .v4l2_capabilities = 0, /*IVTV_CAP_ENCODER,*/
1157 .hw_video = IVTV_HW_CX25840,
1158 .hw_audio = IVTV_HW_CX25840,
1159 .hw_audio_ctrl = IVTV_HW_CX25840,
1160 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER | IVTV_HW_WM8739,
1161 .video_inputs = {
1162 { IVTV_CARD_INPUT_SVIDEO1, 0, CX25840_SVIDEO3 },
1163 { IVTV_CARD_INPUT_COMPOSITE1, 0, CX25840_COMPOSITE1 },
1164 },
1165 .audio_inputs = {
1166 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL, 1 },
1167 },
1168 .radio_input = { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO_SERIAL, 2 },
1169 /* enable line-in + reset tuner */
1170 .gpio_init = { .direction = 0xe000, .initial_value = 0x4000 },
1171 .xceive_pin = 10,
1172 .tuners = {
1173 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
1174 },
1175 .pci_list = ivtv_pci_aver_m104,
1176 .i2c = &ivtv_i2c_std,
1177};
1178
1179/* ------------------------------------------------------------------------- */
1180
1181/* Buffalo PC-MV5L/PCI cards */
1182
1183static const struct ivtv_card_pci_info ivtv_pci_buffalo[] = {
1184 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_MELCO, 0x052b },
1185 { 0, 0, 0 }
1186};
1187
1188static const struct ivtv_card ivtv_card_buffalo = {
1189 .type = IVTV_CARD_BUFFALO_MV5L,
1190 .name = "Buffalo PC-MV5L/PCI",
1191 .v4l2_capabilities = IVTV_CAP_ENCODER,
1192 .hw_video = IVTV_HW_CX25840,
1193 .hw_audio = IVTV_HW_CX25840,
1194 .hw_audio_ctrl = IVTV_HW_CX25840,
1195 .hw_all = IVTV_HW_CX25840 | IVTV_HW_TUNER,
1196 .video_inputs = {
1197 { IVTV_CARD_INPUT_VID_TUNER, 0, CX25840_COMPOSITE2 },
1198 { IVTV_CARD_INPUT_SVIDEO1, 1,
1199 CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 },
1200 { IVTV_CARD_INPUT_COMPOSITE1, 1, CX25840_COMPOSITE1 },
1201 },
1202 .audio_inputs = {
1203 { IVTV_CARD_INPUT_AUD_TUNER, CX25840_AUDIO5 },
1204 { IVTV_CARD_INPUT_LINE_IN1, CX25840_AUDIO_SERIAL },
1205 },
1206 .xceive_pin = 12,
1207 .tuners = {
1208 { .std = V4L2_STD_ALL, .tuner = TUNER_XC2028 },
1209 },
1210 .pci_list = ivtv_pci_buffalo,
1211 .i2c = &ivtv_i2c_std,
1212};
1213
1214/* ------------------------------------------------------------------------- */
1215/* Sony Kikyou */
1216
1217static const struct ivtv_card_pci_info ivtv_pci_kikyou[] = {
1218 { PCI_DEVICE_ID_IVTV16, IVTV_PCI_ID_SONY, 0x813d },
1219 { 0, 0, 0 }
1220};
1221
1222static const struct ivtv_card ivtv_card_kikyou = {
1223 .type = IVTV_CARD_KIKYOU,
1224 .name = "Sony VAIO Giga Pocket (ENX Kikyou)",
1225 .v4l2_capabilities = IVTV_CAP_ENCODER,
1226 .hw_video = IVTV_HW_SAA7115,
1227 .hw_audio = IVTV_HW_GPIO,
1228 .hw_audio_ctrl = IVTV_HW_GPIO,
1229 .hw_all = IVTV_HW_GPIO | IVTV_HW_SAA7115 | IVTV_HW_TUNER,
1230 .video_inputs = {
1231 { IVTV_CARD_INPUT_VID_TUNER, 0, IVTV_SAA71XX_COMPOSITE1 },
1232 { IVTV_CARD_INPUT_COMPOSITE1, 1, IVTV_SAA71XX_COMPOSITE1 },
1233 { IVTV_CARD_INPUT_SVIDEO1, 1, IVTV_SAA71XX_SVIDEO1 },
1234 },
1235 .audio_inputs = {
1236 { IVTV_CARD_INPUT_AUD_TUNER, IVTV_GPIO_TUNER },
1237 { IVTV_CARD_INPUT_LINE_IN1, IVTV_GPIO_LINE_IN },
1238 { IVTV_CARD_INPUT_LINE_IN2, IVTV_GPIO_LINE_IN },
1239 },
1240 .gpio_init = { .direction = 0x03e1, .initial_value = 0x0320 },
1241 .gpio_audio_input = { .mask = 0x0060,
1242 .tuner = 0x0020,
1243 .linein = 0x0000,
1244 .radio = 0x0060 },
1245 .gpio_audio_mute = { .mask = 0x0000,
1246 .mute = 0x0000 }, /* 0x200? Disable for now. */
1247 .gpio_audio_mode = { .mask = 0x0080,
1248 .mono = 0x0000,
1249 .stereo = 0x0000, /* SAP */
1250 .lang1 = 0x0080,
1251 .lang2 = 0x0000,
1252 .both = 0x0080 },
1253 .tuners = {
1254 { .std = V4L2_STD_ALL, .tuner = TUNER_SONY_BTF_PXN01Z },
1255 },
1256 .pci_list = ivtv_pci_kikyou,
1257 .i2c = &ivtv_i2c_std,
1258};
1259
1260
1261static const struct ivtv_card *ivtv_card_list[] = {
1262 &ivtv_card_pvr250,
1263 &ivtv_card_pvr350,
1264 &ivtv_card_pvr150,
1265 &ivtv_card_m179,
1266 &ivtv_card_mpg600,
1267 &ivtv_card_mpg160,
1268 &ivtv_card_pg600,
1269 &ivtv_card_avc2410,
1270 &ivtv_card_avc2010,
1271 &ivtv_card_tg5000tv,
1272 &ivtv_card_va2000,
1273 &ivtv_card_cx23416gyc,
1274 &ivtv_card_gv_mvprx,
1275 &ivtv_card_gv_mvprx2e,
1276 &ivtv_card_gotview_pci_dvd,
1277 &ivtv_card_gotview_pci_dvd2,
1278 &ivtv_card_yuan_mpc622,
1279 &ivtv_card_dctmvtvp1,
1280 &ivtv_card_pg600v2,
1281 &ivtv_card_club3d,
1282 &ivtv_card_avertv_mce116,
1283 &ivtv_card_asus_falcon2,
1284 &ivtv_card_aver_pvr150,
1285 &ivtv_card_aver_ezmaker,
1286 &ivtv_card_aver_m104,
1287 &ivtv_card_buffalo,
1288 &ivtv_card_aver_ultra1500mce,
1289 &ivtv_card_kikyou,
1290
1291 /* Variations of standard cards but with the same PCI IDs.
1292 These cards must come last in this list. */
1293 &ivtv_card_pvr350_v1,
1294 &ivtv_card_cx23416gyc_nogr,
1295 &ivtv_card_cx23416gyc_nogrycs,
1296};
1297
1298const struct ivtv_card *ivtv_get_card(u16 index)
1299{
1300 if (index >= ARRAY_SIZE(ivtv_card_list))
1301 return NULL;
1302 return ivtv_card_list[index];
1303}
1304
1305int ivtv_get_input(struct ivtv *itv, u16 index, struct v4l2_input *input)
1306{
1307 const struct ivtv_card_video_input *card_input = itv->card->video_inputs + index;
1308 static const char * const input_strs[] = {
1309 "Tuner 1",
1310 "S-Video 1",
1311 "S-Video 2",
1312 "Composite 1",
1313 "Composite 2",
1314 "Composite 3"
1315 };
1316
1317 if (index >= itv->nof_inputs)
1318 return -EINVAL;
1319 input->index = index;
1320 strlcpy(input->name, input_strs[card_input->video_type - 1],
1321 sizeof(input->name));
1322 input->type = (card_input->video_type == IVTV_CARD_INPUT_VID_TUNER ?
1323 V4L2_INPUT_TYPE_TUNER : V4L2_INPUT_TYPE_CAMERA);
1324 input->audioset = (1 << itv->nof_audio_inputs) - 1;
1325 input->std = (input->type == V4L2_INPUT_TYPE_TUNER) ?
1326 itv->tuner_std : V4L2_STD_ALL;
1327 return 0;
1328}
1329
1330int ivtv_get_output(struct ivtv *itv, u16 index, struct v4l2_output *output)
1331{
1332 const struct ivtv_card_output *card_output = itv->card->video_outputs + index;
1333
1334 if (index >= itv->card->nof_outputs)
1335 return -EINVAL;
1336 output->index = index;
1337 strlcpy(output->name, card_output->name, sizeof(output->name));
1338 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1339 output->audioset = 1;
1340 output->std = V4L2_STD_ALL;
1341 return 0;
1342}
1343
1344int ivtv_get_audio_input(struct ivtv *itv, u16 index, struct v4l2_audio *audio)
1345{
1346 const struct ivtv_card_audio_input *aud_input = itv->card->audio_inputs + index;
1347 static const char * const input_strs[] = {
1348 "Tuner 1",
1349 "Line In 1",
1350 "Line In 2"
1351 };
1352
1353 memset(audio, 0, sizeof(*audio));
1354 if (index >= itv->nof_audio_inputs)
1355 return -EINVAL;
1356 strlcpy(audio->name, input_strs[aud_input->audio_type - 1],
1357 sizeof(audio->name));
1358 audio->index = index;
1359 audio->capability = V4L2_AUDCAP_STEREO;
1360 return 0;
1361}
1362
1363int ivtv_get_audio_output(struct ivtv *itv, u16 index, struct v4l2_audioout *aud_output)
1364{
1365 memset(aud_output, 0, sizeof(*aud_output));
1366 if (itv->card->video_outputs == NULL || index != 0)
1367 return -EINVAL;
1368 strlcpy(aud_output->name, "A/V Audio Out", sizeof(aud_output->name));
1369 return 0;
1370}
diff --git a/drivers/media/pci/ivtv/ivtv-cards.h b/drivers/media/pci/ivtv/ivtv-cards.h
new file mode 100644
index 000000000000..e6f5c02981f1
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-cards.h
@@ -0,0 +1,309 @@
1/*
2 Functions to query card hardware
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_CARDS_H
22#define IVTV_CARDS_H
23
24/* Supported cards */
25#define IVTV_CARD_PVR_250 0 /* WinTV PVR 250 */
26#define IVTV_CARD_PVR_350 1 /* encoder, decoder, tv-out */
27#define IVTV_CARD_PVR_150 2 /* WinTV PVR 150 and PVR 500 (really just two
28 PVR150s on one PCI board) */
29#define IVTV_CARD_M179 3 /* AVerMedia M179 (encoder only) */
30#define IVTV_CARD_MPG600 4 /* Kuroutoshikou ITVC16-STVLP/YUAN MPG600, encoder only */
31#define IVTV_CARD_MPG160 5 /* Kuroutoshikou ITVC15-STVLP/YUAN MPG160
32 cx23415 based, but does not have tv-out */
33#define IVTV_CARD_PG600 6 /* YUAN PG600/DIAMONDMM PVR-550 based on the CX Falcon 2 */
34#define IVTV_CARD_AVC2410 7 /* Adaptec AVC-2410 */
35#define IVTV_CARD_AVC2010 8 /* Adaptec AVD-2010 (No Tuner) */
36#define IVTV_CARD_TG5000TV 9 /* NAGASE TRANSGEAR 5000TV, encoder only */
37#define IVTV_CARD_VA2000MAX_SNT6 10 /* VA2000MAX-STN6 */
38#define IVTV_CARD_CX23416GYC 11 /* Kuroutoshikou CX23416GYC-STVLP (Yuan MPG600GR OEM) */
39#define IVTV_CARD_GV_MVPRX 12 /* I/O Data GV-MVP/RX, RX2, RX2W */
40#define IVTV_CARD_GV_MVPRX2E 13 /* I/O Data GV-MVP/RX2E */
41#define IVTV_CARD_GOTVIEW_PCI_DVD 14 /* GotView PCI DVD */
42#define IVTV_CARD_GOTVIEW_PCI_DVD2 15 /* GotView PCI DVD2 */
43#define IVTV_CARD_YUAN_MPC622 16 /* Yuan MPC622 miniPCI */
44#define IVTV_CARD_DCTMTVP1 17 /* DIGITAL COWBOY DCT-MTVP1 */
45#define IVTV_CARD_PG600V2 18 /* Yuan PG600V2/GotView PCI DVD Lite */
46#define IVTV_CARD_CLUB3D 19 /* Club3D ZAP-TV1x01 */
47#define IVTV_CARD_AVERTV_MCE116 20 /* AVerTV MCE 116 Plus */
48#define IVTV_CARD_ASUS_FALCON2 21 /* ASUS Falcon2 */
49#define IVTV_CARD_AVER_PVR150PLUS 22 /* AVerMedia PVR-150 Plus */
50#define IVTV_CARD_AVER_EZMAKER 23 /* AVerMedia EZMaker PCI Deluxe */
51#define IVTV_CARD_AVER_M104 24 /* AverMedia M104 miniPCI card */
52#define IVTV_CARD_BUFFALO_MV5L 25 /* Buffalo PC-MV5L/PCI card */
53#define IVTV_CARD_AVER_ULTRA1500MCE 26 /* AVerMedia UltraTV 1500 MCE */
54#define IVTV_CARD_KIKYOU 27 /* Sony VAIO Giga Pocket (ENX Kikyou) */
55#define IVTV_CARD_LAST 27
56
57/* Variants of existing cards but with the same PCI IDs. The driver
58 detects these based on other device information.
59 These cards must always come last.
60 New cards must be inserted above, and the indices of the cards below
61 must be adjusted accordingly. */
62
63/* PVR-350 V1 (uses saa7114) */
64#define IVTV_CARD_PVR_350_V1 (IVTV_CARD_LAST+1)
65/* 2 variants of Kuroutoshikou CX23416GYC-STVLP (Yuan MPG600GR OEM) */
66#define IVTV_CARD_CX23416GYC_NOGR (IVTV_CARD_LAST+2)
67#define IVTV_CARD_CX23416GYC_NOGRYCS (IVTV_CARD_LAST+3)
68
69/* system vendor and device IDs */
70#define PCI_VENDOR_ID_ICOMP 0x4444
71#define PCI_DEVICE_ID_IVTV15 0x0803
72#define PCI_DEVICE_ID_IVTV16 0x0016
73
74/* subsystem vendor ID */
75#define IVTV_PCI_ID_HAUPPAUGE 0x0070
76#define IVTV_PCI_ID_HAUPPAUGE_ALT1 0x0270
77#define IVTV_PCI_ID_HAUPPAUGE_ALT2 0x4070
78#define IVTV_PCI_ID_ADAPTEC 0x9005
79#define IVTV_PCI_ID_ASUSTEK 0x1043
80#define IVTV_PCI_ID_AVERMEDIA 0x1461
81#define IVTV_PCI_ID_YUAN1 0x12ab
82#define IVTV_PCI_ID_YUAN2 0xff01
83#define IVTV_PCI_ID_YUAN3 0xffab
84#define IVTV_PCI_ID_YUAN4 0xfbab
85#define IVTV_PCI_ID_DIAMONDMM 0xff92
86#define IVTV_PCI_ID_IODATA 0x10fc
87#define IVTV_PCI_ID_MELCO 0x1154
88#define IVTV_PCI_ID_GOTVIEW1 0xffac
89#define IVTV_PCI_ID_GOTVIEW2 0xffad
90#define IVTV_PCI_ID_SONY 0x104d
91
92/* hardware flags, no gaps allowed */
93#define IVTV_HW_CX25840 (1 << 0)
94#define IVTV_HW_SAA7115 (1 << 1)
95#define IVTV_HW_SAA7127 (1 << 2)
96#define IVTV_HW_MSP34XX (1 << 3)
97#define IVTV_HW_TUNER (1 << 4)
98#define IVTV_HW_WM8775 (1 << 5)
99#define IVTV_HW_CS53L32A (1 << 6)
100#define IVTV_HW_TVEEPROM (1 << 7)
101#define IVTV_HW_SAA7114 (1 << 8)
102#define IVTV_HW_UPD64031A (1 << 9)
103#define IVTV_HW_UPD6408X (1 << 10)
104#define IVTV_HW_SAA717X (1 << 11)
105#define IVTV_HW_WM8739 (1 << 12)
106#define IVTV_HW_VP27SMPX (1 << 13)
107#define IVTV_HW_M52790 (1 << 14)
108#define IVTV_HW_GPIO (1 << 15)
109#define IVTV_HW_I2C_IR_RX_AVER (1 << 16)
110#define IVTV_HW_I2C_IR_RX_HAUP_EXT (1 << 17) /* External before internal */
111#define IVTV_HW_I2C_IR_RX_HAUP_INT (1 << 18)
112#define IVTV_HW_Z8F0811_IR_TX_HAUP (1 << 19)
113#define IVTV_HW_Z8F0811_IR_RX_HAUP (1 << 20)
114#define IVTV_HW_I2C_IR_RX_ADAPTEC (1 << 21)
115
116#define IVTV_HW_Z8F0811_IR_HAUP (IVTV_HW_Z8F0811_IR_RX_HAUP | \
117 IVTV_HW_Z8F0811_IR_TX_HAUP)
118
119#define IVTV_HW_SAA711X (IVTV_HW_SAA7115 | IVTV_HW_SAA7114)
120
121#define IVTV_HW_IR_RX_ANY (IVTV_HW_I2C_IR_RX_AVER | \
122 IVTV_HW_I2C_IR_RX_HAUP_EXT | \
123 IVTV_HW_I2C_IR_RX_HAUP_INT | \
124 IVTV_HW_Z8F0811_IR_RX_HAUP | \
125 IVTV_HW_I2C_IR_RX_ADAPTEC)
126
127#define IVTV_HW_IR_TX_ANY (IVTV_HW_Z8F0811_IR_TX_HAUP)
128
129#define IVTV_HW_IR_ANY (IVTV_HW_IR_RX_ANY | IVTV_HW_IR_TX_ANY)
130
131/* video inputs */
132#define IVTV_CARD_INPUT_VID_TUNER 1
133#define IVTV_CARD_INPUT_SVIDEO1 2
134#define IVTV_CARD_INPUT_SVIDEO2 3
135#define IVTV_CARD_INPUT_COMPOSITE1 4
136#define IVTV_CARD_INPUT_COMPOSITE2 5
137#define IVTV_CARD_INPUT_COMPOSITE3 6
138
139/* audio inputs */
140#define IVTV_CARD_INPUT_AUD_TUNER 1
141#define IVTV_CARD_INPUT_LINE_IN1 2
142#define IVTV_CARD_INPUT_LINE_IN2 3
143
144#define IVTV_CARD_MAX_VIDEO_INPUTS 6
145#define IVTV_CARD_MAX_AUDIO_INPUTS 3
146#define IVTV_CARD_MAX_TUNERS 3
147
148/* SAA71XX HW inputs */
149#define IVTV_SAA71XX_COMPOSITE0 0
150#define IVTV_SAA71XX_COMPOSITE1 1
151#define IVTV_SAA71XX_COMPOSITE2 2
152#define IVTV_SAA71XX_COMPOSITE3 3
153#define IVTV_SAA71XX_COMPOSITE4 4
154#define IVTV_SAA71XX_COMPOSITE5 5
155#define IVTV_SAA71XX_SVIDEO0 6
156#define IVTV_SAA71XX_SVIDEO1 7
157#define IVTV_SAA71XX_SVIDEO2 8
158#define IVTV_SAA71XX_SVIDEO3 9
159
160/* SAA717X needs to mark the tuner input by ORing with this flag */
161#define IVTV_SAA717X_TUNER_FLAG 0x80
162
163/* Dummy HW input */
164#define IVTV_DUMMY_AUDIO 0
165
166/* GPIO HW inputs */
167#define IVTV_GPIO_TUNER 0
168#define IVTV_GPIO_LINE_IN 1
169
170/* SAA717X HW inputs */
171#define IVTV_SAA717X_IN0 0
172#define IVTV_SAA717X_IN1 1
173#define IVTV_SAA717X_IN2 2
174
175/* V4L2 capability aliases */
176#define IVTV_CAP_ENCODER (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER | \
177 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE | \
178 V4L2_CAP_SLICED_VBI_CAPTURE)
179#define IVTV_CAP_DECODER (V4L2_CAP_VIDEO_OUTPUT | \
180 V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_OVERLAY)
181
182struct ivtv_card_video_input {
183 u8 video_type; /* video input type */
184 u8 audio_index; /* index in ivtv_card_audio_input array */
185 u16 video_input; /* hardware video input */
186};
187
188struct ivtv_card_audio_input {
189 u8 audio_type; /* audio input type */
190 u32 audio_input; /* hardware audio input */
191 u16 muxer_input; /* hardware muxer input for boards with a
192 multiplexer chip */
193};
194
195struct ivtv_card_output {
196 u8 name[32];
197 u16 video_output; /* hardware video output */
198};
199
200struct ivtv_card_pci_info {
201 u16 device;
202 u16 subsystem_vendor;
203 u16 subsystem_device;
204};
205
206/* GPIO definitions */
207
208/* The mask is the set of bits used by the operation */
209
210struct ivtv_gpio_init { /* set initial GPIO DIR and OUT values */
211 u16 direction; /* DIR setting. Leave to 0 if no init is needed */
212 u16 initial_value;
213};
214
215struct ivtv_gpio_video_input { /* select tuner/line in input */
216 u16 mask; /* leave to 0 if not supported */
217 u16 tuner;
218 u16 composite;
219 u16 svideo;
220};
221
222struct ivtv_gpio_audio_input { /* select tuner/line in input */
223 u16 mask; /* leave to 0 if not supported */
224 u16 tuner;
225 u16 linein;
226 u16 radio;
227};
228
229struct ivtv_gpio_audio_mute {
230 u16 mask; /* leave to 0 if not supported */
231 u16 mute; /* set this value to mute, 0 to unmute */
232};
233
234struct ivtv_gpio_audio_mode {
235 u16 mask; /* leave to 0 if not supported */
236 u16 mono; /* set audio to mono */
237 u16 stereo; /* set audio to stereo */
238 u16 lang1; /* set audio to the first language */
239 u16 lang2; /* set audio to the second language */
240 u16 both; /* both languages are output */
241};
242
243struct ivtv_gpio_audio_freq {
244 u16 mask; /* leave to 0 if not supported */
245 u16 f32000;
246 u16 f44100;
247 u16 f48000;
248};
249
250struct ivtv_gpio_audio_detect {
251 u16 mask; /* leave to 0 if not supported */
252 u16 stereo; /* if the input matches this value then
253 stereo is detected */
254};
255
256struct ivtv_card_tuner {
257 v4l2_std_id std; /* standard for which the tuner is suitable */
258 int tuner; /* tuner ID (from tuner.h) */
259};
260
261struct ivtv_card_tuner_i2c {
262 unsigned short radio[2];/* radio tuner i2c address to probe */
263 unsigned short demod[2];/* demodulator i2c address to probe */
264 unsigned short tv[4]; /* tv tuner i2c addresses to probe */
265};
266
267/* for card information/parameters */
268struct ivtv_card {
269 int type;
270 char *name;
271 char *comment;
272 u32 v4l2_capabilities;
273 u32 hw_video; /* hardware used to process video */
274 u32 hw_audio; /* hardware used to process audio */
275 u32 hw_audio_ctrl; /* hardware used for the V4L2 controls (only 1 dev allowed) */
276 u32 hw_muxer; /* hardware used to multiplex audio input */
277 u32 hw_all; /* all hardware used by the board */
278 struct ivtv_card_video_input video_inputs[IVTV_CARD_MAX_VIDEO_INPUTS];
279 struct ivtv_card_audio_input audio_inputs[IVTV_CARD_MAX_AUDIO_INPUTS];
280 struct ivtv_card_audio_input radio_input;
281 int nof_outputs;
282 const struct ivtv_card_output *video_outputs;
283 u8 gr_config; /* config byte for the ghost reduction device */
284 u8 xceive_pin; /* XCeive tuner GPIO reset pin */
285
286 /* GPIO card-specific settings */
287 struct ivtv_gpio_init gpio_init;
288 struct ivtv_gpio_video_input gpio_video_input;
289 struct ivtv_gpio_audio_input gpio_audio_input;
290 struct ivtv_gpio_audio_mute gpio_audio_mute;
291 struct ivtv_gpio_audio_mode gpio_audio_mode;
292 struct ivtv_gpio_audio_freq gpio_audio_freq;
293 struct ivtv_gpio_audio_detect gpio_audio_detect;
294
295 struct ivtv_card_tuner tuners[IVTV_CARD_MAX_TUNERS];
296 struct ivtv_card_tuner_i2c *i2c;
297
298 /* list of device and subsystem vendor/devices that
299 correspond to this card type. */
300 const struct ivtv_card_pci_info *pci_list;
301};
302
303int ivtv_get_input(struct ivtv *itv, u16 index, struct v4l2_input *input);
304int ivtv_get_output(struct ivtv *itv, u16 index, struct v4l2_output *output);
305int ivtv_get_audio_input(struct ivtv *itv, u16 index, struct v4l2_audio *input);
306int ivtv_get_audio_output(struct ivtv *itv, u16 index, struct v4l2_audioout *output);
307const struct ivtv_card *ivtv_get_card(u16 index);
308
309#endif
diff --git a/drivers/media/pci/ivtv/ivtv-controls.c b/drivers/media/pci/ivtv/ivtv-controls.c
new file mode 100644
index 000000000000..c60424601cb9
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-controls.c
@@ -0,0 +1,163 @@
1/*
2 ioctl control functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "ivtv-driver.h"
22#include "ivtv-ioctl.h"
23#include "ivtv-controls.h"
24#include "ivtv-mailbox.h"
25
26static int ivtv_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
27{
28 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
29
30 /* First try to allocate sliced VBI buffers if needed. */
31 if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
32 int i;
33
34 for (i = 0; i < IVTV_VBI_FRAMES; i++) {
35 /* Yuck, hardcoded. Needs to be a define */
36 itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
37 if (itv->vbi.sliced_mpeg_data[i] == NULL) {
38 while (--i >= 0) {
39 kfree(itv->vbi.sliced_mpeg_data[i]);
40 itv->vbi.sliced_mpeg_data[i] = NULL;
41 }
42 return -ENOMEM;
43 }
44 }
45 }
46
47 itv->vbi.insert_mpeg = fmt;
48
49 if (itv->vbi.insert_mpeg == 0) {
50 return 0;
51 }
52 /* Need sliced data for mpeg insertion */
53 if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
54 if (itv->is_60hz)
55 itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
56 else
57 itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
58 ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
59 }
60 return 0;
61}
62
63static int ivtv_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
64{
65 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
66 int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
67 struct v4l2_mbus_framefmt fmt;
68
69 /* fix videodecoder resolution */
70 fmt.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
71 fmt.height = cxhdl->height;
72 fmt.code = V4L2_MBUS_FMT_FIXED;
73 v4l2_subdev_call(itv->sd_video, video, s_mbus_fmt, &fmt);
74 return 0;
75}
76
77static int ivtv_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
78{
79 static const u32 freqs[3] = { 44100, 48000, 32000 };
80 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
81
82 /* The audio clock of the digitizer must match the codec sample
83 rate otherwise you get some very strange effects. */
84 if (idx < ARRAY_SIZE(freqs))
85 ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
86 return 0;
87}
88
89static int ivtv_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
90{
91 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
92
93 itv->dualwatch_stereo_mode = val;
94 return 0;
95}
96
97struct cx2341x_handler_ops ivtv_cxhdl_ops = {
98 .s_audio_mode = ivtv_s_audio_mode,
99 .s_audio_sampling_freq = ivtv_s_audio_sampling_freq,
100 .s_video_encoding = ivtv_s_video_encoding,
101 .s_stream_vbi_fmt = ivtv_s_stream_vbi_fmt,
102};
103
104int ivtv_g_pts_frame(struct ivtv *itv, s64 *pts, s64 *frame)
105{
106 u32 data[CX2341X_MBOX_MAX_DATA];
107
108 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
109 *pts = (s64)((u64)itv->last_dec_timing[2] << 32) |
110 (u64)itv->last_dec_timing[1];
111 *frame = itv->last_dec_timing[0];
112 return 0;
113 }
114 *pts = 0;
115 *frame = 0;
116 if (atomic_read(&itv->decoding)) {
117 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
118 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
119 return -EIO;
120 }
121 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
122 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
123 *pts = (s64)((u64) data[2] << 32) | (u64) data[1];
124 *frame = data[0];
125 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
126 }
127 return 0;
128}
129
130static int ivtv_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
131{
132 struct ivtv *itv = container_of(ctrl->handler, struct ivtv, cxhdl.hdl);
133
134 switch (ctrl->id) {
135 /* V4L2_CID_MPEG_VIDEO_DEC_PTS and V4L2_CID_MPEG_VIDEO_DEC_FRAME
136 control cluster */
137 case V4L2_CID_MPEG_VIDEO_DEC_PTS:
138 return ivtv_g_pts_frame(itv, &itv->ctrl_pts->val64,
139 &itv->ctrl_frame->val64);
140 }
141 return 0;
142}
143
144static int ivtv_s_ctrl(struct v4l2_ctrl *ctrl)
145{
146 struct ivtv *itv = container_of(ctrl->handler, struct ivtv, cxhdl.hdl);
147
148 switch (ctrl->id) {
149 /* V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK and MULTILINGUAL_PLAYBACK
150 control cluster */
151 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
152 itv->audio_stereo_mode = itv->ctrl_audio_playback->val - 1;
153 itv->audio_bilingual_mode = itv->ctrl_audio_multilingual_playback->val - 1;
154 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
155 break;
156 }
157 return 0;
158}
159
160const struct v4l2_ctrl_ops ivtv_hdl_out_ops = {
161 .s_ctrl = ivtv_s_ctrl,
162 .g_volatile_ctrl = ivtv_g_volatile_ctrl,
163};
diff --git a/drivers/media/pci/ivtv/ivtv-controls.h b/drivers/media/pci/ivtv/ivtv-controls.h
new file mode 100644
index 000000000000..3999e6358312
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-controls.h
@@ -0,0 +1,28 @@
1/*
2 ioctl control functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_CONTROLS_H
22#define IVTV_CONTROLS_H
23
24extern struct cx2341x_handler_ops ivtv_cxhdl_ops;
25extern const struct v4l2_ctrl_ops ivtv_hdl_out_ops;
26int ivtv_g_pts_frame(struct ivtv *itv, s64 *pts, s64 *frame);
27
28#endif
diff --git a/drivers/media/pci/ivtv/ivtv-driver.c b/drivers/media/pci/ivtv/ivtv-driver.c
new file mode 100644
index 000000000000..5462ce2f60ea
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-driver.c
@@ -0,0 +1,1498 @@
1/*
2 ivtv driver initialization and card probing
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/* Main Driver file for the ivtv project:
23 * Driver for the Conexant CX23415/CX23416 chip.
24 * Author: Kevin Thayer (nufan_wfk at yahoo.com)
25 * License: GPL
26 * http://www.ivtvdriver.org
27 *
28 * -----
29 * MPG600/MPG160 support by T.Adachi <tadachi@tadachi-net.com>
30 * and Takeru KOMORIYA<komoriya@paken.org>
31 *
32 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
33 * using information provided by Jiun-Kuei Jung @ AVerMedia.
34 *
35 * Kurouto Sikou CX23416GYC-STVLP tested by K.Ohta <alpha292@bremen.or.jp>
36 * using information from T.Adachi,Takeru KOMORIYA and others :-)
37 *
38 * Nagase TRANSGEAR 5000TV, Aopen VA2000MAX-STN6 and I/O data GV-MVP/RX
39 * version by T.Adachi. Special thanks Mr.Suzuki
40 */
41
42#include "ivtv-driver.h"
43#include "ivtv-version.h"
44#include "ivtv-fileops.h"
45#include "ivtv-i2c.h"
46#include "ivtv-firmware.h"
47#include "ivtv-queue.h"
48#include "ivtv-udma.h"
49#include "ivtv-irq.h"
50#include "ivtv-mailbox.h"
51#include "ivtv-streams.h"
52#include "ivtv-ioctl.h"
53#include "ivtv-cards.h"
54#include "ivtv-vbi.h"
55#include "ivtv-routing.h"
56#include "ivtv-controls.h"
57#include "ivtv-gpio.h"
58#include <linux/dma-mapping.h>
59#include <media/tveeprom.h>
60#include <media/saa7115.h>
61#include <media/v4l2-chip-ident.h>
62#include "tuner-xc2028.h"
63
64/* If you have already X v4l cards, then set this to X. This way
65 the device numbers stay matched. Example: you have a WinTV card
66 without radio and a PVR-350 with. Normally this would give a
67 video1 device together with a radio0 device for the PVR. By
68 setting this to 1 you ensure that radio0 is now also radio1. */
69int ivtv_first_minor;
70
71/* add your revision and whatnot here */
72static struct pci_device_id ivtv_pci_tbl[] __devinitdata = {
73 {PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV15,
74 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
75 {PCI_VENDOR_ID_ICOMP, PCI_DEVICE_ID_IVTV16,
76 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
77 {0,}
78};
79
80MODULE_DEVICE_TABLE(pci,ivtv_pci_tbl);
81
82/* ivtv instance counter */
83static atomic_t ivtv_instance = ATOMIC_INIT(0);
84
85/* Parameter declarations */
86static int cardtype[IVTV_MAX_CARDS];
87static int tuner[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
88 -1, -1, -1, -1, -1, -1, -1, -1,
89 -1, -1, -1, -1, -1, -1, -1, -1,
90 -1, -1, -1, -1, -1, -1, -1, -1 };
91static int radio[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
92 -1, -1, -1, -1, -1, -1, -1, -1,
93 -1, -1, -1, -1, -1, -1, -1, -1,
94 -1, -1, -1, -1, -1, -1, -1, -1 };
95static int i2c_clock_period[IVTV_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
96 -1, -1, -1, -1, -1, -1, -1, -1,
97 -1, -1, -1, -1, -1, -1, -1, -1,
98 -1, -1, -1, -1, -1, -1, -1, -1 };
99
100static unsigned int cardtype_c = 1;
101static unsigned int tuner_c = 1;
102static int radio_c = 1;
103static unsigned int i2c_clock_period_c = 1;
104static char pal[] = "---";
105static char secam[] = "--";
106static char ntsc[] = "-";
107
108/* Buffers */
109
110/* DMA Buffers, Default size in MB allocated */
111#define IVTV_DEFAULT_ENC_MPG_BUFFERS 4
112#define IVTV_DEFAULT_ENC_YUV_BUFFERS 2
113#define IVTV_DEFAULT_ENC_VBI_BUFFERS 1
114/* Exception: size in kB for this stream (MB is overkill) */
115#define IVTV_DEFAULT_ENC_PCM_BUFFERS 320
116#define IVTV_DEFAULT_DEC_MPG_BUFFERS 1
117#define IVTV_DEFAULT_DEC_YUV_BUFFERS 1
118/* Exception: size in kB for this stream (MB is way overkill) */
119#define IVTV_DEFAULT_DEC_VBI_BUFFERS 64
120
121static int enc_mpg_buffers = IVTV_DEFAULT_ENC_MPG_BUFFERS;
122static int enc_yuv_buffers = IVTV_DEFAULT_ENC_YUV_BUFFERS;
123static int enc_vbi_buffers = IVTV_DEFAULT_ENC_VBI_BUFFERS;
124static int enc_pcm_buffers = IVTV_DEFAULT_ENC_PCM_BUFFERS;
125static int dec_mpg_buffers = IVTV_DEFAULT_DEC_MPG_BUFFERS;
126static int dec_yuv_buffers = IVTV_DEFAULT_DEC_YUV_BUFFERS;
127static int dec_vbi_buffers = IVTV_DEFAULT_DEC_VBI_BUFFERS;
128
129static int ivtv_yuv_mode;
130static int ivtv_yuv_threshold = -1;
131static int ivtv_pci_latency = 1;
132
133int ivtv_debug;
134#ifdef CONFIG_VIDEO_ADV_DEBUG
135int ivtv_fw_debug;
136#endif
137
138static int tunertype = -1;
139static int newi2c = -1;
140
141module_param_array(tuner, int, &tuner_c, 0644);
142module_param_array(radio, int, &radio_c, 0644);
143module_param_array(cardtype, int, &cardtype_c, 0644);
144module_param_string(pal, pal, sizeof(pal), 0644);
145module_param_string(secam, secam, sizeof(secam), 0644);
146module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
147module_param_named(debug,ivtv_debug, int, 0644);
148#ifdef CONFIG_VIDEO_ADV_DEBUG
149module_param_named(fw_debug, ivtv_fw_debug, int, 0644);
150#endif
151module_param(ivtv_pci_latency, int, 0644);
152module_param(ivtv_yuv_mode, int, 0644);
153module_param(ivtv_yuv_threshold, int, 0644);
154module_param(ivtv_first_minor, int, 0644);
155
156module_param(enc_mpg_buffers, int, 0644);
157module_param(enc_yuv_buffers, int, 0644);
158module_param(enc_vbi_buffers, int, 0644);
159module_param(enc_pcm_buffers, int, 0644);
160module_param(dec_mpg_buffers, int, 0644);
161module_param(dec_yuv_buffers, int, 0644);
162module_param(dec_vbi_buffers, int, 0644);
163
164module_param(tunertype, int, 0644);
165module_param(newi2c, int, 0644);
166module_param_array(i2c_clock_period, int, &i2c_clock_period_c, 0644);
167
168MODULE_PARM_DESC(tuner, "Tuner type selection,\n"
169 "\t\t\tsee tuner.h for values");
170MODULE_PARM_DESC(radio,
171 "Enable or disable the radio. Use only if autodetection\n"
172 "\t\t\tfails. 0 = disable, 1 = enable");
173MODULE_PARM_DESC(cardtype,
174 "Only use this option if your card is not detected properly.\n"
175 "\t\tSpecify card type:\n"
176 "\t\t\t 1 = WinTV PVR 250\n"
177 "\t\t\t 2 = WinTV PVR 350\n"
178 "\t\t\t 3 = WinTV PVR-150 or PVR-500\n"
179 "\t\t\t 4 = AVerMedia M179\n"
180 "\t\t\t 5 = YUAN MPG600/Kuroutoshikou iTVC16-STVLP\n"
181 "\t\t\t 6 = YUAN MPG160/Kuroutoshikou iTVC15-STVLP\n"
182 "\t\t\t 7 = YUAN PG600/DIAMONDMM PVR-550 (CX Falcon 2)\n"
183 "\t\t\t 8 = Adaptec AVC-2410\n"
184 "\t\t\t 9 = Adaptec AVC-2010\n"
185 "\t\t\t10 = NAGASE TRANSGEAR 5000TV\n"
186 "\t\t\t11 = AOpen VA2000MAX-STN6\n"
187 "\t\t\t12 = YUAN MPG600GR/Kuroutoshikou CX23416GYC-STVLP\n"
188 "\t\t\t13 = I/O Data GV-MVP/RX\n"
189 "\t\t\t14 = I/O Data GV-MVP/RX2E\n"
190 "\t\t\t15 = GOTVIEW PCI DVD\n"
191 "\t\t\t16 = GOTVIEW PCI DVD2 Deluxe\n"
192 "\t\t\t17 = Yuan MPC622\n"
193 "\t\t\t18 = Digital Cowboy DCT-MTVP1\n"
194 "\t\t\t19 = Yuan PG600V2/GotView PCI DVD Lite\n"
195 "\t\t\t20 = Club3D ZAP-TV1x01\n"
196 "\t\t\t21 = AverTV MCE 116 Plus\n"
197 "\t\t\t22 = ASUS Falcon2\n"
198 "\t\t\t23 = AverMedia PVR-150 Plus\n"
199 "\t\t\t24 = AverMedia EZMaker PCI Deluxe\n"
200 "\t\t\t25 = AverMedia M104 (not yet working)\n"
201 "\t\t\t26 = Buffalo PC-MV5L/PCI\n"
202 "\t\t\t27 = AVerMedia UltraTV 1500 MCE\n"
203 "\t\t\t28 = Sony VAIO Giga Pocket (ENX Kikyou)\n"
204 "\t\t\t 0 = Autodetect (default)\n"
205 "\t\t\t-1 = Ignore this card\n\t\t");
206MODULE_PARM_DESC(pal, "Set PAL standard: BGH, DK, I, M, N, Nc, 60");
207MODULE_PARM_DESC(secam, "Set SECAM standard: BGH, DK, L, LC");
208MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J (Japan), K (South Korea)");
209MODULE_PARM_DESC(tunertype,
210 "Specify tuner type:\n"
211 "\t\t\t 0 = tuner for PAL-B/G/H/D/K/I, SECAM-B/G/H/D/K/L/Lc\n"
212 "\t\t\t 1 = tuner for NTSC-M/J/K, PAL-M/N/Nc\n"
213 "\t\t\t-1 = Autodetect (default)\n");
214MODULE_PARM_DESC(debug,
215 "Debug level (bitmask). Default: 0\n"
216 "\t\t\t 1/0x0001: warning\n"
217 "\t\t\t 2/0x0002: info\n"
218 "\t\t\t 4/0x0004: mailbox\n"
219 "\t\t\t 8/0x0008: ioctl\n"
220 "\t\t\t 16/0x0010: file\n"
221 "\t\t\t 32/0x0020: dma\n"
222 "\t\t\t 64/0x0040: irq\n"
223 "\t\t\t 128/0x0080: decoder\n"
224 "\t\t\t 256/0x0100: yuv\n"
225 "\t\t\t 512/0x0200: i2c\n"
226 "\t\t\t1024/0x0400: high volume\n");
227#ifdef CONFIG_VIDEO_ADV_DEBUG
228MODULE_PARM_DESC(fw_debug,
229 "Enable code for debugging firmware problems. Default: 0\n");
230#endif
231MODULE_PARM_DESC(ivtv_pci_latency,
232 "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n"
233 "\t\t\tDefault: Yes");
234MODULE_PARM_DESC(ivtv_yuv_mode,
235 "Specify the yuv playback mode:\n"
236 "\t\t\t0 = interlaced\n\t\t\t1 = progressive\n\t\t\t2 = auto\n"
237 "\t\t\tDefault: 0 (interlaced)");
238MODULE_PARM_DESC(ivtv_yuv_threshold,
239 "If ivtv_yuv_mode is 2 (auto) then playback content as\n\t\tprogressive if src height <= ivtv_yuvthreshold\n"
240 "\t\t\tDefault: 480");
241MODULE_PARM_DESC(enc_mpg_buffers,
242 "Encoder MPG Buffers (in MB)\n"
243 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_MPG_BUFFERS));
244MODULE_PARM_DESC(enc_yuv_buffers,
245 "Encoder YUV Buffers (in MB)\n"
246 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_YUV_BUFFERS));
247MODULE_PARM_DESC(enc_vbi_buffers,
248 "Encoder VBI Buffers (in MB)\n"
249 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_VBI_BUFFERS));
250MODULE_PARM_DESC(enc_pcm_buffers,
251 "Encoder PCM buffers (in kB)\n"
252 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_ENC_PCM_BUFFERS));
253MODULE_PARM_DESC(dec_mpg_buffers,
254 "Decoder MPG buffers (in MB)\n"
255 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_MPG_BUFFERS));
256MODULE_PARM_DESC(dec_yuv_buffers,
257 "Decoder YUV buffers (in MB)\n"
258 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_YUV_BUFFERS));
259MODULE_PARM_DESC(dec_vbi_buffers,
260 "Decoder VBI buffers (in kB)\n"
261 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_DEC_VBI_BUFFERS));
262MODULE_PARM_DESC(newi2c,
263 "Use new I2C implementation\n"
264 "\t\t\t-1 is autodetect, 0 is off, 1 is on\n"
265 "\t\t\tDefault is autodetect");
266MODULE_PARM_DESC(i2c_clock_period,
267 "Period of SCL for the I2C bus controlled by the CX23415/6\n"
268 "\t\t\tMin: 10 usec (100 kHz), Max: 4500 usec (222 Hz)\n"
269 "\t\t\tDefault: " __stringify(IVTV_DEFAULT_I2C_CLOCK_PERIOD));
270
271MODULE_PARM_DESC(ivtv_first_minor, "Set device node number assigned to first card");
272
273MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil");
274MODULE_DESCRIPTION("CX23415/CX23416 driver");
275MODULE_SUPPORTED_DEVICE
276 ("CX23415/CX23416 MPEG2 encoder (WinTV PVR-150/250/350/500,\n"
277 "\t\t\tYuan MPG series and similar)");
278MODULE_LICENSE("GPL");
279
280MODULE_VERSION(IVTV_VERSION);
281
282void ivtv_clear_irq_mask(struct ivtv *itv, u32 mask)
283{
284 itv->irqmask &= ~mask;
285 write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
286}
287
288void ivtv_set_irq_mask(struct ivtv *itv, u32 mask)
289{
290 itv->irqmask |= mask;
291 write_reg_sync(itv->irqmask, IVTV_REG_IRQMASK);
292}
293
294int ivtv_set_output_mode(struct ivtv *itv, int mode)
295{
296 int old_mode;
297
298 spin_lock(&itv->lock);
299 old_mode = itv->output_mode;
300 if (old_mode == 0)
301 itv->output_mode = old_mode = mode;
302 spin_unlock(&itv->lock);
303 return old_mode;
304}
305
306struct ivtv_stream *ivtv_get_output_stream(struct ivtv *itv)
307{
308 switch (itv->output_mode) {
309 case OUT_MPG:
310 return &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
311 case OUT_YUV:
312 return &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
313 default:
314 return NULL;
315 }
316}
317
318int ivtv_waitq(wait_queue_head_t *waitq)
319{
320 DEFINE_WAIT(wait);
321
322 prepare_to_wait(waitq, &wait, TASK_INTERRUPTIBLE);
323 schedule();
324 finish_wait(waitq, &wait);
325 return signal_pending(current) ? -EINTR : 0;
326}
327
328/* Generic utility functions */
329int ivtv_msleep_timeout(unsigned int msecs, int intr)
330{
331 int timeout = msecs_to_jiffies(msecs);
332
333 do {
334 set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
335 timeout = schedule_timeout(timeout);
336 if (intr) {
337 int ret = signal_pending(current);
338
339 if (ret)
340 return ret;
341 }
342 } while (timeout);
343 return 0;
344}
345
346/* Release ioremapped memory */
347static void ivtv_iounmap(struct ivtv *itv)
348{
349 if (itv == NULL)
350 return;
351
352 /* Release registers memory */
353 if (itv->reg_mem != NULL) {
354 IVTV_DEBUG_INFO("releasing reg_mem\n");
355 iounmap(itv->reg_mem);
356 itv->reg_mem = NULL;
357 }
358 /* Release io memory */
359 if (itv->has_cx23415 && itv->dec_mem != NULL) {
360 IVTV_DEBUG_INFO("releasing dec_mem\n");
361 iounmap(itv->dec_mem);
362 }
363 itv->dec_mem = NULL;
364
365 /* Release io memory */
366 if (itv->enc_mem != NULL) {
367 IVTV_DEBUG_INFO("releasing enc_mem\n");
368 iounmap(itv->enc_mem);
369 itv->enc_mem = NULL;
370 }
371}
372
373/* Hauppauge card? get values from tveeprom */
374void ivtv_read_eeprom(struct ivtv *itv, struct tveeprom *tv)
375{
376 u8 eedata[256];
377
378 itv->i2c_client.addr = 0xA0 >> 1;
379 tveeprom_read(&itv->i2c_client, eedata, sizeof(eedata));
380 tveeprom_hauppauge_analog(&itv->i2c_client, tv, eedata);
381}
382
383static void ivtv_process_eeprom(struct ivtv *itv)
384{
385 struct tveeprom tv;
386 int pci_slot = PCI_SLOT(itv->pdev->devfn);
387
388 ivtv_read_eeprom(itv, &tv);
389
390 /* Many thanks to Steven Toth from Hauppauge for providing the
391 model numbers */
392 switch (tv.model) {
393 /* In a few cases the PCI subsystem IDs do not correctly
394 identify the card. A better method is to check the
395 model number from the eeprom instead. */
396 case 30012 ... 30039: /* Low profile PVR250 */
397 case 32000 ... 32999:
398 case 48000 ... 48099: /* 48??? range are PVR250s with a cx23415 */
399 case 48400 ... 48599:
400 itv->card = ivtv_get_card(IVTV_CARD_PVR_250);
401 break;
402 case 48100 ... 48399:
403 case 48600 ... 48999:
404 itv->card = ivtv_get_card(IVTV_CARD_PVR_350);
405 break;
406 case 23000 ... 23999: /* PVR500 */
407 case 25000 ... 25999: /* Low profile PVR150 */
408 case 26000 ... 26999: /* Regular PVR150 */
409 itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
410 break;
411 case 0:
412 IVTV_ERR("Invalid EEPROM\n");
413 return;
414 default:
415 IVTV_ERR("Unknown model %d, defaulting to PVR-150\n", tv.model);
416 itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
417 break;
418 }
419
420 switch (tv.model) {
421 /* Old style PVR350 (with an saa7114) uses this input for
422 the tuner. */
423 case 48254:
424 itv->card = ivtv_get_card(IVTV_CARD_PVR_350_V1);
425 break;
426 default:
427 break;
428 }
429
430 itv->v4l2_cap = itv->card->v4l2_capabilities;
431 itv->card_name = itv->card->name;
432 itv->card_i2c = itv->card->i2c;
433
434 /* If this is a PVR500 then it should be possible to detect whether it is the
435 first or second unit by looking at the subsystem device ID: is bit 4 is
436 set, then it is the second unit (according to info from Hauppauge).
437
438 However, while this works for most cards, I have seen a few PVR500 cards
439 where both units have the same subsystem ID.
440
441 So instead I look at the reported 'PCI slot' (which is the slot on the PVR500
442 PCI bridge) and if it is 8, then it is assumed to be the first unit, otherwise
443 it is the second unit. It is possible that it is a different slot when ivtv is
444 used in Xen, in that case I ignore this card here. The worst that can happen
445 is that the card presents itself with a non-working radio device.
446
447 This detection is needed since the eeprom reports incorrectly that a radio is
448 present on the second unit. */
449 if (tv.model / 1000 == 23) {
450 static const struct ivtv_card_tuner_i2c ivtv_i2c_radio = {
451 .radio = { 0x60, I2C_CLIENT_END },
452 .demod = { 0x43, I2C_CLIENT_END },
453 .tv = { 0x61, I2C_CLIENT_END },
454 };
455
456 itv->card_name = "WinTV PVR 500";
457 itv->card_i2c = &ivtv_i2c_radio;
458 if (pci_slot == 8 || pci_slot == 9) {
459 int is_first = (pci_slot & 1) == 0;
460
461 itv->card_name = is_first ? "WinTV PVR 500 (unit #1)" :
462 "WinTV PVR 500 (unit #2)";
463 if (!is_first) {
464 IVTV_INFO("Correcting tveeprom data: no radio present on second unit\n");
465 tv.has_radio = 0;
466 }
467 }
468 }
469 IVTV_INFO("Autodetected %s\n", itv->card_name);
470
471 switch (tv.tuner_hauppauge_model) {
472 case 85:
473 case 99:
474 case 112:
475 itv->pvr150_workaround = 1;
476 break;
477 default:
478 break;
479 }
480 if (tv.tuner_type == TUNER_ABSENT)
481 IVTV_ERR("tveeprom cannot autodetect tuner!\n");
482
483 if (itv->options.tuner == -1)
484 itv->options.tuner = tv.tuner_type;
485 if (itv->options.radio == -1)
486 itv->options.radio = (tv.has_radio != 0);
487 /* only enable newi2c if an IR blaster is present */
488 if (itv->options.newi2c == -1 && tv.has_ir) {
489 itv->options.newi2c = (tv.has_ir & 4) ? 1 : 0;
490 if (itv->options.newi2c) {
491 IVTV_INFO("Reopen i2c bus for IR-blaster support\n");
492 exit_ivtv_i2c(itv);
493 init_ivtv_i2c(itv);
494 }
495 }
496
497 if (itv->std != 0)
498 /* user specified tuner standard */
499 return;
500
501 /* autodetect tuner standard */
502 if (tv.tuner_formats & V4L2_STD_PAL) {
503 IVTV_DEBUG_INFO("PAL tuner detected\n");
504 itv->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
505 } else if (tv.tuner_formats & V4L2_STD_NTSC) {
506 IVTV_DEBUG_INFO("NTSC tuner detected\n");
507 itv->std |= V4L2_STD_NTSC_M;
508 } else if (tv.tuner_formats & V4L2_STD_SECAM) {
509 IVTV_DEBUG_INFO("SECAM tuner detected\n");
510 itv->std |= V4L2_STD_SECAM_L;
511 } else {
512 IVTV_INFO("No tuner detected, default to NTSC-M\n");
513 itv->std |= V4L2_STD_NTSC_M;
514 }
515}
516
517static v4l2_std_id ivtv_parse_std(struct ivtv *itv)
518{
519 switch (pal[0]) {
520 case '6':
521 tunertype = 0;
522 return V4L2_STD_PAL_60;
523 case 'b':
524 case 'B':
525 case 'g':
526 case 'G':
527 case 'h':
528 case 'H':
529 tunertype = 0;
530 return V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
531 case 'n':
532 case 'N':
533 tunertype = 1;
534 if (pal[1] == 'c' || pal[1] == 'C')
535 return V4L2_STD_PAL_Nc;
536 return V4L2_STD_PAL_N;
537 case 'i':
538 case 'I':
539 tunertype = 0;
540 return V4L2_STD_PAL_I;
541 case 'd':
542 case 'D':
543 case 'k':
544 case 'K':
545 tunertype = 0;
546 return V4L2_STD_PAL_DK;
547 case 'M':
548 case 'm':
549 tunertype = 1;
550 return V4L2_STD_PAL_M;
551 case '-':
552 break;
553 default:
554 IVTV_WARN("pal= argument not recognised\n");
555 return 0;
556 }
557
558 switch (secam[0]) {
559 case 'b':
560 case 'B':
561 case 'g':
562 case 'G':
563 case 'h':
564 case 'H':
565 tunertype = 0;
566 return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
567 case 'd':
568 case 'D':
569 case 'k':
570 case 'K':
571 tunertype = 0;
572 return V4L2_STD_SECAM_DK;
573 case 'l':
574 case 'L':
575 tunertype = 0;
576 if (secam[1] == 'C' || secam[1] == 'c')
577 return V4L2_STD_SECAM_LC;
578 return V4L2_STD_SECAM_L;
579 case '-':
580 break;
581 default:
582 IVTV_WARN("secam= argument not recognised\n");
583 return 0;
584 }
585
586 switch (ntsc[0]) {
587 case 'm':
588 case 'M':
589 tunertype = 1;
590 return V4L2_STD_NTSC_M;
591 case 'j':
592 case 'J':
593 tunertype = 1;
594 return V4L2_STD_NTSC_M_JP;
595 case 'k':
596 case 'K':
597 tunertype = 1;
598 return V4L2_STD_NTSC_M_KR;
599 case '-':
600 break;
601 default:
602 IVTV_WARN("ntsc= argument not recognised\n");
603 return 0;
604 }
605
606 /* no match found */
607 return 0;
608}
609
610static void ivtv_process_options(struct ivtv *itv)
611{
612 const char *chipname;
613 int i, j;
614
615 itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers * 1024;
616 itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers * 1024;
617 itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers * 1024;
618 itv->options.kilobytes[IVTV_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers;
619 itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_MPG] = dec_mpg_buffers * 1024;
620 itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_YUV] = dec_yuv_buffers * 1024;
621 itv->options.kilobytes[IVTV_DEC_STREAM_TYPE_VBI] = dec_vbi_buffers;
622 itv->options.cardtype = cardtype[itv->instance];
623 itv->options.tuner = tuner[itv->instance];
624 itv->options.radio = radio[itv->instance];
625
626 itv->options.i2c_clock_period = i2c_clock_period[itv->instance];
627 if (itv->options.i2c_clock_period == -1)
628 itv->options.i2c_clock_period = IVTV_DEFAULT_I2C_CLOCK_PERIOD;
629 else if (itv->options.i2c_clock_period < 10)
630 itv->options.i2c_clock_period = 10;
631 else if (itv->options.i2c_clock_period > 4500)
632 itv->options.i2c_clock_period = 4500;
633
634 itv->options.newi2c = newi2c;
635 if (tunertype < -1 || tunertype > 1) {
636 IVTV_WARN("Invalid tunertype argument, will autodetect instead\n");
637 tunertype = -1;
638 }
639 itv->std = ivtv_parse_std(itv);
640 if (itv->std == 0 && tunertype >= 0)
641 itv->std = tunertype ? V4L2_STD_MN : (V4L2_STD_ALL & ~V4L2_STD_MN);
642 itv->has_cx23415 = (itv->pdev->device == PCI_DEVICE_ID_IVTV15);
643 chipname = itv->has_cx23415 ? "cx23415" : "cx23416";
644 if (itv->options.cardtype == -1) {
645 IVTV_INFO("Ignore card (detected %s based chip)\n", chipname);
646 return;
647 }
648 if ((itv->card = ivtv_get_card(itv->options.cardtype - 1))) {
649 IVTV_INFO("User specified %s card (detected %s based chip)\n",
650 itv->card->name, chipname);
651 } else if (itv->options.cardtype != 0) {
652 IVTV_ERR("Unknown user specified type, trying to autodetect card\n");
653 }
654 if (itv->card == NULL) {
655 if (itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE ||
656 itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT1 ||
657 itv->pdev->subsystem_vendor == IVTV_PCI_ID_HAUPPAUGE_ALT2) {
658 itv->card = ivtv_get_card(itv->has_cx23415 ? IVTV_CARD_PVR_350 : IVTV_CARD_PVR_150);
659 IVTV_INFO("Autodetected Hauppauge card (%s based)\n",
660 chipname);
661 }
662 }
663 if (itv->card == NULL) {
664 for (i = 0; (itv->card = ivtv_get_card(i)); i++) {
665 if (itv->card->pci_list == NULL)
666 continue;
667 for (j = 0; itv->card->pci_list[j].device; j++) {
668 if (itv->pdev->device !=
669 itv->card->pci_list[j].device)
670 continue;
671 if (itv->pdev->subsystem_vendor !=
672 itv->card->pci_list[j].subsystem_vendor)
673 continue;
674 if (itv->pdev->subsystem_device !=
675 itv->card->pci_list[j].subsystem_device)
676 continue;
677 IVTV_INFO("Autodetected %s card (%s based)\n",
678 itv->card->name, chipname);
679 goto done;
680 }
681 }
682 }
683done:
684
685 if (itv->card == NULL) {
686 itv->card = ivtv_get_card(IVTV_CARD_PVR_150);
687 IVTV_ERR("Unknown card: vendor/device: [%04x:%04x]\n",
688 itv->pdev->vendor, itv->pdev->device);
689 IVTV_ERR(" subsystem vendor/device: [%04x:%04x]\n",
690 itv->pdev->subsystem_vendor, itv->pdev->subsystem_device);
691 IVTV_ERR(" %s based\n", chipname);
692 IVTV_ERR("Defaulting to %s card\n", itv->card->name);
693 IVTV_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n");
694 IVTV_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n");
695 IVTV_ERR("Prefix your subject line with [UNKNOWN IVTV CARD].\n");
696 }
697 itv->v4l2_cap = itv->card->v4l2_capabilities;
698 itv->card_name = itv->card->name;
699 itv->card_i2c = itv->card->i2c;
700}
701
702/* Precondition: the ivtv structure has been memset to 0. Only
703 the dev and num fields have been filled in.
704 No assumptions on the card type may be made here (see ivtv_init_struct2
705 for that).
706 */
707static int __devinit ivtv_init_struct1(struct ivtv *itv)
708{
709 struct sched_param param = { .sched_priority = 99 };
710
711 itv->base_addr = pci_resource_start(itv->pdev, 0);
712 itv->enc_mbox.max_mbox = 2; /* the encoder has 3 mailboxes (0-2) */
713 itv->dec_mbox.max_mbox = 1; /* the decoder has 2 mailboxes (0-1) */
714
715 mutex_init(&itv->serialize_lock);
716 mutex_init(&itv->i2c_bus_lock);
717 mutex_init(&itv->udma.lock);
718
719 spin_lock_init(&itv->lock);
720 spin_lock_init(&itv->dma_reg_lock);
721
722 init_kthread_worker(&itv->irq_worker);
723 itv->irq_worker_task = kthread_run(kthread_worker_fn, &itv->irq_worker,
724 itv->v4l2_dev.name);
725 if (IS_ERR(itv->irq_worker_task)) {
726 IVTV_ERR("Could not create ivtv task\n");
727 return -1;
728 }
729 /* must use the FIFO scheduler as it is realtime sensitive */
730 sched_setscheduler(itv->irq_worker_task, SCHED_FIFO, &param);
731
732 init_kthread_work(&itv->irq_work, ivtv_irq_work_handler);
733
734 /* Initial settings */
735 itv->cxhdl.port = CX2341X_PORT_MEMORY;
736 itv->cxhdl.capabilities = CX2341X_CAP_HAS_SLICED_VBI;
737 init_waitqueue_head(&itv->eos_waitq);
738 init_waitqueue_head(&itv->event_waitq);
739 init_waitqueue_head(&itv->vsync_waitq);
740 init_waitqueue_head(&itv->dma_waitq);
741 init_timer(&itv->dma_timer);
742 itv->dma_timer.function = ivtv_unfinished_dma;
743 itv->dma_timer.data = (unsigned long)itv;
744
745 itv->cur_dma_stream = -1;
746 itv->cur_pio_stream = -1;
747
748 /* Ctrls */
749 itv->speed = 1000;
750
751 /* VBI */
752 itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
753 itv->vbi.sliced_in = &itv->vbi.in.fmt.sliced;
754
755 /* Init the sg table for osd/yuv output */
756 sg_init_table(itv->udma.SGlist, IVTV_DMA_SG_OSD_ENT);
757
758 /* OSD */
759 itv->osd_global_alpha_state = 1;
760 itv->osd_global_alpha = 255;
761
762 /* YUV */
763 atomic_set(&itv->yuv_info.next_dma_frame, -1);
764 itv->yuv_info.lace_mode = ivtv_yuv_mode;
765 itv->yuv_info.lace_threshold = ivtv_yuv_threshold;
766 itv->yuv_info.max_frames_buffered = 3;
767 itv->yuv_info.track_osd = 1;
768 return 0;
769}
770
771/* Second initialization part. Here the card type has been
772 autodetected. */
773static void __devinit ivtv_init_struct2(struct ivtv *itv)
774{
775 int i;
776
777 for (i = 0; i < IVTV_CARD_MAX_VIDEO_INPUTS; i++)
778 if (itv->card->video_inputs[i].video_type == 0)
779 break;
780 itv->nof_inputs = i;
781 for (i = 0; i < IVTV_CARD_MAX_AUDIO_INPUTS; i++)
782 if (itv->card->audio_inputs[i].audio_type == 0)
783 break;
784 itv->nof_audio_inputs = i;
785
786 if (itv->card->hw_all & IVTV_HW_CX25840) {
787 itv->vbi.sliced_size = 288; /* multiple of 16, real size = 284 */
788 } else {
789 itv->vbi.sliced_size = 64; /* multiple of 16, real size = 52 */
790 }
791
792 /* Find tuner input */
793 for (i = 0; i < itv->nof_inputs; i++) {
794 if (itv->card->video_inputs[i].video_type ==
795 IVTV_CARD_INPUT_VID_TUNER)
796 break;
797 }
798 if (i == itv->nof_inputs)
799 i = 0;
800 itv->active_input = i;
801 itv->audio_input = itv->card->video_inputs[i].audio_index;
802}
803
804static int ivtv_setup_pci(struct ivtv *itv, struct pci_dev *pdev,
805 const struct pci_device_id *pci_id)
806{
807 u16 cmd;
808 unsigned char pci_latency;
809
810 IVTV_DEBUG_INFO("Enabling pci device\n");
811
812 if (pci_enable_device(pdev)) {
813 IVTV_ERR("Can't enable device!\n");
814 return -EIO;
815 }
816 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
817 IVTV_ERR("No suitable DMA available.\n");
818 return -EIO;
819 }
820 if (!request_mem_region(itv->base_addr, IVTV_ENCODER_SIZE, "ivtv encoder")) {
821 IVTV_ERR("Cannot request encoder memory region.\n");
822 return -EIO;
823 }
824
825 if (!request_mem_region(itv->base_addr + IVTV_REG_OFFSET,
826 IVTV_REG_SIZE, "ivtv registers")) {
827 IVTV_ERR("Cannot request register memory region.\n");
828 release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
829 return -EIO;
830 }
831
832 if (itv->has_cx23415 &&
833 !request_mem_region(itv->base_addr + IVTV_DECODER_OFFSET,
834 IVTV_DECODER_SIZE, "ivtv decoder")) {
835 IVTV_ERR("Cannot request decoder memory region.\n");
836 release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
837 release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
838 return -EIO;
839 }
840
841 /* Check for bus mastering */
842 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
843 if (!(cmd & PCI_COMMAND_MASTER)) {
844 IVTV_DEBUG_INFO("Attempting to enable Bus Mastering\n");
845 pci_set_master(pdev);
846 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
847 if (!(cmd & PCI_COMMAND_MASTER)) {
848 IVTV_ERR("Bus Mastering is not enabled\n");
849 return -ENXIO;
850 }
851 }
852 IVTV_DEBUG_INFO("Bus Mastering Enabled.\n");
853
854 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
855
856 if (pci_latency < 64 && ivtv_pci_latency) {
857 IVTV_INFO("Unreasonably low latency timer, "
858 "setting to 64 (was %d)\n", pci_latency);
859 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
860 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
861 }
862 /* This config space value relates to DMA latencies. The
863 default value 0x8080 is too low however and will lead
864 to DMA errors. 0xffff is the max value which solves
865 these problems. */
866 pci_write_config_dword(pdev, 0x40, 0xffff);
867
868 IVTV_DEBUG_INFO("%d (rev %d) at %02x:%02x.%x, "
869 "irq: %d, latency: %d, memory: 0x%llx\n",
870 pdev->device, pdev->revision, pdev->bus->number,
871 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
872 pdev->irq, pci_latency, (u64)itv->base_addr);
873
874 return 0;
875}
876
877static void ivtv_load_and_init_modules(struct ivtv *itv)
878{
879 u32 hw = itv->card->hw_all;
880 unsigned i;
881
882 /* check which i2c devices are actually found */
883 for (i = 0; i < 32; i++) {
884 u32 device = 1 << i;
885
886 if (!(device & hw))
887 continue;
888 if (device == IVTV_HW_GPIO || device == IVTV_HW_TVEEPROM) {
889 /* GPIO and TVEEPROM do not use i2c probing */
890 itv->hw_flags |= device;
891 continue;
892 }
893 if (ivtv_i2c_register(itv, i) == 0)
894 itv->hw_flags |= device;
895 }
896
897 /* probe for legacy IR controllers that aren't in card definitions */
898 if ((itv->hw_flags & IVTV_HW_IR_ANY) == 0)
899 ivtv_i2c_new_ir_legacy(itv);
900
901 if (itv->card->hw_all & IVTV_HW_CX25840)
902 itv->sd_video = ivtv_find_hw(itv, IVTV_HW_CX25840);
903 else if (itv->card->hw_all & IVTV_HW_SAA717X)
904 itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA717X);
905 else if (itv->card->hw_all & IVTV_HW_SAA7114)
906 itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7114);
907 else
908 itv->sd_video = ivtv_find_hw(itv, IVTV_HW_SAA7115);
909 itv->sd_audio = ivtv_find_hw(itv, itv->card->hw_audio_ctrl);
910 itv->sd_muxer = ivtv_find_hw(itv, itv->card->hw_muxer);
911
912 hw = itv->hw_flags;
913
914 if (itv->card->type == IVTV_CARD_CX23416GYC) {
915 /* Several variations of this card exist, detect which card
916 type should be used. */
917 if ((hw & (IVTV_HW_UPD64031A | IVTV_HW_UPD6408X)) == 0)
918 itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGRYCS);
919 else if ((hw & IVTV_HW_UPD64031A) == 0)
920 itv->card = ivtv_get_card(IVTV_CARD_CX23416GYC_NOGR);
921 }
922 else if (itv->card->type == IVTV_CARD_GV_MVPRX ||
923 itv->card->type == IVTV_CARD_GV_MVPRX2E) {
924 /* The crystal frequency of GVMVPRX is 24.576MHz */
925 v4l2_subdev_call(itv->sd_video, video, s_crystal_freq,
926 SAA7115_FREQ_24_576_MHZ, SAA7115_FREQ_FL_UCGC);
927 }
928
929 if (hw & IVTV_HW_CX25840) {
930 itv->vbi.raw_decoder_line_size = 1444;
931 itv->vbi.raw_decoder_sav_odd_field = 0x20;
932 itv->vbi.raw_decoder_sav_even_field = 0x60;
933 itv->vbi.sliced_decoder_line_size = 272;
934 itv->vbi.sliced_decoder_sav_odd_field = 0xB0;
935 itv->vbi.sliced_decoder_sav_even_field = 0xF0;
936 }
937
938 if (hw & IVTV_HW_SAA711X) {
939 struct v4l2_dbg_chip_ident v;
940
941 /* determine the exact saa711x model */
942 itv->hw_flags &= ~IVTV_HW_SAA711X;
943
944 v.match.type = V4L2_CHIP_MATCH_I2C_DRIVER;
945 strlcpy(v.match.name, "saa7115", sizeof(v.match.name));
946 ivtv_call_hw(itv, IVTV_HW_SAA711X, core, g_chip_ident, &v);
947 if (v.ident == V4L2_IDENT_SAA7114) {
948 itv->hw_flags |= IVTV_HW_SAA7114;
949 /* VBI is not yet supported by the saa7114 driver. */
950 itv->v4l2_cap &= ~(V4L2_CAP_SLICED_VBI_CAPTURE|V4L2_CAP_VBI_CAPTURE);
951 } else {
952 itv->hw_flags |= IVTV_HW_SAA7115;
953 }
954 itv->vbi.raw_decoder_line_size = 1443;
955 itv->vbi.raw_decoder_sav_odd_field = 0x25;
956 itv->vbi.raw_decoder_sav_even_field = 0x62;
957 itv->vbi.sliced_decoder_line_size = 51;
958 itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
959 itv->vbi.sliced_decoder_sav_even_field = 0xEC;
960 }
961
962 if (hw & IVTV_HW_SAA717X) {
963 itv->vbi.raw_decoder_line_size = 1443;
964 itv->vbi.raw_decoder_sav_odd_field = 0x25;
965 itv->vbi.raw_decoder_sav_even_field = 0x62;
966 itv->vbi.sliced_decoder_line_size = 51;
967 itv->vbi.sliced_decoder_sav_odd_field = 0xAB;
968 itv->vbi.sliced_decoder_sav_even_field = 0xEC;
969 }
970}
971
972static int __devinit ivtv_probe(struct pci_dev *pdev,
973 const struct pci_device_id *pci_id)
974{
975 int retval = 0;
976 int vbi_buf_size;
977 struct ivtv *itv;
978
979 itv = kzalloc(sizeof(struct ivtv), GFP_ATOMIC);
980 if (itv == NULL)
981 return -ENOMEM;
982 itv->pdev = pdev;
983 itv->instance = v4l2_device_set_name(&itv->v4l2_dev, "ivtv",
984 &ivtv_instance);
985
986 retval = v4l2_device_register(&pdev->dev, &itv->v4l2_dev);
987 if (retval) {
988 kfree(itv);
989 return retval;
990 }
991 IVTV_INFO("Initializing card %d\n", itv->instance);
992
993 ivtv_process_options(itv);
994 if (itv->options.cardtype == -1) {
995 retval = -ENODEV;
996 goto err;
997 }
998 if (ivtv_init_struct1(itv)) {
999 retval = -ENOMEM;
1000 goto err;
1001 }
1002 retval = cx2341x_handler_init(&itv->cxhdl, 50);
1003 if (retval)
1004 goto err;
1005 itv->v4l2_dev.ctrl_handler = &itv->cxhdl.hdl;
1006 itv->cxhdl.ops = &ivtv_cxhdl_ops;
1007 itv->cxhdl.priv = itv;
1008 itv->cxhdl.func = ivtv_api_func;
1009
1010 IVTV_DEBUG_INFO("base addr: 0x%llx\n", (u64)itv->base_addr);
1011
1012 /* PCI Device Setup */
1013 retval = ivtv_setup_pci(itv, pdev, pci_id);
1014 if (retval == -EIO)
1015 goto free_worker;
1016 if (retval == -ENXIO)
1017 goto free_mem;
1018
1019 /* map io memory */
1020 IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
1021 (u64)itv->base_addr + IVTV_ENCODER_OFFSET, IVTV_ENCODER_SIZE);
1022 itv->enc_mem = ioremap_nocache(itv->base_addr + IVTV_ENCODER_OFFSET,
1023 IVTV_ENCODER_SIZE);
1024 if (!itv->enc_mem) {
1025 IVTV_ERR("ioremap failed. Can't get a window into CX23415/6 "
1026 "encoder memory\n");
1027 IVTV_ERR("Each capture card with a CX23415/6 needs 8 MB of "
1028 "vmalloc address space for this window\n");
1029 IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
1030 IVTV_ERR("Use the vmalloc= kernel command line option to set "
1031 "VmallocTotal to a larger value\n");
1032 retval = -ENOMEM;
1033 goto free_mem;
1034 }
1035
1036 if (itv->has_cx23415) {
1037 IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
1038 (u64)itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
1039 itv->dec_mem = ioremap_nocache(itv->base_addr + IVTV_DECODER_OFFSET,
1040 IVTV_DECODER_SIZE);
1041 if (!itv->dec_mem) {
1042 IVTV_ERR("ioremap failed. Can't get a window into "
1043 "CX23415 decoder memory\n");
1044 IVTV_ERR("Each capture card with a CX23415 needs 8 MB "
1045 "of vmalloc address space for this window\n");
1046 IVTV_ERR("Check the output of 'grep Vmalloc "
1047 "/proc/meminfo'\n");
1048 IVTV_ERR("Use the vmalloc= kernel command line option "
1049 "to set VmallocTotal to a larger value\n");
1050 retval = -ENOMEM;
1051 goto free_mem;
1052 }
1053 }
1054 else {
1055 itv->dec_mem = itv->enc_mem;
1056 }
1057
1058 /* map registers memory */
1059 IVTV_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n",
1060 (u64)itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1061 itv->reg_mem =
1062 ioremap_nocache(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1063 if (!itv->reg_mem) {
1064 IVTV_ERR("ioremap failed. Can't get a window into CX23415/6 "
1065 "register space\n");
1066 IVTV_ERR("Each capture card with a CX23415/6 needs 64 kB of "
1067 "vmalloc address space for this window\n");
1068 IVTV_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n");
1069 IVTV_ERR("Use the vmalloc= kernel command line option to set "
1070 "VmallocTotal to a larger value\n");
1071 retval = -ENOMEM;
1072 goto free_io;
1073 }
1074
1075 retval = ivtv_gpio_init(itv);
1076 if (retval)
1077 goto free_io;
1078
1079 /* active i2c */
1080 IVTV_DEBUG_INFO("activating i2c...\n");
1081 if (init_ivtv_i2c(itv)) {
1082 IVTV_ERR("Could not initialize i2c\n");
1083 goto free_io;
1084 }
1085
1086 if (itv->card->hw_all & IVTV_HW_TVEEPROM) {
1087 /* Based on the model number the cardtype may be changed.
1088 The PCI IDs are not always reliable. */
1089 ivtv_process_eeprom(itv);
1090 }
1091 if (itv->card->comment)
1092 IVTV_INFO("%s", itv->card->comment);
1093 if (itv->card->v4l2_capabilities == 0) {
1094 /* card was detected but is not supported */
1095 retval = -ENODEV;
1096 goto free_i2c;
1097 }
1098
1099 if (itv->std == 0) {
1100 itv->std = V4L2_STD_NTSC_M;
1101 }
1102
1103 if (itv->options.tuner == -1) {
1104 int i;
1105
1106 for (i = 0; i < IVTV_CARD_MAX_TUNERS; i++) {
1107 if ((itv->std & itv->card->tuners[i].std) == 0)
1108 continue;
1109 itv->options.tuner = itv->card->tuners[i].tuner;
1110 break;
1111 }
1112 }
1113 /* if no tuner was found, then pick the first tuner in the card list */
1114 if (itv->options.tuner == -1 && itv->card->tuners[0].std) {
1115 itv->std = itv->card->tuners[0].std;
1116 if (itv->std & V4L2_STD_PAL)
1117 itv->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H;
1118 else if (itv->std & V4L2_STD_NTSC)
1119 itv->std = V4L2_STD_NTSC_M;
1120 else if (itv->std & V4L2_STD_SECAM)
1121 itv->std = V4L2_STD_SECAM_L;
1122 itv->options.tuner = itv->card->tuners[0].tuner;
1123 }
1124 if (itv->options.radio == -1)
1125 itv->options.radio = (itv->card->radio_input.audio_type != 0);
1126
1127 /* The card is now fully identified, continue with card-specific
1128 initialization. */
1129 ivtv_init_struct2(itv);
1130
1131 ivtv_load_and_init_modules(itv);
1132
1133 if (itv->std & V4L2_STD_525_60) {
1134 itv->is_60hz = 1;
1135 itv->is_out_60hz = 1;
1136 } else {
1137 itv->is_50hz = 1;
1138 itv->is_out_50hz = 1;
1139 }
1140
1141 itv->yuv_info.osd_full_w = 720;
1142 itv->yuv_info.osd_full_h = itv->is_out_50hz ? 576 : 480;
1143 itv->yuv_info.v4l2_src_w = itv->yuv_info.osd_full_w;
1144 itv->yuv_info.v4l2_src_h = itv->yuv_info.osd_full_h;
1145
1146 cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1147
1148 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_MPG] = 0x08000;
1149 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_PCM] = 0x01200;
1150 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_MPG] = 0x10000;
1151 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_YUV] = 0x10000;
1152 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_YUV] = 0x08000;
1153
1154 /* Setup VBI Raw Size. Should be big enough to hold PAL.
1155 It is possible to switch between PAL and NTSC, so we need to
1156 take the largest size here. */
1157 /* 1456 is multiple of 16, real size = 1444 */
1158 itv->vbi.raw_size = 1456;
1159 /* We use a buffer size of 1/2 of the total size needed for a
1160 frame. This is actually very useful, since we now receive
1161 a field at a time and that makes 'compressing' the raw data
1162 down to size by stripping off the SAV codes a lot easier.
1163 Note: having two different buffer sizes prevents standard
1164 switching on the fly. We need to find a better solution... */
1165 vbi_buf_size = itv->vbi.raw_size * (itv->is_60hz ? 24 : 36) / 2;
1166 itv->stream_buf_size[IVTV_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
1167 itv->stream_buf_size[IVTV_DEC_STREAM_TYPE_VBI] = sizeof(struct v4l2_sliced_vbi_data) * 36;
1168
1169 if (itv->options.radio > 0)
1170 itv->v4l2_cap |= V4L2_CAP_RADIO;
1171
1172 if (itv->options.tuner > -1) {
1173 struct tuner_setup setup;
1174
1175 setup.addr = ADDR_UNSET;
1176 setup.type = itv->options.tuner;
1177 setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */
1178 if (itv->options.radio > 0)
1179 setup.mode_mask |= T_RADIO;
1180 setup.tuner_callback = (setup.type == TUNER_XC2028) ?
1181 ivtv_reset_tuner_gpio : NULL;
1182 ivtv_call_all(itv, tuner, s_type_addr, &setup);
1183 if (setup.type == TUNER_XC2028) {
1184 static struct xc2028_ctrl ctrl = {
1185 .fname = XC2028_DEFAULT_FIRMWARE,
1186 .max_len = 64,
1187 };
1188 struct v4l2_priv_tun_config cfg = {
1189 .tuner = itv->options.tuner,
1190 .priv = &ctrl,
1191 };
1192 ivtv_call_all(itv, tuner, s_config, &cfg);
1193 }
1194 }
1195
1196 /* The tuner is fixed to the standard. The other inputs (e.g. S-Video)
1197 are not. */
1198 itv->tuner_std = itv->std;
1199
1200 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1201 struct v4l2_ctrl_handler *hdl = itv->v4l2_dev.ctrl_handler;
1202
1203 itv->ctrl_pts = v4l2_ctrl_new_std(hdl, &ivtv_hdl_out_ops,
1204 V4L2_CID_MPEG_VIDEO_DEC_PTS, 0, 0, 0, 0);
1205 itv->ctrl_frame = v4l2_ctrl_new_std(hdl, &ivtv_hdl_out_ops,
1206 V4L2_CID_MPEG_VIDEO_DEC_FRAME, 0, 0, 0, 0);
1207 /* Note: V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO is not supported,
1208 mask that menu item. */
1209 itv->ctrl_audio_playback =
1210 v4l2_ctrl_new_std_menu(hdl, &ivtv_hdl_out_ops,
1211 V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK,
1212 V4L2_MPEG_AUDIO_DEC_PLAYBACK_SWAPPED_STEREO,
1213 1 << V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO,
1214 V4L2_MPEG_AUDIO_DEC_PLAYBACK_STEREO);
1215 itv->ctrl_audio_multilingual_playback =
1216 v4l2_ctrl_new_std_menu(hdl, &ivtv_hdl_out_ops,
1217 V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK,
1218 V4L2_MPEG_AUDIO_DEC_PLAYBACK_SWAPPED_STEREO,
1219 1 << V4L2_MPEG_AUDIO_DEC_PLAYBACK_AUTO,
1220 V4L2_MPEG_AUDIO_DEC_PLAYBACK_LEFT);
1221 if (hdl->error) {
1222 retval = hdl->error;
1223 goto free_i2c;
1224 }
1225 v4l2_ctrl_cluster(2, &itv->ctrl_pts);
1226 v4l2_ctrl_cluster(2, &itv->ctrl_audio_playback);
1227 ivtv_call_all(itv, video, s_std_output, itv->std);
1228 /* Turn off the output signal. The mpeg decoder is not yet
1229 active so without this you would get a green image until the
1230 mpeg decoder becomes active. */
1231 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
1232 }
1233
1234 /* clear interrupt mask, effectively disabling interrupts */
1235 ivtv_set_irq_mask(itv, 0xffffffff);
1236
1237 /* Register IRQ */
1238 retval = request_irq(itv->pdev->irq, ivtv_irq_handler,
1239 IRQF_SHARED | IRQF_DISABLED, itv->v4l2_dev.name, (void *)itv);
1240 if (retval) {
1241 IVTV_ERR("Failed to register irq %d\n", retval);
1242 goto free_i2c;
1243 }
1244
1245 retval = ivtv_streams_setup(itv);
1246 if (retval) {
1247 IVTV_ERR("Error %d setting up streams\n", retval);
1248 goto free_irq;
1249 }
1250 retval = ivtv_streams_register(itv);
1251 if (retval) {
1252 IVTV_ERR("Error %d registering devices\n", retval);
1253 goto free_streams;
1254 }
1255 IVTV_INFO("Initialized card: %s\n", itv->card_name);
1256 return 0;
1257
1258free_streams:
1259 ivtv_streams_cleanup(itv, 1);
1260free_irq:
1261 free_irq(itv->pdev->irq, (void *)itv);
1262free_i2c:
1263 v4l2_ctrl_handler_free(&itv->cxhdl.hdl);
1264 exit_ivtv_i2c(itv);
1265free_io:
1266 ivtv_iounmap(itv);
1267free_mem:
1268 release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
1269 release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1270 if (itv->has_cx23415)
1271 release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
1272free_worker:
1273 kthread_stop(itv->irq_worker_task);
1274err:
1275 if (retval == 0)
1276 retval = -ENODEV;
1277 IVTV_ERR("Error %d on initialization\n", retval);
1278
1279 v4l2_device_unregister(&itv->v4l2_dev);
1280 kfree(itv);
1281 return retval;
1282}
1283
1284int ivtv_init_on_first_open(struct ivtv *itv)
1285{
1286 struct v4l2_frequency vf;
1287 /* Needed to call ioctls later */
1288 struct ivtv_open_id fh;
1289 int fw_retry_count = 3;
1290 int video_input;
1291
1292 fh.itv = itv;
1293
1294 if (test_bit(IVTV_F_I_FAILED, &itv->i_flags))
1295 return -ENXIO;
1296
1297 if (test_and_set_bit(IVTV_F_I_INITED, &itv->i_flags))
1298 return 0;
1299
1300 while (--fw_retry_count > 0) {
1301 /* load firmware */
1302 if (ivtv_firmware_init(itv) == 0)
1303 break;
1304 if (fw_retry_count > 1)
1305 IVTV_WARN("Retry loading firmware\n");
1306 }
1307
1308 if (fw_retry_count == 0) {
1309 set_bit(IVTV_F_I_FAILED, &itv->i_flags);
1310 return -ENXIO;
1311 }
1312
1313 /* Try and get firmware versions */
1314 IVTV_DEBUG_INFO("Getting firmware version..\n");
1315 ivtv_firmware_versions(itv);
1316
1317 if (itv->card->hw_all & IVTV_HW_CX25840)
1318 v4l2_subdev_call(itv->sd_video, core, load_fw);
1319
1320 vf.tuner = 0;
1321 vf.type = V4L2_TUNER_ANALOG_TV;
1322 vf.frequency = 6400; /* the tuner 'baseline' frequency */
1323
1324 /* Set initial frequency. For PAL/SECAM broadcasts no
1325 'default' channel exists AFAIK. */
1326 if (itv->std == V4L2_STD_NTSC_M_JP) {
1327 vf.frequency = 1460; /* ch. 1 91250*16/1000 */
1328 }
1329 else if (itv->std & V4L2_STD_NTSC_M) {
1330 vf.frequency = 1076; /* ch. 4 67250*16/1000 */
1331 }
1332
1333 video_input = itv->active_input;
1334 itv->active_input++; /* Force update of input */
1335 ivtv_s_input(NULL, &fh, video_input);
1336
1337 /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code
1338 in one place. */
1339 itv->std++; /* Force full standard initialization */
1340 itv->std_out = itv->std;
1341 ivtv_s_frequency(NULL, &fh, &vf);
1342
1343 if (itv->card->v4l2_capabilities & V4L2_CAP_VIDEO_OUTPUT) {
1344 /* Turn on the TV-out: ivtv_init_mpeg_decoder() initializes
1345 the mpeg decoder so now the saa7127 receives a proper
1346 signal. */
1347 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);
1348 ivtv_init_mpeg_decoder(itv);
1349 }
1350
1351 /* On a cx23416 this seems to be able to enable DMA to the chip? */
1352 if (!itv->has_cx23415)
1353 write_reg_sync(0x03, IVTV_REG_DMACONTROL);
1354
1355 ivtv_s_std_enc(itv, &itv->tuner_std);
1356
1357 /* Default interrupts enabled. For the PVR350 this includes the
1358 decoder VSYNC interrupt, which is always on. It is not only used
1359 during decoding but also by the OSD.
1360 Some old PVR250 cards had a cx23415, so testing for that is too
1361 general. Instead test if the card has video output capability. */
1362 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1363 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT | IVTV_IRQ_DEC_VSYNC);
1364 ivtv_set_osd_alpha(itv);
1365 ivtv_s_std_dec(itv, &itv->tuner_std);
1366 } else {
1367 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_INIT);
1368 }
1369
1370 /* Setup initial controls */
1371 cx2341x_handler_setup(&itv->cxhdl);
1372 return 0;
1373}
1374
1375static void ivtv_remove(struct pci_dev *pdev)
1376{
1377 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
1378 struct ivtv *itv = to_ivtv(v4l2_dev);
1379 int i;
1380
1381 IVTV_DEBUG_INFO("Removing card\n");
1382
1383 if (test_bit(IVTV_F_I_INITED, &itv->i_flags)) {
1384 /* Stop all captures */
1385 IVTV_DEBUG_INFO("Stopping all streams\n");
1386 if (atomic_read(&itv->capturing) > 0)
1387 ivtv_stop_all_captures(itv);
1388
1389 /* Stop all decoding */
1390 IVTV_DEBUG_INFO("Stopping decoding\n");
1391
1392 /* Turn off the TV-out */
1393 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1394 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
1395 if (atomic_read(&itv->decoding) > 0) {
1396 int type;
1397
1398 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
1399 type = IVTV_DEC_STREAM_TYPE_YUV;
1400 else
1401 type = IVTV_DEC_STREAM_TYPE_MPG;
1402 ivtv_stop_v4l2_decode_stream(&itv->streams[type],
1403 V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY, 0);
1404 }
1405 ivtv_halt_firmware(itv);
1406 }
1407
1408 /* Interrupts */
1409 ivtv_set_irq_mask(itv, 0xffffffff);
1410 del_timer_sync(&itv->dma_timer);
1411
1412 /* Kill irq worker */
1413 flush_kthread_worker(&itv->irq_worker);
1414 kthread_stop(itv->irq_worker_task);
1415
1416 ivtv_streams_cleanup(itv, 1);
1417 ivtv_udma_free(itv);
1418
1419 v4l2_ctrl_handler_free(&itv->cxhdl.hdl);
1420
1421 exit_ivtv_i2c(itv);
1422
1423 free_irq(itv->pdev->irq, (void *)itv);
1424 ivtv_iounmap(itv);
1425
1426 release_mem_region(itv->base_addr, IVTV_ENCODER_SIZE);
1427 release_mem_region(itv->base_addr + IVTV_REG_OFFSET, IVTV_REG_SIZE);
1428 if (itv->has_cx23415)
1429 release_mem_region(itv->base_addr + IVTV_DECODER_OFFSET, IVTV_DECODER_SIZE);
1430
1431 pci_disable_device(itv->pdev);
1432 for (i = 0; i < IVTV_VBI_FRAMES; i++)
1433 kfree(itv->vbi.sliced_mpeg_data[i]);
1434
1435 printk(KERN_INFO "ivtv: Removed %s\n", itv->card_name);
1436
1437 v4l2_device_unregister(&itv->v4l2_dev);
1438 kfree(itv);
1439}
1440
1441/* define a pci_driver for card detection */
1442static struct pci_driver ivtv_pci_driver = {
1443 .name = "ivtv",
1444 .id_table = ivtv_pci_tbl,
1445 .probe = ivtv_probe,
1446 .remove = ivtv_remove,
1447};
1448
1449static int __init module_start(void)
1450{
1451 printk(KERN_INFO "ivtv: Start initialization, version %s\n", IVTV_VERSION);
1452
1453 /* Validate parameters */
1454 if (ivtv_first_minor < 0 || ivtv_first_minor >= IVTV_MAX_CARDS) {
1455 printk(KERN_ERR "ivtv: Exiting, ivtv_first_minor must be between 0 and %d\n",
1456 IVTV_MAX_CARDS - 1);
1457 return -1;
1458 }
1459
1460 if (ivtv_debug < 0 || ivtv_debug > 2047) {
1461 ivtv_debug = 0;
1462 printk(KERN_INFO "ivtv: Debug value must be >= 0 and <= 2047\n");
1463 }
1464
1465 if (pci_register_driver(&ivtv_pci_driver)) {
1466 printk(KERN_ERR "ivtv: Error detecting PCI card\n");
1467 return -ENODEV;
1468 }
1469 printk(KERN_INFO "ivtv: End initialization\n");
1470 return 0;
1471}
1472
1473static void __exit module_cleanup(void)
1474{
1475 pci_unregister_driver(&ivtv_pci_driver);
1476}
1477
1478/* Note: These symbols are exported because they are used by the ivtvfb
1479 framebuffer module and an infrared module for the IR-blaster. */
1480EXPORT_SYMBOL(ivtv_set_irq_mask);
1481EXPORT_SYMBOL(ivtv_api);
1482EXPORT_SYMBOL(ivtv_vapi);
1483EXPORT_SYMBOL(ivtv_vapi_result);
1484EXPORT_SYMBOL(ivtv_clear_irq_mask);
1485EXPORT_SYMBOL(ivtv_debug);
1486#ifdef CONFIG_VIDEO_ADV_DEBUG
1487EXPORT_SYMBOL(ivtv_fw_debug);
1488#endif
1489EXPORT_SYMBOL(ivtv_reset_ir_gpio);
1490EXPORT_SYMBOL(ivtv_udma_setup);
1491EXPORT_SYMBOL(ivtv_udma_unmap);
1492EXPORT_SYMBOL(ivtv_udma_alloc);
1493EXPORT_SYMBOL(ivtv_udma_prepare);
1494EXPORT_SYMBOL(ivtv_init_on_first_open);
1495EXPORT_SYMBOL(ivtv_firmware_check);
1496
1497module_init(module_start);
1498module_exit(module_cleanup);
diff --git a/drivers/media/pci/ivtv/ivtv-driver.h b/drivers/media/pci/ivtv/ivtv-driver.h
new file mode 100644
index 000000000000..a7e00f8938f8
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-driver.h
@@ -0,0 +1,839 @@
1/*
2 ivtv driver internal defines and structures
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#ifndef IVTV_DRIVER_H
23#define IVTV_DRIVER_H
24
25/* Internal header for ivtv project:
26 * Driver for the cx23415/6 chip.
27 * Author: Kevin Thayer (nufan_wfk at yahoo.com)
28 * License: GPL
29 * http://www.ivtvdriver.org
30 *
31 * -----
32 * MPG600/MPG160 support by T.Adachi <tadachi@tadachi-net.com>
33 * and Takeru KOMORIYA<komoriya@paken.org>
34 *
35 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
36 * using information provided by Jiun-Kuei Jung @ AVerMedia.
37 */
38
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/sched.h>
43#include <linux/fs.h>
44#include <linux/pci.h>
45#include <linux/interrupt.h>
46#include <linux/spinlock.h>
47#include <linux/i2c.h>
48#include <linux/i2c-algo-bit.h>
49#include <linux/list.h>
50#include <linux/unistd.h>
51#include <linux/pagemap.h>
52#include <linux/scatterlist.h>
53#include <linux/kthread.h>
54#include <linux/mutex.h>
55#include <linux/slab.h>
56#include <asm/uaccess.h>
57#include <asm/byteorder.h>
58
59#include <linux/dvb/video.h>
60#include <linux/dvb/audio.h>
61#include <media/v4l2-common.h>
62#include <media/v4l2-ioctl.h>
63#include <media/v4l2-ctrls.h>
64#include <media/v4l2-device.h>
65#include <media/v4l2-fh.h>
66#include <media/tuner.h>
67#include <media/cx2341x.h>
68#include <media/ir-kbd-i2c.h>
69
70#include <linux/ivtv.h>
71
72/* Memory layout */
73#define IVTV_ENCODER_OFFSET 0x00000000
74#define IVTV_ENCODER_SIZE 0x00800000 /* Total size is 0x01000000, but only first half is used */
75#define IVTV_DECODER_OFFSET 0x01000000
76#define IVTV_DECODER_SIZE 0x00800000 /* Total size is 0x01000000, but only first half is used */
77#define IVTV_REG_OFFSET 0x02000000
78#define IVTV_REG_SIZE 0x00010000
79
80/* Maximum ivtv driver instances. Some people have a huge number of
81 capture cards, so set this to a high value. */
82#define IVTV_MAX_CARDS 32
83
84#define IVTV_ENC_STREAM_TYPE_MPG 0
85#define IVTV_ENC_STREAM_TYPE_YUV 1
86#define IVTV_ENC_STREAM_TYPE_VBI 2
87#define IVTV_ENC_STREAM_TYPE_PCM 3
88#define IVTV_ENC_STREAM_TYPE_RAD 4
89#define IVTV_DEC_STREAM_TYPE_MPG 5
90#define IVTV_DEC_STREAM_TYPE_VBI 6
91#define IVTV_DEC_STREAM_TYPE_VOUT 7
92#define IVTV_DEC_STREAM_TYPE_YUV 8
93#define IVTV_MAX_STREAMS 9
94
95#define IVTV_DMA_SG_OSD_ENT (2883584/PAGE_SIZE) /* sg entities */
96
97/* DMA Registers */
98#define IVTV_REG_DMAXFER (0x0000)
99#define IVTV_REG_DMASTATUS (0x0004)
100#define IVTV_REG_DECDMAADDR (0x0008)
101#define IVTV_REG_ENCDMAADDR (0x000c)
102#define IVTV_REG_DMACONTROL (0x0010)
103#define IVTV_REG_IRQSTATUS (0x0040)
104#define IVTV_REG_IRQMASK (0x0048)
105
106/* Setup Registers */
107#define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
108#define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
109#define IVTV_REG_DEC_SDRAM_REFRESH (0x08F8)
110#define IVTV_REG_DEC_SDRAM_PRECHARGE (0x08FC)
111#define IVTV_REG_VDM (0x2800)
112#define IVTV_REG_AO (0x2D00)
113#define IVTV_REG_BYTEFLUSH (0x2D24)
114#define IVTV_REG_SPU (0x9050)
115#define IVTV_REG_HW_BLOCKS (0x9054)
116#define IVTV_REG_VPU (0x9058)
117#define IVTV_REG_APU (0xA064)
118
119/* Other registers */
120#define IVTV_REG_DEC_LINE_FIELD (0x28C0)
121
122/* debugging */
123extern int ivtv_debug;
124#ifdef CONFIG_VIDEO_ADV_DEBUG
125extern int ivtv_fw_debug;
126#endif
127
128#define IVTV_DBGFLG_WARN (1 << 0)
129#define IVTV_DBGFLG_INFO (1 << 1)
130#define IVTV_DBGFLG_MB (1 << 2)
131#define IVTV_DBGFLG_IOCTL (1 << 3)
132#define IVTV_DBGFLG_FILE (1 << 4)
133#define IVTV_DBGFLG_DMA (1 << 5)
134#define IVTV_DBGFLG_IRQ (1 << 6)
135#define IVTV_DBGFLG_DEC (1 << 7)
136#define IVTV_DBGFLG_YUV (1 << 8)
137#define IVTV_DBGFLG_I2C (1 << 9)
138/* Flag to turn on high volume debugging */
139#define IVTV_DBGFLG_HIGHVOL (1 << 10)
140
141#define IVTV_DEBUG(x, type, fmt, args...) \
142 do { \
143 if ((x) & ivtv_debug) \
144 v4l2_info(&itv->v4l2_dev, " " type ": " fmt , ##args); \
145 } while (0)
146#define IVTV_DEBUG_WARN(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_WARN, "warn", fmt , ## args)
147#define IVTV_DEBUG_INFO(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_INFO, "info", fmt , ## args)
148#define IVTV_DEBUG_MB(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_MB, "mb", fmt , ## args)
149#define IVTV_DEBUG_DMA(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_DMA, "dma", fmt , ## args)
150#define IVTV_DEBUG_IOCTL(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_IOCTL, "ioctl", fmt , ## args)
151#define IVTV_DEBUG_FILE(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_FILE, "file", fmt , ## args)
152#define IVTV_DEBUG_I2C(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_I2C, "i2c", fmt , ## args)
153#define IVTV_DEBUG_IRQ(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_IRQ, "irq", fmt , ## args)
154#define IVTV_DEBUG_DEC(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_DEC, "dec", fmt , ## args)
155#define IVTV_DEBUG_YUV(fmt, args...) IVTV_DEBUG(IVTV_DBGFLG_YUV, "yuv", fmt , ## args)
156
157#define IVTV_DEBUG_HIGH_VOL(x, type, fmt, args...) \
158 do { \
159 if (((x) & ivtv_debug) && (ivtv_debug & IVTV_DBGFLG_HIGHVOL)) \
160 v4l2_info(&itv->v4l2_dev, " " type ": " fmt , ##args); \
161 } while (0)
162#define IVTV_DEBUG_HI_WARN(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_WARN, "warn", fmt , ## args)
163#define IVTV_DEBUG_HI_INFO(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_INFO, "info", fmt , ## args)
164#define IVTV_DEBUG_HI_MB(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_MB, "mb", fmt , ## args)
165#define IVTV_DEBUG_HI_DMA(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_DMA, "dma", fmt , ## args)
166#define IVTV_DEBUG_HI_IOCTL(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_IOCTL, "ioctl", fmt , ## args)
167#define IVTV_DEBUG_HI_FILE(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_FILE, "file", fmt , ## args)
168#define IVTV_DEBUG_HI_I2C(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_I2C, "i2c", fmt , ## args)
169#define IVTV_DEBUG_HI_IRQ(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_IRQ, "irq", fmt , ## args)
170#define IVTV_DEBUG_HI_DEC(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_DEC, "dec", fmt , ## args)
171#define IVTV_DEBUG_HI_YUV(fmt, args...) IVTV_DEBUG_HIGH_VOL(IVTV_DBGFLG_YUV, "yuv", fmt , ## args)
172
173/* Standard kernel messages */
174#define IVTV_ERR(fmt, args...) v4l2_err(&itv->v4l2_dev, fmt , ## args)
175#define IVTV_WARN(fmt, args...) v4l2_warn(&itv->v4l2_dev, fmt , ## args)
176#define IVTV_INFO(fmt, args...) v4l2_info(&itv->v4l2_dev, fmt , ## args)
177
178/* output modes (cx23415 only) */
179#define OUT_NONE 0
180#define OUT_MPG 1
181#define OUT_YUV 2
182#define OUT_UDMA_YUV 3
183#define OUT_PASSTHROUGH 4
184
185#define IVTV_MAX_PGM_INDEX (400)
186
187/* Default I2C SCL period in microseconds */
188#define IVTV_DEFAULT_I2C_CLOCK_PERIOD 20
189
190struct ivtv_options {
191 int kilobytes[IVTV_MAX_STREAMS]; /* size in kilobytes of each stream */
192 int cardtype; /* force card type on load */
193 int tuner; /* set tuner on load */
194 int radio; /* enable/disable radio */
195 int newi2c; /* new I2C algorithm */
196 int i2c_clock_period; /* period of SCL for I2C bus */
197};
198
199/* ivtv-specific mailbox template */
200struct ivtv_mailbox {
201 u32 flags;
202 u32 cmd;
203 u32 retval;
204 u32 timeout;
205 u32 data[CX2341X_MBOX_MAX_DATA];
206};
207
208struct ivtv_api_cache {
209 unsigned long last_jiffies; /* when last command was issued */
210 u32 data[CX2341X_MBOX_MAX_DATA]; /* last sent api data */
211};
212
213struct ivtv_mailbox_data {
214 volatile struct ivtv_mailbox __iomem *mbox;
215 /* Bits 0-2 are for the encoder mailboxes, 0-1 are for the decoder mailboxes.
216 If the bit is set, then the corresponding mailbox is in use by the driver. */
217 unsigned long busy;
218 u8 max_mbox;
219};
220
221/* per-buffer bit flags */
222#define IVTV_F_B_NEED_BUF_SWAP (1 << 0) /* this buffer should be byte swapped */
223
224/* per-stream, s_flags */
225#define IVTV_F_S_DMA_PENDING 0 /* this stream has pending DMA */
226#define IVTV_F_S_DMA_HAS_VBI 1 /* the current DMA request also requests VBI data */
227#define IVTV_F_S_NEEDS_DATA 2 /* this decoding stream needs more data */
228
229#define IVTV_F_S_CLAIMED 3 /* this stream is claimed */
230#define IVTV_F_S_STREAMING 4 /* the fw is decoding/encoding this stream */
231#define IVTV_F_S_INTERNAL_USE 5 /* this stream is used internally (sliced VBI processing) */
232#define IVTV_F_S_PASSTHROUGH 6 /* this stream is in passthrough mode */
233#define IVTV_F_S_STREAMOFF 7 /* signal end of stream EOS */
234#define IVTV_F_S_APPL_IO 8 /* this stream is used read/written by an application */
235
236#define IVTV_F_S_PIO_PENDING 9 /* this stream has pending PIO */
237#define IVTV_F_S_PIO_HAS_VBI 1 /* the current PIO request also requests VBI data */
238
239/* per-ivtv, i_flags */
240#define IVTV_F_I_DMA 0 /* DMA in progress */
241#define IVTV_F_I_UDMA 1 /* UDMA in progress */
242#define IVTV_F_I_UDMA_PENDING 2 /* UDMA pending */
243#define IVTV_F_I_SPEED_CHANGE 3 /* a speed change is in progress */
244#define IVTV_F_I_EOS 4 /* end of encoder stream reached */
245#define IVTV_F_I_RADIO_USER 5 /* the radio tuner is selected */
246#define IVTV_F_I_DIG_RST 6 /* reset digitizer */
247#define IVTV_F_I_DEC_YUV 7 /* YUV instead of MPG is being decoded */
248#define IVTV_F_I_UPDATE_CC 9 /* CC should be updated */
249#define IVTV_F_I_UPDATE_WSS 10 /* WSS should be updated */
250#define IVTV_F_I_UPDATE_VPS 11 /* VPS should be updated */
251#define IVTV_F_I_DECODING_YUV 12 /* this stream is YUV frame decoding */
252#define IVTV_F_I_ENC_PAUSED 13 /* the encoder is paused */
253#define IVTV_F_I_VALID_DEC_TIMINGS 14 /* last_dec_timing is valid */
254#define IVTV_F_I_HAVE_WORK 15 /* used in the interrupt handler: there is work to be done */
255#define IVTV_F_I_WORK_HANDLER_VBI 16 /* there is work to be done for VBI */
256#define IVTV_F_I_WORK_HANDLER_YUV 17 /* there is work to be done for YUV */
257#define IVTV_F_I_WORK_HANDLER_PIO 18 /* there is work to be done for PIO */
258#define IVTV_F_I_PIO 19 /* PIO in progress */
259#define IVTV_F_I_DEC_PAUSED 20 /* the decoder is paused */
260#define IVTV_F_I_INITED 21 /* set after first open */
261#define IVTV_F_I_FAILED 22 /* set if first open failed */
262
263/* Event notifications */
264#define IVTV_F_I_EV_DEC_STOPPED 28 /* decoder stopped event */
265#define IVTV_F_I_EV_VSYNC 29 /* VSYNC event */
266#define IVTV_F_I_EV_VSYNC_FIELD 30 /* VSYNC event field (0 = first, 1 = second field) */
267#define IVTV_F_I_EV_VSYNC_ENABLED 31 /* VSYNC event enabled */
268
269/* Scatter-Gather array element, used in DMA transfers */
270struct ivtv_sg_element {
271 __le32 src;
272 __le32 dst;
273 __le32 size;
274};
275
276struct ivtv_sg_host_element {
277 u32 src;
278 u32 dst;
279 u32 size;
280};
281
282struct ivtv_user_dma {
283 struct mutex lock;
284 int page_count;
285 struct page *map[IVTV_DMA_SG_OSD_ENT];
286 /* Needed when dealing with highmem userspace buffers */
287 struct page *bouncemap[IVTV_DMA_SG_OSD_ENT];
288
289 /* Base Dev SG Array for cx23415/6 */
290 struct ivtv_sg_element SGarray[IVTV_DMA_SG_OSD_ENT];
291 dma_addr_t SG_handle;
292 int SG_length;
293
294 /* SG List of Buffers */
295 struct scatterlist SGlist[IVTV_DMA_SG_OSD_ENT];
296};
297
298struct ivtv_dma_page_info {
299 unsigned long uaddr;
300 unsigned long first;
301 unsigned long last;
302 unsigned int offset;
303 unsigned int tail;
304 int page_count;
305};
306
307struct ivtv_buffer {
308 struct list_head list;
309 dma_addr_t dma_handle;
310 unsigned short b_flags;
311 unsigned short dma_xfer_cnt;
312 char *buf;
313 u32 bytesused;
314 u32 readpos;
315};
316
317struct ivtv_queue {
318 struct list_head list; /* the list of buffers in this queue */
319 u32 buffers; /* number of buffers in this queue */
320 u32 length; /* total number of bytes of available buffer space */
321 u32 bytesused; /* total number of bytes used in this queue */
322};
323
324struct ivtv; /* forward reference */
325
326struct ivtv_stream {
327 /* These first four fields are always set, even if the stream
328 is not actually created. */
329 struct video_device *vdev; /* NULL when stream not created */
330 struct ivtv *itv; /* for ease of use */
331 const char *name; /* name of the stream */
332 int type; /* stream type */
333 u32 caps; /* V4L2 capabilities */
334
335 struct v4l2_fh *fh; /* pointer to the streaming filehandle */
336 spinlock_t qlock; /* locks access to the queues */
337 unsigned long s_flags; /* status flags, see above */
338 int dma; /* can be PCI_DMA_TODEVICE, PCI_DMA_FROMDEVICE or PCI_DMA_NONE */
339 u32 pending_offset;
340 u32 pending_backup;
341 u64 pending_pts;
342
343 u32 dma_offset;
344 u32 dma_backup;
345 u64 dma_pts;
346
347 int subtype;
348 wait_queue_head_t waitq;
349 u32 dma_last_offset;
350
351 /* Buffer Stats */
352 u32 buffers;
353 u32 buf_size;
354 u32 buffers_stolen;
355
356 /* Buffer Queues */
357 struct ivtv_queue q_free; /* free buffers */
358 struct ivtv_queue q_full; /* full buffers */
359 struct ivtv_queue q_io; /* waiting for I/O */
360 struct ivtv_queue q_dma; /* waiting for DMA */
361 struct ivtv_queue q_predma; /* waiting for DMA */
362
363 /* DMA xfer counter, buffers belonging to the same DMA
364 xfer will have the same dma_xfer_cnt. */
365 u16 dma_xfer_cnt;
366
367 /* Base Dev SG Array for cx23415/6 */
368 struct ivtv_sg_host_element *sg_pending;
369 struct ivtv_sg_host_element *sg_processing;
370 struct ivtv_sg_element *sg_dma;
371 dma_addr_t sg_handle;
372 int sg_pending_size;
373 int sg_processing_size;
374 int sg_processed;
375
376 /* SG List of Buffers */
377 struct scatterlist *SGlist;
378};
379
380struct ivtv_open_id {
381 struct v4l2_fh fh;
382 int type; /* stream type */
383 int yuv_frames; /* 1: started OUT_UDMA_YUV output mode */
384 struct ivtv *itv;
385};
386
387static inline struct ivtv_open_id *fh2id(struct v4l2_fh *fh)
388{
389 return container_of(fh, struct ivtv_open_id, fh);
390}
391
392struct yuv_frame_info
393{
394 u32 update;
395 s32 src_x;
396 s32 src_y;
397 u32 src_w;
398 u32 src_h;
399 s32 dst_x;
400 s32 dst_y;
401 u32 dst_w;
402 u32 dst_h;
403 s32 pan_x;
404 s32 pan_y;
405 u32 vis_w;
406 u32 vis_h;
407 u32 interlaced_y;
408 u32 interlaced_uv;
409 s32 tru_x;
410 u32 tru_w;
411 u32 tru_h;
412 u32 offset_y;
413 s32 lace_mode;
414 u32 sync_field;
415 u32 delay;
416 u32 interlaced;
417};
418
419#define IVTV_YUV_MODE_INTERLACED 0x00
420#define IVTV_YUV_MODE_PROGRESSIVE 0x01
421#define IVTV_YUV_MODE_AUTO 0x02
422#define IVTV_YUV_MODE_MASK 0x03
423
424#define IVTV_YUV_SYNC_EVEN 0x00
425#define IVTV_YUV_SYNC_ODD 0x04
426#define IVTV_YUV_SYNC_MASK 0x04
427
428#define IVTV_YUV_BUFFERS 8
429
430struct yuv_playback_info
431{
432 u32 reg_2834;
433 u32 reg_2838;
434 u32 reg_283c;
435 u32 reg_2840;
436 u32 reg_2844;
437 u32 reg_2848;
438 u32 reg_2854;
439 u32 reg_285c;
440 u32 reg_2864;
441
442 u32 reg_2870;
443 u32 reg_2874;
444 u32 reg_2890;
445 u32 reg_2898;
446 u32 reg_289c;
447
448 u32 reg_2918;
449 u32 reg_291c;
450 u32 reg_2920;
451 u32 reg_2924;
452 u32 reg_2928;
453 u32 reg_292c;
454 u32 reg_2930;
455
456 u32 reg_2934;
457
458 u32 reg_2938;
459 u32 reg_293c;
460 u32 reg_2940;
461 u32 reg_2944;
462 u32 reg_2948;
463 u32 reg_294c;
464 u32 reg_2950;
465 u32 reg_2954;
466 u32 reg_2958;
467 u32 reg_295c;
468 u32 reg_2960;
469 u32 reg_2964;
470 u32 reg_2968;
471 u32 reg_296c;
472
473 u32 reg_2970;
474
475 int v_filter_1;
476 int v_filter_2;
477 int h_filter;
478
479 u8 track_osd; /* Should yuv output track the OSD size & position */
480
481 u32 osd_x_offset;
482 u32 osd_y_offset;
483
484 u32 osd_x_pan;
485 u32 osd_y_pan;
486
487 u32 osd_vis_w;
488 u32 osd_vis_h;
489
490 u32 osd_full_w;
491 u32 osd_full_h;
492
493 int decode_height;
494
495 int lace_mode;
496 int lace_threshold;
497 int lace_sync_field;
498
499 atomic_t next_dma_frame;
500 atomic_t next_fill_frame;
501
502 u32 yuv_forced_update;
503 int update_frame;
504
505 u8 fields_lapsed; /* Counter used when delaying a frame */
506
507 struct yuv_frame_info new_frame_info[IVTV_YUV_BUFFERS];
508 struct yuv_frame_info old_frame_info;
509 struct yuv_frame_info old_frame_info_args;
510
511 void *blanking_ptr;
512 dma_addr_t blanking_dmaptr;
513
514 int stream_size;
515
516 u8 draw_frame; /* PVR350 buffer to draw into */
517 u8 max_frames_buffered; /* Maximum number of frames to buffer */
518
519 struct v4l2_rect main_rect;
520 u32 v4l2_src_w;
521 u32 v4l2_src_h;
522
523 u8 running; /* Have any frames been displayed */
524};
525
526#define IVTV_VBI_FRAMES 32
527
528/* VBI data */
529struct vbi_cc {
530 u8 odd[2]; /* two-byte payload of odd field */
531 u8 even[2]; /* two-byte payload of even field */;
532};
533
534struct vbi_vps {
535 u8 data[5]; /* five-byte VPS payload */
536};
537
538struct vbi_info {
539 /* VBI general data, does not change during streaming */
540
541 u32 raw_decoder_line_size; /* raw VBI line size from digitizer */
542 u8 raw_decoder_sav_odd_field; /* raw VBI Start Active Video digitizer code of odd field */
543 u8 raw_decoder_sav_even_field; /* raw VBI Start Active Video digitizer code of even field */
544 u32 sliced_decoder_line_size; /* sliced VBI line size from digitizer */
545 u8 sliced_decoder_sav_odd_field; /* sliced VBI Start Active Video digitizer code of odd field */
546 u8 sliced_decoder_sav_even_field; /* sliced VBI Start Active Video digitizer code of even field */
547
548 u32 start[2]; /* start of first VBI line in the odd/even fields */
549 u32 count; /* number of VBI lines per field */
550 u32 raw_size; /* size of raw VBI line from the digitizer */
551 u32 sliced_size; /* size of sliced VBI line from the digitizer */
552
553 u32 dec_start; /* start in decoder memory of VBI re-insertion buffers */
554 u32 enc_start; /* start in encoder memory of VBI capture buffers */
555 u32 enc_size; /* size of VBI capture area */
556 int fpi; /* number of VBI frames per interrupt */
557
558 struct v4l2_format in; /* current VBI capture format */
559 struct v4l2_sliced_vbi_format *sliced_in; /* convenience pointer to sliced struct in vbi.in union */
560 int insert_mpeg; /* if non-zero, then embed VBI data in MPEG stream */
561
562 /* Raw VBI compatibility hack */
563
564 u32 frame; /* frame counter hack needed for backwards compatibility
565 of old VBI software */
566
567 /* Sliced VBI output data */
568
569 struct vbi_cc cc_payload[256]; /* sliced VBI CC payload array: it is an array to
570 prevent dropping CC data if they couldn't be
571 processed fast enough */
572 int cc_payload_idx; /* index in cc_payload */
573 u8 cc_missing_cnt; /* counts number of frames without CC for passthrough mode */
574 int wss_payload; /* sliced VBI WSS payload */
575 u8 wss_missing_cnt; /* counts number of frames without WSS for passthrough mode */
576 struct vbi_vps vps_payload; /* sliced VBI VPS payload */
577
578 /* Sliced VBI capture data */
579
580 struct v4l2_sliced_vbi_data sliced_data[36]; /* sliced VBI storage for VBI encoder stream */
581 struct v4l2_sliced_vbi_data sliced_dec_data[36];/* sliced VBI storage for VBI decoder stream */
582
583 /* VBI Embedding data */
584
585 /* Buffer for VBI data inserted into MPEG stream.
586 The first byte is a dummy byte that's never used.
587 The next 16 bytes contain the MPEG header for the VBI data,
588 the remainder is the actual VBI data.
589 The max size accepted by the MPEG VBI reinsertion turns out
590 to be 1552 bytes, which happens to be 4 + (1 + 42) * (2 * 18) bytes,
591 where 4 is a four byte header, 42 is the max sliced VBI payload, 1 is
592 a single line header byte and 2 * 18 is the number of VBI lines per frame.
593
594 However, it seems that the data must be 1K aligned, so we have to
595 pad the data until the 1 or 2 K boundary.
596
597 This pointer array will allocate 2049 bytes to store each VBI frame. */
598 u8 *sliced_mpeg_data[IVTV_VBI_FRAMES];
599 u32 sliced_mpeg_size[IVTV_VBI_FRAMES];
600 struct ivtv_buffer sliced_mpeg_buf; /* temporary buffer holding data from sliced_mpeg_data */
601 u32 inserted_frame; /* index in sliced_mpeg_size of next sliced data
602 to be inserted in the MPEG stream */
603};
604
605/* forward declaration of struct defined in ivtv-cards.h */
606struct ivtv_card;
607
608/* Struct to hold info about ivtv cards */
609struct ivtv {
610 /* General fixed card data */
611 struct pci_dev *pdev; /* PCI device */
612 const struct ivtv_card *card; /* card information */
613 const char *card_name; /* full name of the card */
614 const struct ivtv_card_tuner_i2c *card_i2c; /* i2c addresses to probe for tuner */
615 u8 has_cx23415; /* 1 if it is a cx23415 based card, 0 for cx23416 */
616 u8 pvr150_workaround; /* 1 if the cx25840 needs to workaround a PVR150 bug */
617 u8 nof_inputs; /* number of video inputs */
618 u8 nof_audio_inputs; /* number of audio inputs */
619 u32 v4l2_cap; /* V4L2 capabilities of card */
620 u32 hw_flags; /* hardware description of the board */
621 v4l2_std_id tuner_std; /* the norm of the card's tuner (fixed) */
622 struct v4l2_subdev *sd_video; /* controlling video decoder subdev */
623 struct v4l2_subdev *sd_audio; /* controlling audio subdev */
624 struct v4l2_subdev *sd_muxer; /* controlling audio muxer subdev */
625 resource_size_t base_addr; /* PCI resource base address */
626 volatile void __iomem *enc_mem; /* pointer to mapped encoder memory */
627 volatile void __iomem *dec_mem; /* pointer to mapped decoder memory */
628 volatile void __iomem *reg_mem; /* pointer to mapped registers */
629 struct ivtv_options options; /* user options */
630
631 struct v4l2_device v4l2_dev;
632 struct cx2341x_handler cxhdl;
633 struct {
634 /* PTS/Frame count control cluster */
635 struct v4l2_ctrl *ctrl_pts;
636 struct v4l2_ctrl *ctrl_frame;
637 };
638 struct {
639 /* Audio Playback control cluster */
640 struct v4l2_ctrl *ctrl_audio_playback;
641 struct v4l2_ctrl *ctrl_audio_multilingual_playback;
642 };
643 struct v4l2_ctrl_handler hdl_gpio;
644 struct v4l2_subdev sd_gpio; /* GPIO sub-device */
645 u16 instance;
646
647 /* High-level state info */
648 unsigned long i_flags; /* global ivtv flags */
649 u8 is_50hz; /* 1 if the current capture standard is 50 Hz */
650 u8 is_60hz /* 1 if the current capture standard is 60 Hz */;
651 u8 is_out_50hz /* 1 if the current TV output standard is 50 Hz */;
652 u8 is_out_60hz /* 1 if the current TV output standard is 60 Hz */;
653 int output_mode; /* decoder output mode: NONE, MPG, YUV, UDMA YUV, passthrough */
654 u32 audio_input; /* current audio input */
655 u32 active_input; /* current video input */
656 u32 active_output; /* current video output */
657 v4l2_std_id std; /* current capture TV standard */
658 v4l2_std_id std_out; /* current TV output standard */
659 u8 audio_stereo_mode; /* decoder setting how to handle stereo MPEG audio */
660 u8 audio_bilingual_mode; /* decoder setting how to handle bilingual MPEG audio */
661
662 /* Locking */
663 spinlock_t lock; /* lock access to this struct */
664 struct mutex serialize_lock; /* mutex used to serialize open/close/start/stop/ioctl operations */
665
666 /* Streams */
667 int stream_buf_size[IVTV_MAX_STREAMS]; /* stream buffer size */
668 struct ivtv_stream streams[IVTV_MAX_STREAMS]; /* stream data */
669 atomic_t capturing; /* count number of active capture streams */
670 atomic_t decoding; /* count number of active decoding streams */
671
672
673 /* Interrupts & DMA */
674 u32 irqmask; /* active interrupts */
675 u32 irq_rr_idx; /* round-robin stream index */
676 struct kthread_worker irq_worker; /* kthread worker for PIO/YUV/VBI actions */
677 struct task_struct *irq_worker_task; /* task for irq_worker */
678 struct kthread_work irq_work; /* kthread work entry */
679 spinlock_t dma_reg_lock; /* lock access to DMA engine registers */
680 int cur_dma_stream; /* index of current stream doing DMA (-1 if none) */
681 int cur_pio_stream; /* index of current stream doing PIO (-1 if none) */
682 u32 dma_data_req_offset; /* store offset in decoder memory of current DMA request */
683 u32 dma_data_req_size; /* store size of current DMA request */
684 int dma_retries; /* current DMA retry attempt */
685 struct ivtv_user_dma udma; /* user based DMA for OSD */
686 struct timer_list dma_timer; /* timer used to catch unfinished DMAs */
687 u32 last_vsync_field; /* last seen vsync field */
688 wait_queue_head_t dma_waitq; /* wake up when the current DMA is finished */
689 wait_queue_head_t eos_waitq; /* wake up when EOS arrives */
690 wait_queue_head_t event_waitq; /* wake up when the next decoder event arrives */
691 wait_queue_head_t vsync_waitq; /* wake up when the next decoder vsync arrives */
692
693
694 /* Mailbox */
695 struct ivtv_mailbox_data enc_mbox; /* encoder mailboxes */
696 struct ivtv_mailbox_data dec_mbox; /* decoder mailboxes */
697 struct ivtv_api_cache api_cache[256]; /* cached API commands */
698
699
700 /* I2C */
701 struct i2c_adapter i2c_adap;
702 struct i2c_algo_bit_data i2c_algo;
703 struct i2c_client i2c_client;
704 int i2c_state; /* i2c bit state */
705 struct mutex i2c_bus_lock; /* lock i2c bus */
706
707 struct IR_i2c_init_data ir_i2c_init_data;
708
709 /* Program Index information */
710 u32 pgm_info_offset; /* start of pgm info in encoder memory */
711 u32 pgm_info_num; /* number of elements in the pgm cyclic buffer in encoder memory */
712 u32 pgm_info_write_idx; /* last index written by the card that was transferred to pgm_info[] */
713 u32 pgm_info_read_idx; /* last index in pgm_info read by the application */
714 struct v4l2_enc_idx_entry pgm_info[IVTV_MAX_PGM_INDEX]; /* filled from the pgm cyclic buffer on the card */
715
716
717 /* Miscellaneous */
718 u32 open_id; /* incremented each time an open occurs, is >= 1 */
719 int search_pack_header; /* 1 if ivtv_copy_buf_to_user() is scanning for a pack header (0xba) */
720 int speed; /* current playback speed setting */
721 u8 speed_mute_audio; /* 1 if audio should be muted when fast forward */
722 u64 mpg_data_received; /* number of bytes received from the MPEG stream */
723 u64 vbi_data_inserted; /* number of VBI bytes inserted into the MPEG stream */
724 u32 last_dec_timing[3]; /* cache last retrieved pts/scr/frame values */
725 unsigned long dualwatch_jiffies;/* jiffies value of the previous dualwatch check */
726 u32 dualwatch_stereo_mode; /* current detected dualwatch stereo mode */
727
728
729 /* VBI state info */
730 struct vbi_info vbi; /* VBI-specific data */
731
732
733 /* YUV playback */
734 struct yuv_playback_info yuv_info; /* YUV playback data */
735
736
737 /* OSD support */
738 unsigned long osd_video_pbase;
739 int osd_global_alpha_state; /* 1 = global alpha is on */
740 int osd_local_alpha_state; /* 1 = local alpha is on */
741 int osd_chroma_key_state; /* 1 = chroma-keying is on */
742 u8 osd_global_alpha; /* current global alpha */
743 u32 osd_chroma_key; /* current chroma key */
744 struct v4l2_rect osd_rect; /* current OSD position and size */
745 struct v4l2_rect main_rect; /* current Main window position and size */
746 struct osd_info *osd_info; /* ivtvfb private OSD info */
747 void (*ivtvfb_restore)(struct ivtv *itv); /* Used for a warm start */
748};
749
750static inline struct ivtv *to_ivtv(struct v4l2_device *v4l2_dev)
751{
752 return container_of(v4l2_dev, struct ivtv, v4l2_dev);
753}
754
755/* Globals */
756extern int ivtv_first_minor;
757
758/*==============Prototypes==================*/
759
760/* Hardware/IRQ */
761void ivtv_set_irq_mask(struct ivtv *itv, u32 mask);
762void ivtv_clear_irq_mask(struct ivtv *itv, u32 mask);
763
764/* try to set output mode, return current mode. */
765int ivtv_set_output_mode(struct ivtv *itv, int mode);
766
767/* return current output stream based on current mode */
768struct ivtv_stream *ivtv_get_output_stream(struct ivtv *itv);
769
770/* Return non-zero if a signal is pending */
771int ivtv_msleep_timeout(unsigned int msecs, int intr);
772
773/* Wait on queue, returns -EINTR if interrupted */
774int ivtv_waitq(wait_queue_head_t *waitq);
775
776/* Read Hauppauge eeprom */
777struct tveeprom; /* forward reference */
778void ivtv_read_eeprom(struct ivtv *itv, struct tveeprom *tv);
779
780/* First-open initialization: load firmware, init cx25840, etc. */
781int ivtv_init_on_first_open(struct ivtv *itv);
782
783/* Test if the current VBI mode is raw (1) or sliced (0) */
784static inline int ivtv_raw_vbi(const struct ivtv *itv)
785{
786 return itv->vbi.in.type == V4L2_BUF_TYPE_VBI_CAPTURE;
787}
788
789/* This is a PCI post thing, where if the pci register is not read, then
790 the write doesn't always take effect right away. By reading back the
791 register any pending PCI writes will be performed (in order), and so
792 you can be sure that the writes are guaranteed to be done.
793
794 Rarely needed, only in some timing sensitive cases.
795 Apparently if this is not done some motherboards seem
796 to kill the firmware and get into the broken state until computer is
797 rebooted. */
798#define write_sync(val, reg) \
799 do { writel(val, reg); readl(reg); } while (0)
800
801#define read_reg(reg) readl(itv->reg_mem + (reg))
802#define write_reg(val, reg) writel(val, itv->reg_mem + (reg))
803#define write_reg_sync(val, reg) \
804 do { write_reg(val, reg); read_reg(reg); } while (0)
805
806#define read_enc(addr) readl(itv->enc_mem + (u32)(addr))
807#define write_enc(val, addr) writel(val, itv->enc_mem + (u32)(addr))
808#define write_enc_sync(val, addr) \
809 do { write_enc(val, addr); read_enc(addr); } while (0)
810
811#define read_dec(addr) readl(itv->dec_mem + (u32)(addr))
812#define write_dec(val, addr) writel(val, itv->dec_mem + (u32)(addr))
813#define write_dec_sync(val, addr) \
814 do { write_dec(val, addr); read_dec(addr); } while (0)
815
816/* Call the specified callback for all subdevs matching hw (if 0, then
817 match them all). Ignore any errors. */
818#define ivtv_call_hw(itv, hw, o, f, args...) \
819 do { \
820 struct v4l2_subdev *__sd; \
821 __v4l2_device_call_subdevs_p(&(itv)->v4l2_dev, __sd, \
822 !(hw) || (__sd->grp_id & (hw)), o, f , ##args); \
823 } while (0)
824
825#define ivtv_call_all(itv, o, f, args...) ivtv_call_hw(itv, 0, o, f , ##args)
826
827/* Call the specified callback for all subdevs matching hw (if 0, then
828 match them all). If the callback returns an error other than 0 or
829 -ENOIOCTLCMD, then return with that error code. */
830#define ivtv_call_hw_err(itv, hw, o, f, args...) \
831({ \
832 struct v4l2_subdev *__sd; \
833 __v4l2_device_call_subdevs_until_err_p(&(itv)->v4l2_dev, __sd, \
834 !(hw) || (__sd->grp_id & (hw)), o, f , ##args); \
835})
836
837#define ivtv_call_all_err(itv, o, f, args...) ivtv_call_hw_err(itv, 0, o, f , ##args)
838
839#endif
diff --git a/drivers/media/pci/ivtv/ivtv-fileops.c b/drivers/media/pci/ivtv/ivtv-fileops.c
new file mode 100644
index 000000000000..88bce907cdef
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-fileops.c
@@ -0,0 +1,1070 @@
1/*
2 file operation functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include "ivtv-driver.h"
23#include "ivtv-fileops.h"
24#include "ivtv-i2c.h"
25#include "ivtv-queue.h"
26#include "ivtv-udma.h"
27#include "ivtv-irq.h"
28#include "ivtv-vbi.h"
29#include "ivtv-mailbox.h"
30#include "ivtv-routing.h"
31#include "ivtv-streams.h"
32#include "ivtv-yuv.h"
33#include "ivtv-ioctl.h"
34#include "ivtv-cards.h"
35#include "ivtv-firmware.h"
36#include <media/v4l2-event.h>
37#include <media/saa7115.h>
38
39/* This function tries to claim the stream for a specific file descriptor.
40 If no one else is using this stream then the stream is claimed and
41 associated VBI streams are also automatically claimed.
42 Possible error returns: -EBUSY if someone else has claimed
43 the stream or 0 on success. */
44static int ivtv_claim_stream(struct ivtv_open_id *id, int type)
45{
46 struct ivtv *itv = id->itv;
47 struct ivtv_stream *s = &itv->streams[type];
48 struct ivtv_stream *s_vbi;
49 int vbi_type;
50
51 if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
52 /* someone already claimed this stream */
53 if (s->fh == &id->fh) {
54 /* yes, this file descriptor did. So that's OK. */
55 return 0;
56 }
57 if (s->fh == NULL && (type == IVTV_DEC_STREAM_TYPE_VBI ||
58 type == IVTV_ENC_STREAM_TYPE_VBI)) {
59 /* VBI is handled already internally, now also assign
60 the file descriptor to this stream for external
61 reading of the stream. */
62 s->fh = &id->fh;
63 IVTV_DEBUG_INFO("Start Read VBI\n");
64 return 0;
65 }
66 /* someone else is using this stream already */
67 IVTV_DEBUG_INFO("Stream %d is busy\n", type);
68 return -EBUSY;
69 }
70 s->fh = &id->fh;
71 if (type == IVTV_DEC_STREAM_TYPE_VBI) {
72 /* Enable reinsertion interrupt */
73 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
74 }
75
76 /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
77 IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
78 (provided VBI insertion is on and sliced VBI is selected), for all
79 other streams we're done */
80 if (type == IVTV_DEC_STREAM_TYPE_MPG) {
81 vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
82 } else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
83 itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
84 vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
85 } else {
86 return 0;
87 }
88 s_vbi = &itv->streams[vbi_type];
89
90 if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
91 /* Enable reinsertion interrupt */
92 if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
93 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
94 }
95 /* mark that it is used internally */
96 set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
97 return 0;
98}
99
100/* This function releases a previously claimed stream. It will take into
101 account associated VBI streams. */
102void ivtv_release_stream(struct ivtv_stream *s)
103{
104 struct ivtv *itv = s->itv;
105 struct ivtv_stream *s_vbi;
106
107 s->fh = NULL;
108 if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
109 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
110 /* this stream is still in use internally */
111 return;
112 }
113 if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
114 IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
115 return;
116 }
117
118 ivtv_flush_queues(s);
119
120 /* disable reinsertion interrupt */
121 if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
122 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
123
124 /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
125 IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
126 for all other streams we're done */
127 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
128 s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
129 else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
130 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
131 else
132 return;
133
134 /* clear internal use flag */
135 if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
136 /* was already cleared */
137 return;
138 }
139 if (s_vbi->fh) {
140 /* VBI stream still claimed by a file descriptor */
141 return;
142 }
143 /* disable reinsertion interrupt */
144 if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
145 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
146 clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
147 ivtv_flush_queues(s_vbi);
148}
149
150static void ivtv_dualwatch(struct ivtv *itv)
151{
152 struct v4l2_tuner vt;
153 u32 new_stereo_mode;
154 const u32 dual = 0x02;
155
156 new_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
157 memset(&vt, 0, sizeof(vt));
158 ivtv_call_all(itv, tuner, g_tuner, &vt);
159 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
160 new_stereo_mode = dual;
161
162 if (new_stereo_mode == itv->dualwatch_stereo_mode)
163 return;
164
165 IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
166 itv->dualwatch_stereo_mode, new_stereo_mode);
167 if (v4l2_ctrl_s_ctrl(itv->cxhdl.audio_mode, new_stereo_mode))
168 IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
169}
170
171static void ivtv_update_pgm_info(struct ivtv *itv)
172{
173 u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
174 int cnt;
175 int i = 0;
176
177 if (wr_idx >= itv->pgm_info_num) {
178 IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
179 return;
180 }
181 cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
182 while (i < cnt) {
183 int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
184 struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
185 u32 addr = itv->pgm_info_offset + 4 + idx * 24;
186 const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1,
187 V4L2_ENC_IDX_FRAME_B, -1, -1, -1 };
188 // 1=I, 2=P, 4=B
189
190 e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
191 if (e->offset > itv->mpg_data_received) {
192 break;
193 }
194 e->offset += itv->vbi_data_inserted;
195 e->length = read_enc(addr);
196 e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
197 e->flags = mapping[read_enc(addr + 12) & 7];
198 i++;
199 }
200 itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
201}
202
203static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
204{
205 struct ivtv *itv = s->itv;
206 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
207 struct ivtv_buffer *buf;
208 DEFINE_WAIT(wait);
209
210 *err = 0;
211 while (1) {
212 if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
213 /* Process pending program info updates and pending VBI data */
214 ivtv_update_pgm_info(itv);
215
216 if (time_after(jiffies,
217 itv->dualwatch_jiffies +
218 msecs_to_jiffies(1000))) {
219 itv->dualwatch_jiffies = jiffies;
220 ivtv_dualwatch(itv);
221 }
222
223 if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
224 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
225 while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
226 /* byteswap and process VBI data */
227 ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
228 ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
229 }
230 }
231 buf = &itv->vbi.sliced_mpeg_buf;
232 if (buf->readpos != buf->bytesused) {
233 return buf;
234 }
235 }
236
237 /* do we have leftover data? */
238 buf = ivtv_dequeue(s, &s->q_io);
239 if (buf)
240 return buf;
241
242 /* do we have new data? */
243 buf = ivtv_dequeue(s, &s->q_full);
244 if (buf) {
245 if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0)
246 return buf;
247 buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP;
248 if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
249 /* byteswap MPG data */
250 ivtv_buf_swap(buf);
251 else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
252 /* byteswap and process VBI data */
253 ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
254 }
255 return buf;
256 }
257
258 /* return if end of stream */
259 if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
260 IVTV_DEBUG_INFO("EOS %s\n", s->name);
261 return NULL;
262 }
263
264 /* return if file was opened with O_NONBLOCK */
265 if (non_block) {
266 *err = -EAGAIN;
267 return NULL;
268 }
269
270 /* wait for more data to arrive */
271 mutex_unlock(&itv->serialize_lock);
272 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
273 /* New buffers might have become available before we were added to the waitqueue */
274 if (!s->q_full.buffers)
275 schedule();
276 finish_wait(&s->waitq, &wait);
277 mutex_lock(&itv->serialize_lock);
278 if (signal_pending(current)) {
279 /* return if a signal was received */
280 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
281 *err = -EINTR;
282 return NULL;
283 }
284 }
285}
286
287static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
288{
289 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
290
291 itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
292 itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
293 itv->vbi.sliced_mpeg_buf.readpos = 0;
294}
295
296static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
297 char __user *ubuf, size_t ucount)
298{
299 struct ivtv *itv = s->itv;
300 size_t len = buf->bytesused - buf->readpos;
301
302 if (len > ucount) len = ucount;
303 if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
304 !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) {
305 const char *start = buf->buf + buf->readpos;
306 const char *p = start + 1;
307 const u8 *q;
308 u8 ch = itv->search_pack_header ? 0xba : 0xe0;
309 int stuffing, i;
310
311 while (start + len > p && (q = memchr(p, 0, start + len - p))) {
312 p = q + 1;
313 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
314 q[1] != 0 || q[2] != 1 || q[3] != ch) {
315 continue;
316 }
317 if (!itv->search_pack_header) {
318 if ((q[6] & 0xc0) != 0x80)
319 continue;
320 if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
321 ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
322 ch = 0xba;
323 itv->search_pack_header = 1;
324 p = q + 9;
325 }
326 continue;
327 }
328 stuffing = q[13] & 7;
329 /* all stuffing bytes must be 0xff */
330 for (i = 0; i < stuffing; i++)
331 if (q[14 + i] != 0xff)
332 break;
333 if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
334 q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
335 q[16 + stuffing] == 1) {
336 itv->search_pack_header = 0;
337 len = (char *)q - start;
338 ivtv_setup_sliced_vbi_buf(itv);
339 break;
340 }
341 }
342 }
343 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
344 IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
345 return -EFAULT;
346 }
347 /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount,
348 buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len,
349 buf == &itv->vbi.sliced_mpeg_buf); */
350 buf->readpos += len;
351 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
352 itv->mpg_data_received += len;
353 return len;
354}
355
356static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
357{
358 struct ivtv *itv = s->itv;
359 size_t tot_written = 0;
360 int single_frame = 0;
361
362 if (atomic_read(&itv->capturing) == 0 && s->fh == NULL) {
363 /* shouldn't happen */
364 IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
365 return -EIO;
366 }
367
368 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
369 arrive one-by-one, so make sure we never output more than one VBI frame at a time */
370 if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
371 (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv)))
372 single_frame = 1;
373
374 for (;;) {
375 struct ivtv_buffer *buf;
376 int rc;
377
378 buf = ivtv_get_buffer(s, non_block, &rc);
379 /* if there is no data available... */
380 if (buf == NULL) {
381 /* if we got data, then return that regardless */
382 if (tot_written)
383 break;
384 /* EOS condition */
385 if (rc == 0) {
386 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
387 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
388 ivtv_release_stream(s);
389 }
390 /* set errno */
391 return rc;
392 }
393 rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
394 if (buf != &itv->vbi.sliced_mpeg_buf) {
395 ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
396 }
397 else if (buf->readpos == buf->bytesused) {
398 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
399 itv->vbi.sliced_mpeg_size[idx] = 0;
400 itv->vbi.inserted_frame++;
401 itv->vbi_data_inserted += buf->bytesused;
402 }
403 if (rc < 0)
404 return rc;
405 tot_written += rc;
406
407 if (tot_written == tot_count || single_frame)
408 break;
409 }
410 return tot_written;
411}
412
413static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
414 loff_t *pos, int non_block)
415{
416 ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
417 struct ivtv *itv = s->itv;
418
419 IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
420 if (rc > 0)
421 pos += rc;
422 return rc;
423}
424
425int ivtv_start_capture(struct ivtv_open_id *id)
426{
427 struct ivtv *itv = id->itv;
428 struct ivtv_stream *s = &itv->streams[id->type];
429 struct ivtv_stream *s_vbi;
430
431 if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
432 s->type == IVTV_DEC_STREAM_TYPE_MPG ||
433 s->type == IVTV_DEC_STREAM_TYPE_YUV ||
434 s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
435 /* you cannot read from these stream types. */
436 return -EPERM;
437 }
438
439 /* Try to claim this stream. */
440 if (ivtv_claim_stream(id, s->type))
441 return -EBUSY;
442
443 /* This stream does not need to start capturing */
444 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
445 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
446 return 0;
447 }
448
449 /* If capture is already in progress, then we also have to
450 do nothing extra. */
451 if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
452 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
453 return 0;
454 }
455
456 /* Start VBI capture if required */
457 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
458 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
459 test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
460 !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
461 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed
462 automatically when the MPG stream is claimed.
463 We only need to start the VBI capturing. */
464 if (ivtv_start_v4l2_encode_stream(s_vbi)) {
465 IVTV_DEBUG_WARN("VBI capture start failed\n");
466
467 /* Failure, clean up and return an error */
468 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
469 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
470 /* also releases the associated VBI stream */
471 ivtv_release_stream(s);
472 return -EIO;
473 }
474 IVTV_DEBUG_INFO("VBI insertion started\n");
475 }
476
477 /* Tell the card to start capturing */
478 if (!ivtv_start_v4l2_encode_stream(s)) {
479 /* We're done */
480 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
481 /* Resume a possibly paused encoder */
482 if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
483 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
484 return 0;
485 }
486
487 /* failure, clean up */
488 IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
489
490 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released
491 automatically when the MPG stream is released.
492 We only need to stop the VBI capturing. */
493 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
494 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
495 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
496 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
497 }
498 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
499 ivtv_release_stream(s);
500 return -EIO;
501}
502
503ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
504{
505 struct ivtv_open_id *id = fh2id(filp->private_data);
506 struct ivtv *itv = id->itv;
507 struct ivtv_stream *s = &itv->streams[id->type];
508 ssize_t rc;
509
510 IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
511
512 if (mutex_lock_interruptible(&itv->serialize_lock))
513 return -ERESTARTSYS;
514 rc = ivtv_start_capture(id);
515 if (!rc)
516 rc = ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
517 mutex_unlock(&itv->serialize_lock);
518 return rc;
519}
520
521int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
522{
523 struct ivtv *itv = id->itv;
524 struct ivtv_stream *s = &itv->streams[id->type];
525 int rc;
526
527 if (atomic_read(&itv->decoding) == 0) {
528 if (ivtv_claim_stream(id, s->type)) {
529 /* someone else is using this stream already */
530 IVTV_DEBUG_WARN("start decode, stream already claimed\n");
531 return -EBUSY;
532 }
533 rc = ivtv_start_v4l2_decode_stream(s, 0);
534 if (rc < 0) {
535 if (rc == -EAGAIN)
536 rc = ivtv_start_v4l2_decode_stream(s, 0);
537 if (rc < 0)
538 return rc;
539 }
540 }
541 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
542 return ivtv_set_speed(itv, speed);
543 return 0;
544}
545
546static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
547{
548 struct ivtv_open_id *id = fh2id(filp->private_data);
549 struct ivtv *itv = id->itv;
550 struct ivtv_stream *s = &itv->streams[id->type];
551 struct yuv_playback_info *yi = &itv->yuv_info;
552 struct ivtv_buffer *buf;
553 struct ivtv_queue q;
554 int bytes_written = 0;
555 int mode;
556 int rc;
557 DEFINE_WAIT(wait);
558
559 IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
560
561 if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
562 s->type != IVTV_DEC_STREAM_TYPE_YUV &&
563 s->type != IVTV_DEC_STREAM_TYPE_VOUT)
564 /* not decoder streams */
565 return -EPERM;
566
567 /* Try to claim this stream */
568 if (ivtv_claim_stream(id, s->type))
569 return -EBUSY;
570
571 /* This stream does not need to start any decoding */
572 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
573 int elems = count / sizeof(struct v4l2_sliced_vbi_data);
574
575 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
576 return ivtv_write_vbi_from_user(itv,
577 (const struct v4l2_sliced_vbi_data __user *)user_buf, elems);
578 }
579
580 mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
581
582 if (ivtv_set_output_mode(itv, mode) != mode) {
583 ivtv_release_stream(s);
584 return -EBUSY;
585 }
586 ivtv_queue_init(&q);
587 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
588
589 /* Start decoder (returns 0 if already started) */
590 rc = ivtv_start_decoding(id, itv->speed);
591 if (rc) {
592 IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
593
594 /* failure, clean up */
595 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
596 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
597 return rc;
598 }
599
600retry:
601 /* If possible, just DMA the entire frame - Check the data transfer size
602 since we may get here before the stream has been fully set-up */
603 if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
604 while (count >= itv->dma_data_req_size) {
605 rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
606
607 if (rc < 0)
608 return rc;
609
610 bytes_written += itv->dma_data_req_size;
611 user_buf += itv->dma_data_req_size;
612 count -= itv->dma_data_req_size;
613 }
614 if (count == 0) {
615 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
616 return bytes_written;
617 }
618 }
619
620 for (;;) {
621 /* Gather buffers */
622 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
623 ivtv_enqueue(s, buf, &q);
624 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
625 ivtv_enqueue(s, buf, &q);
626 }
627 if (q.buffers)
628 break;
629 if (filp->f_flags & O_NONBLOCK)
630 return -EAGAIN;
631 mutex_unlock(&itv->serialize_lock);
632 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
633 /* New buffers might have become free before we were added to the waitqueue */
634 if (!s->q_free.buffers)
635 schedule();
636 finish_wait(&s->waitq, &wait);
637 mutex_lock(&itv->serialize_lock);
638 if (signal_pending(current)) {
639 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
640 return -EINTR;
641 }
642 }
643
644 /* copy user data into buffers */
645 while ((buf = ivtv_dequeue(s, &q))) {
646 /* yuv is a pain. Don't copy more data than needed for a single
647 frame, otherwise we lose sync with the incoming stream */
648 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
649 yi->stream_size + count > itv->dma_data_req_size)
650 rc = ivtv_buf_copy_from_user(s, buf, user_buf,
651 itv->dma_data_req_size - yi->stream_size);
652 else
653 rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
654
655 /* Make sure we really got all the user data */
656 if (rc < 0) {
657 ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
658 return rc;
659 }
660 user_buf += rc;
661 count -= rc;
662 bytes_written += rc;
663
664 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
665 yi->stream_size += rc;
666 /* If we have a complete yuv frame, break loop now */
667 if (yi->stream_size == itv->dma_data_req_size) {
668 ivtv_enqueue(s, buf, &s->q_full);
669 yi->stream_size = 0;
670 break;
671 }
672 }
673
674 if (buf->bytesused != s->buf_size) {
675 /* incomplete, leave in q_io for next time */
676 ivtv_enqueue(s, buf, &s->q_io);
677 break;
678 }
679 /* Byteswap MPEG buffer */
680 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
681 ivtv_buf_swap(buf);
682 ivtv_enqueue(s, buf, &s->q_full);
683 }
684
685 if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
686 if (s->q_full.length >= itv->dma_data_req_size) {
687 int got_sig;
688
689 if (mode == OUT_YUV)
690 ivtv_yuv_setup_stream_frame(itv);
691
692 mutex_unlock(&itv->serialize_lock);
693 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
694 while (!(got_sig = signal_pending(current)) &&
695 test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
696 schedule();
697 }
698 finish_wait(&itv->dma_waitq, &wait);
699 mutex_lock(&itv->serialize_lock);
700 if (got_sig) {
701 IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
702 return -EINTR;
703 }
704
705 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
706 ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
707 ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
708 }
709 }
710 /* more user data is available, wait until buffers become free
711 to transfer the rest. */
712 if (count && !(filp->f_flags & O_NONBLOCK))
713 goto retry;
714 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
715 return bytes_written;
716}
717
718ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
719{
720 struct ivtv_open_id *id = fh2id(filp->private_data);
721 struct ivtv *itv = id->itv;
722 ssize_t res;
723
724 if (mutex_lock_interruptible(&itv->serialize_lock))
725 return -ERESTARTSYS;
726 res = ivtv_write(filp, user_buf, count, pos);
727 mutex_unlock(&itv->serialize_lock);
728 return res;
729}
730
731unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
732{
733 struct ivtv_open_id *id = fh2id(filp->private_data);
734 struct ivtv *itv = id->itv;
735 struct ivtv_stream *s = &itv->streams[id->type];
736 int res = 0;
737
738 /* add stream's waitq to the poll list */
739 IVTV_DEBUG_HI_FILE("Decoder poll\n");
740
741 /* If there are subscribed events, then only use the new event
742 API instead of the old video.h based API. */
743 if (!list_empty(&id->fh.subscribed)) {
744 poll_wait(filp, &id->fh.wait, wait);
745 /* Turn off the old-style vsync events */
746 clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
747 if (v4l2_event_pending(&id->fh))
748 res = POLLPRI;
749 } else {
750 /* This is the old-style API which is here only for backwards
751 compatibility. */
752 poll_wait(filp, &s->waitq, wait);
753 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
754 if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
755 test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
756 res = POLLPRI;
757 }
758
759 /* Allow write if buffers are available for writing */
760 if (s->q_free.buffers)
761 res |= POLLOUT | POLLWRNORM;
762 return res;
763}
764
765unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table *wait)
766{
767 unsigned long req_events = poll_requested_events(wait);
768 struct ivtv_open_id *id = fh2id(filp->private_data);
769 struct ivtv *itv = id->itv;
770 struct ivtv_stream *s = &itv->streams[id->type];
771 int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
772 unsigned res = 0;
773
774 /* Start a capture if there is none */
775 if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags) &&
776 (req_events & (POLLIN | POLLRDNORM))) {
777 int rc;
778
779 mutex_lock(&itv->serialize_lock);
780 rc = ivtv_start_capture(id);
781 mutex_unlock(&itv->serialize_lock);
782 if (rc) {
783 IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
784 s->name, rc);
785 return POLLERR;
786 }
787 IVTV_DEBUG_FILE("Encoder poll started capture\n");
788 }
789
790 /* add stream's waitq to the poll list */
791 IVTV_DEBUG_HI_FILE("Encoder poll\n");
792 poll_wait(filp, &s->waitq, wait);
793 if (v4l2_event_pending(&id->fh))
794 res |= POLLPRI;
795 else
796 poll_wait(filp, &id->fh.wait, wait);
797
798 if (s->q_full.length || s->q_io.length)
799 return res | POLLIN | POLLRDNORM;
800 if (eof)
801 return res | POLLHUP;
802 return res;
803}
804
805void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
806{
807 struct ivtv *itv = id->itv;
808 struct ivtv_stream *s = &itv->streams[id->type];
809
810 IVTV_DEBUG_FILE("close() of %s\n", s->name);
811
812 /* 'Unclaim' this stream */
813
814 /* Stop capturing */
815 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
816 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
817
818 IVTV_DEBUG_INFO("close stopping capture\n");
819 /* Special case: a running VBI capture for VBI insertion
820 in the mpeg stream. Need to stop that too. */
821 if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
822 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
823 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
824 IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
825 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
826 }
827 if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
828 id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
829 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
830 /* Also used internally, don't stop capturing */
831 s->fh = NULL;
832 }
833 else {
834 ivtv_stop_v4l2_encode_stream(s, gop_end);
835 }
836 }
837 if (!gop_end) {
838 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
839 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
840 ivtv_release_stream(s);
841 }
842}
843
844static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
845{
846 struct ivtv *itv = id->itv;
847 struct ivtv_stream *s = &itv->streams[id->type];
848
849 IVTV_DEBUG_FILE("close() of %s\n", s->name);
850
851 if (id->type == IVTV_DEC_STREAM_TYPE_YUV &&
852 test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
853 /* Restore registers we've changed & clean up any mess */
854 ivtv_yuv_close(itv);
855 }
856
857 /* Stop decoding */
858 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
859 IVTV_DEBUG_INFO("close stopping decode\n");
860
861 ivtv_stop_v4l2_decode_stream(s, flags, pts);
862 itv->output_mode = OUT_NONE;
863 }
864 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
865 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
866
867 if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
868 itv->output_mode = OUT_NONE;
869
870 itv->speed = 0;
871 clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
872 ivtv_release_stream(s);
873}
874
875int ivtv_v4l2_close(struct file *filp)
876{
877 struct v4l2_fh *fh = filp->private_data;
878 struct ivtv_open_id *id = fh2id(fh);
879 struct ivtv *itv = id->itv;
880 struct ivtv_stream *s = &itv->streams[id->type];
881
882 IVTV_DEBUG_FILE("close %s\n", s->name);
883
884 mutex_lock(&itv->serialize_lock);
885
886 /* Stop radio */
887 if (id->type == IVTV_ENC_STREAM_TYPE_RAD &&
888 v4l2_fh_is_singular_file(filp)) {
889 /* Closing radio device, return to TV mode */
890 ivtv_mute(itv);
891 /* Mark that the radio is no longer in use */
892 clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
893 /* Switch tuner to TV */
894 ivtv_call_all(itv, core, s_std, itv->std);
895 /* Select correct audio input (i.e. TV tuner or Line in) */
896 ivtv_audio_set_io(itv);
897 if (itv->hw_flags & IVTV_HW_SAA711X) {
898 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
899 SAA7115_FREQ_32_11_MHZ, 0);
900 }
901 if (atomic_read(&itv->capturing) > 0) {
902 /* Undo video mute */
903 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
904 v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute) |
905 (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
906 }
907 /* Done! Unmute and continue. */
908 ivtv_unmute(itv);
909 }
910
911 v4l2_fh_del(fh);
912 v4l2_fh_exit(fh);
913
914 /* Easy case first: this stream was never claimed by us */
915 if (s->fh != &id->fh)
916 goto close_done;
917
918 /* 'Unclaim' this stream */
919
920 if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
921 struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
922
923 ivtv_stop_decoding(id, V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY, 0);
924
925 /* If all output streams are closed, and if the user doesn't have
926 IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */
927 if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
928 /* disable CC on TV-out */
929 ivtv_disable_cc(itv);
930 }
931 } else {
932 ivtv_stop_capture(id, 0);
933 }
934close_done:
935 kfree(id);
936 mutex_unlock(&itv->serialize_lock);
937 return 0;
938}
939
940static int ivtv_open(struct file *filp)
941{
942 struct video_device *vdev = video_devdata(filp);
943 struct ivtv_stream *s = video_get_drvdata(vdev);
944 struct ivtv *itv = s->itv;
945 struct ivtv_open_id *item;
946 int res = 0;
947
948 IVTV_DEBUG_FILE("open %s\n", s->name);
949
950 if (ivtv_init_on_first_open(itv)) {
951 IVTV_ERR("Failed to initialize on device %s\n",
952 video_device_node_name(vdev));
953 return -ENXIO;
954 }
955
956#ifdef CONFIG_VIDEO_ADV_DEBUG
957 /* Unless ivtv_fw_debug is set, error out if firmware dead. */
958 if (ivtv_fw_debug) {
959 IVTV_WARN("Opening %s with dead firmware lockout disabled\n",
960 video_device_node_name(vdev));
961 IVTV_WARN("Selected firmware errors will be ignored\n");
962 } else {
963#else
964 if (1) {
965#endif
966 res = ivtv_firmware_check(itv, "ivtv_serialized_open");
967 if (res == -EAGAIN)
968 res = ivtv_firmware_check(itv, "ivtv_serialized_open");
969 if (res < 0)
970 return -EIO;
971 }
972
973 if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
974 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
975 return -EBUSY;
976
977 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
978 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
979 return -EBUSY;
980
981 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
982 if (read_reg(0x82c) == 0) {
983 IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
984 /* return -ENODEV; */
985 }
986 ivtv_udma_alloc(itv);
987 }
988
989 /* Allocate memory */
990 item = kzalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
991 if (NULL == item) {
992 IVTV_DEBUG_WARN("nomem on v4l2 open\n");
993 return -ENOMEM;
994 }
995 v4l2_fh_init(&item->fh, s->vdev);
996 item->itv = itv;
997 item->type = s->type;
998
999 filp->private_data = &item->fh;
1000 v4l2_fh_add(&item->fh);
1001
1002 if (item->type == IVTV_ENC_STREAM_TYPE_RAD &&
1003 v4l2_fh_is_singular_file(filp)) {
1004 if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1005 if (atomic_read(&itv->capturing) > 0) {
1006 /* switching to radio while capture is
1007 in progress is not polite */
1008 v4l2_fh_del(&item->fh);
1009 v4l2_fh_exit(&item->fh);
1010 kfree(item);
1011 return -EBUSY;
1012 }
1013 }
1014 /* Mark that the radio is being used. */
1015 set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
1016 /* We have the radio */
1017 ivtv_mute(itv);
1018 /* Switch tuner to radio */
1019 ivtv_call_all(itv, tuner, s_radio);
1020 /* Select the correct audio input (i.e. radio tuner) */
1021 ivtv_audio_set_io(itv);
1022 if (itv->hw_flags & IVTV_HW_SAA711X) {
1023 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
1024 SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL);
1025 }
1026 /* Done! Unmute and continue. */
1027 ivtv_unmute(itv);
1028 }
1029
1030 /* YUV or MPG Decoding Mode? */
1031 if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
1032 clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
1033 } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
1034 set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
1035 /* For yuv, we need to know the dma size before we start */
1036 itv->dma_data_req_size =
1037 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
1038 itv->yuv_info.stream_size = 0;
1039 }
1040 return 0;
1041}
1042
1043int ivtv_v4l2_open(struct file *filp)
1044{
1045 struct video_device *vdev = video_devdata(filp);
1046 int res;
1047
1048 if (mutex_lock_interruptible(vdev->lock))
1049 return -ERESTARTSYS;
1050 res = ivtv_open(filp);
1051 mutex_unlock(vdev->lock);
1052 return res;
1053}
1054
1055void ivtv_mute(struct ivtv *itv)
1056{
1057 if (atomic_read(&itv->capturing))
1058 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
1059 IVTV_DEBUG_INFO("Mute\n");
1060}
1061
1062void ivtv_unmute(struct ivtv *itv)
1063{
1064 if (atomic_read(&itv->capturing)) {
1065 ivtv_msleep_timeout(100, 0);
1066 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
1067 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
1068 }
1069 IVTV_DEBUG_INFO("Unmute\n");
1070}
diff --git a/drivers/media/pci/ivtv/ivtv-fileops.h b/drivers/media/pci/ivtv/ivtv-fileops.h
new file mode 100644
index 000000000000..049a2923965d
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-fileops.h
@@ -0,0 +1,44 @@
1/*
2 file operation functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_FILEOPS_H
22#define IVTV_FILEOPS_H
23
24/* Testing/Debugging */
25int ivtv_v4l2_open(struct file *filp);
26ssize_t ivtv_v4l2_read(struct file *filp, char __user *buf, size_t count,
27 loff_t * pos);
28ssize_t ivtv_v4l2_write(struct file *filp, const char __user *buf, size_t count,
29 loff_t * pos);
30int ivtv_v4l2_close(struct file *filp);
31unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait);
32unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table * wait);
33int ivtv_start_capture(struct ivtv_open_id *id);
34void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end);
35int ivtv_start_decoding(struct ivtv_open_id *id, int speed);
36void ivtv_mute(struct ivtv *itv);
37void ivtv_unmute(struct ivtv *itv);
38
39/* Utilities */
40
41/* Release a previously claimed stream. */
42void ivtv_release_stream(struct ivtv_stream *s);
43
44#endif
diff --git a/drivers/media/pci/ivtv/ivtv-firmware.c b/drivers/media/pci/ivtv/ivtv-firmware.c
new file mode 100644
index 000000000000..6ec7705af555
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-firmware.c
@@ -0,0 +1,402 @@
1/*
2 ivtv firmware functions.
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include "ivtv-driver.h"
23#include "ivtv-mailbox.h"
24#include "ivtv-firmware.h"
25#include "ivtv-yuv.h"
26#include "ivtv-ioctl.h"
27#include "ivtv-cards.h"
28#include <linux/firmware.h>
29#include <media/saa7127.h>
30
31#define IVTV_MASK_SPU_ENABLE 0xFFFFFFFE
32#define IVTV_MASK_VPU_ENABLE15 0xFFFFFFF6
33#define IVTV_MASK_VPU_ENABLE16 0xFFFFFFFB
34#define IVTV_CMD_VDM_STOP 0x00000000
35#define IVTV_CMD_AO_STOP 0x00000005
36#define IVTV_CMD_APU_PING 0x00000000
37#define IVTV_CMD_VPU_STOP15 0xFFFFFFFE
38#define IVTV_CMD_VPU_STOP16 0xFFFFFFEE
39#define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
40#define IVTV_CMD_SPU_STOP 0x00000001
41#define IVTV_CMD_SDRAM_PRECHARGE_INIT 0x0000001A
42#define IVTV_CMD_SDRAM_REFRESH_INIT 0x80000640
43#define IVTV_SDRAM_SLEEPTIME 600
44
45#define IVTV_DECODE_INIT_MPEG_FILENAME "v4l-cx2341x-init.mpg"
46#define IVTV_DECODE_INIT_MPEG_SIZE (152*1024)
47
48/* Encoder/decoder firmware sizes */
49#define IVTV_FW_ENC_SIZE (376836)
50#define IVTV_FW_DEC_SIZE (256*1024)
51
52static int load_fw_direct(const char *fn, volatile u8 __iomem *mem, struct ivtv *itv, long size)
53{
54 const struct firmware *fw = NULL;
55 int retries = 3;
56
57retry:
58 if (retries && request_firmware(&fw, fn, &itv->pdev->dev) == 0) {
59 int i;
60 volatile u32 __iomem *dst = (volatile u32 __iomem *)mem;
61 const u32 *src = (const u32 *)fw->data;
62
63 if (fw->size != size) {
64 /* Due to race conditions in firmware loading (esp. with udev <0.95)
65 the wrong file was sometimes loaded. So we check filesizes to
66 see if at least the right-sized file was loaded. If not, then we
67 retry. */
68 IVTV_INFO("Retry: file loaded was not %s (expected size %ld, got %zd)\n", fn, size, fw->size);
69 release_firmware(fw);
70 retries--;
71 goto retry;
72 }
73 for (i = 0; i < fw->size; i += 4) {
74 /* no need for endianness conversion on the ppc */
75 __raw_writel(*src, dst);
76 dst++;
77 src++;
78 }
79 IVTV_INFO("Loaded %s firmware (%zd bytes)\n", fn, fw->size);
80 release_firmware(fw);
81 return size;
82 }
83 IVTV_ERR("Unable to open firmware %s (must be %ld bytes)\n", fn, size);
84 IVTV_ERR("Did you put the firmware in the hotplug firmware directory?\n");
85 return -ENOMEM;
86}
87
88void ivtv_halt_firmware(struct ivtv *itv)
89{
90 IVTV_DEBUG_INFO("Preparing for firmware halt.\n");
91 if (itv->has_cx23415 && itv->dec_mbox.mbox)
92 ivtv_vapi(itv, CX2341X_DEC_HALT_FW, 0);
93 if (itv->enc_mbox.mbox)
94 ivtv_vapi(itv, CX2341X_ENC_HALT_FW, 0);
95
96 ivtv_msleep_timeout(10, 0);
97 itv->enc_mbox.mbox = itv->dec_mbox.mbox = NULL;
98
99 IVTV_DEBUG_INFO("Stopping VDM\n");
100 write_reg(IVTV_CMD_VDM_STOP, IVTV_REG_VDM);
101
102 IVTV_DEBUG_INFO("Stopping AO\n");
103 write_reg(IVTV_CMD_AO_STOP, IVTV_REG_AO);
104
105 IVTV_DEBUG_INFO("pinging (?) APU\n");
106 write_reg(IVTV_CMD_APU_PING, IVTV_REG_APU);
107
108 IVTV_DEBUG_INFO("Stopping VPU\n");
109 if (!itv->has_cx23415)
110 write_reg(IVTV_CMD_VPU_STOP16, IVTV_REG_VPU);
111 else
112 write_reg(IVTV_CMD_VPU_STOP15, IVTV_REG_VPU);
113
114 IVTV_DEBUG_INFO("Resetting Hw Blocks\n");
115 write_reg(IVTV_CMD_HW_BLOCKS_RST, IVTV_REG_HW_BLOCKS);
116
117 IVTV_DEBUG_INFO("Stopping SPU\n");
118 write_reg(IVTV_CMD_SPU_STOP, IVTV_REG_SPU);
119
120 ivtv_msleep_timeout(10, 0);
121
122 IVTV_DEBUG_INFO("init Encoder SDRAM pre-charge\n");
123 write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_ENC_SDRAM_PRECHARGE);
124
125 IVTV_DEBUG_INFO("init Encoder SDRAM refresh to 1us\n");
126 write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_ENC_SDRAM_REFRESH);
127
128 if (itv->has_cx23415) {
129 IVTV_DEBUG_INFO("init Decoder SDRAM pre-charge\n");
130 write_reg(IVTV_CMD_SDRAM_PRECHARGE_INIT, IVTV_REG_DEC_SDRAM_PRECHARGE);
131
132 IVTV_DEBUG_INFO("init Decoder SDRAM refresh to 1us\n");
133 write_reg(IVTV_CMD_SDRAM_REFRESH_INIT, IVTV_REG_DEC_SDRAM_REFRESH);
134 }
135
136 IVTV_DEBUG_INFO("Sleeping for %dms\n", IVTV_SDRAM_SLEEPTIME);
137 ivtv_msleep_timeout(IVTV_SDRAM_SLEEPTIME, 0);
138}
139
140void ivtv_firmware_versions(struct ivtv *itv)
141{
142 u32 data[CX2341X_MBOX_MAX_DATA];
143
144 /* Encoder */
145 ivtv_vapi_result(itv, data, CX2341X_ENC_GET_VERSION, 0);
146 IVTV_INFO("Encoder revision: 0x%08x\n", data[0]);
147
148 if (data[0] != 0x02060039)
149 IVTV_WARN("Recommended firmware version is 0x02060039.\n");
150
151 if (itv->has_cx23415) {
152 /* Decoder */
153 ivtv_vapi_result(itv, data, CX2341X_DEC_GET_VERSION, 0);
154 IVTV_INFO("Decoder revision: 0x%08x\n", data[0]);
155 }
156}
157
158static int ivtv_firmware_copy(struct ivtv *itv)
159{
160 IVTV_DEBUG_INFO("Loading encoder image\n");
161 if (load_fw_direct(CX2341X_FIRM_ENC_FILENAME,
162 itv->enc_mem, itv, IVTV_FW_ENC_SIZE) != IVTV_FW_ENC_SIZE) {
163 IVTV_DEBUG_WARN("failed loading encoder firmware\n");
164 return -3;
165 }
166 if (!itv->has_cx23415)
167 return 0;
168
169 IVTV_DEBUG_INFO("Loading decoder image\n");
170 if (load_fw_direct(CX2341X_FIRM_DEC_FILENAME,
171 itv->dec_mem, itv, IVTV_FW_DEC_SIZE) != IVTV_FW_DEC_SIZE) {
172 IVTV_DEBUG_WARN("failed loading decoder firmware\n");
173 return -1;
174 }
175 return 0;
176}
177
178static volatile struct ivtv_mailbox __iomem *ivtv_search_mailbox(const volatile u8 __iomem *mem, u32 size)
179{
180 int i;
181
182 /* mailbox is preceded by a 16 byte 'magic cookie' starting at a 256-byte
183 address boundary */
184 for (i = 0; i < size; i += 0x100) {
185 if (readl(mem + i) == 0x12345678 &&
186 readl(mem + i + 4) == 0x34567812 &&
187 readl(mem + i + 8) == 0x56781234 &&
188 readl(mem + i + 12) == 0x78123456) {
189 return (volatile struct ivtv_mailbox __iomem *)(mem + i + 16);
190 }
191 }
192 return NULL;
193}
194
195int ivtv_firmware_init(struct ivtv *itv)
196{
197 int err;
198
199 ivtv_halt_firmware(itv);
200
201 /* load firmware */
202 err = ivtv_firmware_copy(itv);
203 if (err) {
204 IVTV_DEBUG_WARN("Error %d loading firmware\n", err);
205 return err;
206 }
207
208 /* start firmware */
209 write_reg(read_reg(IVTV_REG_SPU) & IVTV_MASK_SPU_ENABLE, IVTV_REG_SPU);
210 ivtv_msleep_timeout(100, 0);
211 if (itv->has_cx23415)
212 write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE15, IVTV_REG_VPU);
213 else
214 write_reg(read_reg(IVTV_REG_VPU) & IVTV_MASK_VPU_ENABLE16, IVTV_REG_VPU);
215 ivtv_msleep_timeout(100, 0);
216
217 /* find mailboxes and ping firmware */
218 itv->enc_mbox.mbox = ivtv_search_mailbox(itv->enc_mem, IVTV_ENCODER_SIZE);
219 if (itv->enc_mbox.mbox == NULL)
220 IVTV_ERR("Encoder mailbox not found\n");
221 else if (ivtv_vapi(itv, CX2341X_ENC_PING_FW, 0)) {
222 IVTV_ERR("Encoder firmware dead!\n");
223 itv->enc_mbox.mbox = NULL;
224 }
225 if (itv->enc_mbox.mbox == NULL)
226 return -ENODEV;
227
228 if (!itv->has_cx23415)
229 return 0;
230
231 itv->dec_mbox.mbox = ivtv_search_mailbox(itv->dec_mem, IVTV_DECODER_SIZE);
232 if (itv->dec_mbox.mbox == NULL) {
233 IVTV_ERR("Decoder mailbox not found\n");
234 } else if (itv->has_cx23415 && ivtv_vapi(itv, CX2341X_DEC_PING_FW, 0)) {
235 IVTV_ERR("Decoder firmware dead!\n");
236 itv->dec_mbox.mbox = NULL;
237 } else {
238 /* Firmware okay, so check yuv output filter table */
239 ivtv_yuv_filter_check(itv);
240 }
241 return itv->dec_mbox.mbox ? 0 : -ENODEV;
242}
243
244void ivtv_init_mpeg_decoder(struct ivtv *itv)
245{
246 u32 data[CX2341X_MBOX_MAX_DATA];
247 long readbytes;
248 volatile u8 __iomem *mem_offset;
249
250 data[0] = 0;
251 data[1] = itv->cxhdl.width; /* YUV source width */
252 data[2] = itv->cxhdl.height;
253 data[3] = itv->cxhdl.audio_properties; /* Audio settings to use,
254 bitmap. see docs. */
255 if (ivtv_api(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, data)) {
256 IVTV_ERR("ivtv_init_mpeg_decoder failed to set decoder source\n");
257 return;
258 }
259
260 if (ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1) != 0) {
261 IVTV_ERR("ivtv_init_mpeg_decoder failed to start playback\n");
262 return;
263 }
264 ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, 2, data);
265 mem_offset = itv->dec_mem + data[1];
266
267 if ((readbytes = load_fw_direct(IVTV_DECODE_INIT_MPEG_FILENAME,
268 mem_offset, itv, IVTV_DECODE_INIT_MPEG_SIZE)) <= 0) {
269 IVTV_DEBUG_WARN("failed to read mpeg decoder initialisation file %s\n",
270 IVTV_DECODE_INIT_MPEG_FILENAME);
271 } else {
272 ivtv_vapi(itv, CX2341X_DEC_SCHED_DMA_FROM_HOST, 3, 0, readbytes, 0);
273 ivtv_msleep_timeout(100, 0);
274 }
275 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 4, 0, 0, 0, 1);
276}
277
278/* Try to restart the card & restore previous settings */
279int ivtv_firmware_restart(struct ivtv *itv)
280{
281 int rc = 0;
282 v4l2_std_id std;
283
284 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
285 /* Display test image during restart */
286 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
287 SAA7127_INPUT_TYPE_TEST_IMAGE,
288 itv->card->video_outputs[itv->active_output].video_output,
289 0);
290
291 mutex_lock(&itv->udma.lock);
292
293 rc = ivtv_firmware_init(itv);
294 if (rc) {
295 mutex_unlock(&itv->udma.lock);
296 return rc;
297 }
298
299 /* Allow settings to reload */
300 ivtv_mailbox_cache_invalidate(itv);
301
302 /* Restore encoder video standard */
303 std = itv->std;
304 itv->std = 0;
305 ivtv_s_std_enc(itv, &std);
306
307 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
308 ivtv_init_mpeg_decoder(itv);
309
310 /* Restore decoder video standard */
311 std = itv->std_out;
312 itv->std_out = 0;
313 ivtv_s_std_dec(itv, &std);
314
315 /* Restore framebuffer if active */
316 if (itv->ivtvfb_restore)
317 itv->ivtvfb_restore(itv);
318
319 /* Restore alpha settings */
320 ivtv_set_osd_alpha(itv);
321
322 /* Restore normal output */
323 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
324 SAA7127_INPUT_TYPE_NORMAL,
325 itv->card->video_outputs[itv->active_output].video_output,
326 0);
327 }
328
329 mutex_unlock(&itv->udma.lock);
330 return rc;
331}
332
333/* Check firmware running state. The checks fall through
334 allowing multiple failures to be logged. */
335int ivtv_firmware_check(struct ivtv *itv, char *where)
336{
337 int res = 0;
338
339 /* Check encoder is still running */
340 if (ivtv_vapi(itv, CX2341X_ENC_PING_FW, 0) < 0) {
341 IVTV_WARN("Encoder has died : %s\n", where);
342 res = -1;
343 }
344
345 /* Also check audio. Only check if not in use & encoder is okay */
346 if (!res && !atomic_read(&itv->capturing) &&
347 (!atomic_read(&itv->decoding) ||
348 (atomic_read(&itv->decoding) < 2 && test_bit(IVTV_F_I_DEC_YUV,
349 &itv->i_flags)))) {
350
351 if (ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12) < 0) {
352 IVTV_WARN("Audio has died (Encoder OK) : %s\n", where);
353 res = -2;
354 }
355 }
356
357 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
358 /* Second audio check. Skip if audio already failed */
359 if (res != -2 && read_dec(0x100) != read_dec(0x104)) {
360 /* Wait & try again to be certain. */
361 ivtv_msleep_timeout(14, 0);
362 if (read_dec(0x100) != read_dec(0x104)) {
363 IVTV_WARN("Audio has died (Decoder) : %s\n",
364 where);
365 res = -1;
366 }
367 }
368
369 /* Check decoder is still running */
370 if (ivtv_vapi(itv, CX2341X_DEC_PING_FW, 0) < 0) {
371 IVTV_WARN("Decoder has died : %s\n", where);
372 res = -1;
373 }
374 }
375
376 /* If something failed & currently idle, try to reload */
377 if (res && !atomic_read(&itv->capturing) &&
378 !atomic_read(&itv->decoding)) {
379 IVTV_INFO("Detected in %s that firmware had failed - "
380 "Reloading\n", where);
381 res = ivtv_firmware_restart(itv);
382 /*
383 * Even if restarted ok, still signal a problem had occurred.
384 * The caller can come through this function again to check
385 * if things are really ok after the restart.
386 */
387 if (!res) {
388 IVTV_INFO("Firmware restart okay\n");
389 res = -EAGAIN;
390 } else {
391 IVTV_INFO("Firmware restart failed\n");
392 }
393 } else if (res) {
394 res = -EIO;
395 }
396
397 return res;
398}
399
400MODULE_FIRMWARE(CX2341X_FIRM_ENC_FILENAME);
401MODULE_FIRMWARE(CX2341X_FIRM_DEC_FILENAME);
402MODULE_FIRMWARE(IVTV_DECODE_INIT_MPEG_FILENAME);
diff --git a/drivers/media/pci/ivtv/ivtv-firmware.h b/drivers/media/pci/ivtv/ivtv-firmware.h
new file mode 100644
index 000000000000..52bb4e5598fd
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-firmware.h
@@ -0,0 +1,31 @@
1/*
2 ivtv firmware functions.
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#ifndef IVTV_FIRMWARE_H
23#define IVTV_FIRMWARE_H
24
25int ivtv_firmware_init(struct ivtv *itv);
26void ivtv_firmware_versions(struct ivtv *itv);
27void ivtv_halt_firmware(struct ivtv *itv);
28void ivtv_init_mpeg_decoder(struct ivtv *itv);
29int ivtv_firmware_check(struct ivtv *itv, char *where);
30
31#endif
diff --git a/drivers/media/pci/ivtv/ivtv-gpio.c b/drivers/media/pci/ivtv/ivtv-gpio.c
new file mode 100644
index 000000000000..8f0d07789053
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-gpio.c
@@ -0,0 +1,374 @@
1/*
2 gpio functions.
3 Merging GPIO support into driver:
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include "ivtv-driver.h"
23#include "ivtv-cards.h"
24#include "ivtv-gpio.h"
25#include "tuner-xc2028.h"
26#include <media/tuner.h>
27#include <media/v4l2-ctrls.h>
28
29/*
30 * GPIO assignment of Yuan MPG600/MPG160
31 *
32 * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
33 * OUTPUT IN1 IN0 AM3 AM2 AM1 AM0
34 * INPUT DM1 DM0
35 *
36 * IN* : Input selection
37 * IN1 IN0
38 * 1 1 N/A
39 * 1 0 Line
40 * 0 1 N/A
41 * 0 0 Tuner
42 *
43 * AM* : Audio Mode
44 * AM3 0: Normal 1: Mixed(Sub+Main channel)
45 * AM2 0: Subchannel 1: Main channel
46 * AM1 0: Stereo 1: Mono
47 * AM0 0: Normal 1: Mute
48 *
49 * DM* : Detected tuner audio Mode
50 * DM1 0: Stereo 1: Mono
51 * DM0 0: Multiplex 1: Normal
52 *
53 * GPIO Initial Settings
54 * MPG600 MPG160
55 * DIR 0x3080 0x7080
56 * OUTPUT 0x000C 0x400C
57 *
58 * Special thanks to Makoto Iguchi <iguchi@tahoo.org> and Mr. Anonymous
59 * for analyzing GPIO of MPG160.
60 *
61 *****************************************************************************
62 *
63 * GPIO assignment of Avermedia M179 (per information direct from AVerMedia)
64 *
65 * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
66 * OUTPUT IN0 AM0 IN1 AM1 AM2 IN2 BR0 BR1
67 * INPUT
68 *
69 * IN* : Input selection
70 * IN0 IN1 IN2
71 * * 1 * Mute
72 * 0 0 0 Line-In
73 * 1 0 0 TV Tuner Audio
74 * 0 0 1 FM Audio
75 * 1 0 1 Mute
76 *
77 * AM* : Audio Mode
78 * AM0 AM1 AM2
79 * 0 0 0 TV Tuner Audio: L_OUT=(L+R)/2, R_OUT=SAP
80 * 0 0 1 TV Tuner Audio: L_OUT=R_OUT=SAP (SAP)
81 * 0 1 0 TV Tuner Audio: L_OUT=L, R_OUT=R (stereo)
82 * 0 1 1 TV Tuner Audio: mute
83 * 1 * * TV Tuner Audio: L_OUT=R_OUT=(L+R)/2 (mono)
84 *
85 * BR* : Audio Sample Rate (BR stands for bitrate for some reason)
86 * BR0 BR1
87 * 0 0 32 kHz
88 * 0 1 44.1 kHz
89 * 1 0 48 kHz
90 *
91 * DM* : Detected tuner audio Mode
92 * Unknown currently
93 *
94 * Special thanks to AVerMedia Technologies, Inc. and Jiun-Kuei Jung at
95 * AVerMedia for providing the GPIO information used to add support
96 * for the M179 cards.
97 */
98
99/********************* GPIO stuffs *********************/
100
101/* GPIO registers */
102#define IVTV_REG_GPIO_IN 0x9008
103#define IVTV_REG_GPIO_OUT 0x900c
104#define IVTV_REG_GPIO_DIR 0x9020
105
106void ivtv_reset_ir_gpio(struct ivtv *itv)
107{
108 int curdir, curout;
109
110 if (itv->card->type != IVTV_CARD_PVR_150)
111 return;
112 IVTV_DEBUG_INFO("Resetting PVR150 IR\n");
113 curout = read_reg(IVTV_REG_GPIO_OUT);
114 curdir = read_reg(IVTV_REG_GPIO_DIR);
115 curdir |= 0x80;
116 write_reg(curdir, IVTV_REG_GPIO_DIR);
117 curout = (curout & ~0xF) | 1;
118 write_reg(curout, IVTV_REG_GPIO_OUT);
119 /* We could use something else for smaller time */
120 schedule_timeout_interruptible(msecs_to_jiffies(1));
121 curout |= 2;
122 write_reg(curout, IVTV_REG_GPIO_OUT);
123 curdir &= ~0x80;
124 write_reg(curdir, IVTV_REG_GPIO_DIR);
125}
126
127/* Xceive tuner reset function */
128int ivtv_reset_tuner_gpio(void *dev, int component, int cmd, int value)
129{
130 struct i2c_algo_bit_data *algo = dev;
131 struct ivtv *itv = algo->data;
132 u32 curout;
133
134 if (cmd != XC2028_TUNER_RESET)
135 return 0;
136 IVTV_DEBUG_INFO("Resetting tuner\n");
137 curout = read_reg(IVTV_REG_GPIO_OUT);
138 curout &= ~(1 << itv->card->xceive_pin);
139 write_reg(curout, IVTV_REG_GPIO_OUT);
140 schedule_timeout_interruptible(msecs_to_jiffies(1));
141
142 curout |= 1 << itv->card->xceive_pin;
143 write_reg(curout, IVTV_REG_GPIO_OUT);
144 schedule_timeout_interruptible(msecs_to_jiffies(1));
145 return 0;
146}
147
148static inline struct ivtv *sd_to_ivtv(struct v4l2_subdev *sd)
149{
150 return container_of(sd, struct ivtv, sd_gpio);
151}
152
153static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
154{
155 return &container_of(ctrl->handler, struct ivtv, hdl_gpio)->sd_gpio;
156}
157
158static int subdev_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
159{
160 struct ivtv *itv = sd_to_ivtv(sd);
161 u16 mask, data;
162
163 mask = itv->card->gpio_audio_freq.mask;
164 switch (freq) {
165 case 32000:
166 data = itv->card->gpio_audio_freq.f32000;
167 break;
168 case 44100:
169 data = itv->card->gpio_audio_freq.f44100;
170 break;
171 case 48000:
172 default:
173 data = itv->card->gpio_audio_freq.f48000;
174 break;
175 }
176 if (mask)
177 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
178 return 0;
179}
180
181static int subdev_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
182{
183 struct ivtv *itv = sd_to_ivtv(sd);
184 u16 mask;
185
186 mask = itv->card->gpio_audio_detect.mask;
187 if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))
188 vt->rxsubchans = V4L2_TUNER_SUB_STEREO |
189 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
190 else
191 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
192 return 0;
193}
194
195static int subdev_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
196{
197 struct ivtv *itv = sd_to_ivtv(sd);
198 u16 mask, data;
199
200 mask = itv->card->gpio_audio_mode.mask;
201 switch (vt->audmode) {
202 case V4L2_TUNER_MODE_LANG1:
203 data = itv->card->gpio_audio_mode.lang1;
204 break;
205 case V4L2_TUNER_MODE_LANG2:
206 data = itv->card->gpio_audio_mode.lang2;
207 break;
208 case V4L2_TUNER_MODE_MONO:
209 data = itv->card->gpio_audio_mode.mono;
210 break;
211 case V4L2_TUNER_MODE_STEREO:
212 case V4L2_TUNER_MODE_LANG1_LANG2:
213 default:
214 data = itv->card->gpio_audio_mode.stereo;
215 break;
216 }
217 if (mask)
218 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
219 return 0;
220}
221
222static int subdev_s_radio(struct v4l2_subdev *sd)
223{
224 struct ivtv *itv = sd_to_ivtv(sd);
225 u16 mask, data;
226
227 mask = itv->card->gpio_audio_input.mask;
228 data = itv->card->gpio_audio_input.radio;
229 if (mask)
230 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
231 return 0;
232}
233
234static int subdev_s_audio_routing(struct v4l2_subdev *sd,
235 u32 input, u32 output, u32 config)
236{
237 struct ivtv *itv = sd_to_ivtv(sd);
238 u16 mask, data;
239
240 if (input > 2)
241 return -EINVAL;
242 mask = itv->card->gpio_audio_input.mask;
243 switch (input) {
244 case 0:
245 data = itv->card->gpio_audio_input.tuner;
246 break;
247 case 1:
248 data = itv->card->gpio_audio_input.linein;
249 break;
250 case 2:
251 default:
252 data = itv->card->gpio_audio_input.radio;
253 break;
254 }
255 if (mask)
256 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
257 return 0;
258}
259
260static int subdev_s_ctrl(struct v4l2_ctrl *ctrl)
261{
262 struct v4l2_subdev *sd = to_sd(ctrl);
263 struct ivtv *itv = sd_to_ivtv(sd);
264 u16 mask, data;
265
266 switch (ctrl->id) {
267 case V4L2_CID_AUDIO_MUTE:
268 mask = itv->card->gpio_audio_mute.mask;
269 data = ctrl->val ? itv->card->gpio_audio_mute.mute : 0;
270 if (mask)
271 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) |
272 (data & mask), IVTV_REG_GPIO_OUT);
273 return 0;
274 }
275 return -EINVAL;
276}
277
278
279static int subdev_log_status(struct v4l2_subdev *sd)
280{
281 struct ivtv *itv = sd_to_ivtv(sd);
282
283 IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
284 read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
285 read_reg(IVTV_REG_GPIO_IN));
286 v4l2_ctrl_handler_log_status(&itv->hdl_gpio, sd->name);
287 return 0;
288}
289
290static int subdev_s_video_routing(struct v4l2_subdev *sd,
291 u32 input, u32 output, u32 config)
292{
293 struct ivtv *itv = sd_to_ivtv(sd);
294 u16 mask, data;
295
296 if (input > 2) /* 0:Tuner 1:Composite 2:S-Video */
297 return -EINVAL;
298 mask = itv->card->gpio_video_input.mask;
299 if (input == 0)
300 data = itv->card->gpio_video_input.tuner;
301 else if (input == 1)
302 data = itv->card->gpio_video_input.composite;
303 else
304 data = itv->card->gpio_video_input.svideo;
305 if (mask)
306 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
307 return 0;
308}
309
310static const struct v4l2_ctrl_ops gpio_ctrl_ops = {
311 .s_ctrl = subdev_s_ctrl,
312};
313
314static const struct v4l2_subdev_core_ops subdev_core_ops = {
315 .log_status = subdev_log_status,
316 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
317 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
318 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
319 .g_ctrl = v4l2_subdev_g_ctrl,
320 .s_ctrl = v4l2_subdev_s_ctrl,
321 .queryctrl = v4l2_subdev_queryctrl,
322 .querymenu = v4l2_subdev_querymenu,
323};
324
325static const struct v4l2_subdev_tuner_ops subdev_tuner_ops = {
326 .s_radio = subdev_s_radio,
327 .g_tuner = subdev_g_tuner,
328 .s_tuner = subdev_s_tuner,
329};
330
331static const struct v4l2_subdev_audio_ops subdev_audio_ops = {
332 .s_clock_freq = subdev_s_clock_freq,
333 .s_routing = subdev_s_audio_routing,
334};
335
336static const struct v4l2_subdev_video_ops subdev_video_ops = {
337 .s_routing = subdev_s_video_routing,
338};
339
340static const struct v4l2_subdev_ops subdev_ops = {
341 .core = &subdev_core_ops,
342 .tuner = &subdev_tuner_ops,
343 .audio = &subdev_audio_ops,
344 .video = &subdev_video_ops,
345};
346
347int ivtv_gpio_init(struct ivtv *itv)
348{
349 u16 pin = 0;
350
351 if (itv->card->xceive_pin)
352 pin = 1 << itv->card->xceive_pin;
353
354 if ((itv->card->gpio_init.direction | pin) == 0)
355 return 0;
356
357 IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
358 read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));
359
360 /* init output data then direction */
361 write_reg(itv->card->gpio_init.initial_value | pin, IVTV_REG_GPIO_OUT);
362 write_reg(itv->card->gpio_init.direction | pin, IVTV_REG_GPIO_DIR);
363 v4l2_subdev_init(&itv->sd_gpio, &subdev_ops);
364 snprintf(itv->sd_gpio.name, sizeof(itv->sd_gpio.name), "%s-gpio", itv->v4l2_dev.name);
365 itv->sd_gpio.grp_id = IVTV_HW_GPIO;
366 v4l2_ctrl_handler_init(&itv->hdl_gpio, 1);
367 v4l2_ctrl_new_std(&itv->hdl_gpio, &gpio_ctrl_ops,
368 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
369 if (itv->hdl_gpio.error)
370 return itv->hdl_gpio.error;
371 itv->sd_gpio.ctrl_handler = &itv->hdl_gpio;
372 v4l2_ctrl_handler_setup(&itv->hdl_gpio);
373 return v4l2_device_register_subdev(&itv->v4l2_dev, &itv->sd_gpio);
374}
diff --git a/drivers/media/pci/ivtv/ivtv-gpio.h b/drivers/media/pci/ivtv/ivtv-gpio.h
new file mode 100644
index 000000000000..0b5d19c8ecb4
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-gpio.h
@@ -0,0 +1,29 @@
1/*
2 gpio functions.
3 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_GPIO_H
22#define IVTV_GPIO_H
23
24/* GPIO stuff */
25int ivtv_gpio_init(struct ivtv *itv);
26void ivtv_reset_ir_gpio(struct ivtv *itv);
27int ivtv_reset_tuner_gpio(void *dev, int component, int cmd, int value);
28
29#endif
diff --git a/drivers/media/pci/ivtv/ivtv-i2c.c b/drivers/media/pci/ivtv/ivtv-i2c.c
new file mode 100644
index 000000000000..d47f41a0ef66
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-i2c.c
@@ -0,0 +1,760 @@
1/*
2 I2C functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 This file includes an i2c implementation that was reverse engineered
23 from the Hauppauge windows driver. Older ivtv versions used i2c-algo-bit,
24 which whilst fine under most circumstances, had trouble with the Zilog
25 CPU on the PVR-150 which handles IR functions (occasional inability to
26 communicate with the chip until it was reset) and also with the i2c
27 bus being completely unreachable when multiple PVR cards were present.
28
29 The implementation is very similar to i2c-algo-bit, but there are enough
30 subtle differences that the two are hard to merge. The general strategy
31 employed by i2c-algo-bit is to use udelay() to implement the timing
32 when putting out bits on the scl/sda lines. The general strategy taken
33 here is to poll the lines for state changes (see ivtv_waitscl and
34 ivtv_waitsda). In addition there are small delays at various locations
35 which poll the SCL line 5 times (ivtv_scldelay). I would guess that
36 since this is memory mapped I/O that the length of those delays is tied
37 to the PCI bus clock. There is some extra code to do with recovery
38 and retries. Since it is not known what causes the actual i2c problems
39 in the first place, the only goal if one was to attempt to use
40 i2c-algo-bit would be to try to make it follow the same code path.
41 This would be a lot of work, and I'm also not convinced that it would
42 provide a generic benefit to i2c-algo-bit. Therefore consider this
43 an engineering solution -- not pretty, but it works.
44
45 Some more general comments about what we are doing:
46
47 The i2c bus is a 2 wire serial bus, with clock (SCL) and data (SDA)
48 lines. To communicate on the bus (as a master, we don't act as a slave),
49 we first initiate a start condition (ivtv_start). We then write the
50 address of the device that we want to communicate with, along with a flag
51 that indicates whether this is a read or a write. The slave then issues
52 an ACK signal (ivtv_ack), which tells us that it is ready for reading /
53 writing. We then proceed with reading or writing (ivtv_read/ivtv_write),
54 and finally issue a stop condition (ivtv_stop) to make the bus available
55 to other masters.
56
57 There is an additional form of transaction where a write may be
58 immediately followed by a read. In this case, there is no intervening
59 stop condition. (Only the msp3400 chip uses this method of data transfer).
60 */
61
62#include "ivtv-driver.h"
63#include "ivtv-cards.h"
64#include "ivtv-gpio.h"
65#include "ivtv-i2c.h"
66#include <media/cx25840.h>
67
68/* i2c implementation for cx23415/6 chip, ivtv project.
69 * Author: Kevin Thayer (nufan_wfk at yahoo.com)
70 */
71/* i2c stuff */
72#define IVTV_REG_I2C_SETSCL_OFFSET 0x7000
73#define IVTV_REG_I2C_SETSDA_OFFSET 0x7004
74#define IVTV_REG_I2C_GETSCL_OFFSET 0x7008
75#define IVTV_REG_I2C_GETSDA_OFFSET 0x700c
76
77#define IVTV_CS53L32A_I2C_ADDR 0x11
78#define IVTV_M52790_I2C_ADDR 0x48
79#define IVTV_CX25840_I2C_ADDR 0x44
80#define IVTV_SAA7115_I2C_ADDR 0x21
81#define IVTV_SAA7127_I2C_ADDR 0x44
82#define IVTV_SAA717x_I2C_ADDR 0x21
83#define IVTV_MSP3400_I2C_ADDR 0x40
84#define IVTV_HAUPPAUGE_I2C_ADDR 0x50
85#define IVTV_WM8739_I2C_ADDR 0x1a
86#define IVTV_WM8775_I2C_ADDR 0x1b
87#define IVTV_TEA5767_I2C_ADDR 0x60
88#define IVTV_UPD64031A_I2C_ADDR 0x12
89#define IVTV_UPD64083_I2C_ADDR 0x5c
90#define IVTV_VP27SMPX_I2C_ADDR 0x5b
91#define IVTV_M52790_I2C_ADDR 0x48
92#define IVTV_AVERMEDIA_IR_RX_I2C_ADDR 0x40
93#define IVTV_HAUP_EXT_IR_RX_I2C_ADDR 0x1a
94#define IVTV_HAUP_INT_IR_RX_I2C_ADDR 0x18
95#define IVTV_Z8F0811_IR_TX_I2C_ADDR 0x70
96#define IVTV_Z8F0811_IR_RX_I2C_ADDR 0x71
97#define IVTV_ADAPTEC_IR_ADDR 0x6b
98
99/* This array should match the IVTV_HW_ defines */
100static const u8 hw_addrs[] = {
101 IVTV_CX25840_I2C_ADDR,
102 IVTV_SAA7115_I2C_ADDR,
103 IVTV_SAA7127_I2C_ADDR,
104 IVTV_MSP3400_I2C_ADDR,
105 0,
106 IVTV_WM8775_I2C_ADDR,
107 IVTV_CS53L32A_I2C_ADDR,
108 0,
109 IVTV_SAA7115_I2C_ADDR,
110 IVTV_UPD64031A_I2C_ADDR,
111 IVTV_UPD64083_I2C_ADDR,
112 IVTV_SAA717x_I2C_ADDR,
113 IVTV_WM8739_I2C_ADDR,
114 IVTV_VP27SMPX_I2C_ADDR,
115 IVTV_M52790_I2C_ADDR,
116 0, /* IVTV_HW_GPIO dummy driver ID */
117 IVTV_AVERMEDIA_IR_RX_I2C_ADDR, /* IVTV_HW_I2C_IR_RX_AVER */
118 IVTV_HAUP_EXT_IR_RX_I2C_ADDR, /* IVTV_HW_I2C_IR_RX_HAUP_EXT */
119 IVTV_HAUP_INT_IR_RX_I2C_ADDR, /* IVTV_HW_I2C_IR_RX_HAUP_INT */
120 IVTV_Z8F0811_IR_TX_I2C_ADDR, /* IVTV_HW_Z8F0811_IR_TX_HAUP */
121 IVTV_Z8F0811_IR_RX_I2C_ADDR, /* IVTV_HW_Z8F0811_IR_RX_HAUP */
122 IVTV_ADAPTEC_IR_ADDR, /* IVTV_HW_I2C_IR_RX_ADAPTEC */
123};
124
125/* This array should match the IVTV_HW_ defines */
126static const char * const hw_devicenames[] = {
127 "cx25840",
128 "saa7115",
129 "saa7127_auto", /* saa7127 or saa7129 */
130 "msp3400",
131 "tuner",
132 "wm8775",
133 "cs53l32a",
134 "tveeprom",
135 "saa7114",
136 "upd64031a",
137 "upd64083",
138 "saa717x",
139 "wm8739",
140 "vp27smpx",
141 "m52790",
142 "gpio",
143 "ir_video", /* IVTV_HW_I2C_IR_RX_AVER */
144 "ir_video", /* IVTV_HW_I2C_IR_RX_HAUP_EXT */
145 "ir_video", /* IVTV_HW_I2C_IR_RX_HAUP_INT */
146 "ir_tx_z8f0811_haup", /* IVTV_HW_Z8F0811_IR_TX_HAUP */
147 "ir_rx_z8f0811_haup", /* IVTV_HW_Z8F0811_IR_RX_HAUP */
148 "ir_video", /* IVTV_HW_I2C_IR_RX_ADAPTEC */
149};
150
151static int get_key_adaptec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
152{
153 unsigned char keybuf[4];
154
155 keybuf[0] = 0x00;
156 i2c_master_send(ir->c, keybuf, 1);
157 /* poll IR chip */
158 if (i2c_master_recv(ir->c, keybuf, sizeof(keybuf)) != sizeof(keybuf)) {
159 return 0;
160 }
161
162 /* key pressed ? */
163 if (keybuf[2] == 0xff)
164 return 0;
165
166 /* remove repeat bit */
167 keybuf[2] &= 0x7f;
168 keybuf[3] |= 0x80;
169
170 *ir_key = keybuf[3] | keybuf[2] << 8 | keybuf[1] << 16 |keybuf[0] << 24;
171 *ir_raw = *ir_key;
172
173 return 1;
174}
175
176static int ivtv_i2c_new_ir(struct ivtv *itv, u32 hw, const char *type, u8 addr)
177{
178 struct i2c_board_info info;
179 struct i2c_adapter *adap = &itv->i2c_adap;
180 struct IR_i2c_init_data *init_data = &itv->ir_i2c_init_data;
181 unsigned short addr_list[2] = { addr, I2C_CLIENT_END };
182
183 /* Only allow one IR transmitter to be registered per board */
184 if (hw & IVTV_HW_IR_TX_ANY) {
185 if (itv->hw_flags & IVTV_HW_IR_TX_ANY)
186 return -1;
187 memset(&info, 0, sizeof(struct i2c_board_info));
188 strlcpy(info.type, type, I2C_NAME_SIZE);
189 return i2c_new_probed_device(adap, &info, addr_list, NULL)
190 == NULL ? -1 : 0;
191 }
192
193 /* Only allow one IR receiver to be registered per board */
194 if (itv->hw_flags & IVTV_HW_IR_RX_ANY)
195 return -1;
196
197 /* Our default information for ir-kbd-i2c.c to use */
198 switch (hw) {
199 case IVTV_HW_I2C_IR_RX_AVER:
200 init_data->ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
201 init_data->internal_get_key_func =
202 IR_KBD_GET_KEY_AVERMEDIA_CARDBUS;
203 init_data->type = RC_TYPE_OTHER;
204 init_data->name = "AVerMedia AVerTV card";
205 break;
206 case IVTV_HW_I2C_IR_RX_HAUP_EXT:
207 case IVTV_HW_I2C_IR_RX_HAUP_INT:
208 init_data->ir_codes = RC_MAP_HAUPPAUGE;
209 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP;
210 init_data->type = RC_TYPE_RC5;
211 init_data->name = itv->card_name;
212 break;
213 case IVTV_HW_Z8F0811_IR_RX_HAUP:
214 /* Default to grey remote */
215 init_data->ir_codes = RC_MAP_HAUPPAUGE;
216 init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
217 init_data->type = RC_TYPE_RC5;
218 init_data->name = itv->card_name;
219 break;
220 case IVTV_HW_I2C_IR_RX_ADAPTEC:
221 init_data->get_key = get_key_adaptec;
222 init_data->name = itv->card_name;
223 /* FIXME: The protocol and RC_MAP needs to be corrected */
224 init_data->ir_codes = RC_MAP_EMPTY;
225 init_data->type = RC_TYPE_UNKNOWN;
226 break;
227 }
228
229 memset(&info, 0, sizeof(struct i2c_board_info));
230 info.platform_data = init_data;
231 strlcpy(info.type, type, I2C_NAME_SIZE);
232
233 return i2c_new_probed_device(adap, &info, addr_list, NULL) == NULL ?
234 -1 : 0;
235}
236
237/* Instantiate the IR receiver device using probing -- undesirable */
238struct i2c_client *ivtv_i2c_new_ir_legacy(struct ivtv *itv)
239{
240 struct i2c_board_info info;
241 /*
242 * The external IR receiver is at i2c address 0x34.
243 * The internal IR receiver is at i2c address 0x30.
244 *
245 * In theory, both can be fitted, and Hauppauge suggests an external
246 * overrides an internal. That's why we probe 0x1a (~0x34) first. CB
247 *
248 * Some of these addresses we probe may collide with other i2c address
249 * allocations, so this function must be called after all other i2c
250 * devices we care about are registered.
251 */
252 const unsigned short addr_list[] = {
253 0x1a, /* Hauppauge IR external - collides with WM8739 */
254 0x18, /* Hauppauge IR internal */
255 I2C_CLIENT_END
256 };
257
258 memset(&info, 0, sizeof(struct i2c_board_info));
259 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
260 return i2c_new_probed_device(&itv->i2c_adap, &info, addr_list, NULL);
261}
262
263int ivtv_i2c_register(struct ivtv *itv, unsigned idx)
264{
265 struct v4l2_subdev *sd;
266 struct i2c_adapter *adap = &itv->i2c_adap;
267 const char *type = hw_devicenames[idx];
268 u32 hw = 1 << idx;
269
270 if (idx >= ARRAY_SIZE(hw_addrs))
271 return -1;
272 if (hw == IVTV_HW_TUNER) {
273 /* special tuner handling */
274 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,
275 itv->card_i2c->radio);
276 if (sd)
277 sd->grp_id = 1 << idx;
278 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,
279 itv->card_i2c->demod);
280 if (sd)
281 sd->grp_id = 1 << idx;
282 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,
283 itv->card_i2c->tv);
284 if (sd)
285 sd->grp_id = 1 << idx;
286 return sd ? 0 : -1;
287 }
288
289 if (hw & IVTV_HW_IR_ANY)
290 return ivtv_i2c_new_ir(itv, hw, type, hw_addrs[idx]);
291
292 /* Is it not an I2C device or one we do not wish to register? */
293 if (!hw_addrs[idx])
294 return -1;
295
296 /* It's an I2C device other than an analog tuner or IR chip */
297 if (hw == IVTV_HW_UPD64031A || hw == IVTV_HW_UPD6408X) {
298 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev,
299 adap, type, 0, I2C_ADDRS(hw_addrs[idx]));
300 } else if (hw == IVTV_HW_CX25840) {
301 struct cx25840_platform_data pdata;
302 struct i2c_board_info cx25840_info = {
303 .type = "cx25840",
304 .addr = hw_addrs[idx],
305 .platform_data = &pdata,
306 };
307
308 pdata.pvr150_workaround = itv->pvr150_workaround;
309 sd = v4l2_i2c_new_subdev_board(&itv->v4l2_dev, adap,
310 &cx25840_info, NULL);
311 } else {
312 sd = v4l2_i2c_new_subdev(&itv->v4l2_dev,
313 adap, type, hw_addrs[idx], NULL);
314 }
315 if (sd)
316 sd->grp_id = 1 << idx;
317 return sd ? 0 : -1;
318}
319
320struct v4l2_subdev *ivtv_find_hw(struct ivtv *itv, u32 hw)
321{
322 struct v4l2_subdev *result = NULL;
323 struct v4l2_subdev *sd;
324
325 spin_lock(&itv->v4l2_dev.lock);
326 v4l2_device_for_each_subdev(sd, &itv->v4l2_dev) {
327 if (sd->grp_id == hw) {
328 result = sd;
329 break;
330 }
331 }
332 spin_unlock(&itv->v4l2_dev.lock);
333 return result;
334}
335
336/* Set the serial clock line to the desired state */
337static void ivtv_setscl(struct ivtv *itv, int state)
338{
339 /* write them out */
340 /* write bits are inverted */
341 write_reg(~state, IVTV_REG_I2C_SETSCL_OFFSET);
342}
343
344/* Set the serial data line to the desired state */
345static void ivtv_setsda(struct ivtv *itv, int state)
346{
347 /* write them out */
348 /* write bits are inverted */
349 write_reg(~state & 1, IVTV_REG_I2C_SETSDA_OFFSET);
350}
351
352/* Read the serial clock line */
353static int ivtv_getscl(struct ivtv *itv)
354{
355 return read_reg(IVTV_REG_I2C_GETSCL_OFFSET) & 1;
356}
357
358/* Read the serial data line */
359static int ivtv_getsda(struct ivtv *itv)
360{
361 return read_reg(IVTV_REG_I2C_GETSDA_OFFSET) & 1;
362}
363
364/* Implement a short delay by polling the serial clock line */
365static void ivtv_scldelay(struct ivtv *itv)
366{
367 int i;
368
369 for (i = 0; i < 5; ++i)
370 ivtv_getscl(itv);
371}
372
373/* Wait for the serial clock line to become set to a specific value */
374static int ivtv_waitscl(struct ivtv *itv, int val)
375{
376 int i;
377
378 ivtv_scldelay(itv);
379 for (i = 0; i < 1000; ++i) {
380 if (ivtv_getscl(itv) == val)
381 return 1;
382 }
383 return 0;
384}
385
386/* Wait for the serial data line to become set to a specific value */
387static int ivtv_waitsda(struct ivtv *itv, int val)
388{
389 int i;
390
391 ivtv_scldelay(itv);
392 for (i = 0; i < 1000; ++i) {
393 if (ivtv_getsda(itv) == val)
394 return 1;
395 }
396 return 0;
397}
398
399/* Wait for the slave to issue an ACK */
400static int ivtv_ack(struct ivtv *itv)
401{
402 int ret = 0;
403
404 if (ivtv_getscl(itv) == 1) {
405 IVTV_DEBUG_HI_I2C("SCL was high starting an ack\n");
406 ivtv_setscl(itv, 0);
407 if (!ivtv_waitscl(itv, 0)) {
408 IVTV_DEBUG_I2C("Could not set SCL low starting an ack\n");
409 return -EREMOTEIO;
410 }
411 }
412 ivtv_setsda(itv, 1);
413 ivtv_scldelay(itv);
414 ivtv_setscl(itv, 1);
415 if (!ivtv_waitsda(itv, 0)) {
416 IVTV_DEBUG_I2C("Slave did not ack\n");
417 ret = -EREMOTEIO;
418 }
419 ivtv_setscl(itv, 0);
420 if (!ivtv_waitscl(itv, 0)) {
421 IVTV_DEBUG_I2C("Failed to set SCL low after ACK\n");
422 ret = -EREMOTEIO;
423 }
424 return ret;
425}
426
427/* Write a single byte to the i2c bus and wait for the slave to ACK */
428static int ivtv_sendbyte(struct ivtv *itv, unsigned char byte)
429{
430 int i, bit;
431
432 IVTV_DEBUG_HI_I2C("write %x\n",byte);
433 for (i = 0; i < 8; ++i, byte<<=1) {
434 ivtv_setscl(itv, 0);
435 if (!ivtv_waitscl(itv, 0)) {
436 IVTV_DEBUG_I2C("Error setting SCL low\n");
437 return -EREMOTEIO;
438 }
439 bit = (byte>>7)&1;
440 ivtv_setsda(itv, bit);
441 if (!ivtv_waitsda(itv, bit)) {
442 IVTV_DEBUG_I2C("Error setting SDA\n");
443 return -EREMOTEIO;
444 }
445 ivtv_setscl(itv, 1);
446 if (!ivtv_waitscl(itv, 1)) {
447 IVTV_DEBUG_I2C("Slave not ready for bit\n");
448 return -EREMOTEIO;
449 }
450 }
451 ivtv_setscl(itv, 0);
452 if (!ivtv_waitscl(itv, 0)) {
453 IVTV_DEBUG_I2C("Error setting SCL low\n");
454 return -EREMOTEIO;
455 }
456 return ivtv_ack(itv);
457}
458
459/* Read a byte from the i2c bus and send a NACK if applicable (i.e. for the
460 final byte) */
461static int ivtv_readbyte(struct ivtv *itv, unsigned char *byte, int nack)
462{
463 int i;
464
465 *byte = 0;
466
467 ivtv_setsda(itv, 1);
468 ivtv_scldelay(itv);
469 for (i = 0; i < 8; ++i) {
470 ivtv_setscl(itv, 0);
471 ivtv_scldelay(itv);
472 ivtv_setscl(itv, 1);
473 if (!ivtv_waitscl(itv, 1)) {
474 IVTV_DEBUG_I2C("Error setting SCL high\n");
475 return -EREMOTEIO;
476 }
477 *byte = ((*byte)<<1)|ivtv_getsda(itv);
478 }
479 ivtv_setscl(itv, 0);
480 ivtv_scldelay(itv);
481 ivtv_setsda(itv, nack);
482 ivtv_scldelay(itv);
483 ivtv_setscl(itv, 1);
484 ivtv_scldelay(itv);
485 ivtv_setscl(itv, 0);
486 ivtv_scldelay(itv);
487 IVTV_DEBUG_HI_I2C("read %x\n",*byte);
488 return 0;
489}
490
491/* Issue a start condition on the i2c bus to alert slaves to prepare for
492 an address write */
493static int ivtv_start(struct ivtv *itv)
494{
495 int sda;
496
497 sda = ivtv_getsda(itv);
498 if (sda != 1) {
499 IVTV_DEBUG_HI_I2C("SDA was low at start\n");
500 ivtv_setsda(itv, 1);
501 if (!ivtv_waitsda(itv, 1)) {
502 IVTV_DEBUG_I2C("SDA stuck low\n");
503 return -EREMOTEIO;
504 }
505 }
506 if (ivtv_getscl(itv) != 1) {
507 ivtv_setscl(itv, 1);
508 if (!ivtv_waitscl(itv, 1)) {
509 IVTV_DEBUG_I2C("SCL stuck low at start\n");
510 return -EREMOTEIO;
511 }
512 }
513 ivtv_setsda(itv, 0);
514 ivtv_scldelay(itv);
515 return 0;
516}
517
518/* Issue a stop condition on the i2c bus to release it */
519static int ivtv_stop(struct ivtv *itv)
520{
521 int i;
522
523 if (ivtv_getscl(itv) != 0) {
524 IVTV_DEBUG_HI_I2C("SCL not low when stopping\n");
525 ivtv_setscl(itv, 0);
526 if (!ivtv_waitscl(itv, 0)) {
527 IVTV_DEBUG_I2C("SCL could not be set low\n");
528 }
529 }
530 ivtv_setsda(itv, 0);
531 ivtv_scldelay(itv);
532 ivtv_setscl(itv, 1);
533 if (!ivtv_waitscl(itv, 1)) {
534 IVTV_DEBUG_I2C("SCL could not be set high\n");
535 return -EREMOTEIO;
536 }
537 ivtv_scldelay(itv);
538 ivtv_setsda(itv, 1);
539 if (!ivtv_waitsda(itv, 1)) {
540 IVTV_DEBUG_I2C("resetting I2C\n");
541 for (i = 0; i < 16; ++i) {
542 ivtv_setscl(itv, 0);
543 ivtv_scldelay(itv);
544 ivtv_setscl(itv, 1);
545 ivtv_scldelay(itv);
546 ivtv_setsda(itv, 1);
547 }
548 ivtv_waitsda(itv, 1);
549 return -EREMOTEIO;
550 }
551 return 0;
552}
553
554/* Write a message to the given i2c slave. do_stop may be 0 to prevent
555 issuing the i2c stop condition (when following with a read) */
556static int ivtv_write(struct ivtv *itv, unsigned char addr, unsigned char *data, u32 len, int do_stop)
557{
558 int retry, ret = -EREMOTEIO;
559 u32 i;
560
561 for (retry = 0; ret != 0 && retry < 8; ++retry) {
562 ret = ivtv_start(itv);
563
564 if (ret == 0) {
565 ret = ivtv_sendbyte(itv, addr<<1);
566 for (i = 0; ret == 0 && i < len; ++i)
567 ret = ivtv_sendbyte(itv, data[i]);
568 }
569 if (ret != 0 || do_stop) {
570 ivtv_stop(itv);
571 }
572 }
573 if (ret)
574 IVTV_DEBUG_I2C("i2c write to %x failed\n", addr);
575 return ret;
576}
577
578/* Read data from the given i2c slave. A stop condition is always issued. */
579static int ivtv_read(struct ivtv *itv, unsigned char addr, unsigned char *data, u32 len)
580{
581 int retry, ret = -EREMOTEIO;
582 u32 i;
583
584 for (retry = 0; ret != 0 && retry < 8; ++retry) {
585 ret = ivtv_start(itv);
586 if (ret == 0)
587 ret = ivtv_sendbyte(itv, (addr << 1) | 1);
588 for (i = 0; ret == 0 && i < len; ++i) {
589 ret = ivtv_readbyte(itv, &data[i], i == len - 1);
590 }
591 ivtv_stop(itv);
592 }
593 if (ret)
594 IVTV_DEBUG_I2C("i2c read from %x failed\n", addr);
595 return ret;
596}
597
598/* Kernel i2c transfer implementation. Takes a number of messages to be read
599 or written. If a read follows a write, this will occur without an
600 intervening stop condition */
601static int ivtv_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
602{
603 struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);
604 struct ivtv *itv = to_ivtv(v4l2_dev);
605 int retval;
606 int i;
607
608 mutex_lock(&itv->i2c_bus_lock);
609 for (i = retval = 0; retval == 0 && i < num; i++) {
610 if (msgs[i].flags & I2C_M_RD)
611 retval = ivtv_read(itv, msgs[i].addr, msgs[i].buf, msgs[i].len);
612 else {
613 /* if followed by a read, don't stop */
614 int stop = !(i + 1 < num && msgs[i + 1].flags == I2C_M_RD);
615
616 retval = ivtv_write(itv, msgs[i].addr, msgs[i].buf, msgs[i].len, stop);
617 }
618 }
619 mutex_unlock(&itv->i2c_bus_lock);
620 return retval ? retval : num;
621}
622
623/* Kernel i2c capabilities */
624static u32 ivtv_functionality(struct i2c_adapter *adap)
625{
626 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
627}
628
629static struct i2c_algorithm ivtv_algo = {
630 .master_xfer = ivtv_xfer,
631 .functionality = ivtv_functionality,
632};
633
634/* template for our-bit banger */
635static struct i2c_adapter ivtv_i2c_adap_hw_template = {
636 .name = "ivtv i2c driver",
637 .algo = &ivtv_algo,
638 .algo_data = NULL, /* filled from template */
639 .owner = THIS_MODULE,
640};
641
642static void ivtv_setscl_old(void *data, int state)
643{
644 struct ivtv *itv = (struct ivtv *)data;
645
646 if (state)
647 itv->i2c_state |= 0x01;
648 else
649 itv->i2c_state &= ~0x01;
650
651 /* write them out */
652 /* write bits are inverted */
653 write_reg(~itv->i2c_state, IVTV_REG_I2C_SETSCL_OFFSET);
654}
655
656static void ivtv_setsda_old(void *data, int state)
657{
658 struct ivtv *itv = (struct ivtv *)data;
659
660 if (state)
661 itv->i2c_state |= 0x01;
662 else
663 itv->i2c_state &= ~0x01;
664
665 /* write them out */
666 /* write bits are inverted */
667 write_reg(~itv->i2c_state, IVTV_REG_I2C_SETSDA_OFFSET);
668}
669
670static int ivtv_getscl_old(void *data)
671{
672 struct ivtv *itv = (struct ivtv *)data;
673
674 return read_reg(IVTV_REG_I2C_GETSCL_OFFSET) & 1;
675}
676
677static int ivtv_getsda_old(void *data)
678{
679 struct ivtv *itv = (struct ivtv *)data;
680
681 return read_reg(IVTV_REG_I2C_GETSDA_OFFSET) & 1;
682}
683
684/* template for i2c-bit-algo */
685static struct i2c_adapter ivtv_i2c_adap_template = {
686 .name = "ivtv i2c driver",
687 .algo = NULL, /* set by i2c-algo-bit */
688 .algo_data = NULL, /* filled from template */
689 .owner = THIS_MODULE,
690};
691
692#define IVTV_ALGO_BIT_TIMEOUT (2) /* seconds */
693
694static const struct i2c_algo_bit_data ivtv_i2c_algo_template = {
695 .setsda = ivtv_setsda_old,
696 .setscl = ivtv_setscl_old,
697 .getsda = ivtv_getsda_old,
698 .getscl = ivtv_getscl_old,
699 .udelay = IVTV_DEFAULT_I2C_CLOCK_PERIOD / 2, /* microseconds */
700 .timeout = IVTV_ALGO_BIT_TIMEOUT * HZ, /* jiffies */
701};
702
703static struct i2c_client ivtv_i2c_client_template = {
704 .name = "ivtv internal",
705};
706
707/* init + register i2c adapter */
708int init_ivtv_i2c(struct ivtv *itv)
709{
710 int retval;
711
712 IVTV_DEBUG_I2C("i2c init\n");
713
714 /* Sanity checks for the I2C hardware arrays. They must be the
715 * same size.
716 */
717 if (ARRAY_SIZE(hw_devicenames) != ARRAY_SIZE(hw_addrs)) {
718 IVTV_ERR("Mismatched I2C hardware arrays\n");
719 return -ENODEV;
720 }
721 if (itv->options.newi2c > 0) {
722 memcpy(&itv->i2c_adap, &ivtv_i2c_adap_hw_template,
723 sizeof(struct i2c_adapter));
724 } else {
725 memcpy(&itv->i2c_adap, &ivtv_i2c_adap_template,
726 sizeof(struct i2c_adapter));
727 memcpy(&itv->i2c_algo, &ivtv_i2c_algo_template,
728 sizeof(struct i2c_algo_bit_data));
729 }
730 itv->i2c_algo.udelay = itv->options.i2c_clock_period / 2;
731 itv->i2c_algo.data = itv;
732 itv->i2c_adap.algo_data = &itv->i2c_algo;
733
734 sprintf(itv->i2c_adap.name + strlen(itv->i2c_adap.name), " #%d",
735 itv->instance);
736 i2c_set_adapdata(&itv->i2c_adap, &itv->v4l2_dev);
737
738 memcpy(&itv->i2c_client, &ivtv_i2c_client_template,
739 sizeof(struct i2c_client));
740 itv->i2c_client.adapter = &itv->i2c_adap;
741 itv->i2c_adap.dev.parent = &itv->pdev->dev;
742
743 IVTV_DEBUG_I2C("setting scl and sda to 1\n");
744 ivtv_setscl(itv, 1);
745 ivtv_setsda(itv, 1);
746
747 if (itv->options.newi2c > 0)
748 retval = i2c_add_adapter(&itv->i2c_adap);
749 else
750 retval = i2c_bit_add_bus(&itv->i2c_adap);
751
752 return retval;
753}
754
755void exit_ivtv_i2c(struct ivtv *itv)
756{
757 IVTV_DEBUG_I2C("i2c exit\n");
758
759 i2c_del_adapter(&itv->i2c_adap);
760}
diff --git a/drivers/media/pci/ivtv/ivtv-i2c.h b/drivers/media/pci/ivtv/ivtv-i2c.h
new file mode 100644
index 000000000000..7b9ec1cfeb80
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-i2c.h
@@ -0,0 +1,32 @@
1/*
2 I2C functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_I2C_H
22#define IVTV_I2C_H
23
24struct i2c_client *ivtv_i2c_new_ir_legacy(struct ivtv *itv);
25int ivtv_i2c_register(struct ivtv *itv, unsigned idx);
26struct v4l2_subdev *ivtv_find_hw(struct ivtv *itv, u32 hw);
27
28/* init + register i2c adapter */
29int init_ivtv_i2c(struct ivtv *itv);
30void exit_ivtv_i2c(struct ivtv *itv);
31
32#endif
diff --git a/drivers/media/pci/ivtv/ivtv-ioctl.c b/drivers/media/pci/ivtv/ivtv-ioctl.c
new file mode 100644
index 000000000000..32a591062d0b
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-ioctl.c
@@ -0,0 +1,1899 @@
1/*
2 ioctl system call
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "ivtv-driver.h"
22#include "ivtv-version.h"
23#include "ivtv-mailbox.h"
24#include "ivtv-i2c.h"
25#include "ivtv-queue.h"
26#include "ivtv-fileops.h"
27#include "ivtv-vbi.h"
28#include "ivtv-routing.h"
29#include "ivtv-streams.h"
30#include "ivtv-yuv.h"
31#include "ivtv-ioctl.h"
32#include "ivtv-gpio.h"
33#include "ivtv-controls.h"
34#include "ivtv-cards.h"
35#include <media/saa7127.h>
36#include <media/tveeprom.h>
37#include <media/v4l2-chip-ident.h>
38#include <media/v4l2-event.h>
39#include <linux/dvb/audio.h>
40
41u16 ivtv_service2vbi(int type)
42{
43 switch (type) {
44 case V4L2_SLICED_TELETEXT_B:
45 return IVTV_SLICED_TYPE_TELETEXT_B;
46 case V4L2_SLICED_CAPTION_525:
47 return IVTV_SLICED_TYPE_CAPTION_525;
48 case V4L2_SLICED_WSS_625:
49 return IVTV_SLICED_TYPE_WSS_625;
50 case V4L2_SLICED_VPS:
51 return IVTV_SLICED_TYPE_VPS;
52 default:
53 return 0;
54 }
55}
56
57static int valid_service_line(int field, int line, int is_pal)
58{
59 return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
60 (!is_pal && line >= 10 && line < 22);
61}
62
63static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
64{
65 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
66 int i;
67
68 set = set & valid_set;
69 if (set == 0 || !valid_service_line(field, line, is_pal)) {
70 return 0;
71 }
72 if (!is_pal) {
73 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
74 return V4L2_SLICED_CAPTION_525;
75 }
76 else {
77 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
78 return V4L2_SLICED_VPS;
79 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
80 return V4L2_SLICED_WSS_625;
81 if (line == 23)
82 return 0;
83 }
84 for (i = 0; i < 32; i++) {
85 if ((1 << i) & set)
86 return 1 << i;
87 }
88 return 0;
89}
90
91void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
92{
93 u16 set = fmt->service_set;
94 int f, l;
95
96 fmt->service_set = 0;
97 for (f = 0; f < 2; f++) {
98 for (l = 0; l < 24; l++) {
99 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
100 }
101 }
102}
103
104static void check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
105{
106 int f, l;
107
108 for (f = 0; f < 2; f++) {
109 for (l = 0; l < 24; l++) {
110 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
111 }
112 }
113}
114
115u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
116{
117 int f, l;
118 u16 set = 0;
119
120 for (f = 0; f < 2; f++) {
121 for (l = 0; l < 24; l++) {
122 set |= fmt->service_lines[f][l];
123 }
124 }
125 return set;
126}
127
128void ivtv_set_osd_alpha(struct ivtv *itv)
129{
130 ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
131 itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
132 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
133}
134
135int ivtv_set_speed(struct ivtv *itv, int speed)
136{
137 u32 data[CX2341X_MBOX_MAX_DATA];
138 int single_step = (speed == 1 || speed == -1);
139 DEFINE_WAIT(wait);
140
141 if (speed == 0) speed = 1000;
142
143 /* No change? */
144 if (speed == itv->speed && !single_step)
145 return 0;
146
147 if (single_step && (speed < 0) == (itv->speed < 0)) {
148 /* Single step video and no need to change direction */
149 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
150 itv->speed = speed;
151 return 0;
152 }
153 if (single_step)
154 /* Need to change direction */
155 speed = speed < 0 ? -1000 : 1000;
156
157 data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
158 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
159 data[1] = (speed < 0);
160 data[2] = speed < 0 ? 3 : 7;
161 data[3] = v4l2_ctrl_g_ctrl(itv->cxhdl.video_b_frames);
162 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
163 data[5] = 0;
164 data[6] = 0;
165
166 if (speed == 1500 || speed == -1500) data[0] |= 1;
167 else if (speed == 2000 || speed == -2000) data[0] |= 2;
168 else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
169 else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
170
171 /* If not decoding, just change speed setting */
172 if (atomic_read(&itv->decoding) > 0) {
173 int got_sig = 0;
174
175 /* Stop all DMA and decoding activity */
176 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
177
178 /* Wait for any DMA to finish */
179 mutex_unlock(&itv->serialize_lock);
180 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
181 while (test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
182 got_sig = signal_pending(current);
183 if (got_sig)
184 break;
185 got_sig = 0;
186 schedule();
187 }
188 finish_wait(&itv->dma_waitq, &wait);
189 mutex_lock(&itv->serialize_lock);
190 if (got_sig)
191 return -EINTR;
192
193 /* Change Speed safely */
194 ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
195 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
196 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
197 }
198 if (single_step) {
199 speed = (speed < 0) ? -1 : 1;
200 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
201 }
202 itv->speed = speed;
203 return 0;
204}
205
206static int ivtv_validate_speed(int cur_speed, int new_speed)
207{
208 int fact = new_speed < 0 ? -1 : 1;
209 int s;
210
211 if (cur_speed == 0)
212 cur_speed = 1000;
213 if (new_speed < 0)
214 new_speed = -new_speed;
215 if (cur_speed < 0)
216 cur_speed = -cur_speed;
217
218 if (cur_speed <= new_speed) {
219 if (new_speed > 1500)
220 return fact * 2000;
221 if (new_speed > 1000)
222 return fact * 1500;
223 }
224 else {
225 if (new_speed >= 2000)
226 return fact * 2000;
227 if (new_speed >= 1500)
228 return fact * 1500;
229 if (new_speed >= 1000)
230 return fact * 1000;
231 }
232 if (new_speed == 0)
233 return 1000;
234 if (new_speed == 1 || new_speed == 1000)
235 return fact * new_speed;
236
237 s = new_speed;
238 new_speed = 1000 / new_speed;
239 if (1000 / cur_speed == new_speed)
240 new_speed += (cur_speed < s) ? -1 : 1;
241 if (new_speed > 60) return 1000 / (fact * 60);
242 return 1000 / (fact * new_speed);
243}
244
245static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
246 struct v4l2_decoder_cmd *dc, int try)
247{
248 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
249
250 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
251 return -EINVAL;
252
253 switch (dc->cmd) {
254 case V4L2_DEC_CMD_START: {
255 dc->flags &= V4L2_DEC_CMD_START_MUTE_AUDIO;
256 dc->start.speed = ivtv_validate_speed(itv->speed, dc->start.speed);
257 if (dc->start.speed < 0)
258 dc->start.format = V4L2_DEC_START_FMT_GOP;
259 else
260 dc->start.format = V4L2_DEC_START_FMT_NONE;
261 if (dc->start.speed != 500 && dc->start.speed != 1500)
262 dc->flags = dc->start.speed == 1000 ? 0 :
263 V4L2_DEC_CMD_START_MUTE_AUDIO;
264 if (try) break;
265
266 itv->speed_mute_audio = dc->flags & V4L2_DEC_CMD_START_MUTE_AUDIO;
267 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
268 return -EBUSY;
269 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
270 /* forces ivtv_set_speed to be called */
271 itv->speed = 0;
272 }
273 return ivtv_start_decoding(id, dc->start.speed);
274 }
275
276 case V4L2_DEC_CMD_STOP:
277 dc->flags &= V4L2_DEC_CMD_STOP_IMMEDIATELY | V4L2_DEC_CMD_STOP_TO_BLACK;
278 if (dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY)
279 dc->stop.pts = 0;
280 if (try) break;
281 if (atomic_read(&itv->decoding) == 0)
282 return 0;
283 if (itv->output_mode != OUT_MPG)
284 return -EBUSY;
285
286 itv->output_mode = OUT_NONE;
287 return ivtv_stop_v4l2_decode_stream(s, dc->flags, dc->stop.pts);
288
289 case V4L2_DEC_CMD_PAUSE:
290 dc->flags &= V4L2_DEC_CMD_PAUSE_TO_BLACK;
291 if (try) break;
292 if (itv->output_mode != OUT_MPG)
293 return -EBUSY;
294 if (atomic_read(&itv->decoding) > 0) {
295 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
296 (dc->flags & V4L2_DEC_CMD_PAUSE_TO_BLACK) ? 1 : 0);
297 set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
298 }
299 break;
300
301 case V4L2_DEC_CMD_RESUME:
302 dc->flags = 0;
303 if (try) break;
304 if (itv->output_mode != OUT_MPG)
305 return -EBUSY;
306 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
307 int speed = itv->speed;
308 itv->speed = 0;
309 return ivtv_start_decoding(id, speed);
310 }
311 break;
312
313 default:
314 return -EINVAL;
315 }
316 return 0;
317}
318
319static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
320{
321 struct ivtv *itv = fh2id(fh)->itv;
322 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
323
324 vbifmt->reserved[0] = 0;
325 vbifmt->reserved[1] = 0;
326 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
327 return -EINVAL;
328 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
329 if (itv->is_60hz) {
330 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
331 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
332 } else {
333 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
334 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
335 }
336 vbifmt->service_set = ivtv_get_service_set(vbifmt);
337 return 0;
338}
339
340static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
341{
342 struct ivtv_open_id *id = fh2id(fh);
343 struct ivtv *itv = id->itv;
344 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
345
346 pixfmt->width = itv->cxhdl.width;
347 pixfmt->height = itv->cxhdl.height;
348 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
349 pixfmt->field = V4L2_FIELD_INTERLACED;
350 pixfmt->priv = 0;
351 if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
352 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
353 /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
354 pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
355 pixfmt->bytesperline = 720;
356 } else {
357 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
358 pixfmt->sizeimage = 128 * 1024;
359 pixfmt->bytesperline = 0;
360 }
361 return 0;
362}
363
364static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
365{
366 struct ivtv *itv = fh2id(fh)->itv;
367 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
368
369 vbifmt->sampling_rate = 27000000;
370 vbifmt->offset = 248;
371 vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
372 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
373 vbifmt->start[0] = itv->vbi.start[0];
374 vbifmt->start[1] = itv->vbi.start[1];
375 vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
376 vbifmt->flags = 0;
377 vbifmt->reserved[0] = 0;
378 vbifmt->reserved[1] = 0;
379 return 0;
380}
381
382static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
383{
384 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
385 struct ivtv_open_id *id = fh2id(fh);
386 struct ivtv *itv = id->itv;
387
388 vbifmt->reserved[0] = 0;
389 vbifmt->reserved[1] = 0;
390 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
391
392 if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
393 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
394 V4L2_SLICED_VBI_525;
395 ivtv_expand_service_set(vbifmt, itv->is_50hz);
396 return 0;
397 }
398
399 v4l2_subdev_call(itv->sd_video, vbi, g_sliced_fmt, vbifmt);
400 vbifmt->service_set = ivtv_get_service_set(vbifmt);
401 return 0;
402}
403
404static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
405{
406 struct ivtv_open_id *id = fh2id(fh);
407 struct ivtv *itv = id->itv;
408 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
409
410 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
411 return -EINVAL;
412 pixfmt->width = itv->main_rect.width;
413 pixfmt->height = itv->main_rect.height;
414 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
415 pixfmt->field = V4L2_FIELD_INTERLACED;
416 pixfmt->priv = 0;
417 if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
418 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
419 case IVTV_YUV_MODE_INTERLACED:
420 pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
421 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
422 break;
423 case IVTV_YUV_MODE_PROGRESSIVE:
424 pixfmt->field = V4L2_FIELD_NONE;
425 break;
426 default:
427 pixfmt->field = V4L2_FIELD_ANY;
428 break;
429 }
430 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
431 pixfmt->bytesperline = 720;
432 pixfmt->width = itv->yuv_info.v4l2_src_w;
433 pixfmt->height = itv->yuv_info.v4l2_src_h;
434 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
435 pixfmt->sizeimage =
436 1080 * ((pixfmt->height + 31) & ~31);
437 } else {
438 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
439 pixfmt->sizeimage = 128 * 1024;
440 pixfmt->bytesperline = 0;
441 }
442 return 0;
443}
444
445static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
446{
447 struct ivtv *itv = fh2id(fh)->itv;
448 struct v4l2_window *winfmt = &fmt->fmt.win;
449
450 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
451 return -EINVAL;
452 winfmt->chromakey = itv->osd_chroma_key;
453 winfmt->global_alpha = itv->osd_global_alpha;
454 winfmt->field = V4L2_FIELD_INTERLACED;
455 winfmt->clips = NULL;
456 winfmt->clipcount = 0;
457 winfmt->bitmap = NULL;
458 winfmt->w.top = winfmt->w.left = 0;
459 winfmt->w.width = itv->osd_rect.width;
460 winfmt->w.height = itv->osd_rect.height;
461 return 0;
462}
463
464static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
465{
466 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
467}
468
469static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
470{
471 struct ivtv_open_id *id = fh2id(fh);
472 struct ivtv *itv = id->itv;
473 int w = fmt->fmt.pix.width;
474 int h = fmt->fmt.pix.height;
475 int min_h = 2;
476
477 w = min(w, 720);
478 w = max(w, 2);
479 if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
480 /* YUV height must be a multiple of 32 */
481 h &= ~0x1f;
482 min_h = 32;
483 }
484 h = min(h, itv->is_50hz ? 576 : 480);
485 h = max(h, min_h);
486 ivtv_g_fmt_vid_cap(file, fh, fmt);
487 fmt->fmt.pix.width = w;
488 fmt->fmt.pix.height = h;
489 return 0;
490}
491
492static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
493{
494 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
495}
496
497static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
498{
499 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
500 struct ivtv_open_id *id = fh2id(fh);
501 struct ivtv *itv = id->itv;
502
503 if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
504 return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
505
506 /* set sliced VBI capture format */
507 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
508 vbifmt->reserved[0] = 0;
509 vbifmt->reserved[1] = 0;
510
511 if (vbifmt->service_set)
512 ivtv_expand_service_set(vbifmt, itv->is_50hz);
513 check_service_set(vbifmt, itv->is_50hz);
514 vbifmt->service_set = ivtv_get_service_set(vbifmt);
515 return 0;
516}
517
518static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
519{
520 struct ivtv_open_id *id = fh2id(fh);
521 s32 w = fmt->fmt.pix.width;
522 s32 h = fmt->fmt.pix.height;
523 int field = fmt->fmt.pix.field;
524 int ret = ivtv_g_fmt_vid_out(file, fh, fmt);
525
526 w = min(w, 720);
527 w = max(w, 2);
528 /* Why can the height be 576 even when the output is NTSC?
529
530 Internally the buffers of the PVR350 are always set to 720x576. The
531 decoded video frame will always be placed in the top left corner of
532 this buffer. For any video which is not 720x576, the buffer will
533 then be cropped to remove the unused right and lower areas, with
534 the remaining image being scaled by the hardware to fit the display
535 area. The video can be scaled both up and down, so a 720x480 video
536 can be displayed full-screen on PAL and a 720x576 video can be
537 displayed without cropping on NTSC.
538
539 Note that the scaling only occurs on the video stream, the osd
540 resolution is locked to the broadcast standard and not scaled.
541
542 Thanks to Ian Armstrong for this explanation. */
543 h = min(h, 576);
544 h = max(h, 2);
545 if (id->type == IVTV_DEC_STREAM_TYPE_YUV)
546 fmt->fmt.pix.field = field;
547 fmt->fmt.pix.width = w;
548 fmt->fmt.pix.height = h;
549 return ret;
550}
551
552static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
553{
554 struct ivtv *itv = fh2id(fh)->itv;
555 u32 chromakey = fmt->fmt.win.chromakey;
556 u8 global_alpha = fmt->fmt.win.global_alpha;
557
558 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
559 return -EINVAL;
560 ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
561 fmt->fmt.win.chromakey = chromakey;
562 fmt->fmt.win.global_alpha = global_alpha;
563 return 0;
564}
565
566static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
567{
568 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
569}
570
571static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
572{
573 struct ivtv_open_id *id = fh2id(fh);
574 struct ivtv *itv = id->itv;
575 struct v4l2_mbus_framefmt mbus_fmt;
576 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
577 int w = fmt->fmt.pix.width;
578 int h = fmt->fmt.pix.height;
579
580 if (ret)
581 return ret;
582
583 if (itv->cxhdl.width == w && itv->cxhdl.height == h)
584 return 0;
585
586 if (atomic_read(&itv->capturing) > 0)
587 return -EBUSY;
588
589 itv->cxhdl.width = w;
590 itv->cxhdl.height = h;
591 if (v4l2_ctrl_g_ctrl(itv->cxhdl.video_encoding) == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
592 fmt->fmt.pix.width /= 2;
593 mbus_fmt.width = fmt->fmt.pix.width;
594 mbus_fmt.height = h;
595 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
596 v4l2_subdev_call(itv->sd_video, video, s_mbus_fmt, &mbus_fmt);
597 return ivtv_g_fmt_vid_cap(file, fh, fmt);
598}
599
600static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
601{
602 struct ivtv *itv = fh2id(fh)->itv;
603
604 if (!ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
605 return -EBUSY;
606 itv->vbi.sliced_in->service_set = 0;
607 itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
608 v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &fmt->fmt.vbi);
609 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
610}
611
612static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
613{
614 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
615 struct ivtv_open_id *id = fh2id(fh);
616 struct ivtv *itv = id->itv;
617 int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
618
619 if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
620 return ret;
621
622 check_service_set(vbifmt, itv->is_50hz);
623 if (ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
624 return -EBUSY;
625 itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
626 v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, vbifmt);
627 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
628 return 0;
629}
630
631static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
632{
633 struct ivtv_open_id *id = fh2id(fh);
634 struct ivtv *itv = id->itv;
635 struct yuv_playback_info *yi = &itv->yuv_info;
636 int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
637
638 if (ret)
639 return ret;
640
641 if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
642 return 0;
643
644 /* Return now if we already have some frame data */
645 if (yi->stream_size)
646 return -EBUSY;
647
648 yi->v4l2_src_w = fmt->fmt.pix.width;
649 yi->v4l2_src_h = fmt->fmt.pix.height;
650
651 switch (fmt->fmt.pix.field) {
652 case V4L2_FIELD_NONE:
653 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
654 break;
655 case V4L2_FIELD_ANY:
656 yi->lace_mode = IVTV_YUV_MODE_AUTO;
657 break;
658 case V4L2_FIELD_INTERLACED_BT:
659 yi->lace_mode =
660 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
661 break;
662 case V4L2_FIELD_INTERLACED_TB:
663 default:
664 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
665 break;
666 }
667 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
668
669 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
670 itv->dma_data_req_size =
671 1080 * ((yi->v4l2_src_h + 31) & ~31);
672
673 return 0;
674}
675
676static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
677{
678 struct ivtv *itv = fh2id(fh)->itv;
679 int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
680
681 if (ret == 0) {
682 itv->osd_chroma_key = fmt->fmt.win.chromakey;
683 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
684 ivtv_set_osd_alpha(itv);
685 }
686 return ret;
687}
688
689static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_dbg_chip_ident *chip)
690{
691 struct ivtv *itv = fh2id(fh)->itv;
692
693 chip->ident = V4L2_IDENT_NONE;
694 chip->revision = 0;
695 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
696 if (v4l2_chip_match_host(&chip->match))
697 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
698 return 0;
699 }
700 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
701 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
702 return -EINVAL;
703 /* TODO: is this correct? */
704 return ivtv_call_all_err(itv, core, g_chip_ident, chip);
705}
706
707#ifdef CONFIG_VIDEO_ADV_DEBUG
708static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
709{
710 struct v4l2_dbg_register *regs = arg;
711 volatile u8 __iomem *reg_start;
712
713 if (!capable(CAP_SYS_ADMIN))
714 return -EPERM;
715 if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
716 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
717 else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
718 regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
719 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
720 else if (regs->reg < IVTV_ENCODER_SIZE)
721 reg_start = itv->enc_mem;
722 else
723 return -EINVAL;
724
725 regs->size = 4;
726 if (cmd == VIDIOC_DBG_G_REGISTER)
727 regs->val = readl(regs->reg + reg_start);
728 else
729 writel(regs->val, regs->reg + reg_start);
730 return 0;
731}
732
733static int ivtv_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
734{
735 struct ivtv *itv = fh2id(fh)->itv;
736
737 if (v4l2_chip_match_host(&reg->match))
738 return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
739 /* TODO: subdev errors should not be ignored, this should become a
740 subdev helper function. */
741 ivtv_call_all(itv, core, g_register, reg);
742 return 0;
743}
744
745static int ivtv_s_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
746{
747 struct ivtv *itv = fh2id(fh)->itv;
748
749 if (v4l2_chip_match_host(&reg->match))
750 return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
751 /* TODO: subdev errors should not be ignored, this should become a
752 subdev helper function. */
753 ivtv_call_all(itv, core, s_register, reg);
754 return 0;
755}
756#endif
757
758static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
759{
760 struct ivtv_open_id *id = fh2id(file->private_data);
761 struct ivtv *itv = id->itv;
762 struct ivtv_stream *s = &itv->streams[id->type];
763
764 strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
765 strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
766 snprintf(vcap->bus_info, sizeof(vcap->bus_info), "PCI:%s", pci_name(itv->pdev));
767 vcap->capabilities = itv->v4l2_cap | V4L2_CAP_DEVICE_CAPS;
768 vcap->device_caps = s->caps;
769 return 0;
770}
771
772static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
773{
774 struct ivtv *itv = fh2id(fh)->itv;
775
776 return ivtv_get_audio_input(itv, vin->index, vin);
777}
778
779static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
780{
781 struct ivtv *itv = fh2id(fh)->itv;
782
783 vin->index = itv->audio_input;
784 return ivtv_get_audio_input(itv, vin->index, vin);
785}
786
787static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
788{
789 struct ivtv *itv = fh2id(fh)->itv;
790
791 if (vout->index >= itv->nof_audio_inputs)
792 return -EINVAL;
793
794 itv->audio_input = vout->index;
795 ivtv_audio_set_io(itv);
796
797 return 0;
798}
799
800static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
801{
802 struct ivtv *itv = fh2id(fh)->itv;
803
804 /* set it to defaults from our table */
805 return ivtv_get_audio_output(itv, vin->index, vin);
806}
807
808static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
809{
810 struct ivtv *itv = fh2id(fh)->itv;
811
812 vin->index = 0;
813 return ivtv_get_audio_output(itv, vin->index, vin);
814}
815
816static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
817{
818 struct ivtv *itv = fh2id(fh)->itv;
819
820 return ivtv_get_audio_output(itv, vout->index, vout);
821}
822
823static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
824{
825 struct ivtv *itv = fh2id(fh)->itv;
826
827 /* set it to defaults from our table */
828 return ivtv_get_input(itv, vin->index, vin);
829}
830
831static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
832{
833 struct ivtv *itv = fh2id(fh)->itv;
834
835 return ivtv_get_output(itv, vout->index, vout);
836}
837
838static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
839{
840 struct ivtv_open_id *id = fh2id(fh);
841 struct ivtv *itv = id->itv;
842 struct yuv_playback_info *yi = &itv->yuv_info;
843 int streamtype;
844
845 streamtype = id->type;
846
847 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
848 return -EINVAL;
849 cropcap->bounds.top = cropcap->bounds.left = 0;
850 cropcap->bounds.width = 720;
851 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
852 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
853 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
854 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
855 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
856 if (yi->track_osd) {
857 cropcap->bounds.width = yi->osd_full_w;
858 cropcap->bounds.height = yi->osd_full_h;
859 } else {
860 cropcap->bounds.width = 720;
861 cropcap->bounds.height =
862 itv->is_out_50hz ? 576 : 480;
863 }
864 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
865 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
866 } else {
867 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
868 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
869 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
870 }
871 cropcap->defrect = cropcap->bounds;
872 return 0;
873}
874
875static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
876{
877 struct ivtv_open_id *id = fh2id(fh);
878 struct ivtv *itv = id->itv;
879 struct yuv_playback_info *yi = &itv->yuv_info;
880 int streamtype;
881
882 streamtype = id->type;
883
884 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
885 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
886 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
887 yi->main_rect = crop->c;
888 return 0;
889 } else {
890 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
891 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
892 itv->main_rect = crop->c;
893 return 0;
894 }
895 }
896 return -EINVAL;
897 }
898 return -EINVAL;
899}
900
901static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
902{
903 struct ivtv_open_id *id = fh2id(fh);
904 struct ivtv *itv = id->itv;
905 struct yuv_playback_info *yi = &itv->yuv_info;
906 int streamtype;
907
908 streamtype = id->type;
909
910 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
911 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
912 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
913 crop->c = yi->main_rect;
914 else
915 crop->c = itv->main_rect;
916 return 0;
917 }
918 return -EINVAL;
919}
920
921static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
922{
923 static struct v4l2_fmtdesc formats[] = {
924 { 0, 0, 0,
925 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
926 { 0, 0, 0, 0 }
927 },
928 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
929 "MPEG", V4L2_PIX_FMT_MPEG,
930 { 0, 0, 0, 0 }
931 }
932 };
933 enum v4l2_buf_type type = fmt->type;
934
935 if (fmt->index > 1)
936 return -EINVAL;
937
938 *fmt = formats[fmt->index];
939 fmt->type = type;
940 return 0;
941}
942
943static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
944{
945 struct ivtv *itv = fh2id(fh)->itv;
946
947 static struct v4l2_fmtdesc formats[] = {
948 { 0, 0, 0,
949 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
950 { 0, 0, 0, 0 }
951 },
952 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
953 "MPEG", V4L2_PIX_FMT_MPEG,
954 { 0, 0, 0, 0 }
955 }
956 };
957 enum v4l2_buf_type type = fmt->type;
958
959 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
960 return -EINVAL;
961
962 if (fmt->index > 1)
963 return -EINVAL;
964
965 *fmt = formats[fmt->index];
966 fmt->type = type;
967
968 return 0;
969}
970
971static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
972{
973 struct ivtv *itv = fh2id(fh)->itv;
974
975 *i = itv->active_input;
976
977 return 0;
978}
979
980int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
981{
982 struct ivtv *itv = fh2id(fh)->itv;
983
984 if (inp < 0 || inp >= itv->nof_inputs)
985 return -EINVAL;
986
987 if (inp == itv->active_input) {
988 IVTV_DEBUG_INFO("Input unchanged\n");
989 return 0;
990 }
991
992 if (atomic_read(&itv->capturing) > 0) {
993 return -EBUSY;
994 }
995
996 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
997 itv->active_input, inp);
998
999 itv->active_input = inp;
1000 /* Set the audio input to whatever is appropriate for the
1001 input type. */
1002 itv->audio_input = itv->card->video_inputs[inp].audio_index;
1003
1004 /* prevent others from messing with the streams until
1005 we're finished changing inputs. */
1006 ivtv_mute(itv);
1007 ivtv_video_set_io(itv);
1008 ivtv_audio_set_io(itv);
1009 ivtv_unmute(itv);
1010
1011 return 0;
1012}
1013
1014static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1015{
1016 struct ivtv *itv = fh2id(fh)->itv;
1017
1018 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1019 return -EINVAL;
1020
1021 *i = itv->active_output;
1022
1023 return 0;
1024}
1025
1026static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1027{
1028 struct ivtv *itv = fh2id(fh)->itv;
1029
1030 if (outp >= itv->card->nof_outputs)
1031 return -EINVAL;
1032
1033 if (outp == itv->active_output) {
1034 IVTV_DEBUG_INFO("Output unchanged\n");
1035 return 0;
1036 }
1037 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1038 itv->active_output, outp);
1039
1040 itv->active_output = outp;
1041 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1042 SAA7127_INPUT_TYPE_NORMAL,
1043 itv->card->video_outputs[outp].video_output, 0);
1044
1045 return 0;
1046}
1047
1048static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1049{
1050 struct ivtv *itv = fh2id(fh)->itv;
1051
1052 if (vf->tuner != 0)
1053 return -EINVAL;
1054
1055 ivtv_call_all(itv, tuner, g_frequency, vf);
1056 return 0;
1057}
1058
1059int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1060{
1061 struct ivtv *itv = fh2id(fh)->itv;
1062
1063 if (vf->tuner != 0)
1064 return -EINVAL;
1065
1066 ivtv_mute(itv);
1067 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1068 ivtv_call_all(itv, tuner, s_frequency, vf);
1069 ivtv_unmute(itv);
1070 return 0;
1071}
1072
1073static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1074{
1075 struct ivtv *itv = fh2id(fh)->itv;
1076
1077 *std = itv->std;
1078 return 0;
1079}
1080
1081void ivtv_s_std_enc(struct ivtv *itv, v4l2_std_id *std)
1082{
1083 itv->std = *std;
1084 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1085 itv->is_50hz = !itv->is_60hz;
1086 cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1087 itv->cxhdl.width = 720;
1088 itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1089 itv->vbi.count = itv->is_50hz ? 18 : 12;
1090 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1091 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1092
1093 if (itv->hw_flags & IVTV_HW_CX25840)
1094 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1095
1096 /* Tuner */
1097 ivtv_call_all(itv, core, s_std, itv->std);
1098}
1099
1100void ivtv_s_std_dec(struct ivtv *itv, v4l2_std_id *std)
1101{
1102 struct yuv_playback_info *yi = &itv->yuv_info;
1103 DEFINE_WAIT(wait);
1104 int f;
1105
1106 /* set display standard */
1107 itv->std_out = *std;
1108 itv->is_out_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1109 itv->is_out_50hz = !itv->is_out_60hz;
1110 ivtv_call_all(itv, video, s_std_output, itv->std_out);
1111
1112 /*
1113 * The next firmware call is time sensitive. Time it to
1114 * avoid risk of a hard lock, by trying to ensure the call
1115 * happens within the first 100 lines of the top field.
1116 * Make 4 attempts to sync to the decoder before giving up.
1117 */
1118 mutex_unlock(&itv->serialize_lock);
1119 for (f = 0; f < 4; f++) {
1120 prepare_to_wait(&itv->vsync_waitq, &wait,
1121 TASK_UNINTERRUPTIBLE);
1122 if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1123 break;
1124 schedule_timeout(msecs_to_jiffies(25));
1125 }
1126 finish_wait(&itv->vsync_waitq, &wait);
1127 mutex_lock(&itv->serialize_lock);
1128
1129 if (f == 4)
1130 IVTV_WARN("Mode change failed to sync to decoder\n");
1131
1132 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1133 itv->main_rect.left = 0;
1134 itv->main_rect.top = 0;
1135 itv->main_rect.width = 720;
1136 itv->main_rect.height = itv->is_out_50hz ? 576 : 480;
1137 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1138 720, itv->main_rect.height, 0, 0);
1139 yi->main_rect = itv->main_rect;
1140 if (!itv->osd_info) {
1141 yi->osd_full_w = 720;
1142 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1143 }
1144}
1145
1146int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1147{
1148 struct ivtv *itv = fh2id(fh)->itv;
1149
1150 if ((*std & V4L2_STD_ALL) == 0)
1151 return -EINVAL;
1152
1153 if (*std == itv->std)
1154 return 0;
1155
1156 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1157 atomic_read(&itv->capturing) > 0 ||
1158 atomic_read(&itv->decoding) > 0) {
1159 /* Switching standard would mess with already running
1160 streams, prevent that by returning EBUSY. */
1161 return -EBUSY;
1162 }
1163
1164 IVTV_DEBUG_INFO("Switching standard to %llx.\n",
1165 (unsigned long long)itv->std);
1166
1167 ivtv_s_std_enc(itv, std);
1168 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1169 ivtv_s_std_dec(itv, std);
1170
1171 return 0;
1172}
1173
1174static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1175{
1176 struct ivtv_open_id *id = fh2id(fh);
1177 struct ivtv *itv = id->itv;
1178
1179 if (vt->index != 0)
1180 return -EINVAL;
1181
1182 ivtv_call_all(itv, tuner, s_tuner, vt);
1183
1184 return 0;
1185}
1186
1187static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1188{
1189 struct ivtv *itv = fh2id(fh)->itv;
1190
1191 if (vt->index != 0)
1192 return -EINVAL;
1193
1194 ivtv_call_all(itv, tuner, g_tuner, vt);
1195
1196 if (vt->type == V4L2_TUNER_RADIO)
1197 strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1198 else
1199 strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1200 return 0;
1201}
1202
1203static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1204{
1205 struct ivtv *itv = fh2id(fh)->itv;
1206 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1207 int f, l;
1208
1209 if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1210 for (f = 0; f < 2; f++) {
1211 for (l = 0; l < 24; l++) {
1212 if (valid_service_line(f, l, itv->is_50hz))
1213 cap->service_lines[f][l] = set;
1214 }
1215 }
1216 } else if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1217 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1218 return -EINVAL;
1219 if (itv->is_60hz) {
1220 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1221 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1222 } else {
1223 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1224 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1225 }
1226 } else {
1227 return -EINVAL;
1228 }
1229
1230 set = 0;
1231 for (f = 0; f < 2; f++)
1232 for (l = 0; l < 24; l++)
1233 set |= cap->service_lines[f][l];
1234 cap->service_set = set;
1235 return 0;
1236}
1237
1238static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1239{
1240 struct ivtv *itv = fh2id(fh)->itv;
1241 struct v4l2_enc_idx_entry *e = idx->entry;
1242 int entries;
1243 int i;
1244
1245 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1246 IVTV_MAX_PGM_INDEX;
1247 if (entries > V4L2_ENC_IDX_ENTRIES)
1248 entries = V4L2_ENC_IDX_ENTRIES;
1249 idx->entries = 0;
1250 for (i = 0; i < entries; i++) {
1251 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1252 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1253 idx->entries++;
1254 e++;
1255 }
1256 }
1257 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1258 return 0;
1259}
1260
1261static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1262{
1263 struct ivtv_open_id *id = fh2id(fh);
1264 struct ivtv *itv = id->itv;
1265
1266
1267 switch (enc->cmd) {
1268 case V4L2_ENC_CMD_START:
1269 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1270 enc->flags = 0;
1271 return ivtv_start_capture(id);
1272
1273 case V4L2_ENC_CMD_STOP:
1274 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1275 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1276 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1277 return 0;
1278
1279 case V4L2_ENC_CMD_PAUSE:
1280 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1281 enc->flags = 0;
1282
1283 if (!atomic_read(&itv->capturing))
1284 return -EPERM;
1285 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1286 return 0;
1287
1288 ivtv_mute(itv);
1289 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1290 break;
1291
1292 case V4L2_ENC_CMD_RESUME:
1293 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1294 enc->flags = 0;
1295
1296 if (!atomic_read(&itv->capturing))
1297 return -EPERM;
1298
1299 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1300 return 0;
1301
1302 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1303 ivtv_unmute(itv);
1304 break;
1305 default:
1306 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1307 return -EINVAL;
1308 }
1309
1310 return 0;
1311}
1312
1313static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1314{
1315 struct ivtv *itv = fh2id(fh)->itv;
1316
1317 switch (enc->cmd) {
1318 case V4L2_ENC_CMD_START:
1319 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1320 enc->flags = 0;
1321 return 0;
1322
1323 case V4L2_ENC_CMD_STOP:
1324 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1325 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1326 return 0;
1327
1328 case V4L2_ENC_CMD_PAUSE:
1329 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1330 enc->flags = 0;
1331 return 0;
1332
1333 case V4L2_ENC_CMD_RESUME:
1334 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1335 enc->flags = 0;
1336 return 0;
1337 default:
1338 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1339 return -EINVAL;
1340 }
1341}
1342
1343static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1344{
1345 struct ivtv *itv = fh2id(fh)->itv;
1346 u32 data[CX2341X_MBOX_MAX_DATA];
1347 struct yuv_playback_info *yi = &itv->yuv_info;
1348
1349 int pixfmt;
1350 static u32 pixel_format[16] = {
1351 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1352 V4L2_PIX_FMT_RGB565,
1353 V4L2_PIX_FMT_RGB555,
1354 V4L2_PIX_FMT_RGB444,
1355 V4L2_PIX_FMT_RGB32,
1356 0,
1357 0,
1358 0,
1359 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1360 V4L2_PIX_FMT_YUV565,
1361 V4L2_PIX_FMT_YUV555,
1362 V4L2_PIX_FMT_YUV444,
1363 V4L2_PIX_FMT_YUV32,
1364 0,
1365 0,
1366 0,
1367 };
1368
1369 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1370 return -EINVAL;
1371 if (!itv->osd_video_pbase)
1372 return -EINVAL;
1373
1374 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1375 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1376
1377 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1378 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1379 pixfmt = (data[0] >> 3) & 0xf;
1380
1381 fb->fmt.pixelformat = pixel_format[pixfmt];
1382 fb->fmt.width = itv->osd_rect.width;
1383 fb->fmt.height = itv->osd_rect.height;
1384 fb->fmt.field = V4L2_FIELD_INTERLACED;
1385 fb->fmt.bytesperline = fb->fmt.width;
1386 fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1387 fb->fmt.field = V4L2_FIELD_INTERLACED;
1388 fb->fmt.priv = 0;
1389 if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1390 fb->fmt.bytesperline *= 2;
1391 if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1392 fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1393 fb->fmt.bytesperline *= 2;
1394 fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1395 fb->base = (void *)itv->osd_video_pbase;
1396 fb->flags = 0;
1397
1398 if (itv->osd_chroma_key_state)
1399 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1400
1401 if (itv->osd_global_alpha_state)
1402 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1403
1404 if (yi->track_osd)
1405 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1406
1407 pixfmt &= 7;
1408
1409 /* no local alpha for RGB565 or unknown formats */
1410 if (pixfmt == 1 || pixfmt > 4)
1411 return 0;
1412
1413 /* 16-bit formats have inverted local alpha */
1414 if (pixfmt == 2 || pixfmt == 3)
1415 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1416 else
1417 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1418
1419 if (itv->osd_local_alpha_state) {
1420 /* 16-bit formats have inverted local alpha */
1421 if (pixfmt == 2 || pixfmt == 3)
1422 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1423 else
1424 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1425 }
1426
1427 return 0;
1428}
1429
1430static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1431{
1432 struct ivtv_open_id *id = fh2id(fh);
1433 struct ivtv *itv = id->itv;
1434 struct yuv_playback_info *yi = &itv->yuv_info;
1435
1436 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1437 return -EINVAL;
1438 if (!itv->osd_video_pbase)
1439 return -EINVAL;
1440
1441 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1442 itv->osd_local_alpha_state =
1443 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1444 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1445 ivtv_set_osd_alpha(itv);
1446 yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1447 return ivtv_g_fbuf(file, fh, fb);
1448}
1449
1450static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1451{
1452 struct ivtv_open_id *id = fh2id(fh);
1453 struct ivtv *itv = id->itv;
1454
1455 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1456 return -EINVAL;
1457
1458 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1459
1460 return 0;
1461}
1462
1463static int ivtv_subscribe_event(struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
1464{
1465 switch (sub->type) {
1466 case V4L2_EVENT_VSYNC:
1467 case V4L2_EVENT_EOS:
1468 return v4l2_event_subscribe(fh, sub, 0, NULL);
1469 case V4L2_EVENT_CTRL:
1470 return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
1471 default:
1472 return -EINVAL;
1473 }
1474}
1475
1476static int ivtv_log_status(struct file *file, void *fh)
1477{
1478 struct ivtv *itv = fh2id(fh)->itv;
1479 u32 data[CX2341X_MBOX_MAX_DATA];
1480
1481 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1482 struct v4l2_input vidin;
1483 struct v4l2_audio audin;
1484 int i;
1485
1486 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1487 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1488 struct tveeprom tv;
1489
1490 ivtv_read_eeprom(itv, &tv);
1491 }
1492 ivtv_call_all(itv, core, log_status);
1493 ivtv_get_input(itv, itv->active_input, &vidin);
1494 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1495 IVTV_INFO("Video Input: %s\n", vidin.name);
1496 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1497 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1498 if (has_output) {
1499 struct v4l2_output vidout;
1500 struct v4l2_audioout audout;
1501 int mode = itv->output_mode;
1502 static const char * const output_modes[5] = {
1503 "None",
1504 "MPEG Streaming",
1505 "YUV Streaming",
1506 "YUV Frames",
1507 "Passthrough",
1508 };
1509 static const char * const alpha_mode[4] = {
1510 "None",
1511 "Global",
1512 "Local",
1513 "Global and Local"
1514 };
1515 static const char * const pixel_format[16] = {
1516 "ARGB Indexed",
1517 "RGB 5:6:5",
1518 "ARGB 1:5:5:5",
1519 "ARGB 1:4:4:4",
1520 "ARGB 8:8:8:8",
1521 "5",
1522 "6",
1523 "7",
1524 "AYUV Indexed",
1525 "YUV 5:6:5",
1526 "AYUV 1:5:5:5",
1527 "AYUV 1:4:4:4",
1528 "AYUV 8:8:8:8",
1529 "13",
1530 "14",
1531 "15",
1532 };
1533
1534 ivtv_get_output(itv, itv->active_output, &vidout);
1535 ivtv_get_audio_output(itv, 0, &audout);
1536 IVTV_INFO("Video Output: %s\n", vidout.name);
1537 if (mode < 0 || mode > OUT_PASSTHROUGH)
1538 mode = OUT_NONE;
1539 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1540 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1541 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1542 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1543 data[0] & 1 ? "On" : "Off",
1544 alpha_mode[(data[0] >> 1) & 0x3],
1545 pixel_format[(data[0] >> 3) & 0xf]);
1546 }
1547 IVTV_INFO("Tuner: %s\n",
1548 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1549 v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1550 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1551 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1552 struct ivtv_stream *s = &itv->streams[i];
1553
1554 if (s->vdev == NULL || s->buffers == 0)
1555 continue;
1556 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1557 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1558 (s->buffers * s->buf_size) / 1024, s->buffers);
1559 }
1560
1561 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1562 (long long)itv->mpg_data_received,
1563 (long long)itv->vbi_data_inserted);
1564 return 0;
1565}
1566
1567static int ivtv_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1568{
1569 struct ivtv_open_id *id = fh2id(file->private_data);
1570 struct ivtv *itv = id->itv;
1571
1572 IVTV_DEBUG_IOCTL("VIDIOC_DECODER_CMD %d\n", dec->cmd);
1573 return ivtv_video_command(itv, id, dec, false);
1574}
1575
1576static int ivtv_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1577{
1578 struct ivtv_open_id *id = fh2id(file->private_data);
1579 struct ivtv *itv = id->itv;
1580
1581 IVTV_DEBUG_IOCTL("VIDIOC_TRY_DECODER_CMD %d\n", dec->cmd);
1582 return ivtv_video_command(itv, id, dec, true);
1583}
1584
1585static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1586{
1587 struct ivtv_open_id *id = fh2id(filp->private_data);
1588 struct ivtv *itv = id->itv;
1589 int nonblocking = filp->f_flags & O_NONBLOCK;
1590 struct ivtv_stream *s = &itv->streams[id->type];
1591 unsigned long iarg = (unsigned long)arg;
1592
1593 switch (cmd) {
1594 case IVTV_IOC_DMA_FRAME: {
1595 struct ivtv_dma_frame *args = arg;
1596
1597 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1598 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1599 return -EINVAL;
1600 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1601 return -EINVAL;
1602 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1603 return 0;
1604 if (ivtv_start_decoding(id, id->type)) {
1605 return -EBUSY;
1606 }
1607 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1608 ivtv_release_stream(s);
1609 return -EBUSY;
1610 }
1611 /* Mark that this file handle started the UDMA_YUV mode */
1612 id->yuv_frames = 1;
1613 if (args->y_source == NULL)
1614 return 0;
1615 return ivtv_yuv_prep_frame(itv, args);
1616 }
1617
1618 case IVTV_IOC_PASSTHROUGH_MODE:
1619 IVTV_DEBUG_IOCTL("IVTV_IOC_PASSTHROUGH_MODE\n");
1620 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1621 return -EINVAL;
1622 return ivtv_passthrough_mode(itv, *(int *)arg != 0);
1623
1624 case VIDEO_GET_PTS: {
1625 s64 *pts = arg;
1626 s64 frame;
1627
1628 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1629 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1630 *pts = s->dma_pts;
1631 break;
1632 }
1633 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1634 return -EINVAL;
1635 return ivtv_g_pts_frame(itv, pts, &frame);
1636 }
1637
1638 case VIDEO_GET_FRAME_COUNT: {
1639 s64 *frame = arg;
1640 s64 pts;
1641
1642 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1643 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1644 *frame = 0;
1645 break;
1646 }
1647 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1648 return -EINVAL;
1649 return ivtv_g_pts_frame(itv, &pts, frame);
1650 }
1651
1652 case VIDEO_PLAY: {
1653 struct v4l2_decoder_cmd dc;
1654
1655 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1656 memset(&dc, 0, sizeof(dc));
1657 dc.cmd = V4L2_DEC_CMD_START;
1658 return ivtv_video_command(itv, id, &dc, 0);
1659 }
1660
1661 case VIDEO_STOP: {
1662 struct v4l2_decoder_cmd dc;
1663
1664 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1665 memset(&dc, 0, sizeof(dc));
1666 dc.cmd = V4L2_DEC_CMD_STOP;
1667 dc.flags = V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY;
1668 return ivtv_video_command(itv, id, &dc, 0);
1669 }
1670
1671 case VIDEO_FREEZE: {
1672 struct v4l2_decoder_cmd dc;
1673
1674 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1675 memset(&dc, 0, sizeof(dc));
1676 dc.cmd = V4L2_DEC_CMD_PAUSE;
1677 return ivtv_video_command(itv, id, &dc, 0);
1678 }
1679
1680 case VIDEO_CONTINUE: {
1681 struct v4l2_decoder_cmd dc;
1682
1683 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1684 memset(&dc, 0, sizeof(dc));
1685 dc.cmd = V4L2_DEC_CMD_RESUME;
1686 return ivtv_video_command(itv, id, &dc, 0);
1687 }
1688
1689 case VIDEO_COMMAND:
1690 case VIDEO_TRY_COMMAND: {
1691 /* Note: struct v4l2_decoder_cmd has the same layout as
1692 struct video_command */
1693 struct v4l2_decoder_cmd *dc = arg;
1694 int try = (cmd == VIDEO_TRY_COMMAND);
1695
1696 if (try)
1697 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", dc->cmd);
1698 else
1699 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", dc->cmd);
1700 return ivtv_video_command(itv, id, dc, try);
1701 }
1702
1703 case VIDEO_GET_EVENT: {
1704 struct video_event *ev = arg;
1705 DEFINE_WAIT(wait);
1706
1707 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1708 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1709 return -EINVAL;
1710 memset(ev, 0, sizeof(*ev));
1711 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1712
1713 while (1) {
1714 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1715 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1716 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1717 ev->type = VIDEO_EVENT_VSYNC;
1718 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1719 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1720 if (itv->output_mode == OUT_UDMA_YUV &&
1721 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1722 IVTV_YUV_MODE_PROGRESSIVE) {
1723 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1724 }
1725 }
1726 if (ev->type)
1727 return 0;
1728 if (nonblocking)
1729 return -EAGAIN;
1730 /* Wait for event. Note that serialize_lock is locked,
1731 so to allow other processes to access the driver while
1732 we are waiting unlock first and later lock again. */
1733 mutex_unlock(&itv->serialize_lock);
1734 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1735 if (!test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags) &&
1736 !test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags))
1737 schedule();
1738 finish_wait(&itv->event_waitq, &wait);
1739 mutex_lock(&itv->serialize_lock);
1740 if (signal_pending(current)) {
1741 /* return if a signal was received */
1742 IVTV_DEBUG_INFO("User stopped wait for event\n");
1743 return -EINTR;
1744 }
1745 }
1746 break;
1747 }
1748
1749 case VIDEO_SELECT_SOURCE:
1750 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1751 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1752 return -EINVAL;
1753 return ivtv_passthrough_mode(itv, iarg == VIDEO_SOURCE_DEMUX);
1754
1755 case AUDIO_SET_MUTE:
1756 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1757 itv->speed_mute_audio = iarg;
1758 return 0;
1759
1760 case AUDIO_CHANNEL_SELECT:
1761 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1762 if (iarg > AUDIO_STEREO_SWAPPED)
1763 return -EINVAL;
1764 return v4l2_ctrl_s_ctrl(itv->ctrl_audio_playback, iarg + 1);
1765
1766 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1767 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1768 if (iarg > AUDIO_STEREO_SWAPPED)
1769 return -EINVAL;
1770 return v4l2_ctrl_s_ctrl(itv->ctrl_audio_multilingual_playback, iarg + 1);
1771
1772 default:
1773 return -EINVAL;
1774 }
1775 return 0;
1776}
1777
1778static long ivtv_default(struct file *file, void *fh, bool valid_prio,
1779 int cmd, void *arg)
1780{
1781 struct ivtv *itv = fh2id(fh)->itv;
1782
1783 if (!valid_prio) {
1784 switch (cmd) {
1785 case IVTV_IOC_PASSTHROUGH_MODE:
1786 case VIDEO_PLAY:
1787 case VIDEO_STOP:
1788 case VIDEO_FREEZE:
1789 case VIDEO_CONTINUE:
1790 case VIDEO_COMMAND:
1791 case VIDEO_SELECT_SOURCE:
1792 case AUDIO_SET_MUTE:
1793 case AUDIO_CHANNEL_SELECT:
1794 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1795 return -EBUSY;
1796 }
1797 }
1798
1799 switch (cmd) {
1800 case VIDIOC_INT_RESET: {
1801 u32 val = *(u32 *)arg;
1802
1803 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1804 ivtv_reset_ir_gpio(itv);
1805 if (val & 0x02)
1806 v4l2_subdev_call(itv->sd_video, core, reset, 0);
1807 break;
1808 }
1809
1810 case IVTV_IOC_DMA_FRAME:
1811 case IVTV_IOC_PASSTHROUGH_MODE:
1812 case VIDEO_GET_PTS:
1813 case VIDEO_GET_FRAME_COUNT:
1814 case VIDEO_GET_EVENT:
1815 case VIDEO_PLAY:
1816 case VIDEO_STOP:
1817 case VIDEO_FREEZE:
1818 case VIDEO_CONTINUE:
1819 case VIDEO_COMMAND:
1820 case VIDEO_TRY_COMMAND:
1821 case VIDEO_SELECT_SOURCE:
1822 case AUDIO_SET_MUTE:
1823 case AUDIO_CHANNEL_SELECT:
1824 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1825 return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1826
1827 default:
1828 return -ENOTTY;
1829 }
1830 return 0;
1831}
1832
1833static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1834 .vidioc_querycap = ivtv_querycap,
1835 .vidioc_s_audio = ivtv_s_audio,
1836 .vidioc_g_audio = ivtv_g_audio,
1837 .vidioc_enumaudio = ivtv_enumaudio,
1838 .vidioc_s_audout = ivtv_s_audout,
1839 .vidioc_g_audout = ivtv_g_audout,
1840 .vidioc_enum_input = ivtv_enum_input,
1841 .vidioc_enum_output = ivtv_enum_output,
1842 .vidioc_enumaudout = ivtv_enumaudout,
1843 .vidioc_cropcap = ivtv_cropcap,
1844 .vidioc_s_crop = ivtv_s_crop,
1845 .vidioc_g_crop = ivtv_g_crop,
1846 .vidioc_g_input = ivtv_g_input,
1847 .vidioc_s_input = ivtv_s_input,
1848 .vidioc_g_output = ivtv_g_output,
1849 .vidioc_s_output = ivtv_s_output,
1850 .vidioc_g_frequency = ivtv_g_frequency,
1851 .vidioc_s_frequency = ivtv_s_frequency,
1852 .vidioc_s_tuner = ivtv_s_tuner,
1853 .vidioc_g_tuner = ivtv_g_tuner,
1854 .vidioc_g_enc_index = ivtv_g_enc_index,
1855 .vidioc_g_fbuf = ivtv_g_fbuf,
1856 .vidioc_s_fbuf = ivtv_s_fbuf,
1857 .vidioc_g_std = ivtv_g_std,
1858 .vidioc_s_std = ivtv_s_std,
1859 .vidioc_overlay = ivtv_overlay,
1860 .vidioc_log_status = ivtv_log_status,
1861 .vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap,
1862 .vidioc_encoder_cmd = ivtv_encoder_cmd,
1863 .vidioc_try_encoder_cmd = ivtv_try_encoder_cmd,
1864 .vidioc_decoder_cmd = ivtv_decoder_cmd,
1865 .vidioc_try_decoder_cmd = ivtv_try_decoder_cmd,
1866 .vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out,
1867 .vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap,
1868 .vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap,
1869 .vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap,
1870 .vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out,
1871 .vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay,
1872 .vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out,
1873 .vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap,
1874 .vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap,
1875 .vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap,
1876 .vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out,
1877 .vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay,
1878 .vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out,
1879 .vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap,
1880 .vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap,
1881 .vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap,
1882 .vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out,
1883 .vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay,
1884 .vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out,
1885 .vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap,
1886 .vidioc_g_chip_ident = ivtv_g_chip_ident,
1887#ifdef CONFIG_VIDEO_ADV_DEBUG
1888 .vidioc_g_register = ivtv_g_register,
1889 .vidioc_s_register = ivtv_s_register,
1890#endif
1891 .vidioc_default = ivtv_default,
1892 .vidioc_subscribe_event = ivtv_subscribe_event,
1893 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1894};
1895
1896void ivtv_set_funcs(struct video_device *vdev)
1897{
1898 vdev->ioctl_ops = &ivtv_ioctl_ops;
1899}
diff --git a/drivers/media/pci/ivtv/ivtv-ioctl.h b/drivers/media/pci/ivtv/ivtv-ioctl.h
new file mode 100644
index 000000000000..7c553d16579b
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-ioctl.h
@@ -0,0 +1,35 @@
1/*
2 ioctl system call
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_IOCTL_H
22#define IVTV_IOCTL_H
23
24u16 ivtv_service2vbi(int type);
25void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal);
26u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt);
27void ivtv_set_osd_alpha(struct ivtv *itv);
28int ivtv_set_speed(struct ivtv *itv, int speed);
29void ivtv_set_funcs(struct video_device *vdev);
30void ivtv_s_std_enc(struct ivtv *itv, v4l2_std_id *std);
31void ivtv_s_std_dec(struct ivtv *itv, v4l2_std_id *std);
32int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf);
33int ivtv_s_input(struct file *file, void *fh, unsigned int inp);
34
35#endif
diff --git a/drivers/media/pci/ivtv/ivtv-irq.c b/drivers/media/pci/ivtv/ivtv-irq.c
new file mode 100644
index 000000000000..1b3b9578bf47
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-irq.c
@@ -0,0 +1,1038 @@
1/* interrupt handling
2 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
3 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "ivtv-driver.h"
22#include "ivtv-queue.h"
23#include "ivtv-udma.h"
24#include "ivtv-irq.h"
25#include "ivtv-mailbox.h"
26#include "ivtv-vbi.h"
27#include "ivtv-yuv.h"
28#include <media/v4l2-event.h>
29
30#define DMA_MAGIC_COOKIE 0x000001fe
31
32static void ivtv_dma_dec_start(struct ivtv_stream *s);
33
34static const int ivtv_stream_map[] = {
35 IVTV_ENC_STREAM_TYPE_MPG,
36 IVTV_ENC_STREAM_TYPE_YUV,
37 IVTV_ENC_STREAM_TYPE_PCM,
38 IVTV_ENC_STREAM_TYPE_VBI,
39};
40
41
42static void ivtv_pio_work_handler(struct ivtv *itv)
43{
44 struct ivtv_stream *s = &itv->streams[itv->cur_pio_stream];
45 struct ivtv_buffer *buf;
46 int i = 0;
47
48 IVTV_DEBUG_HI_DMA("ivtv_pio_work_handler\n");
49 if (itv->cur_pio_stream < 0 || itv->cur_pio_stream >= IVTV_MAX_STREAMS ||
50 s->vdev == NULL || !ivtv_use_pio(s)) {
51 itv->cur_pio_stream = -1;
52 /* trigger PIO complete user interrupt */
53 write_reg(IVTV_IRQ_ENC_PIO_COMPLETE, 0x44);
54 return;
55 }
56 IVTV_DEBUG_HI_DMA("Process PIO %s\n", s->name);
57 list_for_each_entry(buf, &s->q_dma.list, list) {
58 u32 size = s->sg_processing[i].size & 0x3ffff;
59
60 /* Copy the data from the card to the buffer */
61 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
62 memcpy_fromio(buf->buf, itv->dec_mem + s->sg_processing[i].src - IVTV_DECODER_OFFSET, size);
63 }
64 else {
65 memcpy_fromio(buf->buf, itv->enc_mem + s->sg_processing[i].src, size);
66 }
67 i++;
68 if (i == s->sg_processing_size)
69 break;
70 }
71 write_reg(IVTV_IRQ_ENC_PIO_COMPLETE, 0x44);
72}
73
74void ivtv_irq_work_handler(struct kthread_work *work)
75{
76 struct ivtv *itv = container_of(work, struct ivtv, irq_work);
77
78 if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_PIO, &itv->i_flags))
79 ivtv_pio_work_handler(itv);
80
81 if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_VBI, &itv->i_flags))
82 ivtv_vbi_work_handler(itv);
83
84 if (test_and_clear_bit(IVTV_F_I_WORK_HANDLER_YUV, &itv->i_flags))
85 ivtv_yuv_work_handler(itv);
86}
87
88/* Determine the required DMA size, setup enough buffers in the predma queue and
89 actually copy the data from the card to the buffers in case a PIO transfer is
90 required for this stream.
91 */
92static int stream_enc_dma_append(struct ivtv_stream *s, u32 data[CX2341X_MBOX_MAX_DATA])
93{
94 struct ivtv *itv = s->itv;
95 struct ivtv_buffer *buf;
96 u32 bytes_needed = 0;
97 u32 offset, size;
98 u32 UVoffset = 0, UVsize = 0;
99 int skip_bufs = s->q_predma.buffers;
100 int idx = s->sg_pending_size;
101 int rc;
102
103 /* sanity checks */
104 if (s->vdev == NULL) {
105 IVTV_DEBUG_WARN("Stream %s not started\n", s->name);
106 return -1;
107 }
108 if (!test_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
109 IVTV_DEBUG_WARN("Stream %s not open\n", s->name);
110 return -1;
111 }
112
113 /* determine offset, size and PTS for the various streams */
114 switch (s->type) {
115 case IVTV_ENC_STREAM_TYPE_MPG:
116 offset = data[1];
117 size = data[2];
118 s->pending_pts = 0;
119 break;
120
121 case IVTV_ENC_STREAM_TYPE_YUV:
122 offset = data[1];
123 size = data[2];
124 UVoffset = data[3];
125 UVsize = data[4];
126 s->pending_pts = ((u64) data[5] << 32) | data[6];
127 break;
128
129 case IVTV_ENC_STREAM_TYPE_PCM:
130 offset = data[1] + 12;
131 size = data[2] - 12;
132 s->pending_pts = read_dec(offset - 8) |
133 ((u64)(read_dec(offset - 12)) << 32);
134 if (itv->has_cx23415)
135 offset += IVTV_DECODER_OFFSET;
136 break;
137
138 case IVTV_ENC_STREAM_TYPE_VBI:
139 size = itv->vbi.enc_size * itv->vbi.fpi;
140 offset = read_enc(itv->vbi.enc_start - 4) + 12;
141 if (offset == 12) {
142 IVTV_DEBUG_INFO("VBI offset == 0\n");
143 return -1;
144 }
145 s->pending_pts = read_enc(offset - 4) | ((u64)read_enc(offset - 8) << 32);
146 break;
147
148 case IVTV_DEC_STREAM_TYPE_VBI:
149 size = read_dec(itv->vbi.dec_start + 4) + 8;
150 offset = read_dec(itv->vbi.dec_start) + itv->vbi.dec_start;
151 s->pending_pts = 0;
152 offset += IVTV_DECODER_OFFSET;
153 break;
154 default:
155 /* shouldn't happen */
156 return -1;
157 }
158
159 /* if this is the start of the DMA then fill in the magic cookie */
160 if (s->sg_pending_size == 0 && ivtv_use_dma(s)) {
161 if (itv->has_cx23415 && (s->type == IVTV_ENC_STREAM_TYPE_PCM ||
162 s->type == IVTV_DEC_STREAM_TYPE_VBI)) {
163 s->pending_backup = read_dec(offset - IVTV_DECODER_OFFSET);
164 write_dec_sync(cpu_to_le32(DMA_MAGIC_COOKIE), offset - IVTV_DECODER_OFFSET);
165 }
166 else {
167 s->pending_backup = read_enc(offset);
168 write_enc_sync(cpu_to_le32(DMA_MAGIC_COOKIE), offset);
169 }
170 s->pending_offset = offset;
171 }
172
173 bytes_needed = size;
174 if (s->type == IVTV_ENC_STREAM_TYPE_YUV) {
175 /* The size for the Y samples needs to be rounded upwards to a
176 multiple of the buf_size. The UV samples then start in the
177 next buffer. */
178 bytes_needed = s->buf_size * ((bytes_needed + s->buf_size - 1) / s->buf_size);
179 bytes_needed += UVsize;
180 }
181
182 IVTV_DEBUG_HI_DMA("%s %s: 0x%08x bytes at 0x%08x\n",
183 ivtv_use_pio(s) ? "PIO" : "DMA", s->name, bytes_needed, offset);
184
185 rc = ivtv_queue_move(s, &s->q_free, &s->q_full, &s->q_predma, bytes_needed);
186 if (rc < 0) { /* Insufficient buffers */
187 IVTV_DEBUG_WARN("Cannot obtain %d bytes for %s data transfer\n",
188 bytes_needed, s->name);
189 return -1;
190 }
191 if (rc && !s->buffers_stolen && test_bit(IVTV_F_S_APPL_IO, &s->s_flags)) {
192 IVTV_WARN("All %s stream buffers are full. Dropping data.\n", s->name);
193 IVTV_WARN("Cause: the application is not reading fast enough.\n");
194 }
195 s->buffers_stolen = rc;
196
197 /* got the buffers, now fill in sg_pending */
198 buf = list_entry(s->q_predma.list.next, struct ivtv_buffer, list);
199 memset(buf->buf, 0, 128);
200 list_for_each_entry(buf, &s->q_predma.list, list) {
201 if (skip_bufs-- > 0)
202 continue;
203 s->sg_pending[idx].dst = buf->dma_handle;
204 s->sg_pending[idx].src = offset;
205 s->sg_pending[idx].size = s->buf_size;
206 buf->bytesused = min(size, s->buf_size);
207 buf->dma_xfer_cnt = s->dma_xfer_cnt;
208
209 s->q_predma.bytesused += buf->bytesused;
210 size -= buf->bytesused;
211 offset += s->buf_size;
212
213 /* Sync SG buffers */
214 ivtv_buf_sync_for_device(s, buf);
215
216 if (size == 0) { /* YUV */
217 /* process the UV section */
218 offset = UVoffset;
219 size = UVsize;
220 }
221 idx++;
222 }
223 s->sg_pending_size = idx;
224 return 0;
225}
226
227static void dma_post(struct ivtv_stream *s)
228{
229 struct ivtv *itv = s->itv;
230 struct ivtv_buffer *buf = NULL;
231 struct list_head *p;
232 u32 offset;
233 __le32 *u32buf;
234 int x = 0;
235
236 IVTV_DEBUG_HI_DMA("%s %s completed (%x)\n", ivtv_use_pio(s) ? "PIO" : "DMA",
237 s->name, s->dma_offset);
238 list_for_each(p, &s->q_dma.list) {
239 buf = list_entry(p, struct ivtv_buffer, list);
240 u32buf = (__le32 *)buf->buf;
241
242 /* Sync Buffer */
243 ivtv_buf_sync_for_cpu(s, buf);
244
245 if (x == 0 && ivtv_use_dma(s)) {
246 offset = s->dma_last_offset;
247 if (u32buf[offset / 4] != DMA_MAGIC_COOKIE)
248 {
249 for (offset = 0; offset < 64; offset++) {
250 if (u32buf[offset] == DMA_MAGIC_COOKIE) {
251 break;
252 }
253 }
254 offset *= 4;
255 if (offset == 256) {
256 IVTV_DEBUG_WARN("%s: Couldn't find start of buffer within the first 256 bytes\n", s->name);
257 offset = s->dma_last_offset;
258 }
259 if (s->dma_last_offset != offset)
260 IVTV_DEBUG_WARN("%s: offset %d -> %d\n", s->name, s->dma_last_offset, offset);
261 s->dma_last_offset = offset;
262 }
263 if (itv->has_cx23415 && (s->type == IVTV_ENC_STREAM_TYPE_PCM ||
264 s->type == IVTV_DEC_STREAM_TYPE_VBI)) {
265 write_dec_sync(0, s->dma_offset - IVTV_DECODER_OFFSET);
266 }
267 else {
268 write_enc_sync(0, s->dma_offset);
269 }
270 if (offset) {
271 buf->bytesused -= offset;
272 memcpy(buf->buf, buf->buf + offset, buf->bytesused + offset);
273 }
274 *u32buf = cpu_to_le32(s->dma_backup);
275 }
276 x++;
277 /* flag byteswap ABCD -> DCBA for MPG & VBI data outside irq */
278 if (s->type == IVTV_ENC_STREAM_TYPE_MPG ||
279 s->type == IVTV_ENC_STREAM_TYPE_VBI)
280 buf->b_flags |= IVTV_F_B_NEED_BUF_SWAP;
281 }
282 if (buf)
283 buf->bytesused += s->dma_last_offset;
284 if (buf && s->type == IVTV_DEC_STREAM_TYPE_VBI) {
285 list_for_each_entry(buf, &s->q_dma.list, list) {
286 /* Parse and Groom VBI Data */
287 s->q_dma.bytesused -= buf->bytesused;
288 ivtv_process_vbi_data(itv, buf, 0, s->type);
289 s->q_dma.bytesused += buf->bytesused;
290 }
291 if (s->fh == NULL) {
292 ivtv_queue_move(s, &s->q_dma, NULL, &s->q_free, 0);
293 return;
294 }
295 }
296 ivtv_queue_move(s, &s->q_dma, NULL, &s->q_full, s->q_dma.bytesused);
297 if (s->fh)
298 wake_up(&s->waitq);
299}
300
301void ivtv_dma_stream_dec_prepare(struct ivtv_stream *s, u32 offset, int lock)
302{
303 struct ivtv *itv = s->itv;
304 struct yuv_playback_info *yi = &itv->yuv_info;
305 u8 frame = yi->draw_frame;
306 struct yuv_frame_info *f = &yi->new_frame_info[frame];
307 struct ivtv_buffer *buf;
308 u32 y_size = 720 * ((f->src_h + 31) & ~31);
309 u32 uv_offset = offset + IVTV_YUV_BUFFER_UV_OFFSET;
310 int y_done = 0;
311 int bytes_written = 0;
312 unsigned long flags = 0;
313 int idx = 0;
314
315 IVTV_DEBUG_HI_DMA("DEC PREPARE DMA %s: %08x %08x\n", s->name, s->q_predma.bytesused, offset);
316
317 /* Insert buffer block for YUV if needed */
318 if (s->type == IVTV_DEC_STREAM_TYPE_YUV && f->offset_y) {
319 if (yi->blanking_dmaptr) {
320 s->sg_pending[idx].src = yi->blanking_dmaptr;
321 s->sg_pending[idx].dst = offset;
322 s->sg_pending[idx].size = 720 * 16;
323 }
324 offset += 720 * 16;
325 idx++;
326 }
327
328 list_for_each_entry(buf, &s->q_predma.list, list) {
329 /* YUV UV Offset from Y Buffer */
330 if (s->type == IVTV_DEC_STREAM_TYPE_YUV && !y_done &&
331 (bytes_written + buf->bytesused) >= y_size) {
332 s->sg_pending[idx].src = buf->dma_handle;
333 s->sg_pending[idx].dst = offset;
334 s->sg_pending[idx].size = y_size - bytes_written;
335 offset = uv_offset;
336 if (s->sg_pending[idx].size != buf->bytesused) {
337 idx++;
338 s->sg_pending[idx].src =
339 buf->dma_handle + s->sg_pending[idx - 1].size;
340 s->sg_pending[idx].dst = offset;
341 s->sg_pending[idx].size =
342 buf->bytesused - s->sg_pending[idx - 1].size;
343 offset += s->sg_pending[idx].size;
344 }
345 y_done = 1;
346 } else {
347 s->sg_pending[idx].src = buf->dma_handle;
348 s->sg_pending[idx].dst = offset;
349 s->sg_pending[idx].size = buf->bytesused;
350 offset += buf->bytesused;
351 }
352 bytes_written += buf->bytesused;
353
354 /* Sync SG buffers */
355 ivtv_buf_sync_for_device(s, buf);
356 idx++;
357 }
358 s->sg_pending_size = idx;
359
360 /* Sync Hardware SG List of buffers */
361 ivtv_stream_sync_for_device(s);
362 if (lock)
363 spin_lock_irqsave(&itv->dma_reg_lock, flags);
364 if (!test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
365 ivtv_dma_dec_start(s);
366 }
367 else {
368 set_bit(IVTV_F_S_DMA_PENDING, &s->s_flags);
369 }
370 if (lock)
371 spin_unlock_irqrestore(&itv->dma_reg_lock, flags);
372}
373
374static void ivtv_dma_enc_start_xfer(struct ivtv_stream *s)
375{
376 struct ivtv *itv = s->itv;
377
378 s->sg_dma->src = cpu_to_le32(s->sg_processing[s->sg_processed].src);
379 s->sg_dma->dst = cpu_to_le32(s->sg_processing[s->sg_processed].dst);
380 s->sg_dma->size = cpu_to_le32(s->sg_processing[s->sg_processed].size | 0x80000000);
381 s->sg_processed++;
382 /* Sync Hardware SG List of buffers */
383 ivtv_stream_sync_for_device(s);
384 write_reg(s->sg_handle, IVTV_REG_ENCDMAADDR);
385 write_reg_sync(read_reg(IVTV_REG_DMAXFER) | 0x02, IVTV_REG_DMAXFER);
386 itv->dma_timer.expires = jiffies + msecs_to_jiffies(300);
387 add_timer(&itv->dma_timer);
388}
389
390static void ivtv_dma_dec_start_xfer(struct ivtv_stream *s)
391{
392 struct ivtv *itv = s->itv;
393
394 s->sg_dma->src = cpu_to_le32(s->sg_processing[s->sg_processed].src);
395 s->sg_dma->dst = cpu_to_le32(s->sg_processing[s->sg_processed].dst);
396 s->sg_dma->size = cpu_to_le32(s->sg_processing[s->sg_processed].size | 0x80000000);
397 s->sg_processed++;
398 /* Sync Hardware SG List of buffers */
399 ivtv_stream_sync_for_device(s);
400 write_reg(s->sg_handle, IVTV_REG_DECDMAADDR);
401 write_reg_sync(read_reg(IVTV_REG_DMAXFER) | 0x01, IVTV_REG_DMAXFER);
402 itv->dma_timer.expires = jiffies + msecs_to_jiffies(300);
403 add_timer(&itv->dma_timer);
404}
405
406/* start the encoder DMA */
407static void ivtv_dma_enc_start(struct ivtv_stream *s)
408{
409 struct ivtv *itv = s->itv;
410 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
411 int i;
412
413 IVTV_DEBUG_HI_DMA("start %s for %s\n", ivtv_use_dma(s) ? "DMA" : "PIO", s->name);
414
415 if (s->q_predma.bytesused)
416 ivtv_queue_move(s, &s->q_predma, NULL, &s->q_dma, s->q_predma.bytesused);
417
418 if (ivtv_use_dma(s))
419 s->sg_pending[s->sg_pending_size - 1].size += 256;
420
421 /* If this is an MPEG stream, and VBI data is also pending, then append the
422 VBI DMA to the MPEG DMA and transfer both sets of data at once.
423
424 VBI DMA is a second class citizen compared to MPEG and mixing them together
425 will confuse the firmware (the end of a VBI DMA is seen as the end of a
426 MPEG DMA, thus effectively dropping an MPEG frame). So instead we make
427 sure we only use the MPEG DMA to transfer the VBI DMA if both are in
428 use. This way no conflicts occur. */
429 clear_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags);
430 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && s_vbi->sg_pending_size &&
431 s->sg_pending_size + s_vbi->sg_pending_size <= s->buffers) {
432 ivtv_queue_move(s_vbi, &s_vbi->q_predma, NULL, &s_vbi->q_dma, s_vbi->q_predma.bytesused);
433 if (ivtv_use_dma(s_vbi))
434 s_vbi->sg_pending[s_vbi->sg_pending_size - 1].size += 256;
435 for (i = 0; i < s_vbi->sg_pending_size; i++) {
436 s->sg_pending[s->sg_pending_size++] = s_vbi->sg_pending[i];
437 }
438 s_vbi->dma_offset = s_vbi->pending_offset;
439 s_vbi->sg_pending_size = 0;
440 s_vbi->dma_xfer_cnt++;
441 set_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags);
442 IVTV_DEBUG_HI_DMA("include DMA for %s\n", s_vbi->name);
443 }
444
445 s->dma_xfer_cnt++;
446 memcpy(s->sg_processing, s->sg_pending, sizeof(struct ivtv_sg_host_element) * s->sg_pending_size);
447 s->sg_processing_size = s->sg_pending_size;
448 s->sg_pending_size = 0;
449 s->sg_processed = 0;
450 s->dma_offset = s->pending_offset;
451 s->dma_backup = s->pending_backup;
452 s->dma_pts = s->pending_pts;
453
454 if (ivtv_use_pio(s)) {
455 set_bit(IVTV_F_I_WORK_HANDLER_PIO, &itv->i_flags);
456 set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags);
457 set_bit(IVTV_F_I_PIO, &itv->i_flags);
458 itv->cur_pio_stream = s->type;
459 }
460 else {
461 itv->dma_retries = 0;
462 ivtv_dma_enc_start_xfer(s);
463 set_bit(IVTV_F_I_DMA, &itv->i_flags);
464 itv->cur_dma_stream = s->type;
465 }
466}
467
468static void ivtv_dma_dec_start(struct ivtv_stream *s)
469{
470 struct ivtv *itv = s->itv;
471
472 if (s->q_predma.bytesused)
473 ivtv_queue_move(s, &s->q_predma, NULL, &s->q_dma, s->q_predma.bytesused);
474 s->dma_xfer_cnt++;
475 memcpy(s->sg_processing, s->sg_pending, sizeof(struct ivtv_sg_host_element) * s->sg_pending_size);
476 s->sg_processing_size = s->sg_pending_size;
477 s->sg_pending_size = 0;
478 s->sg_processed = 0;
479
480 IVTV_DEBUG_HI_DMA("start DMA for %s\n", s->name);
481 itv->dma_retries = 0;
482 ivtv_dma_dec_start_xfer(s);
483 set_bit(IVTV_F_I_DMA, &itv->i_flags);
484 itv->cur_dma_stream = s->type;
485}
486
487static void ivtv_irq_dma_read(struct ivtv *itv)
488{
489 struct ivtv_stream *s = NULL;
490 struct ivtv_buffer *buf;
491 int hw_stream_type = 0;
492
493 IVTV_DEBUG_HI_IRQ("DEC DMA READ\n");
494
495 del_timer(&itv->dma_timer);
496
497 if (!test_bit(IVTV_F_I_UDMA, &itv->i_flags) && itv->cur_dma_stream < 0)
498 return;
499
500 if (!test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
501 s = &itv->streams[itv->cur_dma_stream];
502 ivtv_stream_sync_for_cpu(s);
503
504 if (read_reg(IVTV_REG_DMASTATUS) & 0x14) {
505 IVTV_DEBUG_WARN("DEC DMA ERROR %x (xfer %d of %d, retry %d)\n",
506 read_reg(IVTV_REG_DMASTATUS),
507 s->sg_processed, s->sg_processing_size, itv->dma_retries);
508 write_reg(read_reg(IVTV_REG_DMASTATUS) & 3, IVTV_REG_DMASTATUS);
509 if (itv->dma_retries == 3) {
510 /* Too many retries, give up on this frame */
511 itv->dma_retries = 0;
512 s->sg_processed = s->sg_processing_size;
513 }
514 else {
515 /* Retry, starting with the first xfer segment.
516 Just retrying the current segment is not sufficient. */
517 s->sg_processed = 0;
518 itv->dma_retries++;
519 }
520 }
521 if (s->sg_processed < s->sg_processing_size) {
522 /* DMA next buffer */
523 ivtv_dma_dec_start_xfer(s);
524 return;
525 }
526 if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
527 hw_stream_type = 2;
528 IVTV_DEBUG_HI_DMA("DEC DATA READ %s: %d\n", s->name, s->q_dma.bytesused);
529
530 /* For some reason must kick the firmware, like PIO mode,
531 I think this tells the firmware we are done and the size
532 of the xfer so it can calculate what we need next.
533 I think we can do this part ourselves but would have to
534 fully calculate xfer info ourselves and not use interrupts
535 */
536 ivtv_vapi(itv, CX2341X_DEC_SCHED_DMA_FROM_HOST, 3, 0, s->q_dma.bytesused,
537 hw_stream_type);
538
539 /* Free last DMA call */
540 while ((buf = ivtv_dequeue(s, &s->q_dma)) != NULL) {
541 ivtv_buf_sync_for_cpu(s, buf);
542 ivtv_enqueue(s, buf, &s->q_free);
543 }
544 wake_up(&s->waitq);
545 }
546 clear_bit(IVTV_F_I_UDMA, &itv->i_flags);
547 clear_bit(IVTV_F_I_DMA, &itv->i_flags);
548 itv->cur_dma_stream = -1;
549 wake_up(&itv->dma_waitq);
550}
551
552static void ivtv_irq_enc_dma_complete(struct ivtv *itv)
553{
554 u32 data[CX2341X_MBOX_MAX_DATA];
555 struct ivtv_stream *s;
556
557 ivtv_api_get_data(&itv->enc_mbox, IVTV_MBOX_DMA_END, 2, data);
558 IVTV_DEBUG_HI_IRQ("ENC DMA COMPLETE %x %d (%d)\n", data[0], data[1], itv->cur_dma_stream);
559
560 del_timer(&itv->dma_timer);
561
562 if (itv->cur_dma_stream < 0)
563 return;
564
565 s = &itv->streams[itv->cur_dma_stream];
566 ivtv_stream_sync_for_cpu(s);
567
568 if (data[0] & 0x18) {
569 IVTV_DEBUG_WARN("ENC DMA ERROR %x (offset %08x, xfer %d of %d, retry %d)\n", data[0],
570 s->dma_offset, s->sg_processed, s->sg_processing_size, itv->dma_retries);
571 write_reg(read_reg(IVTV_REG_DMASTATUS) & 3, IVTV_REG_DMASTATUS);
572 if (itv->dma_retries == 3) {
573 /* Too many retries, give up on this frame */
574 itv->dma_retries = 0;
575 s->sg_processed = s->sg_processing_size;
576 }
577 else {
578 /* Retry, starting with the first xfer segment.
579 Just retrying the current segment is not sufficient. */
580 s->sg_processed = 0;
581 itv->dma_retries++;
582 }
583 }
584 if (s->sg_processed < s->sg_processing_size) {
585 /* DMA next buffer */
586 ivtv_dma_enc_start_xfer(s);
587 return;
588 }
589 clear_bit(IVTV_F_I_DMA, &itv->i_flags);
590 itv->cur_dma_stream = -1;
591 dma_post(s);
592 if (test_and_clear_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags)) {
593 s = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
594 dma_post(s);
595 }
596 s->sg_processing_size = 0;
597 s->sg_processed = 0;
598 wake_up(&itv->dma_waitq);
599}
600
601static void ivtv_irq_enc_pio_complete(struct ivtv *itv)
602{
603 struct ivtv_stream *s;
604
605 if (itv->cur_pio_stream < 0 || itv->cur_pio_stream >= IVTV_MAX_STREAMS) {
606 itv->cur_pio_stream = -1;
607 return;
608 }
609 s = &itv->streams[itv->cur_pio_stream];
610 IVTV_DEBUG_HI_IRQ("ENC PIO COMPLETE %s\n", s->name);
611 clear_bit(IVTV_F_I_PIO, &itv->i_flags);
612 itv->cur_pio_stream = -1;
613 dma_post(s);
614 if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
615 ivtv_vapi(itv, CX2341X_ENC_SCHED_DMA_TO_HOST, 3, 0, 0, 0);
616 else if (s->type == IVTV_ENC_STREAM_TYPE_YUV)
617 ivtv_vapi(itv, CX2341X_ENC_SCHED_DMA_TO_HOST, 3, 0, 0, 1);
618 else if (s->type == IVTV_ENC_STREAM_TYPE_PCM)
619 ivtv_vapi(itv, CX2341X_ENC_SCHED_DMA_TO_HOST, 3, 0, 0, 2);
620 clear_bit(IVTV_F_I_PIO, &itv->i_flags);
621 if (test_and_clear_bit(IVTV_F_S_DMA_HAS_VBI, &s->s_flags)) {
622 s = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
623 dma_post(s);
624 }
625 wake_up(&itv->dma_waitq);
626}
627
628static void ivtv_irq_dma_err(struct ivtv *itv)
629{
630 u32 data[CX2341X_MBOX_MAX_DATA];
631 u32 status;
632
633 del_timer(&itv->dma_timer);
634
635 ivtv_api_get_data(&itv->enc_mbox, IVTV_MBOX_DMA_END, 2, data);
636 status = read_reg(IVTV_REG_DMASTATUS);
637 IVTV_DEBUG_WARN("DMA ERROR %08x %08x %08x %d\n", data[0], data[1],
638 status, itv->cur_dma_stream);
639 /*
640 * We do *not* write back to the IVTV_REG_DMASTATUS register to
641 * clear the error status, if either the encoder write (0x02) or
642 * decoder read (0x01) bus master DMA operation do not indicate
643 * completed. We can race with the DMA engine, which may have
644 * transitioned to completed status *after* we read the register.
645 * Setting a IVTV_REG_DMASTATUS flag back to "busy" status, after the
646 * DMA engine has completed, will cause the DMA engine to stop working.
647 */
648 status &= 0x3;
649 if (status == 0x3)
650 write_reg(status, IVTV_REG_DMASTATUS);
651
652 if (!test_bit(IVTV_F_I_UDMA, &itv->i_flags) &&
653 itv->cur_dma_stream >= 0 && itv->cur_dma_stream < IVTV_MAX_STREAMS) {
654 struct ivtv_stream *s = &itv->streams[itv->cur_dma_stream];
655
656 if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
657 /* retry */
658 /*
659 * FIXME - handle cases of DMA error similar to
660 * encoder below, except conditioned on status & 0x1
661 */
662 ivtv_dma_dec_start(s);
663 return;
664 } else {
665 if ((status & 0x2) == 0) {
666 /*
667 * CX2341x Bus Master DMA write is ongoing.
668 * Reset the timer and let it complete.
669 */
670 itv->dma_timer.expires =
671 jiffies + msecs_to_jiffies(600);
672 add_timer(&itv->dma_timer);
673 return;
674 }
675
676 if (itv->dma_retries < 3) {
677 /*
678 * CX2341x Bus Master DMA write has ended.
679 * Retry the write, starting with the first
680 * xfer segment. Just retrying the current
681 * segment is not sufficient.
682 */
683 s->sg_processed = 0;
684 itv->dma_retries++;
685 ivtv_dma_enc_start_xfer(s);
686 return;
687 }
688 /* Too many retries, give up on this one */
689 }
690
691 }
692 if (test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
693 ivtv_udma_start(itv);
694 return;
695 }
696 clear_bit(IVTV_F_I_UDMA, &itv->i_flags);
697 clear_bit(IVTV_F_I_DMA, &itv->i_flags);
698 itv->cur_dma_stream = -1;
699 wake_up(&itv->dma_waitq);
700}
701
702static void ivtv_irq_enc_start_cap(struct ivtv *itv)
703{
704 u32 data[CX2341X_MBOX_MAX_DATA];
705 struct ivtv_stream *s;
706
707 /* Get DMA destination and size arguments from card */
708 ivtv_api_get_data(&itv->enc_mbox, IVTV_MBOX_DMA, 7, data);
709 IVTV_DEBUG_HI_IRQ("ENC START CAP %d: %08x %08x\n", data[0], data[1], data[2]);
710
711 if (data[0] > 2 || data[1] == 0 || data[2] == 0) {
712 IVTV_DEBUG_WARN("Unknown input: %08x %08x %08x\n",
713 data[0], data[1], data[2]);
714 return;
715 }
716 s = &itv->streams[ivtv_stream_map[data[0]]];
717 if (!stream_enc_dma_append(s, data)) {
718 set_bit(ivtv_use_pio(s) ? IVTV_F_S_PIO_PENDING : IVTV_F_S_DMA_PENDING, &s->s_flags);
719 }
720}
721
722static void ivtv_irq_enc_vbi_cap(struct ivtv *itv)
723{
724 u32 data[CX2341X_MBOX_MAX_DATA];
725 struct ivtv_stream *s;
726
727 IVTV_DEBUG_HI_IRQ("ENC START VBI CAP\n");
728 s = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
729
730 if (!stream_enc_dma_append(s, data))
731 set_bit(ivtv_use_pio(s) ? IVTV_F_S_PIO_PENDING : IVTV_F_S_DMA_PENDING, &s->s_flags);
732}
733
734static void ivtv_irq_dec_vbi_reinsert(struct ivtv *itv)
735{
736 u32 data[CX2341X_MBOX_MAX_DATA];
737 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
738
739 IVTV_DEBUG_HI_IRQ("DEC VBI REINSERT\n");
740 if (test_bit(IVTV_F_S_CLAIMED, &s->s_flags) &&
741 !stream_enc_dma_append(s, data)) {
742 set_bit(IVTV_F_S_PIO_PENDING, &s->s_flags);
743 }
744}
745
746static void ivtv_irq_dec_data_req(struct ivtv *itv)
747{
748 u32 data[CX2341X_MBOX_MAX_DATA];
749 struct ivtv_stream *s;
750
751 /* YUV or MPG */
752
753 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags)) {
754 ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, 2, data);
755 itv->dma_data_req_size =
756 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
757 itv->dma_data_req_offset = data[1];
758 if (atomic_read(&itv->yuv_info.next_dma_frame) >= 0)
759 ivtv_yuv_frame_complete(itv);
760 s = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
761 }
762 else {
763 ivtv_api_get_data(&itv->dec_mbox, IVTV_MBOX_DMA, 3, data);
764 itv->dma_data_req_size = min_t(u32, data[2], 0x10000);
765 itv->dma_data_req_offset = data[1];
766 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
767 }
768 IVTV_DEBUG_HI_IRQ("DEC DATA REQ %s: %d %08x %u\n", s->name, s->q_full.bytesused,
769 itv->dma_data_req_offset, itv->dma_data_req_size);
770 if (itv->dma_data_req_size == 0 || s->q_full.bytesused < itv->dma_data_req_size) {
771 set_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
772 }
773 else {
774 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
775 ivtv_yuv_setup_stream_frame(itv);
776 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
777 ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
778 ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 0);
779 }
780}
781
782static void ivtv_irq_vsync(struct ivtv *itv)
783{
784 /* The vsync interrupt is unusual in that it won't clear until
785 * the end of the first line for the current field, at which
786 * point it clears itself. This can result in repeated vsync
787 * interrupts, or a missed vsync. Read some of the registers
788 * to determine the line being displayed and ensure we handle
789 * one vsync per frame.
790 */
791 unsigned int frame = read_reg(IVTV_REG_DEC_LINE_FIELD) & 1;
792 struct yuv_playback_info *yi = &itv->yuv_info;
793 int last_dma_frame = atomic_read(&yi->next_dma_frame);
794 struct yuv_frame_info *f = &yi->new_frame_info[last_dma_frame];
795
796 if (0) IVTV_DEBUG_IRQ("DEC VSYNC\n");
797
798 if (((frame ^ f->sync_field) == 0 &&
799 ((itv->last_vsync_field & 1) ^ f->sync_field)) ||
800 (frame != (itv->last_vsync_field & 1) && !f->interlaced)) {
801 int next_dma_frame = last_dma_frame;
802
803 if (!(f->interlaced && f->delay && yi->fields_lapsed < 1)) {
804 if (next_dma_frame >= 0 && next_dma_frame != atomic_read(&yi->next_fill_frame)) {
805 write_reg(yuv_offset[next_dma_frame] >> 4, 0x82c);
806 write_reg((yuv_offset[next_dma_frame] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x830);
807 write_reg(yuv_offset[next_dma_frame] >> 4, 0x834);
808 write_reg((yuv_offset[next_dma_frame] + IVTV_YUV_BUFFER_UV_OFFSET) >> 4, 0x838);
809 next_dma_frame = (next_dma_frame + 1) % IVTV_YUV_BUFFERS;
810 atomic_set(&yi->next_dma_frame, next_dma_frame);
811 yi->fields_lapsed = -1;
812 yi->running = 1;
813 }
814 }
815 }
816 if (frame != (itv->last_vsync_field & 1)) {
817 static const struct v4l2_event evtop = {
818 .type = V4L2_EVENT_VSYNC,
819 .u.vsync.field = V4L2_FIELD_TOP,
820 };
821 static const struct v4l2_event evbottom = {
822 .type = V4L2_EVENT_VSYNC,
823 .u.vsync.field = V4L2_FIELD_BOTTOM,
824 };
825 struct ivtv_stream *s = ivtv_get_output_stream(itv);
826
827 itv->last_vsync_field += 1;
828 if (frame == 0) {
829 clear_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
830 clear_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags);
831 }
832 else {
833 set_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags);
834 }
835 if (test_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags)) {
836 set_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags);
837 wake_up(&itv->event_waitq);
838 if (s)
839 wake_up(&s->waitq);
840 }
841 if (s && s->vdev)
842 v4l2_event_queue(s->vdev, frame ? &evtop : &evbottom);
843 wake_up(&itv->vsync_waitq);
844
845 /* Send VBI to saa7127 */
846 if (frame && (itv->output_mode == OUT_PASSTHROUGH ||
847 test_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags) ||
848 test_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags) ||
849 test_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags))) {
850 set_bit(IVTV_F_I_WORK_HANDLER_VBI, &itv->i_flags);
851 set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags);
852 }
853
854 /* Check if we need to update the yuv registers */
855 if (yi->running && (yi->yuv_forced_update || f->update)) {
856 if (!f->update) {
857 last_dma_frame =
858 (u8)(atomic_read(&yi->next_dma_frame) -
859 1) % IVTV_YUV_BUFFERS;
860 f = &yi->new_frame_info[last_dma_frame];
861 }
862
863 if (f->src_w) {
864 yi->update_frame = last_dma_frame;
865 f->update = 0;
866 yi->yuv_forced_update = 0;
867 set_bit(IVTV_F_I_WORK_HANDLER_YUV, &itv->i_flags);
868 set_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags);
869 }
870 }
871
872 yi->fields_lapsed++;
873 }
874}
875
876#define IVTV_IRQ_DMA (IVTV_IRQ_DMA_READ | IVTV_IRQ_ENC_DMA_COMPLETE | IVTV_IRQ_DMA_ERR | IVTV_IRQ_ENC_START_CAP | IVTV_IRQ_ENC_VBI_CAP | IVTV_IRQ_DEC_DATA_REQ | IVTV_IRQ_DEC_VBI_RE_INSERT)
877
878irqreturn_t ivtv_irq_handler(int irq, void *dev_id)
879{
880 struct ivtv *itv = (struct ivtv *)dev_id;
881 u32 combo;
882 u32 stat;
883 int i;
884 u8 vsync_force = 0;
885
886 spin_lock(&itv->dma_reg_lock);
887 /* get contents of irq status register */
888 stat = read_reg(IVTV_REG_IRQSTATUS);
889
890 combo = ~itv->irqmask & stat;
891
892 /* Clear out IRQ */
893 if (combo) write_reg(combo, IVTV_REG_IRQSTATUS);
894
895 if (0 == combo) {
896 /* The vsync interrupt is unusual and clears itself. If we
897 * took too long, we may have missed it. Do some checks
898 */
899 if (~itv->irqmask & IVTV_IRQ_DEC_VSYNC) {
900 /* vsync is enabled, see if we're in a new field */
901 if ((itv->last_vsync_field & 1) !=
902 (read_reg(IVTV_REG_DEC_LINE_FIELD) & 1)) {
903 /* New field, looks like we missed it */
904 IVTV_DEBUG_YUV("VSync interrupt missed %d\n",
905 read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16);
906 vsync_force = 1;
907 }
908 }
909
910 if (!vsync_force) {
911 /* No Vsync expected, wasn't for us */
912 spin_unlock(&itv->dma_reg_lock);
913 return IRQ_NONE;
914 }
915 }
916
917 /* Exclude interrupts noted below from the output, otherwise the log is flooded with
918 these messages */
919 if (combo & ~0xff6d0400)
920 IVTV_DEBUG_HI_IRQ("======= valid IRQ bits: 0x%08x ======\n", combo);
921
922 if (combo & IVTV_IRQ_DEC_DMA_COMPLETE) {
923 IVTV_DEBUG_HI_IRQ("DEC DMA COMPLETE\n");
924 }
925
926 if (combo & IVTV_IRQ_DMA_READ) {
927 ivtv_irq_dma_read(itv);
928 }
929
930 if (combo & IVTV_IRQ_ENC_DMA_COMPLETE) {
931 ivtv_irq_enc_dma_complete(itv);
932 }
933
934 if (combo & IVTV_IRQ_ENC_PIO_COMPLETE) {
935 ivtv_irq_enc_pio_complete(itv);
936 }
937
938 if (combo & IVTV_IRQ_DMA_ERR) {
939 ivtv_irq_dma_err(itv);
940 }
941
942 if (combo & IVTV_IRQ_ENC_START_CAP) {
943 ivtv_irq_enc_start_cap(itv);
944 }
945
946 if (combo & IVTV_IRQ_ENC_VBI_CAP) {
947 ivtv_irq_enc_vbi_cap(itv);
948 }
949
950 if (combo & IVTV_IRQ_DEC_VBI_RE_INSERT) {
951 ivtv_irq_dec_vbi_reinsert(itv);
952 }
953
954 if (combo & IVTV_IRQ_ENC_EOS) {
955 IVTV_DEBUG_IRQ("ENC EOS\n");
956 set_bit(IVTV_F_I_EOS, &itv->i_flags);
957 wake_up(&itv->eos_waitq);
958 }
959
960 if (combo & IVTV_IRQ_DEC_DATA_REQ) {
961 ivtv_irq_dec_data_req(itv);
962 }
963
964 /* Decoder Vertical Sync - We can't rely on 'combo', so check if vsync enabled */
965 if (~itv->irqmask & IVTV_IRQ_DEC_VSYNC) {
966 ivtv_irq_vsync(itv);
967 }
968
969 if (combo & IVTV_IRQ_ENC_VIM_RST) {
970 IVTV_DEBUG_IRQ("VIM RST\n");
971 /*ivtv_vapi(itv, CX2341X_ENC_REFRESH_INPUT, 0); */
972 }
973
974 if (combo & IVTV_IRQ_DEC_AUD_MODE_CHG) {
975 IVTV_DEBUG_INFO("Stereo mode changed\n");
976 }
977
978 if ((combo & IVTV_IRQ_DMA) && !test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
979 itv->irq_rr_idx++;
980 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
981 int idx = (i + itv->irq_rr_idx) % IVTV_MAX_STREAMS;
982 struct ivtv_stream *s = &itv->streams[idx];
983
984 if (!test_and_clear_bit(IVTV_F_S_DMA_PENDING, &s->s_flags))
985 continue;
986 if (s->type >= IVTV_DEC_STREAM_TYPE_MPG)
987 ivtv_dma_dec_start(s);
988 else
989 ivtv_dma_enc_start(s);
990 break;
991 }
992
993 if (i == IVTV_MAX_STREAMS &&
994 test_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
995 ivtv_udma_start(itv);
996 }
997
998 if ((combo & IVTV_IRQ_DMA) && !test_bit(IVTV_F_I_PIO, &itv->i_flags)) {
999 itv->irq_rr_idx++;
1000 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1001 int idx = (i + itv->irq_rr_idx) % IVTV_MAX_STREAMS;
1002 struct ivtv_stream *s = &itv->streams[idx];
1003
1004 if (!test_and_clear_bit(IVTV_F_S_PIO_PENDING, &s->s_flags))
1005 continue;
1006 if (s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type < IVTV_DEC_STREAM_TYPE_MPG)
1007 ivtv_dma_enc_start(s);
1008 break;
1009 }
1010 }
1011
1012 if (test_and_clear_bit(IVTV_F_I_HAVE_WORK, &itv->i_flags)) {
1013 queue_kthread_work(&itv->irq_worker, &itv->irq_work);
1014 }
1015
1016 spin_unlock(&itv->dma_reg_lock);
1017
1018 /* If we've just handled a 'forced' vsync, it's safest to say it
1019 * wasn't ours. Another device may have triggered it at just
1020 * the right time.
1021 */
1022 return vsync_force ? IRQ_NONE : IRQ_HANDLED;
1023}
1024
1025void ivtv_unfinished_dma(unsigned long arg)
1026{
1027 struct ivtv *itv = (struct ivtv *)arg;
1028
1029 if (!test_bit(IVTV_F_I_DMA, &itv->i_flags))
1030 return;
1031 IVTV_ERR("DMA TIMEOUT %08x %d\n", read_reg(IVTV_REG_DMASTATUS), itv->cur_dma_stream);
1032
1033 write_reg(read_reg(IVTV_REG_DMASTATUS) & 3, IVTV_REG_DMASTATUS);
1034 clear_bit(IVTV_F_I_UDMA, &itv->i_flags);
1035 clear_bit(IVTV_F_I_DMA, &itv->i_flags);
1036 itv->cur_dma_stream = -1;
1037 wake_up(&itv->dma_waitq);
1038}
diff --git a/drivers/media/pci/ivtv/ivtv-irq.h b/drivers/media/pci/ivtv/ivtv-irq.h
new file mode 100644
index 000000000000..1e84433737cc
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-irq.h
@@ -0,0 +1,53 @@
1/*
2 interrupt handling
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#ifndef IVTV_IRQ_H
23#define IVTV_IRQ_H
24
25#define IVTV_IRQ_ENC_START_CAP (0x1 << 31)
26#define IVTV_IRQ_ENC_EOS (0x1 << 30)
27#define IVTV_IRQ_ENC_VBI_CAP (0x1 << 29)
28#define IVTV_IRQ_ENC_VIM_RST (0x1 << 28)
29#define IVTV_IRQ_ENC_DMA_COMPLETE (0x1 << 27)
30#define IVTV_IRQ_ENC_PIO_COMPLETE (0x1 << 25)
31#define IVTV_IRQ_DEC_AUD_MODE_CHG (0x1 << 24)
32#define IVTV_IRQ_DEC_DATA_REQ (0x1 << 22)
33#define IVTV_IRQ_DEC_DMA_COMPLETE (0x1 << 20)
34#define IVTV_IRQ_DEC_VBI_RE_INSERT (0x1 << 19)
35#define IVTV_IRQ_DMA_ERR (0x1 << 18)
36#define IVTV_IRQ_DMA_WRITE (0x1 << 17)
37#define IVTV_IRQ_DMA_READ (0x1 << 16)
38#define IVTV_IRQ_DEC_VSYNC (0x1 << 10)
39
40/* IRQ Masks */
41#define IVTV_IRQ_MASK_INIT (IVTV_IRQ_DMA_ERR|IVTV_IRQ_ENC_DMA_COMPLETE|\
42 IVTV_IRQ_DMA_READ|IVTV_IRQ_ENC_PIO_COMPLETE)
43
44#define IVTV_IRQ_MASK_CAPTURE (IVTV_IRQ_ENC_START_CAP | IVTV_IRQ_ENC_EOS)
45#define IVTV_IRQ_MASK_DECODE (IVTV_IRQ_DEC_DATA_REQ|IVTV_IRQ_DEC_AUD_MODE_CHG)
46
47irqreturn_t ivtv_irq_handler(int irq, void *dev_id);
48
49void ivtv_irq_work_handler(struct kthread_work *work);
50void ivtv_dma_stream_dec_prepare(struct ivtv_stream *s, u32 offset, int lock);
51void ivtv_unfinished_dma(unsigned long arg);
52
53#endif
diff --git a/drivers/media/pci/ivtv/ivtv-mailbox.c b/drivers/media/pci/ivtv/ivtv-mailbox.c
new file mode 100644
index 000000000000..e3ce96763785
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-mailbox.c
@@ -0,0 +1,387 @@
1/*
2 mailbox functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <stdarg.h>
23
24#include "ivtv-driver.h"
25#include "ivtv-mailbox.h"
26
27/* Firmware mailbox flags*/
28#define IVTV_MBOX_FIRMWARE_DONE 0x00000004
29#define IVTV_MBOX_DRIVER_DONE 0x00000002
30#define IVTV_MBOX_DRIVER_BUSY 0x00000001
31#define IVTV_MBOX_FREE 0x00000000
32
33/* Firmware mailbox standard timeout */
34#define IVTV_API_STD_TIMEOUT 0x02000000
35
36#define API_CACHE (1 << 0) /* Allow the command to be stored in the cache */
37#define API_RESULT (1 << 1) /* Allow 1 second for this cmd to end */
38#define API_FAST_RESULT (3 << 1) /* Allow 0.1 second for this cmd to end */
39#define API_DMA (1 << 3) /* DMA mailbox, has special handling */
40#define API_HIGH_VOL (1 << 5) /* High volume command (i.e. called during encoding or decoding) */
41#define API_NO_WAIT_MB (1 << 4) /* Command may not wait for a free mailbox */
42#define API_NO_WAIT_RES (1 << 5) /* Command may not wait for the result */
43#define API_NO_POLL (1 << 6) /* Avoid pointless polling */
44
45struct ivtv_api_info {
46 int flags; /* Flags, see above */
47 const char *name; /* The name of the command */
48};
49
50#define API_ENTRY(x, f) [x] = { (f), #x }
51
52static const struct ivtv_api_info api_info[256] = {
53 /* MPEG encoder API */
54 API_ENTRY(CX2341X_ENC_PING_FW, API_FAST_RESULT),
55 API_ENTRY(CX2341X_ENC_START_CAPTURE, API_RESULT | API_NO_POLL),
56 API_ENTRY(CX2341X_ENC_STOP_CAPTURE, API_RESULT),
57 API_ENTRY(CX2341X_ENC_SET_AUDIO_ID, API_CACHE),
58 API_ENTRY(CX2341X_ENC_SET_VIDEO_ID, API_CACHE),
59 API_ENTRY(CX2341X_ENC_SET_PCR_ID, API_CACHE),
60 API_ENTRY(CX2341X_ENC_SET_FRAME_RATE, API_CACHE),
61 API_ENTRY(CX2341X_ENC_SET_FRAME_SIZE, API_CACHE),
62 API_ENTRY(CX2341X_ENC_SET_BIT_RATE, API_CACHE),
63 API_ENTRY(CX2341X_ENC_SET_GOP_PROPERTIES, API_CACHE),
64 API_ENTRY(CX2341X_ENC_SET_ASPECT_RATIO, API_CACHE),
65 API_ENTRY(CX2341X_ENC_SET_DNR_FILTER_MODE, API_CACHE),
66 API_ENTRY(CX2341X_ENC_SET_DNR_FILTER_PROPS, API_CACHE),
67 API_ENTRY(CX2341X_ENC_SET_CORING_LEVELS, API_CACHE),
68 API_ENTRY(CX2341X_ENC_SET_SPATIAL_FILTER_TYPE, API_CACHE),
69 API_ENTRY(CX2341X_ENC_SET_VBI_LINE, API_RESULT),
70 API_ENTRY(CX2341X_ENC_SET_STREAM_TYPE, API_CACHE),
71 API_ENTRY(CX2341X_ENC_SET_OUTPUT_PORT, API_CACHE),
72 API_ENTRY(CX2341X_ENC_SET_AUDIO_PROPERTIES, API_CACHE),
73 API_ENTRY(CX2341X_ENC_HALT_FW, API_FAST_RESULT),
74 API_ENTRY(CX2341X_ENC_GET_VERSION, API_FAST_RESULT),
75 API_ENTRY(CX2341X_ENC_SET_GOP_CLOSURE, API_CACHE),
76 API_ENTRY(CX2341X_ENC_GET_SEQ_END, API_RESULT),
77 API_ENTRY(CX2341X_ENC_SET_PGM_INDEX_INFO, API_FAST_RESULT),
78 API_ENTRY(CX2341X_ENC_SET_VBI_CONFIG, API_RESULT),
79 API_ENTRY(CX2341X_ENC_SET_DMA_BLOCK_SIZE, API_CACHE),
80 API_ENTRY(CX2341X_ENC_GET_PREV_DMA_INFO_MB_10, API_FAST_RESULT),
81 API_ENTRY(CX2341X_ENC_GET_PREV_DMA_INFO_MB_9, API_FAST_RESULT),
82 API_ENTRY(CX2341X_ENC_SCHED_DMA_TO_HOST, API_DMA | API_HIGH_VOL),
83 API_ENTRY(CX2341X_ENC_INITIALIZE_INPUT, API_RESULT),
84 API_ENTRY(CX2341X_ENC_SET_FRAME_DROP_RATE, API_CACHE),
85 API_ENTRY(CX2341X_ENC_PAUSE_ENCODER, API_RESULT),
86 API_ENTRY(CX2341X_ENC_REFRESH_INPUT, API_NO_WAIT_MB | API_HIGH_VOL),
87 API_ENTRY(CX2341X_ENC_SET_COPYRIGHT, API_CACHE),
88 API_ENTRY(CX2341X_ENC_SET_EVENT_NOTIFICATION, API_RESULT),
89 API_ENTRY(CX2341X_ENC_SET_NUM_VSYNC_LINES, API_CACHE),
90 API_ENTRY(CX2341X_ENC_SET_PLACEHOLDER, API_CACHE),
91 API_ENTRY(CX2341X_ENC_MUTE_VIDEO, API_RESULT),
92 API_ENTRY(CX2341X_ENC_MUTE_AUDIO, API_RESULT),
93 API_ENTRY(CX2341X_ENC_SET_VERT_CROP_LINE, API_FAST_RESULT),
94 API_ENTRY(CX2341X_ENC_MISC, API_FAST_RESULT),
95 /* Obsolete PULLDOWN API command */
96 API_ENTRY(0xb1, API_CACHE),
97
98 /* MPEG decoder API */
99 API_ENTRY(CX2341X_DEC_PING_FW, API_FAST_RESULT),
100 API_ENTRY(CX2341X_DEC_START_PLAYBACK, API_RESULT | API_NO_POLL),
101 API_ENTRY(CX2341X_DEC_STOP_PLAYBACK, API_RESULT),
102 API_ENTRY(CX2341X_DEC_SET_PLAYBACK_SPEED, API_RESULT),
103 API_ENTRY(CX2341X_DEC_STEP_VIDEO, API_RESULT),
104 API_ENTRY(CX2341X_DEC_SET_DMA_BLOCK_SIZE, API_CACHE),
105 API_ENTRY(CX2341X_DEC_GET_XFER_INFO, API_FAST_RESULT),
106 API_ENTRY(CX2341X_DEC_GET_DMA_STATUS, API_FAST_RESULT),
107 API_ENTRY(CX2341X_DEC_SCHED_DMA_FROM_HOST, API_DMA | API_HIGH_VOL),
108 API_ENTRY(CX2341X_DEC_PAUSE_PLAYBACK, API_RESULT),
109 API_ENTRY(CX2341X_DEC_HALT_FW, API_FAST_RESULT),
110 API_ENTRY(CX2341X_DEC_SET_STANDARD, API_CACHE),
111 API_ENTRY(CX2341X_DEC_GET_VERSION, API_FAST_RESULT),
112 API_ENTRY(CX2341X_DEC_SET_STREAM_INPUT, API_CACHE),
113 API_ENTRY(CX2341X_DEC_GET_TIMING_INFO, API_RESULT /*| API_NO_WAIT_RES*/),
114 API_ENTRY(CX2341X_DEC_SET_AUDIO_MODE, API_CACHE),
115 API_ENTRY(CX2341X_DEC_SET_EVENT_NOTIFICATION, API_RESULT),
116 API_ENTRY(CX2341X_DEC_SET_DISPLAY_BUFFERS, API_CACHE),
117 API_ENTRY(CX2341X_DEC_EXTRACT_VBI, API_RESULT),
118 API_ENTRY(CX2341X_DEC_SET_DECODER_SOURCE, API_FAST_RESULT),
119 API_ENTRY(CX2341X_DEC_SET_PREBUFFERING, API_CACHE),
120
121 /* OSD API */
122 API_ENTRY(CX2341X_OSD_GET_FRAMEBUFFER, API_FAST_RESULT),
123 API_ENTRY(CX2341X_OSD_GET_PIXEL_FORMAT, API_FAST_RESULT),
124 API_ENTRY(CX2341X_OSD_SET_PIXEL_FORMAT, API_CACHE),
125 API_ENTRY(CX2341X_OSD_GET_STATE, API_FAST_RESULT),
126 API_ENTRY(CX2341X_OSD_SET_STATE, API_CACHE),
127 API_ENTRY(CX2341X_OSD_GET_OSD_COORDS, API_FAST_RESULT),
128 API_ENTRY(CX2341X_OSD_SET_OSD_COORDS, API_CACHE),
129 API_ENTRY(CX2341X_OSD_GET_SCREEN_COORDS, API_FAST_RESULT),
130 API_ENTRY(CX2341X_OSD_SET_SCREEN_COORDS, API_CACHE),
131 API_ENTRY(CX2341X_OSD_GET_GLOBAL_ALPHA, API_FAST_RESULT),
132 API_ENTRY(CX2341X_OSD_SET_GLOBAL_ALPHA, API_CACHE),
133 API_ENTRY(CX2341X_OSD_SET_BLEND_COORDS, API_CACHE),
134 API_ENTRY(CX2341X_OSD_GET_FLICKER_STATE, API_FAST_RESULT),
135 API_ENTRY(CX2341X_OSD_SET_FLICKER_STATE, API_CACHE),
136 API_ENTRY(CX2341X_OSD_BLT_COPY, API_RESULT),
137 API_ENTRY(CX2341X_OSD_BLT_FILL, API_RESULT),
138 API_ENTRY(CX2341X_OSD_BLT_TEXT, API_RESULT),
139 API_ENTRY(CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, API_CACHE),
140 API_ENTRY(CX2341X_OSD_SET_CHROMA_KEY, API_CACHE),
141 API_ENTRY(CX2341X_OSD_GET_ALPHA_CONTENT_INDEX, API_FAST_RESULT),
142 API_ENTRY(CX2341X_OSD_SET_ALPHA_CONTENT_INDEX, API_CACHE)
143};
144
145static int try_mailbox(struct ivtv *itv, struct ivtv_mailbox_data *mbdata, int mb)
146{
147 u32 flags = readl(&mbdata->mbox[mb].flags);
148 int is_free = flags == IVTV_MBOX_FREE || (flags & IVTV_MBOX_FIRMWARE_DONE);
149
150 /* if the mailbox is free, then try to claim it */
151 if (is_free && !test_and_set_bit(mb, &mbdata->busy)) {
152 write_sync(IVTV_MBOX_DRIVER_BUSY, &mbdata->mbox[mb].flags);
153 return 1;
154 }
155 return 0;
156}
157
158/* Try to find a free mailbox. Note mailbox 0 is reserved for DMA and so is not
159 attempted here. */
160static int get_mailbox(struct ivtv *itv, struct ivtv_mailbox_data *mbdata, int flags)
161{
162 unsigned long then = jiffies;
163 int i, mb;
164 int max_mbox = mbdata->max_mbox;
165 int retries = 100;
166
167 /* All slow commands use the same mailbox, serializing them and also
168 leaving the other mailbox free for simple fast commands. */
169 if ((flags & API_FAST_RESULT) == API_RESULT)
170 max_mbox = 1;
171
172 /* find free non-DMA mailbox */
173 for (i = 0; i < retries; i++) {
174 for (mb = 1; mb <= max_mbox; mb++)
175 if (try_mailbox(itv, mbdata, mb))
176 return mb;
177
178 /* Sleep before a retry, if not atomic */
179 if (!(flags & API_NO_WAIT_MB)) {
180 if (time_after(jiffies,
181 then + msecs_to_jiffies(10*retries)))
182 break;
183 ivtv_msleep_timeout(10, 0);
184 }
185 }
186 return -ENODEV;
187}
188
189static void write_mailbox(volatile struct ivtv_mailbox __iomem *mbox, int cmd, int args, u32 data[])
190{
191 int i;
192
193 write_sync(cmd, &mbox->cmd);
194 write_sync(IVTV_API_STD_TIMEOUT, &mbox->timeout);
195
196 for (i = 0; i < CX2341X_MBOX_MAX_DATA; i++)
197 write_sync(data[i], &mbox->data[i]);
198
199 write_sync(IVTV_MBOX_DRIVER_DONE | IVTV_MBOX_DRIVER_BUSY, &mbox->flags);
200}
201
202static void clear_all_mailboxes(struct ivtv *itv, struct ivtv_mailbox_data *mbdata)
203{
204 int i;
205
206 for (i = 0; i <= mbdata->max_mbox; i++) {
207 IVTV_DEBUG_WARN("Clearing mailbox %d: cmd 0x%08x flags 0x%08x\n",
208 i, readl(&mbdata->mbox[i].cmd), readl(&mbdata->mbox[i].flags));
209 write_sync(0, &mbdata->mbox[i].flags);
210 clear_bit(i, &mbdata->busy);
211 }
212}
213
214static int ivtv_api_call(struct ivtv *itv, int cmd, int args, u32 data[])
215{
216 struct ivtv_mailbox_data *mbdata = (cmd >= 128) ? &itv->enc_mbox : &itv->dec_mbox;
217 volatile struct ivtv_mailbox __iomem *mbox;
218 int api_timeout = msecs_to_jiffies(1000);
219 int flags, mb, i;
220 unsigned long then;
221
222 /* sanity checks */
223 if (NULL == mbdata) {
224 IVTV_ERR("No mailbox allocated\n");
225 return -ENODEV;
226 }
227 if (args < 0 || args > CX2341X_MBOX_MAX_DATA ||
228 cmd < 0 || cmd > 255 || api_info[cmd].name == NULL) {
229 IVTV_ERR("Invalid MB call: cmd = 0x%02x, args = %d\n", cmd, args);
230 return -EINVAL;
231 }
232
233 if (api_info[cmd].flags & API_HIGH_VOL) {
234 IVTV_DEBUG_HI_MB("MB Call: %s\n", api_info[cmd].name);
235 }
236 else {
237 IVTV_DEBUG_MB("MB Call: %s\n", api_info[cmd].name);
238 }
239
240 /* clear possibly uninitialized part of data array */
241 for (i = args; i < CX2341X_MBOX_MAX_DATA; i++)
242 data[i] = 0;
243
244 /* If this command was issued within the last 30 minutes and with identical
245 data, then just return 0 as there is no need to issue this command again.
246 Just an optimization to prevent unnecessary use of mailboxes. */
247 if (itv->api_cache[cmd].last_jiffies &&
248 time_before(jiffies,
249 itv->api_cache[cmd].last_jiffies +
250 msecs_to_jiffies(1800000)) &&
251 !memcmp(data, itv->api_cache[cmd].data, sizeof(itv->api_cache[cmd].data))) {
252 itv->api_cache[cmd].last_jiffies = jiffies;
253 return 0;
254 }
255
256 flags = api_info[cmd].flags;
257
258 if (flags & API_DMA) {
259 for (i = 0; i < 100; i++) {
260 mb = i % (mbdata->max_mbox + 1);
261 if (try_mailbox(itv, mbdata, mb)) {
262 write_mailbox(&mbdata->mbox[mb], cmd, args, data);
263 clear_bit(mb, &mbdata->busy);
264 return 0;
265 }
266 IVTV_DEBUG_WARN("%s: mailbox %d not free %08x\n",
267 api_info[cmd].name, mb, readl(&mbdata->mbox[mb].flags));
268 }
269 IVTV_WARN("Could not find free DMA mailbox for %s\n", api_info[cmd].name);
270 clear_all_mailboxes(itv, mbdata);
271 return -EBUSY;
272 }
273
274 if ((flags & API_FAST_RESULT) == API_FAST_RESULT)
275 api_timeout = msecs_to_jiffies(100);
276
277 mb = get_mailbox(itv, mbdata, flags);
278 if (mb < 0) {
279 IVTV_DEBUG_WARN("No free mailbox found (%s)\n", api_info[cmd].name);
280 clear_all_mailboxes(itv, mbdata);
281 return -EBUSY;
282 }
283 mbox = &mbdata->mbox[mb];
284 write_mailbox(mbox, cmd, args, data);
285 if (flags & API_CACHE) {
286 memcpy(itv->api_cache[cmd].data, data, sizeof(itv->api_cache[cmd].data));
287 itv->api_cache[cmd].last_jiffies = jiffies;
288 }
289 if ((flags & API_RESULT) == 0) {
290 clear_bit(mb, &mbdata->busy);
291 return 0;
292 }
293
294 /* Get results */
295 then = jiffies;
296
297 if (!(flags & API_NO_POLL)) {
298 /* First try to poll, then switch to delays */
299 for (i = 0; i < 100; i++) {
300 if (readl(&mbox->flags) & IVTV_MBOX_FIRMWARE_DONE)
301 break;
302 }
303 }
304 while (!(readl(&mbox->flags) & IVTV_MBOX_FIRMWARE_DONE)) {
305 if (time_after(jiffies, then + api_timeout)) {
306 IVTV_DEBUG_WARN("Could not get result (%s)\n", api_info[cmd].name);
307 /* reset the mailbox, but it is likely too late already */
308 write_sync(0, &mbox->flags);
309 clear_bit(mb, &mbdata->busy);
310 return -EIO;
311 }
312 if (flags & API_NO_WAIT_RES)
313 mdelay(1);
314 else
315 ivtv_msleep_timeout(1, 0);
316 }
317 if (time_after(jiffies, then + msecs_to_jiffies(100)))
318 IVTV_DEBUG_WARN("%s took %u jiffies\n",
319 api_info[cmd].name,
320 jiffies_to_msecs(jiffies - then));
321
322 for (i = 0; i < CX2341X_MBOX_MAX_DATA; i++)
323 data[i] = readl(&mbox->data[i]);
324 write_sync(0, &mbox->flags);
325 clear_bit(mb, &mbdata->busy);
326 return 0;
327}
328
329int ivtv_api(struct ivtv *itv, int cmd, int args, u32 data[])
330{
331 int res = ivtv_api_call(itv, cmd, args, data);
332
333 /* Allow a single retry, probably already too late though.
334 If there is no free mailbox then that is usually an indication
335 of a more serious problem. */
336 return (res == -EBUSY) ? ivtv_api_call(itv, cmd, args, data) : res;
337}
338
339int ivtv_api_func(void *priv, u32 cmd, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
340{
341 return ivtv_api(priv, cmd, in, data);
342}
343
344int ivtv_vapi_result(struct ivtv *itv, u32 data[CX2341X_MBOX_MAX_DATA], int cmd, int args, ...)
345{
346 va_list ap;
347 int i;
348
349 va_start(ap, args);
350 for (i = 0; i < args; i++) {
351 data[i] = va_arg(ap, u32);
352 }
353 va_end(ap);
354 return ivtv_api(itv, cmd, args, data);
355}
356
357int ivtv_vapi(struct ivtv *itv, int cmd, int args, ...)
358{
359 u32 data[CX2341X_MBOX_MAX_DATA];
360 va_list ap;
361 int i;
362
363 va_start(ap, args);
364 for (i = 0; i < args; i++) {
365 data[i] = va_arg(ap, u32);
366 }
367 va_end(ap);
368 return ivtv_api(itv, cmd, args, data);
369}
370
371/* This one is for stuff that can't sleep.. irq handlers, etc.. */
372void ivtv_api_get_data(struct ivtv_mailbox_data *mbdata, int mb,
373 int argc, u32 data[])
374{
375 volatile u32 __iomem *p = mbdata->mbox[mb].data;
376 int i;
377 for (i = 0; i < argc; i++, p++)
378 data[i] = readl(p);
379}
380
381/* Wipe api cache */
382void ivtv_mailbox_cache_invalidate(struct ivtv *itv)
383{
384 int i;
385 for (i = 0; i < 256; i++)
386 itv->api_cache[i].last_jiffies = 0;
387}
diff --git a/drivers/media/pci/ivtv/ivtv-mailbox.h b/drivers/media/pci/ivtv/ivtv-mailbox.h
new file mode 100644
index 000000000000..2c834d2cb56f
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-mailbox.h
@@ -0,0 +1,35 @@
1/*
2 mailbox functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_MAILBOX_H
22#define IVTV_MAILBOX_H
23
24#define IVTV_MBOX_DMA_END 8
25#define IVTV_MBOX_DMA 9
26
27void ivtv_api_get_data(struct ivtv_mailbox_data *mbdata, int mb,
28 int argc, u32 data[]);
29int ivtv_api(struct ivtv *itv, int cmd, int args, u32 data[]);
30int ivtv_vapi_result(struct ivtv *itv, u32 data[CX2341X_MBOX_MAX_DATA], int cmd, int args, ...);
31int ivtv_vapi(struct ivtv *itv, int cmd, int args, ...);
32int ivtv_api_func(void *priv, u32 cmd, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA]);
33void ivtv_mailbox_cache_invalidate(struct ivtv *itv);
34
35#endif
diff --git a/drivers/media/pci/ivtv/ivtv-queue.c b/drivers/media/pci/ivtv/ivtv-queue.c
new file mode 100644
index 000000000000..7fde36e6d227
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-queue.c
@@ -0,0 +1,297 @@
1/*
2 buffer queues.
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include "ivtv-driver.h"
23#include "ivtv-queue.h"
24
25int ivtv_buf_copy_from_user(struct ivtv_stream *s, struct ivtv_buffer *buf, const char __user *src, int copybytes)
26{
27 if (s->buf_size - buf->bytesused < copybytes)
28 copybytes = s->buf_size - buf->bytesused;
29 if (copy_from_user(buf->buf + buf->bytesused, src, copybytes)) {
30 return -EFAULT;
31 }
32 buf->bytesused += copybytes;
33 return copybytes;
34}
35
36void ivtv_buf_swap(struct ivtv_buffer *buf)
37{
38 int i;
39
40 for (i = 0; i < buf->bytesused; i += 4)
41 swab32s((u32 *)(buf->buf + i));
42}
43
44void ivtv_queue_init(struct ivtv_queue *q)
45{
46 INIT_LIST_HEAD(&q->list);
47 q->buffers = 0;
48 q->length = 0;
49 q->bytesused = 0;
50}
51
52void ivtv_enqueue(struct ivtv_stream *s, struct ivtv_buffer *buf, struct ivtv_queue *q)
53{
54 unsigned long flags;
55
56 /* clear the buffer if it is going to be enqueued to the free queue */
57 if (q == &s->q_free) {
58 buf->bytesused = 0;
59 buf->readpos = 0;
60 buf->b_flags = 0;
61 buf->dma_xfer_cnt = 0;
62 }
63 spin_lock_irqsave(&s->qlock, flags);
64 list_add_tail(&buf->list, &q->list);
65 q->buffers++;
66 q->length += s->buf_size;
67 q->bytesused += buf->bytesused - buf->readpos;
68 spin_unlock_irqrestore(&s->qlock, flags);
69}
70
71struct ivtv_buffer *ivtv_dequeue(struct ivtv_stream *s, struct ivtv_queue *q)
72{
73 struct ivtv_buffer *buf = NULL;
74 unsigned long flags;
75
76 spin_lock_irqsave(&s->qlock, flags);
77 if (!list_empty(&q->list)) {
78 buf = list_entry(q->list.next, struct ivtv_buffer, list);
79 list_del_init(q->list.next);
80 q->buffers--;
81 q->length -= s->buf_size;
82 q->bytesused -= buf->bytesused - buf->readpos;
83 }
84 spin_unlock_irqrestore(&s->qlock, flags);
85 return buf;
86}
87
88static void ivtv_queue_move_buf(struct ivtv_stream *s, struct ivtv_queue *from,
89 struct ivtv_queue *to, int clear)
90{
91 struct ivtv_buffer *buf = list_entry(from->list.next, struct ivtv_buffer, list);
92
93 list_move_tail(from->list.next, &to->list);
94 from->buffers--;
95 from->length -= s->buf_size;
96 from->bytesused -= buf->bytesused - buf->readpos;
97 /* special handling for q_free */
98 if (clear)
99 buf->bytesused = buf->readpos = buf->b_flags = buf->dma_xfer_cnt = 0;
100 to->buffers++;
101 to->length += s->buf_size;
102 to->bytesused += buf->bytesused - buf->readpos;
103}
104
105/* Move 'needed_bytes' worth of buffers from queue 'from' into queue 'to'.
106 If 'needed_bytes' == 0, then move all buffers from 'from' into 'to'.
107 If 'steal' != NULL, then buffers may also taken from that queue if
108 needed, but only if 'from' is the free queue.
109
110 The buffer is automatically cleared if it goes to the free queue. It is
111 also cleared if buffers need to be taken from the 'steal' queue and
112 the 'from' queue is the free queue.
113
114 When 'from' is q_free, then needed_bytes is compared to the total
115 available buffer length, otherwise needed_bytes is compared to the
116 bytesused value. For the 'steal' queue the total available buffer
117 length is always used.
118
119 -ENOMEM is returned if the buffers could not be obtained, 0 if all
120 buffers where obtained from the 'from' list and if non-zero then
121 the number of stolen buffers is returned. */
122int ivtv_queue_move(struct ivtv_stream *s, struct ivtv_queue *from, struct ivtv_queue *steal,
123 struct ivtv_queue *to, int needed_bytes)
124{
125 unsigned long flags;
126 int rc = 0;
127 int from_free = from == &s->q_free;
128 int to_free = to == &s->q_free;
129 int bytes_available, bytes_steal;
130
131 spin_lock_irqsave(&s->qlock, flags);
132 if (needed_bytes == 0) {
133 from_free = 1;
134 needed_bytes = from->length;
135 }
136
137 bytes_available = from_free ? from->length : from->bytesused;
138 bytes_steal = (from_free && steal) ? steal->length : 0;
139
140 if (bytes_available + bytes_steal < needed_bytes) {
141 spin_unlock_irqrestore(&s->qlock, flags);
142 return -ENOMEM;
143 }
144 while (bytes_available < needed_bytes) {
145 struct ivtv_buffer *buf = list_entry(steal->list.prev, struct ivtv_buffer, list);
146 u16 dma_xfer_cnt = buf->dma_xfer_cnt;
147
148 /* move buffers from the tail of the 'steal' queue to the tail of the
149 'from' queue. Always copy all the buffers with the same dma_xfer_cnt
150 value, this ensures that you do not end up with partial frame data
151 if one frame is stored in multiple buffers. */
152 while (dma_xfer_cnt == buf->dma_xfer_cnt) {
153 list_move_tail(steal->list.prev, &from->list);
154 rc++;
155 steal->buffers--;
156 steal->length -= s->buf_size;
157 steal->bytesused -= buf->bytesused - buf->readpos;
158 buf->bytesused = buf->readpos = buf->b_flags = buf->dma_xfer_cnt = 0;
159 from->buffers++;
160 from->length += s->buf_size;
161 bytes_available += s->buf_size;
162 if (list_empty(&steal->list))
163 break;
164 buf = list_entry(steal->list.prev, struct ivtv_buffer, list);
165 }
166 }
167 if (from_free) {
168 u32 old_length = to->length;
169
170 while (to->length - old_length < needed_bytes) {
171 ivtv_queue_move_buf(s, from, to, 1);
172 }
173 }
174 else {
175 u32 old_bytesused = to->bytesused;
176
177 while (to->bytesused - old_bytesused < needed_bytes) {
178 ivtv_queue_move_buf(s, from, to, to_free);
179 }
180 }
181 spin_unlock_irqrestore(&s->qlock, flags);
182 return rc;
183}
184
185void ivtv_flush_queues(struct ivtv_stream *s)
186{
187 ivtv_queue_move(s, &s->q_io, NULL, &s->q_free, 0);
188 ivtv_queue_move(s, &s->q_full, NULL, &s->q_free, 0);
189 ivtv_queue_move(s, &s->q_dma, NULL, &s->q_free, 0);
190 ivtv_queue_move(s, &s->q_predma, NULL, &s->q_free, 0);
191}
192
193int ivtv_stream_alloc(struct ivtv_stream *s)
194{
195 struct ivtv *itv = s->itv;
196 int SGsize = sizeof(struct ivtv_sg_host_element) * s->buffers;
197 int i;
198
199 if (s->buffers == 0)
200 return 0;
201
202 IVTV_DEBUG_INFO("Allocate %s%s stream: %d x %d buffers (%dkB total)\n",
203 s->dma != PCI_DMA_NONE ? "DMA " : "",
204 s->name, s->buffers, s->buf_size, s->buffers * s->buf_size / 1024);
205
206 s->sg_pending = kzalloc(SGsize, GFP_KERNEL|__GFP_NOWARN);
207 if (s->sg_pending == NULL) {
208 IVTV_ERR("Could not allocate sg_pending for %s stream\n", s->name);
209 return -ENOMEM;
210 }
211 s->sg_pending_size = 0;
212
213 s->sg_processing = kzalloc(SGsize, GFP_KERNEL|__GFP_NOWARN);
214 if (s->sg_processing == NULL) {
215 IVTV_ERR("Could not allocate sg_processing for %s stream\n", s->name);
216 kfree(s->sg_pending);
217 s->sg_pending = NULL;
218 return -ENOMEM;
219 }
220 s->sg_processing_size = 0;
221
222 s->sg_dma = kzalloc(sizeof(struct ivtv_sg_element),
223 GFP_KERNEL|__GFP_NOWARN);
224 if (s->sg_dma == NULL) {
225 IVTV_ERR("Could not allocate sg_dma for %s stream\n", s->name);
226 kfree(s->sg_pending);
227 s->sg_pending = NULL;
228 kfree(s->sg_processing);
229 s->sg_processing = NULL;
230 return -ENOMEM;
231 }
232 if (ivtv_might_use_dma(s)) {
233 s->sg_handle = pci_map_single(itv->pdev, s->sg_dma,
234 sizeof(struct ivtv_sg_element), PCI_DMA_TODEVICE);
235 ivtv_stream_sync_for_cpu(s);
236 }
237
238 /* allocate stream buffers. Initially all buffers are in q_free. */
239 for (i = 0; i < s->buffers; i++) {
240 struct ivtv_buffer *buf = kzalloc(sizeof(struct ivtv_buffer),
241 GFP_KERNEL|__GFP_NOWARN);
242
243 if (buf == NULL)
244 break;
245 buf->buf = kmalloc(s->buf_size + 256, GFP_KERNEL|__GFP_NOWARN);
246 if (buf->buf == NULL) {
247 kfree(buf);
248 break;
249 }
250 INIT_LIST_HEAD(&buf->list);
251 if (ivtv_might_use_dma(s)) {
252 buf->dma_handle = pci_map_single(s->itv->pdev,
253 buf->buf, s->buf_size + 256, s->dma);
254 ivtv_buf_sync_for_cpu(s, buf);
255 }
256 ivtv_enqueue(s, buf, &s->q_free);
257 }
258 if (i == s->buffers)
259 return 0;
260 IVTV_ERR("Couldn't allocate buffers for %s stream\n", s->name);
261 ivtv_stream_free(s);
262 return -ENOMEM;
263}
264
265void ivtv_stream_free(struct ivtv_stream *s)
266{
267 struct ivtv_buffer *buf;
268
269 /* move all buffers to q_free */
270 ivtv_flush_queues(s);
271
272 /* empty q_free */
273 while ((buf = ivtv_dequeue(s, &s->q_free))) {
274 if (ivtv_might_use_dma(s))
275 pci_unmap_single(s->itv->pdev, buf->dma_handle,
276 s->buf_size + 256, s->dma);
277 kfree(buf->buf);
278 kfree(buf);
279 }
280
281 /* Free SG Array/Lists */
282 if (s->sg_dma != NULL) {
283 if (s->sg_handle != IVTV_DMA_UNMAPPED) {
284 pci_unmap_single(s->itv->pdev, s->sg_handle,
285 sizeof(struct ivtv_sg_element), PCI_DMA_TODEVICE);
286 s->sg_handle = IVTV_DMA_UNMAPPED;
287 }
288 kfree(s->sg_pending);
289 kfree(s->sg_processing);
290 kfree(s->sg_dma);
291 s->sg_pending = NULL;
292 s->sg_processing = NULL;
293 s->sg_dma = NULL;
294 s->sg_pending_size = 0;
295 s->sg_processing_size = 0;
296 }
297}
diff --git a/drivers/media/pci/ivtv/ivtv-queue.h b/drivers/media/pci/ivtv/ivtv-queue.h
new file mode 100644
index 000000000000..91233839a26c
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-queue.h
@@ -0,0 +1,96 @@
1/*
2 buffer queues.
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#ifndef IVTV_QUEUE_H
23#define IVTV_QUEUE_H
24
25#define IVTV_DMA_UNMAPPED ((u32) -1)
26#define SLICED_VBI_PIO 0
27
28/* ivtv_buffer utility functions */
29
30static inline int ivtv_might_use_pio(struct ivtv_stream *s)
31{
32 return s->dma == PCI_DMA_NONE || (SLICED_VBI_PIO && s->type == IVTV_ENC_STREAM_TYPE_VBI);
33}
34
35static inline int ivtv_use_pio(struct ivtv_stream *s)
36{
37 struct ivtv *itv = s->itv;
38
39 return s->dma == PCI_DMA_NONE ||
40 (SLICED_VBI_PIO && s->type == IVTV_ENC_STREAM_TYPE_VBI && itv->vbi.sliced_in->service_set);
41}
42
43static inline int ivtv_might_use_dma(struct ivtv_stream *s)
44{
45 return s->dma != PCI_DMA_NONE;
46}
47
48static inline int ivtv_use_dma(struct ivtv_stream *s)
49{
50 return !ivtv_use_pio(s);
51}
52
53static inline void ivtv_buf_sync_for_cpu(struct ivtv_stream *s, struct ivtv_buffer *buf)
54{
55 if (ivtv_use_dma(s))
56 pci_dma_sync_single_for_cpu(s->itv->pdev, buf->dma_handle,
57 s->buf_size + 256, s->dma);
58}
59
60static inline void ivtv_buf_sync_for_device(struct ivtv_stream *s, struct ivtv_buffer *buf)
61{
62 if (ivtv_use_dma(s))
63 pci_dma_sync_single_for_device(s->itv->pdev, buf->dma_handle,
64 s->buf_size + 256, s->dma);
65}
66
67int ivtv_buf_copy_from_user(struct ivtv_stream *s, struct ivtv_buffer *buf, const char __user *src, int copybytes);
68void ivtv_buf_swap(struct ivtv_buffer *buf);
69
70/* ivtv_queue utility functions */
71void ivtv_queue_init(struct ivtv_queue *q);
72void ivtv_enqueue(struct ivtv_stream *s, struct ivtv_buffer *buf, struct ivtv_queue *q);
73struct ivtv_buffer *ivtv_dequeue(struct ivtv_stream *s, struct ivtv_queue *q);
74int ivtv_queue_move(struct ivtv_stream *s, struct ivtv_queue *from, struct ivtv_queue *steal,
75 struct ivtv_queue *to, int needed_bytes);
76void ivtv_flush_queues(struct ivtv_stream *s);
77
78/* ivtv_stream utility functions */
79int ivtv_stream_alloc(struct ivtv_stream *s);
80void ivtv_stream_free(struct ivtv_stream *s);
81
82static inline void ivtv_stream_sync_for_cpu(struct ivtv_stream *s)
83{
84 if (ivtv_use_dma(s))
85 pci_dma_sync_single_for_cpu(s->itv->pdev, s->sg_handle,
86 sizeof(struct ivtv_sg_element), PCI_DMA_TODEVICE);
87}
88
89static inline void ivtv_stream_sync_for_device(struct ivtv_stream *s)
90{
91 if (ivtv_use_dma(s))
92 pci_dma_sync_single_for_device(s->itv->pdev, s->sg_handle,
93 sizeof(struct ivtv_sg_element), PCI_DMA_TODEVICE);
94}
95
96#endif
diff --git a/drivers/media/pci/ivtv/ivtv-routing.c b/drivers/media/pci/ivtv/ivtv-routing.c
new file mode 100644
index 000000000000..8898c569a1c9
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-routing.c
@@ -0,0 +1,119 @@
1/*
2 Audio/video-routing-related ivtv functions.
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "ivtv-driver.h"
22#include "ivtv-i2c.h"
23#include "ivtv-cards.h"
24#include "ivtv-gpio.h"
25#include "ivtv-routing.h"
26
27#include <media/msp3400.h>
28#include <media/m52790.h>
29#include <media/upd64031a.h>
30#include <media/upd64083.h>
31
32/* Selects the audio input and output according to the current
33 settings. */
34void ivtv_audio_set_io(struct ivtv *itv)
35{
36 const struct ivtv_card_audio_input *in;
37 u32 input, output = 0;
38
39 /* Determine which input to use */
40 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
41 in = &itv->card->radio_input;
42 else
43 in = &itv->card->audio_inputs[itv->audio_input];
44
45 /* handle muxer chips */
46 input = in->muxer_input;
47 if (itv->card->hw_muxer & IVTV_HW_M52790)
48 output = M52790_OUT_STEREO;
49 v4l2_subdev_call(itv->sd_muxer, audio, s_routing,
50 input, output, 0);
51
52 input = in->audio_input;
53 output = 0;
54 if (itv->card->hw_audio & IVTV_HW_MSP34XX)
55 output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
56 ivtv_call_hw(itv, itv->card->hw_audio, audio, s_routing,
57 input, output, 0);
58}
59
60/* Selects the video input and output according to the current
61 settings. */
62void ivtv_video_set_io(struct ivtv *itv)
63{
64 int inp = itv->active_input;
65 u32 input;
66 u32 type;
67
68 v4l2_subdev_call(itv->sd_video, video, s_routing,
69 itv->card->video_inputs[inp].video_input, 0, 0);
70
71 type = itv->card->video_inputs[inp].video_type;
72
73 if (type == IVTV_CARD_INPUT_VID_TUNER) {
74 input = 0; /* Tuner */
75 } else if (type < IVTV_CARD_INPUT_COMPOSITE1) {
76 input = 2; /* S-Video */
77 } else {
78 input = 1; /* Composite */
79 }
80
81 if (itv->card->hw_video & IVTV_HW_GPIO)
82 ivtv_call_hw(itv, IVTV_HW_GPIO, video, s_routing,
83 input, 0, 0);
84
85 if (itv->card->hw_video & IVTV_HW_UPD64031A) {
86 if (type == IVTV_CARD_INPUT_VID_TUNER ||
87 type >= IVTV_CARD_INPUT_COMPOSITE1) {
88 /* Composite: GR on, connect to 3DYCS */
89 input = UPD64031A_GR_ON | UPD64031A_3DYCS_COMPOSITE;
90 } else {
91 /* S-Video: GR bypassed, turn it off */
92 input = UPD64031A_GR_OFF | UPD64031A_3DYCS_DISABLE;
93 }
94 input |= itv->card->gr_config;
95
96 ivtv_call_hw(itv, IVTV_HW_UPD64031A, video, s_routing,
97 input, 0, 0);
98 }
99
100 if (itv->card->hw_video & IVTV_HW_UPD6408X) {
101 input = UPD64083_YCS_MODE;
102 if (type > IVTV_CARD_INPUT_VID_TUNER &&
103 type < IVTV_CARD_INPUT_COMPOSITE1) {
104 /* S-Video uses YCNR mode and internal Y-ADC, the
105 upd64031a is not used. */
106 input |= UPD64083_YCNR_MODE;
107 }
108 else if (itv->card->hw_video & IVTV_HW_UPD64031A) {
109 /* Use upd64031a output for tuner and
110 composite(CX23416GYC only) inputs */
111 if (type == IVTV_CARD_INPUT_VID_TUNER ||
112 itv->card->type == IVTV_CARD_CX23416GYC) {
113 input |= UPD64083_EXT_Y_ADC;
114 }
115 }
116 ivtv_call_hw(itv, IVTV_HW_UPD6408X, video, s_routing,
117 input, 0, 0);
118 }
119}
diff --git a/drivers/media/pci/ivtv/ivtv-routing.h b/drivers/media/pci/ivtv/ivtv-routing.h
new file mode 100644
index 000000000000..c72a9731ca01
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-routing.h
@@ -0,0 +1,27 @@
1/*
2 Audio/video-routing-related ivtv functions.
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_ROUTING_H
22#define IVTV_ROUTING_H
23
24void ivtv_audio_set_io(struct ivtv *itv);
25void ivtv_video_set_io(struct ivtv *itv);
26
27#endif
diff --git a/drivers/media/pci/ivtv/ivtv-streams.c b/drivers/media/pci/ivtv/ivtv-streams.c
new file mode 100644
index 000000000000..f08ec17cc3dc
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-streams.c
@@ -0,0 +1,1014 @@
1/*
2 init/start/stop/exit stream functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/* License: GPL
23 * Author: Kevin Thayer <nufan_wfk at yahoo dot com>
24 *
25 * This file will hold API related functions, both internal (firmware api)
26 * and external (v4l2, etc)
27 *
28 * -----
29 * MPG600/MPG160 support by T.Adachi <tadachi@tadachi-net.com>
30 * and Takeru KOMORIYA<komoriya@paken.org>
31 *
32 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org>
33 * using information provided by Jiun-Kuei Jung @ AVerMedia.
34 */
35
36#include "ivtv-driver.h"
37#include "ivtv-fileops.h"
38#include "ivtv-queue.h"
39#include "ivtv-mailbox.h"
40#include "ivtv-ioctl.h"
41#include "ivtv-irq.h"
42#include "ivtv-yuv.h"
43#include "ivtv-cards.h"
44#include "ivtv-streams.h"
45#include "ivtv-firmware.h"
46#include <media/v4l2-event.h>
47
48static const struct v4l2_file_operations ivtv_v4l2_enc_fops = {
49 .owner = THIS_MODULE,
50 .read = ivtv_v4l2_read,
51 .write = ivtv_v4l2_write,
52 .open = ivtv_v4l2_open,
53 .unlocked_ioctl = video_ioctl2,
54 .release = ivtv_v4l2_close,
55 .poll = ivtv_v4l2_enc_poll,
56};
57
58static const struct v4l2_file_operations ivtv_v4l2_dec_fops = {
59 .owner = THIS_MODULE,
60 .read = ivtv_v4l2_read,
61 .write = ivtv_v4l2_write,
62 .open = ivtv_v4l2_open,
63 .unlocked_ioctl = video_ioctl2,
64 .release = ivtv_v4l2_close,
65 .poll = ivtv_v4l2_dec_poll,
66};
67
68#define IVTV_V4L2_DEC_MPG_OFFSET 16 /* offset from 0 to register decoder mpg v4l2 minors on */
69#define IVTV_V4L2_ENC_PCM_OFFSET 24 /* offset from 0 to register pcm v4l2 minors on */
70#define IVTV_V4L2_ENC_YUV_OFFSET 32 /* offset from 0 to register yuv v4l2 minors on */
71#define IVTV_V4L2_DEC_YUV_OFFSET 48 /* offset from 0 to register decoder yuv v4l2 minors on */
72#define IVTV_V4L2_DEC_VBI_OFFSET 8 /* offset from 0 to register decoder vbi input v4l2 minors on */
73#define IVTV_V4L2_DEC_VOUT_OFFSET 16 /* offset from 0 to register vbi output v4l2 minors on */
74
75static struct {
76 const char *name;
77 int vfl_type;
78 int num_offset;
79 int dma, pio;
80 enum v4l2_buf_type buf_type;
81 u32 v4l2_caps;
82 const struct v4l2_file_operations *fops;
83} ivtv_stream_info[] = {
84 { /* IVTV_ENC_STREAM_TYPE_MPG */
85 "encoder MPG",
86 VFL_TYPE_GRABBER, 0,
87 PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VIDEO_CAPTURE,
88 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
89 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
90 &ivtv_v4l2_enc_fops
91 },
92 { /* IVTV_ENC_STREAM_TYPE_YUV */
93 "encoder YUV",
94 VFL_TYPE_GRABBER, IVTV_V4L2_ENC_YUV_OFFSET,
95 PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VIDEO_CAPTURE,
96 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
97 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
98 &ivtv_v4l2_enc_fops
99 },
100 { /* IVTV_ENC_STREAM_TYPE_VBI */
101 "encoder VBI",
102 VFL_TYPE_VBI, 0,
103 PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VBI_CAPTURE,
104 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_TUNER |
105 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
106 &ivtv_v4l2_enc_fops
107 },
108 { /* IVTV_ENC_STREAM_TYPE_PCM */
109 "encoder PCM",
110 VFL_TYPE_GRABBER, IVTV_V4L2_ENC_PCM_OFFSET,
111 PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_PRIVATE,
112 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
113 &ivtv_v4l2_enc_fops
114 },
115 { /* IVTV_ENC_STREAM_TYPE_RAD */
116 "encoder radio",
117 VFL_TYPE_RADIO, 0,
118 PCI_DMA_NONE, 1, V4L2_BUF_TYPE_PRIVATE,
119 V4L2_CAP_RADIO | V4L2_CAP_TUNER,
120 &ivtv_v4l2_enc_fops
121 },
122 { /* IVTV_DEC_STREAM_TYPE_MPG */
123 "decoder MPG",
124 VFL_TYPE_GRABBER, IVTV_V4L2_DEC_MPG_OFFSET,
125 PCI_DMA_TODEVICE, 0, V4L2_BUF_TYPE_VIDEO_OUTPUT,
126 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
127 &ivtv_v4l2_dec_fops
128 },
129 { /* IVTV_DEC_STREAM_TYPE_VBI */
130 "decoder VBI",
131 VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,
132 PCI_DMA_NONE, 1, V4L2_BUF_TYPE_VBI_CAPTURE,
133 V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_READWRITE,
134 &ivtv_v4l2_enc_fops
135 },
136 { /* IVTV_DEC_STREAM_TYPE_VOUT */
137 "decoder VOUT",
138 VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,
139 PCI_DMA_NONE, 1, V4L2_BUF_TYPE_VBI_OUTPUT,
140 V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
141 &ivtv_v4l2_dec_fops
142 },
143 { /* IVTV_DEC_STREAM_TYPE_YUV */
144 "decoder YUV",
145 VFL_TYPE_GRABBER, IVTV_V4L2_DEC_YUV_OFFSET,
146 PCI_DMA_TODEVICE, 0, V4L2_BUF_TYPE_VIDEO_OUTPUT,
147 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
148 &ivtv_v4l2_dec_fops
149 }
150};
151
152static void ivtv_stream_init(struct ivtv *itv, int type)
153{
154 struct ivtv_stream *s = &itv->streams[type];
155 struct video_device *vdev = s->vdev;
156
157 /* we need to keep vdev, so restore it afterwards */
158 memset(s, 0, sizeof(*s));
159 s->vdev = vdev;
160
161 /* initialize ivtv_stream fields */
162 s->itv = itv;
163 s->type = type;
164 s->name = ivtv_stream_info[type].name;
165 s->caps = ivtv_stream_info[type].v4l2_caps;
166
167 if (ivtv_stream_info[type].pio)
168 s->dma = PCI_DMA_NONE;
169 else
170 s->dma = ivtv_stream_info[type].dma;
171 s->buf_size = itv->stream_buf_size[type];
172 if (s->buf_size)
173 s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;
174 spin_lock_init(&s->qlock);
175 init_waitqueue_head(&s->waitq);
176 s->sg_handle = IVTV_DMA_UNMAPPED;
177 ivtv_queue_init(&s->q_free);
178 ivtv_queue_init(&s->q_full);
179 ivtv_queue_init(&s->q_dma);
180 ivtv_queue_init(&s->q_predma);
181 ivtv_queue_init(&s->q_io);
182}
183
184static int ivtv_prep_dev(struct ivtv *itv, int type)
185{
186 struct ivtv_stream *s = &itv->streams[type];
187 int num_offset = ivtv_stream_info[type].num_offset;
188 int num = itv->instance + ivtv_first_minor + num_offset;
189
190 /* These four fields are always initialized. If vdev == NULL, then
191 this stream is not in use. In that case no other fields but these
192 four can be used. */
193 s->vdev = NULL;
194 s->itv = itv;
195 s->type = type;
196 s->name = ivtv_stream_info[type].name;
197
198 /* Check whether the radio is supported */
199 if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO))
200 return 0;
201 if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
202 return 0;
203
204 /* User explicitly selected 0 buffers for these streams, so don't
205 create them. */
206 if (ivtv_stream_info[type].dma != PCI_DMA_NONE &&
207 itv->options.kilobytes[type] == 0) {
208 IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);
209 return 0;
210 }
211
212 ivtv_stream_init(itv, type);
213
214 /* allocate and initialize the v4l2 video device structure */
215 s->vdev = video_device_alloc();
216 if (s->vdev == NULL) {
217 IVTV_ERR("Couldn't allocate v4l2 video_device for %s\n", s->name);
218 return -ENOMEM;
219 }
220
221 snprintf(s->vdev->name, sizeof(s->vdev->name), "%s %s",
222 itv->v4l2_dev.name, s->name);
223
224 s->vdev->num = num;
225 s->vdev->v4l2_dev = &itv->v4l2_dev;
226 s->vdev->fops = ivtv_stream_info[type].fops;
227 s->vdev->ctrl_handler = itv->v4l2_dev.ctrl_handler;
228 s->vdev->release = video_device_release;
229 s->vdev->tvnorms = V4L2_STD_ALL;
230 s->vdev->lock = &itv->serialize_lock;
231 set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev->flags);
232 ivtv_set_funcs(s->vdev);
233 return 0;
234}
235
236/* Initialize v4l2 variables and prepare v4l2 devices */
237int ivtv_streams_setup(struct ivtv *itv)
238{
239 int type;
240
241 /* Setup V4L2 Devices */
242 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
243 /* Prepare device */
244 if (ivtv_prep_dev(itv, type))
245 break;
246
247 if (itv->streams[type].vdev == NULL)
248 continue;
249
250 /* Allocate Stream */
251 if (ivtv_stream_alloc(&itv->streams[type]))
252 break;
253 }
254 if (type == IVTV_MAX_STREAMS)
255 return 0;
256
257 /* One or more streams could not be initialized. Clean 'em all up. */
258 ivtv_streams_cleanup(itv, 0);
259 return -ENOMEM;
260}
261
262static int ivtv_reg_dev(struct ivtv *itv, int type)
263{
264 struct ivtv_stream *s = &itv->streams[type];
265 int vfl_type = ivtv_stream_info[type].vfl_type;
266 const char *name;
267 int num;
268
269 if (s->vdev == NULL)
270 return 0;
271
272 num = s->vdev->num;
273 /* card number + user defined offset + device offset */
274 if (type != IVTV_ENC_STREAM_TYPE_MPG) {
275 struct ivtv_stream *s_mpg = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG];
276
277 if (s_mpg->vdev)
278 num = s_mpg->vdev->num + ivtv_stream_info[type].num_offset;
279 }
280 video_set_drvdata(s->vdev, s);
281
282 /* Register device. First try the desired minor, then any free one. */
283 if (video_register_device_no_warn(s->vdev, vfl_type, num)) {
284 IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
285 s->name, num);
286 video_device_release(s->vdev);
287 s->vdev = NULL;
288 return -ENOMEM;
289 }
290 name = video_device_node_name(s->vdev);
291
292 switch (vfl_type) {
293 case VFL_TYPE_GRABBER:
294 IVTV_INFO("Registered device %s for %s (%d kB)\n",
295 name, s->name, itv->options.kilobytes[type]);
296 break;
297 case VFL_TYPE_RADIO:
298 IVTV_INFO("Registered device %s for %s\n",
299 name, s->name);
300 break;
301 case VFL_TYPE_VBI:
302 if (itv->options.kilobytes[type])
303 IVTV_INFO("Registered device %s for %s (%d kB)\n",
304 name, s->name, itv->options.kilobytes[type]);
305 else
306 IVTV_INFO("Registered device %s for %s\n",
307 name, s->name);
308 break;
309 }
310 return 0;
311}
312
313/* Register v4l2 devices */
314int ivtv_streams_register(struct ivtv *itv)
315{
316 int type;
317 int err = 0;
318
319 /* Register V4L2 devices */
320 for (type = 0; type < IVTV_MAX_STREAMS; type++)
321 err |= ivtv_reg_dev(itv, type);
322
323 if (err == 0)
324 return 0;
325
326 /* One or more streams could not be initialized. Clean 'em all up. */
327 ivtv_streams_cleanup(itv, 1);
328 return -ENOMEM;
329}
330
331/* Unregister v4l2 devices */
332void ivtv_streams_cleanup(struct ivtv *itv, int unregister)
333{
334 int type;
335
336 /* Teardown all streams */
337 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
338 struct video_device *vdev = itv->streams[type].vdev;
339
340 itv->streams[type].vdev = NULL;
341 if (vdev == NULL)
342 continue;
343
344 ivtv_stream_free(&itv->streams[type]);
345 /* Unregister or release device */
346 if (unregister)
347 video_unregister_device(vdev);
348 else
349 video_device_release(vdev);
350 }
351}
352
353static void ivtv_vbi_setup(struct ivtv *itv)
354{
355 int raw = ivtv_raw_vbi(itv);
356 u32 data[CX2341X_MBOX_MAX_DATA];
357 int lines;
358 int i;
359
360 /* Reset VBI */
361 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);
362
363 /* setup VBI registers */
364 if (raw)
365 v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &itv->vbi.in.fmt.vbi);
366 else
367 v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, &itv->vbi.in.fmt.sliced);
368
369 /* determine number of lines and total number of VBI bytes.
370 A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
371 The '- 1' byte is probably an unused U or V byte. Or something...
372 A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
373 header, 42 data bytes + checksum (to be confirmed) */
374 if (raw) {
375 lines = itv->vbi.count * 2;
376 } else {
377 lines = itv->is_60hz ? 24 : 38;
378 if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840))
379 lines += 2;
380 }
381
382 itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
383
384 /* Note: sliced vs raw flag doesn't seem to have any effect
385 TODO: check mode (0x02) value with older ivtv versions. */
386 data[0] = raw | 0x02 | (0xbd << 8);
387
388 /* Every X number of frames a VBI interrupt arrives (frames as in 25 or 30 fps) */
389 data[1] = 1;
390 /* The VBI frames are stored in a ringbuffer with this size (with a VBI frame as unit) */
391 data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size);
392 /* The start/stop codes determine which VBI lines end up in the raw VBI data area.
393 The codes are from table 24 in the saa7115 datasheet. Each raw/sliced/video line
394 is framed with codes FF0000XX where XX is the SAV/EAV (Start/End of Active Video)
395 code. These values for raw VBI are obtained from a driver disassembly. The sliced
396 start/stop codes was deduced from this, but they do not appear in the driver.
397 Other code pairs that I found are: 0x250E6249/0x13545454 and 0x25256262/0x38137F54.
398 However, I have no idea what these values are for. */
399 if (itv->hw_flags & IVTV_HW_CX25840) {
400 /* Setup VBI for the cx25840 digitizer */
401 if (raw) {
402 data[3] = 0x20602060;
403 data[4] = 0x30703070;
404 } else {
405 data[3] = 0xB0F0B0F0;
406 data[4] = 0xA0E0A0E0;
407 }
408 /* Lines per frame */
409 data[5] = lines;
410 /* bytes per line */
411 data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
412 } else {
413 /* Setup VBI for the saa7115 digitizer */
414 if (raw) {
415 data[3] = 0x25256262;
416 data[4] = 0x387F7F7F;
417 } else {
418 data[3] = 0xABABECEC;
419 data[4] = 0xB6F1F1F1;
420 }
421 /* Lines per frame */
422 data[5] = lines;
423 /* bytes per line */
424 data[6] = itv->vbi.enc_size / lines;
425 }
426
427 IVTV_DEBUG_INFO(
428 "Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n",
429 data[0], data[1], data[2], data[5], data[6]);
430
431 ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data);
432
433 /* returns the VBI encoder memory area. */
434 itv->vbi.enc_start = data[2];
435 itv->vbi.fpi = data[0];
436 if (!itv->vbi.fpi)
437 itv->vbi.fpi = 1;
438
439 IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n",
440 itv->vbi.enc_start, data[1], itv->vbi.fpi);
441
442 /* select VBI lines.
443 Note that the sliced argument seems to have no effect. */
444 for (i = 2; i <= 24; i++) {
445 int valid;
446
447 if (itv->is_60hz) {
448 valid = i >= 10 && i < 22;
449 } else {
450 valid = i >= 6 && i < 24;
451 }
452 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1,
453 valid, 0 , 0, 0);
454 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000,
455 valid, 0, 0, 0);
456 }
457
458 /* Remaining VBI questions:
459 - Is it possible to select particular VBI lines only for inclusion in the MPEG
460 stream? Currently you can only get the first X lines.
461 - Is mixed raw and sliced VBI possible?
462 - What's the meaning of the raw/sliced flag?
463 - What's the meaning of params 2, 3 & 4 of the Select VBI command? */
464}
465
466int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
467{
468 u32 data[CX2341X_MBOX_MAX_DATA];
469 struct ivtv *itv = s->itv;
470 int captype = 0, subtype = 0;
471 int enable_passthrough = 0;
472
473 if (s->vdev == NULL)
474 return -EINVAL;
475
476 IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name);
477
478 switch (s->type) {
479 case IVTV_ENC_STREAM_TYPE_MPG:
480 captype = 0;
481 subtype = 3;
482
483 /* Stop Passthrough */
484 if (itv->output_mode == OUT_PASSTHROUGH) {
485 ivtv_passthrough_mode(itv, 0);
486 enable_passthrough = 1;
487 }
488 itv->mpg_data_received = itv->vbi_data_inserted = 0;
489 itv->dualwatch_jiffies = jiffies;
490 itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
491 itv->search_pack_header = 0;
492 break;
493
494 case IVTV_ENC_STREAM_TYPE_YUV:
495 if (itv->output_mode == OUT_PASSTHROUGH) {
496 captype = 2;
497 subtype = 11; /* video+audio+decoder */
498 break;
499 }
500 captype = 1;
501 subtype = 1;
502 break;
503 case IVTV_ENC_STREAM_TYPE_PCM:
504 captype = 1;
505 subtype = 2;
506 break;
507 case IVTV_ENC_STREAM_TYPE_VBI:
508 captype = 1;
509 subtype = 4;
510
511 itv->vbi.frame = 0;
512 itv->vbi.inserted_frame = 0;
513 memset(itv->vbi.sliced_mpeg_size,
514 0, sizeof(itv->vbi.sliced_mpeg_size));
515 break;
516 default:
517 return -EINVAL;
518 }
519 s->subtype = subtype;
520 s->buffers_stolen = 0;
521
522 /* Clear Streamoff flags in case left from last capture */
523 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
524
525 if (atomic_read(&itv->capturing) == 0) {
526 int digitizer;
527
528 /* Always use frame based mode. Experiments have demonstrated that byte
529 stream based mode results in dropped frames and corruption. Not often,
530 but occasionally. Many thanks go to Leonard Orb who spent a lot of
531 effort and time trying to trace the cause of the drop outs. */
532 /* 1 frame per DMA */
533 /*ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 128, 0); */
534 ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1);
535
536 /* Stuff from Windows, we don't know what it is */
537 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0);
538 /* According to the docs, this should be correct. However, this is
539 untested. I don't dare enable this without having tested it.
540 Only very few old cards actually have this hardware combination.
541 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1,
542 ((itv->hw_flags & IVTV_HW_SAA7114) && itv->is_60hz) ? 10001 : 0);
543 */
544 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415);
545 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0);
546 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1);
547 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
548
549 /* assign placeholder */
550 ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12,
551 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
552
553 if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X))
554 digitizer = 0xF1;
555 else if (itv->card->hw_all & IVTV_HW_SAA7114)
556 digitizer = 0xEF;
557 else /* cx25840 */
558 digitizer = 0x140;
559
560 ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, digitizer, digitizer);
561
562 /* Setup VBI */
563 if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) {
564 ivtv_vbi_setup(itv);
565 }
566
567 /* assign program index info. Mask 7: select I/P/B, Num_req: 400 max */
568 ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400);
569 itv->pgm_info_offset = data[0];
570 itv->pgm_info_num = data[1];
571 itv->pgm_info_write_idx = 0;
572 itv->pgm_info_read_idx = 0;
573
574 IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n",
575 itv->pgm_info_offset, itv->pgm_info_num);
576
577 /* Setup API for Stream */
578 cx2341x_handler_setup(&itv->cxhdl);
579
580 /* mute if capturing radio */
581 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
582 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
583 1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
584 }
585
586 /* Vsync Setup */
587 if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
588 /* event notification (on) */
589 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1);
590 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
591 }
592
593 if (atomic_read(&itv->capturing) == 0) {
594 /* Clear all Pending Interrupts */
595 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
596
597 clear_bit(IVTV_F_I_EOS, &itv->i_flags);
598
599 cx2341x_handler_set_busy(&itv->cxhdl, 1);
600
601 /* Initialize Digitizer for Capture */
602 /* Avoid tinny audio problem - ensure audio clocks are going */
603 v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1);
604 /* Avoid unpredictable PCI bus hang - disable video clocks */
605 v4l2_subdev_call(itv->sd_video, video, s_stream, 0);
606 ivtv_msleep_timeout(300, 0);
607 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
608 v4l2_subdev_call(itv->sd_video, video, s_stream, 1);
609 }
610
611 /* begin_capture */
612 if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype))
613 {
614 IVTV_DEBUG_WARN( "Error starting capture!\n");
615 return -EINVAL;
616 }
617
618 /* Start Passthrough */
619 if (enable_passthrough) {
620 ivtv_passthrough_mode(itv, 1);
621 }
622
623 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
624 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
625 else
626 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
627
628 /* you're live! sit back and await interrupts :) */
629 atomic_inc(&itv->capturing);
630 return 0;
631}
632
633static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
634{
635 u32 data[CX2341X_MBOX_MAX_DATA];
636 struct ivtv *itv = s->itv;
637 int datatype;
638 u16 width;
639 u16 height;
640
641 if (s->vdev == NULL)
642 return -EINVAL;
643
644 IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
645
646 width = itv->cxhdl.width;
647 height = itv->cxhdl.height;
648
649 /* set audio mode to left/stereo for dual/stereo mode. */
650 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
651
652 /* set number of internal decoder buffers */
653 ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0);
654
655 /* prebuffering */
656 ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1);
657
658 /* extract from user packets */
659 ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1);
660 itv->vbi.dec_start = data[0];
661
662 IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n",
663 itv->vbi.dec_start, data[1]);
664
665 /* set decoder source settings */
666 /* Data type: 0 = mpeg from host,
667 1 = yuv from encoder,
668 2 = yuv_from_host */
669 switch (s->type) {
670 case IVTV_DEC_STREAM_TYPE_YUV:
671 if (itv->output_mode == OUT_PASSTHROUGH) {
672 datatype = 1;
673 } else {
674 /* Fake size to avoid switching video standard */
675 datatype = 2;
676 width = 720;
677 height = itv->is_out_50hz ? 576 : 480;
678 }
679 IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype);
680 break;
681 case IVTV_DEC_STREAM_TYPE_MPG:
682 default:
683 datatype = 0;
684 break;
685 }
686 if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,
687 width, height, itv->cxhdl.audio_properties)) {
688 IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
689 }
690
691 /* Decoder sometimes dies here, so wait a moment */
692 ivtv_msleep_timeout(10, 0);
693
694 /* Known failure point for firmware, so check */
695 return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream");
696}
697
698int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
699{
700 struct ivtv *itv = s->itv;
701 int rc;
702
703 if (s->vdev == NULL)
704 return -EINVAL;
705
706 if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags))
707 return 0; /* already started */
708
709 IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);
710
711 rc = ivtv_setup_v4l2_decode_stream(s);
712 if (rc < 0) {
713 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
714 return rc;
715 }
716
717 /* set dma size to 65536 bytes */
718 ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
719
720 /* Clear Streamoff */
721 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
722
723 /* Zero out decoder counters */
724 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[0]);
725 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[1]);
726 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[2]);
727 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[3]);
728 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]);
729 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]);
730 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]);
731 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]);
732
733 /* turn on notification of dual/stereo mode change */
734 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
735
736 /* start playback */
737 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0);
738
739 /* Let things settle before we actually start */
740 ivtv_msleep_timeout(10, 0);
741
742 /* Clear the following Interrupt mask bits for decoding */
743 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
744 IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask);
745
746 /* you're live! sit back and await interrupts :) */
747 atomic_inc(&itv->decoding);
748 return 0;
749}
750
751void ivtv_stop_all_captures(struct ivtv *itv)
752{
753 int i;
754
755 for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) {
756 struct ivtv_stream *s = &itv->streams[i];
757
758 if (s->vdev == NULL)
759 continue;
760 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
761 ivtv_stop_v4l2_encode_stream(s, 0);
762 }
763 }
764}
765
766int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)
767{
768 struct ivtv *itv = s->itv;
769 DECLARE_WAITQUEUE(wait, current);
770 int cap_type;
771 int stopmode;
772
773 if (s->vdev == NULL)
774 return -EINVAL;
775
776 /* This function assumes that you are allowed to stop the capture
777 and that we are actually capturing */
778
779 IVTV_DEBUG_INFO("Stop Capture\n");
780
781 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT)
782 return 0;
783 if (atomic_read(&itv->capturing) == 0)
784 return 0;
785
786 switch (s->type) {
787 case IVTV_ENC_STREAM_TYPE_YUV:
788 cap_type = 1;
789 break;
790 case IVTV_ENC_STREAM_TYPE_PCM:
791 cap_type = 1;
792 break;
793 case IVTV_ENC_STREAM_TYPE_VBI:
794 cap_type = 1;
795 break;
796 case IVTV_ENC_STREAM_TYPE_MPG:
797 default:
798 cap_type = 0;
799 break;
800 }
801
802 /* Stop Capture Mode */
803 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
804 stopmode = 0;
805 } else {
806 stopmode = 1;
807 }
808
809 /* end_capture */
810 /* when: 0 = end of GOP 1 = NOW!, type: 0 = mpeg, subtype: 3 = video+audio */
811 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype);
812
813 if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) {
814 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
815 /* only run these if we're shutting down the last cap */
816 unsigned long duration;
817 unsigned long then = jiffies;
818
819 add_wait_queue(&itv->eos_waitq, &wait);
820
821 set_current_state(TASK_INTERRUPTIBLE);
822
823 /* wait 2s for EOS interrupt */
824 while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&
825 time_before(jiffies,
826 then + msecs_to_jiffies(2000))) {
827 schedule_timeout(msecs_to_jiffies(10));
828 }
829
830 /* To convert jiffies to ms, we must multiply by 1000
831 * and divide by HZ. To avoid runtime division, we
832 * convert this to multiplication by 1000/HZ.
833 * Since integer division truncates, we get the best
834 * accuracy if we do a rounding calculation of the constant.
835 * Think of the case where HZ is 1024.
836 */
837 duration = ((1000 + HZ / 2) / HZ) * (jiffies - then);
838
839 if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) {
840 IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name);
841 IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration);
842 } else {
843 IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration);
844 }
845 set_current_state(TASK_RUNNING);
846 remove_wait_queue(&itv->eos_waitq, &wait);
847 set_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
848 }
849
850 /* Handle any pending interrupts */
851 ivtv_msleep_timeout(100, 0);
852 }
853
854 atomic_dec(&itv->capturing);
855
856 /* Clear capture and no-read bits */
857 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
858
859 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
860 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
861
862 if (atomic_read(&itv->capturing) > 0) {
863 return 0;
864 }
865
866 cx2341x_handler_set_busy(&itv->cxhdl, 0);
867
868 /* Set the following Interrupt mask bits for capture */
869 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
870 del_timer(&itv->dma_timer);
871
872 /* event notification (off) */
873 if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
874 /* type: 0 = refresh */
875 /* on/off: 0 = off, intr: 0x10000000, mbox_id: -1: none */
876 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1);
877 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
878 }
879
880 /* Raw-passthrough is implied on start. Make sure it's stopped so
881 the encoder will re-initialize when next started */
882 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 7);
883
884 wake_up(&s->waitq);
885
886 return 0;
887}
888
889int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)
890{
891 static const struct v4l2_event ev = {
892 .type = V4L2_EVENT_EOS,
893 };
894 struct ivtv *itv = s->itv;
895
896 if (s->vdev == NULL)
897 return -EINVAL;
898
899 if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG)
900 return -EINVAL;
901
902 if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags))
903 return 0;
904
905 IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);
906
907 /* Stop Decoder */
908 if (!(flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) || pts) {
909 u32 tmp = 0;
910
911 /* Wait until the decoder is no longer running */
912 if (pts) {
913 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3,
914 0, (u32)(pts & 0xffffffff), (u32)(pts >> 32));
915 }
916 while (1) {
917 u32 data[CX2341X_MBOX_MAX_DATA];
918 ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0);
919 if (s->q_full.buffers + s->q_dma.buffers == 0) {
920 if (tmp == data[3])
921 break;
922 tmp = data[3];
923 }
924 if (ivtv_msleep_timeout(100, 1))
925 break;
926 }
927 }
928 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & V4L2_DEC_CMD_STOP_TO_BLACK, 0, 0);
929
930 /* turn off notification of dual/stereo mode change */
931 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
932
933 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
934 del_timer(&itv->dma_timer);
935
936 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
937 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
938 ivtv_flush_queues(s);
939
940 /* decoder needs time to settle */
941 ivtv_msleep_timeout(40, 0);
942
943 /* decrement decoding */
944 atomic_dec(&itv->decoding);
945
946 set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags);
947 wake_up(&itv->event_waitq);
948 v4l2_event_queue(s->vdev, &ev);
949
950 /* wake up wait queues */
951 wake_up(&s->waitq);
952
953 return 0;
954}
955
956int ivtv_passthrough_mode(struct ivtv *itv, int enable)
957{
958 struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV];
959 struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
960
961 if (yuv_stream->vdev == NULL || dec_stream->vdev == NULL)
962 return -EINVAL;
963
964 IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n");
965
966 /* Prevent others from starting/stopping streams while we
967 initiate/terminate passthrough mode */
968 if (enable) {
969 if (itv->output_mode == OUT_PASSTHROUGH) {
970 return 0;
971 }
972 if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH)
973 return -EBUSY;
974
975 /* Fully initialize stream, and then unflag init */
976 set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
977 set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
978
979 /* Setup YUV Decoder */
980 ivtv_setup_v4l2_decode_stream(dec_stream);
981
982 /* Start Decoder */
983 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1);
984 atomic_inc(&itv->decoding);
985
986 /* Setup capture if not already done */
987 if (atomic_read(&itv->capturing) == 0) {
988 cx2341x_handler_setup(&itv->cxhdl);
989 cx2341x_handler_set_busy(&itv->cxhdl, 1);
990 }
991
992 /* Start Passthrough Mode */
993 ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11);
994 atomic_inc(&itv->capturing);
995 return 0;
996 }
997
998 if (itv->output_mode != OUT_PASSTHROUGH)
999 return 0;
1000
1001 /* Stop Passthrough Mode */
1002 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11);
1003 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0);
1004
1005 atomic_dec(&itv->capturing);
1006 atomic_dec(&itv->decoding);
1007 clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
1008 clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
1009 itv->output_mode = OUT_NONE;
1010 if (atomic_read(&itv->capturing) == 0)
1011 cx2341x_handler_set_busy(&itv->cxhdl, 0);
1012
1013 return 0;
1014}
diff --git a/drivers/media/pci/ivtv/ivtv-streams.h b/drivers/media/pci/ivtv/ivtv-streams.h
new file mode 100644
index 000000000000..a653a5136417
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-streams.h
@@ -0,0 +1,37 @@
1/*
2 init/start/stop/exit stream functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_STREAMS_H
22#define IVTV_STREAMS_H
23
24int ivtv_streams_setup(struct ivtv *itv);
25int ivtv_streams_register(struct ivtv *itv);
26void ivtv_streams_cleanup(struct ivtv *itv, int unregister);
27
28/* Capture related */
29int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s);
30int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end);
31int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset);
32int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts);
33
34void ivtv_stop_all_captures(struct ivtv *itv);
35int ivtv_passthrough_mode(struct ivtv *itv, int enable);
36
37#endif
diff --git a/drivers/media/pci/ivtv/ivtv-udma.c b/drivers/media/pci/ivtv/ivtv-udma.c
new file mode 100644
index 000000000000..7338cb2d0a38
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-udma.c
@@ -0,0 +1,234 @@
1/*
2 User DMA
3
4 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
5 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
6 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include "ivtv-driver.h"
24#include "ivtv-udma.h"
25
26void ivtv_udma_get_page_info(struct ivtv_dma_page_info *dma_page, unsigned long first, unsigned long size)
27{
28 dma_page->uaddr = first & PAGE_MASK;
29 dma_page->offset = first & ~PAGE_MASK;
30 dma_page->tail = 1 + ((first+size-1) & ~PAGE_MASK);
31 dma_page->first = (first & PAGE_MASK) >> PAGE_SHIFT;
32 dma_page->last = ((first+size-1) & PAGE_MASK) >> PAGE_SHIFT;
33 dma_page->page_count = dma_page->last - dma_page->first + 1;
34 if (dma_page->page_count == 1) dma_page->tail -= dma_page->offset;
35}
36
37int ivtv_udma_fill_sg_list (struct ivtv_user_dma *dma, struct ivtv_dma_page_info *dma_page, int map_offset)
38{
39 int i, offset;
40 unsigned long flags;
41
42 if (map_offset < 0)
43 return map_offset;
44
45 offset = dma_page->offset;
46
47 /* Fill SG Array with new values */
48 for (i = 0; i < dma_page->page_count; i++) {
49 unsigned int len = (i == dma_page->page_count - 1) ?
50 dma_page->tail : PAGE_SIZE - offset;
51
52 if (PageHighMem(dma->map[map_offset])) {
53 void *src;
54
55 if (dma->bouncemap[map_offset] == NULL)
56 dma->bouncemap[map_offset] = alloc_page(GFP_KERNEL);
57 if (dma->bouncemap[map_offset] == NULL)
58 return -1;
59 local_irq_save(flags);
60 src = kmap_atomic(dma->map[map_offset]) + offset;
61 memcpy(page_address(dma->bouncemap[map_offset]) + offset, src, len);
62 kunmap_atomic(src);
63 local_irq_restore(flags);
64 sg_set_page(&dma->SGlist[map_offset], dma->bouncemap[map_offset], len, offset);
65 }
66 else {
67 sg_set_page(&dma->SGlist[map_offset], dma->map[map_offset], len, offset);
68 }
69 offset = 0;
70 map_offset++;
71 }
72 return map_offset;
73}
74
75void ivtv_udma_fill_sg_array (struct ivtv_user_dma *dma, u32 buffer_offset, u32 buffer_offset_2, u32 split) {
76 int i;
77 struct scatterlist *sg;
78
79 for (i = 0, sg = dma->SGlist; i < dma->SG_length; i++, sg++) {
80 dma->SGarray[i].size = cpu_to_le32(sg_dma_len(sg));
81 dma->SGarray[i].src = cpu_to_le32(sg_dma_address(sg));
82 dma->SGarray[i].dst = cpu_to_le32(buffer_offset);
83 buffer_offset += sg_dma_len(sg);
84
85 split -= sg_dma_len(sg);
86 if (split == 0)
87 buffer_offset = buffer_offset_2;
88 }
89}
90
91/* User DMA Buffers */
92void ivtv_udma_alloc(struct ivtv *itv)
93{
94 if (itv->udma.SG_handle == 0) {
95 /* Map DMA Page Array Buffer */
96 itv->udma.SG_handle = pci_map_single(itv->pdev, itv->udma.SGarray,
97 sizeof(itv->udma.SGarray), PCI_DMA_TODEVICE);
98 ivtv_udma_sync_for_cpu(itv);
99 }
100}
101
102int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr,
103 void __user *userbuf, int size_in_bytes)
104{
105 struct ivtv_dma_page_info user_dma;
106 struct ivtv_user_dma *dma = &itv->udma;
107 int i, err;
108
109 IVTV_DEBUG_DMA("ivtv_udma_setup, dst: 0x%08x\n", (unsigned int)ivtv_dest_addr);
110
111 /* Still in USE */
112 if (dma->SG_length || dma->page_count) {
113 IVTV_DEBUG_WARN("ivtv_udma_setup: SG_length %d page_count %d still full?\n",
114 dma->SG_length, dma->page_count);
115 return -EBUSY;
116 }
117
118 ivtv_udma_get_page_info(&user_dma, (unsigned long)userbuf, size_in_bytes);
119
120 if (user_dma.page_count <= 0) {
121 IVTV_DEBUG_WARN("ivtv_udma_setup: Error %d page_count from %d bytes %d offset\n",
122 user_dma.page_count, size_in_bytes, user_dma.offset);
123 return -EINVAL;
124 }
125
126 /* Get user pages for DMA Xfer */
127 down_read(&current->mm->mmap_sem);
128 err = get_user_pages(current, current->mm,
129 user_dma.uaddr, user_dma.page_count, 0, 1, dma->map, NULL);
130 up_read(&current->mm->mmap_sem);
131
132 if (user_dma.page_count != err) {
133 IVTV_DEBUG_WARN("failed to map user pages, returned %d instead of %d\n",
134 err, user_dma.page_count);
135 if (err >= 0) {
136 for (i = 0; i < err; i++)
137 put_page(dma->map[i]);
138 return -EINVAL;
139 }
140 return err;
141 }
142
143 dma->page_count = user_dma.page_count;
144
145 /* Fill SG List with new values */
146 if (ivtv_udma_fill_sg_list(dma, &user_dma, 0) < 0) {
147 for (i = 0; i < dma->page_count; i++) {
148 put_page(dma->map[i]);
149 }
150 dma->page_count = 0;
151 return -ENOMEM;
152 }
153
154 /* Map SG List */
155 dma->SG_length = pci_map_sg(itv->pdev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE);
156
157 /* Fill SG Array with new values */
158 ivtv_udma_fill_sg_array (dma, ivtv_dest_addr, 0, -1);
159
160 /* Tag SG Array with Interrupt Bit */
161 dma->SGarray[dma->SG_length - 1].size |= cpu_to_le32(0x80000000);
162
163 ivtv_udma_sync_for_device(itv);
164 return dma->page_count;
165}
166
167void ivtv_udma_unmap(struct ivtv *itv)
168{
169 struct ivtv_user_dma *dma = &itv->udma;
170 int i;
171
172 IVTV_DEBUG_INFO("ivtv_unmap_user_dma\n");
173
174 /* Nothing to free */
175 if (dma->page_count == 0)
176 return;
177
178 /* Unmap Scatterlist */
179 if (dma->SG_length) {
180 pci_unmap_sg(itv->pdev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE);
181 dma->SG_length = 0;
182 }
183 /* sync DMA */
184 ivtv_udma_sync_for_cpu(itv);
185
186 /* Release User Pages */
187 for (i = 0; i < dma->page_count; i++) {
188 put_page(dma->map[i]);
189 }
190 dma->page_count = 0;
191}
192
193void ivtv_udma_free(struct ivtv *itv)
194{
195 int i;
196
197 /* Unmap SG Array */
198 if (itv->udma.SG_handle) {
199 pci_unmap_single(itv->pdev, itv->udma.SG_handle,
200 sizeof(itv->udma.SGarray), PCI_DMA_TODEVICE);
201 }
202
203 /* Unmap Scatterlist */
204 if (itv->udma.SG_length) {
205 pci_unmap_sg(itv->pdev, itv->udma.SGlist, itv->udma.page_count, PCI_DMA_TODEVICE);
206 }
207
208 for (i = 0; i < IVTV_DMA_SG_OSD_ENT; i++) {
209 if (itv->udma.bouncemap[i])
210 __free_page(itv->udma.bouncemap[i]);
211 }
212}
213
214void ivtv_udma_start(struct ivtv *itv)
215{
216 IVTV_DEBUG_DMA("start UDMA\n");
217 write_reg(itv->udma.SG_handle, IVTV_REG_DECDMAADDR);
218 write_reg_sync(read_reg(IVTV_REG_DMAXFER) | 0x01, IVTV_REG_DMAXFER);
219 set_bit(IVTV_F_I_DMA, &itv->i_flags);
220 set_bit(IVTV_F_I_UDMA, &itv->i_flags);
221 clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags);
222}
223
224void ivtv_udma_prepare(struct ivtv *itv)
225{
226 unsigned long flags;
227
228 spin_lock_irqsave(&itv->dma_reg_lock, flags);
229 if (!test_bit(IVTV_F_I_DMA, &itv->i_flags))
230 ivtv_udma_start(itv);
231 else
232 set_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags);
233 spin_unlock_irqrestore(&itv->dma_reg_lock, flags);
234}
diff --git a/drivers/media/pci/ivtv/ivtv-udma.h b/drivers/media/pci/ivtv/ivtv-udma.h
new file mode 100644
index 000000000000..ee3c9efb5b72
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-udma.h
@@ -0,0 +1,48 @@
1/*
2 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
3 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
4 Copyright (C) 2006-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_UDMA_H
22#define IVTV_UDMA_H
23
24/* User DMA functions */
25void ivtv_udma_get_page_info(struct ivtv_dma_page_info *dma_page, unsigned long first, unsigned long size);
26int ivtv_udma_fill_sg_list(struct ivtv_user_dma *dma, struct ivtv_dma_page_info *dma_page, int map_offset);
27void ivtv_udma_fill_sg_array(struct ivtv_user_dma *dma, u32 buffer_offset, u32 buffer_offset_2, u32 split);
28int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr,
29 void __user *userbuf, int size_in_bytes);
30void ivtv_udma_unmap(struct ivtv *itv);
31void ivtv_udma_free(struct ivtv *itv);
32void ivtv_udma_alloc(struct ivtv *itv);
33void ivtv_udma_prepare(struct ivtv *itv);
34void ivtv_udma_start(struct ivtv *itv);
35
36static inline void ivtv_udma_sync_for_device(struct ivtv *itv)
37{
38 pci_dma_sync_single_for_device(itv->pdev, itv->udma.SG_handle,
39 sizeof(itv->udma.SGarray), PCI_DMA_TODEVICE);
40}
41
42static inline void ivtv_udma_sync_for_cpu(struct ivtv *itv)
43{
44 pci_dma_sync_single_for_cpu(itv->pdev, itv->udma.SG_handle,
45 sizeof(itv->udma.SGarray), PCI_DMA_TODEVICE);
46}
47
48#endif
diff --git a/drivers/media/pci/ivtv/ivtv-vbi.c b/drivers/media/pci/ivtv/ivtv-vbi.c
new file mode 100644
index 000000000000..293db806d936
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-vbi.c
@@ -0,0 +1,549 @@
1/*
2 Vertical Blank Interval support functions
3 Copyright (C) 2004-2007 Hans Verkuil <hverkuil@xs4all.nl>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include "ivtv-driver.h"
21#include "ivtv-i2c.h"
22#include "ivtv-ioctl.h"
23#include "ivtv-queue.h"
24#include "ivtv-cards.h"
25#include "ivtv-vbi.h"
26
27static void ivtv_set_vps(struct ivtv *itv, int enabled)
28{
29 struct v4l2_sliced_vbi_data data;
30
31 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
32 return;
33 data.id = V4L2_SLICED_VPS;
34 data.field = 0;
35 data.line = enabled ? 16 : 0;
36 data.data[2] = itv->vbi.vps_payload.data[0];
37 data.data[8] = itv->vbi.vps_payload.data[1];
38 data.data[9] = itv->vbi.vps_payload.data[2];
39 data.data[10] = itv->vbi.vps_payload.data[3];
40 data.data[11] = itv->vbi.vps_payload.data[4];
41 ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);
42}
43
44static void ivtv_set_cc(struct ivtv *itv, int mode, const struct vbi_cc *cc)
45{
46 struct v4l2_sliced_vbi_data data;
47
48 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
49 return;
50 data.id = V4L2_SLICED_CAPTION_525;
51 data.field = 0;
52 data.line = (mode & 1) ? 21 : 0;
53 data.data[0] = cc->odd[0];
54 data.data[1] = cc->odd[1];
55 ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);
56 data.field = 1;
57 data.line = (mode & 2) ? 21 : 0;
58 data.data[0] = cc->even[0];
59 data.data[1] = cc->even[1];
60 ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);
61}
62
63static void ivtv_set_wss(struct ivtv *itv, int enabled, int mode)
64{
65 struct v4l2_sliced_vbi_data data;
66
67 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
68 return;
69 /* When using a 50 Hz system, always turn on the
70 wide screen signal with 4x3 ratio as the default.
71 Turning this signal on and off can confuse certain
72 TVs. As far as I can tell there is no reason not to
73 transmit this signal. */
74 if ((itv->std_out & V4L2_STD_625_50) && !enabled) {
75 enabled = 1;
76 mode = 0x08; /* 4x3 full format */
77 }
78 data.id = V4L2_SLICED_WSS_625;
79 data.field = 0;
80 data.line = enabled ? 23 : 0;
81 data.data[0] = mode & 0xff;
82 data.data[1] = (mode >> 8) & 0xff;
83 ivtv_call_hw(itv, IVTV_HW_SAA7127, vbi, s_vbi_data, &data);
84}
85
86static int odd_parity(u8 c)
87{
88 c ^= (c >> 4);
89 c ^= (c >> 2);
90 c ^= (c >> 1);
91
92 return c & 1;
93}
94
95static void ivtv_write_vbi_line(struct ivtv *itv,
96 const struct v4l2_sliced_vbi_data *d,
97 struct vbi_cc *cc, int *found_cc)
98{
99 struct vbi_info *vi = &itv->vbi;
100
101 if (d->id == V4L2_SLICED_CAPTION_525 && d->line == 21) {
102 if (d->field) {
103 cc->even[0] = d->data[0];
104 cc->even[1] = d->data[1];
105 } else {
106 cc->odd[0] = d->data[0];
107 cc->odd[1] = d->data[1];
108 }
109 *found_cc = 1;
110 } else if (d->id == V4L2_SLICED_VPS && d->line == 16 && d->field == 0) {
111 struct vbi_vps vps;
112
113 vps.data[0] = d->data[2];
114 vps.data[1] = d->data[8];
115 vps.data[2] = d->data[9];
116 vps.data[3] = d->data[10];
117 vps.data[4] = d->data[11];
118 if (memcmp(&vps, &vi->vps_payload, sizeof(vps))) {
119 vi->vps_payload = vps;
120 set_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags);
121 }
122 } else if (d->id == V4L2_SLICED_WSS_625 &&
123 d->line == 23 && d->field == 0) {
124 int wss = d->data[0] | d->data[1] << 8;
125
126 if (vi->wss_payload != wss) {
127 vi->wss_payload = wss;
128 set_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags);
129 }
130 }
131}
132
133static void ivtv_write_vbi_cc_lines(struct ivtv *itv, const struct vbi_cc *cc)
134{
135 struct vbi_info *vi = &itv->vbi;
136
137 if (vi->cc_payload_idx < ARRAY_SIZE(vi->cc_payload)) {
138 memcpy(&vi->cc_payload[vi->cc_payload_idx], cc,
139 sizeof(struct vbi_cc));
140 vi->cc_payload_idx++;
141 set_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
142 }
143}
144
145static void ivtv_write_vbi(struct ivtv *itv,
146 const struct v4l2_sliced_vbi_data *sliced,
147 size_t cnt)
148{
149 struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
150 int found_cc = 0;
151 size_t i;
152
153 for (i = 0; i < cnt; i++)
154 ivtv_write_vbi_line(itv, sliced + i, &cc, &found_cc);
155
156 if (found_cc)
157 ivtv_write_vbi_cc_lines(itv, &cc);
158}
159
160ssize_t
161ivtv_write_vbi_from_user(struct ivtv *itv,
162 const struct v4l2_sliced_vbi_data __user *sliced,
163 size_t cnt)
164{
165 struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
166 int found_cc = 0;
167 size_t i;
168 struct v4l2_sliced_vbi_data d;
169 ssize_t ret = cnt * sizeof(struct v4l2_sliced_vbi_data);
170
171 for (i = 0; i < cnt; i++) {
172 if (copy_from_user(&d, sliced + i,
173 sizeof(struct v4l2_sliced_vbi_data))) {
174 ret = -EFAULT;
175 break;
176 }
177 ivtv_write_vbi_line(itv, &d, &cc, &found_cc);
178 }
179
180 if (found_cc)
181 ivtv_write_vbi_cc_lines(itv, &cc);
182
183 return ret;
184}
185
186static void copy_vbi_data(struct ivtv *itv, int lines, u32 pts_stamp)
187{
188 int line = 0;
189 int i;
190 u32 linemask[2] = { 0, 0 };
191 unsigned short size;
192 static const u8 mpeg_hdr_data[] = {
193 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x0c, 0x66,
194 0x24, 0x01, 0x01, 0xd1, 0xd3, 0xfa, 0xff, 0xff,
195 0x00, 0x00, 0x01, 0xbd, 0x00, 0x1a, 0x84, 0x80,
196 0x07, 0x21, 0x00, 0x5d, 0x63, 0xa7, 0xff, 0xff
197 };
198 const int sd = sizeof(mpeg_hdr_data); /* start of vbi data */
199 int idx = itv->vbi.frame % IVTV_VBI_FRAMES;
200 u8 *dst = &itv->vbi.sliced_mpeg_data[idx][0];
201
202 for (i = 0; i < lines; i++) {
203 int f, l;
204
205 if (itv->vbi.sliced_data[i].id == 0)
206 continue;
207
208 l = itv->vbi.sliced_data[i].line - 6;
209 f = itv->vbi.sliced_data[i].field;
210 if (f)
211 l += 18;
212 if (l < 32)
213 linemask[0] |= (1 << l);
214 else
215 linemask[1] |= (1 << (l - 32));
216 dst[sd + 12 + line * 43] =
217 ivtv_service2vbi(itv->vbi.sliced_data[i].id);
218 memcpy(dst + sd + 12 + line * 43 + 1, itv->vbi.sliced_data[i].data, 42);
219 line++;
220 }
221 memcpy(dst, mpeg_hdr_data, sizeof(mpeg_hdr_data));
222 if (line == 36) {
223 /* All lines are used, so there is no space for the linemask
224 (the max size of the VBI data is 36 * 43 + 4 bytes).
225 So in this case we use the magic number 'ITV0'. */
226 memcpy(dst + sd, "ITV0", 4);
227 memcpy(dst + sd + 4, dst + sd + 12, line * 43);
228 size = 4 + ((43 * line + 3) & ~3);
229 } else {
230 memcpy(dst + sd, "itv0", 4);
231 cpu_to_le32s(&linemask[0]);
232 cpu_to_le32s(&linemask[1]);
233 memcpy(dst + sd + 4, &linemask[0], 8);
234 size = 12 + ((43 * line + 3) & ~3);
235 }
236 dst[4+16] = (size + 10) >> 8;
237 dst[5+16] = (size + 10) & 0xff;
238 dst[9+16] = 0x21 | ((pts_stamp >> 29) & 0x6);
239 dst[10+16] = (pts_stamp >> 22) & 0xff;
240 dst[11+16] = 1 | ((pts_stamp >> 14) & 0xff);
241 dst[12+16] = (pts_stamp >> 7) & 0xff;
242 dst[13+16] = 1 | ((pts_stamp & 0x7f) << 1);
243 itv->vbi.sliced_mpeg_size[idx] = sd + size;
244}
245
246static int ivtv_convert_ivtv_vbi(struct ivtv *itv, u8 *p)
247{
248 u32 linemask[2];
249 int i, l, id2;
250 int line = 0;
251
252 if (!memcmp(p, "itv0", 4)) {
253 memcpy(linemask, p + 4, 8);
254 p += 12;
255 } else if (!memcmp(p, "ITV0", 4)) {
256 linemask[0] = 0xffffffff;
257 linemask[1] = 0xf;
258 p += 4;
259 } else {
260 /* unknown VBI data, convert to empty VBI frame */
261 linemask[0] = linemask[1] = 0;
262 }
263 for (i = 0; i < 36; i++) {
264 int err = 0;
265
266 if (i < 32 && !(linemask[0] & (1 << i)))
267 continue;
268 if (i >= 32 && !(linemask[1] & (1 << (i - 32))))
269 continue;
270 id2 = *p & 0xf;
271 switch (id2) {
272 case IVTV_SLICED_TYPE_TELETEXT_B:
273 id2 = V4L2_SLICED_TELETEXT_B;
274 break;
275 case IVTV_SLICED_TYPE_CAPTION_525:
276 id2 = V4L2_SLICED_CAPTION_525;
277 err = !odd_parity(p[1]) || !odd_parity(p[2]);
278 break;
279 case IVTV_SLICED_TYPE_VPS:
280 id2 = V4L2_SLICED_VPS;
281 break;
282 case IVTV_SLICED_TYPE_WSS_625:
283 id2 = V4L2_SLICED_WSS_625;
284 break;
285 default:
286 id2 = 0;
287 break;
288 }
289 if (err == 0) {
290 l = (i < 18) ? i + 6 : i - 18 + 6;
291 itv->vbi.sliced_dec_data[line].line = l;
292 itv->vbi.sliced_dec_data[line].field = i >= 18;
293 itv->vbi.sliced_dec_data[line].id = id2;
294 memcpy(itv->vbi.sliced_dec_data[line].data, p + 1, 42);
295 line++;
296 }
297 p += 43;
298 }
299 while (line < 36) {
300 itv->vbi.sliced_dec_data[line].id = 0;
301 itv->vbi.sliced_dec_data[line].line = 0;
302 itv->vbi.sliced_dec_data[line].field = 0;
303 line++;
304 }
305 return line * sizeof(itv->vbi.sliced_dec_data[0]);
306}
307
308/* Compress raw VBI format, removes leading SAV codes and surplus space after the
309 field.
310 Returns new compressed size. */
311static u32 compress_raw_buf(struct ivtv *itv, u8 *buf, u32 size)
312{
313 u32 line_size = itv->vbi.raw_decoder_line_size;
314 u32 lines = itv->vbi.count;
315 u8 sav1 = itv->vbi.raw_decoder_sav_odd_field;
316 u8 sav2 = itv->vbi.raw_decoder_sav_even_field;
317 u8 *q = buf;
318 u8 *p;
319 int i;
320
321 for (i = 0; i < lines; i++) {
322 p = buf + i * line_size;
323
324 /* Look for SAV code */
325 if (p[0] != 0xff || p[1] || p[2] || (p[3] != sav1 && p[3] != sav2)) {
326 break;
327 }
328 memcpy(q, p + 4, line_size - 4);
329 q += line_size - 4;
330 }
331 return lines * (line_size - 4);
332}
333
334
335/* Compressed VBI format, all found sliced blocks put next to one another
336 Returns new compressed size */
337static u32 compress_sliced_buf(struct ivtv *itv, u32 line, u8 *buf, u32 size, u8 sav)
338{
339 u32 line_size = itv->vbi.sliced_decoder_line_size;
340 struct v4l2_decode_vbi_line vbi;
341 int i;
342 unsigned lines = 0;
343
344 /* find the first valid line */
345 for (i = 0; i < size; i++, buf++) {
346 if (buf[0] == 0xff && !buf[1] && !buf[2] && buf[3] == sav)
347 break;
348 }
349
350 size -= i;
351 if (size < line_size) {
352 return line;
353 }
354 for (i = 0; i < size / line_size; i++) {
355 u8 *p = buf + i * line_size;
356
357 /* Look for SAV code */
358 if (p[0] != 0xff || p[1] || p[2] || p[3] != sav) {
359 continue;
360 }
361 vbi.p = p + 4;
362 v4l2_subdev_call(itv->sd_video, vbi, decode_vbi_line, &vbi);
363 if (vbi.type && !(lines & (1 << vbi.line))) {
364 lines |= 1 << vbi.line;
365 itv->vbi.sliced_data[line].id = vbi.type;
366 itv->vbi.sliced_data[line].field = vbi.is_second_field;
367 itv->vbi.sliced_data[line].line = vbi.line;
368 memcpy(itv->vbi.sliced_data[line].data, vbi.p, 42);
369 line++;
370 }
371 }
372 return line;
373}
374
375void ivtv_process_vbi_data(struct ivtv *itv, struct ivtv_buffer *buf,
376 u64 pts_stamp, int streamtype)
377{
378 u8 *p = (u8 *) buf->buf;
379 u32 size = buf->bytesused;
380 int y;
381
382 /* Raw VBI data */
383 if (streamtype == IVTV_ENC_STREAM_TYPE_VBI && ivtv_raw_vbi(itv)) {
384 u8 type;
385
386 ivtv_buf_swap(buf);
387
388 type = p[3];
389
390 size = buf->bytesused = compress_raw_buf(itv, p, size);
391
392 /* second field of the frame? */
393 if (type == itv->vbi.raw_decoder_sav_even_field) {
394 /* Dirty hack needed for backwards
395 compatibility of old VBI software. */
396 p += size - 4;
397 memcpy(p, &itv->vbi.frame, 4);
398 itv->vbi.frame++;
399 }
400 return;
401 }
402
403 /* Sliced VBI data with data insertion */
404 if (streamtype == IVTV_ENC_STREAM_TYPE_VBI) {
405 int lines;
406
407 ivtv_buf_swap(buf);
408
409 /* first field */
410 lines = compress_sliced_buf(itv, 0, p, size / 2,
411 itv->vbi.sliced_decoder_sav_odd_field);
412 /* second field */
413 /* experimentation shows that the second half does not always begin
414 at the exact address. So start a bit earlier (hence 32). */
415 lines = compress_sliced_buf(itv, lines, p + size / 2 - 32, size / 2 + 32,
416 itv->vbi.sliced_decoder_sav_even_field);
417 /* always return at least one empty line */
418 if (lines == 0) {
419 itv->vbi.sliced_data[0].id = 0;
420 itv->vbi.sliced_data[0].line = 0;
421 itv->vbi.sliced_data[0].field = 0;
422 lines = 1;
423 }
424 buf->bytesused = size = lines * sizeof(itv->vbi.sliced_data[0]);
425 memcpy(p, &itv->vbi.sliced_data[0], size);
426
427 if (itv->vbi.insert_mpeg) {
428 copy_vbi_data(itv, lines, pts_stamp);
429 }
430 itv->vbi.frame++;
431 return;
432 }
433
434 /* Sliced VBI re-inserted from an MPEG stream */
435 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
436 /* If the size is not 4-byte aligned, then the starting address
437 for the swapping is also shifted. After swapping the data the
438 real start address of the VBI data is exactly 4 bytes after the
439 original start. It's a bit fiddly but it works like a charm.
440 Non-4-byte alignment happens when an lseek is done on the input
441 mpeg file to a non-4-byte aligned position. So on arrival here
442 the VBI data is also non-4-byte aligned. */
443 int offset = size & 3;
444 int cnt;
445
446 if (offset) {
447 p += 4 - offset;
448 }
449 /* Swap Buffer */
450 for (y = 0; y < size; y += 4) {
451 swab32s((u32 *)(p + y));
452 }
453
454 cnt = ivtv_convert_ivtv_vbi(itv, p + offset);
455 memcpy(buf->buf, itv->vbi.sliced_dec_data, cnt);
456 buf->bytesused = cnt;
457
458 ivtv_write_vbi(itv, itv->vbi.sliced_dec_data,
459 cnt / sizeof(itv->vbi.sliced_dec_data[0]));
460 return;
461 }
462}
463
464void ivtv_disable_cc(struct ivtv *itv)
465{
466 struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
467
468 clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
469 ivtv_set_cc(itv, 0, &cc);
470 itv->vbi.cc_payload_idx = 0;
471}
472
473
474void ivtv_vbi_work_handler(struct ivtv *itv)
475{
476 struct vbi_info *vi = &itv->vbi;
477 struct v4l2_sliced_vbi_data data;
478 struct vbi_cc cc = { .odd = { 0x80, 0x80 }, .even = { 0x80, 0x80 } };
479
480 /* Lock */
481 if (itv->output_mode == OUT_PASSTHROUGH) {
482 if (itv->is_50hz) {
483 data.id = V4L2_SLICED_WSS_625;
484 data.field = 0;
485
486 if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {
487 ivtv_set_wss(itv, 1, data.data[0] & 0xf);
488 vi->wss_missing_cnt = 0;
489 } else if (vi->wss_missing_cnt == 4) {
490 ivtv_set_wss(itv, 1, 0x8); /* 4x3 full format */
491 } else {
492 vi->wss_missing_cnt++;
493 }
494 }
495 else {
496 int mode = 0;
497
498 data.id = V4L2_SLICED_CAPTION_525;
499 data.field = 0;
500 if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {
501 mode |= 1;
502 cc.odd[0] = data.data[0];
503 cc.odd[1] = data.data[1];
504 }
505 data.field = 1;
506 if (v4l2_subdev_call(itv->sd_video, vbi, g_vbi_data, &data) == 0) {
507 mode |= 2;
508 cc.even[0] = data.data[0];
509 cc.even[1] = data.data[1];
510 }
511 if (mode) {
512 vi->cc_missing_cnt = 0;
513 ivtv_set_cc(itv, mode, &cc);
514 } else if (vi->cc_missing_cnt == 4) {
515 ivtv_set_cc(itv, 0, &cc);
516 } else {
517 vi->cc_missing_cnt++;
518 }
519 }
520 return;
521 }
522
523 if (test_and_clear_bit(IVTV_F_I_UPDATE_WSS, &itv->i_flags)) {
524 ivtv_set_wss(itv, 1, vi->wss_payload & 0xf);
525 }
526
527 if (test_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags)) {
528 if (vi->cc_payload_idx == 0) {
529 clear_bit(IVTV_F_I_UPDATE_CC, &itv->i_flags);
530 ivtv_set_cc(itv, 3, &cc);
531 }
532 while (vi->cc_payload_idx) {
533 cc = vi->cc_payload[0];
534
535 memcpy(vi->cc_payload, vi->cc_payload + 1,
536 sizeof(vi->cc_payload) - sizeof(vi->cc_payload[0]));
537 vi->cc_payload_idx--;
538 if (vi->cc_payload_idx && cc.odd[0] == 0x80 && cc.odd[1] == 0x80)
539 continue;
540
541 ivtv_set_cc(itv, 3, &cc);
542 break;
543 }
544 }
545
546 if (test_and_clear_bit(IVTV_F_I_UPDATE_VPS, &itv->i_flags)) {
547 ivtv_set_vps(itv, 1);
548 }
549}
diff --git a/drivers/media/pci/ivtv/ivtv-vbi.h b/drivers/media/pci/ivtv/ivtv-vbi.h
new file mode 100644
index 000000000000..166dd0b75d0f
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-vbi.h
@@ -0,0 +1,34 @@
1/*
2 Vertical Blank Interval support functions
3 Copyright (C) 2004-2007 Hans Verkuil <hverkuil@xs4all.nl>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef IVTV_VBI_H
21#define IVTV_VBI_H
22
23ssize_t
24ivtv_write_vbi_from_user(struct ivtv *itv,
25 const struct v4l2_sliced_vbi_data __user *sliced,
26 size_t count);
27void ivtv_process_vbi_data(struct ivtv *itv, struct ivtv_buffer *buf,
28 u64 pts_stamp, int streamtype);
29int ivtv_used_line(struct ivtv *itv, int line, int field);
30void ivtv_disable_cc(struct ivtv *itv);
31void ivtv_set_vbi(unsigned long arg);
32void ivtv_vbi_work_handler(struct ivtv *itv);
33
34#endif
diff --git a/drivers/media/pci/ivtv/ivtv-version.h b/drivers/media/pci/ivtv/ivtv-version.h
new file mode 100644
index 000000000000..a20f346fcad8
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-version.h
@@ -0,0 +1,26 @@
1/*
2 ivtv driver version information
3 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifndef IVTV_VERSION_H
21#define IVTV_VERSION_H
22
23#define IVTV_DRIVER_NAME "ivtv"
24#define IVTV_VERSION "1.4.3"
25
26#endif
diff --git a/drivers/media/pci/ivtv/ivtv-yuv.c b/drivers/media/pci/ivtv/ivtv-yuv.c
new file mode 100644
index 000000000000..2ad65eb29832
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-yuv.c
@@ -0,0 +1,1296 @@
1/*
2 yuv support
3
4 Copyright (C) 2007 Ian Armstrong <ian@iarmst.demon.co.uk>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "ivtv-driver.h"
22#include "ivtv-udma.h"
23#include "ivtv-yuv.h"
24
25/* YUV buffer offsets */
26const u32 yuv_offset[IVTV_YUV_BUFFERS] = {
27 0x001a8600,
28 0x00240400,
29 0x002d8200,
30 0x00370000,
31 0x00029000,
32 0x000C0E00,
33 0x006B0400,
34 0x00748200
35};
36
37static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
38 struct ivtv_dma_frame *args)
39{
40 struct ivtv_dma_page_info y_dma;
41 struct ivtv_dma_page_info uv_dma;
42 struct yuv_playback_info *yi = &itv->yuv_info;
43 u8 frame = yi->draw_frame;
44 struct yuv_frame_info *f = &yi->new_frame_info[frame];
45 int i;
46 int y_pages, uv_pages;
47 unsigned long y_buffer_offset, uv_buffer_offset;
48 int y_decode_height, uv_decode_height, y_size;
49
50 y_buffer_offset = IVTV_DECODER_OFFSET + yuv_offset[frame];
51 uv_buffer_offset = y_buffer_offset + IVTV_YUV_BUFFER_UV_OFFSET;
52
53 y_decode_height = uv_decode_height = f->src_h + f->src_y;
54
55 if (f->offset_y)
56 y_buffer_offset += 720 * 16;
57
58 if (y_decode_height & 15)
59 y_decode_height = (y_decode_height + 16) & ~15;
60
61 if (uv_decode_height & 31)
62 uv_decode_height = (uv_decode_height + 32) & ~31;
63
64 y_size = 720 * y_decode_height;
65
66 /* Still in USE */
67 if (dma->SG_length || dma->page_count) {
68 IVTV_DEBUG_WARN
69 ("prep_user_dma: SG_length %d page_count %d still full?\n",
70 dma->SG_length, dma->page_count);
71 return -EBUSY;
72 }
73
74 ivtv_udma_get_page_info (&y_dma, (unsigned long)args->y_source, 720 * y_decode_height);
75 ivtv_udma_get_page_info (&uv_dma, (unsigned long)args->uv_source, 360 * uv_decode_height);
76
77 /* Get user pages for DMA Xfer */
78 down_read(&current->mm->mmap_sem);
79 y_pages = get_user_pages(current, current->mm, y_dma.uaddr, y_dma.page_count, 0, 1, &dma->map[0], NULL);
80 uv_pages = 0; /* silence gcc. value is set and consumed only if: */
81 if (y_pages == y_dma.page_count) {
82 uv_pages = get_user_pages(current, current->mm,
83 uv_dma.uaddr, uv_dma.page_count, 0, 1,
84 &dma->map[y_pages], NULL);
85 }
86 up_read(&current->mm->mmap_sem);
87
88 if (y_pages != y_dma.page_count || uv_pages != uv_dma.page_count) {
89 int rc = -EFAULT;
90
91 if (y_pages == y_dma.page_count) {
92 IVTV_DEBUG_WARN
93 ("failed to map uv user pages, returned %d "
94 "expecting %d\n", uv_pages, uv_dma.page_count);
95
96 if (uv_pages >= 0) {
97 for (i = 0; i < uv_pages; i++)
98 put_page(dma->map[y_pages + i]);
99 rc = -EFAULT;
100 } else {
101 rc = uv_pages;
102 }
103 } else {
104 IVTV_DEBUG_WARN
105 ("failed to map y user pages, returned %d "
106 "expecting %d\n", y_pages, y_dma.page_count);
107 }
108 if (y_pages >= 0) {
109 for (i = 0; i < y_pages; i++)
110 put_page(dma->map[i]);
111 /*
112 * Inherit the -EFAULT from rc's
113 * initialization, but allow it to be
114 * overriden by uv_pages above if it was an
115 * actual errno.
116 */
117 } else {
118 rc = y_pages;
119 }
120 return rc;
121 }
122
123 dma->page_count = y_pages + uv_pages;
124
125 /* Fill & map SG List */
126 if (ivtv_udma_fill_sg_list (dma, &uv_dma, ivtv_udma_fill_sg_list (dma, &y_dma, 0)) < 0) {
127 IVTV_DEBUG_WARN("could not allocate bounce buffers for highmem userspace buffers\n");
128 for (i = 0; i < dma->page_count; i++) {
129 put_page(dma->map[i]);
130 }
131 dma->page_count = 0;
132 return -ENOMEM;
133 }
134 dma->SG_length = pci_map_sg(itv->pdev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE);
135
136 /* Fill SG Array with new values */
137 ivtv_udma_fill_sg_array(dma, y_buffer_offset, uv_buffer_offset, y_size);
138
139 /* If we've offset the y plane, ensure top area is blanked */
140 if (f->offset_y && yi->blanking_dmaptr) {
141 dma->SGarray[dma->SG_length].size = cpu_to_le32(720*16);
142 dma->SGarray[dma->SG_length].src = cpu_to_le32(yi->blanking_dmaptr);
143 dma->SGarray[dma->SG_length].dst = cpu_to_le32(IVTV_DECODER_OFFSET + yuv_offset[frame]);
144 dma->SG_length++;
145 }
146
147 /* Tag SG Array with Interrupt Bit */
148 dma->SGarray[dma->SG_length - 1].size |= cpu_to_le32(0x80000000);
149
150 ivtv_udma_sync_for_device(itv);
151 return 0;
152}
153
154/* We rely on a table held in the firmware - Quick check. */
155int ivtv_yuv_filter_check(struct ivtv *itv)
156{
157 int i, y, uv;
158
159 for (i = 0, y = 16, uv = 4; i < 16; i++, y += 24, uv += 12) {
160 if ((read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + y) != i << 16) ||
161 (read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + uv) != i << 16)) {
162 IVTV_WARN ("YUV filter table not found in firmware.\n");
163 return -1;
164 }
165 }
166 return 0;
167}
168
169static void ivtv_yuv_filter(struct ivtv *itv, int h_filter, int v_filter_1, int v_filter_2)
170{
171 u32 i, line;
172
173 /* If any filter is -1, then don't update it */
174 if (h_filter > -1) {
175 if (h_filter > 4)
176 h_filter = 4;
177 i = IVTV_YUV_HORIZONTAL_FILTER_OFFSET + (h_filter * 384);
178 for (line = 0; line < 16; line++) {
179 write_reg(read_dec(i), 0x02804);
180 write_reg(read_dec(i), 0x0281c);
181 i += 4;
182 write_reg(read_dec(i), 0x02808);
183 write_reg(read_dec(i), 0x02820);
184 i += 4;
185 write_reg(read_dec(i), 0x0280c);
186 write_reg(read_dec(i), 0x02824);
187 i += 4;
188 write_reg(read_dec(i), 0x02810);
189 write_reg(read_dec(i), 0x02828);
190 i += 4;
191 write_reg(read_dec(i), 0x02814);
192 write_reg(read_dec(i), 0x0282c);
193 i += 8;
194 write_reg(0, 0x02818);
195 write_reg(0, 0x02830);
196 }
197 IVTV_DEBUG_YUV("h_filter -> %d\n", h_filter);
198 }
199
200 if (v_filter_1 > -1) {
201 if (v_filter_1 > 4)
202 v_filter_1 = 4;
203 i = IVTV_YUV_VERTICAL_FILTER_OFFSET + (v_filter_1 * 192);
204 for (line = 0; line < 16; line++) {
205 write_reg(read_dec(i), 0x02900);
206 i += 4;
207 write_reg(read_dec(i), 0x02904);
208 i += 8;
209 write_reg(0, 0x02908);
210 }
211 IVTV_DEBUG_YUV("v_filter_1 -> %d\n", v_filter_1);
212 }
213
214 if (v_filter_2 > -1) {
215 if (v_filter_2 > 4)
216 v_filter_2 = 4;
217 i = IVTV_YUV_VERTICAL_FILTER_OFFSET + (v_filter_2 * 192);
218 for (line = 0; line < 16; line++) {
219 write_reg(read_dec(i), 0x0290c);
220 i += 4;
221 write_reg(read_dec(i), 0x02910);
222 i += 8;
223 write_reg(0, 0x02914);
224 }
225 IVTV_DEBUG_YUV("v_filter_2 -> %d\n", v_filter_2);
226 }
227}
228
229static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *f)
230{
231 struct yuv_playback_info *yi = &itv->yuv_info;
232 u32 reg_2834, reg_2838, reg_283c;
233 u32 reg_2844, reg_2854, reg_285c;
234 u32 reg_2864, reg_2874, reg_2890;
235 u32 reg_2870, reg_2870_base, reg_2870_offset;
236 int x_cutoff;
237 int h_filter;
238 u32 master_width;
239
240 IVTV_DEBUG_WARN
241 ("Adjust to width %d src_w %d dst_w %d src_x %d dst_x %d\n",
242 f->tru_w, f->src_w, f->dst_w, f->src_x, f->dst_x);
243
244 /* How wide is the src image */
245 x_cutoff = f->src_w + f->src_x;
246
247 /* Set the display width */
248 reg_2834 = f->dst_w;
249 reg_2838 = reg_2834;
250
251 /* Set the display position */
252 reg_2890 = f->dst_x;
253
254 /* Index into the image horizontally */
255 reg_2870 = 0;
256
257 /* 2870 is normally fudged to align video coords with osd coords.
258 If running full screen, it causes an unwanted left shift
259 Remove the fudge if we almost fill the screen.
260 Gradually adjust the offset to avoid the video 'snapping'
261 left/right if it gets dragged through this region.
262 Only do this if osd is full width. */
263 if (f->vis_w == 720) {
264 if ((f->tru_x - f->pan_x > -1) && (f->tru_x - f->pan_x <= 40) && (f->dst_w >= 680))
265 reg_2870 = 10 - (f->tru_x - f->pan_x) / 4;
266 else if ((f->tru_x - f->pan_x < 0) && (f->tru_x - f->pan_x >= -20) && (f->dst_w >= 660))
267 reg_2870 = (10 + (f->tru_x - f->pan_x) / 2);
268
269 if (f->dst_w >= f->src_w)
270 reg_2870 = reg_2870 << 16 | reg_2870;
271 else
272 reg_2870 = ((reg_2870 & ~1) << 15) | (reg_2870 & ~1);
273 }
274
275 if (f->dst_w < f->src_w)
276 reg_2870 = 0x000d000e - reg_2870;
277 else
278 reg_2870 = 0x0012000e - reg_2870;
279
280 /* We're also using 2870 to shift the image left (src_x & negative dst_x) */
281 reg_2870_offset = (f->src_x * ((f->dst_w << 21) / f->src_w)) >> 19;
282
283 if (f->dst_w >= f->src_w) {
284 x_cutoff &= ~1;
285 master_width = (f->src_w * 0x00200000) / (f->dst_w);
286 if (master_width * f->dst_w != f->src_w * 0x00200000)
287 master_width++;
288 reg_2834 = (reg_2834 << 16) | x_cutoff;
289 reg_2838 = (reg_2838 << 16) | x_cutoff;
290 reg_283c = master_width >> 2;
291 reg_2844 = master_width >> 2;
292 reg_2854 = master_width;
293 reg_285c = master_width >> 1;
294 reg_2864 = master_width >> 1;
295
296 /* We also need to factor in the scaling
297 (src_w - dst_w) / (src_w / 4) */
298 if (f->dst_w > f->src_w)
299 reg_2870_base = ((f->dst_w - f->src_w)<<16) / (f->src_w <<14);
300 else
301 reg_2870_base = 0;
302
303 reg_2870 += (((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 2) + (reg_2870_base << 17 | reg_2870_base);
304 reg_2874 = 0;
305 } else if (f->dst_w < f->src_w / 2) {
306 master_width = (f->src_w * 0x00080000) / f->dst_w;
307 if (master_width * f->dst_w != f->src_w * 0x00080000)
308 master_width++;
309 reg_2834 = (reg_2834 << 16) | x_cutoff;
310 reg_2838 = (reg_2838 << 16) | x_cutoff;
311 reg_283c = master_width >> 2;
312 reg_2844 = master_width >> 1;
313 reg_2854 = master_width;
314 reg_285c = master_width >> 1;
315 reg_2864 = master_width >> 1;
316 reg_2870 += ((reg_2870_offset << 15) & 0xFFFF0000) | reg_2870_offset;
317 reg_2870 += (5 - (((f->src_w + f->src_w / 2) - 1) / f->dst_w)) << 16;
318 reg_2874 = 0x00000012;
319 } else {
320 master_width = (f->src_w * 0x00100000) / f->dst_w;
321 if (master_width * f->dst_w != f->src_w * 0x00100000)
322 master_width++;
323 reg_2834 = (reg_2834 << 16) | x_cutoff;
324 reg_2838 = (reg_2838 << 16) | x_cutoff;
325 reg_283c = master_width >> 2;
326 reg_2844 = master_width >> 1;
327 reg_2854 = master_width;
328 reg_285c = master_width >> 1;
329 reg_2864 = master_width >> 1;
330 reg_2870 += ((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 1;
331 reg_2870 += (5 - (((f->src_w * 3) - 1) / f->dst_w)) << 16;
332 reg_2874 = 0x00000001;
333 }
334
335 /* Select the horizontal filter */
336 if (f->src_w == f->dst_w) {
337 /* An exact size match uses filter 0 */
338 h_filter = 0;
339 } else {
340 /* Figure out which filter to use */
341 h_filter = ((f->src_w << 16) / f->dst_w) >> 15;
342 h_filter = (h_filter >> 1) + (h_filter & 1);
343 /* Only an exact size match can use filter 0 */
344 h_filter += !h_filter;
345 }
346
347 write_reg(reg_2834, 0x02834);
348 write_reg(reg_2838, 0x02838);
349 IVTV_DEBUG_YUV("Update reg 0x2834 %08x->%08x 0x2838 %08x->%08x\n",
350 yi->reg_2834, reg_2834, yi->reg_2838, reg_2838);
351
352 write_reg(reg_283c, 0x0283c);
353 write_reg(reg_2844, 0x02844);
354
355 IVTV_DEBUG_YUV("Update reg 0x283c %08x->%08x 0x2844 %08x->%08x\n",
356 yi->reg_283c, reg_283c, yi->reg_2844, reg_2844);
357
358 write_reg(0x00080514, 0x02840);
359 write_reg(0x00100514, 0x02848);
360 IVTV_DEBUG_YUV("Update reg 0x2840 %08x->%08x 0x2848 %08x->%08x\n",
361 yi->reg_2840, 0x00080514, yi->reg_2848, 0x00100514);
362
363 write_reg(reg_2854, 0x02854);
364 IVTV_DEBUG_YUV("Update reg 0x2854 %08x->%08x \n",
365 yi->reg_2854, reg_2854);
366
367 write_reg(reg_285c, 0x0285c);
368 write_reg(reg_2864, 0x02864);
369 IVTV_DEBUG_YUV("Update reg 0x285c %08x->%08x 0x2864 %08x->%08x\n",
370 yi->reg_285c, reg_285c, yi->reg_2864, reg_2864);
371
372 write_reg(reg_2874, 0x02874);
373 IVTV_DEBUG_YUV("Update reg 0x2874 %08x->%08x\n",
374 yi->reg_2874, reg_2874);
375
376 write_reg(reg_2870, 0x02870);
377 IVTV_DEBUG_YUV("Update reg 0x2870 %08x->%08x\n",
378 yi->reg_2870, reg_2870);
379
380 write_reg(reg_2890, 0x02890);
381 IVTV_DEBUG_YUV("Update reg 0x2890 %08x->%08x\n",
382 yi->reg_2890, reg_2890);
383
384 /* Only update the filter if we really need to */
385 if (h_filter != yi->h_filter) {
386 ivtv_yuv_filter(itv, h_filter, -1, -1);
387 yi->h_filter = h_filter;
388 }
389}
390
391static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *f)
392{
393 struct yuv_playback_info *yi = &itv->yuv_info;
394 u32 master_height;
395 u32 reg_2918, reg_291c, reg_2920, reg_2928;
396 u32 reg_2930, reg_2934, reg_293c;
397 u32 reg_2940, reg_2944, reg_294c;
398 u32 reg_2950, reg_2954, reg_2958, reg_295c;
399 u32 reg_2960, reg_2964, reg_2968, reg_296c;
400 u32 reg_289c;
401 u32 src_major_y, src_minor_y;
402 u32 src_major_uv, src_minor_uv;
403 u32 reg_2964_base, reg_2968_base;
404 int v_filter_1, v_filter_2;
405
406 IVTV_DEBUG_WARN
407 ("Adjust to height %d src_h %d dst_h %d src_y %d dst_y %d\n",
408 f->tru_h, f->src_h, f->dst_h, f->src_y, f->dst_y);
409
410 /* What scaling mode is being used... */
411 IVTV_DEBUG_YUV("Scaling mode Y: %s\n",
412 f->interlaced_y ? "Interlaced" : "Progressive");
413
414 IVTV_DEBUG_YUV("Scaling mode UV: %s\n",
415 f->interlaced_uv ? "Interlaced" : "Progressive");
416
417 /* What is the source video being treated as... */
418 IVTV_DEBUG_WARN("Source video: %s\n",
419 f->interlaced ? "Interlaced" : "Progressive");
420
421 /* We offset into the image using two different index methods, so split
422 the y source coord into two parts. */
423 if (f->src_y < 8) {
424 src_minor_uv = f->src_y;
425 src_major_uv = 0;
426 } else {
427 src_minor_uv = 8;
428 src_major_uv = f->src_y - 8;
429 }
430
431 src_minor_y = src_minor_uv;
432 src_major_y = src_major_uv;
433
434 if (f->offset_y)
435 src_minor_y += 16;
436
437 if (f->interlaced_y)
438 reg_2918 = (f->dst_h << 16) | (f->src_h + src_minor_y);
439 else
440 reg_2918 = (f->dst_h << 16) | ((f->src_h + src_minor_y) << 1);
441
442 if (f->interlaced_uv)
443 reg_291c = (f->dst_h << 16) | ((f->src_h + src_minor_uv) >> 1);
444 else
445 reg_291c = (f->dst_h << 16) | (f->src_h + src_minor_uv);
446
447 reg_2964_base = (src_minor_y * ((f->dst_h << 16) / f->src_h)) >> 14;
448 reg_2968_base = (src_minor_uv * ((f->dst_h << 16) / f->src_h)) >> 14;
449
450 if (f->dst_h / 2 >= f->src_h && !f->interlaced_y) {
451 master_height = (f->src_h * 0x00400000) / f->dst_h;
452 if ((f->src_h * 0x00400000) - (master_height * f->dst_h) >= f->dst_h / 2)
453 master_height++;
454 reg_2920 = master_height >> 2;
455 reg_2928 = master_height >> 3;
456 reg_2930 = master_height;
457 reg_2940 = master_height >> 1;
458 reg_2964_base >>= 3;
459 reg_2968_base >>= 3;
460 reg_296c = 0x00000000;
461 } else if (f->dst_h >= f->src_h) {
462 master_height = (f->src_h * 0x00400000) / f->dst_h;
463 master_height = (master_height >> 1) + (master_height & 1);
464 reg_2920 = master_height >> 2;
465 reg_2928 = master_height >> 2;
466 reg_2930 = master_height;
467 reg_2940 = master_height >> 1;
468 reg_296c = 0x00000000;
469 if (f->interlaced_y) {
470 reg_2964_base >>= 3;
471 } else {
472 reg_296c++;
473 reg_2964_base >>= 2;
474 }
475 if (f->interlaced_uv)
476 reg_2928 >>= 1;
477 reg_2968_base >>= 3;
478 } else if (f->dst_h >= f->src_h / 2) {
479 master_height = (f->src_h * 0x00200000) / f->dst_h;
480 master_height = (master_height >> 1) + (master_height & 1);
481 reg_2920 = master_height >> 2;
482 reg_2928 = master_height >> 2;
483 reg_2930 = master_height;
484 reg_2940 = master_height;
485 reg_296c = 0x00000101;
486 if (f->interlaced_y) {
487 reg_2964_base >>= 2;
488 } else {
489 reg_296c++;
490 reg_2964_base >>= 1;
491 }
492 if (f->interlaced_uv)
493 reg_2928 >>= 1;
494 reg_2968_base >>= 2;
495 } else {
496 master_height = (f->src_h * 0x00100000) / f->dst_h;
497 master_height = (master_height >> 1) + (master_height & 1);
498 reg_2920 = master_height >> 2;
499 reg_2928 = master_height >> 2;
500 reg_2930 = master_height;
501 reg_2940 = master_height;
502 reg_2964_base >>= 1;
503 reg_2968_base >>= 2;
504 reg_296c = 0x00000102;
505 }
506
507 /* FIXME These registers change depending on scaled / unscaled output
508 We really need to work out what they should be */
509 if (f->src_h == f->dst_h) {
510 reg_2934 = 0x00020000;
511 reg_293c = 0x00100000;
512 reg_2944 = 0x00040000;
513 reg_294c = 0x000b0000;
514 } else {
515 reg_2934 = 0x00000FF0;
516 reg_293c = 0x00000FF0;
517 reg_2944 = 0x00000FF0;
518 reg_294c = 0x00000FF0;
519 }
520
521 /* The first line to be displayed */
522 reg_2950 = 0x00010000 + src_major_y;
523 if (f->interlaced_y)
524 reg_2950 += 0x00010000;
525 reg_2954 = reg_2950 + 1;
526
527 reg_2958 = 0x00010000 + (src_major_y >> 1);
528 if (f->interlaced_uv)
529 reg_2958 += 0x00010000;
530 reg_295c = reg_2958 + 1;
531
532 if (yi->decode_height == 480)
533 reg_289c = 0x011e0017;
534 else
535 reg_289c = 0x01500017;
536
537 if (f->dst_y < 0)
538 reg_289c = (reg_289c - ((f->dst_y & ~1)<<15))-(f->dst_y >>1);
539 else
540 reg_289c = (reg_289c + ((f->dst_y & ~1)<<15))+(f->dst_y >>1);
541
542 /* How much of the source to decode.
543 Take into account the source offset */
544 reg_2960 = ((src_minor_y + f->src_h + src_major_y) - 1) |
545 (((src_minor_uv + f->src_h + src_major_uv - 1) & ~1) << 15);
546
547 /* Calculate correct value for register 2964 */
548 if (f->src_h == f->dst_h) {
549 reg_2964 = 1;
550 } else {
551 reg_2964 = 2 + ((f->dst_h << 1) / f->src_h);
552 reg_2964 = (reg_2964 >> 1) + (reg_2964 & 1);
553 }
554 reg_2968 = (reg_2964 << 16) + reg_2964 + (reg_2964 >> 1);
555 reg_2964 = (reg_2964 << 16) + reg_2964 + (reg_2964 * 46 / 94);
556
557 /* Okay, we've wasted time working out the correct value,
558 but if we use it, it fouls the the window alignment.
559 Fudge it to what we want... */
560 reg_2964 = 0x00010001 + ((reg_2964 & 0x0000FFFF) - (reg_2964 >> 16));
561 reg_2968 = 0x00010001 + ((reg_2968 & 0x0000FFFF) - (reg_2968 >> 16));
562
563 /* Deviate further from what it should be. I find the flicker headache
564 inducing so try to reduce it slightly. Leave 2968 as-is otherwise
565 colours foul. */
566 if ((reg_2964 != 0x00010001) && (f->dst_h / 2 <= f->src_h))
567 reg_2964 = (reg_2964 & 0xFFFF0000) + ((reg_2964 & 0x0000FFFF) / 2);
568
569 if (!f->interlaced_y)
570 reg_2964 -= 0x00010001;
571 if (!f->interlaced_uv)
572 reg_2968 -= 0x00010001;
573
574 reg_2964 += ((reg_2964_base << 16) | reg_2964_base);
575 reg_2968 += ((reg_2968_base << 16) | reg_2968_base);
576
577 /* Select the vertical filter */
578 if (f->src_h == f->dst_h) {
579 /* An exact size match uses filter 0/1 */
580 v_filter_1 = 0;
581 v_filter_2 = 1;
582 } else {
583 /* Figure out which filter to use */
584 v_filter_1 = ((f->src_h << 16) / f->dst_h) >> 15;
585 v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1);
586 /* Only an exact size match can use filter 0 */
587 v_filter_1 += !v_filter_1;
588 v_filter_2 = v_filter_1;
589 }
590
591 write_reg(reg_2934, 0x02934);
592 write_reg(reg_293c, 0x0293c);
593 IVTV_DEBUG_YUV("Update reg 0x2934 %08x->%08x 0x293c %08x->%08x\n",
594 yi->reg_2934, reg_2934, yi->reg_293c, reg_293c);
595 write_reg(reg_2944, 0x02944);
596 write_reg(reg_294c, 0x0294c);
597 IVTV_DEBUG_YUV("Update reg 0x2944 %08x->%08x 0x294c %08x->%08x\n",
598 yi->reg_2944, reg_2944, yi->reg_294c, reg_294c);
599
600 /* Ensure 2970 is 0 (does it ever change ?) */
601/* write_reg(0,0x02970); */
602/* IVTV_DEBUG_YUV("Update reg 0x2970 %08x->%08x\n", yi->reg_2970, 0); */
603
604 write_reg(reg_2930, 0x02938);
605 write_reg(reg_2930, 0x02930);
606 IVTV_DEBUG_YUV("Update reg 0x2930 %08x->%08x 0x2938 %08x->%08x\n",
607 yi->reg_2930, reg_2930, yi->reg_2938, reg_2930);
608
609 write_reg(reg_2928, 0x02928);
610 write_reg(reg_2928 + 0x514, 0x0292C);
611 IVTV_DEBUG_YUV("Update reg 0x2928 %08x->%08x 0x292c %08x->%08x\n",
612 yi->reg_2928, reg_2928, yi->reg_292c, reg_2928 + 0x514);
613
614 write_reg(reg_2920, 0x02920);
615 write_reg(reg_2920 + 0x514, 0x02924);
616 IVTV_DEBUG_YUV("Update reg 0x2920 %08x->%08x 0x2924 %08x->%08x\n",
617 yi->reg_2920, reg_2920, yi->reg_2924, reg_2920 + 0x514);
618
619 write_reg(reg_2918, 0x02918);
620 write_reg(reg_291c, 0x0291C);
621 IVTV_DEBUG_YUV("Update reg 0x2918 %08x->%08x 0x291C %08x->%08x\n",
622 yi->reg_2918, reg_2918, yi->reg_291c, reg_291c);
623
624 write_reg(reg_296c, 0x0296c);
625 IVTV_DEBUG_YUV("Update reg 0x296c %08x->%08x\n",
626 yi->reg_296c, reg_296c);
627
628 write_reg(reg_2940, 0x02948);
629 write_reg(reg_2940, 0x02940);
630 IVTV_DEBUG_YUV("Update reg 0x2940 %08x->%08x 0x2948 %08x->%08x\n",
631 yi->reg_2940, reg_2940, yi->reg_2948, reg_2940);
632
633 write_reg(reg_2950, 0x02950);
634 write_reg(reg_2954, 0x02954);
635 IVTV_DEBUG_YUV("Update reg 0x2950 %08x->%08x 0x2954 %08x->%08x\n",
636 yi->reg_2950, reg_2950, yi->reg_2954, reg_2954);
637
638 write_reg(reg_2958, 0x02958);
639 write_reg(reg_295c, 0x0295C);
640 IVTV_DEBUG_YUV("Update reg 0x2958 %08x->%08x 0x295C %08x->%08x\n",
641 yi->reg_2958, reg_2958, yi->reg_295c, reg_295c);
642
643 write_reg(reg_2960, 0x02960);
644 IVTV_DEBUG_YUV("Update reg 0x2960 %08x->%08x \n",
645 yi->reg_2960, reg_2960);
646
647 write_reg(reg_2964, 0x02964);
648 write_reg(reg_2968, 0x02968);
649 IVTV_DEBUG_YUV("Update reg 0x2964 %08x->%08x 0x2968 %08x->%08x\n",
650 yi->reg_2964, reg_2964, yi->reg_2968, reg_2968);
651
652 write_reg(reg_289c, 0x0289c);
653 IVTV_DEBUG_YUV("Update reg 0x289c %08x->%08x\n",
654 yi->reg_289c, reg_289c);
655
656 /* Only update filter 1 if we really need to */
657 if (v_filter_1 != yi->v_filter_1) {
658 ivtv_yuv_filter(itv, -1, v_filter_1, -1);
659 yi->v_filter_1 = v_filter_1;
660 }
661
662 /* Only update filter 2 if we really need to */
663 if (v_filter_2 != yi->v_filter_2) {
664 ivtv_yuv_filter(itv, -1, -1, v_filter_2);
665 yi->v_filter_2 = v_filter_2;
666 }
667}
668
669/* Modify the supplied coordinate information to fit the visible osd area */
670static u32 ivtv_yuv_window_setup(struct ivtv *itv, struct yuv_frame_info *f)
671{
672 struct yuv_frame_info *of = &itv->yuv_info.old_frame_info;
673 int osd_crop;
674 u32 osd_scale;
675 u32 yuv_update = 0;
676
677 /* Sorry, but no negative coords for src */
678 if (f->src_x < 0)
679 f->src_x = 0;
680 if (f->src_y < 0)
681 f->src_y = 0;
682
683 /* Can only reduce width down to 1/4 original size */
684 if ((osd_crop = f->src_w - 4 * f->dst_w) > 0) {
685 f->src_x += osd_crop / 2;
686 f->src_w = (f->src_w - osd_crop) & ~3;
687 f->dst_w = f->src_w / 4;
688 f->dst_w += f->dst_w & 1;
689 }
690
691 /* Can only reduce height down to 1/4 original size */
692 if (f->src_h / f->dst_h >= 2) {
693 /* Overflow may be because we're running progressive,
694 so force mode switch */
695 f->interlaced_y = 1;
696 /* Make sure we're still within limits for interlace */
697 if ((osd_crop = f->src_h - 4 * f->dst_h) > 0) {
698 /* If we reach here we'll have to force the height. */
699 f->src_y += osd_crop / 2;
700 f->src_h = (f->src_h - osd_crop) & ~3;
701 f->dst_h = f->src_h / 4;
702 f->dst_h += f->dst_h & 1;
703 }
704 }
705
706 /* If there's nothing to safe to display, we may as well stop now */
707 if ((int)f->dst_w <= 2 || (int)f->dst_h <= 2 ||
708 (int)f->src_w <= 2 || (int)f->src_h <= 2) {
709 return IVTV_YUV_UPDATE_INVALID;
710 }
711
712 /* Ensure video remains inside OSD area */
713 osd_scale = (f->src_h << 16) / f->dst_h;
714
715 if ((osd_crop = f->pan_y - f->dst_y) > 0) {
716 /* Falls off the upper edge - crop */
717 f->src_y += (osd_scale * osd_crop) >> 16;
718 f->src_h -= (osd_scale * osd_crop) >> 16;
719 f->dst_h -= osd_crop;
720 f->dst_y = 0;
721 } else {
722 f->dst_y -= f->pan_y;
723 }
724
725 if ((osd_crop = f->dst_h + f->dst_y - f->vis_h) > 0) {
726 /* Falls off the lower edge - crop */
727 f->dst_h -= osd_crop;
728 f->src_h -= (osd_scale * osd_crop) >> 16;
729 }
730
731 osd_scale = (f->src_w << 16) / f->dst_w;
732
733 if ((osd_crop = f->pan_x - f->dst_x) > 0) {
734 /* Fall off the left edge - crop */
735 f->src_x += (osd_scale * osd_crop) >> 16;
736 f->src_w -= (osd_scale * osd_crop) >> 16;
737 f->dst_w -= osd_crop;
738 f->dst_x = 0;
739 } else {
740 f->dst_x -= f->pan_x;
741 }
742
743 if ((osd_crop = f->dst_w + f->dst_x - f->vis_w) > 0) {
744 /* Falls off the right edge - crop */
745 f->dst_w -= osd_crop;
746 f->src_w -= (osd_scale * osd_crop) >> 16;
747 }
748
749 if (itv->yuv_info.track_osd) {
750 /* The OSD can be moved. Track to it */
751 f->dst_x += itv->yuv_info.osd_x_offset;
752 f->dst_y += itv->yuv_info.osd_y_offset;
753 }
754
755 /* Width & height for both src & dst must be even.
756 Same for coordinates. */
757 f->dst_w &= ~1;
758 f->dst_x &= ~1;
759
760 f->src_w += f->src_x & 1;
761 f->src_x &= ~1;
762
763 f->src_w &= ~1;
764 f->dst_w &= ~1;
765
766 f->dst_h &= ~1;
767 f->dst_y &= ~1;
768
769 f->src_h += f->src_y & 1;
770 f->src_y &= ~1;
771
772 f->src_h &= ~1;
773 f->dst_h &= ~1;
774
775 /* Due to rounding, we may have reduced the output size to <1/4 of
776 the source. Check again, but this time just resize. Don't change
777 source coordinates */
778 if (f->dst_w < f->src_w / 4) {
779 f->src_w &= ~3;
780 f->dst_w = f->src_w / 4;
781 f->dst_w += f->dst_w & 1;
782 }
783 if (f->dst_h < f->src_h / 4) {
784 f->src_h &= ~3;
785 f->dst_h = f->src_h / 4;
786 f->dst_h += f->dst_h & 1;
787 }
788
789 /* Check again. If there's nothing to safe to display, stop now */
790 if ((int)f->dst_w <= 2 || (int)f->dst_h <= 2 ||
791 (int)f->src_w <= 2 || (int)f->src_h <= 2) {
792 return IVTV_YUV_UPDATE_INVALID;
793 }
794
795 /* Both x offset & width are linked, so they have to be done together */
796 if ((of->dst_w != f->dst_w) || (of->src_w != f->src_w) ||
797 (of->dst_x != f->dst_x) || (of->src_x != f->src_x) ||
798 (of->pan_x != f->pan_x) || (of->vis_w != f->vis_w)) {
799 yuv_update |= IVTV_YUV_UPDATE_HORIZONTAL;
800 }
801
802 if ((of->src_h != f->src_h) || (of->dst_h != f->dst_h) ||
803 (of->dst_y != f->dst_y) || (of->src_y != f->src_y) ||
804 (of->pan_y != f->pan_y) || (of->vis_h != f->vis_h) ||
805 (of->lace_mode != f->lace_mode) ||
806 (of->interlaced_y != f->interlaced_y) ||
807 (of->interlaced_uv != f->interlaced_uv)) {
808 yuv_update |= IVTV_YUV_UPDATE_VERTICAL;
809 }
810
811 return yuv_update;
812}
813
814/* Update the scaling register to the requested value */
815void ivtv_yuv_work_handler(struct ivtv *itv)
816{
817 struct yuv_playback_info *yi = &itv->yuv_info;
818 struct yuv_frame_info f;
819 int frame = yi->update_frame;
820 u32 yuv_update;
821
822 IVTV_DEBUG_YUV("Update yuv registers for frame %d\n", frame);
823 f = yi->new_frame_info[frame];
824
825 if (yi->track_osd) {
826 /* Snapshot the osd pan info */
827 f.pan_x = yi->osd_x_pan;
828 f.pan_y = yi->osd_y_pan;
829 f.vis_w = yi->osd_vis_w;
830 f.vis_h = yi->osd_vis_h;
831 } else {
832 /* Not tracking the osd, so assume full screen */
833 f.pan_x = 0;
834 f.pan_y = 0;
835 f.vis_w = 720;
836 f.vis_h = yi->decode_height;
837 }
838
839 /* Calculate the display window coordinates. Exit if nothing left */
840 if (!(yuv_update = ivtv_yuv_window_setup(itv, &f)))
841 return;
842
843 if (yuv_update & IVTV_YUV_UPDATE_INVALID) {
844 write_reg(0x01008080, 0x2898);
845 } else if (yuv_update) {
846 write_reg(0x00108080, 0x2898);
847
848 if (yuv_update & IVTV_YUV_UPDATE_HORIZONTAL)
849 ivtv_yuv_handle_horizontal(itv, &f);
850
851 if (yuv_update & IVTV_YUV_UPDATE_VERTICAL)
852 ivtv_yuv_handle_vertical(itv, &f);
853 }
854 yi->old_frame_info = f;
855}
856
857static void ivtv_yuv_init(struct ivtv *itv)
858{
859 struct yuv_playback_info *yi = &itv->yuv_info;
860
861 IVTV_DEBUG_YUV("ivtv_yuv_init\n");
862
863 /* Take a snapshot of the current register settings */
864 yi->reg_2834 = read_reg(0x02834);
865 yi->reg_2838 = read_reg(0x02838);
866 yi->reg_283c = read_reg(0x0283c);
867 yi->reg_2840 = read_reg(0x02840);
868 yi->reg_2844 = read_reg(0x02844);
869 yi->reg_2848 = read_reg(0x02848);
870 yi->reg_2854 = read_reg(0x02854);
871 yi->reg_285c = read_reg(0x0285c);
872 yi->reg_2864 = read_reg(0x02864);
873 yi->reg_2870 = read_reg(0x02870);
874 yi->reg_2874 = read_reg(0x02874);
875 yi->reg_2898 = read_reg(0x02898);
876 yi->reg_2890 = read_reg(0x02890);
877
878 yi->reg_289c = read_reg(0x0289c);
879 yi->reg_2918 = read_reg(0x02918);
880 yi->reg_291c = read_reg(0x0291c);
881 yi->reg_2920 = read_reg(0x02920);
882 yi->reg_2924 = read_reg(0x02924);
883 yi->reg_2928 = read_reg(0x02928);
884 yi->reg_292c = read_reg(0x0292c);
885 yi->reg_2930 = read_reg(0x02930);
886 yi->reg_2934 = read_reg(0x02934);
887 yi->reg_2938 = read_reg(0x02938);
888 yi->reg_293c = read_reg(0x0293c);
889 yi->reg_2940 = read_reg(0x02940);
890 yi->reg_2944 = read_reg(0x02944);
891 yi->reg_2948 = read_reg(0x02948);
892 yi->reg_294c = read_reg(0x0294c);
893 yi->reg_2950 = read_reg(0x02950);
894 yi->reg_2954 = read_reg(0x02954);
895 yi->reg_2958 = read_reg(0x02958);
896 yi->reg_295c = read_reg(0x0295c);
897 yi->reg_2960 = read_reg(0x02960);
898 yi->reg_2964 = read_reg(0x02964);
899 yi->reg_2968 = read_reg(0x02968);
900 yi->reg_296c = read_reg(0x0296c);
901 yi->reg_2970 = read_reg(0x02970);
902
903 yi->v_filter_1 = -1;
904 yi->v_filter_2 = -1;
905 yi->h_filter = -1;
906
907 /* Set some valid size info */
908 yi->osd_x_offset = read_reg(0x02a04) & 0x00000FFF;
909 yi->osd_y_offset = (read_reg(0x02a04) >> 16) & 0x00000FFF;
910
911 /* Bit 2 of reg 2878 indicates current decoder output format
912 0 : NTSC 1 : PAL */
913 if (read_reg(0x2878) & 4)
914 yi->decode_height = 576;
915 else
916 yi->decode_height = 480;
917
918 if (!itv->osd_info) {
919 yi->osd_vis_w = 720 - yi->osd_x_offset;
920 yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
921 } else {
922 /* If no visible size set, assume full size */
923 if (!yi->osd_vis_w)
924 yi->osd_vis_w = 720 - yi->osd_x_offset;
925
926 if (!yi->osd_vis_h) {
927 yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
928 } else if (yi->osd_vis_h + yi->osd_y_offset > yi->decode_height) {
929 /* If output video standard has changed, requested height may
930 not be legal */
931 IVTV_DEBUG_WARN("Clipping yuv output - fb size (%d) exceeds video standard limit (%d)\n",
932 yi->osd_vis_h + yi->osd_y_offset,
933 yi->decode_height);
934 yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
935 }
936 }
937
938 /* We need a buffer for blanking when Y plane is offset - non-fatal if we can't get one */
939 yi->blanking_ptr = kzalloc(720 * 16, GFP_KERNEL|__GFP_NOWARN);
940 if (yi->blanking_ptr) {
941 yi->blanking_dmaptr = pci_map_single(itv->pdev, yi->blanking_ptr, 720*16, PCI_DMA_TODEVICE);
942 } else {
943 yi->blanking_dmaptr = 0;
944 IVTV_DEBUG_WARN("Failed to allocate yuv blanking buffer\n");
945 }
946
947 /* Enable YUV decoder output */
948 write_reg_sync(0x01, IVTV_REG_VDM);
949
950 set_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags);
951 atomic_set(&yi->next_dma_frame, 0);
952}
953
954/* Get next available yuv buffer on PVR350 */
955static void ivtv_yuv_next_free(struct ivtv *itv)
956{
957 int draw, display;
958 struct yuv_playback_info *yi = &itv->yuv_info;
959
960 if (atomic_read(&yi->next_dma_frame) == -1)
961 ivtv_yuv_init(itv);
962
963 draw = atomic_read(&yi->next_fill_frame);
964 display = atomic_read(&yi->next_dma_frame);
965
966 if (display > draw)
967 display -= IVTV_YUV_BUFFERS;
968
969 if (draw - display >= yi->max_frames_buffered)
970 draw = (u8)(draw - 1) % IVTV_YUV_BUFFERS;
971 else
972 yi->new_frame_info[draw].update = 0;
973
974 yi->draw_frame = draw;
975}
976
977/* Set up frame according to ivtv_dma_frame parameters */
978static void ivtv_yuv_setup_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
979{
980 struct yuv_playback_info *yi = &itv->yuv_info;
981 u8 frame = yi->draw_frame;
982 u8 last_frame = (u8)(frame - 1) % IVTV_YUV_BUFFERS;
983 struct yuv_frame_info *nf = &yi->new_frame_info[frame];
984 struct yuv_frame_info *of = &yi->new_frame_info[last_frame];
985 int lace_threshold = yi->lace_threshold;
986
987 /* Preserve old update flag in case we're overwriting a queued frame */
988 int update = nf->update;
989
990 /* Take a snapshot of the yuv coordinate information */
991 nf->src_x = args->src.left;
992 nf->src_y = args->src.top;
993 nf->src_w = args->src.width;
994 nf->src_h = args->src.height;
995 nf->dst_x = args->dst.left;
996 nf->dst_y = args->dst.top;
997 nf->dst_w = args->dst.width;
998 nf->dst_h = args->dst.height;
999 nf->tru_x = args->dst.left;
1000 nf->tru_w = args->src_width;
1001 nf->tru_h = args->src_height;
1002
1003 /* Are we going to offset the Y plane */
1004 nf->offset_y = (nf->tru_h + nf->src_x < 512 - 16) ? 1 : 0;
1005
1006 nf->update = 0;
1007 nf->interlaced_y = 0;
1008 nf->interlaced_uv = 0;
1009 nf->delay = 0;
1010 nf->sync_field = 0;
1011 nf->lace_mode = yi->lace_mode & IVTV_YUV_MODE_MASK;
1012
1013 if (lace_threshold < 0)
1014 lace_threshold = yi->decode_height - 1;
1015
1016 /* Work out the lace settings */
1017 switch (nf->lace_mode) {
1018 case IVTV_YUV_MODE_PROGRESSIVE: /* Progressive mode */
1019 nf->interlaced = 0;
1020 if (nf->tru_h < 512 || (nf->tru_h > 576 && nf->tru_h < 1021))
1021 nf->interlaced_y = 0;
1022 else
1023 nf->interlaced_y = 1;
1024
1025 if (nf->tru_h < 1021 && (nf->dst_h >= nf->src_h / 2))
1026 nf->interlaced_uv = 0;
1027 else
1028 nf->interlaced_uv = 1;
1029 break;
1030
1031 case IVTV_YUV_MODE_AUTO:
1032 if (nf->tru_h <= lace_threshold || nf->tru_h > 576 || nf->tru_w > 720) {
1033 nf->interlaced = 0;
1034 if ((nf->tru_h < 512) ||
1035 (nf->tru_h > 576 && nf->tru_h < 1021) ||
1036 (nf->tru_w > 720 && nf->tru_h < 1021))
1037 nf->interlaced_y = 0;
1038 else
1039 nf->interlaced_y = 1;
1040 if (nf->tru_h < 1021 && (nf->dst_h >= nf->src_h / 2))
1041 nf->interlaced_uv = 0;
1042 else
1043 nf->interlaced_uv = 1;
1044 } else {
1045 nf->interlaced = 1;
1046 nf->interlaced_y = 1;
1047 nf->interlaced_uv = 1;
1048 }
1049 break;
1050
1051 case IVTV_YUV_MODE_INTERLACED: /* Interlace mode */
1052 default:
1053 nf->interlaced = 1;
1054 nf->interlaced_y = 1;
1055 nf->interlaced_uv = 1;
1056 break;
1057 }
1058
1059 if (memcmp(&yi->old_frame_info_args, nf, sizeof(*nf))) {
1060 yi->old_frame_info_args = *nf;
1061 nf->update = 1;
1062 IVTV_DEBUG_YUV("Requesting reg update for frame %d\n", frame);
1063 }
1064
1065 nf->update |= update;
1066 nf->sync_field = yi->lace_sync_field;
1067 nf->delay = nf->sync_field != of->sync_field;
1068}
1069
1070/* Frame is complete & ready for display */
1071void ivtv_yuv_frame_complete(struct ivtv *itv)
1072{
1073 atomic_set(&itv->yuv_info.next_fill_frame,
1074 (itv->yuv_info.draw_frame + 1) % IVTV_YUV_BUFFERS);
1075}
1076
1077static int ivtv_yuv_udma_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
1078{
1079 DEFINE_WAIT(wait);
1080 int rc = 0;
1081 int got_sig = 0;
1082 /* DMA the frame */
1083 mutex_lock(&itv->udma.lock);
1084
1085 if ((rc = ivtv_yuv_prep_user_dma(itv, &itv->udma, args)) != 0) {
1086 mutex_unlock(&itv->udma.lock);
1087 return rc;
1088 }
1089
1090 ivtv_udma_prepare(itv);
1091 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
1092 /* if no UDMA is pending and no UDMA is in progress, then the DMA
1093 is finished */
1094 while (test_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags) ||
1095 test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
1096 /* don't interrupt if the DMA is in progress but break off
1097 a still pending DMA. */
1098 got_sig = signal_pending(current);
1099 if (got_sig && test_and_clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
1100 break;
1101 got_sig = 0;
1102 schedule();
1103 }
1104 finish_wait(&itv->dma_waitq, &wait);
1105
1106 /* Unmap Last DMA Xfer */
1107 ivtv_udma_unmap(itv);
1108
1109 if (got_sig) {
1110 IVTV_DEBUG_INFO("User stopped YUV UDMA\n");
1111 mutex_unlock(&itv->udma.lock);
1112 return -EINTR;
1113 }
1114
1115 ivtv_yuv_frame_complete(itv);
1116
1117 mutex_unlock(&itv->udma.lock);
1118 return rc;
1119}
1120
1121/* Setup frame according to V4L2 parameters */
1122void ivtv_yuv_setup_stream_frame(struct ivtv *itv)
1123{
1124 struct yuv_playback_info *yi = &itv->yuv_info;
1125 struct ivtv_dma_frame dma_args;
1126
1127 ivtv_yuv_next_free(itv);
1128
1129 /* Copy V4L2 parameters to an ivtv_dma_frame struct... */
1130 dma_args.y_source = NULL;
1131 dma_args.uv_source = NULL;
1132 dma_args.src.left = 0;
1133 dma_args.src.top = 0;
1134 dma_args.src.width = yi->v4l2_src_w;
1135 dma_args.src.height = yi->v4l2_src_h;
1136 dma_args.dst = yi->main_rect;
1137 dma_args.src_width = yi->v4l2_src_w;
1138 dma_args.src_height = yi->v4l2_src_h;
1139
1140 /* ... and use the same setup routine as ivtv_yuv_prep_frame */
1141 ivtv_yuv_setup_frame(itv, &dma_args);
1142
1143 if (!itv->dma_data_req_offset)
1144 itv->dma_data_req_offset = yuv_offset[yi->draw_frame];
1145}
1146
1147/* Attempt to dma a frame from a user buffer */
1148int ivtv_yuv_udma_stream_frame(struct ivtv *itv, void __user *src)
1149{
1150 struct yuv_playback_info *yi = &itv->yuv_info;
1151 struct ivtv_dma_frame dma_args;
1152 int res;
1153
1154 ivtv_yuv_setup_stream_frame(itv);
1155
1156 /* We only need to supply source addresses for this */
1157 dma_args.y_source = src;
1158 dma_args.uv_source = src + 720 * ((yi->v4l2_src_h + 31) & ~31);
1159 /* Wait for frame DMA. Note that serialize_lock is locked,
1160 so to allow other processes to access the driver while
1161 we are waiting unlock first and later lock again. */
1162 mutex_unlock(&itv->serialize_lock);
1163 res = ivtv_yuv_udma_frame(itv, &dma_args);
1164 mutex_lock(&itv->serialize_lock);
1165 return res;
1166}
1167
1168/* IVTV_IOC_DMA_FRAME ioctl handler */
1169int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
1170{
1171 int res;
1172
1173/* IVTV_DEBUG_INFO("yuv_prep_frame\n"); */
1174 ivtv_yuv_next_free(itv);
1175 ivtv_yuv_setup_frame(itv, args);
1176 /* Wait for frame DMA. Note that serialize_lock is locked,
1177 so to allow other processes to access the driver while
1178 we are waiting unlock first and later lock again. */
1179 mutex_unlock(&itv->serialize_lock);
1180 res = ivtv_yuv_udma_frame(itv, args);
1181 mutex_lock(&itv->serialize_lock);
1182 return res;
1183}
1184
1185void ivtv_yuv_close(struct ivtv *itv)
1186{
1187 struct yuv_playback_info *yi = &itv->yuv_info;
1188 int h_filter, v_filter_1, v_filter_2;
1189
1190 IVTV_DEBUG_YUV("ivtv_yuv_close\n");
1191 mutex_unlock(&itv->serialize_lock);
1192 ivtv_waitq(&itv->vsync_waitq);
1193 mutex_lock(&itv->serialize_lock);
1194
1195 yi->running = 0;
1196 atomic_set(&yi->next_dma_frame, -1);
1197 atomic_set(&yi->next_fill_frame, 0);
1198
1199 /* Reset registers we have changed so mpeg playback works */
1200
1201 /* If we fully restore this register, the display may remain active.
1202 Restore, but set one bit to blank the video. Firmware will always
1203 clear this bit when needed, so not a problem. */
1204 write_reg(yi->reg_2898 | 0x01000000, 0x2898);
1205
1206 write_reg(yi->reg_2834, 0x02834);
1207 write_reg(yi->reg_2838, 0x02838);
1208 write_reg(yi->reg_283c, 0x0283c);
1209 write_reg(yi->reg_2840, 0x02840);
1210 write_reg(yi->reg_2844, 0x02844);
1211 write_reg(yi->reg_2848, 0x02848);
1212 write_reg(yi->reg_2854, 0x02854);
1213 write_reg(yi->reg_285c, 0x0285c);
1214 write_reg(yi->reg_2864, 0x02864);
1215 write_reg(yi->reg_2870, 0x02870);
1216 write_reg(yi->reg_2874, 0x02874);
1217 write_reg(yi->reg_2890, 0x02890);
1218 write_reg(yi->reg_289c, 0x0289c);
1219
1220 write_reg(yi->reg_2918, 0x02918);
1221 write_reg(yi->reg_291c, 0x0291c);
1222 write_reg(yi->reg_2920, 0x02920);
1223 write_reg(yi->reg_2924, 0x02924);
1224 write_reg(yi->reg_2928, 0x02928);
1225 write_reg(yi->reg_292c, 0x0292c);
1226 write_reg(yi->reg_2930, 0x02930);
1227 write_reg(yi->reg_2934, 0x02934);
1228 write_reg(yi->reg_2938, 0x02938);
1229 write_reg(yi->reg_293c, 0x0293c);
1230 write_reg(yi->reg_2940, 0x02940);
1231 write_reg(yi->reg_2944, 0x02944);
1232 write_reg(yi->reg_2948, 0x02948);
1233 write_reg(yi->reg_294c, 0x0294c);
1234 write_reg(yi->reg_2950, 0x02950);
1235 write_reg(yi->reg_2954, 0x02954);
1236 write_reg(yi->reg_2958, 0x02958);
1237 write_reg(yi->reg_295c, 0x0295c);
1238 write_reg(yi->reg_2960, 0x02960);
1239 write_reg(yi->reg_2964, 0x02964);
1240 write_reg(yi->reg_2968, 0x02968);
1241 write_reg(yi->reg_296c, 0x0296c);
1242 write_reg(yi->reg_2970, 0x02970);
1243
1244 /* Prepare to restore filters */
1245
1246 /* First the horizontal filter */
1247 if ((yi->reg_2834 & 0x0000FFFF) == (yi->reg_2834 >> 16)) {
1248 /* An exact size match uses filter 0 */
1249 h_filter = 0;
1250 } else {
1251 /* Figure out which filter to use */
1252 h_filter = ((yi->reg_2834 << 16) / (yi->reg_2834 >> 16)) >> 15;
1253 h_filter = (h_filter >> 1) + (h_filter & 1);
1254 /* Only an exact size match can use filter 0. */
1255 h_filter += !h_filter;
1256 }
1257
1258 /* Now the vertical filter */
1259 if ((yi->reg_2918 & 0x0000FFFF) == (yi->reg_2918 >> 16)) {
1260 /* An exact size match uses filter 0/1 */
1261 v_filter_1 = 0;
1262 v_filter_2 = 1;
1263 } else {
1264 /* Figure out which filter to use */
1265 v_filter_1 = ((yi->reg_2918 << 16) / (yi->reg_2918 >> 16)) >> 15;
1266 v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1);
1267 /* Only an exact size match can use filter 0 */
1268 v_filter_1 += !v_filter_1;
1269 v_filter_2 = v_filter_1;
1270 }
1271
1272 /* Now restore the filters */
1273 ivtv_yuv_filter(itv, h_filter, v_filter_1, v_filter_2);
1274
1275 /* and clear a few registers */
1276 write_reg(0, 0x02814);
1277 write_reg(0, 0x0282c);
1278 write_reg(0, 0x02904);
1279 write_reg(0, 0x02910);
1280
1281 /* Release the blanking buffer */
1282 if (yi->blanking_ptr) {
1283 kfree(yi->blanking_ptr);
1284 yi->blanking_ptr = NULL;
1285 pci_unmap_single(itv->pdev, yi->blanking_dmaptr, 720*16, PCI_DMA_TODEVICE);
1286 }
1287
1288 /* Invalidate the old dimension information */
1289 yi->old_frame_info.src_w = 0;
1290 yi->old_frame_info.src_h = 0;
1291 yi->old_frame_info_args.src_w = 0;
1292 yi->old_frame_info_args.src_h = 0;
1293
1294 /* All done. */
1295 clear_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags);
1296}
diff --git a/drivers/media/pci/ivtv/ivtv-yuv.h b/drivers/media/pci/ivtv/ivtv-yuv.h
new file mode 100644
index 000000000000..ca5173fbf006
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtv-yuv.h
@@ -0,0 +1,44 @@
1/*
2 yuv support
3
4 Copyright (C) 2007 Ian Armstrong <ian@iarmst.demon.co.uk>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef IVTV_YUV_H
22#define IVTV_YUV_H
23
24#define IVTV_YUV_BUFFER_UV_OFFSET 0x65400 /* Offset to UV Buffer */
25
26/* Offset to filter table in firmware */
27#define IVTV_YUV_HORIZONTAL_FILTER_OFFSET 0x025d8
28#define IVTV_YUV_VERTICAL_FILTER_OFFSET 0x03358
29
30#define IVTV_YUV_UPDATE_HORIZONTAL 0x01
31#define IVTV_YUV_UPDATE_VERTICAL 0x02
32#define IVTV_YUV_UPDATE_INVALID 0x04
33
34extern const u32 yuv_offset[IVTV_YUV_BUFFERS];
35
36int ivtv_yuv_filter_check(struct ivtv *itv);
37void ivtv_yuv_setup_stream_frame(struct ivtv *itv);
38int ivtv_yuv_udma_stream_frame(struct ivtv *itv, void __user *src);
39void ivtv_yuv_frame_complete(struct ivtv *itv);
40int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args);
41void ivtv_yuv_close(struct ivtv *itv);
42void ivtv_yuv_work_handler(struct ivtv *itv);
43
44#endif
diff --git a/drivers/media/pci/ivtv/ivtvfb.c b/drivers/media/pci/ivtv/ivtvfb.c
new file mode 100644
index 000000000000..05b94aa8ba32
--- /dev/null
+++ b/drivers/media/pci/ivtv/ivtvfb.c
@@ -0,0 +1,1317 @@
1/*
2 On Screen Display cx23415 Framebuffer driver
3
4 This module presents the cx23415 OSD (onscreen display) framebuffer memory
5 as a standard Linux /dev/fb style framebuffer device. The framebuffer has
6 support for 8, 16 & 32 bpp packed pixel formats with alpha channel. In 16bpp
7 mode, there is a choice of a three color depths (12, 15 or 16 bits), but no
8 local alpha. The colorspace is selectable between rgb & yuv.
9 Depending on the TV standard configured in the ivtv module at load time,
10 the initial resolution is either 640x400 (NTSC) or 640x480 (PAL) at 8bpp.
11 Video timings are locked to ensure a vertical refresh rate of 50Hz (PAL)
12 or 59.94 (NTSC)
13
14 Copyright (c) 2003 Matt T. Yourst <yourst@yourst.com>
15
16 Derived from drivers/video/vesafb.c
17 Portions (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
18
19 2.6 kernel port:
20 Copyright (C) 2004 Matthias Badaire
21
22 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
23
24 Copyright (C) 2006 Ian Armstrong <ian@iarmst.demon.co.uk>
25
26 This program is free software; you can redistribute it and/or modify
27 it under the terms of the GNU General Public License as published by
28 the Free Software Foundation; either version 2 of the License, or
29 (at your option) any later version.
30
31 This program is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 GNU General Public License for more details.
35
36 You should have received a copy of the GNU General Public License
37 along with this program; if not, write to the Free Software
38 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 */
40
41#include <linux/module.h>
42#include <linux/kernel.h>
43#include <linux/fb.h>
44#include <linux/ivtvfb.h>
45#include <linux/slab.h>
46
47#ifdef CONFIG_MTRR
48#include <asm/mtrr.h>
49#endif
50
51#include "ivtv-driver.h"
52#include "ivtv-cards.h"
53#include "ivtv-i2c.h"
54#include "ivtv-udma.h"
55#include "ivtv-mailbox.h"
56#include "ivtv-firmware.h"
57
58/* card parameters */
59static int ivtvfb_card_id = -1;
60static int ivtvfb_debug = 0;
61static bool osd_laced;
62static int osd_depth;
63static int osd_upper;
64static int osd_left;
65static int osd_yres;
66static int osd_xres;
67
68module_param(ivtvfb_card_id, int, 0444);
69module_param_named(debug,ivtvfb_debug, int, 0644);
70module_param(osd_laced, bool, 0444);
71module_param(osd_depth, int, 0444);
72module_param(osd_upper, int, 0444);
73module_param(osd_left, int, 0444);
74module_param(osd_yres, int, 0444);
75module_param(osd_xres, int, 0444);
76
77MODULE_PARM_DESC(ivtvfb_card_id,
78 "Only use framebuffer of the specified ivtv card (0-31)\n"
79 "\t\t\tdefault -1: initialize all available framebuffers");
80
81MODULE_PARM_DESC(debug,
82 "Debug level (bitmask). Default: errors only\n"
83 "\t\t\t(debug = 3 gives full debugging)");
84
85/* Why upper, left, xres, yres, depth, laced ? To match terminology used
86 by fbset.
87 Why start at 1 for left & upper coordinate ? Because X doesn't allow 0 */
88
89MODULE_PARM_DESC(osd_laced,
90 "Interlaced mode\n"
91 "\t\t\t0=off\n"
92 "\t\t\t1=on\n"
93 "\t\t\tdefault off");
94
95MODULE_PARM_DESC(osd_depth,
96 "Bits per pixel - 8, 16, 32\n"
97 "\t\t\tdefault 8");
98
99MODULE_PARM_DESC(osd_upper,
100 "Vertical start position\n"
101 "\t\t\tdefault 0 (Centered)");
102
103MODULE_PARM_DESC(osd_left,
104 "Horizontal start position\n"
105 "\t\t\tdefault 0 (Centered)");
106
107MODULE_PARM_DESC(osd_yres,
108 "Display height\n"
109 "\t\t\tdefault 480 (PAL)\n"
110 "\t\t\t 400 (NTSC)");
111
112MODULE_PARM_DESC(osd_xres,
113 "Display width\n"
114 "\t\t\tdefault 640");
115
116MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil, John Harvey, Ian Armstrong");
117MODULE_LICENSE("GPL");
118
119/* --------------------------------------------------------------------- */
120
121#define IVTVFB_DBGFLG_WARN (1 << 0)
122#define IVTVFB_DBGFLG_INFO (1 << 1)
123
124#define IVTVFB_DEBUG(x, type, fmt, args...) \
125 do { \
126 if ((x) & ivtvfb_debug) \
127 printk(KERN_INFO "ivtvfb%d " type ": " fmt, itv->instance , ## args); \
128 } while (0)
129#define IVTVFB_DEBUG_WARN(fmt, args...) IVTVFB_DEBUG(IVTVFB_DBGFLG_WARN, "warning", fmt , ## args)
130#define IVTVFB_DEBUG_INFO(fmt, args...) IVTVFB_DEBUG(IVTVFB_DBGFLG_INFO, "info", fmt , ## args)
131
132/* Standard kernel messages */
133#define IVTVFB_ERR(fmt, args...) printk(KERN_ERR "ivtvfb%d: " fmt, itv->instance , ## args)
134#define IVTVFB_WARN(fmt, args...) printk(KERN_WARNING "ivtvfb%d: " fmt, itv->instance , ## args)
135#define IVTVFB_INFO(fmt, args...) printk(KERN_INFO "ivtvfb%d: " fmt, itv->instance , ## args)
136
137/* --------------------------------------------------------------------- */
138
139#define IVTV_OSD_MAX_WIDTH 720
140#define IVTV_OSD_MAX_HEIGHT 576
141
142#define IVTV_OSD_BPP_8 0x00
143#define IVTV_OSD_BPP_16_444 0x03
144#define IVTV_OSD_BPP_16_555 0x02
145#define IVTV_OSD_BPP_16_565 0x01
146#define IVTV_OSD_BPP_32 0x04
147
148struct osd_info {
149 /* Physical base address */
150 unsigned long video_pbase;
151 /* Relative base address (relative to start of decoder memory) */
152 u32 video_rbase;
153 /* Mapped base address */
154 volatile char __iomem *video_vbase;
155 /* Buffer size */
156 u32 video_buffer_size;
157
158#ifdef CONFIG_MTRR
159 /* video_base rounded down as required by hardware MTRRs */
160 unsigned long fb_start_aligned_physaddr;
161 /* video_base rounded up as required by hardware MTRRs */
162 unsigned long fb_end_aligned_physaddr;
163#endif
164
165 /* Store the buffer offset */
166 int set_osd_coords_x;
167 int set_osd_coords_y;
168
169 /* Current dimensions (NOT VISIBLE SIZE!) */
170 int display_width;
171 int display_height;
172 int display_byte_stride;
173
174 /* Current bits per pixel */
175 int bits_per_pixel;
176 int bytes_per_pixel;
177
178 /* Frame buffer stuff */
179 struct fb_info ivtvfb_info;
180 struct fb_var_screeninfo ivtvfb_defined;
181 struct fb_fix_screeninfo ivtvfb_fix;
182
183 /* Used for a warm start */
184 struct fb_var_screeninfo fbvar_cur;
185 int blank_cur;
186 u32 palette_cur[256];
187 u32 pan_cur;
188};
189
190struct ivtv_osd_coords {
191 unsigned long offset;
192 unsigned long max_offset;
193 int pixel_stride;
194 int lines;
195 int x;
196 int y;
197};
198
199/* --------------------------------------------------------------------- */
200
201/* ivtv API calls for framebuffer related support */
202
203static int ivtvfb_get_framebuffer(struct ivtv *itv, u32 *fbbase,
204 u32 *fblength)
205{
206 u32 data[CX2341X_MBOX_MAX_DATA];
207 int rc;
208
209 ivtv_firmware_check(itv, "ivtvfb_get_framebuffer");
210 rc = ivtv_vapi_result(itv, data, CX2341X_OSD_GET_FRAMEBUFFER, 0);
211 *fbbase = data[0];
212 *fblength = data[1];
213 return rc;
214}
215
216static int ivtvfb_get_osd_coords(struct ivtv *itv,
217 struct ivtv_osd_coords *osd)
218{
219 struct osd_info *oi = itv->osd_info;
220 u32 data[CX2341X_MBOX_MAX_DATA];
221
222 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_OSD_COORDS, 0);
223
224 osd->offset = data[0] - oi->video_rbase;
225 osd->max_offset = oi->display_width * oi->display_height * 4;
226 osd->pixel_stride = data[1];
227 osd->lines = data[2];
228 osd->x = data[3];
229 osd->y = data[4];
230 return 0;
231}
232
233static int ivtvfb_set_osd_coords(struct ivtv *itv, const struct ivtv_osd_coords *osd)
234{
235 struct osd_info *oi = itv->osd_info;
236
237 oi->display_width = osd->pixel_stride;
238 oi->display_byte_stride = osd->pixel_stride * oi->bytes_per_pixel;
239 oi->set_osd_coords_x += osd->x;
240 oi->set_osd_coords_y = osd->y;
241
242 return ivtv_vapi(itv, CX2341X_OSD_SET_OSD_COORDS, 5,
243 osd->offset + oi->video_rbase,
244 osd->pixel_stride,
245 osd->lines, osd->x, osd->y);
246}
247
248static int ivtvfb_set_display_window(struct ivtv *itv, struct v4l2_rect *ivtv_window)
249{
250 int osd_height_limit = itv->is_out_50hz ? 576 : 480;
251
252 /* Only fail if resolution too high, otherwise fudge the start coords. */
253 if ((ivtv_window->height > osd_height_limit) || (ivtv_window->width > IVTV_OSD_MAX_WIDTH))
254 return -EINVAL;
255
256 /* Ensure we don't exceed display limits */
257 if (ivtv_window->top + ivtv_window->height > osd_height_limit) {
258 IVTVFB_DEBUG_WARN("ivtv_ioctl_fb_set_display_window - Invalid height setting (%d, %d)\n",
259 ivtv_window->top, ivtv_window->height);
260 ivtv_window->top = osd_height_limit - ivtv_window->height;
261 }
262
263 if (ivtv_window->left + ivtv_window->width > IVTV_OSD_MAX_WIDTH) {
264 IVTVFB_DEBUG_WARN("ivtv_ioctl_fb_set_display_window - Invalid width setting (%d, %d)\n",
265 ivtv_window->left, ivtv_window->width);
266 ivtv_window->left = IVTV_OSD_MAX_WIDTH - ivtv_window->width;
267 }
268
269 /* Set the OSD origin */
270 write_reg((ivtv_window->top << 16) | ivtv_window->left, 0x02a04);
271
272 /* How much to display */
273 write_reg(((ivtv_window->top+ivtv_window->height) << 16) | (ivtv_window->left+ivtv_window->width), 0x02a08);
274
275 /* Pass this info back the yuv handler */
276 itv->yuv_info.osd_vis_w = ivtv_window->width;
277 itv->yuv_info.osd_vis_h = ivtv_window->height;
278 itv->yuv_info.osd_x_offset = ivtv_window->left;
279 itv->yuv_info.osd_y_offset = ivtv_window->top;
280
281 return 0;
282}
283
284static int ivtvfb_prep_dec_dma_to_device(struct ivtv *itv,
285 unsigned long ivtv_dest_addr, void __user *userbuf,
286 int size_in_bytes)
287{
288 DEFINE_WAIT(wait);
289 int got_sig = 0;
290
291 mutex_lock(&itv->udma.lock);
292 /* Map User DMA */
293 if (ivtv_udma_setup(itv, ivtv_dest_addr, userbuf, size_in_bytes) <= 0) {
294 mutex_unlock(&itv->udma.lock);
295 IVTVFB_WARN("ivtvfb_prep_dec_dma_to_device, "
296 "Error with get_user_pages: %d bytes, %d pages returned\n",
297 size_in_bytes, itv->udma.page_count);
298
299 /* get_user_pages must have failed completely */
300 return -EIO;
301 }
302
303 IVTVFB_DEBUG_INFO("ivtvfb_prep_dec_dma_to_device, %d bytes, %d pages\n",
304 size_in_bytes, itv->udma.page_count);
305
306 ivtv_udma_prepare(itv);
307 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
308 /* if no UDMA is pending and no UDMA is in progress, then the DMA
309 is finished */
310 while (test_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags) ||
311 test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
312 /* don't interrupt if the DMA is in progress but break off
313 a still pending DMA. */
314 got_sig = signal_pending(current);
315 if (got_sig && test_and_clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
316 break;
317 got_sig = 0;
318 schedule();
319 }
320 finish_wait(&itv->dma_waitq, &wait);
321
322 /* Unmap Last DMA Xfer */
323 ivtv_udma_unmap(itv);
324 mutex_unlock(&itv->udma.lock);
325 if (got_sig) {
326 IVTV_DEBUG_INFO("User stopped OSD\n");
327 return -EINTR;
328 }
329
330 return 0;
331}
332
333static int ivtvfb_prep_frame(struct ivtv *itv, int cmd, void __user *source,
334 unsigned long dest_offset, int count)
335{
336 DEFINE_WAIT(wait);
337 struct osd_info *oi = itv->osd_info;
338
339 /* Nothing to do */
340 if (count == 0) {
341 IVTVFB_DEBUG_WARN("ivtvfb_prep_frame: Nothing to do. count = 0\n");
342 return -EINVAL;
343 }
344
345 /* Check Total FB Size */
346 if ((dest_offset + count) > oi->video_buffer_size) {
347 IVTVFB_WARN("ivtvfb_prep_frame: Overflowing the framebuffer %ld, only %d available\n",
348 dest_offset + count, oi->video_buffer_size);
349 return -E2BIG;
350 }
351
352 /* Not fatal, but will have undesirable results */
353 if ((unsigned long)source & 3)
354 IVTVFB_WARN("ivtvfb_prep_frame: Source address not 32 bit aligned (0x%08lx)\n",
355 (unsigned long)source);
356
357 if (dest_offset & 3)
358 IVTVFB_WARN("ivtvfb_prep_frame: Dest offset not 32 bit aligned (%ld)\n", dest_offset);
359
360 if (count & 3)
361 IVTVFB_WARN("ivtvfb_prep_frame: Count not a multiple of 4 (%d)\n", count);
362
363 /* Check Source */
364 if (!access_ok(VERIFY_READ, source + dest_offset, count)) {
365 IVTVFB_WARN("Invalid userspace pointer 0x%08lx\n",
366 (unsigned long)source);
367
368 IVTVFB_DEBUG_WARN("access_ok() failed for offset 0x%08lx source 0x%08lx count %d\n",
369 dest_offset, (unsigned long)source,
370 count);
371 return -EINVAL;
372 }
373
374 /* OSD Address to send DMA to */
375 dest_offset += IVTV_DECODER_OFFSET + oi->video_rbase;
376
377 /* Fill Buffers */
378 return ivtvfb_prep_dec_dma_to_device(itv, dest_offset, source, count);
379}
380
381static ssize_t ivtvfb_write(struct fb_info *info, const char __user *buf,
382 size_t count, loff_t *ppos)
383{
384 unsigned long p = *ppos;
385 void *dst;
386 int err = 0;
387 int dma_err;
388 unsigned long total_size;
389 struct ivtv *itv = (struct ivtv *) info->par;
390 unsigned long dma_offset =
391 IVTV_DECODER_OFFSET + itv->osd_info->video_rbase;
392 unsigned long dma_size;
393 u16 lead = 0, tail = 0;
394
395 if (info->state != FBINFO_STATE_RUNNING)
396 return -EPERM;
397
398 total_size = info->screen_size;
399
400 if (total_size == 0)
401 total_size = info->fix.smem_len;
402
403 if (p > total_size)
404 return -EFBIG;
405
406 if (count > total_size) {
407 err = -EFBIG;
408 count = total_size;
409 }
410
411 if (count + p > total_size) {
412 if (!err)
413 err = -ENOSPC;
414 count = total_size - p;
415 }
416
417 dst = (void __force *) (info->screen_base + p);
418
419 if (info->fbops->fb_sync)
420 info->fbops->fb_sync(info);
421
422 /* If transfer size > threshold and both src/dst
423 addresses are aligned, use DMA */
424 if (count >= 4096 &&
425 ((unsigned long)buf & 3) == ((unsigned long)dst & 3)) {
426 /* Odd address = can't DMA. Align */
427 if ((unsigned long)dst & 3) {
428 lead = 4 - ((unsigned long)dst & 3);
429 if (copy_from_user(dst, buf, lead))
430 return -EFAULT;
431 buf += lead;
432 dst += lead;
433 }
434 /* DMA resolution is 32 bits */
435 if ((count - lead) & 3)
436 tail = (count - lead) & 3;
437 /* DMA the data */
438 dma_size = count - lead - tail;
439 dma_err = ivtvfb_prep_dec_dma_to_device(itv,
440 p + lead + dma_offset, (void __user *)buf, dma_size);
441 if (dma_err)
442 return dma_err;
443 dst += dma_size;
444 buf += dma_size;
445 /* Copy any leftover data */
446 if (tail && copy_from_user(dst, buf, tail))
447 return -EFAULT;
448 } else if (copy_from_user(dst, buf, count)) {
449 return -EFAULT;
450 }
451
452 if (!err)
453 *ppos += count;
454
455 return (err) ? err : count;
456}
457
458static int ivtvfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
459{
460 DEFINE_WAIT(wait);
461 struct ivtv *itv = (struct ivtv *)info->par;
462 int rc = 0;
463
464 switch (cmd) {
465 case FBIOGET_VBLANK: {
466 struct fb_vblank vblank;
467 u32 trace;
468
469 memset(&vblank, 0, sizeof(struct fb_vblank));
470
471 vblank.flags = FB_VBLANK_HAVE_COUNT |FB_VBLANK_HAVE_VCOUNT |
472 FB_VBLANK_HAVE_VSYNC;
473 trace = read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16;
474 if (itv->is_out_50hz && trace > 312)
475 trace -= 312;
476 else if (itv->is_out_60hz && trace > 262)
477 trace -= 262;
478 if (trace == 1)
479 vblank.flags |= FB_VBLANK_VSYNCING;
480 vblank.count = itv->last_vsync_field;
481 vblank.vcount = trace;
482 vblank.hcount = 0;
483 if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
484 return -EFAULT;
485 return 0;
486 }
487
488 case FBIO_WAITFORVSYNC:
489 prepare_to_wait(&itv->vsync_waitq, &wait, TASK_INTERRUPTIBLE);
490 if (!schedule_timeout(msecs_to_jiffies(50)))
491 rc = -ETIMEDOUT;
492 finish_wait(&itv->vsync_waitq, &wait);
493 return rc;
494
495 case IVTVFB_IOC_DMA_FRAME: {
496 struct ivtvfb_dma_frame args;
497
498 IVTVFB_DEBUG_INFO("IVTVFB_IOC_DMA_FRAME\n");
499 if (copy_from_user(&args, (void __user *)arg, sizeof(args)))
500 return -EFAULT;
501
502 return ivtvfb_prep_frame(itv, cmd, args.source, args.dest_offset, args.count);
503 }
504
505 default:
506 IVTVFB_DEBUG_INFO("Unknown ioctl %08x\n", cmd);
507 return -EINVAL;
508 }
509 return 0;
510}
511
512/* Framebuffer device handling */
513
514static int ivtvfb_set_var(struct ivtv *itv, struct fb_var_screeninfo *var)
515{
516 struct osd_info *oi = itv->osd_info;
517 struct ivtv_osd_coords ivtv_osd;
518 struct v4l2_rect ivtv_window;
519 int osd_mode = -1;
520
521 IVTVFB_DEBUG_INFO("ivtvfb_set_var\n");
522
523 /* Select color space */
524 if (var->nonstd) /* YUV */
525 write_reg(read_reg(0x02a00) | 0x0002000, 0x02a00);
526 else /* RGB */
527 write_reg(read_reg(0x02a00) & ~0x0002000, 0x02a00);
528
529 /* Set the color mode */
530 switch (var->bits_per_pixel) {
531 case 8:
532 osd_mode = IVTV_OSD_BPP_8;
533 break;
534 case 32:
535 osd_mode = IVTV_OSD_BPP_32;
536 break;
537 case 16:
538 switch (var->green.length) {
539 case 4:
540 osd_mode = IVTV_OSD_BPP_16_444;
541 break;
542 case 5:
543 osd_mode = IVTV_OSD_BPP_16_555;
544 break;
545 case 6:
546 osd_mode = IVTV_OSD_BPP_16_565;
547 break;
548 default:
549 IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid bpp\n");
550 }
551 break;
552 default:
553 IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid bpp\n");
554 }
555
556 /* Set video mode. Although rare, the display can become scrambled even
557 if we don't change mode. Always 'bounce' to osd_mode via mode 0 */
558 if (osd_mode != -1) {
559 ivtv_vapi(itv, CX2341X_OSD_SET_PIXEL_FORMAT, 1, 0);
560 ivtv_vapi(itv, CX2341X_OSD_SET_PIXEL_FORMAT, 1, osd_mode);
561 }
562
563 oi->bits_per_pixel = var->bits_per_pixel;
564 oi->bytes_per_pixel = var->bits_per_pixel / 8;
565
566 /* Set the flicker filter */
567 switch (var->vmode & FB_VMODE_MASK) {
568 case FB_VMODE_NONINTERLACED: /* Filter on */
569 ivtv_vapi(itv, CX2341X_OSD_SET_FLICKER_STATE, 1, 1);
570 break;
571 case FB_VMODE_INTERLACED: /* Filter off */
572 ivtv_vapi(itv, CX2341X_OSD_SET_FLICKER_STATE, 1, 0);
573 break;
574 default:
575 IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid video mode\n");
576 }
577
578 /* Read the current osd info */
579 ivtvfb_get_osd_coords(itv, &ivtv_osd);
580
581 /* Now set the OSD to the size we want */
582 ivtv_osd.pixel_stride = var->xres_virtual;
583 ivtv_osd.lines = var->yres_virtual;
584 ivtv_osd.x = 0;
585 ivtv_osd.y = 0;
586 ivtvfb_set_osd_coords(itv, &ivtv_osd);
587
588 /* Can't seem to find the right API combo for this.
589 Use another function which does what we need through direct register access. */
590 ivtv_window.width = var->xres;
591 ivtv_window.height = var->yres;
592
593 /* Minimum margin cannot be 0, as X won't allow such a mode */
594 if (!var->upper_margin)
595 var->upper_margin++;
596 if (!var->left_margin)
597 var->left_margin++;
598 ivtv_window.top = var->upper_margin - 1;
599 ivtv_window.left = var->left_margin - 1;
600
601 ivtvfb_set_display_window(itv, &ivtv_window);
602
603 /* Pass screen size back to yuv handler */
604 itv->yuv_info.osd_full_w = ivtv_osd.pixel_stride;
605 itv->yuv_info.osd_full_h = ivtv_osd.lines;
606
607 /* Force update of yuv registers */
608 itv->yuv_info.yuv_forced_update = 1;
609
610 /* Keep a copy of these settings */
611 memcpy(&oi->fbvar_cur, var, sizeof(oi->fbvar_cur));
612
613 IVTVFB_DEBUG_INFO("Display size: %dx%d (virtual %dx%d) @ %dbpp\n",
614 var->xres, var->yres,
615 var->xres_virtual, var->yres_virtual,
616 var->bits_per_pixel);
617
618 IVTVFB_DEBUG_INFO("Display position: %d, %d\n",
619 var->left_margin, var->upper_margin);
620
621 IVTVFB_DEBUG_INFO("Display filter: %s\n",
622 (var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED ? "on" : "off");
623 IVTVFB_DEBUG_INFO("Color space: %s\n", var->nonstd ? "YUV" : "RGB");
624
625 return 0;
626}
627
628static int ivtvfb_get_fix(struct ivtv *itv, struct fb_fix_screeninfo *fix)
629{
630 struct osd_info *oi = itv->osd_info;
631
632 IVTVFB_DEBUG_INFO("ivtvfb_get_fix\n");
633 memset(fix, 0, sizeof(struct fb_fix_screeninfo));
634 strlcpy(fix->id, "cx23415 TV out", sizeof(fix->id));
635 fix->smem_start = oi->video_pbase;
636 fix->smem_len = oi->video_buffer_size;
637 fix->type = FB_TYPE_PACKED_PIXELS;
638 fix->visual = (oi->bits_per_pixel == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
639 fix->xpanstep = 1;
640 fix->ypanstep = 1;
641 fix->ywrapstep = 0;
642 fix->line_length = oi->display_byte_stride;
643 fix->accel = FB_ACCEL_NONE;
644 return 0;
645}
646
647/* Check the requested display mode, returning -EINVAL if we can't
648 handle it. */
649
650static int _ivtvfb_check_var(struct fb_var_screeninfo *var, struct ivtv *itv)
651{
652 struct osd_info *oi = itv->osd_info;
653 int osd_height_limit;
654 u32 pixclock, hlimit, vlimit;
655
656 IVTVFB_DEBUG_INFO("ivtvfb_check_var\n");
657
658 /* Set base references for mode calcs. */
659 if (itv->is_out_50hz) {
660 pixclock = 84316;
661 hlimit = 776;
662 vlimit = 591;
663 osd_height_limit = 576;
664 }
665 else {
666 pixclock = 83926;
667 hlimit = 776;
668 vlimit = 495;
669 osd_height_limit = 480;
670 }
671
672 if (var->bits_per_pixel == 8 || var->bits_per_pixel == 32) {
673 var->transp.offset = 24;
674 var->transp.length = 8;
675 var->red.offset = 16;
676 var->red.length = 8;
677 var->green.offset = 8;
678 var->green.length = 8;
679 var->blue.offset = 0;
680 var->blue.length = 8;
681 }
682 else if (var->bits_per_pixel == 16) {
683 /* To find out the true mode, check green length */
684 switch (var->green.length) {
685 case 4:
686 var->red.offset = 8;
687 var->red.length = 4;
688 var->green.offset = 4;
689 var->green.length = 4;
690 var->blue.offset = 0;
691 var->blue.length = 4;
692 var->transp.offset = 12;
693 var->transp.length = 1;
694 break;
695 case 5:
696 var->red.offset = 10;
697 var->red.length = 5;
698 var->green.offset = 5;
699 var->green.length = 5;
700 var->blue.offset = 0;
701 var->blue.length = 5;
702 var->transp.offset = 15;
703 var->transp.length = 1;
704 break;
705 default:
706 var->red.offset = 11;
707 var->red.length = 5;
708 var->green.offset = 5;
709 var->green.length = 6;
710 var->blue.offset = 0;
711 var->blue.length = 5;
712 var->transp.offset = 0;
713 var->transp.length = 0;
714 break;
715 }
716 }
717 else {
718 IVTVFB_DEBUG_WARN("Invalid colour mode: %d\n", var->bits_per_pixel);
719 return -EINVAL;
720 }
721
722 /* Check the resolution */
723 if (var->xres > IVTV_OSD_MAX_WIDTH || var->yres > osd_height_limit) {
724 IVTVFB_DEBUG_WARN("Invalid resolution: %dx%d\n",
725 var->xres, var->yres);
726 return -EINVAL;
727 }
728
729 /* Max horizontal size is 1023 @ 32bpp, 2046 & 16bpp, 4092 @ 8bpp */
730 if (var->xres_virtual > 4095 / (var->bits_per_pixel / 8) ||
731 var->xres_virtual * var->yres_virtual * (var->bits_per_pixel / 8) > oi->video_buffer_size ||
732 var->xres_virtual < var->xres ||
733 var->yres_virtual < var->yres) {
734 IVTVFB_DEBUG_WARN("Invalid virtual resolution: %dx%d\n",
735 var->xres_virtual, var->yres_virtual);
736 return -EINVAL;
737 }
738
739 /* Some extra checks if in 8 bit mode */
740 if (var->bits_per_pixel == 8) {
741 /* Width must be a multiple of 4 */
742 if (var->xres & 3) {
743 IVTVFB_DEBUG_WARN("Invalid resolution for 8bpp: %d\n", var->xres);
744 return -EINVAL;
745 }
746 if (var->xres_virtual & 3) {
747 IVTVFB_DEBUG_WARN("Invalid virtual resolution for 8bpp: %d)\n", var->xres_virtual);
748 return -EINVAL;
749 }
750 }
751 else if (var->bits_per_pixel == 16) {
752 /* Width must be a multiple of 2 */
753 if (var->xres & 1) {
754 IVTVFB_DEBUG_WARN("Invalid resolution for 16bpp: %d\n", var->xres);
755 return -EINVAL;
756 }
757 if (var->xres_virtual & 1) {
758 IVTVFB_DEBUG_WARN("Invalid virtual resolution for 16bpp: %d)\n", var->xres_virtual);
759 return -EINVAL;
760 }
761 }
762
763 /* Now check the offsets */
764 if (var->xoffset >= var->xres_virtual || var->yoffset >= var->yres_virtual) {
765 IVTVFB_DEBUG_WARN("Invalid offset: %d (%d) %d (%d)\n",
766 var->xoffset, var->xres_virtual, var->yoffset, var->yres_virtual);
767 return -EINVAL;
768 }
769
770 /* Check pixel format */
771 if (var->nonstd > 1) {
772 IVTVFB_DEBUG_WARN("Invalid nonstd % d\n", var->nonstd);
773 return -EINVAL;
774 }
775
776 /* Check video mode */
777 if (((var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED) &&
778 ((var->vmode & FB_VMODE_MASK) != FB_VMODE_INTERLACED)) {
779 IVTVFB_DEBUG_WARN("Invalid video mode: %d\n", var->vmode & FB_VMODE_MASK);
780 return -EINVAL;
781 }
782
783 /* Check the left & upper margins
784 If the margins are too large, just center the screen
785 (enforcing margins causes too many problems) */
786
787 if (var->left_margin + var->xres > IVTV_OSD_MAX_WIDTH + 1)
788 var->left_margin = 1 + ((IVTV_OSD_MAX_WIDTH - var->xres) / 2);
789
790 if (var->upper_margin + var->yres > (itv->is_out_50hz ? 577 : 481))
791 var->upper_margin = 1 + (((itv->is_out_50hz ? 576 : 480) -
792 var->yres) / 2);
793
794 /* Maintain overall 'size' for a constant refresh rate */
795 var->right_margin = hlimit - var->left_margin - var->xres;
796 var->lower_margin = vlimit - var->upper_margin - var->yres;
797
798 /* Fixed sync times */
799 var->hsync_len = 24;
800 var->vsync_len = 2;
801
802 /* Non-interlaced / interlaced mode is used to switch the OSD filter
803 on or off. Adjust the clock timings to maintain a constant
804 vertical refresh rate. */
805 if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED)
806 var->pixclock = pixclock / 2;
807 else
808 var->pixclock = pixclock;
809
810 itv->osd_rect.width = var->xres;
811 itv->osd_rect.height = var->yres;
812
813 IVTVFB_DEBUG_INFO("Display size: %dx%d (virtual %dx%d) @ %dbpp\n",
814 var->xres, var->yres,
815 var->xres_virtual, var->yres_virtual,
816 var->bits_per_pixel);
817
818 IVTVFB_DEBUG_INFO("Display position: %d, %d\n",
819 var->left_margin, var->upper_margin);
820
821 IVTVFB_DEBUG_INFO("Display filter: %s\n",
822 (var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED ? "on" : "off");
823 IVTVFB_DEBUG_INFO("Color space: %s\n", var->nonstd ? "YUV" : "RGB");
824 return 0;
825}
826
827static int ivtvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
828{
829 struct ivtv *itv = (struct ivtv *) info->par;
830 IVTVFB_DEBUG_INFO("ivtvfb_check_var\n");
831 return _ivtvfb_check_var(var, itv);
832}
833
834static int ivtvfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
835{
836 u32 osd_pan_index;
837 struct ivtv *itv = (struct ivtv *) info->par;
838
839 if (var->yoffset + info->var.yres > info->var.yres_virtual ||
840 var->xoffset + info->var.xres > info->var.xres_virtual)
841 return -EINVAL;
842
843 osd_pan_index = var->yoffset * info->fix.line_length
844 + var->xoffset * info->var.bits_per_pixel / 8;
845 write_reg(osd_pan_index, 0x02A0C);
846
847 /* Pass this info back the yuv handler */
848 itv->yuv_info.osd_x_pan = var->xoffset;
849 itv->yuv_info.osd_y_pan = var->yoffset;
850 /* Force update of yuv registers */
851 itv->yuv_info.yuv_forced_update = 1;
852 /* Remember this value */
853 itv->osd_info->pan_cur = osd_pan_index;
854 return 0;
855}
856
857static int ivtvfb_set_par(struct fb_info *info)
858{
859 int rc = 0;
860 struct ivtv *itv = (struct ivtv *) info->par;
861
862 IVTVFB_DEBUG_INFO("ivtvfb_set_par\n");
863
864 rc = ivtvfb_set_var(itv, &info->var);
865 ivtvfb_pan_display(&info->var, info);
866 ivtvfb_get_fix(itv, &info->fix);
867 ivtv_firmware_check(itv, "ivtvfb_set_par");
868 return rc;
869}
870
871static int ivtvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
872 unsigned blue, unsigned transp,
873 struct fb_info *info)
874{
875 u32 color, *palette;
876 struct ivtv *itv = (struct ivtv *)info->par;
877
878 if (regno >= info->cmap.len)
879 return -EINVAL;
880
881 color = ((transp & 0xFF00) << 16) |((red & 0xFF00) << 8) | (green & 0xFF00) | ((blue & 0xFF00) >> 8);
882 if (info->var.bits_per_pixel <= 8) {
883 write_reg(regno, 0x02a30);
884 write_reg(color, 0x02a34);
885 itv->osd_info->palette_cur[regno] = color;
886 return 0;
887 }
888 if (regno >= 16)
889 return -EINVAL;
890
891 palette = info->pseudo_palette;
892 if (info->var.bits_per_pixel == 16) {
893 switch (info->var.green.length) {
894 case 4:
895 color = ((red & 0xf000) >> 4) |
896 ((green & 0xf000) >> 8) |
897 ((blue & 0xf000) >> 12);
898 break;
899 case 5:
900 color = ((red & 0xf800) >> 1) |
901 ((green & 0xf800) >> 6) |
902 ((blue & 0xf800) >> 11);
903 break;
904 case 6:
905 color = (red & 0xf800 ) |
906 ((green & 0xfc00) >> 5) |
907 ((blue & 0xf800) >> 11);
908 break;
909 }
910 }
911 palette[regno] = color;
912 return 0;
913}
914
915/* We don't really support blanking. All this does is enable or
916 disable the OSD. */
917static int ivtvfb_blank(int blank_mode, struct fb_info *info)
918{
919 struct ivtv *itv = (struct ivtv *)info->par;
920
921 IVTVFB_DEBUG_INFO("Set blanking mode : %d\n", blank_mode);
922 switch (blank_mode) {
923 case FB_BLANK_UNBLANK:
924 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, 1);
925 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);
926 break;
927 case FB_BLANK_NORMAL:
928 case FB_BLANK_HSYNC_SUSPEND:
929 case FB_BLANK_VSYNC_SUSPEND:
930 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, 0);
931 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);
932 break;
933 case FB_BLANK_POWERDOWN:
934 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
935 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, 0);
936 break;
937 }
938 itv->osd_info->blank_cur = blank_mode;
939 return 0;
940}
941
942static struct fb_ops ivtvfb_ops = {
943 .owner = THIS_MODULE,
944 .fb_write = ivtvfb_write,
945 .fb_check_var = ivtvfb_check_var,
946 .fb_set_par = ivtvfb_set_par,
947 .fb_setcolreg = ivtvfb_setcolreg,
948 .fb_fillrect = cfb_fillrect,
949 .fb_copyarea = cfb_copyarea,
950 .fb_imageblit = cfb_imageblit,
951 .fb_cursor = NULL,
952 .fb_ioctl = ivtvfb_ioctl,
953 .fb_pan_display = ivtvfb_pan_display,
954 .fb_blank = ivtvfb_blank,
955};
956
957/* Restore hardware after firmware restart */
958static void ivtvfb_restore(struct ivtv *itv)
959{
960 struct osd_info *oi = itv->osd_info;
961 int i;
962
963 ivtvfb_set_var(itv, &oi->fbvar_cur);
964 ivtvfb_blank(oi->blank_cur, &oi->ivtvfb_info);
965 for (i = 0; i < 256; i++) {
966 write_reg(i, 0x02a30);
967 write_reg(oi->palette_cur[i], 0x02a34);
968 }
969 write_reg(oi->pan_cur, 0x02a0c);
970}
971
972/* Initialization */
973
974
975/* Setup our initial video mode */
976static int ivtvfb_init_vidmode(struct ivtv *itv)
977{
978 struct osd_info *oi = itv->osd_info;
979 struct v4l2_rect start_window;
980 int max_height;
981
982 /* Color mode */
983
984 if (osd_depth != 8 && osd_depth != 16 && osd_depth != 32)
985 osd_depth = 8;
986 oi->bits_per_pixel = osd_depth;
987 oi->bytes_per_pixel = oi->bits_per_pixel / 8;
988
989 /* Horizontal size & position */
990
991 if (osd_xres > 720)
992 osd_xres = 720;
993
994 /* Must be a multiple of 4 for 8bpp & 2 for 16bpp */
995 if (osd_depth == 8)
996 osd_xres &= ~3;
997 else if (osd_depth == 16)
998 osd_xres &= ~1;
999
1000 start_window.width = osd_xres ? osd_xres : 640;
1001
1002 /* Check horizontal start (osd_left). */
1003 if (osd_left && osd_left + start_window.width > 721) {
1004 IVTVFB_ERR("Invalid osd_left - assuming default\n");
1005 osd_left = 0;
1006 }
1007
1008 /* Hardware coords start at 0, user coords start at 1. */
1009 osd_left--;
1010
1011 start_window.left = osd_left >= 0 ?
1012 osd_left : ((IVTV_OSD_MAX_WIDTH - start_window.width) / 2);
1013
1014 oi->display_byte_stride =
1015 start_window.width * oi->bytes_per_pixel;
1016
1017 /* Vertical size & position */
1018
1019 max_height = itv->is_out_50hz ? 576 : 480;
1020
1021 if (osd_yres > max_height)
1022 osd_yres = max_height;
1023
1024 start_window.height = osd_yres ?
1025 osd_yres : itv->is_out_50hz ? 480 : 400;
1026
1027 /* Check vertical start (osd_upper). */
1028 if (osd_upper + start_window.height > max_height + 1) {
1029 IVTVFB_ERR("Invalid osd_upper - assuming default\n");
1030 osd_upper = 0;
1031 }
1032
1033 /* Hardware coords start at 0, user coords start at 1. */
1034 osd_upper--;
1035
1036 start_window.top = osd_upper >= 0 ? osd_upper : ((max_height - start_window.height) / 2);
1037
1038 oi->display_width = start_window.width;
1039 oi->display_height = start_window.height;
1040
1041 /* Generate a valid fb_var_screeninfo */
1042
1043 oi->ivtvfb_defined.xres = oi->display_width;
1044 oi->ivtvfb_defined.yres = oi->display_height;
1045 oi->ivtvfb_defined.xres_virtual = oi->display_width;
1046 oi->ivtvfb_defined.yres_virtual = oi->display_height;
1047 oi->ivtvfb_defined.bits_per_pixel = oi->bits_per_pixel;
1048 oi->ivtvfb_defined.vmode = (osd_laced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED);
1049 oi->ivtvfb_defined.left_margin = start_window.left + 1;
1050 oi->ivtvfb_defined.upper_margin = start_window.top + 1;
1051 oi->ivtvfb_defined.accel_flags = FB_ACCEL_NONE;
1052 oi->ivtvfb_defined.nonstd = 0;
1053
1054 /* We've filled in the most data, let the usual mode check
1055 routine fill in the rest. */
1056 _ivtvfb_check_var(&oi->ivtvfb_defined, itv);
1057
1058 /* Generate valid fb_fix_screeninfo */
1059
1060 ivtvfb_get_fix(itv, &oi->ivtvfb_fix);
1061
1062 /* Generate valid fb_info */
1063
1064 oi->ivtvfb_info.node = -1;
1065 oi->ivtvfb_info.flags = FBINFO_FLAG_DEFAULT;
1066 oi->ivtvfb_info.fbops = &ivtvfb_ops;
1067 oi->ivtvfb_info.par = itv;
1068 oi->ivtvfb_info.var = oi->ivtvfb_defined;
1069 oi->ivtvfb_info.fix = oi->ivtvfb_fix;
1070 oi->ivtvfb_info.screen_base = (u8 __iomem *)oi->video_vbase;
1071 oi->ivtvfb_info.fbops = &ivtvfb_ops;
1072
1073 /* Supply some monitor specs. Bogus values will do for now */
1074 oi->ivtvfb_info.monspecs.hfmin = 8000;
1075 oi->ivtvfb_info.monspecs.hfmax = 70000;
1076 oi->ivtvfb_info.monspecs.vfmin = 10;
1077 oi->ivtvfb_info.monspecs.vfmax = 100;
1078
1079 /* Allocate color map */
1080 if (fb_alloc_cmap(&oi->ivtvfb_info.cmap, 256, 1)) {
1081 IVTVFB_ERR("abort, unable to alloc cmap\n");
1082 return -ENOMEM;
1083 }
1084
1085 /* Allocate the pseudo palette */
1086 oi->ivtvfb_info.pseudo_palette =
1087 kmalloc(sizeof(u32) * 16, GFP_KERNEL|__GFP_NOWARN);
1088
1089 if (!oi->ivtvfb_info.pseudo_palette) {
1090 IVTVFB_ERR("abort, unable to alloc pseudo palette\n");
1091 return -ENOMEM;
1092 }
1093
1094 return 0;
1095}
1096
1097/* Find OSD buffer base & size. Add to mtrr. Zero osd buffer. */
1098
1099static int ivtvfb_init_io(struct ivtv *itv)
1100{
1101 struct osd_info *oi = itv->osd_info;
1102
1103 mutex_lock(&itv->serialize_lock);
1104 if (ivtv_init_on_first_open(itv)) {
1105 mutex_unlock(&itv->serialize_lock);
1106 IVTVFB_ERR("Failed to initialize ivtv\n");
1107 return -ENXIO;
1108 }
1109 mutex_unlock(&itv->serialize_lock);
1110
1111 if (ivtvfb_get_framebuffer(itv, &oi->video_rbase,
1112 &oi->video_buffer_size) < 0) {
1113 IVTVFB_ERR("Firmware failed to respond\n");
1114 return -EIO;
1115 }
1116
1117 /* The osd buffer size depends on the number of video buffers allocated
1118 on the PVR350 itself. For now we'll hardcode the smallest osd buffer
1119 size to prevent any overlap. */
1120 oi->video_buffer_size = 1704960;
1121
1122 oi->video_pbase = itv->base_addr + IVTV_DECODER_OFFSET + oi->video_rbase;
1123 oi->video_vbase = itv->dec_mem + oi->video_rbase;
1124
1125 if (!oi->video_vbase) {
1126 IVTVFB_ERR("abort, video memory 0x%x @ 0x%lx isn't mapped!\n",
1127 oi->video_buffer_size, oi->video_pbase);
1128 return -EIO;
1129 }
1130
1131 IVTVFB_INFO("Framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
1132 oi->video_pbase, oi->video_vbase,
1133 oi->video_buffer_size / 1024);
1134
1135#ifdef CONFIG_MTRR
1136 {
1137 /* Find the largest power of two that maps the whole buffer */
1138 int size_shift = 31;
1139
1140 while (!(oi->video_buffer_size & (1 << size_shift))) {
1141 size_shift--;
1142 }
1143 size_shift++;
1144 oi->fb_start_aligned_physaddr = oi->video_pbase & ~((1 << size_shift) - 1);
1145 oi->fb_end_aligned_physaddr = oi->video_pbase + oi->video_buffer_size;
1146 oi->fb_end_aligned_physaddr += (1 << size_shift) - 1;
1147 oi->fb_end_aligned_physaddr &= ~((1 << size_shift) - 1);
1148 if (mtrr_add(oi->fb_start_aligned_physaddr,
1149 oi->fb_end_aligned_physaddr - oi->fb_start_aligned_physaddr,
1150 MTRR_TYPE_WRCOMB, 1) < 0) {
1151 IVTVFB_INFO("disabled mttr\n");
1152 oi->fb_start_aligned_physaddr = 0;
1153 oi->fb_end_aligned_physaddr = 0;
1154 }
1155 }
1156#endif
1157
1158 /* Blank the entire osd. */
1159 memset_io(oi->video_vbase, 0, oi->video_buffer_size);
1160
1161 return 0;
1162}
1163
1164/* Release any memory we've grabbed & remove mtrr entry */
1165static void ivtvfb_release_buffers (struct ivtv *itv)
1166{
1167 struct osd_info *oi = itv->osd_info;
1168
1169 /* Release cmap */
1170 if (oi->ivtvfb_info.cmap.len)
1171 fb_dealloc_cmap(&oi->ivtvfb_info.cmap);
1172
1173 /* Release pseudo palette */
1174 if (oi->ivtvfb_info.pseudo_palette)
1175 kfree(oi->ivtvfb_info.pseudo_palette);
1176
1177#ifdef CONFIG_MTRR
1178 if (oi->fb_end_aligned_physaddr) {
1179 mtrr_del(-1, oi->fb_start_aligned_physaddr,
1180 oi->fb_end_aligned_physaddr - oi->fb_start_aligned_physaddr);
1181 }
1182#endif
1183
1184 kfree(oi);
1185 itv->osd_info = NULL;
1186}
1187
1188/* Initialize the specified card */
1189
1190static int ivtvfb_init_card(struct ivtv *itv)
1191{
1192 int rc;
1193
1194 if (itv->osd_info) {
1195 IVTVFB_ERR("Card %d already initialised\n", ivtvfb_card_id);
1196 return -EBUSY;
1197 }
1198
1199 itv->osd_info = kzalloc(sizeof(struct osd_info),
1200 GFP_ATOMIC|__GFP_NOWARN);
1201 if (itv->osd_info == NULL) {
1202 IVTVFB_ERR("Failed to allocate memory for osd_info\n");
1203 return -ENOMEM;
1204 }
1205
1206 /* Find & setup the OSD buffer */
1207 rc = ivtvfb_init_io(itv);
1208 if (rc) {
1209 ivtvfb_release_buffers(itv);
1210 return rc;
1211 }
1212
1213 /* Set the startup video mode information */
1214 if ((rc = ivtvfb_init_vidmode(itv))) {
1215 ivtvfb_release_buffers(itv);
1216 return rc;
1217 }
1218
1219 /* Register the framebuffer */
1220 if (register_framebuffer(&itv->osd_info->ivtvfb_info) < 0) {
1221 ivtvfb_release_buffers(itv);
1222 return -EINVAL;
1223 }
1224
1225 itv->osd_video_pbase = itv->osd_info->video_pbase;
1226
1227 /* Set the card to the requested mode */
1228 ivtvfb_set_par(&itv->osd_info->ivtvfb_info);
1229
1230 /* Set color 0 to black */
1231 write_reg(0, 0x02a30);
1232 write_reg(0, 0x02a34);
1233
1234 /* Enable the osd */
1235 ivtvfb_blank(FB_BLANK_UNBLANK, &itv->osd_info->ivtvfb_info);
1236
1237 /* Enable restart */
1238 itv->ivtvfb_restore = ivtvfb_restore;
1239
1240 /* Allocate DMA */
1241 ivtv_udma_alloc(itv);
1242 return 0;
1243
1244}
1245
1246static int __init ivtvfb_callback_init(struct device *dev, void *p)
1247{
1248 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
1249 struct ivtv *itv = container_of(v4l2_dev, struct ivtv, v4l2_dev);
1250
1251 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1252 if (ivtvfb_init_card(itv) == 0) {
1253 IVTVFB_INFO("Framebuffer registered on %s\n",
1254 itv->v4l2_dev.name);
1255 (*(int *)p)++;
1256 }
1257 }
1258 return 0;
1259}
1260
1261static int ivtvfb_callback_cleanup(struct device *dev, void *p)
1262{
1263 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
1264 struct ivtv *itv = container_of(v4l2_dev, struct ivtv, v4l2_dev);
1265 struct osd_info *oi = itv->osd_info;
1266
1267 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1268 if (unregister_framebuffer(&itv->osd_info->ivtvfb_info)) {
1269 IVTVFB_WARN("Framebuffer %d is in use, cannot unload\n",
1270 itv->instance);
1271 return 0;
1272 }
1273 IVTVFB_INFO("Unregister framebuffer %d\n", itv->instance);
1274 itv->ivtvfb_restore = NULL;
1275 ivtvfb_blank(FB_BLANK_VSYNC_SUSPEND, &oi->ivtvfb_info);
1276 ivtvfb_release_buffers(itv);
1277 itv->osd_video_pbase = 0;
1278 }
1279 return 0;
1280}
1281
1282static int __init ivtvfb_init(void)
1283{
1284 struct device_driver *drv;
1285 int registered = 0;
1286 int err;
1287
1288 if (ivtvfb_card_id < -1 || ivtvfb_card_id >= IVTV_MAX_CARDS) {
1289 printk(KERN_ERR "ivtvfb: ivtvfb_card_id parameter is out of range (valid range: -1 - %d)\n",
1290 IVTV_MAX_CARDS - 1);
1291 return -EINVAL;
1292 }
1293
1294 drv = driver_find("ivtv", &pci_bus_type);
1295 err = driver_for_each_device(drv, NULL, &registered, ivtvfb_callback_init);
1296 (void)err; /* suppress compiler warning */
1297 if (!registered) {
1298 printk(KERN_ERR "ivtvfb: no cards found\n");
1299 return -ENODEV;
1300 }
1301 return 0;
1302}
1303
1304static void ivtvfb_cleanup(void)
1305{
1306 struct device_driver *drv;
1307 int err;
1308
1309 printk(KERN_INFO "ivtvfb: Unloading framebuffer module\n");
1310
1311 drv = driver_find("ivtv", &pci_bus_type);
1312 err = driver_for_each_device(drv, NULL, NULL, ivtvfb_callback_cleanup);
1313 (void)err; /* suppress compiler warning */
1314}
1315
1316module_init(ivtvfb_init);
1317module_exit(ivtvfb_cleanup);
diff --git a/drivers/media/pci/saa7134/Kconfig b/drivers/media/pci/saa7134/Kconfig
new file mode 100644
index 000000000000..39fc0187a747
--- /dev/null
+++ b/drivers/media/pci/saa7134/Kconfig
@@ -0,0 +1,64 @@
1config VIDEO_SAA7134
2 tristate "Philips SAA7134 support"
3 depends on VIDEO_DEV && PCI && I2C
4 select VIDEOBUF_DMA_SG
5 select VIDEO_TUNER
6 select VIDEO_TVEEPROM
7 select CRC32
8 select VIDEO_SAA6588 if VIDEO_HELPER_CHIPS_AUTO
9 ---help---
10 This is a video4linux driver for Philips SAA713x based
11 TV cards.
12
13 To compile this driver as a module, choose M here: the
14 module will be called saa7134.
15
16config VIDEO_SAA7134_ALSA
17 tristate "Philips SAA7134 DMA audio support"
18 depends on VIDEO_SAA7134 && SND
19 select SND_PCM
20 ---help---
21 This is a video4linux driver for direct (DMA) audio in
22 Philips SAA713x based TV cards using ALSA
23
24 To compile this driver as a module, choose M here: the
25 module will be called saa7134-alsa.
26
27config VIDEO_SAA7134_RC
28 bool "Philips SAA7134 Remote Controller support"
29 depends on RC_CORE
30 depends on VIDEO_SAA7134
31 depends on !(RC_CORE=m && VIDEO_SAA7134=y)
32 default y
33 ---help---
34 Enables Remote Controller support on saa7134 driver.
35
36config VIDEO_SAA7134_DVB
37 tristate "DVB/ATSC Support for saa7134 based TV cards"
38 depends on VIDEO_SAA7134 && DVB_CORE
39 select VIDEOBUF_DVB
40 select DVB_PLL if !DVB_FE_CUSTOMISE
41 select DVB_MT352 if !DVB_FE_CUSTOMISE
42 select DVB_TDA1004X if !DVB_FE_CUSTOMISE
43 select DVB_NXT200X if !DVB_FE_CUSTOMISE
44 select DVB_TDA10086 if !DVB_FE_CUSTOMISE
45 select DVB_TDA826X if !DVB_FE_CUSTOMISE
46 select DVB_ISL6421 if !DVB_FE_CUSTOMISE
47 select DVB_ISL6405 if !DVB_FE_CUSTOMISE
48 select MEDIA_TUNER_TDA827X if !MEDIA_TUNER_CUSTOMISE
49 select MEDIA_TUNER_SIMPLE if !MEDIA_TUNER_CUSTOMISE
50 select DVB_ZL10036 if !DVB_FE_CUSTOMISE
51 select DVB_MT312 if !DVB_FE_CUSTOMISE
52 select DVB_LNBP21 if !DVB_FE_CUSTOMISE
53 select DVB_ZL10353 if !DVB_FE_CUSTOMISE
54 select DVB_LGDT3305 if !DVB_FE_CUSTOMISE
55 select DVB_TDA10048 if !DVB_FE_CUSTOMISE
56 select MEDIA_TUNER_TDA18271 if !MEDIA_TUNER_CUSTOMISE
57 select MEDIA_TUNER_TDA8290 if !MEDIA_TUNER_CUSTOMISE
58 select DVB_ZL10039 if !DVB_FE_CUSTOMISE
59 ---help---
60 This adds support for DVB cards based on the
61 Philips saa7134 chip.
62
63 To compile this driver as a module, choose M here: the
64 module will be called saa7134-dvb.
diff --git a/drivers/media/pci/saa7134/Makefile b/drivers/media/pci/saa7134/Makefile
new file mode 100644
index 000000000000..aba50088dcdc
--- /dev/null
+++ b/drivers/media/pci/saa7134/Makefile
@@ -0,0 +1,16 @@
1
2saa7134-y := saa7134-cards.o saa7134-core.o saa7134-i2c.o
3saa7134-y += saa7134-ts.o saa7134-tvaudio.o saa7134-vbi.o
4saa7134-y += saa7134-video.o
5saa7134-$(CONFIG_VIDEO_SAA7134_RC) += saa7134-input.o
6
7obj-$(CONFIG_VIDEO_SAA7134) += saa6752hs.o saa7134.o saa7134-empress.o
8
9obj-$(CONFIG_VIDEO_SAA7134_ALSA) += saa7134-alsa.o
10
11obj-$(CONFIG_VIDEO_SAA7134_DVB) += saa7134-dvb.o
12
13ccflags-y += -I$(srctree)/drivers/media/video
14ccflags-y += -I$(srctree)/drivers/media/tuners
15ccflags-y += -I$(srctree)/drivers/media/dvb-core
16ccflags-y += -I$(srctree)/drivers/media/dvb-frontends
diff --git a/drivers/media/pci/saa7134/saa6752hs.c b/drivers/media/pci/saa7134/saa6752hs.c
new file mode 100644
index 000000000000..f147b05bd860
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa6752hs.c
@@ -0,0 +1,1012 @@
1 /*
2 saa6752hs - i2c-driver for the saa6752hs by Philips
3
4 Copyright (C) 2004 Andrew de Quincey
5
6 AC-3 support:
7
8 Copyright (C) 2008 Hans Verkuil <hverkuil@xs4all.nl>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License vs published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mvss Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/string.h>
28#include <linux/timer.h>
29#include <linux/delay.h>
30#include <linux/errno.h>
31#include <linux/slab.h>
32#include <linux/poll.h>
33#include <linux/i2c.h>
34#include <linux/types.h>
35#include <linux/videodev2.h>
36#include <media/v4l2-device.h>
37#include <media/v4l2-common.h>
38#include <media/v4l2-chip-ident.h>
39#include <linux/init.h>
40#include <linux/crc32.h>
41
42#define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
43#define MPEG_VIDEO_MAX_BITRATE_MAX 27000
44#define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
45#define MPEG_PID_MAX ((1 << 14) - 1)
46
47
48MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
49MODULE_AUTHOR("Andrew de Quincey");
50MODULE_LICENSE("GPL");
51
52enum saa6752hs_videoformat {
53 SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
54 SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
55 SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
56 SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
57 SAA6752HS_VF_UNKNOWN,
58};
59
60struct saa6752hs_mpeg_params {
61 /* transport streams */
62 __u16 ts_pid_pmt;
63 __u16 ts_pid_audio;
64 __u16 ts_pid_video;
65 __u16 ts_pid_pcr;
66
67 /* audio */
68 enum v4l2_mpeg_audio_encoding au_encoding;
69 enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
70 enum v4l2_mpeg_audio_ac3_bitrate au_ac3_bitrate;
71
72 /* video */
73 enum v4l2_mpeg_video_aspect vi_aspect;
74 enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
75 __u32 vi_bitrate;
76 __u32 vi_bitrate_peak;
77};
78
79static const struct v4l2_format v4l2_format_table[] =
80{
81 [SAA6752HS_VF_D1] =
82 { .fmt = { .pix = { .width = 720, .height = 576 }}},
83 [SAA6752HS_VF_2_3_D1] =
84 { .fmt = { .pix = { .width = 480, .height = 576 }}},
85 [SAA6752HS_VF_1_2_D1] =
86 { .fmt = { .pix = { .width = 352, .height = 576 }}},
87 [SAA6752HS_VF_SIF] =
88 { .fmt = { .pix = { .width = 352, .height = 288 }}},
89 [SAA6752HS_VF_UNKNOWN] =
90 { .fmt = { .pix = { .width = 0, .height = 0}}},
91};
92
93struct saa6752hs_state {
94 struct v4l2_subdev sd;
95 int chip;
96 u32 revision;
97 int has_ac3;
98 struct saa6752hs_mpeg_params params;
99 enum saa6752hs_videoformat video_format;
100 v4l2_std_id standard;
101};
102
103enum saa6752hs_command {
104 SAA6752HS_COMMAND_RESET = 0,
105 SAA6752HS_COMMAND_STOP = 1,
106 SAA6752HS_COMMAND_START = 2,
107 SAA6752HS_COMMAND_PAUSE = 3,
108 SAA6752HS_COMMAND_RECONFIGURE = 4,
109 SAA6752HS_COMMAND_SLEEP = 5,
110 SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
111
112 SAA6752HS_COMMAND_MAX
113};
114
115static inline struct saa6752hs_state *to_state(struct v4l2_subdev *sd)
116{
117 return container_of(sd, struct saa6752hs_state, sd);
118}
119
120/* ---------------------------------------------------------------------- */
121
122static u8 PAT[] = {
123 0xc2, /* i2c register */
124 0x00, /* table number for encoder */
125
126 0x47, /* sync */
127 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
128 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
129
130 0x00, /* PSI pointer to start of table */
131
132 0x00, /* tid(0) */
133 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
134
135 0x00, 0x01, /* transport_stream_id(1) */
136
137 0xc1, /* version_number(0), current_next_indicator(1) */
138
139 0x00, 0x00, /* section_number(0), last_section_number(0) */
140
141 0x00, 0x01, /* program_number(1) */
142
143 0xe0, 0x00, /* PMT PID */
144
145 0x00, 0x00, 0x00, 0x00 /* CRC32 */
146};
147
148static u8 PMT[] = {
149 0xc2, /* i2c register */
150 0x01, /* table number for encoder */
151
152 0x47, /* sync */
153 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
154 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
155
156 0x00, /* PSI pointer to start of table */
157
158 0x02, /* tid(2) */
159 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
160
161 0x00, 0x01, /* program_number(1) */
162
163 0xc1, /* version_number(0), current_next_indicator(1) */
164
165 0x00, 0x00, /* section_number(0), last_section_number(0) */
166
167 0xe0, 0x00, /* PCR_PID */
168
169 0xf0, 0x00, /* program_info_length(0) */
170
171 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
172 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
173
174 0x00, 0x00, 0x00, 0x00 /* CRC32 */
175};
176
177static u8 PMT_AC3[] = {
178 0xc2, /* i2c register */
179 0x01, /* table number for encoder(1) */
180 0x47, /* sync */
181
182 0x40, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0) */
183 0x10, /* PMT PID (0x0010) */
184 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
185
186 0x00, /* PSI pointer to start of table */
187
188 0x02, /* TID (2) */
189 0xb0, 0x1a, /* section_syntax_indicator(1), section_length(26) */
190
191 0x00, 0x01, /* program_number(1) */
192
193 0xc1, /* version_number(0), current_next_indicator(1) */
194
195 0x00, 0x00, /* section_number(0), last_section_number(0) */
196
197 0xe1, 0x04, /* PCR_PID (0x0104) */
198
199 0xf0, 0x00, /* program_info_length(0) */
200
201 0x02, 0xe1, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
202 0x06, 0xe1, 0x03, 0xf0, 0x03, /* audio stream type(6), pid */
203 0x6a, /* AC3 */
204 0x01, /* Descriptor_length(1) */
205 0x00, /* component_type_flag(0), bsid_flag(0), mainid_flag(0), asvc_flag(0), reserved flags(0) */
206
207 0xED, 0xDE, 0x2D, 0xF3 /* CRC32 BE */
208};
209
210static struct saa6752hs_mpeg_params param_defaults =
211{
212 .ts_pid_pmt = 16,
213 .ts_pid_video = 260,
214 .ts_pid_audio = 256,
215 .ts_pid_pcr = 259,
216
217 .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
218 .vi_bitrate = 4000,
219 .vi_bitrate_peak = 6000,
220 .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
221
222 .au_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
223 .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
224 .au_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
225};
226
227/* ---------------------------------------------------------------------- */
228
229static int saa6752hs_chip_command(struct i2c_client *client,
230 enum saa6752hs_command command)
231{
232 unsigned char buf[3];
233 unsigned long timeout;
234 int status = 0;
235
236 /* execute the command */
237 switch(command) {
238 case SAA6752HS_COMMAND_RESET:
239 buf[0] = 0x00;
240 break;
241
242 case SAA6752HS_COMMAND_STOP:
243 buf[0] = 0x03;
244 break;
245
246 case SAA6752HS_COMMAND_START:
247 buf[0] = 0x02;
248 break;
249
250 case SAA6752HS_COMMAND_PAUSE:
251 buf[0] = 0x04;
252 break;
253
254 case SAA6752HS_COMMAND_RECONFIGURE:
255 buf[0] = 0x05;
256 break;
257
258 case SAA6752HS_COMMAND_SLEEP:
259 buf[0] = 0x06;
260 break;
261
262 case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
263 buf[0] = 0x07;
264 break;
265
266 default:
267 return -EINVAL;
268 }
269
270 /* set it and wait for it to be so */
271 i2c_master_send(client, buf, 1);
272 timeout = jiffies + HZ * 3;
273 for (;;) {
274 /* get the current status */
275 buf[0] = 0x10;
276 i2c_master_send(client, buf, 1);
277 i2c_master_recv(client, buf, 1);
278
279 if (!(buf[0] & 0x20))
280 break;
281 if (time_after(jiffies,timeout)) {
282 status = -ETIMEDOUT;
283 break;
284 }
285
286 msleep(10);
287 }
288
289 /* delay a bit to let encoder settle */
290 msleep(50);
291
292 return status;
293}
294
295
296static inline void set_reg8(struct i2c_client *client, uint8_t reg, uint8_t val)
297{
298 u8 buf[2];
299
300 buf[0] = reg;
301 buf[1] = val;
302 i2c_master_send(client, buf, 2);
303}
304
305static inline void set_reg16(struct i2c_client *client, uint8_t reg, uint16_t val)
306{
307 u8 buf[3];
308
309 buf[0] = reg;
310 buf[1] = val >> 8;
311 buf[2] = val & 0xff;
312 i2c_master_send(client, buf, 3);
313}
314
315static int saa6752hs_set_bitrate(struct i2c_client *client,
316 struct saa6752hs_state *h)
317{
318 struct saa6752hs_mpeg_params *params = &h->params;
319 int tot_bitrate;
320 int is_384k;
321
322 /* set the bitrate mode */
323 set_reg8(client, 0x71,
324 params->vi_bitrate_mode != V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
325
326 /* set the video bitrate */
327 if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
328 /* set the target bitrate */
329 set_reg16(client, 0x80, params->vi_bitrate);
330
331 /* set the max bitrate */
332 set_reg16(client, 0x81, params->vi_bitrate_peak);
333 tot_bitrate = params->vi_bitrate_peak;
334 } else {
335 /* set the target bitrate (no max bitrate for CBR) */
336 set_reg16(client, 0x81, params->vi_bitrate);
337 tot_bitrate = params->vi_bitrate;
338 }
339
340 /* set the audio encoding */
341 set_reg8(client, 0x93,
342 params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3);
343
344 /* set the audio bitrate */
345 if (params->au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3)
346 is_384k = V4L2_MPEG_AUDIO_AC3_BITRATE_384K == params->au_ac3_bitrate;
347 else
348 is_384k = V4L2_MPEG_AUDIO_L2_BITRATE_384K == params->au_l2_bitrate;
349 set_reg8(client, 0x94, is_384k);
350 tot_bitrate += is_384k ? 384 : 256;
351
352 /* Note: the total max bitrate is determined by adding the video and audio
353 bitrates together and also adding an extra 768kbit/s to stay on the
354 safe side. If more control should be required, then an extra MPEG control
355 should be added. */
356 tot_bitrate += 768;
357 if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
358 tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
359
360 /* set the total bitrate */
361 set_reg16(client, 0xb1, tot_bitrate);
362 return 0;
363}
364
365
366static int get_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
367 struct v4l2_ext_control *ctrl)
368{
369 switch (ctrl->id) {
370 case V4L2_CID_MPEG_STREAM_TYPE:
371 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
372 break;
373 case V4L2_CID_MPEG_STREAM_PID_PMT:
374 ctrl->value = params->ts_pid_pmt;
375 break;
376 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
377 ctrl->value = params->ts_pid_audio;
378 break;
379 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
380 ctrl->value = params->ts_pid_video;
381 break;
382 case V4L2_CID_MPEG_STREAM_PID_PCR:
383 ctrl->value = params->ts_pid_pcr;
384 break;
385 case V4L2_CID_MPEG_AUDIO_ENCODING:
386 ctrl->value = params->au_encoding;
387 break;
388 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
389 ctrl->value = params->au_l2_bitrate;
390 break;
391 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
392 if (!has_ac3)
393 return -EINVAL;
394 ctrl->value = params->au_ac3_bitrate;
395 break;
396 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
397 ctrl->value = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
398 break;
399 case V4L2_CID_MPEG_VIDEO_ENCODING:
400 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
401 break;
402 case V4L2_CID_MPEG_VIDEO_ASPECT:
403 ctrl->value = params->vi_aspect;
404 break;
405 case V4L2_CID_MPEG_VIDEO_BITRATE:
406 ctrl->value = params->vi_bitrate * 1000;
407 break;
408 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
409 ctrl->value = params->vi_bitrate_peak * 1000;
410 break;
411 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
412 ctrl->value = params->vi_bitrate_mode;
413 break;
414 default:
415 return -EINVAL;
416 }
417 return 0;
418}
419
420static int handle_ctrl(int has_ac3, struct saa6752hs_mpeg_params *params,
421 struct v4l2_ext_control *ctrl, int set)
422{
423 int old = 0, new;
424
425 new = ctrl->value;
426 switch (ctrl->id) {
427 case V4L2_CID_MPEG_STREAM_TYPE:
428 old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
429 if (set && new != old)
430 return -ERANGE;
431 new = old;
432 break;
433 case V4L2_CID_MPEG_STREAM_PID_PMT:
434 old = params->ts_pid_pmt;
435 if (set && new > MPEG_PID_MAX)
436 return -ERANGE;
437 if (new > MPEG_PID_MAX)
438 new = MPEG_PID_MAX;
439 params->ts_pid_pmt = new;
440 break;
441 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
442 old = params->ts_pid_audio;
443 if (set && new > MPEG_PID_MAX)
444 return -ERANGE;
445 if (new > MPEG_PID_MAX)
446 new = MPEG_PID_MAX;
447 params->ts_pid_audio = new;
448 break;
449 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
450 old = params->ts_pid_video;
451 if (set && new > MPEG_PID_MAX)
452 return -ERANGE;
453 if (new > MPEG_PID_MAX)
454 new = MPEG_PID_MAX;
455 params->ts_pid_video = new;
456 break;
457 case V4L2_CID_MPEG_STREAM_PID_PCR:
458 old = params->ts_pid_pcr;
459 if (set && new > MPEG_PID_MAX)
460 return -ERANGE;
461 if (new > MPEG_PID_MAX)
462 new = MPEG_PID_MAX;
463 params->ts_pid_pcr = new;
464 break;
465 case V4L2_CID_MPEG_AUDIO_ENCODING:
466 old = params->au_encoding;
467 if (set && new != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 &&
468 (!has_ac3 || new != V4L2_MPEG_AUDIO_ENCODING_AC3))
469 return -ERANGE;
470 params->au_encoding = new;
471 break;
472 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
473 old = params->au_l2_bitrate;
474 if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
475 new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
476 return -ERANGE;
477 if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
478 new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
479 else
480 new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
481 params->au_l2_bitrate = new;
482 break;
483 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
484 if (!has_ac3)
485 return -EINVAL;
486 old = params->au_ac3_bitrate;
487 if (set && new != V4L2_MPEG_AUDIO_AC3_BITRATE_256K &&
488 new != V4L2_MPEG_AUDIO_AC3_BITRATE_384K)
489 return -ERANGE;
490 if (new <= V4L2_MPEG_AUDIO_AC3_BITRATE_256K)
491 new = V4L2_MPEG_AUDIO_AC3_BITRATE_256K;
492 else
493 new = V4L2_MPEG_AUDIO_AC3_BITRATE_384K;
494 params->au_ac3_bitrate = new;
495 break;
496 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
497 old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
498 if (set && new != old)
499 return -ERANGE;
500 new = old;
501 break;
502 case V4L2_CID_MPEG_VIDEO_ENCODING:
503 old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
504 if (set && new != old)
505 return -ERANGE;
506 new = old;
507 break;
508 case V4L2_CID_MPEG_VIDEO_ASPECT:
509 old = params->vi_aspect;
510 if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
511 new != V4L2_MPEG_VIDEO_ASPECT_4x3)
512 return -ERANGE;
513 if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
514 new = V4L2_MPEG_VIDEO_ASPECT_4x3;
515 params->vi_aspect = new;
516 break;
517 case V4L2_CID_MPEG_VIDEO_BITRATE:
518 old = params->vi_bitrate * 1000;
519 new = 1000 * (new / 1000);
520 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
521 return -ERANGE;
522 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
523 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
524 params->vi_bitrate = new / 1000;
525 break;
526 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
527 old = params->vi_bitrate_peak * 1000;
528 new = 1000 * (new / 1000);
529 if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
530 return -ERANGE;
531 if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
532 new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
533 params->vi_bitrate_peak = new / 1000;
534 break;
535 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
536 old = params->vi_bitrate_mode;
537 params->vi_bitrate_mode = new;
538 break;
539 default:
540 return -EINVAL;
541 }
542 ctrl->value = new;
543 return 0;
544}
545
546
547static int saa6752hs_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
548{
549 struct saa6752hs_state *h = to_state(sd);
550 struct saa6752hs_mpeg_params *params = &h->params;
551 int err;
552
553 switch (qctrl->id) {
554 case V4L2_CID_MPEG_AUDIO_ENCODING:
555 return v4l2_ctrl_query_fill(qctrl,
556 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
557 h->has_ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 :
558 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
559 1, V4L2_MPEG_AUDIO_ENCODING_LAYER_2);
560
561 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
562 return v4l2_ctrl_query_fill(qctrl,
563 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
564 V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
565 V4L2_MPEG_AUDIO_L2_BITRATE_256K);
566
567 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
568 if (!h->has_ac3)
569 return -EINVAL;
570 return v4l2_ctrl_query_fill(qctrl,
571 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
572 V4L2_MPEG_AUDIO_AC3_BITRATE_384K, 1,
573 V4L2_MPEG_AUDIO_AC3_BITRATE_256K);
574
575 case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
576 return v4l2_ctrl_query_fill(qctrl,
577 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
578 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000, 1,
579 V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000);
580
581 case V4L2_CID_MPEG_VIDEO_ENCODING:
582 return v4l2_ctrl_query_fill(qctrl,
583 V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
584 V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
585 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
586
587 case V4L2_CID_MPEG_VIDEO_ASPECT:
588 return v4l2_ctrl_query_fill(qctrl,
589 V4L2_MPEG_VIDEO_ASPECT_4x3,
590 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
591 V4L2_MPEG_VIDEO_ASPECT_4x3);
592
593 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
594 err = v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 8000000);
595 if (err == 0 &&
596 params->vi_bitrate_mode ==
597 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
598 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
599 return err;
600
601 case V4L2_CID_MPEG_STREAM_TYPE:
602 return v4l2_ctrl_query_fill(qctrl,
603 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
604 V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1,
605 V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
606
607 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
608 return v4l2_ctrl_query_fill(qctrl,
609 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
610 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
611 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
612 case V4L2_CID_MPEG_VIDEO_BITRATE:
613 return v4l2_ctrl_query_fill(qctrl, 0, 27000000, 1, 6000000);
614 case V4L2_CID_MPEG_STREAM_PID_PMT:
615 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 16);
616 case V4L2_CID_MPEG_STREAM_PID_AUDIO:
617 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 260);
618 case V4L2_CID_MPEG_STREAM_PID_VIDEO:
619 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 256);
620 case V4L2_CID_MPEG_STREAM_PID_PCR:
621 return v4l2_ctrl_query_fill(qctrl, 0, (1 << 14) - 1, 1, 259);
622
623 default:
624 break;
625 }
626 return -EINVAL;
627}
628
629static int saa6752hs_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qmenu)
630{
631 static const u32 mpeg_audio_encoding[] = {
632 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
633 V4L2_CTRL_MENU_IDS_END
634 };
635 static const u32 mpeg_audio_ac3_encoding[] = {
636 V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
637 V4L2_MPEG_AUDIO_ENCODING_AC3,
638 V4L2_CTRL_MENU_IDS_END
639 };
640 static u32 mpeg_audio_l2_bitrate[] = {
641 V4L2_MPEG_AUDIO_L2_BITRATE_256K,
642 V4L2_MPEG_AUDIO_L2_BITRATE_384K,
643 V4L2_CTRL_MENU_IDS_END
644 };
645 static u32 mpeg_audio_ac3_bitrate[] = {
646 V4L2_MPEG_AUDIO_AC3_BITRATE_256K,
647 V4L2_MPEG_AUDIO_AC3_BITRATE_384K,
648 V4L2_CTRL_MENU_IDS_END
649 };
650 struct saa6752hs_state *h = to_state(sd);
651 struct v4l2_queryctrl qctrl;
652 int err;
653
654 qctrl.id = qmenu->id;
655 err = saa6752hs_queryctrl(sd, &qctrl);
656 if (err)
657 return err;
658 switch (qmenu->id) {
659 case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
660 return v4l2_ctrl_query_menu_valid_items(qmenu,
661 mpeg_audio_l2_bitrate);
662 case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
663 if (!h->has_ac3)
664 return -EINVAL;
665 return v4l2_ctrl_query_menu_valid_items(qmenu,
666 mpeg_audio_ac3_bitrate);
667 case V4L2_CID_MPEG_AUDIO_ENCODING:
668 return v4l2_ctrl_query_menu_valid_items(qmenu,
669 h->has_ac3 ? mpeg_audio_ac3_encoding :
670 mpeg_audio_encoding);
671 }
672 return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
673}
674
675static int saa6752hs_init(struct v4l2_subdev *sd, u32 leading_null_bytes)
676{
677 unsigned char buf[9], buf2[4];
678 struct saa6752hs_state *h = to_state(sd);
679 struct i2c_client *client = v4l2_get_subdevdata(sd);
680 unsigned size;
681 u32 crc;
682 unsigned char localPAT[256];
683 unsigned char localPMT[256];
684
685 /* Set video format - must be done first as it resets other settings */
686 set_reg8(client, 0x41, h->video_format);
687
688 /* Set number of lines in input signal */
689 set_reg8(client, 0x40, (h->standard & V4L2_STD_525_60) ? 1 : 0);
690
691 /* set bitrate */
692 saa6752hs_set_bitrate(client, h);
693
694 /* Set GOP structure {3, 13} */
695 set_reg16(client, 0x72, 0x030d);
696
697 /* Set minimum Q-scale {4} */
698 set_reg8(client, 0x82, 0x04);
699
700 /* Set maximum Q-scale {12} */
701 set_reg8(client, 0x83, 0x0c);
702
703 /* Set Output Protocol */
704 set_reg8(client, 0xd0, 0x81);
705
706 /* Set video output stream format {TS} */
707 set_reg8(client, 0xb0, 0x05);
708
709 /* Set leading null byte for TS */
710 set_reg16(client, 0xf6, leading_null_bytes);
711
712 /* compute PAT */
713 memcpy(localPAT, PAT, sizeof(PAT));
714 localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
715 localPAT[18] = h->params.ts_pid_pmt & 0xff;
716 crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
717 localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
718 localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
719 localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
720 localPAT[sizeof(PAT) - 1] = crc & 0xFF;
721
722 /* compute PMT */
723 if (h->params.au_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
724 size = sizeof(PMT_AC3);
725 memcpy(localPMT, PMT_AC3, size);
726 } else {
727 size = sizeof(PMT);
728 memcpy(localPMT, PMT, size);
729 }
730 localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
731 localPMT[4] = h->params.ts_pid_pmt & 0xff;
732 localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
733 localPMT[16] = h->params.ts_pid_pcr & 0xFF;
734 localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
735 localPMT[21] = h->params.ts_pid_video & 0xFF;
736 localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
737 localPMT[26] = h->params.ts_pid_audio & 0xFF;
738 crc = crc32_be(~0, &localPMT[7], size - 7 - 4);
739 localPMT[size - 4] = (crc >> 24) & 0xFF;
740 localPMT[size - 3] = (crc >> 16) & 0xFF;
741 localPMT[size - 2] = (crc >> 8) & 0xFF;
742 localPMT[size - 1] = crc & 0xFF;
743
744 /* Set Audio PID */
745 set_reg16(client, 0xc1, h->params.ts_pid_audio);
746
747 /* Set Video PID */
748 set_reg16(client, 0xc0, h->params.ts_pid_video);
749
750 /* Set PCR PID */
751 set_reg16(client, 0xc4, h->params.ts_pid_pcr);
752
753 /* Send SI tables */
754 i2c_master_send(client, localPAT, sizeof(PAT));
755 i2c_master_send(client, localPMT, size);
756
757 /* mute then unmute audio. This removes buzzing artefacts */
758 set_reg8(client, 0xa4, 1);
759 set_reg8(client, 0xa4, 0);
760
761 /* start it going */
762 saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
763
764 /* readout current state */
765 buf[0] = 0xE1;
766 buf[1] = 0xA7;
767 buf[2] = 0xFE;
768 buf[3] = 0x82;
769 buf[4] = 0xB0;
770 i2c_master_send(client, buf, 5);
771 i2c_master_recv(client, buf2, 4);
772
773 /* change aspect ratio */
774 buf[0] = 0xE0;
775 buf[1] = 0xA7;
776 buf[2] = 0xFE;
777 buf[3] = 0x82;
778 buf[4] = 0xB0;
779 buf[5] = buf2[0];
780 switch (h->params.vi_aspect) {
781 case V4L2_MPEG_VIDEO_ASPECT_16x9:
782 buf[6] = buf2[1] | 0x40;
783 break;
784 case V4L2_MPEG_VIDEO_ASPECT_4x3:
785 default:
786 buf[6] = buf2[1] & 0xBF;
787 break;
788 }
789 buf[7] = buf2[2];
790 buf[8] = buf2[3];
791 i2c_master_send(client, buf, 9);
792
793 return 0;
794}
795
796static int saa6752hs_do_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls, int set)
797{
798 struct saa6752hs_state *h = to_state(sd);
799 struct saa6752hs_mpeg_params params;
800 int i;
801
802 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
803 return -EINVAL;
804
805 params = h->params;
806 for (i = 0; i < ctrls->count; i++) {
807 int err = handle_ctrl(h->has_ac3, &params, ctrls->controls + i, set);
808
809 if (err) {
810 ctrls->error_idx = i;
811 return err;
812 }
813 }
814 if (set)
815 h->params = params;
816 return 0;
817}
818
819static int saa6752hs_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
820{
821 return saa6752hs_do_ext_ctrls(sd, ctrls, 1);
822}
823
824static int saa6752hs_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
825{
826 return saa6752hs_do_ext_ctrls(sd, ctrls, 0);
827}
828
829static int saa6752hs_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ctrls)
830{
831 struct saa6752hs_state *h = to_state(sd);
832 int i;
833
834 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
835 return -EINVAL;
836
837 for (i = 0; i < ctrls->count; i++) {
838 int err = get_ctrl(h->has_ac3, &h->params, ctrls->controls + i);
839
840 if (err) {
841 ctrls->error_idx = i;
842 return err;
843 }
844 }
845 return 0;
846}
847
848static int saa6752hs_g_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
849{
850 struct saa6752hs_state *h = to_state(sd);
851
852 if (h->video_format == SAA6752HS_VF_UNKNOWN)
853 h->video_format = SAA6752HS_VF_D1;
854 f->width = v4l2_format_table[h->video_format].fmt.pix.width;
855 f->height = v4l2_format_table[h->video_format].fmt.pix.height;
856 f->code = V4L2_MBUS_FMT_FIXED;
857 f->field = V4L2_FIELD_INTERLACED;
858 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
859 return 0;
860}
861
862static int saa6752hs_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
863{
864 struct saa6752hs_state *h = to_state(sd);
865 int dist_352, dist_480, dist_720;
866
867 if (f->code != V4L2_MBUS_FMT_FIXED)
868 return -EINVAL;
869
870 /*
871 FIXME: translate and round width/height into EMPRESS
872 subsample type:
873
874 type | PAL | NTSC
875 ---------------------------
876 SIF | 352x288 | 352x240
877 1/2 D1 | 352x576 | 352x480
878 2/3 D1 | 480x576 | 480x480
879 D1 | 720x576 | 720x480
880 */
881
882 dist_352 = abs(f->width - 352);
883 dist_480 = abs(f->width - 480);
884 dist_720 = abs(f->width - 720);
885 if (dist_720 < dist_480) {
886 f->width = 720;
887 f->height = 576;
888 h->video_format = SAA6752HS_VF_D1;
889 } else if (dist_480 < dist_352) {
890 f->width = 480;
891 f->height = 576;
892 h->video_format = SAA6752HS_VF_2_3_D1;
893 } else {
894 f->width = 352;
895 if (abs(f->height - 576) <
896 abs(f->height - 288)) {
897 f->height = 576;
898 h->video_format = SAA6752HS_VF_1_2_D1;
899 } else {
900 f->height = 288;
901 h->video_format = SAA6752HS_VF_SIF;
902 }
903 }
904 f->field = V4L2_FIELD_INTERLACED;
905 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
906 return 0;
907}
908
909static int saa6752hs_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
910{
911 struct saa6752hs_state *h = to_state(sd);
912
913 h->standard = std;
914 return 0;
915}
916
917static int saa6752hs_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
918{
919 struct i2c_client *client = v4l2_get_subdevdata(sd);
920 struct saa6752hs_state *h = to_state(sd);
921
922 return v4l2_chip_ident_i2c_client(client,
923 chip, h->chip, h->revision);
924}
925
926/* ----------------------------------------------------------------------- */
927
928static const struct v4l2_subdev_core_ops saa6752hs_core_ops = {
929 .g_chip_ident = saa6752hs_g_chip_ident,
930 .init = saa6752hs_init,
931 .queryctrl = saa6752hs_queryctrl,
932 .querymenu = saa6752hs_querymenu,
933 .g_ext_ctrls = saa6752hs_g_ext_ctrls,
934 .s_ext_ctrls = saa6752hs_s_ext_ctrls,
935 .try_ext_ctrls = saa6752hs_try_ext_ctrls,
936 .s_std = saa6752hs_s_std,
937};
938
939static const struct v4l2_subdev_video_ops saa6752hs_video_ops = {
940 .s_mbus_fmt = saa6752hs_s_mbus_fmt,
941 .g_mbus_fmt = saa6752hs_g_mbus_fmt,
942};
943
944static const struct v4l2_subdev_ops saa6752hs_ops = {
945 .core = &saa6752hs_core_ops,
946 .video = &saa6752hs_video_ops,
947};
948
949static int saa6752hs_probe(struct i2c_client *client,
950 const struct i2c_device_id *id)
951{
952 struct saa6752hs_state *h = kzalloc(sizeof(*h), GFP_KERNEL);
953 struct v4l2_subdev *sd;
954 u8 addr = 0x13;
955 u8 data[12];
956
957 v4l_info(client, "chip found @ 0x%x (%s)\n",
958 client->addr << 1, client->adapter->name);
959 if (h == NULL)
960 return -ENOMEM;
961 sd = &h->sd;
962 v4l2_i2c_subdev_init(sd, client, &saa6752hs_ops);
963
964 i2c_master_send(client, &addr, 1);
965 i2c_master_recv(client, data, sizeof(data));
966 h->chip = V4L2_IDENT_SAA6752HS;
967 h->revision = (data[8] << 8) | data[9];
968 h->has_ac3 = 0;
969 if (h->revision == 0x0206) {
970 h->chip = V4L2_IDENT_SAA6752HS_AC3;
971 h->has_ac3 = 1;
972 v4l_info(client, "support AC-3\n");
973 }
974 h->params = param_defaults;
975 h->standard = 0; /* Assume 625 input lines */
976 return 0;
977}
978
979static int saa6752hs_remove(struct i2c_client *client)
980{
981 struct v4l2_subdev *sd = i2c_get_clientdata(client);
982
983 v4l2_device_unregister_subdev(sd);
984 kfree(to_state(sd));
985 return 0;
986}
987
988static const struct i2c_device_id saa6752hs_id[] = {
989 { "saa6752hs", 0 },
990 { }
991};
992MODULE_DEVICE_TABLE(i2c, saa6752hs_id);
993
994static struct i2c_driver saa6752hs_driver = {
995 .driver = {
996 .owner = THIS_MODULE,
997 .name = "saa6752hs",
998 },
999 .probe = saa6752hs_probe,
1000 .remove = saa6752hs_remove,
1001 .id_table = saa6752hs_id,
1002};
1003
1004module_i2c_driver(saa6752hs_driver);
1005
1006/*
1007 * Overrides for Emacs so that we follow Linus's tabbing style.
1008 * ---------------------------------------------------------------------------
1009 * Local variables:
1010 * c-basic-offset: 8
1011 * End:
1012 */
diff --git a/drivers/media/pci/saa7134/saa7134-alsa.c b/drivers/media/pci/saa7134/saa7134-alsa.c
new file mode 100644
index 000000000000..10460fd3ce39
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-alsa.c
@@ -0,0 +1,1209 @@
1/*
2 * SAA713x ALSA support for V4L
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
18
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/wait.h>
23#include <linux/module.h>
24#include <sound/core.h>
25#include <sound/control.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/initval.h>
29#include <linux/interrupt.h>
30
31#include "saa7134.h"
32#include "saa7134-reg.h"
33
34static unsigned int debug;
35module_param(debug, int, 0644);
36MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
37
38/*
39 * Configuration macros
40 */
41
42/* defaults */
43#define MIXER_ADDR_UNSELECTED -1
44#define MIXER_ADDR_TVTUNER 0
45#define MIXER_ADDR_LINE1 1
46#define MIXER_ADDR_LINE2 2
47#define MIXER_ADDR_LAST 2
48
49
50static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
51static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
52static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
53
54module_param_array(index, int, NULL, 0444);
55module_param_array(enable, int, NULL, 0444);
56MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
57MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
58
59#define dprintk(fmt, arg...) if (debug) \
60 printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ##arg)
61
62
63
64/*
65 * Main chip structure
66 */
67
68typedef struct snd_card_saa7134 {
69 struct snd_card *card;
70 spinlock_t mixer_lock;
71 int mixer_volume[MIXER_ADDR_LAST+1][2];
72 int capture_source_addr;
73 int capture_source[2];
74 struct snd_kcontrol *capture_ctl[MIXER_ADDR_LAST+1];
75 struct pci_dev *pci;
76 struct saa7134_dev *dev;
77
78 unsigned long iobase;
79 s16 irq;
80 u16 mute_was_on;
81
82 spinlock_t lock;
83} snd_card_saa7134_t;
84
85
86/*
87 * PCM structure
88 */
89
90typedef struct snd_card_saa7134_pcm {
91 struct saa7134_dev *dev;
92
93 spinlock_t lock;
94
95 struct snd_pcm_substream *substream;
96} snd_card_saa7134_pcm_t;
97
98static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
99
100
101/*
102 * saa7134 DMA audio stop
103 *
104 * Called when the capture device is released or the buffer overflows
105 *
106 * - Copied verbatim from saa7134-oss's dsp_dma_stop.
107 *
108 */
109
110static void saa7134_dma_stop(struct saa7134_dev *dev)
111{
112 dev->dmasound.dma_blk = -1;
113 dev->dmasound.dma_running = 0;
114 saa7134_set_dmabits(dev);
115}
116
117/*
118 * saa7134 DMA audio start
119 *
120 * Called when preparing the capture device for use
121 *
122 * - Copied verbatim from saa7134-oss's dsp_dma_start.
123 *
124 */
125
126static void saa7134_dma_start(struct saa7134_dev *dev)
127{
128 dev->dmasound.dma_blk = 0;
129 dev->dmasound.dma_running = 1;
130 saa7134_set_dmabits(dev);
131}
132
133/*
134 * saa7134 audio DMA IRQ handler
135 *
136 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
137 * Handles shifting between the 2 buffers, manages the read counters,
138 * and notifies ALSA when periods elapse
139 *
140 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
141 *
142 */
143
144static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
145 unsigned long status)
146{
147 int next_blk, reg = 0;
148
149 spin_lock(&dev->slock);
150 if (UNSET == dev->dmasound.dma_blk) {
151 dprintk("irq: recording stopped\n");
152 goto done;
153 }
154 if (0 != (status & 0x0f000000))
155 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
156 if (0 == (status & 0x10000000)) {
157 /* odd */
158 if (0 == (dev->dmasound.dma_blk & 0x01))
159 reg = SAA7134_RS_BA1(6);
160 } else {
161 /* even */
162 if (1 == (dev->dmasound.dma_blk & 0x01))
163 reg = SAA7134_RS_BA2(6);
164 }
165 if (0 == reg) {
166 dprintk("irq: field oops [%s]\n",
167 (status & 0x10000000) ? "even" : "odd");
168 goto done;
169 }
170
171 if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
172 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->dmasound.read_count,
173 dev->dmasound.bufsize, dev->dmasound.blocks);
174 spin_unlock(&dev->slock);
175 snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
176 return;
177 }
178
179 /* next block addr */
180 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
181 saa_writel(reg,next_blk * dev->dmasound.blksize);
182 if (debug > 2)
183 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
184 (status & 0x10000000) ? "even" : "odd ", next_blk,
185 next_blk * dev->dmasound.blksize, dev->dmasound.blocks, dev->dmasound.blksize, dev->dmasound.read_count);
186
187 /* update status & wake waiting readers */
188 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
189 dev->dmasound.read_count += dev->dmasound.blksize;
190
191 dev->dmasound.recording_on = reg;
192
193 if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
194 spin_unlock(&dev->slock);
195 snd_pcm_period_elapsed(dev->dmasound.substream);
196 spin_lock(&dev->slock);
197 }
198
199 done:
200 spin_unlock(&dev->slock);
201
202}
203
204/*
205 * IRQ request handler
206 *
207 * Runs along with saa7134's IRQ handler, discards anything that isn't
208 * DMA sound
209 *
210 */
211
212static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
213{
214 struct saa7134_dmasound *dmasound = dev_id;
215 struct saa7134_dev *dev = dmasound->priv_data;
216
217 unsigned long report, status;
218 int loop, handled = 0;
219
220 for (loop = 0; loop < 10; loop++) {
221 report = saa_readl(SAA7134_IRQ_REPORT);
222 status = saa_readl(SAA7134_IRQ_STATUS);
223
224 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
225 handled = 1;
226 saa_writel(SAA7134_IRQ_REPORT,
227 SAA7134_IRQ_REPORT_DONE_RA3);
228 saa7134_irq_alsa_done(dev, status);
229 } else {
230 goto out;
231 }
232 }
233
234 if (loop == 10) {
235 dprintk("error! looping IRQ!");
236 }
237
238out:
239 return IRQ_RETVAL(handled);
240}
241
242/*
243 * ALSA capture trigger
244 *
245 * - One of the ALSA capture callbacks.
246 *
247 * Called whenever a capture is started or stopped. Must be defined,
248 * but there's nothing we want to do here
249 *
250 */
251
252static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
253 int cmd)
254{
255 struct snd_pcm_runtime *runtime = substream->runtime;
256 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
257 struct saa7134_dev *dev=pcm->dev;
258 int err = 0;
259
260 spin_lock(&dev->slock);
261 if (cmd == SNDRV_PCM_TRIGGER_START) {
262 /* start dma */
263 saa7134_dma_start(dev);
264 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
265 /* stop dma */
266 saa7134_dma_stop(dev);
267 } else {
268 err = -EINVAL;
269 }
270 spin_unlock(&dev->slock);
271
272 return err;
273}
274
275/*
276 * DMA buffer initialization
277 *
278 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
279 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
280 * usage of V4L's
281 *
282 * - Copied verbatim from saa7134-oss.
283 *
284 */
285
286static int dsp_buffer_init(struct saa7134_dev *dev)
287{
288 int err;
289
290 BUG_ON(!dev->dmasound.bufsize);
291
292 videobuf_dma_init(&dev->dmasound.dma);
293 err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
294 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
295 if (0 != err)
296 return err;
297 return 0;
298}
299
300/*
301 * DMA buffer release
302 *
303 * Called after closing the device, during snd_card_saa7134_capture_close
304 *
305 */
306
307static int dsp_buffer_free(struct saa7134_dev *dev)
308{
309 BUG_ON(!dev->dmasound.blksize);
310
311 videobuf_dma_free(&dev->dmasound.dma);
312
313 dev->dmasound.blocks = 0;
314 dev->dmasound.blksize = 0;
315 dev->dmasound.bufsize = 0;
316
317 return 0;
318}
319
320/*
321 * Setting the capture source and updating the ALSA controls
322 */
323static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
324 int left, int right, bool force_notify)
325{
326 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
327 int change = 0, addr = kcontrol->private_value;
328 int active, old_addr;
329 u32 anabar, xbarin;
330 int analog_io, rate;
331 struct saa7134_dev *dev;
332
333 dev = chip->dev;
334
335 spin_lock_irq(&chip->mixer_lock);
336
337 active = left != 0 || right != 0;
338 old_addr = chip->capture_source_addr;
339
340 /* The active capture source cannot be deactivated */
341 if (active) {
342 change = old_addr != addr ||
343 chip->capture_source[0] != left ||
344 chip->capture_source[1] != right;
345
346 chip->capture_source[0] = left;
347 chip->capture_source[1] = right;
348 chip->capture_source_addr = addr;
349 dev->dmasound.input = addr;
350 }
351 spin_unlock_irq(&chip->mixer_lock);
352
353 if (change) {
354 switch (dev->pci->device) {
355
356 case PCI_DEVICE_ID_PHILIPS_SAA7134:
357 switch (addr) {
358 case MIXER_ADDR_TVTUNER:
359 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
360 0xc0, 0xc0);
361 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
362 0x03, 0x00);
363 break;
364 case MIXER_ADDR_LINE1:
365 case MIXER_ADDR_LINE2:
366 analog_io = (MIXER_ADDR_LINE1 == addr) ?
367 0x00 : 0x08;
368 rate = (32000 == dev->dmasound.rate) ?
369 0x01 : 0x03;
370 saa_andorb(SAA7134_ANALOG_IO_SELECT,
371 0x08, analog_io);
372 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
373 0xc0, 0x80);
374 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
375 0x03, rate);
376 break;
377 }
378
379 break;
380 case PCI_DEVICE_ID_PHILIPS_SAA7133:
381 case PCI_DEVICE_ID_PHILIPS_SAA7135:
382 xbarin = 0x03; /* adc */
383 anabar = 0;
384 switch (addr) {
385 case MIXER_ADDR_TVTUNER:
386 xbarin = 0; /* Demodulator */
387 anabar = 2; /* DACs */
388 break;
389 case MIXER_ADDR_LINE1:
390 anabar = 0; /* aux1, aux1 */
391 break;
392 case MIXER_ADDR_LINE2:
393 anabar = 9; /* aux2, aux2 */
394 break;
395 }
396
397 /* output xbar always main channel */
398 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
399 0xbbbb10);
400
401 if (left || right) {
402 /* We've got data, turn the input on */
403 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
404 xbarin);
405 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
406 } else {
407 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
408 0);
409 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
410 }
411 break;
412 }
413 }
414
415 if (change) {
416 if (force_notify)
417 snd_ctl_notify(chip->card,
418 SNDRV_CTL_EVENT_MASK_VALUE,
419 &chip->capture_ctl[addr]->id);
420
421 if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
422 snd_ctl_notify(chip->card,
423 SNDRV_CTL_EVENT_MASK_VALUE,
424 &chip->capture_ctl[old_addr]->id);
425 }
426
427 return change;
428}
429
430/*
431 * ALSA PCM preparation
432 *
433 * - One of the ALSA capture callbacks.
434 *
435 * Called right after the capture device is opened, this function configures
436 * the buffer using the previously defined functions, allocates the memory,
437 * sets up the hardware registers, and then starts the DMA. When this function
438 * returns, the audio should be flowing.
439 *
440 */
441
442static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
443{
444 struct snd_pcm_runtime *runtime = substream->runtime;
445 int bswap, sign;
446 u32 fmt, control;
447 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
448 struct saa7134_dev *dev;
449 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
450
451 pcm->dev->dmasound.substream = substream;
452
453 dev = saa7134->dev;
454
455 if (snd_pcm_format_width(runtime->format) == 8)
456 fmt = 0x00;
457 else
458 fmt = 0x01;
459
460 if (snd_pcm_format_signed(runtime->format))
461 sign = 1;
462 else
463 sign = 0;
464
465 if (snd_pcm_format_big_endian(runtime->format))
466 bswap = 1;
467 else
468 bswap = 0;
469
470 switch (dev->pci->device) {
471 case PCI_DEVICE_ID_PHILIPS_SAA7134:
472 if (1 == runtime->channels)
473 fmt |= (1 << 3);
474 if (2 == runtime->channels)
475 fmt |= (3 << 3);
476 if (sign)
477 fmt |= 0x04;
478
479 fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
480 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
481 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
482 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
483 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
484
485 break;
486 case PCI_DEVICE_ID_PHILIPS_SAA7133:
487 case PCI_DEVICE_ID_PHILIPS_SAA7135:
488 if (1 == runtime->channels)
489 fmt |= (1 << 4);
490 if (2 == runtime->channels)
491 fmt |= (2 << 4);
492 if (!sign)
493 fmt |= 0x04;
494 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
495 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
496 break;
497 }
498
499 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
500 runtime->format, runtime->channels, fmt,
501 bswap ? 'b' : '-');
502 /* dma: setup channel 6 (= AUDIO) */
503 control = SAA7134_RS_CONTROL_BURST_16 |
504 SAA7134_RS_CONTROL_ME |
505 (dev->dmasound.pt.dma >> 12);
506 if (bswap)
507 control |= SAA7134_RS_CONTROL_BSWAP;
508
509 saa_writel(SAA7134_RS_BA1(6),0);
510 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
511 saa_writel(SAA7134_RS_PITCH(6),0);
512 saa_writel(SAA7134_RS_CONTROL(6),control);
513
514 dev->dmasound.rate = runtime->rate;
515
516 /* Setup and update the card/ALSA controls */
517 snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
518 true);
519
520 return 0;
521
522}
523
524/*
525 * ALSA pointer fetching
526 *
527 * - One of the ALSA capture callbacks.
528 *
529 * Called whenever a period elapses, it must return the current hardware
530 * position of the buffer.
531 * Also resets the read counter used to prevent overruns
532 *
533 */
534
535static snd_pcm_uframes_t
536snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
537{
538 struct snd_pcm_runtime *runtime = substream->runtime;
539 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
540 struct saa7134_dev *dev=pcm->dev;
541
542 if (dev->dmasound.read_count) {
543 dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
544 dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
545 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
546 dev->dmasound.read_offset = 0;
547 }
548
549 return bytes_to_frames(runtime, dev->dmasound.read_offset);
550}
551
552/*
553 * ALSA hardware capabilities definition
554 *
555 * Report only 32kHz for ALSA:
556 *
557 * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
558 * only
559 * - SAA7134 for TV mode uses DemDec mode (32kHz)
560 * - Radio works in 32kHz only
561 * - When recording 48kHz from Line1/Line2, switching of capture source to TV
562 * means
563 * switching to 32kHz without any frequency translation
564 */
565
566static struct snd_pcm_hardware snd_card_saa7134_capture =
567{
568 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
569 SNDRV_PCM_INFO_BLOCK_TRANSFER |
570 SNDRV_PCM_INFO_MMAP_VALID),
571 .formats = SNDRV_PCM_FMTBIT_S16_LE | \
572 SNDRV_PCM_FMTBIT_S16_BE | \
573 SNDRV_PCM_FMTBIT_S8 | \
574 SNDRV_PCM_FMTBIT_U8 | \
575 SNDRV_PCM_FMTBIT_U16_LE | \
576 SNDRV_PCM_FMTBIT_U16_BE,
577 .rates = SNDRV_PCM_RATE_32000,
578 .rate_min = 32000,
579 .rate_max = 32000,
580 .channels_min = 1,
581 .channels_max = 2,
582 .buffer_bytes_max = (256*1024),
583 .period_bytes_min = 64,
584 .period_bytes_max = (256*1024),
585 .periods_min = 4,
586 .periods_max = 1024,
587};
588
589static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
590{
591 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
592
593 kfree(pcm);
594}
595
596
597/*
598 * ALSA hardware params
599 *
600 * - One of the ALSA capture callbacks.
601 *
602 * Called on initialization, right before the PCM preparation
603 *
604 */
605
606static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
607 struct snd_pcm_hw_params * hw_params)
608{
609 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
610 struct saa7134_dev *dev;
611 unsigned int period_size, periods;
612 int err;
613
614 period_size = params_period_bytes(hw_params);
615 periods = params_periods(hw_params);
616
617 if (period_size < 0x100 || period_size > 0x10000)
618 return -EINVAL;
619 if (periods < 4)
620 return -EINVAL;
621 if (period_size * periods > 1024 * 1024)
622 return -EINVAL;
623
624 dev = saa7134->dev;
625
626 if (dev->dmasound.blocks == periods &&
627 dev->dmasound.blksize == period_size)
628 return 0;
629
630 /* release the old buffer */
631 if (substream->runtime->dma_area) {
632 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
633 videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
634 dsp_buffer_free(dev);
635 substream->runtime->dma_area = NULL;
636 }
637 dev->dmasound.blocks = periods;
638 dev->dmasound.blksize = period_size;
639 dev->dmasound.bufsize = period_size * periods;
640
641 err = dsp_buffer_init(dev);
642 if (0 != err) {
643 dev->dmasound.blocks = 0;
644 dev->dmasound.blksize = 0;
645 dev->dmasound.bufsize = 0;
646 return err;
647 }
648
649 if (0 != (err = videobuf_dma_map(&dev->pci->dev, &dev->dmasound.dma))) {
650 dsp_buffer_free(dev);
651 return err;
652 }
653 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt))) {
654 videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
655 dsp_buffer_free(dev);
656 return err;
657 }
658 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
659 dev->dmasound.dma.sglist,
660 dev->dmasound.dma.sglen,
661 0))) {
662 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
663 videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
664 dsp_buffer_free(dev);
665 return err;
666 }
667
668 /* I should be able to use runtime->dma_addr in the control
669 byte, but it doesn't work. So I allocate the DMA using the
670 V4L functions, and force ALSA to use that as the DMA area */
671
672 substream->runtime->dma_area = dev->dmasound.dma.vaddr;
673 substream->runtime->dma_bytes = dev->dmasound.bufsize;
674 substream->runtime->dma_addr = 0;
675
676 return 0;
677
678}
679
680/*
681 * ALSA hardware release
682 *
683 * - One of the ALSA capture callbacks.
684 *
685 * Called after closing the device, but before snd_card_saa7134_capture_close
686 * It stops the DMA audio and releases the buffers.
687 *
688 */
689
690static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
691{
692 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
693 struct saa7134_dev *dev;
694
695 dev = saa7134->dev;
696
697 if (substream->runtime->dma_area) {
698 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
699 videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
700 dsp_buffer_free(dev);
701 substream->runtime->dma_area = NULL;
702 }
703
704 return 0;
705}
706
707/*
708 * ALSA capture finish
709 *
710 * - One of the ALSA capture callbacks.
711 *
712 * Called after closing the device.
713 *
714 */
715
716static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
717{
718 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
719 struct saa7134_dev *dev = saa7134->dev;
720
721 if (saa7134->mute_was_on) {
722 dev->ctl_mute = 1;
723 saa7134_tvaudio_setmute(dev);
724 }
725 return 0;
726}
727
728/*
729 * ALSA capture start
730 *
731 * - One of the ALSA capture callbacks.
732 *
733 * Called when opening the device. It creates and populates the PCM
734 * structure
735 *
736 */
737
738static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
739{
740 struct snd_pcm_runtime *runtime = substream->runtime;
741 snd_card_saa7134_pcm_t *pcm;
742 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
743 struct saa7134_dev *dev;
744 int amux, err;
745
746 if (!saa7134) {
747 printk(KERN_ERR "BUG: saa7134 can't find device struct."
748 " Can't proceed with open\n");
749 return -ENODEV;
750 }
751 dev = saa7134->dev;
752 mutex_lock(&dev->dmasound.lock);
753
754 dev->dmasound.read_count = 0;
755 dev->dmasound.read_offset = 0;
756
757 amux = dev->input->amux;
758 if ((amux < 1) || (amux > 3))
759 amux = 1;
760 dev->dmasound.input = amux - 1;
761
762 mutex_unlock(&dev->dmasound.lock);
763
764 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
765 if (pcm == NULL)
766 return -ENOMEM;
767
768 pcm->dev=saa7134->dev;
769
770 spin_lock_init(&pcm->lock);
771
772 pcm->substream = substream;
773 runtime->private_data = pcm;
774 runtime->private_free = snd_card_saa7134_runtime_free;
775 runtime->hw = snd_card_saa7134_capture;
776
777 if (dev->ctl_mute != 0) {
778 saa7134->mute_was_on = 1;
779 dev->ctl_mute = 0;
780 saa7134_tvaudio_setmute(dev);
781 }
782
783 err = snd_pcm_hw_constraint_integer(runtime,
784 SNDRV_PCM_HW_PARAM_PERIODS);
785 if (err < 0)
786 return err;
787
788 err = snd_pcm_hw_constraint_step(runtime, 0,
789 SNDRV_PCM_HW_PARAM_PERIODS, 2);
790 if (err < 0)
791 return err;
792
793 return 0;
794}
795
796/*
797 * page callback (needed for mmap)
798 */
799
800static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
801 unsigned long offset)
802{
803 void *pageptr = substream->runtime->dma_area + offset;
804 return vmalloc_to_page(pageptr);
805}
806
807/*
808 * ALSA capture callbacks definition
809 */
810
811static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
812 .open = snd_card_saa7134_capture_open,
813 .close = snd_card_saa7134_capture_close,
814 .ioctl = snd_pcm_lib_ioctl,
815 .hw_params = snd_card_saa7134_hw_params,
816 .hw_free = snd_card_saa7134_hw_free,
817 .prepare = snd_card_saa7134_capture_prepare,
818 .trigger = snd_card_saa7134_capture_trigger,
819 .pointer = snd_card_saa7134_capture_pointer,
820 .page = snd_card_saa7134_page,
821};
822
823/*
824 * ALSA PCM setup
825 *
826 * Called when initializing the board. Sets up the name and hooks up
827 * the callbacks
828 *
829 */
830
831static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
832{
833 struct snd_pcm *pcm;
834 int err;
835
836 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
837 return err;
838 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
839 pcm->private_data = saa7134;
840 pcm->info_flags = 0;
841 strcpy(pcm->name, "SAA7134 PCM");
842 return 0;
843}
844
845#define SAA713x_VOLUME(xname, xindex, addr) \
846{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
847 .info = snd_saa7134_volume_info, \
848 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
849 .private_value = addr }
850
851static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
852 struct snd_ctl_elem_info * uinfo)
853{
854 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
855 uinfo->count = 2;
856 uinfo->value.integer.min = 0;
857 uinfo->value.integer.max = 20;
858 return 0;
859}
860
861static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
862 struct snd_ctl_elem_value * ucontrol)
863{
864 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
865 int addr = kcontrol->private_value;
866
867 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
868 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
869 return 0;
870}
871
872static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
873 struct snd_ctl_elem_value * ucontrol)
874{
875 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
876 struct saa7134_dev *dev = chip->dev;
877
878 int change, addr = kcontrol->private_value;
879 int left, right;
880
881 left = ucontrol->value.integer.value[0];
882 if (left < 0)
883 left = 0;
884 if (left > 20)
885 left = 20;
886 right = ucontrol->value.integer.value[1];
887 if (right < 0)
888 right = 0;
889 if (right > 20)
890 right = 20;
891 spin_lock_irq(&chip->mixer_lock);
892 change = 0;
893 if (chip->mixer_volume[addr][0] != left) {
894 change = 1;
895 right = left;
896 }
897 if (chip->mixer_volume[addr][1] != right) {
898 change = 1;
899 left = right;
900 }
901 if (change) {
902 switch (dev->pci->device) {
903 case PCI_DEVICE_ID_PHILIPS_SAA7134:
904 switch (addr) {
905 case MIXER_ADDR_TVTUNER:
906 left = 20;
907 break;
908 case MIXER_ADDR_LINE1:
909 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
910 (left > 10) ? 0x00 : 0x10);
911 break;
912 case MIXER_ADDR_LINE2:
913 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
914 (left > 10) ? 0x00 : 0x20);
915 break;
916 }
917 break;
918 case PCI_DEVICE_ID_PHILIPS_SAA7133:
919 case PCI_DEVICE_ID_PHILIPS_SAA7135:
920 switch (addr) {
921 case MIXER_ADDR_TVTUNER:
922 left = 20;
923 break;
924 case MIXER_ADDR_LINE1:
925 saa_andorb(0x0594, 0x10,
926 (left > 10) ? 0x00 : 0x10);
927 break;
928 case MIXER_ADDR_LINE2:
929 saa_andorb(0x0594, 0x20,
930 (left > 10) ? 0x00 : 0x20);
931 break;
932 }
933 break;
934 }
935 chip->mixer_volume[addr][0] = left;
936 chip->mixer_volume[addr][1] = right;
937 }
938 spin_unlock_irq(&chip->mixer_lock);
939 return change;
940}
941
942#define SAA713x_CAPSRC(xname, xindex, addr) \
943{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
944 .info = snd_saa7134_capsrc_info, \
945 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
946 .private_value = addr }
947
948static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
949 struct snd_ctl_elem_info * uinfo)
950{
951 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
952 uinfo->count = 2;
953 uinfo->value.integer.min = 0;
954 uinfo->value.integer.max = 1;
955 return 0;
956}
957
958static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
959 struct snd_ctl_elem_value * ucontrol)
960{
961 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
962 int addr = kcontrol->private_value;
963
964 spin_lock_irq(&chip->mixer_lock);
965 if (chip->capture_source_addr == addr) {
966 ucontrol->value.integer.value[0] = chip->capture_source[0];
967 ucontrol->value.integer.value[1] = chip->capture_source[1];
968 } else {
969 ucontrol->value.integer.value[0] = 0;
970 ucontrol->value.integer.value[1] = 0;
971 }
972 spin_unlock_irq(&chip->mixer_lock);
973
974 return 0;
975}
976
977static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
978 struct snd_ctl_elem_value * ucontrol)
979{
980 int left, right;
981 left = ucontrol->value.integer.value[0] & 1;
982 right = ucontrol->value.integer.value[1] & 1;
983
984 return snd_saa7134_capsrc_set(kcontrol, left, right, false);
985}
986
987static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
988SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
989SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
990SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
991};
992
993static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
994SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
995SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
996SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
997};
998
999/*
1000 * ALSA mixer setup
1001 *
1002 * Called when initializing the board. Sets up the name and hooks up
1003 * the callbacks
1004 *
1005 */
1006
1007static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
1008{
1009 struct snd_card *card = chip->card;
1010 struct snd_kcontrol *kcontrol;
1011 unsigned int idx;
1012 int err, addr;
1013
1014 strcpy(card->mixername, "SAA7134 Mixer");
1015
1016 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1017 kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1018 chip);
1019 err = snd_ctl_add(card, kcontrol);
1020 if (err < 0)
1021 return err;
1022 }
1023
1024 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1025 kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1026 chip);
1027 addr = snd_saa7134_capture_controls[idx].private_value;
1028 chip->capture_ctl[addr] = kcontrol;
1029 err = snd_ctl_add(card, kcontrol);
1030 if (err < 0)
1031 return err;
1032 }
1033
1034 chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
1035 return 0;
1036}
1037
1038static void snd_saa7134_free(struct snd_card * card)
1039{
1040 snd_card_saa7134_t *chip = card->private_data;
1041
1042 if (chip->dev->dmasound.priv_data == NULL)
1043 return;
1044
1045 if (chip->irq >= 0)
1046 free_irq(chip->irq, &chip->dev->dmasound);
1047
1048 chip->dev->dmasound.priv_data = NULL;
1049
1050}
1051
1052/*
1053 * ALSA initialization
1054 *
1055 * Called by the init routine, once for each saa7134 device present,
1056 * it creates the basic structures and registers the ALSA devices
1057 *
1058 */
1059
1060static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
1061{
1062
1063 struct snd_card *card;
1064 snd_card_saa7134_t *chip;
1065 int err;
1066
1067
1068 if (devnum >= SNDRV_CARDS)
1069 return -ENODEV;
1070 if (!enable[devnum])
1071 return -ENODEV;
1072
1073 err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
1074 sizeof(snd_card_saa7134_t), &card);
1075 if (err < 0)
1076 return err;
1077
1078 strcpy(card->driver, "SAA7134");
1079
1080 /* Card "creation" */
1081
1082 card->private_free = snd_saa7134_free;
1083 chip = card->private_data;
1084
1085 spin_lock_init(&chip->lock);
1086 spin_lock_init(&chip->mixer_lock);
1087
1088 chip->dev = dev;
1089
1090 chip->card = card;
1091
1092 chip->pci = dev->pci;
1093 chip->iobase = pci_resource_start(dev->pci, 0);
1094
1095
1096 err = request_irq(dev->pci->irq, saa7134_alsa_irq,
1097 IRQF_SHARED | IRQF_DISABLED, dev->name,
1098 (void*) &dev->dmasound);
1099
1100 if (err < 0) {
1101 printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
1102 dev->name, dev->pci->irq);
1103 goto __nodev;
1104 }
1105
1106 chip->irq = dev->pci->irq;
1107
1108 mutex_init(&dev->dmasound.lock);
1109
1110 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1111 goto __nodev;
1112
1113 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1114 goto __nodev;
1115
1116 snd_card_set_dev(card, &chip->pci->dev);
1117
1118 /* End of "creation" */
1119
1120 strcpy(card->shortname, "SAA7134");
1121 sprintf(card->longname, "%s at 0x%lx irq %d",
1122 chip->dev->name, chip->iobase, chip->irq);
1123
1124 printk(KERN_INFO "%s/alsa: %s registered as card %d\n",dev->name,card->longname,index[devnum]);
1125
1126 if ((err = snd_card_register(card)) == 0) {
1127 snd_saa7134_cards[devnum] = card;
1128 return 0;
1129 }
1130
1131__nodev:
1132 snd_card_free(card);
1133 return err;
1134}
1135
1136
1137static int alsa_device_init(struct saa7134_dev *dev)
1138{
1139 dev->dmasound.priv_data = dev;
1140 alsa_card_saa7134_create(dev,dev->nr);
1141 return 1;
1142}
1143
1144static int alsa_device_exit(struct saa7134_dev *dev)
1145{
1146
1147 snd_card_free(snd_saa7134_cards[dev->nr]);
1148 snd_saa7134_cards[dev->nr] = NULL;
1149 return 1;
1150}
1151
1152/*
1153 * Module initializer
1154 *
1155 * Loops through present saa7134 cards, and assigns an ALSA device
1156 * to each one
1157 *
1158 */
1159
1160static int saa7134_alsa_init(void)
1161{
1162 struct saa7134_dev *dev = NULL;
1163 struct list_head *list;
1164
1165 saa7134_dmasound_init = alsa_device_init;
1166 saa7134_dmasound_exit = alsa_device_exit;
1167
1168 printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
1169
1170 list_for_each(list,&saa7134_devlist) {
1171 dev = list_entry(list, struct saa7134_dev, devlist);
1172 if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
1173 printk(KERN_INFO "%s/alsa: %s doesn't support digital audio\n",
1174 dev->name, saa7134_boards[dev->board].name);
1175 else
1176 alsa_device_init(dev);
1177 }
1178
1179 if (dev == NULL)
1180 printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
1181
1182 return 0;
1183
1184}
1185
1186/*
1187 * Module destructor
1188 */
1189
1190static void saa7134_alsa_exit(void)
1191{
1192 int idx;
1193
1194 for (idx = 0; idx < SNDRV_CARDS; idx++) {
1195 snd_card_free(snd_saa7134_cards[idx]);
1196 }
1197
1198 saa7134_dmasound_init = NULL;
1199 saa7134_dmasound_exit = NULL;
1200 printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
1201
1202 return;
1203}
1204
1205/* We initialize this late, to make sure the sound system is up and running */
1206late_initcall(saa7134_alsa_init);
1207module_exit(saa7134_alsa_exit);
1208MODULE_LICENSE("GPL");
1209MODULE_AUTHOR("Ricardo Cerqueira");
diff --git a/drivers/media/pci/saa7134/saa7134-cards.c b/drivers/media/pci/saa7134/saa7134-cards.c
new file mode 100644
index 000000000000..bc08f1dbc293
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-cards.c
@@ -0,0 +1,8026 @@
1/*
2 *
3 * device driver for philips saa7134 based TV cards
4 * card-specific stuff.
5 *
6 * (c) 2001-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/i2c.h>
26#include <linux/i2c-algo-bit.h>
27
28#include "saa7134-reg.h"
29#include "saa7134.h"
30#include "tuner-xc2028.h"
31#include <media/v4l2-common.h>
32#include <media/tveeprom.h>
33#include "tea5767.h"
34#include "tda18271.h"
35#include "xc5000.h"
36#include "s5h1411.h"
37
38/* commly used strings */
39static char name_mute[] = "mute";
40static char name_radio[] = "Radio";
41static char name_tv[] = "Television";
42static char name_tv_mono[] = "TV (mono only)";
43static char name_comp[] = "Composite";
44static char name_comp1[] = "Composite1";
45static char name_comp2[] = "Composite2";
46static char name_comp3[] = "Composite3";
47static char name_comp4[] = "Composite4";
48static char name_svideo[] = "S-Video";
49
50/* ------------------------------------------------------------------ */
51/* board config info */
52
53/* If radio_type !=UNSET, radio_addr should be specified
54 */
55
56struct saa7134_board saa7134_boards[] = {
57 [SAA7134_BOARD_UNKNOWN] = {
58 .name = "UNKNOWN/GENERIC",
59 .audio_clock = 0x00187de7,
60 .tuner_type = TUNER_ABSENT,
61 .radio_type = UNSET,
62 .tuner_addr = ADDR_UNSET,
63 .radio_addr = ADDR_UNSET,
64
65 .inputs = {{
66 .name = "default",
67 .vmux = 0,
68 .amux = LINE1,
69 }},
70 },
71 [SAA7134_BOARD_PROTEUS_PRO] = {
72 /* /me */
73 .name = "Proteus Pro [philips reference design]",
74 .audio_clock = 0x00187de7,
75 .tuner_type = TUNER_PHILIPS_PAL,
76 .radio_type = UNSET,
77 .tuner_addr = ADDR_UNSET,
78 .radio_addr = ADDR_UNSET,
79
80 .inputs = {{
81 .name = name_comp1,
82 .vmux = 0,
83 .amux = LINE1,
84 },{
85 .name = name_tv,
86 .vmux = 1,
87 .amux = TV,
88 .tv = 1,
89 },{
90 .name = name_tv_mono,
91 .vmux = 1,
92 .amux = LINE2,
93 .tv = 1,
94 }},
95 .radio = {
96 .name = name_radio,
97 .amux = LINE2,
98 },
99 },
100 [SAA7134_BOARD_FLYVIDEO3000] = {
101 /* "Marco d'Itri" <md@Linux.IT> */
102 .name = "LifeView FlyVIDEO3000",
103 .audio_clock = 0x00200000,
104 .tuner_type = TUNER_PHILIPS_PAL,
105 .radio_type = UNSET,
106 .tuner_addr = ADDR_UNSET,
107 .radio_addr = ADDR_UNSET,
108
109 .gpiomask = 0xe000,
110 .inputs = {{
111 .name = name_tv,
112 .vmux = 1,
113 .amux = TV,
114 .gpio = 0x8000,
115 .tv = 1,
116 },{
117 .name = name_tv_mono,
118 .vmux = 1,
119 .amux = LINE2,
120 .gpio = 0x0000,
121 .tv = 1,
122 },{
123 .name = name_comp1,
124 .vmux = 0,
125 .amux = LINE2,
126 .gpio = 0x4000,
127 },{
128 .name = name_comp2,
129 .vmux = 3,
130 .amux = LINE2,
131 .gpio = 0x4000,
132 },{
133 .name = name_svideo,
134 .vmux = 8,
135 .amux = LINE2,
136 .gpio = 0x4000,
137 }},
138 .radio = {
139 .name = name_radio,
140 .amux = LINE2,
141 .gpio = 0x2000,
142 },
143 .mute = {
144 .name = name_mute,
145 .amux = TV,
146 .gpio = 0x8000,
147 },
148 },
149 [SAA7134_BOARD_FLYVIDEO2000] = {
150 /* "TC Wan" <tcwan@cs.usm.my> */
151 .name = "LifeView/Typhoon FlyVIDEO2000",
152 .audio_clock = 0x00200000,
153 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
154 .radio_type = UNSET,
155 .tuner_addr = ADDR_UNSET,
156 .radio_addr = ADDR_UNSET,
157
158 .gpiomask = 0xe000,
159 .inputs = {{
160 .name = name_tv,
161 .vmux = 1,
162 .amux = LINE2,
163 .gpio = 0x0000,
164 .tv = 1,
165 },{
166 .name = name_comp1,
167 .vmux = 0,
168 .amux = LINE2,
169 .gpio = 0x4000,
170 },{
171 .name = name_comp2,
172 .vmux = 3,
173 .amux = LINE2,
174 .gpio = 0x4000,
175 },{
176 .name = name_svideo,
177 .vmux = 8,
178 .amux = LINE2,
179 .gpio = 0x4000,
180 }},
181 .radio = {
182 .name = name_radio,
183 .amux = LINE2,
184 .gpio = 0x2000,
185 },
186 .mute = {
187 .name = name_mute,
188 .amux = LINE2,
189 .gpio = 0x8000,
190 },
191 },
192 [SAA7134_BOARD_FLYTVPLATINUM_MINI] = {
193 /* "Arnaud Quette" <aquette@free.fr> */
194 .name = "LifeView FlyTV Platinum Mini",
195 .audio_clock = 0x00200000,
196 .tuner_type = TUNER_PHILIPS_TDA8290,
197 .radio_type = UNSET,
198 .tuner_addr = ADDR_UNSET,
199 .radio_addr = ADDR_UNSET,
200
201 .inputs = {{
202 .name = name_tv,
203 .vmux = 1,
204 .amux = TV,
205 .tv = 1,
206 },{
207 .name = name_comp1, /* Composite signal on S-Video input */
208 .vmux = 0,
209 .amux = LINE2,
210 },{
211 .name = name_comp2, /* Composite input */
212 .vmux = 3,
213 .amux = LINE2,
214 },{
215 .name = name_svideo,
216 .vmux = 8,
217 .amux = LINE2,
218 }},
219 },
220 [SAA7134_BOARD_FLYTVPLATINUM_FM] = {
221 /* LifeView FlyTV Platinum FM (LR214WF) */
222 /* "Peter Missel <peter.missel@onlinehome.de> */
223 .name = "LifeView FlyTV Platinum FM / Gold",
224 .audio_clock = 0x00200000,
225 .tuner_type = TUNER_PHILIPS_TDA8290,
226 .radio_type = UNSET,
227 .tuner_addr = ADDR_UNSET,
228 .radio_addr = ADDR_UNSET,
229
230 .gpiomask = 0x1E000, /* Set GP16 and unused 15,14,13 to Output */
231 .inputs = {{
232 .name = name_tv,
233 .vmux = 1,
234 .amux = TV,
235 .gpio = 0x10000, /* GP16=1 selects TV input */
236 .tv = 1,
237 },{
238/* .name = name_tv_mono,
239 .vmux = 1,
240 .amux = LINE2,
241 .gpio = 0x0000,
242 .tv = 1,
243 },{
244*/ .name = name_comp1, /* Composite signal on S-Video input */
245 .vmux = 0,
246 .amux = LINE2,
247/* .gpio = 0x4000, */
248 },{
249 .name = name_comp2, /* Composite input */
250 .vmux = 3,
251 .amux = LINE2,
252/* .gpio = 0x4000, */
253 },{
254 .name = name_svideo, /* S-Video signal on S-Video input */
255 .vmux = 8,
256 .amux = LINE2,
257/* .gpio = 0x4000, */
258 }},
259 .radio = {
260 .name = name_radio,
261 .amux = TV,
262 .gpio = 0x00000, /* GP16=0 selects FM radio antenna */
263 },
264 .mute = {
265 .name = name_mute,
266 .amux = TV,
267 .gpio = 0x10000,
268 },
269 },
270 [SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM] = {
271 /* RoverMedia TV Link Pro FM (LR138 REV:I) */
272 /* Eugene Yudin <Eugene.Yudin@gmail.com> */
273 .name = "RoverMedia TV Link Pro FM",
274 .audio_clock = 0x00200000,
275 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3, /* TCL MFPE05 2 */
276 .radio_type = UNSET,
277 .tuner_addr = ADDR_UNSET,
278 .radio_addr = ADDR_UNSET,
279 .tda9887_conf = TDA9887_PRESENT,
280 .gpiomask = 0xe000,
281 .inputs = { {
282 .name = name_tv,
283 .vmux = 1,
284 .amux = TV,
285 .gpio = 0x8000,
286 .tv = 1,
287 }, {
288 .name = name_tv_mono,
289 .vmux = 1,
290 .amux = LINE2,
291 .gpio = 0x0000,
292 .tv = 1,
293 }, {
294 .name = name_comp1,
295 .vmux = 0,
296 .amux = LINE2,
297 .gpio = 0x4000,
298 }, {
299 .name = name_comp2,
300 .vmux = 3,
301 .amux = LINE2,
302 .gpio = 0x4000,
303 }, {
304 .name = name_svideo,
305 .vmux = 8,
306 .amux = LINE2,
307 .gpio = 0x4000,
308 } },
309 .radio = {
310 .name = name_radio,
311 .amux = LINE2,
312 .gpio = 0x2000,
313 },
314 .mute = {
315 .name = name_mute,
316 .amux = TV,
317 .gpio = 0x8000,
318 },
319 },
320 [SAA7134_BOARD_EMPRESS] = {
321 /* "Gert Vervoort" <gert.vervoort@philips.com> */
322 .name = "EMPRESS",
323 .audio_clock = 0x00187de7,
324 .tuner_type = TUNER_PHILIPS_PAL,
325 .radio_type = UNSET,
326 .tuner_addr = ADDR_UNSET,
327 .radio_addr = ADDR_UNSET,
328 .empress_addr = 0x20,
329
330 .inputs = {{
331 .name = name_comp1,
332 .vmux = 0,
333 .amux = LINE1,
334 },{
335 .name = name_svideo,
336 .vmux = 8,
337 .amux = LINE1,
338 },{
339 .name = name_tv,
340 .vmux = 1,
341 .amux = LINE2,
342 .tv = 1,
343 }},
344 .radio = {
345 .name = name_radio,
346 .amux = LINE2,
347 },
348 .mpeg = SAA7134_MPEG_EMPRESS,
349 .video_out = CCIR656,
350 },
351 [SAA7134_BOARD_MONSTERTV] = {
352 /* "K.Ohta" <alpha292@bremen.or.jp> */
353 .name = "SKNet Monster TV",
354 .audio_clock = 0x00187de7,
355 .tuner_type = TUNER_PHILIPS_NTSC_M,
356 .radio_type = UNSET,
357 .tuner_addr = ADDR_UNSET,
358 .radio_addr = ADDR_UNSET,
359
360 .inputs = {{
361 .name = name_tv,
362 .vmux = 1,
363 .amux = TV,
364 .tv = 1,
365 },{
366 .name = name_comp1,
367 .vmux = 0,
368 .amux = LINE1,
369 },{
370 .name = name_svideo,
371 .vmux = 8,
372 .amux = LINE1,
373 }},
374 .radio = {
375 .name = name_radio,
376 .amux = LINE2,
377 },
378 },
379 [SAA7134_BOARD_MD9717] = {
380 .name = "Tevion MD 9717",
381 .audio_clock = 0x00200000,
382 .tuner_type = TUNER_PHILIPS_PAL,
383 .radio_type = UNSET,
384 .tuner_addr = ADDR_UNSET,
385 .radio_addr = ADDR_UNSET,
386 .inputs = {{
387 .name = name_tv,
388 .vmux = 1,
389 .amux = TV,
390 .tv = 1,
391 },{
392 /* workaround for problems with normal TV sound */
393 .name = name_tv_mono,
394 .vmux = 1,
395 .amux = LINE2,
396 .tv = 1,
397 },{
398 .name = name_comp1,
399 .vmux = 0,
400 .amux = LINE1,
401 },{
402 .name = name_comp2,
403 .vmux = 3,
404 .amux = LINE1,
405 },{
406 .name = name_svideo,
407 .vmux = 8,
408 .amux = LINE1,
409 }},
410 .radio = {
411 .name = name_radio,
412 .amux = LINE2,
413 },
414 .mute = {
415 .name = name_mute,
416 .amux = TV,
417 },
418 },
419 [SAA7134_BOARD_TVSTATION_RDS] = {
420 /* Typhoon TV Tuner RDS: Art.Nr. 50694 */
421 .name = "KNC One TV-Station RDS / Typhoon TV Tuner RDS",
422 .audio_clock = 0x00200000,
423 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
424 .radio_type = UNSET,
425 .tuner_addr = ADDR_UNSET,
426 .radio_addr = ADDR_UNSET,
427 .tda9887_conf = TDA9887_PRESENT,
428 .inputs = {{
429 .name = name_tv,
430 .vmux = 1,
431 .amux = TV,
432 .tv = 1,
433 },{
434 .name = name_tv_mono,
435 .vmux = 1,
436 .amux = LINE2,
437 .tv = 1,
438 },{
439
440 .name = name_svideo,
441 .vmux = 8,
442 .amux = LINE1,
443 },{
444 .name = name_comp1,
445 .vmux = 3,
446 .amux = LINE1,
447 },{
448
449 .name = "CVid over SVid",
450 .vmux = 0,
451 .amux = LINE1,
452 }},
453 .radio = {
454 .name = name_radio,
455 .amux = LINE2,
456 },
457 },
458 [SAA7134_BOARD_TVSTATION_DVR] = {
459 .name = "KNC One TV-Station DVR",
460 .audio_clock = 0x00200000,
461 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
462 .radio_type = UNSET,
463 .tuner_addr = ADDR_UNSET,
464 .radio_addr = ADDR_UNSET,
465 .empress_addr = 0x20,
466 .tda9887_conf = TDA9887_PRESENT,
467 .gpiomask = 0x820000,
468 .inputs = {{
469 .name = name_tv,
470 .vmux = 1,
471 .amux = TV,
472 .tv = 1,
473 .gpio = 0x20000,
474 },{
475 .name = name_svideo,
476 .vmux = 8,
477 .amux = LINE1,
478 .gpio = 0x20000,
479 },{
480 .name = name_comp1,
481 .vmux = 3,
482 .amux = LINE1,
483 .gpio = 0x20000,
484 }},
485 .radio = {
486 .name = name_radio,
487 .amux = LINE2,
488 .gpio = 0x20000,
489 },
490 .mpeg = SAA7134_MPEG_EMPRESS,
491 .video_out = CCIR656,
492 },
493 [SAA7134_BOARD_CINERGY400] = {
494 .name = "Terratec Cinergy 400 TV",
495 .audio_clock = 0x00200000,
496 .tuner_type = TUNER_PHILIPS_PAL,
497 .radio_type = UNSET,
498 .tuner_addr = ADDR_UNSET,
499 .radio_addr = ADDR_UNSET,
500 .inputs = {{
501 .name = name_tv,
502 .vmux = 1,
503 .amux = TV,
504 .tv = 1,
505 },{
506 .name = name_comp1,
507 .vmux = 4,
508 .amux = LINE1,
509 },{
510 .name = name_svideo,
511 .vmux = 8,
512 .amux = LINE1,
513 },{
514 .name = name_comp2, /* CVideo over SVideo Connector */
515 .vmux = 0,
516 .amux = LINE1,
517 }}
518 },
519 [SAA7134_BOARD_MD5044] = {
520 .name = "Medion 5044",
521 .audio_clock = 0x00187de7, /* was: 0x00200000, */
522 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
523 .radio_type = UNSET,
524 .tuner_addr = ADDR_UNSET,
525 .radio_addr = ADDR_UNSET,
526 .tda9887_conf = TDA9887_PRESENT,
527 .inputs = {{
528 .name = name_tv,
529 .vmux = 1,
530 .amux = TV,
531 .tv = 1,
532 },{
533 /* workaround for problems with normal TV sound */
534 .name = name_tv_mono,
535 .vmux = 1,
536 .amux = LINE2,
537 .tv = 1,
538 },{
539 .name = name_comp1,
540 .vmux = 0,
541 .amux = LINE2,
542 },{
543 .name = name_comp2,
544 .vmux = 3,
545 .amux = LINE2,
546 },{
547 .name = name_svideo,
548 .vmux = 8,
549 .amux = LINE2,
550 }},
551 .radio = {
552 .name = name_radio,
553 .amux = LINE2,
554 },
555 },
556 [SAA7134_BOARD_KWORLD] = {
557 .name = "Kworld/KuroutoShikou SAA7130-TVPCI",
558 .audio_clock = 0x00187de7,
559 .tuner_type = TUNER_PHILIPS_NTSC_M,
560 .radio_type = UNSET,
561 .tuner_addr = ADDR_UNSET,
562 .radio_addr = ADDR_UNSET,
563 .inputs = {{
564 .name = name_svideo,
565 .vmux = 8,
566 .amux = LINE1,
567 },{
568 .name = name_comp1,
569 .vmux = 3,
570 .amux = LINE1,
571 },{
572 .name = name_tv,
573 .vmux = 1,
574 .amux = LINE2,
575 .tv = 1,
576 }},
577 },
578 [SAA7134_BOARD_CINERGY600] = {
579 .name = "Terratec Cinergy 600 TV",
580 .audio_clock = 0x00200000,
581 .tuner_type = TUNER_PHILIPS_PAL,
582 .radio_type = UNSET,
583 .tuner_addr = ADDR_UNSET,
584 .radio_addr = ADDR_UNSET,
585 .tda9887_conf = TDA9887_PRESENT,
586 .inputs = {{
587 .name = name_tv,
588 .vmux = 1,
589 .amux = TV,
590 .tv = 1,
591 },{
592 .name = name_comp1,
593 .vmux = 4,
594 .amux = LINE1,
595 },{
596 .name = name_svideo,
597 .vmux = 8,
598 .amux = LINE1,
599 },{
600 .name = name_comp2, /* CVideo over SVideo Connector */
601 .vmux = 0,
602 .amux = LINE1,
603 }},
604 .radio = {
605 .name = name_radio,
606 .amux = LINE2,
607 },
608 },
609 [SAA7134_BOARD_MD7134] = {
610 .name = "Medion 7134",
611 .audio_clock = 0x00187de7,
612 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
613 .radio_type = UNSET,
614 .tuner_addr = ADDR_UNSET,
615 .radio_addr = ADDR_UNSET,
616 .tda9887_conf = TDA9887_PRESENT,
617 .mpeg = SAA7134_MPEG_DVB,
618 .inputs = {{
619 .name = name_tv,
620 .vmux = 1,
621 .amux = TV,
622 .tv = 1,
623 },{
624 .name = name_comp1,
625 .vmux = 0,
626 .amux = LINE1,
627 },{
628 .name = name_svideo,
629 .vmux = 8,
630 .amux = LINE1,
631 }},
632 .radio = {
633 .name = name_radio,
634 .amux = LINE2,
635 },
636 .mute = {
637 .name = name_mute,
638 .amux = TV,
639 },
640 },
641 [SAA7134_BOARD_TYPHOON_90031] = {
642 /* aka Typhoon "TV+Radio", Art.Nr 90031 */
643 /* Tom Zoerner <tomzo at users sourceforge net> */
644 .name = "Typhoon TV+Radio 90031",
645 .audio_clock = 0x00200000,
646 .tuner_type = TUNER_PHILIPS_PAL,
647 .radio_type = UNSET,
648 .tuner_addr = ADDR_UNSET,
649 .radio_addr = ADDR_UNSET,
650 .tda9887_conf = TDA9887_PRESENT,
651 .inputs = {{
652 .name = name_tv,
653 .vmux = 1,
654 .amux = TV,
655 .tv = 1,
656 },{
657 .name = name_comp1,
658 .vmux = 3,
659 .amux = LINE1,
660 },{
661 .name = name_svideo,
662 .vmux = 8,
663 .amux = LINE1,
664 }},
665 .radio = {
666 .name = name_radio,
667 .amux = LINE2,
668 },
669 },
670 [SAA7134_BOARD_ELSA] = {
671 .name = "ELSA EX-VISION 300TV",
672 .audio_clock = 0x00187de7,
673 .tuner_type = TUNER_HITACHI_NTSC,
674 .radio_type = UNSET,
675 .tuner_addr = ADDR_UNSET,
676 .radio_addr = ADDR_UNSET,
677 .inputs = {{
678 .name = name_svideo,
679 .vmux = 8,
680 .amux = LINE1,
681 },{
682 .name = name_comp1,
683 .vmux = 0,
684 .amux = LINE1,
685 },{
686 .name = name_tv,
687 .vmux = 4,
688 .amux = LINE2,
689 .tv = 1,
690 }},
691 },
692 [SAA7134_BOARD_ELSA_500TV] = {
693 .name = "ELSA EX-VISION 500TV",
694 .audio_clock = 0x00187de7,
695 .tuner_type = TUNER_HITACHI_NTSC,
696 .radio_type = UNSET,
697 .tuner_addr = ADDR_UNSET,
698 .radio_addr = ADDR_UNSET,
699 .inputs = {{
700 .name = name_svideo,
701 .vmux = 7,
702 .amux = LINE1,
703 },{
704 .name = name_tv,
705 .vmux = 8,
706 .amux = TV,
707 .tv = 1,
708 },{
709 .name = name_tv_mono,
710 .vmux = 8,
711 .amux = LINE2,
712 .tv = 1,
713 }},
714 },
715 [SAA7134_BOARD_ELSA_700TV] = {
716 .name = "ELSA EX-VISION 700TV",
717 .audio_clock = 0x00187de7,
718 .tuner_type = TUNER_HITACHI_NTSC,
719 .radio_type = UNSET,
720 .tuner_addr = ADDR_UNSET,
721 .radio_addr = ADDR_UNSET,
722 .inputs = {{
723 .name = name_tv,
724 .vmux = 4,
725 .amux = LINE2,
726 .tv = 1,
727 },{
728 .name = name_comp1,
729 .vmux = 6,
730 .amux = LINE1,
731 },{
732 .name = name_svideo,
733 .vmux = 7,
734 .amux = LINE1,
735 }},
736 .mute = {
737 .name = name_mute,
738 .amux = TV,
739 },
740 },
741 [SAA7134_BOARD_ASUSTeK_TVFM7134] = {
742 .name = "ASUS TV-FM 7134",
743 .audio_clock = 0x00187de7,
744 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
745 .radio_type = UNSET,
746 .tuner_addr = ADDR_UNSET,
747 .radio_addr = ADDR_UNSET,
748 .tda9887_conf = TDA9887_PRESENT,
749 .inputs = {{
750 .name = name_tv,
751 .vmux = 1,
752 .amux = TV,
753 .tv = 1,
754 },{
755 .name = name_comp1,
756 .vmux = 4,
757 .amux = LINE2,
758 },{
759 .name = name_svideo,
760 .vmux = 6,
761 .amux = LINE2,
762 }},
763 .radio = {
764 .name = name_radio,
765 .amux = LINE1,
766 },
767 },
768 [SAA7134_BOARD_ASUSTeK_TVFM7135] = {
769 .name = "ASUS TV-FM 7135",
770 .audio_clock = 0x00187de7,
771 .tuner_type = TUNER_PHILIPS_TDA8290,
772 .radio_type = UNSET,
773 .tuner_addr = ADDR_UNSET,
774 .radio_addr = ADDR_UNSET,
775 .gpiomask = 0x200000,
776 .inputs = {{
777 .name = name_tv,
778 .vmux = 1,
779 .amux = TV,
780 .gpio = 0x0000,
781 .tv = 1,
782 },{
783 .name = name_comp1,
784 .vmux = 4,
785 .amux = LINE2,
786 .gpio = 0x0000,
787 },{
788 .name = name_svideo,
789 .vmux = 6,
790 .amux = LINE2,
791 .gpio = 0x0000,
792 }},
793 .radio = {
794 .name = name_radio,
795 .amux = TV,
796 .gpio = 0x200000,
797 },
798 .mute = {
799 .name = name_mute,
800 .gpio = 0x0000,
801 },
802
803 },
804 [SAA7134_BOARD_VA1000POWER] = {
805 .name = "AOPEN VA1000 POWER",
806 .audio_clock = 0x00187de7,
807 .tuner_type = TUNER_PHILIPS_NTSC,
808 .radio_type = UNSET,
809 .tuner_addr = ADDR_UNSET,
810 .radio_addr = ADDR_UNSET,
811 .inputs = {{
812 .name = name_svideo,
813 .vmux = 8,
814 .amux = LINE1,
815 },{
816 .name = name_comp1,
817 .vmux = 3,
818 .amux = LINE1,
819 },{
820 .name = name_tv,
821 .vmux = 1,
822 .amux = LINE2,
823 .tv = 1,
824 }},
825 },
826 [SAA7134_BOARD_10MOONSTVMASTER] = {
827 /* "lilicheng" <llc@linuxfans.org> */
828 .name = "10MOONS PCI TV CAPTURE CARD",
829 .audio_clock = 0x00200000,
830 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
831 .radio_type = UNSET,
832 .tuner_addr = ADDR_UNSET,
833 .radio_addr = ADDR_UNSET,
834 .gpiomask = 0xe000,
835 .inputs = {{
836 .name = name_tv,
837 .vmux = 1,
838 .amux = LINE2,
839 .gpio = 0x0000,
840 .tv = 1,
841 },{
842 .name = name_comp1,
843 .vmux = 0,
844 .amux = LINE2,
845 .gpio = 0x4000,
846 },{
847 .name = name_comp2,
848 .vmux = 3,
849 .amux = LINE2,
850 .gpio = 0x4000,
851 },{
852 .name = name_svideo,
853 .vmux = 8,
854 .amux = LINE2,
855 .gpio = 0x4000,
856 }},
857 .radio = {
858 .name = name_radio,
859 .amux = LINE2,
860 .gpio = 0x2000,
861 },
862 .mute = {
863 .name = name_mute,
864 .amux = LINE2,
865 .gpio = 0x8000,
866 },
867 },
868 [SAA7134_BOARD_BMK_MPEX_NOTUNER] = {
869 /* "Andrew de Quincey" <adq@lidskialf.net> */
870 .name = "BMK MPEX No Tuner",
871 .audio_clock = 0x200000,
872 .tuner_type = TUNER_ABSENT,
873 .radio_type = UNSET,
874 .tuner_addr = ADDR_UNSET,
875 .radio_addr = ADDR_UNSET,
876 .empress_addr = 0x20,
877 .inputs = {{
878 .name = name_comp1,
879 .vmux = 4,
880 .amux = LINE1,
881 },{
882 .name = name_comp2,
883 .vmux = 3,
884 .amux = LINE1,
885 },{
886 .name = name_comp3,
887 .vmux = 0,
888 .amux = LINE1,
889 },{
890 .name = name_comp4,
891 .vmux = 1,
892 .amux = LINE1,
893 },{
894 .name = name_svideo,
895 .vmux = 8,
896 .amux = LINE1,
897 }},
898 .mpeg = SAA7134_MPEG_EMPRESS,
899 .video_out = CCIR656,
900 },
901 [SAA7134_BOARD_VIDEOMATE_TV] = {
902 .name = "Compro VideoMate TV",
903 .audio_clock = 0x00187de7,
904 .tuner_type = TUNER_PHILIPS_NTSC_M,
905 .radio_type = UNSET,
906 .tuner_addr = ADDR_UNSET,
907 .radio_addr = ADDR_UNSET,
908 .inputs = {{
909 .name = name_svideo,
910 .vmux = 8,
911 .amux = LINE1,
912 },{
913 .name = name_comp1,
914 .vmux = 3,
915 .amux = LINE1,
916 },{
917 .name = name_tv,
918 .vmux = 1,
919 .amux = LINE2,
920 .tv = 1,
921 }},
922 },
923 [SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUS] = {
924 .name = "Compro VideoMate TV Gold+",
925 .audio_clock = 0x00187de7,
926 .tuner_type = TUNER_PHILIPS_NTSC_M,
927 .gpiomask = 0x800c0000,
928 .radio_type = UNSET,
929 .tuner_addr = ADDR_UNSET,
930 .radio_addr = ADDR_UNSET,
931 .inputs = {{
932 .name = name_svideo,
933 .vmux = 8,
934 .amux = LINE1,
935 .gpio = 0x06c00012,
936 },{
937 .name = name_comp1,
938 .vmux = 3,
939 .amux = LINE1,
940 .gpio = 0x0ac20012,
941 },{
942 .name = name_tv,
943 .vmux = 1,
944 .amux = LINE2,
945 .gpio = 0x08c20012,
946 .tv = 1,
947 }}, /* radio and probably mute is missing */
948 },
949 [SAA7134_BOARD_CRONOS_PLUS] = {
950 /*
951 gpio pins:
952 0 .. 3 BASE_ID
953 4 .. 7 PROTECT_ID
954 8 .. 11 USER_OUT
955 12 .. 13 USER_IN
956 14 .. 15 VIDIN_SEL
957 */
958 .name = "Matrox CronosPlus",
959 .tuner_type = TUNER_ABSENT,
960 .radio_type = UNSET,
961 .tuner_addr = ADDR_UNSET,
962 .radio_addr = ADDR_UNSET,
963 .gpiomask = 0xcf00,
964 .inputs = {{
965 .name = name_comp1,
966 .vmux = 0,
967 .gpio = 2 << 14,
968 },{
969 .name = name_comp2,
970 .vmux = 0,
971 .gpio = 1 << 14,
972 },{
973 .name = name_comp3,
974 .vmux = 0,
975 .gpio = 0 << 14,
976 },{
977 .name = name_comp4,
978 .vmux = 0,
979 .gpio = 3 << 14,
980 },{
981 .name = name_svideo,
982 .vmux = 8,
983 .gpio = 2 << 14,
984 }},
985 },
986 [SAA7134_BOARD_MD2819] = {
987 .name = "AverMedia M156 / Medion 2819",
988 .audio_clock = 0x00187de7,
989 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
990 .radio_type = UNSET,
991 .tuner_addr = ADDR_UNSET,
992 .radio_addr = ADDR_UNSET,
993 .tda9887_conf = TDA9887_PRESENT,
994 .gpiomask = 0x03,
995 .inputs = {{
996 .name = name_tv,
997 .vmux = 1,
998 .amux = TV,
999 .tv = 1,
1000 .gpio = 0x00,
1001 }, {
1002 .name = name_comp1,
1003 .vmux = 3,
1004 .amux = LINE1,
1005 .gpio = 0x02,
1006 }, {
1007 .name = name_comp2,
1008 .vmux = 0,
1009 .amux = LINE1,
1010 .gpio = 0x02,
1011 }, {
1012 .name = name_svideo,
1013 .vmux = 8,
1014 .amux = LINE1,
1015 .gpio = 0x02,
1016 } },
1017 .radio = {
1018 .name = name_radio,
1019 .amux = LINE1,
1020 .gpio = 0x01,
1021 },
1022 .mute = {
1023 .name = name_mute,
1024 .amux = TV,
1025 .gpio = 0x00,
1026 },
1027 },
1028 [SAA7134_BOARD_BMK_MPEX_TUNER] = {
1029 /* "Greg Wickham <greg.wickham@grangenet.net> */
1030 .name = "BMK MPEX Tuner",
1031 .audio_clock = 0x200000,
1032 .tuner_type = TUNER_PHILIPS_PAL,
1033 .radio_type = UNSET,
1034 .tuner_addr = ADDR_UNSET,
1035 .radio_addr = ADDR_UNSET,
1036 .empress_addr = 0x20,
1037 .inputs = {{
1038 .name = name_comp1,
1039 .vmux = 1,
1040 .amux = LINE1,
1041 },{
1042 .name = name_svideo,
1043 .vmux = 8,
1044 .amux = LINE1,
1045 },{
1046 .name = name_tv,
1047 .vmux = 3,
1048 .amux = TV,
1049 .tv = 1,
1050 }},
1051 .mpeg = SAA7134_MPEG_EMPRESS,
1052 .video_out = CCIR656,
1053 },
1054 [SAA7134_BOARD_ASUSTEK_TVFM7133] = {
1055 .name = "ASUS TV-FM 7133",
1056 .audio_clock = 0x00187de7,
1057 /* probably wrong, the 7133 one is the NTSC version ...
1058 * .tuner_type = TUNER_PHILIPS_FM1236_MK3 */
1059 .tuner_type = TUNER_LG_NTSC_NEW_TAPC,
1060 .radio_type = UNSET,
1061 .tuner_addr = ADDR_UNSET,
1062 .radio_addr = ADDR_UNSET,
1063 .tda9887_conf = TDA9887_PRESENT,
1064 .inputs = {{
1065 .name = name_tv,
1066 .vmux = 1,
1067 .amux = TV,
1068 .tv = 1,
1069
1070 },{
1071 .name = name_comp1,
1072 .vmux = 4,
1073 .amux = LINE2,
1074 },{
1075 .name = name_svideo,
1076 .vmux = 6,
1077 .amux = LINE2,
1078 }},
1079 .radio = {
1080 .name = name_radio,
1081 .amux = LINE1,
1082 },
1083 },
1084 [SAA7134_BOARD_PINNACLE_PCTV_STEREO] = {
1085 .name = "Pinnacle PCTV Stereo (saa7134)",
1086 .audio_clock = 0x00187de7,
1087 .tuner_type = TUNER_MT2032,
1088 .radio_type = UNSET,
1089 .tuner_addr = ADDR_UNSET,
1090 .radio_addr = ADDR_UNSET,
1091 .tda9887_conf = TDA9887_PRESENT | TDA9887_INTERCARRIER | TDA9887_PORT2_INACTIVE,
1092 .inputs = {{
1093 .name = name_tv,
1094 .vmux = 3,
1095 .amux = TV,
1096 .tv = 1,
1097 },{
1098 .name = name_comp1,
1099 .vmux = 0,
1100 .amux = LINE2,
1101 },{
1102 .name = name_comp2,
1103 .vmux = 1,
1104 .amux = LINE2,
1105 },{
1106 .name = name_svideo,
1107 .vmux = 8,
1108 .amux = LINE2,
1109 }},
1110 },
1111 [SAA7134_BOARD_MANLI_MTV002] = {
1112 /* Ognjen Nastic <ognjen@logosoft.ba> */
1113 .name = "Manli MuchTV M-TV002",
1114 .audio_clock = 0x00200000,
1115 .tuner_type = TUNER_PHILIPS_PAL,
1116 .radio_type = UNSET,
1117 .tuner_addr = ADDR_UNSET,
1118 .radio_addr = ADDR_UNSET,
1119 .inputs = {{
1120 .name = name_svideo,
1121 .vmux = 8,
1122 .amux = LINE1,
1123 },{
1124 .name = name_comp1,
1125 .vmux = 1,
1126 .amux = LINE1,
1127 },{
1128 .name = name_tv,
1129 .vmux = 3,
1130 .amux = LINE2,
1131 .tv = 1,
1132 }},
1133 .radio = {
1134 .name = name_radio,
1135 .amux = LINE2,
1136 },
1137 },
1138 [SAA7134_BOARD_MANLI_MTV001] = {
1139 /* Ognjen Nastic <ognjen@logosoft.ba> UNTESTED */
1140 .name = "Manli MuchTV M-TV001",
1141 .audio_clock = 0x00200000,
1142 .tuner_type = TUNER_PHILIPS_PAL,
1143 .radio_type = UNSET,
1144 .tuner_addr = ADDR_UNSET,
1145 .radio_addr = ADDR_UNSET,
1146 .inputs = {{
1147 .name = name_svideo,
1148 .vmux = 8,
1149 .amux = LINE1,
1150 },{
1151 .name = name_comp1,
1152 .vmux = 1,
1153 .amux = LINE1,
1154 },{
1155 .name = name_tv,
1156 .vmux = 3,
1157 .amux = LINE2,
1158 .tv = 1,
1159 }},
1160 .mute = {
1161 .name = name_mute,
1162 .amux = LINE1,
1163 },
1164 },
1165 [SAA7134_BOARD_TG3000TV] = {
1166 /* TransGear 3000TV */
1167 .name = "Nagase Sangyo TransGear 3000TV",
1168 .audio_clock = 0x00187de7,
1169 .tuner_type = TUNER_PHILIPS_NTSC_M,
1170 .radio_type = UNSET,
1171 .tuner_addr = ADDR_UNSET,
1172 .radio_addr = ADDR_UNSET,
1173 .inputs = {{
1174 .name = name_tv,
1175 .vmux = 1,
1176 .amux = LINE2,
1177 .tv = 1,
1178 },{
1179 .name = name_comp1,
1180 .vmux = 3,
1181 .amux = LINE2,
1182 },{
1183 .name = name_svideo,
1184 .vmux = 8,
1185 .amux = LINE2,
1186 }},
1187 },
1188 [SAA7134_BOARD_ECS_TVP3XP] = {
1189 .name = "Elitegroup ECS TVP3XP FM1216 Tuner Card(PAL-BG,FM) ",
1190 .audio_clock = 0x187de7, /* xtal 32.1 MHz */
1191 .tuner_type = TUNER_PHILIPS_PAL,
1192 .radio_type = UNSET,
1193 .tuner_addr = ADDR_UNSET,
1194 .radio_addr = ADDR_UNSET,
1195 .inputs = {{
1196 .name = name_tv,
1197 .vmux = 1,
1198 .amux = TV,
1199 .tv = 1,
1200 },{
1201 .name = name_tv_mono,
1202 .vmux = 1,
1203 .amux = LINE2,
1204 .tv = 1,
1205 },{
1206 .name = name_comp1,
1207 .vmux = 3,
1208 .amux = LINE1,
1209 },{
1210 .name = name_svideo,
1211 .vmux = 8,
1212 .amux = LINE1,
1213 },{
1214 .name = "CVid over SVid",
1215 .vmux = 0,
1216 .amux = LINE1,
1217 }},
1218 .radio = {
1219 .name = name_radio,
1220 .amux = LINE2,
1221 },
1222 },
1223 [SAA7134_BOARD_ECS_TVP3XP_4CB5] = {
1224 .name = "Elitegroup ECS TVP3XP FM1236 Tuner Card (NTSC,FM)",
1225 .audio_clock = 0x187de7,
1226 .tuner_type = TUNER_PHILIPS_NTSC,
1227 .radio_type = UNSET,
1228 .tuner_addr = ADDR_UNSET,
1229 .radio_addr = ADDR_UNSET,
1230 .inputs = {{
1231 .name = name_tv,
1232 .vmux = 1,
1233 .amux = TV,
1234 .tv = 1,
1235 },{
1236 .name = name_tv_mono,
1237 .vmux = 1,
1238 .amux = LINE2,
1239 .tv = 1,
1240 },{
1241 .name = name_comp1,
1242 .vmux = 3,
1243 .amux = LINE1,
1244 },{
1245 .name = name_svideo,
1246 .vmux = 8,
1247 .amux = LINE1,
1248 },{
1249 .name = "CVid over SVid",
1250 .vmux = 0,
1251 .amux = LINE1,
1252 }},
1253 .radio = {
1254 .name = name_radio,
1255 .amux = LINE2,
1256 },
1257 },
1258 [SAA7134_BOARD_ECS_TVP3XP_4CB6] = {
1259 /* Barry Scott <barry.scott@onelan.co.uk> */
1260 .name = "Elitegroup ECS TVP3XP FM1246 Tuner Card (PAL,FM)",
1261 .audio_clock = 0x187de7,
1262 .tuner_type = TUNER_PHILIPS_PAL_I,
1263 .radio_type = UNSET,
1264 .tuner_addr = ADDR_UNSET,
1265 .radio_addr = ADDR_UNSET,
1266 .inputs = {{
1267 .name = name_tv,
1268 .vmux = 1,
1269 .amux = TV,
1270 .tv = 1,
1271 },{
1272 .name = name_tv_mono,
1273 .vmux = 1,
1274 .amux = LINE2,
1275 .tv = 1,
1276 },{
1277 .name = name_comp1,
1278 .vmux = 3,
1279 .amux = LINE1,
1280 },{
1281 .name = name_svideo,
1282 .vmux = 8,
1283 .amux = LINE1,
1284 },{
1285 .name = "CVid over SVid",
1286 .vmux = 0,
1287 .amux = LINE1,
1288 }},
1289 .radio = {
1290 .name = name_radio,
1291 .amux = LINE2,
1292 },
1293 },
1294 [SAA7134_BOARD_AVACSSMARTTV] = {
1295 /* Roman Pszonczenko <romka@kolos.math.uni.lodz.pl> */
1296 .name = "AVACS SmartTV",
1297 .audio_clock = 0x00187de7,
1298 .tuner_type = TUNER_PHILIPS_PAL,
1299 .radio_type = UNSET,
1300 .tuner_addr = ADDR_UNSET,
1301 .radio_addr = ADDR_UNSET,
1302 .inputs = {{
1303 .name = name_tv,
1304 .vmux = 1,
1305 .amux = TV,
1306 .tv = 1,
1307 },{
1308 .name = name_tv_mono,
1309 .vmux = 1,
1310 .amux = LINE2,
1311 .tv = 1,
1312 },{
1313 .name = name_comp1,
1314 .vmux = 0,
1315 .amux = LINE2,
1316 },{
1317 .name = name_comp2,
1318 .vmux = 3,
1319 .amux = LINE2,
1320 },{
1321 .name = name_svideo,
1322 .vmux = 8,
1323 .amux = LINE2,
1324 }},
1325 .radio = {
1326 .name = name_radio,
1327 .amux = LINE2,
1328 .gpio = 0x200000,
1329 },
1330 },
1331 [SAA7134_BOARD_AVERMEDIA_DVD_EZMAKER] = {
1332 /* Michael Smith <msmith@cbnco.com> */
1333 .name = "AVerMedia DVD EZMaker",
1334 .audio_clock = 0x00187de7,
1335 .tuner_type = TUNER_ABSENT,
1336 .radio_type = UNSET,
1337 .tuner_addr = ADDR_UNSET,
1338 .radio_addr = ADDR_UNSET,
1339 .inputs = {{
1340 .name = name_comp1,
1341 .vmux = 3,
1342 },{
1343 .name = name_svideo,
1344 .vmux = 8,
1345 }},
1346 },
1347 [SAA7134_BOARD_AVERMEDIA_M103] = {
1348 /* Massimo Piccioni <dafastidio@libero.it> */
1349 .name = "AVerMedia MiniPCI DVB-T Hybrid M103",
1350 .audio_clock = 0x187de7,
1351 .tuner_type = TUNER_XC2028,
1352 .radio_type = UNSET,
1353 .tuner_addr = ADDR_UNSET,
1354 .radio_addr = ADDR_UNSET,
1355 .mpeg = SAA7134_MPEG_DVB,
1356 .inputs = {{
1357 .name = name_tv,
1358 .vmux = 1,
1359 .amux = TV,
1360 .tv = 1,
1361 } },
1362 },
1363 [SAA7134_BOARD_NOVAC_PRIMETV7133] = {
1364 /* toshii@netbsd.org */
1365 .name = "Noval Prime TV 7133",
1366 .audio_clock = 0x00200000,
1367 .tuner_type = TUNER_ALPS_TSBH1_NTSC,
1368 .radio_type = UNSET,
1369 .tuner_addr = ADDR_UNSET,
1370 .radio_addr = ADDR_UNSET,
1371 .inputs = {{
1372 .name = name_comp1,
1373 .vmux = 3,
1374 },{
1375 .name = name_tv,
1376 .vmux = 1,
1377 .amux = TV,
1378 .tv = 1,
1379 },{
1380 .name = name_svideo,
1381 .vmux = 8,
1382 }},
1383 },
1384 [SAA7134_BOARD_AVERMEDIA_STUDIO_305] = {
1385 .name = "AverMedia AverTV Studio 305",
1386 .audio_clock = 0x00187de7,
1387 .tuner_type = TUNER_PHILIPS_FM1256_IH3,
1388 .radio_type = UNSET,
1389 .tuner_addr = ADDR_UNSET,
1390 .radio_addr = ADDR_UNSET,
1391 .tda9887_conf = TDA9887_PRESENT,
1392 .inputs = {{
1393 .name = name_tv,
1394 .vmux = 1,
1395 .amux = LINE2,
1396 .tv = 1,
1397 },{
1398 .name = name_comp1,
1399 .vmux = 0,
1400 .amux = LINE2,
1401 },{
1402 .name = name_comp2,
1403 .vmux = 3,
1404 .amux = LINE2,
1405 },{
1406 .name = name_svideo,
1407 .vmux = 8,
1408 .amux = LINE2,
1409 }},
1410 .radio = {
1411 .name = name_radio,
1412 .amux = LINE2,
1413 },
1414 .mute = {
1415 .name = name_mute,
1416 .amux = LINE1,
1417 },
1418 },
1419 [SAA7134_BOARD_AVERMEDIA_STUDIO_505] = {
1420 /* Vasiliy Temnikov <vaka@newmail.ru> */
1421 .name = "AverMedia AverTV Studio 505",
1422 .audio_clock = 0x00187de7,
1423 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
1424 .radio_type = UNSET,
1425 .tuner_addr = ADDR_UNSET,
1426 .radio_addr = ADDR_UNSET,
1427 .tda9887_conf = TDA9887_PRESENT,
1428 .inputs = { {
1429 .name = name_tv,
1430 .vmux = 1,
1431 .amux = LINE2,
1432 .tv = 1,
1433 }, {
1434 .name = name_comp1,
1435 .vmux = 0,
1436 .amux = LINE2,
1437 }, {
1438 .name = name_comp2,
1439 .vmux = 3,
1440 .amux = LINE2,
1441 },{
1442 .name = name_svideo,
1443 .vmux = 8,
1444 .amux = LINE2,
1445 } },
1446 .radio = {
1447 .name = name_radio,
1448 .amux = LINE2,
1449 },
1450 .mute = {
1451 .name = name_mute,
1452 .amux = LINE1,
1453 },
1454 },
1455 [SAA7134_BOARD_UPMOST_PURPLE_TV] = {
1456 .name = "UPMOST PURPLE TV",
1457 .audio_clock = 0x00187de7,
1458 .tuner_type = TUNER_PHILIPS_FM1236_MK3,
1459 .radio_type = UNSET,
1460 .tuner_addr = ADDR_UNSET,
1461 .radio_addr = ADDR_UNSET,
1462 .tda9887_conf = TDA9887_PRESENT,
1463 .inputs = {{
1464 .name = name_tv,
1465 .vmux = 7,
1466 .amux = TV,
1467 .tv = 1,
1468 },{
1469 .name = name_svideo,
1470 .vmux = 7,
1471 .amux = LINE1,
1472 }},
1473 },
1474 [SAA7134_BOARD_ITEMS_MTV005] = {
1475 /* Norman Jonas <normanjonas@arcor.de> */
1476 .name = "Items MuchTV Plus / IT-005",
1477 .audio_clock = 0x00187de7,
1478 .tuner_type = TUNER_PHILIPS_PAL,
1479 .radio_type = UNSET,
1480 .tuner_addr = ADDR_UNSET,
1481 .radio_addr = ADDR_UNSET,
1482 .inputs = {{
1483 .name = name_tv,
1484 .vmux = 3,
1485 .amux = TV,
1486 .tv = 1,
1487 },{
1488 .name = name_comp1,
1489 .vmux = 1,
1490 .amux = LINE1,
1491 },{
1492 .name = name_svideo,
1493 .vmux = 8,
1494 .amux = LINE1,
1495 }},
1496 .radio = {
1497 .name = name_radio,
1498 .amux = LINE2,
1499 },
1500 },
1501 [SAA7134_BOARD_CINERGY200] = {
1502 .name = "Terratec Cinergy 200 TV",
1503 .audio_clock = 0x00200000,
1504 .tuner_type = TUNER_PHILIPS_PAL,
1505 .radio_type = UNSET,
1506 .tuner_addr = ADDR_UNSET,
1507 .radio_addr = ADDR_UNSET,
1508 .inputs = {{
1509 .name = name_tv,
1510 .vmux = 1,
1511 .amux = LINE2,
1512 .tv = 1,
1513 },{
1514 .name = name_comp1,
1515 .vmux = 4,
1516 .amux = LINE1,
1517 },{
1518 .name = name_svideo,
1519 .vmux = 8,
1520 .amux = LINE1,
1521 },{
1522 .name = name_comp2, /* CVideo over SVideo Connector */
1523 .vmux = 0,
1524 .amux = LINE1,
1525 }},
1526 .mute = {
1527 .name = name_mute,
1528 .amux = LINE2,
1529 },
1530 },
1531 [SAA7134_BOARD_VIDEOMATE_TV_PVR] = {
1532 /* Alain St-Denis <alain@topaze.homeip.net> */
1533 .name = "Compro VideoMate TV PVR/FM",
1534 .audio_clock = 0x00187de7,
1535 .tuner_type = TUNER_PHILIPS_NTSC_M,
1536 .radio_type = UNSET,
1537 .tuner_addr = ADDR_UNSET,
1538 .radio_addr = ADDR_UNSET,
1539 .gpiomask = 0x808c0080,
1540 .inputs = {{
1541 .name = name_svideo,
1542 .vmux = 8,
1543 .amux = LINE1,
1544 .gpio = 0x00080,
1545 },{
1546 .name = name_comp1,
1547 .vmux = 3,
1548 .amux = LINE1,
1549 .gpio = 0x00080,
1550 },{
1551 .name = name_tv,
1552 .vmux = 1,
1553 .amux = LINE2_LEFT,
1554 .tv = 1,
1555 .gpio = 0x00080,
1556 }},
1557 .radio = {
1558 .name = name_radio,
1559 .amux = LINE2,
1560 .gpio = 0x80000,
1561 },
1562 .mute = {
1563 .name = name_mute,
1564 .amux = LINE2,
1565 .gpio = 0x40000,
1566 },
1567 },
1568 [SAA7134_BOARD_SABRENT_SBTTVFM] = {
1569 /* Michael Rodriguez-Torrent <mrtorrent@asu.edu> */
1570 .name = "Sabrent SBT-TVFM (saa7130)",
1571 .audio_clock = 0x00187de7,
1572 .tuner_type = TUNER_PHILIPS_NTSC_M,
1573 .radio_type = UNSET,
1574 .tuner_addr = ADDR_UNSET,
1575 .radio_addr = ADDR_UNSET,
1576 .inputs = {{
1577 .name = name_comp1,
1578 .vmux = 1,
1579 .amux = LINE1,
1580 },{
1581 .name = name_tv,
1582 .vmux = 3,
1583 .amux = LINE2,
1584 .tv = 1,
1585 },{
1586 .name = name_svideo,
1587 .vmux = 8,
1588 .amux = LINE1,
1589 }},
1590 .radio = {
1591 .name = name_radio,
1592 .amux = LINE2,
1593 },
1594 },
1595 [SAA7134_BOARD_ZOLID_XPERT_TV7134] = {
1596 /* Helge Jensen <helge.jensen@slog.dk> */
1597 .name = ":Zolid Xpert TV7134",
1598 .audio_clock = 0x00187de7,
1599 .tuner_type = TUNER_PHILIPS_NTSC,
1600 .radio_type = UNSET,
1601 .tuner_addr = ADDR_UNSET,
1602 .radio_addr = ADDR_UNSET,
1603 .inputs = {{
1604 .name = name_svideo,
1605 .vmux = 8,
1606 .amux = LINE1,
1607 },{
1608 .name = name_comp1,
1609 .vmux = 3,
1610 .amux = LINE1,
1611 },{
1612 .name = name_tv,
1613 .vmux = 1,
1614 .amux = LINE2,
1615 .tv = 1,
1616 }},
1617 },
1618 [SAA7134_BOARD_EMPIRE_PCI_TV_RADIO_LE] = {
1619 /* "Matteo Az" <matte.az@nospam.libero.it> ;-) */
1620 .name = "Empire PCI TV-Radio LE",
1621 .audio_clock = 0x00187de7,
1622 .tuner_type = TUNER_PHILIPS_PAL,
1623 .radio_type = UNSET,
1624 .tuner_addr = ADDR_UNSET,
1625 .radio_addr = ADDR_UNSET,
1626 .gpiomask = 0x4000,
1627 .inputs = {{
1628 .name = name_tv_mono,
1629 .vmux = 1,
1630 .amux = LINE2,
1631 .gpio = 0x8000,
1632 .tv = 1,
1633 },{
1634 .name = name_comp1,
1635 .vmux = 3,
1636 .amux = LINE1,
1637 .gpio = 0x8000,
1638 },{
1639 .name = name_svideo,
1640 .vmux = 6,
1641 .amux = LINE1,
1642 .gpio = 0x8000,
1643 }},
1644 .radio = {
1645 .name = name_radio,
1646 .amux = LINE1,
1647 .gpio = 0x8000,
1648 },
1649 .mute = {
1650 .name = name_mute,
1651 .amux = TV,
1652 .gpio =0x8000,
1653 }
1654 },
1655 [SAA7134_BOARD_AVERMEDIA_STUDIO_307] = {
1656 /*
1657 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
1658 Lots of thanks to Andrey Zolotarev <zolotarev_andrey@mail.ru>
1659 */
1660 .name = "Avermedia AVerTV Studio 307",
1661 .audio_clock = 0x00187de7,
1662 .tuner_type = TUNER_PHILIPS_FM1256_IH3,
1663 .radio_type = UNSET,
1664 .tuner_addr = ADDR_UNSET,
1665 .radio_addr = ADDR_UNSET,
1666 .tda9887_conf = TDA9887_PRESENT,
1667 .gpiomask = 0x03,
1668 .inputs = {{
1669 .name = name_tv,
1670 .vmux = 1,
1671 .amux = TV,
1672 .tv = 1,
1673 .gpio = 0x00,
1674 },{
1675 .name = name_comp,
1676 .vmux = 3,
1677 .amux = LINE1,
1678 .gpio = 0x02,
1679 },{
1680 .name = name_svideo,
1681 .vmux = 8,
1682 .amux = LINE1,
1683 .gpio = 0x02,
1684 }},
1685 .radio = {
1686 .name = name_radio,
1687 .amux = LINE1,
1688 .gpio = 0x01,
1689 },
1690 .mute = {
1691 .name = name_mute,
1692 .amux = LINE1,
1693 .gpio = 0x00,
1694 },
1695 },
1696 [SAA7134_BOARD_AVERMEDIA_GO_007_FM] = {
1697 .name = "Avermedia AVerTV GO 007 FM",
1698 .audio_clock = 0x00187de7,
1699 .tuner_type = TUNER_PHILIPS_TDA8290,
1700 .radio_type = UNSET,
1701 .tuner_addr = ADDR_UNSET,
1702 .radio_addr = ADDR_UNSET,
1703 .gpiomask = 0x00300003,
1704 /* .gpiomask = 0x8c240003, */
1705 .inputs = {{
1706 .name = name_tv,
1707 .vmux = 1,
1708 .amux = TV,
1709 .tv = 1,
1710 .gpio = 0x01,
1711 },{
1712 .name = name_comp1,
1713 .vmux = 0,
1714 .amux = LINE1,
1715 .gpio = 0x02,
1716 },{
1717 .name = name_svideo,
1718 .vmux = 6,
1719 .amux = LINE1,
1720 .gpio = 0x02,
1721 }},
1722 .radio = {
1723 .name = name_radio,
1724 .amux = TV,
1725 .gpio = 0x00300001,
1726 },
1727 .mute = {
1728 .name = name_mute,
1729 .amux = TV,
1730 .gpio = 0x01,
1731 },
1732 },
1733 [SAA7134_BOARD_AVERMEDIA_CARDBUS] = {
1734 /* Kees.Blom@cwi.nl */
1735 .name = "AVerMedia Cardbus TV/Radio (E500)",
1736 .audio_clock = 0x187de7,
1737 .tuner_type = TUNER_PHILIPS_TDA8290,
1738 .radio_type = UNSET,
1739 .tuner_addr = ADDR_UNSET,
1740 .radio_addr = ADDR_UNSET,
1741 .inputs = {{
1742 .name = name_tv,
1743 .vmux = 1,
1744 .amux = TV,
1745 .tv = 1,
1746 },{
1747 .name = name_comp1,
1748 .vmux = 3,
1749 .amux = LINE2,
1750 },{
1751 .name = name_svideo,
1752 .vmux = 8,
1753 .amux = LINE1,
1754 }},
1755 .radio = {
1756 .name = name_radio,
1757 .amux = LINE1,
1758 },
1759 },
1760 [SAA7134_BOARD_AVERMEDIA_CARDBUS_501] = {
1761 /* Oldrich Jedlicka <oldium.pro@seznam.cz> */
1762 .name = "AVerMedia Cardbus TV/Radio (E501R)",
1763 .audio_clock = 0x187de7,
1764 .tuner_type = TUNER_ALPS_TSBE5_PAL,
1765 .radio_type = TUNER_TEA5767,
1766 .tuner_addr = 0x61,
1767 .radio_addr = 0x60,
1768 .tda9887_conf = TDA9887_PRESENT,
1769 .gpiomask = 0x08000000,
1770 .inputs = { {
1771 .name = name_tv,
1772 .vmux = 1,
1773 .amux = TV,
1774 .tv = 1,
1775 .gpio = 0x08000000,
1776 }, {
1777 .name = name_comp1,
1778 .vmux = 3,
1779 .amux = LINE1,
1780 .gpio = 0x08000000,
1781 }, {
1782 .name = name_svideo,
1783 .vmux = 8,
1784 .amux = LINE1,
1785 .gpio = 0x08000000,
1786 } },
1787 .radio = {
1788 .name = name_radio,
1789 .amux = LINE2,
1790 .gpio = 0x00000000,
1791 },
1792 },
1793 [SAA7134_BOARD_CINERGY400_CARDBUS] = {
1794 .name = "Terratec Cinergy 400 mobile",
1795 .audio_clock = 0x187de7,
1796 .tuner_type = TUNER_ALPS_TSBE5_PAL,
1797 .radio_type = UNSET,
1798 .tuner_addr = ADDR_UNSET,
1799 .radio_addr = ADDR_UNSET,
1800 .tda9887_conf = TDA9887_PRESENT,
1801 .inputs = {{
1802 .name = name_tv,
1803 .vmux = 1,
1804 .amux = TV,
1805 .tv = 1,
1806 },{
1807 .name = name_tv_mono,
1808 .vmux = 1,
1809 .amux = LINE2,
1810 .tv = 1,
1811 },{
1812 .name = name_comp1,
1813 .vmux = 3,
1814 .amux = LINE1,
1815 },{
1816 .name = name_svideo,
1817 .vmux = 8,
1818 .amux = LINE1,
1819 }},
1820 },
1821 [SAA7134_BOARD_CINERGY600_MK3] = {
1822 .name = "Terratec Cinergy 600 TV MK3",
1823 .audio_clock = 0x00200000,
1824 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
1825 .radio_type = UNSET,
1826 .tuner_addr = ADDR_UNSET,
1827 .radio_addr = ADDR_UNSET,
1828 .rds_addr = 0x10,
1829 .tda9887_conf = TDA9887_PRESENT,
1830 .inputs = {{
1831 .name = name_tv,
1832 .vmux = 1,
1833 .amux = TV,
1834 .tv = 1,
1835 },{
1836 .name = name_comp1,
1837 .vmux = 4,
1838 .amux = LINE1,
1839 },{
1840 .name = name_svideo,
1841 .vmux = 8,
1842 .amux = LINE1,
1843 },{
1844 .name = name_comp2, /* CVideo over SVideo Connector */
1845 .vmux = 0,
1846 .amux = LINE1,
1847 }},
1848 .radio = {
1849 .name = name_radio,
1850 .amux = LINE2,
1851 },
1852 },
1853 [SAA7134_BOARD_VIDEOMATE_GOLD_PLUS] = {
1854 /* Dylan Walkden <dylan_walkden@hotmail.com> */
1855 .name = "Compro VideoMate Gold+ Pal",
1856 .audio_clock = 0x00187de7,
1857 .tuner_type = TUNER_PHILIPS_PAL,
1858 .radio_type = UNSET,
1859 .tuner_addr = ADDR_UNSET,
1860 .radio_addr = ADDR_UNSET,
1861 .gpiomask = 0x1ce780,
1862 .inputs = {{
1863 .name = name_svideo,
1864 .vmux = 0, /* CVideo over SVideo Connector - ok? */
1865 .amux = LINE1,
1866 .gpio = 0x008080,
1867 },{
1868 .name = name_comp1,
1869 .vmux = 3,
1870 .amux = LINE1,
1871 .gpio = 0x008080,
1872 },{
1873 .name = name_tv,
1874 .vmux = 1,
1875 .amux = TV,
1876 .tv = 1,
1877 .gpio = 0x008080,
1878 }},
1879 .radio = {
1880 .name = name_radio,
1881 .amux = LINE2,
1882 .gpio = 0x80000,
1883 },
1884 .mute = {
1885 .name = name_mute,
1886 .amux = LINE2,
1887 .gpio = 0x0c8000,
1888 },
1889 },
1890 [SAA7134_BOARD_PINNACLE_300I_DVBT_PAL] = {
1891 .name = "Pinnacle PCTV 300i DVB-T + PAL",
1892 .audio_clock = 0x00187de7,
1893 .tuner_type = TUNER_MT2032,
1894 .radio_type = UNSET,
1895 .tuner_addr = ADDR_UNSET,
1896 .radio_addr = ADDR_UNSET,
1897 .tda9887_conf = TDA9887_PRESENT | TDA9887_INTERCARRIER | TDA9887_PORT2_INACTIVE,
1898 .mpeg = SAA7134_MPEG_DVB,
1899 .inputs = {{
1900 .name = name_tv,
1901 .vmux = 3,
1902 .amux = TV,
1903 .tv = 1,
1904 },{
1905 .name = name_comp1,
1906 .vmux = 0,
1907 .amux = LINE2,
1908 },{
1909 .name = name_comp2,
1910 .vmux = 1,
1911 .amux = LINE2,
1912 },{
1913 .name = name_svideo,
1914 .vmux = 8,
1915 .amux = LINE2,
1916 }},
1917 },
1918 [SAA7134_BOARD_PROVIDEO_PV952] = {
1919 /* andreas.kretschmer@web.de */
1920 .name = "ProVideo PV952",
1921 .audio_clock = 0x00187de7,
1922 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
1923 .radio_type = UNSET,
1924 .tuner_addr = ADDR_UNSET,
1925 .radio_addr = ADDR_UNSET,
1926 .tda9887_conf = TDA9887_PRESENT,
1927 .inputs = {{
1928 .name = name_comp1,
1929 .vmux = 0,
1930 .amux = LINE1,
1931 },{
1932 .name = name_tv,
1933 .vmux = 1,
1934 .amux = TV,
1935 .tv = 1,
1936 },{
1937 .name = name_tv_mono,
1938 .vmux = 1,
1939 .amux = LINE2,
1940 .tv = 1,
1941 }},
1942 .radio = {
1943 .name = name_radio,
1944 .amux = LINE2,
1945 },
1946 },
1947 [SAA7134_BOARD_AVERMEDIA_305] = {
1948 /* much like the "studio" version but without radio
1949 * and another tuner (sirspiritus@yandex.ru) */
1950 .name = "AverMedia AverTV/305",
1951 .audio_clock = 0x00187de7,
1952 .tuner_type = TUNER_PHILIPS_FQ1216ME,
1953 .radio_type = UNSET,
1954 .tuner_addr = ADDR_UNSET,
1955 .radio_addr = ADDR_UNSET,
1956 .tda9887_conf = TDA9887_PRESENT,
1957 .inputs = {{
1958 .name = name_tv,
1959 .vmux = 1,
1960 .amux = LINE2,
1961 .tv = 1,
1962 },{
1963 .name = name_comp1,
1964 .vmux = 0,
1965 .amux = LINE2,
1966 },{
1967 .name = name_comp2,
1968 .vmux = 3,
1969 .amux = LINE2,
1970 },{
1971 .name = name_svideo,
1972 .vmux = 8,
1973 .amux = LINE2,
1974 }},
1975 .mute = {
1976 .name = name_mute,
1977 .amux = LINE1,
1978 },
1979 },
1980 [SAA7134_BOARD_FLYDVBTDUO] = {
1981 /* LifeView FlyDVB-T DUO */
1982 /* "Nico Sabbi <nsabbi@tiscali.it> Hartmut Hackmann hartmut.hackmann@t-online.de*/
1983 .name = "LifeView FlyDVB-T DUO / MSI TV@nywhere Duo",
1984 .audio_clock = 0x00200000,
1985 .tuner_type = TUNER_PHILIPS_TDA8290,
1986 .radio_type = UNSET,
1987 .tuner_addr = ADDR_UNSET,
1988 .radio_addr = ADDR_UNSET,
1989 .gpiomask = 0x00200000,
1990 .mpeg = SAA7134_MPEG_DVB,
1991 .inputs = {{
1992 .name = name_tv,
1993 .vmux = 1,
1994 .amux = TV,
1995 .gpio = 0x200000, /* GPIO21=High for TV input */
1996 .tv = 1,
1997 },{
1998 .name = name_comp1, /* Composite signal on S-Video input */
1999 .vmux = 0,
2000 .amux = LINE2,
2001 },{
2002 .name = name_comp2, /* Composite input */
2003 .vmux = 3,
2004 .amux = LINE2,
2005 },{
2006 .name = name_svideo, /* S-Video signal on S-Video input */
2007 .vmux = 8,
2008 .amux = LINE2,
2009 }},
2010 .radio = {
2011 .name = name_radio,
2012 .amux = TV,
2013 .gpio = 0x000000, /* GPIO21=Low for FM radio antenna */
2014 },
2015 },
2016 [SAA7134_BOARD_PHILIPS_TOUGH] = {
2017 .name = "Philips TOUGH DVB-T reference design",
2018 .tuner_type = TUNER_ABSENT,
2019 .audio_clock = 0x00187de7,
2020 .radio_type = UNSET,
2021 .tuner_addr = ADDR_UNSET,
2022 .radio_addr = ADDR_UNSET,
2023 .mpeg = SAA7134_MPEG_DVB,
2024 .inputs = {{
2025 .name = name_comp1,
2026 .vmux = 0,
2027 .amux = LINE1,
2028 },{
2029 .name = name_svideo,
2030 .vmux = 8,
2031 .amux = LINE1,
2032 }},
2033 },
2034 [SAA7134_BOARD_AVERMEDIA_307] = {
2035 /*
2036 Davydov Vladimir <vladimir@iqmedia.com>
2037 */
2038 .name = "Avermedia AVerTV 307",
2039 .audio_clock = 0x00187de7,
2040 .tuner_type = TUNER_PHILIPS_FQ1216ME,
2041 .radio_type = UNSET,
2042 .tuner_addr = ADDR_UNSET,
2043 .radio_addr = ADDR_UNSET,
2044 .tda9887_conf = TDA9887_PRESENT,
2045 .inputs = {{
2046 .name = name_tv,
2047 .vmux = 1,
2048 .amux = TV,
2049 .tv = 1,
2050 },{
2051 .name = name_comp1,
2052 .vmux = 0,
2053 .amux = LINE1,
2054 },{
2055 .name = name_comp2,
2056 .vmux = 3,
2057 .amux = LINE1,
2058 },{
2059 .name = name_svideo,
2060 .vmux = 8,
2061 .amux = LINE1,
2062 }},
2063 },
2064 [SAA7134_BOARD_ADS_INSTANT_TV] = {
2065 .name = "ADS Tech Instant TV (saa7135)",
2066 .audio_clock = 0x00187de7,
2067 .tuner_type = TUNER_PHILIPS_TDA8290,
2068 .radio_type = UNSET,
2069 .tuner_addr = ADDR_UNSET,
2070 .radio_addr = ADDR_UNSET,
2071 .inputs = {{
2072 .name = name_tv,
2073 .vmux = 1,
2074 .amux = TV,
2075 .tv = 1,
2076 },{
2077 .name = name_comp1,
2078 .vmux = 3,
2079 .amux = LINE2,
2080 },{
2081 .name = name_svideo,
2082 .vmux = 8,
2083 .amux = LINE2,
2084 }},
2085 },
2086 [SAA7134_BOARD_KWORLD_VSTREAM_XPERT] = {
2087 .name = "Kworld/Tevion V-Stream Xpert TV PVR7134",
2088 .audio_clock = 0x00187de7,
2089 .tuner_type = TUNER_PHILIPS_PAL_I,
2090 .radio_type = UNSET,
2091 .tuner_addr = ADDR_UNSET,
2092 .radio_addr = ADDR_UNSET,
2093 .gpiomask = 0x0700,
2094 .inputs = {{
2095 .name = name_tv,
2096 .vmux = 1,
2097 .amux = TV,
2098 .tv = 1,
2099 .gpio = 0x000,
2100 },{
2101 .name = name_comp1,
2102 .vmux = 3,
2103 .amux = LINE1,
2104 .gpio = 0x200, /* gpio by DScaler */
2105 },{
2106 .name = name_svideo,
2107 .vmux = 0,
2108 .amux = LINE1,
2109 .gpio = 0x200,
2110 }},
2111 .radio = {
2112 .name = name_radio,
2113 .amux = LINE1,
2114 .gpio = 0x100,
2115 },
2116 .mute = {
2117 .name = name_mute,
2118 .amux = TV,
2119 .gpio = 0x000,
2120 },
2121 },
2122 [SAA7134_BOARD_FLYDVBT_DUO_CARDBUS] = {
2123 .name = "LifeView/Typhoon/Genius FlyDVB-T Duo Cardbus",
2124 .audio_clock = 0x00200000,
2125 .tuner_type = TUNER_PHILIPS_TDA8290,
2126 .radio_type = UNSET,
2127 .tuner_addr = ADDR_UNSET,
2128 .radio_addr = ADDR_UNSET,
2129 .mpeg = SAA7134_MPEG_DVB,
2130 .gpiomask = 0x00200000,
2131 .inputs = {{
2132 .name = name_tv,
2133 .vmux = 1,
2134 .amux = TV,
2135 .gpio = 0x200000, /* GPIO21=High for TV input */
2136 .tv = 1,
2137 },{
2138 .name = name_svideo, /* S-Video signal on S-Video input */
2139 .vmux = 8,
2140 .amux = LINE2,
2141 },{
2142 .name = name_comp1, /* Composite signal on S-Video input */
2143 .vmux = 0,
2144 .amux = LINE2,
2145 },{
2146 .name = name_comp2, /* Composite input */
2147 .vmux = 3,
2148 .amux = LINE2,
2149 }},
2150 .radio = {
2151 .name = name_radio,
2152 .amux = TV,
2153 .gpio = 0x000000, /* GPIO21=Low for FM radio antenna */
2154 },
2155 },
2156 [SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII] = {
2157 .name = "Compro VideoMate TV Gold+II",
2158 .audio_clock = 0x002187de7,
2159 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
2160 .radio_type = TUNER_TEA5767,
2161 .tuner_addr = 0x63,
2162 .radio_addr = 0x60,
2163 .gpiomask = 0x8c1880,
2164 .inputs = {{
2165 .name = name_svideo,
2166 .vmux = 0,
2167 .amux = LINE1,
2168 .gpio = 0x800800,
2169 },{
2170 .name = name_comp1,
2171 .vmux = 3,
2172 .amux = LINE1,
2173 .gpio = 0x801000,
2174 },{
2175 .name = name_tv,
2176 .vmux = 1,
2177 .amux = TV,
2178 .tv = 1,
2179 .gpio = 0x800000,
2180 }},
2181 .radio = {
2182 .name = name_radio,
2183 .amux = TV,
2184 .gpio = 0x880000,
2185 },
2186 .mute = {
2187 .name = name_mute,
2188 .amux = LINE2,
2189 .gpio = 0x840000,
2190 },
2191 },
2192 [SAA7134_BOARD_KWORLD_XPERT] = {
2193 /*
2194 FIXME:
2195 - Remote control doesn't initialize properly.
2196 - Audio volume starts muted,
2197 then gradually increases after channel change.
2198 - Overlay scaling problems (application error?)
2199 - Composite S-Video untested.
2200 From: Konrad Rzepecki <hannibal@megapolis.pl>
2201 */
2202 .name = "Kworld Xpert TV PVR7134",
2203 .audio_clock = 0x00187de7,
2204 .tuner_type = TUNER_TENA_9533_DI,
2205 .radio_type = TUNER_TEA5767,
2206 .tuner_addr = 0x61,
2207 .radio_addr = 0x60,
2208 .gpiomask = 0x0700,
2209 .inputs = {{
2210 .name = name_tv,
2211 .vmux = 1,
2212 .amux = TV,
2213 .tv = 1,
2214 .gpio = 0x000,
2215 },{
2216 .name = name_comp1,
2217 .vmux = 3,
2218 .amux = LINE1,
2219 .gpio = 0x200, /* gpio by DScaler */
2220 },{
2221 .name = name_svideo,
2222 .vmux = 0,
2223 .amux = LINE1,
2224 .gpio = 0x200,
2225 }},
2226 .radio = {
2227 .name = name_radio,
2228 .amux = LINE1,
2229 .gpio = 0x100,
2230 },
2231 .mute = {
2232 .name = name_mute,
2233 .amux = TV,
2234 .gpio = 0x000,
2235 },
2236 },
2237 [SAA7134_BOARD_FLYTV_DIGIMATRIX] = {
2238 .name = "FlyTV mini Asus Digimatrix",
2239 .audio_clock = 0x00200000,
2240 .tuner_type = TUNER_LG_TALN,
2241 .radio_type = UNSET,
2242 .tuner_addr = ADDR_UNSET,
2243 .radio_addr = ADDR_UNSET,
2244 .inputs = {{
2245 .name = name_tv,
2246 .vmux = 1,
2247 .amux = TV,
2248 .tv = 1,
2249 },{
2250 .name = name_tv_mono,
2251 .vmux = 1,
2252 .amux = LINE2,
2253 .tv = 1,
2254 },{
2255 .name = name_comp1,
2256 .vmux = 0,
2257 .amux = LINE2,
2258 },{
2259 .name = name_comp2,
2260 .vmux = 3,
2261 .amux = LINE2,
2262 },{
2263 .name = name_svideo,
2264 .vmux = 8,
2265 .amux = LINE2,
2266 }},
2267 .radio = {
2268 .name = name_radio, /* radio unconfirmed */
2269 .amux = LINE2,
2270 },
2271 },
2272 [SAA7134_BOARD_KWORLD_TERMINATOR] = {
2273 /* Kworld V-Stream Studio TV Terminator */
2274 /* "James Webb <jrwebb@qwest.net> */
2275 .name = "V-Stream Studio TV Terminator",
2276 .audio_clock = 0x00187de7,
2277 .tuner_type = TUNER_PHILIPS_TDA8290,
2278 .radio_type = UNSET,
2279 .tuner_addr = ADDR_UNSET,
2280 .radio_addr = ADDR_UNSET,
2281 .gpiomask = 1 << 21,
2282 .inputs = {{
2283 .name = name_tv,
2284 .vmux = 1,
2285 .amux = TV,
2286 .gpio = 0x0000000,
2287 .tv = 1,
2288 },{
2289 .name = name_comp1, /* Composite input */
2290 .vmux = 3,
2291 .amux = LINE2,
2292 .gpio = 0x0000000,
2293 },{
2294 .name = name_svideo, /* S-Video input */
2295 .vmux = 8,
2296 .amux = LINE2,
2297 .gpio = 0x0000000,
2298 }},
2299 .radio = {
2300 .name = name_radio,
2301 .amux = TV,
2302 .gpio = 0x0200000,
2303 },
2304 },
2305 [SAA7134_BOARD_YUAN_TUN900] = {
2306 /* FIXME:
2307 * S-Video and composite sources untested.
2308 * Radio not working.
2309 * Remote control not yet implemented.
2310 * From : codemaster@webgeeks.be */
2311 .name = "Yuan TUN-900 (saa7135)",
2312 .audio_clock = 0x00187de7,
2313 .tuner_type = TUNER_PHILIPS_TDA8290,
2314 .radio_type = UNSET,
2315 .tuner_addr= ADDR_UNSET,
2316 .radio_addr= ADDR_UNSET,
2317 .gpiomask = 0x00010003,
2318 .inputs = {{
2319 .name = name_tv,
2320 .vmux = 1,
2321 .amux = TV,
2322 .tv = 1,
2323 .gpio = 0x01,
2324 },{
2325 .name = name_comp1,
2326 .vmux = 0,
2327 .amux = LINE2,
2328 .gpio = 0x02,
2329 },{
2330 .name = name_svideo,
2331 .vmux = 6,
2332 .amux = LINE2,
2333 .gpio = 0x02,
2334 }},
2335 .radio = {
2336 .name = name_radio,
2337 .amux = LINE1,
2338 .gpio = 0x00010003,
2339 },
2340 .mute = {
2341 .name = name_mute,
2342 .amux = TV,
2343 .gpio = 0x01,
2344 },
2345 },
2346 [SAA7134_BOARD_BEHOLD_409FM] = {
2347 /* <http://tuner.beholder.ru>, Sergey <skiv@orel.ru> */
2348 /* Beholder Intl. Ltd. 2008 */
2349 /*Dmitry Belimov <d.belimov@gmail.com> */
2350 .name = "Beholder BeholdTV 409 FM",
2351 .audio_clock = 0x00187de7,
2352 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
2353 .radio_type = UNSET,
2354 .tuner_addr = ADDR_UNSET,
2355 .radio_addr = ADDR_UNSET,
2356 .tda9887_conf = TDA9887_PRESENT,
2357 .gpiomask = 0x00008000,
2358 .inputs = {{
2359 .name = name_tv,
2360 .vmux = 3,
2361 .amux = TV,
2362 .tv = 1,
2363 },{
2364 .name = name_comp1,
2365 .vmux = 1,
2366 .amux = LINE1,
2367 },{
2368 .name = name_svideo,
2369 .vmux = 8,
2370 .amux = LINE1,
2371 }},
2372 .radio = {
2373 .name = name_radio,
2374 .amux = LINE2,
2375 },
2376 },
2377 [SAA7134_BOARD_GOTVIEW_7135] = {
2378 /* Mike Baikov <mike@baikov.com> */
2379 /* Andrey Cvetcov <ays14@yandex.ru> */
2380 .name = "GoTView 7135 PCI",
2381 .audio_clock = 0x00187de7,
2382 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
2383 .radio_type = UNSET,
2384 .tuner_addr = ADDR_UNSET,
2385 .radio_addr = ADDR_UNSET,
2386 .tda9887_conf = TDA9887_PRESENT,
2387 .gpiomask = 0x00200003,
2388 .inputs = {{
2389 .name = name_tv,
2390 .vmux = 1,
2391 .amux = TV,
2392 .tv = 1,
2393 .gpio = 0x00200003,
2394 },{
2395 .name = name_tv_mono,
2396 .vmux = 1,
2397 .amux = LINE2,
2398 .gpio = 0x00200003,
2399 },{
2400 .name = name_comp1,
2401 .vmux = 3,
2402 .amux = LINE1,
2403 .gpio = 0x00200003,
2404 },{
2405 .name = name_svideo,
2406 .vmux = 8,
2407 .amux = LINE1,
2408 .gpio = 0x00200003,
2409 }},
2410 .radio = {
2411 .name = name_radio,
2412 .amux = LINE2,
2413 .gpio = 0x00200003,
2414 },
2415 .mute = {
2416 .name = name_mute,
2417 .amux = TV,
2418 .gpio = 0x00200003,
2419 },
2420 },
2421 [SAA7134_BOARD_PHILIPS_EUROPA] = {
2422 .name = "Philips EUROPA V3 reference design",
2423 .audio_clock = 0x00187de7,
2424 .tuner_type = TUNER_PHILIPS_TD1316,
2425 .radio_type = UNSET,
2426 .tuner_addr = 0x61,
2427 .radio_addr = ADDR_UNSET,
2428 .tda9887_conf = TDA9887_PRESENT | TDA9887_PORT1_ACTIVE,
2429 .mpeg = SAA7134_MPEG_DVB,
2430 .inputs = {{
2431 .name = name_tv,
2432 .vmux = 3,
2433 .amux = TV,
2434 .tv = 1,
2435 },{
2436 .name = name_comp1,
2437 .vmux = 0,
2438 .amux = LINE2,
2439 },{
2440 .name = name_svideo,
2441 .vmux = 8,
2442 .amux = LINE2,
2443 }},
2444 },
2445 [SAA7134_BOARD_VIDEOMATE_DVBT_300] = {
2446 .name = "Compro Videomate DVB-T300",
2447 .audio_clock = 0x00187de7,
2448 .tuner_type = TUNER_PHILIPS_TD1316,
2449 .radio_type = UNSET,
2450 .tuner_addr = 0x61,
2451 .radio_addr = ADDR_UNSET,
2452 .tda9887_conf = TDA9887_PRESENT | TDA9887_PORT1_ACTIVE,
2453 .mpeg = SAA7134_MPEG_DVB,
2454 .inputs = {{
2455 .name = name_tv,
2456 .vmux = 3,
2457 .amux = TV,
2458 .tv = 1,
2459 },{
2460 .name = name_comp1,
2461 .vmux = 1,
2462 .amux = LINE2,
2463 },{
2464 .name = name_svideo,
2465 .vmux = 8,
2466 .amux = LINE2,
2467 }},
2468 },
2469 [SAA7134_BOARD_VIDEOMATE_DVBT_200] = {
2470 .name = "Compro Videomate DVB-T200",
2471 .tuner_type = TUNER_ABSENT,
2472 .audio_clock = 0x00187de7,
2473 .radio_type = UNSET,
2474 .tuner_addr = ADDR_UNSET,
2475 .radio_addr = ADDR_UNSET,
2476 .mpeg = SAA7134_MPEG_DVB,
2477 .inputs = {{
2478 .name = name_comp1,
2479 .vmux = 0,
2480 .amux = LINE1,
2481 },{
2482 .name = name_svideo,
2483 .vmux = 8,
2484 .amux = LINE1,
2485 }},
2486 },
2487 [SAA7134_BOARD_RTD_VFG7350] = {
2488 .name = "RTD Embedded Technologies VFG7350",
2489 .audio_clock = 0x00200000,
2490 .tuner_type = TUNER_ABSENT,
2491 .radio_type = UNSET,
2492 .tuner_addr = ADDR_UNSET,
2493 .radio_addr = ADDR_UNSET,
2494 .empress_addr = 0x21,
2495 .inputs = {{
2496 .name = "Composite 0",
2497 .vmux = 0,
2498 .amux = LINE1,
2499 },{
2500 .name = "Composite 1",
2501 .vmux = 1,
2502 .amux = LINE2,
2503 },{
2504 .name = "Composite 2",
2505 .vmux = 2,
2506 .amux = LINE1,
2507 },{
2508 .name = "Composite 3",
2509 .vmux = 3,
2510 .amux = LINE2,
2511 },{
2512 .name = "S-Video 0",
2513 .vmux = 8,
2514 .amux = LINE1,
2515 },{
2516 .name = "S-Video 1",
2517 .vmux = 9,
2518 .amux = LINE2,
2519 }},
2520 .mpeg = SAA7134_MPEG_EMPRESS,
2521 .video_out = CCIR656,
2522 .vid_port_opts = ( SET_T_CODE_POLARITY_NON_INVERTED |
2523 SET_CLOCK_NOT_DELAYED |
2524 SET_CLOCK_INVERTED |
2525 SET_VSYNC_OFF ),
2526 },
2527 [SAA7134_BOARD_RTD_VFG7330] = {
2528 .name = "RTD Embedded Technologies VFG7330",
2529 .audio_clock = 0x00200000,
2530 .tuner_type = TUNER_ABSENT,
2531 .radio_type = UNSET,
2532 .tuner_addr = ADDR_UNSET,
2533 .radio_addr = ADDR_UNSET,
2534 .inputs = {{
2535 .name = "Composite 0",
2536 .vmux = 0,
2537 .amux = LINE1,
2538 },{
2539 .name = "Composite 1",
2540 .vmux = 1,
2541 .amux = LINE2,
2542 },{
2543 .name = "Composite 2",
2544 .vmux = 2,
2545 .amux = LINE1,
2546 },{
2547 .name = "Composite 3",
2548 .vmux = 3,
2549 .amux = LINE2,
2550 },{
2551 .name = "S-Video 0",
2552 .vmux = 8,
2553 .amux = LINE1,
2554 },{
2555 .name = "S-Video 1",
2556 .vmux = 9,
2557 .amux = LINE2,
2558 }},
2559 },
2560 [SAA7134_BOARD_FLYTVPLATINUM_MINI2] = {
2561 .name = "LifeView FlyTV Platinum Mini2",
2562 .audio_clock = 0x00200000,
2563 .tuner_type = TUNER_PHILIPS_TDA8290,
2564 .radio_type = UNSET,
2565 .tuner_addr = ADDR_UNSET,
2566 .radio_addr = ADDR_UNSET,
2567
2568 .inputs = {{
2569 .name = name_tv,
2570 .vmux = 1,
2571 .amux = TV,
2572 .tv = 1,
2573 },{
2574 .name = name_comp1, /* Composite signal on S-Video input */
2575 .vmux = 0,
2576 .amux = LINE2,
2577 },{
2578 .name = name_comp2, /* Composite input */
2579 .vmux = 3,
2580 .amux = LINE2,
2581 },{
2582 .name = name_svideo,
2583 .vmux = 8,
2584 .amux = LINE2,
2585 }},
2586 },
2587 [SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180] = {
2588 /* Michael Krufky <mkrufky@m1k.net>
2589 * Uses Alps Electric TDHU2, containing NXT2004 ATSC Decoder
2590 * AFAIK, there is no analog demod, thus,
2591 * no support for analog television.
2592 */
2593 .name = "AVerMedia AVerTVHD MCE A180",
2594 .audio_clock = 0x00187de7,
2595 .tuner_type = TUNER_ABSENT,
2596 .radio_type = UNSET,
2597 .tuner_addr = ADDR_UNSET,
2598 .radio_addr = ADDR_UNSET,
2599 .mpeg = SAA7134_MPEG_DVB,
2600 .inputs = {{
2601 .name = name_comp1,
2602 .vmux = 3,
2603 .amux = LINE2,
2604 },{
2605 .name = name_svideo,
2606 .vmux = 8,
2607 .amux = LINE2,
2608 }},
2609 },
2610 [SAA7134_BOARD_MONSTERTV_MOBILE] = {
2611 .name = "SKNet MonsterTV Mobile",
2612 .audio_clock = 0x00187de7,
2613 .tuner_type = TUNER_PHILIPS_TDA8290,
2614 .radio_type = UNSET,
2615 .tuner_addr = ADDR_UNSET,
2616 .radio_addr = ADDR_UNSET,
2617
2618 .inputs = {{
2619 .name = name_tv,
2620 .vmux = 1,
2621 .amux = TV,
2622 .tv = 1,
2623 },{
2624 .name = name_comp1,
2625 .vmux = 3,
2626 .amux = LINE1,
2627 },{
2628 .name = name_svideo,
2629 .vmux = 6,
2630 .amux = LINE1,
2631 }},
2632 },
2633 [SAA7134_BOARD_PINNACLE_PCTV_110i] = {
2634 .name = "Pinnacle PCTV 40i/50i/110i (saa7133)",
2635 .audio_clock = 0x00187de7,
2636 .tuner_type = TUNER_PHILIPS_TDA8290,
2637 .radio_type = UNSET,
2638 .tuner_addr = ADDR_UNSET,
2639 .radio_addr = ADDR_UNSET,
2640 .gpiomask = 0x080200000,
2641 .inputs = { {
2642 .name = name_tv,
2643 .vmux = 4,
2644 .amux = TV,
2645 .tv = 1,
2646 }, {
2647 .name = name_comp1,
2648 .vmux = 1,
2649 .amux = LINE2,
2650 }, {
2651 .name = name_comp2,
2652 .vmux = 0,
2653 .amux = LINE2,
2654 }, {
2655 .name = name_svideo,
2656 .vmux = 8,
2657 .amux = LINE2,
2658 } },
2659 .radio = {
2660 .name = name_radio,
2661 .amux = TV,
2662 .gpio = 0x0200000,
2663 },
2664 },
2665 [SAA7134_BOARD_ASUSTeK_P7131_DUAL] = {
2666 .name = "ASUSTeK P7131 Dual",
2667 .audio_clock = 0x00187de7,
2668 .tuner_type = TUNER_PHILIPS_TDA8290,
2669 .radio_type = UNSET,
2670 .tuner_addr = ADDR_UNSET,
2671 .radio_addr = ADDR_UNSET,
2672 .gpiomask = 1 << 21,
2673 .mpeg = SAA7134_MPEG_DVB,
2674 .inputs = {{
2675 .name = name_tv,
2676 .vmux = 1,
2677 .amux = TV,
2678 .tv = 1,
2679 .gpio = 0x0000000,
2680 },{
2681 .name = name_comp1,
2682 .vmux = 3,
2683 .amux = LINE2,
2684 .gpio = 0x0200000,
2685 },{
2686 .name = name_comp2,
2687 .vmux = 0,
2688 .amux = LINE2,
2689 .gpio = 0x0200000,
2690 },{
2691 .name = name_svideo,
2692 .vmux = 8,
2693 .amux = LINE2,
2694 .gpio = 0x0200000,
2695 }},
2696 .radio = {
2697 .name = name_radio,
2698 .amux = TV,
2699 .gpio = 0x0200000,
2700 },
2701 },
2702 [SAA7134_BOARD_SEDNA_PC_TV_CARDBUS] = {
2703 /* Paul Tom Zalac <pzalac@gmail.com> */
2704 /* Pavel Mihaylov <bin@bash.info> */
2705 .name = "Sedna/MuchTV PC TV Cardbus TV/Radio (ITO25 Rev:2B)",
2706 /* Sedna/MuchTV (OEM) Cardbus TV Tuner */
2707 .audio_clock = 0x00187de7,
2708 .tuner_type = TUNER_PHILIPS_TDA8290,
2709 .radio_type = UNSET,
2710 .tuner_addr = ADDR_UNSET,
2711 .radio_addr = ADDR_UNSET,
2712 .gpiomask = 0xe880c0,
2713 .inputs = {{
2714 .name = name_tv,
2715 .vmux = 3,
2716 .amux = TV,
2717 .tv = 1,
2718 },{
2719 .name = name_comp1,
2720 .vmux = 1,
2721 .amux = LINE1,
2722 },{
2723 .name = name_svideo,
2724 .vmux = 6,
2725 .amux = LINE1,
2726 }},
2727 .radio = {
2728 .name = name_radio,
2729 .amux = LINE2,
2730 },
2731 },
2732 [SAA7134_BOARD_ASUSTEK_DIGIMATRIX_TV] = {
2733 /* "Cyril Lacoux (Yack)" <clacoux@ifeelgood.org> */
2734 .name = "ASUS Digimatrix TV",
2735 .audio_clock = 0x00200000,
2736 .tuner_type = TUNER_PHILIPS_FQ1216ME,
2737 .tda9887_conf = TDA9887_PRESENT,
2738 .radio_type = UNSET,
2739 .tuner_addr = ADDR_UNSET,
2740 .radio_addr = ADDR_UNSET,
2741 .inputs = {{
2742 .name = name_tv,
2743 .vmux = 1,
2744 .amux = TV,
2745 .tv = 1,
2746 },{
2747 .name = name_comp1,
2748 .vmux = 3,
2749 .amux = LINE1,
2750 },{
2751 .name = name_svideo,
2752 .vmux = 8,
2753 .amux = LINE1,
2754 }},
2755 },
2756 [SAA7134_BOARD_PHILIPS_TIGER] = {
2757 .name = "Philips Tiger reference design",
2758 .audio_clock = 0x00187de7,
2759 .tuner_type = TUNER_PHILIPS_TDA8290,
2760 .radio_type = UNSET,
2761 .tuner_addr = ADDR_UNSET,
2762 .radio_addr = ADDR_UNSET,
2763 .tuner_config = 0,
2764 .mpeg = SAA7134_MPEG_DVB,
2765 .gpiomask = 0x0200000,
2766 .inputs = {{
2767 .name = name_tv,
2768 .vmux = 1,
2769 .amux = TV,
2770 .tv = 1,
2771 },{
2772 .name = name_comp1,
2773 .vmux = 3,
2774 .amux = LINE1,
2775 },{
2776 .name = name_svideo,
2777 .vmux = 8,
2778 .amux = LINE1,
2779 }},
2780 .radio = {
2781 .name = name_radio,
2782 .amux = TV,
2783 .gpio = 0x0200000,
2784 },
2785 },
2786 [SAA7134_BOARD_MSI_TVATANYWHERE_PLUS] = {
2787 .name = "MSI TV@Anywhere plus",
2788 .audio_clock = 0x00187de7,
2789 .tuner_type = TUNER_PHILIPS_TDA8290,
2790 .radio_type = UNSET,
2791 .tuner_addr = ADDR_UNSET,
2792 .radio_addr = ADDR_UNSET,
2793 .gpiomask = 1 << 21,
2794 .inputs = {{
2795 .name = name_tv,
2796 .vmux = 1,
2797 .amux = TV,
2798 .tv = 1,
2799 },{
2800 .name = name_comp1,
2801 .vmux = 3,
2802 .amux = LINE2, /* unconfirmed, taken from Philips driver */
2803 },{
2804 .name = name_comp2,
2805 .vmux = 0, /* untested, Composite over S-Video */
2806 .amux = LINE2,
2807 },{
2808 .name = name_svideo,
2809 .vmux = 8,
2810 .amux = LINE2,
2811 }},
2812 .radio = {
2813 .name = name_radio,
2814 .amux = TV,
2815 .gpio = 0x0200000,
2816 },
2817 },
2818 [SAA7134_BOARD_CINERGY250PCI] = {
2819 /* remote-control does not work. The signal about a
2820 key press comes in via gpio, but the key code
2821 doesn't. Neither does it have an i2c remote control
2822 interface. */
2823 .name = "Terratec Cinergy 250 PCI TV",
2824 .audio_clock = 0x00187de7,
2825 .tuner_type = TUNER_PHILIPS_TDA8290,
2826 .radio_type = UNSET,
2827 .tuner_addr = ADDR_UNSET,
2828 .radio_addr = ADDR_UNSET,
2829 .gpiomask = 0x80200000,
2830 .inputs = {{
2831 .name = name_tv,
2832 .vmux = 1,
2833 .amux = TV,
2834 .tv = 1,
2835 },{
2836 .name = name_svideo, /* NOT tested */
2837 .vmux = 8,
2838 .amux = LINE1,
2839 }},
2840 .radio = {
2841 .name = name_radio,
2842 .amux = TV,
2843 .gpio = 0x0200000,
2844 },
2845 },
2846 [SAA7134_BOARD_FLYDVB_TRIO] = {
2847 /* LifeView LR319 FlyDVB Trio */
2848 /* Peter Missel <peter.missel@onlinehome.de> */
2849 .name = "LifeView FlyDVB Trio",
2850 .audio_clock = 0x00200000,
2851 .tuner_type = TUNER_PHILIPS_TDA8290,
2852 .radio_type = UNSET,
2853 .tuner_addr = ADDR_UNSET,
2854 .radio_addr = ADDR_UNSET,
2855 .gpiomask = 0x00200000,
2856 .mpeg = SAA7134_MPEG_DVB,
2857 .inputs = {{
2858 .name = name_tv, /* Analog broadcast/cable TV */
2859 .vmux = 1,
2860 .amux = TV,
2861 .gpio = 0x200000, /* GPIO21=High for TV input */
2862 .tv = 1,
2863 },{
2864 .name = name_svideo, /* S-Video signal on S-Video input */
2865 .vmux = 8,
2866 .amux = LINE2,
2867 },{
2868 .name = name_comp1, /* Composite signal on S-Video input */
2869 .vmux = 0,
2870 .amux = LINE2,
2871 },{
2872 .name = name_comp2, /* Composite input */
2873 .vmux = 3,
2874 .amux = LINE2,
2875 }},
2876 .radio = {
2877 .name = name_radio,
2878 .amux = TV,
2879 .gpio = 0x000000, /* GPIO21=Low for FM radio antenna */
2880 },
2881 },
2882 [SAA7134_BOARD_AVERMEDIA_777] = {
2883 .name = "AverTV DVB-T 777",
2884 .audio_clock = 0x00187de7,
2885 .tuner_type = TUNER_ABSENT,
2886 .radio_type = UNSET,
2887 .tuner_addr = ADDR_UNSET,
2888 .radio_addr = ADDR_UNSET,
2889 .mpeg = SAA7134_MPEG_DVB,
2890 .inputs = {{
2891 .name = name_comp1,
2892 .vmux = 1,
2893 .amux = LINE1,
2894 },{
2895 .name = name_svideo,
2896 .vmux = 8,
2897 .amux = LINE1,
2898 }},
2899 },
2900 [SAA7134_BOARD_FLYDVBT_LR301] = {
2901 /* LifeView FlyDVB-T */
2902 /* Giampiero Giancipoli <gianci@libero.it> */
2903 .name = "LifeView FlyDVB-T / Genius VideoWonder DVB-T",
2904 .audio_clock = 0x00200000,
2905 .tuner_type = TUNER_ABSENT,
2906 .radio_type = UNSET,
2907 .tuner_addr = ADDR_UNSET,
2908 .radio_addr = ADDR_UNSET,
2909 .mpeg = SAA7134_MPEG_DVB,
2910 .inputs = {{
2911 .name = name_comp1, /* Composite input */
2912 .vmux = 3,
2913 .amux = LINE2,
2914 },{
2915 .name = name_svideo, /* S-Video signal on S-Video input */
2916 .vmux = 8,
2917 .amux = LINE2,
2918 }},
2919 },
2920 [SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331] = {
2921 .name = "ADS Instant TV Duo Cardbus PTV331",
2922 .audio_clock = 0x00200000,
2923 .tuner_type = TUNER_PHILIPS_TDA8290,
2924 .radio_type = UNSET,
2925 .tuner_addr = ADDR_UNSET,
2926 .radio_addr = ADDR_UNSET,
2927 .mpeg = SAA7134_MPEG_DVB,
2928 .gpiomask = 0x00600000, /* Bit 21 0=Radio, Bit 22 0=TV */
2929 .inputs = {{
2930 .name = name_tv,
2931 .vmux = 1,
2932 .amux = TV,
2933 .tv = 1,
2934 .gpio = 0x00200000,
2935 }},
2936 },
2937 [SAA7134_BOARD_TEVION_DVBT_220RF] = {
2938 .name = "Tevion/KWorld DVB-T 220RF",
2939 .audio_clock = 0x00187de7,
2940 .tuner_type = TUNER_PHILIPS_TDA8290,
2941 .radio_type = UNSET,
2942 .tuner_addr = ADDR_UNSET,
2943 .radio_addr = ADDR_UNSET,
2944 .mpeg = SAA7134_MPEG_DVB,
2945 .gpiomask = 1 << 21,
2946 .inputs = {{
2947 .name = name_tv,
2948 .vmux = 1,
2949 .amux = TV,
2950 .tv = 1,
2951 },{
2952 .name = name_comp1,
2953 .vmux = 3,
2954 .amux = LINE1,
2955 },{
2956 .name = name_comp2,
2957 .vmux = 0,
2958 .amux = LINE1,
2959 },{
2960 .name = name_svideo,
2961 .vmux = 8,
2962 .amux = LINE1,
2963 }},
2964 .radio = {
2965 .name = name_radio,
2966 .amux = TV,
2967 .gpio = 0x0200000,
2968 },
2969 },
2970 [SAA7134_BOARD_KWORLD_DVBT_210] = {
2971 .name = "KWorld DVB-T 210",
2972 .audio_clock = 0x00187de7,
2973 .tuner_type = TUNER_PHILIPS_TDA8290,
2974 .radio_type = UNSET,
2975 .tuner_addr = ADDR_UNSET,
2976 .radio_addr = ADDR_UNSET,
2977 .mpeg = SAA7134_MPEG_DVB,
2978 .gpiomask = 1 << 21,
2979 .inputs = {{
2980 .name = name_tv,
2981 .vmux = 1,
2982 .amux = TV,
2983 .tv = 1,
2984 },{
2985 .name = name_comp1,
2986 .vmux = 3,
2987 .amux = LINE1,
2988 },{
2989 .name = name_svideo,
2990 .vmux = 8,
2991 .amux = LINE1,
2992 }},
2993 .radio = {
2994 .name = name_radio,
2995 .amux = TV,
2996 .gpio = 0x0200000,
2997 },
2998 },
2999 [SAA7134_BOARD_KWORLD_ATSC110] = {
3000 .name = "Kworld ATSC110/115",
3001 .audio_clock = 0x00187de7,
3002 .tuner_type = TUNER_PHILIPS_TUV1236D,
3003 .radio_type = UNSET,
3004 .tuner_addr = ADDR_UNSET,
3005 .radio_addr = ADDR_UNSET,
3006 .tda9887_conf = TDA9887_PRESENT,
3007 .mpeg = SAA7134_MPEG_DVB,
3008 .inputs = {{
3009 .name = name_tv,
3010 .vmux = 1,
3011 .amux = TV,
3012 .tv = 1,
3013 },{
3014 .name = name_comp1,
3015 .vmux = 3,
3016 .amux = LINE2,
3017 },{
3018 .name = name_svideo,
3019 .vmux = 8,
3020 .amux = LINE2,
3021 }},
3022 },
3023 [SAA7134_BOARD_AVERMEDIA_A169_B] = {
3024 /* AVerMedia A169 */
3025 /* Rickard Osser <ricky@osser.se> */
3026 /* This card has two saa7134 chips on it,
3027 but only one of them is currently working. */
3028 .name = "AVerMedia A169 B",
3029 .audio_clock = 0x02187de7,
3030 .tuner_type = TUNER_LG_TALN,
3031 .radio_type = UNSET,
3032 .tuner_addr = ADDR_UNSET,
3033 .radio_addr = ADDR_UNSET,
3034 .tda9887_conf = TDA9887_PRESENT,
3035 .gpiomask = 0x0a60000,
3036 },
3037 [SAA7134_BOARD_AVERMEDIA_A169_B1] = {
3038 /* AVerMedia A169 */
3039 /* Rickard Osser <ricky@osser.se> */
3040 .name = "AVerMedia A169 B1",
3041 .audio_clock = 0x02187de7,
3042 .tuner_type = TUNER_LG_TALN,
3043 .radio_type = UNSET,
3044 .tuner_addr = ADDR_UNSET,
3045 .radio_addr = ADDR_UNSET,
3046 .tda9887_conf = TDA9887_PRESENT,
3047 .gpiomask = 0xca60000,
3048 .inputs = {{
3049 .name = name_tv,
3050 .vmux = 4,
3051 .amux = TV,
3052 .tv = 1,
3053 .gpio = 0x04a61000,
3054 },{
3055 .name = name_comp2, /* Composite SVIDEO (B/W if signal is carried with SVIDEO) */
3056 .vmux = 1,
3057 .amux = LINE2,
3058 },{
3059 .name = name_svideo,
3060 .vmux = 9, /* 9 is correct as S-VIDEO1 according to a169.inf! */
3061 .amux = LINE1,
3062 }},
3063 },
3064 [SAA7134_BOARD_MD7134_BRIDGE_2] = {
3065 /* The second saa7134 on this card only serves as DVB-S host bridge */
3066 .name = "Medion 7134 Bridge #2",
3067 .audio_clock = 0x00187de7,
3068 .radio_type = UNSET,
3069 .tuner_addr = ADDR_UNSET,
3070 .radio_addr = ADDR_UNSET,
3071 .mpeg = SAA7134_MPEG_DVB,
3072 },
3073 [SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS] = {
3074 .name = "LifeView FlyDVB-T Hybrid Cardbus/MSI TV @nywhere A/D NB",
3075 .audio_clock = 0x00200000,
3076 .tuner_type = TUNER_PHILIPS_TDA8290,
3077 .radio_type = UNSET,
3078 .tuner_addr = ADDR_UNSET,
3079 .radio_addr = ADDR_UNSET,
3080 .mpeg = SAA7134_MPEG_DVB,
3081 .gpiomask = 0x00600000, /* Bit 21 0=Radio, Bit 22 0=TV */
3082 .inputs = {{
3083 .name = name_tv,
3084 .vmux = 1,
3085 .amux = TV,
3086 .gpio = 0x200000, /* GPIO21=High for TV input */
3087 .tv = 1,
3088 },{
3089 .name = name_svideo, /* S-Video signal on S-Video input */
3090 .vmux = 8,
3091 .amux = LINE2,
3092 },{
3093 .name = name_comp1, /* Composite signal on S-Video input */
3094 .vmux = 0,
3095 .amux = LINE2,
3096 },{
3097 .name = name_comp2, /* Composite input */
3098 .vmux = 3,
3099 .amux = LINE2,
3100 }},
3101 .radio = {
3102 .name = name_radio,
3103 .amux = TV,
3104 .gpio = 0x000000, /* GPIO21=Low for FM radio antenna */
3105 },
3106 },
3107 [SAA7134_BOARD_FLYVIDEO3000_NTSC] = {
3108 /* "Zac Bowling" <zac@zacbowling.com> */
3109 .name = "LifeView FlyVIDEO3000 (NTSC)",
3110 .audio_clock = 0x00200000,
3111 .tuner_type = TUNER_PHILIPS_NTSC,
3112 .radio_type = UNSET,
3113 .tuner_addr = ADDR_UNSET,
3114 .radio_addr = ADDR_UNSET,
3115
3116 .gpiomask = 0xe000,
3117 .inputs = {{
3118 .name = name_tv,
3119 .vmux = 1,
3120 .amux = TV,
3121 .gpio = 0x8000,
3122 .tv = 1,
3123 },{
3124 .name = name_tv_mono,
3125 .vmux = 1,
3126 .amux = LINE2,
3127 .gpio = 0x0000,
3128 .tv = 1,
3129 },{
3130 .name = name_comp1,
3131 .vmux = 0,
3132 .amux = LINE2,
3133 .gpio = 0x4000,
3134 },{
3135 .name = name_comp2,
3136 .vmux = 3,
3137 .amux = LINE2,
3138 .gpio = 0x4000,
3139 },{
3140 .name = name_svideo,
3141 .vmux = 8,
3142 .amux = LINE2,
3143 .gpio = 0x4000,
3144 }},
3145 .radio = {
3146 .name = name_radio,
3147 .amux = LINE2,
3148 .gpio = 0x2000,
3149 },
3150 .mute = {
3151 .name = name_mute,
3152 .amux = TV,
3153 .gpio = 0x8000,
3154 },
3155 },
3156 [SAA7134_BOARD_MEDION_MD8800_QUADRO] = {
3157 .name = "Medion Md8800 Quadro",
3158 .audio_clock = 0x00187de7,
3159 .tuner_type = TUNER_PHILIPS_TDA8290,
3160 .radio_type = UNSET,
3161 .tuner_addr = ADDR_UNSET,
3162 .radio_addr = ADDR_UNSET,
3163 .mpeg = SAA7134_MPEG_DVB,
3164 .inputs = {{
3165 .name = name_tv,
3166 .vmux = 1,
3167 .amux = TV,
3168 .tv = 1,
3169 },{
3170 .name = name_comp1,
3171 .vmux = 0,
3172 .amux = LINE1,
3173 },{
3174 .name = name_svideo,
3175 .vmux = 8,
3176 .amux = LINE1,
3177 }},
3178 },
3179 [SAA7134_BOARD_FLYDVBS_LR300] = {
3180 /* LifeView FlyDVB-s */
3181 /* Igor M. Liplianin <liplianin@tut.by> */
3182 .name = "LifeView FlyDVB-S /Acorp TV134DS",
3183 .audio_clock = 0x00200000,
3184 .tuner_type = TUNER_ABSENT,
3185 .radio_type = UNSET,
3186 .tuner_addr = ADDR_UNSET,
3187 .radio_addr = ADDR_UNSET,
3188 .mpeg = SAA7134_MPEG_DVB,
3189 .inputs = {{
3190 .name = name_comp1, /* Composite input */
3191 .vmux = 3,
3192 .amux = LINE1,
3193 },{
3194 .name = name_svideo, /* S-Video signal on S-Video input */
3195 .vmux = 8,
3196 .amux = LINE1,
3197 }},
3198 },
3199 [SAA7134_BOARD_PROTEUS_2309] = {
3200 .name = "Proteus Pro 2309",
3201 .audio_clock = 0x00187de7,
3202 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
3203 .radio_type = UNSET,
3204 .tuner_addr = ADDR_UNSET,
3205 .radio_addr = ADDR_UNSET,
3206 .tda9887_conf = TDA9887_PRESENT,
3207 .inputs = {{
3208 .name = name_tv,
3209 .vmux = 1,
3210 .amux = LINE2,
3211 .tv = 1,
3212 },{
3213 .name = name_comp1,
3214 .vmux = 0,
3215 .amux = LINE2,
3216 },{
3217 .name = name_comp2,
3218 .vmux = 3,
3219 .amux = LINE2,
3220 },{
3221 .name = name_svideo,
3222 .vmux = 8,
3223 .amux = LINE2,
3224 }},
3225 .mute = {
3226 .name = name_mute,
3227 .amux = LINE1,
3228 },
3229 },
3230 [SAA7134_BOARD_AVERMEDIA_A16AR] = {
3231 /* Petr Baudis <pasky@ucw.cz> */
3232 .name = "AVerMedia TV Hybrid A16AR",
3233 .audio_clock = 0x187de7,
3234 .tuner_type = TUNER_PHILIPS_TD1316, /* untested */
3235 .radio_type = TUNER_TEA5767, /* untested */
3236 .tuner_addr = ADDR_UNSET,
3237 .radio_addr = 0x60,
3238 .tda9887_conf = TDA9887_PRESENT,
3239 .mpeg = SAA7134_MPEG_DVB,
3240 .inputs = {{
3241 .name = name_tv,
3242 .vmux = 1,
3243 .amux = TV,
3244 .tv = 1,
3245 },{
3246 .name = name_comp1,
3247 .vmux = 3,
3248 .amux = LINE2,
3249 },{
3250 .name = name_svideo,
3251 .vmux = 8,
3252 .amux = LINE1,
3253 }},
3254 .radio = {
3255 .name = name_radio,
3256 .amux = LINE1,
3257 },
3258 },
3259 [SAA7134_BOARD_ASUS_EUROPA2_HYBRID] = {
3260 .name = "Asus Europa2 OEM",
3261 .audio_clock = 0x00187de7,
3262 .tuner_type = TUNER_PHILIPS_FMD1216ME_MK3,
3263 .radio_type = UNSET,
3264 .tuner_addr = ADDR_UNSET,
3265 .radio_addr = ADDR_UNSET,
3266 .tda9887_conf = TDA9887_PRESENT| TDA9887_PORT1_ACTIVE | TDA9887_PORT2_ACTIVE,
3267 .mpeg = SAA7134_MPEG_DVB,
3268 .inputs = {{
3269 .name = name_tv,
3270 .vmux = 3,
3271 .amux = TV,
3272 .tv = 1,
3273 },{
3274 .name = name_comp1,
3275 .vmux = 4,
3276 .amux = LINE2,
3277 },{
3278 .name = name_svideo,
3279 .vmux = 8,
3280 .amux = LINE2,
3281 }},
3282 .radio = {
3283 .name = name_radio,
3284 .amux = LINE1,
3285 },
3286 },
3287 [SAA7134_BOARD_PINNACLE_PCTV_310i] = {
3288 .name = "Pinnacle PCTV 310i",
3289 .audio_clock = 0x00187de7,
3290 .tuner_type = TUNER_PHILIPS_TDA8290,
3291 .radio_type = UNSET,
3292 .tuner_addr = ADDR_UNSET,
3293 .radio_addr = ADDR_UNSET,
3294 .tuner_config = 1,
3295 .mpeg = SAA7134_MPEG_DVB,
3296 .gpiomask = 0x000200000,
3297 .inputs = {{
3298 .name = name_tv,
3299 .vmux = 4,
3300 .amux = TV,
3301 .tv = 1,
3302 },{
3303 .name = name_comp1,
3304 .vmux = 1,
3305 .amux = LINE2,
3306 },{
3307 .name = name_comp2,
3308 .vmux = 0,
3309 .amux = LINE2,
3310 },{
3311 .name = name_svideo,
3312 .vmux = 8,
3313 .amux = LINE2,
3314 }},
3315 .radio = {
3316 .name = name_radio,
3317 .amux = TV,
3318 .gpio = 0x0200000,
3319 },
3320 },
3321 [SAA7134_BOARD_AVERMEDIA_STUDIO_507] = {
3322 /* Mikhail Fedotov <mo_fedotov@mail.ru> */
3323 .name = "Avermedia AVerTV Studio 507",
3324 .audio_clock = 0x00187de7,
3325 .tuner_type = TUNER_PHILIPS_FM1256_IH3,
3326 .radio_type = UNSET,
3327 .tuner_addr = ADDR_UNSET,
3328 .radio_addr = ADDR_UNSET,
3329 .tda9887_conf = TDA9887_PRESENT,
3330 .gpiomask = 0x03,
3331 .inputs = {{
3332 .name = name_tv,
3333 .vmux = 1,
3334 .amux = TV,
3335 .tv = 1,
3336 .gpio = 0x00,
3337 },{
3338 .name = name_comp1,
3339 .vmux = 0,
3340 .amux = LINE2,
3341 .gpio = 0x00,
3342 },{
3343 .name = name_comp2,
3344 .vmux = 3,
3345 .amux = LINE2,
3346 .gpio = 0x00,
3347 },{
3348 .name = name_svideo,
3349 .vmux = 8,
3350 .amux = LINE2,
3351 .gpio = 0x00,
3352 }},
3353 .radio = {
3354 .name = name_radio,
3355 .amux = LINE2,
3356 .gpio = 0x01,
3357 },
3358 .mute = {
3359 .name = name_mute,
3360 .amux = LINE1,
3361 .gpio = 0x00,
3362 },
3363 },
3364 [SAA7134_BOARD_VIDEOMATE_DVBT_200A] = {
3365 /* Francis Barber <fedora@barber-family.id.au> */
3366 .name = "Compro Videomate DVB-T200A",
3367 .audio_clock = 0x00187de7,
3368 .tuner_type = TUNER_ABSENT,
3369 .radio_type = UNSET,
3370 .tuner_addr = ADDR_UNSET,
3371 .radio_addr = ADDR_UNSET,
3372 .tda9887_conf = TDA9887_PRESENT | TDA9887_PORT1_ACTIVE,
3373 .mpeg = SAA7134_MPEG_DVB,
3374 .inputs = {{
3375 .name = name_tv,
3376 .vmux = 3,
3377 .amux = TV,
3378 .tv = 1,
3379 },{
3380 .name = name_comp1,
3381 .vmux = 1,
3382 .amux = LINE2,
3383 },{
3384 .name = name_svideo,
3385 .vmux = 8,
3386 .amux = LINE2,
3387 }},
3388 },
3389 [SAA7134_BOARD_HAUPPAUGE_HVR1110] = {
3390 /* Thomas Genty <tomlohave@gmail.com> */
3391 /* David Bentham <db260179@hotmail.com> */
3392 .name = "Hauppauge WinTV-HVR1110 DVB-T/Hybrid",
3393 .audio_clock = 0x00187de7,
3394 .tuner_type = TUNER_PHILIPS_TDA8290,
3395 .radio_type = UNSET,
3396 .tuner_addr = ADDR_UNSET,
3397 .radio_addr = ADDR_UNSET,
3398 .tuner_config = 1,
3399 .mpeg = SAA7134_MPEG_DVB,
3400 .gpiomask = 0x0200100,
3401 .inputs = {{
3402 .name = name_tv,
3403 .vmux = 1,
3404 .amux = TV,
3405 .tv = 1,
3406 .gpio = 0x0000100,
3407 }, {
3408 .name = name_comp1,
3409 .vmux = 3,
3410 .amux = LINE1,
3411 }, {
3412 .name = name_svideo,
3413 .vmux = 8,
3414 .amux = LINE1,
3415 } },
3416 .radio = {
3417 .name = name_radio,
3418 .amux = TV,
3419 .gpio = 0x0200100,
3420 },
3421 },
3422 [SAA7134_BOARD_HAUPPAUGE_HVR1150] = {
3423 .name = "Hauppauge WinTV-HVR1150 ATSC/QAM-Hybrid",
3424 .audio_clock = 0x00187de7,
3425 .tuner_type = TUNER_PHILIPS_TDA8290,
3426 .radio_type = UNSET,
3427 .tuner_addr = ADDR_UNSET,
3428 .radio_addr = ADDR_UNSET,
3429 .tuner_config = 3,
3430 .mpeg = SAA7134_MPEG_DVB,
3431 .ts_type = SAA7134_MPEG_TS_SERIAL,
3432 .ts_force_val = 1,
3433 .gpiomask = 0x0800100, /* GPIO 21 is an INPUT */
3434 .inputs = {{
3435 .name = name_tv,
3436 .vmux = 1,
3437 .amux = TV,
3438 .tv = 1,
3439 .gpio = 0x0000100,
3440 }, {
3441 .name = name_comp1,
3442 .vmux = 3,
3443 .amux = LINE1,
3444 }, {
3445 .name = name_svideo,
3446 .vmux = 8,
3447 .amux = LINE1,
3448 } },
3449 .radio = {
3450 .name = name_radio,
3451 .amux = TV,
3452 .gpio = 0x0800100, /* GPIO 23 HI for FM */
3453 },
3454 },
3455 [SAA7134_BOARD_HAUPPAUGE_HVR1120] = {
3456 .name = "Hauppauge WinTV-HVR1120 DVB-T/Hybrid",
3457 .audio_clock = 0x00187de7,
3458 .tuner_type = TUNER_PHILIPS_TDA8290,
3459 .radio_type = UNSET,
3460 .tuner_addr = ADDR_UNSET,
3461 .radio_addr = ADDR_UNSET,
3462 .tuner_config = 3,
3463 .mpeg = SAA7134_MPEG_DVB,
3464 .ts_type = SAA7134_MPEG_TS_SERIAL,
3465 .gpiomask = 0x0800100, /* GPIO 21 is an INPUT */
3466 .inputs = {{
3467 .name = name_tv,
3468 .vmux = 1,
3469 .amux = TV,
3470 .tv = 1,
3471 .gpio = 0x0000100,
3472 }, {
3473 .name = name_comp1,
3474 .vmux = 3,
3475 .amux = LINE1,
3476 }, {
3477 .name = name_svideo,
3478 .vmux = 8,
3479 .amux = LINE1,
3480 } },
3481 .radio = {
3482 .name = name_radio,
3483 .amux = TV,
3484 .gpio = 0x0800100, /* GPIO 23 HI for FM */
3485 },
3486 },
3487 [SAA7134_BOARD_CINERGY_HT_PCMCIA] = {
3488 .name = "Terratec Cinergy HT PCMCIA",
3489 .audio_clock = 0x00187de7,
3490 .tuner_type = TUNER_PHILIPS_TDA8290,
3491 .radio_type = UNSET,
3492 .tuner_addr = ADDR_UNSET,
3493 .radio_addr = ADDR_UNSET,
3494 .mpeg = SAA7134_MPEG_DVB,
3495 .inputs = {{
3496 .name = name_tv,
3497 .vmux = 1,
3498 .amux = TV,
3499 .tv = 1,
3500 },{
3501 .name = name_comp1,
3502 .vmux = 0,
3503 .amux = LINE1,
3504 },{
3505 .name = name_svideo,
3506 .vmux = 6,
3507 .amux = LINE1,
3508 }},
3509 },
3510 [SAA7134_BOARD_ENCORE_ENLTV] = {
3511 /* Steven Walter <stevenrwalter@gmail.com>
3512 Juan Pablo Sormani <sorman@gmail.com> */
3513 .name = "Encore ENLTV",
3514 .audio_clock = 0x00200000,
3515 .tuner_type = TUNER_TNF_5335MF,
3516 .radio_type = UNSET,
3517 .tuner_addr = ADDR_UNSET,
3518 .radio_addr = ADDR_UNSET,
3519 .inputs = {{
3520 .name = name_tv,
3521 .vmux = 1,
3522 .amux = 3,
3523 .tv = 1,
3524 },{
3525 .name = name_tv_mono,
3526 .vmux = 7,
3527 .amux = 4,
3528 .tv = 1,
3529 },{
3530 .name = name_comp1,
3531 .vmux = 3,
3532 .amux = 2,
3533 },{
3534 .name = name_svideo,
3535 .vmux = 0,
3536 .amux = 2,
3537 }},
3538 .radio = {
3539 .name = name_radio,
3540 .amux = LINE2,
3541/* .gpio = 0x00300001,*/
3542 .gpio = 0x20000,
3543
3544 },
3545 .mute = {
3546 .name = name_mute,
3547 .amux = 0,
3548 },
3549 },
3550 [SAA7134_BOARD_ENCORE_ENLTV_FM] = {
3551 /* Juan Pablo Sormani <sorman@gmail.com> */
3552 .name = "Encore ENLTV-FM",
3553 .audio_clock = 0x00200000,
3554 .tuner_type = TUNER_PHILIPS_FCV1236D,
3555 .radio_type = UNSET,
3556 .tuner_addr = ADDR_UNSET,
3557 .radio_addr = ADDR_UNSET,
3558 .inputs = {{
3559 .name = name_tv,
3560 .vmux = 1,
3561 .amux = 3,
3562 .tv = 1,
3563 },{
3564 .name = name_tv_mono,
3565 .vmux = 7,
3566 .amux = 4,
3567 .tv = 1,
3568 },{
3569 .name = name_comp1,
3570 .vmux = 3,
3571 .amux = 2,
3572 },{
3573 .name = name_svideo,
3574 .vmux = 0,
3575 .amux = 2,
3576 }},
3577 .radio = {
3578 .name = name_radio,
3579 .amux = LINE2,
3580 .gpio = 0x20000,
3581
3582 },
3583 .mute = {
3584 .name = name_mute,
3585 .amux = 0,
3586 },
3587 },
3588 [SAA7134_BOARD_ENCORE_ENLTV_FM53] = {
3589 .name = "Encore ENLTV-FM v5.3",
3590 .audio_clock = 0x00200000,
3591 .tuner_type = TUNER_TNF_5335MF,
3592 .radio_type = UNSET,
3593 .tuner_addr = ADDR_UNSET,
3594 .radio_addr = ADDR_UNSET,
3595 .gpiomask = 0x7000,
3596 .inputs = { {
3597 .name = name_tv,
3598 .vmux = 1,
3599 .amux = 1,
3600 .tv = 1,
3601 .gpio = 0x50000,
3602 }, {
3603 .name = name_comp1,
3604 .vmux = 3,
3605 .amux = 2,
3606 .gpio = 0x2000,
3607 }, {
3608 .name = name_svideo,
3609 .vmux = 8,
3610 .amux = 2,
3611 .gpio = 0x2000,
3612 } },
3613 .radio = {
3614 .name = name_radio,
3615 .vmux = 1,
3616 .amux = 1,
3617 },
3618 .mute = {
3619 .name = name_mute,
3620 .gpio = 0xf000,
3621 .amux = 0,
3622 },
3623 },
3624 [SAA7134_BOARD_ENCORE_ENLTV_FM3] = {
3625 .name = "Encore ENLTV-FM 3",
3626 .audio_clock = 0x02187de7,
3627 .tuner_type = TUNER_TENA_TNF_5337,
3628 .radio_type = TUNER_TEA5767,
3629 .tuner_addr = 0x61,
3630 .radio_addr = 0x60,
3631 .inputs = { {
3632 .name = name_tv,
3633 .vmux = 1,
3634 .amux = LINE2,
3635 .tv = 1,
3636 }, {
3637 .name = name_comp1,
3638 .vmux = 3,
3639 .amux = LINE1,
3640 }, {
3641 .name = name_svideo,
3642 .vmux = 8,
3643 .amux = LINE1,
3644 } },
3645 .radio = {
3646 .name = name_radio,
3647 .vmux = 1,
3648 .amux = LINE1,
3649 },
3650 .mute = {
3651 .name = name_mute,
3652 .amux = LINE1,
3653 .gpio = 0x43000,
3654 },
3655 },
3656 [SAA7134_BOARD_CINERGY_HT_PCI] = {
3657 .name = "Terratec Cinergy HT PCI",
3658 .audio_clock = 0x00187de7,
3659 .tuner_type = TUNER_PHILIPS_TDA8290,
3660 .radio_type = UNSET,
3661 .tuner_addr = ADDR_UNSET,
3662 .radio_addr = ADDR_UNSET,
3663 .mpeg = SAA7134_MPEG_DVB,
3664 .inputs = {{
3665 .name = name_tv,
3666 .vmux = 1,
3667 .amux = TV,
3668 .tv = 1,
3669 },{
3670 .name = name_comp1,
3671 .vmux = 0,
3672 .amux = LINE1,
3673 },{
3674 .name = name_svideo,
3675 .vmux = 6,
3676 .amux = LINE1,
3677 }},
3678 },
3679 [SAA7134_BOARD_PHILIPS_TIGER_S] = {
3680 .name = "Philips Tiger - S Reference design",
3681 .audio_clock = 0x00187de7,
3682 .tuner_type = TUNER_PHILIPS_TDA8290,
3683 .radio_type = UNSET,
3684 .tuner_addr = ADDR_UNSET,
3685 .radio_addr = ADDR_UNSET,
3686 .tuner_config = 2,
3687 .mpeg = SAA7134_MPEG_DVB,
3688 .gpiomask = 0x0200000,
3689 .inputs = {{
3690 .name = name_tv,
3691 .vmux = 1,
3692 .amux = TV,
3693 .tv = 1,
3694 },{
3695 .name = name_comp1,
3696 .vmux = 3,
3697 .amux = LINE1,
3698 },{
3699 .name = name_svideo,
3700 .vmux = 8,
3701 .amux = LINE1,
3702 }},
3703 .radio = {
3704 .name = name_radio,
3705 .amux = TV,
3706 .gpio = 0x0200000,
3707 },
3708 },
3709 [SAA7134_BOARD_AVERMEDIA_M102] = {
3710 .name = "Avermedia M102",
3711 .audio_clock = 0x00187de7,
3712 .tuner_type = TUNER_PHILIPS_TDA8290,
3713 .radio_type = UNSET,
3714 .tuner_addr = ADDR_UNSET,
3715 .radio_addr = ADDR_UNSET,
3716 .gpiomask = 1<<21,
3717 .inputs = {{
3718 .name = name_tv,
3719 .vmux = 1,
3720 .amux = TV,
3721 .tv = 1,
3722 },{
3723 .name = name_comp1,
3724 .vmux = 0,
3725 .amux = LINE2,
3726 },{
3727 .name = name_svideo,
3728 .vmux = 6,
3729 .amux = LINE2,
3730 }},
3731 },
3732 [SAA7134_BOARD_ASUS_P7131_4871] = {
3733 .name = "ASUS P7131 4871",
3734 .audio_clock = 0x00187de7,
3735 .tuner_type = TUNER_PHILIPS_TDA8290,
3736 .radio_type = UNSET,
3737 .tuner_addr = ADDR_UNSET,
3738 .radio_addr = ADDR_UNSET,
3739 .tuner_config = 2,
3740 .mpeg = SAA7134_MPEG_DVB,
3741 .gpiomask = 0x0200000,
3742 .inputs = {{
3743 .name = name_tv,
3744 .vmux = 1,
3745 .amux = TV,
3746 .tv = 1,
3747 .gpio = 0x0200000,
3748 }},
3749 },
3750 [SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA] = {
3751 .name = "ASUSTeK P7131 Hybrid",
3752 .audio_clock = 0x00187de7,
3753 .tuner_type = TUNER_PHILIPS_TDA8290,
3754 .radio_type = UNSET,
3755 .tuner_addr = ADDR_UNSET,
3756 .radio_addr = ADDR_UNSET,
3757 .tuner_config = 2,
3758 .gpiomask = 1 << 21,
3759 .mpeg = SAA7134_MPEG_DVB,
3760 .inputs = {{
3761 .name = name_tv,
3762 .vmux = 1,
3763 .amux = TV,
3764 .tv = 1,
3765 .gpio = 0x0000000,
3766 },{
3767 .name = name_comp1,
3768 .vmux = 3,
3769 .amux = LINE2,
3770 .gpio = 0x0200000,
3771 },{
3772 .name = name_comp2,
3773 .vmux = 0,
3774 .amux = LINE2,
3775 .gpio = 0x0200000,
3776 },{
3777 .name = name_svideo,
3778 .vmux = 8,
3779 .amux = LINE2,
3780 .gpio = 0x0200000,
3781 }},
3782 .radio = {
3783 .name = name_radio,
3784 .amux = TV,
3785 .gpio = 0x0200000,
3786 },
3787 },
3788 [SAA7134_BOARD_ASUSTeK_P7131_ANALOG] = {
3789 .name = "ASUSTeK P7131 Analog",
3790 .audio_clock = 0x00187de7,
3791 .tuner_type = TUNER_PHILIPS_TDA8290,
3792 .radio_type = UNSET,
3793 .tuner_addr = ADDR_UNSET,
3794 .radio_addr = ADDR_UNSET,
3795 .gpiomask = 1 << 21,
3796 .inputs = {{
3797 .name = name_tv,
3798 .vmux = 1,
3799 .amux = TV,
3800 .tv = 1,
3801 .gpio = 0x0000000,
3802 }, {
3803 .name = name_comp1,
3804 .vmux = 3,
3805 .amux = LINE2,
3806 }, {
3807 .name = name_comp2,
3808 .vmux = 0,
3809 .amux = LINE2,
3810 }, {
3811 .name = name_svideo,
3812 .vmux = 8,
3813 .amux = LINE2,
3814 } },
3815 .radio = {
3816 .name = name_radio,
3817 .amux = TV,
3818 .gpio = 0x0200000,
3819 },
3820 },
3821 [SAA7134_BOARD_SABRENT_TV_PCB05] = {
3822 .name = "Sabrent PCMCIA TV-PCB05",
3823 .audio_clock = 0x00187de7,
3824 .tuner_type = TUNER_PHILIPS_TDA8290,
3825 .radio_type = UNSET,
3826 .tuner_addr = ADDR_UNSET,
3827 .radio_addr = ADDR_UNSET,
3828 .inputs = {{
3829 .name = name_tv,
3830 .vmux = 1,
3831 .amux = TV,
3832 .tv = 1,
3833 },{
3834 .name = name_comp1,
3835 .vmux = 3,
3836 .amux = LINE1,
3837 },{
3838 .name = name_comp2,
3839 .vmux = 0,
3840 .amux = LINE1,
3841 },{
3842 .name = name_svideo,
3843 .vmux = 8,
3844 .amux = LINE1,
3845 }},
3846 .mute = {
3847 .name = name_mute,
3848 .amux = TV,
3849 },
3850 },
3851 [SAA7134_BOARD_10MOONSTVMASTER3] = {
3852 /* Tony Wan <aloha_cn@hotmail.com> */
3853 .name = "10MOONS TM300 TV Card",
3854 .audio_clock = 0x00200000,
3855 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
3856 .radio_type = UNSET,
3857 .tuner_addr = ADDR_UNSET,
3858 .radio_addr = ADDR_UNSET,
3859 .gpiomask = 0x7000,
3860 .inputs = {{
3861 .name = name_tv,
3862 .vmux = 1,
3863 .amux = LINE2,
3864 .gpio = 0x0000,
3865 .tv = 1,
3866 },{
3867 .name = name_comp1,
3868 .vmux = 3,
3869 .amux = LINE1,
3870 .gpio = 0x2000,
3871 },{
3872 .name = name_svideo,
3873 .vmux = 8,
3874 .amux = LINE1,
3875 .gpio = 0x2000,
3876 }},
3877 .mute = {
3878 .name = name_mute,
3879 .amux = LINE2,
3880 .gpio = 0x3000,
3881 },
3882 },
3883 [SAA7134_BOARD_AVERMEDIA_SUPER_007] = {
3884 .name = "Avermedia Super 007",
3885 .audio_clock = 0x00187de7,
3886 .tuner_type = TUNER_PHILIPS_TDA8290,
3887 .radio_type = UNSET,
3888 .tuner_addr = ADDR_UNSET,
3889 .radio_addr = ADDR_UNSET,
3890 .tuner_config = 0,
3891 .mpeg = SAA7134_MPEG_DVB,
3892 .inputs = {{
3893 .name = name_tv, /* FIXME: analog tv untested */
3894 .vmux = 1,
3895 .amux = TV,
3896 .tv = 1,
3897 }},
3898 },
3899 [SAA7134_BOARD_AVERMEDIA_M135A] = {
3900 .name = "Avermedia PCI pure analog (M135A)",
3901 .audio_clock = 0x00187de7,
3902 .tuner_type = TUNER_PHILIPS_TDA8290,
3903 .radio_type = UNSET,
3904 .tuner_addr = ADDR_UNSET,
3905 .radio_addr = ADDR_UNSET,
3906 .tuner_config = 2,
3907 .gpiomask = 0x020200000,
3908 .inputs = {{
3909 .name = name_tv,
3910 .vmux = 1,
3911 .amux = TV,
3912 .tv = 1,
3913 }, {
3914 .name = name_comp1,
3915 .vmux = 3,
3916 .amux = LINE1,
3917 }, {
3918 .name = name_svideo,
3919 .vmux = 8,
3920 .amux = LINE1,
3921 } },
3922 .radio = {
3923 .name = name_radio,
3924 .amux = TV,
3925 .gpio = 0x00200000,
3926 },
3927 .mute = {
3928 .name = name_mute,
3929 .amux = TV,
3930 .gpio = 0x01,
3931 },
3932 },
3933 [SAA7134_BOARD_AVERMEDIA_M733A] = {
3934 .name = "Avermedia PCI M733A",
3935 .audio_clock = 0x00187de7,
3936 .tuner_type = TUNER_PHILIPS_TDA8290,
3937 .radio_type = UNSET,
3938 .tuner_addr = ADDR_UNSET,
3939 .radio_addr = ADDR_UNSET,
3940 .tuner_config = 0,
3941 .gpiomask = 0x020200000,
3942 .inputs = {{
3943 .name = name_tv,
3944 .vmux = 1,
3945 .amux = TV,
3946 .tv = 1,
3947 }, {
3948 .name = name_comp1,
3949 .vmux = 3,
3950 .amux = LINE1,
3951 }, {
3952 .name = name_svideo,
3953 .vmux = 8,
3954 .amux = LINE1,
3955 } },
3956 .radio = {
3957 .name = name_radio,
3958 .amux = TV,
3959 .gpio = 0x00200000,
3960 },
3961 .mute = {
3962 .name = name_mute,
3963 .amux = TV,
3964 .gpio = 0x01,
3965 },
3966 },
3967 [SAA7134_BOARD_BEHOLD_401] = {
3968 /* Beholder Intl. Ltd. 2008 */
3969 /*Dmitry Belimov <d.belimov@gmail.com> */
3970 .name = "Beholder BeholdTV 401",
3971 .audio_clock = 0x00187de7,
3972 .tuner_type = TUNER_PHILIPS_FQ1216ME,
3973 .radio_type = UNSET,
3974 .tuner_addr = ADDR_UNSET,
3975 .radio_addr = ADDR_UNSET,
3976 .gpiomask = 0x00008000,
3977 .inputs = {{
3978 .name = name_svideo,
3979 .vmux = 8,
3980 .amux = LINE1,
3981 },{
3982 .name = name_comp1,
3983 .vmux = 1,
3984 .amux = LINE1,
3985 },{
3986 .name = name_tv,
3987 .vmux = 3,
3988 .amux = LINE2,
3989 .tv = 1,
3990 }},
3991 .mute = {
3992 .name = name_mute,
3993 .amux = LINE1,
3994 },
3995 },
3996 [SAA7134_BOARD_BEHOLD_403] = {
3997 /* Beholder Intl. Ltd. 2008 */
3998 /*Dmitry Belimov <d.belimov@gmail.com> */
3999 .name = "Beholder BeholdTV 403",
4000 .audio_clock = 0x00187de7,
4001 .tuner_type = TUNER_PHILIPS_FQ1216ME,
4002 .radio_type = UNSET,
4003 .tuner_addr = ADDR_UNSET,
4004 .radio_addr = ADDR_UNSET,
4005 .gpiomask = 0x00008000,
4006 .inputs = {{
4007 .name = name_svideo,
4008 .vmux = 8,
4009 .amux = LINE1,
4010 },{
4011 .name = name_comp1,
4012 .vmux = 1,
4013 .amux = LINE1,
4014 },{
4015 .name = name_tv,
4016 .vmux = 3,
4017 .amux = LINE2,
4018 .tv = 1,
4019 }},
4020 },
4021 [SAA7134_BOARD_BEHOLD_403FM] = {
4022 /* Beholder Intl. Ltd. 2008 */
4023 /*Dmitry Belimov <d.belimov@gmail.com> */
4024 .name = "Beholder BeholdTV 403 FM",
4025 .audio_clock = 0x00187de7,
4026 .tuner_type = TUNER_PHILIPS_FQ1216ME,
4027 .radio_type = UNSET,
4028 .tuner_addr = ADDR_UNSET,
4029 .radio_addr = ADDR_UNSET,
4030 .gpiomask = 0x00008000,
4031 .inputs = {{
4032 .name = name_svideo,
4033 .vmux = 8,
4034 .amux = LINE1,
4035 },{
4036 .name = name_comp1,
4037 .vmux = 1,
4038 .amux = LINE1,
4039 },{
4040 .name = name_tv,
4041 .vmux = 3,
4042 .amux = LINE2,
4043 .tv = 1,
4044 }},
4045 .radio = {
4046 .name = name_radio,
4047 .amux = LINE2,
4048 },
4049 },
4050 [SAA7134_BOARD_BEHOLD_405] = {
4051 /* Beholder Intl. Ltd. 2008 */
4052 /*Dmitry Belimov <d.belimov@gmail.com> */
4053 .name = "Beholder BeholdTV 405",
4054 .audio_clock = 0x00187de7,
4055 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4056 .radio_type = UNSET,
4057 .tuner_addr = ADDR_UNSET,
4058 .radio_addr = ADDR_UNSET,
4059 .tda9887_conf = TDA9887_PRESENT,
4060 .gpiomask = 0x00008000,
4061 .inputs = {{
4062 .name = name_svideo,
4063 .vmux = 8,
4064 .amux = LINE1,
4065 },{
4066 .name = name_comp1,
4067 .vmux = 3,
4068 .amux = LINE1,
4069 },{
4070 .name = name_tv,
4071 .vmux = 3,
4072 .amux = LINE2,
4073 .tv = 1,
4074 }},
4075 },
4076 [SAA7134_BOARD_BEHOLD_405FM] = {
4077 /* Sergey <skiv@orel.ru> */
4078 /* Beholder Intl. Ltd. 2008 */
4079 /*Dmitry Belimov <d.belimov@gmail.com> */
4080 .name = "Beholder BeholdTV 405 FM",
4081 .audio_clock = 0x00187de7,
4082 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4083 .radio_type = UNSET,
4084 .tuner_addr = ADDR_UNSET,
4085 .radio_addr = ADDR_UNSET,
4086 .tda9887_conf = TDA9887_PRESENT,
4087 .gpiomask = 0x00008000,
4088 .inputs = {{
4089 .name = name_svideo,
4090 .vmux = 8,
4091 .amux = LINE1,
4092 },{
4093 .name = name_comp1,
4094 .vmux = 3,
4095 .amux = LINE1,
4096 },{
4097 .name = name_tv,
4098 .vmux = 3,
4099 .amux = LINE2,
4100 .tv = 1,
4101 }},
4102 .radio = {
4103 .name = name_radio,
4104 .amux = LINE2,
4105 },
4106 },
4107 [SAA7134_BOARD_BEHOLD_407] = {
4108 /* Beholder Intl. Ltd. 2008 */
4109 /*Dmitry Belimov <d.belimov@gmail.com> */
4110 .name = "Beholder BeholdTV 407",
4111 .audio_clock = 0x00187de7,
4112 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4113 .radio_type = UNSET,
4114 .tuner_addr = ADDR_UNSET,
4115 .radio_addr = ADDR_UNSET,
4116 .tda9887_conf = TDA9887_PRESENT,
4117 .gpiomask = 0x00008000,
4118 .inputs = {{
4119 .name = name_svideo,
4120 .vmux = 8,
4121 .amux = LINE1,
4122 .gpio = 0xc0c000,
4123 },{
4124 .name = name_comp1,
4125 .vmux = 1,
4126 .amux = LINE1,
4127 .gpio = 0xc0c000,
4128 },{
4129 .name = name_tv,
4130 .vmux = 3,
4131 .amux = TV,
4132 .tv = 1,
4133 .gpio = 0xc0c000,
4134 }},
4135 },
4136 [SAA7134_BOARD_BEHOLD_407FM] = {
4137 /* Beholder Intl. Ltd. 2008 */
4138 /*Dmitry Belimov <d.belimov@gmail.com> */
4139 .name = "Beholder BeholdTV 407 FM",
4140 .audio_clock = 0x00187de7,
4141 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4142 .radio_type = UNSET,
4143 .tuner_addr = ADDR_UNSET,
4144 .radio_addr = ADDR_UNSET,
4145 .tda9887_conf = TDA9887_PRESENT,
4146 .gpiomask = 0x00008000,
4147 .inputs = {{
4148 .name = name_svideo,
4149 .vmux = 8,
4150 .amux = LINE1,
4151 .gpio = 0xc0c000,
4152 },{
4153 .name = name_comp1,
4154 .vmux = 1,
4155 .amux = LINE1,
4156 .gpio = 0xc0c000,
4157 },{
4158 .name = name_tv,
4159 .vmux = 3,
4160 .amux = TV,
4161 .tv = 1,
4162 .gpio = 0xc0c000,
4163 }},
4164 .radio = {
4165 .name = name_radio,
4166 .amux = LINE2,
4167 .gpio = 0xc0c000,
4168 },
4169 },
4170 [SAA7134_BOARD_BEHOLD_409] = {
4171 /* Beholder Intl. Ltd. 2008 */
4172 /*Dmitry Belimov <d.belimov@gmail.com> */
4173 .name = "Beholder BeholdTV 409",
4174 .audio_clock = 0x00187de7,
4175 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4176 .radio_type = UNSET,
4177 .tuner_addr = ADDR_UNSET,
4178 .radio_addr = ADDR_UNSET,
4179 .tda9887_conf = TDA9887_PRESENT,
4180 .gpiomask = 0x00008000,
4181 .inputs = {{
4182 .name = name_tv,
4183 .vmux = 3,
4184 .amux = TV,
4185 .tv = 1,
4186 },{
4187 .name = name_comp1,
4188 .vmux = 1,
4189 .amux = LINE1,
4190 },{
4191 .name = name_svideo,
4192 .vmux = 8,
4193 .amux = LINE1,
4194 }},
4195 },
4196 [SAA7134_BOARD_BEHOLD_505FM] = {
4197 /* Beholder Intl. Ltd. 2008 */
4198 /*Dmitry Belimov <d.belimov@gmail.com> */
4199 .name = "Beholder BeholdTV 505 FM",
4200 .audio_clock = 0x00200000,
4201 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4202 .radio_type = UNSET,
4203 .tuner_addr = ADDR_UNSET,
4204 .radio_addr = ADDR_UNSET,
4205 .tda9887_conf = TDA9887_PRESENT,
4206 .gpiomask = 0x00008000,
4207 .inputs = {{
4208 .name = name_tv,
4209 .vmux = 3,
4210 .amux = LINE2,
4211 .tv = 1,
4212 }, {
4213 .name = name_comp1,
4214 .vmux = 1,
4215 .amux = LINE1,
4216 }, {
4217 .name = name_svideo,
4218 .vmux = 8,
4219 .amux = LINE1,
4220 } },
4221 .mute = {
4222 .name = name_mute,
4223 .amux = LINE1,
4224 },
4225 .radio = {
4226 .name = name_radio,
4227 .amux = LINE2,
4228 },
4229 },
4230 [SAA7134_BOARD_BEHOLD_505RDS_MK5] = {
4231 /* Beholder Intl. Ltd. 2008 */
4232 /*Dmitry Belimov <d.belimov@gmail.com> */
4233 .name = "Beholder BeholdTV 505 RDS",
4234 .audio_clock = 0x00200000,
4235 .tuner_type = TUNER_PHILIPS_FM1216MK5,
4236 .radio_type = UNSET,
4237 .tuner_addr = ADDR_UNSET,
4238 .radio_addr = ADDR_UNSET,
4239 .rds_addr = 0x10,
4240 .tda9887_conf = TDA9887_PRESENT,
4241 .gpiomask = 0x00008000,
4242 .inputs = {{
4243 .name = name_tv,
4244 .vmux = 3,
4245 .amux = LINE2,
4246 .tv = 1,
4247 },{
4248 .name = name_comp1,
4249 .vmux = 1,
4250 .amux = LINE1,
4251 },{
4252 .name = name_svideo,
4253 .vmux = 8,
4254 .amux = LINE1,
4255 }},
4256 .mute = {
4257 .name = name_mute,
4258 .amux = LINE1,
4259 },
4260 .radio = {
4261 .name = name_radio,
4262 .amux = LINE2,
4263 },
4264 },
4265 [SAA7134_BOARD_BEHOLD_507_9FM] = {
4266 /* Beholder Intl. Ltd. 2008 */
4267 /*Dmitry Belimov <d.belimov@gmail.com> */
4268 .name = "Beholder BeholdTV 507 FM / BeholdTV 509 FM",
4269 .audio_clock = 0x00187de7,
4270 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4271 .radio_type = UNSET,
4272 .tuner_addr = ADDR_UNSET,
4273 .radio_addr = ADDR_UNSET,
4274 .tda9887_conf = TDA9887_PRESENT,
4275 .gpiomask = 0x00008000,
4276 .inputs = {{
4277 .name = name_tv,
4278 .vmux = 3,
4279 .amux = TV,
4280 .tv = 1,
4281 },{
4282 .name = name_comp1,
4283 .vmux = 1,
4284 .amux = LINE1,
4285 },{
4286 .name = name_svideo,
4287 .vmux = 8,
4288 .amux = LINE1,
4289 }},
4290 .radio = {
4291 .name = name_radio,
4292 .amux = LINE2,
4293 },
4294 },
4295 [SAA7134_BOARD_BEHOLD_507RDS_MK5] = {
4296 /* Beholder Intl. Ltd. 2008 */
4297 /*Dmitry Belimov <d.belimov@gmail.com> */
4298 .name = "Beholder BeholdTV 507 RDS",
4299 .audio_clock = 0x00187de7,
4300 .tuner_type = TUNER_PHILIPS_FM1216MK5,
4301 .radio_type = UNSET,
4302 .tuner_addr = ADDR_UNSET,
4303 .radio_addr = ADDR_UNSET,
4304 .rds_addr = 0x10,
4305 .tda9887_conf = TDA9887_PRESENT,
4306 .gpiomask = 0x00008000,
4307 .inputs = {{
4308 .name = name_tv,
4309 .vmux = 3,
4310 .amux = TV,
4311 .tv = 1,
4312 }, {
4313 .name = name_comp1,
4314 .vmux = 1,
4315 .amux = LINE1,
4316 }, {
4317 .name = name_svideo,
4318 .vmux = 8,
4319 .amux = LINE1,
4320 } },
4321 .radio = {
4322 .name = name_radio,
4323 .amux = LINE2,
4324 },
4325 },
4326 [SAA7134_BOARD_BEHOLD_507RDS_MK3] = {
4327 /* Beholder Intl. Ltd. 2008 */
4328 /*Dmitry Belimov <d.belimov@gmail.com> */
4329 .name = "Beholder BeholdTV 507 RDS",
4330 .audio_clock = 0x00187de7,
4331 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4332 .radio_type = UNSET,
4333 .tuner_addr = ADDR_UNSET,
4334 .radio_addr = ADDR_UNSET,
4335 .rds_addr = 0x10,
4336 .tda9887_conf = TDA9887_PRESENT,
4337 .gpiomask = 0x00008000,
4338 .inputs = {{
4339 .name = name_tv,
4340 .vmux = 3,
4341 .amux = TV,
4342 .tv = 1,
4343 }, {
4344 .name = name_comp1,
4345 .vmux = 1,
4346 .amux = LINE1,
4347 }, {
4348 .name = name_svideo,
4349 .vmux = 8,
4350 .amux = LINE1,
4351 } },
4352 .radio = {
4353 .name = name_radio,
4354 .amux = LINE2,
4355 },
4356 },
4357 [SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM] = {
4358 /* Beholder Intl. Ltd. 2008 */
4359 /* Dmitry Belimov <d.belimov@gmail.com> */
4360 .name = "Beholder BeholdTV Columbus TV/FM",
4361 .audio_clock = 0x00187de7,
4362 .tuner_type = TUNER_ALPS_TSBE5_PAL,
4363 .radio_type = TUNER_TEA5767,
4364 .tuner_addr = 0xc2 >> 1,
4365 .radio_addr = 0xc0 >> 1,
4366 .tda9887_conf = TDA9887_PRESENT,
4367 .gpiomask = 0x000A8004,
4368 .inputs = {{
4369 .name = name_tv,
4370 .vmux = 3,
4371 .amux = TV,
4372 .tv = 1,
4373 .gpio = 0x000A8004,
4374 }, {
4375 .name = name_comp1,
4376 .vmux = 1,
4377 .amux = LINE1,
4378 .gpio = 0x000A8000,
4379 }, {
4380 .name = name_svideo,
4381 .vmux = 8,
4382 .amux = LINE1,
4383 .gpio = 0x000A8000,
4384 } },
4385 .radio = {
4386 .name = name_radio,
4387 .amux = LINE2,
4388 .gpio = 0x000A8000,
4389 },
4390 },
4391 [SAA7134_BOARD_BEHOLD_607FM_MK3] = {
4392 /* Andrey Melnikoff <temnota@kmv.ru> */
4393 .name = "Beholder BeholdTV 607 FM",
4394 .audio_clock = 0x00187de7,
4395 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4396 .radio_type = UNSET,
4397 .tuner_addr = ADDR_UNSET,
4398 .radio_addr = ADDR_UNSET,
4399 .tda9887_conf = TDA9887_PRESENT,
4400 .inputs = {{
4401 .name = name_tv,
4402 .vmux = 3,
4403 .amux = TV,
4404 .tv = 1,
4405 }, {
4406 .name = name_comp1,
4407 .vmux = 1,
4408 .amux = LINE1,
4409 }, {
4410 .name = name_svideo,
4411 .vmux = 8,
4412 .amux = LINE1,
4413 } },
4414 .radio = {
4415 .name = name_radio,
4416 .amux = LINE2,
4417 },
4418 },
4419 [SAA7134_BOARD_BEHOLD_609FM_MK3] = {
4420 /* Andrey Melnikoff <temnota@kmv.ru> */
4421 .name = "Beholder BeholdTV 609 FM",
4422 .audio_clock = 0x00187de7,
4423 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4424 .radio_type = UNSET,
4425 .tuner_addr = ADDR_UNSET,
4426 .radio_addr = ADDR_UNSET,
4427 .tda9887_conf = TDA9887_PRESENT,
4428 .inputs = {{
4429 .name = name_tv,
4430 .vmux = 3,
4431 .amux = TV,
4432 .tv = 1,
4433 }, {
4434 .name = name_comp1,
4435 .vmux = 1,
4436 .amux = LINE1,
4437 }, {
4438 .name = name_svideo,
4439 .vmux = 8,
4440 .amux = LINE1,
4441 } },
4442 .radio = {
4443 .name = name_radio,
4444 .amux = LINE2,
4445 },
4446 },
4447 [SAA7134_BOARD_BEHOLD_607FM_MK5] = {
4448 /* Andrey Melnikoff <temnota@kmv.ru> */
4449 .name = "Beholder BeholdTV 607 FM",
4450 .audio_clock = 0x00187de7,
4451 .tuner_type = TUNER_PHILIPS_FM1216MK5,
4452 .radio_type = UNSET,
4453 .tuner_addr = ADDR_UNSET,
4454 .radio_addr = ADDR_UNSET,
4455 .tda9887_conf = TDA9887_PRESENT,
4456 .inputs = {{
4457 .name = name_tv,
4458 .vmux = 3,
4459 .amux = TV,
4460 .tv = 1,
4461 }, {
4462 .name = name_comp1,
4463 .vmux = 1,
4464 .amux = LINE1,
4465 }, {
4466 .name = name_svideo,
4467 .vmux = 8,
4468 .amux = LINE1,
4469 } },
4470 .radio = {
4471 .name = name_radio,
4472 .amux = LINE2,
4473 },
4474 },
4475 [SAA7134_BOARD_BEHOLD_609FM_MK5] = {
4476 /* Andrey Melnikoff <temnota@kmv.ru> */
4477 .name = "Beholder BeholdTV 609 FM",
4478 .audio_clock = 0x00187de7,
4479 .tuner_type = TUNER_PHILIPS_FM1216MK5,
4480 .radio_type = UNSET,
4481 .tuner_addr = ADDR_UNSET,
4482 .radio_addr = ADDR_UNSET,
4483 .tda9887_conf = TDA9887_PRESENT,
4484 .inputs = {{
4485 .name = name_tv,
4486 .vmux = 3,
4487 .amux = TV,
4488 .tv = 1,
4489 }, {
4490 .name = name_comp1,
4491 .vmux = 1,
4492 .amux = LINE1,
4493 }, {
4494 .name = name_svideo,
4495 .vmux = 8,
4496 .amux = LINE1,
4497 } },
4498 .radio = {
4499 .name = name_radio,
4500 .amux = LINE2,
4501 },
4502 },
4503 [SAA7134_BOARD_BEHOLD_607RDS_MK3] = {
4504 /* Andrey Melnikoff <temnota@kmv.ru> */
4505 .name = "Beholder BeholdTV 607 RDS",
4506 .audio_clock = 0x00187de7,
4507 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4508 .radio_type = UNSET,
4509 .tuner_addr = ADDR_UNSET,
4510 .radio_addr = ADDR_UNSET,
4511 .rds_addr = 0x10,
4512 .tda9887_conf = TDA9887_PRESENT,
4513 .inputs = {{
4514 .name = name_tv,
4515 .vmux = 3,
4516 .amux = TV,
4517 .tv = 1,
4518 }, {
4519 .name = name_comp1,
4520 .vmux = 1,
4521 .amux = LINE1,
4522 }, {
4523 .name = name_svideo,
4524 .vmux = 8,
4525 .amux = LINE1,
4526 } },
4527 .radio = {
4528 .name = name_radio,
4529 .amux = LINE2,
4530 },
4531 },
4532 [SAA7134_BOARD_BEHOLD_609RDS_MK3] = {
4533 /* Andrey Melnikoff <temnota@kmv.ru> */
4534 .name = "Beholder BeholdTV 609 RDS",
4535 .audio_clock = 0x00187de7,
4536 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4537 .radio_type = UNSET,
4538 .tuner_addr = ADDR_UNSET,
4539 .radio_addr = ADDR_UNSET,
4540 .rds_addr = 0x10,
4541 .tda9887_conf = TDA9887_PRESENT,
4542 .inputs = {{
4543 .name = name_tv,
4544 .vmux = 3,
4545 .amux = TV,
4546 .tv = 1,
4547 }, {
4548 .name = name_comp1,
4549 .vmux = 1,
4550 .amux = LINE1,
4551 }, {
4552 .name = name_svideo,
4553 .vmux = 8,
4554 .amux = LINE1,
4555 } },
4556 .radio = {
4557 .name = name_radio,
4558 .amux = LINE2,
4559 },
4560 },
4561 [SAA7134_BOARD_BEHOLD_607RDS_MK5] = {
4562 /* Andrey Melnikoff <temnota@kmv.ru> */
4563 .name = "Beholder BeholdTV 607 RDS",
4564 .audio_clock = 0x00187de7,
4565 .tuner_type = TUNER_PHILIPS_FM1216MK5,
4566 .radio_type = UNSET,
4567 .tuner_addr = ADDR_UNSET,
4568 .radio_addr = ADDR_UNSET,
4569 .rds_addr = 0x10,
4570 .tda9887_conf = TDA9887_PRESENT,
4571 .inputs = {{
4572 .name = name_tv,
4573 .vmux = 3,
4574 .amux = TV,
4575 .tv = 1,
4576 }, {
4577 .name = name_comp1,
4578 .vmux = 1,
4579 .amux = LINE1,
4580 }, {
4581 .name = name_svideo,
4582 .vmux = 8,
4583 .amux = LINE1,
4584 } },
4585 .radio = {
4586 .name = name_radio,
4587 .amux = LINE2,
4588 },
4589 },
4590 [SAA7134_BOARD_BEHOLD_609RDS_MK5] = {
4591 /* Andrey Melnikoff <temnota@kmv.ru> */
4592 .name = "Beholder BeholdTV 609 RDS",
4593 .audio_clock = 0x00187de7,
4594 .tuner_type = TUNER_PHILIPS_FM1216MK5,
4595 .radio_type = UNSET,
4596 .tuner_addr = ADDR_UNSET,
4597 .radio_addr = ADDR_UNSET,
4598 .rds_addr = 0x10,
4599 .tda9887_conf = TDA9887_PRESENT,
4600 .inputs = {{
4601 .name = name_tv,
4602 .vmux = 3,
4603 .amux = TV,
4604 .tv = 1,
4605 },{
4606 .name = name_comp1,
4607 .vmux = 1,
4608 .amux = LINE1,
4609 },{
4610 .name = name_svideo,
4611 .vmux = 8,
4612 .amux = LINE1,
4613 }},
4614 .radio = {
4615 .name = name_radio,
4616 .amux = LINE2,
4617 },
4618 },
4619 [SAA7134_BOARD_BEHOLD_M6] = {
4620 /* Igor Kuznetsov <igk@igk.ru> */
4621 /* Andrey Melnikoff <temnota@kmv.ru> */
4622 /* Beholder Intl. Ltd. Dmitry Belimov <d.belimov@gmail.com> */
4623 /* Alexey Osipov <lion-simba@pridelands.ru> */
4624 .name = "Beholder BeholdTV M6",
4625 .audio_clock = 0x00187de7,
4626 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4627 .radio_type = UNSET,
4628 .tuner_addr = ADDR_UNSET,
4629 .radio_addr = ADDR_UNSET,
4630 .empress_addr = 0x20,
4631 .tda9887_conf = TDA9887_PRESENT,
4632 .inputs = { {
4633 .name = name_tv,
4634 .vmux = 3,
4635 .amux = TV,
4636 .tv = 1,
4637 }, {
4638 .name = name_comp1,
4639 .vmux = 1,
4640 .amux = LINE1,
4641 }, {
4642 .name = name_svideo,
4643 .vmux = 8,
4644 .amux = LINE1,
4645 } },
4646 .radio = {
4647 .name = name_radio,
4648 .amux = LINE2,
4649 },
4650 .mpeg = SAA7134_MPEG_EMPRESS,
4651 .video_out = CCIR656,
4652 .vid_port_opts = (SET_T_CODE_POLARITY_NON_INVERTED |
4653 SET_CLOCK_NOT_DELAYED |
4654 SET_CLOCK_INVERTED |
4655 SET_VSYNC_OFF),
4656 },
4657 [SAA7134_BOARD_BEHOLD_M63] = {
4658 /* Igor Kuznetsov <igk@igk.ru> */
4659 /* Andrey Melnikoff <temnota@kmv.ru> */
4660 /* Beholder Intl. Ltd. Dmitry Belimov <d.belimov@gmail.com> */
4661 .name = "Beholder BeholdTV M63",
4662 .audio_clock = 0x00187de7,
4663 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
4664 .radio_type = UNSET,
4665 .tuner_addr = ADDR_UNSET,
4666 .radio_addr = ADDR_UNSET,
4667 .empress_addr = 0x20,
4668 .tda9887_conf = TDA9887_PRESENT,
4669 .inputs = { {
4670 .name = name_tv,
4671 .vmux = 3,
4672 .amux = TV,
4673 .tv = 1,
4674 }, {
4675 .name = name_comp1,
4676 .vmux = 1,
4677 .amux = LINE1,
4678 }, {
4679 .name = name_svideo,
4680 .vmux = 8,
4681 .amux = LINE1,
4682 } },
4683 .radio = {
4684 .name = name_radio,
4685 .amux = LINE2,
4686 },
4687 .mpeg = SAA7134_MPEG_EMPRESS,
4688 .video_out = CCIR656,
4689 .vid_port_opts = (SET_T_CODE_POLARITY_NON_INVERTED |
4690 SET_CLOCK_NOT_DELAYED |
4691 SET_CLOCK_INVERTED |
4692 SET_VSYNC_OFF),
4693 },
4694 [SAA7134_BOARD_BEHOLD_M6_EXTRA] = {
4695 /* Igor Kuznetsov <igk@igk.ru> */
4696 /* Andrey Melnikoff <temnota@kmv.ru> */
4697 /* Beholder Intl. Ltd. Dmitry Belimov <d.belimov@gmail.com> */
4698 /* Alexey Osipov <lion-simba@pridelands.ru> */
4699 .name = "Beholder BeholdTV M6 Extra",
4700 .audio_clock = 0x00187de7,
4701 .tuner_type = TUNER_PHILIPS_FM1216MK5,
4702 .radio_type = UNSET,
4703 .tuner_addr = ADDR_UNSET,
4704 .radio_addr = ADDR_UNSET,
4705 .rds_addr = 0x10,
4706 .empress_addr = 0x20,
4707 .tda9887_conf = TDA9887_PRESENT,
4708 .inputs = { {
4709 .name = name_tv,
4710 .vmux = 3,
4711 .amux = TV,
4712 .tv = 1,
4713 }, {
4714 .name = name_comp1,
4715 .vmux = 1,
4716 .amux = LINE1,
4717 }, {
4718 .name = name_svideo,
4719 .vmux = 8,
4720 .amux = LINE1,
4721 } },
4722 .radio = {
4723 .name = name_radio,
4724 .amux = LINE2,
4725 },
4726 .mpeg = SAA7134_MPEG_EMPRESS,
4727 .video_out = CCIR656,
4728 .vid_port_opts = (SET_T_CODE_POLARITY_NON_INVERTED |
4729 SET_CLOCK_NOT_DELAYED |
4730 SET_CLOCK_INVERTED |
4731 SET_VSYNC_OFF),
4732 },
4733 [SAA7134_BOARD_TWINHAN_DTV_DVB_3056] = {
4734 .name = "Twinhan Hybrid DTV-DVB 3056 PCI",
4735 .audio_clock = 0x00187de7,
4736 .tuner_type = TUNER_PHILIPS_TDA8290,
4737 .radio_type = UNSET,
4738 .tuner_addr = ADDR_UNSET,
4739 .radio_addr = ADDR_UNSET,
4740 .tuner_config = 2,
4741 .mpeg = SAA7134_MPEG_DVB,
4742 .gpiomask = 0x0200000,
4743 .inputs = {{
4744 .name = name_tv,
4745 .vmux = 1,
4746 .amux = TV,
4747 .tv = 1,
4748 }, {
4749 .name = name_comp1,
4750 .vmux = 3,
4751 .amux = LINE1,
4752 }, {
4753 .name = name_svideo,
4754 .vmux = 8, /* untested */
4755 .amux = LINE1,
4756 } },
4757 .radio = {
4758 .name = name_radio,
4759 .amux = TV,
4760 .gpio = 0x0200000,
4761 },
4762 },
4763 [SAA7134_BOARD_GENIUS_TVGO_A11MCE] = {
4764 /* Adrian Pardini <pardo.bsso@gmail.com> */
4765 .name = "Genius TVGO AM11MCE",
4766 .audio_clock = 0x00200000,
4767 .tuner_type = TUNER_TNF_5335MF,
4768 .radio_type = UNSET,
4769 .tuner_addr = ADDR_UNSET,
4770 .radio_addr = ADDR_UNSET,
4771 .gpiomask = 0xf000,
4772 .inputs = {{
4773 .name = name_tv_mono,
4774 .vmux = 1,
4775 .amux = LINE2,
4776 .gpio = 0x0000,
4777 .tv = 1,
4778 }, {
4779 .name = name_comp1,
4780 .vmux = 3,
4781 .amux = LINE1,
4782 .gpio = 0x2000,
4783 .tv = 1
4784 }, {
4785 .name = name_svideo,
4786 .vmux = 8,
4787 .amux = LINE1,
4788 .gpio = 0x2000,
4789 } },
4790 .radio = {
4791 .name = name_radio,
4792 .amux = LINE2,
4793 .gpio = 0x1000,
4794 },
4795 .mute = {
4796 .name = name_mute,
4797 .amux = LINE2,
4798 .gpio = 0x6000,
4799 },
4800 },
4801 [SAA7134_BOARD_PHILIPS_SNAKE] = {
4802 .name = "NXP Snake DVB-S reference design",
4803 .audio_clock = 0x00200000,
4804 .tuner_type = TUNER_ABSENT,
4805 .radio_type = UNSET,
4806 .tuner_addr = ADDR_UNSET,
4807 .radio_addr = ADDR_UNSET,
4808 .mpeg = SAA7134_MPEG_DVB,
4809 .inputs = {{
4810 .name = name_comp1,
4811 .vmux = 3,
4812 .amux = LINE1,
4813 }, {
4814 .name = name_svideo,
4815 .vmux = 8,
4816 .amux = LINE1,
4817 } },
4818 },
4819 [SAA7134_BOARD_CREATIX_CTX953] = {
4820 .name = "Medion/Creatix CTX953 Hybrid",
4821 .audio_clock = 0x00187de7,
4822 .tuner_type = TUNER_PHILIPS_TDA8290,
4823 .radio_type = UNSET,
4824 .tuner_addr = ADDR_UNSET,
4825 .radio_addr = ADDR_UNSET,
4826 .tuner_config = 0,
4827 .mpeg = SAA7134_MPEG_DVB,
4828 .inputs = {{
4829 .name = name_tv,
4830 .vmux = 1,
4831 .amux = TV,
4832 .tv = 1,
4833 }, {
4834 .name = name_comp1,
4835 .vmux = 0,
4836 .amux = LINE1,
4837 }, {
4838 .name = name_svideo,
4839 .vmux = 8,
4840 .amux = LINE1,
4841 } },
4842 },
4843 [SAA7134_BOARD_MSI_TVANYWHERE_AD11] = {
4844 .name = "MSI TV@nywhere A/D v1.1",
4845 .audio_clock = 0x00187de7,
4846 .tuner_type = TUNER_PHILIPS_TDA8290,
4847 .radio_type = UNSET,
4848 .tuner_addr = ADDR_UNSET,
4849 .radio_addr = ADDR_UNSET,
4850 .tuner_config = 2,
4851 .mpeg = SAA7134_MPEG_DVB,
4852 .gpiomask = 0x0200000,
4853 .inputs = { {
4854 .name = name_tv,
4855 .vmux = 1,
4856 .amux = TV,
4857 .tv = 1,
4858 }, {
4859 .name = name_comp1,
4860 .vmux = 3,
4861 .amux = LINE1,
4862 }, {
4863 .name = name_svideo,
4864 .vmux = 8,
4865 .amux = LINE1,
4866 } },
4867 .radio = {
4868 .name = name_radio,
4869 .amux = TV,
4870 .gpio = 0x0200000,
4871 },
4872 },
4873 [SAA7134_BOARD_AVERMEDIA_CARDBUS_506] = {
4874 .name = "AVerMedia Cardbus TV/Radio (E506R)",
4875 .audio_clock = 0x187de7,
4876 .tuner_type = TUNER_XC2028,
4877 .radio_type = UNSET,
4878 .tuner_addr = ADDR_UNSET,
4879 .radio_addr = ADDR_UNSET,
4880 .mpeg = SAA7134_MPEG_DVB,
4881 .inputs = {{
4882 .name = name_tv,
4883 .vmux = 1,
4884 .amux = TV,
4885 .tv = 1,
4886 }, {
4887 .name = name_comp1,
4888 .vmux = 3,
4889 .amux = LINE1,
4890 }, {
4891 .name = name_svideo,
4892 .vmux = 8,
4893 .amux = LINE2,
4894 } },
4895 .radio = {
4896 .name = name_radio,
4897 .amux = TV,
4898 },
4899 },
4900 [SAA7134_BOARD_AVERMEDIA_A16D] = {
4901 .name = "AVerMedia Hybrid TV/Radio (A16D)",
4902 .audio_clock = 0x187de7,
4903 .tuner_type = TUNER_XC2028,
4904 .radio_type = UNSET,
4905 .tuner_addr = ADDR_UNSET,
4906 .radio_addr = ADDR_UNSET,
4907 .mpeg = SAA7134_MPEG_DVB,
4908 .inputs = {{
4909 .name = name_tv,
4910 .vmux = 1,
4911 .amux = TV,
4912 .tv = 1,
4913 }, {
4914 .name = name_svideo,
4915 .vmux = 8,
4916 .amux = LINE1,
4917 }, {
4918 .name = name_comp,
4919 .vmux = 0,
4920 .amux = LINE1,
4921 } },
4922 .radio = {
4923 .name = name_radio,
4924 .amux = TV,
4925 },
4926 },
4927 [SAA7134_BOARD_AVERMEDIA_M115] = {
4928 .name = "Avermedia M115",
4929 .audio_clock = 0x187de7,
4930 .tuner_type = TUNER_XC2028,
4931 .radio_type = UNSET,
4932 .tuner_addr = ADDR_UNSET,
4933 .radio_addr = ADDR_UNSET,
4934 .inputs = {{
4935 .name = name_tv,
4936 .vmux = 1,
4937 .amux = TV,
4938 .tv = 1,
4939 }, {
4940 .name = name_comp1,
4941 .vmux = 3,
4942 .amux = LINE1,
4943 }, {
4944 .name = name_svideo,
4945 .vmux = 8,
4946 .amux = LINE2,
4947 } },
4948 },
4949 [SAA7134_BOARD_VIDEOMATE_T750] = {
4950 /* John Newbigin <jn@it.swin.edu.au> */
4951 .name = "Compro VideoMate T750",
4952 .audio_clock = 0x00187de7,
4953 .tuner_type = TUNER_XC2028,
4954 .radio_type = UNSET,
4955 .tuner_addr = 0x61,
4956 .radio_addr = ADDR_UNSET,
4957 .mpeg = SAA7134_MPEG_DVB,
4958 .inputs = {{
4959 .name = name_tv,
4960 .vmux = 3,
4961 .amux = TV,
4962 .tv = 1,
4963 }, {
4964 .name = name_comp1,
4965 .vmux = 1,
4966 .amux = LINE2,
4967 }, {
4968 .name = name_svideo,
4969 .vmux = 8,
4970 .amux = LINE2,
4971 } },
4972 .radio = {
4973 .name = name_radio,
4974 .amux = TV,
4975 }
4976 },
4977 [SAA7134_BOARD_AVERMEDIA_A700_PRO] = {
4978 /* Matthias Schwarzott <zzam@gentoo.org> */
4979 .name = "Avermedia DVB-S Pro A700",
4980 .audio_clock = 0x00187de7,
4981 .tuner_type = TUNER_ABSENT,
4982 .radio_type = UNSET,
4983 .tuner_addr = ADDR_UNSET,
4984 .radio_addr = ADDR_UNSET,
4985 .mpeg = SAA7134_MPEG_DVB,
4986 .inputs = { {
4987 .name = name_comp,
4988 .vmux = 1,
4989 .amux = LINE1,
4990 }, {
4991 .name = name_svideo,
4992 .vmux = 6,
4993 .amux = LINE1,
4994 } },
4995 },
4996 [SAA7134_BOARD_AVERMEDIA_A700_HYBRID] = {
4997 /* Matthias Schwarzott <zzam@gentoo.org> */
4998 .name = "Avermedia DVB-S Hybrid+FM A700",
4999 .audio_clock = 0x00187de7,
5000 .tuner_type = TUNER_XC2028,
5001 .radio_type = UNSET,
5002 .tuner_addr = ADDR_UNSET,
5003 .radio_addr = ADDR_UNSET,
5004 .mpeg = SAA7134_MPEG_DVB,
5005 .inputs = { {
5006 .name = name_tv,
5007 .vmux = 4,
5008 .amux = TV,
5009 .tv = 1,
5010 }, {
5011 .name = name_comp,
5012 .vmux = 1,
5013 .amux = LINE1,
5014 }, {
5015 .name = name_svideo,
5016 .vmux = 6,
5017 .amux = LINE1,
5018 } },
5019 .radio = {
5020 .name = name_radio,
5021 .amux = TV,
5022 },
5023 },
5024 [SAA7134_BOARD_BEHOLD_H6] = {
5025 /* Igor Kuznetsov <igk@igk.ru> */
5026 .name = "Beholder BeholdTV H6",
5027 .audio_clock = 0x00187de7,
5028 .tuner_type = TUNER_PHILIPS_FMD1216MEX_MK3,
5029 .radio_type = UNSET,
5030 .tuner_addr = ADDR_UNSET,
5031 .radio_addr = ADDR_UNSET,
5032 .tda9887_conf = TDA9887_PRESENT,
5033 .mpeg = SAA7134_MPEG_DVB,
5034 .inputs = {{
5035 .name = name_tv,
5036 .vmux = 3,
5037 .amux = TV,
5038 .tv = 1,
5039 }, {
5040 .name = name_comp1,
5041 .vmux = 1,
5042 .amux = LINE1,
5043 }, {
5044 .name = name_svideo,
5045 .vmux = 8,
5046 .amux = LINE1,
5047 } },
5048 .radio = {
5049 .name = name_radio,
5050 .amux = LINE2,
5051 },
5052 },
5053 [SAA7134_BOARD_ASUSTeK_TIGER_3IN1] = {
5054 .name = "Asus Tiger 3in1",
5055 .audio_clock = 0x00187de7,
5056 .tuner_type = TUNER_PHILIPS_TDA8290,
5057 .radio_type = UNSET,
5058 .tuner_addr = ADDR_UNSET,
5059 .radio_addr = ADDR_UNSET,
5060 .tuner_config = 2,
5061 .gpiomask = 1 << 21,
5062 .mpeg = SAA7134_MPEG_DVB,
5063 .inputs = {{
5064 .name = name_tv,
5065 .vmux = 1,
5066 .amux = TV,
5067 .tv = 1,
5068 }, {
5069 .name = name_comp,
5070 .vmux = 0,
5071 .amux = LINE2,
5072 }, {
5073 .name = name_svideo,
5074 .vmux = 8,
5075 .amux = LINE2,
5076 } },
5077 .radio = {
5078 .name = name_radio,
5079 .amux = TV,
5080 .gpio = 0x0200000,
5081 },
5082 },
5083 [SAA7134_BOARD_ASUSTeK_PS3_100] = {
5084 .name = "Asus My Cinema PS3-100",
5085 .audio_clock = 0x00187de7,
5086 .tuner_type = TUNER_PHILIPS_TDA8290,
5087 .radio_type = UNSET,
5088 .tuner_addr = ADDR_UNSET,
5089 .radio_addr = ADDR_UNSET,
5090 .tuner_config = 2,
5091 .gpiomask = 1 << 21,
5092 .mpeg = SAA7134_MPEG_DVB,
5093 .inputs = {{
5094 .name = name_tv,
5095 .vmux = 1,
5096 .amux = TV,
5097 .tv = 1,
5098 }, {
5099 .name = name_comp,
5100 .vmux = 0,
5101 .amux = LINE2,
5102 }, {
5103 .name = name_svideo,
5104 .vmux = 8,
5105 .amux = LINE2,
5106 } },
5107 .radio = {
5108 .name = name_radio,
5109 .amux = TV,
5110 .gpio = 0x0200000,
5111 },
5112 },
5113 [SAA7134_BOARD_REAL_ANGEL_220] = {
5114 .name = "Zogis Real Angel 220",
5115 .audio_clock = 0x00187de7,
5116 .tuner_type = TUNER_TNF_5335MF,
5117 .radio_type = UNSET,
5118 .tuner_addr = ADDR_UNSET,
5119 .radio_addr = ADDR_UNSET,
5120 .gpiomask = 0x801a8087,
5121 .inputs = { {
5122 .name = name_tv,
5123 .vmux = 3,
5124 .amux = LINE2,
5125 .tv = 1,
5126 .gpio = 0x624000,
5127 }, {
5128 .name = name_comp1,
5129 .vmux = 1,
5130 .amux = LINE1,
5131 .gpio = 0x624000,
5132 }, {
5133 .name = name_svideo,
5134 .vmux = 1,
5135 .amux = LINE1,
5136 .gpio = 0x624000,
5137 } },
5138 .radio = {
5139 .name = name_radio,
5140 .amux = LINE2,
5141 .gpio = 0x624001,
5142 },
5143 .mute = {
5144 .name = name_mute,
5145 .amux = TV,
5146 },
5147 },
5148 [SAA7134_BOARD_ADS_INSTANT_HDTV_PCI] = {
5149 .name = "ADS Tech Instant HDTV",
5150 .audio_clock = 0x00187de7,
5151 .tuner_type = TUNER_PHILIPS_TUV1236D,
5152 .radio_type = UNSET,
5153 .tuner_addr = ADDR_UNSET,
5154 .radio_addr = ADDR_UNSET,
5155 .tda9887_conf = TDA9887_PRESENT,
5156 .mpeg = SAA7134_MPEG_DVB,
5157 .inputs = { {
5158 .name = name_tv,
5159 .vmux = 1,
5160 .amux = TV,
5161 .tv = 1,
5162 }, {
5163 .name = name_comp,
5164 .vmux = 4,
5165 .amux = LINE1,
5166 }, {
5167 .name = name_svideo,
5168 .vmux = 8,
5169 .amux = LINE1,
5170 } },
5171 },
5172 [SAA7134_BOARD_ASUSTeK_TIGER] = {
5173 .name = "Asus Tiger Rev:1.00",
5174 .audio_clock = 0x00187de7,
5175 .tuner_type = TUNER_PHILIPS_TDA8290,
5176 .radio_type = UNSET,
5177 .tuner_addr = ADDR_UNSET,
5178 .radio_addr = ADDR_UNSET,
5179 .tuner_config = 0,
5180 .mpeg = SAA7134_MPEG_DVB,
5181 .gpiomask = 0x0200000,
5182 .inputs = { {
5183 .name = name_tv,
5184 .vmux = 1,
5185 .amux = TV,
5186 .tv = 1,
5187 }, {
5188 .name = name_comp1,
5189 .vmux = 3,
5190 .amux = LINE2,
5191 }, {
5192 .name = name_comp2,
5193 .vmux = 0,
5194 .amux = LINE2,
5195 }, {
5196 .name = name_svideo,
5197 .vmux = 8,
5198 .amux = LINE2,
5199 } },
5200 .radio = {
5201 .name = name_radio,
5202 .amux = TV,
5203 .gpio = 0x0200000,
5204 },
5205 },
5206 [SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG] = {
5207 .name = "Kworld Plus TV Analog Lite PCI",
5208 .audio_clock = 0x00187de7,
5209 .tuner_type = TUNER_YMEC_TVF_5533MF,
5210 .radio_type = TUNER_TEA5767,
5211 .tuner_addr = ADDR_UNSET,
5212 .radio_addr = 0x60,
5213 .gpiomask = 0x80000700,
5214 .inputs = { {
5215 .name = name_tv,
5216 .vmux = 1,
5217 .amux = LINE2,
5218 .tv = 1,
5219 .gpio = 0x100,
5220 }, {
5221 .name = name_comp1,
5222 .vmux = 3,
5223 .amux = LINE1,
5224 .gpio = 0x200,
5225 }, {
5226 .name = name_svideo,
5227 .vmux = 8,
5228 .amux = LINE1,
5229 .gpio = 0x200,
5230 } },
5231 .radio = {
5232 .name = name_radio,
5233 .vmux = 1,
5234 .amux = LINE1,
5235 .gpio = 0x100,
5236 },
5237 .mute = {
5238 .name = name_mute,
5239 .vmux = 8,
5240 .amux = 2,
5241 },
5242 },
5243 [SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG] = {
5244 .name = "Kworld PCI SBTVD/ISDB-T Full-Seg Hybrid",
5245 .audio_clock = 0x00187de7,
5246 .tuner_type = TUNER_PHILIPS_TDA8290,
5247 .tuner_addr = ADDR_UNSET,
5248 .radio_type = UNSET,
5249 .radio_addr = ADDR_UNSET,
5250 .gpiomask = 0x8e054000,
5251 .mpeg = SAA7134_MPEG_DVB,
5252 .ts_type = SAA7134_MPEG_TS_PARALLEL,
5253 .inputs = { {
5254 .name = name_tv,
5255 .vmux = 1,
5256 .amux = TV,
5257 .tv = 1,
5258#if 0 /* FIXME */
5259 }, {
5260 .name = name_comp1,
5261 .vmux = 3,
5262 .amux = LINE1,
5263 .gpio = 0x200,
5264 }, {
5265 .name = name_svideo,
5266 .vmux = 8,
5267 .amux = LINE1,
5268 .gpio = 0x200,
5269#endif
5270 } },
5271#if 0
5272 .radio = {
5273 .name = name_radio,
5274 .vmux = 1,
5275 .amux = LINE1,
5276 .gpio = 0x100,
5277 },
5278#endif
5279 .mute = {
5280 .name = name_mute,
5281 .vmux = 0,
5282 .amux = TV,
5283 },
5284 },
5285 [SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS] = {
5286 .name = "Avermedia AVerTV GO 007 FM Plus",
5287 .audio_clock = 0x00187de7,
5288 .tuner_type = TUNER_PHILIPS_TDA8290,
5289 .radio_type = UNSET,
5290 .tuner_addr = ADDR_UNSET,
5291 .radio_addr = ADDR_UNSET,
5292 .gpiomask = 0x00300003,
5293 /* .gpiomask = 0x8c240003, */
5294 .inputs = { {
5295 .name = name_tv,
5296 .vmux = 1,
5297 .amux = TV,
5298 .tv = 1,
5299 .gpio = 0x01,
5300 }, {
5301 .name = name_svideo,
5302 .vmux = 6,
5303 .amux = LINE1,
5304 .gpio = 0x02,
5305 } },
5306 .radio = {
5307 .name = name_radio,
5308 .amux = TV,
5309 .gpio = 0x00300001,
5310 },
5311 .mute = {
5312 .name = name_mute,
5313 .amux = TV,
5314 .gpio = 0x01,
5315 },
5316 },
5317 [SAA7134_BOARD_AVERMEDIA_STUDIO_507UA] = {
5318 /* Andy Shevchenko <andy@smile.org.ua> */
5319 .name = "Avermedia AVerTV Studio 507UA",
5320 .audio_clock = 0x00187de7,
5321 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3, /* Should be MK5 */
5322 .radio_type = UNSET,
5323 .tuner_addr = ADDR_UNSET,
5324 .radio_addr = ADDR_UNSET,
5325 .tda9887_conf = TDA9887_PRESENT,
5326 .gpiomask = 0x03,
5327 .inputs = { {
5328 .name = name_tv,
5329 .vmux = 1,
5330 .amux = TV,
5331 .tv = 1,
5332 .gpio = 0x00,
5333 }, {
5334 .name = name_comp1,
5335 .vmux = 3,
5336 .amux = LINE1,
5337 .gpio = 0x00,
5338 }, {
5339 .name = name_svideo,
5340 .vmux = 8,
5341 .amux = LINE1,
5342 .gpio = 0x00,
5343 } },
5344 .radio = {
5345 .name = name_radio,
5346 .amux = LINE2,
5347 .gpio = 0x01,
5348 },
5349 .mute = {
5350 .name = name_mute,
5351 .amux = LINE1,
5352 .gpio = 0x00,
5353 },
5354 },
5355 [SAA7134_BOARD_VIDEOMATE_S350] = {
5356 /* Jan D. Louw <jd.louw@mweb.co.za */
5357 .name = "Compro VideoMate S350/S300",
5358 .audio_clock = 0x00187de7,
5359 .tuner_type = TUNER_ABSENT,
5360 .radio_type = UNSET,
5361 .tuner_addr = ADDR_UNSET,
5362 .radio_addr = ADDR_UNSET,
5363 .mpeg = SAA7134_MPEG_DVB,
5364 .inputs = { {
5365 .name = name_comp1,
5366 .vmux = 0,
5367 .amux = LINE1,
5368 }, {
5369 .name = name_svideo,
5370 .vmux = 8, /* Not tested */
5371 .amux = LINE1
5372 } },
5373 },
5374 [SAA7134_BOARD_BEHOLD_X7] = {
5375 /* Beholder Intl. Ltd. Dmitry Belimov <d.belimov@gmail.com> */
5376 .name = "Beholder BeholdTV X7",
5377 .audio_clock = 0x00187de7,
5378 .tuner_type = TUNER_XC5000,
5379 .radio_type = UNSET,
5380 .tuner_addr = ADDR_UNSET,
5381 .radio_addr = ADDR_UNSET,
5382 .mpeg = SAA7134_MPEG_DVB,
5383 .inputs = { {
5384 .name = name_tv,
5385 .vmux = 2,
5386 .amux = TV,
5387 .tv = 1,
5388 }, {
5389 .name = name_comp1,
5390 .vmux = 0,
5391 .amux = LINE1,
5392 }, {
5393 .name = name_svideo,
5394 .vmux = 9,
5395 .amux = LINE1,
5396 } },
5397 .radio = {
5398 .name = name_radio,
5399 .amux = TV,
5400 },
5401 },
5402 [SAA7134_BOARD_ZOLID_HYBRID_PCI] = {
5403 .name = "Zolid Hybrid TV Tuner PCI",
5404 .audio_clock = 0x00187de7,
5405 .tuner_type = TUNER_PHILIPS_TDA8290,
5406 .radio_type = UNSET,
5407 .tuner_addr = ADDR_UNSET,
5408 .radio_addr = ADDR_UNSET,
5409 .tuner_config = 0,
5410 .mpeg = SAA7134_MPEG_DVB,
5411 .ts_type = SAA7134_MPEG_TS_PARALLEL,
5412 .inputs = {{
5413 .name = name_tv,
5414 .vmux = 1,
5415 .amux = TV,
5416 .tv = 1,
5417 } },
5418 .radio = { /* untested */
5419 .name = name_radio,
5420 .amux = TV,
5421 },
5422 },
5423 [SAA7134_BOARD_ASUS_EUROPA_HYBRID] = {
5424 .name = "Asus Europa Hybrid OEM",
5425 .audio_clock = 0x00187de7,
5426 .tuner_type = TUNER_PHILIPS_TD1316,
5427 .radio_type = UNSET,
5428 .tuner_addr = 0x61,
5429 .radio_addr = ADDR_UNSET,
5430 .tda9887_conf = TDA9887_PRESENT | TDA9887_PORT1_ACTIVE,
5431 .mpeg = SAA7134_MPEG_DVB,
5432 .inputs = { {
5433 .name = name_tv,
5434 .vmux = 3,
5435 .amux = TV,
5436 .tv = 1,
5437 }, {
5438 .name = name_comp1,
5439 .vmux = 4,
5440 .amux = LINE2,
5441 }, {
5442 .name = name_svideo,
5443 .vmux = 8,
5444 .amux = LINE2,
5445 } },
5446 },
5447 [SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S] = {
5448 .name = "Leadtek Winfast DTV1000S",
5449 .audio_clock = 0x00187de7,
5450 .tuner_type = TUNER_PHILIPS_TDA8290,
5451 .radio_type = UNSET,
5452 .tuner_addr = ADDR_UNSET,
5453 .radio_addr = ADDR_UNSET,
5454 .mpeg = SAA7134_MPEG_DVB,
5455 .inputs = { {
5456 .name = name_comp1,
5457 .vmux = 3,
5458 }, {
5459 .name = name_svideo,
5460 .vmux = 8,
5461 } },
5462 },
5463 [SAA7134_BOARD_BEHOLD_505RDS_MK3] = {
5464 /* Beholder Intl. Ltd. 2008 */
5465 /*Dmitry Belimov <d.belimov@gmail.com> */
5466 .name = "Beholder BeholdTV 505 RDS",
5467 .audio_clock = 0x00200000,
5468 .tuner_type = TUNER_PHILIPS_FM1216ME_MK3,
5469 .radio_type = UNSET,
5470 .tuner_addr = ADDR_UNSET,
5471 .radio_addr = ADDR_UNSET,
5472 .rds_addr = 0x10,
5473 .tda9887_conf = TDA9887_PRESENT,
5474 .gpiomask = 0x00008000,
5475 .inputs = {{
5476 .name = name_tv,
5477 .vmux = 3,
5478 .amux = LINE2,
5479 .tv = 1,
5480 }, {
5481 .name = name_comp1,
5482 .vmux = 1,
5483 .amux = LINE1,
5484 }, {
5485 .name = name_svideo,
5486 .vmux = 8,
5487 .amux = LINE1,
5488 } },
5489 .mute = {
5490 .name = name_mute,
5491 .amux = LINE1,
5492 },
5493 .radio = {
5494 .name = name_radio,
5495 .amux = LINE2,
5496 },
5497 },
5498 [SAA7134_BOARD_HAWELL_HW_404M7] = {
5499 /* Hawell HW-404M7 & Hawell HW-808M7 */
5500 /* Bogoslovskiy Viktor <bogovic@bk.ru> */
5501 .name = "Hawell HW-404M7",
5502 .audio_clock = 0x00200000,
5503 .tuner_type = UNSET,
5504 .radio_type = UNSET,
5505 .tuner_addr = ADDR_UNSET,
5506 .radio_addr = ADDR_UNSET,
5507 .gpiomask = 0x389c00,
5508 .inputs = {{
5509 .name = name_comp1,
5510 .vmux = 3,
5511 .amux = LINE1,
5512 .gpio = 0x01fc00,
5513 } },
5514 },
5515 [SAA7134_BOARD_BEHOLD_H7] = {
5516 /* Beholder Intl. Ltd. Dmitry Belimov <d.belimov@gmail.com> */
5517 .name = "Beholder BeholdTV H7",
5518 .audio_clock = 0x00187de7,
5519 .tuner_type = TUNER_XC5000,
5520 .radio_type = UNSET,
5521 .tuner_addr = ADDR_UNSET,
5522 .radio_addr = ADDR_UNSET,
5523 .mpeg = SAA7134_MPEG_DVB,
5524 .ts_type = SAA7134_MPEG_TS_PARALLEL,
5525 .inputs = { {
5526 .name = name_tv,
5527 .vmux = 2,
5528 .amux = TV,
5529 .tv = 1,
5530 }, {
5531 .name = name_comp1,
5532 .vmux = 0,
5533 .amux = LINE1,
5534 }, {
5535 .name = name_svideo,
5536 .vmux = 9,
5537 .amux = LINE1,
5538 } },
5539 .radio = {
5540 .name = name_radio,
5541 .amux = TV,
5542 },
5543 },
5544 [SAA7134_BOARD_BEHOLD_A7] = {
5545 /* Beholder Intl. Ltd. Dmitry Belimov <d.belimov@gmail.com> */
5546 .name = "Beholder BeholdTV A7",
5547 .audio_clock = 0x00187de7,
5548 .tuner_type = TUNER_XC5000,
5549 .radio_type = UNSET,
5550 .tuner_addr = ADDR_UNSET,
5551 .radio_addr = ADDR_UNSET,
5552 .inputs = { {
5553 .name = name_tv,
5554 .vmux = 2,
5555 .amux = TV,
5556 .tv = 1,
5557 }, {
5558 .name = name_comp1,
5559 .vmux = 0,
5560 .amux = LINE1,
5561 }, {
5562 .name = name_svideo,
5563 .vmux = 9,
5564 .amux = LINE1,
5565 } },
5566 .radio = {
5567 .name = name_radio,
5568 .amux = TV,
5569 },
5570 },
5571 [SAA7134_BOARD_TECHNOTREND_BUDGET_T3000] = {
5572 .name = "TechoTrend TT-budget T-3000",
5573 .tuner_type = TUNER_PHILIPS_TD1316,
5574 .audio_clock = 0x00187de7,
5575 .radio_type = UNSET,
5576 .tuner_addr = 0x63,
5577 .radio_addr = ADDR_UNSET,
5578 .tda9887_conf = TDA9887_PRESENT | TDA9887_PORT1_ACTIVE,
5579 .mpeg = SAA7134_MPEG_DVB,
5580 .inputs = {{
5581 .name = name_tv,
5582 .vmux = 3,
5583 .amux = TV,
5584 .tv = 1,
5585 }, {
5586 .name = name_comp1,
5587 .vmux = 0,
5588 .amux = LINE2,
5589 }, {
5590 .name = name_svideo,
5591 .vmux = 8,
5592 .amux = LINE2,
5593 } },
5594 },
5595 [SAA7134_BOARD_VIDEOMATE_M1F] = {
5596 /* Pavel Osnova <pvosnova@gmail.com> */
5597 .name = "Compro VideoMate Vista M1F",
5598 .audio_clock = 0x00187de7,
5599 .tuner_type = TUNER_LG_PAL_NEW_TAPC,
5600 .radio_type = TUNER_TEA5767,
5601 .tuner_addr = ADDR_UNSET,
5602 .radio_addr = 0x60,
5603 .inputs = { {
5604 .name = name_tv,
5605 .vmux = 1,
5606 .amux = TV,
5607 .tv = 1,
5608 }, {
5609 .name = name_comp1,
5610 .vmux = 3,
5611 .amux = LINE2,
5612 }, {
5613 .name = name_svideo,
5614 .vmux = 8,
5615 .amux = LINE2,
5616 } },
5617 .radio = {
5618 .name = name_radio,
5619 .amux = LINE1,
5620 },
5621 .mute = {
5622 .name = name_mute,
5623 .amux = TV,
5624 },
5625 },
5626 [SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2] = {
5627 /* Timothy Lee <timothy.lee@siriushk.com> */
5628 .name = "MagicPro ProHDTV Pro2 DMB-TH/Hybrid",
5629 .audio_clock = 0x00187de7,
5630 .tuner_type = TUNER_PHILIPS_TDA8290,
5631 .radio_type = UNSET,
5632 .tuner_config = 3,
5633 .tuner_addr = ADDR_UNSET,
5634 .radio_addr = ADDR_UNSET,
5635 .gpiomask = 0x02050000,
5636 .mpeg = SAA7134_MPEG_DVB,
5637 .ts_type = SAA7134_MPEG_TS_PARALLEL,
5638 .inputs = { {
5639 .name = name_tv,
5640 .vmux = 1,
5641 .amux = TV,
5642 .tv = 1,
5643 .gpio = 0x00050000,
5644 }, {
5645 .name = name_comp1,
5646 .vmux = 3,
5647 .amux = LINE1,
5648 .gpio = 0x00050000,
5649 }, {
5650 .name = name_svideo,
5651 .vmux = 8,
5652 .amux = LINE1,
5653 .gpio = 0x00050000,
5654 } },
5655 .radio = {
5656 .name = name_radio,
5657 .amux = TV,
5658 .gpio = 0x00050000,
5659 },
5660 .mute = {
5661 .name = name_mute,
5662 .vmux = 0,
5663 .amux = TV,
5664 .gpio = 0x00050000,
5665 },
5666 },
5667 [SAA7134_BOARD_BEHOLD_501] = {
5668 /* Beholder Intl. Ltd. 2010 */
5669 /* Dmitry Belimov <d.belimov@gmail.com> */
5670 .name = "Beholder BeholdTV 501",
5671 .audio_clock = 0x00200000,
5672 .tuner_type = TUNER_ABSENT,
5673 .radio_type = UNSET,
5674 .tuner_addr = ADDR_UNSET,
5675 .radio_addr = ADDR_UNSET,
5676 .gpiomask = 0x00008000,
5677 .inputs = { {
5678 .name = name_tv,
5679 .vmux = 3,
5680 .amux = LINE2,
5681 .tv = 1,
5682 }, {
5683 .name = name_comp1,
5684 .vmux = 1,
5685 .amux = LINE1,
5686 }, {
5687 .name = name_svideo,
5688 .vmux = 8,
5689 .amux = LINE1,
5690 } },
5691 .mute = {
5692 .name = name_mute,
5693 .amux = LINE1,
5694 },
5695 },
5696 [SAA7134_BOARD_BEHOLD_503FM] = {
5697 /* Beholder Intl. Ltd. 2010 */
5698 /* Dmitry Belimov <d.belimov@gmail.com> */
5699 .name = "Beholder BeholdTV 503 FM",
5700 .audio_clock = 0x00200000,
5701 .tuner_type = TUNER_ABSENT,
5702 .radio_type = UNSET,
5703 .tuner_addr = ADDR_UNSET,
5704 .radio_addr = ADDR_UNSET,
5705 .gpiomask = 0x00008000,
5706 .inputs = { {
5707 .name = name_tv,
5708 .vmux = 3,
5709 .amux = LINE2,
5710 .tv = 1,
5711 }, {
5712 .name = name_comp1,
5713 .vmux = 1,
5714 .amux = LINE1,
5715 }, {
5716 .name = name_svideo,
5717 .vmux = 8,
5718 .amux = LINE1,
5719 } },
5720 .mute = {
5721 .name = name_mute,
5722 .amux = LINE1,
5723 },
5724 },
5725 [SAA7134_BOARD_SENSORAY811_911] = {
5726 .name = "Sensoray 811/911",
5727 .audio_clock = 0x00200000,
5728 .tuner_type = TUNER_ABSENT,
5729 .radio_type = UNSET,
5730 .tuner_addr = ADDR_UNSET,
5731 .radio_addr = ADDR_UNSET,
5732 .inputs = {{
5733 .name = name_comp1,
5734 .vmux = 0,
5735 .amux = LINE1,
5736 }, {
5737 .name = name_comp3,
5738 .vmux = 2,
5739 .amux = LINE1,
5740 }, {
5741 .name = name_svideo,
5742 .vmux = 8,
5743 .amux = LINE1,
5744 } },
5745 },
5746 [SAA7134_BOARD_KWORLD_PC150U] = {
5747 .name = "Kworld PC150-U",
5748 .audio_clock = 0x00187de7,
5749 .tuner_type = TUNER_PHILIPS_TDA8290,
5750 .radio_type = UNSET,
5751 .tuner_addr = ADDR_UNSET,
5752 .radio_addr = ADDR_UNSET,
5753 .mpeg = SAA7134_MPEG_DVB,
5754 .gpiomask = 1 << 21,
5755 .ts_type = SAA7134_MPEG_TS_PARALLEL,
5756 .inputs = { {
5757 .name = name_tv,
5758 .vmux = 1,
5759 .amux = TV,
5760 .tv = 1,
5761 }, {
5762 .name = name_comp,
5763 .vmux = 3,
5764 .amux = LINE1,
5765 }, {
5766 .name = name_svideo,
5767 .vmux = 8,
5768 .amux = LINE2,
5769 } },
5770 .radio = {
5771 .name = name_radio,
5772 .amux = TV,
5773 .gpio = 0x0000000,
5774 },
5775 },
5776
5777};
5778
5779const unsigned int saa7134_bcount = ARRAY_SIZE(saa7134_boards);
5780
5781/* ------------------------------------------------------------------ */
5782/* PCI ids + subsystem IDs */
5783
5784struct pci_device_id saa7134_pci_tbl[] = {
5785 {
5786 .vendor = PCI_VENDOR_ID_PHILIPS,
5787 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5788 .subvendor = PCI_VENDOR_ID_PHILIPS,
5789 .subdevice = 0x2001,
5790 .driver_data = SAA7134_BOARD_PROTEUS_PRO,
5791 },{
5792 .vendor = PCI_VENDOR_ID_PHILIPS,
5793 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5794 .subvendor = PCI_VENDOR_ID_PHILIPS,
5795 .subdevice = 0x2001,
5796 .driver_data = SAA7134_BOARD_PROTEUS_PRO,
5797 },{
5798 .vendor = PCI_VENDOR_ID_PHILIPS,
5799 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5800 .subvendor = PCI_VENDOR_ID_PHILIPS,
5801 .subdevice = 0x6752,
5802 .driver_data = SAA7134_BOARD_EMPRESS,
5803 },{
5804 .vendor = PCI_VENDOR_ID_PHILIPS,
5805 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5806 .subvendor = 0x1131,
5807 .subdevice = 0x4e85,
5808 .driver_data = SAA7134_BOARD_MONSTERTV,
5809 },{
5810 .vendor = PCI_VENDOR_ID_PHILIPS,
5811 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5812 .subvendor = 0x153b,
5813 .subdevice = 0x1142,
5814 .driver_data = SAA7134_BOARD_CINERGY400,
5815 },{
5816 .vendor = PCI_VENDOR_ID_PHILIPS,
5817 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5818 .subvendor = 0x153b,
5819 .subdevice = 0x1143,
5820 .driver_data = SAA7134_BOARD_CINERGY600,
5821 },{
5822 .vendor = PCI_VENDOR_ID_PHILIPS,
5823 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5824 .subvendor = 0x153b,
5825 .subdevice = 0x1158,
5826 .driver_data = SAA7134_BOARD_CINERGY600_MK3,
5827 },{
5828 .vendor = PCI_VENDOR_ID_PHILIPS,
5829 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5830 .subvendor = 0x153b,
5831 .subdevice = 0x1162,
5832 .driver_data = SAA7134_BOARD_CINERGY400_CARDBUS,
5833 },{
5834 .vendor = PCI_VENDOR_ID_PHILIPS,
5835 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5836 .subvendor = 0x5169,
5837 .subdevice = 0x0138,
5838 .driver_data = SAA7134_BOARD_FLYVIDEO3000_NTSC,
5839 },{
5840 .vendor = PCI_VENDOR_ID_PHILIPS,
5841 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5842 .subvendor = 0x5168,
5843 .subdevice = 0x0138,
5844 .driver_data = SAA7134_BOARD_FLYVIDEO3000,
5845 },{
5846 .vendor = PCI_VENDOR_ID_PHILIPS,
5847 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5848 .subvendor = 0x4e42, /* "Typhoon PCI Capture TV Card" Art.No. 50673 */
5849 .subdevice = 0x0138,
5850 .driver_data = SAA7134_BOARD_FLYVIDEO3000,
5851 },{
5852 .vendor = PCI_VENDOR_ID_PHILIPS,
5853 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
5854 .subvendor = 0x5168,
5855 .subdevice = 0x0138,
5856 .driver_data = SAA7134_BOARD_FLYVIDEO2000,
5857 },{
5858 .vendor = PCI_VENDOR_ID_PHILIPS,
5859 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
5860 .subvendor = 0x4e42, /* Typhoon */
5861 .subdevice = 0x0138, /* LifeView FlyTV Prime30 OEM */
5862 .driver_data = SAA7134_BOARD_FLYVIDEO2000,
5863 },{
5864 .vendor = PCI_VENDOR_ID_PHILIPS,
5865 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5866 .subvendor = 0x5168,
5867 .subdevice = 0x0212, /* minipci, LR212 */
5868 .driver_data = SAA7134_BOARD_FLYTVPLATINUM_MINI,
5869 },{
5870 .vendor = PCI_VENDOR_ID_PHILIPS,
5871 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5872 .subvendor = 0x14c0,
5873 .subdevice = 0x1212, /* minipci, LR1212 */
5874 .driver_data = SAA7134_BOARD_FLYTVPLATINUM_MINI2,
5875 },{
5876 .vendor = PCI_VENDOR_ID_PHILIPS,
5877 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5878 .subvendor = 0x4e42,
5879 .subdevice = 0x0212, /* OEM minipci, LR212 */
5880 .driver_data = SAA7134_BOARD_FLYTVPLATINUM_MINI,
5881 },{
5882 .vendor = PCI_VENDOR_ID_PHILIPS,
5883 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5884 .subvendor = 0x5168, /* Animation Technologies (LifeView) */
5885 .subdevice = 0x0214, /* Standard PCI, LR214 Rev E and earlier (SAA7135) */
5886 .driver_data = SAA7134_BOARD_FLYTVPLATINUM_FM,
5887 },{
5888 .vendor = PCI_VENDOR_ID_PHILIPS,
5889 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5890 .subvendor = 0x5168, /* Animation Technologies (LifeView) */
5891 .subdevice = 0x5214, /* Standard PCI, LR214 Rev F onwards (SAA7131) */
5892 .driver_data = SAA7134_BOARD_FLYTVPLATINUM_FM,
5893 },{
5894 .vendor = PCI_VENDOR_ID_PHILIPS,
5895 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5896 .subvendor = 0x1489, /* KYE */
5897 .subdevice = 0x0214, /* Genius VideoWonder ProTV */
5898 .driver_data = SAA7134_BOARD_FLYTVPLATINUM_FM, /* is an LR214WF actually */
5899 },{
5900 .vendor = PCI_VENDOR_ID_PHILIPS,
5901 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5902 .subvendor = 0x16be,
5903 .subdevice = 0x0003,
5904 .driver_data = SAA7134_BOARD_MD7134,
5905 },{
5906 .vendor = PCI_VENDOR_ID_PHILIPS,
5907 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5908 .subvendor = 0x16be, /* CTX946 analog TV, HW mpeg, DVB-T */
5909 .subdevice = 0x5000, /* only analog TV and DVB-T for now */
5910 .driver_data = SAA7134_BOARD_MD7134,
5911 }, {
5912 .vendor = PCI_VENDOR_ID_PHILIPS,
5913 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
5914 .subvendor = 0x1048,
5915 .subdevice = 0x226b,
5916 .driver_data = SAA7134_BOARD_ELSA,
5917 },{
5918 .vendor = PCI_VENDOR_ID_PHILIPS,
5919 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
5920 .subvendor = 0x1048,
5921 .subdevice = 0x226a,
5922 .driver_data = SAA7134_BOARD_ELSA_500TV,
5923 },{
5924 .vendor = PCI_VENDOR_ID_PHILIPS,
5925 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
5926 .subvendor = 0x1048,
5927 .subdevice = 0x226c,
5928 .driver_data = SAA7134_BOARD_ELSA_700TV,
5929 },{
5930 .vendor = PCI_VENDOR_ID_PHILIPS,
5931 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5932 .subvendor = PCI_VENDOR_ID_ASUSTEK,
5933 .subdevice = 0x4842,
5934 .driver_data = SAA7134_BOARD_ASUSTeK_TVFM7134,
5935 },{
5936 .vendor = PCI_VENDOR_ID_PHILIPS,
5937 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5938 .subvendor = PCI_VENDOR_ID_ASUSTEK,
5939 .subdevice = 0x4845,
5940 .driver_data = SAA7134_BOARD_ASUSTeK_TVFM7135,
5941 },{
5942 .vendor = PCI_VENDOR_ID_PHILIPS,
5943 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5944 .subvendor = PCI_VENDOR_ID_ASUSTEK,
5945 .subdevice = 0x4830,
5946 .driver_data = SAA7134_BOARD_ASUSTeK_TVFM7134,
5947 },{
5948 .vendor = PCI_VENDOR_ID_PHILIPS,
5949 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5950 .subvendor = PCI_VENDOR_ID_ASUSTEK,
5951 .subdevice = 0x4843,
5952 .driver_data = SAA7134_BOARD_ASUSTEK_TVFM7133,
5953 },{
5954 .vendor = PCI_VENDOR_ID_PHILIPS,
5955 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5956 .subvendor = PCI_VENDOR_ID_ASUSTEK,
5957 .subdevice = 0x4840,
5958 .driver_data = SAA7134_BOARD_ASUSTeK_TVFM7134,
5959 },{
5960 .vendor = PCI_VENDOR_ID_PHILIPS,
5961 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5962 .subvendor = PCI_VENDOR_ID_PHILIPS,
5963 .subdevice = 0xfe01,
5964 .driver_data = SAA7134_BOARD_TVSTATION_RDS,
5965 },{
5966 .vendor = PCI_VENDOR_ID_PHILIPS,
5967 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5968 .subvendor = 0x1894,
5969 .subdevice = 0xfe01,
5970 .driver_data = SAA7134_BOARD_TVSTATION_RDS,
5971 },{
5972 .vendor = PCI_VENDOR_ID_PHILIPS,
5973 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5974 .subvendor = 0x1894,
5975 .subdevice = 0xa006,
5976 .driver_data = SAA7134_BOARD_TVSTATION_DVR,
5977 },{
5978 .vendor = PCI_VENDOR_ID_PHILIPS,
5979 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
5980 .subvendor = 0x1131,
5981 .subdevice = 0x7133,
5982 .driver_data = SAA7134_BOARD_VA1000POWER,
5983 },{
5984 .vendor = PCI_VENDOR_ID_PHILIPS,
5985 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
5986 .subvendor = PCI_VENDOR_ID_PHILIPS,
5987 .subdevice = 0x2001,
5988 .driver_data = SAA7134_BOARD_10MOONSTVMASTER,
5989 },{
5990 .vendor = PCI_VENDOR_ID_PHILIPS,
5991 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5992 .subvendor = 0x185b,
5993 .subdevice = 0xc100,
5994 .driver_data = SAA7134_BOARD_VIDEOMATE_TV,
5995 },{
5996 .vendor = PCI_VENDOR_ID_PHILIPS,
5997 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
5998 .subvendor = 0x185b,
5999 .subdevice = 0xc100,
6000 .driver_data = SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUS,
6001 },{
6002 .vendor = PCI_VENDOR_ID_PHILIPS,
6003 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6004 .subvendor = PCI_VENDOR_ID_MATROX,
6005 .subdevice = 0x48d0,
6006 .driver_data = SAA7134_BOARD_CRONOS_PLUS,
6007 },{
6008 .vendor = PCI_VENDOR_ID_PHILIPS,
6009 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6010 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6011 .subdevice = 0xa70b,
6012 .driver_data = SAA7134_BOARD_MD2819,
6013 },{
6014 .vendor = PCI_VENDOR_ID_PHILIPS,
6015 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6016 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6017 .subdevice = 0xa7a1,
6018 .driver_data = SAA7134_BOARD_AVERMEDIA_A700_PRO,
6019 }, {
6020 .vendor = PCI_VENDOR_ID_PHILIPS,
6021 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6022 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6023 .subdevice = 0xa7a2,
6024 .driver_data = SAA7134_BOARD_AVERMEDIA_A700_HYBRID,
6025 }, {
6026 .vendor = PCI_VENDOR_ID_PHILIPS,
6027 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6028 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6029 .subdevice = 0x2115,
6030 .driver_data = SAA7134_BOARD_AVERMEDIA_STUDIO_305,
6031 },{
6032 .vendor = PCI_VENDOR_ID_PHILIPS,
6033 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6034 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6035 .subdevice = 0xa115,
6036 .driver_data = SAA7134_BOARD_AVERMEDIA_STUDIO_505,
6037 }, {
6038 .vendor = PCI_VENDOR_ID_PHILIPS,
6039 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6040 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6041 .subdevice = 0x2108,
6042 .driver_data = SAA7134_BOARD_AVERMEDIA_305,
6043 },{
6044 .vendor = PCI_VENDOR_ID_PHILIPS,
6045 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6046 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6047 .subdevice = 0x10ff,
6048 .driver_data = SAA7134_BOARD_AVERMEDIA_DVD_EZMAKER,
6049 },{
6050 /* AVerMedia CardBus */
6051 .vendor = PCI_VENDOR_ID_PHILIPS,
6052 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6053 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6054 .subdevice = 0xd6ee,
6055 .driver_data = SAA7134_BOARD_AVERMEDIA_CARDBUS,
6056 },{
6057 /* AVerMedia CardBus */
6058 .vendor = PCI_VENDOR_ID_PHILIPS,
6059 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6060 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6061 .subdevice = 0xb7e9,
6062 .driver_data = SAA7134_BOARD_AVERMEDIA_CARDBUS_501,
6063 }, {
6064 /* TransGear 3000TV */
6065 .vendor = PCI_VENDOR_ID_PHILIPS,
6066 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6067 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6068 .subdevice = 0x050c,
6069 .driver_data = SAA7134_BOARD_TG3000TV,
6070 },{
6071 .vendor = PCI_VENDOR_ID_PHILIPS,
6072 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6073 .subvendor = 0x11bd,
6074 .subdevice = 0x002b,
6075 .driver_data = SAA7134_BOARD_PINNACLE_PCTV_STEREO,
6076 },{
6077 .vendor = PCI_VENDOR_ID_PHILIPS,
6078 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6079 .subvendor = 0x11bd,
6080 .subdevice = 0x002d,
6081 .driver_data = SAA7134_BOARD_PINNACLE_300I_DVBT_PAL,
6082 },{
6083 .vendor = PCI_VENDOR_ID_PHILIPS,
6084 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6085 .subvendor = 0x1019,
6086 .subdevice = 0x4cb4,
6087 .driver_data = SAA7134_BOARD_ECS_TVP3XP,
6088 },{
6089 .vendor = PCI_VENDOR_ID_PHILIPS,
6090 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6091 .subvendor = 0x1019,
6092 .subdevice = 0x4cb5,
6093 .driver_data = SAA7134_BOARD_ECS_TVP3XP_4CB5,
6094 },{
6095 .vendor = PCI_VENDOR_ID_PHILIPS,
6096 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6097 .subvendor = 0x1019,
6098 .subdevice = 0x4cb6,
6099 .driver_data = SAA7134_BOARD_ECS_TVP3XP_4CB6,
6100 },{
6101 .vendor = PCI_VENDOR_ID_PHILIPS,
6102 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6103 .subvendor = 0x12ab,
6104 .subdevice = 0x0800,
6105 .driver_data = SAA7134_BOARD_UPMOST_PURPLE_TV,
6106 },{
6107 .vendor = PCI_VENDOR_ID_PHILIPS,
6108 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6109 .subvendor = 0x153b,
6110 .subdevice = 0x1152,
6111 .driver_data = SAA7134_BOARD_CINERGY200,
6112 },{
6113 .vendor = PCI_VENDOR_ID_PHILIPS,
6114 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6115 .subvendor = 0x185b,
6116 .subdevice = 0xc100,
6117 .driver_data = SAA7134_BOARD_VIDEOMATE_TV_PVR,
6118 },{
6119 .vendor = PCI_VENDOR_ID_PHILIPS,
6120 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6121 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6122 .subdevice = 0x9715,
6123 .driver_data = SAA7134_BOARD_AVERMEDIA_STUDIO_307,
6124 },{
6125 .vendor = PCI_VENDOR_ID_PHILIPS,
6126 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6127 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6128 .subdevice = 0xa70a,
6129 .driver_data = SAA7134_BOARD_AVERMEDIA_307,
6130 },{
6131 .vendor = PCI_VENDOR_ID_PHILIPS,
6132 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6133 .subvendor = 0x185b,
6134 .subdevice = 0xc200,
6135 .driver_data = SAA7134_BOARD_VIDEOMATE_GOLD_PLUS,
6136 },{
6137 .vendor = PCI_VENDOR_ID_PHILIPS,
6138 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6139 .subvendor = 0x1540,
6140 .subdevice = 0x9524,
6141 .driver_data = SAA7134_BOARD_PROVIDEO_PV952,
6142
6143 },{
6144 .vendor = PCI_VENDOR_ID_PHILIPS,
6145 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6146 .subvendor = 0x5168,
6147 .subdevice = 0x0502, /* Cardbus version */
6148 .driver_data = SAA7134_BOARD_FLYDVBT_DUO_CARDBUS,
6149 },{
6150 .vendor = PCI_VENDOR_ID_PHILIPS,
6151 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6152 .subvendor = 0x5168,
6153 .subdevice = 0x0306, /* PCI version */
6154 .driver_data = SAA7134_BOARD_FLYDVBTDUO,
6155 },{
6156 .vendor = PCI_VENDOR_ID_PHILIPS,
6157 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6158 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6159 .subdevice = 0xf31f,
6160 .driver_data = SAA7134_BOARD_AVERMEDIA_GO_007_FM,
6161
6162 },{
6163 .vendor = PCI_VENDOR_ID_PHILIPS,
6164 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6165 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6166 .subdevice = 0xf11d,
6167 .driver_data = SAA7134_BOARD_AVERMEDIA_M135A,
6168 }, {
6169 .vendor = PCI_VENDOR_ID_PHILIPS,
6170 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6171 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6172 .subdevice = 0x4155,
6173 .driver_data = SAA7134_BOARD_AVERMEDIA_M733A,
6174 }, {
6175 .vendor = PCI_VENDOR_ID_PHILIPS,
6176 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6177 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6178 .subdevice = 0x4255,
6179 .driver_data = SAA7134_BOARD_AVERMEDIA_M733A,
6180 }, {
6181 .vendor = PCI_VENDOR_ID_PHILIPS,
6182 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6183 .subvendor = PCI_VENDOR_ID_PHILIPS,
6184 .subdevice = 0x2004,
6185 .driver_data = SAA7134_BOARD_PHILIPS_TOUGH,
6186 },{
6187 .vendor = PCI_VENDOR_ID_PHILIPS,
6188 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6189 .subvendor = 0x1421,
6190 .subdevice = 0x0350, /* PCI version */
6191 .driver_data = SAA7134_BOARD_ADS_INSTANT_TV,
6192 },{
6193 .vendor = PCI_VENDOR_ID_PHILIPS,
6194 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6195 .subvendor = 0x1421,
6196 .subdevice = 0x0351, /* PCI version, new revision */
6197 .driver_data = SAA7134_BOARD_ADS_INSTANT_TV,
6198 },{
6199 .vendor = PCI_VENDOR_ID_PHILIPS,
6200 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6201 .subvendor = 0x1421,
6202 .subdevice = 0x0370, /* cardbus version */
6203 .driver_data = SAA7134_BOARD_ADS_INSTANT_TV,
6204 },{
6205 .vendor = PCI_VENDOR_ID_PHILIPS,
6206 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6207 .subvendor = 0x1421,
6208 .subdevice = 0x1370, /* cardbus version */
6209 .driver_data = SAA7134_BOARD_ADS_INSTANT_TV,
6210
6211 },{
6212 .vendor = PCI_VENDOR_ID_PHILIPS,
6213 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6214 .subvendor = 0x4e42, /* Typhoon */
6215 .subdevice = 0x0502, /* LifeView LR502 OEM */
6216 .driver_data = SAA7134_BOARD_FLYDVBT_DUO_CARDBUS,
6217 },{
6218 .vendor = PCI_VENDOR_ID_PHILIPS,
6219 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6220 .subvendor = 0x1043,
6221 .subdevice = 0x0210, /* mini pci NTSC version */
6222 .driver_data = SAA7134_BOARD_FLYTV_DIGIMATRIX,
6223 },{
6224 .vendor = PCI_VENDOR_ID_PHILIPS,
6225 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6226 .subvendor = 0x1043,
6227 .subdevice = 0x0210, /* mini pci PAL/SECAM version */
6228 .driver_data = SAA7134_BOARD_ASUSTEK_DIGIMATRIX_TV,
6229
6230 },{
6231 .vendor = PCI_VENDOR_ID_PHILIPS,
6232 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6233 .subvendor = 0x0000, /* It shouldn't break anything, since subdevice id seems unique */
6234 .subdevice = 0x4091,
6235 .driver_data = SAA7134_BOARD_BEHOLD_409FM,
6236 },{
6237 .vendor = PCI_VENDOR_ID_PHILIPS,
6238 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6239 .subvendor = 0x5456, /* GoTView */
6240 .subdevice = 0x7135,
6241 .driver_data = SAA7134_BOARD_GOTVIEW_7135,
6242 },{
6243 .vendor = PCI_VENDOR_ID_PHILIPS,
6244 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6245 .subvendor = PCI_VENDOR_ID_PHILIPS,
6246 .subdevice = 0x2004,
6247 .driver_data = SAA7134_BOARD_PHILIPS_EUROPA,
6248 },{
6249 .vendor = PCI_VENDOR_ID_PHILIPS,
6250 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6251 .subvendor = 0x185b,
6252 .subdevice = 0xc900,
6253 .driver_data = SAA7134_BOARD_VIDEOMATE_DVBT_300,
6254 },{
6255 .vendor = PCI_VENDOR_ID_PHILIPS,
6256 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6257 .subvendor = 0x185b,
6258 .subdevice = 0xc901,
6259 .driver_data = SAA7134_BOARD_VIDEOMATE_DVBT_200,
6260 },{
6261 .vendor = PCI_VENDOR_ID_PHILIPS,
6262 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6263 .subvendor = 0x1435,
6264 .subdevice = 0x7350,
6265 .driver_data = SAA7134_BOARD_RTD_VFG7350,
6266 },{
6267 .vendor = PCI_VENDOR_ID_PHILIPS,
6268 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6269 .subvendor = 0x1435,
6270 .subdevice = 0x7330,
6271 .driver_data = SAA7134_BOARD_RTD_VFG7330,
6272 },{
6273 .vendor = PCI_VENDOR_ID_PHILIPS,
6274 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6275 .subvendor = 0x1461,
6276 .subdevice = 0x1044,
6277 .driver_data = SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180,
6278 },{
6279 .vendor = PCI_VENDOR_ID_PHILIPS,
6280 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6281 .subvendor = 0x1131,
6282 .subdevice = 0x4ee9,
6283 .driver_data = SAA7134_BOARD_MONSTERTV_MOBILE,
6284 },{
6285 .vendor = PCI_VENDOR_ID_PHILIPS,
6286 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6287 .subvendor = 0x11bd,
6288 .subdevice = 0x002e,
6289 .driver_data = SAA7134_BOARD_PINNACLE_PCTV_110i,
6290 },{
6291 .vendor = PCI_VENDOR_ID_PHILIPS,
6292 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6293 .subvendor = 0x1043,
6294 .subdevice = 0x4862,
6295 .driver_data = SAA7134_BOARD_ASUSTeK_P7131_DUAL,
6296 },{
6297 .vendor = PCI_VENDOR_ID_PHILIPS,
6298 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6299 .subvendor = PCI_VENDOR_ID_PHILIPS,
6300 .subdevice = 0x2018,
6301 .driver_data = SAA7134_BOARD_PHILIPS_TIGER,
6302 },{
6303 .vendor = PCI_VENDOR_ID_PHILIPS,
6304 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6305 .subvendor = 0x1462,
6306 .subdevice = 0x6231, /* tda8275a, ks003 IR */
6307 .driver_data = SAA7134_BOARD_MSI_TVATANYWHERE_PLUS,
6308 },{
6309 .vendor = PCI_VENDOR_ID_PHILIPS,
6310 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6311 .subvendor = 0x1462,
6312 .subdevice = 0x8624, /* tda8275, ks003 IR */
6313 .driver_data = SAA7134_BOARD_MSI_TVATANYWHERE_PLUS,
6314 },{
6315 .vendor = PCI_VENDOR_ID_PHILIPS,
6316 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6317 .subvendor = 0x153b,
6318 .subdevice = 0x1160,
6319 .driver_data = SAA7134_BOARD_CINERGY250PCI,
6320 },{
6321 .vendor = PCI_VENDOR_ID_PHILIPS,
6322 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, /* SAA 7131E */
6323 .subvendor = 0x5168,
6324 .subdevice = 0x0319,
6325 .driver_data = SAA7134_BOARD_FLYDVB_TRIO,
6326 },{
6327 .vendor = PCI_VENDOR_ID_PHILIPS,
6328 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6329 .subvendor = 0x1461,
6330 .subdevice = 0x2c05,
6331 .driver_data = SAA7134_BOARD_AVERMEDIA_777,
6332 },{
6333 .vendor = PCI_VENDOR_ID_PHILIPS,
6334 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6335 .subvendor = 0x5168,
6336 .subdevice = 0x0301,
6337 .driver_data = SAA7134_BOARD_FLYDVBT_LR301,
6338 },{
6339 .vendor = PCI_VENDOR_ID_PHILIPS,
6340 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6341 .subvendor = 0x0331,
6342 .subdevice = 0x1421,
6343 .driver_data = SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331,
6344 },{
6345 .vendor = PCI_VENDOR_ID_PHILIPS,
6346 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6347 .subvendor = 0x17de,
6348 .subdevice = 0x7201,
6349 .driver_data = SAA7134_BOARD_TEVION_DVBT_220RF,
6350 },{
6351 .vendor = PCI_VENDOR_ID_PHILIPS,
6352 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6353 .subvendor = 0x17de,
6354 .subdevice = 0x7250,
6355 .driver_data = SAA7134_BOARD_KWORLD_DVBT_210,
6356 },{
6357 .vendor = PCI_VENDOR_ID_PHILIPS,
6358 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, /* SAA7135HL */
6359 .subvendor = 0x17de,
6360 .subdevice = 0x7350,
6361 .driver_data = SAA7134_BOARD_KWORLD_ATSC110,
6362 },{
6363 .vendor = PCI_VENDOR_ID_PHILIPS,
6364 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, /* SAA7135HL */
6365 .subvendor = 0x17de,
6366 .subdevice = 0x7352,
6367 .driver_data = SAA7134_BOARD_KWORLD_ATSC110, /* ATSC 115 */
6368 },{
6369 .vendor = PCI_VENDOR_ID_PHILIPS,
6370 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, /* SAA7135HL */
6371 .subvendor = 0x17de,
6372 .subdevice = 0xa134,
6373 .driver_data = SAA7134_BOARD_KWORLD_PC150U,
6374 }, {
6375 .vendor = PCI_VENDOR_ID_PHILIPS,
6376 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6377 .subvendor = 0x1461,
6378 .subdevice = 0x7360,
6379 .driver_data = SAA7134_BOARD_AVERMEDIA_A169_B,
6380 },{
6381 .vendor = PCI_VENDOR_ID_PHILIPS,
6382 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6383 .subvendor = 0x1461,
6384 .subdevice = 0x6360,
6385 .driver_data = SAA7134_BOARD_AVERMEDIA_A169_B1,
6386 },{
6387 .vendor = PCI_VENDOR_ID_PHILIPS,
6388 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6389 .subvendor = 0x16be,
6390 .subdevice = 0x0005,
6391 .driver_data = SAA7134_BOARD_MD7134_BRIDGE_2,
6392 },{
6393 .vendor = PCI_VENDOR_ID_PHILIPS,
6394 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6395 .subvendor = 0x5168,
6396 .subdevice = 0x0300,
6397 .driver_data = SAA7134_BOARD_FLYDVBS_LR300,
6398 },{
6399 .vendor = PCI_VENDOR_ID_PHILIPS,
6400 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6401 .subvendor = 0x4e42,
6402 .subdevice = 0x0300,/* LR300 */
6403 .driver_data = SAA7134_BOARD_FLYDVBS_LR300,
6404 },{
6405 .vendor = PCI_VENDOR_ID_PHILIPS,
6406 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6407 .subvendor = 0x1489,
6408 .subdevice = 0x0301,
6409 .driver_data = SAA7134_BOARD_FLYDVBT_LR301,
6410 },{
6411 .vendor = PCI_VENDOR_ID_PHILIPS,
6412 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6413 .subvendor = 0x5168, /* Animation Technologies (LifeView) */
6414 .subdevice = 0x0304,
6415 .driver_data = SAA7134_BOARD_FLYTVPLATINUM_FM,
6416 },{
6417 .vendor = PCI_VENDOR_ID_PHILIPS,
6418 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6419 .subvendor = 0x5168,
6420 .subdevice = 0x3306,
6421 .driver_data = SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS,
6422 },{
6423 .vendor = PCI_VENDOR_ID_PHILIPS,
6424 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6425 .subvendor = 0x5168,
6426 .subdevice = 0x3502, /* whats the difference to 0x3306 ?*/
6427 .driver_data = SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS,
6428 },{
6429 .vendor = PCI_VENDOR_ID_PHILIPS,
6430 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6431 .subvendor = 0x5168,
6432 .subdevice = 0x3307, /* FlyDVB-T Hybrid Mini PCI */
6433 .driver_data = SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS,
6434 }, {
6435 .vendor = PCI_VENDOR_ID_PHILIPS,
6436 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6437 .subvendor = 0x16be,
6438 .subdevice = 0x0007,
6439 .driver_data = SAA7134_BOARD_MEDION_MD8800_QUADRO,
6440 },{
6441 .vendor = PCI_VENDOR_ID_PHILIPS,
6442 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6443 .subvendor = 0x16be,
6444 .subdevice = 0x0008,
6445 .driver_data = SAA7134_BOARD_MEDION_MD8800_QUADRO,
6446 },{
6447 .vendor = PCI_VENDOR_ID_PHILIPS,
6448 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6449 .subvendor = 0x16be,
6450 .subdevice = 0x000d, /* triple CTX948_V1.1.1 */
6451 .driver_data = SAA7134_BOARD_MEDION_MD8800_QUADRO,
6452 }, {
6453 .vendor = PCI_VENDOR_ID_PHILIPS,
6454 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6455 .subvendor = 0x1461,
6456 .subdevice = 0x2c05,
6457 .driver_data = SAA7134_BOARD_AVERMEDIA_777,
6458 },{
6459 .vendor = PCI_VENDOR_ID_PHILIPS,
6460 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6461 .subvendor = 0x1489,
6462 .subdevice = 0x0502, /* Cardbus version */
6463 .driver_data = SAA7134_BOARD_FLYDVBT_DUO_CARDBUS,
6464 },{
6465 .vendor = PCI_VENDOR_ID_PHILIPS,
6466 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6467 .subvendor = 0x0919, /* Philips Proteus PRO 2309 */
6468 .subdevice = 0x2003,
6469 .driver_data = SAA7134_BOARD_PROTEUS_2309,
6470 },{
6471 .vendor = PCI_VENDOR_ID_PHILIPS,
6472 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6473 .subvendor = 0x1461,
6474 .subdevice = 0x2c00,
6475 .driver_data = SAA7134_BOARD_AVERMEDIA_A16AR,
6476 },{
6477 .vendor = PCI_VENDOR_ID_PHILIPS,
6478 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6479 .subvendor = 0x1043,
6480 .subdevice = 0x4860,
6481 .driver_data = SAA7134_BOARD_ASUS_EUROPA2_HYBRID,
6482 },{
6483 .vendor = PCI_VENDOR_ID_PHILIPS,
6484 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6485 .subvendor = 0x11bd,
6486 .subdevice = 0x002f,
6487 .driver_data = SAA7134_BOARD_PINNACLE_PCTV_310i,
6488 },{
6489 .vendor = PCI_VENDOR_ID_PHILIPS,
6490 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6491 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6492 .subdevice = 0x9715,
6493 .driver_data = SAA7134_BOARD_AVERMEDIA_STUDIO_507,
6494 },{
6495 .vendor = PCI_VENDOR_ID_PHILIPS,
6496 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6497 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6498 .subdevice = 0xa11b,
6499 .driver_data = SAA7134_BOARD_AVERMEDIA_STUDIO_507UA,
6500 }, {
6501 .vendor = PCI_VENDOR_ID_PHILIPS,
6502 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6503 .subvendor = 0x1043,
6504 .subdevice = 0x4876,
6505 .driver_data = SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA,
6506 },{
6507 .vendor = PCI_VENDOR_ID_PHILIPS,
6508 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6509 .subvendor = 0x0070,
6510 .subdevice = 0x6700,
6511 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
6512 },{
6513 .vendor = PCI_VENDOR_ID_PHILIPS,
6514 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6515 .subvendor = 0x0070,
6516 .subdevice = 0x6701,
6517 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
6518 },{
6519 .vendor = PCI_VENDOR_ID_PHILIPS,
6520 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6521 .subvendor = 0x0070,
6522 .subdevice = 0x6702,
6523 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
6524 },{
6525 .vendor = PCI_VENDOR_ID_PHILIPS,
6526 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6527 .subvendor = 0x0070,
6528 .subdevice = 0x6703,
6529 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
6530 },{
6531 .vendor = PCI_VENDOR_ID_PHILIPS,
6532 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6533 .subvendor = 0x0070,
6534 .subdevice = 0x6704,
6535 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
6536 },{
6537 .vendor = PCI_VENDOR_ID_PHILIPS,
6538 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6539 .subvendor = 0x0070,
6540 .subdevice = 0x6705,
6541 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1110,
6542 },{
6543 .vendor = PCI_VENDOR_ID_PHILIPS,
6544 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6545 .subvendor = 0x0070,
6546 .subdevice = 0x6706,
6547 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1150,
6548 },{
6549 .vendor = PCI_VENDOR_ID_PHILIPS,
6550 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6551 .subvendor = 0x0070,
6552 .subdevice = 0x6707,
6553 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1120,
6554 },{
6555 .vendor = PCI_VENDOR_ID_PHILIPS,
6556 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6557 .subvendor = 0x0070,
6558 .subdevice = 0x6708,
6559 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1150,
6560 },{
6561 .vendor = PCI_VENDOR_ID_PHILIPS,
6562 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6563 .subvendor = 0x0070,
6564 .subdevice = 0x6709,
6565 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1120,
6566 },{
6567 .vendor = PCI_VENDOR_ID_PHILIPS,
6568 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6569 .subvendor = 0x0070,
6570 .subdevice = 0x670a,
6571 .driver_data = SAA7134_BOARD_HAUPPAUGE_HVR1120,
6572 },{
6573 .vendor = PCI_VENDOR_ID_PHILIPS,
6574 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6575 .subvendor = 0x153b,
6576 .subdevice = 0x1172,
6577 .driver_data = SAA7134_BOARD_CINERGY_HT_PCMCIA,
6578 },{
6579 .vendor = PCI_VENDOR_ID_PHILIPS,
6580 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6581 .subvendor = PCI_VENDOR_ID_PHILIPS,
6582 .subdevice = 0x2342,
6583 .driver_data = SAA7134_BOARD_ENCORE_ENLTV,
6584 },{
6585 .vendor = PCI_VENDOR_ID_PHILIPS,
6586 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6587 .subvendor = 0x1131,
6588 .subdevice = 0x2341,
6589 .driver_data = SAA7134_BOARD_ENCORE_ENLTV,
6590 },{
6591 .vendor = PCI_VENDOR_ID_PHILIPS,
6592 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6593 .subvendor = 0x3016,
6594 .subdevice = 0x2344,
6595 .driver_data = SAA7134_BOARD_ENCORE_ENLTV,
6596 },{
6597 .vendor = PCI_VENDOR_ID_PHILIPS,
6598 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6599 .subvendor = 0x1131,
6600 .subdevice = 0x230f,
6601 .driver_data = SAA7134_BOARD_ENCORE_ENLTV_FM,
6602 },{
6603 .vendor = PCI_VENDOR_ID_PHILIPS,
6604 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6605 .subvendor = 0x1a7f,
6606 .subdevice = 0x2008,
6607 .driver_data = SAA7134_BOARD_ENCORE_ENLTV_FM53,
6608 }, {
6609 .vendor = PCI_VENDOR_ID_PHILIPS,
6610 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6611 .subvendor = 0x1a7f,
6612 .subdevice = 0x2108,
6613 .driver_data = SAA7134_BOARD_ENCORE_ENLTV_FM3,
6614 }, {
6615 .vendor = PCI_VENDOR_ID_PHILIPS,
6616 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6617 .subvendor = 0x153b,
6618 .subdevice = 0x1175,
6619 .driver_data = SAA7134_BOARD_CINERGY_HT_PCI,
6620 },{
6621 .vendor = PCI_VENDOR_ID_PHILIPS,
6622 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6623 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6624 .subdevice = 0xf31e,
6625 .driver_data = SAA7134_BOARD_AVERMEDIA_M102,
6626 },{
6627 .vendor = PCI_VENDOR_ID_PHILIPS,
6628 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6629 .subvendor = 0x4E42, /* MSI */
6630 .subdevice = 0x0306, /* TV@nywhere DUO */
6631 .driver_data = SAA7134_BOARD_FLYDVBTDUO,
6632 },{
6633 .vendor = PCI_VENDOR_ID_PHILIPS,
6634 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6635 .subvendor = 0x1043,
6636 .subdevice = 0x4871,
6637 .driver_data = SAA7134_BOARD_ASUS_P7131_4871,
6638 },{
6639 .vendor = PCI_VENDOR_ID_PHILIPS,
6640 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6641 .subvendor = 0x1043,
6642 .subdevice = 0x4857, /* REV:1.00 */
6643 .driver_data = SAA7134_BOARD_ASUSTeK_TIGER,
6644 },{
6645 .vendor = PCI_VENDOR_ID_PHILIPS,
6646 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6647 .subvendor = 0x0919, /* SinoVideo PCI 2309 Proteus (7134) */
6648 .subdevice = 0x2003, /* OEM cardbus */
6649 .driver_data = SAA7134_BOARD_SABRENT_TV_PCB05,
6650 },{
6651 .vendor = PCI_VENDOR_ID_PHILIPS,
6652 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6653 .subvendor = PCI_VENDOR_ID_PHILIPS,
6654 .subdevice = 0x2304,
6655 .driver_data = SAA7134_BOARD_10MOONSTVMASTER3,
6656 },{
6657 .vendor = PCI_VENDOR_ID_PHILIPS,
6658 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6659 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6660 .subdevice = 0xf01d, /* AVerTV DVB-T Super 007 */
6661 .driver_data = SAA7134_BOARD_AVERMEDIA_SUPER_007,
6662 },{
6663 .vendor = PCI_VENDOR_ID_PHILIPS,
6664 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6665 .subvendor = 0x0000,
6666 .subdevice = 0x4016,
6667 .driver_data = SAA7134_BOARD_BEHOLD_401,
6668 },{
6669 .vendor = PCI_VENDOR_ID_PHILIPS,
6670 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6671 .subvendor = 0x0000,
6672 .subdevice = 0x4036,
6673 .driver_data = SAA7134_BOARD_BEHOLD_403,
6674 },{
6675 .vendor = PCI_VENDOR_ID_PHILIPS,
6676 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6677 .subvendor = 0x0000,
6678 .subdevice = 0x4037,
6679 .driver_data = SAA7134_BOARD_BEHOLD_403FM,
6680 },{
6681 .vendor = PCI_VENDOR_ID_PHILIPS,
6682 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6683 .subvendor = 0x0000,
6684 .subdevice = 0x4050,
6685 .driver_data = SAA7134_BOARD_BEHOLD_405,
6686 },{
6687 .vendor = PCI_VENDOR_ID_PHILIPS,
6688 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6689 .subvendor = 0x0000,
6690 .subdevice = 0x4051,
6691 .driver_data = SAA7134_BOARD_BEHOLD_405FM,
6692 },{
6693 .vendor = PCI_VENDOR_ID_PHILIPS,
6694 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6695 .subvendor = 0x0000,
6696 .subdevice = 0x4070,
6697 .driver_data = SAA7134_BOARD_BEHOLD_407,
6698 },{
6699 .vendor = PCI_VENDOR_ID_PHILIPS,
6700 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6701 .subvendor = 0x0000,
6702 .subdevice = 0x4071,
6703 .driver_data = SAA7134_BOARD_BEHOLD_407FM,
6704 },{
6705 .vendor = PCI_VENDOR_ID_PHILIPS,
6706 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6707 .subvendor = 0x0000,
6708 .subdevice = 0x4090,
6709 .driver_data = SAA7134_BOARD_BEHOLD_409,
6710 },{
6711 .vendor = PCI_VENDOR_ID_PHILIPS,
6712 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6713 .subvendor = 0x0000,
6714 .subdevice = 0x505B,
6715 .driver_data = SAA7134_BOARD_BEHOLD_505RDS_MK5,
6716 }, {
6717 .vendor = PCI_VENDOR_ID_PHILIPS,
6718 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6719 .subvendor = 0x0000,
6720 .subdevice = 0x5051,
6721 .driver_data = SAA7134_BOARD_BEHOLD_505RDS_MK3,
6722 },{
6723 .vendor = PCI_VENDOR_ID_PHILIPS,
6724 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6725 .subvendor = 0x5ace,
6726 .subdevice = 0x5050,
6727 .driver_data = SAA7134_BOARD_BEHOLD_505FM,
6728 },{
6729 .vendor = PCI_VENDOR_ID_PHILIPS,
6730 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6731 .subvendor = 0x0000,
6732 .subdevice = 0x5071,
6733 .driver_data = SAA7134_BOARD_BEHOLD_507RDS_MK3,
6734 },{
6735 .vendor = PCI_VENDOR_ID_PHILIPS,
6736 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6737 .subvendor = 0x0000,
6738 .subdevice = 0x507B,
6739 .driver_data = SAA7134_BOARD_BEHOLD_507RDS_MK5,
6740 },{
6741 .vendor = PCI_VENDOR_ID_PHILIPS,
6742 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6743 .subvendor = 0x5ace,
6744 .subdevice = 0x5070,
6745 .driver_data = SAA7134_BOARD_BEHOLD_507_9FM,
6746 },{
6747 .vendor = PCI_VENDOR_ID_PHILIPS,
6748 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6749 .subvendor = 0x5ace,
6750 .subdevice = 0x5090,
6751 .driver_data = SAA7134_BOARD_BEHOLD_507_9FM,
6752 },{
6753 .vendor = PCI_VENDOR_ID_PHILIPS,
6754 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6755 .subvendor = 0x0000,
6756 .subdevice = 0x5201,
6757 .driver_data = SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM,
6758 },{
6759 .vendor = PCI_VENDOR_ID_PHILIPS,
6760 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6761 .subvendor = 0x5ace,
6762 .subdevice = 0x6070,
6763 .driver_data = SAA7134_BOARD_BEHOLD_607FM_MK3,
6764 },{
6765 .vendor = PCI_VENDOR_ID_PHILIPS,
6766 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6767 .subvendor = 0x5ace,
6768 .subdevice = 0x6071,
6769 .driver_data = SAA7134_BOARD_BEHOLD_607FM_MK5,
6770 },{
6771 .vendor = PCI_VENDOR_ID_PHILIPS,
6772 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6773 .subvendor = 0x5ace,
6774 .subdevice = 0x6072,
6775 .driver_data = SAA7134_BOARD_BEHOLD_607RDS_MK3,
6776 },{
6777 .vendor = PCI_VENDOR_ID_PHILIPS,
6778 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6779 .subvendor = 0x5ace,
6780 .subdevice = 0x6073,
6781 .driver_data = SAA7134_BOARD_BEHOLD_607RDS_MK5,
6782 },{
6783 .vendor = PCI_VENDOR_ID_PHILIPS,
6784 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6785 .subvendor = 0x5ace,
6786 .subdevice = 0x6090,
6787 .driver_data = SAA7134_BOARD_BEHOLD_609FM_MK3,
6788 },{
6789 .vendor = PCI_VENDOR_ID_PHILIPS,
6790 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6791 .subvendor = 0x5ace,
6792 .subdevice = 0x6091,
6793 .driver_data = SAA7134_BOARD_BEHOLD_609FM_MK5,
6794 },{
6795 .vendor = PCI_VENDOR_ID_PHILIPS,
6796 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6797 .subvendor = 0x5ace,
6798 .subdevice = 0x6092,
6799 .driver_data = SAA7134_BOARD_BEHOLD_609RDS_MK3,
6800 },{
6801 .vendor = PCI_VENDOR_ID_PHILIPS,
6802 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6803 .subvendor = 0x5ace,
6804 .subdevice = 0x6093,
6805 .driver_data = SAA7134_BOARD_BEHOLD_609RDS_MK5,
6806 },{
6807 .vendor = PCI_VENDOR_ID_PHILIPS,
6808 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6809 .subvendor = 0x5ace,
6810 .subdevice = 0x6190,
6811 .driver_data = SAA7134_BOARD_BEHOLD_M6,
6812 },{
6813 .vendor = PCI_VENDOR_ID_PHILIPS,
6814 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6815 .subvendor = 0x5ace,
6816 .subdevice = 0x6193,
6817 .driver_data = SAA7134_BOARD_BEHOLD_M6_EXTRA,
6818 }, {
6819 .vendor = PCI_VENDOR_ID_PHILIPS,
6820 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6821 .subvendor = 0x5ace,
6822 .subdevice = 0x6191,
6823 .driver_data = SAA7134_BOARD_BEHOLD_M63,
6824 },{
6825 .vendor = PCI_VENDOR_ID_PHILIPS,
6826 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6827 .subvendor = 0x4e42,
6828 .subdevice = 0x3502,
6829 .driver_data = SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS,
6830 }, {
6831 .vendor = PCI_VENDOR_ID_PHILIPS,
6832 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6833 .subvendor = 0x1822, /*Twinhan Technology Co. Ltd*/
6834 .subdevice = 0x0022,
6835 .driver_data = SAA7134_BOARD_TWINHAN_DTV_DVB_3056,
6836 }, {
6837 .vendor = PCI_VENDOR_ID_PHILIPS,
6838 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6839 .subvendor = 0x16be,
6840 .subdevice = 0x0010, /* Medion version CTX953_V.1.4.3 */
6841 .driver_data = SAA7134_BOARD_CREATIX_CTX953,
6842 }, {
6843 .vendor = PCI_VENDOR_ID_PHILIPS,
6844 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6845 .subvendor = 0x1462, /* MSI */
6846 .subdevice = 0x8625, /* TV@nywhere A/D v1.1 */
6847 .driver_data = SAA7134_BOARD_MSI_TVANYWHERE_AD11,
6848 },{
6849 .vendor = PCI_VENDOR_ID_PHILIPS,
6850 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6851 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6852 .subdevice = 0xf436,
6853 .driver_data = SAA7134_BOARD_AVERMEDIA_CARDBUS_506,
6854 }, {
6855 .vendor = PCI_VENDOR_ID_PHILIPS,
6856 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6857 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6858 .subdevice = 0xf936,
6859 .driver_data = SAA7134_BOARD_AVERMEDIA_A16D,
6860 }, {
6861 .vendor = PCI_VENDOR_ID_PHILIPS,
6862 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6863 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6864 .subdevice = 0xa836,
6865 .driver_data = SAA7134_BOARD_AVERMEDIA_M115,
6866 }, {
6867 .vendor = PCI_VENDOR_ID_PHILIPS,
6868 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6869 .subvendor = 0x185b,
6870 .subdevice = 0xc900,
6871 .driver_data = SAA7134_BOARD_VIDEOMATE_T750,
6872 }, {
6873 .vendor = PCI_VENDOR_ID_PHILIPS,
6874 .device = PCI_DEVICE_ID_PHILIPS_SAA7133, /* SAA7135HL */
6875 .subvendor = 0x1421,
6876 .subdevice = 0x0380,
6877 .driver_data = SAA7134_BOARD_ADS_INSTANT_HDTV_PCI,
6878 }, {
6879 .vendor = PCI_VENDOR_ID_PHILIPS,
6880 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6881 .subvendor = 0x5169,
6882 .subdevice = 0x1502,
6883 .driver_data = SAA7134_BOARD_FLYTVPLATINUM_MINI,
6884 }, {
6885 .vendor = PCI_VENDOR_ID_PHILIPS,
6886 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6887 .subvendor = 0x5ace,
6888 .subdevice = 0x6290,
6889 .driver_data = SAA7134_BOARD_BEHOLD_H6,
6890 }, {
6891 .vendor = PCI_VENDOR_ID_PHILIPS,
6892 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6893 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6894 .subdevice = 0xf636,
6895 .driver_data = SAA7134_BOARD_AVERMEDIA_M103,
6896 }, {
6897 .vendor = PCI_VENDOR_ID_PHILIPS,
6898 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6899 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6900 .subdevice = 0xf736,
6901 .driver_data = SAA7134_BOARD_AVERMEDIA_M103,
6902 }, {
6903 .vendor = PCI_VENDOR_ID_PHILIPS,
6904 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6905 .subvendor = 0x1043,
6906 .subdevice = 0x4878, /* REV:1.02G */
6907 .driver_data = SAA7134_BOARD_ASUSTeK_TIGER_3IN1,
6908 }, {
6909 .vendor = PCI_VENDOR_ID_PHILIPS,
6910 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6911 .subvendor = 0x1043,
6912 .subdevice = 0x48cd,
6913 .driver_data = SAA7134_BOARD_ASUSTeK_PS3_100,
6914 }, {
6915 .vendor = PCI_VENDOR_ID_PHILIPS,
6916 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6917 .subvendor = 0x17de,
6918 .subdevice = 0x7128,
6919 .driver_data = SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG,
6920 }, {
6921 .vendor = PCI_VENDOR_ID_PHILIPS,
6922 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6923 .subvendor = 0x17de,
6924 .subdevice = 0xb136,
6925 .driver_data = SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG,
6926 }, {
6927 .vendor = PCI_VENDOR_ID_PHILIPS,
6928 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6929 .subvendor = 0x1461, /* Avermedia Technologies Inc */
6930 .subdevice = 0xf31d,
6931 .driver_data = SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS,
6932 }, {
6933 .vendor = PCI_VENDOR_ID_PHILIPS,
6934 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6935 .subvendor = 0x185b,
6936 .subdevice = 0xc900,
6937 .driver_data = SAA7134_BOARD_VIDEOMATE_S350,
6938 }, {
6939 .vendor = PCI_VENDOR_ID_PHILIPS,
6940 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6941 .subvendor = 0x5ace, /* Beholder Intl. Ltd. */
6942 .subdevice = 0x7595,
6943 .driver_data = SAA7134_BOARD_BEHOLD_X7,
6944 }, {
6945 .vendor = PCI_VENDOR_ID_PHILIPS,
6946 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6947 .subvendor = 0x19d1, /* RoverMedia */
6948 .subdevice = 0x0138, /* LifeView FlyTV Prime30 OEM */
6949 .driver_data = SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM,
6950 }, {
6951 .vendor = PCI_VENDOR_ID_PHILIPS,
6952 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6953 .subvendor = PCI_VENDOR_ID_PHILIPS,
6954 .subdevice = 0x2004,
6955 .driver_data = SAA7134_BOARD_ZOLID_HYBRID_PCI,
6956 }, {
6957 .vendor = PCI_VENDOR_ID_PHILIPS,
6958 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
6959 .subvendor = 0x1043,
6960 .subdevice = 0x4847,
6961 .driver_data = SAA7134_BOARD_ASUS_EUROPA_HYBRID,
6962 }, {
6963 .vendor = PCI_VENDOR_ID_PHILIPS,
6964 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
6965 .subvendor = 0x107d,
6966 .subdevice = 0x6655,
6967 .driver_data = SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S,
6968 }, {
6969 .vendor = PCI_VENDOR_ID_PHILIPS,
6970 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6971 .subvendor = 0x13c2,
6972 .subdevice = 0x2804,
6973 .driver_data = SAA7134_BOARD_TECHNOTREND_BUDGET_T3000,
6974 }, {
6975 .vendor = PCI_VENDOR_ID_PHILIPS,
6976 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6977 .subvendor = 0x5ace, /* Beholder Intl. Ltd. */
6978 .subdevice = 0x7190,
6979 .driver_data = SAA7134_BOARD_BEHOLD_H7,
6980 }, {
6981 .vendor = PCI_VENDOR_ID_PHILIPS,
6982 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6983 .subvendor = 0x5ace, /* Beholder Intl. Ltd. */
6984 .subdevice = 0x7090,
6985 .driver_data = SAA7134_BOARD_BEHOLD_A7,
6986 }, {
6987 .vendor = PCI_VENDOR_ID_PHILIPS,
6988 .device = PCI_DEVICE_ID_PHILIPS_SAA7135,
6989 .subvendor = 0x185b,
6990 .subdevice = 0xc900,
6991 .driver_data = SAA7134_BOARD_VIDEOMATE_M1F,
6992 }, {
6993 .vendor = PCI_VENDOR_ID_PHILIPS,
6994 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
6995 .subvendor = 0x5ace,
6996 .subdevice = 0x5030,
6997 .driver_data = SAA7134_BOARD_BEHOLD_503FM,
6998 }, {
6999 .vendor = PCI_VENDOR_ID_PHILIPS,
7000 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
7001 .subvendor = 0x5ace,
7002 .subdevice = 0x5010,
7003 .driver_data = SAA7134_BOARD_BEHOLD_501,
7004 }, {
7005 .vendor = PCI_VENDOR_ID_PHILIPS,
7006 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
7007 .subvendor = 0x17de,
7008 .subdevice = 0xd136,
7009 .driver_data = SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2,
7010 }, {
7011 .vendor = PCI_VENDOR_ID_PHILIPS,
7012 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
7013 .subvendor = 0x6000,
7014 .subdevice = 0x0811,
7015 .driver_data = SAA7134_BOARD_SENSORAY811_911,
7016 }, {
7017 .vendor = PCI_VENDOR_ID_PHILIPS,
7018 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
7019 .subvendor = 0x6000,
7020 .subdevice = 0x0911,
7021 .driver_data = SAA7134_BOARD_SENSORAY811_911,
7022 }, {
7023 /* --- boards without eeprom + subsystem ID --- */
7024 .vendor = PCI_VENDOR_ID_PHILIPS,
7025 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
7026 .subvendor = PCI_VENDOR_ID_PHILIPS,
7027 .subdevice = 0,
7028 .driver_data = SAA7134_BOARD_NOAUTO,
7029 },{
7030 .vendor = PCI_VENDOR_ID_PHILIPS,
7031 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
7032 .subvendor = PCI_VENDOR_ID_PHILIPS,
7033 .subdevice = 0,
7034 .driver_data = SAA7134_BOARD_NOAUTO,
7035 },{
7036 /* --- default catch --- */
7037 .vendor = PCI_VENDOR_ID_PHILIPS,
7038 .device = PCI_DEVICE_ID_PHILIPS_SAA7130,
7039 .subvendor = PCI_ANY_ID,
7040 .subdevice = PCI_ANY_ID,
7041 .driver_data = SAA7134_BOARD_UNKNOWN,
7042 },{
7043 .vendor = PCI_VENDOR_ID_PHILIPS,
7044 .device = PCI_DEVICE_ID_PHILIPS_SAA7133,
7045 .subvendor = PCI_ANY_ID,
7046 .subdevice = PCI_ANY_ID,
7047 .driver_data = SAA7134_BOARD_UNKNOWN,
7048 },{
7049 .vendor = PCI_VENDOR_ID_PHILIPS,
7050 .device = PCI_DEVICE_ID_PHILIPS_SAA7134,
7051 .subvendor = PCI_ANY_ID,
7052 .subdevice = PCI_ANY_ID,
7053 .driver_data = SAA7134_BOARD_UNKNOWN,
7054 },{
7055 .vendor = PCI_VENDOR_ID_PHILIPS,
7056 .device = PCI_DEVICE_ID_PHILIPS_SAA7135,
7057 .subvendor = PCI_ANY_ID,
7058 .subdevice = PCI_ANY_ID,
7059 .driver_data = SAA7134_BOARD_UNKNOWN,
7060 },{
7061 /* --- end of list --- */
7062 }
7063};
7064MODULE_DEVICE_TABLE(pci, saa7134_pci_tbl);
7065
7066/* ----------------------------------------------------------- */
7067/* flyvideo tweaks */
7068
7069
7070static void board_flyvideo(struct saa7134_dev *dev)
7071{
7072 printk("%s: there are different flyvideo cards with different tuners\n"
7073 "%s: out there, you might have to use the tuner=<nr> insmod\n"
7074 "%s: option to override the default value.\n",
7075 dev->name, dev->name, dev->name);
7076}
7077
7078static int saa7134_xc2028_callback(struct saa7134_dev *dev,
7079 int command, int arg)
7080{
7081 switch (command) {
7082 case XC2028_TUNER_RESET:
7083 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x00008000, 0x00000000);
7084 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x00008000, 0x00008000);
7085 switch (dev->board) {
7086 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
7087 case SAA7134_BOARD_AVERMEDIA_M103:
7088 saa7134_set_gpio(dev, 23, 0);
7089 msleep(10);
7090 saa7134_set_gpio(dev, 23, 1);
7091 break;
7092 case SAA7134_BOARD_AVERMEDIA_A16D:
7093 saa7134_set_gpio(dev, 21, 0);
7094 msleep(10);
7095 saa7134_set_gpio(dev, 21, 1);
7096 break;
7097 case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
7098 saa7134_set_gpio(dev, 18, 0);
7099 msleep(10);
7100 saa7134_set_gpio(dev, 18, 1);
7101 break;
7102 case SAA7134_BOARD_VIDEOMATE_T750:
7103 saa7134_set_gpio(dev, 20, 0);
7104 msleep(10);
7105 saa7134_set_gpio(dev, 20, 1);
7106 break;
7107 }
7108 return 0;
7109 }
7110 return -EINVAL;
7111}
7112
7113static int saa7134_xc5000_callback(struct saa7134_dev *dev,
7114 int command, int arg)
7115{
7116 switch (dev->board) {
7117 case SAA7134_BOARD_BEHOLD_X7:
7118 case SAA7134_BOARD_BEHOLD_H7:
7119 case SAA7134_BOARD_BEHOLD_A7:
7120 if (command == XC5000_TUNER_RESET) {
7121 /* Down and UP pheripherial RESET pin for reset all chips */
7122 saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
7123 msleep(10);
7124 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
7125 msleep(10);
7126 }
7127 break;
7128 default:
7129 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x06e20000, 0x06e20000);
7130 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x06a20000, 0x06a20000);
7131 saa_andorl(SAA7133_ANALOG_IO_SELECT >> 2, 0x02, 0x02);
7132 saa_andorl(SAA7134_ANALOG_IN_CTRL1 >> 2, 0x81, 0x81);
7133 saa_andorl(SAA7134_AUDIO_CLOCK0 >> 2, 0x03187de7, 0x03187de7);
7134 saa_andorl(SAA7134_AUDIO_PLL_CTRL >> 2, 0x03, 0x03);
7135 saa_andorl(SAA7134_AUDIO_CLOCKS_PER_FIELD0 >> 2,
7136 0x0001e000, 0x0001e000);
7137 break;
7138 }
7139 return 0;
7140}
7141
7142static int saa7134_tda8290_827x_callback(struct saa7134_dev *dev,
7143 int command, int arg)
7144{
7145 u8 sync_control;
7146
7147 switch (command) {
7148 case 0: /* switch LNA gain through GPIO 22*/
7149 saa7134_set_gpio(dev, 22, arg) ;
7150 break;
7151 case 1: /* vsync output at GPIO22. 50 / 60Hz */
7152 saa_andorb(SAA7134_VIDEO_PORT_CTRL3, 0x80, 0x80);
7153 saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x03);
7154 if (arg == 1)
7155 sync_control = 11;
7156 else
7157 sync_control = 17;
7158 saa_writeb(SAA7134_VGATE_START, sync_control);
7159 saa_writeb(SAA7134_VGATE_STOP, sync_control + 1);
7160 saa_andorb(SAA7134_MISC_VGATE_MSB, 0x03, 0x00);
7161 break;
7162 default:
7163 return -EINVAL;
7164 }
7165
7166 return 0;
7167}
7168
7169static inline int saa7134_tda18271_hvr11x0_toggle_agc(struct saa7134_dev *dev,
7170 enum tda18271_mode mode)
7171{
7172 /* toggle AGC switch through GPIO 26 */
7173 switch (mode) {
7174 case TDA18271_ANALOG:
7175 saa7134_set_gpio(dev, 26, 0);
7176 break;
7177 case TDA18271_DIGITAL:
7178 saa7134_set_gpio(dev, 26, 1);
7179 break;
7180 default:
7181 return -EINVAL;
7182 }
7183 return 0;
7184}
7185
7186static inline int saa7134_kworld_sbtvd_toggle_agc(struct saa7134_dev *dev,
7187 enum tda18271_mode mode)
7188{
7189 /* toggle AGC switch through GPIO 27 */
7190 switch (mode) {
7191 case TDA18271_ANALOG:
7192 saa_writel(SAA7134_GPIO_GPMODE0 >> 2, 0x4000);
7193 saa_writel(SAA7134_GPIO_GPSTATUS0 >> 2, 0x4000);
7194 msleep(20);
7195 break;
7196 case TDA18271_DIGITAL:
7197 saa_writel(SAA7134_GPIO_GPMODE0 >> 2, 0x14000);
7198 saa_writel(SAA7134_GPIO_GPSTATUS0 >> 2, 0x14000);
7199 msleep(20);
7200 saa_writel(SAA7134_GPIO_GPMODE0 >> 2, 0x54000);
7201 saa_writel(SAA7134_GPIO_GPSTATUS0 >> 2, 0x54000);
7202 msleep(30);
7203 break;
7204 default:
7205 return -EINVAL;
7206 }
7207 return 0;
7208}
7209
7210static int saa7134_kworld_pc150u_toggle_agc(struct saa7134_dev *dev,
7211 enum tda18271_mode mode)
7212{
7213 switch (mode) {
7214 case TDA18271_ANALOG:
7215 saa7134_set_gpio(dev, 18, 0);
7216 break;
7217 case TDA18271_DIGITAL:
7218 saa7134_set_gpio(dev, 18, 1);
7219 msleep(30);
7220 break;
7221 default:
7222 return -EINVAL;
7223 }
7224 return 0;
7225}
7226
7227static int saa7134_tda8290_18271_callback(struct saa7134_dev *dev,
7228 int command, int arg)
7229{
7230 int ret = 0;
7231
7232 switch (command) {
7233 case TDA18271_CALLBACK_CMD_AGC_ENABLE: /* 0 */
7234 switch (dev->board) {
7235 case SAA7134_BOARD_HAUPPAUGE_HVR1150:
7236 case SAA7134_BOARD_HAUPPAUGE_HVR1120:
7237 case SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2:
7238 ret = saa7134_tda18271_hvr11x0_toggle_agc(dev, arg);
7239 break;
7240 case SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG:
7241 ret = saa7134_kworld_sbtvd_toggle_agc(dev, arg);
7242 break;
7243 case SAA7134_BOARD_KWORLD_PC150U:
7244 ret = saa7134_kworld_pc150u_toggle_agc(dev, arg);
7245 break;
7246 default:
7247 break;
7248 }
7249 break;
7250 default:
7251 ret = -EINVAL;
7252 break;
7253 }
7254 return ret;
7255}
7256
7257static int saa7134_tda8290_callback(struct saa7134_dev *dev,
7258 int command, int arg)
7259{
7260 int ret;
7261
7262 switch (dev->board) {
7263 case SAA7134_BOARD_HAUPPAUGE_HVR1150:
7264 case SAA7134_BOARD_HAUPPAUGE_HVR1120:
7265 case SAA7134_BOARD_AVERMEDIA_M733A:
7266 case SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG:
7267 case SAA7134_BOARD_KWORLD_PC150U:
7268 case SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2:
7269 /* tda8290 + tda18271 */
7270 ret = saa7134_tda8290_18271_callback(dev, command, arg);
7271 break;
7272 default:
7273 /* tda8290 + tda827x */
7274 ret = saa7134_tda8290_827x_callback(dev, command, arg);
7275 break;
7276 }
7277 return ret;
7278}
7279
7280int saa7134_tuner_callback(void *priv, int component, int command, int arg)
7281{
7282 struct saa7134_dev *dev = priv;
7283
7284 if (dev != NULL) {
7285 switch (dev->tuner_type) {
7286 case TUNER_PHILIPS_TDA8290:
7287 return saa7134_tda8290_callback(dev, command, arg);
7288 case TUNER_XC2028:
7289 return saa7134_xc2028_callback(dev, command, arg);
7290 case TUNER_XC5000:
7291 return saa7134_xc5000_callback(dev, command, arg);
7292 }
7293 } else {
7294 printk(KERN_ERR "saa7134: Error - device struct undefined.\n");
7295 return -EINVAL;
7296 }
7297 return -EINVAL;
7298}
7299EXPORT_SYMBOL(saa7134_tuner_callback);
7300
7301/* ----------------------------------------------------------- */
7302
7303static void hauppauge_eeprom(struct saa7134_dev *dev, u8 *eeprom_data)
7304{
7305 struct tveeprom tv;
7306
7307 tveeprom_hauppauge_analog(&dev->i2c_client, &tv, eeprom_data);
7308
7309 /* Make sure we support the board model */
7310 switch (tv.model) {
7311 case 67019: /* WinTV-HVR1110 (Retail, IR Blaster, hybrid, FM, SVid/Comp, 3.5mm audio in) */
7312 case 67109: /* WinTV-HVR1000 (Retail, IR Receive, analog, no FM, SVid/Comp, 3.5mm audio in) */
7313 case 67201: /* WinTV-HVR1150 (Retail, IR Receive, hybrid, FM, SVid/Comp, 3.5mm audio in) */
7314 case 67301: /* WinTV-HVR1000 (Retail, IR Receive, analog, no FM, SVid/Comp, 3.5mm audio in) */
7315 case 67209: /* WinTV-HVR1110 (Retail, IR Receive, hybrid, FM, SVid/Comp, 3.5mm audio in) */
7316 case 67559: /* WinTV-HVR1110 (OEM, no IR, hybrid, FM, SVid/Comp, RCA aud) */
7317 case 67569: /* WinTV-HVR1110 (OEM, no IR, hybrid, FM) */
7318 case 67579: /* WinTV-HVR1110 (OEM, no IR, hybrid, no FM) */
7319 case 67589: /* WinTV-HVR1110 (OEM, no IR, hybrid, no FM, SVid/Comp, RCA aud) */
7320 case 67599: /* WinTV-HVR1110 (OEM, no IR, hybrid, no FM, SVid/Comp, RCA aud) */
7321 case 67651: /* WinTV-HVR1150 (OEM, no IR, hybrid, FM, SVid/Comp, RCA aud) */
7322 case 67659: /* WinTV-HVR1110 (OEM, no IR, hybrid, FM, SVid/Comp, RCA aud) */
7323 break;
7324 default:
7325 printk(KERN_WARNING "%s: warning: "
7326 "unknown hauppauge model #%d\n", dev->name, tv.model);
7327 break;
7328 }
7329
7330 printk(KERN_INFO "%s: hauppauge eeprom: model=%d\n",
7331 dev->name, tv.model);
7332}
7333
7334/* ----------------------------------------------------------- */
7335
7336int saa7134_board_init1(struct saa7134_dev *dev)
7337{
7338 /* Always print gpio, often manufacturers encode tuner type and other info. */
7339 saa_writel(SAA7134_GPIO_GPMODE0 >> 2, 0);
7340 dev->gpio_value = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
7341 printk(KERN_INFO "%s: board init: gpio is %x\n", dev->name, dev->gpio_value);
7342
7343 switch (dev->board) {
7344 case SAA7134_BOARD_FLYVIDEO2000:
7345 case SAA7134_BOARD_FLYVIDEO3000:
7346 case SAA7134_BOARD_FLYVIDEO3000_NTSC:
7347 dev->has_remote = SAA7134_REMOTE_GPIO;
7348 board_flyvideo(dev);
7349 break;
7350 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
7351 case SAA7134_BOARD_FLYTVPLATINUM_FM:
7352 case SAA7134_BOARD_CINERGY400:
7353 case SAA7134_BOARD_CINERGY600:
7354 case SAA7134_BOARD_CINERGY600_MK3:
7355 case SAA7134_BOARD_ECS_TVP3XP:
7356 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
7357 case SAA7134_BOARD_ECS_TVP3XP_4CB6:
7358 case SAA7134_BOARD_MD2819:
7359 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
7360 case SAA7134_BOARD_KWORLD_XPERT:
7361 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
7362 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
7363 case SAA7134_BOARD_AVERMEDIA_305:
7364 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
7365 case SAA7134_BOARD_AVERMEDIA_307:
7366 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
7367 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
7368 case SAA7134_BOARD_AVERMEDIA_777:
7369 case SAA7134_BOARD_AVERMEDIA_M135A:
7370/* case SAA7134_BOARD_SABRENT_SBTTVFM: */ /* not finished yet */
7371 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
7372 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
7373 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
7374 case SAA7134_BOARD_VIDEOMATE_M1F:
7375 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
7376 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
7377 case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
7378 case SAA7134_BOARD_MANLI_MTV001:
7379 case SAA7134_BOARD_MANLI_MTV002:
7380 case SAA7134_BOARD_BEHOLD_409FM:
7381 case SAA7134_BOARD_AVACSSMARTTV:
7382 case SAA7134_BOARD_GOTVIEW_7135:
7383 case SAA7134_BOARD_KWORLD_TERMINATOR:
7384 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
7385 case SAA7134_BOARD_FLYDVBT_LR301:
7386 case SAA7134_BOARD_ASUSTeK_PS3_100:
7387 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
7388 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
7389 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
7390 case SAA7134_BOARD_FLYDVBTDUO:
7391 case SAA7134_BOARD_PROTEUS_2309:
7392 case SAA7134_BOARD_AVERMEDIA_A16AR:
7393 case SAA7134_BOARD_ENCORE_ENLTV:
7394 case SAA7134_BOARD_ENCORE_ENLTV_FM:
7395 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
7396 case SAA7134_BOARD_ENCORE_ENLTV_FM3:
7397 case SAA7134_BOARD_10MOONSTVMASTER3:
7398 case SAA7134_BOARD_BEHOLD_401:
7399 case SAA7134_BOARD_BEHOLD_403:
7400 case SAA7134_BOARD_BEHOLD_403FM:
7401 case SAA7134_BOARD_BEHOLD_405:
7402 case SAA7134_BOARD_BEHOLD_405FM:
7403 case SAA7134_BOARD_BEHOLD_407:
7404 case SAA7134_BOARD_BEHOLD_407FM:
7405 case SAA7134_BOARD_BEHOLD_409:
7406 case SAA7134_BOARD_BEHOLD_505FM:
7407 case SAA7134_BOARD_BEHOLD_505RDS_MK5:
7408 case SAA7134_BOARD_BEHOLD_505RDS_MK3:
7409 case SAA7134_BOARD_BEHOLD_507_9FM:
7410 case SAA7134_BOARD_BEHOLD_507RDS_MK3:
7411 case SAA7134_BOARD_BEHOLD_507RDS_MK5:
7412 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
7413 case SAA7134_BOARD_REAL_ANGEL_220:
7414 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
7415 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
7416 case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
7417 case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
7418 dev->has_remote = SAA7134_REMOTE_GPIO;
7419 break;
7420 case SAA7134_BOARD_FLYDVBS_LR300:
7421 saa_writeb(SAA7134_GPIO_GPMODE3, 0x80);
7422 saa_writeb(SAA7134_GPIO_GPSTATUS2, 0x40);
7423 dev->has_remote = SAA7134_REMOTE_GPIO;
7424 break;
7425 case SAA7134_BOARD_MD5044:
7426 printk("%s: seems there are two different versions of the MD5044\n"
7427 "%s: (with the same ID) out there. If sound doesn't work for\n"
7428 "%s: you try the audio_clock_override=0x200000 insmod option.\n",
7429 dev->name,dev->name,dev->name);
7430 break;
7431 case SAA7134_BOARD_CINERGY400_CARDBUS:
7432 /* power-up tuner chip */
7433 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x00040000, 0x00040000);
7434 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x00040000, 0x00000000);
7435 break;
7436 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
7437 /* this turns the remote control chip off to work around a bug in it */
7438 saa_writeb(SAA7134_GPIO_GPMODE1, 0x80);
7439 saa_writeb(SAA7134_GPIO_GPSTATUS1, 0x80);
7440 break;
7441 case SAA7134_BOARD_MONSTERTV_MOBILE:
7442 /* power-up tuner chip */
7443 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x00040000, 0x00040000);
7444 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x00040000, 0x00000004);
7445 break;
7446 case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
7447 /* turn the fan on */
7448 saa_writeb(SAA7134_GPIO_GPMODE3, 0x08);
7449 saa_writeb(SAA7134_GPIO_GPSTATUS3, 0x06);
7450 break;
7451 case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
7452 case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
7453 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x08000000, 0x08000000);
7454 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x08000000, 0x00000000);
7455 break;
7456 case SAA7134_BOARD_AVERMEDIA_CARDBUS:
7457 case SAA7134_BOARD_AVERMEDIA_M115:
7458 /* power-down tuner chip */
7459 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0xffffffff, 0);
7460 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0xffffffff, 0);
7461 msleep(10);
7462 /* power-up tuner chip */
7463 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0xffffffff, 0xffffffff);
7464 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0xffffffff, 0xffffffff);
7465 msleep(10);
7466 break;
7467 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
7468 /* power-down tuner chip */
7469 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x08400000, 0x08400000);
7470 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x08400000, 0);
7471 msleep(10);
7472 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x08400000, 0x08400000);
7473 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x08400000, 0x08400000);
7474 msleep(10);
7475 dev->has_remote = SAA7134_REMOTE_I2C;
7476 break;
7477 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
7478 saa7134_set_gpio(dev, 23, 0);
7479 msleep(10);
7480 saa7134_set_gpio(dev, 23, 1);
7481 dev->has_remote = SAA7134_REMOTE_I2C;
7482 break;
7483 case SAA7134_BOARD_AVERMEDIA_M103:
7484 saa7134_set_gpio(dev, 23, 0);
7485 msleep(10);
7486 saa7134_set_gpio(dev, 23, 1);
7487 break;
7488 case SAA7134_BOARD_AVERMEDIA_A16D:
7489 saa7134_set_gpio(dev, 21, 0);
7490 msleep(10);
7491 saa7134_set_gpio(dev, 21, 1);
7492 msleep(1);
7493 dev->has_remote = SAA7134_REMOTE_GPIO;
7494 break;
7495 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
7496 /* power-down tuner chip */
7497 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x000A8004, 0x000A8004);
7498 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x000A8004, 0);
7499 msleep(10);
7500 /* power-up tuner chip */
7501 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x000A8004, 0x000A8004);
7502 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x000A8004, 0x000A8004);
7503 msleep(10);
7504 /* remote via GPIO */
7505 dev->has_remote = SAA7134_REMOTE_GPIO;
7506 break;
7507 case SAA7134_BOARD_RTD_VFG7350:
7508
7509 /*
7510 * Make sure Production Test Register at offset 0x1D1 is cleared
7511 * to take chip out of test mode. Clearing bit 4 (TST_EN_AOUT)
7512 * prevents pin 105 from remaining low; keeping pin 105 low
7513 * continually resets the SAA6752 chip.
7514 */
7515
7516 saa_writeb (SAA7134_PRODUCTION_TEST_MODE, 0x00);
7517 break;
7518 case SAA7134_BOARD_HAUPPAUGE_HVR1150:
7519 case SAA7134_BOARD_HAUPPAUGE_HVR1120:
7520 dev->has_remote = SAA7134_REMOTE_GPIO;
7521 /* GPIO 26 high for digital, low for analog */
7522 saa7134_set_gpio(dev, 26, 0);
7523 msleep(1);
7524
7525 saa7134_set_gpio(dev, 22, 0);
7526 msleep(10);
7527 saa7134_set_gpio(dev, 22, 1);
7528 break;
7529 /* i2c remotes */
7530 case SAA7134_BOARD_PINNACLE_PCTV_110i:
7531 case SAA7134_BOARD_PINNACLE_PCTV_310i:
7532 case SAA7134_BOARD_UPMOST_PURPLE_TV:
7533 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
7534 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
7535 case SAA7134_BOARD_BEHOLD_607FM_MK3:
7536 case SAA7134_BOARD_BEHOLD_607FM_MK5:
7537 case SAA7134_BOARD_BEHOLD_609FM_MK3:
7538 case SAA7134_BOARD_BEHOLD_609FM_MK5:
7539 case SAA7134_BOARD_BEHOLD_607RDS_MK3:
7540 case SAA7134_BOARD_BEHOLD_607RDS_MK5:
7541 case SAA7134_BOARD_BEHOLD_609RDS_MK3:
7542 case SAA7134_BOARD_BEHOLD_609RDS_MK5:
7543 case SAA7134_BOARD_BEHOLD_M6:
7544 case SAA7134_BOARD_BEHOLD_M63:
7545 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
7546 case SAA7134_BOARD_BEHOLD_H6:
7547 case SAA7134_BOARD_BEHOLD_X7:
7548 case SAA7134_BOARD_BEHOLD_H7:
7549 case SAA7134_BOARD_BEHOLD_A7:
7550 case SAA7134_BOARD_KWORLD_PC150U:
7551 dev->has_remote = SAA7134_REMOTE_I2C;
7552 break;
7553 case SAA7134_BOARD_AVERMEDIA_A169_B:
7554 printk("%s: %s: dual saa713x broadcast decoders\n"
7555 "%s: Sorry, none of the inputs to this chip are supported yet.\n"
7556 "%s: Dual decoder functionality is disabled for now, use the other chip.\n",
7557 dev->name,card(dev).name,dev->name,dev->name);
7558 break;
7559 case SAA7134_BOARD_AVERMEDIA_M102:
7560 /* enable tuner */
7561 dev->has_remote = SAA7134_REMOTE_GPIO;
7562 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x8c040007, 0x8c040007);
7563 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0c0007cd, 0x0c0007cd);
7564 break;
7565 case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
7566 case SAA7134_BOARD_AVERMEDIA_A700_PRO:
7567 /* write windows gpio values */
7568 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x80040100, 0x80040100);
7569 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x80040100, 0x00040100);
7570 break;
7571 case SAA7134_BOARD_VIDEOMATE_S350:
7572 dev->has_remote = SAA7134_REMOTE_GPIO;
7573 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x0000C000, 0x0000C000);
7574 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0000C000, 0x0000C000);
7575 break;
7576 case SAA7134_BOARD_AVERMEDIA_M733A:
7577 saa7134_set_gpio(dev, 1, 1);
7578 msleep(10);
7579 saa7134_set_gpio(dev, 1, 0);
7580 msleep(10);
7581 saa7134_set_gpio(dev, 1, 1);
7582 dev->has_remote = SAA7134_REMOTE_GPIO;
7583 break;
7584 case SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2:
7585 /* enable LGS-8G75 */
7586 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x0e050000, 0x0c050000);
7587 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x0e050000, 0x0c050000);
7588 break;
7589 case SAA7134_BOARD_VIDEOMATE_T750:
7590 /* enable the analog tuner */
7591 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x00008000, 0x00008000);
7592 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, 0x00008000, 0x00008000);
7593 break;
7594 }
7595 return 0;
7596}
7597
7598static void saa7134_tuner_setup(struct saa7134_dev *dev)
7599{
7600 struct tuner_setup tun_setup;
7601 unsigned int mode_mask = T_RADIO | T_ANALOG_TV;
7602
7603 memset(&tun_setup, 0, sizeof(tun_setup));
7604 tun_setup.tuner_callback = saa7134_tuner_callback;
7605
7606 if (saa7134_boards[dev->board].radio_type != UNSET) {
7607 tun_setup.type = saa7134_boards[dev->board].radio_type;
7608 tun_setup.addr = saa7134_boards[dev->board].radio_addr;
7609
7610 tun_setup.mode_mask = T_RADIO;
7611
7612 saa_call_all(dev, tuner, s_type_addr, &tun_setup);
7613 mode_mask &= ~T_RADIO;
7614 }
7615
7616 if ((dev->tuner_type != TUNER_ABSENT) && (dev->tuner_type != UNSET)) {
7617 tun_setup.type = dev->tuner_type;
7618 tun_setup.addr = dev->tuner_addr;
7619 tun_setup.config = saa7134_boards[dev->board].tuner_config;
7620 tun_setup.tuner_callback = saa7134_tuner_callback;
7621
7622 tun_setup.mode_mask = mode_mask;
7623
7624 saa_call_all(dev, tuner, s_type_addr, &tun_setup);
7625 }
7626
7627 if (dev->tda9887_conf) {
7628 struct v4l2_priv_tun_config tda9887_cfg;
7629
7630 tda9887_cfg.tuner = TUNER_TDA9887;
7631 tda9887_cfg.priv = &dev->tda9887_conf;
7632
7633 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
7634 }
7635
7636 if (dev->tuner_type == TUNER_XC2028) {
7637 struct v4l2_priv_tun_config xc2028_cfg;
7638 struct xc2028_ctrl ctl;
7639
7640 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
7641 memset(&ctl, 0, sizeof(ctl));
7642
7643 ctl.fname = XC2028_DEFAULT_FIRMWARE;
7644 ctl.max_len = 64;
7645
7646 switch (dev->board) {
7647 case SAA7134_BOARD_AVERMEDIA_A16D:
7648 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
7649 case SAA7134_BOARD_AVERMEDIA_M103:
7650 case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
7651 ctl.demod = XC3028_FE_ZARLINK456;
7652 break;
7653 default:
7654 ctl.demod = XC3028_FE_OREN538;
7655 ctl.mts = 1;
7656 }
7657
7658 xc2028_cfg.tuner = TUNER_XC2028;
7659 xc2028_cfg.priv = &ctl;
7660
7661 saa_call_all(dev, tuner, s_config, &xc2028_cfg);
7662 }
7663}
7664
7665/* stuff which needs working i2c */
7666int saa7134_board_init2(struct saa7134_dev *dev)
7667{
7668 unsigned char buf;
7669 int board;
7670
7671 /* Put here the code that enables the chips that are needed
7672 for analog mode and doesn't depend on the tuner attachment.
7673 It is also a good idea to get tuner type from eeprom, etc before
7674 initializing tuner, since we can avoid loading tuner driver
7675 on devices that has TUNER_ABSENT
7676 */
7677 switch (dev->board) {
7678 case SAA7134_BOARD_BMK_MPEX_NOTUNER:
7679 case SAA7134_BOARD_BMK_MPEX_TUNER:
7680 /* Checks if the device has a tuner at 0x60 addr
7681 If the device doesn't have a tuner, TUNER_ABSENT
7682 will be used at tuner_type, avoiding loading tuner
7683 without needing it
7684 */
7685 dev->i2c_client.addr = 0x60;
7686 board = (i2c_master_recv(&dev->i2c_client, &buf, 0) < 0)
7687 ? SAA7134_BOARD_BMK_MPEX_NOTUNER
7688 : SAA7134_BOARD_BMK_MPEX_TUNER;
7689 if (board == dev->board)
7690 break;
7691 dev->board = board;
7692 printk("%s: board type fixup: %s\n", dev->name,
7693 saa7134_boards[dev->board].name);
7694 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
7695
7696 break;
7697 case SAA7134_BOARD_MD7134:
7698 {
7699 u8 subaddr;
7700 u8 data[3];
7701 int ret, tuner_t;
7702 struct i2c_msg msg[] = {{.addr=0x50, .flags=0, .buf=&subaddr, .len = 1},
7703 {.addr=0x50, .flags=I2C_M_RD, .buf=data, .len = 3}};
7704
7705 subaddr= 0x14;
7706 tuner_t = 0;
7707
7708 /* Retrieve device data from eeprom, checking for the
7709 proper tuner_type.
7710 */
7711 ret = i2c_transfer(&dev->i2c_adap, msg, 2);
7712 if (ret != 2) {
7713 printk(KERN_ERR "EEPROM read failure\n");
7714 } else if ((data[0] != 0) && (data[0] != 0xff)) {
7715 /* old config structure */
7716 subaddr = data[0] + 2;
7717 msg[1].len = 2;
7718 i2c_transfer(&dev->i2c_adap, msg, 2);
7719 tuner_t = (data[0] << 8) + data[1];
7720 switch (tuner_t){
7721 case 0x0103:
7722 dev->tuner_type = TUNER_PHILIPS_PAL;
7723 break;
7724 case 0x010C:
7725 dev->tuner_type = TUNER_PHILIPS_FM1216ME_MK3;
7726 break;
7727 default:
7728 printk(KERN_ERR "%s Can't determine tuner type %x from EEPROM\n", dev->name, tuner_t);
7729 }
7730 } else if ((data[1] != 0) && (data[1] != 0xff)) {
7731 /* new config structure */
7732 subaddr = data[1] + 1;
7733 msg[1].len = 1;
7734 i2c_transfer(&dev->i2c_adap, msg, 2);
7735 subaddr = data[0] + 1;
7736 msg[1].len = 2;
7737 i2c_transfer(&dev->i2c_adap, msg, 2);
7738 tuner_t = (data[1] << 8) + data[0];
7739 switch (tuner_t) {
7740 case 0x0005:
7741 dev->tuner_type = TUNER_PHILIPS_FM1216ME_MK3;
7742 break;
7743 case 0x001d:
7744 dev->tuner_type = TUNER_PHILIPS_FMD1216ME_MK3;
7745 printk(KERN_INFO "%s Board has DVB-T\n", dev->name);
7746 break;
7747 default:
7748 printk(KERN_ERR "%s Can't determine tuner type %x from EEPROM\n", dev->name, tuner_t);
7749 }
7750 } else {
7751 printk(KERN_ERR "%s unexpected config structure\n", dev->name);
7752 }
7753
7754 printk(KERN_INFO "%s Tuner type is %d\n", dev->name, dev->tuner_type);
7755 break;
7756 }
7757 case SAA7134_BOARD_PHILIPS_EUROPA:
7758 if (dev->autodetected && (dev->eedata[0x41] == 0x1c)) {
7759 /* Reconfigure board as Snake reference design */
7760 dev->board = SAA7134_BOARD_PHILIPS_SNAKE;
7761 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
7762 printk(KERN_INFO "%s: Reconfigured board as %s\n",
7763 dev->name, saa7134_boards[dev->board].name);
7764 break;
7765 }
7766 /* break intentionally omitted */
7767 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
7768 case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
7769 case SAA7134_BOARD_ASUS_EUROPA_HYBRID:
7770 case SAA7134_BOARD_TECHNOTREND_BUDGET_T3000:
7771 {
7772
7773 /* The Philips EUROPA based hybrid boards have the tuner
7774 connected through the channel decoder. We have to make it
7775 transparent to find it
7776 */
7777 u8 data[] = { 0x07, 0x02};
7778 struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
7779 i2c_transfer(&dev->i2c_adap, &msg, 1);
7780
7781 break;
7782 }
7783 case SAA7134_BOARD_PHILIPS_TIGER:
7784 case SAA7134_BOARD_PHILIPS_TIGER_S:
7785 {
7786 u8 data[] = { 0x3c, 0x33, 0x60};
7787 struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
7788 if (dev->autodetected && (dev->eedata[0x49] == 0x50)) {
7789 dev->board = SAA7134_BOARD_PHILIPS_TIGER_S;
7790 printk(KERN_INFO "%s: Reconfigured board as %s\n",
7791 dev->name, saa7134_boards[dev->board].name);
7792 }
7793 if (dev->board == SAA7134_BOARD_PHILIPS_TIGER_S) {
7794 dev->tuner_type = TUNER_PHILIPS_TDA8290;
7795
7796 data[2] = 0x68;
7797 i2c_transfer(&dev->i2c_adap, &msg, 1);
7798 break;
7799 }
7800 i2c_transfer(&dev->i2c_adap, &msg, 1);
7801 break;
7802 }
7803 case SAA7134_BOARD_ASUSTeK_TVFM7135:
7804 /* The card below is detected as card=53, but is different */
7805 if (dev->autodetected && (dev->eedata[0x27] == 0x03)) {
7806 dev->board = SAA7134_BOARD_ASUSTeK_P7131_ANALOG;
7807 printk(KERN_INFO "%s: P7131 analog only, using "
7808 "entry of %s\n",
7809 dev->name, saa7134_boards[dev->board].name);
7810
7811 /* IR init has already happened for other cards, so
7812 * we have to catch up. */
7813 dev->has_remote = SAA7134_REMOTE_GPIO;
7814 saa7134_input_init1(dev);
7815 }
7816 break;
7817 case SAA7134_BOARD_HAUPPAUGE_HVR1150:
7818 case SAA7134_BOARD_HAUPPAUGE_HVR1120:
7819 hauppauge_eeprom(dev, dev->eedata+0x80);
7820 break;
7821 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
7822 hauppauge_eeprom(dev, dev->eedata+0x80);
7823 /* break intentionally omitted */
7824 case SAA7134_BOARD_PINNACLE_PCTV_310i:
7825 case SAA7134_BOARD_KWORLD_DVBT_210:
7826 case SAA7134_BOARD_TEVION_DVBT_220RF:
7827 case SAA7134_BOARD_ASUSTeK_TIGER:
7828 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
7829 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
7830 case SAA7134_BOARD_MEDION_MD8800_QUADRO:
7831 case SAA7134_BOARD_AVERMEDIA_SUPER_007:
7832 case SAA7134_BOARD_TWINHAN_DTV_DVB_3056:
7833 case SAA7134_BOARD_CREATIX_CTX953:
7834 {
7835 /* this is a hybrid board, initialize to analog mode
7836 * and configure firmware eeprom address
7837 */
7838 u8 data[] = { 0x3c, 0x33, 0x60};
7839 struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
7840 i2c_transfer(&dev->i2c_adap, &msg, 1);
7841 break;
7842 }
7843 case SAA7134_BOARD_ASUSTeK_TIGER_3IN1:
7844 {
7845 u8 data[] = { 0x3c, 0x33, 0x60};
7846 struct i2c_msg msg = {.addr = 0x0b, .flags = 0, .buf = data,
7847 .len = sizeof(data)};
7848 i2c_transfer(&dev->i2c_adap, &msg, 1);
7849 break;
7850 }
7851 case SAA7134_BOARD_ASUSTeK_PS3_100:
7852 {
7853 u8 data[] = { 0x3c, 0x33, 0x60};
7854 struct i2c_msg msg = {.addr = 0x0b, .flags = 0, .buf = data,
7855 .len = sizeof(data)};
7856 i2c_transfer(&dev->i2c_adap, &msg, 1);
7857 break;
7858 }
7859 case SAA7134_BOARD_FLYDVB_TRIO:
7860 {
7861 u8 temp = 0;
7862 int rc;
7863 u8 data[] = { 0x3c, 0x33, 0x62};
7864 struct i2c_msg msg = {.addr=0x09, .flags=0, .buf=data, .len = sizeof(data)};
7865 i2c_transfer(&dev->i2c_adap, &msg, 1);
7866
7867 /*
7868 * send weak up message to pic16C505 chip
7869 * @ LifeView FlyDVB Trio
7870 */
7871 msg.buf = &temp;
7872 msg.addr = 0x0b;
7873 msg.len = 1;
7874 if (1 != i2c_transfer(&dev->i2c_adap, &msg, 1)) {
7875 printk(KERN_WARNING "%s: send wake up byte to pic16C505"
7876 "(IR chip) failed\n", dev->name);
7877 } else {
7878 msg.flags = I2C_M_RD;
7879 rc = i2c_transfer(&dev->i2c_adap, &msg, 1);
7880 printk(KERN_INFO "%s: probe IR chip @ i2c 0x%02x: %s\n",
7881 dev->name, msg.addr,
7882 (1 == rc) ? "yes" : "no");
7883 if (rc == 1)
7884 dev->has_remote = SAA7134_REMOTE_I2C;
7885 }
7886 break;
7887 }
7888 case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
7889 case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
7890 {
7891 /* initialize analog mode */
7892 u8 data[] = { 0x3c, 0x33, 0x6a};
7893 struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
7894 i2c_transfer(&dev->i2c_adap, &msg, 1);
7895 break;
7896 }
7897 case SAA7134_BOARD_CINERGY_HT_PCMCIA:
7898 case SAA7134_BOARD_CINERGY_HT_PCI:
7899 {
7900 /* initialize analog mode */
7901 u8 data[] = { 0x3c, 0x33, 0x68};
7902 struct i2c_msg msg = {.addr=0x08, .flags=0, .buf=data, .len = sizeof(data)};
7903 i2c_transfer(&dev->i2c_adap, &msg, 1);
7904 break;
7905 }
7906 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
7907 case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
7908 /* The T200 and the T200A share the same pci id. Consequently,
7909 * we are going to query eeprom to try to find out which one we
7910 * are actually looking at. */
7911
7912 /* Don't do this if the board was specifically selected with an
7913 * insmod option or if we have the default configuration T200*/
7914 if (!dev->autodetected || (dev->eedata[0x41] == 0xd0))
7915 break;
7916 if (dev->eedata[0x41] == 0x02) {
7917 /* Reconfigure board as T200A */
7918 dev->board = SAA7134_BOARD_VIDEOMATE_DVBT_200A;
7919 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
7920 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
7921 printk(KERN_INFO "%s: Reconfigured board as %s\n",
7922 dev->name, saa7134_boards[dev->board].name);
7923 } else {
7924 printk(KERN_WARNING "%s: Unexpected tuner type info: %x in eeprom\n",
7925 dev->name, dev->eedata[0x41]);
7926 break;
7927 }
7928 break;
7929 case SAA7134_BOARD_ADS_INSTANT_HDTV_PCI:
7930 case SAA7134_BOARD_KWORLD_ATSC110:
7931 {
7932 struct i2c_msg msg = { .addr = 0x0a, .flags = 0 };
7933 int i;
7934 static u8 buffer[][2] = {
7935 { 0x10, 0x12 },
7936 { 0x13, 0x04 },
7937 { 0x16, 0x00 },
7938 { 0x14, 0x04 },
7939 { 0x17, 0x00 },
7940 };
7941
7942 for (i = 0; i < ARRAY_SIZE(buffer); i++) {
7943 msg.buf = &buffer[i][0];
7944 msg.len = ARRAY_SIZE(buffer[0]);
7945 if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
7946 printk(KERN_WARNING
7947 "%s: Unable to enable tuner(%i).\n",
7948 dev->name, i);
7949 }
7950 break;
7951 }
7952 case SAA7134_BOARD_BEHOLD_H6:
7953 {
7954 u8 data[] = { 0x09, 0x9f, 0x86, 0x11};
7955 struct i2c_msg msg = {.addr = 0x61, .flags = 0, .buf = data,
7956 .len = sizeof(data)};
7957
7958 /* The tuner TUNER_PHILIPS_FMD1216MEX_MK3 after hardware */
7959 /* start has disabled IF and enabled DVB-T. When saa7134 */
7960 /* scan I2C devices it not detect IF tda9887 and can`t */
7961 /* watch TV without software reboot. For solve this problem */
7962 /* switch the tuner to analog TV mode manually. */
7963 if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1)
7964 printk(KERN_WARNING
7965 "%s: Unable to enable IF of the tuner.\n",
7966 dev->name);
7967 break;
7968 }
7969 case SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG:
7970 saa_writel(SAA7134_GPIO_GPMODE0 >> 2, 0x4000);
7971 saa_writel(SAA7134_GPIO_GPSTATUS0 >> 2, 0x4000);
7972
7973 saa7134_set_gpio(dev, 27, 0);
7974 break;
7975 } /* switch() */
7976
7977 /* initialize tuner */
7978 if (TUNER_ABSENT != dev->tuner_type) {
7979 int has_demod = (dev->tda9887_conf & TDA9887_PRESENT);
7980
7981 /* Note: radio tuner address is always filled in,
7982 so we do not need to probe for a radio tuner device. */
7983 if (dev->radio_type != UNSET)
7984 v4l2_i2c_new_subdev(&dev->v4l2_dev,
7985 &dev->i2c_adap, "tuner",
7986 dev->radio_addr, NULL);
7987 if (has_demod)
7988 v4l2_i2c_new_subdev(&dev->v4l2_dev,
7989 &dev->i2c_adap, "tuner",
7990 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
7991 if (dev->tuner_addr == ADDR_UNSET) {
7992 enum v4l2_i2c_tuner_type type =
7993 has_demod ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
7994
7995 v4l2_i2c_new_subdev(&dev->v4l2_dev,
7996 &dev->i2c_adap, "tuner",
7997 0, v4l2_i2c_tuner_addrs(type));
7998 } else {
7999 v4l2_i2c_new_subdev(&dev->v4l2_dev,
8000 &dev->i2c_adap, "tuner",
8001 dev->tuner_addr, NULL);
8002 }
8003 }
8004
8005 saa7134_tuner_setup(dev);
8006
8007 switch (dev->board) {
8008 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
8009 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
8010 {
8011 struct v4l2_priv_tun_config tea5767_cfg;
8012 struct tea5767_ctrl ctl;
8013
8014 dev->i2c_client.addr = 0xC0;
8015 /* set TEA5767(analog FM) defines */
8016 memset(&ctl, 0, sizeof(ctl));
8017 ctl.xtal_freq = TEA5767_HIGH_LO_13MHz;
8018 tea5767_cfg.tuner = TUNER_TEA5767;
8019 tea5767_cfg.priv = &ctl;
8020 saa_call_all(dev, tuner, s_config, &tea5767_cfg);
8021 break;
8022 }
8023 } /* switch() */
8024
8025 return 0;
8026}
diff --git a/drivers/media/pci/saa7134/saa7134-core.c b/drivers/media/pci/saa7134/saa7134-core.c
new file mode 100644
index 000000000000..5fbb4e49495c
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-core.c
@@ -0,0 +1,1368 @@
1/*
2 *
3 * device driver for philips saa7134 based TV cards
4 * driver core
5 *
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/kmod.h>
29#include <linux/sound.h>
30#include <linux/interrupt.h>
31#include <linux/delay.h>
32#include <linux/mutex.h>
33#include <linux/dma-mapping.h>
34#include <linux/pm.h>
35
36#include "saa7134-reg.h"
37#include "saa7134.h"
38
39MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
40MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
41MODULE_LICENSE("GPL");
42MODULE_VERSION(SAA7134_VERSION);
43
44
45/* ------------------------------------------------------------------ */
46
47static unsigned int irq_debug;
48module_param(irq_debug, int, 0644);
49MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
50
51static unsigned int core_debug;
52module_param(core_debug, int, 0644);
53MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
54
55static unsigned int gpio_tracking;
56module_param(gpio_tracking, int, 0644);
57MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
58
59static unsigned int alsa = 1;
60module_param(alsa, int, 0644);
61MODULE_PARM_DESC(alsa,"enable/disable ALSA DMA sound [dmasound]");
62
63static unsigned int latency = UNSET;
64module_param(latency, int, 0444);
65MODULE_PARM_DESC(latency,"pci latency timer");
66
67int saa7134_no_overlay=-1;
68module_param_named(no_overlay, saa7134_no_overlay, int, 0444);
69MODULE_PARM_DESC(no_overlay,"allow override overlay default (0 disables, 1 enables)"
70 " [some VIA/SIS chipsets are known to have problem with overlay]");
71
72static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
73static unsigned int vbi_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
74static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
75static unsigned int tuner[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
76static unsigned int card[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
77
78
79module_param_array(video_nr, int, NULL, 0444);
80module_param_array(vbi_nr, int, NULL, 0444);
81module_param_array(radio_nr, int, NULL, 0444);
82module_param_array(tuner, int, NULL, 0444);
83module_param_array(card, int, NULL, 0444);
84
85MODULE_PARM_DESC(video_nr, "video device number");
86MODULE_PARM_DESC(vbi_nr, "vbi device number");
87MODULE_PARM_DESC(radio_nr, "radio device number");
88MODULE_PARM_DESC(tuner, "tuner type");
89MODULE_PARM_DESC(card, "card type");
90
91DEFINE_MUTEX(saa7134_devlist_lock);
92EXPORT_SYMBOL(saa7134_devlist_lock);
93LIST_HEAD(saa7134_devlist);
94EXPORT_SYMBOL(saa7134_devlist);
95static LIST_HEAD(mops_list);
96static unsigned int saa7134_devcount;
97
98int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
99int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
100
101#define dprintk(fmt, arg...) if (core_debug) \
102 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
103
104void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
105{
106 unsigned long mode,status;
107
108 if (!gpio_tracking)
109 return;
110 /* rising SAA7134_GPIO_GPRESCAN reads the status */
111 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
112 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
113 mode = saa_readl(SAA7134_GPIO_GPMODE0 >> 2) & 0xfffffff;
114 status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
115 printk(KERN_DEBUG
116 "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
117 dev->name, mode, (~mode) & status, mode & status, msg);
118}
119
120void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)
121{
122 u32 index, bitval;
123
124 index = 1 << bit_no;
125 switch (value) {
126 case 0: /* static value */
127 case 1: dprintk("setting GPIO%d to static %d\n", bit_no, value);
128 /* turn sync mode off if necessary */
129 if (index & 0x00c00000)
130 saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x00);
131 if (value)
132 bitval = index;
133 else
134 bitval = 0;
135 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
136 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
137 break;
138 case 3: /* tristate */
139 dprintk("setting GPIO%d to tristate\n", bit_no);
140 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
141 break;
142 }
143}
144
145/* ------------------------------------------------------------------ */
146
147
148/* ----------------------------------------------------------- */
149/* delayed request_module */
150
151#if defined(CONFIG_MODULES) && defined(MODULE)
152
153static void request_module_async(struct work_struct *work){
154 struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
155 if (card_is_empress(dev))
156 request_module("saa7134-empress");
157 if (card_is_dvb(dev))
158 request_module("saa7134-dvb");
159 if (alsa) {
160 if (dev->pci->device != PCI_DEVICE_ID_PHILIPS_SAA7130)
161 request_module("saa7134-alsa");
162 }
163}
164
165static void request_submodules(struct saa7134_dev *dev)
166{
167 INIT_WORK(&dev->request_module_wk, request_module_async);
168 schedule_work(&dev->request_module_wk);
169}
170
171static void flush_request_submodules(struct saa7134_dev *dev)
172{
173 flush_work_sync(&dev->request_module_wk);
174}
175
176#else
177#define request_submodules(dev)
178#define flush_request_submodules(dev)
179#endif /* CONFIG_MODULES */
180
181/* ------------------------------------------------------------------ */
182
183/* nr of (saa7134-)pages for the given buffer size */
184static int saa7134_buffer_pages(int size)
185{
186 size = PAGE_ALIGN(size);
187 size += PAGE_SIZE; /* for non-page-aligned buffers */
188 size /= 4096;
189 return size;
190}
191
192/* calc max # of buffers from size (must not exceed the 4MB virtual
193 * address space per DMA channel) */
194int saa7134_buffer_count(unsigned int size, unsigned int count)
195{
196 unsigned int maxcount;
197
198 maxcount = 1024 / saa7134_buffer_pages(size);
199 if (count > maxcount)
200 count = maxcount;
201 return count;
202}
203
204int saa7134_buffer_startpage(struct saa7134_buf *buf)
205{
206 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
207}
208
209unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
210{
211 unsigned long base;
212 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
213
214 base = saa7134_buffer_startpage(buf) * 4096;
215 base += dma->sglist[0].offset;
216 return base;
217}
218
219/* ------------------------------------------------------------------ */
220
221int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
222{
223 __le32 *cpu;
224 dma_addr_t dma_addr = 0;
225
226 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
227 if (NULL == cpu)
228 return -ENOMEM;
229 pt->size = SAA7134_PGTABLE_SIZE;
230 pt->cpu = cpu;
231 pt->dma = dma_addr;
232 return 0;
233}
234
235int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
236 struct scatterlist *list, unsigned int length,
237 unsigned int startpage)
238{
239 __le32 *ptr;
240 unsigned int i,p;
241
242 BUG_ON(NULL == pt || NULL == pt->cpu);
243
244 ptr = pt->cpu + startpage;
245 for (i = 0; i < length; i++, list++)
246 for (p = 0; p * 4096 < list->length; p++, ptr++)
247 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
248 return 0;
249}
250
251void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
252{
253 if (NULL == pt->cpu)
254 return;
255 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
256 pt->cpu = NULL;
257}
258
259/* ------------------------------------------------------------------ */
260
261void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf)
262{
263 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
264 BUG_ON(in_interrupt());
265
266 videobuf_waiton(q, &buf->vb, 0, 0);
267 videobuf_dma_unmap(q->dev, dma);
268 videobuf_dma_free(dma);
269 buf->vb.state = VIDEOBUF_NEEDS_INIT;
270}
271
272/* ------------------------------------------------------------------ */
273
274int saa7134_buffer_queue(struct saa7134_dev *dev,
275 struct saa7134_dmaqueue *q,
276 struct saa7134_buf *buf)
277{
278 struct saa7134_buf *next = NULL;
279
280 assert_spin_locked(&dev->slock);
281 dprintk("buffer_queue %p\n",buf);
282 if (NULL == q->curr) {
283 if (!q->need_two) {
284 q->curr = buf;
285 buf->activate(dev,buf,NULL);
286 } else if (list_empty(&q->queue)) {
287 list_add_tail(&buf->vb.queue,&q->queue);
288 buf->vb.state = VIDEOBUF_QUEUED;
289 } else {
290 next = list_entry(q->queue.next,struct saa7134_buf,
291 vb.queue);
292 q->curr = buf;
293 buf->activate(dev,buf,next);
294 }
295 } else {
296 list_add_tail(&buf->vb.queue,&q->queue);
297 buf->vb.state = VIDEOBUF_QUEUED;
298 }
299 return 0;
300}
301
302void saa7134_buffer_finish(struct saa7134_dev *dev,
303 struct saa7134_dmaqueue *q,
304 unsigned int state)
305{
306 assert_spin_locked(&dev->slock);
307 dprintk("buffer_finish %p\n",q->curr);
308
309 /* finish current buffer */
310 q->curr->vb.state = state;
311 do_gettimeofday(&q->curr->vb.ts);
312 wake_up(&q->curr->vb.done);
313 q->curr = NULL;
314}
315
316void saa7134_buffer_next(struct saa7134_dev *dev,
317 struct saa7134_dmaqueue *q)
318{
319 struct saa7134_buf *buf,*next = NULL;
320
321 assert_spin_locked(&dev->slock);
322 BUG_ON(NULL != q->curr);
323
324 if (!list_empty(&q->queue)) {
325 /* activate next one from queue */
326 buf = list_entry(q->queue.next,struct saa7134_buf,vb.queue);
327 dprintk("buffer_next %p [prev=%p/next=%p]\n",
328 buf,q->queue.prev,q->queue.next);
329 list_del(&buf->vb.queue);
330 if (!list_empty(&q->queue))
331 next = list_entry(q->queue.next,struct saa7134_buf,
332 vb.queue);
333 q->curr = buf;
334 buf->activate(dev,buf,next);
335 dprintk("buffer_next #2 prev=%p/next=%p\n",
336 q->queue.prev,q->queue.next);
337 } else {
338 /* nothing to do -- just stop DMA */
339 dprintk("buffer_next %p\n",NULL);
340 saa7134_set_dmabits(dev);
341 del_timer(&q->timeout);
342
343 if (card_has_mpeg(dev))
344 if (dev->ts_started)
345 saa7134_ts_stop(dev);
346 }
347}
348
349void saa7134_buffer_timeout(unsigned long data)
350{
351 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
352 struct saa7134_dev *dev = q->dev;
353 unsigned long flags;
354
355 spin_lock_irqsave(&dev->slock,flags);
356
357 /* try to reset the hardware (SWRST) */
358 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
359 saa_writeb(SAA7134_REGION_ENABLE, 0x80);
360 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
361
362 /* flag current buffer as failed,
363 try to start over with the next one. */
364 if (q->curr) {
365 dprintk("timeout on %p\n",q->curr);
366 saa7134_buffer_finish(dev,q,VIDEOBUF_ERROR);
367 }
368 saa7134_buffer_next(dev,q);
369 spin_unlock_irqrestore(&dev->slock,flags);
370}
371
372/* ------------------------------------------------------------------ */
373
374int saa7134_set_dmabits(struct saa7134_dev *dev)
375{
376 u32 split, task=0, ctrl=0, irq=0;
377 enum v4l2_field cap = V4L2_FIELD_ANY;
378 enum v4l2_field ov = V4L2_FIELD_ANY;
379
380 assert_spin_locked(&dev->slock);
381
382 if (dev->insuspend)
383 return 0;
384
385 /* video capture -- dma 0 + video task A */
386 if (dev->video_q.curr) {
387 task |= 0x01;
388 ctrl |= SAA7134_MAIN_CTRL_TE0;
389 irq |= SAA7134_IRQ1_INTE_RA0_1 |
390 SAA7134_IRQ1_INTE_RA0_0;
391 cap = dev->video_q.curr->vb.field;
392 }
393
394 /* video capture -- dma 1+2 (planar modes) */
395 if (dev->video_q.curr &&
396 dev->video_q.curr->fmt->planar) {
397 ctrl |= SAA7134_MAIN_CTRL_TE4 |
398 SAA7134_MAIN_CTRL_TE5;
399 }
400
401 /* screen overlay -- dma 0 + video task B */
402 if (dev->ovenable) {
403 task |= 0x10;
404 ctrl |= SAA7134_MAIN_CTRL_TE1;
405 ov = dev->ovfield;
406 }
407
408 /* vbi capture -- dma 0 + vbi task A+B */
409 if (dev->vbi_q.curr) {
410 task |= 0x22;
411 ctrl |= SAA7134_MAIN_CTRL_TE2 |
412 SAA7134_MAIN_CTRL_TE3;
413 irq |= SAA7134_IRQ1_INTE_RA0_7 |
414 SAA7134_IRQ1_INTE_RA0_6 |
415 SAA7134_IRQ1_INTE_RA0_5 |
416 SAA7134_IRQ1_INTE_RA0_4;
417 }
418
419 /* audio capture -- dma 3 */
420 if (dev->dmasound.dma_running) {
421 ctrl |= SAA7134_MAIN_CTRL_TE6;
422 irq |= SAA7134_IRQ1_INTE_RA3_1 |
423 SAA7134_IRQ1_INTE_RA3_0;
424 }
425
426 /* TS capture -- dma 5 */
427 if (dev->ts_q.curr) {
428 ctrl |= SAA7134_MAIN_CTRL_TE5;
429 irq |= SAA7134_IRQ1_INTE_RA2_1 |
430 SAA7134_IRQ1_INTE_RA2_0;
431 }
432
433 /* set task conditions + field handling */
434 if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
435 /* default config -- use full frames */
436 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
437 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
438 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x02);
439 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x02);
440 split = 0;
441 } else {
442 /* split fields between tasks */
443 if (V4L2_FIELD_TOP == cap) {
444 /* odd A, even B, repeat */
445 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
446 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
447 } else {
448 /* odd B, even A, repeat */
449 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
450 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
451 }
452 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
453 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
454 split = 1;
455 }
456
457 /* irqs */
458 saa_writeb(SAA7134_REGION_ENABLE, task);
459 saa_writel(SAA7134_IRQ1, irq);
460 saa_andorl(SAA7134_MAIN_CTRL,
461 SAA7134_MAIN_CTRL_TE0 |
462 SAA7134_MAIN_CTRL_TE1 |
463 SAA7134_MAIN_CTRL_TE2 |
464 SAA7134_MAIN_CTRL_TE3 |
465 SAA7134_MAIN_CTRL_TE4 |
466 SAA7134_MAIN_CTRL_TE5 |
467 SAA7134_MAIN_CTRL_TE6,
468 ctrl);
469 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
470 task, ctrl, irq, split ? "no" : "yes");
471
472 return 0;
473}
474
475/* ------------------------------------------------------------------ */
476/* IRQ handler + helpers */
477
478static char *irqbits[] = {
479 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
480 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
481 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
482 "GPIO16", "GPIO18", "GPIO22", "GPIO23"
483};
484#define IRQBITS ARRAY_SIZE(irqbits)
485
486static void print_irqstatus(struct saa7134_dev *dev, int loop,
487 unsigned long report, unsigned long status)
488{
489 unsigned int i;
490
491 printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
492 dev->name,loop,jiffies,report,status);
493 for (i = 0; i < IRQBITS; i++) {
494 if (!(report & (1 << i)))
495 continue;
496 printk(" %s",irqbits[i]);
497 }
498 if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
499 printk(" | RA0=%s,%s,%s,%ld",
500 (status & 0x40) ? "vbi" : "video",
501 (status & 0x20) ? "b" : "a",
502 (status & 0x10) ? "odd" : "even",
503 (status & 0x0f));
504 }
505 printk("\n");
506}
507
508static irqreturn_t saa7134_irq(int irq, void *dev_id)
509{
510 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
511 unsigned long report,status;
512 int loop, handled = 0;
513
514 if (dev->insuspend)
515 goto out;
516
517 for (loop = 0; loop < 10; loop++) {
518 report = saa_readl(SAA7134_IRQ_REPORT);
519 status = saa_readl(SAA7134_IRQ_STATUS);
520
521 /* If dmasound support is active and we get a sound report,
522 * mask out the report and let the saa7134-alsa module deal
523 * with it */
524 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
525 (dev->dmasound.priv_data != NULL) )
526 {
527 if (irq_debug > 1)
528 printk(KERN_DEBUG "%s/irq: preserving DMA sound interrupt\n",
529 dev->name);
530 report &= ~SAA7134_IRQ_REPORT_DONE_RA3;
531 }
532
533 if (0 == report) {
534 if (irq_debug > 1)
535 printk(KERN_DEBUG "%s/irq: no (more) work\n",
536 dev->name);
537 goto out;
538 }
539
540 handled = 1;
541 saa_writel(SAA7134_IRQ_REPORT,report);
542 if (irq_debug)
543 print_irqstatus(dev,loop,report,status);
544
545
546 if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
547 (report & SAA7134_IRQ_REPORT_INTL))
548 saa7134_irq_video_signalchange(dev);
549
550
551 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
552 (status & 0x60) == 0)
553 saa7134_irq_video_done(dev,status);
554
555 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
556 (status & 0x40) == 0x40)
557 saa7134_irq_vbi_done(dev,status);
558
559 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
560 card_has_mpeg(dev))
561 saa7134_irq_ts_done(dev,status);
562
563 if (report & SAA7134_IRQ_REPORT_GPIO16) {
564 switch (dev->has_remote) {
565 case SAA7134_REMOTE_GPIO:
566 if (!dev->remote)
567 break;
568 if (dev->remote->mask_keydown & 0x10000) {
569 saa7134_input_irq(dev);
570 }
571 break;
572
573 case SAA7134_REMOTE_I2C:
574 break; /* FIXME: invoke I2C get_key() */
575
576 default: /* GPIO16 not used by IR remote */
577 break;
578 }
579 }
580
581 if (report & SAA7134_IRQ_REPORT_GPIO18) {
582 switch (dev->has_remote) {
583 case SAA7134_REMOTE_GPIO:
584 if (!dev->remote)
585 break;
586 if ((dev->remote->mask_keydown & 0x40000) ||
587 (dev->remote->mask_keyup & 0x40000)) {
588 saa7134_input_irq(dev);
589 }
590 break;
591
592 case SAA7134_REMOTE_I2C:
593 break; /* FIXME: invoke I2C get_key() */
594
595 default: /* GPIO18 not used by IR remote */
596 break;
597 }
598 }
599 }
600
601 if (10 == loop) {
602 print_irqstatus(dev,loop,report,status);
603 if (report & SAA7134_IRQ_REPORT_PE) {
604 /* disable all parity error */
605 printk(KERN_WARNING "%s/irq: looping -- "
606 "clearing PE (parity error!) enable bit\n",dev->name);
607 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
608 } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
609 /* disable gpio16 IRQ */
610 printk(KERN_WARNING "%s/irq: looping -- "
611 "clearing GPIO16 enable bit\n",dev->name);
612 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_P);
613 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_N);
614 } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
615 /* disable gpio18 IRQs */
616 printk(KERN_WARNING "%s/irq: looping -- "
617 "clearing GPIO18 enable bit\n",dev->name);
618 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
619 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_N);
620 } else {
621 /* disable all irqs */
622 printk(KERN_WARNING "%s/irq: looping -- "
623 "clearing all enable bits\n",dev->name);
624 saa_writel(SAA7134_IRQ1,0);
625 saa_writel(SAA7134_IRQ2,0);
626 }
627 }
628
629 out:
630 return IRQ_RETVAL(handled);
631}
632
633/* ------------------------------------------------------------------ */
634
635/* early init (no i2c, no irq) */
636
637static int saa7134_hw_enable1(struct saa7134_dev *dev)
638{
639 /* RAM FIFO config */
640 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
641 saa_writel(SAA7134_THRESHOULD, 0x02020202);
642
643 /* enable audio + video processing */
644 saa_writel(SAA7134_MAIN_CTRL,
645 SAA7134_MAIN_CTRL_VPLLE |
646 SAA7134_MAIN_CTRL_APLLE |
647 SAA7134_MAIN_CTRL_EXOSC |
648 SAA7134_MAIN_CTRL_EVFE1 |
649 SAA7134_MAIN_CTRL_EVFE2 |
650 SAA7134_MAIN_CTRL_ESFE |
651 SAA7134_MAIN_CTRL_EBDAC);
652
653 /*
654 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
655 * OSS initialization writes to registers via the audio DSP; these
656 * writes will fail unless the audio clock has been started. At worst,
657 * audio will not work.
658 */
659
660 /* enable peripheral devices */
661 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
662
663 /* set vertical line numbering start (vbi needs this) */
664 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
665
666 return 0;
667}
668
669static int saa7134_hwinit1(struct saa7134_dev *dev)
670{
671 dprintk("hwinit1\n");
672
673 saa_writel(SAA7134_IRQ1, 0);
674 saa_writel(SAA7134_IRQ2, 0);
675
676 /* Clear any stale IRQ reports */
677 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
678
679 mutex_init(&dev->lock);
680 spin_lock_init(&dev->slock);
681
682 saa7134_track_gpio(dev,"pre-init");
683 saa7134_video_init1(dev);
684 saa7134_vbi_init1(dev);
685 if (card_has_mpeg(dev))
686 saa7134_ts_init1(dev);
687 saa7134_input_init1(dev);
688
689 saa7134_hw_enable1(dev);
690
691 return 0;
692}
693
694/* late init (with i2c + irq) */
695static int saa7134_hw_enable2(struct saa7134_dev *dev)
696{
697
698 unsigned int irq2_mask;
699
700 /* enable IRQ's */
701 irq2_mask =
702 SAA7134_IRQ2_INTE_DEC3 |
703 SAA7134_IRQ2_INTE_DEC2 |
704 SAA7134_IRQ2_INTE_DEC1 |
705 SAA7134_IRQ2_INTE_DEC0 |
706 SAA7134_IRQ2_INTE_PE |
707 SAA7134_IRQ2_INTE_AR;
708
709 if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
710 if (dev->remote->mask_keydown & 0x10000)
711 irq2_mask |= SAA7134_IRQ2_INTE_GPIO16_N;
712 else { /* Allow enabling both IRQ edge triggers */
713 if (dev->remote->mask_keydown & 0x40000)
714 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_P;
715 if (dev->remote->mask_keyup & 0x40000)
716 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_N;
717 }
718 }
719
720 if (dev->has_remote == SAA7134_REMOTE_I2C) {
721 request_module("ir-kbd-i2c");
722 }
723
724 saa_writel(SAA7134_IRQ1, 0);
725 saa_writel(SAA7134_IRQ2, irq2_mask);
726
727 return 0;
728}
729
730static int saa7134_hwinit2(struct saa7134_dev *dev)
731{
732
733 dprintk("hwinit2\n");
734
735 saa7134_video_init2(dev);
736 saa7134_tvaudio_init2(dev);
737
738 saa7134_hw_enable2(dev);
739
740 return 0;
741}
742
743
744/* shutdown */
745static int saa7134_hwfini(struct saa7134_dev *dev)
746{
747 dprintk("hwfini\n");
748
749 if (card_has_mpeg(dev))
750 saa7134_ts_fini(dev);
751 saa7134_input_fini(dev);
752 saa7134_vbi_fini(dev);
753 saa7134_tvaudio_fini(dev);
754 return 0;
755}
756
757static void __devinit must_configure_manually(int has_eeprom)
758{
759 unsigned int i,p;
760
761 if (!has_eeprom)
762 printk(KERN_WARNING
763 "saa7134: <rant>\n"
764 "saa7134: Congratulations! Your TV card vendor saved a few\n"
765 "saa7134: cents for a eeprom, thus your pci board has no\n"
766 "saa7134: subsystem ID and I can't identify it automatically\n"
767 "saa7134: </rant>\n"
768 "saa7134: I feel better now. Ok, here are the good news:\n"
769 "saa7134: You can use the card=<nr> insmod option to specify\n"
770 "saa7134: which board do you have. The list:\n");
771 else
772 printk(KERN_WARNING
773 "saa7134: Board is currently unknown. You might try to use the card=<nr>\n"
774 "saa7134: insmod option to specify which board do you have, but this is\n"
775 "saa7134: somewhat risky, as might damage your card. It is better to ask\n"
776 "saa7134: for support at linux-media@vger.kernel.org.\n"
777 "saa7134: The supported cards are:\n");
778
779 for (i = 0; i < saa7134_bcount; i++) {
780 printk(KERN_WARNING "saa7134: card=%d -> %-40.40s",
781 i,saa7134_boards[i].name);
782 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
783 if (saa7134_pci_tbl[p].driver_data != i)
784 continue;
785 printk(" %04x:%04x",
786 saa7134_pci_tbl[p].subvendor,
787 saa7134_pci_tbl[p].subdevice);
788 }
789 printk("\n");
790 }
791}
792
793static struct video_device *vdev_init(struct saa7134_dev *dev,
794 struct video_device *template,
795 char *type)
796{
797 struct video_device *vfd;
798
799 vfd = video_device_alloc();
800 if (NULL == vfd)
801 return NULL;
802 *vfd = *template;
803 vfd->v4l2_dev = &dev->v4l2_dev;
804 vfd->release = video_device_release;
805 vfd->debug = video_debug;
806 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
807 dev->name, type, saa7134_boards[dev->board].name);
808 video_set_drvdata(vfd, dev);
809 return vfd;
810}
811
812static void saa7134_unregister_video(struct saa7134_dev *dev)
813{
814 if (dev->video_dev) {
815 if (video_is_registered(dev->video_dev))
816 video_unregister_device(dev->video_dev);
817 else
818 video_device_release(dev->video_dev);
819 dev->video_dev = NULL;
820 }
821 if (dev->vbi_dev) {
822 if (video_is_registered(dev->vbi_dev))
823 video_unregister_device(dev->vbi_dev);
824 else
825 video_device_release(dev->vbi_dev);
826 dev->vbi_dev = NULL;
827 }
828 if (dev->radio_dev) {
829 if (video_is_registered(dev->radio_dev))
830 video_unregister_device(dev->radio_dev);
831 else
832 video_device_release(dev->radio_dev);
833 dev->radio_dev = NULL;
834 }
835}
836
837static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
838 struct saa7134_dev *dev)
839{
840 int err;
841
842 if (NULL != dev->mops)
843 return;
844 if (saa7134_boards[dev->board].mpeg != ops->type)
845 return;
846 err = ops->init(dev);
847 if (0 != err)
848 return;
849 dev->mops = ops;
850}
851
852static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
853 struct saa7134_dev *dev)
854{
855 if (NULL == dev->mops)
856 return;
857 if (dev->mops != ops)
858 return;
859 dev->mops->fini(dev);
860 dev->mops = NULL;
861}
862
863static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
864 const struct pci_device_id *pci_id)
865{
866 struct saa7134_dev *dev;
867 struct saa7134_mpeg_ops *mops;
868 int err;
869
870 if (saa7134_devcount == SAA7134_MAXBOARDS)
871 return -ENOMEM;
872
873 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
874 if (NULL == dev)
875 return -ENOMEM;
876
877 err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
878 if (err)
879 goto fail0;
880
881 /* pci init */
882 dev->pci = pci_dev;
883 if (pci_enable_device(pci_dev)) {
884 err = -EIO;
885 goto fail1;
886 }
887
888 dev->nr = saa7134_devcount;
889 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
890
891 /* pci quirks */
892 if (pci_pci_problems) {
893 if (pci_pci_problems & PCIPCI_TRITON)
894 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
895 if (pci_pci_problems & PCIPCI_NATOMA)
896 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
897 if (pci_pci_problems & PCIPCI_VIAETBF)
898 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
899 if (pci_pci_problems & PCIPCI_VSFX)
900 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
901#ifdef PCIPCI_ALIMAGIK
902 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
903 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
904 dev->name);
905 latency = 0x0A;
906 }
907#endif
908 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
909 printk(KERN_INFO "%s: quirk: this driver and your "
910 "chipset may not work together"
911 " in overlay mode.\n",dev->name);
912 if (!saa7134_no_overlay) {
913 printk(KERN_INFO "%s: quirk: overlay "
914 "mode will be disabled.\n",
915 dev->name);
916 saa7134_no_overlay = 1;
917 } else {
918 printk(KERN_INFO "%s: quirk: overlay "
919 "mode will be forced. Use this"
920 " option at your own risk.\n",
921 dev->name);
922 }
923 }
924 }
925 if (UNSET != latency) {
926 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
927 dev->name,latency);
928 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
929 }
930
931 /* print pci info */
932 dev->pci_rev = pci_dev->revision;
933 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
934 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
935 "latency: %d, mmio: 0x%llx\n", dev->name,
936 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
937 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
938 pci_set_master(pci_dev);
939 if (!pci_dma_supported(pci_dev, DMA_BIT_MASK(32))) {
940 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
941 err = -EIO;
942 goto fail1;
943 }
944
945 /* board config */
946 dev->board = pci_id->driver_data;
947 if (card[dev->nr] >= 0 &&
948 card[dev->nr] < saa7134_bcount)
949 dev->board = card[dev->nr];
950 if (SAA7134_BOARD_UNKNOWN == dev->board)
951 must_configure_manually(0);
952 else if (SAA7134_BOARD_NOAUTO == dev->board) {
953 must_configure_manually(1);
954 dev->board = SAA7134_BOARD_UNKNOWN;
955 }
956 dev->autodetected = card[dev->nr] != dev->board;
957 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
958 dev->tuner_addr = saa7134_boards[dev->board].tuner_addr;
959 dev->radio_type = saa7134_boards[dev->board].radio_type;
960 dev->radio_addr = saa7134_boards[dev->board].radio_addr;
961 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
962 if (UNSET != tuner[dev->nr])
963 dev->tuner_type = tuner[dev->nr];
964 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
965 dev->name,pci_dev->subsystem_vendor,
966 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
967 dev->board, dev->autodetected ?
968 "autodetected" : "insmod option");
969
970 /* get mmio */
971 if (!request_mem_region(pci_resource_start(pci_dev,0),
972 pci_resource_len(pci_dev,0),
973 dev->name)) {
974 err = -EBUSY;
975 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx\n",
976 dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
977 goto fail1;
978 }
979 dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
980 pci_resource_len(pci_dev, 0));
981 dev->bmmio = (__u8 __iomem *)dev->lmmio;
982 if (NULL == dev->lmmio) {
983 err = -EIO;
984 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
985 dev->name);
986 goto fail2;
987 }
988
989 /* initialize hardware #1 */
990 saa7134_board_init1(dev);
991 saa7134_hwinit1(dev);
992
993 /* get irq */
994 err = request_irq(pci_dev->irq, saa7134_irq,
995 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
996 if (err < 0) {
997 printk(KERN_ERR "%s: can't get IRQ %d\n",
998 dev->name,pci_dev->irq);
999 goto fail3;
1000 }
1001
1002 /* wait a bit, register i2c bus */
1003 msleep(100);
1004 saa7134_i2c_register(dev);
1005 saa7134_board_init2(dev);
1006
1007 saa7134_hwinit2(dev);
1008
1009 /* load i2c helpers */
1010 if (card_is_empress(dev)) {
1011 struct v4l2_subdev *sd =
1012 v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
1013 "saa6752hs",
1014 saa7134_boards[dev->board].empress_addr, NULL);
1015
1016 if (sd)
1017 sd->grp_id = GRP_EMPRESS;
1018 }
1019
1020 if (saa7134_boards[dev->board].rds_addr) {
1021 struct v4l2_subdev *sd;
1022
1023 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1024 &dev->i2c_adap, "saa6588",
1025 0, I2C_ADDRS(saa7134_boards[dev->board].rds_addr));
1026 if (sd) {
1027 printk(KERN_INFO "%s: found RDS decoder\n", dev->name);
1028 dev->has_rds = 1;
1029 }
1030 }
1031
1032 v4l2_prio_init(&dev->prio);
1033
1034 mutex_lock(&saa7134_devlist_lock);
1035 list_for_each_entry(mops, &mops_list, next)
1036 mpeg_ops_attach(mops, dev);
1037 list_add_tail(&dev->devlist, &saa7134_devlist);
1038 mutex_unlock(&saa7134_devlist_lock);
1039
1040 /* check for signal */
1041 saa7134_irq_video_signalchange(dev);
1042
1043 if (TUNER_ABSENT != dev->tuner_type)
1044 saa_call_all(dev, core, s_power, 0);
1045
1046 /* register v4l devices */
1047 if (saa7134_no_overlay > 0)
1048 printk(KERN_INFO "%s: Overlay support disabled.\n", dev->name);
1049
1050 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1051 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1052 video_nr[dev->nr]);
1053 if (err < 0) {
1054 printk(KERN_INFO "%s: can't register video device\n",
1055 dev->name);
1056 goto fail4;
1057 }
1058 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1059 dev->name, video_device_node_name(dev->video_dev));
1060
1061 dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1062
1063 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1064 vbi_nr[dev->nr]);
1065 if (err < 0)
1066 goto fail4;
1067 printk(KERN_INFO "%s: registered device %s\n",
1068 dev->name, video_device_node_name(dev->vbi_dev));
1069
1070 if (card_has_radio(dev)) {
1071 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1072 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1073 radio_nr[dev->nr]);
1074 if (err < 0)
1075 goto fail4;
1076 printk(KERN_INFO "%s: registered device %s\n",
1077 dev->name, video_device_node_name(dev->radio_dev));
1078 }
1079
1080 /* everything worked */
1081 saa7134_devcount++;
1082
1083 if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1084 saa7134_dmasound_init(dev);
1085
1086 request_submodules(dev);
1087 return 0;
1088
1089 fail4:
1090 saa7134_unregister_video(dev);
1091 saa7134_i2c_unregister(dev);
1092 free_irq(pci_dev->irq, dev);
1093 fail3:
1094 saa7134_hwfini(dev);
1095 iounmap(dev->lmmio);
1096 fail2:
1097 release_mem_region(pci_resource_start(pci_dev,0),
1098 pci_resource_len(pci_dev,0));
1099 fail1:
1100 v4l2_device_unregister(&dev->v4l2_dev);
1101 fail0:
1102 kfree(dev);
1103 return err;
1104}
1105
1106static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1107{
1108 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1109 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1110 struct saa7134_mpeg_ops *mops;
1111
1112 flush_request_submodules(dev);
1113
1114 /* Release DMA sound modules if present */
1115 if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1116 saa7134_dmasound_exit(dev);
1117 }
1118
1119 /* debugging ... */
1120 if (irq_debug) {
1121 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1122 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1123 print_irqstatus(dev,42,report,status);
1124 }
1125
1126 /* disable peripheral devices */
1127 saa_writeb(SAA7134_SPECIAL_MODE,0);
1128
1129 /* shutdown hardware */
1130 saa_writel(SAA7134_IRQ1,0);
1131 saa_writel(SAA7134_IRQ2,0);
1132 saa_writel(SAA7134_MAIN_CTRL,0);
1133
1134 /* shutdown subsystems */
1135 saa7134_hwfini(dev);
1136
1137 /* unregister */
1138 mutex_lock(&saa7134_devlist_lock);
1139 list_del(&dev->devlist);
1140 list_for_each_entry(mops, &mops_list, next)
1141 mpeg_ops_detach(mops, dev);
1142 mutex_unlock(&saa7134_devlist_lock);
1143 saa7134_devcount--;
1144
1145 saa7134_i2c_unregister(dev);
1146 saa7134_unregister_video(dev);
1147
1148
1149 /* the DMA sound modules should be unloaded before reaching
1150 this, but just in case they are still present... */
1151 if (dev->dmasound.priv_data != NULL) {
1152 free_irq(pci_dev->irq, &dev->dmasound);
1153 dev->dmasound.priv_data = NULL;
1154 }
1155
1156
1157 /* release resources */
1158 free_irq(pci_dev->irq, dev);
1159 iounmap(dev->lmmio);
1160 release_mem_region(pci_resource_start(pci_dev,0),
1161 pci_resource_len(pci_dev,0));
1162
1163
1164 v4l2_device_unregister(&dev->v4l2_dev);
1165
1166 /* free memory */
1167 kfree(dev);
1168}
1169
1170#ifdef CONFIG_PM
1171
1172/* resends a current buffer in queue after resume */
1173static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1174 struct saa7134_dmaqueue *q)
1175{
1176 struct saa7134_buf *buf, *next;
1177
1178 assert_spin_locked(&dev->slock);
1179
1180 buf = q->curr;
1181 next = buf;
1182 dprintk("buffer_requeue\n");
1183
1184 if (!buf)
1185 return 0;
1186
1187 dprintk("buffer_requeue : resending active buffers \n");
1188
1189 if (!list_empty(&q->queue))
1190 next = list_entry(q->queue.next, struct saa7134_buf,
1191 vb.queue);
1192 buf->activate(dev, buf, next);
1193
1194 return 0;
1195}
1196
1197static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1198{
1199 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1200 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1201
1202 /* disable overlay - apps should enable it explicitly on resume*/
1203 dev->ovenable = 0;
1204
1205 /* Disable interrupts, DMA, and rest of the chip*/
1206 saa_writel(SAA7134_IRQ1, 0);
1207 saa_writel(SAA7134_IRQ2, 0);
1208 saa_writel(SAA7134_MAIN_CTRL, 0);
1209
1210 dev->insuspend = 1;
1211 synchronize_irq(pci_dev->irq);
1212
1213 /* ACK interrupts once more, just in case,
1214 since the IRQ handler won't ack them anymore*/
1215
1216 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1217
1218 /* Disable timeout timers - if we have active buffers, we will
1219 fill them on resume*/
1220
1221 del_timer(&dev->video_q.timeout);
1222 del_timer(&dev->vbi_q.timeout);
1223 del_timer(&dev->ts_q.timeout);
1224
1225 if (dev->remote)
1226 saa7134_ir_stop(dev);
1227
1228 pci_save_state(pci_dev);
1229 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1230
1231 return 0;
1232}
1233
1234static int saa7134_resume(struct pci_dev *pci_dev)
1235{
1236 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1237 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1238 unsigned long flags;
1239
1240 pci_set_power_state(pci_dev, PCI_D0);
1241 pci_restore_state(pci_dev);
1242
1243 /* Do things that are done in saa7134_initdev ,
1244 except of initializing memory structures.*/
1245
1246 saa7134_board_init1(dev);
1247
1248 /* saa7134_hwinit1 */
1249 if (saa7134_boards[dev->board].video_out)
1250 saa7134_videoport_init(dev);
1251 if (card_has_mpeg(dev))
1252 saa7134_ts_init_hw(dev);
1253 if (dev->remote)
1254 saa7134_ir_start(dev);
1255 saa7134_hw_enable1(dev);
1256
1257 msleep(100);
1258
1259 saa7134_board_init2(dev);
1260
1261 /*saa7134_hwinit2*/
1262 saa7134_set_tvnorm_hw(dev);
1263 saa7134_tvaudio_setmute(dev);
1264 saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1265 saa7134_tvaudio_init(dev);
1266 saa7134_enable_i2s(dev);
1267 saa7134_hw_enable2(dev);
1268
1269 saa7134_irq_video_signalchange(dev);
1270
1271 /*resume unfinished buffer(s)*/
1272 spin_lock_irqsave(&dev->slock, flags);
1273 saa7134_buffer_requeue(dev, &dev->video_q);
1274 saa7134_buffer_requeue(dev, &dev->vbi_q);
1275 saa7134_buffer_requeue(dev, &dev->ts_q);
1276
1277 /* FIXME: Disable DMA audio sound - temporary till proper support
1278 is implemented*/
1279
1280 dev->dmasound.dma_running = 0;
1281
1282 /* start DMA now*/
1283 dev->insuspend = 0;
1284 smp_wmb();
1285 saa7134_set_dmabits(dev);
1286 spin_unlock_irqrestore(&dev->slock, flags);
1287
1288 return 0;
1289}
1290#endif
1291
1292/* ----------------------------------------------------------- */
1293
1294int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1295{
1296 struct saa7134_dev *dev;
1297
1298 mutex_lock(&saa7134_devlist_lock);
1299 list_for_each_entry(dev, &saa7134_devlist, devlist)
1300 mpeg_ops_attach(ops, dev);
1301 list_add_tail(&ops->next,&mops_list);
1302 mutex_unlock(&saa7134_devlist_lock);
1303 return 0;
1304}
1305
1306void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1307{
1308 struct saa7134_dev *dev;
1309
1310 mutex_lock(&saa7134_devlist_lock);
1311 list_del(&ops->next);
1312 list_for_each_entry(dev, &saa7134_devlist, devlist)
1313 mpeg_ops_detach(ops, dev);
1314 mutex_unlock(&saa7134_devlist_lock);
1315}
1316
1317EXPORT_SYMBOL(saa7134_ts_register);
1318EXPORT_SYMBOL(saa7134_ts_unregister);
1319
1320/* ----------------------------------------------------------- */
1321
1322static struct pci_driver saa7134_pci_driver = {
1323 .name = "saa7134",
1324 .id_table = saa7134_pci_tbl,
1325 .probe = saa7134_initdev,
1326 .remove = __devexit_p(saa7134_finidev),
1327#ifdef CONFIG_PM
1328 .suspend = saa7134_suspend,
1329 .resume = saa7134_resume
1330#endif
1331};
1332
1333static int __init saa7134_init(void)
1334{
1335 INIT_LIST_HEAD(&saa7134_devlist);
1336 printk(KERN_INFO "saa7130/34: v4l2 driver version %s loaded\n",
1337 SAA7134_VERSION);
1338 return pci_register_driver(&saa7134_pci_driver);
1339}
1340
1341static void __exit saa7134_fini(void)
1342{
1343 pci_unregister_driver(&saa7134_pci_driver);
1344}
1345
1346module_init(saa7134_init);
1347module_exit(saa7134_fini);
1348
1349/* ----------------------------------------------------------- */
1350
1351EXPORT_SYMBOL(saa7134_set_gpio);
1352EXPORT_SYMBOL(saa7134_boards);
1353
1354/* ----------------- for the DMA sound modules --------------- */
1355
1356EXPORT_SYMBOL(saa7134_dmasound_init);
1357EXPORT_SYMBOL(saa7134_dmasound_exit);
1358EXPORT_SYMBOL(saa7134_pgtable_free);
1359EXPORT_SYMBOL(saa7134_pgtable_build);
1360EXPORT_SYMBOL(saa7134_pgtable_alloc);
1361EXPORT_SYMBOL(saa7134_set_dmabits);
1362
1363/* ----------------------------------------------------------- */
1364/*
1365 * Local variables:
1366 * c-basic-offset: 8
1367 * End:
1368 */
diff --git a/drivers/media/pci/saa7134/saa7134-dvb.c b/drivers/media/pci/saa7134/saa7134-dvb.c
new file mode 100644
index 000000000000..b209de40a4f8
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-dvb.c
@@ -0,0 +1,1936 @@
1/*
2 *
3 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
4 *
5 * Extended 3 / 2005 by Hartmut Hackmann to support various
6 * cards with the tda10046 DVB-T channel decoder
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/delay.h>
28#include <linux/kthread.h>
29#include <linux/suspend.h>
30
31#include "saa7134-reg.h"
32#include "saa7134.h"
33#include <media/v4l2-common.h>
34#include "dvb-pll.h"
35#include <dvb_frontend.h>
36
37#include "mt352.h"
38#include "mt352_priv.h" /* FIXME */
39#include "tda1004x.h"
40#include "nxt200x.h"
41#include "tuner-xc2028.h"
42#include "xc5000.h"
43
44#include "tda10086.h"
45#include "tda826x.h"
46#include "tda827x.h"
47#include "isl6421.h"
48#include "isl6405.h"
49#include "lnbp21.h"
50#include "tuner-simple.h"
51#include "tda10048.h"
52#include "tda18271.h"
53#include "lgdt3305.h"
54#include "tda8290.h"
55#include "mb86a20s.h"
56#include "lgs8gxx.h"
57
58#include "zl10353.h"
59#include "qt1010.h"
60
61#include "zl10036.h"
62#include "zl10039.h"
63#include "mt312.h"
64#include "s5h1411.h"
65
66MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
67MODULE_LICENSE("GPL");
68
69static unsigned int antenna_pwr;
70
71module_param(antenna_pwr, int, 0444);
72MODULE_PARM_DESC(antenna_pwr,"enable antenna power (Pinnacle 300i)");
73
74static int use_frontend;
75module_param(use_frontend, int, 0644);
76MODULE_PARM_DESC(use_frontend,"for cards with multiple frontends (0: terrestrial, 1: satellite)");
77
78static int debug;
79module_param(debug, int, 0644);
80MODULE_PARM_DESC(debug, "Turn on/off module debugging (default:off).");
81
82DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
83
84#define dprintk(fmt, arg...) do { if (debug) \
85 printk(KERN_DEBUG "%s/dvb: " fmt, dev->name , ## arg); } while(0)
86
87/* Print a warning */
88#define wprintk(fmt, arg...) \
89 printk(KERN_WARNING "%s/dvb: " fmt, dev->name, ## arg)
90
91/* ------------------------------------------------------------------
92 * mt352 based DVB-T cards
93 */
94
95static int pinnacle_antenna_pwr(struct saa7134_dev *dev, int on)
96{
97 u32 ok;
98
99 if (!on) {
100 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 26));
101 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
102 return 0;
103 }
104
105 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 26));
106 saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
107 udelay(10);
108
109 saa_setl(SAA7134_GPIO_GPMODE0 >> 2, (1 << 28));
110 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
111 udelay(10);
112 saa_setl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 28));
113 udelay(10);
114 ok = saa_readl(SAA7134_GPIO_GPSTATUS0) & (1 << 27);
115 dprintk("%s %s\n", __func__, ok ? "on" : "off");
116
117 if (!ok)
118 saa_clearl(SAA7134_GPIO_GPSTATUS0 >> 2, (1 << 26));
119 return ok;
120}
121
122static int mt352_pinnacle_init(struct dvb_frontend* fe)
123{
124 static u8 clock_config [] = { CLOCK_CTL, 0x3d, 0x28 };
125 static u8 reset [] = { RESET, 0x80 };
126 static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
127 static u8 agc_cfg [] = { AGC_TARGET, 0x28, 0xa0 };
128 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x31 };
129 static u8 fsm_ctl_cfg[] = { 0x7b, 0x04 };
130 static u8 gpp_ctl_cfg [] = { GPP_CTL, 0x0f };
131 static u8 scan_ctl_cfg [] = { SCAN_CTL, 0x0d };
132 static u8 irq_cfg [] = { INTERRUPT_EN_0, 0x00, 0x00, 0x00, 0x00 };
133 struct saa7134_dev *dev= fe->dvb->priv;
134
135 dprintk("%s called\n", __func__);
136
137 mt352_write(fe, clock_config, sizeof(clock_config));
138 udelay(200);
139 mt352_write(fe, reset, sizeof(reset));
140 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
141 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
142 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
143 mt352_write(fe, gpp_ctl_cfg, sizeof(gpp_ctl_cfg));
144
145 mt352_write(fe, fsm_ctl_cfg, sizeof(fsm_ctl_cfg));
146 mt352_write(fe, scan_ctl_cfg, sizeof(scan_ctl_cfg));
147 mt352_write(fe, irq_cfg, sizeof(irq_cfg));
148
149 return 0;
150}
151
152static int mt352_aver777_init(struct dvb_frontend* fe)
153{
154 static u8 clock_config [] = { CLOCK_CTL, 0x38, 0x2d };
155 static u8 reset [] = { RESET, 0x80 };
156 static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
157 static u8 agc_cfg [] = { AGC_TARGET, 0x28, 0xa0 };
158 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
159
160 mt352_write(fe, clock_config, sizeof(clock_config));
161 udelay(200);
162 mt352_write(fe, reset, sizeof(reset));
163 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
164 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
165 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
166
167 return 0;
168}
169
170static int mt352_avermedia_xc3028_init(struct dvb_frontend *fe)
171{
172 static u8 clock_config [] = { CLOCK_CTL, 0x38, 0x2d };
173 static u8 reset [] = { RESET, 0x80 };
174 static u8 adc_ctl_1_cfg [] = { ADC_CTL_1, 0x40 };
175 static u8 agc_cfg [] = { AGC_TARGET, 0xe };
176 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x33 };
177
178 mt352_write(fe, clock_config, sizeof(clock_config));
179 udelay(200);
180 mt352_write(fe, reset, sizeof(reset));
181 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
182 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
183 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
184 return 0;
185}
186
187static int mt352_pinnacle_tuner_set_params(struct dvb_frontend *fe)
188{
189 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
190 u8 off[] = { 0x00, 0xf1};
191 u8 on[] = { 0x00, 0x71};
192 struct i2c_msg msg = {.addr=0x43, .flags=0, .buf=off, .len = sizeof(off)};
193
194 struct saa7134_dev *dev = fe->dvb->priv;
195 struct v4l2_frequency f;
196
197 /* set frequency (mt2050) */
198 f.tuner = 0;
199 f.type = V4L2_TUNER_DIGITAL_TV;
200 f.frequency = c->frequency / 1000 * 16 / 1000;
201 if (fe->ops.i2c_gate_ctrl)
202 fe->ops.i2c_gate_ctrl(fe, 1);
203 i2c_transfer(&dev->i2c_adap, &msg, 1);
204 saa_call_all(dev, tuner, s_frequency, &f);
205 msg.buf = on;
206 if (fe->ops.i2c_gate_ctrl)
207 fe->ops.i2c_gate_ctrl(fe, 1);
208 i2c_transfer(&dev->i2c_adap, &msg, 1);
209
210 pinnacle_antenna_pwr(dev, antenna_pwr);
211
212 /* mt352 setup */
213 return mt352_pinnacle_init(fe);
214}
215
216static struct mt352_config pinnacle_300i = {
217 .demod_address = 0x3c >> 1,
218 .adc_clock = 20333,
219 .if2 = 36150,
220 .no_tuner = 1,
221 .demod_init = mt352_pinnacle_init,
222};
223
224static struct mt352_config avermedia_777 = {
225 .demod_address = 0xf,
226 .demod_init = mt352_aver777_init,
227};
228
229static struct mt352_config avermedia_xc3028_mt352_dev = {
230 .demod_address = (0x1e >> 1),
231 .no_tuner = 1,
232 .demod_init = mt352_avermedia_xc3028_init,
233};
234
235static struct tda18271_std_map mb86a20s_tda18271_std_map = {
236 .dvbt_6 = { .if_freq = 3300, .agc_mode = 3, .std = 4,
237 .if_lvl = 7, .rfagc_top = 0x37, },
238};
239
240static struct tda18271_config kworld_tda18271_config = {
241 .std_map = &mb86a20s_tda18271_std_map,
242 .gate = TDA18271_GATE_DIGITAL,
243 .config = 3, /* Use tuner callback for AGC */
244
245};
246
247static const struct mb86a20s_config kworld_mb86a20s_config = {
248 .demod_address = 0x10,
249};
250
251static int kworld_sbtvd_gate_ctrl(struct dvb_frontend* fe, int enable)
252{
253 struct saa7134_dev *dev = fe->dvb->priv;
254
255 unsigned char initmsg[] = {0x45, 0x97};
256 unsigned char msg_enable[] = {0x45, 0xc1};
257 unsigned char msg_disable[] = {0x45, 0x81};
258 struct i2c_msg msg = {.addr = 0x4b, .flags = 0, .buf = initmsg, .len = 2};
259
260 if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1) {
261 wprintk("could not access the I2C gate\n");
262 return -EIO;
263 }
264 if (enable)
265 msg.buf = msg_enable;
266 else
267 msg.buf = msg_disable;
268 if (i2c_transfer(&dev->i2c_adap, &msg, 1) != 1) {
269 wprintk("could not access the I2C gate\n");
270 return -EIO;
271 }
272 msleep(20);
273 return 0;
274}
275
276/* ==================================================================
277 * tda1004x based DVB-T cards, helper functions
278 */
279
280static int philips_tda1004x_request_firmware(struct dvb_frontend *fe,
281 const struct firmware **fw, char *name)
282{
283 struct saa7134_dev *dev = fe->dvb->priv;
284 return request_firmware(fw, name, &dev->pci->dev);
285}
286
287/* ------------------------------------------------------------------
288 * these tuners are tu1216, td1316(a)
289 */
290
291static int philips_tda6651_pll_set(struct dvb_frontend *fe)
292{
293 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
294 struct saa7134_dev *dev = fe->dvb->priv;
295 struct tda1004x_state *state = fe->demodulator_priv;
296 u8 addr = state->config->tuner_address;
297 u8 tuner_buf[4];
298 struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tuner_buf,.len =
299 sizeof(tuner_buf) };
300 int tuner_frequency = 0;
301 u8 band, cp, filter;
302
303 /* determine charge pump */
304 tuner_frequency = c->frequency + 36166000;
305 if (tuner_frequency < 87000000)
306 return -EINVAL;
307 else if (tuner_frequency < 130000000)
308 cp = 3;
309 else if (tuner_frequency < 160000000)
310 cp = 5;
311 else if (tuner_frequency < 200000000)
312 cp = 6;
313 else if (tuner_frequency < 290000000)
314 cp = 3;
315 else if (tuner_frequency < 420000000)
316 cp = 5;
317 else if (tuner_frequency < 480000000)
318 cp = 6;
319 else if (tuner_frequency < 620000000)
320 cp = 3;
321 else if (tuner_frequency < 830000000)
322 cp = 5;
323 else if (tuner_frequency < 895000000)
324 cp = 7;
325 else
326 return -EINVAL;
327
328 /* determine band */
329 if (c->frequency < 49000000)
330 return -EINVAL;
331 else if (c->frequency < 161000000)
332 band = 1;
333 else if (c->frequency < 444000000)
334 band = 2;
335 else if (c->frequency < 861000000)
336 band = 4;
337 else
338 return -EINVAL;
339
340 /* setup PLL filter */
341 switch (c->bandwidth_hz) {
342 case 6000000:
343 filter = 0;
344 break;
345
346 case 7000000:
347 filter = 0;
348 break;
349
350 case 8000000:
351 filter = 1;
352 break;
353
354 default:
355 return -EINVAL;
356 }
357
358 /* calculate divisor
359 * ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
360 */
361 tuner_frequency = (((c->frequency / 1000) * 6) + 217496) / 1000;
362
363 /* setup tuner buffer */
364 tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
365 tuner_buf[1] = tuner_frequency & 0xff;
366 tuner_buf[2] = 0xca;
367 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
368
369 if (fe->ops.i2c_gate_ctrl)
370 fe->ops.i2c_gate_ctrl(fe, 1);
371 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1) {
372 wprintk("could not write to tuner at addr: 0x%02x\n",
373 addr << 1);
374 return -EIO;
375 }
376 msleep(1);
377 return 0;
378}
379
380static int philips_tu1216_init(struct dvb_frontend *fe)
381{
382 struct saa7134_dev *dev = fe->dvb->priv;
383 struct tda1004x_state *state = fe->demodulator_priv;
384 u8 addr = state->config->tuner_address;
385 static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
386 struct i2c_msg tuner_msg = {.addr = addr,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
387
388 /* setup PLL configuration */
389 if (fe->ops.i2c_gate_ctrl)
390 fe->ops.i2c_gate_ctrl(fe, 1);
391 if (i2c_transfer(&dev->i2c_adap, &tuner_msg, 1) != 1)
392 return -EIO;
393 msleep(1);
394
395 return 0;
396}
397
398/* ------------------------------------------------------------------ */
399
400static struct tda1004x_config philips_tu1216_60_config = {
401 .demod_address = 0x8,
402 .invert = 1,
403 .invert_oclk = 0,
404 .xtal_freq = TDA10046_XTAL_4M,
405 .agc_config = TDA10046_AGC_DEFAULT,
406 .if_freq = TDA10046_FREQ_3617,
407 .tuner_address = 0x60,
408 .request_firmware = philips_tda1004x_request_firmware
409};
410
411static struct tda1004x_config philips_tu1216_61_config = {
412
413 .demod_address = 0x8,
414 .invert = 1,
415 .invert_oclk = 0,
416 .xtal_freq = TDA10046_XTAL_4M,
417 .agc_config = TDA10046_AGC_DEFAULT,
418 .if_freq = TDA10046_FREQ_3617,
419 .tuner_address = 0x61,
420 .request_firmware = philips_tda1004x_request_firmware
421};
422
423/* ------------------------------------------------------------------ */
424
425static int philips_td1316_tuner_init(struct dvb_frontend *fe)
426{
427 struct saa7134_dev *dev = fe->dvb->priv;
428 struct tda1004x_state *state = fe->demodulator_priv;
429 u8 addr = state->config->tuner_address;
430 static u8 msg[] = { 0x0b, 0xf5, 0x86, 0xab };
431 struct i2c_msg init_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
432
433 /* setup PLL configuration */
434 if (fe->ops.i2c_gate_ctrl)
435 fe->ops.i2c_gate_ctrl(fe, 1);
436 if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
437 return -EIO;
438 return 0;
439}
440
441static int philips_td1316_tuner_set_params(struct dvb_frontend *fe)
442{
443 return philips_tda6651_pll_set(fe);
444}
445
446static int philips_td1316_tuner_sleep(struct dvb_frontend *fe)
447{
448 struct saa7134_dev *dev = fe->dvb->priv;
449 struct tda1004x_state *state = fe->demodulator_priv;
450 u8 addr = state->config->tuner_address;
451 static u8 msg[] = { 0x0b, 0xdc, 0x86, 0xa4 };
452 struct i2c_msg analog_msg = {.addr = addr,.flags = 0,.buf = msg,.len = sizeof(msg) };
453
454 /* switch the tuner to analog mode */
455 if (fe->ops.i2c_gate_ctrl)
456 fe->ops.i2c_gate_ctrl(fe, 1);
457 if (i2c_transfer(&dev->i2c_adap, &analog_msg, 1) != 1)
458 return -EIO;
459 return 0;
460}
461
462/* ------------------------------------------------------------------ */
463
464static int philips_europa_tuner_init(struct dvb_frontend *fe)
465{
466 struct saa7134_dev *dev = fe->dvb->priv;
467 static u8 msg[] = { 0x00, 0x40};
468 struct i2c_msg init_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
469
470
471 if (philips_td1316_tuner_init(fe))
472 return -EIO;
473 msleep(1);
474 if (i2c_transfer(&dev->i2c_adap, &init_msg, 1) != 1)
475 return -EIO;
476
477 return 0;
478}
479
480static int philips_europa_tuner_sleep(struct dvb_frontend *fe)
481{
482 struct saa7134_dev *dev = fe->dvb->priv;
483
484 static u8 msg[] = { 0x00, 0x14 };
485 struct i2c_msg analog_msg = {.addr = 0x43,.flags = 0,.buf = msg,.len = sizeof(msg) };
486
487 if (philips_td1316_tuner_sleep(fe))
488 return -EIO;
489
490 /* switch the board to analog mode */
491 if (fe->ops.i2c_gate_ctrl)
492 fe->ops.i2c_gate_ctrl(fe, 1);
493 i2c_transfer(&dev->i2c_adap, &analog_msg, 1);
494 return 0;
495}
496
497static int philips_europa_demod_sleep(struct dvb_frontend *fe)
498{
499 struct saa7134_dev *dev = fe->dvb->priv;
500
501 if (dev->original_demod_sleep)
502 dev->original_demod_sleep(fe);
503 fe->ops.i2c_gate_ctrl(fe, 1);
504 return 0;
505}
506
507static struct tda1004x_config philips_europa_config = {
508
509 .demod_address = 0x8,
510 .invert = 0,
511 .invert_oclk = 0,
512 .xtal_freq = TDA10046_XTAL_4M,
513 .agc_config = TDA10046_AGC_IFO_AUTO_POS,
514 .if_freq = TDA10046_FREQ_052,
515 .tuner_address = 0x61,
516 .request_firmware = philips_tda1004x_request_firmware
517};
518
519static struct tda1004x_config medion_cardbus = {
520 .demod_address = 0x08,
521 .invert = 1,
522 .invert_oclk = 0,
523 .xtal_freq = TDA10046_XTAL_16M,
524 .agc_config = TDA10046_AGC_IFO_AUTO_NEG,
525 .if_freq = TDA10046_FREQ_3613,
526 .tuner_address = 0x61,
527 .request_firmware = philips_tda1004x_request_firmware
528};
529
530static struct tda1004x_config technotrend_budget_t3000_config = {
531 .demod_address = 0x8,
532 .invert = 1,
533 .invert_oclk = 0,
534 .xtal_freq = TDA10046_XTAL_4M,
535 .agc_config = TDA10046_AGC_DEFAULT,
536 .if_freq = TDA10046_FREQ_3617,
537 .tuner_address = 0x63,
538 .request_firmware = philips_tda1004x_request_firmware
539};
540
541/* ------------------------------------------------------------------
542 * tda 1004x based cards with philips silicon tuner
543 */
544
545static int tda8290_i2c_gate_ctrl( struct dvb_frontend* fe, int enable)
546{
547 struct tda1004x_state *state = fe->demodulator_priv;
548
549 u8 addr = state->config->i2c_gate;
550 static u8 tda8290_close[] = { 0x21, 0xc0};
551 static u8 tda8290_open[] = { 0x21, 0x80};
552 struct i2c_msg tda8290_msg = {.addr = addr,.flags = 0, .len = 2};
553 if (enable) {
554 tda8290_msg.buf = tda8290_close;
555 } else {
556 tda8290_msg.buf = tda8290_open;
557 }
558 if (i2c_transfer(state->i2c, &tda8290_msg, 1) != 1) {
559 struct saa7134_dev *dev = fe->dvb->priv;
560 wprintk("could not access tda8290 I2C gate\n");
561 return -EIO;
562 }
563 msleep(20);
564 return 0;
565}
566
567static int philips_tda827x_tuner_init(struct dvb_frontend *fe)
568{
569 struct saa7134_dev *dev = fe->dvb->priv;
570 struct tda1004x_state *state = fe->demodulator_priv;
571
572 switch (state->config->antenna_switch) {
573 case 0: break;
574 case 1: dprintk("setting GPIO21 to 0 (TV antenna?)\n");
575 saa7134_set_gpio(dev, 21, 0);
576 break;
577 case 2: dprintk("setting GPIO21 to 1 (Radio antenna?)\n");
578 saa7134_set_gpio(dev, 21, 1);
579 break;
580 }
581 return 0;
582}
583
584static int philips_tda827x_tuner_sleep(struct dvb_frontend *fe)
585{
586 struct saa7134_dev *dev = fe->dvb->priv;
587 struct tda1004x_state *state = fe->demodulator_priv;
588
589 switch (state->config->antenna_switch) {
590 case 0: break;
591 case 1: dprintk("setting GPIO21 to 1 (Radio antenna?)\n");
592 saa7134_set_gpio(dev, 21, 1);
593 break;
594 case 2: dprintk("setting GPIO21 to 0 (TV antenna?)\n");
595 saa7134_set_gpio(dev, 21, 0);
596 break;
597 }
598 return 0;
599}
600
601static int configure_tda827x_fe(struct saa7134_dev *dev,
602 struct tda1004x_config *cdec_conf,
603 struct tda827x_config *tuner_conf)
604{
605 struct videobuf_dvb_frontend *fe0;
606
607 /* Get the first frontend */
608 fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
609
610 fe0->dvb.frontend = dvb_attach(tda10046_attach, cdec_conf, &dev->i2c_adap);
611 if (fe0->dvb.frontend) {
612 if (cdec_conf->i2c_gate)
613 fe0->dvb.frontend->ops.i2c_gate_ctrl = tda8290_i2c_gate_ctrl;
614 if (dvb_attach(tda827x_attach, fe0->dvb.frontend,
615 cdec_conf->tuner_address,
616 &dev->i2c_adap, tuner_conf))
617 return 0;
618
619 wprintk("no tda827x tuner found at addr: %02x\n",
620 cdec_conf->tuner_address);
621 }
622 return -EINVAL;
623}
624
625/* ------------------------------------------------------------------ */
626
627static struct tda827x_config tda827x_cfg_0 = {
628 .init = philips_tda827x_tuner_init,
629 .sleep = philips_tda827x_tuner_sleep,
630 .config = 0,
631 .switch_addr = 0
632};
633
634static struct tda827x_config tda827x_cfg_1 = {
635 .init = philips_tda827x_tuner_init,
636 .sleep = philips_tda827x_tuner_sleep,
637 .config = 1,
638 .switch_addr = 0x4b
639};
640
641static struct tda827x_config tda827x_cfg_2 = {
642 .init = philips_tda827x_tuner_init,
643 .sleep = philips_tda827x_tuner_sleep,
644 .config = 2,
645 .switch_addr = 0x4b
646};
647
648static struct tda827x_config tda827x_cfg_2_sw42 = {
649 .init = philips_tda827x_tuner_init,
650 .sleep = philips_tda827x_tuner_sleep,
651 .config = 2,
652 .switch_addr = 0x42
653};
654
655/* ------------------------------------------------------------------ */
656
657static struct tda1004x_config tda827x_lifeview_config = {
658 .demod_address = 0x08,
659 .invert = 1,
660 .invert_oclk = 0,
661 .xtal_freq = TDA10046_XTAL_16M,
662 .agc_config = TDA10046_AGC_TDA827X,
663 .gpio_config = TDA10046_GP11_I,
664 .if_freq = TDA10046_FREQ_045,
665 .tuner_address = 0x60,
666 .request_firmware = philips_tda1004x_request_firmware
667};
668
669static struct tda1004x_config philips_tiger_config = {
670 .demod_address = 0x08,
671 .invert = 1,
672 .invert_oclk = 0,
673 .xtal_freq = TDA10046_XTAL_16M,
674 .agc_config = TDA10046_AGC_TDA827X,
675 .gpio_config = TDA10046_GP11_I,
676 .if_freq = TDA10046_FREQ_045,
677 .i2c_gate = 0x4b,
678 .tuner_address = 0x61,
679 .antenna_switch= 1,
680 .request_firmware = philips_tda1004x_request_firmware
681};
682
683static struct tda1004x_config cinergy_ht_config = {
684 .demod_address = 0x08,
685 .invert = 1,
686 .invert_oclk = 0,
687 .xtal_freq = TDA10046_XTAL_16M,
688 .agc_config = TDA10046_AGC_TDA827X,
689 .gpio_config = TDA10046_GP01_I,
690 .if_freq = TDA10046_FREQ_045,
691 .i2c_gate = 0x4b,
692 .tuner_address = 0x61,
693 .request_firmware = philips_tda1004x_request_firmware
694};
695
696static struct tda1004x_config cinergy_ht_pci_config = {
697 .demod_address = 0x08,
698 .invert = 1,
699 .invert_oclk = 0,
700 .xtal_freq = TDA10046_XTAL_16M,
701 .agc_config = TDA10046_AGC_TDA827X,
702 .gpio_config = TDA10046_GP01_I,
703 .if_freq = TDA10046_FREQ_045,
704 .i2c_gate = 0x4b,
705 .tuner_address = 0x60,
706 .request_firmware = philips_tda1004x_request_firmware
707};
708
709static struct tda1004x_config philips_tiger_s_config = {
710 .demod_address = 0x08,
711 .invert = 1,
712 .invert_oclk = 0,
713 .xtal_freq = TDA10046_XTAL_16M,
714 .agc_config = TDA10046_AGC_TDA827X,
715 .gpio_config = TDA10046_GP01_I,
716 .if_freq = TDA10046_FREQ_045,
717 .i2c_gate = 0x4b,
718 .tuner_address = 0x61,
719 .antenna_switch= 1,
720 .request_firmware = philips_tda1004x_request_firmware
721};
722
723static struct tda1004x_config pinnacle_pctv_310i_config = {
724 .demod_address = 0x08,
725 .invert = 1,
726 .invert_oclk = 0,
727 .xtal_freq = TDA10046_XTAL_16M,
728 .agc_config = TDA10046_AGC_TDA827X,
729 .gpio_config = TDA10046_GP11_I,
730 .if_freq = TDA10046_FREQ_045,
731 .i2c_gate = 0x4b,
732 .tuner_address = 0x61,
733 .request_firmware = philips_tda1004x_request_firmware
734};
735
736static struct tda1004x_config hauppauge_hvr_1110_config = {
737 .demod_address = 0x08,
738 .invert = 1,
739 .invert_oclk = 0,
740 .xtal_freq = TDA10046_XTAL_16M,
741 .agc_config = TDA10046_AGC_TDA827X,
742 .gpio_config = TDA10046_GP11_I,
743 .if_freq = TDA10046_FREQ_045,
744 .i2c_gate = 0x4b,
745 .tuner_address = 0x61,
746 .request_firmware = philips_tda1004x_request_firmware
747};
748
749static struct tda1004x_config asus_p7131_dual_config = {
750 .demod_address = 0x08,
751 .invert = 1,
752 .invert_oclk = 0,
753 .xtal_freq = TDA10046_XTAL_16M,
754 .agc_config = TDA10046_AGC_TDA827X,
755 .gpio_config = TDA10046_GP11_I,
756 .if_freq = TDA10046_FREQ_045,
757 .i2c_gate = 0x4b,
758 .tuner_address = 0x61,
759 .antenna_switch= 2,
760 .request_firmware = philips_tda1004x_request_firmware
761};
762
763static struct tda1004x_config lifeview_trio_config = {
764 .demod_address = 0x09,
765 .invert = 1,
766 .invert_oclk = 0,
767 .xtal_freq = TDA10046_XTAL_16M,
768 .agc_config = TDA10046_AGC_TDA827X,
769 .gpio_config = TDA10046_GP00_I,
770 .if_freq = TDA10046_FREQ_045,
771 .tuner_address = 0x60,
772 .request_firmware = philips_tda1004x_request_firmware
773};
774
775static struct tda1004x_config tevion_dvbt220rf_config = {
776 .demod_address = 0x08,
777 .invert = 1,
778 .invert_oclk = 0,
779 .xtal_freq = TDA10046_XTAL_16M,
780 .agc_config = TDA10046_AGC_TDA827X,
781 .gpio_config = TDA10046_GP11_I,
782 .if_freq = TDA10046_FREQ_045,
783 .tuner_address = 0x60,
784 .request_firmware = philips_tda1004x_request_firmware
785};
786
787static struct tda1004x_config md8800_dvbt_config = {
788 .demod_address = 0x08,
789 .invert = 1,
790 .invert_oclk = 0,
791 .xtal_freq = TDA10046_XTAL_16M,
792 .agc_config = TDA10046_AGC_TDA827X,
793 .gpio_config = TDA10046_GP01_I,
794 .if_freq = TDA10046_FREQ_045,
795 .i2c_gate = 0x4b,
796 .tuner_address = 0x60,
797 .request_firmware = philips_tda1004x_request_firmware
798};
799
800static struct tda1004x_config asus_p7131_4871_config = {
801 .demod_address = 0x08,
802 .invert = 1,
803 .invert_oclk = 0,
804 .xtal_freq = TDA10046_XTAL_16M,
805 .agc_config = TDA10046_AGC_TDA827X,
806 .gpio_config = TDA10046_GP01_I,
807 .if_freq = TDA10046_FREQ_045,
808 .i2c_gate = 0x4b,
809 .tuner_address = 0x61,
810 .antenna_switch= 2,
811 .request_firmware = philips_tda1004x_request_firmware
812};
813
814static struct tda1004x_config asus_p7131_hybrid_lna_config = {
815 .demod_address = 0x08,
816 .invert = 1,
817 .invert_oclk = 0,
818 .xtal_freq = TDA10046_XTAL_16M,
819 .agc_config = TDA10046_AGC_TDA827X,
820 .gpio_config = TDA10046_GP11_I,
821 .if_freq = TDA10046_FREQ_045,
822 .i2c_gate = 0x4b,
823 .tuner_address = 0x61,
824 .antenna_switch= 2,
825 .request_firmware = philips_tda1004x_request_firmware
826};
827
828static struct tda1004x_config kworld_dvb_t_210_config = {
829 .demod_address = 0x08,
830 .invert = 1,
831 .invert_oclk = 0,
832 .xtal_freq = TDA10046_XTAL_16M,
833 .agc_config = TDA10046_AGC_TDA827X,
834 .gpio_config = TDA10046_GP11_I,
835 .if_freq = TDA10046_FREQ_045,
836 .i2c_gate = 0x4b,
837 .tuner_address = 0x61,
838 .antenna_switch= 1,
839 .request_firmware = philips_tda1004x_request_firmware
840};
841
842static struct tda1004x_config avermedia_super_007_config = {
843 .demod_address = 0x08,
844 .invert = 1,
845 .invert_oclk = 0,
846 .xtal_freq = TDA10046_XTAL_16M,
847 .agc_config = TDA10046_AGC_TDA827X,
848 .gpio_config = TDA10046_GP01_I,
849 .if_freq = TDA10046_FREQ_045,
850 .i2c_gate = 0x4b,
851 .tuner_address = 0x60,
852 .antenna_switch= 1,
853 .request_firmware = philips_tda1004x_request_firmware
854};
855
856static struct tda1004x_config twinhan_dtv_dvb_3056_config = {
857 .demod_address = 0x08,
858 .invert = 1,
859 .invert_oclk = 0,
860 .xtal_freq = TDA10046_XTAL_16M,
861 .agc_config = TDA10046_AGC_TDA827X,
862 .gpio_config = TDA10046_GP01_I,
863 .if_freq = TDA10046_FREQ_045,
864 .i2c_gate = 0x42,
865 .tuner_address = 0x61,
866 .antenna_switch = 1,
867 .request_firmware = philips_tda1004x_request_firmware
868};
869
870static struct tda1004x_config asus_tiger_3in1_config = {
871 .demod_address = 0x0b,
872 .invert = 1,
873 .invert_oclk = 0,
874 .xtal_freq = TDA10046_XTAL_16M,
875 .agc_config = TDA10046_AGC_TDA827X,
876 .gpio_config = TDA10046_GP11_I,
877 .if_freq = TDA10046_FREQ_045,
878 .i2c_gate = 0x4b,
879 .tuner_address = 0x61,
880 .antenna_switch = 1,
881 .request_firmware = philips_tda1004x_request_firmware
882};
883
884static struct tda1004x_config asus_ps3_100_config = {
885 .demod_address = 0x0b,
886 .invert = 1,
887 .invert_oclk = 0,
888 .xtal_freq = TDA10046_XTAL_16M,
889 .agc_config = TDA10046_AGC_TDA827X,
890 .gpio_config = TDA10046_GP11_I,
891 .if_freq = TDA10046_FREQ_045,
892 .i2c_gate = 0x4b,
893 .tuner_address = 0x61,
894 .antenna_switch = 1,
895 .request_firmware = philips_tda1004x_request_firmware
896};
897
898/* ------------------------------------------------------------------
899 * special case: this card uses saa713x GPIO22 for the mode switch
900 */
901
902static int ads_duo_tuner_init(struct dvb_frontend *fe)
903{
904 struct saa7134_dev *dev = fe->dvb->priv;
905 philips_tda827x_tuner_init(fe);
906 /* route TDA8275a AGC input to the channel decoder */
907 saa7134_set_gpio(dev, 22, 1);
908 return 0;
909}
910
911static int ads_duo_tuner_sleep(struct dvb_frontend *fe)
912{
913 struct saa7134_dev *dev = fe->dvb->priv;
914 /* route TDA8275a AGC input to the analog IF chip*/
915 saa7134_set_gpio(dev, 22, 0);
916 philips_tda827x_tuner_sleep(fe);
917 return 0;
918}
919
920static struct tda827x_config ads_duo_cfg = {
921 .init = ads_duo_tuner_init,
922 .sleep = ads_duo_tuner_sleep,
923 .config = 0
924};
925
926static struct tda1004x_config ads_tech_duo_config = {
927 .demod_address = 0x08,
928 .invert = 1,
929 .invert_oclk = 0,
930 .xtal_freq = TDA10046_XTAL_16M,
931 .agc_config = TDA10046_AGC_TDA827X,
932 .gpio_config = TDA10046_GP00_I,
933 .if_freq = TDA10046_FREQ_045,
934 .tuner_address = 0x61,
935 .request_firmware = philips_tda1004x_request_firmware
936};
937
938static struct zl10353_config behold_h6_config = {
939 .demod_address = 0x1e>>1,
940 .no_tuner = 1,
941 .parallel_ts = 1,
942 .disable_i2c_gate_ctrl = 1,
943};
944
945static struct xc5000_config behold_x7_tunerconfig = {
946 .i2c_address = 0xc2>>1,
947 .if_khz = 4560,
948 .radio_input = XC5000_RADIO_FM1,
949};
950
951static struct zl10353_config behold_x7_config = {
952 .demod_address = 0x1e>>1,
953 .if2 = 45600,
954 .no_tuner = 1,
955 .parallel_ts = 1,
956 .disable_i2c_gate_ctrl = 1,
957};
958
959static struct zl10353_config videomate_t750_zl10353_config = {
960 .demod_address = 0x0f,
961 .no_tuner = 1,
962 .parallel_ts = 1,
963 .disable_i2c_gate_ctrl = 1,
964};
965
966static struct qt1010_config videomate_t750_qt1010_config = {
967 .i2c_address = 0x62
968};
969
970
971/* ==================================================================
972 * tda10086 based DVB-S cards, helper functions
973 */
974
975static struct tda10086_config flydvbs = {
976 .demod_address = 0x0e,
977 .invert = 0,
978 .diseqc_tone = 0,
979 .xtal_freq = TDA10086_XTAL_16M,
980};
981
982static struct tda10086_config sd1878_4m = {
983 .demod_address = 0x0e,
984 .invert = 0,
985 .diseqc_tone = 0,
986 .xtal_freq = TDA10086_XTAL_4M,
987};
988
989/* ------------------------------------------------------------------
990 * special case: lnb supply is connected to the gated i2c
991 */
992
993static int md8800_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
994{
995 int res = -EIO;
996 struct saa7134_dev *dev = fe->dvb->priv;
997 if (fe->ops.i2c_gate_ctrl) {
998 fe->ops.i2c_gate_ctrl(fe, 1);
999 if (dev->original_set_voltage)
1000 res = dev->original_set_voltage(fe, voltage);
1001 fe->ops.i2c_gate_ctrl(fe, 0);
1002 }
1003 return res;
1004};
1005
1006static int md8800_set_high_voltage(struct dvb_frontend *fe, long arg)
1007{
1008 int res = -EIO;
1009 struct saa7134_dev *dev = fe->dvb->priv;
1010 if (fe->ops.i2c_gate_ctrl) {
1011 fe->ops.i2c_gate_ctrl(fe, 1);
1012 if (dev->original_set_high_voltage)
1013 res = dev->original_set_high_voltage(fe, arg);
1014 fe->ops.i2c_gate_ctrl(fe, 0);
1015 }
1016 return res;
1017};
1018
1019static int md8800_set_voltage2(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
1020{
1021 struct saa7134_dev *dev = fe->dvb->priv;
1022 u8 wbuf[2] = { 0x1f, 00 };
1023 u8 rbuf;
1024 struct i2c_msg msg[] = { { .addr = 0x08, .flags = 0, .buf = wbuf, .len = 1 },
1025 { .addr = 0x08, .flags = I2C_M_RD, .buf = &rbuf, .len = 1 } };
1026
1027 if (i2c_transfer(&dev->i2c_adap, msg, 2) != 2)
1028 return -EIO;
1029 /* NOTE: this assumes that gpo1 is used, it might be bit 5 (gpo2) */
1030 if (voltage == SEC_VOLTAGE_18)
1031 wbuf[1] = rbuf | 0x10;
1032 else
1033 wbuf[1] = rbuf & 0xef;
1034 msg[0].len = 2;
1035 i2c_transfer(&dev->i2c_adap, msg, 1);
1036 return 0;
1037}
1038
1039static int md8800_set_high_voltage2(struct dvb_frontend *fe, long arg)
1040{
1041 struct saa7134_dev *dev = fe->dvb->priv;
1042 wprintk("%s: sorry can't set high LNB supply voltage from here\n", __func__);
1043 return -EIO;
1044}
1045
1046/* ==================================================================
1047 * nxt200x based ATSC cards, helper functions
1048 */
1049
1050static struct nxt200x_config avertvhda180 = {
1051 .demod_address = 0x0a,
1052};
1053
1054static struct nxt200x_config kworldatsc110 = {
1055 .demod_address = 0x0a,
1056};
1057
1058/* ------------------------------------------------------------------ */
1059
1060static struct mt312_config avertv_a700_mt312 = {
1061 .demod_address = 0x0e,
1062 .voltage_inverted = 1,
1063};
1064
1065static struct zl10036_config avertv_a700_tuner = {
1066 .tuner_address = 0x60,
1067};
1068
1069static struct mt312_config zl10313_compro_s350_config = {
1070 .demod_address = 0x0e,
1071};
1072
1073static struct lgdt3305_config hcw_lgdt3305_config = {
1074 .i2c_addr = 0x0e,
1075 .mpeg_mode = LGDT3305_MPEG_SERIAL,
1076 .tpclk_edge = LGDT3305_TPCLK_RISING_EDGE,
1077 .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
1078 .deny_i2c_rptr = 1,
1079 .spectral_inversion = 1,
1080 .qam_if_khz = 4000,
1081 .vsb_if_khz = 3250,
1082};
1083
1084static struct tda10048_config hcw_tda10048_config = {
1085 .demod_address = 0x10 >> 1,
1086 .output_mode = TDA10048_SERIAL_OUTPUT,
1087 .fwbulkwritelen = TDA10048_BULKWRITE_200,
1088 .inversion = TDA10048_INVERSION_ON,
1089 .dtv6_if_freq_khz = TDA10048_IF_3300,
1090 .dtv7_if_freq_khz = TDA10048_IF_3500,
1091 .dtv8_if_freq_khz = TDA10048_IF_4000,
1092 .clk_freq_khz = TDA10048_CLK_16000,
1093 .disable_gate_access = 1,
1094};
1095
1096static struct tda18271_std_map hauppauge_tda18271_std_map = {
1097 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 4,
1098 .if_lvl = 1, .rfagc_top = 0x58, },
1099 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 5,
1100 .if_lvl = 1, .rfagc_top = 0x58, },
1101};
1102
1103static struct tda18271_config hcw_tda18271_config = {
1104 .std_map = &hauppauge_tda18271_std_map,
1105 .gate = TDA18271_GATE_ANALOG,
1106 .config = 3,
1107 .output_opt = TDA18271_OUTPUT_LT_OFF,
1108};
1109
1110static struct tda829x_config tda829x_no_probe = {
1111 .probe_tuner = TDA829X_DONT_PROBE,
1112};
1113
1114static struct tda10048_config zolid_tda10048_config = {
1115 .demod_address = 0x10 >> 1,
1116 .output_mode = TDA10048_PARALLEL_OUTPUT,
1117 .fwbulkwritelen = TDA10048_BULKWRITE_200,
1118 .inversion = TDA10048_INVERSION_ON,
1119 .dtv6_if_freq_khz = TDA10048_IF_3300,
1120 .dtv7_if_freq_khz = TDA10048_IF_3500,
1121 .dtv8_if_freq_khz = TDA10048_IF_4000,
1122 .clk_freq_khz = TDA10048_CLK_16000,
1123 .disable_gate_access = 1,
1124};
1125
1126static struct tda18271_config zolid_tda18271_config = {
1127 .gate = TDA18271_GATE_ANALOG,
1128};
1129
1130static struct tda10048_config dtv1000s_tda10048_config = {
1131 .demod_address = 0x10 >> 1,
1132 .output_mode = TDA10048_PARALLEL_OUTPUT,
1133 .fwbulkwritelen = TDA10048_BULKWRITE_200,
1134 .inversion = TDA10048_INVERSION_ON,
1135 .dtv6_if_freq_khz = TDA10048_IF_3300,
1136 .dtv7_if_freq_khz = TDA10048_IF_3800,
1137 .dtv8_if_freq_khz = TDA10048_IF_4300,
1138 .clk_freq_khz = TDA10048_CLK_16000,
1139 .disable_gate_access = 1,
1140};
1141
1142static struct tda18271_std_map dtv1000s_tda18271_std_map = {
1143 .dvbt_6 = { .if_freq = 3300, .agc_mode = 3, .std = 4,
1144 .if_lvl = 1, .rfagc_top = 0x37, },
1145 .dvbt_7 = { .if_freq = 3800, .agc_mode = 3, .std = 5,
1146 .if_lvl = 1, .rfagc_top = 0x37, },
1147 .dvbt_8 = { .if_freq = 4300, .agc_mode = 3, .std = 6,
1148 .if_lvl = 1, .rfagc_top = 0x37, },
1149};
1150
1151static struct tda18271_config dtv1000s_tda18271_config = {
1152 .std_map = &dtv1000s_tda18271_std_map,
1153 .gate = TDA18271_GATE_ANALOG,
1154};
1155
1156static struct lgs8gxx_config prohdtv_pro2_lgs8g75_config = {
1157 .prod = LGS8GXX_PROD_LGS8G75,
1158 .demod_address = 0x1d,
1159 .serial_ts = 0,
1160 .ts_clk_pol = 1,
1161 .ts_clk_gated = 0,
1162 .if_clk_freq = 30400, /* 30.4 MHz */
1163 .if_freq = 4000, /* 4.00 MHz */
1164 .if_neg_center = 0,
1165 .ext_adc = 0,
1166 .adc_signed = 1,
1167 .adc_vpp = 3, /* 2.0 Vpp */
1168 .if_neg_edge = 1,
1169};
1170
1171static struct tda18271_config prohdtv_pro2_tda18271_config = {
1172 .gate = TDA18271_GATE_ANALOG,
1173 .output_opt = TDA18271_OUTPUT_LT_OFF,
1174};
1175
1176static struct tda18271_std_map kworld_tda18271_std_map = {
1177 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 3,
1178 .if_lvl = 6, .rfagc_top = 0x37 },
1179 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0,
1180 .if_lvl = 6, .rfagc_top = 0x37 },
1181};
1182
1183static struct tda18271_config kworld_pc150u_tda18271_config = {
1184 .std_map = &kworld_tda18271_std_map,
1185 .gate = TDA18271_GATE_ANALOG,
1186 .output_opt = TDA18271_OUTPUT_LT_OFF,
1187 .config = 3, /* Use tuner callback for AGC */
1188 .rf_cal_on_startup = 1
1189};
1190
1191static struct s5h1411_config kworld_s5h1411_config = {
1192 .output_mode = S5H1411_PARALLEL_OUTPUT,
1193 .gpio = S5H1411_GPIO_OFF,
1194 .qam_if = S5H1411_IF_4000,
1195 .vsb_if = S5H1411_IF_3250,
1196 .inversion = S5H1411_INVERSION_ON,
1197 .status_mode = S5H1411_DEMODLOCKING,
1198 .mpeg_timing =
1199 S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
1200};
1201
1202
1203/* ==================================================================
1204 * Core code
1205 */
1206
1207static int dvb_init(struct saa7134_dev *dev)
1208{
1209 int ret;
1210 int attach_xc3028 = 0;
1211 struct videobuf_dvb_frontend *fe0;
1212
1213 /* FIXME: add support for multi-frontend */
1214 mutex_init(&dev->frontends.lock);
1215 INIT_LIST_HEAD(&dev->frontends.felist);
1216
1217 printk(KERN_INFO "%s() allocating 1 frontend\n", __func__);
1218 fe0 = videobuf_dvb_alloc_frontend(&dev->frontends, 1);
1219 if (!fe0) {
1220 printk(KERN_ERR "%s() failed to alloc\n", __func__);
1221 return -ENOMEM;
1222 }
1223
1224 /* init struct videobuf_dvb */
1225 dev->ts.nr_bufs = 32;
1226 dev->ts.nr_packets = 32*4;
1227 fe0->dvb.name = dev->name;
1228 videobuf_queue_sg_init(&fe0->dvb.dvbq, &saa7134_ts_qops,
1229 &dev->pci->dev, &dev->slock,
1230 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1231 V4L2_FIELD_ALTERNATE,
1232 sizeof(struct saa7134_buf),
1233 dev, NULL);
1234
1235 switch (dev->board) {
1236 case SAA7134_BOARD_PINNACLE_300I_DVBT_PAL:
1237 dprintk("pinnacle 300i dvb setup\n");
1238 fe0->dvb.frontend = dvb_attach(mt352_attach, &pinnacle_300i,
1239 &dev->i2c_adap);
1240 if (fe0->dvb.frontend) {
1241 fe0->dvb.frontend->ops.tuner_ops.set_params = mt352_pinnacle_tuner_set_params;
1242 }
1243 break;
1244 case SAA7134_BOARD_AVERMEDIA_777:
1245 case SAA7134_BOARD_AVERMEDIA_A16AR:
1246 dprintk("avertv 777 dvb setup\n");
1247 fe0->dvb.frontend = dvb_attach(mt352_attach, &avermedia_777,
1248 &dev->i2c_adap);
1249 if (fe0->dvb.frontend) {
1250 dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1251 &dev->i2c_adap, 0x61,
1252 TUNER_PHILIPS_TD1316);
1253 }
1254 break;
1255 case SAA7134_BOARD_AVERMEDIA_A16D:
1256 dprintk("AverMedia A16D dvb setup\n");
1257 fe0->dvb.frontend = dvb_attach(mt352_attach,
1258 &avermedia_xc3028_mt352_dev,
1259 &dev->i2c_adap);
1260 attach_xc3028 = 1;
1261 break;
1262 case SAA7134_BOARD_MD7134:
1263 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1264 &medion_cardbus,
1265 &dev->i2c_adap);
1266 if (fe0->dvb.frontend) {
1267 dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1268 &dev->i2c_adap, medion_cardbus.tuner_address,
1269 TUNER_PHILIPS_FMD1216ME_MK3);
1270 }
1271 break;
1272 case SAA7134_BOARD_PHILIPS_TOUGH:
1273 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1274 &philips_tu1216_60_config,
1275 &dev->i2c_adap);
1276 if (fe0->dvb.frontend) {
1277 fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1278 fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1279 }
1280 break;
1281 case SAA7134_BOARD_FLYDVBTDUO:
1282 case SAA7134_BOARD_FLYDVBT_DUO_CARDBUS:
1283 if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1284 &tda827x_cfg_0) < 0)
1285 goto detach_frontend;
1286 break;
1287 case SAA7134_BOARD_PHILIPS_EUROPA:
1288 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
1289 case SAA7134_BOARD_ASUS_EUROPA_HYBRID:
1290 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1291 &philips_europa_config,
1292 &dev->i2c_adap);
1293 if (fe0->dvb.frontend) {
1294 dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1295 fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1296 fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1297 fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1298 fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1299 }
1300 break;
1301 case SAA7134_BOARD_TECHNOTREND_BUDGET_T3000:
1302 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1303 &technotrend_budget_t3000_config,
1304 &dev->i2c_adap);
1305 if (fe0->dvb.frontend) {
1306 dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1307 fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1308 fe0->dvb.frontend->ops.tuner_ops.init = philips_europa_tuner_init;
1309 fe0->dvb.frontend->ops.tuner_ops.sleep = philips_europa_tuner_sleep;
1310 fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1311 }
1312 break;
1313 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
1314 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1315 &philips_tu1216_61_config,
1316 &dev->i2c_adap);
1317 if (fe0->dvb.frontend) {
1318 fe0->dvb.frontend->ops.tuner_ops.init = philips_tu1216_init;
1319 fe0->dvb.frontend->ops.tuner_ops.set_params = philips_tda6651_pll_set;
1320 }
1321 break;
1322 case SAA7134_BOARD_KWORLD_DVBT_210:
1323 if (configure_tda827x_fe(dev, &kworld_dvb_t_210_config,
1324 &tda827x_cfg_2) < 0)
1325 goto detach_frontend;
1326 break;
1327 case SAA7134_BOARD_HAUPPAUGE_HVR1120:
1328 fe0->dvb.frontend = dvb_attach(tda10048_attach,
1329 &hcw_tda10048_config,
1330 &dev->i2c_adap);
1331 if (fe0->dvb.frontend != NULL) {
1332 dvb_attach(tda829x_attach, fe0->dvb.frontend,
1333 &dev->i2c_adap, 0x4b,
1334 &tda829x_no_probe);
1335 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1336 0x60, &dev->i2c_adap,
1337 &hcw_tda18271_config);
1338 }
1339 break;
1340 case SAA7134_BOARD_PHILIPS_TIGER:
1341 if (configure_tda827x_fe(dev, &philips_tiger_config,
1342 &tda827x_cfg_0) < 0)
1343 goto detach_frontend;
1344 break;
1345 case SAA7134_BOARD_PINNACLE_PCTV_310i:
1346 if (configure_tda827x_fe(dev, &pinnacle_pctv_310i_config,
1347 &tda827x_cfg_1) < 0)
1348 goto detach_frontend;
1349 break;
1350 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1351 if (configure_tda827x_fe(dev, &hauppauge_hvr_1110_config,
1352 &tda827x_cfg_1) < 0)
1353 goto detach_frontend;
1354 break;
1355 case SAA7134_BOARD_HAUPPAUGE_HVR1150:
1356 fe0->dvb.frontend = dvb_attach(lgdt3305_attach,
1357 &hcw_lgdt3305_config,
1358 &dev->i2c_adap);
1359 if (fe0->dvb.frontend) {
1360 dvb_attach(tda829x_attach, fe0->dvb.frontend,
1361 &dev->i2c_adap, 0x4b,
1362 &tda829x_no_probe);
1363 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1364 0x60, &dev->i2c_adap,
1365 &hcw_tda18271_config);
1366 }
1367 break;
1368 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
1369 if (configure_tda827x_fe(dev, &asus_p7131_dual_config,
1370 &tda827x_cfg_0) < 0)
1371 goto detach_frontend;
1372 break;
1373 case SAA7134_BOARD_FLYDVBT_LR301:
1374 if (configure_tda827x_fe(dev, &tda827x_lifeview_config,
1375 &tda827x_cfg_0) < 0)
1376 goto detach_frontend;
1377 break;
1378 case SAA7134_BOARD_FLYDVB_TRIO:
1379 if (!use_frontend) { /* terrestrial */
1380 if (configure_tda827x_fe(dev, &lifeview_trio_config,
1381 &tda827x_cfg_0) < 0)
1382 goto detach_frontend;
1383 } else { /* satellite */
1384 fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs, &dev->i2c_adap);
1385 if (fe0->dvb.frontend) {
1386 if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x63,
1387 &dev->i2c_adap, 0) == NULL) {
1388 wprintk("%s: Lifeview Trio, No tda826x found!\n", __func__);
1389 goto detach_frontend;
1390 }
1391 if (dvb_attach(isl6421_attach, fe0->dvb.frontend, &dev->i2c_adap,
1392 0x08, 0, 0) == NULL) {
1393 wprintk("%s: Lifeview Trio, No ISL6421 found!\n", __func__);
1394 goto detach_frontend;
1395 }
1396 }
1397 }
1398 break;
1399 case SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331:
1400 case SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS:
1401 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1402 &ads_tech_duo_config,
1403 &dev->i2c_adap);
1404 if (fe0->dvb.frontend) {
1405 if (dvb_attach(tda827x_attach,fe0->dvb.frontend,
1406 ads_tech_duo_config.tuner_address, &dev->i2c_adap,
1407 &ads_duo_cfg) == NULL) {
1408 wprintk("no tda827x tuner found at addr: %02x\n",
1409 ads_tech_duo_config.tuner_address);
1410 goto detach_frontend;
1411 }
1412 } else
1413 wprintk("failed to attach tda10046\n");
1414 break;
1415 case SAA7134_BOARD_TEVION_DVBT_220RF:
1416 if (configure_tda827x_fe(dev, &tevion_dvbt220rf_config,
1417 &tda827x_cfg_0) < 0)
1418 goto detach_frontend;
1419 break;
1420 case SAA7134_BOARD_MEDION_MD8800_QUADRO:
1421 if (!use_frontend) { /* terrestrial */
1422 if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1423 &tda827x_cfg_0) < 0)
1424 goto detach_frontend;
1425 } else { /* satellite */
1426 fe0->dvb.frontend = dvb_attach(tda10086_attach,
1427 &flydvbs, &dev->i2c_adap);
1428 if (fe0->dvb.frontend) {
1429 struct dvb_frontend *fe = fe0->dvb.frontend;
1430 u8 dev_id = dev->eedata[2];
1431 u8 data = 0xc4;
1432 struct i2c_msg msg = {.addr = 0x08, .flags = 0, .len = 1};
1433
1434 if (dvb_attach(tda826x_attach, fe0->dvb.frontend,
1435 0x60, &dev->i2c_adap, 0) == NULL) {
1436 wprintk("%s: Medion Quadro, no tda826x "
1437 "found !\n", __func__);
1438 goto detach_frontend;
1439 }
1440 if (dev_id != 0x08) {
1441 /* we need to open the i2c gate (we know it exists) */
1442 fe->ops.i2c_gate_ctrl(fe, 1);
1443 if (dvb_attach(isl6405_attach, fe,
1444 &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1445 wprintk("%s: Medion Quadro, no ISL6405 "
1446 "found !\n", __func__);
1447 goto detach_frontend;
1448 }
1449 if (dev_id == 0x07) {
1450 /* fire up the 2nd section of the LNB supply since
1451 we can't do this from the other section */
1452 msg.buf = &data;
1453 i2c_transfer(&dev->i2c_adap, &msg, 1);
1454 }
1455 fe->ops.i2c_gate_ctrl(fe, 0);
1456 dev->original_set_voltage = fe->ops.set_voltage;
1457 fe->ops.set_voltage = md8800_set_voltage;
1458 dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1459 fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1460 } else {
1461 fe->ops.set_voltage = md8800_set_voltage2;
1462 fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage2;
1463 }
1464 }
1465 }
1466 break;
1467 case SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180:
1468 fe0->dvb.frontend = dvb_attach(nxt200x_attach, &avertvhda180,
1469 &dev->i2c_adap);
1470 if (fe0->dvb.frontend)
1471 dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x61,
1472 NULL, DVB_PLL_TDHU2);
1473 break;
1474 case SAA7134_BOARD_ADS_INSTANT_HDTV_PCI:
1475 case SAA7134_BOARD_KWORLD_ATSC110:
1476 fe0->dvb.frontend = dvb_attach(nxt200x_attach, &kworldatsc110,
1477 &dev->i2c_adap);
1478 if (fe0->dvb.frontend)
1479 dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1480 &dev->i2c_adap, 0x61,
1481 TUNER_PHILIPS_TUV1236D);
1482 break;
1483 case SAA7134_BOARD_KWORLD_PC150U:
1484 saa7134_set_gpio(dev, 18, 1); /* Switch to digital mode */
1485 saa7134_tuner_callback(dev, 0,
1486 TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1487 fe0->dvb.frontend = dvb_attach(s5h1411_attach,
1488 &kworld_s5h1411_config,
1489 &dev->i2c_adap);
1490 if (fe0->dvb.frontend != NULL) {
1491 dvb_attach(tda829x_attach, fe0->dvb.frontend,
1492 &dev->i2c_adap, 0x4b,
1493 &tda829x_no_probe);
1494 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1495 0x60, &dev->i2c_adap,
1496 &kworld_pc150u_tda18271_config);
1497 }
1498 break;
1499 case SAA7134_BOARD_FLYDVBS_LR300:
1500 fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1501 &dev->i2c_adap);
1502 if (fe0->dvb.frontend) {
1503 if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1504 &dev->i2c_adap, 0) == NULL) {
1505 wprintk("%s: No tda826x found!\n", __func__);
1506 goto detach_frontend;
1507 }
1508 if (dvb_attach(isl6421_attach, fe0->dvb.frontend,
1509 &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1510 wprintk("%s: No ISL6421 found!\n", __func__);
1511 goto detach_frontend;
1512 }
1513 }
1514 break;
1515 case SAA7134_BOARD_ASUS_EUROPA2_HYBRID:
1516 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1517 &medion_cardbus,
1518 &dev->i2c_adap);
1519 if (fe0->dvb.frontend) {
1520 dev->original_demod_sleep = fe0->dvb.frontend->ops.sleep;
1521 fe0->dvb.frontend->ops.sleep = philips_europa_demod_sleep;
1522
1523 dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1524 &dev->i2c_adap, medion_cardbus.tuner_address,
1525 TUNER_PHILIPS_FMD1216ME_MK3);
1526 }
1527 break;
1528 case SAA7134_BOARD_VIDEOMATE_DVBT_200A:
1529 fe0->dvb.frontend = dvb_attach(tda10046_attach,
1530 &philips_europa_config,
1531 &dev->i2c_adap);
1532 if (fe0->dvb.frontend) {
1533 fe0->dvb.frontend->ops.tuner_ops.init = philips_td1316_tuner_init;
1534 fe0->dvb.frontend->ops.tuner_ops.set_params = philips_td1316_tuner_set_params;
1535 }
1536 break;
1537 case SAA7134_BOARD_CINERGY_HT_PCMCIA:
1538 if (configure_tda827x_fe(dev, &cinergy_ht_config,
1539 &tda827x_cfg_0) < 0)
1540 goto detach_frontend;
1541 break;
1542 case SAA7134_BOARD_CINERGY_HT_PCI:
1543 if (configure_tda827x_fe(dev, &cinergy_ht_pci_config,
1544 &tda827x_cfg_0) < 0)
1545 goto detach_frontend;
1546 break;
1547 case SAA7134_BOARD_PHILIPS_TIGER_S:
1548 if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1549 &tda827x_cfg_2) < 0)
1550 goto detach_frontend;
1551 break;
1552 case SAA7134_BOARD_ASUS_P7131_4871:
1553 if (configure_tda827x_fe(dev, &asus_p7131_4871_config,
1554 &tda827x_cfg_2) < 0)
1555 goto detach_frontend;
1556 break;
1557 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
1558 if (configure_tda827x_fe(dev, &asus_p7131_hybrid_lna_config,
1559 &tda827x_cfg_2) < 0)
1560 goto detach_frontend;
1561 break;
1562 case SAA7134_BOARD_AVERMEDIA_SUPER_007:
1563 if (configure_tda827x_fe(dev, &avermedia_super_007_config,
1564 &tda827x_cfg_0) < 0)
1565 goto detach_frontend;
1566 break;
1567 case SAA7134_BOARD_TWINHAN_DTV_DVB_3056:
1568 if (configure_tda827x_fe(dev, &twinhan_dtv_dvb_3056_config,
1569 &tda827x_cfg_2_sw42) < 0)
1570 goto detach_frontend;
1571 break;
1572 case SAA7134_BOARD_PHILIPS_SNAKE:
1573 fe0->dvb.frontend = dvb_attach(tda10086_attach, &flydvbs,
1574 &dev->i2c_adap);
1575 if (fe0->dvb.frontend) {
1576 if (dvb_attach(tda826x_attach, fe0->dvb.frontend, 0x60,
1577 &dev->i2c_adap, 0) == NULL) {
1578 wprintk("%s: No tda826x found!\n", __func__);
1579 goto detach_frontend;
1580 }
1581 if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1582 &dev->i2c_adap, 0, 0) == NULL) {
1583 wprintk("%s: No lnbp21 found!\n", __func__);
1584 goto detach_frontend;
1585 }
1586 }
1587 break;
1588 case SAA7134_BOARD_CREATIX_CTX953:
1589 if (configure_tda827x_fe(dev, &md8800_dvbt_config,
1590 &tda827x_cfg_0) < 0)
1591 goto detach_frontend;
1592 break;
1593 case SAA7134_BOARD_MSI_TVANYWHERE_AD11:
1594 if (configure_tda827x_fe(dev, &philips_tiger_s_config,
1595 &tda827x_cfg_2) < 0)
1596 goto detach_frontend;
1597 break;
1598 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
1599 dprintk("AverMedia E506R dvb setup\n");
1600 saa7134_set_gpio(dev, 25, 0);
1601 msleep(10);
1602 saa7134_set_gpio(dev, 25, 1);
1603 fe0->dvb.frontend = dvb_attach(mt352_attach,
1604 &avermedia_xc3028_mt352_dev,
1605 &dev->i2c_adap);
1606 attach_xc3028 = 1;
1607 break;
1608 case SAA7134_BOARD_MD7134_BRIDGE_2:
1609 fe0->dvb.frontend = dvb_attach(tda10086_attach,
1610 &sd1878_4m, &dev->i2c_adap);
1611 if (fe0->dvb.frontend) {
1612 struct dvb_frontend *fe;
1613 if (dvb_attach(dvb_pll_attach, fe0->dvb.frontend, 0x60,
1614 &dev->i2c_adap, DVB_PLL_PHILIPS_SD1878_TDA8261) == NULL) {
1615 wprintk("%s: MD7134 DVB-S, no SD1878 "
1616 "found !\n", __func__);
1617 goto detach_frontend;
1618 }
1619 /* we need to open the i2c gate (we know it exists) */
1620 fe = fe0->dvb.frontend;
1621 fe->ops.i2c_gate_ctrl(fe, 1);
1622 if (dvb_attach(isl6405_attach, fe,
1623 &dev->i2c_adap, 0x08, 0, 0) == NULL) {
1624 wprintk("%s: MD7134 DVB-S, no ISL6405 "
1625 "found !\n", __func__);
1626 goto detach_frontend;
1627 }
1628 fe->ops.i2c_gate_ctrl(fe, 0);
1629 dev->original_set_voltage = fe->ops.set_voltage;
1630 fe->ops.set_voltage = md8800_set_voltage;
1631 dev->original_set_high_voltage = fe->ops.enable_high_lnb_voltage;
1632 fe->ops.enable_high_lnb_voltage = md8800_set_high_voltage;
1633 }
1634 break;
1635 case SAA7134_BOARD_AVERMEDIA_M103:
1636 saa7134_set_gpio(dev, 25, 0);
1637 msleep(10);
1638 saa7134_set_gpio(dev, 25, 1);
1639 fe0->dvb.frontend = dvb_attach(mt352_attach,
1640 &avermedia_xc3028_mt352_dev,
1641 &dev->i2c_adap);
1642 attach_xc3028 = 1;
1643 break;
1644 case SAA7134_BOARD_ASUSTeK_TIGER_3IN1:
1645 if (!use_frontend) { /* terrestrial */
1646 if (configure_tda827x_fe(dev, &asus_tiger_3in1_config,
1647 &tda827x_cfg_2) < 0)
1648 goto detach_frontend;
1649 } else { /* satellite */
1650 fe0->dvb.frontend = dvb_attach(tda10086_attach,
1651 &flydvbs, &dev->i2c_adap);
1652 if (fe0->dvb.frontend) {
1653 if (dvb_attach(tda826x_attach,
1654 fe0->dvb.frontend, 0x60,
1655 &dev->i2c_adap, 0) == NULL) {
1656 wprintk("%s: Asus Tiger 3in1, no "
1657 "tda826x found!\n", __func__);
1658 goto detach_frontend;
1659 }
1660 if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1661 &dev->i2c_adap, 0, 0) == NULL) {
1662 wprintk("%s: Asus Tiger 3in1, no lnbp21"
1663 " found!\n", __func__);
1664 goto detach_frontend;
1665 }
1666 }
1667 }
1668 break;
1669 case SAA7134_BOARD_ASUSTeK_PS3_100:
1670 if (!use_frontend) { /* terrestrial */
1671 if (configure_tda827x_fe(dev, &asus_ps3_100_config,
1672 &tda827x_cfg_2) < 0)
1673 goto detach_frontend;
1674 } else { /* satellite */
1675 fe0->dvb.frontend = dvb_attach(tda10086_attach,
1676 &flydvbs, &dev->i2c_adap);
1677 if (fe0->dvb.frontend) {
1678 if (dvb_attach(tda826x_attach,
1679 fe0->dvb.frontend, 0x60,
1680 &dev->i2c_adap, 0) == NULL) {
1681 wprintk("%s: Asus My Cinema PS3-100, no "
1682 "tda826x found!\n", __func__);
1683 goto detach_frontend;
1684 }
1685 if (dvb_attach(lnbp21_attach, fe0->dvb.frontend,
1686 &dev->i2c_adap, 0, 0) == NULL) {
1687 wprintk("%s: Asus My Cinema PS3-100, no lnbp21"
1688 " found!\n", __func__);
1689 goto detach_frontend;
1690 }
1691 }
1692 }
1693 break;
1694 case SAA7134_BOARD_ASUSTeK_TIGER:
1695 if (configure_tda827x_fe(dev, &philips_tiger_config,
1696 &tda827x_cfg_0) < 0)
1697 goto detach_frontend;
1698 break;
1699 case SAA7134_BOARD_BEHOLD_H6:
1700 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1701 &behold_h6_config,
1702 &dev->i2c_adap);
1703 if (fe0->dvb.frontend) {
1704 dvb_attach(simple_tuner_attach, fe0->dvb.frontend,
1705 &dev->i2c_adap, 0x61,
1706 TUNER_PHILIPS_FMD1216MEX_MK3);
1707 }
1708 break;
1709 case SAA7134_BOARD_BEHOLD_X7:
1710 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1711 &behold_x7_config,
1712 &dev->i2c_adap);
1713 if (fe0->dvb.frontend) {
1714 dvb_attach(xc5000_attach, fe0->dvb.frontend,
1715 &dev->i2c_adap, &behold_x7_tunerconfig);
1716 }
1717 break;
1718 case SAA7134_BOARD_BEHOLD_H7:
1719 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1720 &behold_x7_config,
1721 &dev->i2c_adap);
1722 if (fe0->dvb.frontend) {
1723 dvb_attach(xc5000_attach, fe0->dvb.frontend,
1724 &dev->i2c_adap, &behold_x7_tunerconfig);
1725 }
1726 break;
1727 case SAA7134_BOARD_AVERMEDIA_A700_PRO:
1728 case SAA7134_BOARD_AVERMEDIA_A700_HYBRID:
1729 /* Zarlink ZL10313 */
1730 fe0->dvb.frontend = dvb_attach(mt312_attach,
1731 &avertv_a700_mt312, &dev->i2c_adap);
1732 if (fe0->dvb.frontend) {
1733 if (dvb_attach(zl10036_attach, fe0->dvb.frontend,
1734 &avertv_a700_tuner, &dev->i2c_adap) == NULL) {
1735 wprintk("%s: No zl10036 found!\n",
1736 __func__);
1737 }
1738 }
1739 break;
1740 case SAA7134_BOARD_VIDEOMATE_S350:
1741 fe0->dvb.frontend = dvb_attach(mt312_attach,
1742 &zl10313_compro_s350_config, &dev->i2c_adap);
1743 if (fe0->dvb.frontend)
1744 if (dvb_attach(zl10039_attach, fe0->dvb.frontend,
1745 0x60, &dev->i2c_adap) == NULL)
1746 wprintk("%s: No zl10039 found!\n",
1747 __func__);
1748
1749 break;
1750 case SAA7134_BOARD_VIDEOMATE_T750:
1751 fe0->dvb.frontend = dvb_attach(zl10353_attach,
1752 &videomate_t750_zl10353_config,
1753 &dev->i2c_adap);
1754 if (fe0->dvb.frontend != NULL) {
1755 if (dvb_attach(qt1010_attach,
1756 fe0->dvb.frontend,
1757 &dev->i2c_adap,
1758 &videomate_t750_qt1010_config) == NULL)
1759 wprintk("error attaching QT1010\n");
1760 }
1761 break;
1762 case SAA7134_BOARD_ZOLID_HYBRID_PCI:
1763 fe0->dvb.frontend = dvb_attach(tda10048_attach,
1764 &zolid_tda10048_config,
1765 &dev->i2c_adap);
1766 if (fe0->dvb.frontend != NULL) {
1767 dvb_attach(tda829x_attach, fe0->dvb.frontend,
1768 &dev->i2c_adap, 0x4b,
1769 &tda829x_no_probe);
1770 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1771 0x60, &dev->i2c_adap,
1772 &zolid_tda18271_config);
1773 }
1774 break;
1775 case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
1776 fe0->dvb.frontend = dvb_attach(tda10048_attach,
1777 &dtv1000s_tda10048_config,
1778 &dev->i2c_adap);
1779 if (fe0->dvb.frontend != NULL) {
1780 dvb_attach(tda829x_attach, fe0->dvb.frontend,
1781 &dev->i2c_adap, 0x4b,
1782 &tda829x_no_probe);
1783 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1784 0x60, &dev->i2c_adap,
1785 &dtv1000s_tda18271_config);
1786 }
1787 break;
1788 case SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG:
1789 /* Switch to digital mode */
1790 saa7134_tuner_callback(dev, 0,
1791 TDA18271_CALLBACK_CMD_AGC_ENABLE, 1);
1792 fe0->dvb.frontend = dvb_attach(mb86a20s_attach,
1793 &kworld_mb86a20s_config,
1794 &dev->i2c_adap);
1795 if (fe0->dvb.frontend != NULL) {
1796 dvb_attach(tda829x_attach, fe0->dvb.frontend,
1797 &dev->i2c_adap, 0x4b,
1798 &tda829x_no_probe);
1799 fe0->dvb.frontend->ops.i2c_gate_ctrl = kworld_sbtvd_gate_ctrl;
1800 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1801 0x60, &dev->i2c_adap,
1802 &kworld_tda18271_config);
1803 }
1804
1805 /* mb86a20s need to use the I2C gateway */
1806 break;
1807 case SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2:
1808 fe0->dvb.frontend = dvb_attach(lgs8gxx_attach,
1809 &prohdtv_pro2_lgs8g75_config,
1810 &dev->i2c_adap);
1811 if (fe0->dvb.frontend != NULL) {
1812 dvb_attach(tda829x_attach, fe0->dvb.frontend,
1813 &dev->i2c_adap, 0x4b,
1814 &tda829x_no_probe);
1815 dvb_attach(tda18271_attach, fe0->dvb.frontend,
1816 0x60, &dev->i2c_adap,
1817 &prohdtv_pro2_tda18271_config);
1818 }
1819 break;
1820 default:
1821 wprintk("Huh? unknown DVB card?\n");
1822 break;
1823 }
1824
1825 if (attach_xc3028) {
1826 struct dvb_frontend *fe;
1827 struct xc2028_config cfg = {
1828 .i2c_adap = &dev->i2c_adap,
1829 .i2c_addr = 0x61,
1830 };
1831
1832 if (!fe0->dvb.frontend)
1833 goto detach_frontend;
1834
1835 fe = dvb_attach(xc2028_attach, fe0->dvb.frontend, &cfg);
1836 if (!fe) {
1837 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
1838 dev->name);
1839 goto detach_frontend;
1840 }
1841 }
1842
1843 if (NULL == fe0->dvb.frontend) {
1844 printk(KERN_ERR "%s/dvb: frontend initialization failed\n", dev->name);
1845 goto detach_frontend;
1846 }
1847 /* define general-purpose callback pointer */
1848 fe0->dvb.frontend->callback = saa7134_tuner_callback;
1849
1850 /* register everything else */
1851 ret = videobuf_dvb_register_bus(&dev->frontends, THIS_MODULE, dev,
1852 &dev->pci->dev, adapter_nr, 0);
1853
1854 /* this sequence is necessary to make the tda1004x load its firmware
1855 * and to enter analog mode of hybrid boards
1856 */
1857 if (!ret) {
1858 if (fe0->dvb.frontend->ops.init)
1859 fe0->dvb.frontend->ops.init(fe0->dvb.frontend);
1860 if (fe0->dvb.frontend->ops.sleep)
1861 fe0->dvb.frontend->ops.sleep(fe0->dvb.frontend);
1862 if (fe0->dvb.frontend->ops.tuner_ops.sleep)
1863 fe0->dvb.frontend->ops.tuner_ops.sleep(fe0->dvb.frontend);
1864 }
1865 return ret;
1866
1867detach_frontend:
1868 videobuf_dvb_dealloc_frontends(&dev->frontends);
1869 return -EINVAL;
1870}
1871
1872static int dvb_fini(struct saa7134_dev *dev)
1873{
1874 struct videobuf_dvb_frontend *fe0;
1875
1876 /* Get the first frontend */
1877 fe0 = videobuf_dvb_get_frontend(&dev->frontends, 1);
1878 if (!fe0)
1879 return -EINVAL;
1880
1881 /* FIXME: I suspect that this code is bogus, since the entry for
1882 Pinnacle 300I DVB-T PAL already defines the proper init to allow
1883 the detection of mt2032 (TDA9887_PORT2_INACTIVE)
1884 */
1885 if (dev->board == SAA7134_BOARD_PINNACLE_300I_DVBT_PAL) {
1886 struct v4l2_priv_tun_config tda9887_cfg;
1887 static int on = TDA9887_PRESENT | TDA9887_PORT2_INACTIVE;
1888
1889 tda9887_cfg.tuner = TUNER_TDA9887;
1890 tda9887_cfg.priv = &on;
1891
1892 /* otherwise we don't detect the tuner on next insmod */
1893 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1894 } else if (dev->board == SAA7134_BOARD_MEDION_MD8800_QUADRO) {
1895 if ((dev->eedata[2] == 0x07) && use_frontend) {
1896 /* turn off the 2nd lnb supply */
1897 u8 data = 0x80;
1898 struct i2c_msg msg = {.addr = 0x08, .buf = &data, .flags = 0, .len = 1};
1899 struct dvb_frontend *fe;
1900 fe = fe0->dvb.frontend;
1901 if (fe->ops.i2c_gate_ctrl) {
1902 fe->ops.i2c_gate_ctrl(fe, 1);
1903 i2c_transfer(&dev->i2c_adap, &msg, 1);
1904 fe->ops.i2c_gate_ctrl(fe, 0);
1905 }
1906 }
1907 }
1908 videobuf_dvb_unregister_bus(&dev->frontends);
1909 return 0;
1910}
1911
1912static struct saa7134_mpeg_ops dvb_ops = {
1913 .type = SAA7134_MPEG_DVB,
1914 .init = dvb_init,
1915 .fini = dvb_fini,
1916};
1917
1918static int __init dvb_register(void)
1919{
1920 return saa7134_ts_register(&dvb_ops);
1921}
1922
1923static void __exit dvb_unregister(void)
1924{
1925 saa7134_ts_unregister(&dvb_ops);
1926}
1927
1928module_init(dvb_register);
1929module_exit(dvb_unregister);
1930
1931/* ------------------------------------------------------------------ */
1932/*
1933 * Local variables:
1934 * c-basic-offset: 8
1935 * End:
1936 */
diff --git a/drivers/media/pci/saa7134/saa7134-empress.c b/drivers/media/pci/saa7134/saa7134-empress.c
new file mode 100644
index 000000000000..dde361a9194e
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-empress.c
@@ -0,0 +1,590 @@
1/*
2 *
3 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/init.h>
21#include <linux/list.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25
26#include "saa7134-reg.h"
27#include "saa7134.h"
28
29#include <media/saa6752hs.h>
30#include <media/v4l2-common.h>
31#include <media/v4l2-chip-ident.h>
32
33/* ------------------------------------------------------------------ */
34
35MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
36MODULE_LICENSE("GPL");
37
38static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
39
40module_param_array(empress_nr, int, NULL, 0444);
41MODULE_PARM_DESC(empress_nr,"ts device number");
42
43static unsigned int debug;
44module_param(debug, int, 0644);
45MODULE_PARM_DESC(debug,"enable debug messages");
46
47#define dprintk(fmt, arg...) if (debug) \
48 printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
49
50/* ------------------------------------------------------------------ */
51
52static void ts_reset_encoder(struct saa7134_dev* dev)
53{
54 if (!dev->empress_started)
55 return;
56
57 saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
58 msleep(10);
59 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
60 msleep(100);
61 dev->empress_started = 0;
62}
63
64static int ts_init_encoder(struct saa7134_dev* dev)
65{
66 u32 leading_null_bytes = 0;
67
68 /* If more cards start to need this, then this
69 should probably be added to the card definitions. */
70 switch (dev->board) {
71 case SAA7134_BOARD_BEHOLD_M6:
72 case SAA7134_BOARD_BEHOLD_M63:
73 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
74 leading_null_bytes = 1;
75 break;
76 }
77 ts_reset_encoder(dev);
78 saa_call_all(dev, core, init, leading_null_bytes);
79 dev->empress_started = 1;
80 return 0;
81}
82
83/* ------------------------------------------------------------------ */
84
85static int ts_open(struct file *file)
86{
87 struct video_device *vdev = video_devdata(file);
88 struct saa7134_dev *dev = video_drvdata(file);
89 int err;
90
91 dprintk("open dev=%s\n", video_device_node_name(vdev));
92 err = -EBUSY;
93 if (!mutex_trylock(&dev->empress_tsq.vb_lock))
94 return err;
95 if (atomic_read(&dev->empress_users))
96 goto done;
97
98 /* Unmute audio */
99 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
100 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
101
102 atomic_inc(&dev->empress_users);
103 file->private_data = dev;
104 err = 0;
105
106done:
107 mutex_unlock(&dev->empress_tsq.vb_lock);
108 return err;
109}
110
111static int ts_release(struct file *file)
112{
113 struct saa7134_dev *dev = file->private_data;
114
115 videobuf_stop(&dev->empress_tsq);
116 videobuf_mmap_free(&dev->empress_tsq);
117
118 /* stop the encoder */
119 ts_reset_encoder(dev);
120
121 /* Mute audio */
122 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
123 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
124
125 atomic_dec(&dev->empress_users);
126
127 return 0;
128}
129
130static ssize_t
131ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
132{
133 struct saa7134_dev *dev = file->private_data;
134
135 if (!dev->empress_started)
136 ts_init_encoder(dev);
137
138 return videobuf_read_stream(&dev->empress_tsq,
139 data, count, ppos, 0,
140 file->f_flags & O_NONBLOCK);
141}
142
143static unsigned int
144ts_poll(struct file *file, struct poll_table_struct *wait)
145{
146 struct saa7134_dev *dev = file->private_data;
147
148 return videobuf_poll_stream(file, &dev->empress_tsq, wait);
149}
150
151
152static int
153ts_mmap(struct file *file, struct vm_area_struct * vma)
154{
155 struct saa7134_dev *dev = file->private_data;
156
157 return videobuf_mmap_mapper(&dev->empress_tsq, vma);
158}
159
160/*
161 * This function is _not_ called directly, but from
162 * video_generic_ioctl (and maybe others). userspace
163 * copying is done already, arg is a kernel pointer.
164 */
165
166static int empress_querycap(struct file *file, void *priv,
167 struct v4l2_capability *cap)
168{
169 struct saa7134_dev *dev = file->private_data;
170
171 strcpy(cap->driver, "saa7134");
172 strlcpy(cap->card, saa7134_boards[dev->board].name,
173 sizeof(cap->card));
174 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
175 cap->capabilities =
176 V4L2_CAP_VIDEO_CAPTURE |
177 V4L2_CAP_READWRITE |
178 V4L2_CAP_STREAMING;
179 return 0;
180}
181
182static int empress_enum_input(struct file *file, void *priv,
183 struct v4l2_input *i)
184{
185 if (i->index != 0)
186 return -EINVAL;
187
188 i->type = V4L2_INPUT_TYPE_CAMERA;
189 strcpy(i->name, "CCIR656");
190
191 return 0;
192}
193
194static int empress_g_input(struct file *file, void *priv, unsigned int *i)
195{
196 *i = 0;
197 return 0;
198}
199
200static int empress_s_input(struct file *file, void *priv, unsigned int i)
201{
202 if (i != 0)
203 return -EINVAL;
204
205 return 0;
206}
207
208static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
209 struct v4l2_fmtdesc *f)
210{
211 if (f->index != 0)
212 return -EINVAL;
213
214 strlcpy(f->description, "MPEG TS", sizeof(f->description));
215 f->pixelformat = V4L2_PIX_FMT_MPEG;
216
217 return 0;
218}
219
220static int empress_g_fmt_vid_cap(struct file *file, void *priv,
221 struct v4l2_format *f)
222{
223 struct saa7134_dev *dev = file->private_data;
224 struct v4l2_mbus_framefmt mbus_fmt;
225
226 saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt);
227
228 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
229 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
230 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
231
232 return 0;
233}
234
235static int empress_s_fmt_vid_cap(struct file *file, void *priv,
236 struct v4l2_format *f)
237{
238 struct saa7134_dev *dev = file->private_data;
239 struct v4l2_mbus_framefmt mbus_fmt;
240
241 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
242 saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
243 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
244
245 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
246 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
247
248 return 0;
249}
250
251static int empress_try_fmt_vid_cap(struct file *file, void *priv,
252 struct v4l2_format *f)
253{
254 struct saa7134_dev *dev = file->private_data;
255
256 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
257 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
258
259 return 0;
260}
261
262static int empress_reqbufs(struct file *file, void *priv,
263 struct v4l2_requestbuffers *p)
264{
265 struct saa7134_dev *dev = file->private_data;
266
267 return videobuf_reqbufs(&dev->empress_tsq, p);
268}
269
270static int empress_querybuf(struct file *file, void *priv,
271 struct v4l2_buffer *b)
272{
273 struct saa7134_dev *dev = file->private_data;
274
275 return videobuf_querybuf(&dev->empress_tsq, b);
276}
277
278static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
279{
280 struct saa7134_dev *dev = file->private_data;
281
282 return videobuf_qbuf(&dev->empress_tsq, b);
283}
284
285static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
286{
287 struct saa7134_dev *dev = file->private_data;
288
289 return videobuf_dqbuf(&dev->empress_tsq, b,
290 file->f_flags & O_NONBLOCK);
291}
292
293static int empress_streamon(struct file *file, void *priv,
294 enum v4l2_buf_type type)
295{
296 struct saa7134_dev *dev = file->private_data;
297
298 return videobuf_streamon(&dev->empress_tsq);
299}
300
301static int empress_streamoff(struct file *file, void *priv,
302 enum v4l2_buf_type type)
303{
304 struct saa7134_dev *dev = file->private_data;
305
306 return videobuf_streamoff(&dev->empress_tsq);
307}
308
309static int empress_s_ext_ctrls(struct file *file, void *priv,
310 struct v4l2_ext_controls *ctrls)
311{
312 struct saa7134_dev *dev = file->private_data;
313 int err;
314
315 /* count == 0 is abused in saa6752hs.c, so that special
316 case is handled here explicitly. */
317 if (ctrls->count == 0)
318 return 0;
319
320 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
321 return -EINVAL;
322
323 err = saa_call_empress(dev, core, s_ext_ctrls, ctrls);
324 ts_init_encoder(dev);
325
326 return err;
327}
328
329static int empress_g_ext_ctrls(struct file *file, void *priv,
330 struct v4l2_ext_controls *ctrls)
331{
332 struct saa7134_dev *dev = file->private_data;
333
334 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
335 return -EINVAL;
336 return saa_call_empress(dev, core, g_ext_ctrls, ctrls);
337}
338
339static int empress_g_ctrl(struct file *file, void *priv,
340 struct v4l2_control *c)
341{
342 struct saa7134_dev *dev = file->private_data;
343
344 return saa7134_g_ctrl_internal(dev, NULL, c);
345}
346
347static int empress_s_ctrl(struct file *file, void *priv,
348 struct v4l2_control *c)
349{
350 struct saa7134_dev *dev = file->private_data;
351
352 return saa7134_s_ctrl_internal(dev, NULL, c);
353}
354
355static int empress_queryctrl(struct file *file, void *priv,
356 struct v4l2_queryctrl *c)
357{
358 /* Must be sorted from low to high control ID! */
359 static const u32 user_ctrls[] = {
360 V4L2_CID_USER_CLASS,
361 V4L2_CID_BRIGHTNESS,
362 V4L2_CID_CONTRAST,
363 V4L2_CID_SATURATION,
364 V4L2_CID_HUE,
365 V4L2_CID_AUDIO_VOLUME,
366 V4L2_CID_AUDIO_MUTE,
367 V4L2_CID_HFLIP,
368 0
369 };
370
371 /* Must be sorted from low to high control ID! */
372 static const u32 mpeg_ctrls[] = {
373 V4L2_CID_MPEG_CLASS,
374 V4L2_CID_MPEG_STREAM_TYPE,
375 V4L2_CID_MPEG_STREAM_PID_PMT,
376 V4L2_CID_MPEG_STREAM_PID_AUDIO,
377 V4L2_CID_MPEG_STREAM_PID_VIDEO,
378 V4L2_CID_MPEG_STREAM_PID_PCR,
379 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
380 V4L2_CID_MPEG_AUDIO_ENCODING,
381 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
382 V4L2_CID_MPEG_VIDEO_ENCODING,
383 V4L2_CID_MPEG_VIDEO_ASPECT,
384 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
385 V4L2_CID_MPEG_VIDEO_BITRATE,
386 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
387 0
388 };
389 static const u32 *ctrl_classes[] = {
390 user_ctrls,
391 mpeg_ctrls,
392 NULL
393 };
394 struct saa7134_dev *dev = file->private_data;
395
396 c->id = v4l2_ctrl_next(ctrl_classes, c->id);
397 if (c->id == 0)
398 return -EINVAL;
399 if (c->id == V4L2_CID_USER_CLASS || c->id == V4L2_CID_MPEG_CLASS)
400 return v4l2_ctrl_query_fill(c, 0, 0, 0, 0);
401 if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
402 return saa7134_queryctrl(file, priv, c);
403 return saa_call_empress(dev, core, queryctrl, c);
404}
405
406static int empress_querymenu(struct file *file, void *priv,
407 struct v4l2_querymenu *c)
408{
409 struct saa7134_dev *dev = file->private_data;
410
411 if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
412 return -EINVAL;
413 return saa_call_empress(dev, core, querymenu, c);
414}
415
416static int empress_g_chip_ident(struct file *file, void *fh,
417 struct v4l2_dbg_chip_ident *chip)
418{
419 struct saa7134_dev *dev = file->private_data;
420
421 chip->ident = V4L2_IDENT_NONE;
422 chip->revision = 0;
423 if (chip->match.type == V4L2_CHIP_MATCH_I2C_DRIVER &&
424 !strcmp(chip->match.name, "saa6752hs"))
425 return saa_call_empress(dev, core, g_chip_ident, chip);
426 if (chip->match.type == V4L2_CHIP_MATCH_I2C_ADDR)
427 return saa_call_empress(dev, core, g_chip_ident, chip);
428 return -EINVAL;
429}
430
431static int empress_s_std(struct file *file, void *priv, v4l2_std_id *id)
432{
433 struct saa7134_dev *dev = file->private_data;
434
435 return saa7134_s_std_internal(dev, NULL, id);
436}
437
438static int empress_g_std(struct file *file, void *priv, v4l2_std_id *id)
439{
440 struct saa7134_dev *dev = file->private_data;
441
442 *id = dev->tvnorm->id;
443 return 0;
444}
445
446static const struct v4l2_file_operations ts_fops =
447{
448 .owner = THIS_MODULE,
449 .open = ts_open,
450 .release = ts_release,
451 .read = ts_read,
452 .poll = ts_poll,
453 .mmap = ts_mmap,
454 .ioctl = video_ioctl2,
455};
456
457static const struct v4l2_ioctl_ops ts_ioctl_ops = {
458 .vidioc_querycap = empress_querycap,
459 .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
460 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap,
461 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
462 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
463 .vidioc_reqbufs = empress_reqbufs,
464 .vidioc_querybuf = empress_querybuf,
465 .vidioc_qbuf = empress_qbuf,
466 .vidioc_dqbuf = empress_dqbuf,
467 .vidioc_streamon = empress_streamon,
468 .vidioc_streamoff = empress_streamoff,
469 .vidioc_s_ext_ctrls = empress_s_ext_ctrls,
470 .vidioc_g_ext_ctrls = empress_g_ext_ctrls,
471 .vidioc_enum_input = empress_enum_input,
472 .vidioc_g_input = empress_g_input,
473 .vidioc_s_input = empress_s_input,
474 .vidioc_queryctrl = empress_queryctrl,
475 .vidioc_querymenu = empress_querymenu,
476 .vidioc_g_ctrl = empress_g_ctrl,
477 .vidioc_s_ctrl = empress_s_ctrl,
478 .vidioc_g_chip_ident = empress_g_chip_ident,
479 .vidioc_s_std = empress_s_std,
480 .vidioc_g_std = empress_g_std,
481};
482
483/* ----------------------------------------------------------- */
484
485static struct video_device saa7134_empress_template = {
486 .name = "saa7134-empress",
487 .fops = &ts_fops,
488 .ioctl_ops = &ts_ioctl_ops,
489
490 .tvnorms = SAA7134_NORMS,
491 .current_norm = V4L2_STD_PAL,
492};
493
494static void empress_signal_update(struct work_struct *work)
495{
496 struct saa7134_dev* dev =
497 container_of(work, struct saa7134_dev, empress_workqueue);
498
499 if (dev->nosignal) {
500 dprintk("no video signal\n");
501 } else {
502 dprintk("video signal acquired\n");
503 }
504}
505
506static void empress_signal_change(struct saa7134_dev *dev)
507{
508 schedule_work(&dev->empress_workqueue);
509}
510
511
512static int empress_init(struct saa7134_dev *dev)
513{
514 int err;
515
516 dprintk("%s: %s\n",dev->name,__func__);
517 dev->empress_dev = video_device_alloc();
518 if (NULL == dev->empress_dev)
519 return -ENOMEM;
520 *(dev->empress_dev) = saa7134_empress_template;
521 dev->empress_dev->parent = &dev->pci->dev;
522 dev->empress_dev->release = video_device_release;
523 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
524 "%s empress (%s)", dev->name,
525 saa7134_boards[dev->board].name);
526
527 INIT_WORK(&dev->empress_workqueue, empress_signal_update);
528
529 video_set_drvdata(dev->empress_dev, dev);
530 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
531 empress_nr[dev->nr]);
532 if (err < 0) {
533 printk(KERN_INFO "%s: can't register video device\n",
534 dev->name);
535 video_device_release(dev->empress_dev);
536 dev->empress_dev = NULL;
537 return err;
538 }
539 printk(KERN_INFO "%s: registered device %s [mpeg]\n",
540 dev->name, video_device_node_name(dev->empress_dev));
541
542 videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
543 &dev->pci->dev, &dev->slock,
544 V4L2_BUF_TYPE_VIDEO_CAPTURE,
545 V4L2_FIELD_ALTERNATE,
546 sizeof(struct saa7134_buf),
547 dev, NULL);
548
549 empress_signal_update(&dev->empress_workqueue);
550 return 0;
551}
552
553static int empress_fini(struct saa7134_dev *dev)
554{
555 dprintk("%s: %s\n",dev->name,__func__);
556
557 if (NULL == dev->empress_dev)
558 return 0;
559 flush_work_sync(&dev->empress_workqueue);
560 video_unregister_device(dev->empress_dev);
561 dev->empress_dev = NULL;
562 return 0;
563}
564
565static struct saa7134_mpeg_ops empress_ops = {
566 .type = SAA7134_MPEG_EMPRESS,
567 .init = empress_init,
568 .fini = empress_fini,
569 .signal_change = empress_signal_change,
570};
571
572static int __init empress_register(void)
573{
574 return saa7134_ts_register(&empress_ops);
575}
576
577static void __exit empress_unregister(void)
578{
579 saa7134_ts_unregister(&empress_ops);
580}
581
582module_init(empress_register);
583module_exit(empress_unregister);
584
585/* ----------------------------------------------------------- */
586/*
587 * Local variables:
588 * c-basic-offset: 8
589 * End:
590 */
diff --git a/drivers/media/pci/saa7134/saa7134-i2c.c b/drivers/media/pci/saa7134/saa7134-i2c.c
new file mode 100644
index 000000000000..a176ec3285e0
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-i2c.c
@@ -0,0 +1,435 @@
1/*
2 *
3 * device driver for philips saa7134 based TV cards
4 * i2c interface support
5 *
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/delay.h>
28
29#include "saa7134-reg.h"
30#include "saa7134.h"
31#include <media/v4l2-common.h>
32
33/* ----------------------------------------------------------- */
34
35static unsigned int i2c_debug;
36module_param(i2c_debug, int, 0644);
37MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
38
39static unsigned int i2c_scan;
40module_param(i2c_scan, int, 0444);
41MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
42
43#define d1printk if (1 == i2c_debug) printk
44#define d2printk if (2 == i2c_debug) printk
45
46#define I2C_WAIT_DELAY 32
47#define I2C_WAIT_RETRY 16
48
49/* ----------------------------------------------------------- */
50
51static char *str_i2c_status[] = {
52 "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
53 "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
54 "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
55};
56
57enum i2c_status {
58 IDLE = 0, // no I2C command pending
59 DONE_STOP = 1, // I2C command done and STOP executed
60 BUSY = 2, // executing I2C command
61 TO_SCL = 3, // executing I2C command, time out on clock stretching
62 TO_ARB = 4, // time out on arbitration trial, still trying
63 DONE_WRITE = 5, // I2C command done and awaiting next write command
64 DONE_READ = 6, // I2C command done and awaiting next read command
65 DONE_WRITE_TO = 7, // see 5, and time out on status echo
66 DONE_READ_TO = 8, // see 6, and time out on status echo
67 NO_DEVICE = 9, // no acknowledge on device slave address
68 NO_ACKN = 10, // no acknowledge after data byte transfer
69 BUS_ERR = 11, // bus error
70 ARB_LOST = 12, // arbitration lost during transfer
71 SEQ_ERR = 13, // erroneous programming sequence
72 ST_ERR = 14, // wrong status echoing
73 SW_ERR = 15 // software error
74};
75
76static char *str_i2c_attr[] = {
77 "NOP", "STOP", "CONTINUE", "START"
78};
79
80enum i2c_attr {
81 NOP = 0, // no operation on I2C bus
82 STOP = 1, // stop condition, no associated byte transfer
83 CONTINUE = 2, // continue with byte transfer
84 START = 3 // start condition with byte transfer
85};
86
87static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
88{
89 enum i2c_status status;
90
91 status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
92 d2printk(KERN_DEBUG "%s: i2c stat <= %s\n",dev->name,
93 str_i2c_status[status]);
94 return status;
95}
96
97static inline void i2c_set_status(struct saa7134_dev *dev,
98 enum i2c_status status)
99{
100 d2printk(KERN_DEBUG "%s: i2c stat => %s\n",dev->name,
101 str_i2c_status[status]);
102 saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
103}
104
105static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
106{
107 d2printk(KERN_DEBUG "%s: i2c attr => %s\n",dev->name,
108 str_i2c_attr[attr]);
109 saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
110}
111
112static inline int i2c_is_error(enum i2c_status status)
113{
114 switch (status) {
115 case NO_DEVICE:
116 case NO_ACKN:
117 case BUS_ERR:
118 case ARB_LOST:
119 case SEQ_ERR:
120 case ST_ERR:
121 return true;
122 default:
123 return false;
124 }
125}
126
127static inline int i2c_is_idle(enum i2c_status status)
128{
129 switch (status) {
130 case IDLE:
131 case DONE_STOP:
132 return true;
133 default:
134 return false;
135 }
136}
137
138static inline int i2c_is_busy(enum i2c_status status)
139{
140 switch (status) {
141 case BUSY:
142 case TO_SCL:
143 case TO_ARB:
144 return true;
145 default:
146 return false;
147 }
148}
149
150static int i2c_is_busy_wait(struct saa7134_dev *dev)
151{
152 enum i2c_status status;
153 int count;
154
155 for (count = 0; count < I2C_WAIT_RETRY; count++) {
156 status = i2c_get_status(dev);
157 if (!i2c_is_busy(status))
158 break;
159 saa_wait(I2C_WAIT_DELAY);
160 }
161 if (I2C_WAIT_RETRY == count)
162 return false;
163 return true;
164}
165
166static int i2c_reset(struct saa7134_dev *dev)
167{
168 enum i2c_status status;
169 int count;
170
171 d2printk(KERN_DEBUG "%s: i2c reset\n",dev->name);
172 status = i2c_get_status(dev);
173 if (!i2c_is_error(status))
174 return true;
175 i2c_set_status(dev,status);
176
177 for (count = 0; count < I2C_WAIT_RETRY; count++) {
178 status = i2c_get_status(dev);
179 if (!i2c_is_error(status))
180 break;
181 udelay(I2C_WAIT_DELAY);
182 }
183 if (I2C_WAIT_RETRY == count)
184 return false;
185
186 if (!i2c_is_idle(status))
187 return false;
188
189 i2c_set_attr(dev,NOP);
190 return true;
191}
192
193static inline int i2c_send_byte(struct saa7134_dev *dev,
194 enum i2c_attr attr,
195 unsigned char data)
196{
197 enum i2c_status status;
198 __u32 dword;
199
200 /* have to write both attr + data in one 32bit word */
201 dword = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2);
202 dword &= 0x0f;
203 dword |= (attr << 6);
204 dword |= ((__u32)data << 8);
205 dword |= 0x00 << 16; /* 100 kHz */
206// dword |= 0x40 << 16; /* 400 kHz */
207 dword |= 0xf0 << 24;
208 saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword);
209 d2printk(KERN_DEBUG "%s: i2c data => 0x%x\n",dev->name,data);
210
211 if (!i2c_is_busy_wait(dev))
212 return -EIO;
213 status = i2c_get_status(dev);
214 if (i2c_is_error(status))
215 return -EIO;
216 return 0;
217}
218
219static inline int i2c_recv_byte(struct saa7134_dev *dev)
220{
221 enum i2c_status status;
222 unsigned char data;
223
224 i2c_set_attr(dev,CONTINUE);
225 if (!i2c_is_busy_wait(dev))
226 return -EIO;
227 status = i2c_get_status(dev);
228 if (i2c_is_error(status))
229 return -EIO;
230 data = saa_readb(SAA7134_I2C_DATA);
231 d2printk(KERN_DEBUG "%s: i2c data <= 0x%x\n",dev->name,data);
232 return data;
233}
234
235static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
236 struct i2c_msg *msgs, int num)
237{
238 struct saa7134_dev *dev = i2c_adap->algo_data;
239 enum i2c_status status;
240 unsigned char data;
241 int addr,rc,i,byte;
242
243 status = i2c_get_status(dev);
244 if (!i2c_is_idle(status))
245 if (!i2c_reset(dev))
246 return -EIO;
247
248 d2printk("start xfer\n");
249 d1printk(KERN_DEBUG "%s: i2c xfer:",dev->name);
250 for (i = 0; i < num; i++) {
251 if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
252 /* send address */
253 d2printk("send address\n");
254 addr = msgs[i].addr << 1;
255 if (msgs[i].flags & I2C_M_RD)
256 addr |= 1;
257 if (i > 0 && msgs[i].flags &
258 I2C_M_RD && msgs[i].addr != 0x40 &&
259 msgs[i].addr != 0x19) {
260 /* workaround for a saa7134 i2c bug
261 * needed to talk to the mt352 demux
262 * thanks to pinnacle for the hint */
263 int quirk = 0xfe;
264 d1printk(" [%02x quirk]",quirk);
265 i2c_send_byte(dev,START,quirk);
266 i2c_recv_byte(dev);
267 }
268 d1printk(" < %02x", addr);
269 rc = i2c_send_byte(dev,START,addr);
270 if (rc < 0)
271 goto err;
272 }
273 if (msgs[i].flags & I2C_M_RD) {
274 /* read bytes */
275 d2printk("read bytes\n");
276 for (byte = 0; byte < msgs[i].len; byte++) {
277 d1printk(" =");
278 rc = i2c_recv_byte(dev);
279 if (rc < 0)
280 goto err;
281 d1printk("%02x", rc);
282 msgs[i].buf[byte] = rc;
283 }
284 /* discard mysterious extra byte when reading
285 from Samsung S5H1411. i2c bus gets error
286 if we do not. */
287 if (0x19 == msgs[i].addr) {
288 d1printk(" ?");
289 rc = i2c_recv_byte(dev);
290 if (rc < 0)
291 goto err;
292 d1printk("%02x", rc);
293 }
294 } else {
295 /* write bytes */
296 d2printk("write bytes\n");
297 for (byte = 0; byte < msgs[i].len; byte++) {
298 data = msgs[i].buf[byte];
299 d1printk(" %02x", data);
300 rc = i2c_send_byte(dev,CONTINUE,data);
301 if (rc < 0)
302 goto err;
303 }
304 }
305 }
306 d2printk("xfer done\n");
307 d1printk(" >");
308 i2c_set_attr(dev,STOP);
309 rc = -EIO;
310 if (!i2c_is_busy_wait(dev))
311 goto err;
312 status = i2c_get_status(dev);
313 if (i2c_is_error(status))
314 goto err;
315 /* ensure that the bus is idle for at least one bit slot */
316 msleep(1);
317
318 d1printk("\n");
319 return num;
320 err:
321 if (1 == i2c_debug) {
322 status = i2c_get_status(dev);
323 printk(" ERROR: %s\n",str_i2c_status[status]);
324 }
325 return rc;
326}
327
328/* ----------------------------------------------------------- */
329
330static u32 functionality(struct i2c_adapter *adap)
331{
332 return I2C_FUNC_SMBUS_EMUL;
333}
334
335static struct i2c_algorithm saa7134_algo = {
336 .master_xfer = saa7134_i2c_xfer,
337 .functionality = functionality,
338};
339
340static struct i2c_adapter saa7134_adap_template = {
341 .owner = THIS_MODULE,
342 .name = "saa7134",
343 .algo = &saa7134_algo,
344};
345
346static struct i2c_client saa7134_client_template = {
347 .name = "saa7134 internal",
348};
349
350/* ----------------------------------------------------------- */
351
352static int
353saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
354{
355 unsigned char buf;
356 int i,err;
357
358 dev->i2c_client.addr = 0xa0 >> 1;
359 buf = 0;
360 if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
361 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
362 dev->name,err);
363 return -1;
364 }
365 if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
366 printk(KERN_WARNING "%s: i2c eeprom read error (err=%d)\n",
367 dev->name,err);
368 return -1;
369 }
370 for (i = 0; i < len; i++) {
371 if (0 == (i % 16))
372 printk(KERN_INFO "%s: i2c eeprom %02x:",dev->name,i);
373 printk(" %02x",eedata[i]);
374 if (15 == (i % 16))
375 printk("\n");
376 }
377 return 0;
378}
379
380static char *i2c_devs[128] = {
381 [ 0x20 ] = "mpeg encoder (saa6752hs)",
382 [ 0xa0 >> 1 ] = "eeprom",
383 [ 0xc0 >> 1 ] = "tuner (analog)",
384 [ 0x86 >> 1 ] = "tda9887",
385 [ 0x5a >> 1 ] = "remote control",
386};
387
388static void do_i2c_scan(char *name, struct i2c_client *c)
389{
390 unsigned char buf;
391 int i,rc;
392
393 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
394 c->addr = i;
395 rc = i2c_master_recv(c,&buf,0);
396 if (rc < 0)
397 continue;
398 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
399 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
400 }
401}
402
403int saa7134_i2c_register(struct saa7134_dev *dev)
404{
405 dev->i2c_adap = saa7134_adap_template;
406 dev->i2c_adap.dev.parent = &dev->pci->dev;
407 strcpy(dev->i2c_adap.name,dev->name);
408 dev->i2c_adap.algo_data = dev;
409 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
410 i2c_add_adapter(&dev->i2c_adap);
411
412 dev->i2c_client = saa7134_client_template;
413 dev->i2c_client.adapter = &dev->i2c_adap;
414
415 saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
416 if (i2c_scan)
417 do_i2c_scan(dev->name,&dev->i2c_client);
418
419 /* Instantiate the IR receiver device, if present */
420 saa7134_probe_i2c_ir(dev);
421 return 0;
422}
423
424int saa7134_i2c_unregister(struct saa7134_dev *dev)
425{
426 i2c_del_adapter(&dev->i2c_adap);
427 return 0;
428}
429
430/* ----------------------------------------------------------- */
431/*
432 * Local variables:
433 * c-basic-offset: 8
434 * End:
435 */
diff --git a/drivers/media/pci/saa7134/saa7134-input.c b/drivers/media/pci/saa7134/saa7134-input.c
new file mode 100644
index 000000000000..0f78f5e537e2
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-input.c
@@ -0,0 +1,1041 @@
1/*
2 *
3 * handle saa7134 IR remotes via linux kernel input layer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/slab.h>
26
27#include "saa7134-reg.h"
28#include "saa7134.h"
29
30#define MODULE_NAME "saa7134"
31
32static unsigned int disable_ir;
33module_param(disable_ir, int, 0444);
34MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
35
36static unsigned int ir_debug;
37module_param(ir_debug, int, 0644);
38MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
39
40static int pinnacle_remote;
41module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
42MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
43
44#define dprintk(fmt, arg...) if (ir_debug) \
45 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
46#define i2cdprintk(fmt, arg...) if (ir_debug) \
47 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
48
49/* Helper function for raw decoding at GPIO16 or GPIO18 */
50static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
51
52/* -------------------- GPIO generic keycode builder -------------------- */
53
54static int build_key(struct saa7134_dev *dev)
55{
56 struct saa7134_card_ir *ir = dev->remote;
57 u32 gpio, data;
58
59 /* here comes the additional handshake steps for some cards */
60 switch (dev->board) {
61 case SAA7134_BOARD_GOTVIEW_7135:
62 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
63 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
64 break;
65 }
66 /* rising SAA7134_GPIO_GPRESCAN reads the status */
67 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
68 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
69
70 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
71 if (ir->polling) {
72 if (ir->last_gpio == gpio)
73 return 0;
74 ir->last_gpio = gpio;
75 }
76
77 data = ir_extract_bits(gpio, ir->mask_keycode);
78 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
79 gpio, ir->mask_keycode, data);
80
81 switch (dev->board) {
82 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
83 if (data == ir->mask_keycode)
84 rc_keyup(ir->dev);
85 else
86 rc_keydown_notimeout(ir->dev, data, 0);
87 return 0;
88 }
89
90 if (ir->polling) {
91 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
92 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
93 rc_keydown_notimeout(ir->dev, data, 0);
94 } else {
95 rc_keyup(ir->dev);
96 }
97 }
98 else { /* IRQ driven mode - handle key press and release in one go */
99 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
100 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
101 rc_keydown_notimeout(ir->dev, data, 0);
102 rc_keyup(ir->dev);
103 }
104 }
105
106 return 0;
107}
108
109/* --------------------- Chip specific I2C key builders ----------------- */
110
111static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
112{
113 int gpio;
114 int attempt = 0;
115 unsigned char b;
116
117 /* We need this to access GPI Used by the saa_readl macro. */
118 struct saa7134_dev *dev = ir->c->adapter->algo_data;
119
120 if (dev == NULL) {
121 i2cdprintk("get_key_flydvb_trio: "
122 "ir->c->adapter->algo_data is NULL!\n");
123 return -EIO;
124 }
125
126 /* rising SAA7134_GPIGPRESCAN reads the status */
127 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
128 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
129
130 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
131
132 if (0x40000 & ~gpio)
133 return 0; /* No button press */
134
135 /* No button press - only before first key pressed */
136 if (b == 0xFF)
137 return 0;
138
139 /* poll IR chip */
140 /* weak up the IR chip */
141 b = 0;
142
143 while (1 != i2c_master_send(ir->c, &b, 1)) {
144 if ((attempt++) < 10) {
145 /*
146 * wait a bit for next attempt -
147 * I don't know how make it better
148 */
149 msleep(10);
150 continue;
151 }
152 i2cdprintk("send wake up byte to pic16C505 (IR chip)"
153 "failed %dx\n", attempt);
154 return -EIO;
155 }
156 if (1 != i2c_master_recv(ir->c, &b, 1)) {
157 i2cdprintk("read error\n");
158 return -EIO;
159 }
160
161 *ir_key = b;
162 *ir_raw = b;
163 return 1;
164}
165
166static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
167 u32 *ir_raw)
168{
169 unsigned char b;
170 int gpio;
171
172 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
173 struct saa7134_dev *dev = ir->c->adapter->algo_data;
174 if (dev == NULL) {
175 i2cdprintk("get_key_msi_tvanywhere_plus: "
176 "ir->c->adapter->algo_data is NULL!\n");
177 return -EIO;
178 }
179
180 /* rising SAA7134_GPIO_GPRESCAN reads the status */
181
182 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
183 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
184
185 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
186
187 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
188 I2C receive if gpio&0x40 is not low. */
189
190 if (gpio & 0x40)
191 return 0; /* No button press */
192
193 /* GPIO says there is a button press. Get it. */
194
195 if (1 != i2c_master_recv(ir->c, &b, 1)) {
196 i2cdprintk("read error\n");
197 return -EIO;
198 }
199
200 /* No button press */
201
202 if (b == 0xff)
203 return 0;
204
205 /* Button pressed */
206
207 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
208 *ir_key = b;
209 *ir_raw = b;
210 return 1;
211}
212
213/* copied and modified from get_key_msi_tvanywhere_plus() */
214static int get_key_kworld_pc150u(struct IR_i2c *ir, u32 *ir_key,
215 u32 *ir_raw)
216{
217 unsigned char b;
218 unsigned int gpio;
219
220 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
221 struct saa7134_dev *dev = ir->c->adapter->algo_data;
222 if (dev == NULL) {
223 i2cdprintk("get_key_kworld_pc150u: "
224 "ir->c->adapter->algo_data is NULL!\n");
225 return -EIO;
226 }
227
228 /* rising SAA7134_GPIO_GPRESCAN reads the status */
229
230 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
231 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
232
233 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
234
235 /* GPIO&0x100 is pulsed low when a button is pressed. Don't do
236 I2C receive if gpio&0x100 is not low. */
237
238 if (gpio & 0x100)
239 return 0; /* No button press */
240
241 /* GPIO says there is a button press. Get it. */
242
243 if (1 != i2c_master_recv(ir->c, &b, 1)) {
244 i2cdprintk("read error\n");
245 return -EIO;
246 }
247
248 /* No button press */
249
250 if (b == 0xff)
251 return 0;
252
253 /* Button pressed */
254
255 dprintk("get_key_kworld_pc150u: Key = 0x%02X\n", b);
256 *ir_key = b;
257 *ir_raw = b;
258 return 1;
259}
260
261static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
262{
263 unsigned char b;
264
265 /* poll IR chip */
266 if (1 != i2c_master_recv(ir->c, &b, 1)) {
267 i2cdprintk("read error\n");
268 return -EIO;
269 }
270
271 /* no button press */
272 if (b==0)
273 return 0;
274
275 /* repeating */
276 if (b & 0x80)
277 return 1;
278
279 *ir_key = b;
280 *ir_raw = b;
281 return 1;
282}
283
284static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
285{
286 unsigned char buf[5];
287
288 /* poll IR chip */
289 if (5 != i2c_master_recv(ir->c, buf, 5))
290 return -EIO;
291
292 /* Check if some key were pressed */
293 if (!(buf[0] & 0x80))
294 return 0;
295
296 /*
297 * buf[3] & 0x80 is always high.
298 * buf[3] & 0x40 is a parity bit. A repeat event is marked
299 * by preserving it into two separate readings
300 * buf[4] bits 0 and 1, and buf[1] and buf[2] are always
301 * zero.
302 */
303 *ir_key = 0x1fff & ((buf[3] << 8) | (buf[4] >> 2));
304 *ir_raw = *ir_key;
305 return 1;
306}
307
308
309static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
310{
311 unsigned char data[12];
312 u32 gpio;
313
314 struct saa7134_dev *dev = ir->c->adapter->algo_data;
315
316 /* rising SAA7134_GPIO_GPRESCAN reads the status */
317 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
318 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
319
320 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
321
322 if (0x400000 & ~gpio)
323 return 0; /* No button press */
324
325 ir->c->addr = 0x5a >> 1;
326
327 if (12 != i2c_master_recv(ir->c, data, 12)) {
328 i2cdprintk("read error\n");
329 return -EIO;
330 }
331
332 if (data[9] != (unsigned char)(~data[8]))
333 return 0;
334
335 *ir_raw = ((data[10] << 16) | (data[11] << 8) | (data[9] << 0));
336 *ir_key = *ir_raw;
337
338 return 1;
339}
340
341/* Common (grey or coloured) pinnacle PCTV remote handling
342 *
343 */
344static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
345 int parity_offset, int marker, int code_modulo)
346{
347 unsigned char b[4];
348 unsigned int start = 0,parity = 0,code = 0;
349
350 /* poll IR chip */
351 if (4 != i2c_master_recv(ir->c, b, 4)) {
352 i2cdprintk("read error\n");
353 return -EIO;
354 }
355
356 for (start = 0; start < ARRAY_SIZE(b); start++) {
357 if (b[start] == marker) {
358 code=b[(start+parity_offset + 1) % 4];
359 parity=b[(start+parity_offset) % 4];
360 }
361 }
362
363 /* Empty Request */
364 if (parity == 0)
365 return 0;
366
367 /* Repeating... */
368 if (ir->old == parity)
369 return 0;
370
371 ir->old = parity;
372
373 /* drop special codes when a key is held down a long time for the grey controller
374 In this case, the second bit of the code is asserted */
375 if (marker == 0xfe && (code & 0x40))
376 return 0;
377
378 code %= code_modulo;
379
380 *ir_raw = code;
381 *ir_key = code;
382
383 i2cdprintk("Pinnacle PCTV key %02x\n", code);
384
385 return 1;
386}
387
388/* The grey pinnacle PCTV remote
389 *
390 * There are one issue with this remote:
391 * - I2c packet does not change when the same key is pressed quickly. The workaround
392 * is to hold down each key for about half a second, so that another code is generated
393 * in the i2c packet, and the function can distinguish key presses.
394 *
395 * Sylvain Pasche <sylvain.pasche@gmail.com>
396 */
397static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
398{
399
400 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
401}
402
403
404/* The new pinnacle PCTV remote (with the colored buttons)
405 *
406 * Ricardo Cerqueira <v4l@cerqueira.org>
407 */
408static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
409{
410 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
411 *
412 * this is the only value that results in 42 unique
413 * codes < 128
414 */
415
416 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
417}
418
419void saa7134_input_irq(struct saa7134_dev *dev)
420{
421 struct saa7134_card_ir *ir;
422
423 if (!dev || !dev->remote)
424 return;
425
426 ir = dev->remote;
427 if (!ir->running)
428 return;
429
430 if (!ir->polling && !ir->raw_decode) {
431 build_key(dev);
432 } else if (ir->raw_decode) {
433 saa7134_raw_decode_irq(dev);
434 }
435}
436
437static void saa7134_input_timer(unsigned long data)
438{
439 struct saa7134_dev *dev = (struct saa7134_dev *)data;
440 struct saa7134_card_ir *ir = dev->remote;
441
442 build_key(dev);
443 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
444}
445
446static void ir_raw_decode_timer_end(unsigned long data)
447{
448 struct saa7134_dev *dev = (struct saa7134_dev *)data;
449
450 ir_raw_event_handle(dev->remote->dev);
451}
452
453static int __saa7134_ir_start(void *priv)
454{
455 struct saa7134_dev *dev = priv;
456 struct saa7134_card_ir *ir;
457
458 if (!dev || !dev->remote)
459 return -EINVAL;
460
461 ir = dev->remote;
462 if (ir->running)
463 return 0;
464
465 /* Moved here from saa7134_input_init1() because the latter
466 * is not called on device resume */
467 switch (dev->board) {
468 case SAA7134_BOARD_MD2819:
469 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
470 case SAA7134_BOARD_AVERMEDIA_305:
471 case SAA7134_BOARD_AVERMEDIA_307:
472 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
473 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
474 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
475 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
476 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
477 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
478 case SAA7134_BOARD_AVERMEDIA_M102:
479 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
480 /* Without this we won't receive key up events */
481 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
482 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
483 break;
484 case SAA7134_BOARD_AVERMEDIA_777:
485 case SAA7134_BOARD_AVERMEDIA_A16AR:
486 /* Without this we won't receive key up events */
487 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
488 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
489 break;
490 case SAA7134_BOARD_AVERMEDIA_A16D:
491 /* Without this we won't receive key up events */
492 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
493 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
494 break;
495 case SAA7134_BOARD_GOTVIEW_7135:
496 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
497 break;
498 }
499
500 ir->running = true;
501
502 if (ir->polling) {
503 setup_timer(&ir->timer, saa7134_input_timer,
504 (unsigned long)dev);
505 ir->timer.expires = jiffies + HZ;
506 add_timer(&ir->timer);
507 } else if (ir->raw_decode) {
508 /* set timer_end for code completion */
509 setup_timer(&ir->timer, ir_raw_decode_timer_end,
510 (unsigned long)dev);
511 }
512
513 return 0;
514}
515
516static void __saa7134_ir_stop(void *priv)
517{
518 struct saa7134_dev *dev = priv;
519 struct saa7134_card_ir *ir;
520
521 if (!dev || !dev->remote)
522 return;
523
524 ir = dev->remote;
525 if (!ir->running)
526 return;
527
528 if (ir->polling || ir->raw_decode)
529 del_timer_sync(&ir->timer);
530
531 ir->running = false;
532
533 return;
534}
535
536int saa7134_ir_start(struct saa7134_dev *dev)
537{
538 if (dev->remote->users)
539 return __saa7134_ir_start(dev);
540
541 return 0;
542}
543
544void saa7134_ir_stop(struct saa7134_dev *dev)
545{
546 if (dev->remote->users)
547 __saa7134_ir_stop(dev);
548}
549
550static int saa7134_ir_open(struct rc_dev *rc)
551{
552 struct saa7134_dev *dev = rc->priv;
553
554 dev->remote->users++;
555 return __saa7134_ir_start(dev);
556}
557
558static void saa7134_ir_close(struct rc_dev *rc)
559{
560 struct saa7134_dev *dev = rc->priv;
561
562 dev->remote->users--;
563 if (!dev->remote->users)
564 __saa7134_ir_stop(dev);
565}
566
567int saa7134_input_init1(struct saa7134_dev *dev)
568{
569 struct saa7134_card_ir *ir;
570 struct rc_dev *rc;
571 char *ir_codes = NULL;
572 u32 mask_keycode = 0;
573 u32 mask_keydown = 0;
574 u32 mask_keyup = 0;
575 unsigned polling = 0;
576 bool raw_decode = false;
577 int err;
578
579 if (dev->has_remote != SAA7134_REMOTE_GPIO)
580 return -ENODEV;
581 if (disable_ir)
582 return -ENODEV;
583
584 /* detect & configure */
585 switch (dev->board) {
586 case SAA7134_BOARD_FLYVIDEO2000:
587 case SAA7134_BOARD_FLYVIDEO3000:
588 case SAA7134_BOARD_FLYTVPLATINUM_FM:
589 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
590 case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
591 ir_codes = RC_MAP_FLYVIDEO;
592 mask_keycode = 0xEC00000;
593 mask_keydown = 0x0040000;
594 break;
595 case SAA7134_BOARD_CINERGY400:
596 case SAA7134_BOARD_CINERGY600:
597 case SAA7134_BOARD_CINERGY600_MK3:
598 ir_codes = RC_MAP_CINERGY;
599 mask_keycode = 0x00003f;
600 mask_keyup = 0x040000;
601 break;
602 case SAA7134_BOARD_ECS_TVP3XP:
603 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
604 ir_codes = RC_MAP_EZTV;
605 mask_keycode = 0x00017c;
606 mask_keyup = 0x000002;
607 polling = 50; // ms
608 break;
609 case SAA7134_BOARD_KWORLD_XPERT:
610 case SAA7134_BOARD_AVACSSMARTTV:
611 ir_codes = RC_MAP_PIXELVIEW;
612 mask_keycode = 0x00001F;
613 mask_keyup = 0x000020;
614 polling = 50; // ms
615 break;
616 case SAA7134_BOARD_MD2819:
617 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
618 case SAA7134_BOARD_AVERMEDIA_305:
619 case SAA7134_BOARD_AVERMEDIA_307:
620 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
621 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
622 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
623 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
624 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
625 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
626 case SAA7134_BOARD_AVERMEDIA_M102:
627 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
628 ir_codes = RC_MAP_AVERMEDIA;
629 mask_keycode = 0x0007C8;
630 mask_keydown = 0x000010;
631 polling = 50; // ms
632 /* GPIO stuff moved to __saa7134_ir_start() */
633 break;
634 case SAA7134_BOARD_AVERMEDIA_M135A:
635 ir_codes = RC_MAP_AVERMEDIA_M135A;
636 mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
637 mask_keyup = 0x0040000;
638 mask_keycode = 0xffff;
639 raw_decode = true;
640 break;
641 case SAA7134_BOARD_AVERMEDIA_M733A:
642 ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
643 mask_keydown = 0x0040000;
644 mask_keyup = 0x0040000;
645 mask_keycode = 0xffff;
646 raw_decode = true;
647 break;
648 case SAA7134_BOARD_AVERMEDIA_777:
649 case SAA7134_BOARD_AVERMEDIA_A16AR:
650 ir_codes = RC_MAP_AVERMEDIA;
651 mask_keycode = 0x02F200;
652 mask_keydown = 0x000400;
653 polling = 50; // ms
654 /* GPIO stuff moved to __saa7134_ir_start() */
655 break;
656 case SAA7134_BOARD_AVERMEDIA_A16D:
657 ir_codes = RC_MAP_AVERMEDIA_A16D;
658 mask_keycode = 0x02F200;
659 mask_keydown = 0x000400;
660 polling = 50; /* ms */
661 /* GPIO stuff moved to __saa7134_ir_start() */
662 break;
663 case SAA7134_BOARD_KWORLD_TERMINATOR:
664 ir_codes = RC_MAP_PIXELVIEW;
665 mask_keycode = 0x00001f;
666 mask_keyup = 0x000060;
667 polling = 50; // ms
668 break;
669 case SAA7134_BOARD_MANLI_MTV001:
670 case SAA7134_BOARD_MANLI_MTV002:
671 ir_codes = RC_MAP_MANLI;
672 mask_keycode = 0x001f00;
673 mask_keyup = 0x004000;
674 polling = 50; /* ms */
675 break;
676 case SAA7134_BOARD_BEHOLD_409FM:
677 case SAA7134_BOARD_BEHOLD_401:
678 case SAA7134_BOARD_BEHOLD_403:
679 case SAA7134_BOARD_BEHOLD_403FM:
680 case SAA7134_BOARD_BEHOLD_405:
681 case SAA7134_BOARD_BEHOLD_405FM:
682 case SAA7134_BOARD_BEHOLD_407:
683 case SAA7134_BOARD_BEHOLD_407FM:
684 case SAA7134_BOARD_BEHOLD_409:
685 case SAA7134_BOARD_BEHOLD_505FM:
686 case SAA7134_BOARD_BEHOLD_505RDS_MK5:
687 case SAA7134_BOARD_BEHOLD_505RDS_MK3:
688 case SAA7134_BOARD_BEHOLD_507_9FM:
689 case SAA7134_BOARD_BEHOLD_507RDS_MK3:
690 case SAA7134_BOARD_BEHOLD_507RDS_MK5:
691 ir_codes = RC_MAP_MANLI;
692 mask_keycode = 0x003f00;
693 mask_keyup = 0x004000;
694 polling = 50; /* ms */
695 break;
696 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
697 ir_codes = RC_MAP_BEHOLD_COLUMBUS;
698 mask_keycode = 0x003f00;
699 mask_keyup = 0x004000;
700 polling = 50; // ms
701 break;
702 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
703 ir_codes = RC_MAP_PCTV_SEDNA;
704 mask_keycode = 0x001f00;
705 mask_keyup = 0x004000;
706 polling = 50; // ms
707 break;
708 case SAA7134_BOARD_GOTVIEW_7135:
709 ir_codes = RC_MAP_GOTVIEW7135;
710 mask_keycode = 0x0003CC;
711 mask_keydown = 0x000010;
712 polling = 5; /* ms */
713 /* GPIO stuff moved to __saa7134_ir_start() */
714 break;
715 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
716 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
717 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
718 ir_codes = RC_MAP_VIDEOMATE_TV_PVR;
719 mask_keycode = 0x00003F;
720 mask_keyup = 0x400000;
721 polling = 50; // ms
722 break;
723 case SAA7134_BOARD_PROTEUS_2309:
724 ir_codes = RC_MAP_PROTEUS_2309;
725 mask_keycode = 0x00007F;
726 mask_keyup = 0x000080;
727 polling = 50; // ms
728 break;
729 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
730 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
731 ir_codes = RC_MAP_VIDEOMATE_TV_PVR;
732 mask_keycode = 0x003F00;
733 mask_keyup = 0x040000;
734 break;
735 case SAA7134_BOARD_FLYDVBS_LR300:
736 case SAA7134_BOARD_FLYDVBT_LR301:
737 case SAA7134_BOARD_FLYDVBTDUO:
738 ir_codes = RC_MAP_FLYDVB;
739 mask_keycode = 0x0001F00;
740 mask_keydown = 0x0040000;
741 break;
742 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
743 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
744 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
745 ir_codes = RC_MAP_ASUS_PC39;
746 mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
747 mask_keyup = 0x0040000;
748 mask_keycode = 0xffff;
749 raw_decode = true;
750 break;
751 case SAA7134_BOARD_ASUSTeK_PS3_100:
752 ir_codes = RC_MAP_ASUS_PS3_100;
753 mask_keydown = 0x0040000;
754 mask_keyup = 0x0040000;
755 mask_keycode = 0xffff;
756 raw_decode = true;
757 break;
758 case SAA7134_BOARD_ENCORE_ENLTV:
759 case SAA7134_BOARD_ENCORE_ENLTV_FM:
760 ir_codes = RC_MAP_ENCORE_ENLTV;
761 mask_keycode = 0x00007f;
762 mask_keyup = 0x040000;
763 polling = 50; // ms
764 break;
765 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
766 case SAA7134_BOARD_ENCORE_ENLTV_FM3:
767 ir_codes = RC_MAP_ENCORE_ENLTV_FM53;
768 mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
769 mask_keyup = 0x0040000;
770 mask_keycode = 0xffff;
771 raw_decode = true;
772 break;
773 case SAA7134_BOARD_10MOONSTVMASTER3:
774 ir_codes = RC_MAP_ENCORE_ENLTV;
775 mask_keycode = 0x5f80000;
776 mask_keyup = 0x8000000;
777 polling = 50; //ms
778 break;
779 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
780 ir_codes = RC_MAP_GENIUS_TVGO_A11MCE;
781 mask_keycode = 0xff;
782 mask_keydown = 0xf00000;
783 polling = 50; /* ms */
784 break;
785 case SAA7134_BOARD_REAL_ANGEL_220:
786 ir_codes = RC_MAP_REAL_AUDIO_220_32_KEYS;
787 mask_keycode = 0x3f00;
788 mask_keyup = 0x4000;
789 polling = 50; /* ms */
790 break;
791 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
792 ir_codes = RC_MAP_KWORLD_PLUS_TV_ANALOG;
793 mask_keycode = 0x7f;
794 polling = 40; /* ms */
795 break;
796 case SAA7134_BOARD_VIDEOMATE_S350:
797 ir_codes = RC_MAP_VIDEOMATE_S350;
798 mask_keycode = 0x003f00;
799 mask_keydown = 0x040000;
800 break;
801 case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
802 ir_codes = RC_MAP_WINFAST;
803 mask_keycode = 0x5f00;
804 mask_keyup = 0x020000;
805 polling = 50; /* ms */
806 break;
807 case SAA7134_BOARD_VIDEOMATE_M1F:
808 ir_codes = RC_MAP_VIDEOMATE_K100;
809 mask_keycode = 0x0ff00;
810 mask_keyup = 0x040000;
811 break;
812 case SAA7134_BOARD_HAUPPAUGE_HVR1150:
813 case SAA7134_BOARD_HAUPPAUGE_HVR1120:
814 ir_codes = RC_MAP_HAUPPAUGE;
815 mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
816 mask_keyup = 0x0040000;
817 mask_keycode = 0xffff;
818 raw_decode = true;
819 break;
820 }
821 if (NULL == ir_codes) {
822 printk("%s: Oops: IR config error [card=%d]\n",
823 dev->name, dev->board);
824 return -ENODEV;
825 }
826
827 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
828 rc = rc_allocate_device();
829 if (!ir || !rc) {
830 err = -ENOMEM;
831 goto err_out_free;
832 }
833
834 ir->dev = rc;
835 dev->remote = ir;
836
837 /* init hardware-specific stuff */
838 ir->mask_keycode = mask_keycode;
839 ir->mask_keydown = mask_keydown;
840 ir->mask_keyup = mask_keyup;
841 ir->polling = polling;
842 ir->raw_decode = raw_decode;
843
844 /* init input device */
845 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
846 saa7134_boards[dev->board].name);
847 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
848 pci_name(dev->pci));
849
850 rc->priv = dev;
851 rc->open = saa7134_ir_open;
852 rc->close = saa7134_ir_close;
853 if (raw_decode)
854 rc->driver_type = RC_DRIVER_IR_RAW;
855
856 rc->input_name = ir->name;
857 rc->input_phys = ir->phys;
858 rc->input_id.bustype = BUS_PCI;
859 rc->input_id.version = 1;
860 if (dev->pci->subsystem_vendor) {
861 rc->input_id.vendor = dev->pci->subsystem_vendor;
862 rc->input_id.product = dev->pci->subsystem_device;
863 } else {
864 rc->input_id.vendor = dev->pci->vendor;
865 rc->input_id.product = dev->pci->device;
866 }
867 rc->dev.parent = &dev->pci->dev;
868 rc->map_name = ir_codes;
869 rc->driver_name = MODULE_NAME;
870
871 err = rc_register_device(rc);
872 if (err)
873 goto err_out_free;
874
875 return 0;
876
877err_out_free:
878 rc_free_device(rc);
879 dev->remote = NULL;
880 kfree(ir);
881 return err;
882}
883
884void saa7134_input_fini(struct saa7134_dev *dev)
885{
886 if (NULL == dev->remote)
887 return;
888
889 saa7134_ir_stop(dev);
890 rc_unregister_device(dev->remote->dev);
891 kfree(dev->remote);
892 dev->remote = NULL;
893}
894
895void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
896{
897 struct i2c_board_info info;
898 struct i2c_msg msg_msi = {
899 .addr = 0x50,
900 .flags = I2C_M_RD,
901 .len = 0,
902 .buf = NULL,
903 };
904 int rc;
905
906 if (disable_ir) {
907 dprintk("IR has been disabled, not probing for i2c remote\n");
908 return;
909 }
910
911 memset(&info, 0, sizeof(struct i2c_board_info));
912 memset(&dev->init_data, 0, sizeof(dev->init_data));
913 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
914
915 switch (dev->board) {
916 case SAA7134_BOARD_PINNACLE_PCTV_110i:
917 case SAA7134_BOARD_PINNACLE_PCTV_310i:
918 dev->init_data.name = "Pinnacle PCTV";
919 if (pinnacle_remote == 0) {
920 dev->init_data.get_key = get_key_pinnacle_color;
921 dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
922 info.addr = 0x47;
923 } else {
924 dev->init_data.get_key = get_key_pinnacle_grey;
925 dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
926 info.addr = 0x47;
927 }
928 break;
929 case SAA7134_BOARD_UPMOST_PURPLE_TV:
930 dev->init_data.name = "Purple TV";
931 dev->init_data.get_key = get_key_purpletv;
932 dev->init_data.ir_codes = RC_MAP_PURPLETV;
933 info.addr = 0x7a;
934 break;
935 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
936 dev->init_data.name = "MSI TV@nywhere Plus";
937 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
938 dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
939 /*
940 * MSI TV@nyware Plus requires more frequent polling
941 * otherwise it will miss some keypresses
942 */
943 dev->init_data.polling_interval = 50;
944 info.addr = 0x30;
945 /* MSI TV@nywhere Plus controller doesn't seem to
946 respond to probes unless we read something from
947 an existing device. Weird...
948 REVISIT: might no longer be needed */
949 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
950 dprintk("probe 0x%02x @ %s: %s\n",
951 msg_msi.addr, dev->i2c_adap.name,
952 (1 == rc) ? "yes" : "no");
953 break;
954 case SAA7134_BOARD_KWORLD_PC150U:
955 /* copied and modified from MSI TV@nywhere Plus */
956 dev->init_data.name = "Kworld PC150-U";
957 dev->init_data.get_key = get_key_kworld_pc150u;
958 dev->init_data.ir_codes = RC_MAP_KWORLD_PC150U;
959 info.addr = 0x30;
960 /* MSI TV@nywhere Plus controller doesn't seem to
961 respond to probes unless we read something from
962 an existing device. Weird...
963 REVISIT: might no longer be needed */
964 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
965 dprintk("probe 0x%02x @ %s: %s\n",
966 msg_msi.addr, dev->i2c_adap.name,
967 (1 == rc) ? "yes" : "no");
968 break;
969 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
970 dev->init_data.name = "HVR 1110";
971 dev->init_data.get_key = get_key_hvr1110;
972 dev->init_data.ir_codes = RC_MAP_HAUPPAUGE;
973 info.addr = 0x71;
974 break;
975 case SAA7134_BOARD_BEHOLD_607FM_MK3:
976 case SAA7134_BOARD_BEHOLD_607FM_MK5:
977 case SAA7134_BOARD_BEHOLD_609FM_MK3:
978 case SAA7134_BOARD_BEHOLD_609FM_MK5:
979 case SAA7134_BOARD_BEHOLD_607RDS_MK3:
980 case SAA7134_BOARD_BEHOLD_607RDS_MK5:
981 case SAA7134_BOARD_BEHOLD_609RDS_MK3:
982 case SAA7134_BOARD_BEHOLD_609RDS_MK5:
983 case SAA7134_BOARD_BEHOLD_M6:
984 case SAA7134_BOARD_BEHOLD_M63:
985 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
986 case SAA7134_BOARD_BEHOLD_H6:
987 case SAA7134_BOARD_BEHOLD_X7:
988 case SAA7134_BOARD_BEHOLD_H7:
989 case SAA7134_BOARD_BEHOLD_A7:
990 dev->init_data.name = "BeholdTV";
991 dev->init_data.get_key = get_key_beholdm6xx;
992 dev->init_data.ir_codes = RC_MAP_BEHOLD;
993 dev->init_data.type = RC_TYPE_NEC;
994 info.addr = 0x2d;
995 break;
996 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
997 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
998 info.addr = 0x40;
999 break;
1000 case SAA7134_BOARD_FLYDVB_TRIO:
1001 dev->init_data.name = "FlyDVB Trio";
1002 dev->init_data.get_key = get_key_flydvb_trio;
1003 dev->init_data.ir_codes = RC_MAP_FLYDVB;
1004 info.addr = 0x0b;
1005 break;
1006 default:
1007 dprintk("No I2C IR support for board %x\n", dev->board);
1008 return;
1009 }
1010
1011 if (dev->init_data.name)
1012 info.platform_data = &dev->init_data;
1013 i2c_new_device(&dev->i2c_adap, &info);
1014}
1015
1016static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
1017{
1018 struct saa7134_card_ir *ir = dev->remote;
1019 unsigned long timeout;
1020 int space;
1021
1022 /* Generate initial event */
1023 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1024 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1025 space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1026 ir_raw_event_store_edge(dev->remote->dev, space ? IR_SPACE : IR_PULSE);
1027
1028 /*
1029 * Wait 15 ms from the start of the first IR event before processing
1030 * the event. This time is enough for NEC protocol. May need adjustments
1031 * to work with other protocols.
1032 */
1033 smp_mb();
1034
1035 if (!timer_pending(&ir->timer)) {
1036 timeout = jiffies + msecs_to_jiffies(15);
1037 mod_timer(&ir->timer, timeout);
1038 }
1039
1040 return 1;
1041}
diff --git a/drivers/media/pci/saa7134/saa7134-reg.h b/drivers/media/pci/saa7134/saa7134-reg.h
new file mode 100644
index 000000000000..e7e0af101fa7
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-reg.h
@@ -0,0 +1,378 @@
1/*
2 *
3 * philips saa7134 registers
4 */
5
6/* ------------------------------------------------------------------ */
7/*
8 * PCI ID's
9 */
10#ifndef PCI_DEVICE_ID_PHILIPS_SAA7130
11# define PCI_DEVICE_ID_PHILIPS_SAA7130 0x7130
12#endif
13#ifndef PCI_DEVICE_ID_PHILIPS_SAA7133
14# define PCI_DEVICE_ID_PHILIPS_SAA7133 0x7133
15#endif
16#ifndef PCI_DEVICE_ID_PHILIPS_SAA7134
17# define PCI_DEVICE_ID_PHILIPS_SAA7134 0x7134
18#endif
19#ifndef PCI_DEVICE_ID_PHILIPS_SAA7135
20# define PCI_DEVICE_ID_PHILIPS_SAA7135 0x7135
21#endif
22
23/* ------------------------------------------------------------------ */
24/*
25 * registers -- 32 bit
26 */
27
28/* DMA channels, n = 0 ... 6 */
29#define SAA7134_RS_BA1(n) ((0x200 >> 2) + 4*n)
30#define SAA7134_RS_BA2(n) ((0x204 >> 2) + 4*n)
31#define SAA7134_RS_PITCH(n) ((0x208 >> 2) + 4*n)
32#define SAA7134_RS_CONTROL(n) ((0x20c >> 2) + 4*n)
33#define SAA7134_RS_CONTROL_WSWAP (0x01 << 25)
34#define SAA7134_RS_CONTROL_BSWAP (0x01 << 24)
35#define SAA7134_RS_CONTROL_BURST_2 (0x01 << 21)
36#define SAA7134_RS_CONTROL_BURST_4 (0x02 << 21)
37#define SAA7134_RS_CONTROL_BURST_8 (0x03 << 21)
38#define SAA7134_RS_CONTROL_BURST_16 (0x04 << 21)
39#define SAA7134_RS_CONTROL_BURST_32 (0x05 << 21)
40#define SAA7134_RS_CONTROL_BURST_64 (0x06 << 21)
41#define SAA7134_RS_CONTROL_BURST_MAX (0x07 << 21)
42#define SAA7134_RS_CONTROL_ME (0x01 << 20)
43#define SAA7134_FIFO_SIZE (0x2a0 >> 2)
44#define SAA7134_THRESHOULD (0x2a4 >> 2)
45
46#define SAA7133_NUM_SAMPLES (0x588 >> 2)
47#define SAA7133_AUDIO_CHANNEL (0x58c >> 2)
48#define SAA7133_AUDIO_FORMAT (0x58f >> 2)
49#define SAA7133_DIGITAL_OUTPUT_SEL1 (0x46c >> 2)
50#define SAA7133_DIGITAL_OUTPUT_SEL2 (0x470 >> 2)
51#define SAA7133_DIGITAL_INPUT_XBAR1 (0x464 >> 2)
52#define SAA7133_ANALOG_IO_SELECT (0x594 >> 2)
53
54/* main control */
55#define SAA7134_MAIN_CTRL (0x2a8 >> 2)
56#define SAA7134_MAIN_CTRL_VPLLE (1 << 15)
57#define SAA7134_MAIN_CTRL_APLLE (1 << 14)
58#define SAA7134_MAIN_CTRL_EXOSC (1 << 13)
59#define SAA7134_MAIN_CTRL_EVFE1 (1 << 12)
60#define SAA7134_MAIN_CTRL_EVFE2 (1 << 11)
61#define SAA7134_MAIN_CTRL_ESFE (1 << 10)
62#define SAA7134_MAIN_CTRL_EBADC (1 << 9)
63#define SAA7134_MAIN_CTRL_EBDAC (1 << 8)
64#define SAA7134_MAIN_CTRL_TE6 (1 << 6)
65#define SAA7134_MAIN_CTRL_TE5 (1 << 5)
66#define SAA7134_MAIN_CTRL_TE4 (1 << 4)
67#define SAA7134_MAIN_CTRL_TE3 (1 << 3)
68#define SAA7134_MAIN_CTRL_TE2 (1 << 2)
69#define SAA7134_MAIN_CTRL_TE1 (1 << 1)
70#define SAA7134_MAIN_CTRL_TE0 (1 << 0)
71
72/* DMA status */
73#define SAA7134_DMA_STATUS (0x2ac >> 2)
74
75/* audio / video status */
76#define SAA7134_AV_STATUS (0x2c0 >> 2)
77#define SAA7134_AV_STATUS_STEREO (1 << 17)
78#define SAA7134_AV_STATUS_DUAL (1 << 16)
79#define SAA7134_AV_STATUS_PILOT (1 << 15)
80#define SAA7134_AV_STATUS_SMB (1 << 14)
81#define SAA7134_AV_STATUS_DMB (1 << 13)
82#define SAA7134_AV_STATUS_VDSP (1 << 12)
83#define SAA7134_AV_STATUS_IIC_STATUS (3 << 10)
84#define SAA7134_AV_STATUS_MVM (7 << 7)
85#define SAA7134_AV_STATUS_FIDT (1 << 6)
86#define SAA7134_AV_STATUS_INTL (1 << 5)
87#define SAA7134_AV_STATUS_RDCAP (1 << 4)
88#define SAA7134_AV_STATUS_PWR_ON (1 << 3)
89#define SAA7134_AV_STATUS_LOAD_ERR (1 << 2)
90#define SAA7134_AV_STATUS_TRIG_ERR (1 << 1)
91#define SAA7134_AV_STATUS_CONF_ERR (1 << 0)
92
93/* interrupt */
94#define SAA7134_IRQ1 (0x2c4 >> 2)
95#define SAA7134_IRQ1_INTE_RA3_1 (1 << 25)
96#define SAA7134_IRQ1_INTE_RA3_0 (1 << 24)
97#define SAA7134_IRQ1_INTE_RA2_3 (1 << 19)
98#define SAA7134_IRQ1_INTE_RA2_2 (1 << 18)
99#define SAA7134_IRQ1_INTE_RA2_1 (1 << 17)
100#define SAA7134_IRQ1_INTE_RA2_0 (1 << 16)
101#define SAA7134_IRQ1_INTE_RA1_3 (1 << 11)
102#define SAA7134_IRQ1_INTE_RA1_2 (1 << 10)
103#define SAA7134_IRQ1_INTE_RA1_1 (1 << 9)
104#define SAA7134_IRQ1_INTE_RA1_0 (1 << 8)
105#define SAA7134_IRQ1_INTE_RA0_7 (1 << 7)
106#define SAA7134_IRQ1_INTE_RA0_6 (1 << 6)
107#define SAA7134_IRQ1_INTE_RA0_5 (1 << 5)
108#define SAA7134_IRQ1_INTE_RA0_4 (1 << 4)
109#define SAA7134_IRQ1_INTE_RA0_3 (1 << 3)
110#define SAA7134_IRQ1_INTE_RA0_2 (1 << 2)
111#define SAA7134_IRQ1_INTE_RA0_1 (1 << 1)
112#define SAA7134_IRQ1_INTE_RA0_0 (1 << 0)
113
114#define SAA7134_IRQ2 (0x2c8 >> 2)
115#define SAA7134_IRQ2_INTE_GPIO23_N (1 << 17) /* negative edge */
116#define SAA7134_IRQ2_INTE_GPIO23_P (1 << 16) /* positive edge */
117#define SAA7134_IRQ2_INTE_GPIO22_N (1 << 15) /* negative edge */
118#define SAA7134_IRQ2_INTE_GPIO22_P (1 << 14) /* positive edge */
119#define SAA7134_IRQ2_INTE_GPIO18_N (1 << 13) /* negative edge */
120#define SAA7134_IRQ2_INTE_GPIO18_P (1 << 12) /* positive edge */
121#define SAA7134_IRQ2_INTE_GPIO16_N (1 << 11) /* negative edge */
122#define SAA7134_IRQ2_INTE_GPIO16_P (1 << 10) /* positive edge */
123#define SAA7134_IRQ2_INTE_SC2 (1 << 9)
124#define SAA7134_IRQ2_INTE_SC1 (1 << 8)
125#define SAA7134_IRQ2_INTE_SC0 (1 << 7)
126#define SAA7134_IRQ2_INTE_DEC4 (1 << 6)
127#define SAA7134_IRQ2_INTE_DEC3 (1 << 5)
128#define SAA7134_IRQ2_INTE_DEC2 (1 << 4)
129#define SAA7134_IRQ2_INTE_DEC1 (1 << 3)
130#define SAA7134_IRQ2_INTE_DEC0 (1 << 2)
131#define SAA7134_IRQ2_INTE_PE (1 << 1)
132#define SAA7134_IRQ2_INTE_AR (1 << 0)
133
134#define SAA7134_IRQ_REPORT (0x2cc >> 2)
135#define SAA7134_IRQ_REPORT_GPIO23 (1 << 17)
136#define SAA7134_IRQ_REPORT_GPIO22 (1 << 16)
137#define SAA7134_IRQ_REPORT_GPIO18 (1 << 15)
138#define SAA7134_IRQ_REPORT_GPIO16 (1 << 14)
139#define SAA7134_IRQ_REPORT_LOAD_ERR (1 << 13)
140#define SAA7134_IRQ_REPORT_CONF_ERR (1 << 12)
141#define SAA7134_IRQ_REPORT_TRIG_ERR (1 << 11)
142#define SAA7134_IRQ_REPORT_MMC (1 << 10)
143#define SAA7134_IRQ_REPORT_FIDT (1 << 9)
144#define SAA7134_IRQ_REPORT_INTL (1 << 8)
145#define SAA7134_IRQ_REPORT_RDCAP (1 << 7)
146#define SAA7134_IRQ_REPORT_PWR_ON (1 << 6)
147#define SAA7134_IRQ_REPORT_PE (1 << 5)
148#define SAA7134_IRQ_REPORT_AR (1 << 4)
149#define SAA7134_IRQ_REPORT_DONE_RA3 (1 << 3)
150#define SAA7134_IRQ_REPORT_DONE_RA2 (1 << 2)
151#define SAA7134_IRQ_REPORT_DONE_RA1 (1 << 1)
152#define SAA7134_IRQ_REPORT_DONE_RA0 (1 << 0)
153#define SAA7134_IRQ_STATUS (0x2d0 >> 2)
154
155
156/* ------------------------------------------------------------------ */
157/*
158 * registers -- 8 bit
159 */
160
161/* video decoder */
162#define SAA7134_INCR_DELAY 0x101
163#define SAA7134_ANALOG_IN_CTRL1 0x102
164#define SAA7134_ANALOG_IN_CTRL2 0x103
165#define SAA7134_ANALOG_IN_CTRL3 0x104
166#define SAA7134_ANALOG_IN_CTRL4 0x105
167#define SAA7134_HSYNC_START 0x106
168#define SAA7134_HSYNC_STOP 0x107
169#define SAA7134_SYNC_CTRL 0x108
170#define SAA7134_LUMA_CTRL 0x109
171#define SAA7134_DEC_LUMA_BRIGHT 0x10a
172#define SAA7134_DEC_LUMA_CONTRAST 0x10b
173#define SAA7134_DEC_CHROMA_SATURATION 0x10c
174#define SAA7134_DEC_CHROMA_HUE 0x10d
175#define SAA7134_CHROMA_CTRL1 0x10e
176#define SAA7134_CHROMA_GAIN 0x10f
177#define SAA7134_CHROMA_CTRL2 0x110
178#define SAA7134_MODE_DELAY_CTRL 0x111
179
180#define SAA7134_ANALOG_ADC 0x114
181#define SAA7134_VGATE_START 0x115
182#define SAA7134_VGATE_STOP 0x116
183#define SAA7134_MISC_VGATE_MSB 0x117
184#define SAA7134_RAW_DATA_GAIN 0x118
185#define SAA7134_RAW_DATA_OFFSET 0x119
186#define SAA7134_STATUS_VIDEO1 0x11e
187#define SAA7134_STATUS_VIDEO2 0x11f
188
189/* video scaler */
190#define SAA7134_SOURCE_TIMING1 0x000
191#define SAA7134_SOURCE_TIMING2 0x001
192#define SAA7134_REGION_ENABLE 0x004
193#define SAA7134_SCALER_STATUS0 0x006
194#define SAA7134_SCALER_STATUS1 0x007
195#define SAA7134_START_GREEN 0x00c
196#define SAA7134_START_BLUE 0x00d
197#define SAA7134_START_RED 0x00e
198#define SAA7134_GREEN_PATH(x) (0x010 +x)
199#define SAA7134_BLUE_PATH(x) (0x020 +x)
200#define SAA7134_RED_PATH(x) (0x030 +x)
201
202#define TASK_A 0x040
203#define TASK_B 0x080
204#define SAA7134_TASK_CONDITIONS(t) (0x000 +t)
205#define SAA7134_FIELD_HANDLING(t) (0x001 +t)
206#define SAA7134_DATA_PATH(t) (0x002 +t)
207#define SAA7134_VBI_H_START1(t) (0x004 +t)
208#define SAA7134_VBI_H_START2(t) (0x005 +t)
209#define SAA7134_VBI_H_STOP1(t) (0x006 +t)
210#define SAA7134_VBI_H_STOP2(t) (0x007 +t)
211#define SAA7134_VBI_V_START1(t) (0x008 +t)
212#define SAA7134_VBI_V_START2(t) (0x009 +t)
213#define SAA7134_VBI_V_STOP1(t) (0x00a +t)
214#define SAA7134_VBI_V_STOP2(t) (0x00b +t)
215#define SAA7134_VBI_H_LEN1(t) (0x00c +t)
216#define SAA7134_VBI_H_LEN2(t) (0x00d +t)
217#define SAA7134_VBI_V_LEN1(t) (0x00e +t)
218#define SAA7134_VBI_V_LEN2(t) (0x00f +t)
219
220#define SAA7134_VIDEO_H_START1(t) (0x014 +t)
221#define SAA7134_VIDEO_H_START2(t) (0x015 +t)
222#define SAA7134_VIDEO_H_STOP1(t) (0x016 +t)
223#define SAA7134_VIDEO_H_STOP2(t) (0x017 +t)
224#define SAA7134_VIDEO_V_START1(t) (0x018 +t)
225#define SAA7134_VIDEO_V_START2(t) (0x019 +t)
226#define SAA7134_VIDEO_V_STOP1(t) (0x01a +t)
227#define SAA7134_VIDEO_V_STOP2(t) (0x01b +t)
228#define SAA7134_VIDEO_PIXELS1(t) (0x01c +t)
229#define SAA7134_VIDEO_PIXELS2(t) (0x01d +t)
230#define SAA7134_VIDEO_LINES1(t) (0x01e +t)
231#define SAA7134_VIDEO_LINES2(t) (0x01f +t)
232
233#define SAA7134_H_PRESCALE(t) (0x020 +t)
234#define SAA7134_ACC_LENGTH(t) (0x021 +t)
235#define SAA7134_LEVEL_CTRL(t) (0x022 +t)
236#define SAA7134_FIR_PREFILTER_CTRL(t) (0x023 +t)
237#define SAA7134_LUMA_BRIGHT(t) (0x024 +t)
238#define SAA7134_LUMA_CONTRAST(t) (0x025 +t)
239#define SAA7134_CHROMA_SATURATION(t) (0x026 +t)
240#define SAA7134_VBI_H_SCALE_INC1(t) (0x028 +t)
241#define SAA7134_VBI_H_SCALE_INC2(t) (0x029 +t)
242#define SAA7134_VBI_PHASE_OFFSET_LUMA(t) (0x02a +t)
243#define SAA7134_VBI_PHASE_OFFSET_CHROMA(t) (0x02b +t)
244#define SAA7134_H_SCALE_INC1(t) (0x02c +t)
245#define SAA7134_H_SCALE_INC2(t) (0x02d +t)
246#define SAA7134_H_PHASE_OFF_LUMA(t) (0x02e +t)
247#define SAA7134_H_PHASE_OFF_CHROMA(t) (0x02f +t)
248#define SAA7134_V_SCALE_RATIO1(t) (0x030 +t)
249#define SAA7134_V_SCALE_RATIO2(t) (0x031 +t)
250#define SAA7134_V_FILTER(t) (0x032 +t)
251#define SAA7134_V_PHASE_OFFSET0(t) (0x034 +t)
252#define SAA7134_V_PHASE_OFFSET1(t) (0x035 +t)
253#define SAA7134_V_PHASE_OFFSET2(t) (0x036 +t)
254#define SAA7134_V_PHASE_OFFSET3(t) (0x037 +t)
255
256/* clipping & dma */
257#define SAA7134_OFMT_VIDEO_A 0x300
258#define SAA7134_OFMT_DATA_A 0x301
259#define SAA7134_OFMT_VIDEO_B 0x302
260#define SAA7134_OFMT_DATA_B 0x303
261#define SAA7134_ALPHA_NOCLIP 0x304
262#define SAA7134_ALPHA_CLIP 0x305
263#define SAA7134_UV_PIXEL 0x308
264#define SAA7134_CLIP_RED 0x309
265#define SAA7134_CLIP_GREEN 0x30a
266#define SAA7134_CLIP_BLUE 0x30b
267
268/* i2c bus */
269#define SAA7134_I2C_ATTR_STATUS 0x180
270#define SAA7134_I2C_DATA 0x181
271#define SAA7134_I2C_CLOCK_SELECT 0x182
272#define SAA7134_I2C_TIMER 0x183
273
274/* audio */
275#define SAA7134_NICAM_ADD_DATA1 0x140
276#define SAA7134_NICAM_ADD_DATA2 0x141
277#define SAA7134_NICAM_STATUS 0x142
278#define SAA7134_AUDIO_STATUS 0x143
279#define SAA7134_NICAM_ERROR_COUNT 0x144
280#define SAA7134_IDENT_SIF 0x145
281#define SAA7134_LEVEL_READOUT1 0x146
282#define SAA7134_LEVEL_READOUT2 0x147
283#define SAA7134_NICAM_ERROR_LOW 0x148
284#define SAA7134_NICAM_ERROR_HIGH 0x149
285#define SAA7134_DCXO_IDENT_CTRL 0x14a
286#define SAA7134_DEMODULATOR 0x14b
287#define SAA7134_AGC_GAIN_SELECT 0x14c
288#define SAA7134_CARRIER1_FREQ0 0x150
289#define SAA7134_CARRIER1_FREQ1 0x151
290#define SAA7134_CARRIER1_FREQ2 0x152
291#define SAA7134_CARRIER2_FREQ0 0x154
292#define SAA7134_CARRIER2_FREQ1 0x155
293#define SAA7134_CARRIER2_FREQ2 0x156
294#define SAA7134_NUM_SAMPLES0 0x158
295#define SAA7134_NUM_SAMPLES1 0x159
296#define SAA7134_NUM_SAMPLES2 0x15a
297#define SAA7134_AUDIO_FORMAT_CTRL 0x15b
298#define SAA7134_MONITOR_SELECT 0x160
299#define SAA7134_FM_DEEMPHASIS 0x161
300#define SAA7134_FM_DEMATRIX 0x162
301#define SAA7134_CHANNEL1_LEVEL 0x163
302#define SAA7134_CHANNEL2_LEVEL 0x164
303#define SAA7134_NICAM_CONFIG 0x165
304#define SAA7134_NICAM_LEVEL_ADJUST 0x166
305#define SAA7134_STEREO_DAC_OUTPUT_SELECT 0x167
306#define SAA7134_I2S_OUTPUT_FORMAT 0x168
307#define SAA7134_I2S_OUTPUT_SELECT 0x169
308#define SAA7134_I2S_OUTPUT_LEVEL 0x16a
309#define SAA7134_DSP_OUTPUT_SELECT 0x16b
310#define SAA7134_AUDIO_MUTE_CTRL 0x16c
311#define SAA7134_SIF_SAMPLE_FREQ 0x16d
312#define SAA7134_ANALOG_IO_SELECT 0x16e
313#define SAA7134_AUDIO_CLOCK0 0x170
314#define SAA7134_AUDIO_CLOCK1 0x171
315#define SAA7134_AUDIO_CLOCK2 0x172
316#define SAA7134_AUDIO_PLL_CTRL 0x173
317#define SAA7134_AUDIO_CLOCKS_PER_FIELD0 0x174
318#define SAA7134_AUDIO_CLOCKS_PER_FIELD1 0x175
319#define SAA7134_AUDIO_CLOCKS_PER_FIELD2 0x176
320
321/* video port output */
322#define SAA7134_VIDEO_PORT_CTRL0 0x190
323#define SAA7134_VIDEO_PORT_CTRL1 0x191
324#define SAA7134_VIDEO_PORT_CTRL2 0x192
325#define SAA7134_VIDEO_PORT_CTRL3 0x193
326#define SAA7134_VIDEO_PORT_CTRL4 0x194
327#define SAA7134_VIDEO_PORT_CTRL5 0x195
328#define SAA7134_VIDEO_PORT_CTRL6 0x196
329#define SAA7134_VIDEO_PORT_CTRL7 0x197
330#define SAA7134_VIDEO_PORT_CTRL8 0x198
331
332/* transport stream interface */
333#define SAA7134_TS_PARALLEL 0x1a0
334#define SAA7134_TS_PARALLEL_SERIAL 0x1a1
335#define SAA7134_TS_SERIAL0 0x1a2
336#define SAA7134_TS_SERIAL1 0x1a3
337#define SAA7134_TS_DMA0 0x1a4
338#define SAA7134_TS_DMA1 0x1a5
339#define SAA7134_TS_DMA2 0x1a6
340
341/* GPIO Controls */
342#define SAA7134_GPIO_GPRESCAN 0x80
343#define SAA7134_GPIO_27_25 0x0E
344
345#define SAA7134_GPIO_GPMODE0 0x1B0
346#define SAA7134_GPIO_GPMODE1 0x1B1
347#define SAA7134_GPIO_GPMODE2 0x1B2
348#define SAA7134_GPIO_GPMODE3 0x1B3
349#define SAA7134_GPIO_GPSTATUS0 0x1B4
350#define SAA7134_GPIO_GPSTATUS1 0x1B5
351#define SAA7134_GPIO_GPSTATUS2 0x1B6
352#define SAA7134_GPIO_GPSTATUS3 0x1B7
353
354/* I2S output */
355#define SAA7134_I2S_AUDIO_OUTPUT 0x1c0
356
357/* test modes */
358#define SAA7134_SPECIAL_MODE 0x1d0
359#define SAA7134_PRODUCTION_TEST_MODE 0x1d1
360
361/* audio -- saa7133 + saa7135 only */
362#define SAA7135_DSP_RWSTATE 0x580
363#define SAA7135_DSP_RWSTATE_ERR (1 << 3)
364#define SAA7135_DSP_RWSTATE_IDA (1 << 2)
365#define SAA7135_DSP_RWSTATE_RDB (1 << 1)
366#define SAA7135_DSP_RWSTATE_WRR (1 << 0)
367
368#define SAA7135_DSP_RWCLEAR 0x586
369#define SAA7135_DSP_RWCLEAR_RERR 1
370
371#define SAA7133_I2S_AUDIO_CONTROL 0x591
372/* ------------------------------------------------------------------ */
373/*
374 * Local variables:
375 * c-basic-offset: 8
376 * End:
377 */
378
diff --git a/drivers/media/pci/saa7134/saa7134-ts.c b/drivers/media/pci/saa7134/saa7134-ts.c
new file mode 100644
index 000000000000..2e3f4b412d8c
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-ts.c
@@ -0,0 +1,327 @@
1/*
2 *
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
5 *
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/delay.h>
28
29#include "saa7134-reg.h"
30#include "saa7134.h"
31
32/* ------------------------------------------------------------------ */
33
34static unsigned int ts_debug;
35module_param(ts_debug, int, 0644);
36MODULE_PARM_DESC(ts_debug,"enable debug messages [ts]");
37
38#define dprintk(fmt, arg...) if (ts_debug) \
39 printk(KERN_DEBUG "%s/ts: " fmt, dev->name , ## arg)
40
41/* ------------------------------------------------------------------ */
42
43static int buffer_activate(struct saa7134_dev *dev,
44 struct saa7134_buf *buf,
45 struct saa7134_buf *next)
46{
47
48 dprintk("buffer_activate [%p]",buf);
49 buf->vb.state = VIDEOBUF_ACTIVE;
50 buf->top_seen = 0;
51
52 if (NULL == next)
53 next = buf;
54 if (V4L2_FIELD_TOP == buf->vb.field) {
55 dprintk("- [top] buf=%p next=%p\n",buf,next);
56 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(buf));
57 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(next));
58 } else {
59 dprintk("- [bottom] buf=%p next=%p\n",buf,next);
60 saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(next));
61 saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(buf));
62 }
63
64 /* start DMA */
65 saa7134_set_dmabits(dev);
66
67 mod_timer(&dev->ts_q.timeout, jiffies+TS_BUFFER_TIMEOUT);
68
69 if (!dev->ts_started)
70 saa7134_ts_start(dev);
71
72 return 0;
73}
74
75static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
76 enum v4l2_field field)
77{
78 struct saa7134_dev *dev = q->priv_data;
79 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
80 unsigned int lines, llength, size;
81 int err;
82
83 dprintk("buffer_prepare [%p,%s]\n",buf,v4l2_field_names[field]);
84
85 llength = TS_PACKET_SIZE;
86 lines = dev->ts.nr_packets;
87
88 size = lines * llength;
89 if (0 != buf->vb.baddr && buf->vb.bsize < size)
90 return -EINVAL;
91
92 if (buf->vb.size != size) {
93 saa7134_dma_free(q,buf);
94 }
95
96 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
97
98 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
99
100 dprintk("buffer_prepare: needs_init\n");
101
102 buf->vb.width = llength;
103 buf->vb.height = lines;
104 buf->vb.size = size;
105 buf->pt = &dev->ts.pt_ts;
106
107 err = videobuf_iolock(q,&buf->vb,NULL);
108 if (err)
109 goto oops;
110 err = saa7134_pgtable_build(dev->pci,buf->pt,
111 dma->sglist,
112 dma->sglen,
113 saa7134_buffer_startpage(buf));
114 if (err)
115 goto oops;
116 }
117
118 buf->vb.state = VIDEOBUF_PREPARED;
119 buf->activate = buffer_activate;
120 buf->vb.field = field;
121 return 0;
122
123 oops:
124 saa7134_dma_free(q,buf);
125 return err;
126}
127
128static int
129buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
130{
131 struct saa7134_dev *dev = q->priv_data;
132
133 *size = TS_PACKET_SIZE * dev->ts.nr_packets;
134 if (0 == *count)
135 *count = dev->ts.nr_bufs;
136 *count = saa7134_buffer_count(*size,*count);
137
138 return 0;
139}
140
141static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
142{
143 struct saa7134_dev *dev = q->priv_data;
144 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
145
146 saa7134_buffer_queue(dev,&dev->ts_q,buf);
147}
148
149static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
150{
151 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
152 struct saa7134_dev *dev = q->priv_data;
153
154 if (dev->ts_started)
155 saa7134_ts_stop(dev);
156
157 saa7134_dma_free(q,buf);
158}
159
160struct videobuf_queue_ops saa7134_ts_qops = {
161 .buf_setup = buffer_setup,
162 .buf_prepare = buffer_prepare,
163 .buf_queue = buffer_queue,
164 .buf_release = buffer_release,
165};
166EXPORT_SYMBOL_GPL(saa7134_ts_qops);
167
168/* ----------------------------------------------------------- */
169/* exported stuff */
170
171static unsigned int tsbufs = 8;
172module_param(tsbufs, int, 0444);
173MODULE_PARM_DESC(tsbufs, "number of ts buffers for read/write IO, range 2-32");
174
175static unsigned int ts_nr_packets = 64;
176module_param(ts_nr_packets, int, 0444);
177MODULE_PARM_DESC(ts_nr_packets,"size of a ts buffers (in ts packets)");
178
179int saa7134_ts_init_hw(struct saa7134_dev *dev)
180{
181 /* deactivate TS softreset */
182 saa_writeb(SAA7134_TS_SERIAL1, 0x00);
183 /* TSSOP high active, TSVAL high active, TSLOCK ignored */
184 saa_writeb(SAA7134_TS_PARALLEL, 0x6c);
185 saa_writeb(SAA7134_TS_PARALLEL_SERIAL, (TS_PACKET_SIZE-1));
186 saa_writeb(SAA7134_TS_DMA0, ((dev->ts.nr_packets-1)&0xff));
187 saa_writeb(SAA7134_TS_DMA1, (((dev->ts.nr_packets-1)>>8)&0xff));
188 /* TSNOPIT=0, TSCOLAP=0 */
189 saa_writeb(SAA7134_TS_DMA2,
190 ((((dev->ts.nr_packets-1)>>16)&0x3f) | 0x00));
191
192 return 0;
193}
194
195int saa7134_ts_init1(struct saa7134_dev *dev)
196{
197 /* sanitycheck insmod options */
198 if (tsbufs < 2)
199 tsbufs = 2;
200 if (tsbufs > VIDEO_MAX_FRAME)
201 tsbufs = VIDEO_MAX_FRAME;
202 if (ts_nr_packets < 4)
203 ts_nr_packets = 4;
204 if (ts_nr_packets > 312)
205 ts_nr_packets = 312;
206 dev->ts.nr_bufs = tsbufs;
207 dev->ts.nr_packets = ts_nr_packets;
208
209 INIT_LIST_HEAD(&dev->ts_q.queue);
210 init_timer(&dev->ts_q.timeout);
211 dev->ts_q.timeout.function = saa7134_buffer_timeout;
212 dev->ts_q.timeout.data = (unsigned long)(&dev->ts_q);
213 dev->ts_q.dev = dev;
214 dev->ts_q.need_two = 1;
215 dev->ts_started = 0;
216 saa7134_pgtable_alloc(dev->pci,&dev->ts.pt_ts);
217
218 /* init TS hw */
219 saa7134_ts_init_hw(dev);
220
221 return 0;
222}
223
224/* Function for stop TS */
225int saa7134_ts_stop(struct saa7134_dev *dev)
226{
227 dprintk("TS stop\n");
228
229 BUG_ON(!dev->ts_started);
230
231 /* Stop TS stream */
232 switch (saa7134_boards[dev->board].ts_type) {
233 case SAA7134_MPEG_TS_PARALLEL:
234 saa_writeb(SAA7134_TS_PARALLEL, 0x6c);
235 dev->ts_started = 0;
236 break;
237 case SAA7134_MPEG_TS_SERIAL:
238 saa_writeb(SAA7134_TS_SERIAL0, 0x40);
239 dev->ts_started = 0;
240 break;
241 }
242 return 0;
243}
244
245/* Function for start TS */
246int saa7134_ts_start(struct saa7134_dev *dev)
247{
248 dprintk("TS start\n");
249
250 BUG_ON(dev->ts_started);
251
252 /* dma: setup channel 5 (= TS) */
253 saa_writeb(SAA7134_TS_DMA0, (dev->ts.nr_packets - 1) & 0xff);
254 saa_writeb(SAA7134_TS_DMA1,
255 ((dev->ts.nr_packets - 1) >> 8) & 0xff);
256 /* TSNOPIT=0, TSCOLAP=0 */
257 saa_writeb(SAA7134_TS_DMA2,
258 (((dev->ts.nr_packets - 1) >> 16) & 0x3f) | 0x00);
259 saa_writel(SAA7134_RS_PITCH(5), TS_PACKET_SIZE);
260 saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_16 |
261 SAA7134_RS_CONTROL_ME |
262 (dev->ts.pt_ts.dma >> 12));
263
264 /* reset hardware TS buffers */
265 saa_writeb(SAA7134_TS_SERIAL1, 0x00);
266 saa_writeb(SAA7134_TS_SERIAL1, 0x03);
267 saa_writeb(SAA7134_TS_SERIAL1, 0x00);
268 saa_writeb(SAA7134_TS_SERIAL1, 0x01);
269
270 /* TS clock non-inverted */
271 saa_writeb(SAA7134_TS_SERIAL1, 0x00);
272
273 /* Start TS stream */
274 switch (saa7134_boards[dev->board].ts_type) {
275 case SAA7134_MPEG_TS_PARALLEL:
276 saa_writeb(SAA7134_TS_SERIAL0, 0x40);
277 saa_writeb(SAA7134_TS_PARALLEL, 0xec |
278 (saa7134_boards[dev->board].ts_force_val << 4));
279 break;
280 case SAA7134_MPEG_TS_SERIAL:
281 saa_writeb(SAA7134_TS_SERIAL0, 0xd8);
282 saa_writeb(SAA7134_TS_PARALLEL, 0x6c |
283 (saa7134_boards[dev->board].ts_force_val << 4));
284 saa_writeb(SAA7134_TS_PARALLEL_SERIAL, 0xbc);
285 saa_writeb(SAA7134_TS_SERIAL1, 0x02);
286 break;
287 }
288
289 dev->ts_started = 1;
290
291 return 0;
292}
293
294int saa7134_ts_fini(struct saa7134_dev *dev)
295{
296 saa7134_pgtable_free(dev->pci,&dev->ts.pt_ts);
297 return 0;
298}
299
300void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status)
301{
302 enum v4l2_field field;
303
304 spin_lock(&dev->slock);
305 if (dev->ts_q.curr) {
306 field = dev->ts_q.curr->vb.field;
307 if (field == V4L2_FIELD_TOP) {
308 if ((status & 0x100000) != 0x000000)
309 goto done;
310 } else {
311 if ((status & 0x100000) != 0x100000)
312 goto done;
313 }
314 saa7134_buffer_finish(dev,&dev->ts_q,VIDEOBUF_DONE);
315 }
316 saa7134_buffer_next(dev,&dev->ts_q);
317
318 done:
319 spin_unlock(&dev->slock);
320}
321
322/* ----------------------------------------------------------- */
323/*
324 * Local variables:
325 * c-basic-offset: 8
326 * End:
327 */
diff --git a/drivers/media/pci/saa7134/saa7134-tvaudio.c b/drivers/media/pci/saa7134/saa7134-tvaudio.c
new file mode 100644
index 000000000000..b7a99bee2f98
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-tvaudio.c
@@ -0,0 +1,1087 @@
1/*
2 *
3 * device driver for philips saa7134 based TV cards
4 * tv audio decoder (fm stereo, nicam, ...)
5 *
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/kthread.h>
28#include <linux/delay.h>
29#include <linux/freezer.h>
30#include <asm/div64.h>
31
32#include "saa7134-reg.h"
33#include "saa7134.h"
34
35/* ------------------------------------------------------------------ */
36
37static unsigned int audio_debug;
38module_param(audio_debug, int, 0644);
39MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]");
40
41static unsigned int audio_ddep;
42module_param(audio_ddep, int, 0644);
43MODULE_PARM_DESC(audio_ddep,"audio ddep overwrite");
44
45static int audio_clock_override = UNSET;
46module_param(audio_clock_override, int, 0644);
47
48static int audio_clock_tweak;
49module_param(audio_clock_tweak, int, 0644);
50MODULE_PARM_DESC(audio_clock_tweak, "Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])");
51
52#define dprintk(fmt, arg...) if (audio_debug) \
53 printk(KERN_DEBUG "%s/audio: " fmt, dev->name , ## arg)
54#define d2printk(fmt, arg...) if (audio_debug > 1) \
55 printk(KERN_DEBUG "%s/audio: " fmt, dev->name, ## arg)
56
57#define print_regb(reg) printk("%s: reg 0x%03x [%-16s]: 0x%02x\n", \
58 dev->name,(SAA7134_##reg),(#reg),saa_readb((SAA7134_##reg)))
59
60/* msecs */
61#define SCAN_INITIAL_DELAY 1000
62#define SCAN_SAMPLE_DELAY 200
63#define SCAN_SUBCARRIER_DELAY 2000
64
65/* ------------------------------------------------------------------ */
66/* saa7134 code */
67
68static struct mainscan {
69 char *name;
70 v4l2_std_id std;
71 int carr;
72} mainscan[] = {
73 {
74 .name = "MN",
75 .std = V4L2_STD_MN,
76 .carr = 4500,
77 },{
78 .name = "BGH",
79 .std = V4L2_STD_B | V4L2_STD_GH,
80 .carr = 5500,
81 },{
82 .name = "I",
83 .std = V4L2_STD_PAL_I,
84 .carr = 6000,
85 },{
86 .name = "DKL",
87 .std = V4L2_STD_DK | V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC,
88 .carr = 6500,
89 }
90};
91
92static struct saa7134_tvaudio tvaudio[] = {
93 {
94 .name = "PAL-B/G FM-stereo",
95 .std = V4L2_STD_PAL_BG,
96 .mode = TVAUDIO_FM_BG_STEREO,
97 .carr1 = 5500,
98 .carr2 = 5742,
99 },{
100 .name = "PAL-D/K1 FM-stereo",
101 .std = V4L2_STD_PAL_DK,
102 .carr1 = 6500,
103 .carr2 = 6258,
104 .mode = TVAUDIO_FM_BG_STEREO,
105 },{
106 .name = "PAL-D/K2 FM-stereo",
107 .std = V4L2_STD_PAL_DK,
108 .carr1 = 6500,
109 .carr2 = 6742,
110 .mode = TVAUDIO_FM_BG_STEREO,
111 },{
112 .name = "PAL-D/K3 FM-stereo",
113 .std = V4L2_STD_PAL_DK,
114 .carr1 = 6500,
115 .carr2 = 5742,
116 .mode = TVAUDIO_FM_BG_STEREO,
117 },{
118 .name = "PAL-B/G NICAM",
119 .std = V4L2_STD_PAL_BG,
120 .carr1 = 5500,
121 .carr2 = 5850,
122 .mode = TVAUDIO_NICAM_FM,
123 },{
124 .name = "PAL-I NICAM",
125 .std = V4L2_STD_PAL_I,
126 .carr1 = 6000,
127 .carr2 = 6552,
128 .mode = TVAUDIO_NICAM_FM,
129 },{
130 .name = "PAL-D/K NICAM",
131 .std = V4L2_STD_PAL_DK,
132 .carr1 = 6500,
133 .carr2 = 5850,
134 .mode = TVAUDIO_NICAM_FM,
135 },{
136 .name = "SECAM-L NICAM",
137 .std = V4L2_STD_SECAM_L,
138 .carr1 = 6500,
139 .carr2 = 5850,
140 .mode = TVAUDIO_NICAM_AM,
141 },{
142 .name = "SECAM-D/K NICAM",
143 .std = V4L2_STD_SECAM_DK,
144 .carr1 = 6500,
145 .carr2 = 5850,
146 .mode = TVAUDIO_NICAM_FM,
147 },{
148 .name = "NTSC-A2 FM-stereo",
149 .std = V4L2_STD_NTSC,
150 .carr1 = 4500,
151 .carr2 = 4724,
152 .mode = TVAUDIO_FM_K_STEREO,
153 },{
154 .name = "NTSC-M",
155 .std = V4L2_STD_NTSC,
156 .carr1 = 4500,
157 .carr2 = -1,
158 .mode = TVAUDIO_FM_MONO,
159 }
160};
161#define TVAUDIO ARRAY_SIZE(tvaudio)
162
163/* ------------------------------------------------------------------ */
164
165static u32 tvaudio_carr2reg(u32 carrier)
166{
167 u64 a = carrier;
168
169 a <<= 24;
170 do_div(a,12288);
171 return a;
172}
173
174static void tvaudio_setcarrier(struct saa7134_dev *dev,
175 int primary, int secondary)
176{
177 if (-1 == secondary)
178 secondary = primary;
179 saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
180 saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
181}
182
183#define SAA7134_MUTE_MASK 0xbb
184#define SAA7134_MUTE_ANALOG 0x04
185#define SAA7134_MUTE_I2S 0x40
186
187static void mute_input_7134(struct saa7134_dev *dev)
188{
189 unsigned int mute;
190 struct saa7134_input *in;
191 int ausel=0, ics=0, ocs=0;
192 int mask;
193
194 /* look what is to do ... */
195 in = dev->input;
196 mute = (dev->ctl_mute ||
197 (dev->automute && (&card(dev).radio) != in));
198 if (card(dev).mute.name) {
199 /*
200 * 7130 - we'll mute using some unconnected audio input
201 * 7134 - we'll probably should switch external mux with gpio
202 */
203 if (mute)
204 in = &card(dev).mute;
205 }
206
207 if (dev->hw_mute == mute &&
208 dev->hw_input == in && !dev->insuspend) {
209 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
210 mute,in->name);
211 return;
212 }
213
214 dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
215 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
216 dev->hw_mute = mute;
217 dev->hw_input = in;
218
219 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
220 /* 7134 mute */
221 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ?
222 SAA7134_MUTE_MASK |
223 SAA7134_MUTE_ANALOG |
224 SAA7134_MUTE_I2S :
225 SAA7134_MUTE_MASK);
226
227 /* switch internal audio mux */
228 switch (in->amux) {
229 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
230 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
231 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
232 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
233 }
234 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
235 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
236 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
237 // for oss, we need to change the clock configuration
238 if (in->amux == TV)
239 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
240 else
241 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
242
243 /* switch gpio-connected external audio mux */
244 if (0 == card(dev).gpiomask)
245 return;
246
247 mask = card(dev).gpiomask;
248 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
249 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
250 saa7134_track_gpio(dev,in->name);
251}
252
253static void tvaudio_setmode(struct saa7134_dev *dev,
254 struct saa7134_tvaudio *audio,
255 char *note)
256{
257 int acpf, tweak = 0;
258
259 if (dev->tvnorm->id == V4L2_STD_NTSC) {
260 acpf = 0x19066;
261 } else {
262 acpf = 0x1e000;
263 }
264 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
265 tweak = audio_clock_tweak;
266
267 if (note)
268 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
269 note,audio->name,
270 audio->carr1 / 1000, audio->carr1 % 1000,
271 audio->carr2 / 1000, audio->carr2 % 1000,
272 acpf, tweak);
273
274 acpf += tweak;
275 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
276 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
277 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
278 tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
279
280 switch (audio->mode) {
281 case TVAUDIO_FM_MONO:
282 case TVAUDIO_FM_BG_STEREO:
283 saa_writeb(SAA7134_DEMODULATOR, 0x00);
284 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
285 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
286 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
287 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
288 break;
289 case TVAUDIO_FM_K_STEREO:
290 saa_writeb(SAA7134_DEMODULATOR, 0x00);
291 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
292 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
293 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
294 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
295 break;
296 case TVAUDIO_NICAM_FM:
297 saa_writeb(SAA7134_DEMODULATOR, 0x10);
298 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
299 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
300 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
301 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
302 break;
303 case TVAUDIO_NICAM_AM:
304 saa_writeb(SAA7134_DEMODULATOR, 0x12);
305 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
306 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
307 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
308 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
309 break;
310 case TVAUDIO_FM_SAT_STEREO:
311 /* not implemented (yet) */
312 break;
313 }
314}
315
316static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
317{
318 if (dev->thread.scan1 == dev->thread.scan2 &&
319 !kthread_should_stop()) {
320 if (timeout < 0) {
321 set_current_state(TASK_INTERRUPTIBLE);
322 schedule();
323 } else {
324 schedule_timeout_interruptible
325 (msecs_to_jiffies(timeout));
326 }
327 }
328 return dev->thread.scan1 != dev->thread.scan2;
329}
330
331static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
332{
333 __s32 left,right,value;
334
335 if (!(dev->tvnorm->id & scan->std)) {
336 value = 0;
337 dprintk("skipping %d.%03d MHz [%4s]\n",
338 scan->carr / 1000, scan->carr % 1000, scan->name);
339 return 0;
340 }
341
342 if (audio_debug > 1) {
343 int i;
344 dprintk("debug %d:",scan->carr);
345 for (i = -150; i <= 150; i += 30) {
346 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
347 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
348 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
349 return -1;
350 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
351 if (0 == i)
352 printk(" # %6d # ",value >> 16);
353 else
354 printk(" %6d",value >> 16);
355 }
356 printk("\n");
357 }
358
359 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
360 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
361 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
362 return -1;
363 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
364
365 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
366 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
367 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
368 return -1;
369 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
370
371 left >>= 16;
372 right >>= 16;
373 value = left > right ? left - right : right - left;
374 dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
375 scan->carr / 1000, scan->carr % 1000,
376 scan->name, value, left, right);
377 return value;
378}
379
380
381static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
382{
383 __u32 idp, nicam, nicam_status;
384 int retval = -1;
385
386 switch (audio->mode) {
387 case TVAUDIO_FM_MONO:
388 return V4L2_TUNER_SUB_MONO;
389 case TVAUDIO_FM_K_STEREO:
390 case TVAUDIO_FM_BG_STEREO:
391 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
392 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
393 if (0x03 == (idp & 0x03))
394 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
395 else if (0x05 == (idp & 0x05))
396 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
397 else if (0x01 == (idp & 0x01))
398 retval = V4L2_TUNER_SUB_MONO;
399 break;
400 case TVAUDIO_FM_SAT_STEREO:
401 /* not implemented (yet) */
402 break;
403 case TVAUDIO_NICAM_FM:
404 case TVAUDIO_NICAM_AM:
405 nicam = saa_readb(SAA7134_AUDIO_STATUS);
406 dprintk("getstereo: nicam=0x%x\n",nicam);
407 if (nicam & 0x1) {
408 nicam_status = saa_readb(SAA7134_NICAM_STATUS);
409 dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
410
411 switch (nicam_status & 0x03) {
412 case 0x01:
413 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
414 break;
415 case 0x02:
416 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
417 break;
418 default:
419 retval = V4L2_TUNER_SUB_MONO;
420 }
421 } else {
422 /* No nicam detected */
423 }
424 break;
425 }
426 if (retval != -1)
427 dprintk("found audio subchannels:%s%s%s%s\n",
428 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
429 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
430 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
431 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
432 return retval;
433}
434
435static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
436 u32 mode)
437{
438 static char *name[] = {
439 [ V4L2_TUNER_MODE_MONO ] = "mono",
440 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
441 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
442 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
443 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = "lang1+lang2",
444 };
445 static u32 fm[] = {
446 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
447 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
448 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
449 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
450 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = 0x80, /* auto */
451 };
452 u32 reg;
453
454 switch (audio->mode) {
455 case TVAUDIO_FM_MONO:
456 /* nothing to do ... */
457 break;
458 case TVAUDIO_FM_K_STEREO:
459 case TVAUDIO_FM_BG_STEREO:
460 case TVAUDIO_NICAM_AM:
461 case TVAUDIO_NICAM_FM:
462 dprintk("setstereo [fm] => %s\n",
463 name[ mode % ARRAY_SIZE(name) ]);
464 reg = fm[ mode % ARRAY_SIZE(fm) ];
465 saa_writeb(SAA7134_FM_DEMATRIX, reg);
466 break;
467 case TVAUDIO_FM_SAT_STEREO:
468 /* Not implemented */
469 break;
470 }
471 return 0;
472}
473
474static int tvaudio_thread(void *data)
475{
476 struct saa7134_dev *dev = data;
477 int carr_vals[ARRAY_SIZE(mainscan)];
478 unsigned int i, audio, nscan;
479 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
480
481 set_freezable();
482
483 for (;;) {
484 tvaudio_sleep(dev,-1);
485 if (kthread_should_stop())
486 goto done;
487
488 restart:
489 try_to_freeze();
490
491 dev->thread.scan1 = dev->thread.scan2;
492 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
493 dev->tvaudio = NULL;
494
495 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
496 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
497
498 if (dev->ctl_automute)
499 dev->automute = 1;
500
501 mute_input_7134(dev);
502
503 /* give the tuner some time */
504 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
505 goto restart;
506
507 max1 = 0;
508 max2 = 0;
509 nscan = 0;
510 carrier = 0;
511 default_carrier = 0;
512 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
513 if (!(dev->tvnorm->id & mainscan[i].std))
514 continue;
515 if (!default_carrier)
516 default_carrier = mainscan[i].carr;
517 nscan++;
518 }
519
520 if (1 == nscan) {
521 /* only one candidate -- skip scan ;) */
522 dprintk("only one main carrier candidate - skipping scan\n");
523 max1 = 12345;
524 carrier = default_carrier;
525 } else {
526 /* scan for the main carrier */
527 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
528 tvaudio_setmode(dev,&tvaudio[0],NULL);
529 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
530 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
531 if (dev->thread.scan1 != dev->thread.scan2)
532 goto restart;
533 }
534 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
535 if (max1 < carr_vals[i]) {
536 max2 = max1;
537 max1 = carr_vals[i];
538 carrier = mainscan[i].carr;
539 } else if (max2 < carr_vals[i]) {
540 max2 = carr_vals[i];
541 }
542 }
543 }
544
545 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
546 /* found good carrier */
547 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
548 dev->tvnorm->name, carrier/1000, carrier%1000,
549 max1, max2);
550 dev->last_carrier = carrier;
551 dev->automute = 0;
552
553 } else if (0 != dev->last_carrier) {
554 /* no carrier -- try last detected one as fallback */
555 carrier = dev->last_carrier;
556 dprintk("audio carrier scan failed, "
557 "using %d.%03d MHz [last detected]\n",
558 carrier/1000, carrier%1000);
559 dev->automute = 1;
560
561 } else {
562 /* no carrier + no fallback -- use default */
563 carrier = default_carrier;
564 dprintk("audio carrier scan failed, "
565 "using %d.%03d MHz [default]\n",
566 carrier/1000, carrier%1000);
567 dev->automute = 1;
568 }
569 tvaudio_setcarrier(dev,carrier,carrier);
570 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
571 saa7134_tvaudio_setmute(dev);
572 /* find the exact tv audio norm */
573 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
574 if (dev->tvnorm->id != UNSET &&
575 !(dev->tvnorm->id & tvaudio[i].std))
576 continue;
577 if (tvaudio[i].carr1 != carrier)
578 continue;
579 /* Note: at least the primary carrier is right here */
580 if (UNSET == audio)
581 audio = i;
582 tvaudio_setmode(dev,&tvaudio[i],"trying");
583 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
584 goto restart;
585 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
586 audio = i;
587 break;
588 }
589 }
590 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
591 if (UNSET == audio)
592 continue;
593 tvaudio_setmode(dev,&tvaudio[audio],"using");
594
595 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
596 dev->tvaudio = &tvaudio[audio];
597
598 lastmode = 42;
599 for (;;) {
600
601 try_to_freeze();
602
603 if (tvaudio_sleep(dev,5000))
604 goto restart;
605 if (kthread_should_stop())
606 break;
607 if (UNSET == dev->thread.mode) {
608 rx = tvaudio_getstereo(dev, &tvaudio[audio]);
609 mode = saa7134_tvaudio_rx2mode(rx);
610 } else {
611 mode = dev->thread.mode;
612 }
613 if (lastmode != mode) {
614 tvaudio_setstereo(dev,&tvaudio[audio],mode);
615 lastmode = mode;
616 }
617 }
618 }
619
620 done:
621 dev->thread.stopped = 1;
622 return 0;
623}
624
625/* ------------------------------------------------------------------ */
626/* saa7133 / saa7135 code */
627
628static char *stdres[0x20] = {
629 [0x00] = "no standard detected",
630 [0x01] = "B/G (in progress)",
631 [0x02] = "D/K (in progress)",
632 [0x03] = "M (in progress)",
633
634 [0x04] = "B/G A2",
635 [0x05] = "B/G NICAM",
636 [0x06] = "D/K A2 (1)",
637 [0x07] = "D/K A2 (2)",
638 [0x08] = "D/K A2 (3)",
639 [0x09] = "D/K NICAM",
640 [0x0a] = "L NICAM",
641 [0x0b] = "I NICAM",
642
643 [0x0c] = "M Korea",
644 [0x0d] = "M BTSC ",
645 [0x0e] = "M EIAJ",
646
647 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
648 [0x10] = "FM radio / IF 10.7 / 75 deemp",
649 [0x11] = "FM radio / IF sel / 50 deemp",
650 [0x12] = "FM radio / IF sel / 75 deemp",
651
652 [0x13 ... 0x1e ] = "unknown",
653 [0x1f] = "??? [in progress]",
654};
655
656#define DSP_RETRY 32
657#define DSP_DELAY 16
658#define SAA7135_DSP_RWCLEAR_RERR 1
659
660static inline int saa_dsp_reset_error_bit(struct saa7134_dev *dev)
661{
662 int state = saa_readb(SAA7135_DSP_RWSTATE);
663 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
664 d2printk("%s: resetting error bit\n", dev->name);
665 saa_writeb(SAA7135_DSP_RWCLEAR, SAA7135_DSP_RWCLEAR_RERR);
666 }
667 return 0;
668}
669
670static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
671{
672 int state, count = DSP_RETRY;
673
674 state = saa_readb(SAA7135_DSP_RWSTATE);
675 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
676 printk(KERN_WARNING "%s: dsp access error\n", dev->name);
677 saa_dsp_reset_error_bit(dev);
678 return -EIO;
679 }
680 while (0 == (state & bit)) {
681 if (unlikely(0 == count)) {
682 printk("%s: dsp access wait timeout [bit=%s]\n",
683 dev->name,
684 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
685 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
686 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
687 "???");
688 return -EIO;
689 }
690 saa_wait(DSP_DELAY);
691 state = saa_readb(SAA7135_DSP_RWSTATE);
692 count--;
693 }
694 return 0;
695}
696
697
698int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
699{
700 int err;
701
702 d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
703 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
704 if (err < 0)
705 return err;
706 saa_writel(reg,value);
707 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
708 if (err < 0)
709 return err;
710 return 0;
711}
712
713static int getstereo_7133(struct saa7134_dev *dev)
714{
715 int retval = V4L2_TUNER_SUB_MONO;
716 u32 value;
717
718 value = saa_readl(0x528 >> 2);
719 if (value & 0x20)
720 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
721 if (value & 0x40)
722 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
723 return retval;
724}
725
726static int mute_input_7133(struct saa7134_dev *dev)
727{
728 u32 reg = 0;
729 u32 xbarin, xbarout;
730 int mask;
731 struct saa7134_input *in;
732
733 xbarin = 0x03;
734 switch (dev->input->amux) {
735 case TV:
736 reg = 0x02;
737 xbarin = 0;
738 break;
739 case LINE1:
740 reg = 0x00;
741 break;
742 case LINE2:
743 case LINE2_LEFT:
744 reg = 0x09;
745 break;
746 }
747 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
748 if (dev->ctl_mute) {
749 reg = 0x07;
750 xbarout = 0xbbbbbb;
751 } else
752 xbarout = 0xbbbb10;
753 saa_dsp_writel(dev, 0x46c >> 2, xbarout);
754
755 saa_writel(0x594 >> 2, reg);
756
757
758 /* switch gpio-connected external audio mux */
759 if (0 != card(dev).gpiomask) {
760 mask = card(dev).gpiomask;
761
762 if (card(dev).mute.name && dev->ctl_mute)
763 in = &card(dev).mute;
764 else
765 in = dev->input;
766
767 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
768 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
769 saa7134_track_gpio(dev,in->name);
770 }
771
772 return 0;
773}
774
775static int tvaudio_thread_ddep(void *data)
776{
777 struct saa7134_dev *dev = data;
778 u32 value, norms;
779
780 set_freezable();
781 for (;;) {
782 tvaudio_sleep(dev,-1);
783 if (kthread_should_stop())
784 goto done;
785 restart:
786 try_to_freeze();
787
788 dev->thread.scan1 = dev->thread.scan2;
789 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
790
791 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
792 /* insmod option override */
793 norms = (audio_ddep << 2) | 0x01;
794 dprintk("ddep override: %s\n",stdres[audio_ddep]);
795 } else if (&card(dev).radio == dev->input) {
796 dprintk("FM Radio\n");
797 if (dev->tuner_type == TUNER_PHILIPS_TDA8290) {
798 norms = (0x11 << 2) | 0x01;
799 saa_dsp_writel(dev, 0x42c >> 2, 0x729555);
800 } else {
801 norms = (0x0f << 2) | 0x01;
802 }
803 } else {
804 /* (let chip) scan for sound carrier */
805 norms = 0;
806 if (dev->tvnorm->id & (V4L2_STD_B | V4L2_STD_GH))
807 norms |= 0x04;
808 if (dev->tvnorm->id & V4L2_STD_PAL_I)
809 norms |= 0x20;
810 if (dev->tvnorm->id & V4L2_STD_DK)
811 norms |= 0x08;
812 if (dev->tvnorm->id & V4L2_STD_MN)
813 norms |= 0x40;
814 if (dev->tvnorm->id & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))
815 norms |= 0x10;
816 if (0 == norms)
817 norms = 0x7c; /* all */
818 dprintk("scanning:%s%s%s%s%s\n",
819 (norms & 0x04) ? " B/G" : "",
820 (norms & 0x08) ? " D/K" : "",
821 (norms & 0x10) ? " L/L'" : "",
822 (norms & 0x20) ? " I" : "",
823 (norms & 0x40) ? " M" : "");
824 }
825
826 /* kick automatic standard detection */
827 saa_dsp_writel(dev, 0x454 >> 2, 0);
828 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
829
830 /* setup crossbars */
831 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
832 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
833
834 if (tvaudio_sleep(dev,3000))
835 goto restart;
836 value = saa_readl(0x528 >> 2) & 0xffffff;
837
838 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
839 value, stdres[value & 0x1f],
840 (value & 0x000020) ? ",stereo" : "",
841 (value & 0x000040) ? ",dual" : "");
842 dprintk("detailed status: "
843 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
844 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
845 (value & 0x000100) ? " A2/EIAJ dual " : "",
846 (value & 0x000200) ? " A2/EIAJ stereo " : "",
847 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
848
849 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
850 (value & 0x001000) ? " SAP carrier " : "",
851 (value & 0x002000) ? " BTSC stereo noise mute " : "",
852 (value & 0x004000) ? " SAP noise mute " : "",
853 (value & 0x008000) ? " VDSP " : "",
854
855 (value & 0x010000) ? " NICST " : "",
856 (value & 0x020000) ? " NICDU " : "",
857 (value & 0x040000) ? " NICAM muted " : "",
858 (value & 0x080000) ? " NICAM reserve sound " : "",
859
860 (value & 0x100000) ? " init done " : "");
861 }
862
863 done:
864 dev->thread.stopped = 1;
865 return 0;
866}
867
868/* ------------------------------------------------------------------ */
869/* common stuff + external entry points */
870
871void saa7134_enable_i2s(struct saa7134_dev *dev)
872{
873 int i2s_format;
874
875 if (!card_is_empress(dev))
876 return;
877
878 if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
879 return;
880
881 /* configure GPIO for out */
882 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x0E000000, 0x00000000);
883
884 switch (dev->pci->device) {
885 case PCI_DEVICE_ID_PHILIPS_SAA7133:
886 case PCI_DEVICE_ID_PHILIPS_SAA7135:
887 /* Set I2S format (SONY)  */
888 saa_writeb(SAA7133_I2S_AUDIO_CONTROL, 0x00);
889 /* Start I2S */
890 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x11);
891 break;
892
893 case PCI_DEVICE_ID_PHILIPS_SAA7134:
894 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
895
896 /* enable I2S audio output for the mpeg encoder */
897 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
898 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
899 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
900 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
901
902 default:
903 break;
904 }
905}
906
907int saa7134_tvaudio_rx2mode(u32 rx)
908{
909 u32 mode;
910
911 mode = V4L2_TUNER_MODE_MONO;
912 if (rx & V4L2_TUNER_SUB_STEREO)
913 mode = V4L2_TUNER_MODE_STEREO;
914 else if (rx & V4L2_TUNER_SUB_LANG1)
915 mode = V4L2_TUNER_MODE_LANG1;
916 else if (rx & V4L2_TUNER_SUB_LANG2)
917 mode = V4L2_TUNER_MODE_LANG2;
918 return mode;
919}
920
921void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
922{
923 switch (dev->pci->device) {
924 case PCI_DEVICE_ID_PHILIPS_SAA7130:
925 case PCI_DEVICE_ID_PHILIPS_SAA7134:
926 mute_input_7134(dev);
927 break;
928 case PCI_DEVICE_ID_PHILIPS_SAA7133:
929 case PCI_DEVICE_ID_PHILIPS_SAA7135:
930 mute_input_7133(dev);
931 break;
932 }
933}
934
935void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
936 struct saa7134_input *in)
937{
938 dev->input = in;
939 switch (dev->pci->device) {
940 case PCI_DEVICE_ID_PHILIPS_SAA7130:
941 case PCI_DEVICE_ID_PHILIPS_SAA7134:
942 mute_input_7134(dev);
943 break;
944 case PCI_DEVICE_ID_PHILIPS_SAA7133:
945 case PCI_DEVICE_ID_PHILIPS_SAA7135:
946 mute_input_7133(dev);
947 break;
948 }
949 saa7134_enable_i2s(dev);
950}
951
952void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
953{
954 switch (dev->pci->device) {
955 case PCI_DEVICE_ID_PHILIPS_SAA7134:
956 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
957 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
958 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
959 break;
960 }
961}
962
963int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
964{
965 int retval = V4L2_TUNER_SUB_MONO;
966
967 switch (dev->pci->device) {
968 case PCI_DEVICE_ID_PHILIPS_SAA7134:
969 if (dev->tvaudio)
970 retval = tvaudio_getstereo(dev,dev->tvaudio);
971 break;
972 case PCI_DEVICE_ID_PHILIPS_SAA7133:
973 case PCI_DEVICE_ID_PHILIPS_SAA7135:
974 retval = getstereo_7133(dev);
975 break;
976 }
977 return retval;
978}
979
980void saa7134_tvaudio_init(struct saa7134_dev *dev)
981{
982 int clock = saa7134_boards[dev->board].audio_clock;
983
984 if (UNSET != audio_clock_override)
985 clock = audio_clock_override;
986
987 switch (dev->pci->device) {
988 case PCI_DEVICE_ID_PHILIPS_SAA7134:
989 /* init all audio registers */
990 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
991 if (need_resched())
992 schedule();
993 else
994 udelay(10);
995
996 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
997 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
998 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
999 /* frame locked audio is mandatory for NICAM */
1000 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01);
1001 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
1002 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
1003 break;
1004 case PCI_DEVICE_ID_PHILIPS_SAA7133:
1005 case PCI_DEVICE_ID_PHILIPS_SAA7135:
1006 saa_writel(0x598 >> 2, clock);
1007 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
1008 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
1009 }
1010}
1011
1012int saa7134_tvaudio_init2(struct saa7134_dev *dev)
1013{
1014 int (*my_thread)(void *data) = NULL;
1015
1016 switch (dev->pci->device) {
1017 case PCI_DEVICE_ID_PHILIPS_SAA7134:
1018 my_thread = tvaudio_thread;
1019 break;
1020 case PCI_DEVICE_ID_PHILIPS_SAA7133:
1021 case PCI_DEVICE_ID_PHILIPS_SAA7135:
1022 my_thread = tvaudio_thread_ddep;
1023 break;
1024 }
1025
1026 dev->thread.thread = NULL;
1027 dev->thread.scan1 = dev->thread.scan2 = 0;
1028 if (my_thread) {
1029 saa7134_tvaudio_init(dev);
1030 /* start tvaudio thread */
1031 dev->thread.thread = kthread_run(my_thread, dev, "%s", dev->name);
1032 if (IS_ERR(dev->thread.thread)) {
1033 printk(KERN_WARNING "%s: kernel_thread() failed\n",
1034 dev->name);
1035 /* XXX: missing error handling here */
1036 }
1037 }
1038
1039 saa7134_enable_i2s(dev);
1040 return 0;
1041}
1042
1043int saa7134_tvaudio_close(struct saa7134_dev *dev)
1044{
1045 dev->automute = 1;
1046 /* anything else to undo? */
1047 return 0;
1048}
1049
1050int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1051{
1052 /* shutdown tvaudio thread */
1053 if (dev->thread.thread && !dev->thread.stopped)
1054 kthread_stop(dev->thread.thread);
1055
1056 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1057 return 0;
1058}
1059
1060int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1061{
1062 if (dev->input->amux != TV) {
1063 dprintk("sound IF not in use, skipping scan\n");
1064 dev->automute = 0;
1065 saa7134_tvaudio_setmute(dev);
1066 } else if (dev->thread.thread) {
1067 dev->thread.mode = UNSET;
1068 dev->thread.scan2++;
1069
1070 if (!dev->insuspend && !dev->thread.stopped)
1071 wake_up_process(dev->thread.thread);
1072 } else {
1073 dev->automute = 0;
1074 saa7134_tvaudio_setmute(dev);
1075 }
1076 return 0;
1077}
1078
1079EXPORT_SYMBOL(saa_dsp_writel);
1080EXPORT_SYMBOL(saa7134_tvaudio_setmute);
1081
1082/* ----------------------------------------------------------- */
1083/*
1084 * Local variables:
1085 * c-basic-offset: 8
1086 * End:
1087 */
diff --git a/drivers/media/pci/saa7134/saa7134-vbi.c b/drivers/media/pci/saa7134/saa7134-vbi.c
new file mode 100644
index 000000000000..e9aa94b807f1
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-vbi.c
@@ -0,0 +1,255 @@
1/*
2 *
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
5 *
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27
28#include "saa7134-reg.h"
29#include "saa7134.h"
30
31/* ------------------------------------------------------------------ */
32
33static unsigned int vbi_debug;
34module_param(vbi_debug, int, 0644);
35MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");
36
37static unsigned int vbibufs = 4;
38module_param(vbibufs, int, 0444);
39MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32");
40
41#define dprintk(fmt, arg...) if (vbi_debug) \
42 printk(KERN_DEBUG "%s/vbi: " fmt, dev->name , ## arg)
43
44/* ------------------------------------------------------------------ */
45
46#define VBI_LINE_COUNT 16
47#define VBI_LINE_LENGTH 2048
48#define VBI_SCALE 0x200
49
50static void task_init(struct saa7134_dev *dev, struct saa7134_buf *buf,
51 int task)
52{
53 struct saa7134_tvnorm *norm = dev->tvnorm;
54
55 /* setup video scaler */
56 saa_writeb(SAA7134_VBI_H_START1(task), norm->h_start & 0xff);
57 saa_writeb(SAA7134_VBI_H_START2(task), norm->h_start >> 8);
58 saa_writeb(SAA7134_VBI_H_STOP1(task), norm->h_stop & 0xff);
59 saa_writeb(SAA7134_VBI_H_STOP2(task), norm->h_stop >> 8);
60 saa_writeb(SAA7134_VBI_V_START1(task), norm->vbi_v_start_0 & 0xff);
61 saa_writeb(SAA7134_VBI_V_START2(task), norm->vbi_v_start_0 >> 8);
62 saa_writeb(SAA7134_VBI_V_STOP1(task), norm->vbi_v_stop_0 & 0xff);
63 saa_writeb(SAA7134_VBI_V_STOP2(task), norm->vbi_v_stop_0 >> 8);
64
65 saa_writeb(SAA7134_VBI_H_SCALE_INC1(task), VBI_SCALE & 0xff);
66 saa_writeb(SAA7134_VBI_H_SCALE_INC2(task), VBI_SCALE >> 8);
67 saa_writeb(SAA7134_VBI_PHASE_OFFSET_LUMA(task), 0x00);
68 saa_writeb(SAA7134_VBI_PHASE_OFFSET_CHROMA(task), 0x00);
69
70 saa_writeb(SAA7134_VBI_H_LEN1(task), buf->vb.width & 0xff);
71 saa_writeb(SAA7134_VBI_H_LEN2(task), buf->vb.width >> 8);
72 saa_writeb(SAA7134_VBI_V_LEN1(task), buf->vb.height & 0xff);
73 saa_writeb(SAA7134_VBI_V_LEN2(task), buf->vb.height >> 8);
74
75 saa_andorb(SAA7134_DATA_PATH(task), 0xc0, 0x00);
76}
77
78/* ------------------------------------------------------------------ */
79
80static int buffer_activate(struct saa7134_dev *dev,
81 struct saa7134_buf *buf,
82 struct saa7134_buf *next)
83{
84 unsigned long control,base;
85
86 dprintk("buffer_activate [%p]\n",buf);
87 buf->vb.state = VIDEOBUF_ACTIVE;
88 buf->top_seen = 0;
89
90 task_init(dev,buf,TASK_A);
91 task_init(dev,buf,TASK_B);
92 saa_writeb(SAA7134_OFMT_DATA_A, 0x06);
93 saa_writeb(SAA7134_OFMT_DATA_B, 0x06);
94
95 /* DMA: setup channel 2+3 (= VBI Task A+B) */
96 base = saa7134_buffer_base(buf);
97 control = SAA7134_RS_CONTROL_BURST_16 |
98 SAA7134_RS_CONTROL_ME |
99 (buf->pt->dma >> 12);
100 saa_writel(SAA7134_RS_BA1(2),base);
101 saa_writel(SAA7134_RS_BA2(2),base + buf->vb.size/2);
102 saa_writel(SAA7134_RS_PITCH(2),buf->vb.width);
103 saa_writel(SAA7134_RS_CONTROL(2),control);
104 saa_writel(SAA7134_RS_BA1(3),base);
105 saa_writel(SAA7134_RS_BA2(3),base + buf->vb.size/2);
106 saa_writel(SAA7134_RS_PITCH(3),buf->vb.width);
107 saa_writel(SAA7134_RS_CONTROL(3),control);
108
109 /* start DMA */
110 saa7134_set_dmabits(dev);
111 mod_timer(&dev->vbi_q.timeout, jiffies+BUFFER_TIMEOUT);
112
113 return 0;
114}
115
116static int buffer_prepare(struct videobuf_queue *q,
117 struct videobuf_buffer *vb,
118 enum v4l2_field field)
119{
120 struct saa7134_fh *fh = q->priv_data;
121 struct saa7134_dev *dev = fh->dev;
122 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
123 struct saa7134_tvnorm *norm = dev->tvnorm;
124 unsigned int lines, llength, size;
125 int err;
126
127 lines = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
128 if (lines > VBI_LINE_COUNT)
129 lines = VBI_LINE_COUNT;
130 llength = VBI_LINE_LENGTH;
131 size = lines * llength * 2;
132 if (0 != buf->vb.baddr && buf->vb.bsize < size)
133 return -EINVAL;
134
135 if (buf->vb.size != size)
136 saa7134_dma_free(q,buf);
137
138 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
139 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
140
141 buf->vb.width = llength;
142 buf->vb.height = lines;
143 buf->vb.size = size;
144 buf->pt = &fh->pt_vbi;
145
146 err = videobuf_iolock(q,&buf->vb,NULL);
147 if (err)
148 goto oops;
149 err = saa7134_pgtable_build(dev->pci,buf->pt,
150 dma->sglist,
151 dma->sglen,
152 saa7134_buffer_startpage(buf));
153 if (err)
154 goto oops;
155 }
156 buf->vb.state = VIDEOBUF_PREPARED;
157 buf->activate = buffer_activate;
158 buf->vb.field = field;
159 return 0;
160
161 oops:
162 saa7134_dma_free(q,buf);
163 return err;
164}
165
166static int
167buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
168{
169 struct saa7134_fh *fh = q->priv_data;
170 struct saa7134_dev *dev = fh->dev;
171 int llength,lines;
172
173 lines = dev->tvnorm->vbi_v_stop_0 - dev->tvnorm->vbi_v_start_0 +1;
174 llength = VBI_LINE_LENGTH;
175 *size = lines * llength * 2;
176 if (0 == *count)
177 *count = vbibufs;
178 *count = saa7134_buffer_count(*size,*count);
179 return 0;
180}
181
182static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
183{
184 struct saa7134_fh *fh = q->priv_data;
185 struct saa7134_dev *dev = fh->dev;
186 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
187
188 saa7134_buffer_queue(dev,&dev->vbi_q,buf);
189}
190
191static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
192{
193 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
194
195 saa7134_dma_free(q,buf);
196}
197
198struct videobuf_queue_ops saa7134_vbi_qops = {
199 .buf_setup = buffer_setup,
200 .buf_prepare = buffer_prepare,
201 .buf_queue = buffer_queue,
202 .buf_release = buffer_release,
203};
204
205/* ------------------------------------------------------------------ */
206
207int saa7134_vbi_init1(struct saa7134_dev *dev)
208{
209 INIT_LIST_HEAD(&dev->vbi_q.queue);
210 init_timer(&dev->vbi_q.timeout);
211 dev->vbi_q.timeout.function = saa7134_buffer_timeout;
212 dev->vbi_q.timeout.data = (unsigned long)(&dev->vbi_q);
213 dev->vbi_q.dev = dev;
214
215 if (vbibufs < 2)
216 vbibufs = 2;
217 if (vbibufs > VIDEO_MAX_FRAME)
218 vbibufs = VIDEO_MAX_FRAME;
219 return 0;
220}
221
222int saa7134_vbi_fini(struct saa7134_dev *dev)
223{
224 /* nothing */
225 return 0;
226}
227
228void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status)
229{
230 spin_lock(&dev->slock);
231 if (dev->vbi_q.curr) {
232 dev->vbi_fieldcount++;
233 /* make sure we have seen both fields */
234 if ((status & 0x10) == 0x00) {
235 dev->vbi_q.curr->top_seen = 1;
236 goto done;
237 }
238 if (!dev->vbi_q.curr->top_seen)
239 goto done;
240
241 dev->vbi_q.curr->vb.field_count = dev->vbi_fieldcount;
242 saa7134_buffer_finish(dev,&dev->vbi_q,VIDEOBUF_DONE);
243 }
244 saa7134_buffer_next(dev,&dev->vbi_q);
245
246 done:
247 spin_unlock(&dev->slock);
248}
249
250/* ----------------------------------------------------------- */
251/*
252 * Local variables:
253 * c-basic-offset: 8
254 * End:
255 */
diff --git a/drivers/media/pci/saa7134/saa7134-video.c b/drivers/media/pci/saa7134/saa7134-video.c
new file mode 100644
index 000000000000..6de10b1e7251
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134-video.c
@@ -0,0 +1,2661 @@
1/*
2 *
3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
5 *
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/sort.h>
29
30#include "saa7134-reg.h"
31#include "saa7134.h"
32#include <media/v4l2-common.h>
33#include <media/saa6588.h>
34
35/* ------------------------------------------------------------------ */
36
37unsigned int video_debug;
38static unsigned int gbuffers = 8;
39static unsigned int noninterlaced; /* 0 */
40static unsigned int gbufsize = 720*576*4;
41static unsigned int gbufsize_max = 720*576*4;
42static char secam[] = "--";
43module_param(video_debug, int, 0644);
44MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
45module_param(gbuffers, int, 0444);
46MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
47module_param(noninterlaced, int, 0644);
48MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
49module_param_string(secam, secam, sizeof(secam), 0644);
50MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
51
52
53#define dprintk(fmt, arg...) if (video_debug&0x04) \
54 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
55
56/* ------------------------------------------------------------------ */
57/* Defines for Video Output Port Register at address 0x191 */
58
59/* Bit 0: VIP code T bit polarity */
60
61#define VP_T_CODE_P_NON_INVERTED 0x00
62#define VP_T_CODE_P_INVERTED 0x01
63
64/* ------------------------------------------------------------------ */
65/* Defines for Video Output Port Register at address 0x195 */
66
67/* Bit 2: Video output clock delay control */
68
69#define VP_CLK_CTRL2_NOT_DELAYED 0x00
70#define VP_CLK_CTRL2_DELAYED 0x04
71
72/* Bit 1: Video output clock invert control */
73
74#define VP_CLK_CTRL1_NON_INVERTED 0x00
75#define VP_CLK_CTRL1_INVERTED 0x02
76
77/* ------------------------------------------------------------------ */
78/* Defines for Video Output Port Register at address 0x196 */
79
80/* Bits 2 to 0: VSYNC pin video vertical sync type */
81
82#define VP_VS_TYPE_MASK 0x07
83
84#define VP_VS_TYPE_OFF 0x00
85#define VP_VS_TYPE_V123 0x01
86#define VP_VS_TYPE_V_ITU 0x02
87#define VP_VS_TYPE_VGATE_L 0x03
88#define VP_VS_TYPE_RESERVED1 0x04
89#define VP_VS_TYPE_RESERVED2 0x05
90#define VP_VS_TYPE_F_ITU 0x06
91#define VP_VS_TYPE_SC_FID 0x07
92
93/* ------------------------------------------------------------------ */
94/* data structs for video */
95
96static int video_out[][9] = {
97 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
98};
99
100static struct saa7134_format formats[] = {
101 {
102 .name = "8 bpp gray",
103 .fourcc = V4L2_PIX_FMT_GREY,
104 .depth = 8,
105 .pm = 0x06,
106 },{
107 .name = "15 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_RGB555,
109 .depth = 16,
110 .pm = 0x13 | 0x80,
111 },{
112 .name = "15 bpp RGB, be",
113 .fourcc = V4L2_PIX_FMT_RGB555X,
114 .depth = 16,
115 .pm = 0x13 | 0x80,
116 .bswap = 1,
117 },{
118 .name = "16 bpp RGB, le",
119 .fourcc = V4L2_PIX_FMT_RGB565,
120 .depth = 16,
121 .pm = 0x10 | 0x80,
122 },{
123 .name = "16 bpp RGB, be",
124 .fourcc = V4L2_PIX_FMT_RGB565X,
125 .depth = 16,
126 .pm = 0x10 | 0x80,
127 .bswap = 1,
128 },{
129 .name = "24 bpp RGB, le",
130 .fourcc = V4L2_PIX_FMT_BGR24,
131 .depth = 24,
132 .pm = 0x11,
133 },{
134 .name = "24 bpp RGB, be",
135 .fourcc = V4L2_PIX_FMT_RGB24,
136 .depth = 24,
137 .pm = 0x11,
138 .bswap = 1,
139 },{
140 .name = "32 bpp RGB, le",
141 .fourcc = V4L2_PIX_FMT_BGR32,
142 .depth = 32,
143 .pm = 0x12,
144 },{
145 .name = "32 bpp RGB, be",
146 .fourcc = V4L2_PIX_FMT_RGB32,
147 .depth = 32,
148 .pm = 0x12,
149 .bswap = 1,
150 .wswap = 1,
151 },{
152 .name = "4:2:2 packed, YUYV",
153 .fourcc = V4L2_PIX_FMT_YUYV,
154 .depth = 16,
155 .pm = 0x00,
156 .bswap = 1,
157 .yuv = 1,
158 },{
159 .name = "4:2:2 packed, UYVY",
160 .fourcc = V4L2_PIX_FMT_UYVY,
161 .depth = 16,
162 .pm = 0x00,
163 .yuv = 1,
164 },{
165 .name = "4:2:2 planar, Y-Cb-Cr",
166 .fourcc = V4L2_PIX_FMT_YUV422P,
167 .depth = 16,
168 .pm = 0x09,
169 .yuv = 1,
170 .planar = 1,
171 .hshift = 1,
172 .vshift = 0,
173 },{
174 .name = "4:2:0 planar, Y-Cb-Cr",
175 .fourcc = V4L2_PIX_FMT_YUV420,
176 .depth = 12,
177 .pm = 0x0a,
178 .yuv = 1,
179 .planar = 1,
180 .hshift = 1,
181 .vshift = 1,
182 },{
183 .name = "4:2:0 planar, Y-Cb-Cr",
184 .fourcc = V4L2_PIX_FMT_YVU420,
185 .depth = 12,
186 .pm = 0x0a,
187 .yuv = 1,
188 .planar = 1,
189 .uvswap = 1,
190 .hshift = 1,
191 .vshift = 1,
192 }
193};
194#define FORMATS ARRAY_SIZE(formats)
195
196#define NORM_625_50 \
197 .h_start = 0, \
198 .h_stop = 719, \
199 .video_v_start = 24, \
200 .video_v_stop = 311, \
201 .vbi_v_start_0 = 7, \
202 .vbi_v_stop_0 = 22, \
203 .vbi_v_start_1 = 319, \
204 .src_timing = 4
205
206#define NORM_525_60 \
207 .h_start = 0, \
208 .h_stop = 719, \
209 .video_v_start = 23, \
210 .video_v_stop = 262, \
211 .vbi_v_start_0 = 10, \
212 .vbi_v_stop_0 = 21, \
213 .vbi_v_start_1 = 273, \
214 .src_timing = 7
215
216static struct saa7134_tvnorm tvnorms[] = {
217 {
218 .name = "PAL", /* autodetect */
219 .id = V4L2_STD_PAL,
220 NORM_625_50,
221
222 .sync_control = 0x18,
223 .luma_control = 0x40,
224 .chroma_ctrl1 = 0x81,
225 .chroma_gain = 0x2a,
226 .chroma_ctrl2 = 0x06,
227 .vgate_misc = 0x1c,
228
229 },{
230 .name = "PAL-BG",
231 .id = V4L2_STD_PAL_BG,
232 NORM_625_50,
233
234 .sync_control = 0x18,
235 .luma_control = 0x40,
236 .chroma_ctrl1 = 0x81,
237 .chroma_gain = 0x2a,
238 .chroma_ctrl2 = 0x06,
239 .vgate_misc = 0x1c,
240
241 },{
242 .name = "PAL-I",
243 .id = V4L2_STD_PAL_I,
244 NORM_625_50,
245
246 .sync_control = 0x18,
247 .luma_control = 0x40,
248 .chroma_ctrl1 = 0x81,
249 .chroma_gain = 0x2a,
250 .chroma_ctrl2 = 0x06,
251 .vgate_misc = 0x1c,
252
253 },{
254 .name = "PAL-DK",
255 .id = V4L2_STD_PAL_DK,
256 NORM_625_50,
257
258 .sync_control = 0x18,
259 .luma_control = 0x40,
260 .chroma_ctrl1 = 0x81,
261 .chroma_gain = 0x2a,
262 .chroma_ctrl2 = 0x06,
263 .vgate_misc = 0x1c,
264
265 },{
266 .name = "NTSC",
267 .id = V4L2_STD_NTSC,
268 NORM_525_60,
269
270 .sync_control = 0x59,
271 .luma_control = 0x40,
272 .chroma_ctrl1 = 0x89,
273 .chroma_gain = 0x2a,
274 .chroma_ctrl2 = 0x0e,
275 .vgate_misc = 0x18,
276
277 },{
278 .name = "SECAM",
279 .id = V4L2_STD_SECAM,
280 NORM_625_50,
281
282 .sync_control = 0x18,
283 .luma_control = 0x1b,
284 .chroma_ctrl1 = 0xd1,
285 .chroma_gain = 0x80,
286 .chroma_ctrl2 = 0x00,
287 .vgate_misc = 0x1c,
288
289 },{
290 .name = "SECAM-DK",
291 .id = V4L2_STD_SECAM_DK,
292 NORM_625_50,
293
294 .sync_control = 0x18,
295 .luma_control = 0x1b,
296 .chroma_ctrl1 = 0xd1,
297 .chroma_gain = 0x80,
298 .chroma_ctrl2 = 0x00,
299 .vgate_misc = 0x1c,
300
301 },{
302 .name = "SECAM-L",
303 .id = V4L2_STD_SECAM_L,
304 NORM_625_50,
305
306 .sync_control = 0x18,
307 .luma_control = 0x1b,
308 .chroma_ctrl1 = 0xd1,
309 .chroma_gain = 0x80,
310 .chroma_ctrl2 = 0x00,
311 .vgate_misc = 0x1c,
312
313 },{
314 .name = "SECAM-Lc",
315 .id = V4L2_STD_SECAM_LC,
316 NORM_625_50,
317
318 .sync_control = 0x18,
319 .luma_control = 0x1b,
320 .chroma_ctrl1 = 0xd1,
321 .chroma_gain = 0x80,
322 .chroma_ctrl2 = 0x00,
323 .vgate_misc = 0x1c,
324
325 },{
326 .name = "PAL-M",
327 .id = V4L2_STD_PAL_M,
328 NORM_525_60,
329
330 .sync_control = 0x59,
331 .luma_control = 0x40,
332 .chroma_ctrl1 = 0xb9,
333 .chroma_gain = 0x2a,
334 .chroma_ctrl2 = 0x0e,
335 .vgate_misc = 0x18,
336
337 },{
338 .name = "PAL-Nc",
339 .id = V4L2_STD_PAL_Nc,
340 NORM_625_50,
341
342 .sync_control = 0x18,
343 .luma_control = 0x40,
344 .chroma_ctrl1 = 0xa1,
345 .chroma_gain = 0x2a,
346 .chroma_ctrl2 = 0x06,
347 .vgate_misc = 0x1c,
348
349 },{
350 .name = "PAL-60",
351 .id = V4L2_STD_PAL_60,
352
353 .h_start = 0,
354 .h_stop = 719,
355 .video_v_start = 23,
356 .video_v_stop = 262,
357 .vbi_v_start_0 = 10,
358 .vbi_v_stop_0 = 21,
359 .vbi_v_start_1 = 273,
360 .src_timing = 7,
361
362 .sync_control = 0x18,
363 .luma_control = 0x40,
364 .chroma_ctrl1 = 0x81,
365 .chroma_gain = 0x2a,
366 .chroma_ctrl2 = 0x06,
367 .vgate_misc = 0x1c,
368 }
369};
370#define TVNORMS ARRAY_SIZE(tvnorms)
371
372#define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
373#define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
374#define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
375#define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
376#define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
377
378static const struct v4l2_queryctrl no_ctrl = {
379 .name = "42",
380 .flags = V4L2_CTRL_FLAG_DISABLED,
381};
382static const struct v4l2_queryctrl video_ctrls[] = {
383 /* --- video --- */
384 {
385 .id = V4L2_CID_BRIGHTNESS,
386 .name = "Brightness",
387 .minimum = 0,
388 .maximum = 255,
389 .step = 1,
390 .default_value = 128,
391 .type = V4L2_CTRL_TYPE_INTEGER,
392 },{
393 .id = V4L2_CID_CONTRAST,
394 .name = "Contrast",
395 .minimum = 0,
396 .maximum = 127,
397 .step = 1,
398 .default_value = 68,
399 .type = V4L2_CTRL_TYPE_INTEGER,
400 },{
401 .id = V4L2_CID_SATURATION,
402 .name = "Saturation",
403 .minimum = 0,
404 .maximum = 127,
405 .step = 1,
406 .default_value = 64,
407 .type = V4L2_CTRL_TYPE_INTEGER,
408 },{
409 .id = V4L2_CID_HUE,
410 .name = "Hue",
411 .minimum = -128,
412 .maximum = 127,
413 .step = 1,
414 .default_value = 0,
415 .type = V4L2_CTRL_TYPE_INTEGER,
416 },{
417 .id = V4L2_CID_HFLIP,
418 .name = "Mirror",
419 .minimum = 0,
420 .maximum = 1,
421 .type = V4L2_CTRL_TYPE_BOOLEAN,
422 },
423 /* --- audio --- */
424 {
425 .id = V4L2_CID_AUDIO_MUTE,
426 .name = "Mute",
427 .minimum = 0,
428 .maximum = 1,
429 .type = V4L2_CTRL_TYPE_BOOLEAN,
430 },{
431 .id = V4L2_CID_AUDIO_VOLUME,
432 .name = "Volume",
433 .minimum = -15,
434 .maximum = 15,
435 .step = 1,
436 .default_value = 0,
437 .type = V4L2_CTRL_TYPE_INTEGER,
438 },
439 /* --- private --- */
440 {
441 .id = V4L2_CID_PRIVATE_INVERT,
442 .name = "Invert",
443 .minimum = 0,
444 .maximum = 1,
445 .type = V4L2_CTRL_TYPE_BOOLEAN,
446 },{
447 .id = V4L2_CID_PRIVATE_Y_ODD,
448 .name = "y offset odd field",
449 .minimum = 0,
450 .maximum = 128,
451 .step = 1,
452 .default_value = 0,
453 .type = V4L2_CTRL_TYPE_INTEGER,
454 },{
455 .id = V4L2_CID_PRIVATE_Y_EVEN,
456 .name = "y offset even field",
457 .minimum = 0,
458 .maximum = 128,
459 .step = 1,
460 .default_value = 0,
461 .type = V4L2_CTRL_TYPE_INTEGER,
462 },{
463 .id = V4L2_CID_PRIVATE_AUTOMUTE,
464 .name = "automute",
465 .minimum = 0,
466 .maximum = 1,
467 .default_value = 1,
468 .type = V4L2_CTRL_TYPE_BOOLEAN,
469 }
470};
471static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
472
473static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
474{
475 unsigned int i;
476
477 for (i = 0; i < CTRLS; i++)
478 if (video_ctrls[i].id == id)
479 return video_ctrls+i;
480 return NULL;
481}
482
483static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
484{
485 unsigned int i;
486
487 for (i = 0; i < FORMATS; i++)
488 if (formats[i].fourcc == fourcc)
489 return formats+i;
490 return NULL;
491}
492
493/* ----------------------------------------------------------------------- */
494/* resource management */
495
496static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
497{
498 if (fh->resources & bit)
499 /* have it already allocated */
500 return 1;
501
502 /* is it free? */
503 mutex_lock(&dev->lock);
504 if (dev->resources & bit) {
505 /* no, someone else uses it */
506 mutex_unlock(&dev->lock);
507 return 0;
508 }
509 /* it's free, grab it */
510 fh->resources |= bit;
511 dev->resources |= bit;
512 dprintk("res: get %d\n",bit);
513 mutex_unlock(&dev->lock);
514 return 1;
515}
516
517static int res_check(struct saa7134_fh *fh, unsigned int bit)
518{
519 return (fh->resources & bit);
520}
521
522static int res_locked(struct saa7134_dev *dev, unsigned int bit)
523{
524 return (dev->resources & bit);
525}
526
527static
528void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
529{
530 BUG_ON((fh->resources & bits) != bits);
531
532 mutex_lock(&dev->lock);
533 fh->resources &= ~bits;
534 dev->resources &= ~bits;
535 dprintk("res: put %d\n",bits);
536 mutex_unlock(&dev->lock);
537}
538
539/* ------------------------------------------------------------------ */
540
541static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
542{
543 dprintk("set tv norm = %s\n",norm->name);
544 dev->tvnorm = norm;
545
546 /* setup cropping */
547 dev->crop_bounds.left = norm->h_start;
548 dev->crop_defrect.left = norm->h_start;
549 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
550 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
551
552 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
553 dev->crop_defrect.top = norm->video_v_start*2;
554 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
555 - dev->crop_bounds.top;
556 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
557
558 dev->crop_current = dev->crop_defrect;
559
560 saa7134_set_tvnorm_hw(dev);
561}
562
563static void video_mux(struct saa7134_dev *dev, int input)
564{
565 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
566 dev->ctl_input = input;
567 set_tvnorm(dev, dev->tvnorm);
568 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
569}
570
571
572static void saa7134_set_decoder(struct saa7134_dev *dev)
573{
574 int luma_control, sync_control, mux;
575
576 struct saa7134_tvnorm *norm = dev->tvnorm;
577 mux = card_in(dev, dev->ctl_input).vmux;
578
579 luma_control = norm->luma_control;
580 sync_control = norm->sync_control;
581
582 if (mux > 5)
583 luma_control |= 0x80; /* svideo */
584 if (noninterlaced || dev->nosignal)
585 sync_control |= 0x20;
586
587 /* setup video decoder */
588 saa_writeb(SAA7134_INCR_DELAY, 0x08);
589 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
590 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
591
592 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
593 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
594 saa_writeb(SAA7134_HSYNC_START, 0xeb);
595 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
596 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
597
598 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
599 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
600 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
601
602 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
603 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
604
605 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
606 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
607
608 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
609 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
610 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
611
612 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
613 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
614
615 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
616 saa_writeb(SAA7134_VGATE_START, 0x11);
617 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
618 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
619 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
620 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
621}
622
623void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
624{
625 saa7134_set_decoder(dev);
626
627 if (card_in(dev, dev->ctl_input).tv)
628 saa_call_all(dev, core, s_std, dev->tvnorm->id);
629 /* Set the correct norm for the saa6752hs. This function
630 does nothing if there is no saa6752hs. */
631 saa_call_empress(dev, core, s_std, dev->tvnorm->id);
632}
633
634static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
635{
636 static const struct {
637 int xpsc;
638 int xacl;
639 int xc2_1;
640 int xdcg;
641 int vpfy;
642 } vals[] = {
643 /* XPSC XACL XC2_1 XDCG VPFY */
644 { 1, 0, 0, 0, 0 },
645 { 2, 2, 1, 2, 2 },
646 { 3, 4, 1, 3, 2 },
647 { 4, 8, 1, 4, 2 },
648 { 5, 8, 1, 4, 2 },
649 { 6, 8, 1, 4, 3 },
650 { 7, 8, 1, 4, 3 },
651 { 8, 15, 0, 4, 3 },
652 { 9, 15, 0, 4, 3 },
653 { 10, 16, 1, 5, 3 },
654 };
655 static const int count = ARRAY_SIZE(vals);
656 int i;
657
658 for (i = 0; i < count; i++)
659 if (vals[i].xpsc == prescale)
660 break;
661 if (i == count)
662 return;
663
664 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
665 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
666 saa_writeb(SAA7134_LEVEL_CTRL(task),
667 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
668 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
669 (vals[i].vpfy << 2) | vals[i].vpfy);
670}
671
672static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
673{
674 int val,mirror;
675
676 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
677 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
678
679 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
680 if (yscale < 2048) {
681 /* LPI */
682 dprintk("yscale LPI yscale=%d\n",yscale);
683 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
684 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
685 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
686 } else {
687 /* ACM */
688 val = 0x40 * 1024 / yscale;
689 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
690 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
691 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
692 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
693 }
694 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
695}
696
697static void set_size(struct saa7134_dev *dev, int task,
698 int width, int height, int interlace)
699{
700 int prescale,xscale,yscale,y_even,y_odd;
701 int h_start, h_stop, v_start, v_stop;
702 int div = interlace ? 2 : 1;
703
704 /* setup video scaler */
705 h_start = dev->crop_current.left;
706 v_start = dev->crop_current.top/2;
707 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
708 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
709
710 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
711 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
712 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
713 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
714 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
715 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
716 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
717 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
718
719 prescale = dev->crop_current.width / width;
720 if (0 == prescale)
721 prescale = 1;
722 xscale = 1024 * dev->crop_current.width / prescale / width;
723 yscale = 512 * div * dev->crop_current.height / height;
724 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
725 set_h_prescale(dev,task,prescale);
726 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
727 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
728 set_v_scale(dev,task,yscale);
729
730 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
731 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
732 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
733 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
734
735 /* deinterlace y offsets */
736 y_odd = dev->ctl_y_odd;
737 y_even = dev->ctl_y_even;
738 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
739 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
740 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
741 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
742}
743
744/* ------------------------------------------------------------------ */
745
746struct cliplist {
747 __u16 position;
748 __u8 enable;
749 __u8 disable;
750};
751
752static void set_cliplist(struct saa7134_dev *dev, int reg,
753 struct cliplist *cl, int entries, char *name)
754{
755 __u8 winbits = 0;
756 int i;
757
758 for (i = 0; i < entries; i++) {
759 winbits |= cl[i].enable;
760 winbits &= ~cl[i].disable;
761 if (i < 15 && cl[i].position == cl[i+1].position)
762 continue;
763 saa_writeb(reg + 0, winbits);
764 saa_writeb(reg + 2, cl[i].position & 0xff);
765 saa_writeb(reg + 3, cl[i].position >> 8);
766 dprintk("clip: %s winbits=%02x pos=%d\n",
767 name,winbits,cl[i].position);
768 reg += 8;
769 }
770 for (; reg < 0x400; reg += 8) {
771 saa_writeb(reg+ 0, 0);
772 saa_writeb(reg + 1, 0);
773 saa_writeb(reg + 2, 0);
774 saa_writeb(reg + 3, 0);
775 }
776}
777
778static int clip_range(int val)
779{
780 if (val < 0)
781 val = 0;
782 return val;
783}
784
785/* Sort into smallest position first order */
786static int cliplist_cmp(const void *a, const void *b)
787{
788 const struct cliplist *cla = a;
789 const struct cliplist *clb = b;
790 if (cla->position < clb->position)
791 return -1;
792 if (cla->position > clb->position)
793 return 1;
794 return 0;
795}
796
797static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
798 int nclips, int interlace)
799{
800 struct cliplist col[16], row[16];
801 int cols = 0, rows = 0, i;
802 int div = interlace ? 2 : 1;
803
804 memset(col, 0, sizeof(col));
805 memset(row, 0, sizeof(row));
806 for (i = 0; i < nclips && i < 8; i++) {
807 col[cols].position = clip_range(clips[i].c.left);
808 col[cols].enable = (1 << i);
809 cols++;
810 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
811 col[cols].disable = (1 << i);
812 cols++;
813 row[rows].position = clip_range(clips[i].c.top / div);
814 row[rows].enable = (1 << i);
815 rows++;
816 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
817 / div);
818 row[rows].disable = (1 << i);
819 rows++;
820 }
821 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
822 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
823 set_cliplist(dev,0x380,col,cols,"cols");
824 set_cliplist(dev,0x384,row,rows,"rows");
825 return 0;
826}
827
828static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
829{
830 enum v4l2_field field;
831 int maxw, maxh;
832
833 if (NULL == dev->ovbuf.base)
834 return -EINVAL;
835 if (NULL == dev->ovfmt)
836 return -EINVAL;
837 if (win->w.width < 48 || win->w.height < 32)
838 return -EINVAL;
839 if (win->clipcount > 2048)
840 return -EINVAL;
841
842 field = win->field;
843 maxw = dev->crop_current.width;
844 maxh = dev->crop_current.height;
845
846 if (V4L2_FIELD_ANY == field) {
847 field = (win->w.height > maxh/2)
848 ? V4L2_FIELD_INTERLACED
849 : V4L2_FIELD_TOP;
850 }
851 switch (field) {
852 case V4L2_FIELD_TOP:
853 case V4L2_FIELD_BOTTOM:
854 maxh = maxh / 2;
855 break;
856 case V4L2_FIELD_INTERLACED:
857 break;
858 default:
859 return -EINVAL;
860 }
861
862 win->field = field;
863 if (win->w.width > maxw)
864 win->w.width = maxw;
865 if (win->w.height > maxh)
866 win->w.height = maxh;
867 return 0;
868}
869
870static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
871{
872 unsigned long base,control,bpl;
873 int err;
874
875 err = verify_preview(dev,&fh->win);
876 if (0 != err)
877 return err;
878
879 dev->ovfield = fh->win.field;
880 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
881 fh->win.w.width,fh->win.w.height,
882 fh->win.w.left,fh->win.w.top,
883 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
884
885 /* setup window + clipping */
886 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
887 V4L2_FIELD_HAS_BOTH(dev->ovfield));
888 setup_clipping(dev,fh->clips,fh->nclips,
889 V4L2_FIELD_HAS_BOTH(dev->ovfield));
890 if (dev->ovfmt->yuv)
891 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
892 else
893 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
894 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
895
896 /* dma: setup channel 1 (= Video Task B) */
897 base = (unsigned long)dev->ovbuf.base;
898 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
899 base += dev->ovfmt->depth/8 * fh->win.w.left;
900 bpl = dev->ovbuf.fmt.bytesperline;
901 control = SAA7134_RS_CONTROL_BURST_16;
902 if (dev->ovfmt->bswap)
903 control |= SAA7134_RS_CONTROL_BSWAP;
904 if (dev->ovfmt->wswap)
905 control |= SAA7134_RS_CONTROL_WSWAP;
906 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
907 saa_writel(SAA7134_RS_BA1(1),base);
908 saa_writel(SAA7134_RS_BA2(1),base+bpl);
909 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
910 saa_writel(SAA7134_RS_CONTROL(1),control);
911 } else {
912 saa_writel(SAA7134_RS_BA1(1),base);
913 saa_writel(SAA7134_RS_BA2(1),base);
914 saa_writel(SAA7134_RS_PITCH(1),bpl);
915 saa_writel(SAA7134_RS_CONTROL(1),control);
916 }
917
918 /* start dma */
919 dev->ovenable = 1;
920 saa7134_set_dmabits(dev);
921
922 return 0;
923}
924
925static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
926{
927 dev->ovenable = 0;
928 saa7134_set_dmabits(dev);
929 return 0;
930}
931
932/* ------------------------------------------------------------------ */
933
934static int buffer_activate(struct saa7134_dev *dev,
935 struct saa7134_buf *buf,
936 struct saa7134_buf *next)
937{
938 unsigned long base,control,bpl;
939 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
940
941 dprintk("buffer_activate buf=%p\n",buf);
942 buf->vb.state = VIDEOBUF_ACTIVE;
943 buf->top_seen = 0;
944
945 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
946 V4L2_FIELD_HAS_BOTH(buf->vb.field));
947 if (buf->fmt->yuv)
948 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
949 else
950 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
951 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
952
953 /* DMA: setup channel 0 (= Video Task A0) */
954 base = saa7134_buffer_base(buf);
955 if (buf->fmt->planar)
956 bpl = buf->vb.width;
957 else
958 bpl = (buf->vb.width * buf->fmt->depth) / 8;
959 control = SAA7134_RS_CONTROL_BURST_16 |
960 SAA7134_RS_CONTROL_ME |
961 (buf->pt->dma >> 12);
962 if (buf->fmt->bswap)
963 control |= SAA7134_RS_CONTROL_BSWAP;
964 if (buf->fmt->wswap)
965 control |= SAA7134_RS_CONTROL_WSWAP;
966 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
967 /* interlaced */
968 saa_writel(SAA7134_RS_BA1(0),base);
969 saa_writel(SAA7134_RS_BA2(0),base+bpl);
970 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
971 } else {
972 /* non-interlaced */
973 saa_writel(SAA7134_RS_BA1(0),base);
974 saa_writel(SAA7134_RS_BA2(0),base);
975 saa_writel(SAA7134_RS_PITCH(0),bpl);
976 }
977 saa_writel(SAA7134_RS_CONTROL(0),control);
978
979 if (buf->fmt->planar) {
980 /* DMA: setup channel 4+5 (= planar task A) */
981 bpl_uv = bpl >> buf->fmt->hshift;
982 lines_uv = buf->vb.height >> buf->fmt->vshift;
983 base2 = base + bpl * buf->vb.height;
984 base3 = base2 + bpl_uv * lines_uv;
985 if (buf->fmt->uvswap)
986 tmp = base2, base2 = base3, base3 = tmp;
987 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
988 bpl_uv,lines_uv,base2,base3);
989 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
990 /* interlaced */
991 saa_writel(SAA7134_RS_BA1(4),base2);
992 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
993 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
994 saa_writel(SAA7134_RS_BA1(5),base3);
995 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
996 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
997 } else {
998 /* non-interlaced */
999 saa_writel(SAA7134_RS_BA1(4),base2);
1000 saa_writel(SAA7134_RS_BA2(4),base2);
1001 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
1002 saa_writel(SAA7134_RS_BA1(5),base3);
1003 saa_writel(SAA7134_RS_BA2(5),base3);
1004 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
1005 }
1006 saa_writel(SAA7134_RS_CONTROL(4),control);
1007 saa_writel(SAA7134_RS_CONTROL(5),control);
1008 }
1009
1010 /* start DMA */
1011 saa7134_set_dmabits(dev);
1012 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
1013 return 0;
1014}
1015
1016static int buffer_prepare(struct videobuf_queue *q,
1017 struct videobuf_buffer *vb,
1018 enum v4l2_field field)
1019{
1020 struct saa7134_fh *fh = q->priv_data;
1021 struct saa7134_dev *dev = fh->dev;
1022 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1023 unsigned int size;
1024 int err;
1025
1026 /* sanity checks */
1027 if (NULL == fh->fmt)
1028 return -EINVAL;
1029 if (fh->width < 48 ||
1030 fh->height < 32 ||
1031 fh->width/4 > dev->crop_current.width ||
1032 fh->height/4 > dev->crop_current.height ||
1033 fh->width > dev->crop_bounds.width ||
1034 fh->height > dev->crop_bounds.height)
1035 return -EINVAL;
1036 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
1037 if (0 != buf->vb.baddr && buf->vb.bsize < size)
1038 return -EINVAL;
1039
1040 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
1041 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
1042 fh->fmt->name);
1043 if (buf->vb.width != fh->width ||
1044 buf->vb.height != fh->height ||
1045 buf->vb.size != size ||
1046 buf->vb.field != field ||
1047 buf->fmt != fh->fmt) {
1048 saa7134_dma_free(q,buf);
1049 }
1050
1051 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1052 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
1053
1054 buf->vb.width = fh->width;
1055 buf->vb.height = fh->height;
1056 buf->vb.size = size;
1057 buf->vb.field = field;
1058 buf->fmt = fh->fmt;
1059 buf->pt = &fh->pt_cap;
1060 dev->video_q.curr = NULL;
1061
1062 err = videobuf_iolock(q,&buf->vb,&dev->ovbuf);
1063 if (err)
1064 goto oops;
1065 err = saa7134_pgtable_build(dev->pci,buf->pt,
1066 dma->sglist,
1067 dma->sglen,
1068 saa7134_buffer_startpage(buf));
1069 if (err)
1070 goto oops;
1071 }
1072 buf->vb.state = VIDEOBUF_PREPARED;
1073 buf->activate = buffer_activate;
1074 return 0;
1075
1076 oops:
1077 saa7134_dma_free(q,buf);
1078 return err;
1079}
1080
1081static int
1082buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1083{
1084 struct saa7134_fh *fh = q->priv_data;
1085
1086 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1087 if (0 == *count)
1088 *count = gbuffers;
1089 *count = saa7134_buffer_count(*size,*count);
1090 return 0;
1091}
1092
1093static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1094{
1095 struct saa7134_fh *fh = q->priv_data;
1096 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1097
1098 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1099}
1100
1101static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1102{
1103 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1104
1105 saa7134_dma_free(q,buf);
1106}
1107
1108static struct videobuf_queue_ops video_qops = {
1109 .buf_setup = buffer_setup,
1110 .buf_prepare = buffer_prepare,
1111 .buf_queue = buffer_queue,
1112 .buf_release = buffer_release,
1113};
1114
1115/* ------------------------------------------------------------------ */
1116
1117int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1118{
1119 const struct v4l2_queryctrl* ctrl;
1120
1121 ctrl = ctrl_by_id(c->id);
1122 if (NULL == ctrl)
1123 return -EINVAL;
1124 switch (c->id) {
1125 case V4L2_CID_BRIGHTNESS:
1126 c->value = dev->ctl_bright;
1127 break;
1128 case V4L2_CID_HUE:
1129 c->value = dev->ctl_hue;
1130 break;
1131 case V4L2_CID_CONTRAST:
1132 c->value = dev->ctl_contrast;
1133 break;
1134 case V4L2_CID_SATURATION:
1135 c->value = dev->ctl_saturation;
1136 break;
1137 case V4L2_CID_AUDIO_MUTE:
1138 c->value = dev->ctl_mute;
1139 break;
1140 case V4L2_CID_AUDIO_VOLUME:
1141 c->value = dev->ctl_volume;
1142 break;
1143 case V4L2_CID_PRIVATE_INVERT:
1144 c->value = dev->ctl_invert;
1145 break;
1146 case V4L2_CID_HFLIP:
1147 c->value = dev->ctl_mirror;
1148 break;
1149 case V4L2_CID_PRIVATE_Y_EVEN:
1150 c->value = dev->ctl_y_even;
1151 break;
1152 case V4L2_CID_PRIVATE_Y_ODD:
1153 c->value = dev->ctl_y_odd;
1154 break;
1155 case V4L2_CID_PRIVATE_AUTOMUTE:
1156 c->value = dev->ctl_automute;
1157 break;
1158 default:
1159 return -EINVAL;
1160 }
1161 return 0;
1162}
1163EXPORT_SYMBOL_GPL(saa7134_g_ctrl_internal);
1164
1165static int saa7134_g_ctrl(struct file *file, void *priv, struct v4l2_control *c)
1166{
1167 struct saa7134_fh *fh = priv;
1168
1169 return saa7134_g_ctrl_internal(fh->dev, fh, c);
1170}
1171
1172int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c)
1173{
1174 const struct v4l2_queryctrl* ctrl;
1175 unsigned long flags;
1176 int restart_overlay = 0;
1177 int err;
1178
1179 /* When called from the empress code fh == NULL.
1180 That needs to be fixed somehow, but for now this is
1181 good enough. */
1182 if (fh) {
1183 err = v4l2_prio_check(&dev->prio, fh->prio);
1184 if (0 != err)
1185 return err;
1186 }
1187 err = -EINVAL;
1188
1189 mutex_lock(&dev->lock);
1190
1191 ctrl = ctrl_by_id(c->id);
1192 if (NULL == ctrl)
1193 goto error;
1194
1195 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1196 switch (ctrl->type) {
1197 case V4L2_CTRL_TYPE_BOOLEAN:
1198 case V4L2_CTRL_TYPE_MENU:
1199 case V4L2_CTRL_TYPE_INTEGER:
1200 if (c->value < ctrl->minimum)
1201 c->value = ctrl->minimum;
1202 if (c->value > ctrl->maximum)
1203 c->value = ctrl->maximum;
1204 break;
1205 default:
1206 /* nothing */;
1207 };
1208 switch (c->id) {
1209 case V4L2_CID_BRIGHTNESS:
1210 dev->ctl_bright = c->value;
1211 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1212 break;
1213 case V4L2_CID_HUE:
1214 dev->ctl_hue = c->value;
1215 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1216 break;
1217 case V4L2_CID_CONTRAST:
1218 dev->ctl_contrast = c->value;
1219 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1220 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1221 break;
1222 case V4L2_CID_SATURATION:
1223 dev->ctl_saturation = c->value;
1224 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1225 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1226 break;
1227 case V4L2_CID_AUDIO_MUTE:
1228 dev->ctl_mute = c->value;
1229 saa7134_tvaudio_setmute(dev);
1230 break;
1231 case V4L2_CID_AUDIO_VOLUME:
1232 dev->ctl_volume = c->value;
1233 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1234 break;
1235 case V4L2_CID_PRIVATE_INVERT:
1236 dev->ctl_invert = c->value;
1237 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1238 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1239 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1240 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1241 break;
1242 case V4L2_CID_HFLIP:
1243 dev->ctl_mirror = c->value;
1244 restart_overlay = 1;
1245 break;
1246 case V4L2_CID_PRIVATE_Y_EVEN:
1247 dev->ctl_y_even = c->value;
1248 restart_overlay = 1;
1249 break;
1250 case V4L2_CID_PRIVATE_Y_ODD:
1251 dev->ctl_y_odd = c->value;
1252 restart_overlay = 1;
1253 break;
1254 case V4L2_CID_PRIVATE_AUTOMUTE:
1255 {
1256 struct v4l2_priv_tun_config tda9887_cfg;
1257
1258 tda9887_cfg.tuner = TUNER_TDA9887;
1259 tda9887_cfg.priv = &dev->tda9887_conf;
1260
1261 dev->ctl_automute = c->value;
1262 if (dev->tda9887_conf) {
1263 if (dev->ctl_automute)
1264 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1265 else
1266 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1267
1268 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1269 }
1270 break;
1271 }
1272 default:
1273 goto error;
1274 }
1275 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1276 spin_lock_irqsave(&dev->slock,flags);
1277 stop_preview(dev,fh);
1278 start_preview(dev,fh);
1279 spin_unlock_irqrestore(&dev->slock,flags);
1280 }
1281 err = 0;
1282
1283error:
1284 mutex_unlock(&dev->lock);
1285 return err;
1286}
1287EXPORT_SYMBOL_GPL(saa7134_s_ctrl_internal);
1288
1289static int saa7134_s_ctrl(struct file *file, void *f, struct v4l2_control *c)
1290{
1291 struct saa7134_fh *fh = f;
1292
1293 return saa7134_s_ctrl_internal(fh->dev, fh, c);
1294}
1295
1296/* ------------------------------------------------------------------ */
1297
1298static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1299{
1300 struct videobuf_queue* q = NULL;
1301
1302 switch (fh->type) {
1303 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1304 q = &fh->cap;
1305 break;
1306 case V4L2_BUF_TYPE_VBI_CAPTURE:
1307 q = &fh->vbi;
1308 break;
1309 default:
1310 BUG();
1311 }
1312 return q;
1313}
1314
1315static int saa7134_resource(struct saa7134_fh *fh)
1316{
1317 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1318 return RESOURCE_VIDEO;
1319
1320 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1321 return RESOURCE_VBI;
1322
1323 BUG();
1324 return 0;
1325}
1326
1327static int video_open(struct file *file)
1328{
1329 struct video_device *vdev = video_devdata(file);
1330 struct saa7134_dev *dev = video_drvdata(file);
1331 struct saa7134_fh *fh;
1332 enum v4l2_buf_type type = 0;
1333 int radio = 0;
1334
1335 switch (vdev->vfl_type) {
1336 case VFL_TYPE_GRABBER:
1337 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1338 break;
1339 case VFL_TYPE_VBI:
1340 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1341 break;
1342 case VFL_TYPE_RADIO:
1343 radio = 1;
1344 break;
1345 }
1346
1347 dprintk("open dev=%s radio=%d type=%s\n", video_device_node_name(vdev),
1348 radio, v4l2_type_names[type]);
1349
1350 /* allocate + initialize per filehandle data */
1351 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1352 if (NULL == fh)
1353 return -ENOMEM;
1354
1355 file->private_data = fh;
1356 fh->dev = dev;
1357 fh->radio = radio;
1358 fh->type = type;
1359 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1360 fh->width = 720;
1361 fh->height = 576;
1362 v4l2_prio_open(&dev->prio, &fh->prio);
1363
1364 videobuf_queue_sg_init(&fh->cap, &video_qops,
1365 &dev->pci->dev, &dev->slock,
1366 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1367 V4L2_FIELD_INTERLACED,
1368 sizeof(struct saa7134_buf),
1369 fh, NULL);
1370 videobuf_queue_sg_init(&fh->vbi, &saa7134_vbi_qops,
1371 &dev->pci->dev, &dev->slock,
1372 V4L2_BUF_TYPE_VBI_CAPTURE,
1373 V4L2_FIELD_SEQ_TB,
1374 sizeof(struct saa7134_buf),
1375 fh, NULL);
1376 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1377 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1378
1379 if (fh->radio) {
1380 /* switch to radio mode */
1381 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1382 saa_call_all(dev, tuner, s_radio);
1383 } else {
1384 /* switch to video/vbi mode */
1385 video_mux(dev,dev->ctl_input);
1386 }
1387 return 0;
1388}
1389
1390static ssize_t
1391video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1392{
1393 struct saa7134_fh *fh = file->private_data;
1394
1395 switch (fh->type) {
1396 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1397 if (res_locked(fh->dev,RESOURCE_VIDEO))
1398 return -EBUSY;
1399 return videobuf_read_one(saa7134_queue(fh),
1400 data, count, ppos,
1401 file->f_flags & O_NONBLOCK);
1402 case V4L2_BUF_TYPE_VBI_CAPTURE:
1403 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1404 return -EBUSY;
1405 return videobuf_read_stream(saa7134_queue(fh),
1406 data, count, ppos, 1,
1407 file->f_flags & O_NONBLOCK);
1408 break;
1409 default:
1410 BUG();
1411 return 0;
1412 }
1413}
1414
1415static unsigned int
1416video_poll(struct file *file, struct poll_table_struct *wait)
1417{
1418 struct saa7134_fh *fh = file->private_data;
1419 struct videobuf_buffer *buf = NULL;
1420 unsigned int rc = 0;
1421
1422 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1423 return videobuf_poll_stream(file, &fh->vbi, wait);
1424
1425 if (res_check(fh,RESOURCE_VIDEO)) {
1426 mutex_lock(&fh->cap.vb_lock);
1427 if (!list_empty(&fh->cap.stream))
1428 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1429 } else {
1430 mutex_lock(&fh->cap.vb_lock);
1431 if (UNSET == fh->cap.read_off) {
1432 /* need to capture a new frame */
1433 if (res_locked(fh->dev,RESOURCE_VIDEO))
1434 goto err;
1435 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field))
1436 goto err;
1437 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1438 fh->cap.read_off = 0;
1439 }
1440 buf = fh->cap.read_buf;
1441 }
1442
1443 if (!buf)
1444 goto err;
1445
1446 poll_wait(file, &buf->done, wait);
1447 if (buf->state == VIDEOBUF_DONE ||
1448 buf->state == VIDEOBUF_ERROR)
1449 rc = POLLIN|POLLRDNORM;
1450 mutex_unlock(&fh->cap.vb_lock);
1451 return rc;
1452
1453err:
1454 mutex_unlock(&fh->cap.vb_lock);
1455 return POLLERR;
1456}
1457
1458static int video_release(struct file *file)
1459{
1460 struct saa7134_fh *fh = file->private_data;
1461 struct saa7134_dev *dev = fh->dev;
1462 struct saa6588_command cmd;
1463 unsigned long flags;
1464
1465 saa7134_tvaudio_close(dev);
1466
1467 /* turn off overlay */
1468 if (res_check(fh, RESOURCE_OVERLAY)) {
1469 spin_lock_irqsave(&dev->slock,flags);
1470 stop_preview(dev,fh);
1471 spin_unlock_irqrestore(&dev->slock,flags);
1472 res_free(dev,fh,RESOURCE_OVERLAY);
1473 }
1474
1475 /* stop video capture */
1476 if (res_check(fh, RESOURCE_VIDEO)) {
1477 videobuf_streamoff(&fh->cap);
1478 res_free(dev,fh,RESOURCE_VIDEO);
1479 }
1480 if (fh->cap.read_buf) {
1481 buffer_release(&fh->cap,fh->cap.read_buf);
1482 kfree(fh->cap.read_buf);
1483 }
1484
1485 /* stop vbi capture */
1486 if (res_check(fh, RESOURCE_VBI)) {
1487 videobuf_stop(&fh->vbi);
1488 res_free(dev,fh,RESOURCE_VBI);
1489 }
1490
1491 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1492 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1493 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1494 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1495 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1496
1497 saa_call_all(dev, core, s_power, 0);
1498 if (fh->radio)
1499 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1500
1501 /* free stuff */
1502 videobuf_mmap_free(&fh->cap);
1503 videobuf_mmap_free(&fh->vbi);
1504 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1505 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1506
1507 v4l2_prio_close(&dev->prio, fh->prio);
1508 file->private_data = NULL;
1509 kfree(fh);
1510 return 0;
1511}
1512
1513static int video_mmap(struct file *file, struct vm_area_struct * vma)
1514{
1515 struct saa7134_fh *fh = file->private_data;
1516
1517 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1518}
1519
1520static ssize_t radio_read(struct file *file, char __user *data,
1521 size_t count, loff_t *ppos)
1522{
1523 struct saa7134_fh *fh = file->private_data;
1524 struct saa7134_dev *dev = fh->dev;
1525 struct saa6588_command cmd;
1526
1527 cmd.block_count = count/3;
1528 cmd.buffer = data;
1529 cmd.instance = file;
1530 cmd.result = -ENODEV;
1531
1532 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1533
1534 return cmd.result;
1535}
1536
1537static unsigned int radio_poll(struct file *file, poll_table *wait)
1538{
1539 struct saa7134_fh *fh = file->private_data;
1540 struct saa7134_dev *dev = fh->dev;
1541 struct saa6588_command cmd;
1542
1543 cmd.instance = file;
1544 cmd.event_list = wait;
1545 cmd.result = -ENODEV;
1546 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1547
1548 return cmd.result;
1549}
1550
1551/* ------------------------------------------------------------------ */
1552
1553static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1554 struct v4l2_format *f)
1555{
1556 struct saa7134_fh *fh = priv;
1557 struct saa7134_dev *dev = fh->dev;
1558 struct saa7134_tvnorm *norm = dev->tvnorm;
1559
1560 f->fmt.vbi.sampling_rate = 6750000 * 4;
1561 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1562 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1563 f->fmt.vbi.offset = 64 * 4;
1564 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1565 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1566 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1567 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1568 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1569
1570 return 0;
1571}
1572
1573static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1574 struct v4l2_format *f)
1575{
1576 struct saa7134_fh *fh = priv;
1577
1578 f->fmt.pix.width = fh->width;
1579 f->fmt.pix.height = fh->height;
1580 f->fmt.pix.field = fh->cap.field;
1581 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1582 f->fmt.pix.bytesperline =
1583 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1584 f->fmt.pix.sizeimage =
1585 f->fmt.pix.height * f->fmt.pix.bytesperline;
1586 return 0;
1587}
1588
1589static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1590 struct v4l2_format *f)
1591{
1592 struct saa7134_fh *fh = priv;
1593
1594 if (saa7134_no_overlay > 0) {
1595 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1596 return -EINVAL;
1597 }
1598 f->fmt.win = fh->win;
1599
1600 return 0;
1601}
1602
1603static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1604 struct v4l2_format *f)
1605{
1606 struct saa7134_fh *fh = priv;
1607 struct saa7134_dev *dev = fh->dev;
1608 struct saa7134_format *fmt;
1609 enum v4l2_field field;
1610 unsigned int maxw, maxh;
1611
1612 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1613 if (NULL == fmt)
1614 return -EINVAL;
1615
1616 field = f->fmt.pix.field;
1617 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1618 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1619
1620 if (V4L2_FIELD_ANY == field) {
1621 field = (f->fmt.pix.height > maxh/2)
1622 ? V4L2_FIELD_INTERLACED
1623 : V4L2_FIELD_BOTTOM;
1624 }
1625 switch (field) {
1626 case V4L2_FIELD_TOP:
1627 case V4L2_FIELD_BOTTOM:
1628 maxh = maxh / 2;
1629 break;
1630 case V4L2_FIELD_INTERLACED:
1631 break;
1632 default:
1633 return -EINVAL;
1634 }
1635
1636 f->fmt.pix.field = field;
1637 if (f->fmt.pix.width < 48)
1638 f->fmt.pix.width = 48;
1639 if (f->fmt.pix.height < 32)
1640 f->fmt.pix.height = 32;
1641 if (f->fmt.pix.width > maxw)
1642 f->fmt.pix.width = maxw;
1643 if (f->fmt.pix.height > maxh)
1644 f->fmt.pix.height = maxh;
1645 f->fmt.pix.width &= ~0x03;
1646 f->fmt.pix.bytesperline =
1647 (f->fmt.pix.width * fmt->depth) >> 3;
1648 f->fmt.pix.sizeimage =
1649 f->fmt.pix.height * f->fmt.pix.bytesperline;
1650
1651 return 0;
1652}
1653
1654static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1655 struct v4l2_format *f)
1656{
1657 struct saa7134_fh *fh = priv;
1658 struct saa7134_dev *dev = fh->dev;
1659
1660 if (saa7134_no_overlay > 0) {
1661 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1662 return -EINVAL;
1663 }
1664
1665 return verify_preview(dev, &f->fmt.win);
1666}
1667
1668static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1669 struct v4l2_format *f)
1670{
1671 struct saa7134_fh *fh = priv;
1672 int err;
1673
1674 err = saa7134_try_fmt_vid_cap(file, priv, f);
1675 if (0 != err)
1676 return err;
1677
1678 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1679 fh->width = f->fmt.pix.width;
1680 fh->height = f->fmt.pix.height;
1681 fh->cap.field = f->fmt.pix.field;
1682 return 0;
1683}
1684
1685static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1686 struct v4l2_format *f)
1687{
1688 struct saa7134_fh *fh = priv;
1689 struct saa7134_dev *dev = fh->dev;
1690 int err;
1691 unsigned long flags;
1692
1693 if (saa7134_no_overlay > 0) {
1694 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1695 return -EINVAL;
1696 }
1697 err = verify_preview(dev, &f->fmt.win);
1698 if (0 != err)
1699 return err;
1700
1701 mutex_lock(&dev->lock);
1702
1703 fh->win = f->fmt.win;
1704 fh->nclips = f->fmt.win.clipcount;
1705
1706 if (fh->nclips > 8)
1707 fh->nclips = 8;
1708
1709 if (copy_from_user(fh->clips, f->fmt.win.clips,
1710 sizeof(struct v4l2_clip)*fh->nclips)) {
1711 mutex_unlock(&dev->lock);
1712 return -EFAULT;
1713 }
1714
1715 if (res_check(fh, RESOURCE_OVERLAY)) {
1716 spin_lock_irqsave(&dev->slock, flags);
1717 stop_preview(dev, fh);
1718 start_preview(dev, fh);
1719 spin_unlock_irqrestore(&dev->slock, flags);
1720 }
1721
1722 mutex_unlock(&dev->lock);
1723 return 0;
1724}
1725
1726int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c)
1727{
1728 const struct v4l2_queryctrl *ctrl;
1729
1730 if ((c->id < V4L2_CID_BASE ||
1731 c->id >= V4L2_CID_LASTP1) &&
1732 (c->id < V4L2_CID_PRIVATE_BASE ||
1733 c->id >= V4L2_CID_PRIVATE_LASTP1))
1734 return -EINVAL;
1735 ctrl = ctrl_by_id(c->id);
1736 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1737 return 0;
1738}
1739EXPORT_SYMBOL_GPL(saa7134_queryctrl);
1740
1741static int saa7134_enum_input(struct file *file, void *priv,
1742 struct v4l2_input *i)
1743{
1744 struct saa7134_fh *fh = priv;
1745 struct saa7134_dev *dev = fh->dev;
1746 unsigned int n;
1747
1748 n = i->index;
1749 if (n >= SAA7134_INPUT_MAX)
1750 return -EINVAL;
1751 if (NULL == card_in(dev, i->index).name)
1752 return -EINVAL;
1753 i->index = n;
1754 i->type = V4L2_INPUT_TYPE_CAMERA;
1755 strcpy(i->name, card_in(dev, n).name);
1756 if (card_in(dev, n).tv)
1757 i->type = V4L2_INPUT_TYPE_TUNER;
1758 i->audioset = 1;
1759 if (n == dev->ctl_input) {
1760 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1761 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1762
1763 if (0 != (v1 & 0x40))
1764 i->status |= V4L2_IN_ST_NO_H_LOCK;
1765 if (0 != (v2 & 0x40))
1766 i->status |= V4L2_IN_ST_NO_SYNC;
1767 if (0 != (v2 & 0x0e))
1768 i->status |= V4L2_IN_ST_MACROVISION;
1769 }
1770 i->std = SAA7134_NORMS;
1771 return 0;
1772}
1773
1774static int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1775{
1776 struct saa7134_fh *fh = priv;
1777 struct saa7134_dev *dev = fh->dev;
1778
1779 *i = dev->ctl_input;
1780 return 0;
1781}
1782
1783static int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1784{
1785 struct saa7134_fh *fh = priv;
1786 struct saa7134_dev *dev = fh->dev;
1787 int err;
1788
1789 err = v4l2_prio_check(&dev->prio, fh->prio);
1790 if (0 != err)
1791 return err;
1792
1793 if (i >= SAA7134_INPUT_MAX)
1794 return -EINVAL;
1795 if (NULL == card_in(dev, i).name)
1796 return -EINVAL;
1797 mutex_lock(&dev->lock);
1798 video_mux(dev, i);
1799 mutex_unlock(&dev->lock);
1800 return 0;
1801}
1802
1803static int saa7134_querycap(struct file *file, void *priv,
1804 struct v4l2_capability *cap)
1805{
1806 struct saa7134_fh *fh = priv;
1807 struct saa7134_dev *dev = fh->dev;
1808
1809 unsigned int tuner_type = dev->tuner_type;
1810
1811 strcpy(cap->driver, "saa7134");
1812 strlcpy(cap->card, saa7134_boards[dev->board].name,
1813 sizeof(cap->card));
1814 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1815 cap->capabilities =
1816 V4L2_CAP_VIDEO_CAPTURE |
1817 V4L2_CAP_VBI_CAPTURE |
1818 V4L2_CAP_READWRITE |
1819 V4L2_CAP_STREAMING |
1820 V4L2_CAP_TUNER;
1821 if (dev->has_rds)
1822 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
1823 if (saa7134_no_overlay <= 0)
1824 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
1825
1826 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1827 cap->capabilities &= ~V4L2_CAP_TUNER;
1828 return 0;
1829}
1830
1831int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id)
1832{
1833 unsigned long flags;
1834 unsigned int i;
1835 v4l2_std_id fixup;
1836 int err;
1837
1838 /* When called from the empress code fh == NULL.
1839 That needs to be fixed somehow, but for now this is
1840 good enough. */
1841 if (fh) {
1842 err = v4l2_prio_check(&dev->prio, fh->prio);
1843 if (0 != err)
1844 return err;
1845 } else if (res_locked(dev, RESOURCE_OVERLAY)) {
1846 /* Don't change the std from the mpeg device
1847 if overlay is active. */
1848 return -EBUSY;
1849 }
1850
1851 for (i = 0; i < TVNORMS; i++)
1852 if (*id == tvnorms[i].id)
1853 break;
1854
1855 if (i == TVNORMS)
1856 for (i = 0; i < TVNORMS; i++)
1857 if (*id & tvnorms[i].id)
1858 break;
1859 if (i == TVNORMS)
1860 return -EINVAL;
1861
1862 if ((*id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1863 if (secam[0] == 'L' || secam[0] == 'l') {
1864 if (secam[1] == 'C' || secam[1] == 'c')
1865 fixup = V4L2_STD_SECAM_LC;
1866 else
1867 fixup = V4L2_STD_SECAM_L;
1868 } else {
1869 if (secam[0] == 'D' || secam[0] == 'd')
1870 fixup = V4L2_STD_SECAM_DK;
1871 else
1872 fixup = V4L2_STD_SECAM;
1873 }
1874 for (i = 0; i < TVNORMS; i++) {
1875 if (fixup == tvnorms[i].id)
1876 break;
1877 }
1878 if (i == TVNORMS)
1879 return -EINVAL;
1880 }
1881
1882 *id = tvnorms[i].id;
1883
1884 mutex_lock(&dev->lock);
1885 if (fh && res_check(fh, RESOURCE_OVERLAY)) {
1886 spin_lock_irqsave(&dev->slock, flags);
1887 stop_preview(dev, fh);
1888 spin_unlock_irqrestore(&dev->slock, flags);
1889
1890 set_tvnorm(dev, &tvnorms[i]);
1891
1892 spin_lock_irqsave(&dev->slock, flags);
1893 start_preview(dev, fh);
1894 spin_unlock_irqrestore(&dev->slock, flags);
1895 } else
1896 set_tvnorm(dev, &tvnorms[i]);
1897
1898 saa7134_tvaudio_do_scan(dev);
1899 mutex_unlock(&dev->lock);
1900 return 0;
1901}
1902EXPORT_SYMBOL_GPL(saa7134_s_std_internal);
1903
1904static int saa7134_s_std(struct file *file, void *priv, v4l2_std_id *id)
1905{
1906 struct saa7134_fh *fh = priv;
1907
1908 return saa7134_s_std_internal(fh->dev, fh, id);
1909}
1910
1911static int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1912{
1913 struct saa7134_fh *fh = priv;
1914 struct saa7134_dev *dev = fh->dev;
1915
1916 *id = dev->tvnorm->id;
1917 return 0;
1918}
1919
1920static int saa7134_cropcap(struct file *file, void *priv,
1921 struct v4l2_cropcap *cap)
1922{
1923 struct saa7134_fh *fh = priv;
1924 struct saa7134_dev *dev = fh->dev;
1925
1926 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1927 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1928 return -EINVAL;
1929 cap->bounds = dev->crop_bounds;
1930 cap->defrect = dev->crop_defrect;
1931 cap->pixelaspect.numerator = 1;
1932 cap->pixelaspect.denominator = 1;
1933 if (dev->tvnorm->id & V4L2_STD_525_60) {
1934 cap->pixelaspect.numerator = 11;
1935 cap->pixelaspect.denominator = 10;
1936 }
1937 if (dev->tvnorm->id & V4L2_STD_625_50) {
1938 cap->pixelaspect.numerator = 54;
1939 cap->pixelaspect.denominator = 59;
1940 }
1941 return 0;
1942}
1943
1944static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1945{
1946 struct saa7134_fh *fh = f;
1947 struct saa7134_dev *dev = fh->dev;
1948
1949 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1950 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1951 return -EINVAL;
1952 crop->c = dev->crop_current;
1953 return 0;
1954}
1955
1956static int saa7134_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
1957{
1958 struct saa7134_fh *fh = f;
1959 struct saa7134_dev *dev = fh->dev;
1960 struct v4l2_rect *b = &dev->crop_bounds;
1961
1962 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1963 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1964 return -EINVAL;
1965 if (crop->c.height < 0)
1966 return -EINVAL;
1967 if (crop->c.width < 0)
1968 return -EINVAL;
1969
1970 if (res_locked(fh->dev, RESOURCE_OVERLAY))
1971 return -EBUSY;
1972 if (res_locked(fh->dev, RESOURCE_VIDEO))
1973 return -EBUSY;
1974
1975 if (crop->c.top < b->top)
1976 crop->c.top = b->top;
1977 if (crop->c.top > b->top + b->height)
1978 crop->c.top = b->top + b->height;
1979 if (crop->c.height > b->top - crop->c.top + b->height)
1980 crop->c.height = b->top - crop->c.top + b->height;
1981
1982 if (crop->c.left < b->left)
1983 crop->c.left = b->left;
1984 if (crop->c.left > b->left + b->width)
1985 crop->c.left = b->left + b->width;
1986 if (crop->c.width > b->left - crop->c.left + b->width)
1987 crop->c.width = b->left - crop->c.left + b->width;
1988
1989 dev->crop_current = crop->c;
1990 return 0;
1991}
1992
1993static int saa7134_g_tuner(struct file *file, void *priv,
1994 struct v4l2_tuner *t)
1995{
1996 struct saa7134_fh *fh = priv;
1997 struct saa7134_dev *dev = fh->dev;
1998 int n;
1999
2000 if (0 != t->index)
2001 return -EINVAL;
2002 memset(t, 0, sizeof(*t));
2003 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
2004 if (card_in(dev, n).tv)
2005 break;
2006 }
2007 if (n == SAA7134_INPUT_MAX)
2008 return -EINVAL;
2009 if (NULL != card_in(dev, n).name) {
2010 strcpy(t->name, "Television");
2011 t->type = V4L2_TUNER_ANALOG_TV;
2012 t->capability = V4L2_TUNER_CAP_NORM |
2013 V4L2_TUNER_CAP_STEREO |
2014 V4L2_TUNER_CAP_LANG1 |
2015 V4L2_TUNER_CAP_LANG2;
2016 t->rangehigh = 0xffffffffUL;
2017 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
2018 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
2019 }
2020 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
2021 t->signal = 0xffff;
2022 return 0;
2023}
2024
2025static int saa7134_s_tuner(struct file *file, void *priv,
2026 struct v4l2_tuner *t)
2027{
2028 struct saa7134_fh *fh = priv;
2029 struct saa7134_dev *dev = fh->dev;
2030 int rx, mode, err;
2031
2032 err = v4l2_prio_check(&dev->prio, fh->prio);
2033 if (0 != err)
2034 return err;
2035
2036 mode = dev->thread.mode;
2037 if (UNSET == mode) {
2038 rx = saa7134_tvaudio_getstereo(dev);
2039 mode = saa7134_tvaudio_rx2mode(rx);
2040 }
2041 if (mode != t->audmode)
2042 dev->thread.mode = t->audmode;
2043
2044 return 0;
2045}
2046
2047static int saa7134_g_frequency(struct file *file, void *priv,
2048 struct v4l2_frequency *f)
2049{
2050 struct saa7134_fh *fh = priv;
2051 struct saa7134_dev *dev = fh->dev;
2052
2053 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2054 f->frequency = dev->ctl_freq;
2055
2056 return 0;
2057}
2058
2059static int saa7134_s_frequency(struct file *file, void *priv,
2060 struct v4l2_frequency *f)
2061{
2062 struct saa7134_fh *fh = priv;
2063 struct saa7134_dev *dev = fh->dev;
2064 int err;
2065
2066 err = v4l2_prio_check(&dev->prio, fh->prio);
2067 if (0 != err)
2068 return err;
2069
2070 if (0 != f->tuner)
2071 return -EINVAL;
2072 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
2073 return -EINVAL;
2074 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
2075 return -EINVAL;
2076 mutex_lock(&dev->lock);
2077 dev->ctl_freq = f->frequency;
2078
2079 saa_call_all(dev, tuner, s_frequency, f);
2080
2081 saa7134_tvaudio_do_scan(dev);
2082 mutex_unlock(&dev->lock);
2083 return 0;
2084}
2085
2086static int saa7134_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2087{
2088 strcpy(a->name, "audio");
2089 return 0;
2090}
2091
2092static int saa7134_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
2093{
2094 return 0;
2095}
2096
2097static int saa7134_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2098{
2099 struct saa7134_fh *fh = f;
2100 struct saa7134_dev *dev = fh->dev;
2101
2102 *p = v4l2_prio_max(&dev->prio);
2103 return 0;
2104}
2105
2106static int saa7134_s_priority(struct file *file, void *f,
2107 enum v4l2_priority prio)
2108{
2109 struct saa7134_fh *fh = f;
2110 struct saa7134_dev *dev = fh->dev;
2111
2112 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
2113}
2114
2115static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
2116 struct v4l2_fmtdesc *f)
2117{
2118 if (f->index >= FORMATS)
2119 return -EINVAL;
2120
2121 strlcpy(f->description, formats[f->index].name,
2122 sizeof(f->description));
2123
2124 f->pixelformat = formats[f->index].fourcc;
2125
2126 return 0;
2127}
2128
2129static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
2130 struct v4l2_fmtdesc *f)
2131{
2132 if (saa7134_no_overlay > 0) {
2133 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2134 return -EINVAL;
2135 }
2136
2137 if ((f->index >= FORMATS) || formats[f->index].planar)
2138 return -EINVAL;
2139
2140 strlcpy(f->description, formats[f->index].name,
2141 sizeof(f->description));
2142
2143 f->pixelformat = formats[f->index].fourcc;
2144
2145 return 0;
2146}
2147
2148static int saa7134_g_fbuf(struct file *file, void *f,
2149 struct v4l2_framebuffer *fb)
2150{
2151 struct saa7134_fh *fh = f;
2152 struct saa7134_dev *dev = fh->dev;
2153
2154 *fb = dev->ovbuf;
2155 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2156
2157 return 0;
2158}
2159
2160static int saa7134_s_fbuf(struct file *file, void *f,
2161 struct v4l2_framebuffer *fb)
2162{
2163 struct saa7134_fh *fh = f;
2164 struct saa7134_dev *dev = fh->dev;
2165 struct saa7134_format *fmt;
2166
2167 if (!capable(CAP_SYS_ADMIN) &&
2168 !capable(CAP_SYS_RAWIO))
2169 return -EPERM;
2170
2171 /* check args */
2172 fmt = format_by_fourcc(fb->fmt.pixelformat);
2173 if (NULL == fmt)
2174 return -EINVAL;
2175
2176 /* ok, accept it */
2177 dev->ovbuf = *fb;
2178 dev->ovfmt = fmt;
2179 if (0 == dev->ovbuf.fmt.bytesperline)
2180 dev->ovbuf.fmt.bytesperline =
2181 dev->ovbuf.fmt.width*fmt->depth/8;
2182 return 0;
2183}
2184
2185static int saa7134_overlay(struct file *file, void *f, unsigned int on)
2186{
2187 struct saa7134_fh *fh = f;
2188 struct saa7134_dev *dev = fh->dev;
2189 unsigned long flags;
2190
2191 if (on) {
2192 if (saa7134_no_overlay > 0) {
2193 dprintk("no_overlay\n");
2194 return -EINVAL;
2195 }
2196
2197 if (!res_get(dev, fh, RESOURCE_OVERLAY))
2198 return -EBUSY;
2199 spin_lock_irqsave(&dev->slock, flags);
2200 start_preview(dev, fh);
2201 spin_unlock_irqrestore(&dev->slock, flags);
2202 }
2203 if (!on) {
2204 if (!res_check(fh, RESOURCE_OVERLAY))
2205 return -EINVAL;
2206 spin_lock_irqsave(&dev->slock, flags);
2207 stop_preview(dev, fh);
2208 spin_unlock_irqrestore(&dev->slock, flags);
2209 res_free(dev, fh, RESOURCE_OVERLAY);
2210 }
2211 return 0;
2212}
2213
2214static int saa7134_reqbufs(struct file *file, void *priv,
2215 struct v4l2_requestbuffers *p)
2216{
2217 struct saa7134_fh *fh = priv;
2218 return videobuf_reqbufs(saa7134_queue(fh), p);
2219}
2220
2221static int saa7134_querybuf(struct file *file, void *priv,
2222 struct v4l2_buffer *b)
2223{
2224 struct saa7134_fh *fh = priv;
2225 return videobuf_querybuf(saa7134_queue(fh), b);
2226}
2227
2228static int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2229{
2230 struct saa7134_fh *fh = priv;
2231 return videobuf_qbuf(saa7134_queue(fh), b);
2232}
2233
2234static int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2235{
2236 struct saa7134_fh *fh = priv;
2237 return videobuf_dqbuf(saa7134_queue(fh), b,
2238 file->f_flags & O_NONBLOCK);
2239}
2240
2241static int saa7134_streamon(struct file *file, void *priv,
2242 enum v4l2_buf_type type)
2243{
2244 struct saa7134_fh *fh = priv;
2245 struct saa7134_dev *dev = fh->dev;
2246 int res = saa7134_resource(fh);
2247
2248 if (!res_get(dev, fh, res))
2249 return -EBUSY;
2250
2251 return videobuf_streamon(saa7134_queue(fh));
2252}
2253
2254static int saa7134_streamoff(struct file *file, void *priv,
2255 enum v4l2_buf_type type)
2256{
2257 int err;
2258 struct saa7134_fh *fh = priv;
2259 struct saa7134_dev *dev = fh->dev;
2260 int res = saa7134_resource(fh);
2261
2262 err = videobuf_streamoff(saa7134_queue(fh));
2263 if (err < 0)
2264 return err;
2265 res_free(dev, fh, res);
2266 return 0;
2267}
2268
2269static int saa7134_g_parm(struct file *file, void *fh,
2270 struct v4l2_streamparm *parm)
2271{
2272 return 0;
2273}
2274
2275#ifdef CONFIG_VIDEO_ADV_DEBUG
2276static int vidioc_g_register (struct file *file, void *priv,
2277 struct v4l2_dbg_register *reg)
2278{
2279 struct saa7134_fh *fh = priv;
2280 struct saa7134_dev *dev = fh->dev;
2281
2282 if (!v4l2_chip_match_host(&reg->match))
2283 return -EINVAL;
2284 reg->val = saa_readb(reg->reg);
2285 reg->size = 1;
2286 return 0;
2287}
2288
2289static int vidioc_s_register (struct file *file, void *priv,
2290 struct v4l2_dbg_register *reg)
2291{
2292 struct saa7134_fh *fh = priv;
2293 struct saa7134_dev *dev = fh->dev;
2294
2295 if (!v4l2_chip_match_host(&reg->match))
2296 return -EINVAL;
2297 saa_writeb(reg->reg&0xffffff, reg->val);
2298 return 0;
2299}
2300#endif
2301
2302static int radio_querycap(struct file *file, void *priv,
2303 struct v4l2_capability *cap)
2304{
2305 struct saa7134_fh *fh = file->private_data;
2306 struct saa7134_dev *dev = fh->dev;
2307
2308 strcpy(cap->driver, "saa7134");
2309 strlcpy(cap->card, saa7134_boards[dev->board].name, sizeof(cap->card));
2310 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
2311 cap->capabilities = V4L2_CAP_TUNER;
2312 return 0;
2313}
2314
2315static int radio_g_tuner(struct file *file, void *priv,
2316 struct v4l2_tuner *t)
2317{
2318 struct saa7134_fh *fh = file->private_data;
2319 struct saa7134_dev *dev = fh->dev;
2320
2321 if (0 != t->index)
2322 return -EINVAL;
2323
2324 memset(t, 0, sizeof(*t));
2325 strcpy(t->name, "Radio");
2326 t->type = V4L2_TUNER_RADIO;
2327
2328 saa_call_all(dev, tuner, g_tuner, t);
2329 if (dev->input->amux == TV) {
2330 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
2331 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
2332 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
2333 }
2334 return 0;
2335}
2336static int radio_s_tuner(struct file *file, void *priv,
2337 struct v4l2_tuner *t)
2338{
2339 struct saa7134_fh *fh = file->private_data;
2340 struct saa7134_dev *dev = fh->dev;
2341
2342 if (0 != t->index)
2343 return -EINVAL;
2344
2345 saa_call_all(dev, tuner, s_tuner, t);
2346 return 0;
2347}
2348
2349static int radio_enum_input(struct file *file, void *priv,
2350 struct v4l2_input *i)
2351{
2352 if (i->index != 0)
2353 return -EINVAL;
2354
2355 strcpy(i->name, "Radio");
2356 i->type = V4L2_INPUT_TYPE_TUNER;
2357
2358 return 0;
2359}
2360
2361static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
2362{
2363 *i = 0;
2364 return 0;
2365}
2366
2367static int radio_g_audio(struct file *file, void *priv,
2368 struct v4l2_audio *a)
2369{
2370 memset(a, 0, sizeof(*a));
2371 strcpy(a->name, "Radio");
2372 return 0;
2373}
2374
2375static int radio_s_audio(struct file *file, void *priv,
2376 struct v4l2_audio *a)
2377{
2378 return 0;
2379}
2380
2381static int radio_s_input(struct file *filp, void *priv, unsigned int i)
2382{
2383 return 0;
2384}
2385
2386static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
2387{
2388 return 0;
2389}
2390
2391static int radio_queryctrl(struct file *file, void *priv,
2392 struct v4l2_queryctrl *c)
2393{
2394 const struct v4l2_queryctrl *ctrl;
2395
2396 if (c->id < V4L2_CID_BASE ||
2397 c->id >= V4L2_CID_LASTP1)
2398 return -EINVAL;
2399 if (c->id == V4L2_CID_AUDIO_MUTE) {
2400 ctrl = ctrl_by_id(c->id);
2401 *c = *ctrl;
2402 } else
2403 *c = no_ctrl;
2404 return 0;
2405}
2406
2407static const struct v4l2_file_operations video_fops =
2408{
2409 .owner = THIS_MODULE,
2410 .open = video_open,
2411 .release = video_release,
2412 .read = video_read,
2413 .poll = video_poll,
2414 .mmap = video_mmap,
2415 .ioctl = video_ioctl2,
2416};
2417
2418static const struct v4l2_ioctl_ops video_ioctl_ops = {
2419 .vidioc_querycap = saa7134_querycap,
2420 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
2421 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
2422 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
2423 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
2424 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
2425 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
2426 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
2427 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
2428 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2429 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2430 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
2431 .vidioc_g_audio = saa7134_g_audio,
2432 .vidioc_s_audio = saa7134_s_audio,
2433 .vidioc_cropcap = saa7134_cropcap,
2434 .vidioc_reqbufs = saa7134_reqbufs,
2435 .vidioc_querybuf = saa7134_querybuf,
2436 .vidioc_qbuf = saa7134_qbuf,
2437 .vidioc_dqbuf = saa7134_dqbuf,
2438 .vidioc_s_std = saa7134_s_std,
2439 .vidioc_g_std = saa7134_g_std,
2440 .vidioc_enum_input = saa7134_enum_input,
2441 .vidioc_g_input = saa7134_g_input,
2442 .vidioc_s_input = saa7134_s_input,
2443 .vidioc_queryctrl = saa7134_queryctrl,
2444 .vidioc_g_ctrl = saa7134_g_ctrl,
2445 .vidioc_s_ctrl = saa7134_s_ctrl,
2446 .vidioc_streamon = saa7134_streamon,
2447 .vidioc_streamoff = saa7134_streamoff,
2448 .vidioc_g_tuner = saa7134_g_tuner,
2449 .vidioc_s_tuner = saa7134_s_tuner,
2450 .vidioc_g_crop = saa7134_g_crop,
2451 .vidioc_s_crop = saa7134_s_crop,
2452 .vidioc_g_fbuf = saa7134_g_fbuf,
2453 .vidioc_s_fbuf = saa7134_s_fbuf,
2454 .vidioc_overlay = saa7134_overlay,
2455 .vidioc_g_priority = saa7134_g_priority,
2456 .vidioc_s_priority = saa7134_s_priority,
2457 .vidioc_g_parm = saa7134_g_parm,
2458 .vidioc_g_frequency = saa7134_g_frequency,
2459 .vidioc_s_frequency = saa7134_s_frequency,
2460#ifdef CONFIG_VIDEO_ADV_DEBUG
2461 .vidioc_g_register = vidioc_g_register,
2462 .vidioc_s_register = vidioc_s_register,
2463#endif
2464};
2465
2466static const struct v4l2_file_operations radio_fops = {
2467 .owner = THIS_MODULE,
2468 .open = video_open,
2469 .read = radio_read,
2470 .release = video_release,
2471 .ioctl = video_ioctl2,
2472 .poll = radio_poll,
2473};
2474
2475static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2476 .vidioc_querycap = radio_querycap,
2477 .vidioc_g_tuner = radio_g_tuner,
2478 .vidioc_enum_input = radio_enum_input,
2479 .vidioc_g_audio = radio_g_audio,
2480 .vidioc_s_tuner = radio_s_tuner,
2481 .vidioc_s_audio = radio_s_audio,
2482 .vidioc_s_input = radio_s_input,
2483 .vidioc_s_std = radio_s_std,
2484 .vidioc_queryctrl = radio_queryctrl,
2485 .vidioc_g_input = radio_g_input,
2486 .vidioc_g_ctrl = saa7134_g_ctrl,
2487 .vidioc_s_ctrl = saa7134_s_ctrl,
2488 .vidioc_g_frequency = saa7134_g_frequency,
2489 .vidioc_s_frequency = saa7134_s_frequency,
2490};
2491
2492/* ----------------------------------------------------------- */
2493/* exported stuff */
2494
2495struct video_device saa7134_video_template = {
2496 .name = "saa7134-video",
2497 .fops = &video_fops,
2498 .ioctl_ops = &video_ioctl_ops,
2499 .tvnorms = SAA7134_NORMS,
2500 .current_norm = V4L2_STD_PAL,
2501};
2502
2503struct video_device saa7134_radio_template = {
2504 .name = "saa7134-radio",
2505 .fops = &radio_fops,
2506 .ioctl_ops = &radio_ioctl_ops,
2507};
2508
2509int saa7134_video_init1(struct saa7134_dev *dev)
2510{
2511 /* sanitycheck insmod options */
2512 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2513 gbuffers = 2;
2514 if (gbufsize < 0 || gbufsize > gbufsize_max)
2515 gbufsize = gbufsize_max;
2516 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2517
2518 /* put some sensible defaults into the data structures ... */
2519 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2520 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2521 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2522 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2523 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2524 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2525 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2526 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2527
2528 if (dev->tda9887_conf && dev->ctl_automute)
2529 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2530 dev->automute = 0;
2531
2532 INIT_LIST_HEAD(&dev->video_q.queue);
2533 init_timer(&dev->video_q.timeout);
2534 dev->video_q.timeout.function = saa7134_buffer_timeout;
2535 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2536 dev->video_q.dev = dev;
2537
2538 if (saa7134_boards[dev->board].video_out)
2539 saa7134_videoport_init(dev);
2540
2541 return 0;
2542}
2543
2544int saa7134_videoport_init(struct saa7134_dev *dev)
2545{
2546 /* enable video output */
2547 int vo = saa7134_boards[dev->board].video_out;
2548 int video_reg;
2549 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2550
2551 /* Configure videoport */
2552 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2553 video_reg = video_out[vo][1];
2554 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2555 video_reg &= ~VP_T_CODE_P_INVERTED;
2556 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2557 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2558 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2559 video_reg = video_out[vo][5];
2560 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2561 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2562 if (vid_port_opts & SET_CLOCK_INVERTED)
2563 video_reg |= VP_CLK_CTRL1_INVERTED;
2564 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2565 video_reg = video_out[vo][6];
2566 if (vid_port_opts & SET_VSYNC_OFF) {
2567 video_reg &= ~VP_VS_TYPE_MASK;
2568 video_reg |= VP_VS_TYPE_OFF;
2569 }
2570 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2571 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2572 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2573
2574 /* Start videoport */
2575 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2576
2577 return 0;
2578}
2579
2580int saa7134_video_init2(struct saa7134_dev *dev)
2581{
2582 /* init video hw */
2583 set_tvnorm(dev,&tvnorms[0]);
2584 video_mux(dev,0);
2585 saa7134_tvaudio_setmute(dev);
2586 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2587 return 0;
2588}
2589
2590void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2591{
2592 static const char *st[] = {
2593 "(no signal)", "NTSC", "PAL", "SECAM" };
2594 u32 st1,st2;
2595
2596 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2597 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2598 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2599 (st1 & 0x40) ? "not locked" : "locked",
2600 (st2 & 0x40) ? "no" : "yes",
2601 st[st1 & 0x03]);
2602 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2603
2604 if (dev->nosignal) {
2605 /* no video signal -> mute audio */
2606 if (dev->ctl_automute)
2607 dev->automute = 1;
2608 saa7134_tvaudio_setmute(dev);
2609 } else {
2610 /* wake up tvaudio audio carrier scan thread */
2611 saa7134_tvaudio_do_scan(dev);
2612 }
2613
2614 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2615 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2616 else
2617 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2618
2619 if (dev->mops && dev->mops->signal_change)
2620 dev->mops->signal_change(dev);
2621}
2622
2623
2624void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2625{
2626 enum v4l2_field field;
2627
2628 spin_lock(&dev->slock);
2629 if (dev->video_q.curr) {
2630 dev->video_fieldcount++;
2631 field = dev->video_q.curr->vb.field;
2632 if (V4L2_FIELD_HAS_BOTH(field)) {
2633 /* make sure we have seen both fields */
2634 if ((status & 0x10) == 0x00) {
2635 dev->video_q.curr->top_seen = 1;
2636 goto done;
2637 }
2638 if (!dev->video_q.curr->top_seen)
2639 goto done;
2640 } else if (field == V4L2_FIELD_TOP) {
2641 if ((status & 0x10) != 0x10)
2642 goto done;
2643 } else if (field == V4L2_FIELD_BOTTOM) {
2644 if ((status & 0x10) != 0x00)
2645 goto done;
2646 }
2647 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2648 saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE);
2649 }
2650 saa7134_buffer_next(dev,&dev->video_q);
2651
2652 done:
2653 spin_unlock(&dev->slock);
2654}
2655
2656/* ----------------------------------------------------------- */
2657/*
2658 * Local variables:
2659 * c-basic-offset: 8
2660 * End:
2661 */
diff --git a/drivers/media/pci/saa7134/saa7134.h b/drivers/media/pci/saa7134/saa7134.h
new file mode 100644
index 000000000000..c24b6512bd8f
--- /dev/null
+++ b/drivers/media/pci/saa7134/saa7134.h
@@ -0,0 +1,855 @@
1/*
2 *
3 * v4l2 device driver for philips saa7134 based TV cards
4 *
5 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#define SAA7134_VERSION "0, 2, 17"
23
24#include <linux/pci.h>
25#include <linux/i2c.h>
26#include <linux/videodev2.h>
27#include <linux/kdev_t.h>
28#include <linux/input.h>
29#include <linux/notifier.h>
30#include <linux/delay.h>
31#include <linux/mutex.h>
32
33#include <asm/io.h>
34
35#include <media/v4l2-common.h>
36#include <media/v4l2-ioctl.h>
37#include <media/v4l2-device.h>
38#include <media/tuner.h>
39#include <media/rc-core.h>
40#include <media/ir-kbd-i2c.h>
41#include <media/videobuf-dma-sg.h>
42#include <sound/core.h>
43#include <sound/pcm.h>
44#if defined(CONFIG_VIDEO_SAA7134_DVB) || defined(CONFIG_VIDEO_SAA7134_DVB_MODULE)
45#include <media/videobuf-dvb.h>
46#endif
47
48#define UNSET (-1U)
49
50/* ----------------------------------------------------------- */
51/* enums */
52
53enum saa7134_tvaudio_mode {
54 TVAUDIO_FM_MONO = 1,
55 TVAUDIO_FM_BG_STEREO = 2,
56 TVAUDIO_FM_SAT_STEREO = 3,
57 TVAUDIO_FM_K_STEREO = 4,
58 TVAUDIO_NICAM_AM = 5,
59 TVAUDIO_NICAM_FM = 6,
60};
61
62enum saa7134_audio_in {
63 TV = 1,
64 LINE1 = 2,
65 LINE2 = 3,
66 LINE2_LEFT,
67};
68
69enum saa7134_video_out {
70 CCIR656 = 1,
71};
72
73/* ----------------------------------------------------------- */
74/* static data */
75
76struct saa7134_tvnorm {
77 char *name;
78 v4l2_std_id id;
79
80 /* video decoder */
81 unsigned int sync_control;
82 unsigned int luma_control;
83 unsigned int chroma_ctrl1;
84 unsigned int chroma_gain;
85 unsigned int chroma_ctrl2;
86 unsigned int vgate_misc;
87
88 /* video scaler */
89 unsigned int h_start;
90 unsigned int h_stop;
91 unsigned int video_v_start;
92 unsigned int video_v_stop;
93 unsigned int vbi_v_start_0;
94 unsigned int vbi_v_stop_0;
95 unsigned int src_timing;
96 unsigned int vbi_v_start_1;
97};
98
99struct saa7134_tvaudio {
100 char *name;
101 v4l2_std_id std;
102 enum saa7134_tvaudio_mode mode;
103 int carr1;
104 int carr2;
105};
106
107struct saa7134_format {
108 char *name;
109 unsigned int fourcc;
110 unsigned int depth;
111 unsigned int pm;
112 unsigned int vshift; /* vertical downsampling (for planar yuv) */
113 unsigned int hshift; /* horizontal downsampling (for planar yuv) */
114 unsigned int bswap:1;
115 unsigned int wswap:1;
116 unsigned int yuv:1;
117 unsigned int planar:1;
118 unsigned int uvswap:1;
119};
120
121struct saa7134_card_ir {
122 struct rc_dev *dev;
123
124 char name[32];
125 char phys[32];
126 unsigned users;
127
128 u32 polling;
129 u32 last_gpio;
130 u32 mask_keycode, mask_keydown, mask_keyup;
131
132 bool running;
133
134 struct timer_list timer;
135
136 /* IR core raw decoding */
137 u32 raw_decode;
138};
139
140/* ----------------------------------------------------------- */
141/* card configuration */
142
143#define SAA7134_BOARD_NOAUTO UNSET
144#define SAA7134_BOARD_UNKNOWN 0
145#define SAA7134_BOARD_PROTEUS_PRO 1
146#define SAA7134_BOARD_FLYVIDEO3000 2
147#define SAA7134_BOARD_FLYVIDEO2000 3
148#define SAA7134_BOARD_EMPRESS 4
149#define SAA7134_BOARD_MONSTERTV 5
150#define SAA7134_BOARD_MD9717 6
151#define SAA7134_BOARD_TVSTATION_RDS 7
152#define SAA7134_BOARD_CINERGY400 8
153#define SAA7134_BOARD_MD5044 9
154#define SAA7134_BOARD_KWORLD 10
155#define SAA7134_BOARD_CINERGY600 11
156#define SAA7134_BOARD_MD7134 12
157#define SAA7134_BOARD_TYPHOON_90031 13
158#define SAA7134_BOARD_ELSA 14
159#define SAA7134_BOARD_ELSA_500TV 15
160#define SAA7134_BOARD_ASUSTeK_TVFM7134 16
161#define SAA7134_BOARD_VA1000POWER 17
162#define SAA7134_BOARD_BMK_MPEX_NOTUNER 18
163#define SAA7134_BOARD_VIDEOMATE_TV 19
164#define SAA7134_BOARD_CRONOS_PLUS 20
165#define SAA7134_BOARD_10MOONSTVMASTER 21
166#define SAA7134_BOARD_MD2819 22
167#define SAA7134_BOARD_BMK_MPEX_TUNER 23
168#define SAA7134_BOARD_TVSTATION_DVR 24
169#define SAA7134_BOARD_ASUSTEK_TVFM7133 25
170#define SAA7134_BOARD_PINNACLE_PCTV_STEREO 26
171#define SAA7134_BOARD_MANLI_MTV002 27
172#define SAA7134_BOARD_MANLI_MTV001 28
173#define SAA7134_BOARD_TG3000TV 29
174#define SAA7134_BOARD_ECS_TVP3XP 30
175#define SAA7134_BOARD_ECS_TVP3XP_4CB5 31
176#define SAA7134_BOARD_AVACSSMARTTV 32
177#define SAA7134_BOARD_AVERMEDIA_DVD_EZMAKER 33
178#define SAA7134_BOARD_NOVAC_PRIMETV7133 34
179#define SAA7134_BOARD_AVERMEDIA_STUDIO_305 35
180#define SAA7134_BOARD_UPMOST_PURPLE_TV 36
181#define SAA7134_BOARD_ITEMS_MTV005 37
182#define SAA7134_BOARD_CINERGY200 38
183#define SAA7134_BOARD_FLYTVPLATINUM_MINI 39
184#define SAA7134_BOARD_VIDEOMATE_TV_PVR 40
185#define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUS 41
186#define SAA7134_BOARD_SABRENT_SBTTVFM 42
187#define SAA7134_BOARD_ZOLID_XPERT_TV7134 43
188#define SAA7134_BOARD_EMPIRE_PCI_TV_RADIO_LE 44
189#define SAA7134_BOARD_AVERMEDIA_STUDIO_307 45
190#define SAA7134_BOARD_AVERMEDIA_CARDBUS 46
191#define SAA7134_BOARD_CINERGY400_CARDBUS 47
192#define SAA7134_BOARD_CINERGY600_MK3 48
193#define SAA7134_BOARD_VIDEOMATE_GOLD_PLUS 49
194#define SAA7134_BOARD_PINNACLE_300I_DVBT_PAL 50
195#define SAA7134_BOARD_PROVIDEO_PV952 51
196#define SAA7134_BOARD_AVERMEDIA_305 52
197#define SAA7134_BOARD_ASUSTeK_TVFM7135 53
198#define SAA7134_BOARD_FLYTVPLATINUM_FM 54
199#define SAA7134_BOARD_FLYDVBTDUO 55
200#define SAA7134_BOARD_AVERMEDIA_307 56
201#define SAA7134_BOARD_AVERMEDIA_GO_007_FM 57
202#define SAA7134_BOARD_ADS_INSTANT_TV 58
203#define SAA7134_BOARD_KWORLD_VSTREAM_XPERT 59
204#define SAA7134_BOARD_FLYDVBT_DUO_CARDBUS 60
205#define SAA7134_BOARD_PHILIPS_TOUGH 61
206#define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII 62
207#define SAA7134_BOARD_KWORLD_XPERT 63
208#define SAA7134_BOARD_FLYTV_DIGIMATRIX 64
209#define SAA7134_BOARD_KWORLD_TERMINATOR 65
210#define SAA7134_BOARD_YUAN_TUN900 66
211#define SAA7134_BOARD_BEHOLD_409FM 67
212#define SAA7134_BOARD_GOTVIEW_7135 68
213#define SAA7134_BOARD_PHILIPS_EUROPA 69
214#define SAA7134_BOARD_VIDEOMATE_DVBT_300 70
215#define SAA7134_BOARD_VIDEOMATE_DVBT_200 71
216#define SAA7134_BOARD_RTD_VFG7350 72
217#define SAA7134_BOARD_RTD_VFG7330 73
218#define SAA7134_BOARD_FLYTVPLATINUM_MINI2 74
219#define SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180 75
220#define SAA7134_BOARD_MONSTERTV_MOBILE 76
221#define SAA7134_BOARD_PINNACLE_PCTV_110i 77
222#define SAA7134_BOARD_ASUSTeK_P7131_DUAL 78
223#define SAA7134_BOARD_SEDNA_PC_TV_CARDBUS 79
224#define SAA7134_BOARD_ASUSTEK_DIGIMATRIX_TV 80
225#define SAA7134_BOARD_PHILIPS_TIGER 81
226#define SAA7134_BOARD_MSI_TVATANYWHERE_PLUS 82
227#define SAA7134_BOARD_CINERGY250PCI 83
228#define SAA7134_BOARD_FLYDVB_TRIO 84
229#define SAA7134_BOARD_AVERMEDIA_777 85
230#define SAA7134_BOARD_FLYDVBT_LR301 86
231#define SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331 87
232#define SAA7134_BOARD_TEVION_DVBT_220RF 88
233#define SAA7134_BOARD_ELSA_700TV 89
234#define SAA7134_BOARD_KWORLD_ATSC110 90
235#define SAA7134_BOARD_AVERMEDIA_A169_B 91
236#define SAA7134_BOARD_AVERMEDIA_A169_B1 92
237#define SAA7134_BOARD_MD7134_BRIDGE_2 93
238#define SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS 94
239#define SAA7134_BOARD_FLYVIDEO3000_NTSC 95
240#define SAA7134_BOARD_MEDION_MD8800_QUADRO 96
241#define SAA7134_BOARD_FLYDVBS_LR300 97
242#define SAA7134_BOARD_PROTEUS_2309 98
243#define SAA7134_BOARD_AVERMEDIA_A16AR 99
244#define SAA7134_BOARD_ASUS_EUROPA2_HYBRID 100
245#define SAA7134_BOARD_PINNACLE_PCTV_310i 101
246#define SAA7134_BOARD_AVERMEDIA_STUDIO_507 102
247#define SAA7134_BOARD_VIDEOMATE_DVBT_200A 103
248#define SAA7134_BOARD_HAUPPAUGE_HVR1110 104
249#define SAA7134_BOARD_CINERGY_HT_PCMCIA 105
250#define SAA7134_BOARD_ENCORE_ENLTV 106
251#define SAA7134_BOARD_ENCORE_ENLTV_FM 107
252#define SAA7134_BOARD_CINERGY_HT_PCI 108
253#define SAA7134_BOARD_PHILIPS_TIGER_S 109
254#define SAA7134_BOARD_AVERMEDIA_M102 110
255#define SAA7134_BOARD_ASUS_P7131_4871 111
256#define SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA 112
257#define SAA7134_BOARD_ECS_TVP3XP_4CB6 113
258#define SAA7134_BOARD_KWORLD_DVBT_210 114
259#define SAA7134_BOARD_SABRENT_TV_PCB05 115
260#define SAA7134_BOARD_10MOONSTVMASTER3 116
261#define SAA7134_BOARD_AVERMEDIA_SUPER_007 117
262#define SAA7134_BOARD_BEHOLD_401 118
263#define SAA7134_BOARD_BEHOLD_403 119
264#define SAA7134_BOARD_BEHOLD_403FM 120
265#define SAA7134_BOARD_BEHOLD_405 121
266#define SAA7134_BOARD_BEHOLD_405FM 122
267#define SAA7134_BOARD_BEHOLD_407 123
268#define SAA7134_BOARD_BEHOLD_407FM 124
269#define SAA7134_BOARD_BEHOLD_409 125
270#define SAA7134_BOARD_BEHOLD_505FM 126
271#define SAA7134_BOARD_BEHOLD_507_9FM 127
272#define SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM 128
273#define SAA7134_BOARD_BEHOLD_607FM_MK3 129
274#define SAA7134_BOARD_BEHOLD_M6 130
275#define SAA7134_BOARD_TWINHAN_DTV_DVB_3056 131
276#define SAA7134_BOARD_GENIUS_TVGO_A11MCE 132
277#define SAA7134_BOARD_PHILIPS_SNAKE 133
278#define SAA7134_BOARD_CREATIX_CTX953 134
279#define SAA7134_BOARD_MSI_TVANYWHERE_AD11 135
280#define SAA7134_BOARD_AVERMEDIA_CARDBUS_506 136
281#define SAA7134_BOARD_AVERMEDIA_A16D 137
282#define SAA7134_BOARD_AVERMEDIA_M115 138
283#define SAA7134_BOARD_VIDEOMATE_T750 139
284#define SAA7134_BOARD_AVERMEDIA_A700_PRO 140
285#define SAA7134_BOARD_AVERMEDIA_A700_HYBRID 141
286#define SAA7134_BOARD_BEHOLD_H6 142
287#define SAA7134_BOARD_BEHOLD_M63 143
288#define SAA7134_BOARD_BEHOLD_M6_EXTRA 144
289#define SAA7134_BOARD_AVERMEDIA_M103 145
290#define SAA7134_BOARD_ASUSTeK_P7131_ANALOG 146
291#define SAA7134_BOARD_ASUSTeK_TIGER_3IN1 147
292#define SAA7134_BOARD_ENCORE_ENLTV_FM53 148
293#define SAA7134_BOARD_AVERMEDIA_M135A 149
294#define SAA7134_BOARD_REAL_ANGEL_220 150
295#define SAA7134_BOARD_ADS_INSTANT_HDTV_PCI 151
296#define SAA7134_BOARD_ASUSTeK_TIGER 152
297#define SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG 153
298#define SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS 154
299#define SAA7134_BOARD_HAUPPAUGE_HVR1150 155
300#define SAA7134_BOARD_HAUPPAUGE_HVR1120 156
301#define SAA7134_BOARD_AVERMEDIA_STUDIO_507UA 157
302#define SAA7134_BOARD_AVERMEDIA_CARDBUS_501 158
303#define SAA7134_BOARD_BEHOLD_505RDS_MK5 159
304#define SAA7134_BOARD_BEHOLD_507RDS_MK3 160
305#define SAA7134_BOARD_BEHOLD_507RDS_MK5 161
306#define SAA7134_BOARD_BEHOLD_607FM_MK5 162
307#define SAA7134_BOARD_BEHOLD_609FM_MK3 163
308#define SAA7134_BOARD_BEHOLD_609FM_MK5 164
309#define SAA7134_BOARD_BEHOLD_607RDS_MK3 165
310#define SAA7134_BOARD_BEHOLD_607RDS_MK5 166
311#define SAA7134_BOARD_BEHOLD_609RDS_MK3 167
312#define SAA7134_BOARD_BEHOLD_609RDS_MK5 168
313#define SAA7134_BOARD_VIDEOMATE_S350 169
314#define SAA7134_BOARD_AVERMEDIA_STUDIO_505 170
315#define SAA7134_BOARD_BEHOLD_X7 171
316#define SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM 172
317#define SAA7134_BOARD_ZOLID_HYBRID_PCI 173
318#define SAA7134_BOARD_ASUS_EUROPA_HYBRID 174
319#define SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S 175
320#define SAA7134_BOARD_BEHOLD_505RDS_MK3 176
321#define SAA7134_BOARD_HAWELL_HW_404M7 177
322#define SAA7134_BOARD_BEHOLD_H7 178
323#define SAA7134_BOARD_BEHOLD_A7 179
324#define SAA7134_BOARD_AVERMEDIA_M733A 180
325#define SAA7134_BOARD_TECHNOTREND_BUDGET_T3000 181
326#define SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG 182
327#define SAA7134_BOARD_VIDEOMATE_M1F 183
328#define SAA7134_BOARD_ENCORE_ENLTV_FM3 184
329#define SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2 185
330#define SAA7134_BOARD_BEHOLD_501 186
331#define SAA7134_BOARD_BEHOLD_503FM 187
332#define SAA7134_BOARD_SENSORAY811_911 188
333#define SAA7134_BOARD_KWORLD_PC150U 189
334#define SAA7134_BOARD_ASUSTeK_PS3_100 190
335
336#define SAA7134_MAXBOARDS 32
337#define SAA7134_INPUT_MAX 8
338
339/* ----------------------------------------------------------- */
340/* Since we support 2 remote types, lets tell them apart */
341
342#define SAA7134_REMOTE_GPIO 1
343#define SAA7134_REMOTE_I2C 2
344
345/* ----------------------------------------------------------- */
346/* Video Output Port Register Initialization Options */
347
348#define SET_T_CODE_POLARITY_NON_INVERTED (1 << 0)
349#define SET_CLOCK_NOT_DELAYED (1 << 1)
350#define SET_CLOCK_INVERTED (1 << 2)
351#define SET_VSYNC_OFF (1 << 3)
352
353struct saa7134_input {
354 char *name;
355 unsigned int vmux;
356 enum saa7134_audio_in amux;
357 unsigned int gpio;
358 unsigned int tv:1;
359};
360
361enum saa7134_mpeg_type {
362 SAA7134_MPEG_UNUSED,
363 SAA7134_MPEG_EMPRESS,
364 SAA7134_MPEG_DVB,
365};
366
367enum saa7134_mpeg_ts_type {
368 SAA7134_MPEG_TS_PARALLEL = 0,
369 SAA7134_MPEG_TS_SERIAL,
370};
371
372struct saa7134_board {
373 char *name;
374 unsigned int audio_clock;
375
376 /* input switching */
377 unsigned int gpiomask;
378 struct saa7134_input inputs[SAA7134_INPUT_MAX];
379 struct saa7134_input radio;
380 struct saa7134_input mute;
381
382 /* i2c chip info */
383 unsigned int tuner_type;
384 unsigned int radio_type;
385 unsigned char tuner_addr;
386 unsigned char radio_addr;
387 unsigned char empress_addr;
388 unsigned char rds_addr;
389
390 unsigned int tda9887_conf;
391 unsigned int tuner_config;
392
393 /* peripheral I/O */
394 enum saa7134_video_out video_out;
395 enum saa7134_mpeg_type mpeg;
396 enum saa7134_mpeg_ts_type ts_type;
397 unsigned int vid_port_opts;
398 unsigned int ts_force_val:1;
399};
400
401#define card_has_radio(dev) (NULL != saa7134_boards[dev->board].radio.name)
402#define card_is_empress(dev) (SAA7134_MPEG_EMPRESS == saa7134_boards[dev->board].mpeg)
403#define card_is_dvb(dev) (SAA7134_MPEG_DVB == saa7134_boards[dev->board].mpeg)
404#define card_has_mpeg(dev) (SAA7134_MPEG_UNUSED != saa7134_boards[dev->board].mpeg)
405#define card(dev) (saa7134_boards[dev->board])
406#define card_in(dev,n) (saa7134_boards[dev->board].inputs[n])
407
408/* ----------------------------------------------------------- */
409/* device / file handle status */
410
411#define RESOURCE_OVERLAY 1
412#define RESOURCE_VIDEO 2
413#define RESOURCE_VBI 4
414
415#define INTERLACE_AUTO 0
416#define INTERLACE_ON 1
417#define INTERLACE_OFF 2
418
419#define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
420#define TS_BUFFER_TIMEOUT msecs_to_jiffies(1000) /* 1 second */
421
422struct saa7134_dev;
423struct saa7134_dma;
424
425/* saa7134 page table */
426struct saa7134_pgtable {
427 unsigned int size;
428 __le32 *cpu;
429 dma_addr_t dma;
430};
431
432/* tvaudio thread status */
433struct saa7134_thread {
434 struct task_struct *thread;
435 unsigned int scan1;
436 unsigned int scan2;
437 unsigned int mode;
438 unsigned int stopped;
439};
440
441/* buffer for one video/vbi/ts frame */
442struct saa7134_buf {
443 /* common v4l buffer stuff -- must be first */
444 struct videobuf_buffer vb;
445
446 /* saa7134 specific */
447 struct saa7134_format *fmt;
448 unsigned int top_seen;
449 int (*activate)(struct saa7134_dev *dev,
450 struct saa7134_buf *buf,
451 struct saa7134_buf *next);
452
453 /* page tables */
454 struct saa7134_pgtable *pt;
455};
456
457struct saa7134_dmaqueue {
458 struct saa7134_dev *dev;
459 struct saa7134_buf *curr;
460 struct list_head queue;
461 struct timer_list timeout;
462 unsigned int need_two;
463};
464
465/* video filehandle status */
466struct saa7134_fh {
467 struct saa7134_dev *dev;
468 unsigned int radio;
469 enum v4l2_buf_type type;
470 unsigned int resources;
471 enum v4l2_priority prio;
472
473 /* video overlay */
474 struct v4l2_window win;
475 struct v4l2_clip clips[8];
476 unsigned int nclips;
477
478 /* video capture */
479 struct saa7134_format *fmt;
480 unsigned int width,height;
481 struct videobuf_queue cap;
482 struct saa7134_pgtable pt_cap;
483
484 /* vbi capture */
485 struct videobuf_queue vbi;
486 struct saa7134_pgtable pt_vbi;
487};
488
489/* dmasound dsp status */
490struct saa7134_dmasound {
491 struct mutex lock;
492 int minor_mixer;
493 int minor_dsp;
494 unsigned int users_dsp;
495
496 /* mixer */
497 enum saa7134_audio_in input;
498 unsigned int count;
499 unsigned int line1;
500 unsigned int line2;
501
502 /* dsp */
503 unsigned int afmt;
504 unsigned int rate;
505 unsigned int channels;
506 unsigned int recording_on;
507 unsigned int dma_running;
508 unsigned int blocks;
509 unsigned int blksize;
510 unsigned int bufsize;
511 struct saa7134_pgtable pt;
512 struct videobuf_dmabuf dma;
513 unsigned int dma_blk;
514 unsigned int read_offset;
515 unsigned int read_count;
516 void * priv_data;
517 struct snd_pcm_substream *substream;
518};
519
520/* ts/mpeg status */
521struct saa7134_ts {
522 /* TS capture */
523 struct saa7134_pgtable pt_ts;
524 int nr_packets;
525 int nr_bufs;
526};
527
528/* ts/mpeg ops */
529struct saa7134_mpeg_ops {
530 enum saa7134_mpeg_type type;
531 struct list_head next;
532 int (*init)(struct saa7134_dev *dev);
533 int (*fini)(struct saa7134_dev *dev);
534 void (*signal_change)(struct saa7134_dev *dev);
535};
536
537/* global device status */
538struct saa7134_dev {
539 struct list_head devlist;
540 struct mutex lock;
541 spinlock_t slock;
542 struct v4l2_prio_state prio;
543 struct v4l2_device v4l2_dev;
544 /* workstruct for loading modules */
545 struct work_struct request_module_wk;
546
547 /* insmod option/autodetected */
548 int autodetected;
549
550 /* various device info */
551 unsigned int resources;
552 struct video_device *video_dev;
553 struct video_device *radio_dev;
554 struct video_device *vbi_dev;
555 struct saa7134_dmasound dmasound;
556
557 /* infrared remote */
558 int has_remote;
559 struct saa7134_card_ir *remote;
560
561 /* pci i/o */
562 char name[32];
563 int nr;
564 struct pci_dev *pci;
565 unsigned char pci_rev,pci_lat;
566 __u32 __iomem *lmmio;
567 __u8 __iomem *bmmio;
568
569 /* config info */
570 unsigned int board;
571 unsigned int tuner_type;
572 unsigned int radio_type;
573 unsigned char tuner_addr;
574 unsigned char radio_addr;
575
576 unsigned int tda9887_conf;
577 unsigned int gpio_value;
578
579 /* i2c i/o */
580 struct i2c_adapter i2c_adap;
581 struct i2c_client i2c_client;
582 unsigned char eedata[256];
583 int has_rds;
584
585 /* video overlay */
586 struct v4l2_framebuffer ovbuf;
587 struct saa7134_format *ovfmt;
588 unsigned int ovenable;
589 enum v4l2_field ovfield;
590
591 /* video+ts+vbi capture */
592 struct saa7134_dmaqueue video_q;
593 struct saa7134_dmaqueue vbi_q;
594 unsigned int video_fieldcount;
595 unsigned int vbi_fieldcount;
596
597 /* various v4l controls */
598 struct saa7134_tvnorm *tvnorm; /* video */
599 struct saa7134_tvaudio *tvaudio;
600 unsigned int ctl_input;
601 int ctl_bright;
602 int ctl_contrast;
603 int ctl_hue;
604 int ctl_saturation;
605 int ctl_freq;
606 int ctl_mute; /* audio */
607 int ctl_volume;
608 int ctl_invert; /* private */
609 int ctl_mirror;
610 int ctl_y_odd;
611 int ctl_y_even;
612 int ctl_automute;
613
614 /* crop */
615 struct v4l2_rect crop_bounds;
616 struct v4l2_rect crop_defrect;
617 struct v4l2_rect crop_current;
618
619 /* other global state info */
620 unsigned int automute;
621 struct saa7134_thread thread;
622 struct saa7134_input *input;
623 struct saa7134_input *hw_input;
624 unsigned int hw_mute;
625 int last_carrier;
626 int nosignal;
627 unsigned int insuspend;
628
629 /* I2C keyboard data */
630 struct IR_i2c_init_data init_data;
631
632 /* SAA7134_MPEG_* */
633 struct saa7134_ts ts;
634 struct saa7134_dmaqueue ts_q;
635 int ts_started;
636 struct saa7134_mpeg_ops *mops;
637
638 /* SAA7134_MPEG_EMPRESS only */
639 struct video_device *empress_dev;
640 struct videobuf_queue empress_tsq;
641 atomic_t empress_users;
642 struct work_struct empress_workqueue;
643 int empress_started;
644
645#if defined(CONFIG_VIDEO_SAA7134_DVB) || defined(CONFIG_VIDEO_SAA7134_DVB_MODULE)
646 /* SAA7134_MPEG_DVB only */
647 struct videobuf_dvb_frontends frontends;
648 int (*original_demod_sleep)(struct dvb_frontend *fe);
649 int (*original_set_voltage)(struct dvb_frontend *fe, fe_sec_voltage_t voltage);
650 int (*original_set_high_voltage)(struct dvb_frontend *fe, long arg);
651#endif
652 void (*gate_ctrl)(struct saa7134_dev *dev, int open);
653};
654
655/* ----------------------------------------------------------- */
656
657#define saa_readl(reg) readl(dev->lmmio + (reg))
658#define saa_writel(reg,value) writel((value), dev->lmmio + (reg));
659#define saa_andorl(reg,mask,value) \
660 writel((readl(dev->lmmio+(reg)) & ~(mask)) |\
661 ((value) & (mask)), dev->lmmio+(reg))
662#define saa_setl(reg,bit) saa_andorl((reg),(bit),(bit))
663#define saa_clearl(reg,bit) saa_andorl((reg),(bit),0)
664
665#define saa_readb(reg) readb(dev->bmmio + (reg))
666#define saa_writeb(reg,value) writeb((value), dev->bmmio + (reg));
667#define saa_andorb(reg,mask,value) \
668 writeb((readb(dev->bmmio+(reg)) & ~(mask)) |\
669 ((value) & (mask)), dev->bmmio+(reg))
670#define saa_setb(reg,bit) saa_andorb((reg),(bit),(bit))
671#define saa_clearb(reg,bit) saa_andorb((reg),(bit),0)
672
673#define saa_wait(us) { udelay(us); }
674
675#define SAA7134_NORMS (\
676 V4L2_STD_PAL | V4L2_STD_PAL_N | \
677 V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
678 V4L2_STD_NTSC | V4L2_STD_PAL_M | \
679 V4L2_STD_PAL_60)
680
681#define GRP_EMPRESS (1)
682#define saa_call_all(dev, o, f, args...) do { \
683 if (dev->gate_ctrl) \
684 dev->gate_ctrl(dev, 1); \
685 v4l2_device_call_all(&(dev)->v4l2_dev, 0, o, f , ##args); \
686 if (dev->gate_ctrl) \
687 dev->gate_ctrl(dev, 0); \
688} while (0)
689
690#define saa_call_empress(dev, o, f, args...) ({ \
691 long _rc; \
692 if (dev->gate_ctrl) \
693 dev->gate_ctrl(dev, 1); \
694 _rc = v4l2_device_call_until_err(&(dev)->v4l2_dev, \
695 GRP_EMPRESS, o, f , ##args); \
696 if (dev->gate_ctrl) \
697 dev->gate_ctrl(dev, 0); \
698 _rc; \
699})
700
701/* ----------------------------------------------------------- */
702/* saa7134-core.c */
703
704extern struct list_head saa7134_devlist;
705extern struct mutex saa7134_devlist_lock;
706extern int saa7134_no_overlay;
707
708void saa7134_track_gpio(struct saa7134_dev *dev, char *msg);
709void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value);
710
711#define SAA7134_PGTABLE_SIZE 4096
712
713int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt);
714int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
715 struct scatterlist *list, unsigned int length,
716 unsigned int startpage);
717void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt);
718
719int saa7134_buffer_count(unsigned int size, unsigned int count);
720int saa7134_buffer_startpage(struct saa7134_buf *buf);
721unsigned long saa7134_buffer_base(struct saa7134_buf *buf);
722
723int saa7134_buffer_queue(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
724 struct saa7134_buf *buf);
725void saa7134_buffer_finish(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
726 unsigned int state);
727void saa7134_buffer_next(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
728void saa7134_buffer_timeout(unsigned long data);
729void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf);
730
731int saa7134_set_dmabits(struct saa7134_dev *dev);
732
733extern int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
734extern int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
735
736
737/* ----------------------------------------------------------- */
738/* saa7134-cards.c */
739
740extern struct saa7134_board saa7134_boards[];
741extern const unsigned int saa7134_bcount;
742extern struct pci_device_id __devinitdata saa7134_pci_tbl[];
743
744extern int saa7134_board_init1(struct saa7134_dev *dev);
745extern int saa7134_board_init2(struct saa7134_dev *dev);
746int saa7134_tuner_callback(void *priv, int component, int command, int arg);
747
748
749/* ----------------------------------------------------------- */
750/* saa7134-i2c.c */
751
752int saa7134_i2c_register(struct saa7134_dev *dev);
753int saa7134_i2c_unregister(struct saa7134_dev *dev);
754
755
756/* ----------------------------------------------------------- */
757/* saa7134-video.c */
758
759extern unsigned int video_debug;
760extern struct video_device saa7134_video_template;
761extern struct video_device saa7134_radio_template;
762
763int saa7134_s_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c);
764int saa7134_g_ctrl_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, struct v4l2_control *c);
765int saa7134_queryctrl(struct file *file, void *priv, struct v4l2_queryctrl *c);
766int saa7134_s_std_internal(struct saa7134_dev *dev, struct saa7134_fh *fh, v4l2_std_id *id);
767
768int saa7134_videoport_init(struct saa7134_dev *dev);
769void saa7134_set_tvnorm_hw(struct saa7134_dev *dev);
770
771int saa7134_video_init1(struct saa7134_dev *dev);
772int saa7134_video_init2(struct saa7134_dev *dev);
773void saa7134_irq_video_signalchange(struct saa7134_dev *dev);
774void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status);
775
776
777/* ----------------------------------------------------------- */
778/* saa7134-ts.c */
779
780#define TS_PACKET_SIZE 188 /* TS packets 188 bytes */
781
782extern struct videobuf_queue_ops saa7134_ts_qops;
783
784int saa7134_ts_init1(struct saa7134_dev *dev);
785int saa7134_ts_fini(struct saa7134_dev *dev);
786void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status);
787
788int saa7134_ts_register(struct saa7134_mpeg_ops *ops);
789void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops);
790
791int saa7134_ts_init_hw(struct saa7134_dev *dev);
792
793int saa7134_ts_start(struct saa7134_dev *dev);
794int saa7134_ts_stop(struct saa7134_dev *dev);
795
796/* ----------------------------------------------------------- */
797/* saa7134-vbi.c */
798
799extern struct videobuf_queue_ops saa7134_vbi_qops;
800extern struct video_device saa7134_vbi_template;
801
802int saa7134_vbi_init1(struct saa7134_dev *dev);
803int saa7134_vbi_fini(struct saa7134_dev *dev);
804void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status);
805
806
807/* ----------------------------------------------------------- */
808/* saa7134-tvaudio.c */
809
810int saa7134_tvaudio_rx2mode(u32 rx);
811
812void saa7134_tvaudio_setmute(struct saa7134_dev *dev);
813void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
814 struct saa7134_input *in);
815void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level);
816int saa7134_tvaudio_getstereo(struct saa7134_dev *dev);
817
818void saa7134_tvaudio_init(struct saa7134_dev *dev);
819int saa7134_tvaudio_init2(struct saa7134_dev *dev);
820int saa7134_tvaudio_fini(struct saa7134_dev *dev);
821int saa7134_tvaudio_do_scan(struct saa7134_dev *dev);
822int saa7134_tvaudio_close(struct saa7134_dev *dev);
823
824int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value);
825
826void saa7134_enable_i2s(struct saa7134_dev *dev);
827
828/* ----------------------------------------------------------- */
829/* saa7134-oss.c */
830
831extern const struct file_operations saa7134_dsp_fops;
832extern const struct file_operations saa7134_mixer_fops;
833
834int saa7134_oss_init1(struct saa7134_dev *dev);
835int saa7134_oss_fini(struct saa7134_dev *dev);
836void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status);
837
838/* ----------------------------------------------------------- */
839/* saa7134-input.c */
840
841#if defined(CONFIG_VIDEO_SAA7134_RC)
842int saa7134_input_init1(struct saa7134_dev *dev);
843void saa7134_input_fini(struct saa7134_dev *dev);
844void saa7134_input_irq(struct saa7134_dev *dev);
845void saa7134_probe_i2c_ir(struct saa7134_dev *dev);
846int saa7134_ir_start(struct saa7134_dev *dev);
847void saa7134_ir_stop(struct saa7134_dev *dev);
848#else
849#define saa7134_input_init1(dev) ((void)0)
850#define saa7134_input_fini(dev) ((void)0)
851#define saa7134_input_irq(dev) ((void)0)
852#define saa7134_probe_i2c_ir(dev) ((void)0)
853#define saa7134_ir_start(dev) ((void)0)
854#define saa7134_ir_stop(dev) ((void)0)
855#endif
diff --git a/drivers/media/pci/saa7164/Kconfig b/drivers/media/pci/saa7164/Kconfig
new file mode 100644
index 000000000000..353263725172
--- /dev/null
+++ b/drivers/media/pci/saa7164/Kconfig
@@ -0,0 +1,18 @@
1config VIDEO_SAA7164
2 tristate "NXP SAA7164 support"
3 depends on DVB_CORE && PCI && I2C
4 select I2C_ALGOBIT
5 select FW_LOADER
6 select VIDEO_TUNER
7 select VIDEO_TVEEPROM
8 select VIDEOBUF_DVB
9 select DVB_TDA10048 if !DVB_FE_CUSTOMISE
10 select DVB_S5H1411 if !DVB_FE_CUSTOMISE
11 select MEDIA_TUNER_TDA18271 if !MEDIA_TUNER_CUSTOMISE
12 ---help---
13 This is a video4linux driver for NXP SAA7164 based
14 TV cards.
15
16 To compile this driver as a module, choose M here: the
17 module will be called saa7164
18
diff --git a/drivers/media/pci/saa7164/Makefile b/drivers/media/pci/saa7164/Makefile
new file mode 100644
index 000000000000..847110c2e14c
--- /dev/null
+++ b/drivers/media/pci/saa7164/Makefile
@@ -0,0 +1,12 @@
1saa7164-objs := saa7164-cards.o saa7164-core.o saa7164-i2c.o saa7164-dvb.o \
2 saa7164-fw.o saa7164-bus.o saa7164-cmd.o saa7164-api.o \
3 saa7164-buffer.o saa7164-encoder.o saa7164-vbi.o
4
5obj-$(CONFIG_VIDEO_SAA7164) += saa7164.o
6
7ccflags-y += -I$(srctree)/drivers/media/video
8ccflags-y += -I$(srctree)/drivers/media/tuners
9ccflags-y += -I$(srctree)/drivers/media/dvb-core
10ccflags-y += -I$(srctree)/drivers/media/dvb-frontends
11
12ccflags-y += $(extra-cflags-y) $(extra-cflags-m)
diff --git a/drivers/media/pci/saa7164/saa7164-api.c b/drivers/media/pci/saa7164/saa7164-api.c
new file mode 100644
index 000000000000..eff7135cf0e8
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-api.c
@@ -0,0 +1,1524 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/wait.h>
23#include <linux/slab.h>
24
25#include "saa7164.h"
26
27int saa7164_api_get_load_info(struct saa7164_dev *dev, struct tmFwInfoStruct *i)
28{
29 int ret;
30
31 if (!(saa_debug & DBGLVL_CPU))
32 return 0;
33
34 dprintk(DBGLVL_API, "%s()\n", __func__);
35
36 i->deviceinst = 0;
37 i->devicespec = 0;
38 i->mode = 0;
39 i->status = 0;
40
41 ret = saa7164_cmd_send(dev, 0, GET_CUR,
42 GET_FW_STATUS_CONTROL, sizeof(struct tmFwInfoStruct), i);
43 if (ret != SAA_OK)
44 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
45
46 printk(KERN_INFO "saa7164[%d]-CPU: %d percent", dev->nr, i->CPULoad);
47
48 return ret;
49}
50
51int saa7164_api_collect_debug(struct saa7164_dev *dev)
52{
53 struct tmComResDebugGetData d;
54 u8 more = 255;
55 int ret;
56
57 dprintk(DBGLVL_API, "%s()\n", __func__);
58
59 while (more--) {
60
61 memset(&d, 0, sizeof(d));
62
63 ret = saa7164_cmd_send(dev, 0, GET_CUR,
64 GET_DEBUG_DATA_CONTROL, sizeof(d), &d);
65 if (ret != SAA_OK)
66 printk(KERN_ERR "%s() error, ret = 0x%x\n",
67 __func__, ret);
68
69 if (d.dwResult != SAA_OK)
70 break;
71
72 printk(KERN_INFO "saa7164[%d]-FWMSG: %s", dev->nr,
73 d.ucDebugData);
74 }
75
76 return 0;
77}
78
79int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level)
80{
81 struct tmComResDebugSetLevel lvl;
82 int ret;
83
84 dprintk(DBGLVL_API, "%s(level=%d)\n", __func__, level);
85
86 /* Retrieve current state */
87 ret = saa7164_cmd_send(dev, 0, GET_CUR,
88 SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
89 if (ret != SAA_OK)
90 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
91
92 dprintk(DBGLVL_API, "%s() Was %d\n", __func__, lvl.dwDebugLevel);
93
94 lvl.dwDebugLevel = level;
95
96 /* set new state */
97 ret = saa7164_cmd_send(dev, 0, SET_CUR,
98 SET_DEBUG_LEVEL_CONTROL, sizeof(lvl), &lvl);
99 if (ret != SAA_OK)
100 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
101
102 return ret;
103}
104
105int saa7164_api_set_vbi_format(struct saa7164_port *port)
106{
107 struct saa7164_dev *dev = port->dev;
108 struct tmComResProbeCommit fmt, rsp;
109 int ret;
110
111 dprintk(DBGLVL_API, "%s(nr=%d, unitid=0x%x)\n", __func__,
112 port->nr, port->hwcfg.unitid);
113
114 fmt.bmHint = 0;
115 fmt.bFormatIndex = 1;
116 fmt.bFrameIndex = 1;
117
118 /* Probe, see if it can support this format */
119 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
120 SET_CUR, SAA_PROBE_CONTROL, sizeof(fmt), &fmt);
121 if (ret != SAA_OK)
122 printk(KERN_ERR "%s() set error, ret = 0x%x\n", __func__, ret);
123
124 /* See of the format change was successful */
125 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
126 GET_CUR, SAA_PROBE_CONTROL, sizeof(rsp), &rsp);
127 if (ret != SAA_OK) {
128 printk(KERN_ERR "%s() get error, ret = 0x%x\n", __func__, ret);
129 } else {
130 /* Compare requested vs received, should be same */
131 if (memcmp(&fmt, &rsp, sizeof(rsp)) == 0) {
132 dprintk(DBGLVL_API, "SET/PROBE Verified\n");
133
134 /* Ask the device to select the negotiated format */
135 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
136 SET_CUR, SAA_COMMIT_CONTROL, sizeof(fmt), &fmt);
137 if (ret != SAA_OK)
138 printk(KERN_ERR "%s() commit error, ret = 0x%x\n",
139 __func__, ret);
140
141 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid,
142 GET_CUR, SAA_COMMIT_CONTROL, sizeof(rsp), &rsp);
143 if (ret != SAA_OK)
144 printk(KERN_ERR "%s() GET commit error, ret = 0x%x\n",
145 __func__, ret);
146
147 if (memcmp(&fmt, &rsp, sizeof(rsp)) != 0) {
148 printk(KERN_ERR "%s() memcmp error, ret = 0x%x\n",
149 __func__, ret);
150 } else
151 dprintk(DBGLVL_API, "SET/COMMIT Verified\n");
152
153 dprintk(DBGLVL_API, "rsp.bmHint = 0x%x\n", rsp.bmHint);
154 dprintk(DBGLVL_API, "rsp.bFormatIndex = 0x%x\n",
155 rsp.bFormatIndex);
156 dprintk(DBGLVL_API, "rsp.bFrameIndex = 0x%x\n",
157 rsp.bFrameIndex);
158 } else
159 printk(KERN_ERR "%s() compare failed\n", __func__);
160 }
161
162 if (ret == SAA_OK)
163 dprintk(DBGLVL_API, "%s(nr=%d) Success\n", __func__, port->nr);
164
165 return ret;
166}
167
168int saa7164_api_set_gop_size(struct saa7164_port *port)
169{
170 struct saa7164_dev *dev = port->dev;
171 struct tmComResEncVideoGopStructure gs;
172 int ret;
173
174 dprintk(DBGLVL_ENC, "%s()\n", __func__);
175
176 gs.ucRefFrameDist = port->encoder_params.refdist;
177 gs.ucGOPSize = port->encoder_params.gop_size;
178 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
179 EU_VIDEO_GOP_STRUCTURE_CONTROL,
180 sizeof(gs), &gs);
181 if (ret != SAA_OK)
182 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
183
184 return ret;
185}
186
187int saa7164_api_set_encoder(struct saa7164_port *port)
188{
189 struct saa7164_dev *dev = port->dev;
190 struct tmComResEncVideoBitRate vb;
191 struct tmComResEncAudioBitRate ab;
192 int ret;
193
194 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
195 port->hwcfg.sourceid);
196
197 if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
198 port->encoder_profile = EU_PROFILE_PS_DVD;
199 else
200 port->encoder_profile = EU_PROFILE_TS_HQ;
201
202 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
203 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
204 if (ret != SAA_OK)
205 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
206
207 /* Resolution */
208 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
209 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
210 if (ret != SAA_OK)
211 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
212
213 /* Establish video bitrates */
214 if (port->encoder_params.bitrate_mode ==
215 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
216 vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_CONSTANT;
217 else
218 vb.ucVideoBitRateMode = EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK;
219 vb.dwVideoBitRate = port->encoder_params.bitrate;
220 vb.dwVideoBitRatePeak = port->encoder_params.bitrate_peak;
221 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
222 EU_VIDEO_BIT_RATE_CONTROL,
223 sizeof(struct tmComResEncVideoBitRate),
224 &vb);
225 if (ret != SAA_OK)
226 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
227
228 /* Establish audio bitrates */
229 ab.ucAudioBitRateMode = 0;
230 ab.dwAudioBitRate = 384000;
231 ab.dwAudioBitRatePeak = ab.dwAudioBitRate;
232 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
233 EU_AUDIO_BIT_RATE_CONTROL,
234 sizeof(struct tmComResEncAudioBitRate),
235 &ab);
236 if (ret != SAA_OK)
237 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
238 ret);
239
240 saa7164_api_set_aspect_ratio(port);
241 saa7164_api_set_gop_size(port);
242
243 return ret;
244}
245
246int saa7164_api_get_encoder(struct saa7164_port *port)
247{
248 struct saa7164_dev *dev = port->dev;
249 struct tmComResEncVideoBitRate v;
250 struct tmComResEncAudioBitRate a;
251 struct tmComResEncVideoInputAspectRatio ar;
252 int ret;
253
254 dprintk(DBGLVL_ENC, "%s() unitid=0x%x\n", __func__,
255 port->hwcfg.sourceid);
256
257 port->encoder_profile = 0;
258 port->video_format = 0;
259 port->video_resolution = 0;
260 port->audio_format = 0;
261
262 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
263 EU_PROFILE_CONTROL, sizeof(u8), &port->encoder_profile);
264 if (ret != SAA_OK)
265 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
266
267 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
268 EU_VIDEO_RESOLUTION_CONTROL, sizeof(u8),
269 &port->video_resolution);
270 if (ret != SAA_OK)
271 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
272
273 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
274 EU_VIDEO_FORMAT_CONTROL, sizeof(u8), &port->video_format);
275 if (ret != SAA_OK)
276 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
277
278 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
279 EU_VIDEO_BIT_RATE_CONTROL, sizeof(v), &v);
280 if (ret != SAA_OK)
281 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
282
283 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
284 EU_AUDIO_FORMAT_CONTROL, sizeof(u8), &port->audio_format);
285 if (ret != SAA_OK)
286 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
287
288 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
289 EU_AUDIO_BIT_RATE_CONTROL, sizeof(a), &a);
290 if (ret != SAA_OK)
291 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
292
293 /* Aspect Ratio */
294 ar.width = 0;
295 ar.height = 0;
296 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, GET_CUR,
297 EU_VIDEO_INPUT_ASPECT_CONTROL,
298 sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
299 if (ret != SAA_OK)
300 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
301
302 dprintk(DBGLVL_ENC, "encoder_profile = %d\n", port->encoder_profile);
303 dprintk(DBGLVL_ENC, "video_format = %d\n", port->video_format);
304 dprintk(DBGLVL_ENC, "audio_format = %d\n", port->audio_format);
305 dprintk(DBGLVL_ENC, "video_resolution= %d\n", port->video_resolution);
306 dprintk(DBGLVL_ENC, "v.ucVideoBitRateMode = %d\n",
307 v.ucVideoBitRateMode);
308 dprintk(DBGLVL_ENC, "v.dwVideoBitRate = %d\n",
309 v.dwVideoBitRate);
310 dprintk(DBGLVL_ENC, "v.dwVideoBitRatePeak = %d\n",
311 v.dwVideoBitRatePeak);
312 dprintk(DBGLVL_ENC, "a.ucVideoBitRateMode = %d\n",
313 a.ucAudioBitRateMode);
314 dprintk(DBGLVL_ENC, "a.dwVideoBitRate = %d\n",
315 a.dwAudioBitRate);
316 dprintk(DBGLVL_ENC, "a.dwVideoBitRatePeak = %d\n",
317 a.dwAudioBitRatePeak);
318 dprintk(DBGLVL_ENC, "aspect.width / height = %d:%d\n",
319 ar.width, ar.height);
320
321 return ret;
322}
323
324int saa7164_api_set_aspect_ratio(struct saa7164_port *port)
325{
326 struct saa7164_dev *dev = port->dev;
327 struct tmComResEncVideoInputAspectRatio ar;
328 int ret;
329
330 dprintk(DBGLVL_ENC, "%s(%d)\n", __func__,
331 port->encoder_params.ctl_aspect);
332
333 switch (port->encoder_params.ctl_aspect) {
334 case V4L2_MPEG_VIDEO_ASPECT_1x1:
335 ar.width = 1;
336 ar.height = 1;
337 break;
338 case V4L2_MPEG_VIDEO_ASPECT_4x3:
339 ar.width = 4;
340 ar.height = 3;
341 break;
342 case V4L2_MPEG_VIDEO_ASPECT_16x9:
343 ar.width = 16;
344 ar.height = 9;
345 break;
346 case V4L2_MPEG_VIDEO_ASPECT_221x100:
347 ar.width = 221;
348 ar.height = 100;
349 break;
350 default:
351 BUG();
352 }
353
354 dprintk(DBGLVL_ENC, "%s(%d) now %d:%d\n", __func__,
355 port->encoder_params.ctl_aspect,
356 ar.width, ar.height);
357
358 /* Aspect Ratio */
359 ret = saa7164_cmd_send(port->dev, port->hwcfg.sourceid, SET_CUR,
360 EU_VIDEO_INPUT_ASPECT_CONTROL,
361 sizeof(struct tmComResEncVideoInputAspectRatio), &ar);
362 if (ret != SAA_OK)
363 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
364
365 return ret;
366}
367
368int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl)
369{
370 struct saa7164_dev *dev = port->dev;
371 int ret;
372 u16 val;
373
374 if (ctl == PU_BRIGHTNESS_CONTROL)
375 val = port->ctl_brightness;
376 else
377 if (ctl == PU_CONTRAST_CONTROL)
378 val = port->ctl_contrast;
379 else
380 if (ctl == PU_HUE_CONTROL)
381 val = port->ctl_hue;
382 else
383 if (ctl == PU_SATURATION_CONTROL)
384 val = port->ctl_saturation;
385 else
386 if (ctl == PU_SHARPNESS_CONTROL)
387 val = port->ctl_sharpness;
388 else
389 return -EINVAL;
390
391 dprintk(DBGLVL_ENC, "%s() unitid=0x%x ctl=%d, val=%d\n",
392 __func__, port->encunit.vsourceid, ctl, val);
393
394 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, SET_CUR,
395 ctl, sizeof(u16), &val);
396 if (ret != SAA_OK)
397 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
398
399 return ret;
400}
401
402int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl)
403{
404 struct saa7164_dev *dev = port->dev;
405 int ret;
406 u16 val;
407
408 ret = saa7164_cmd_send(port->dev, port->encunit.vsourceid, GET_CUR,
409 ctl, sizeof(u16), &val);
410 if (ret != SAA_OK) {
411 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
412 return ret;
413 }
414
415 dprintk(DBGLVL_ENC, "%s() ctl=%d, val=%d\n",
416 __func__, ctl, val);
417
418 if (ctl == PU_BRIGHTNESS_CONTROL)
419 port->ctl_brightness = val;
420 else
421 if (ctl == PU_CONTRAST_CONTROL)
422 port->ctl_contrast = val;
423 else
424 if (ctl == PU_HUE_CONTROL)
425 port->ctl_hue = val;
426 else
427 if (ctl == PU_SATURATION_CONTROL)
428 port->ctl_saturation = val;
429 else
430 if (ctl == PU_SHARPNESS_CONTROL)
431 port->ctl_sharpness = val;
432
433 return ret;
434}
435
436int saa7164_api_set_videomux(struct saa7164_port *port)
437{
438 struct saa7164_dev *dev = port->dev;
439 u8 inputs[] = { 1, 2, 2, 2, 5, 5, 5 };
440 int ret;
441
442 dprintk(DBGLVL_ENC, "%s() v_mux=%d a_mux=%d\n",
443 __func__, port->mux_input, inputs[port->mux_input - 1]);
444
445 /* Audio Mute */
446 ret = saa7164_api_audio_mute(port, 1);
447 if (ret != SAA_OK)
448 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
449
450 /* Video Mux */
451 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, SET_CUR,
452 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
453 if (ret != SAA_OK)
454 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
455
456 /* Audio Mux */
457 ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
458 SU_INPUT_SELECT_CONTROL, sizeof(u8),
459 &inputs[port->mux_input - 1]);
460 if (ret != SAA_OK)
461 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
462
463 /* Audio UnMute */
464 ret = saa7164_api_audio_mute(port, 0);
465 if (ret != SAA_OK)
466 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
467
468 return ret;
469}
470
471int saa7164_api_audio_mute(struct saa7164_port *port, int mute)
472{
473 struct saa7164_dev *dev = port->dev;
474 u8 v = mute;
475 int ret;
476
477 dprintk(DBGLVL_API, "%s(%d)\n", __func__, mute);
478
479 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
480 MUTE_CONTROL, sizeof(u8), &v);
481 if (ret != SAA_OK)
482 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
483
484 return ret;
485}
486
487/* 0 = silence, 0xff = full */
488int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level)
489{
490 struct saa7164_dev *dev = port->dev;
491 s16 v, min, max;
492 int ret;
493
494 dprintk(DBGLVL_API, "%s(%d)\n", __func__, level);
495
496 /* Obtain the min/max ranges */
497 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MIN,
498 VOLUME_CONTROL, sizeof(u16), &min);
499 if (ret != SAA_OK)
500 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
501
502 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_MAX,
503 VOLUME_CONTROL, sizeof(u16), &max);
504 if (ret != SAA_OK)
505 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
506
507 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
508 (0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
509 if (ret != SAA_OK)
510 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
511
512 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__,
513 level, min, max, v);
514
515 v = level;
516 if (v < min)
517 v = min;
518 if (v > max)
519 v = max;
520
521 /* Left */
522 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
523 (0x01 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
524 if (ret != SAA_OK)
525 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
526
527 /* Right */
528 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
529 (0x02 << 8) | VOLUME_CONTROL, sizeof(s16), &v);
530 if (ret != SAA_OK)
531 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
532
533 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, GET_CUR,
534 (0x01 << 8) | VOLUME_CONTROL, sizeof(u16), &v);
535 if (ret != SAA_OK)
536 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
537
538 dprintk(DBGLVL_API, "%s(%d) min=%d max=%d cur=%d\n", __func__,
539 level, min, max, v);
540
541 return ret;
542}
543
544int saa7164_api_set_audio_std(struct saa7164_port *port)
545{
546 struct saa7164_dev *dev = port->dev;
547 struct tmComResAudioDefaults lvl;
548 struct tmComResTunerStandard tvaudio;
549 int ret;
550
551 dprintk(DBGLVL_API, "%s()\n", __func__);
552
553 /* Establish default levels */
554 lvl.ucDecoderLevel = TMHW_LEV_ADJ_DECLEV_DEFAULT;
555 lvl.ucDecoderFM_Level = TMHW_LEV_ADJ_DECLEV_DEFAULT;
556 lvl.ucMonoLevel = TMHW_LEV_ADJ_MONOLEV_DEFAULT;
557 lvl.ucNICAM_Level = TMHW_LEV_ADJ_NICLEV_DEFAULT;
558 lvl.ucSAP_Level = TMHW_LEV_ADJ_SAPLEV_DEFAULT;
559 lvl.ucADC_Level = TMHW_LEV_ADJ_ADCLEV_DEFAULT;
560 ret = saa7164_cmd_send(port->dev, port->audfeat.unitid, SET_CUR,
561 AUDIO_DEFAULT_CONTROL, sizeof(struct tmComResAudioDefaults),
562 &lvl);
563 if (ret != SAA_OK)
564 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
565
566 /* Manually select the appropriate TV audio standard */
567 if (port->encodernorm.id & V4L2_STD_NTSC) {
568 tvaudio.std = TU_STANDARD_NTSC_M;
569 tvaudio.country = 1;
570 } else {
571 tvaudio.std = TU_STANDARD_PAL_I;
572 tvaudio.country = 44;
573 }
574
575 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
576 TU_STANDARD_CONTROL, sizeof(tvaudio), &tvaudio);
577 if (ret != SAA_OK)
578 printk(KERN_ERR "%s() TU_STANDARD_CONTROL error, ret = 0x%x\n",
579 __func__, ret);
580 return ret;
581}
582
583int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect)
584{
585 struct saa7164_dev *dev = port->dev;
586 struct tmComResTunerStandardAuto p;
587 int ret;
588
589 dprintk(DBGLVL_API, "%s(%d)\n", __func__, autodetect);
590
591 /* Disable TV Audio autodetect if not already set (buggy) */
592 if (autodetect)
593 p.mode = TU_STANDARD_AUTO;
594 else
595 p.mode = TU_STANDARD_MANUAL;
596 ret = saa7164_cmd_send(port->dev, port->tunerunit.unitid, SET_CUR,
597 TU_STANDARD_AUTO_CONTROL, sizeof(p), &p);
598 if (ret != SAA_OK)
599 printk(KERN_ERR
600 "%s() TU_STANDARD_AUTO_CONTROL error, ret = 0x%x\n",
601 __func__, ret);
602
603 return ret;
604}
605
606int saa7164_api_get_videomux(struct saa7164_port *port)
607{
608 struct saa7164_dev *dev = port->dev;
609 int ret;
610
611 ret = saa7164_cmd_send(port->dev, port->vidproc.sourceid, GET_CUR,
612 SU_INPUT_SELECT_CONTROL, sizeof(u8), &port->mux_input);
613 if (ret != SAA_OK)
614 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
615
616 dprintk(DBGLVL_ENC, "%s() v_mux=%d\n",
617 __func__, port->mux_input);
618
619 return ret;
620}
621
622int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
623{
624 struct saa7164_dev *dev = port->dev;
625
626 u16 len = 0;
627 u8 buf[256];
628 int ret;
629 u8 mas;
630
631 dprintk(DBGLVL_API, "%s(nr=%d type=%d val=%x)\n", __func__,
632 port->nr, port->type, val);
633
634 if (port->nr == 0)
635 mas = 0xd0;
636 else
637 mas = 0xe0;
638
639 memset(buf, 0, sizeof(buf));
640
641 buf[0x00] = 0x04;
642 buf[0x01] = 0x00;
643 buf[0x02] = 0x00;
644 buf[0x03] = 0x00;
645
646 buf[0x04] = 0x04;
647 buf[0x05] = 0x00;
648 buf[0x06] = 0x00;
649 buf[0x07] = 0x00;
650
651 buf[0x08] = reg;
652 buf[0x09] = 0x26;
653 buf[0x0a] = mas;
654 buf[0x0b] = 0xb0;
655
656 buf[0x0c] = val;
657 buf[0x0d] = 0x00;
658 buf[0x0e] = 0x00;
659 buf[0x0f] = 0x00;
660
661 ret = saa7164_cmd_send(dev, port->ifunit.unitid, GET_LEN,
662 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
663 if (ret != SAA_OK) {
664 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
665 return -EIO;
666 }
667
668 ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
669 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
670 if (ret != SAA_OK)
671 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
672#if 0
673 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf, 16,
674 false);
675#endif
676 return ret == SAA_OK ? 0 : -EIO;
677}
678
679/* Disable the IF block AGC controls */
680int saa7164_api_configure_dif(struct saa7164_port *port, u32 std)
681{
682 struct saa7164_dev *dev = port->dev;
683 int ret = 0;
684 u8 agc_disable;
685
686 dprintk(DBGLVL_API, "%s(nr=%d, 0x%x)\n", __func__, port->nr, std);
687
688 if (std & V4L2_STD_NTSC) {
689 dprintk(DBGLVL_API, " NTSC\n");
690 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
691 agc_disable = 0;
692 } else if (std & V4L2_STD_PAL_I) {
693 dprintk(DBGLVL_API, " PAL-I\n");
694 saa7164_api_set_dif(port, 0x00, 0x08); /* Video Standard */
695 agc_disable = 0;
696 } else if (std & V4L2_STD_PAL_M) {
697 dprintk(DBGLVL_API, " PAL-M\n");
698 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
699 agc_disable = 0;
700 } else if (std & V4L2_STD_PAL_N) {
701 dprintk(DBGLVL_API, " PAL-N\n");
702 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
703 agc_disable = 0;
704 } else if (std & V4L2_STD_PAL_Nc) {
705 dprintk(DBGLVL_API, " PAL-Nc\n");
706 saa7164_api_set_dif(port, 0x00, 0x01); /* Video Standard */
707 agc_disable = 0;
708 } else if (std & V4L2_STD_PAL_B) {
709 dprintk(DBGLVL_API, " PAL-B\n");
710 saa7164_api_set_dif(port, 0x00, 0x02); /* Video Standard */
711 agc_disable = 0;
712 } else if (std & V4L2_STD_PAL_DK) {
713 dprintk(DBGLVL_API, " PAL-DK\n");
714 saa7164_api_set_dif(port, 0x00, 0x10); /* Video Standard */
715 agc_disable = 0;
716 } else if (std & V4L2_STD_SECAM_L) {
717 dprintk(DBGLVL_API, " SECAM-L\n");
718 saa7164_api_set_dif(port, 0x00, 0x20); /* Video Standard */
719 agc_disable = 0;
720 } else {
721 /* Unknown standard, assume DTV */
722 dprintk(DBGLVL_API, " Unknown (assuming DTV)\n");
723 /* Undefinded Video Standard */
724 saa7164_api_set_dif(port, 0x00, 0x80);
725 agc_disable = 1;
726 }
727
728 saa7164_api_set_dif(port, 0x48, 0xa0); /* AGC Functions 1 */
729 saa7164_api_set_dif(port, 0xc0, agc_disable); /* AGC Output Disable */
730 saa7164_api_set_dif(port, 0x7c, 0x04); /* CVBS EQ */
731 saa7164_api_set_dif(port, 0x04, 0x01); /* Active */
732 msleep(100);
733 saa7164_api_set_dif(port, 0x04, 0x00); /* Active (again) */
734 msleep(100);
735
736 return ret;
737}
738
739/* Ensure the dif is in the correct state for the operating mode
740 * (analog / dtv). We only configure the diff through the analog encoder
741 * so when we're in digital mode we need to find the appropriate encoder
742 * and use it to configure the DIF.
743 */
744int saa7164_api_initialize_dif(struct saa7164_port *port)
745{
746 struct saa7164_dev *dev = port->dev;
747 struct saa7164_port *p = NULL;
748 int ret = -EINVAL;
749 u32 std = 0;
750
751 dprintk(DBGLVL_API, "%s(nr=%d type=%d)\n", __func__,
752 port->nr, port->type);
753
754 if (port->type == SAA7164_MPEG_ENCODER) {
755 /* Pick any analog standard to init the diff.
756 * we'll come back during encoder_init'
757 * and set the correct standard if requried.
758 */
759 std = V4L2_STD_NTSC;
760 } else
761 if (port->type == SAA7164_MPEG_DVB) {
762 if (port->nr == SAA7164_PORT_TS1)
763 p = &dev->ports[SAA7164_PORT_ENC1];
764 else
765 p = &dev->ports[SAA7164_PORT_ENC2];
766 } else
767 if (port->type == SAA7164_MPEG_VBI) {
768 std = V4L2_STD_NTSC;
769 if (port->nr == SAA7164_PORT_VBI1)
770 p = &dev->ports[SAA7164_PORT_ENC1];
771 else
772 p = &dev->ports[SAA7164_PORT_ENC2];
773 } else
774 BUG();
775
776 if (p)
777 ret = saa7164_api_configure_dif(p, std);
778
779 return ret;
780}
781
782int saa7164_api_transition_port(struct saa7164_port *port, u8 mode)
783{
784 struct saa7164_dev *dev = port->dev;
785
786 int ret;
787
788 dprintk(DBGLVL_API, "%s(nr=%d unitid=0x%x,%d)\n",
789 __func__, port->nr, port->hwcfg.unitid, mode);
790
791 ret = saa7164_cmd_send(port->dev, port->hwcfg.unitid, SET_CUR,
792 SAA_STATE_CONTROL, sizeof(mode), &mode);
793 if (ret != SAA_OK)
794 printk(KERN_ERR "%s(portnr %d unitid 0x%x) error, ret = 0x%x\n",
795 __func__, port->nr, port->hwcfg.unitid, ret);
796
797 return ret;
798}
799
800int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version)
801{
802 int ret;
803
804 ret = saa7164_cmd_send(dev, 0, GET_CUR,
805 GET_FW_VERSION_CONTROL, sizeof(u32), version);
806 if (ret != SAA_OK)
807 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
808
809 return ret;
810}
811
812int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen)
813{
814 u8 reg[] = { 0x0f, 0x00 };
815
816 if (buflen < 128)
817 return -ENOMEM;
818
819 /* Assumption: Hauppauge eeprom is at 0xa0 on on bus 0 */
820 /* TODO: Pull the details from the boards struct */
821 return saa7164_api_i2c_read(&dev->i2c_bus[0], 0xa0 >> 1, sizeof(reg),
822 &reg[0], 128, buf);
823}
824
825int saa7164_api_configure_port_vbi(struct saa7164_dev *dev,
826 struct saa7164_port *port)
827{
828 struct tmComResVBIFormatDescrHeader *fmt = &port->vbi_fmt_ntsc;
829
830 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
831 dprintk(DBGLVL_API, " VideoStandard = 0x%x\n", fmt->VideoStandard);
832 dprintk(DBGLVL_API, " StartLine = %d\n", fmt->StartLine);
833 dprintk(DBGLVL_API, " EndLine = %d\n", fmt->EndLine);
834 dprintk(DBGLVL_API, " FieldRate = %d\n", fmt->FieldRate);
835 dprintk(DBGLVL_API, " bNumLines = %d\n", fmt->bNumLines);
836
837 /* Cache the hardware configuration in the port */
838
839 port->bufcounter = port->hwcfg.BARLocation;
840 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
841 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
842 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
843 port->bufptr32l = port->hwcfg.BARLocation +
844 (4 * sizeof(u32)) +
845 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
846 port->bufptr32h = port->hwcfg.BARLocation +
847 (4 * sizeof(u32)) +
848 (sizeof(u32) * port->hwcfg.buffercount);
849 port->bufptr64 = port->hwcfg.BARLocation +
850 (4 * sizeof(u32)) +
851 (sizeof(u32) * port->hwcfg.buffercount);
852 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
853 port->hwcfg.BARLocation);
854
855 dprintk(DBGLVL_API, " = VS_FORMAT_VBI (becomes dev->en[%d])\n",
856 port->nr);
857
858 return 0;
859}
860
861int saa7164_api_configure_port_mpeg2ts(struct saa7164_dev *dev,
862 struct saa7164_port *port,
863 struct tmComResTSFormatDescrHeader *tsfmt)
864{
865 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", tsfmt->bFormatIndex);
866 dprintk(DBGLVL_API, " bDataOffset = 0x%x\n", tsfmt->bDataOffset);
867 dprintk(DBGLVL_API, " bPacketLength= 0x%x\n", tsfmt->bPacketLength);
868 dprintk(DBGLVL_API, " bStrideLength= 0x%x\n", tsfmt->bStrideLength);
869 dprintk(DBGLVL_API, " bguid = (....)\n");
870
871 /* Cache the hardware configuration in the port */
872
873 port->bufcounter = port->hwcfg.BARLocation;
874 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
875 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
876 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
877 port->bufptr32l = port->hwcfg.BARLocation +
878 (4 * sizeof(u32)) +
879 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
880 port->bufptr32h = port->hwcfg.BARLocation +
881 (4 * sizeof(u32)) +
882 (sizeof(u32) * port->hwcfg.buffercount);
883 port->bufptr64 = port->hwcfg.BARLocation +
884 (4 * sizeof(u32)) +
885 (sizeof(u32) * port->hwcfg.buffercount);
886 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
887 port->hwcfg.BARLocation);
888
889 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGTS (becomes dev->ts[%d])\n",
890 port->nr);
891
892 return 0;
893}
894
895int saa7164_api_configure_port_mpeg2ps(struct saa7164_dev *dev,
896 struct saa7164_port *port,
897 struct tmComResPSFormatDescrHeader *fmt)
898{
899 dprintk(DBGLVL_API, " bFormatIndex = 0x%x\n", fmt->bFormatIndex);
900 dprintk(DBGLVL_API, " wPacketLength= 0x%x\n", fmt->wPacketLength);
901 dprintk(DBGLVL_API, " wPackLength= 0x%x\n", fmt->wPackLength);
902 dprintk(DBGLVL_API, " bPackDataType= 0x%x\n", fmt->bPackDataType);
903
904 /* Cache the hardware configuration in the port */
905 /* TODO: CHECK THIS in the port config */
906 port->bufcounter = port->hwcfg.BARLocation;
907 port->pitch = port->hwcfg.BARLocation + (2 * sizeof(u32));
908 port->bufsize = port->hwcfg.BARLocation + (3 * sizeof(u32));
909 port->bufoffset = port->hwcfg.BARLocation + (4 * sizeof(u32));
910 port->bufptr32l = port->hwcfg.BARLocation +
911 (4 * sizeof(u32)) +
912 (sizeof(u32) * port->hwcfg.buffercount) + sizeof(u32);
913 port->bufptr32h = port->hwcfg.BARLocation +
914 (4 * sizeof(u32)) +
915 (sizeof(u32) * port->hwcfg.buffercount);
916 port->bufptr64 = port->hwcfg.BARLocation +
917 (4 * sizeof(u32)) +
918 (sizeof(u32) * port->hwcfg.buffercount);
919 dprintk(DBGLVL_API, " = port->hwcfg.BARLocation = 0x%x\n",
920 port->hwcfg.BARLocation);
921
922 dprintk(DBGLVL_API, " = VS_FORMAT_MPEGPS (becomes dev->enc[%d])\n",
923 port->nr);
924
925 return 0;
926}
927
928int saa7164_api_dump_subdevs(struct saa7164_dev *dev, u8 *buf, int len)
929{
930 struct saa7164_port *tsport = NULL;
931 struct saa7164_port *encport = NULL;
932 struct saa7164_port *vbiport = NULL;
933 u32 idx, next_offset;
934 int i;
935 struct tmComResDescrHeader *hdr, *t;
936 struct tmComResExtDevDescrHeader *exthdr;
937 struct tmComResPathDescrHeader *pathhdr;
938 struct tmComResAntTermDescrHeader *anttermhdr;
939 struct tmComResTunerDescrHeader *tunerunithdr;
940 struct tmComResDMATermDescrHeader *vcoutputtermhdr;
941 struct tmComResTSFormatDescrHeader *tsfmt;
942 struct tmComResPSFormatDescrHeader *psfmt;
943 struct tmComResSelDescrHeader *psel;
944 struct tmComResProcDescrHeader *pdh;
945 struct tmComResAFeatureDescrHeader *afd;
946 struct tmComResEncoderDescrHeader *edh;
947 struct tmComResVBIFormatDescrHeader *vbifmt;
948 u32 currpath = 0;
949
950 dprintk(DBGLVL_API,
951 "%s(?,?,%d) sizeof(struct tmComResDescrHeader) = %d bytes\n",
952 __func__, len, (u32)sizeof(struct tmComResDescrHeader));
953
954 for (idx = 0; idx < (len - sizeof(struct tmComResDescrHeader));) {
955
956 hdr = (struct tmComResDescrHeader *)(buf + idx);
957
958 if (hdr->type != CS_INTERFACE)
959 return SAA_ERR_NOT_SUPPORTED;
960
961 dprintk(DBGLVL_API, "@ 0x%x =\n", idx);
962 switch (hdr->subtype) {
963 case GENERAL_REQUEST:
964 dprintk(DBGLVL_API, " GENERAL_REQUEST\n");
965 break;
966 case VC_TUNER_PATH:
967 dprintk(DBGLVL_API, " VC_TUNER_PATH\n");
968 pathhdr = (struct tmComResPathDescrHeader *)(buf + idx);
969 dprintk(DBGLVL_API, " pathid = 0x%x\n",
970 pathhdr->pathid);
971 currpath = pathhdr->pathid;
972 break;
973 case VC_INPUT_TERMINAL:
974 dprintk(DBGLVL_API, " VC_INPUT_TERMINAL\n");
975 anttermhdr =
976 (struct tmComResAntTermDescrHeader *)(buf + idx);
977 dprintk(DBGLVL_API, " terminalid = 0x%x\n",
978 anttermhdr->terminalid);
979 dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
980 anttermhdr->terminaltype);
981 switch (anttermhdr->terminaltype) {
982 case ITT_ANTENNA:
983 dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
984 break;
985 case LINE_CONNECTOR:
986 dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
987 break;
988 case SPDIF_CONNECTOR:
989 dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
990 break;
991 case COMPOSITE_CONNECTOR:
992 dprintk(DBGLVL_API,
993 " = COMPOSITE_CONNECTOR\n");
994 break;
995 case SVIDEO_CONNECTOR:
996 dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
997 break;
998 case COMPONENT_CONNECTOR:
999 dprintk(DBGLVL_API,
1000 " = COMPONENT_CONNECTOR\n");
1001 break;
1002 case STANDARD_DMA:
1003 dprintk(DBGLVL_API, " = STANDARD_DMA\n");
1004 break;
1005 default:
1006 dprintk(DBGLVL_API, " = undefined (0x%x)\n",
1007 anttermhdr->terminaltype);
1008 }
1009 dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
1010 anttermhdr->assocterminal);
1011 dprintk(DBGLVL_API, " iterminal = 0x%x\n",
1012 anttermhdr->iterminal);
1013 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1014 anttermhdr->controlsize);
1015 break;
1016 case VC_OUTPUT_TERMINAL:
1017 dprintk(DBGLVL_API, " VC_OUTPUT_TERMINAL\n");
1018 vcoutputtermhdr =
1019 (struct tmComResDMATermDescrHeader *)(buf + idx);
1020 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1021 vcoutputtermhdr->unitid);
1022 dprintk(DBGLVL_API, " terminaltype = 0x%x\n",
1023 vcoutputtermhdr->terminaltype);
1024 switch (vcoutputtermhdr->terminaltype) {
1025 case ITT_ANTENNA:
1026 dprintk(DBGLVL_API, " = ITT_ANTENNA\n");
1027 break;
1028 case LINE_CONNECTOR:
1029 dprintk(DBGLVL_API, " = LINE_CONNECTOR\n");
1030 break;
1031 case SPDIF_CONNECTOR:
1032 dprintk(DBGLVL_API, " = SPDIF_CONNECTOR\n");
1033 break;
1034 case COMPOSITE_CONNECTOR:
1035 dprintk(DBGLVL_API,
1036 " = COMPOSITE_CONNECTOR\n");
1037 break;
1038 case SVIDEO_CONNECTOR:
1039 dprintk(DBGLVL_API, " = SVIDEO_CONNECTOR\n");
1040 break;
1041 case COMPONENT_CONNECTOR:
1042 dprintk(DBGLVL_API,
1043 " = COMPONENT_CONNECTOR\n");
1044 break;
1045 case STANDARD_DMA:
1046 dprintk(DBGLVL_API, " = STANDARD_DMA\n");
1047 break;
1048 default:
1049 dprintk(DBGLVL_API, " = undefined (0x%x)\n",
1050 vcoutputtermhdr->terminaltype);
1051 }
1052 dprintk(DBGLVL_API, " assocterminal= 0x%x\n",
1053 vcoutputtermhdr->assocterminal);
1054 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1055 vcoutputtermhdr->sourceid);
1056 dprintk(DBGLVL_API, " iterminal = 0x%x\n",
1057 vcoutputtermhdr->iterminal);
1058 dprintk(DBGLVL_API, " BARLocation = 0x%x\n",
1059 vcoutputtermhdr->BARLocation);
1060 dprintk(DBGLVL_API, " flags = 0x%x\n",
1061 vcoutputtermhdr->flags);
1062 dprintk(DBGLVL_API, " interruptid = 0x%x\n",
1063 vcoutputtermhdr->interruptid);
1064 dprintk(DBGLVL_API, " buffercount = 0x%x\n",
1065 vcoutputtermhdr->buffercount);
1066 dprintk(DBGLVL_API, " metadatasize = 0x%x\n",
1067 vcoutputtermhdr->metadatasize);
1068 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1069 vcoutputtermhdr->controlsize);
1070 dprintk(DBGLVL_API, " numformats = 0x%x\n",
1071 vcoutputtermhdr->numformats);
1072
1073 t = (struct tmComResDescrHeader *)
1074 ((struct tmComResDMATermDescrHeader *)(buf + idx));
1075 next_offset = idx + (vcoutputtermhdr->len);
1076 for (i = 0; i < vcoutputtermhdr->numformats; i++) {
1077 t = (struct tmComResDescrHeader *)
1078 (buf + next_offset);
1079 switch (t->subtype) {
1080 case VS_FORMAT_MPEG2TS:
1081 tsfmt =
1082 (struct tmComResTSFormatDescrHeader *)t;
1083 if (currpath == 1)
1084 tsport = &dev->ports[SAA7164_PORT_TS1];
1085 else
1086 tsport = &dev->ports[SAA7164_PORT_TS2];
1087 memcpy(&tsport->hwcfg, vcoutputtermhdr,
1088 sizeof(*vcoutputtermhdr));
1089 saa7164_api_configure_port_mpeg2ts(dev,
1090 tsport, tsfmt);
1091 break;
1092 case VS_FORMAT_MPEG2PS:
1093 psfmt =
1094 (struct tmComResPSFormatDescrHeader *)t;
1095 if (currpath == 1)
1096 encport = &dev->ports[SAA7164_PORT_ENC1];
1097 else
1098 encport = &dev->ports[SAA7164_PORT_ENC2];
1099 memcpy(&encport->hwcfg, vcoutputtermhdr,
1100 sizeof(*vcoutputtermhdr));
1101 saa7164_api_configure_port_mpeg2ps(dev,
1102 encport, psfmt);
1103 break;
1104 case VS_FORMAT_VBI:
1105 vbifmt =
1106 (struct tmComResVBIFormatDescrHeader *)t;
1107 if (currpath == 1)
1108 vbiport = &dev->ports[SAA7164_PORT_VBI1];
1109 else
1110 vbiport = &dev->ports[SAA7164_PORT_VBI2];
1111 memcpy(&vbiport->hwcfg, vcoutputtermhdr,
1112 sizeof(*vcoutputtermhdr));
1113 memcpy(&vbiport->vbi_fmt_ntsc, vbifmt,
1114 sizeof(*vbifmt));
1115 saa7164_api_configure_port_vbi(dev,
1116 vbiport);
1117 break;
1118 case VS_FORMAT_RDS:
1119 dprintk(DBGLVL_API,
1120 " = VS_FORMAT_RDS\n");
1121 break;
1122 case VS_FORMAT_UNCOMPRESSED:
1123 dprintk(DBGLVL_API,
1124 " = VS_FORMAT_UNCOMPRESSED\n");
1125 break;
1126 case VS_FORMAT_TYPE:
1127 dprintk(DBGLVL_API,
1128 " = VS_FORMAT_TYPE\n");
1129 break;
1130 default:
1131 dprintk(DBGLVL_API,
1132 " = undefined (0x%x)\n",
1133 t->subtype);
1134 }
1135 next_offset += t->len;
1136 }
1137
1138 break;
1139 case TUNER_UNIT:
1140 dprintk(DBGLVL_API, " TUNER_UNIT\n");
1141 tunerunithdr =
1142 (struct tmComResTunerDescrHeader *)(buf + idx);
1143 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1144 tunerunithdr->unitid);
1145 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1146 tunerunithdr->sourceid);
1147 dprintk(DBGLVL_API, " iunit = 0x%x\n",
1148 tunerunithdr->iunit);
1149 dprintk(DBGLVL_API, " tuningstandards = 0x%x\n",
1150 tunerunithdr->tuningstandards);
1151 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1152 tunerunithdr->controlsize);
1153 dprintk(DBGLVL_API, " controls = 0x%x\n",
1154 tunerunithdr->controls);
1155
1156 if (tunerunithdr->unitid == tunerunithdr->iunit) {
1157 if (currpath == 1)
1158 encport = &dev->ports[SAA7164_PORT_ENC1];
1159 else
1160 encport = &dev->ports[SAA7164_PORT_ENC2];
1161 memcpy(&encport->tunerunit, tunerunithdr,
1162 sizeof(struct tmComResTunerDescrHeader));
1163 dprintk(DBGLVL_API,
1164 " (becomes dev->enc[%d] tuner)\n",
1165 encport->nr);
1166 }
1167 break;
1168 case VC_SELECTOR_UNIT:
1169 psel = (struct tmComResSelDescrHeader *)(buf + idx);
1170 dprintk(DBGLVL_API, " VC_SELECTOR_UNIT\n");
1171 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1172 psel->unitid);
1173 dprintk(DBGLVL_API, " nrinpins = 0x%x\n",
1174 psel->nrinpins);
1175 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1176 psel->sourceid);
1177 break;
1178 case VC_PROCESSING_UNIT:
1179 pdh = (struct tmComResProcDescrHeader *)(buf + idx);
1180 dprintk(DBGLVL_API, " VC_PROCESSING_UNIT\n");
1181 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1182 pdh->unitid);
1183 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1184 pdh->sourceid);
1185 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1186 pdh->controlsize);
1187 if (pdh->controlsize == 0x04) {
1188 if (currpath == 1)
1189 encport = &dev->ports[SAA7164_PORT_ENC1];
1190 else
1191 encport = &dev->ports[SAA7164_PORT_ENC2];
1192 memcpy(&encport->vidproc, pdh,
1193 sizeof(struct tmComResProcDescrHeader));
1194 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n",
1195 encport->nr);
1196 }
1197 break;
1198 case FEATURE_UNIT:
1199 afd = (struct tmComResAFeatureDescrHeader *)(buf + idx);
1200 dprintk(DBGLVL_API, " FEATURE_UNIT\n");
1201 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1202 afd->unitid);
1203 dprintk(DBGLVL_API, " sourceid = 0x%x\n",
1204 afd->sourceid);
1205 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1206 afd->controlsize);
1207 if (currpath == 1)
1208 encport = &dev->ports[SAA7164_PORT_ENC1];
1209 else
1210 encport = &dev->ports[SAA7164_PORT_ENC2];
1211 memcpy(&encport->audfeat, afd,
1212 sizeof(struct tmComResAFeatureDescrHeader));
1213 dprintk(DBGLVL_API, " (becomes dev->enc[%d])\n",
1214 encport->nr);
1215 break;
1216 case ENCODER_UNIT:
1217 edh = (struct tmComResEncoderDescrHeader *)(buf + idx);
1218 dprintk(DBGLVL_API, " ENCODER_UNIT\n");
1219 dprintk(DBGLVL_API, " subtype = 0x%x\n", edh->subtype);
1220 dprintk(DBGLVL_API, " unitid = 0x%x\n", edh->unitid);
1221 dprintk(DBGLVL_API, " vsourceid = 0x%x\n",
1222 edh->vsourceid);
1223 dprintk(DBGLVL_API, " asourceid = 0x%x\n",
1224 edh->asourceid);
1225 dprintk(DBGLVL_API, " iunit = 0x%x\n", edh->iunit);
1226 if (edh->iunit == edh->unitid) {
1227 if (currpath == 1)
1228 encport = &dev->ports[SAA7164_PORT_ENC1];
1229 else
1230 encport = &dev->ports[SAA7164_PORT_ENC2];
1231 memcpy(&encport->encunit, edh,
1232 sizeof(struct tmComResEncoderDescrHeader));
1233 dprintk(DBGLVL_API,
1234 " (becomes dev->enc[%d])\n",
1235 encport->nr);
1236 }
1237 break;
1238 case EXTENSION_UNIT:
1239 dprintk(DBGLVL_API, " EXTENSION_UNIT\n");
1240 exthdr = (struct tmComResExtDevDescrHeader *)(buf + idx);
1241 dprintk(DBGLVL_API, " unitid = 0x%x\n",
1242 exthdr->unitid);
1243 dprintk(DBGLVL_API, " deviceid = 0x%x\n",
1244 exthdr->deviceid);
1245 dprintk(DBGLVL_API, " devicetype = 0x%x\n",
1246 exthdr->devicetype);
1247 if (exthdr->devicetype & 0x1)
1248 dprintk(DBGLVL_API, " = Decoder Device\n");
1249 if (exthdr->devicetype & 0x2)
1250 dprintk(DBGLVL_API, " = GPIO Source\n");
1251 if (exthdr->devicetype & 0x4)
1252 dprintk(DBGLVL_API, " = Video Decoder\n");
1253 if (exthdr->devicetype & 0x8)
1254 dprintk(DBGLVL_API, " = Audio Decoder\n");
1255 if (exthdr->devicetype & 0x20)
1256 dprintk(DBGLVL_API, " = Crossbar\n");
1257 if (exthdr->devicetype & 0x40)
1258 dprintk(DBGLVL_API, " = Tuner\n");
1259 if (exthdr->devicetype & 0x80)
1260 dprintk(DBGLVL_API, " = IF PLL\n");
1261 if (exthdr->devicetype & 0x100)
1262 dprintk(DBGLVL_API, " = Demodulator\n");
1263 if (exthdr->devicetype & 0x200)
1264 dprintk(DBGLVL_API, " = RDS Decoder\n");
1265 if (exthdr->devicetype & 0x400)
1266 dprintk(DBGLVL_API, " = Encoder\n");
1267 if (exthdr->devicetype & 0x800)
1268 dprintk(DBGLVL_API, " = IR Decoder\n");
1269 if (exthdr->devicetype & 0x1000)
1270 dprintk(DBGLVL_API, " = EEPROM\n");
1271 if (exthdr->devicetype & 0x2000)
1272 dprintk(DBGLVL_API,
1273 " = VBI Decoder\n");
1274 if (exthdr->devicetype & 0x10000)
1275 dprintk(DBGLVL_API,
1276 " = Streaming Device\n");
1277 if (exthdr->devicetype & 0x20000)
1278 dprintk(DBGLVL_API,
1279 " = DRM Device\n");
1280 if (exthdr->devicetype & 0x40000000)
1281 dprintk(DBGLVL_API,
1282 " = Generic Device\n");
1283 if (exthdr->devicetype & 0x80000000)
1284 dprintk(DBGLVL_API,
1285 " = Config Space Device\n");
1286 dprintk(DBGLVL_API, " numgpiopins = 0x%x\n",
1287 exthdr->numgpiopins);
1288 dprintk(DBGLVL_API, " numgpiogroups = 0x%x\n",
1289 exthdr->numgpiogroups);
1290 dprintk(DBGLVL_API, " controlsize = 0x%x\n",
1291 exthdr->controlsize);
1292 if (exthdr->devicetype & 0x80) {
1293 if (currpath == 1)
1294 encport = &dev->ports[SAA7164_PORT_ENC1];
1295 else
1296 encport = &dev->ports[SAA7164_PORT_ENC2];
1297 memcpy(&encport->ifunit, exthdr,
1298 sizeof(struct tmComResExtDevDescrHeader));
1299 dprintk(DBGLVL_API,
1300 " (becomes dev->enc[%d])\n",
1301 encport->nr);
1302 }
1303 break;
1304 case PVC_INFRARED_UNIT:
1305 dprintk(DBGLVL_API, " PVC_INFRARED_UNIT\n");
1306 break;
1307 case DRM_UNIT:
1308 dprintk(DBGLVL_API, " DRM_UNIT\n");
1309 break;
1310 default:
1311 dprintk(DBGLVL_API, "default %d\n", hdr->subtype);
1312 }
1313
1314 dprintk(DBGLVL_API, " 1.%x\n", hdr->len);
1315 dprintk(DBGLVL_API, " 2.%x\n", hdr->type);
1316 dprintk(DBGLVL_API, " 3.%x\n", hdr->subtype);
1317 dprintk(DBGLVL_API, " 4.%x\n", hdr->unitid);
1318
1319 idx += hdr->len;
1320 }
1321
1322 return 0;
1323}
1324
1325int saa7164_api_enum_subdevs(struct saa7164_dev *dev)
1326{
1327 int ret;
1328 u32 buflen = 0;
1329 u8 *buf;
1330
1331 dprintk(DBGLVL_API, "%s()\n", __func__);
1332
1333 /* Get the total descriptor length */
1334 ret = saa7164_cmd_send(dev, 0, GET_LEN,
1335 GET_DESCRIPTORS_CONTROL, sizeof(buflen), &buflen);
1336 if (ret != SAA_OK)
1337 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1338
1339 dprintk(DBGLVL_API, "%s() total descriptor size = %d bytes.\n",
1340 __func__, buflen);
1341
1342 /* Allocate enough storage for all of the descs */
1343 buf = kzalloc(buflen, GFP_KERNEL);
1344 if (!buf)
1345 return SAA_ERR_NO_RESOURCES;
1346
1347 /* Retrieve them */
1348 ret = saa7164_cmd_send(dev, 0, GET_CUR,
1349 GET_DESCRIPTORS_CONTROL, buflen, buf);
1350 if (ret != SAA_OK) {
1351 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
1352 goto out;
1353 }
1354
1355 if (saa_debug & DBGLVL_API)
1356 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf,
1357 buflen & ~15, false);
1358
1359 saa7164_api_dump_subdevs(dev, buf, buflen);
1360
1361out:
1362 kfree(buf);
1363 return ret;
1364}
1365
1366int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
1367 u32 datalen, u8 *data)
1368{
1369 struct saa7164_dev *dev = bus->dev;
1370 u16 len = 0;
1371 int unitid;
1372 u8 buf[256];
1373 int ret;
1374
1375 dprintk(DBGLVL_API, "%s()\n", __func__);
1376
1377 if (reglen > 4)
1378 return -EIO;
1379
1380 /* Prepare the send buffer */
1381 /* Bytes 00-03 source register length
1382 * 04-07 source bytes to read
1383 * 08... register address
1384 */
1385 memset(buf, 0, sizeof(buf));
1386 memcpy((buf + 2 * sizeof(u32) + 0), reg, reglen);
1387 *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1388 *((u32 *)(buf + 1 * sizeof(u32))) = datalen;
1389
1390 unitid = saa7164_i2caddr_to_unitid(bus, addr);
1391 if (unitid < 0) {
1392 printk(KERN_ERR
1393 "%s() error, cannot translate regaddr 0x%x to unitid\n",
1394 __func__, addr);
1395 return -EIO;
1396 }
1397
1398 ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1399 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1400 if (ret != SAA_OK) {
1401 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1402 return -EIO;
1403 }
1404
1405 dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1406
1407 if (saa_debug & DBGLVL_I2C)
1408 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1, buf,
1409 32, false);
1410
1411 ret = saa7164_cmd_send(bus->dev, unitid, GET_CUR,
1412 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1413 if (ret != SAA_OK)
1414 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1415 else {
1416 if (saa_debug & DBGLVL_I2C)
1417 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
1418 buf, sizeof(buf), false);
1419 memcpy(data, (buf + 2 * sizeof(u32) + reglen), datalen);
1420 }
1421
1422 return ret == SAA_OK ? 0 : -EIO;
1423}
1424
1425/* For a given 8 bit i2c address device, write the buffer */
1426int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, u32 datalen,
1427 u8 *data)
1428{
1429 struct saa7164_dev *dev = bus->dev;
1430 u16 len = 0;
1431 int unitid;
1432 int reglen;
1433 u8 buf[256];
1434 int ret;
1435
1436 dprintk(DBGLVL_API, "%s()\n", __func__);
1437
1438 if ((datalen == 0) || (datalen > 232))
1439 return -EIO;
1440
1441 memset(buf, 0, sizeof(buf));
1442
1443 unitid = saa7164_i2caddr_to_unitid(bus, addr);
1444 if (unitid < 0) {
1445 printk(KERN_ERR
1446 "%s() error, cannot translate regaddr 0x%x to unitid\n",
1447 __func__, addr);
1448 return -EIO;
1449 }
1450
1451 reglen = saa7164_i2caddr_to_reglen(bus, addr);
1452 if (reglen < 0) {
1453 printk(KERN_ERR
1454 "%s() error, cannot translate regaddr to reglen\n",
1455 __func__);
1456 return -EIO;
1457 }
1458
1459 ret = saa7164_cmd_send(bus->dev, unitid, GET_LEN,
1460 EXU_REGISTER_ACCESS_CONTROL, sizeof(len), &len);
1461 if (ret != SAA_OK) {
1462 printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
1463 return -EIO;
1464 }
1465
1466 dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
1467
1468 /* Prepare the send buffer */
1469 /* Bytes 00-03 dest register length
1470 * 04-07 dest bytes to write
1471 * 08... register address
1472 */
1473 *((u32 *)(buf + 0 * sizeof(u32))) = reglen;
1474 *((u32 *)(buf + 1 * sizeof(u32))) = datalen - reglen;
1475 memcpy((buf + 2 * sizeof(u32)), data, datalen);
1476
1477 if (saa_debug & DBGLVL_I2C)
1478 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
1479 buf, sizeof(buf), false);
1480
1481 ret = saa7164_cmd_send(bus->dev, unitid, SET_CUR,
1482 EXU_REGISTER_ACCESS_CONTROL, len, &buf);
1483 if (ret != SAA_OK)
1484 printk(KERN_ERR "%s() error, ret(2) = 0x%x\n", __func__, ret);
1485
1486 return ret == SAA_OK ? 0 : -EIO;
1487}
1488
1489int saa7164_api_modify_gpio(struct saa7164_dev *dev, u8 unitid,
1490 u8 pin, u8 state)
1491{
1492 int ret;
1493 struct tmComResGPIO t;
1494
1495 dprintk(DBGLVL_API, "%s(0x%x, %d, %d)\n",
1496 __func__, unitid, pin, state);
1497
1498 if ((pin > 7) || (state > 2))
1499 return SAA_ERR_BAD_PARAMETER;
1500
1501 t.pin = pin;
1502 t.state = state;
1503
1504 ret = saa7164_cmd_send(dev, unitid, SET_CUR,
1505 EXU_GPIO_CONTROL, sizeof(t), &t);
1506 if (ret != SAA_OK)
1507 printk(KERN_ERR "%s() error, ret = 0x%x\n",
1508 __func__, ret);
1509
1510 return ret;
1511}
1512
1513int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid,
1514 u8 pin)
1515{
1516 return saa7164_api_modify_gpio(dev, unitid, pin, 1);
1517}
1518
1519int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid,
1520 u8 pin)
1521{
1522 return saa7164_api_modify_gpio(dev, unitid, pin, 0);
1523}
1524
diff --git a/drivers/media/pci/saa7164/saa7164-buffer.c b/drivers/media/pci/saa7164/saa7164-buffer.c
new file mode 100644
index 000000000000..66696fa8341d
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-buffer.c
@@ -0,0 +1,322 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/slab.h>
23
24#include "saa7164.h"
25
26/* The PCI address space for buffer handling looks like this:
27 *
28 * +-u32 wide-------------+
29 * | +
30 * +-u64 wide------------------------------------+
31 * + +
32 * +----------------------+
33 * | CurrentBufferPtr + Pointer to current PCI buffer >-+
34 * +----------------------+ |
35 * | Unused + |
36 * +----------------------+ |
37 * | Pitch + = 188 (bytes) |
38 * +----------------------+ |
39 * | PCI buffer size + = pitch * number of lines (312) |
40 * +----------------------+ |
41 * |0| Buf0 Write Offset + |
42 * +----------------------+ v
43 * |1| Buf1 Write Offset + |
44 * +----------------------+ |
45 * |2| Buf2 Write Offset + |
46 * +----------------------+ |
47 * |3| Buf3 Write Offset + |
48 * +----------------------+ |
49 * ... More write offsets |
50 * +---------------------------------------------+ |
51 * +0| set of ptrs to PCI pagetables + |
52 * +---------------------------------------------+ |
53 * +1| set of ptrs to PCI pagetables + <--------+
54 * +---------------------------------------------+
55 * +2| set of ptrs to PCI pagetables +
56 * +---------------------------------------------+
57 * +3| set of ptrs to PCI pagetables + >--+
58 * +---------------------------------------------+ |
59 * ... More buffer pointers | +----------------+
60 * +->| pt[0] TS data |
61 * | +----------------+
62 * |
63 * | +----------------+
64 * +->| pt[1] TS data |
65 * | +----------------+
66 * | etc
67 */
68
69void saa7164_buffer_display(struct saa7164_buffer *buf)
70{
71 struct saa7164_dev *dev = buf->port->dev;
72 int i;
73
74 dprintk(DBGLVL_BUF, "%s() buffer @ 0x%p nr=%d\n",
75 __func__, buf, buf->idx);
76 dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08llx len = 0x%x\n",
77 buf->cpu, (long long)buf->dma, buf->pci_size);
78 dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08llx len = 0x%x\n",
79 buf->pt_cpu, (long long)buf->pt_dma, buf->pt_size);
80
81 /* Format the Page Table Entries to point into the data buffer */
82 for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
83
84 dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
85 i, buf->pt_cpu, (u64)*(buf->pt_cpu));
86
87 }
88}
89/* Allocate a new buffer structure and associated PCI space in bytes.
90 * len must be a multiple of sizeof(u64)
91 */
92struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_port *port,
93 u32 len)
94{
95 struct tmHWStreamParameters *params = &port->hw_streamingparams;
96 struct saa7164_buffer *buf = NULL;
97 struct saa7164_dev *dev = port->dev;
98 int i;
99
100 if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
101 log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
102 goto ret;
103 }
104
105 buf = kzalloc(sizeof(struct saa7164_buffer), GFP_KERNEL);
106 if (!buf) {
107 log_warn("%s() SAA_ERR_NO_RESOURCES\n", __func__);
108 goto ret;
109 }
110
111 buf->idx = -1;
112 buf->port = port;
113 buf->flags = SAA7164_BUFFER_FREE;
114 buf->pos = 0;
115 buf->actual_size = params->pitch * params->numberoflines;
116 buf->crc = 0;
117 /* TODO: arg len is being ignored */
118 buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
119 buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
120
121 /* Allocate contiguous memory */
122 buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
123 &buf->dma);
124 if (!buf->cpu)
125 goto fail1;
126
127 buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
128 &buf->pt_dma);
129 if (!buf->pt_cpu)
130 goto fail2;
131
132 /* init the buffers to a known pattern, easier during debugging */
133 memset_io(buf->cpu, 0xff, buf->pci_size);
134 buf->crc = crc32(0, buf->cpu, buf->actual_size);
135 memset_io(buf->pt_cpu, 0xff, buf->pt_size);
136
137 dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p (%d pageptrs)\n",
138 __func__, buf, params->numpagetables);
139 dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
140 buf->cpu, (long)buf->dma, buf->pci_size);
141 dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
142 buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
143
144 /* Format the Page Table Entries to point into the data buffer */
145 for (i = 0 ; i < params->numpagetables; i++) {
146
147 *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
148 dprintk(DBGLVL_BUF, " pt[%02d] = 0x%p -> 0x%llx\n",
149 i, buf->pt_cpu, (u64)*(buf->pt_cpu));
150
151 }
152
153 goto ret;
154
155fail2:
156 pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
157fail1:
158 kfree(buf);
159
160 buf = NULL;
161ret:
162 return buf;
163}
164
165int saa7164_buffer_dealloc(struct saa7164_buffer *buf)
166{
167 struct saa7164_dev *dev;
168
169 if (!buf || !buf->port)
170 return SAA_ERR_BAD_PARAMETER;
171 dev = buf->port->dev;
172
173 dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n",
174 __func__, buf);
175
176 if (buf->flags != SAA7164_BUFFER_FREE)
177 log_warn(" freeing a non-free buffer\n");
178
179 pci_free_consistent(dev->pci, buf->pci_size, buf->cpu, buf->dma);
180 pci_free_consistent(dev->pci, buf->pt_size, buf->pt_cpu, buf->pt_dma);
181
182 kfree(buf);
183
184 return SAA_OK;
185}
186
187int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i)
188{
189 struct saa7164_dev *dev = port->dev;
190
191 if ((i < 0) || (i >= port->hwcfg.buffercount))
192 return -EINVAL;
193
194 dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
195
196 saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
197
198 return 0;
199}
200
201/* Write a buffer into the hardware */
202int saa7164_buffer_activate(struct saa7164_buffer *buf, int i)
203{
204 struct saa7164_port *port = buf->port;
205 struct saa7164_dev *dev = port->dev;
206
207 if ((i < 0) || (i >= port->hwcfg.buffercount))
208 return -EINVAL;
209
210 dprintk(DBGLVL_BUF, "%s(idx = %d)\n", __func__, i);
211
212 buf->idx = i; /* Note of which buffer list index position we occupy */
213 buf->flags = SAA7164_BUFFER_BUSY;
214 buf->pos = 0;
215
216 /* TODO: Review this in light of 32v64 assignments */
217 saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
218 saa7164_writel(port->bufptr32h + ((sizeof(u32) * 2) * i), buf->pt_dma);
219 saa7164_writel(port->bufptr32l + ((sizeof(u32) * 2) * i), 0);
220
221 dprintk(DBGLVL_BUF, " buf[%d] offset 0x%llx (0x%x) "
222 "buf 0x%llx/%llx (0x%x/%x) nr=%d\n",
223 buf->idx,
224 (u64)port->bufoffset + (i * sizeof(u32)),
225 saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
226 (u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
227 (u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
228 saa7164_readl(port->bufptr32h + ((sizeof(u32) * i) * 2)),
229 saa7164_readl(port->bufptr32l + ((sizeof(u32) * i) * 2)),
230 buf->idx);
231
232 return 0;
233}
234
235int saa7164_buffer_cfg_port(struct saa7164_port *port)
236{
237 struct tmHWStreamParameters *params = &port->hw_streamingparams;
238 struct saa7164_dev *dev = port->dev;
239 struct saa7164_buffer *buf;
240 struct list_head *c, *n;
241 int i = 0;
242
243 dprintk(DBGLVL_BUF, "%s(port=%d)\n", __func__, port->nr);
244
245 saa7164_writel(port->bufcounter, 0);
246 saa7164_writel(port->pitch, params->pitch);
247 saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
248
249 dprintk(DBGLVL_BUF, " configured:\n");
250 dprintk(DBGLVL_BUF, " lmmio 0x%p\n", dev->lmmio);
251 dprintk(DBGLVL_BUF, " bufcounter 0x%x = 0x%x\n", port->bufcounter,
252 saa7164_readl(port->bufcounter));
253
254 dprintk(DBGLVL_BUF, " pitch 0x%x = %d\n", port->pitch,
255 saa7164_readl(port->pitch));
256
257 dprintk(DBGLVL_BUF, " bufsize 0x%x = %d\n", port->bufsize,
258 saa7164_readl(port->bufsize));
259
260 dprintk(DBGLVL_BUF, " buffercount = %d\n", port->hwcfg.buffercount);
261 dprintk(DBGLVL_BUF, " bufoffset = 0x%x\n", port->bufoffset);
262 dprintk(DBGLVL_BUF, " bufptr32h = 0x%x\n", port->bufptr32h);
263 dprintk(DBGLVL_BUF, " bufptr32l = 0x%x\n", port->bufptr32l);
264
265 /* Poke the buffers and offsets into PCI space */
266 mutex_lock(&port->dmaqueue_lock);
267 list_for_each_safe(c, n, &port->dmaqueue.list) {
268 buf = list_entry(c, struct saa7164_buffer, list);
269
270 if (buf->flags != SAA7164_BUFFER_FREE)
271 BUG();
272
273 /* Place the buffer in the h/w queue */
274 saa7164_buffer_activate(buf, i);
275
276 /* Don't exceed the device maximum # bufs */
277 if (i++ > port->hwcfg.buffercount)
278 BUG();
279
280 }
281 mutex_unlock(&port->dmaqueue_lock);
282
283 return 0;
284}
285
286struct saa7164_user_buffer *saa7164_buffer_alloc_user(struct saa7164_dev *dev,
287 u32 len)
288{
289 struct saa7164_user_buffer *buf;
290
291 buf = kzalloc(sizeof(struct saa7164_user_buffer), GFP_KERNEL);
292 if (!buf)
293 return NULL;
294
295 buf->data = kzalloc(len, GFP_KERNEL);
296
297 if (!buf->data) {
298 kfree(buf);
299 return NULL;
300 }
301
302 buf->actual_size = len;
303 buf->pos = 0;
304 buf->crc = 0;
305
306 dprintk(DBGLVL_BUF, "%s() allocated user buffer @ 0x%p\n",
307 __func__, buf);
308
309 return buf;
310}
311
312void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf)
313{
314 if (!buf)
315 return;
316
317 kfree(buf->data);
318 buf->data = NULL;
319
320 kfree(buf);
321}
322
diff --git a/drivers/media/pci/saa7164/saa7164-bus.c b/drivers/media/pci/saa7164/saa7164-bus.c
new file mode 100644
index 000000000000..a7f58a998752
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-bus.c
@@ -0,0 +1,475 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "saa7164.h"
23
24/* The message bus to/from the firmware is a ring buffer in PCI address
25 * space. Establish the defaults.
26 */
27int saa7164_bus_setup(struct saa7164_dev *dev)
28{
29 struct tmComResBusInfo *b = &dev->bus;
30
31 mutex_init(&b->lock);
32
33 b->Type = TYPE_BUS_PCIe;
34 b->m_wMaxReqSize = SAA_DEVICE_MAXREQUESTSIZE;
35
36 b->m_pdwSetRing = (u8 *)(dev->bmmio +
37 ((u32)dev->busdesc.CommandRing));
38
39 b->m_dwSizeSetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
40
41 b->m_pdwGetRing = (u8 *)(dev->bmmio +
42 ((u32)dev->busdesc.ResponseRing));
43
44 b->m_dwSizeGetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
45
46 b->m_dwSetWritePos = ((u32)dev->intfdesc.BARLocation) +
47 (2 * sizeof(u64));
48 b->m_dwSetReadPos = b->m_dwSetWritePos + (1 * sizeof(u32));
49
50 b->m_dwGetWritePos = b->m_dwSetWritePos + (2 * sizeof(u32));
51 b->m_dwGetReadPos = b->m_dwSetWritePos + (3 * sizeof(u32));
52
53 return 0;
54}
55
56void saa7164_bus_dump(struct saa7164_dev *dev)
57{
58 struct tmComResBusInfo *b = &dev->bus;
59
60 dprintk(DBGLVL_BUS, "Dumping the bus structure:\n");
61 dprintk(DBGLVL_BUS, " .type = %d\n", b->Type);
62 dprintk(DBGLVL_BUS, " .dev->bmmio = 0x%p\n", dev->bmmio);
63 dprintk(DBGLVL_BUS, " .m_wMaxReqSize = 0x%x\n", b->m_wMaxReqSize);
64 dprintk(DBGLVL_BUS, " .m_pdwSetRing = 0x%p\n", b->m_pdwSetRing);
65 dprintk(DBGLVL_BUS, " .m_dwSizeSetRing = 0x%x\n", b->m_dwSizeSetRing);
66 dprintk(DBGLVL_BUS, " .m_pdwGetRing = 0x%p\n", b->m_pdwGetRing);
67 dprintk(DBGLVL_BUS, " .m_dwSizeGetRing = 0x%x\n", b->m_dwSizeGetRing);
68
69 dprintk(DBGLVL_BUS, " .m_dwSetReadPos = 0x%x (0x%08x)\n",
70 b->m_dwSetReadPos, saa7164_readl(b->m_dwSetReadPos));
71
72 dprintk(DBGLVL_BUS, " .m_dwSetWritePos = 0x%x (0x%08x)\n",
73 b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
74
75 dprintk(DBGLVL_BUS, " .m_dwGetReadPos = 0x%x (0x%08x)\n",
76 b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
77
78 dprintk(DBGLVL_BUS, " .m_dwGetWritePos = 0x%x (0x%08x)\n",
79 b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
80
81}
82
83/* Intensionally throw a BUG() if the state of the message bus looks corrupt */
84void saa7164_bus_verify(struct saa7164_dev *dev)
85{
86 struct tmComResBusInfo *b = &dev->bus;
87 int bug = 0;
88
89 if (saa7164_readl(b->m_dwSetReadPos) > b->m_dwSizeSetRing)
90 bug++;
91
92 if (saa7164_readl(b->m_dwSetWritePos) > b->m_dwSizeSetRing)
93 bug++;
94
95 if (saa7164_readl(b->m_dwGetReadPos) > b->m_dwSizeGetRing)
96 bug++;
97
98 if (saa7164_readl(b->m_dwGetWritePos) > b->m_dwSizeGetRing)
99 bug++;
100
101 if (bug) {
102 saa_debug = 0xffff; /* Ensure we get the bus dump */
103 saa7164_bus_dump(dev);
104 saa_debug = 1024; /* Ensure we get the bus dump */
105 BUG();
106 }
107}
108
109void saa7164_bus_dumpmsg(struct saa7164_dev *dev, struct tmComResInfo* m,
110 void *buf)
111{
112 dprintk(DBGLVL_BUS, "Dumping msg structure:\n");
113 dprintk(DBGLVL_BUS, " .id = %d\n", m->id);
114 dprintk(DBGLVL_BUS, " .flags = 0x%x\n", m->flags);
115 dprintk(DBGLVL_BUS, " .size = 0x%x\n", m->size);
116 dprintk(DBGLVL_BUS, " .command = 0x%x\n", m->command);
117 dprintk(DBGLVL_BUS, " .controlselector = 0x%x\n", m->controlselector);
118 dprintk(DBGLVL_BUS, " .seqno = %d\n", m->seqno);
119 if (buf)
120 dprintk(DBGLVL_BUS, " .buffer (ignored)\n");
121}
122
123/*
124 * Places a command or a response on the bus. The implementation does not
125 * know if it is a command or a response it just places the data on the
126 * bus depending on the bus information given in the struct tmComResBusInfo
127 * structure. If the command or response does not fit into the bus ring
128 * buffer it will be refused.
129 *
130 * Return Value:
131 * SAA_OK The function executed successfully.
132 * < 0 One or more members are not initialized.
133 */
134int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
135 void *buf)
136{
137 struct tmComResBusInfo *bus = &dev->bus;
138 u32 bytes_to_write, free_write_space, timeout, curr_srp, curr_swp;
139 u32 new_swp, space_rem;
140 int ret = SAA_ERR_BAD_PARAMETER;
141
142 if (!msg) {
143 printk(KERN_ERR "%s() !msg\n", __func__);
144 return SAA_ERR_BAD_PARAMETER;
145 }
146
147 dprintk(DBGLVL_BUS, "%s()\n", __func__);
148
149 saa7164_bus_verify(dev);
150
151 msg->size = cpu_to_le16(msg->size);
152 msg->command = cpu_to_le32(msg->command);
153 msg->controlselector = cpu_to_le16(msg->controlselector);
154
155 if (msg->size > dev->bus.m_wMaxReqSize) {
156 printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
157 __func__);
158 return SAA_ERR_BAD_PARAMETER;
159 }
160
161 if ((msg->size > 0) && (buf == NULL)) {
162 printk(KERN_ERR "%s() Missing message buffer\n", __func__);
163 return SAA_ERR_BAD_PARAMETER;
164 }
165
166 /* Lock the bus from any other access */
167 mutex_lock(&bus->lock);
168
169 bytes_to_write = sizeof(*msg) + msg->size;
170 free_write_space = 0;
171 timeout = SAA_BUS_TIMEOUT;
172 curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));
173 curr_swp = le32_to_cpu(saa7164_readl(bus->m_dwSetWritePos));
174
175 /* Deal with ring wrapping issues */
176 if (curr_srp > curr_swp)
177 /* Deal with the wrapped ring */
178 free_write_space = curr_srp - curr_swp;
179 else
180 /* The ring has not wrapped yet */
181 free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;
182
183 dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
184 bytes_to_write);
185
186 dprintk(DBGLVL_BUS, "%s() free_write_space = %d\n", __func__,
187 free_write_space);
188
189 dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
190 dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);
191
192 /* Process the msg and write the content onto the bus */
193 while (bytes_to_write >= free_write_space) {
194
195 if (timeout-- == 0) {
196 printk(KERN_ERR "%s() bus timeout\n", __func__);
197 ret = SAA_ERR_NO_RESOURCES;
198 goto out;
199 }
200
201 /* TODO: Review this delay, efficient? */
202 /* Wait, allowing the hardware fetch time */
203 mdelay(1);
204
205 /* Check the space usage again */
206 curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));
207
208 /* Deal with ring wrapping issues */
209 if (curr_srp > curr_swp)
210 /* Deal with the wrapped ring */
211 free_write_space = curr_srp - curr_swp;
212 else
213 /* Read didn't wrap around the buffer */
214 free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
215 curr_swp;
216
217 }
218
219 /* Calculate the new write position */
220 new_swp = curr_swp + bytes_to_write;
221
222 dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
223 dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
224 bus->m_dwSizeSetRing);
225
226 /* Mental Note: line 462 tmmhComResBusPCIe.cpp */
227
228 /* Check if we're going to wrap again */
229 if (new_swp > bus->m_dwSizeSetRing) {
230
231 /* Ring wraps */
232 new_swp -= bus->m_dwSizeSetRing;
233
234 space_rem = bus->m_dwSizeSetRing - curr_swp;
235
236 dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
237 space_rem);
238
239 dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
240 (u32)sizeof(*msg));
241
242 if (space_rem < sizeof(*msg)) {
243 dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);
244
245 /* Split the msg into pieces as the ring wraps */
246 memcpy(bus->m_pdwSetRing + curr_swp, msg, space_rem);
247 memcpy(bus->m_pdwSetRing, (u8 *)msg + space_rem,
248 sizeof(*msg) - space_rem);
249
250 memcpy(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
251 buf, msg->size);
252
253 } else if (space_rem == sizeof(*msg)) {
254 dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);
255
256 /* Additional data at the beginning of the ring */
257 memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
258 memcpy(bus->m_pdwSetRing, buf, msg->size);
259
260 } else {
261 /* Additional data wraps around the ring */
262 memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
263 if (msg->size > 0) {
264 memcpy(bus->m_pdwSetRing + curr_swp +
265 sizeof(*msg), buf, space_rem -
266 sizeof(*msg));
267 memcpy(bus->m_pdwSetRing, (u8 *)buf +
268 space_rem - sizeof(*msg),
269 bytes_to_write - space_rem);
270 }
271
272 }
273
274 } /* (new_swp > bus->m_dwSizeSetRing) */
275 else {
276 dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);
277
278 /* The ring buffer doesn't wrap, two simple copies */
279 memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
280 memcpy(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
281 msg->size);
282 }
283
284 dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
285
286 /* Update the bus write position */
287 saa7164_writel(bus->m_dwSetWritePos, cpu_to_le32(new_swp));
288 ret = SAA_OK;
289
290out:
291 saa7164_bus_dump(dev);
292 mutex_unlock(&bus->lock);
293 saa7164_bus_verify(dev);
294 return ret;
295}
296
297/*
298 * Receive a command or a response from the bus. The implementation does not
299 * know if it is a command or a response it simply dequeues the data,
300 * depending on the bus information given in the struct tmComResBusInfo
301 * structure.
302 *
303 * Return Value:
304 * 0 The function executed successfully.
305 * < 0 One or more members are not initialized.
306 */
307int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
308 void *buf, int peekonly)
309{
310 struct tmComResBusInfo *bus = &dev->bus;
311 u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
312 new_grp, buf_size, space_rem;
313 struct tmComResInfo msg_tmp;
314 int ret = SAA_ERR_BAD_PARAMETER;
315
316 saa7164_bus_verify(dev);
317
318 if (msg == NULL)
319 return ret;
320
321 if (msg->size > dev->bus.m_wMaxReqSize) {
322 printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
323 __func__);
324 return ret;
325 }
326
327 if ((peekonly == 0) && (msg->size > 0) && (buf == NULL)) {
328 printk(KERN_ERR
329 "%s() Missing msg buf, size should be %d bytes\n",
330 __func__, msg->size);
331 return ret;
332 }
333
334 mutex_lock(&bus->lock);
335
336 /* Peek the bus to see if a msg exists, if it's not what we're expecting
337 * then return cleanly else read the message from the bus.
338 */
339 curr_gwp = le32_to_cpu(saa7164_readl(bus->m_dwGetWritePos));
340 curr_grp = le32_to_cpu(saa7164_readl(bus->m_dwGetReadPos));
341
342 if (curr_gwp == curr_grp) {
343 ret = SAA_ERR_EMPTY;
344 goto out;
345 }
346
347 bytes_to_read = sizeof(*msg);
348
349 /* Calculate write distance to current read position */
350 write_distance = 0;
351 if (curr_gwp >= curr_grp)
352 /* Write doesn't wrap around the ring */
353 write_distance = curr_gwp - curr_grp;
354 else
355 /* Write wraps around the ring */
356 write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
357
358 if (bytes_to_read > write_distance) {
359 printk(KERN_ERR "%s() No message/response found\n", __func__);
360 ret = SAA_ERR_INVALID_COMMAND;
361 goto out;
362 }
363
364 /* Calculate the new read position */
365 new_grp = curr_grp + bytes_to_read;
366 if (new_grp > bus->m_dwSizeGetRing) {
367
368 /* Ring wraps */
369 new_grp -= bus->m_dwSizeGetRing;
370 space_rem = bus->m_dwSizeGetRing - curr_grp;
371
372 memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
373 memcpy((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
374 bytes_to_read - space_rem);
375
376 } else {
377 /* No wrapping */
378 memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
379 }
380
381 /* No need to update the read positions, because this was a peek */
382 /* If the caller specifically want to peek, return */
383 if (peekonly) {
384 memcpy(msg, &msg_tmp, sizeof(*msg));
385 goto peekout;
386 }
387
388 /* Check if the command/response matches what is expected */
389 if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
390 (msg_tmp.controlselector != msg->controlselector) ||
391 (msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {
392
393 printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
394 saa7164_bus_dumpmsg(dev, msg, buf);
395 saa7164_bus_dumpmsg(dev, &msg_tmp, NULL);
396 ret = SAA_ERR_INVALID_COMMAND;
397 goto out;
398 }
399
400 /* Get the actual command and response from the bus */
401 buf_size = msg->size;
402
403 bytes_to_read = sizeof(*msg) + msg->size;
404 /* Calculate write distance to current read position */
405 write_distance = 0;
406 if (curr_gwp >= curr_grp)
407 /* Write doesn't wrap around the ring */
408 write_distance = curr_gwp - curr_grp;
409 else
410 /* Write wraps around the ring */
411 write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
412
413 if (bytes_to_read > write_distance) {
414 printk(KERN_ERR "%s() Invalid bus state, missing msg "
415 "or mangled ring, faulty H/W / bad code?\n", __func__);
416 ret = SAA_ERR_INVALID_COMMAND;
417 goto out;
418 }
419
420 /* Calculate the new read position */
421 new_grp = curr_grp + bytes_to_read;
422 if (new_grp > bus->m_dwSizeGetRing) {
423
424 /* Ring wraps */
425 new_grp -= bus->m_dwSizeGetRing;
426 space_rem = bus->m_dwSizeGetRing - curr_grp;
427
428 if (space_rem < sizeof(*msg)) {
429 /* msg wraps around the ring */
430 memcpy(msg, bus->m_pdwGetRing + curr_grp, space_rem);
431 memcpy((u8 *)msg + space_rem, bus->m_pdwGetRing,
432 sizeof(*msg) - space_rem);
433 if (buf)
434 memcpy(buf, bus->m_pdwGetRing + sizeof(*msg) -
435 space_rem, buf_size);
436
437 } else if (space_rem == sizeof(*msg)) {
438 memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
439 if (buf)
440 memcpy(buf, bus->m_pdwGetRing, buf_size);
441 } else {
442 /* Additional data wraps around the ring */
443 memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
444 if (buf) {
445 memcpy(buf, bus->m_pdwGetRing + curr_grp +
446 sizeof(*msg), space_rem - sizeof(*msg));
447 memcpy(buf + space_rem - sizeof(*msg),
448 bus->m_pdwGetRing, bytes_to_read -
449 space_rem);
450 }
451
452 }
453
454 } else {
455 /* No wrapping */
456 memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
457 if (buf)
458 memcpy(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
459 buf_size);
460 }
461
462 /* Update the read positions, adjusting the ring */
463 saa7164_writel(bus->m_dwGetReadPos, cpu_to_le32(new_grp));
464
465peekout:
466 msg->size = le16_to_cpu(msg->size);
467 msg->command = le32_to_cpu(msg->command);
468 msg->controlselector = le16_to_cpu(msg->controlselector);
469 ret = SAA_OK;
470out:
471 mutex_unlock(&bus->lock);
472 saa7164_bus_verify(dev);
473 return ret;
474}
475
diff --git a/drivers/media/pci/saa7164/saa7164-cards.c b/drivers/media/pci/saa7164/saa7164-cards.c
new file mode 100644
index 000000000000..5b72da5ce418
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-cards.c
@@ -0,0 +1,773 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/pci.h>
25#include <linux/delay.h>
26
27#include "saa7164.h"
28
29/* The Bridge API needs to understand register widths (in bytes) for the
30 * attached I2C devices, so we can simplify the virtual i2c mechansms
31 * and keep the -i2c.c implementation clean.
32 */
33#define REGLEN_8bit 1
34#define REGLEN_16bit 2
35
36struct saa7164_board saa7164_boards[] = {
37 [SAA7164_BOARD_UNKNOWN] = {
38 /* Bridge will not load any firmware, without knowing
39 * the rev this would be fatal. */
40 .name = "Unknown",
41 },
42 [SAA7164_BOARD_UNKNOWN_REV2] = {
43 /* Bridge will load the v2 f/w and dump descriptors */
44 /* Required during new board bringup */
45 .name = "Generic Rev2",
46 .chiprev = SAA7164_CHIP_REV2,
47 },
48 [SAA7164_BOARD_UNKNOWN_REV3] = {
49 /* Bridge will load the v2 f/w and dump descriptors */
50 /* Required during new board bringup */
51 .name = "Generic Rev3",
52 .chiprev = SAA7164_CHIP_REV3,
53 },
54 [SAA7164_BOARD_HAUPPAUGE_HVR2200] = {
55 .name = "Hauppauge WinTV-HVR2200",
56 .porta = SAA7164_MPEG_DVB,
57 .portb = SAA7164_MPEG_DVB,
58 .portc = SAA7164_MPEG_ENCODER,
59 .portd = SAA7164_MPEG_ENCODER,
60 .porte = SAA7164_MPEG_VBI,
61 .portf = SAA7164_MPEG_VBI,
62 .chiprev = SAA7164_CHIP_REV3,
63 .unit = {{
64 .id = 0x1d,
65 .type = SAA7164_UNIT_EEPROM,
66 .name = "4K EEPROM",
67 .i2c_bus_nr = SAA7164_I2C_BUS_0,
68 .i2c_bus_addr = 0xa0 >> 1,
69 .i2c_reg_len = REGLEN_8bit,
70 }, {
71 .id = 0x04,
72 .type = SAA7164_UNIT_TUNER,
73 .name = "TDA18271-1",
74 .i2c_bus_nr = SAA7164_I2C_BUS_1,
75 .i2c_bus_addr = 0xc0 >> 1,
76 .i2c_reg_len = REGLEN_8bit,
77 }, {
78 .id = 0x1b,
79 .type = SAA7164_UNIT_TUNER,
80 .name = "TDA18271-2",
81 .i2c_bus_nr = SAA7164_I2C_BUS_2,
82 .i2c_bus_addr = 0xc0 >> 1,
83 .i2c_reg_len = REGLEN_8bit,
84 }, {
85 .id = 0x1e,
86 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
87 .name = "TDA10048-1",
88 .i2c_bus_nr = SAA7164_I2C_BUS_1,
89 .i2c_bus_addr = 0x10 >> 1,
90 .i2c_reg_len = REGLEN_8bit,
91 }, {
92 .id = 0x1f,
93 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
94 .name = "TDA10048-2",
95 .i2c_bus_nr = SAA7164_I2C_BUS_2,
96 .i2c_bus_addr = 0x12 >> 1,
97 .i2c_reg_len = REGLEN_8bit,
98 } },
99 },
100 [SAA7164_BOARD_HAUPPAUGE_HVR2200_2] = {
101 .name = "Hauppauge WinTV-HVR2200",
102 .porta = SAA7164_MPEG_DVB,
103 .portb = SAA7164_MPEG_DVB,
104 .portc = SAA7164_MPEG_ENCODER,
105 .portd = SAA7164_MPEG_ENCODER,
106 .porte = SAA7164_MPEG_VBI,
107 .portf = SAA7164_MPEG_VBI,
108 .chiprev = SAA7164_CHIP_REV2,
109 .unit = {{
110 .id = 0x06,
111 .type = SAA7164_UNIT_EEPROM,
112 .name = "4K EEPROM",
113 .i2c_bus_nr = SAA7164_I2C_BUS_0,
114 .i2c_bus_addr = 0xa0 >> 1,
115 .i2c_reg_len = REGLEN_8bit,
116 }, {
117 .id = 0x04,
118 .type = SAA7164_UNIT_TUNER,
119 .name = "TDA18271-1",
120 .i2c_bus_nr = SAA7164_I2C_BUS_1,
121 .i2c_bus_addr = 0xc0 >> 1,
122 .i2c_reg_len = REGLEN_8bit,
123 }, {
124 .id = 0x05,
125 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
126 .name = "TDA10048-1",
127 .i2c_bus_nr = SAA7164_I2C_BUS_1,
128 .i2c_bus_addr = 0x10 >> 1,
129 .i2c_reg_len = REGLEN_8bit,
130 }, {
131 .id = 0x1e,
132 .type = SAA7164_UNIT_TUNER,
133 .name = "TDA18271-2",
134 .i2c_bus_nr = SAA7164_I2C_BUS_2,
135 .i2c_bus_addr = 0xc0 >> 1,
136 .i2c_reg_len = REGLEN_8bit,
137 }, {
138 .id = 0x1f,
139 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
140 .name = "TDA10048-2",
141 .i2c_bus_nr = SAA7164_I2C_BUS_2,
142 .i2c_bus_addr = 0x12 >> 1,
143 .i2c_reg_len = REGLEN_8bit,
144 } },
145 },
146 [SAA7164_BOARD_HAUPPAUGE_HVR2200_3] = {
147 .name = "Hauppauge WinTV-HVR2200",
148 .porta = SAA7164_MPEG_DVB,
149 .portb = SAA7164_MPEG_DVB,
150 .portc = SAA7164_MPEG_ENCODER,
151 .portd = SAA7164_MPEG_ENCODER,
152 .porte = SAA7164_MPEG_VBI,
153 .portf = SAA7164_MPEG_VBI,
154 .chiprev = SAA7164_CHIP_REV2,
155 .unit = {{
156 .id = 0x1d,
157 .type = SAA7164_UNIT_EEPROM,
158 .name = "4K EEPROM",
159 .i2c_bus_nr = SAA7164_I2C_BUS_0,
160 .i2c_bus_addr = 0xa0 >> 1,
161 .i2c_reg_len = REGLEN_8bit,
162 }, {
163 .id = 0x04,
164 .type = SAA7164_UNIT_TUNER,
165 .name = "TDA18271-1",
166 .i2c_bus_nr = SAA7164_I2C_BUS_1,
167 .i2c_bus_addr = 0xc0 >> 1,
168 .i2c_reg_len = REGLEN_8bit,
169 }, {
170 .id = 0x05,
171 .type = SAA7164_UNIT_ANALOG_DEMODULATOR,
172 .name = "TDA8290-1",
173 .i2c_bus_nr = SAA7164_I2C_BUS_1,
174 .i2c_bus_addr = 0x84 >> 1,
175 .i2c_reg_len = REGLEN_8bit,
176 }, {
177 .id = 0x1b,
178 .type = SAA7164_UNIT_TUNER,
179 .name = "TDA18271-2",
180 .i2c_bus_nr = SAA7164_I2C_BUS_2,
181 .i2c_bus_addr = 0xc0 >> 1,
182 .i2c_reg_len = REGLEN_8bit,
183 }, {
184 .id = 0x1c,
185 .type = SAA7164_UNIT_ANALOG_DEMODULATOR,
186 .name = "TDA8290-2",
187 .i2c_bus_nr = SAA7164_I2C_BUS_2,
188 .i2c_bus_addr = 0x84 >> 1,
189 .i2c_reg_len = REGLEN_8bit,
190 }, {
191 .id = 0x1e,
192 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
193 .name = "TDA10048-1",
194 .i2c_bus_nr = SAA7164_I2C_BUS_1,
195 .i2c_bus_addr = 0x10 >> 1,
196 .i2c_reg_len = REGLEN_8bit,
197 }, {
198 .id = 0x1f,
199 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
200 .name = "TDA10048-2",
201 .i2c_bus_nr = SAA7164_I2C_BUS_2,
202 .i2c_bus_addr = 0x12 >> 1,
203 .i2c_reg_len = REGLEN_8bit,
204 } },
205 },
206 [SAA7164_BOARD_HAUPPAUGE_HVR2200_4] = {
207 .name = "Hauppauge WinTV-HVR2200",
208 .porta = SAA7164_MPEG_DVB,
209 .portb = SAA7164_MPEG_DVB,
210 .portc = SAA7164_MPEG_ENCODER,
211 .portd = SAA7164_MPEG_ENCODER,
212 .porte = SAA7164_MPEG_VBI,
213 .portf = SAA7164_MPEG_VBI,
214 .chiprev = SAA7164_CHIP_REV3,
215 .unit = {{
216 .id = 0x1d,
217 .type = SAA7164_UNIT_EEPROM,
218 .name = "4K EEPROM",
219 .i2c_bus_nr = SAA7164_I2C_BUS_0,
220 .i2c_bus_addr = 0xa0 >> 1,
221 .i2c_reg_len = REGLEN_8bit,
222 }, {
223 .id = 0x04,
224 .type = SAA7164_UNIT_TUNER,
225 .name = "TDA18271-1",
226 .i2c_bus_nr = SAA7164_I2C_BUS_1,
227 .i2c_bus_addr = 0xc0 >> 1,
228 .i2c_reg_len = REGLEN_8bit,
229 }, {
230 .id = 0x05,
231 .type = SAA7164_UNIT_ANALOG_DEMODULATOR,
232 .name = "TDA8290-1",
233 .i2c_bus_nr = SAA7164_I2C_BUS_1,
234 .i2c_bus_addr = 0x84 >> 1,
235 .i2c_reg_len = REGLEN_8bit,
236 }, {
237 .id = 0x1b,
238 .type = SAA7164_UNIT_TUNER,
239 .name = "TDA18271-2",
240 .i2c_bus_nr = SAA7164_I2C_BUS_2,
241 .i2c_bus_addr = 0xc0 >> 1,
242 .i2c_reg_len = REGLEN_8bit,
243 }, {
244 .id = 0x1c,
245 .type = SAA7164_UNIT_ANALOG_DEMODULATOR,
246 .name = "TDA8290-2",
247 .i2c_bus_nr = SAA7164_I2C_BUS_2,
248 .i2c_bus_addr = 0x84 >> 1,
249 .i2c_reg_len = REGLEN_8bit,
250 }, {
251 .id = 0x1e,
252 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
253 .name = "TDA10048-1",
254 .i2c_bus_nr = SAA7164_I2C_BUS_1,
255 .i2c_bus_addr = 0x10 >> 1,
256 .i2c_reg_len = REGLEN_8bit,
257 }, {
258 .id = 0x1f,
259 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
260 .name = "TDA10048-2",
261 .i2c_bus_nr = SAA7164_I2C_BUS_2,
262 .i2c_bus_addr = 0x12 >> 1,
263 .i2c_reg_len = REGLEN_8bit,
264 } },
265 },
266 [SAA7164_BOARD_HAUPPAUGE_HVR2250] = {
267 .name = "Hauppauge WinTV-HVR2250",
268 .porta = SAA7164_MPEG_DVB,
269 .portb = SAA7164_MPEG_DVB,
270 .portc = SAA7164_MPEG_ENCODER,
271 .portd = SAA7164_MPEG_ENCODER,
272 .porte = SAA7164_MPEG_VBI,
273 .portf = SAA7164_MPEG_VBI,
274 .chiprev = SAA7164_CHIP_REV3,
275 .unit = {{
276 .id = 0x22,
277 .type = SAA7164_UNIT_EEPROM,
278 .name = "4K EEPROM",
279 .i2c_bus_nr = SAA7164_I2C_BUS_0,
280 .i2c_bus_addr = 0xa0 >> 1,
281 .i2c_reg_len = REGLEN_8bit,
282 }, {
283 .id = 0x04,
284 .type = SAA7164_UNIT_TUNER,
285 .name = "TDA18271-1",
286 .i2c_bus_nr = SAA7164_I2C_BUS_1,
287 .i2c_bus_addr = 0xc0 >> 1,
288 .i2c_reg_len = REGLEN_8bit,
289 }, {
290 .id = 0x07,
291 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
292 .name = "CX24228/S5H1411-1 (TOP)",
293 .i2c_bus_nr = SAA7164_I2C_BUS_1,
294 .i2c_bus_addr = 0x32 >> 1,
295 .i2c_reg_len = REGLEN_8bit,
296 }, {
297 .id = 0x08,
298 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
299 .name = "CX24228/S5H1411-1 (QAM)",
300 .i2c_bus_nr = SAA7164_I2C_BUS_1,
301 .i2c_bus_addr = 0x34 >> 1,
302 .i2c_reg_len = REGLEN_8bit,
303 }, {
304 .id = 0x1e,
305 .type = SAA7164_UNIT_TUNER,
306 .name = "TDA18271-2",
307 .i2c_bus_nr = SAA7164_I2C_BUS_2,
308 .i2c_bus_addr = 0xc0 >> 1,
309 .i2c_reg_len = REGLEN_8bit,
310 }, {
311 .id = 0x20,
312 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
313 .name = "CX24228/S5H1411-2 (TOP)",
314 .i2c_bus_nr = SAA7164_I2C_BUS_2,
315 .i2c_bus_addr = 0x32 >> 1,
316 .i2c_reg_len = REGLEN_8bit,
317 }, {
318 .id = 0x23,
319 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
320 .name = "CX24228/S5H1411-2 (QAM)",
321 .i2c_bus_nr = SAA7164_I2C_BUS_2,
322 .i2c_bus_addr = 0x34 >> 1,
323 .i2c_reg_len = REGLEN_8bit,
324 } },
325 },
326 [SAA7164_BOARD_HAUPPAUGE_HVR2250_2] = {
327 .name = "Hauppauge WinTV-HVR2250",
328 .porta = SAA7164_MPEG_DVB,
329 .portb = SAA7164_MPEG_DVB,
330 .portc = SAA7164_MPEG_ENCODER,
331 .portd = SAA7164_MPEG_ENCODER,
332 .porte = SAA7164_MPEG_VBI,
333 .portf = SAA7164_MPEG_VBI,
334 .chiprev = SAA7164_CHIP_REV3,
335 .unit = {{
336 .id = 0x28,
337 .type = SAA7164_UNIT_EEPROM,
338 .name = "4K EEPROM",
339 .i2c_bus_nr = SAA7164_I2C_BUS_0,
340 .i2c_bus_addr = 0xa0 >> 1,
341 .i2c_reg_len = REGLEN_8bit,
342 }, {
343 .id = 0x04,
344 .type = SAA7164_UNIT_TUNER,
345 .name = "TDA18271-1",
346 .i2c_bus_nr = SAA7164_I2C_BUS_1,
347 .i2c_bus_addr = 0xc0 >> 1,
348 .i2c_reg_len = REGLEN_8bit,
349 }, {
350 .id = 0x07,
351 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
352 .name = "CX24228/S5H1411-1 (TOP)",
353 .i2c_bus_nr = SAA7164_I2C_BUS_1,
354 .i2c_bus_addr = 0x32 >> 1,
355 .i2c_reg_len = REGLEN_8bit,
356 }, {
357 .id = 0x08,
358 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
359 .name = "CX24228/S5H1411-1 (QAM)",
360 .i2c_bus_nr = SAA7164_I2C_BUS_1,
361 .i2c_bus_addr = 0x34 >> 1,
362 .i2c_reg_len = REGLEN_8bit,
363 }, {
364 .id = 0x24,
365 .type = SAA7164_UNIT_TUNER,
366 .name = "TDA18271-2",
367 .i2c_bus_nr = SAA7164_I2C_BUS_2,
368 .i2c_bus_addr = 0xc0 >> 1,
369 .i2c_reg_len = REGLEN_8bit,
370 }, {
371 .id = 0x26,
372 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
373 .name = "CX24228/S5H1411-2 (TOP)",
374 .i2c_bus_nr = SAA7164_I2C_BUS_2,
375 .i2c_bus_addr = 0x32 >> 1,
376 .i2c_reg_len = REGLEN_8bit,
377 }, {
378 .id = 0x29,
379 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
380 .name = "CX24228/S5H1411-2 (QAM)",
381 .i2c_bus_nr = SAA7164_I2C_BUS_2,
382 .i2c_bus_addr = 0x34 >> 1,
383 .i2c_reg_len = REGLEN_8bit,
384 } },
385 },
386 [SAA7164_BOARD_HAUPPAUGE_HVR2250_3] = {
387 .name = "Hauppauge WinTV-HVR2250",
388 .porta = SAA7164_MPEG_DVB,
389 .portb = SAA7164_MPEG_DVB,
390 .portc = SAA7164_MPEG_ENCODER,
391 .portd = SAA7164_MPEG_ENCODER,
392 .porte = SAA7164_MPEG_VBI,
393 .portf = SAA7164_MPEG_VBI,
394 .chiprev = SAA7164_CHIP_REV3,
395 .unit = {{
396 .id = 0x26,
397 .type = SAA7164_UNIT_EEPROM,
398 .name = "4K EEPROM",
399 .i2c_bus_nr = SAA7164_I2C_BUS_0,
400 .i2c_bus_addr = 0xa0 >> 1,
401 .i2c_reg_len = REGLEN_8bit,
402 }, {
403 .id = 0x04,
404 .type = SAA7164_UNIT_TUNER,
405 .name = "TDA18271-1",
406 .i2c_bus_nr = SAA7164_I2C_BUS_1,
407 .i2c_bus_addr = 0xc0 >> 1,
408 .i2c_reg_len = REGLEN_8bit,
409 }, {
410 .id = 0x07,
411 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
412 .name = "CX24228/S5H1411-1 (TOP)",
413 .i2c_bus_nr = SAA7164_I2C_BUS_1,
414 .i2c_bus_addr = 0x32 >> 1,
415 .i2c_reg_len = REGLEN_8bit,
416 }, {
417 .id = 0x08,
418 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
419 .name = "CX24228/S5H1411-1 (QAM)",
420 .i2c_bus_nr = SAA7164_I2C_BUS_1,
421 .i2c_bus_addr = 0x34 >> 1,
422 .i2c_reg_len = REGLEN_8bit,
423 }, {
424 .id = 0x22,
425 .type = SAA7164_UNIT_TUNER,
426 .name = "TDA18271-2",
427 .i2c_bus_nr = SAA7164_I2C_BUS_2,
428 .i2c_bus_addr = 0xc0 >> 1,
429 .i2c_reg_len = REGLEN_8bit,
430 }, {
431 .id = 0x24,
432 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
433 .name = "CX24228/S5H1411-2 (TOP)",
434 .i2c_bus_nr = SAA7164_I2C_BUS_2,
435 .i2c_bus_addr = 0x32 >> 1,
436 .i2c_reg_len = REGLEN_8bit,
437 }, {
438 .id = 0x27,
439 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
440 .name = "CX24228/S5H1411-2 (QAM)",
441 .i2c_bus_nr = SAA7164_I2C_BUS_2,
442 .i2c_bus_addr = 0x34 >> 1,
443 .i2c_reg_len = REGLEN_8bit,
444 } },
445 },
446 [SAA7164_BOARD_HAUPPAUGE_HVR2200_5] = {
447 .name = "Hauppauge WinTV-HVR2200",
448 .porta = SAA7164_MPEG_DVB,
449 .portb = SAA7164_MPEG_DVB,
450 .chiprev = SAA7164_CHIP_REV3,
451 .unit = {{
452 .id = 0x23,
453 .type = SAA7164_UNIT_EEPROM,
454 .name = "4K EEPROM",
455 .i2c_bus_nr = SAA7164_I2C_BUS_0,
456 .i2c_bus_addr = 0xa0 >> 1,
457 .i2c_reg_len = REGLEN_8bit,
458 }, {
459 .id = 0x04,
460 .type = SAA7164_UNIT_TUNER,
461 .name = "TDA18271-1",
462 .i2c_bus_nr = SAA7164_I2C_BUS_1,
463 .i2c_bus_addr = 0xc0 >> 1,
464 .i2c_reg_len = REGLEN_8bit,
465 }, {
466 .id = 0x05,
467 .type = SAA7164_UNIT_ANALOG_DEMODULATOR,
468 .name = "TDA8290-1",
469 .i2c_bus_nr = SAA7164_I2C_BUS_1,
470 .i2c_bus_addr = 0x84 >> 1,
471 .i2c_reg_len = REGLEN_8bit,
472 }, {
473 .id = 0x21,
474 .type = SAA7164_UNIT_TUNER,
475 .name = "TDA18271-2",
476 .i2c_bus_nr = SAA7164_I2C_BUS_2,
477 .i2c_bus_addr = 0xc0 >> 1,
478 .i2c_reg_len = REGLEN_8bit,
479 }, {
480 .id = 0x22,
481 .type = SAA7164_UNIT_ANALOG_DEMODULATOR,
482 .name = "TDA8290-2",
483 .i2c_bus_nr = SAA7164_I2C_BUS_2,
484 .i2c_bus_addr = 0x84 >> 1,
485 .i2c_reg_len = REGLEN_8bit,
486 }, {
487 .id = 0x24,
488 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
489 .name = "TDA10048-1",
490 .i2c_bus_nr = SAA7164_I2C_BUS_1,
491 .i2c_bus_addr = 0x10 >> 1,
492 .i2c_reg_len = REGLEN_8bit,
493 }, {
494 .id = 0x25,
495 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
496 .name = "TDA10048-2",
497 .i2c_bus_nr = SAA7164_I2C_BUS_2,
498 .i2c_bus_addr = 0x12 >> 1,
499 .i2c_reg_len = REGLEN_8bit,
500 } },
501 },
502};
503const unsigned int saa7164_bcount = ARRAY_SIZE(saa7164_boards);
504
505/* ------------------------------------------------------------------ */
506/* PCI subsystem IDs */
507
508struct saa7164_subid saa7164_subids[] = {
509 {
510 .subvendor = 0x0070,
511 .subdevice = 0x8880,
512 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250,
513 }, {
514 .subvendor = 0x0070,
515 .subdevice = 0x8810,
516 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250,
517 }, {
518 .subvendor = 0x0070,
519 .subdevice = 0x8980,
520 .card = SAA7164_BOARD_HAUPPAUGE_HVR2200,
521 }, {
522 .subvendor = 0x0070,
523 .subdevice = 0x8900,
524 .card = SAA7164_BOARD_HAUPPAUGE_HVR2200_2,
525 }, {
526 .subvendor = 0x0070,
527 .subdevice = 0x8901,
528 .card = SAA7164_BOARD_HAUPPAUGE_HVR2200_3,
529 }, {
530 .subvendor = 0x0070,
531 .subdevice = 0x88A1,
532 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250_3,
533 }, {
534 .subvendor = 0x0070,
535 .subdevice = 0x8891,
536 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250_2,
537 }, {
538 .subvendor = 0x0070,
539 .subdevice = 0x8851,
540 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250_2,
541 }, {
542 .subvendor = 0x0070,
543 .subdevice = 0x8940,
544 .card = SAA7164_BOARD_HAUPPAUGE_HVR2200_4,
545 }, {
546 .subvendor = 0x0070,
547 .subdevice = 0x8953,
548 .card = SAA7164_BOARD_HAUPPAUGE_HVR2200_5,
549 },
550};
551const unsigned int saa7164_idcount = ARRAY_SIZE(saa7164_subids);
552
553void saa7164_card_list(struct saa7164_dev *dev)
554{
555 int i;
556
557 if (0 == dev->pci->subsystem_vendor &&
558 0 == dev->pci->subsystem_device) {
559 printk(KERN_ERR
560 "%s: Board has no valid PCIe Subsystem ID and can't\n"
561 "%s: be autodetected. Pass card=<n> insmod option to\n"
562 "%s: workaround that. Send complaints to the vendor\n"
563 "%s: of the TV card. Best regards,\n"
564 "%s: -- tux\n",
565 dev->name, dev->name, dev->name, dev->name, dev->name);
566 } else {
567 printk(KERN_ERR
568 "%s: Your board isn't known (yet) to the driver.\n"
569 "%s: Try to pick one of the existing card configs via\n"
570 "%s: card=<n> insmod option. Updating to the latest\n"
571 "%s: version might help as well.\n",
572 dev->name, dev->name, dev->name, dev->name);
573 }
574
575 printk(KERN_ERR "%s: Here are valid choices for the card=<n> insmod "
576 "option:\n", dev->name);
577
578 for (i = 0; i < saa7164_bcount; i++)
579 printk(KERN_ERR "%s: card=%d -> %s\n",
580 dev->name, i, saa7164_boards[i].name);
581}
582
583/* TODO: clean this define up into the -cards.c structs */
584#define PCIEBRIDGE_UNITID 2
585
586void saa7164_gpio_setup(struct saa7164_dev *dev)
587{
588 switch (dev->board) {
589 case SAA7164_BOARD_HAUPPAUGE_HVR2200:
590 case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
591 case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
592 case SAA7164_BOARD_HAUPPAUGE_HVR2200_4:
593 case SAA7164_BOARD_HAUPPAUGE_HVR2200_5:
594 case SAA7164_BOARD_HAUPPAUGE_HVR2250:
595 case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
596 case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
597 /*
598 GPIO 2: s5h1411 / tda10048-1 demod reset
599 GPIO 3: s5h1411 / tda10048-2 demod reset
600 GPIO 7: IRBlaster Zilog reset
601 */
602
603 /* Reset parts by going in and out of reset */
604 saa7164_api_clear_gpiobit(dev, PCIEBRIDGE_UNITID, 2);
605 saa7164_api_clear_gpiobit(dev, PCIEBRIDGE_UNITID, 3);
606
607 msleep(20);
608
609 saa7164_api_set_gpiobit(dev, PCIEBRIDGE_UNITID, 2);
610 saa7164_api_set_gpiobit(dev, PCIEBRIDGE_UNITID, 3);
611 break;
612 }
613}
614
615static void hauppauge_eeprom(struct saa7164_dev *dev, u8 *eeprom_data)
616{
617 struct tveeprom tv;
618
619 /* TODO: Assumption: eeprom on bus 0 */
620 tveeprom_hauppauge_analog(&dev->i2c_bus[0].i2c_client, &tv,
621 eeprom_data);
622
623 /* Make sure we support the board model */
624 switch (tv.model) {
625 case 88001:
626 /* Development board - Limit circulation */
627 /* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
628 * ATSC/QAM (TDA18271/S5H1411) and basic analog, no IR, FM */
629 case 88021:
630 /* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
631 * ATSC/QAM (TDA18271/S5H1411) and basic analog, MCE CIR, FM */
632 break;
633 case 88041:
634 /* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
635 * ATSC/QAM (TDA18271/S5H1411) and basic analog, no IR, FM */
636 break;
637 case 88061:
638 /* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
639 * ATSC/QAM (TDA18271/S5H1411) and basic analog, FM */
640 break;
641 case 89519:
642 case 89609:
643 /* WinTV-HVR2200 (PCIe, Retail, full-height)
644 * DVB-T (TDA18271/TDA10048) and basic analog, no IR */
645 break;
646 case 89619:
647 /* WinTV-HVR2200 (PCIe, Retail, half-height)
648 * DVB-T (TDA18271/TDA10048) and basic analog, no IR */
649 break;
650 default:
651 printk(KERN_ERR "%s: Warning: Unknown Hauppauge model #%d\n",
652 dev->name, tv.model);
653 break;
654 }
655
656 printk(KERN_INFO "%s: Hauppauge eeprom: model=%d\n", dev->name,
657 tv.model);
658}
659
660void saa7164_card_setup(struct saa7164_dev *dev)
661{
662 static u8 eeprom[256];
663
664 if (dev->i2c_bus[0].i2c_rc == 0) {
665 if (saa7164_api_read_eeprom(dev, &eeprom[0],
666 sizeof(eeprom)) < 0)
667 return;
668 }
669
670 switch (dev->board) {
671 case SAA7164_BOARD_HAUPPAUGE_HVR2200:
672 case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
673 case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
674 case SAA7164_BOARD_HAUPPAUGE_HVR2200_4:
675 case SAA7164_BOARD_HAUPPAUGE_HVR2200_5:
676 case SAA7164_BOARD_HAUPPAUGE_HVR2250:
677 case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
678 case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
679 hauppauge_eeprom(dev, &eeprom[0]);
680 break;
681 }
682}
683
684/* With most other drivers, the kernel expects to communicate with subdrivers
685 * through i2c. This bridge does not allow that, it does not expose any direct
686 * access to I2C. Instead we have to communicate through the device f/w for
687 * register access to 'processing units'. Each unit has a unique
688 * id, regardless of how the physical implementation occurs across
689 * the three physical i2c busses. The being said if we want leverge of
690 * the existing kernel drivers for tuners and demods we have to 'speak i2c',
691 * to this bridge implements 3 virtual i2c buses. This is a helper function
692 * for those.
693 *
694 * Description: Translate the kernels notion of an i2c address and bus into
695 * the appropriate unitid.
696 */
697int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr)
698{
699 /* For a given bus and i2c device address, return the saa7164 unique
700 * unitid. < 0 on error */
701
702 struct saa7164_dev *dev = bus->dev;
703 struct saa7164_unit *unit;
704 int i;
705
706 for (i = 0; i < SAA7164_MAX_UNITS; i++) {
707 unit = &saa7164_boards[dev->board].unit[i];
708
709 if (unit->type == SAA7164_UNIT_UNDEFINED)
710 continue;
711 if ((bus->nr == unit->i2c_bus_nr) &&
712 (addr == unit->i2c_bus_addr))
713 return unit->id;
714 }
715
716 return -1;
717}
718
719/* The 7164 API needs to know the i2c register length in advance.
720 * this is a helper function. Based on a specific chip addr and bus return the
721 * reg length.
722 */
723int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr)
724{
725 /* For a given bus and i2c device address, return the
726 * saa7164 registry address width. < 0 on error
727 */
728
729 struct saa7164_dev *dev = bus->dev;
730 struct saa7164_unit *unit;
731 int i;
732
733 for (i = 0; i < SAA7164_MAX_UNITS; i++) {
734 unit = &saa7164_boards[dev->board].unit[i];
735
736 if (unit->type == SAA7164_UNIT_UNDEFINED)
737 continue;
738
739 if ((bus->nr == unit->i2c_bus_nr) &&
740 (addr == unit->i2c_bus_addr))
741 return unit->i2c_reg_len;
742 }
743
744 return -1;
745}
746/* TODO: implement a 'findeeprom' functio like the above and fix any other
747 * eeprom related todo's in -api.c.
748 */
749
750/* Translate a unitid into a x readable device name, for display purposes. */
751char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid)
752{
753 char *undefed = "UNDEFINED";
754 char *bridge = "BRIDGE";
755 struct saa7164_unit *unit;
756 int i;
757
758 if (unitid == 0)
759 return bridge;
760
761 for (i = 0; i < SAA7164_MAX_UNITS; i++) {
762 unit = &saa7164_boards[dev->board].unit[i];
763
764 if (unit->type == SAA7164_UNIT_UNDEFINED)
765 continue;
766
767 if (unitid == unit->id)
768 return unit->name;
769 }
770
771 return undefed;
772}
773
diff --git a/drivers/media/pci/saa7164/saa7164-cmd.c b/drivers/media/pci/saa7164/saa7164-cmd.c
new file mode 100644
index 000000000000..62fac7f9d04e
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-cmd.c
@@ -0,0 +1,589 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/wait.h>
23
24#include "saa7164.h"
25
26int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
27{
28 int i, ret = -1;
29
30 mutex_lock(&dev->lock);
31 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
32 if (dev->cmds[i].inuse == 0) {
33 dev->cmds[i].inuse = 1;
34 dev->cmds[i].signalled = 0;
35 dev->cmds[i].timeout = 0;
36 ret = dev->cmds[i].seqno;
37 break;
38 }
39 }
40 mutex_unlock(&dev->lock);
41
42 return ret;
43}
44
45void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
46{
47 mutex_lock(&dev->lock);
48 if ((dev->cmds[seqno].inuse == 1) &&
49 (dev->cmds[seqno].seqno == seqno)) {
50 dev->cmds[seqno].inuse = 0;
51 dev->cmds[seqno].signalled = 0;
52 dev->cmds[seqno].timeout = 0;
53 }
54 mutex_unlock(&dev->lock);
55}
56
57void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
58{
59 mutex_lock(&dev->lock);
60 if ((dev->cmds[seqno].inuse == 1) &&
61 (dev->cmds[seqno].seqno == seqno)) {
62 dev->cmds[seqno].timeout = 1;
63 }
64 mutex_unlock(&dev->lock);
65}
66
67u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
68{
69 int ret = 0;
70
71 mutex_lock(&dev->lock);
72 if ((dev->cmds[seqno].inuse == 1) &&
73 (dev->cmds[seqno].seqno == seqno)) {
74 ret = dev->cmds[seqno].timeout;
75 }
76 mutex_unlock(&dev->lock);
77
78 return ret;
79}
80
81/* Commands to the f/w get marshelled to/from this code then onto the PCI
82 * -bus/c running buffer. */
83int saa7164_irq_dequeue(struct saa7164_dev *dev)
84{
85 int ret = SAA_OK, i = 0;
86 u32 timeout;
87 wait_queue_head_t *q = NULL;
88 u8 tmp[512];
89 dprintk(DBGLVL_CMD, "%s()\n", __func__);
90
91 /* While any outstand message on the bus exists... */
92 do {
93
94 /* Peek the msg bus */
95 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
96 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
97 if (ret != SAA_OK)
98 break;
99
100 q = &dev->cmds[tRsp.seqno].wait;
101 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
102 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
103 if (!timeout) {
104 dprintk(DBGLVL_CMD,
105 "%s() signalled seqno(%d) (for dequeue)\n",
106 __func__, tRsp.seqno);
107 dev->cmds[tRsp.seqno].signalled = 1;
108 wake_up(q);
109 } else {
110 printk(KERN_ERR
111 "%s() found timed out command on the bus\n",
112 __func__);
113
114 /* Clean the bus */
115 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
116 printk(KERN_ERR "%s() ret = %x\n", __func__, ret);
117 if (ret == SAA_ERR_EMPTY)
118 /* Someone else already fetched the response */
119 return SAA_OK;
120
121 if (ret != SAA_OK)
122 return ret;
123 }
124
125 /* It's unlikely to have more than 4 or 5 pending messages,
126 * ensure we exit at some point regardless.
127 */
128 } while (i++ < 32);
129
130 return ret;
131}
132
133/* Commands to the f/w get marshelled to/from this code then onto the PCI
134 * -bus/c running buffer. */
135int saa7164_cmd_dequeue(struct saa7164_dev *dev)
136{
137 int loop = 1;
138 int ret;
139 u32 timeout;
140 wait_queue_head_t *q = NULL;
141 u8 tmp[512];
142 dprintk(DBGLVL_CMD, "%s()\n", __func__);
143
144 while (loop) {
145
146 struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
147 ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
148 if (ret == SAA_ERR_EMPTY)
149 return SAA_OK;
150
151 if (ret != SAA_OK)
152 return ret;
153
154 q = &dev->cmds[tRsp.seqno].wait;
155 timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
156 dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
157 if (timeout) {
158 printk(KERN_ERR "found timed out command on the bus\n");
159
160 /* Clean the bus */
161 ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
162 printk(KERN_ERR "ret = %x\n", ret);
163 if (ret == SAA_ERR_EMPTY)
164 /* Someone else already fetched the response */
165 return SAA_OK;
166
167 if (ret != SAA_OK)
168 return ret;
169
170 if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
171 printk(KERN_ERR "split response\n");
172 else
173 saa7164_cmd_free_seqno(dev, tRsp.seqno);
174
175 printk(KERN_ERR " timeout continue\n");
176 continue;
177 }
178
179 dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
180 __func__, tRsp.seqno);
181 dev->cmds[tRsp.seqno].signalled = 1;
182 wake_up(q);
183 return SAA_OK;
184 }
185
186 return SAA_OK;
187}
188
189int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
190 void *buf)
191{
192 struct tmComResBusInfo *bus = &dev->bus;
193 u8 cmd_sent;
194 u16 size, idx;
195 u32 cmds;
196 void *tmp;
197 int ret = -1;
198
199 if (!msg) {
200 printk(KERN_ERR "%s() !msg\n", __func__);
201 return SAA_ERR_BAD_PARAMETER;
202 }
203
204 mutex_lock(&dev->cmds[msg->id].lock);
205
206 size = msg->size;
207 idx = 0;
208 cmds = size / bus->m_wMaxReqSize;
209 if (size % bus->m_wMaxReqSize == 0)
210 cmds -= 1;
211
212 cmd_sent = 0;
213
214 /* Split the request into smaller chunks */
215 for (idx = 0; idx < cmds; idx++) {
216
217 msg->flags |= SAA_CMDFLAG_CONTINUE;
218 msg->size = bus->m_wMaxReqSize;
219 tmp = buf + idx * bus->m_wMaxReqSize;
220
221 ret = saa7164_bus_set(dev, msg, tmp);
222 if (ret != SAA_OK) {
223 printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
224
225 if (cmd_sent) {
226 ret = SAA_ERR_BUSY;
227 goto out;
228 }
229 ret = SAA_ERR_OVERFLOW;
230 goto out;
231 }
232 cmd_sent = 1;
233 }
234
235 /* If not the last command... */
236 if (idx != 0)
237 msg->flags &= ~SAA_CMDFLAG_CONTINUE;
238
239 msg->size = size - idx * bus->m_wMaxReqSize;
240
241 ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
242 if (ret != SAA_OK) {
243 printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
244
245 if (cmd_sent) {
246 ret = SAA_ERR_BUSY;
247 goto out;
248 }
249 ret = SAA_ERR_OVERFLOW;
250 goto out;
251 }
252 ret = SAA_OK;
253
254out:
255 mutex_unlock(&dev->cmds[msg->id].lock);
256 return ret;
257}
258
259/* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
260 * the event never occurred, or SAA_OK if it was signaled during the wait.
261 */
262int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
263{
264 wait_queue_head_t *q = NULL;
265 int ret = SAA_BUS_TIMEOUT;
266 unsigned long stamp;
267 int r;
268
269 if (saa_debug >= 4)
270 saa7164_bus_dump(dev);
271
272 dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
273
274 mutex_lock(&dev->lock);
275 if ((dev->cmds[seqno].inuse == 1) &&
276 (dev->cmds[seqno].seqno == seqno)) {
277 q = &dev->cmds[seqno].wait;
278 }
279 mutex_unlock(&dev->lock);
280
281 if (q) {
282 /* If we haven't been signalled we need to wait */
283 if (dev->cmds[seqno].signalled == 0) {
284 stamp = jiffies;
285 dprintk(DBGLVL_CMD,
286 "%s(seqno=%d) Waiting (signalled=%d)\n",
287 __func__, seqno, dev->cmds[seqno].signalled);
288
289 /* Wait for signalled to be flagged or timeout */
290 /* In a highly stressed system this can easily extend
291 * into multiple seconds before the deferred worker
292 * is scheduled, and we're woken up via signal.
293 * We typically are signalled in < 50ms but it can
294 * take MUCH longer.
295 */
296 wait_event_timeout(*q, dev->cmds[seqno].signalled,
297 (HZ * waitsecs));
298 r = time_before(jiffies, stamp + (HZ * waitsecs));
299 if (r)
300 ret = SAA_OK;
301 else
302 saa7164_cmd_timeout_seqno(dev, seqno);
303
304 dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d "
305 "(signalled=%d)\n", __func__, seqno, r,
306 dev->cmds[seqno].signalled);
307 } else
308 ret = SAA_OK;
309 } else
310 printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
311 __func__, seqno);
312
313 return ret;
314}
315
316void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
317{
318 int i;
319 dprintk(DBGLVL_CMD, "%s()\n", __func__);
320
321 mutex_lock(&dev->lock);
322 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
323 if (dev->cmds[i].inuse == 1) {
324 dprintk(DBGLVL_CMD,
325 "seqno %d inuse, sig = %d, t/out = %d\n",
326 dev->cmds[i].seqno,
327 dev->cmds[i].signalled,
328 dev->cmds[i].timeout);
329 }
330 }
331
332 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
333 if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
334 (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
335 dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
336 __func__, i);
337 dev->cmds[i].signalled = 1;
338 wake_up(&dev->cmds[i].wait);
339 }
340 }
341 mutex_unlock(&dev->lock);
342}
343
344int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
345 u16 controlselector, u16 size, void *buf)
346{
347 struct tmComResInfo command_t, *pcommand_t;
348 struct tmComResInfo response_t, *presponse_t;
349 u8 errdata[256];
350 u16 resp_dsize;
351 u16 data_recd;
352 u32 loop;
353 int ret;
354 int safety = 0;
355
356 dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, "
357 "sel = 0x%x)\n", __func__, saa7164_unitid_name(dev, id), id,
358 command, controlselector);
359
360 if ((size == 0) || (buf == NULL)) {
361 printk(KERN_ERR "%s() Invalid param\n", __func__);
362 return SAA_ERR_BAD_PARAMETER;
363 }
364
365 /* Prepare some basic command/response structures */
366 memset(&command_t, 0, sizeof(command_t));
367 memset(&response_t, 0, sizeof(response_t));
368 pcommand_t = &command_t;
369 presponse_t = &response_t;
370 command_t.id = id;
371 command_t.command = command;
372 command_t.controlselector = controlselector;
373 command_t.size = size;
374
375 /* Allocate a unique sequence number */
376 ret = saa7164_cmd_alloc_seqno(dev);
377 if (ret < 0) {
378 printk(KERN_ERR "%s() No free sequences\n", __func__);
379 ret = SAA_ERR_NO_RESOURCES;
380 goto out;
381 }
382
383 command_t.seqno = (u8)ret;
384
385 /* Send Command */
386 resp_dsize = size;
387 pcommand_t->size = size;
388
389 dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
390 __func__, pcommand_t->seqno);
391
392 dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
393 __func__, pcommand_t->size);
394
395 ret = saa7164_cmd_set(dev, pcommand_t, buf);
396 if (ret != SAA_OK) {
397 printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
398
399 if (ret != SAA_ERR_BUSY)
400 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
401 else
402 /* Flag a timeout, because at least one
403 * command was sent */
404 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
405
406 goto out;
407 }
408
409 /* With split responses we have to collect the msgs piece by piece */
410 data_recd = 0;
411 loop = 1;
412 while (loop) {
413 dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
414
415 ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
416 dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
417
418 /* if power is down and this is not a power command ... */
419
420 if (ret == SAA_BUS_TIMEOUT) {
421 printk(KERN_ERR "Event timed out\n");
422 saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
423 return ret;
424 }
425
426 if (ret != SAA_OK) {
427 printk(KERN_ERR "spurious error\n");
428 return ret;
429 }
430
431 /* Peek response */
432 ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
433 if (ret == SAA_ERR_EMPTY) {
434 dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
435 continue;
436 }
437 if (ret != SAA_OK) {
438 printk(KERN_ERR "peek failed\n");
439 return ret;
440 }
441
442 dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
443 __func__, presponse_t->seqno);
444
445 dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
446 __func__, presponse_t->flags);
447
448 dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
449 __func__, presponse_t->size);
450
451 /* Check if the response was for our command */
452 if (presponse_t->seqno != pcommand_t->seqno) {
453
454 dprintk(DBGLVL_CMD,
455 "wrong event: seqno = %d, "
456 "expected seqno = %d, "
457 "will dequeue regardless\n",
458 presponse_t->seqno, pcommand_t->seqno);
459
460 ret = saa7164_cmd_dequeue(dev);
461 if (ret != SAA_OK) {
462 printk(KERN_ERR "dequeue failed, ret = %d\n",
463 ret);
464 if (safety++ > 16) {
465 printk(KERN_ERR
466 "dequeue exceeded, safety exit\n");
467 return SAA_ERR_BUSY;
468 }
469 }
470
471 continue;
472 }
473
474 if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
475
476 memset(&errdata[0], 0, sizeof(errdata));
477
478 ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
479 if (ret != SAA_OK) {
480 printk(KERN_ERR "get error(2)\n");
481 return ret;
482 }
483
484 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
485
486 dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
487 __func__, errdata[0], errdata[1], errdata[2],
488 errdata[3]);
489
490 /* Map error codes */
491 dprintk(DBGLVL_CMD, "%s() cmd, error code = 0x%x\n",
492 __func__, errdata[0]);
493
494 switch (errdata[0]) {
495 case PVC_ERRORCODE_INVALID_COMMAND:
496 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
497 __func__);
498 ret = SAA_ERR_INVALID_COMMAND;
499 break;
500 case PVC_ERRORCODE_INVALID_DATA:
501 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
502 __func__);
503 ret = SAA_ERR_BAD_PARAMETER;
504 break;
505 case PVC_ERRORCODE_TIMEOUT:
506 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
507 ret = SAA_ERR_TIMEOUT;
508 break;
509 case PVC_ERRORCODE_NAK:
510 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
511 ret = SAA_ERR_NULL_PACKET;
512 break;
513 case PVC_ERRORCODE_UNKNOWN:
514 case PVC_ERRORCODE_INVALID_CONTROL:
515 dprintk(DBGLVL_CMD,
516 "%s() UNKNOWN OR INVALID CONTROL\n",
517 __func__);
518 default:
519 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
520 ret = SAA_ERR_NOT_SUPPORTED;
521 }
522
523 /* See of other commands are on the bus */
524 if (saa7164_cmd_dequeue(dev) != SAA_OK)
525 printk(KERN_ERR "dequeue(2) failed\n");
526
527 return ret;
528 }
529
530 /* If response is invalid */
531 if ((presponse_t->id != pcommand_t->id) ||
532 (presponse_t->command != pcommand_t->command) ||
533 (presponse_t->controlselector !=
534 pcommand_t->controlselector) ||
535 (((resp_dsize - data_recd) != presponse_t->size) &&
536 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
537 ((resp_dsize - data_recd) < presponse_t->size)) {
538
539 /* Invalid */
540 dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
541 ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
542 if (ret != SAA_OK) {
543 printk(KERN_ERR "get failed\n");
544 return ret;
545 }
546
547 /* See of other commands are on the bus */
548 if (saa7164_cmd_dequeue(dev) != SAA_OK)
549 printk(KERN_ERR "dequeue(3) failed\n");
550 continue;
551 }
552
553 /* OK, now we're actually getting out correct response */
554 ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
555 if (ret != SAA_OK) {
556 printk(KERN_ERR "get failed\n");
557 return ret;
558 }
559
560 data_recd = presponse_t->size + data_recd;
561 if (resp_dsize == data_recd) {
562 dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
563 break;
564 }
565
566 /* See of other commands are on the bus */
567 if (saa7164_cmd_dequeue(dev) != SAA_OK)
568 printk(KERN_ERR "dequeue(3) failed\n");
569
570 continue;
571
572 } /* (loop) */
573
574 /* Release the sequence number allocation */
575 saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
576
577 /* if powerdown signal all pending commands */
578
579 dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
580
581 /* See of other commands are on the bus */
582 if (saa7164_cmd_dequeue(dev) != SAA_OK)
583 printk(KERN_ERR "dequeue(4) failed\n");
584
585 ret = SAA_OK;
586out:
587 return ret;
588}
589
diff --git a/drivers/media/pci/saa7164/saa7164-core.c b/drivers/media/pci/saa7164/saa7164-core.c
new file mode 100644
index 000000000000..2c9ad878bef3
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-core.c
@@ -0,0 +1,1488 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/init.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kmod.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30#include <linux/delay.h>
31#include <asm/div64.h>
32
33#ifdef CONFIG_PROC_FS
34#include <linux/proc_fs.h>
35#endif
36#include "saa7164.h"
37
38MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
39MODULE_AUTHOR("Steven Toth <stoth@kernellabs.com>");
40MODULE_LICENSE("GPL");
41
42/*
43 * 1 Basic
44 * 2
45 * 4 i2c
46 * 8 api
47 * 16 cmd
48 * 32 bus
49 */
50
51unsigned int saa_debug;
52module_param_named(debug, saa_debug, int, 0644);
53MODULE_PARM_DESC(debug, "enable debug messages");
54
55unsigned int fw_debug;
56module_param(fw_debug, int, 0644);
57MODULE_PARM_DESC(fw_debug, "Firware debug level def:2");
58
59unsigned int encoder_buffers = SAA7164_MAX_ENCODER_BUFFERS;
60module_param(encoder_buffers, int, 0644);
61MODULE_PARM_DESC(encoder_buffers, "Total buffers in read queue 16-512 def:64");
62
63unsigned int vbi_buffers = SAA7164_MAX_VBI_BUFFERS;
64module_param(vbi_buffers, int, 0644);
65MODULE_PARM_DESC(vbi_buffers, "Total buffers in read queue 16-512 def:64");
66
67unsigned int waitsecs = 10;
68module_param(waitsecs, int, 0644);
69MODULE_PARM_DESC(waitsecs, "timeout on firmware messages");
70
71static unsigned int card[] = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET };
72module_param_array(card, int, NULL, 0444);
73MODULE_PARM_DESC(card, "card type");
74
75unsigned int print_histogram = 64;
76module_param(print_histogram, int, 0644);
77MODULE_PARM_DESC(print_histogram, "print histogram values once");
78
79unsigned int crc_checking = 1;
80module_param(crc_checking, int, 0644);
81MODULE_PARM_DESC(crc_checking, "enable crc sanity checking on buffers");
82
83unsigned int guard_checking = 1;
84module_param(guard_checking, int, 0644);
85MODULE_PARM_DESC(guard_checking,
86 "enable dma sanity checking for buffer overruns");
87
88static unsigned int saa7164_devcount;
89
90static DEFINE_MUTEX(devlist);
91LIST_HEAD(saa7164_devlist);
92
93#define INT_SIZE 16
94
95static void saa7164_pack_verifier(struct saa7164_buffer *buf)
96{
97 u8 *p = (u8 *)buf->cpu;
98 int i;
99
100 for (i = 0; i < buf->actual_size; i += 2048) {
101
102 if ((*(p + i + 0) != 0x00) || (*(p + i + 1) != 0x00) ||
103 (*(p + i + 2) != 0x01) || (*(p + i + 3) != 0xBA)) {
104 printk(KERN_ERR "No pack at 0x%x\n", i);
105#if 0
106 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
107 p + 1, 32, false);
108#endif
109 }
110 }
111}
112
113#define FIXED_VIDEO_PID 0xf1
114#define FIXED_AUDIO_PID 0xf2
115
116static void saa7164_ts_verifier(struct saa7164_buffer *buf)
117{
118 struct saa7164_port *port = buf->port;
119 u32 i;
120 u8 cc, a;
121 u16 pid;
122 u8 __iomem *bufcpu = (u8 *)buf->cpu;
123
124 port->sync_errors = 0;
125 port->v_cc_errors = 0;
126 port->a_cc_errors = 0;
127
128 for (i = 0; i < buf->actual_size; i += 188) {
129 if (*(bufcpu + i) != 0x47)
130 port->sync_errors++;
131
132 /* TODO: Query pid lower 8 bits, ignoring upper bits intensionally */
133 pid = ((*(bufcpu + i + 1) & 0x1f) << 8) | *(bufcpu + i + 2);
134 cc = *(bufcpu + i + 3) & 0x0f;
135
136 if (pid == FIXED_VIDEO_PID) {
137 a = ((port->last_v_cc + 1) & 0x0f);
138 if (a != cc) {
139 printk(KERN_ERR "video cc last = %x current = %x i = %d\n",
140 port->last_v_cc, cc, i);
141 port->v_cc_errors++;
142 }
143
144 port->last_v_cc = cc;
145 } else
146 if (pid == FIXED_AUDIO_PID) {
147 a = ((port->last_a_cc + 1) & 0x0f);
148 if (a != cc) {
149 printk(KERN_ERR "audio cc last = %x current = %x i = %d\n",
150 port->last_a_cc, cc, i);
151 port->a_cc_errors++;
152 }
153
154 port->last_a_cc = cc;
155 }
156
157 }
158
159 /* Only report errors if we've been through this function atleast
160 * once already and the cached cc values are primed. First time through
161 * always generates errors.
162 */
163 if (port->v_cc_errors && (port->done_first_interrupt > 1))
164 printk(KERN_ERR "video pid cc, %d errors\n", port->v_cc_errors);
165
166 if (port->a_cc_errors && (port->done_first_interrupt > 1))
167 printk(KERN_ERR "audio pid cc, %d errors\n", port->a_cc_errors);
168
169 if (port->sync_errors && (port->done_first_interrupt > 1))
170 printk(KERN_ERR "sync_errors = %d\n", port->sync_errors);
171
172 if (port->done_first_interrupt == 1)
173 port->done_first_interrupt++;
174}
175
176static void saa7164_histogram_reset(struct saa7164_histogram *hg, char *name)
177{
178 int i;
179
180 memset(hg, 0, sizeof(struct saa7164_histogram));
181 strcpy(hg->name, name);
182
183 /* First 30ms x 1ms */
184 for (i = 0; i < 30; i++)
185 hg->counter1[0 + i].val = i;
186
187 /* 30 - 200ms x 10ms */
188 for (i = 0; i < 18; i++)
189 hg->counter1[30 + i].val = 30 + (i * 10);
190
191 /* 200 - 2000ms x 100ms */
192 for (i = 0; i < 15; i++)
193 hg->counter1[48 + i].val = 200 + (i * 200);
194
195 /* Catch all massive value (2secs) */
196 hg->counter1[55].val = 2000;
197
198 /* Catch all massive value (4secs) */
199 hg->counter1[56].val = 4000;
200
201 /* Catch all massive value (8secs) */
202 hg->counter1[57].val = 8000;
203
204 /* Catch all massive value (15secs) */
205 hg->counter1[58].val = 15000;
206
207 /* Catch all massive value (30secs) */
208 hg->counter1[59].val = 30000;
209
210 /* Catch all massive value (60secs) */
211 hg->counter1[60].val = 60000;
212
213 /* Catch all massive value (5mins) */
214 hg->counter1[61].val = 300000;
215
216 /* Catch all massive value (15mins) */
217 hg->counter1[62].val = 900000;
218
219 /* Catch all massive values (1hr) */
220 hg->counter1[63].val = 3600000;
221}
222
223void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val)
224{
225 int i;
226 for (i = 0; i < 64; i++) {
227 if (val <= hg->counter1[i].val) {
228 hg->counter1[i].count++;
229 hg->counter1[i].update_time = jiffies;
230 break;
231 }
232 }
233}
234
235static void saa7164_histogram_print(struct saa7164_port *port,
236 struct saa7164_histogram *hg)
237{
238 u32 entries = 0;
239 int i;
240
241 printk(KERN_ERR "Histogram named %s (ms, count, last_update_jiffy)\n", hg->name);
242 for (i = 0; i < 64; i++) {
243 if (hg->counter1[i].count == 0)
244 continue;
245
246 printk(KERN_ERR " %4d %12d %Ld\n",
247 hg->counter1[i].val,
248 hg->counter1[i].count,
249 hg->counter1[i].update_time);
250
251 entries++;
252 }
253 printk(KERN_ERR "Total: %d\n", entries);
254}
255
256static void saa7164_work_enchandler_helper(struct saa7164_port *port, int bufnr)
257{
258 struct saa7164_dev *dev = port->dev;
259 struct saa7164_buffer *buf = NULL;
260 struct saa7164_user_buffer *ubuf = NULL;
261 struct list_head *c, *n;
262 int i = 0;
263 u8 __iomem *p;
264
265 mutex_lock(&port->dmaqueue_lock);
266 list_for_each_safe(c, n, &port->dmaqueue.list) {
267
268 buf = list_entry(c, struct saa7164_buffer, list);
269 if (i++ > port->hwcfg.buffercount) {
270 printk(KERN_ERR "%s() illegal i count %d\n",
271 __func__, i);
272 break;
273 }
274
275 if (buf->idx == bufnr) {
276
277 /* Found the buffer, deal with it */
278 dprintk(DBGLVL_IRQ, "%s() bufnr: %d\n", __func__, bufnr);
279
280 if (crc_checking) {
281 /* Throw a new checksum on the dma buffer */
282 buf->crc = crc32(0, buf->cpu, buf->actual_size);
283 }
284
285 if (guard_checking) {
286 p = (u8 *)buf->cpu;
287 if ((*(p + buf->actual_size + 0) != 0xff) ||
288 (*(p + buf->actual_size + 1) != 0xff) ||
289 (*(p + buf->actual_size + 2) != 0xff) ||
290 (*(p + buf->actual_size + 3) != 0xff) ||
291 (*(p + buf->actual_size + 0x10) != 0xff) ||
292 (*(p + buf->actual_size + 0x11) != 0xff) ||
293 (*(p + buf->actual_size + 0x12) != 0xff) ||
294 (*(p + buf->actual_size + 0x13) != 0xff)) {
295 printk(KERN_ERR "%s() buf %p guard buffer breach\n",
296 __func__, buf);
297#if 0
298 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
299 p + buf->actual_size - 32, 64, false);
300#endif
301 }
302 }
303
304 if ((port->nr != SAA7164_PORT_VBI1) && (port->nr != SAA7164_PORT_VBI2)) {
305 /* Validate the incoming buffer content */
306 if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
307 saa7164_ts_verifier(buf);
308 else if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS)
309 saa7164_pack_verifier(buf);
310 }
311
312 /* find a free user buffer and clone to it */
313 if (!list_empty(&port->list_buf_free.list)) {
314
315 /* Pull the first buffer from the used list */
316 ubuf = list_first_entry(&port->list_buf_free.list,
317 struct saa7164_user_buffer, list);
318
319 if (buf->actual_size <= ubuf->actual_size) {
320
321 memcpy_fromio(ubuf->data, buf->cpu,
322 ubuf->actual_size);
323
324 if (crc_checking) {
325 /* Throw a new checksum on the read buffer */
326 ubuf->crc = crc32(0, ubuf->data, ubuf->actual_size);
327 }
328
329 /* Requeue the buffer on the free list */
330 ubuf->pos = 0;
331
332 list_move_tail(&ubuf->list,
333 &port->list_buf_used.list);
334
335 /* Flag any userland waiters */
336 wake_up_interruptible(&port->wait_read);
337
338 } else {
339 printk(KERN_ERR "buf %p bufsize fails match\n", buf);
340 }
341
342 } else
343 printk(KERN_ERR "encirq no free buffers, increase param encoder_buffers\n");
344
345 /* Ensure offset into buffer remains 0, fill buffer
346 * with known bad data. We check for this data at a later point
347 * in time. */
348 saa7164_buffer_zero_offsets(port, bufnr);
349 memset_io(buf->cpu, 0xff, buf->pci_size);
350 if (crc_checking) {
351 /* Throw yet aanother new checksum on the dma buffer */
352 buf->crc = crc32(0, buf->cpu, buf->actual_size);
353 }
354
355 break;
356 }
357 }
358 mutex_unlock(&port->dmaqueue_lock);
359}
360
361static void saa7164_work_enchandler(struct work_struct *w)
362{
363 struct saa7164_port *port =
364 container_of(w, struct saa7164_port, workenc);
365 struct saa7164_dev *dev = port->dev;
366
367 u32 wp, mcb, rp, cnt = 0;
368
369 port->last_svc_msecs_diff = port->last_svc_msecs;
370 port->last_svc_msecs = jiffies_to_msecs(jiffies);
371
372 port->last_svc_msecs_diff = port->last_svc_msecs -
373 port->last_svc_msecs_diff;
374
375 saa7164_histogram_update(&port->svc_interval,
376 port->last_svc_msecs_diff);
377
378 port->last_irq_svc_msecs_diff = port->last_svc_msecs -
379 port->last_irq_msecs;
380
381 saa7164_histogram_update(&port->irq_svc_interval,
382 port->last_irq_svc_msecs_diff);
383
384 dprintk(DBGLVL_IRQ,
385 "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
386 __func__,
387 port->last_svc_msecs_diff,
388 port->last_irq_svc_msecs_diff,
389 port->last_svc_wp,
390 port->last_svc_rp
391 );
392
393 /* Current write position */
394 wp = saa7164_readl(port->bufcounter);
395 if (wp > (port->hwcfg.buffercount - 1)) {
396 printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp);
397 return;
398 }
399
400 /* Most current complete buffer */
401 if (wp == 0)
402 mcb = (port->hwcfg.buffercount - 1);
403 else
404 mcb = wp - 1;
405
406 while (1) {
407 if (port->done_first_interrupt == 0) {
408 port->done_first_interrupt++;
409 rp = mcb;
410 } else
411 rp = (port->last_svc_rp + 1) % 8;
412
413 if ((rp < 0) || (rp > (port->hwcfg.buffercount - 1))) {
414 printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
415 break;
416 }
417
418 saa7164_work_enchandler_helper(port, rp);
419 port->last_svc_rp = rp;
420 cnt++;
421
422 if (rp == mcb)
423 break;
424 }
425
426 /* TODO: Convert this into a /proc/saa7164 style readable file */
427 if (print_histogram == port->nr) {
428 saa7164_histogram_print(port, &port->irq_interval);
429 saa7164_histogram_print(port, &port->svc_interval);
430 saa7164_histogram_print(port, &port->irq_svc_interval);
431 saa7164_histogram_print(port, &port->read_interval);
432 saa7164_histogram_print(port, &port->poll_interval);
433 /* TODO: fix this to preserve any previous state */
434 print_histogram = 64 + port->nr;
435 }
436}
437
438static void saa7164_work_vbihandler(struct work_struct *w)
439{
440 struct saa7164_port *port =
441 container_of(w, struct saa7164_port, workenc);
442 struct saa7164_dev *dev = port->dev;
443
444 u32 wp, mcb, rp, cnt = 0;
445
446 port->last_svc_msecs_diff = port->last_svc_msecs;
447 port->last_svc_msecs = jiffies_to_msecs(jiffies);
448 port->last_svc_msecs_diff = port->last_svc_msecs -
449 port->last_svc_msecs_diff;
450
451 saa7164_histogram_update(&port->svc_interval,
452 port->last_svc_msecs_diff);
453
454 port->last_irq_svc_msecs_diff = port->last_svc_msecs -
455 port->last_irq_msecs;
456
457 saa7164_histogram_update(&port->irq_svc_interval,
458 port->last_irq_svc_msecs_diff);
459
460 dprintk(DBGLVL_IRQ,
461 "%s() %Ldms elapsed irq->deferred %Ldms wp: %d rp: %d\n",
462 __func__,
463 port->last_svc_msecs_diff,
464 port->last_irq_svc_msecs_diff,
465 port->last_svc_wp,
466 port->last_svc_rp
467 );
468
469 /* Current write position */
470 wp = saa7164_readl(port->bufcounter);
471 if (wp > (port->hwcfg.buffercount - 1)) {
472 printk(KERN_ERR "%s() illegal buf count %d\n", __func__, wp);
473 return;
474 }
475
476 /* Most current complete buffer */
477 if (wp == 0)
478 mcb = (port->hwcfg.buffercount - 1);
479 else
480 mcb = wp - 1;
481
482 while (1) {
483 if (port->done_first_interrupt == 0) {
484 port->done_first_interrupt++;
485 rp = mcb;
486 } else
487 rp = (port->last_svc_rp + 1) % 8;
488
489 if ((rp < 0) || (rp > (port->hwcfg.buffercount - 1))) {
490 printk(KERN_ERR "%s() illegal rp count %d\n", __func__, rp);
491 break;
492 }
493
494 saa7164_work_enchandler_helper(port, rp);
495 port->last_svc_rp = rp;
496 cnt++;
497
498 if (rp == mcb)
499 break;
500 }
501
502 /* TODO: Convert this into a /proc/saa7164 style readable file */
503 if (print_histogram == port->nr) {
504 saa7164_histogram_print(port, &port->irq_interval);
505 saa7164_histogram_print(port, &port->svc_interval);
506 saa7164_histogram_print(port, &port->irq_svc_interval);
507 saa7164_histogram_print(port, &port->read_interval);
508 saa7164_histogram_print(port, &port->poll_interval);
509 /* TODO: fix this to preserve any previous state */
510 print_histogram = 64 + port->nr;
511 }
512}
513
514static void saa7164_work_cmdhandler(struct work_struct *w)
515{
516 struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
517
518 /* Wake up any complete commands */
519 saa7164_irq_dequeue(dev);
520}
521
522static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
523{
524 struct saa7164_port *port = buf->port;
525
526 /* Feed the transport payload into the kernel demux */
527 dvb_dmx_swfilter_packets(&port->dvb.demux, (u8 *)buf->cpu,
528 SAA7164_TS_NUMBER_OF_LINES);
529
530}
531
532static irqreturn_t saa7164_irq_vbi(struct saa7164_port *port)
533{
534 struct saa7164_dev *dev = port->dev;
535
536 /* Store old time */
537 port->last_irq_msecs_diff = port->last_irq_msecs;
538
539 /* Collect new stats */
540 port->last_irq_msecs = jiffies_to_msecs(jiffies);
541
542 /* Calculate stats */
543 port->last_irq_msecs_diff = port->last_irq_msecs -
544 port->last_irq_msecs_diff;
545
546 saa7164_histogram_update(&port->irq_interval,
547 port->last_irq_msecs_diff);
548
549 dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed\n", __func__,
550 port->last_irq_msecs_diff);
551
552 /* Tis calls the vbi irq handler */
553 schedule_work(&port->workenc);
554 return 0;
555}
556
557static irqreturn_t saa7164_irq_encoder(struct saa7164_port *port)
558{
559 struct saa7164_dev *dev = port->dev;
560
561 /* Store old time */
562 port->last_irq_msecs_diff = port->last_irq_msecs;
563
564 /* Collect new stats */
565 port->last_irq_msecs = jiffies_to_msecs(jiffies);
566
567 /* Calculate stats */
568 port->last_irq_msecs_diff = port->last_irq_msecs -
569 port->last_irq_msecs_diff;
570
571 saa7164_histogram_update(&port->irq_interval,
572 port->last_irq_msecs_diff);
573
574 dprintk(DBGLVL_IRQ, "%s() %Ldms elapsed\n", __func__,
575 port->last_irq_msecs_diff);
576
577 schedule_work(&port->workenc);
578 return 0;
579}
580
581static irqreturn_t saa7164_irq_ts(struct saa7164_port *port)
582{
583 struct saa7164_dev *dev = port->dev;
584 struct saa7164_buffer *buf;
585 struct list_head *c, *n;
586 int wp, i = 0, rp;
587
588 /* Find the current write point from the hardware */
589 wp = saa7164_readl(port->bufcounter);
590 if (wp > (port->hwcfg.buffercount - 1))
591 BUG();
592
593 /* Find the previous buffer to the current write point */
594 if (wp == 0)
595 rp = (port->hwcfg.buffercount - 1);
596 else
597 rp = wp - 1;
598
599 /* Lookup the WP in the buffer list */
600 /* TODO: turn this into a worker thread */
601 list_for_each_safe(c, n, &port->dmaqueue.list) {
602 buf = list_entry(c, struct saa7164_buffer, list);
603 if (i++ > port->hwcfg.buffercount)
604 BUG();
605
606 if (buf->idx == rp) {
607 /* Found the buffer, deal with it */
608 dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
609 __func__, wp, rp);
610 saa7164_buffer_deliver(buf);
611 break;
612 }
613
614 }
615 return 0;
616}
617
618/* Primary IRQ handler and dispatch mechanism */
619static irqreturn_t saa7164_irq(int irq, void *dev_id)
620{
621 struct saa7164_dev *dev = dev_id;
622 struct saa7164_port *porta = &dev->ports[SAA7164_PORT_TS1];
623 struct saa7164_port *portb = &dev->ports[SAA7164_PORT_TS2];
624 struct saa7164_port *portc = &dev->ports[SAA7164_PORT_ENC1];
625 struct saa7164_port *portd = &dev->ports[SAA7164_PORT_ENC2];
626 struct saa7164_port *porte = &dev->ports[SAA7164_PORT_VBI1];
627 struct saa7164_port *portf = &dev->ports[SAA7164_PORT_VBI2];
628
629 u32 intid, intstat[INT_SIZE/4];
630 int i, handled = 0, bit;
631
632 if (dev == NULL) {
633 printk(KERN_ERR "%s() No device specified\n", __func__);
634 handled = 0;
635 goto out;
636 }
637
638 /* Check that the hardware is accessible. If the status bytes are
639 * 0xFF then the device is not accessible, the the IRQ belongs
640 * to another driver.
641 * 4 x u32 interrupt registers.
642 */
643 for (i = 0; i < INT_SIZE/4; i++) {
644
645 /* TODO: Convert into saa7164_readl() */
646 /* Read the 4 hardware interrupt registers */
647 intstat[i] = saa7164_readl(dev->int_status + (i * 4));
648
649 if (intstat[i])
650 handled = 1;
651 }
652 if (handled == 0)
653 goto out;
654
655 /* For each of the HW interrupt registers */
656 for (i = 0; i < INT_SIZE/4; i++) {
657
658 if (intstat[i]) {
659 /* Each function of the board has it's own interruptid.
660 * Find the function that triggered then call
661 * it's handler.
662 */
663 for (bit = 0; bit < 32; bit++) {
664
665 if (((intstat[i] >> bit) & 0x00000001) == 0)
666 continue;
667
668 /* Calculate the interrupt id (0x00 to 0x7f) */
669
670 intid = (i * 32) + bit;
671 if (intid == dev->intfdesc.bInterruptId) {
672 /* A response to an cmd/api call */
673 schedule_work(&dev->workcmd);
674 } else if (intid == porta->hwcfg.interruptid) {
675
676 /* Transport path 1 */
677 saa7164_irq_ts(porta);
678
679 } else if (intid == portb->hwcfg.interruptid) {
680
681 /* Transport path 2 */
682 saa7164_irq_ts(portb);
683
684 } else if (intid == portc->hwcfg.interruptid) {
685
686 /* Encoder path 1 */
687 saa7164_irq_encoder(portc);
688
689 } else if (intid == portd->hwcfg.interruptid) {
690
691 /* Encoder path 2 */
692 saa7164_irq_encoder(portd);
693
694 } else if (intid == porte->hwcfg.interruptid) {
695
696 /* VBI path 1 */
697 saa7164_irq_vbi(porte);
698
699 } else if (intid == portf->hwcfg.interruptid) {
700
701 /* VBI path 2 */
702 saa7164_irq_vbi(portf);
703
704 } else {
705 /* Find the function */
706 dprintk(DBGLVL_IRQ,
707 "%s() unhandled interrupt "
708 "reg 0x%x bit 0x%x "
709 "intid = 0x%x\n",
710 __func__, i, bit, intid);
711 }
712 }
713
714 /* Ack it */
715 saa7164_writel(dev->int_ack + (i * 4), intstat[i]);
716
717 }
718 }
719out:
720 return IRQ_RETVAL(handled);
721}
722
723void saa7164_getfirmwarestatus(struct saa7164_dev *dev)
724{
725 struct saa7164_fw_status *s = &dev->fw_status;
726
727 dev->fw_status.status = saa7164_readl(SAA_DEVICE_SYSINIT_STATUS);
728 dev->fw_status.mode = saa7164_readl(SAA_DEVICE_SYSINIT_MODE);
729 dev->fw_status.spec = saa7164_readl(SAA_DEVICE_SYSINIT_SPEC);
730 dev->fw_status.inst = saa7164_readl(SAA_DEVICE_SYSINIT_INST);
731 dev->fw_status.cpuload = saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD);
732 dev->fw_status.remainheap =
733 saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP);
734
735 dprintk(1, "Firmware status:\n");
736 dprintk(1, " .status = 0x%08x\n", s->status);
737 dprintk(1, " .mode = 0x%08x\n", s->mode);
738 dprintk(1, " .spec = 0x%08x\n", s->spec);
739 dprintk(1, " .inst = 0x%08x\n", s->inst);
740 dprintk(1, " .cpuload = 0x%08x\n", s->cpuload);
741 dprintk(1, " .remainheap = 0x%08x\n", s->remainheap);
742}
743
744u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev)
745{
746 u32 reg;
747
748 reg = saa7164_readl(SAA_DEVICE_VERSION);
749 dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
750 (reg & 0x0000fc00) >> 10,
751 (reg & 0x000003e0) >> 5,
752 (reg & 0x0000001f),
753 (reg & 0xffff0000) >> 16,
754 reg);
755
756 return reg;
757}
758
759/* TODO: Debugging func, remove */
760void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
761{
762 int i;
763
764 dprintk(1, "--------------------> "
765 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
766
767 for (i = 0; i < 0x100; i += 16)
768 dprintk(1, "region0[0x%08x] = "
769 "%02x %02x %02x %02x %02x %02x %02x %02x"
770 " %02x %02x %02x %02x %02x %02x %02x %02x\n", i,
771 (u8)saa7164_readb(addr + i + 0),
772 (u8)saa7164_readb(addr + i + 1),
773 (u8)saa7164_readb(addr + i + 2),
774 (u8)saa7164_readb(addr + i + 3),
775 (u8)saa7164_readb(addr + i + 4),
776 (u8)saa7164_readb(addr + i + 5),
777 (u8)saa7164_readb(addr + i + 6),
778 (u8)saa7164_readb(addr + i + 7),
779 (u8)saa7164_readb(addr + i + 8),
780 (u8)saa7164_readb(addr + i + 9),
781 (u8)saa7164_readb(addr + i + 10),
782 (u8)saa7164_readb(addr + i + 11),
783 (u8)saa7164_readb(addr + i + 12),
784 (u8)saa7164_readb(addr + i + 13),
785 (u8)saa7164_readb(addr + i + 14),
786 (u8)saa7164_readb(addr + i + 15)
787 );
788}
789
790static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
791{
792 dprintk(1, "@0x%p hwdesc sizeof(struct tmComResHWDescr) = %d bytes\n",
793 &dev->hwdesc, (u32)sizeof(struct tmComResHWDescr));
794
795 dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength);
796 dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType);
797 dprintk(1, " .bDescriptorSubtype = 0x%x\n",
798 dev->hwdesc.bDescriptorSubtype);
799
800 dprintk(1, " .bcdSpecVersion = 0x%x\n", dev->hwdesc.bcdSpecVersion);
801 dprintk(1, " .dwClockFrequency = 0x%x\n", dev->hwdesc.dwClockFrequency);
802 dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev->hwdesc.dwClockUpdateRes);
803 dprintk(1, " .bCapabilities = 0x%x\n", dev->hwdesc.bCapabilities);
804 dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
805 dev->hwdesc.dwDeviceRegistersLocation);
806
807 dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
808 dev->hwdesc.dwHostMemoryRegion);
809
810 dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
811 dev->hwdesc.dwHostMemoryRegionSize);
812
813 dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
814 dev->hwdesc.dwHostHibernatMemRegion);
815
816 dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
817 dev->hwdesc.dwHostHibernatMemRegionSize);
818}
819
820static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
821{
822 dprintk(1, "@0x%p intfdesc "
823 "sizeof(struct tmComResInterfaceDescr) = %d bytes\n",
824 &dev->intfdesc, (u32)sizeof(struct tmComResInterfaceDescr));
825
826 dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength);
827 dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType);
828 dprintk(1, " .bDescriptorSubtype = 0x%x\n",
829 dev->intfdesc.bDescriptorSubtype);
830
831 dprintk(1, " .bFlags = 0x%x\n", dev->intfdesc.bFlags);
832 dprintk(1, " .bInterfaceType = 0x%x\n", dev->intfdesc.bInterfaceType);
833 dprintk(1, " .bInterfaceId = 0x%x\n", dev->intfdesc.bInterfaceId);
834 dprintk(1, " .bBaseInterface = 0x%x\n", dev->intfdesc.bBaseInterface);
835 dprintk(1, " .bInterruptId = 0x%x\n", dev->intfdesc.bInterruptId);
836 dprintk(1, " .bDebugInterruptId = 0x%x\n",
837 dev->intfdesc.bDebugInterruptId);
838
839 dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation);
840}
841
842static void saa7164_dump_busdesc(struct saa7164_dev *dev)
843{
844 dprintk(1, "@0x%p busdesc sizeof(struct tmComResBusDescr) = %d bytes\n",
845 &dev->busdesc, (u32)sizeof(struct tmComResBusDescr));
846
847 dprintk(1, " .CommandRing = 0x%016Lx\n", dev->busdesc.CommandRing);
848 dprintk(1, " .ResponseRing = 0x%016Lx\n", dev->busdesc.ResponseRing);
849 dprintk(1, " .CommandWrite = 0x%x\n", dev->busdesc.CommandWrite);
850 dprintk(1, " .CommandRead = 0x%x\n", dev->busdesc.CommandRead);
851 dprintk(1, " .ResponseWrite = 0x%x\n", dev->busdesc.ResponseWrite);
852 dprintk(1, " .ResponseRead = 0x%x\n", dev->busdesc.ResponseRead);
853}
854
855/* Much of the hardware configuration and PCI registers are configured
856 * dynamically depending on firmware. We have to cache some initial
857 * structures then use these to locate other important structures
858 * from PCI space.
859 */
860static void saa7164_get_descriptors(struct saa7164_dev *dev)
861{
862 memcpy_fromio(&dev->hwdesc, dev->bmmio, sizeof(struct tmComResHWDescr));
863 memcpy_fromio(&dev->intfdesc, dev->bmmio + sizeof(struct tmComResHWDescr),
864 sizeof(struct tmComResInterfaceDescr));
865 memcpy_fromio(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
866 sizeof(struct tmComResBusDescr));
867
868 if (dev->hwdesc.bLength != sizeof(struct tmComResHWDescr)) {
869 printk(KERN_ERR "Structure struct tmComResHWDescr is mangled\n");
870 printk(KERN_ERR "Need %x got %d\n", dev->hwdesc.bLength,
871 (u32)sizeof(struct tmComResHWDescr));
872 } else
873 saa7164_dump_hwdesc(dev);
874
875 if (dev->intfdesc.bLength != sizeof(struct tmComResInterfaceDescr)) {
876 printk(KERN_ERR "struct struct tmComResInterfaceDescr is mangled\n");
877 printk(KERN_ERR "Need %x got %d\n", dev->intfdesc.bLength,
878 (u32)sizeof(struct tmComResInterfaceDescr));
879 } else
880 saa7164_dump_intfdesc(dev);
881
882 saa7164_dump_busdesc(dev);
883}
884
885static int saa7164_pci_quirks(struct saa7164_dev *dev)
886{
887 return 0;
888}
889
890static int get_resources(struct saa7164_dev *dev)
891{
892 if (request_mem_region(pci_resource_start(dev->pci, 0),
893 pci_resource_len(dev->pci, 0), dev->name)) {
894
895 if (request_mem_region(pci_resource_start(dev->pci, 2),
896 pci_resource_len(dev->pci, 2), dev->name))
897 return 0;
898 }
899
900 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
901 dev->name,
902 (u64)pci_resource_start(dev->pci, 0),
903 (u64)pci_resource_start(dev->pci, 2));
904
905 return -EBUSY;
906}
907
908static int saa7164_port_init(struct saa7164_dev *dev, int portnr)
909{
910 struct saa7164_port *port = NULL;
911
912 if ((portnr < 0) || (portnr >= SAA7164_MAX_PORTS))
913 BUG();
914
915 port = &dev->ports[portnr];
916
917 port->dev = dev;
918 port->nr = portnr;
919
920 if ((portnr == SAA7164_PORT_TS1) || (portnr == SAA7164_PORT_TS2))
921 port->type = SAA7164_MPEG_DVB;
922 else
923 if ((portnr == SAA7164_PORT_ENC1) || (portnr == SAA7164_PORT_ENC2)) {
924 port->type = SAA7164_MPEG_ENCODER;
925
926 /* We need a deferred interrupt handler for cmd handling */
927 INIT_WORK(&port->workenc, saa7164_work_enchandler);
928 } else if ((portnr == SAA7164_PORT_VBI1) || (portnr == SAA7164_PORT_VBI2)) {
929 port->type = SAA7164_MPEG_VBI;
930
931 /* We need a deferred interrupt handler for cmd handling */
932 INIT_WORK(&port->workenc, saa7164_work_vbihandler);
933 } else
934 BUG();
935
936 /* Init all the critical resources */
937 mutex_init(&port->dvb.lock);
938 INIT_LIST_HEAD(&port->dmaqueue.list);
939 mutex_init(&port->dmaqueue_lock);
940
941 INIT_LIST_HEAD(&port->list_buf_used.list);
942 INIT_LIST_HEAD(&port->list_buf_free.list);
943 init_waitqueue_head(&port->wait_read);
944
945
946 saa7164_histogram_reset(&port->irq_interval, "irq intervals");
947 saa7164_histogram_reset(&port->svc_interval, "deferred intervals");
948 saa7164_histogram_reset(&port->irq_svc_interval,
949 "irq to deferred intervals");
950 saa7164_histogram_reset(&port->read_interval,
951 "encoder/vbi read() intervals");
952 saa7164_histogram_reset(&port->poll_interval,
953 "encoder/vbi poll() intervals");
954
955 return 0;
956}
957
958static int saa7164_dev_setup(struct saa7164_dev *dev)
959{
960 int i;
961
962 mutex_init(&dev->lock);
963 atomic_inc(&dev->refcount);
964 dev->nr = saa7164_devcount++;
965
966 snprintf(dev->name, sizeof(dev->name), "saa7164[%d]", dev->nr);
967
968 mutex_lock(&devlist);
969 list_add_tail(&dev->devlist, &saa7164_devlist);
970 mutex_unlock(&devlist);
971
972 /* board config */
973 dev->board = UNSET;
974 if (card[dev->nr] < saa7164_bcount)
975 dev->board = card[dev->nr];
976
977 for (i = 0; UNSET == dev->board && i < saa7164_idcount; i++)
978 if (dev->pci->subsystem_vendor == saa7164_subids[i].subvendor &&
979 dev->pci->subsystem_device ==
980 saa7164_subids[i].subdevice)
981 dev->board = saa7164_subids[i].card;
982
983 if (UNSET == dev->board) {
984 dev->board = SAA7164_BOARD_UNKNOWN;
985 saa7164_card_list(dev);
986 }
987
988 dev->pci_bus = dev->pci->bus->number;
989 dev->pci_slot = PCI_SLOT(dev->pci->devfn);
990
991 /* I2C Defaults / setup */
992 dev->i2c_bus[0].dev = dev;
993 dev->i2c_bus[0].nr = 0;
994 dev->i2c_bus[1].dev = dev;
995 dev->i2c_bus[1].nr = 1;
996 dev->i2c_bus[2].dev = dev;
997 dev->i2c_bus[2].nr = 2;
998
999 /* Transport + Encoder ports 1, 2, 3, 4 - Defaults / setup */
1000 saa7164_port_init(dev, SAA7164_PORT_TS1);
1001 saa7164_port_init(dev, SAA7164_PORT_TS2);
1002 saa7164_port_init(dev, SAA7164_PORT_ENC1);
1003 saa7164_port_init(dev, SAA7164_PORT_ENC2);
1004 saa7164_port_init(dev, SAA7164_PORT_VBI1);
1005 saa7164_port_init(dev, SAA7164_PORT_VBI2);
1006
1007 if (get_resources(dev) < 0) {
1008 printk(KERN_ERR "CORE %s No more PCIe resources for "
1009 "subsystem: %04x:%04x\n",
1010 dev->name, dev->pci->subsystem_vendor,
1011 dev->pci->subsystem_device);
1012
1013 saa7164_devcount--;
1014 return -ENODEV;
1015 }
1016
1017 /* PCI/e allocations */
1018 dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
1019 pci_resource_len(dev->pci, 0));
1020
1021 dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2),
1022 pci_resource_len(dev->pci, 2));
1023
1024 dev->bmmio = (u8 __iomem *)dev->lmmio;
1025 dev->bmmio2 = (u8 __iomem *)dev->lmmio2;
1026
1027 /* Inerrupt and ack register locations offset of bmmio */
1028 dev->int_status = 0x183000 + 0xf80;
1029 dev->int_ack = 0x183000 + 0xf90;
1030
1031 printk(KERN_INFO
1032 "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
1033 dev->name, dev->pci->subsystem_vendor,
1034 dev->pci->subsystem_device, saa7164_boards[dev->board].name,
1035 dev->board, card[dev->nr] == dev->board ?
1036 "insmod option" : "autodetected");
1037
1038 saa7164_pci_quirks(dev);
1039
1040 return 0;
1041}
1042
1043static void saa7164_dev_unregister(struct saa7164_dev *dev)
1044{
1045 dprintk(1, "%s()\n", __func__);
1046
1047 release_mem_region(pci_resource_start(dev->pci, 0),
1048 pci_resource_len(dev->pci, 0));
1049
1050 release_mem_region(pci_resource_start(dev->pci, 2),
1051 pci_resource_len(dev->pci, 2));
1052
1053 if (!atomic_dec_and_test(&dev->refcount))
1054 return;
1055
1056 iounmap(dev->lmmio);
1057 iounmap(dev->lmmio2);
1058
1059 return;
1060}
1061
1062#ifdef CONFIG_PROC_FS
1063static int saa7164_proc_show(struct seq_file *m, void *v)
1064{
1065 struct saa7164_dev *dev;
1066 struct tmComResBusInfo *b;
1067 struct list_head *list;
1068 int i, c;
1069
1070 if (saa7164_devcount == 0)
1071 return 0;
1072
1073 list_for_each(list, &saa7164_devlist) {
1074 dev = list_entry(list, struct saa7164_dev, devlist);
1075 seq_printf(m, "%s = %p\n", dev->name, dev);
1076
1077 /* Lock the bus from any other access */
1078 b = &dev->bus;
1079 mutex_lock(&b->lock);
1080
1081 seq_printf(m, " .m_pdwSetWritePos = 0x%x (0x%08x)\n",
1082 b->m_dwSetReadPos, saa7164_readl(b->m_dwSetReadPos));
1083
1084 seq_printf(m, " .m_pdwSetReadPos = 0x%x (0x%08x)\n",
1085 b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
1086
1087 seq_printf(m, " .m_pdwGetWritePos = 0x%x (0x%08x)\n",
1088 b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
1089
1090 seq_printf(m, " .m_pdwGetReadPos = 0x%x (0x%08x)\n",
1091 b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
1092 c = 0;
1093 seq_printf(m, "\n Set Ring:\n");
1094 seq_printf(m, "\n addr 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
1095 for (i = 0; i < b->m_dwSizeSetRing; i++) {
1096 if (c == 0)
1097 seq_printf(m, " %04x:", i);
1098
1099 seq_printf(m, " %02x", *(b->m_pdwSetRing + i));
1100
1101 if (++c == 16) {
1102 seq_printf(m, "\n");
1103 c = 0;
1104 }
1105 }
1106
1107 c = 0;
1108 seq_printf(m, "\n Get Ring:\n");
1109 seq_printf(m, "\n addr 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
1110 for (i = 0; i < b->m_dwSizeGetRing; i++) {
1111 if (c == 0)
1112 seq_printf(m, " %04x:", i);
1113
1114 seq_printf(m, " %02x", *(b->m_pdwGetRing + i));
1115
1116 if (++c == 16) {
1117 seq_printf(m, "\n");
1118 c = 0;
1119 }
1120 }
1121
1122 mutex_unlock(&b->lock);
1123
1124 }
1125
1126 return 0;
1127}
1128
1129static int saa7164_proc_open(struct inode *inode, struct file *filp)
1130{
1131 return single_open(filp, saa7164_proc_show, NULL);
1132}
1133
1134static const struct file_operations saa7164_proc_fops = {
1135 .open = saa7164_proc_open,
1136 .read = seq_read,
1137 .llseek = seq_lseek,
1138 .release = single_release,
1139};
1140
1141static int saa7164_proc_create(void)
1142{
1143 struct proc_dir_entry *pe;
1144
1145 pe = proc_create("saa7164", S_IRUGO, NULL, &saa7164_proc_fops);
1146 if (!pe)
1147 return -ENOMEM;
1148
1149 return 0;
1150}
1151#endif
1152
1153static int saa7164_thread_function(void *data)
1154{
1155 struct saa7164_dev *dev = data;
1156 struct tmFwInfoStruct fwinfo;
1157 u64 last_poll_time = 0;
1158
1159 dprintk(DBGLVL_THR, "thread started\n");
1160
1161 set_freezable();
1162
1163 while (1) {
1164 msleep_interruptible(100);
1165 if (kthread_should_stop())
1166 break;
1167 try_to_freeze();
1168
1169 dprintk(DBGLVL_THR, "thread running\n");
1170
1171 /* Dump the firmware debug message to console */
1172 /* Polling this costs us 1-2% of the arm CPU */
1173 /* convert this into a respnde to interrupt 0x7a */
1174 saa7164_api_collect_debug(dev);
1175
1176 /* Monitor CPU load every 1 second */
1177 if ((last_poll_time + 1000 /* ms */) < jiffies_to_msecs(jiffies)) {
1178 saa7164_api_get_load_info(dev, &fwinfo);
1179 last_poll_time = jiffies_to_msecs(jiffies);
1180 }
1181
1182 }
1183
1184 dprintk(DBGLVL_THR, "thread exiting\n");
1185 return 0;
1186}
1187
1188static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
1189 const struct pci_device_id *pci_id)
1190{
1191 struct saa7164_dev *dev;
1192 int err, i;
1193 u32 version;
1194
1195 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1196 if (NULL == dev)
1197 return -ENOMEM;
1198
1199 /* pci init */
1200 dev->pci = pci_dev;
1201 if (pci_enable_device(pci_dev)) {
1202 err = -EIO;
1203 goto fail_free;
1204 }
1205
1206 if (saa7164_dev_setup(dev) < 0) {
1207 err = -EINVAL;
1208 goto fail_free;
1209 }
1210
1211 /* print pci info */
1212 dev->pci_rev = pci_dev->revision;
1213 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1214 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1215 "latency: %d, mmio: 0x%llx\n", dev->name,
1216 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1217 dev->pci_lat,
1218 (unsigned long long)pci_resource_start(pci_dev, 0));
1219
1220 pci_set_master(pci_dev);
1221 /* TODO */
1222 if (!pci_dma_supported(pci_dev, 0xffffffff)) {
1223 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
1224 err = -EIO;
1225 goto fail_irq;
1226 }
1227
1228 err = request_irq(pci_dev->irq, saa7164_irq,
1229 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
1230 if (err < 0) {
1231 printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name,
1232 pci_dev->irq);
1233 err = -EIO;
1234 goto fail_irq;
1235 }
1236
1237 pci_set_drvdata(pci_dev, dev);
1238
1239 /* Init the internal command list */
1240 for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
1241 dev->cmds[i].seqno = i;
1242 dev->cmds[i].inuse = 0;
1243 mutex_init(&dev->cmds[i].lock);
1244 init_waitqueue_head(&dev->cmds[i].wait);
1245 }
1246
1247 /* We need a deferred interrupt handler for cmd handling */
1248 INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler);
1249
1250 /* Only load the firmware if we know the board */
1251 if (dev->board != SAA7164_BOARD_UNKNOWN) {
1252
1253 err = saa7164_downloadfirmware(dev);
1254 if (err < 0) {
1255 printk(KERN_ERR
1256 "Failed to boot firmware, no features "
1257 "registered\n");
1258 goto fail_fw;
1259 }
1260
1261 saa7164_get_descriptors(dev);
1262 saa7164_dumpregs(dev, 0);
1263 saa7164_getcurrentfirmwareversion(dev);
1264 saa7164_getfirmwarestatus(dev);
1265 err = saa7164_bus_setup(dev);
1266 if (err < 0)
1267 printk(KERN_ERR
1268 "Failed to setup the bus, will continue\n");
1269 saa7164_bus_dump(dev);
1270
1271 /* Ping the running firmware via the command bus and get the
1272 * firmware version, this checks the bus is running OK.
1273 */
1274 version = 0;
1275 if (saa7164_api_get_fw_version(dev, &version) == SAA_OK)
1276 dprintk(1, "Bus is operating correctly using "
1277 "version %d.%d.%d.%d (0x%x)\n",
1278 (version & 0x0000fc00) >> 10,
1279 (version & 0x000003e0) >> 5,
1280 (version & 0x0000001f),
1281 (version & 0xffff0000) >> 16,
1282 version);
1283 else
1284 printk(KERN_ERR
1285 "Failed to communicate with the firmware\n");
1286
1287 /* Bring up the I2C buses */
1288 saa7164_i2c_register(&dev->i2c_bus[0]);
1289 saa7164_i2c_register(&dev->i2c_bus[1]);
1290 saa7164_i2c_register(&dev->i2c_bus[2]);
1291 saa7164_gpio_setup(dev);
1292 saa7164_card_setup(dev);
1293
1294 /* Parse the dynamic device configuration, find various
1295 * media endpoints (MPEG, WMV, PS, TS) and cache their
1296 * configuration details into the driver, so we can
1297 * reference them later during simething_register() func,
1298 * interrupt handlers, deferred work handlers etc.
1299 */
1300 saa7164_api_enum_subdevs(dev);
1301
1302 /* Begin to create the video sub-systems and register funcs */
1303 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
1304 if (saa7164_dvb_register(&dev->ports[SAA7164_PORT_TS1]) < 0) {
1305 printk(KERN_ERR "%s() Failed to register "
1306 "dvb adapters on porta\n",
1307 __func__);
1308 }
1309 }
1310
1311 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
1312 if (saa7164_dvb_register(&dev->ports[SAA7164_PORT_TS2]) < 0) {
1313 printk(KERN_ERR"%s() Failed to register "
1314 "dvb adapters on portb\n",
1315 __func__);
1316 }
1317 }
1318
1319 if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER) {
1320 if (saa7164_encoder_register(&dev->ports[SAA7164_PORT_ENC1]) < 0) {
1321 printk(KERN_ERR"%s() Failed to register "
1322 "mpeg encoder\n", __func__);
1323 }
1324 }
1325
1326 if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER) {
1327 if (saa7164_encoder_register(&dev->ports[SAA7164_PORT_ENC2]) < 0) {
1328 printk(KERN_ERR"%s() Failed to register "
1329 "mpeg encoder\n", __func__);
1330 }
1331 }
1332
1333 if (saa7164_boards[dev->board].porte == SAA7164_MPEG_VBI) {
1334 if (saa7164_vbi_register(&dev->ports[SAA7164_PORT_VBI1]) < 0) {
1335 printk(KERN_ERR"%s() Failed to register "
1336 "vbi device\n", __func__);
1337 }
1338 }
1339
1340 if (saa7164_boards[dev->board].portf == SAA7164_MPEG_VBI) {
1341 if (saa7164_vbi_register(&dev->ports[SAA7164_PORT_VBI2]) < 0) {
1342 printk(KERN_ERR"%s() Failed to register "
1343 "vbi device\n", __func__);
1344 }
1345 }
1346 saa7164_api_set_debug(dev, fw_debug);
1347
1348 if (fw_debug) {
1349 dev->kthread = kthread_run(saa7164_thread_function, dev,
1350 "saa7164 debug");
1351 if (!dev->kthread)
1352 printk(KERN_ERR "%s() Failed to create "
1353 "debug kernel thread\n", __func__);
1354 }
1355
1356 } /* != BOARD_UNKNOWN */
1357 else
1358 printk(KERN_ERR "%s() Unsupported board detected, "
1359 "registering without firmware\n", __func__);
1360
1361 dprintk(1, "%s() parameter debug = %d\n", __func__, saa_debug);
1362 dprintk(1, "%s() parameter waitsecs = %d\n", __func__, waitsecs);
1363
1364fail_fw:
1365 return 0;
1366
1367fail_irq:
1368 saa7164_dev_unregister(dev);
1369fail_free:
1370 kfree(dev);
1371 return err;
1372}
1373
1374static void saa7164_shutdown(struct saa7164_dev *dev)
1375{
1376 dprintk(1, "%s()\n", __func__);
1377}
1378
1379static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
1380{
1381 struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
1382
1383 if (dev->board != SAA7164_BOARD_UNKNOWN) {
1384 if (fw_debug && dev->kthread) {
1385 kthread_stop(dev->kthread);
1386 dev->kthread = NULL;
1387 }
1388 if (dev->firmwareloaded)
1389 saa7164_api_set_debug(dev, 0x00);
1390 }
1391
1392 saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
1393 &dev->ports[SAA7164_PORT_ENC1].irq_interval);
1394 saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
1395 &dev->ports[SAA7164_PORT_ENC1].svc_interval);
1396 saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
1397 &dev->ports[SAA7164_PORT_ENC1].irq_svc_interval);
1398 saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
1399 &dev->ports[SAA7164_PORT_ENC1].read_interval);
1400 saa7164_histogram_print(&dev->ports[SAA7164_PORT_ENC1],
1401 &dev->ports[SAA7164_PORT_ENC1].poll_interval);
1402 saa7164_histogram_print(&dev->ports[SAA7164_PORT_VBI1],
1403 &dev->ports[SAA7164_PORT_VBI1].read_interval);
1404 saa7164_histogram_print(&dev->ports[SAA7164_PORT_VBI2],
1405 &dev->ports[SAA7164_PORT_VBI2].poll_interval);
1406
1407 saa7164_shutdown(dev);
1408
1409 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
1410 saa7164_dvb_unregister(&dev->ports[SAA7164_PORT_TS1]);
1411
1412 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
1413 saa7164_dvb_unregister(&dev->ports[SAA7164_PORT_TS2]);
1414
1415 if (saa7164_boards[dev->board].portc == SAA7164_MPEG_ENCODER)
1416 saa7164_encoder_unregister(&dev->ports[SAA7164_PORT_ENC1]);
1417
1418 if (saa7164_boards[dev->board].portd == SAA7164_MPEG_ENCODER)
1419 saa7164_encoder_unregister(&dev->ports[SAA7164_PORT_ENC2]);
1420
1421 if (saa7164_boards[dev->board].porte == SAA7164_MPEG_VBI)
1422 saa7164_vbi_unregister(&dev->ports[SAA7164_PORT_VBI1]);
1423
1424 if (saa7164_boards[dev->board].portf == SAA7164_MPEG_VBI)
1425 saa7164_vbi_unregister(&dev->ports[SAA7164_PORT_VBI2]);
1426
1427 saa7164_i2c_unregister(&dev->i2c_bus[0]);
1428 saa7164_i2c_unregister(&dev->i2c_bus[1]);
1429 saa7164_i2c_unregister(&dev->i2c_bus[2]);
1430
1431 pci_disable_device(pci_dev);
1432
1433 /* unregister stuff */
1434 free_irq(pci_dev->irq, dev);
1435 pci_set_drvdata(pci_dev, NULL);
1436
1437 mutex_lock(&devlist);
1438 list_del(&dev->devlist);
1439 mutex_unlock(&devlist);
1440
1441 saa7164_dev_unregister(dev);
1442 kfree(dev);
1443}
1444
1445static struct pci_device_id saa7164_pci_tbl[] = {
1446 {
1447 /* SAA7164 */
1448 .vendor = 0x1131,
1449 .device = 0x7164,
1450 .subvendor = PCI_ANY_ID,
1451 .subdevice = PCI_ANY_ID,
1452 }, {
1453 /* --- end of list --- */
1454 }
1455};
1456MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl);
1457
1458static struct pci_driver saa7164_pci_driver = {
1459 .name = "saa7164",
1460 .id_table = saa7164_pci_tbl,
1461 .probe = saa7164_initdev,
1462 .remove = __devexit_p(saa7164_finidev),
1463 /* TODO */
1464 .suspend = NULL,
1465 .resume = NULL,
1466};
1467
1468static int __init saa7164_init(void)
1469{
1470 printk(KERN_INFO "saa7164 driver loaded\n");
1471
1472#ifdef CONFIG_PROC_FS
1473 saa7164_proc_create();
1474#endif
1475 return pci_register_driver(&saa7164_pci_driver);
1476}
1477
1478static void __exit saa7164_fini(void)
1479{
1480#ifdef CONFIG_PROC_FS
1481 remove_proc_entry("saa7164", NULL);
1482#endif
1483 pci_unregister_driver(&saa7164_pci_driver);
1484}
1485
1486module_init(saa7164_init);
1487module_exit(saa7164_fini);
1488
diff --git a/drivers/media/pci/saa7164/saa7164-dvb.c b/drivers/media/pci/saa7164/saa7164-dvb.c
new file mode 100644
index 000000000000..5c5cc3ebf9bd
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-dvb.c
@@ -0,0 +1,556 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "saa7164.h"
23
24#include "tda10048.h"
25#include "tda18271.h"
26#include "s5h1411.h"
27
28#define DRIVER_NAME "saa7164"
29
30DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
31
32/* addr is in the card struct, get it from there */
33static struct tda10048_config hauppauge_hvr2200_1_config = {
34 .demod_address = 0x10 >> 1,
35 .output_mode = TDA10048_SERIAL_OUTPUT,
36 .fwbulkwritelen = TDA10048_BULKWRITE_200,
37 .inversion = TDA10048_INVERSION_ON,
38 .dtv6_if_freq_khz = TDA10048_IF_3300,
39 .dtv7_if_freq_khz = TDA10048_IF_3500,
40 .dtv8_if_freq_khz = TDA10048_IF_4000,
41 .clk_freq_khz = TDA10048_CLK_16000,
42};
43static struct tda10048_config hauppauge_hvr2200_2_config = {
44 .demod_address = 0x12 >> 1,
45 .output_mode = TDA10048_SERIAL_OUTPUT,
46 .fwbulkwritelen = TDA10048_BULKWRITE_200,
47 .inversion = TDA10048_INVERSION_ON,
48 .dtv6_if_freq_khz = TDA10048_IF_3300,
49 .dtv7_if_freq_khz = TDA10048_IF_3500,
50 .dtv8_if_freq_khz = TDA10048_IF_4000,
51 .clk_freq_khz = TDA10048_CLK_16000,
52};
53
54static struct tda18271_std_map hauppauge_tda18271_std_map = {
55 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 3,
56 .if_lvl = 6, .rfagc_top = 0x37 },
57 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0,
58 .if_lvl = 6, .rfagc_top = 0x37 },
59};
60
61static struct tda18271_config hauppauge_hvr22x0_tuner_config = {
62 .std_map = &hauppauge_tda18271_std_map,
63 .gate = TDA18271_GATE_ANALOG,
64 .role = TDA18271_MASTER,
65};
66
67static struct tda18271_config hauppauge_hvr22x0s_tuner_config = {
68 .std_map = &hauppauge_tda18271_std_map,
69 .gate = TDA18271_GATE_ANALOG,
70 .role = TDA18271_SLAVE,
71 .output_opt = TDA18271_OUTPUT_LT_OFF,
72 .rf_cal_on_startup = 1
73};
74
75static struct s5h1411_config hauppauge_s5h1411_config = {
76 .output_mode = S5H1411_SERIAL_OUTPUT,
77 .gpio = S5H1411_GPIO_ON,
78 .qam_if = S5H1411_IF_4000,
79 .vsb_if = S5H1411_IF_3250,
80 .inversion = S5H1411_INVERSION_ON,
81 .status_mode = S5H1411_DEMODLOCKING,
82 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
83};
84
85static int saa7164_dvb_stop_port(struct saa7164_port *port)
86{
87 struct saa7164_dev *dev = port->dev;
88 int ret;
89
90 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
91 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
92 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
93 __func__, ret);
94 ret = -EIO;
95 } else {
96 dprintk(DBGLVL_DVB, "%s() Stopped\n", __func__);
97 ret = 0;
98 }
99
100 return ret;
101}
102
103static int saa7164_dvb_acquire_port(struct saa7164_port *port)
104{
105 struct saa7164_dev *dev = port->dev;
106 int ret;
107
108 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
109 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
110 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
111 __func__, ret);
112 ret = -EIO;
113 } else {
114 dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
115 ret = 0;
116 }
117
118 return ret;
119}
120
121static int saa7164_dvb_pause_port(struct saa7164_port *port)
122{
123 struct saa7164_dev *dev = port->dev;
124 int ret;
125
126 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
127 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
128 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
129 __func__, ret);
130 ret = -EIO;
131 } else {
132 dprintk(DBGLVL_DVB, "%s() Paused\n", __func__);
133 ret = 0;
134 }
135
136 return ret;
137}
138
139/* Firmware is very windows centric, meaning you have to transition
140 * the part through AVStream / KS Windows stages, forwards or backwards.
141 * States are: stopped, acquired (h/w), paused, started.
142 */
143static int saa7164_dvb_stop_streaming(struct saa7164_port *port)
144{
145 struct saa7164_dev *dev = port->dev;
146 struct saa7164_buffer *buf;
147 struct list_head *p, *q;
148 int ret;
149
150 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
151
152 ret = saa7164_dvb_pause_port(port);
153 ret = saa7164_dvb_acquire_port(port);
154 ret = saa7164_dvb_stop_port(port);
155
156 /* Mark the hardware buffers as free */
157 mutex_lock(&port->dmaqueue_lock);
158 list_for_each_safe(p, q, &port->dmaqueue.list) {
159 buf = list_entry(p, struct saa7164_buffer, list);
160 buf->flags = SAA7164_BUFFER_FREE;
161 }
162 mutex_unlock(&port->dmaqueue_lock);
163
164 return ret;
165}
166
167static int saa7164_dvb_start_port(struct saa7164_port *port)
168{
169 struct saa7164_dev *dev = port->dev;
170 int ret = 0, result;
171
172 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
173
174 saa7164_buffer_cfg_port(port);
175
176 /* Acquire the hardware */
177 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
178 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
179 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
180 __func__, result);
181
182 /* Stop the hardware, regardless */
183 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
184 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
185 printk(KERN_ERR "%s() acquire/forced stop transition "
186 "failed, res = 0x%x\n", __func__, result);
187 }
188 ret = -EIO;
189 goto out;
190 } else
191 dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
192
193 /* Pause the hardware */
194 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
195 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
196 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
197 __func__, result);
198
199 /* Stop the hardware, regardless */
200 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
201 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
202 printk(KERN_ERR "%s() pause/forced stop transition "
203 "failed, res = 0x%x\n", __func__, result);
204 }
205
206 ret = -EIO;
207 goto out;
208 } else
209 dprintk(DBGLVL_DVB, "%s() Paused\n", __func__);
210
211 /* Start the hardware */
212 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
213 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
214 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
215 __func__, result);
216
217 /* Stop the hardware, regardless */
218 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
219 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
220 printk(KERN_ERR "%s() run/forced stop transition "
221 "failed, res = 0x%x\n", __func__, result);
222 }
223
224 ret = -EIO;
225 } else
226 dprintk(DBGLVL_DVB, "%s() Running\n", __func__);
227
228out:
229 return ret;
230}
231
232static int saa7164_dvb_start_feed(struct dvb_demux_feed *feed)
233{
234 struct dvb_demux *demux = feed->demux;
235 struct saa7164_port *port = (struct saa7164_port *) demux->priv;
236 struct saa7164_dvb *dvb = &port->dvb;
237 struct saa7164_dev *dev = port->dev;
238 int ret = 0;
239
240 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
241
242 if (!demux->dmx.frontend)
243 return -EINVAL;
244
245 if (dvb) {
246 mutex_lock(&dvb->lock);
247 if (dvb->feeding++ == 0) {
248 /* Start transport */
249 ret = saa7164_dvb_start_port(port);
250 }
251 mutex_unlock(&dvb->lock);
252 dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
253 __func__, port->nr, dvb->feeding);
254 }
255
256 return ret;
257}
258
259static int saa7164_dvb_stop_feed(struct dvb_demux_feed *feed)
260{
261 struct dvb_demux *demux = feed->demux;
262 struct saa7164_port *port = (struct saa7164_port *) demux->priv;
263 struct saa7164_dvb *dvb = &port->dvb;
264 struct saa7164_dev *dev = port->dev;
265 int ret = 0;
266
267 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
268
269 if (dvb) {
270 mutex_lock(&dvb->lock);
271 if (--dvb->feeding == 0) {
272 /* Stop transport */
273 ret = saa7164_dvb_stop_streaming(port);
274 }
275 mutex_unlock(&dvb->lock);
276 dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
277 __func__, port->nr, dvb->feeding);
278 }
279
280 return ret;
281}
282
283static int dvb_register(struct saa7164_port *port)
284{
285 struct saa7164_dvb *dvb = &port->dvb;
286 struct saa7164_dev *dev = port->dev;
287 struct saa7164_buffer *buf;
288 int result, i;
289
290 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
291
292 if (port->type != SAA7164_MPEG_DVB)
293 BUG();
294
295 /* Sanity check that the PCI configuration space is active */
296 if (port->hwcfg.BARLocation == 0) {
297 result = -ENOMEM;
298 printk(KERN_ERR "%s: dvb_register_adapter failed "
299 "(errno = %d), NO PCI configuration\n",
300 DRIVER_NAME, result);
301 goto fail_adapter;
302 }
303
304 /* Init and establish defaults */
305 port->hw_streamingparams.bitspersample = 8;
306 port->hw_streamingparams.samplesperline = 188;
307 port->hw_streamingparams.numberoflines =
308 (SAA7164_TS_NUMBER_OF_LINES * 188) / 188;
309
310 port->hw_streamingparams.pitch = 188;
311 port->hw_streamingparams.linethreshold = 0;
312 port->hw_streamingparams.pagetablelistvirt = NULL;
313 port->hw_streamingparams.pagetablelistphys = NULL;
314 port->hw_streamingparams.numpagetables = 2 +
315 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
316
317 port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount;
318
319 /* Allocate the PCI resources */
320 for (i = 0; i < port->hwcfg.buffercount; i++) {
321 buf = saa7164_buffer_alloc(port,
322 port->hw_streamingparams.numberoflines *
323 port->hw_streamingparams.pitch);
324
325 if (!buf) {
326 result = -ENOMEM;
327 printk(KERN_ERR "%s: dvb_register_adapter failed "
328 "(errno = %d), unable to allocate buffers\n",
329 DRIVER_NAME, result);
330 goto fail_adapter;
331 }
332
333 mutex_lock(&port->dmaqueue_lock);
334 list_add_tail(&buf->list, &port->dmaqueue.list);
335 mutex_unlock(&port->dmaqueue_lock);
336 }
337
338 /* register adapter */
339 result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
340 &dev->pci->dev, adapter_nr);
341 if (result < 0) {
342 printk(KERN_ERR "%s: dvb_register_adapter failed "
343 "(errno = %d)\n", DRIVER_NAME, result);
344 goto fail_adapter;
345 }
346 dvb->adapter.priv = port;
347
348 /* register frontend */
349 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
350 if (result < 0) {
351 printk(KERN_ERR "%s: dvb_register_frontend failed "
352 "(errno = %d)\n", DRIVER_NAME, result);
353 goto fail_frontend;
354 }
355
356 /* register demux stuff */
357 dvb->demux.dmx.capabilities =
358 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
359 DMX_MEMORY_BASED_FILTERING;
360 dvb->demux.priv = port;
361 dvb->demux.filternum = 256;
362 dvb->demux.feednum = 256;
363 dvb->demux.start_feed = saa7164_dvb_start_feed;
364 dvb->demux.stop_feed = saa7164_dvb_stop_feed;
365 result = dvb_dmx_init(&dvb->demux);
366 if (result < 0) {
367 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
368 DRIVER_NAME, result);
369 goto fail_dmx;
370 }
371
372 dvb->dmxdev.filternum = 256;
373 dvb->dmxdev.demux = &dvb->demux.dmx;
374 dvb->dmxdev.capabilities = 0;
375 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
376 if (result < 0) {
377 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
378 DRIVER_NAME, result);
379 goto fail_dmxdev;
380 }
381
382 dvb->fe_hw.source = DMX_FRONTEND_0;
383 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
384 if (result < 0) {
385 printk(KERN_ERR "%s: add_frontend failed "
386 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
387 goto fail_fe_hw;
388 }
389
390 dvb->fe_mem.source = DMX_MEMORY_FE;
391 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
392 if (result < 0) {
393 printk(KERN_ERR "%s: add_frontend failed "
394 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
395 goto fail_fe_mem;
396 }
397
398 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
399 if (result < 0) {
400 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
401 DRIVER_NAME, result);
402 goto fail_fe_conn;
403 }
404
405 /* register network adapter */
406 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
407 return 0;
408
409fail_fe_conn:
410 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
411fail_fe_mem:
412 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
413fail_fe_hw:
414 dvb_dmxdev_release(&dvb->dmxdev);
415fail_dmxdev:
416 dvb_dmx_release(&dvb->demux);
417fail_dmx:
418 dvb_unregister_frontend(dvb->frontend);
419fail_frontend:
420 dvb_frontend_detach(dvb->frontend);
421 dvb_unregister_adapter(&dvb->adapter);
422fail_adapter:
423 return result;
424}
425
426int saa7164_dvb_unregister(struct saa7164_port *port)
427{
428 struct saa7164_dvb *dvb = &port->dvb;
429 struct saa7164_dev *dev = port->dev;
430 struct saa7164_buffer *b;
431 struct list_head *c, *n;
432
433 dprintk(DBGLVL_DVB, "%s()\n", __func__);
434
435 if (port->type != SAA7164_MPEG_DVB)
436 BUG();
437
438 /* Remove any allocated buffers */
439 mutex_lock(&port->dmaqueue_lock);
440 list_for_each_safe(c, n, &port->dmaqueue.list) {
441 b = list_entry(c, struct saa7164_buffer, list);
442 list_del(c);
443 saa7164_buffer_dealloc(b);
444 }
445 mutex_unlock(&port->dmaqueue_lock);
446
447 if (dvb->frontend == NULL)
448 return 0;
449
450 dvb_net_release(&dvb->net);
451 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
452 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
453 dvb_dmxdev_release(&dvb->dmxdev);
454 dvb_dmx_release(&dvb->demux);
455 dvb_unregister_frontend(dvb->frontend);
456 dvb_frontend_detach(dvb->frontend);
457 dvb_unregister_adapter(&dvb->adapter);
458 return 0;
459}
460
461/* All the DVB attach calls go here, this function get's modified
462 * for each new card.
463 */
464int saa7164_dvb_register(struct saa7164_port *port)
465{
466 struct saa7164_dev *dev = port->dev;
467 struct saa7164_dvb *dvb = &port->dvb;
468 struct saa7164_i2c *i2c_bus = NULL;
469 int ret;
470
471 dprintk(DBGLVL_DVB, "%s()\n", __func__);
472
473 /* init frontend */
474 switch (dev->board) {
475 case SAA7164_BOARD_HAUPPAUGE_HVR2200:
476 case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
477 case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
478 case SAA7164_BOARD_HAUPPAUGE_HVR2200_4:
479 case SAA7164_BOARD_HAUPPAUGE_HVR2200_5:
480 i2c_bus = &dev->i2c_bus[port->nr + 1];
481 switch (port->nr) {
482 case 0:
483 port->dvb.frontend = dvb_attach(tda10048_attach,
484 &hauppauge_hvr2200_1_config,
485 &i2c_bus->i2c_adap);
486
487 if (port->dvb.frontend != NULL) {
488 /* TODO: addr is in the card struct */
489 dvb_attach(tda18271_attach, port->dvb.frontend,
490 0xc0 >> 1, &i2c_bus->i2c_adap,
491 &hauppauge_hvr22x0_tuner_config);
492 }
493
494 break;
495 case 1:
496 port->dvb.frontend = dvb_attach(tda10048_attach,
497 &hauppauge_hvr2200_2_config,
498 &i2c_bus->i2c_adap);
499
500 if (port->dvb.frontend != NULL) {
501 /* TODO: addr is in the card struct */
502 dvb_attach(tda18271_attach, port->dvb.frontend,
503 0xc0 >> 1, &i2c_bus->i2c_adap,
504 &hauppauge_hvr22x0s_tuner_config);
505 }
506
507 break;
508 }
509 break;
510 case SAA7164_BOARD_HAUPPAUGE_HVR2250:
511 case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
512 case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
513 i2c_bus = &dev->i2c_bus[port->nr + 1];
514
515 port->dvb.frontend = dvb_attach(s5h1411_attach,
516 &hauppauge_s5h1411_config,
517 &i2c_bus->i2c_adap);
518
519 if (port->dvb.frontend != NULL) {
520 if (port->nr == 0) {
521 /* Master TDA18271 */
522 /* TODO: addr is in the card struct */
523 dvb_attach(tda18271_attach, port->dvb.frontend,
524 0xc0 >> 1, &i2c_bus->i2c_adap,
525 &hauppauge_hvr22x0_tuner_config);
526 } else {
527 /* Slave TDA18271 */
528 dvb_attach(tda18271_attach, port->dvb.frontend,
529 0xc0 >> 1, &i2c_bus->i2c_adap,
530 &hauppauge_hvr22x0s_tuner_config);
531 }
532 }
533
534 break;
535 default:
536 printk(KERN_ERR "%s: The frontend isn't supported\n",
537 dev->name);
538 break;
539 }
540 if (NULL == dvb->frontend) {
541 printk(KERN_ERR "%s() Frontend initialization failed\n",
542 __func__);
543 return -1;
544 }
545
546 /* register everything */
547 ret = dvb_register(port);
548 if (ret < 0) {
549 if (dvb->frontend->ops.release)
550 dvb->frontend->ops.release(dvb->frontend);
551 return ret;
552 }
553
554 return 0;
555}
556
diff --git a/drivers/media/pci/saa7164/saa7164-encoder.c b/drivers/media/pci/saa7164/saa7164-encoder.c
new file mode 100644
index 000000000000..a9ed686ad08a
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-encoder.c
@@ -0,0 +1,1500 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "saa7164.h"
23
24#define ENCODER_MAX_BITRATE 6500000
25#define ENCODER_MIN_BITRATE 1000000
26#define ENCODER_DEF_BITRATE 5000000
27
28static struct saa7164_tvnorm saa7164_tvnorms[] = {
29 {
30 .name = "NTSC-M",
31 .id = V4L2_STD_NTSC_M,
32 }, {
33 .name = "NTSC-JP",
34 .id = V4L2_STD_NTSC_M_JP,
35 }
36};
37
38static const u32 saa7164_v4l2_ctrls[] = {
39 V4L2_CID_BRIGHTNESS,
40 V4L2_CID_CONTRAST,
41 V4L2_CID_SATURATION,
42 V4L2_CID_HUE,
43 V4L2_CID_AUDIO_VOLUME,
44 V4L2_CID_SHARPNESS,
45 V4L2_CID_MPEG_STREAM_TYPE,
46 V4L2_CID_MPEG_VIDEO_ASPECT,
47 V4L2_CID_MPEG_VIDEO_B_FRAMES,
48 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
49 V4L2_CID_MPEG_AUDIO_MUTE,
50 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
51 V4L2_CID_MPEG_VIDEO_BITRATE,
52 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
53 0
54};
55
56/* Take the encoder configuration form the port struct and
57 * flush it to the hardware.
58 */
59static void saa7164_encoder_configure(struct saa7164_port *port)
60{
61 struct saa7164_dev *dev = port->dev;
62 dprintk(DBGLVL_ENC, "%s()\n", __func__);
63
64 port->encoder_params.width = port->width;
65 port->encoder_params.height = port->height;
66 port->encoder_params.is_50hz =
67 (port->encodernorm.id & V4L2_STD_625_50) != 0;
68
69 /* Set up the DIF (enable it) for analog mode by default */
70 saa7164_api_initialize_dif(port);
71
72 /* Configure the correct video standard */
73 saa7164_api_configure_dif(port, port->encodernorm.id);
74
75 /* Ensure the audio decoder is correct configured */
76 saa7164_api_set_audio_std(port);
77}
78
79static int saa7164_encoder_buffers_dealloc(struct saa7164_port *port)
80{
81 struct list_head *c, *n, *p, *q, *l, *v;
82 struct saa7164_dev *dev = port->dev;
83 struct saa7164_buffer *buf;
84 struct saa7164_user_buffer *ubuf;
85
86 /* Remove any allocated buffers */
87 mutex_lock(&port->dmaqueue_lock);
88
89 dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
90 list_for_each_safe(c, n, &port->dmaqueue.list) {
91 buf = list_entry(c, struct saa7164_buffer, list);
92 list_del(c);
93 saa7164_buffer_dealloc(buf);
94 }
95
96 dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
97 list_for_each_safe(p, q, &port->list_buf_used.list) {
98 ubuf = list_entry(p, struct saa7164_user_buffer, list);
99 list_del(p);
100 saa7164_buffer_dealloc_user(ubuf);
101 }
102
103 dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
104 list_for_each_safe(l, v, &port->list_buf_free.list) {
105 ubuf = list_entry(l, struct saa7164_user_buffer, list);
106 list_del(l);
107 saa7164_buffer_dealloc_user(ubuf);
108 }
109
110 mutex_unlock(&port->dmaqueue_lock);
111 dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
112
113 return 0;
114}
115
116/* Dynamic buffer switch at encoder start time */
117static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
118{
119 struct saa7164_dev *dev = port->dev;
120 struct saa7164_buffer *buf;
121 struct saa7164_user_buffer *ubuf;
122 struct tmHWStreamParameters *params = &port->hw_streamingparams;
123 int result = -ENODEV, i;
124 int len = 0;
125
126 dprintk(DBGLVL_ENC, "%s()\n", __func__);
127
128 if (port->encoder_params.stream_type ==
129 V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
130 dprintk(DBGLVL_ENC,
131 "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n",
132 __func__);
133 params->samplesperline = 128;
134 params->numberoflines = 256;
135 params->pitch = 128;
136 params->numpagetables = 2 +
137 ((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
138 } else
139 if (port->encoder_params.stream_type ==
140 V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
141 dprintk(DBGLVL_ENC,
142 "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n",
143 __func__);
144 params->samplesperline = 188;
145 params->numberoflines = 312;
146 params->pitch = 188;
147 params->numpagetables = 2 +
148 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
149 } else
150 BUG();
151
152 /* Init and establish defaults */
153 params->bitspersample = 8;
154 params->linethreshold = 0;
155 params->pagetablelistvirt = NULL;
156 params->pagetablelistphys = NULL;
157 params->numpagetableentries = port->hwcfg.buffercount;
158
159 /* Allocate the PCI resources, buffers (hard) */
160 for (i = 0; i < port->hwcfg.buffercount; i++) {
161 buf = saa7164_buffer_alloc(port,
162 params->numberoflines *
163 params->pitch);
164
165 if (!buf) {
166 printk(KERN_ERR "%s() failed "
167 "(errno = %d), unable to allocate buffer\n",
168 __func__, result);
169 result = -ENOMEM;
170 goto failed;
171 } else {
172
173 mutex_lock(&port->dmaqueue_lock);
174 list_add_tail(&buf->list, &port->dmaqueue.list);
175 mutex_unlock(&port->dmaqueue_lock);
176
177 }
178 }
179
180 /* Allocate some kernel buffers for copying
181 * to userpsace.
182 */
183 len = params->numberoflines * params->pitch;
184
185 if (encoder_buffers < 16)
186 encoder_buffers = 16;
187 if (encoder_buffers > 512)
188 encoder_buffers = 512;
189
190 for (i = 0; i < encoder_buffers; i++) {
191
192 ubuf = saa7164_buffer_alloc_user(dev, len);
193 if (ubuf) {
194 mutex_lock(&port->dmaqueue_lock);
195 list_add_tail(&ubuf->list, &port->list_buf_free.list);
196 mutex_unlock(&port->dmaqueue_lock);
197 }
198
199 }
200
201 result = 0;
202
203failed:
204 return result;
205}
206
207static int saa7164_encoder_initialize(struct saa7164_port *port)
208{
209 saa7164_encoder_configure(port);
210 return 0;
211}
212
213/* -- V4L2 --------------------------------------------------------- */
214static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
215{
216 struct saa7164_encoder_fh *fh = file->private_data;
217 struct saa7164_port *port = fh->port;
218 struct saa7164_dev *dev = port->dev;
219 unsigned int i;
220
221 dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)*id);
222
223 for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
224 if (*id & saa7164_tvnorms[i].id)
225 break;
226 }
227 if (i == ARRAY_SIZE(saa7164_tvnorms))
228 return -EINVAL;
229
230 port->encodernorm = saa7164_tvnorms[i];
231
232 /* Update the audio decoder while is not running in
233 * auto detect mode.
234 */
235 saa7164_api_set_audio_std(port);
236
237 dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)*id);
238
239 return 0;
240}
241
242static int vidioc_enum_input(struct file *file, void *priv,
243 struct v4l2_input *i)
244{
245 int n;
246
247 char *inputs[] = { "tuner", "composite", "svideo", "aux",
248 "composite 2", "svideo 2", "aux 2" };
249
250 if (i->index >= 7)
251 return -EINVAL;
252
253 strcpy(i->name, inputs[i->index]);
254
255 if (i->index == 0)
256 i->type = V4L2_INPUT_TYPE_TUNER;
257 else
258 i->type = V4L2_INPUT_TYPE_CAMERA;
259
260 for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
261 i->std |= saa7164_tvnorms[n].id;
262
263 return 0;
264}
265
266static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
267{
268 struct saa7164_encoder_fh *fh = file->private_data;
269 struct saa7164_port *port = fh->port;
270 struct saa7164_dev *dev = port->dev;
271
272 if (saa7164_api_get_videomux(port) != SAA_OK)
273 return -EIO;
274
275 *i = (port->mux_input - 1);
276
277 dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
278
279 return 0;
280}
281
282static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
283{
284 struct saa7164_encoder_fh *fh = file->private_data;
285 struct saa7164_port *port = fh->port;
286 struct saa7164_dev *dev = port->dev;
287
288 dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
289
290 if (i >= 7)
291 return -EINVAL;
292
293 port->mux_input = i + 1;
294
295 if (saa7164_api_set_videomux(port) != SAA_OK)
296 return -EIO;
297
298 return 0;
299}
300
301static int vidioc_g_tuner(struct file *file, void *priv,
302 struct v4l2_tuner *t)
303{
304 struct saa7164_encoder_fh *fh = file->private_data;
305 struct saa7164_port *port = fh->port;
306 struct saa7164_dev *dev = port->dev;
307
308 if (0 != t->index)
309 return -EINVAL;
310
311 strcpy(t->name, "tuner");
312 t->type = V4L2_TUNER_ANALOG_TV;
313 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
314
315 dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
316
317 return 0;
318}
319
320static int vidioc_s_tuner(struct file *file, void *priv,
321 struct v4l2_tuner *t)
322{
323 /* Update the A/V core */
324 return 0;
325}
326
327static int vidioc_g_frequency(struct file *file, void *priv,
328 struct v4l2_frequency *f)
329{
330 struct saa7164_encoder_fh *fh = file->private_data;
331 struct saa7164_port *port = fh->port;
332
333 f->type = V4L2_TUNER_ANALOG_TV;
334 f->frequency = port->freq;
335
336 return 0;
337}
338
339static int vidioc_s_frequency(struct file *file, void *priv,
340 struct v4l2_frequency *f)
341{
342 struct saa7164_encoder_fh *fh = file->private_data;
343 struct saa7164_port *port = fh->port;
344 struct saa7164_dev *dev = port->dev;
345 struct saa7164_port *tsport;
346 struct dvb_frontend *fe;
347
348 /* TODO: Pull this for the std */
349 struct analog_parameters params = {
350 .mode = V4L2_TUNER_ANALOG_TV,
351 .audmode = V4L2_TUNER_MODE_STEREO,
352 .std = port->encodernorm.id,
353 .frequency = f->frequency
354 };
355
356 /* Stop the encoder */
357 dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
358 f->frequency, f->tuner);
359
360 if (f->tuner != 0)
361 return -EINVAL;
362
363 if (f->type != V4L2_TUNER_ANALOG_TV)
364 return -EINVAL;
365
366 port->freq = f->frequency;
367
368 /* Update the hardware */
369 if (port->nr == SAA7164_PORT_ENC1)
370 tsport = &dev->ports[SAA7164_PORT_TS1];
371 else
372 if (port->nr == SAA7164_PORT_ENC2)
373 tsport = &dev->ports[SAA7164_PORT_TS2];
374 else
375 BUG();
376
377 fe = tsport->dvb.frontend;
378
379 if (fe && fe->ops.tuner_ops.set_analog_params)
380 fe->ops.tuner_ops.set_analog_params(fe, &params);
381 else
382 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
383
384 saa7164_encoder_initialize(port);
385
386 return 0;
387}
388
389static int vidioc_g_ctrl(struct file *file, void *priv,
390 struct v4l2_control *ctl)
391{
392 struct saa7164_encoder_fh *fh = file->private_data;
393 struct saa7164_port *port = fh->port;
394 struct saa7164_dev *dev = port->dev;
395
396 dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
397 ctl->id, ctl->value);
398
399 switch (ctl->id) {
400 case V4L2_CID_BRIGHTNESS:
401 ctl->value = port->ctl_brightness;
402 break;
403 case V4L2_CID_CONTRAST:
404 ctl->value = port->ctl_contrast;
405 break;
406 case V4L2_CID_SATURATION:
407 ctl->value = port->ctl_saturation;
408 break;
409 case V4L2_CID_HUE:
410 ctl->value = port->ctl_hue;
411 break;
412 case V4L2_CID_SHARPNESS:
413 ctl->value = port->ctl_sharpness;
414 break;
415 case V4L2_CID_AUDIO_VOLUME:
416 ctl->value = port->ctl_volume;
417 break;
418 default:
419 return -EINVAL;
420 }
421
422 return 0;
423}
424
425static int vidioc_s_ctrl(struct file *file, void *priv,
426 struct v4l2_control *ctl)
427{
428 struct saa7164_encoder_fh *fh = file->private_data;
429 struct saa7164_port *port = fh->port;
430 struct saa7164_dev *dev = port->dev;
431 int ret = 0;
432
433 dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
434 ctl->id, ctl->value);
435
436 switch (ctl->id) {
437 case V4L2_CID_BRIGHTNESS:
438 if ((ctl->value >= 0) && (ctl->value <= 255)) {
439 port->ctl_brightness = ctl->value;
440 saa7164_api_set_usercontrol(port,
441 PU_BRIGHTNESS_CONTROL);
442 } else
443 ret = -EINVAL;
444 break;
445 case V4L2_CID_CONTRAST:
446 if ((ctl->value >= 0) && (ctl->value <= 255)) {
447 port->ctl_contrast = ctl->value;
448 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
449 } else
450 ret = -EINVAL;
451 break;
452 case V4L2_CID_SATURATION:
453 if ((ctl->value >= 0) && (ctl->value <= 255)) {
454 port->ctl_saturation = ctl->value;
455 saa7164_api_set_usercontrol(port,
456 PU_SATURATION_CONTROL);
457 } else
458 ret = -EINVAL;
459 break;
460 case V4L2_CID_HUE:
461 if ((ctl->value >= 0) && (ctl->value <= 255)) {
462 port->ctl_hue = ctl->value;
463 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
464 } else
465 ret = -EINVAL;
466 break;
467 case V4L2_CID_SHARPNESS:
468 if ((ctl->value >= 0) && (ctl->value <= 255)) {
469 port->ctl_sharpness = ctl->value;
470 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
471 } else
472 ret = -EINVAL;
473 break;
474 case V4L2_CID_AUDIO_VOLUME:
475 if ((ctl->value >= -83) && (ctl->value <= 24)) {
476 port->ctl_volume = ctl->value;
477 saa7164_api_set_audio_volume(port, port->ctl_volume);
478 } else
479 ret = -EINVAL;
480 break;
481 default:
482 ret = -EINVAL;
483 }
484
485 return ret;
486}
487
488static int saa7164_get_ctrl(struct saa7164_port *port,
489 struct v4l2_ext_control *ctrl)
490{
491 struct saa7164_encoder_params *params = &port->encoder_params;
492
493 switch (ctrl->id) {
494 case V4L2_CID_MPEG_VIDEO_BITRATE:
495 ctrl->value = params->bitrate;
496 break;
497 case V4L2_CID_MPEG_STREAM_TYPE:
498 ctrl->value = params->stream_type;
499 break;
500 case V4L2_CID_MPEG_AUDIO_MUTE:
501 ctrl->value = params->ctl_mute;
502 break;
503 case V4L2_CID_MPEG_VIDEO_ASPECT:
504 ctrl->value = params->ctl_aspect;
505 break;
506 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
507 ctrl->value = params->bitrate_mode;
508 break;
509 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
510 ctrl->value = params->refdist;
511 break;
512 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
513 ctrl->value = params->bitrate_peak;
514 break;
515 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
516 ctrl->value = params->gop_size;
517 break;
518 default:
519 return -EINVAL;
520 }
521 return 0;
522}
523
524static int vidioc_g_ext_ctrls(struct file *file, void *priv,
525 struct v4l2_ext_controls *ctrls)
526{
527 struct saa7164_encoder_fh *fh = file->private_data;
528 struct saa7164_port *port = fh->port;
529 int i, err = 0;
530
531 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
532 for (i = 0; i < ctrls->count; i++) {
533 struct v4l2_ext_control *ctrl = ctrls->controls + i;
534
535 err = saa7164_get_ctrl(port, ctrl);
536 if (err) {
537 ctrls->error_idx = i;
538 break;
539 }
540 }
541 return err;
542
543 }
544
545 return -EINVAL;
546}
547
548static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
549{
550 int ret = -EINVAL;
551
552 switch (ctrl->id) {
553 case V4L2_CID_MPEG_VIDEO_BITRATE:
554 if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
555 (ctrl->value <= ENCODER_MAX_BITRATE))
556 ret = 0;
557 break;
558 case V4L2_CID_MPEG_STREAM_TYPE:
559 if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
560 (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
561 ret = 0;
562 break;
563 case V4L2_CID_MPEG_AUDIO_MUTE:
564 if ((ctrl->value >= 0) &&
565 (ctrl->value <= 1))
566 ret = 0;
567 break;
568 case V4L2_CID_MPEG_VIDEO_ASPECT:
569 if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
570 (ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
571 ret = 0;
572 break;
573 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
574 if ((ctrl->value >= 0) &&
575 (ctrl->value <= 255))
576 ret = 0;
577 break;
578 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
579 if ((ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ||
580 (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR))
581 ret = 0;
582 break;
583 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
584 if ((ctrl->value >= 1) &&
585 (ctrl->value <= 3))
586 ret = 0;
587 break;
588 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
589 if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
590 (ctrl->value <= ENCODER_MAX_BITRATE))
591 ret = 0;
592 break;
593 default:
594 ret = -EINVAL;
595 }
596
597 return ret;
598}
599
600static int vidioc_try_ext_ctrls(struct file *file, void *priv,
601 struct v4l2_ext_controls *ctrls)
602{
603 int i, err = 0;
604
605 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
606 for (i = 0; i < ctrls->count; i++) {
607 struct v4l2_ext_control *ctrl = ctrls->controls + i;
608
609 err = saa7164_try_ctrl(ctrl, 0);
610 if (err) {
611 ctrls->error_idx = i;
612 break;
613 }
614 }
615 return err;
616 }
617
618 return -EINVAL;
619}
620
621static int saa7164_set_ctrl(struct saa7164_port *port,
622 struct v4l2_ext_control *ctrl)
623{
624 struct saa7164_encoder_params *params = &port->encoder_params;
625 int ret = 0;
626
627 switch (ctrl->id) {
628 case V4L2_CID_MPEG_VIDEO_BITRATE:
629 params->bitrate = ctrl->value;
630 break;
631 case V4L2_CID_MPEG_STREAM_TYPE:
632 params->stream_type = ctrl->value;
633 break;
634 case V4L2_CID_MPEG_AUDIO_MUTE:
635 params->ctl_mute = ctrl->value;
636 ret = saa7164_api_audio_mute(port, params->ctl_mute);
637 if (ret != SAA_OK) {
638 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
639 ret);
640 ret = -EIO;
641 }
642 break;
643 case V4L2_CID_MPEG_VIDEO_ASPECT:
644 params->ctl_aspect = ctrl->value;
645 ret = saa7164_api_set_aspect_ratio(port);
646 if (ret != SAA_OK) {
647 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
648 ret);
649 ret = -EIO;
650 }
651 break;
652 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
653 params->bitrate_mode = ctrl->value;
654 break;
655 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
656 params->refdist = ctrl->value;
657 break;
658 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
659 params->bitrate_peak = ctrl->value;
660 break;
661 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
662 params->gop_size = ctrl->value;
663 break;
664 default:
665 return -EINVAL;
666 }
667
668 /* TODO: Update the hardware */
669
670 return ret;
671}
672
673static int vidioc_s_ext_ctrls(struct file *file, void *priv,
674 struct v4l2_ext_controls *ctrls)
675{
676 struct saa7164_encoder_fh *fh = file->private_data;
677 struct saa7164_port *port = fh->port;
678 int i, err = 0;
679
680 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
681 for (i = 0; i < ctrls->count; i++) {
682 struct v4l2_ext_control *ctrl = ctrls->controls + i;
683
684 err = saa7164_try_ctrl(ctrl, 0);
685 if (err) {
686 ctrls->error_idx = i;
687 break;
688 }
689 err = saa7164_set_ctrl(port, ctrl);
690 if (err) {
691 ctrls->error_idx = i;
692 break;
693 }
694 }
695 return err;
696
697 }
698
699 return -EINVAL;
700}
701
702static int vidioc_querycap(struct file *file, void *priv,
703 struct v4l2_capability *cap)
704{
705 struct saa7164_encoder_fh *fh = file->private_data;
706 struct saa7164_port *port = fh->port;
707 struct saa7164_dev *dev = port->dev;
708
709 strcpy(cap->driver, dev->name);
710 strlcpy(cap->card, saa7164_boards[dev->board].name,
711 sizeof(cap->card));
712 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
713
714 cap->capabilities =
715 V4L2_CAP_VIDEO_CAPTURE |
716 V4L2_CAP_READWRITE |
717 0;
718
719 cap->capabilities |= V4L2_CAP_TUNER;
720 cap->version = 0;
721
722 return 0;
723}
724
725static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
726 struct v4l2_fmtdesc *f)
727{
728 if (f->index != 0)
729 return -EINVAL;
730
731 strlcpy(f->description, "MPEG", sizeof(f->description));
732 f->pixelformat = V4L2_PIX_FMT_MPEG;
733
734 return 0;
735}
736
737static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
738 struct v4l2_format *f)
739{
740 struct saa7164_encoder_fh *fh = file->private_data;
741 struct saa7164_port *port = fh->port;
742 struct saa7164_dev *dev = port->dev;
743
744 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
745 f->fmt.pix.bytesperline = 0;
746 f->fmt.pix.sizeimage =
747 port->ts_packet_size * port->ts_packet_count;
748 f->fmt.pix.colorspace = 0;
749 f->fmt.pix.width = port->width;
750 f->fmt.pix.height = port->height;
751
752 dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n",
753 port->width, port->height);
754
755 return 0;
756}
757
758static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
759 struct v4l2_format *f)
760{
761 struct saa7164_encoder_fh *fh = file->private_data;
762 struct saa7164_port *port = fh->port;
763 struct saa7164_dev *dev = port->dev;
764
765 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
766 f->fmt.pix.bytesperline = 0;
767 f->fmt.pix.sizeimage =
768 port->ts_packet_size * port->ts_packet_count;
769 f->fmt.pix.colorspace = 0;
770 dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
771 port->width, port->height);
772 return 0;
773}
774
775static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
776 struct v4l2_format *f)
777{
778 struct saa7164_encoder_fh *fh = file->private_data;
779 struct saa7164_port *port = fh->port;
780 struct saa7164_dev *dev = port->dev;
781
782 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
783 f->fmt.pix.bytesperline = 0;
784 f->fmt.pix.sizeimage =
785 port->ts_packet_size * port->ts_packet_count;
786 f->fmt.pix.colorspace = 0;
787
788 dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
789 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
790
791 return 0;
792}
793
794static int fill_queryctrl(struct saa7164_encoder_params *params,
795 struct v4l2_queryctrl *c)
796{
797 switch (c->id) {
798 case V4L2_CID_BRIGHTNESS:
799 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
800 case V4L2_CID_CONTRAST:
801 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
802 case V4L2_CID_SATURATION:
803 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
804 case V4L2_CID_HUE:
805 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
806 case V4L2_CID_SHARPNESS:
807 return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
808 case V4L2_CID_MPEG_AUDIO_MUTE:
809 return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
810 case V4L2_CID_AUDIO_VOLUME:
811 return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
812 case V4L2_CID_MPEG_VIDEO_BITRATE:
813 return v4l2_ctrl_query_fill(c,
814 ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
815 100000, ENCODER_DEF_BITRATE);
816 case V4L2_CID_MPEG_STREAM_TYPE:
817 return v4l2_ctrl_query_fill(c,
818 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
819 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
820 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
821 case V4L2_CID_MPEG_VIDEO_ASPECT:
822 return v4l2_ctrl_query_fill(c,
823 V4L2_MPEG_VIDEO_ASPECT_1x1,
824 V4L2_MPEG_VIDEO_ASPECT_221x100,
825 1, V4L2_MPEG_VIDEO_ASPECT_4x3);
826 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
827 return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
828 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
829 return v4l2_ctrl_query_fill(c,
830 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
831 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
832 1, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
833 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
834 return v4l2_ctrl_query_fill(c,
835 1, 3, 1, 1);
836 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
837 return v4l2_ctrl_query_fill(c,
838 ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
839 100000, ENCODER_DEF_BITRATE);
840 default:
841 return -EINVAL;
842 }
843}
844
845static int vidioc_queryctrl(struct file *file, void *priv,
846 struct v4l2_queryctrl *c)
847{
848 struct saa7164_encoder_fh *fh = priv;
849 struct saa7164_port *port = fh->port;
850 int i, next;
851 u32 id = c->id;
852
853 memset(c, 0, sizeof(*c));
854
855 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
856 c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
857
858 for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
859 if (next) {
860 if (c->id < saa7164_v4l2_ctrls[i])
861 c->id = saa7164_v4l2_ctrls[i];
862 else
863 continue;
864 }
865
866 if (c->id == saa7164_v4l2_ctrls[i])
867 return fill_queryctrl(&port->encoder_params, c);
868
869 if (c->id < saa7164_v4l2_ctrls[i])
870 break;
871 }
872
873 return -EINVAL;
874}
875
876static int saa7164_encoder_stop_port(struct saa7164_port *port)
877{
878 struct saa7164_dev *dev = port->dev;
879 int ret;
880
881 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
882 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
883 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
884 __func__, ret);
885 ret = -EIO;
886 } else {
887 dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__);
888 ret = 0;
889 }
890
891 return ret;
892}
893
894static int saa7164_encoder_acquire_port(struct saa7164_port *port)
895{
896 struct saa7164_dev *dev = port->dev;
897 int ret;
898
899 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
900 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
901 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
902 __func__, ret);
903 ret = -EIO;
904 } else {
905 dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
906 ret = 0;
907 }
908
909 return ret;
910}
911
912static int saa7164_encoder_pause_port(struct saa7164_port *port)
913{
914 struct saa7164_dev *dev = port->dev;
915 int ret;
916
917 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
918 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
919 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
920 __func__, ret);
921 ret = -EIO;
922 } else {
923 dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
924 ret = 0;
925 }
926
927 return ret;
928}
929
930/* Firmware is very windows centric, meaning you have to transition
931 * the part through AVStream / KS Windows stages, forwards or backwards.
932 * States are: stopped, acquired (h/w), paused, started.
933 * We have to leave here will all of the soft buffers on the free list,
934 * else the cfg_post() func won't have soft buffers to correctly configure.
935 */
936static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
937{
938 struct saa7164_dev *dev = port->dev;
939 struct saa7164_buffer *buf;
940 struct saa7164_user_buffer *ubuf;
941 struct list_head *c, *n;
942 int ret;
943
944 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
945
946 ret = saa7164_encoder_pause_port(port);
947 ret = saa7164_encoder_acquire_port(port);
948 ret = saa7164_encoder_stop_port(port);
949
950 dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
951 port->nr);
952
953 /* Reset the state of any allocated buffer resources */
954 mutex_lock(&port->dmaqueue_lock);
955
956 /* Reset the hard and soft buffer state */
957 list_for_each_safe(c, n, &port->dmaqueue.list) {
958 buf = list_entry(c, struct saa7164_buffer, list);
959 buf->flags = SAA7164_BUFFER_FREE;
960 buf->pos = 0;
961 }
962
963 list_for_each_safe(c, n, &port->list_buf_used.list) {
964 ubuf = list_entry(c, struct saa7164_user_buffer, list);
965 ubuf->pos = 0;
966 list_move_tail(&ubuf->list, &port->list_buf_free.list);
967 }
968
969 mutex_unlock(&port->dmaqueue_lock);
970
971 /* Free any allocated resources */
972 saa7164_encoder_buffers_dealloc(port);
973
974 dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
975
976 return ret;
977}
978
979static int saa7164_encoder_start_streaming(struct saa7164_port *port)
980{
981 struct saa7164_dev *dev = port->dev;
982 int result, ret = 0;
983
984 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
985
986 port->done_first_interrupt = 0;
987
988 /* allocate all of the PCIe DMA buffer resources on the fly,
989 * allowing switching between TS and PS payloads without
990 * requiring a complete driver reload.
991 */
992 saa7164_encoder_buffers_alloc(port);
993
994 /* Configure the encoder with any cache values */
995 saa7164_api_set_encoder(port);
996 saa7164_api_get_encoder(port);
997
998 /* Place the empty buffers on the hardware */
999 saa7164_buffer_cfg_port(port);
1000
1001 /* Acquire the hardware */
1002 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
1003 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1004 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
1005 __func__, result);
1006
1007 /* Stop the hardware, regardless */
1008 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1009 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1010 printk(KERN_ERR "%s() acquire/forced stop transition "
1011 "failed, res = 0x%x\n", __func__, result);
1012 }
1013 ret = -EIO;
1014 goto out;
1015 } else
1016 dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
1017
1018 /* Pause the hardware */
1019 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
1020 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1021 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
1022 __func__, result);
1023
1024 /* Stop the hardware, regardless */
1025 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1026 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1027 printk(KERN_ERR "%s() pause/forced stop transition "
1028 "failed, res = 0x%x\n", __func__, result);
1029 }
1030
1031 ret = -EIO;
1032 goto out;
1033 } else
1034 dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
1035
1036 /* Start the hardware */
1037 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
1038 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1039 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
1040 __func__, result);
1041
1042 /* Stop the hardware, regardless */
1043 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1044 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1045 printk(KERN_ERR "%s() run/forced stop transition "
1046 "failed, res = 0x%x\n", __func__, result);
1047 }
1048
1049 ret = -EIO;
1050 } else
1051 dprintk(DBGLVL_ENC, "%s() Running\n", __func__);
1052
1053out:
1054 return ret;
1055}
1056
1057static int fops_open(struct file *file)
1058{
1059 struct saa7164_dev *dev;
1060 struct saa7164_port *port;
1061 struct saa7164_encoder_fh *fh;
1062
1063 port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
1064 if (!port)
1065 return -ENODEV;
1066
1067 dev = port->dev;
1068
1069 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1070
1071 /* allocate + initialize per filehandle data */
1072 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1073 if (NULL == fh)
1074 return -ENOMEM;
1075
1076 file->private_data = fh;
1077 fh->port = port;
1078
1079 return 0;
1080}
1081
1082static int fops_release(struct file *file)
1083{
1084 struct saa7164_encoder_fh *fh = file->private_data;
1085 struct saa7164_port *port = fh->port;
1086 struct saa7164_dev *dev = port->dev;
1087
1088 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1089
1090 /* Shut device down on last close */
1091 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1092 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
1093 /* stop mpeg capture then cancel buffers */
1094 saa7164_encoder_stop_streaming(port);
1095 }
1096 }
1097
1098 file->private_data = NULL;
1099 kfree(fh);
1100
1101 return 0;
1102}
1103
1104struct saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
1105{
1106 struct saa7164_user_buffer *ubuf = NULL;
1107 struct saa7164_dev *dev = port->dev;
1108 u32 crc;
1109
1110 mutex_lock(&port->dmaqueue_lock);
1111 if (!list_empty(&port->list_buf_used.list)) {
1112 ubuf = list_first_entry(&port->list_buf_used.list,
1113 struct saa7164_user_buffer, list);
1114
1115 if (crc_checking) {
1116 crc = crc32(0, ubuf->data, ubuf->actual_size);
1117 if (crc != ubuf->crc) {
1118 printk(KERN_ERR
1119 "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
1120 __func__,
1121 ubuf, ubuf->crc, crc);
1122 }
1123 }
1124
1125 }
1126 mutex_unlock(&port->dmaqueue_lock);
1127
1128 dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
1129
1130 return ubuf;
1131}
1132
1133static ssize_t fops_read(struct file *file, char __user *buffer,
1134 size_t count, loff_t *pos)
1135{
1136 struct saa7164_encoder_fh *fh = file->private_data;
1137 struct saa7164_port *port = fh->port;
1138 struct saa7164_user_buffer *ubuf = NULL;
1139 struct saa7164_dev *dev = port->dev;
1140 int ret = 0;
1141 int rem, cnt;
1142 u8 *p;
1143
1144 port->last_read_msecs_diff = port->last_read_msecs;
1145 port->last_read_msecs = jiffies_to_msecs(jiffies);
1146 port->last_read_msecs_diff = port->last_read_msecs -
1147 port->last_read_msecs_diff;
1148
1149 saa7164_histogram_update(&port->read_interval,
1150 port->last_read_msecs_diff);
1151
1152 if (*pos) {
1153 printk(KERN_ERR "%s() ESPIPE\n", __func__);
1154 return -ESPIPE;
1155 }
1156
1157 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1158 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1159
1160 if (saa7164_encoder_initialize(port) < 0) {
1161 printk(KERN_ERR "%s() EINVAL\n", __func__);
1162 return -EINVAL;
1163 }
1164
1165 saa7164_encoder_start_streaming(port);
1166 msleep(200);
1167 }
1168 }
1169
1170 /* blocking wait for buffer */
1171 if ((file->f_flags & O_NONBLOCK) == 0) {
1172 if (wait_event_interruptible(port->wait_read,
1173 saa7164_enc_next_buf(port))) {
1174 printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
1175 return -ERESTARTSYS;
1176 }
1177 }
1178
1179 /* Pull the first buffer from the used list */
1180 ubuf = saa7164_enc_next_buf(port);
1181
1182 while ((count > 0) && ubuf) {
1183
1184 /* set remaining bytes to copy */
1185 rem = ubuf->actual_size - ubuf->pos;
1186 cnt = rem > count ? count : rem;
1187
1188 p = ubuf->data + ubuf->pos;
1189
1190 dprintk(DBGLVL_ENC,
1191 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1192 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1193
1194 if (copy_to_user(buffer, p, cnt)) {
1195 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1196 if (!ret) {
1197 printk(KERN_ERR "%s() EFAULT\n", __func__);
1198 ret = -EFAULT;
1199 }
1200 goto err;
1201 }
1202
1203 ubuf->pos += cnt;
1204 count -= cnt;
1205 buffer += cnt;
1206 ret += cnt;
1207
1208 if (ubuf->pos > ubuf->actual_size)
1209 printk(KERN_ERR "read() pos > actual, huh?\n");
1210
1211 if (ubuf->pos == ubuf->actual_size) {
1212
1213 /* finished with current buffer, take next buffer */
1214
1215 /* Requeue the buffer on the free list */
1216 ubuf->pos = 0;
1217
1218 mutex_lock(&port->dmaqueue_lock);
1219 list_move_tail(&ubuf->list, &port->list_buf_free.list);
1220 mutex_unlock(&port->dmaqueue_lock);
1221
1222 /* Dequeue next */
1223 if ((file->f_flags & O_NONBLOCK) == 0) {
1224 if (wait_event_interruptible(port->wait_read,
1225 saa7164_enc_next_buf(port))) {
1226 break;
1227 }
1228 }
1229 ubuf = saa7164_enc_next_buf(port);
1230 }
1231 }
1232err:
1233 if (!ret && !ubuf)
1234 ret = -EAGAIN;
1235
1236 return ret;
1237}
1238
1239static unsigned int fops_poll(struct file *file, poll_table *wait)
1240{
1241 struct saa7164_encoder_fh *fh =
1242 (struct saa7164_encoder_fh *)file->private_data;
1243 struct saa7164_port *port = fh->port;
1244 unsigned int mask = 0;
1245
1246 port->last_poll_msecs_diff = port->last_poll_msecs;
1247 port->last_poll_msecs = jiffies_to_msecs(jiffies);
1248 port->last_poll_msecs_diff = port->last_poll_msecs -
1249 port->last_poll_msecs_diff;
1250
1251 saa7164_histogram_update(&port->poll_interval,
1252 port->last_poll_msecs_diff);
1253
1254 if (!video_is_registered(port->v4l_device))
1255 return -EIO;
1256
1257 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1258 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1259 if (saa7164_encoder_initialize(port) < 0)
1260 return -EINVAL;
1261 saa7164_encoder_start_streaming(port);
1262 msleep(200);
1263 }
1264 }
1265
1266 /* blocking wait for buffer */
1267 if ((file->f_flags & O_NONBLOCK) == 0) {
1268 if (wait_event_interruptible(port->wait_read,
1269 saa7164_enc_next_buf(port))) {
1270 return -ERESTARTSYS;
1271 }
1272 }
1273
1274 /* Pull the first buffer from the used list */
1275 if (!list_empty(&port->list_buf_used.list))
1276 mask |= POLLIN | POLLRDNORM;
1277
1278 return mask;
1279}
1280
1281static const struct v4l2_file_operations mpeg_fops = {
1282 .owner = THIS_MODULE,
1283 .open = fops_open,
1284 .release = fops_release,
1285 .read = fops_read,
1286 .poll = fops_poll,
1287 .unlocked_ioctl = video_ioctl2,
1288};
1289
1290int saa7164_g_chip_ident(struct file *file, void *fh,
1291 struct v4l2_dbg_chip_ident *chip)
1292{
1293 struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1294 struct saa7164_dev *dev = port->dev;
1295 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1296
1297 return 0;
1298}
1299
1300int saa7164_g_register(struct file *file, void *fh,
1301 struct v4l2_dbg_register *reg)
1302{
1303 struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1304 struct saa7164_dev *dev = port->dev;
1305 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1306
1307 if (!capable(CAP_SYS_ADMIN))
1308 return -EPERM;
1309
1310 return 0;
1311}
1312
1313int saa7164_s_register(struct file *file, void *fh,
1314 struct v4l2_dbg_register *reg)
1315{
1316 struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1317 struct saa7164_dev *dev = port->dev;
1318 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1319
1320 if (!capable(CAP_SYS_ADMIN))
1321 return -EPERM;
1322
1323 return 0;
1324}
1325
1326static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1327 .vidioc_s_std = vidioc_s_std,
1328 .vidioc_enum_input = vidioc_enum_input,
1329 .vidioc_g_input = vidioc_g_input,
1330 .vidioc_s_input = vidioc_s_input,
1331 .vidioc_g_tuner = vidioc_g_tuner,
1332 .vidioc_s_tuner = vidioc_s_tuner,
1333 .vidioc_g_frequency = vidioc_g_frequency,
1334 .vidioc_s_frequency = vidioc_s_frequency,
1335 .vidioc_s_ctrl = vidioc_s_ctrl,
1336 .vidioc_g_ctrl = vidioc_g_ctrl,
1337 .vidioc_querycap = vidioc_querycap,
1338 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1339 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1340 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1341 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1342 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1343 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1344 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1345 .vidioc_queryctrl = vidioc_queryctrl,
1346 .vidioc_g_chip_ident = saa7164_g_chip_ident,
1347#ifdef CONFIG_VIDEO_ADV_DEBUG
1348 .vidioc_g_register = saa7164_g_register,
1349 .vidioc_s_register = saa7164_s_register,
1350#endif
1351};
1352
1353static struct video_device saa7164_mpeg_template = {
1354 .name = "saa7164",
1355 .fops = &mpeg_fops,
1356 .ioctl_ops = &mpeg_ioctl_ops,
1357 .minor = -1,
1358 .tvnorms = SAA7164_NORMS,
1359 .current_norm = V4L2_STD_NTSC_M,
1360};
1361
1362static struct video_device *saa7164_encoder_alloc(
1363 struct saa7164_port *port,
1364 struct pci_dev *pci,
1365 struct video_device *template,
1366 char *type)
1367{
1368 struct video_device *vfd;
1369 struct saa7164_dev *dev = port->dev;
1370
1371 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1372
1373 vfd = video_device_alloc();
1374 if (NULL == vfd)
1375 return NULL;
1376
1377 *vfd = *template;
1378 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1379 type, saa7164_boards[dev->board].name);
1380
1381 vfd->parent = &pci->dev;
1382 vfd->release = video_device_release;
1383 return vfd;
1384}
1385
1386int saa7164_encoder_register(struct saa7164_port *port)
1387{
1388 struct saa7164_dev *dev = port->dev;
1389 int result = -ENODEV;
1390
1391 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1392
1393 if (port->type != SAA7164_MPEG_ENCODER)
1394 BUG();
1395
1396 /* Sanity check that the PCI configuration space is active */
1397 if (port->hwcfg.BARLocation == 0) {
1398 printk(KERN_ERR "%s() failed "
1399 "(errno = %d), NO PCI configuration\n",
1400 __func__, result);
1401 result = -ENOMEM;
1402 goto failed;
1403 }
1404
1405 /* Establish encoder defaults here */
1406 /* Set default TV standard */
1407 port->encodernorm = saa7164_tvnorms[0];
1408 port->width = 720;
1409 port->mux_input = 1; /* Composite */
1410 port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1411 port->audio_format = 0;
1412 port->video_resolution = 0;
1413 port->ctl_brightness = 127;
1414 port->ctl_contrast = 66;
1415 port->ctl_hue = 128;
1416 port->ctl_saturation = 62;
1417 port->ctl_sharpness = 8;
1418 port->encoder_params.bitrate = ENCODER_DEF_BITRATE;
1419 port->encoder_params.bitrate_peak = ENCODER_DEF_BITRATE;
1420 port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1421 port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1422 port->encoder_params.ctl_mute = 0;
1423 port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
1424 port->encoder_params.refdist = 1;
1425 port->encoder_params.gop_size = SAA7164_ENCODER_DEFAULT_GOP_SIZE;
1426
1427 if (port->encodernorm.id & V4L2_STD_525_60)
1428 port->height = 480;
1429 else
1430 port->height = 576;
1431
1432 /* Allocate and register the video device node */
1433 port->v4l_device = saa7164_encoder_alloc(port,
1434 dev->pci, &saa7164_mpeg_template, "mpeg");
1435
1436 if (!port->v4l_device) {
1437 printk(KERN_INFO "%s: can't allocate mpeg device\n",
1438 dev->name);
1439 result = -ENOMEM;
1440 goto failed;
1441 }
1442
1443 video_set_drvdata(port->v4l_device, port);
1444 result = video_register_device(port->v4l_device,
1445 VFL_TYPE_GRABBER, -1);
1446 if (result < 0) {
1447 printk(KERN_INFO "%s: can't register mpeg device\n",
1448 dev->name);
1449 /* TODO: We're going to leak here if we don't dealloc
1450 The buffers above. The unreg function can't deal wit it.
1451 */
1452 goto failed;
1453 }
1454
1455 printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1456 dev->name, port->v4l_device->num);
1457
1458 /* Configure the hardware defaults */
1459 saa7164_api_set_videomux(port);
1460 saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1461 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1462 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1463 saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1464 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1465 saa7164_api_audio_mute(port, 0);
1466 saa7164_api_set_audio_volume(port, 20);
1467 saa7164_api_set_aspect_ratio(port);
1468
1469 /* Disable audio standard detection, it's buggy */
1470 saa7164_api_set_audio_detection(port, 0);
1471
1472 saa7164_api_set_encoder(port);
1473 saa7164_api_get_encoder(port);
1474
1475 result = 0;
1476failed:
1477 return result;
1478}
1479
1480void saa7164_encoder_unregister(struct saa7164_port *port)
1481{
1482 struct saa7164_dev *dev = port->dev;
1483
1484 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1485
1486 if (port->type != SAA7164_MPEG_ENCODER)
1487 BUG();
1488
1489 if (port->v4l_device) {
1490 if (port->v4l_device->minor != -1)
1491 video_unregister_device(port->v4l_device);
1492 else
1493 video_device_release(port->v4l_device);
1494
1495 port->v4l_device = NULL;
1496 }
1497
1498 dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1499}
1500
diff --git a/drivers/media/pci/saa7164/saa7164-fw.c b/drivers/media/pci/saa7164/saa7164-fw.c
new file mode 100644
index 000000000000..a266bf0169e6
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-fw.c
@@ -0,0 +1,613 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/firmware.h>
23#include <linux/slab.h>
24
25#include "saa7164.h"
26
27#define SAA7164_REV2_FIRMWARE "NXP7164-2010-03-10.1.fw"
28#define SAA7164_REV2_FIRMWARE_SIZE 4019072
29
30#define SAA7164_REV3_FIRMWARE "NXP7164-2010-03-10.1.fw"
31#define SAA7164_REV3_FIRMWARE_SIZE 4019072
32
33struct fw_header {
34 u32 firmwaresize;
35 u32 bslsize;
36 u32 reserved;
37 u32 version;
38};
39
40int saa7164_dl_wait_ack(struct saa7164_dev *dev, u32 reg)
41{
42 u32 timeout = SAA_DEVICE_TIMEOUT;
43 while ((saa7164_readl(reg) & 0x01) == 0) {
44 timeout -= 10;
45 if (timeout == 0) {
46 printk(KERN_ERR "%s() timeout (no d/l ack)\n",
47 __func__);
48 return -EBUSY;
49 }
50 msleep(100);
51 }
52
53 return 0;
54}
55
56int saa7164_dl_wait_clr(struct saa7164_dev *dev, u32 reg)
57{
58 u32 timeout = SAA_DEVICE_TIMEOUT;
59 while (saa7164_readl(reg) & 0x01) {
60 timeout -= 10;
61 if (timeout == 0) {
62 printk(KERN_ERR "%s() timeout (no d/l clr)\n",
63 __func__);
64 return -EBUSY;
65 }
66 msleep(100);
67 }
68
69 return 0;
70}
71
72/* TODO: move dlflags into dev-> and change to write/readl/b */
73/* TODO: Excessive levels of debug */
74int saa7164_downloadimage(struct saa7164_dev *dev, u8 *src, u32 srcsize,
75 u32 dlflags, u8 *dst, u32 dstsize)
76{
77 u32 reg, timeout, offset;
78 u8 *srcbuf = NULL;
79 int ret;
80
81 u32 dlflag = dlflags;
82 u32 dlflag_ack = dlflag + 4;
83 u32 drflag = dlflag_ack + 4;
84 u32 drflag_ack = drflag + 4;
85 u32 bleflag = drflag_ack + 4;
86
87 dprintk(DBGLVL_FW,
88 "%s(image=%p, size=%d, flags=0x%x, dst=%p, dstsize=0x%x)\n",
89 __func__, src, srcsize, dlflags, dst, dstsize);
90
91 if ((src == NULL) || (dst == NULL)) {
92 ret = -EIO;
93 goto out;
94 }
95
96 srcbuf = kzalloc(4 * 1048576, GFP_KERNEL);
97 if (NULL == srcbuf) {
98 ret = -ENOMEM;
99 goto out;
100 }
101
102 if (srcsize > (4*1048576)) {
103 ret = -ENOMEM;
104 goto out;
105 }
106
107 memcpy(srcbuf, src, srcsize);
108
109 dprintk(DBGLVL_FW, "%s() dlflag = 0x%x\n", __func__, dlflag);
110 dprintk(DBGLVL_FW, "%s() dlflag_ack = 0x%x\n", __func__, dlflag_ack);
111 dprintk(DBGLVL_FW, "%s() drflag = 0x%x\n", __func__, drflag);
112 dprintk(DBGLVL_FW, "%s() drflag_ack = 0x%x\n", __func__, drflag_ack);
113 dprintk(DBGLVL_FW, "%s() bleflag = 0x%x\n", __func__, bleflag);
114
115 reg = saa7164_readl(dlflag);
116 dprintk(DBGLVL_FW, "%s() dlflag (0x%x)= 0x%x\n", __func__, dlflag, reg);
117 if (reg == 1)
118 dprintk(DBGLVL_FW,
119 "%s() Download flag already set, please reboot\n",
120 __func__);
121
122 /* Indicate download start */
123 saa7164_writel(dlflag, 1);
124 ret = saa7164_dl_wait_ack(dev, dlflag_ack);
125 if (ret < 0)
126 goto out;
127
128 /* Ack download start, then wait for wait */
129 saa7164_writel(dlflag, 0);
130 ret = saa7164_dl_wait_clr(dev, dlflag_ack);
131 if (ret < 0)
132 goto out;
133
134 /* Deal with the raw firmware, in the appropriate chunk size */
135 for (offset = 0; srcsize > dstsize;
136 srcsize -= dstsize, offset += dstsize) {
137
138 dprintk(DBGLVL_FW, "%s() memcpy %d\n", __func__, dstsize);
139 memcpy(dst, srcbuf + offset, dstsize);
140
141 /* Flag the data as ready */
142 saa7164_writel(drflag, 1);
143 ret = saa7164_dl_wait_ack(dev, drflag_ack);
144 if (ret < 0)
145 goto out;
146
147 /* Wait for indication data was received */
148 saa7164_writel(drflag, 0);
149 ret = saa7164_dl_wait_clr(dev, drflag_ack);
150 if (ret < 0)
151 goto out;
152
153 }
154
155 dprintk(DBGLVL_FW, "%s() memcpy(l) %d\n", __func__, dstsize);
156 /* Write last block to the device */
157 memcpy(dst, srcbuf+offset, srcsize);
158
159 /* Flag the data as ready */
160 saa7164_writel(drflag, 1);
161 ret = saa7164_dl_wait_ack(dev, drflag_ack);
162 if (ret < 0)
163 goto out;
164
165 saa7164_writel(drflag, 0);
166 timeout = 0;
167 while (saa7164_readl(bleflag) != SAA_DEVICE_IMAGE_BOOTING) {
168 if (saa7164_readl(bleflag) & SAA_DEVICE_IMAGE_CORRUPT) {
169 printk(KERN_ERR "%s() image corrupt\n", __func__);
170 ret = -EBUSY;
171 goto out;
172 }
173
174 if (saa7164_readl(bleflag) & SAA_DEVICE_MEMORY_CORRUPT) {
175 printk(KERN_ERR "%s() device memory corrupt\n",
176 __func__);
177 ret = -EBUSY;
178 goto out;
179 }
180
181 msleep(10); /* Checkpatch throws a < 20ms warning */
182 if (timeout++ > 60)
183 break;
184 }
185
186 printk(KERN_INFO "%s() Image downloaded, booting...\n", __func__);
187
188 ret = saa7164_dl_wait_clr(dev, drflag_ack);
189 if (ret < 0)
190 goto out;
191
192 printk(KERN_INFO "%s() Image booted successfully.\n", __func__);
193 ret = 0;
194
195out:
196 kfree(srcbuf);
197 return ret;
198}
199
200/* TODO: Excessive debug */
201/* Load the firmware. Optionally it can be in ROM or newer versions
202 * can be on disk, saving the expense of the ROM hardware. */
203int saa7164_downloadfirmware(struct saa7164_dev *dev)
204{
205 /* u32 second_timeout = 60 * SAA_DEVICE_TIMEOUT; */
206 u32 tmp, filesize, version, err_flags, first_timeout, fwlength;
207 u32 second_timeout, updatebootloader = 1, bootloadersize = 0;
208 const struct firmware *fw = NULL;
209 struct fw_header *hdr, *boothdr = NULL, *fwhdr;
210 u32 bootloaderversion = 0, fwloadersize;
211 u8 *bootloaderoffset = NULL, *fwloaderoffset;
212 char *fwname;
213 int ret;
214
215 dprintk(DBGLVL_FW, "%s()\n", __func__);
216
217 if (saa7164_boards[dev->board].chiprev == SAA7164_CHIP_REV2) {
218 fwname = SAA7164_REV2_FIRMWARE;
219 fwlength = SAA7164_REV2_FIRMWARE_SIZE;
220 } else {
221 fwname = SAA7164_REV3_FIRMWARE;
222 fwlength = SAA7164_REV3_FIRMWARE_SIZE;
223 }
224
225 version = saa7164_getcurrentfirmwareversion(dev);
226
227 if (version == 0x00) {
228
229 second_timeout = 100;
230 first_timeout = 100;
231 err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
232 dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
233 __func__, err_flags);
234
235 while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
236 dprintk(DBGLVL_FW, "%s() err_flags = %x\n",
237 __func__, err_flags);
238 msleep(10); /* Checkpatch throws a < 20ms warning */
239
240 if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
241 printk(KERN_ERR "%s() firmware corrupt\n",
242 __func__);
243 break;
244 }
245 if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
246 printk(KERN_ERR "%s() device memory corrupt\n",
247 __func__);
248 break;
249 }
250 if (err_flags & SAA_DEVICE_NO_IMAGE) {
251 printk(KERN_ERR "%s() no first image\n",
252 __func__);
253 break;
254 }
255 if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
256 first_timeout -= 10;
257 if (first_timeout == 0) {
258 printk(KERN_ERR
259 "%s() no first image\n",
260 __func__);
261 break;
262 }
263 } else if (err_flags & SAA_DEVICE_IMAGE_LOADING) {
264 second_timeout -= 10;
265 if (second_timeout == 0) {
266 printk(KERN_ERR
267 "%s() FW load time exceeded\n",
268 __func__);
269 break;
270 }
271 } else {
272 second_timeout -= 10;
273 if (second_timeout == 0) {
274 printk(KERN_ERR
275 "%s() Unknown bootloader flags 0x%x\n",
276 __func__, err_flags);
277 break;
278 }
279 }
280
281 err_flags = saa7164_readl(SAA_BOOTLOADERERROR_FLAGS);
282 } /* While != Booting */
283
284 if (err_flags == SAA_DEVICE_IMAGE_BOOTING) {
285 dprintk(DBGLVL_FW, "%s() Loader 1 has loaded.\n",
286 __func__);
287 first_timeout = SAA_DEVICE_TIMEOUT;
288 second_timeout = 60 * SAA_DEVICE_TIMEOUT;
289 second_timeout = 100;
290
291 err_flags = saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
292 dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
293 __func__, err_flags);
294 while (err_flags != SAA_DEVICE_IMAGE_BOOTING) {
295 dprintk(DBGLVL_FW, "%s() err_flags2 = %x\n",
296 __func__, err_flags);
297 msleep(10); /* Checkpatch throws a < 20ms warning */
298
299 if (err_flags & SAA_DEVICE_IMAGE_CORRUPT) {
300 printk(KERN_ERR
301 "%s() firmware corrupt\n",
302 __func__);
303 break;
304 }
305 if (err_flags & SAA_DEVICE_MEMORY_CORRUPT) {
306 printk(KERN_ERR
307 "%s() device memory corrupt\n",
308 __func__);
309 break;
310 }
311 if (err_flags & SAA_DEVICE_NO_IMAGE) {
312 printk(KERN_ERR "%s() no first image\n",
313 __func__);
314 break;
315 }
316 if (err_flags & SAA_DEVICE_IMAGE_SEARCHING) {
317 first_timeout -= 10;
318 if (first_timeout == 0) {
319 printk(KERN_ERR
320 "%s() no second image\n",
321 __func__);
322 break;
323 }
324 } else if (err_flags &
325 SAA_DEVICE_IMAGE_LOADING) {
326 second_timeout -= 10;
327 if (second_timeout == 0) {
328 printk(KERN_ERR
329 "%s() FW load time exceeded\n",
330 __func__);
331 break;
332 }
333 } else {
334 second_timeout -= 10;
335 if (second_timeout == 0) {
336 printk(KERN_ERR
337 "%s() Unknown bootloader flags 0x%x\n",
338 __func__, err_flags);
339 break;
340 }
341 }
342
343 err_flags =
344 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS);
345 } /* err_flags != SAA_DEVICE_IMAGE_BOOTING */
346
347 dprintk(DBGLVL_FW, "%s() Loader flags 1:0x%x 2:0x%x.\n",
348 __func__,
349 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS),
350 saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS));
351
352 } /* err_flags == SAA_DEVICE_IMAGE_BOOTING */
353
354 /* It's possible for both firmwares to have booted,
355 * but that doesn't mean they've finished booting yet.
356 */
357 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
358 SAA_DEVICE_IMAGE_BOOTING) &&
359 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
360 SAA_DEVICE_IMAGE_BOOTING)) {
361
362
363 dprintk(DBGLVL_FW, "%s() Loader 2 has loaded.\n",
364 __func__);
365
366 first_timeout = SAA_DEVICE_TIMEOUT;
367 while (first_timeout) {
368 msleep(10); /* Checkpatch throws a < 20ms warning */
369
370 version =
371 saa7164_getcurrentfirmwareversion(dev);
372 if (version) {
373 dprintk(DBGLVL_FW,
374 "%s() All f/w loaded successfully\n",
375 __func__);
376 break;
377 } else {
378 first_timeout -= 10;
379 if (first_timeout == 0) {
380 printk(KERN_ERR
381 "%s() FW did not boot\n",
382 __func__);
383 break;
384 }
385 }
386 }
387 }
388 version = saa7164_getcurrentfirmwareversion(dev);
389 } /* version == 0 */
390
391 /* Has the firmware really booted? */
392 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
393 SAA_DEVICE_IMAGE_BOOTING) &&
394 (saa7164_readl(SAA_SECONDSTAGEERROR_FLAGS) ==
395 SAA_DEVICE_IMAGE_BOOTING) && (version == 0)) {
396
397 printk(KERN_ERR
398 "%s() The firmware hung, probably bad firmware\n",
399 __func__);
400
401 /* Tell the second stage loader we have a deadlock */
402 saa7164_writel(SAA_DEVICE_DEADLOCK_DETECTED_OFFSET,
403 SAA_DEVICE_DEADLOCK_DETECTED);
404
405 saa7164_getfirmwarestatus(dev);
406
407 return -ENOMEM;
408 }
409
410 dprintk(DBGLVL_FW, "Device has Firmware Version %d.%d.%d.%d\n",
411 (version & 0x0000fc00) >> 10,
412 (version & 0x000003e0) >> 5,
413 (version & 0x0000001f),
414 (version & 0xffff0000) >> 16);
415
416 /* Load the firmwware from the disk if required */
417 if (version == 0) {
418
419 printk(KERN_INFO "%s() Waiting for firmware upload (%s)\n",
420 __func__, fwname);
421
422 ret = request_firmware(&fw, fwname, &dev->pci->dev);
423 if (ret) {
424 printk(KERN_ERR "%s() Upload failed. "
425 "(file not found?)\n", __func__);
426 return -ENOMEM;
427 }
428
429 printk(KERN_INFO "%s() firmware read %Zu bytes.\n",
430 __func__, fw->size);
431
432 if (fw->size != fwlength) {
433 printk(KERN_ERR "xc5000: firmware incorrect size\n");
434 ret = -ENOMEM;
435 goto out;
436 }
437
438 printk(KERN_INFO "%s() firmware loaded.\n", __func__);
439
440 hdr = (struct fw_header *)fw->data;
441 printk(KERN_INFO "Firmware file header part 1:\n");
442 printk(KERN_INFO " .FirmwareSize = 0x%x\n", hdr->firmwaresize);
443 printk(KERN_INFO " .BSLSize = 0x%x\n", hdr->bslsize);
444 printk(KERN_INFO " .Reserved = 0x%x\n", hdr->reserved);
445 printk(KERN_INFO " .Version = 0x%x\n", hdr->version);
446
447 /* Retrieve bootloader if reqd */
448 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0))
449 /* Second bootloader in the firmware file */
450 filesize = hdr->reserved * 16;
451 else
452 filesize = (hdr->firmwaresize + hdr->bslsize) *
453 16 + sizeof(struct fw_header);
454
455 printk(KERN_INFO "%s() SecBootLoader.FileSize = %d\n",
456 __func__, filesize);
457
458 /* Get bootloader (if reqd) and firmware header */
459 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
460 /* Second boot loader is required */
461
462 /* Get the loader header */
463 boothdr = (struct fw_header *)(fw->data +
464 sizeof(struct fw_header));
465
466 bootloaderversion =
467 saa7164_readl(SAA_DEVICE_2ND_VERSION);
468 dprintk(DBGLVL_FW, "Onboard BootLoader:\n");
469 dprintk(DBGLVL_FW, "->Flag 0x%x\n",
470 saa7164_readl(SAA_BOOTLOADERERROR_FLAGS));
471 dprintk(DBGLVL_FW, "->Ack 0x%x\n",
472 saa7164_readl(SAA_DATAREADY_FLAG_ACK));
473 dprintk(DBGLVL_FW, "->FW Version 0x%x\n", version);
474 dprintk(DBGLVL_FW, "->Loader Version 0x%x\n",
475 bootloaderversion);
476
477 if ((saa7164_readl(SAA_BOOTLOADERERROR_FLAGS) ==
478 0x03) && (saa7164_readl(SAA_DATAREADY_FLAG_ACK)
479 == 0x00) && (version == 0x00)) {
480
481 dprintk(DBGLVL_FW, "BootLoader version in "
482 "rom %d.%d.%d.%d\n",
483 (bootloaderversion & 0x0000fc00) >> 10,
484 (bootloaderversion & 0x000003e0) >> 5,
485 (bootloaderversion & 0x0000001f),
486 (bootloaderversion & 0xffff0000) >> 16
487 );
488 dprintk(DBGLVL_FW, "BootLoader version "
489 "in file %d.%d.%d.%d\n",
490 (boothdr->version & 0x0000fc00) >> 10,
491 (boothdr->version & 0x000003e0) >> 5,
492 (boothdr->version & 0x0000001f),
493 (boothdr->version & 0xffff0000) >> 16
494 );
495
496 if (bootloaderversion == boothdr->version)
497 updatebootloader = 0;
498 }
499
500 /* Calculate offset to firmware header */
501 tmp = (boothdr->firmwaresize + boothdr->bslsize) * 16 +
502 (sizeof(struct fw_header) +
503 sizeof(struct fw_header));
504
505 fwhdr = (struct fw_header *)(fw->data+tmp);
506 } else {
507 /* No second boot loader */
508 fwhdr = hdr;
509 }
510
511 dprintk(DBGLVL_FW, "Firmware version in file %d.%d.%d.%d\n",
512 (fwhdr->version & 0x0000fc00) >> 10,
513 (fwhdr->version & 0x000003e0) >> 5,
514 (fwhdr->version & 0x0000001f),
515 (fwhdr->version & 0xffff0000) >> 16
516 );
517
518 if (version == fwhdr->version) {
519 /* No download, firmware already on board */
520 ret = 0;
521 goto out;
522 }
523
524 if ((hdr->firmwaresize == 0) && (hdr->bslsize == 0)) {
525 if (updatebootloader) {
526 /* Get ready to upload the bootloader */
527 bootloadersize = (boothdr->firmwaresize +
528 boothdr->bslsize) * 16 +
529 sizeof(struct fw_header);
530
531 bootloaderoffset = (u8 *)(fw->data +
532 sizeof(struct fw_header));
533
534 dprintk(DBGLVL_FW, "bootloader d/l starts.\n");
535 printk(KERN_INFO "%s() FirmwareSize = 0x%x\n",
536 __func__, boothdr->firmwaresize);
537 printk(KERN_INFO "%s() BSLSize = 0x%x\n",
538 __func__, boothdr->bslsize);
539 printk(KERN_INFO "%s() Reserved = 0x%x\n",
540 __func__, boothdr->reserved);
541 printk(KERN_INFO "%s() Version = 0x%x\n",
542 __func__, boothdr->version);
543 ret = saa7164_downloadimage(
544 dev,
545 bootloaderoffset,
546 bootloadersize,
547 SAA_DOWNLOAD_FLAGS,
548 dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET,
549 SAA_DEVICE_BUFFERBLOCKSIZE);
550 if (ret < 0) {
551 printk(KERN_ERR
552 "bootloader d/l has failed\n");
553 goto out;
554 }
555 dprintk(DBGLVL_FW,
556 "bootloader download complete.\n");
557
558 }
559
560 printk(KERN_ERR "starting firmware download(2)\n");
561 bootloadersize = (boothdr->firmwaresize +
562 boothdr->bslsize) * 16 +
563 sizeof(struct fw_header);
564
565 bootloaderoffset =
566 (u8 *)(fw->data + sizeof(struct fw_header));
567
568 fwloaderoffset = bootloaderoffset + bootloadersize;
569
570 /* TODO: fix this bounds overrun here with old f/ws */
571 fwloadersize = (fwhdr->firmwaresize + fwhdr->bslsize) *
572 16 + sizeof(struct fw_header);
573
574 ret = saa7164_downloadimage(
575 dev,
576 fwloaderoffset,
577 fwloadersize,
578 SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET,
579 dev->bmmio + SAA_DEVICE_2ND_DOWNLOAD_OFFSET,
580 SAA_DEVICE_2ND_BUFFERBLOCKSIZE);
581 if (ret < 0) {
582 printk(KERN_ERR "firmware download failed\n");
583 goto out;
584 }
585 printk(KERN_ERR "firmware download complete.\n");
586
587 } else {
588
589 /* No bootloader update reqd, download firmware only */
590 printk(KERN_ERR "starting firmware download(3)\n");
591
592 ret = saa7164_downloadimage(
593 dev,
594 (u8 *)fw->data,
595 fw->size,
596 SAA_DOWNLOAD_FLAGS,
597 dev->bmmio + SAA_DEVICE_DOWNLOAD_OFFSET,
598 SAA_DEVICE_BUFFERBLOCKSIZE);
599 if (ret < 0) {
600 printk(KERN_ERR "firmware download failed\n");
601 goto out;
602 }
603 printk(KERN_ERR "firmware download complete.\n");
604 }
605 }
606
607 dev->firmwareloaded = 1;
608 ret = 0;
609
610out:
611 release_firmware(fw);
612 return ret;
613}
diff --git a/drivers/media/pci/saa7164/saa7164-i2c.c b/drivers/media/pci/saa7164/saa7164-i2c.c
new file mode 100644
index 000000000000..4f7e3b42263f
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-i2c.c
@@ -0,0 +1,125 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/io.h>
27
28#include "saa7164.h"
29
30static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
31{
32 struct saa7164_i2c *bus = i2c_adap->algo_data;
33 struct saa7164_dev *dev = bus->dev;
34 int i, retval = 0;
35
36 dprintk(DBGLVL_I2C, "%s(num = %d)\n", __func__, num);
37
38 for (i = 0 ; i < num; i++) {
39 dprintk(DBGLVL_I2C, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
40 __func__, num, msgs[i].addr, msgs[i].len);
41 if (msgs[i].flags & I2C_M_RD) {
42 /* Unsupported - Yet*/
43 printk(KERN_ERR "%s() Unsupported - Yet\n", __func__);
44 continue;
45 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
46 msgs[i].addr == msgs[i + 1].addr) {
47 /* write then read from same address */
48
49 retval = saa7164_api_i2c_read(bus, msgs[i].addr,
50 msgs[i].len, msgs[i].buf,
51 msgs[i+1].len, msgs[i+1].buf
52 );
53
54 i++;
55
56 if (retval < 0)
57 goto err;
58 } else {
59 /* write */
60 retval = saa7164_api_i2c_write(bus, msgs[i].addr,
61 msgs[i].len, msgs[i].buf);
62 }
63 if (retval < 0)
64 goto err;
65 }
66 return num;
67
68err:
69 return retval;
70}
71
72static u32 saa7164_functionality(struct i2c_adapter *adap)
73{
74 return I2C_FUNC_I2C;
75}
76
77static struct i2c_algorithm saa7164_i2c_algo_template = {
78 .master_xfer = i2c_xfer,
79 .functionality = saa7164_functionality,
80};
81
82/* ----------------------------------------------------------------------- */
83
84static struct i2c_adapter saa7164_i2c_adap_template = {
85 .name = "saa7164",
86 .owner = THIS_MODULE,
87 .algo = &saa7164_i2c_algo_template,
88};
89
90static struct i2c_client saa7164_i2c_client_template = {
91 .name = "saa7164 internal",
92};
93
94int saa7164_i2c_register(struct saa7164_i2c *bus)
95{
96 struct saa7164_dev *dev = bus->dev;
97
98 dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
99
100 bus->i2c_adap = saa7164_i2c_adap_template;
101 bus->i2c_client = saa7164_i2c_client_template;
102
103 bus->i2c_adap.dev.parent = &dev->pci->dev;
104
105 strlcpy(bus->i2c_adap.name, bus->dev->name,
106 sizeof(bus->i2c_adap.name));
107
108 bus->i2c_adap.algo_data = bus;
109 i2c_set_adapdata(&bus->i2c_adap, bus);
110 i2c_add_adapter(&bus->i2c_adap);
111
112 bus->i2c_client.adapter = &bus->i2c_adap;
113
114 if (0 != bus->i2c_rc)
115 printk(KERN_ERR "%s: i2c bus %d register FAILED\n",
116 dev->name, bus->nr);
117
118 return bus->i2c_rc;
119}
120
121int saa7164_i2c_unregister(struct saa7164_i2c *bus)
122{
123 i2c_del_adapter(&bus->i2c_adap);
124 return 0;
125}
diff --git a/drivers/media/pci/saa7164/saa7164-reg.h b/drivers/media/pci/saa7164/saa7164-reg.h
new file mode 100644
index 000000000000..2bbf81583d33
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-reg.h
@@ -0,0 +1,219 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/* TODO: Retest the driver with errors expressed as negatives */
23
24/* Result codes */
25#define SAA_OK 0
26#define SAA_ERR_BAD_PARAMETER 0x09
27#define SAA_ERR_NO_RESOURCES 0x0c
28#define SAA_ERR_NOT_SUPPORTED 0x13
29#define SAA_ERR_BUSY 0x15
30#define SAA_ERR_READ 0x17
31#define SAA_ERR_TIMEOUT 0x1f
32#define SAA_ERR_OVERFLOW 0x20
33#define SAA_ERR_EMPTY 0x22
34#define SAA_ERR_NOT_STARTED 0x23
35#define SAA_ERR_ALREADY_STARTED 0x24
36#define SAA_ERR_NOT_STOPPED 0x25
37#define SAA_ERR_ALREADY_STOPPED 0x26
38#define SAA_ERR_INVALID_COMMAND 0x3e
39#define SAA_ERR_NULL_PACKET 0x59
40
41/* Errors and flags from the silicon */
42#define PVC_ERRORCODE_UNKNOWN 0x00
43#define PVC_ERRORCODE_INVALID_COMMAND 0x01
44#define PVC_ERRORCODE_INVALID_CONTROL 0x02
45#define PVC_ERRORCODE_INVALID_DATA 0x03
46#define PVC_ERRORCODE_TIMEOUT 0x04
47#define PVC_ERRORCODE_NAK 0x05
48#define PVC_RESPONSEFLAG_ERROR 0x01
49#define PVC_RESPONSEFLAG_OVERFLOW 0x02
50#define PVC_RESPONSEFLAG_RESET 0x04
51#define PVC_RESPONSEFLAG_INTERFACE 0x08
52#define PVC_RESPONSEFLAG_CONTINUED 0x10
53#define PVC_CMDFLAG_INTERRUPT 0x02
54#define PVC_CMDFLAG_INTERFACE 0x04
55#define PVC_CMDFLAG_SERIALIZE 0x08
56#define PVC_CMDFLAG_CONTINUE 0x10
57
58/* Silicon Commands */
59#define GET_DESCRIPTORS_CONTROL 0x01
60#define GET_STRING_CONTROL 0x03
61#define GET_LANGUAGE_CONTROL 0x05
62#define SET_POWER_CONTROL 0x07
63#define GET_FW_STATUS_CONTROL 0x08
64#define GET_FW_VERSION_CONTROL 0x09
65#define SET_DEBUG_LEVEL_CONTROL 0x0B
66#define GET_DEBUG_DATA_CONTROL 0x0C
67#define GET_PRODUCTION_INFO_CONTROL 0x0D
68
69/* cmd defines */
70#define SAA_CMDFLAG_CONTINUE 0x10
71#define SAA_CMD_MAX_MSG_UNITS 256
72
73/* Some defines */
74#define SAA_BUS_TIMEOUT 50
75#define SAA_DEVICE_TIMEOUT 5000
76#define SAA_DEVICE_MAXREQUESTSIZE 256
77
78/* Register addresses */
79#define SAA_DEVICE_VERSION 0x30
80#define SAA_DOWNLOAD_FLAGS 0x34
81#define SAA_DOWNLOAD_FLAG 0x34
82#define SAA_DOWNLOAD_FLAG_ACK 0x38
83#define SAA_DATAREADY_FLAG 0x3C
84#define SAA_DATAREADY_FLAG_ACK 0x40
85
86/* Boot loader register and bit definitions */
87#define SAA_BOOTLOADERERROR_FLAGS 0x44
88#define SAA_DEVICE_IMAGE_SEARCHING 0x01
89#define SAA_DEVICE_IMAGE_LOADING 0x02
90#define SAA_DEVICE_IMAGE_BOOTING 0x03
91#define SAA_DEVICE_IMAGE_CORRUPT 0x04
92#define SAA_DEVICE_MEMORY_CORRUPT 0x08
93#define SAA_DEVICE_NO_IMAGE 0x10
94
95/* Register addresses */
96#define SAA_DEVICE_2ND_VERSION 0x50
97#define SAA_DEVICE_2ND_DOWNLOADFLAG_OFFSET 0x54
98
99/* Register addresses */
100#define SAA_SECONDSTAGEERROR_FLAGS 0x64
101
102/* Bootloader regs and flags */
103#define SAA_DEVICE_DEADLOCK_DETECTED_OFFSET 0x6C
104#define SAA_DEVICE_DEADLOCK_DETECTED 0xDEADDEAD
105
106/* Basic firmware status registers */
107#define SAA_DEVICE_SYSINIT_STATUS_OFFSET 0x70
108#define SAA_DEVICE_SYSINIT_STATUS 0x70
109#define SAA_DEVICE_SYSINIT_MODE 0x74
110#define SAA_DEVICE_SYSINIT_SPEC 0x78
111#define SAA_DEVICE_SYSINIT_INST 0x7C
112#define SAA_DEVICE_SYSINIT_CPULOAD 0x80
113#define SAA_DEVICE_SYSINIT_REMAINHEAP 0x84
114
115#define SAA_DEVICE_DOWNLOAD_OFFSET 0x1000
116#define SAA_DEVICE_BUFFERBLOCKSIZE 0x1000
117
118#define SAA_DEVICE_2ND_BUFFERBLOCKSIZE 0x100000
119#define SAA_DEVICE_2ND_DOWNLOAD_OFFSET 0x200000
120
121/* Descriptors */
122#define CS_INTERFACE 0x24
123
124/* Descriptor subtypes */
125#define VC_INPUT_TERMINAL 0x02
126#define VC_OUTPUT_TERMINAL 0x03
127#define VC_SELECTOR_UNIT 0x04
128#define VC_PROCESSING_UNIT 0x05
129#define FEATURE_UNIT 0x06
130#define TUNER_UNIT 0x09
131#define ENCODER_UNIT 0x0A
132#define EXTENSION_UNIT 0x0B
133#define VC_TUNER_PATH 0xF0
134#define PVC_HARDWARE_DESCRIPTOR 0xF1
135#define PVC_INTERFACE_DESCRIPTOR 0xF2
136#define PVC_INFRARED_UNIT 0xF3
137#define DRM_UNIT 0xF4
138#define GENERAL_REQUEST 0xF5
139
140/* Format Types */
141#define VS_FORMAT_TYPE 0x02
142#define VS_FORMAT_TYPE_I 0x01
143#define VS_FORMAT_UNCOMPRESSED 0x04
144#define VS_FRAME_UNCOMPRESSED 0x05
145#define VS_FORMAT_MPEG2PS 0x09
146#define VS_FORMAT_MPEG2TS 0x0A
147#define VS_FORMAT_MPEG4SL 0x0B
148#define VS_FORMAT_WM9 0x0C
149#define VS_FORMAT_DIVX 0x0D
150#define VS_FORMAT_VBI 0x0E
151#define VS_FORMAT_RDS 0x0F
152
153/* Device extension commands */
154#define EXU_REGISTER_ACCESS_CONTROL 0x00
155#define EXU_GPIO_CONTROL 0x01
156#define EXU_GPIO_GROUP_CONTROL 0x02
157#define EXU_INTERRUPT_CONTROL 0x03
158
159/* State Transition and args */
160#define SAA_PROBE_CONTROL 0x01
161#define SAA_COMMIT_CONTROL 0x02
162#define SAA_STATE_CONTROL 0x03
163#define SAA_DMASTATE_STOP 0x00
164#define SAA_DMASTATE_ACQUIRE 0x01
165#define SAA_DMASTATE_PAUSE 0x02
166#define SAA_DMASTATE_RUN 0x03
167
168/* A/V Mux Input Selector */
169#define SU_INPUT_SELECT_CONTROL 0x01
170
171/* Encoder Profiles */
172#define EU_PROFILE_PS_DVD 0x06
173#define EU_PROFILE_TS_HQ 0x09
174#define EU_VIDEO_FORMAT_MPEG_2 0x02
175
176/* Tuner */
177#define TU_AUDIO_MODE_CONTROL 0x17
178
179/* Video Formats */
180#define TU_STANDARD_CONTROL 0x00
181#define TU_STANDARD_AUTO_CONTROL 0x01
182#define TU_STANDARD_NONE 0x00
183#define TU_STANDARD_NTSC_M 0x01
184#define TU_STANDARD_PAL_I 0x08
185#define TU_STANDARD_MANUAL 0x00
186#define TU_STANDARD_AUTO 0x01
187
188/* Video Controls */
189#define PU_BRIGHTNESS_CONTROL 0x02
190#define PU_CONTRAST_CONTROL 0x03
191#define PU_HUE_CONTROL 0x06
192#define PU_SATURATION_CONTROL 0x07
193#define PU_SHARPNESS_CONTROL 0x08
194
195/* Audio Controls */
196#define MUTE_CONTROL 0x01
197#define VOLUME_CONTROL 0x02
198#define AUDIO_DEFAULT_CONTROL 0x0D
199
200/* Default Volume Levels */
201#define TMHW_LEV_ADJ_DECLEV_DEFAULT 0x00
202#define TMHW_LEV_ADJ_MONOLEV_DEFAULT 0x00
203#define TMHW_LEV_ADJ_NICLEV_DEFAULT 0x00
204#define TMHW_LEV_ADJ_SAPLEV_DEFAULT 0x00
205#define TMHW_LEV_ADJ_ADCLEV_DEFAULT 0x00
206
207/* Encoder Related Commands */
208#define EU_PROFILE_CONTROL 0x00
209#define EU_VIDEO_FORMAT_CONTROL 0x01
210#define EU_VIDEO_BIT_RATE_CONTROL 0x02
211#define EU_VIDEO_RESOLUTION_CONTROL 0x03
212#define EU_VIDEO_GOP_STRUCTURE_CONTROL 0x04
213#define EU_VIDEO_INPUT_ASPECT_CONTROL 0x0A
214#define EU_AUDIO_FORMAT_CONTROL 0x0C
215#define EU_AUDIO_BIT_RATE_CONTROL 0x0D
216
217/* Firmware Debugging */
218#define SET_DEBUG_LEVEL_CONTROL 0x0B
219#define GET_DEBUG_DATA_CONTROL 0x0C
diff --git a/drivers/media/pci/saa7164/saa7164-types.h b/drivers/media/pci/saa7164/saa7164-types.h
new file mode 100644
index 000000000000..1d2140a3eb38
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-types.h
@@ -0,0 +1,442 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/* TODO: Cleanup and shorten the namespace */
23
24/* Some structues are passed directly to/from the firmware and
25 * have strict alignment requirements. This is one of them.
26 */
27struct tmComResHWDescr {
28 u8 bLength;
29 u8 bDescriptorType;
30 u8 bDescriptorSubtype;
31 u16 bcdSpecVersion;
32 u32 dwClockFrequency;
33 u32 dwClockUpdateRes;
34 u8 bCapabilities;
35 u32 dwDeviceRegistersLocation;
36 u32 dwHostMemoryRegion;
37 u32 dwHostMemoryRegionSize;
38 u32 dwHostHibernatMemRegion;
39 u32 dwHostHibernatMemRegionSize;
40} __attribute__((packed));
41
42/* This is DWORD aligned on windows but I can't find the right
43 * gcc syntax to match the binary data from the device.
44 * I've manually padded with Reserved[3] bytes to match the hardware,
45 * but this could break if GCC decies to pack in a different way.
46 */
47struct tmComResInterfaceDescr {
48 u8 bLength;
49 u8 bDescriptorType;
50 u8 bDescriptorSubtype;
51 u8 bFlags;
52 u8 bInterfaceType;
53 u8 bInterfaceId;
54 u8 bBaseInterface;
55 u8 bInterruptId;
56 u8 bDebugInterruptId;
57 u8 BARLocation;
58 u8 Reserved[3];
59};
60
61struct tmComResBusDescr {
62 u64 CommandRing;
63 u64 ResponseRing;
64 u32 CommandWrite;
65 u32 CommandRead;
66 u32 ResponseWrite;
67 u32 ResponseRead;
68};
69
70enum tmBusType {
71 NONE = 0,
72 TYPE_BUS_PCI = 1,
73 TYPE_BUS_PCIe = 2,
74 TYPE_BUS_USB = 3,
75 TYPE_BUS_I2C = 4
76};
77
78struct tmComResBusInfo {
79 enum tmBusType Type;
80 u16 m_wMaxReqSize;
81 u8 *m_pdwSetRing;
82 u32 m_dwSizeSetRing;
83 u8 *m_pdwGetRing;
84 u32 m_dwSizeGetRing;
85 u32 m_dwSetWritePos;
86 u32 m_dwSetReadPos;
87 u32 m_dwGetWritePos;
88 u32 m_dwGetReadPos;
89
90 /* All access is protected */
91 struct mutex lock;
92
93};
94
95struct tmComResInfo {
96 u8 id;
97 u8 flags;
98 u16 size;
99 u32 command;
100 u16 controlselector;
101 u8 seqno;
102} __attribute__((packed));
103
104enum tmComResCmd {
105 SET_CUR = 0x01,
106 GET_CUR = 0x81,
107 GET_MIN = 0x82,
108 GET_MAX = 0x83,
109 GET_RES = 0x84,
110 GET_LEN = 0x85,
111 GET_INFO = 0x86,
112 GET_DEF = 0x87
113};
114
115struct cmd {
116 u8 seqno;
117 u32 inuse;
118 u32 timeout;
119 u32 signalled;
120 struct mutex lock;
121 wait_queue_head_t wait;
122};
123
124struct tmDescriptor {
125 u32 pathid;
126 u32 size;
127 void *descriptor;
128};
129
130struct tmComResDescrHeader {
131 u8 len;
132 u8 type;
133 u8 subtype;
134 u8 unitid;
135} __attribute__((packed));
136
137struct tmComResExtDevDescrHeader {
138 u8 len;
139 u8 type;
140 u8 subtype;
141 u8 unitid;
142 u32 devicetype;
143 u16 deviceid;
144 u32 numgpiopins;
145 u8 numgpiogroups;
146 u8 controlsize;
147} __attribute__((packed));
148
149struct tmComResGPIO {
150 u32 pin;
151 u8 state;
152} __attribute__((packed));
153
154struct tmComResPathDescrHeader {
155 u8 len;
156 u8 type;
157 u8 subtype;
158 u8 pathid;
159} __attribute__((packed));
160
161/* terminaltype */
162enum tmComResTermType {
163 ITT_ANTENNA = 0x0203,
164 LINE_CONNECTOR = 0x0603,
165 SPDIF_CONNECTOR = 0x0605,
166 COMPOSITE_CONNECTOR = 0x0401,
167 SVIDEO_CONNECTOR = 0x0402,
168 COMPONENT_CONNECTOR = 0x0403,
169 STANDARD_DMA = 0xF101
170};
171
172struct tmComResAntTermDescrHeader {
173 u8 len;
174 u8 type;
175 u8 subtype;
176 u8 terminalid;
177 u16 terminaltype;
178 u8 assocterminal;
179 u8 iterminal;
180 u8 controlsize;
181} __attribute__((packed));
182
183struct tmComResTunerDescrHeader {
184 u8 len;
185 u8 type;
186 u8 subtype;
187 u8 unitid;
188 u8 sourceid;
189 u8 iunit;
190 u32 tuningstandards;
191 u8 controlsize;
192 u32 controls;
193} __attribute__((packed));
194
195enum tmBufferFlag {
196 /* the buffer does not contain any valid data */
197 TM_BUFFER_FLAG_EMPTY,
198
199 /* the buffer is filled with valid data */
200 TM_BUFFER_FLAG_DONE,
201
202 /* the buffer is the dummy buffer - TODO??? */
203 TM_BUFFER_FLAG_DUMMY_BUFFER
204};
205
206struct tmBuffer {
207 u64 *pagetablevirt;
208 u64 pagetablephys;
209 u16 offset;
210 u8 *context;
211 u64 timestamp;
212 enum tmBufferFlag BufferFlag;
213 u32 lostbuffers;
214 u32 validbuffers;
215 u64 *dummypagevirt;
216 u64 dummypagephys;
217 u64 *addressvirt;
218};
219
220struct tmHWStreamParameters {
221 u32 bitspersample;
222 u32 samplesperline;
223 u32 numberoflines;
224 u32 pitch;
225 u32 linethreshold;
226 u64 **pagetablelistvirt;
227 u64 *pagetablelistphys;
228 u32 numpagetables;
229 u32 numpagetableentries;
230};
231
232struct tmStreamParameters {
233 struct tmHWStreamParameters HWStreamParameters;
234 u64 qwDummyPageTablePhys;
235 u64 *pDummyPageTableVirt;
236};
237
238struct tmComResDMATermDescrHeader {
239 u8 len;
240 u8 type;
241 u8 subtyle;
242 u8 unitid;
243 u16 terminaltype;
244 u8 assocterminal;
245 u8 sourceid;
246 u8 iterminal;
247 u32 BARLocation;
248 u8 flags;
249 u8 interruptid;
250 u8 buffercount;
251 u8 metadatasize;
252 u8 numformats;
253 u8 controlsize;
254} __attribute__((packed));
255
256/*
257 *
258 * Description:
259 * This is the transport stream format header.
260 *
261 * Settings:
262 * bLength - The size of this descriptor in bytes.
263 * bDescriptorType - CS_INTERFACE.
264 * bDescriptorSubtype - VS_FORMAT_MPEG2TS descriptor subtype.
265 * bFormatIndex - A non-zero constant that uniquely identifies the
266 * format.
267 * bDataOffset - Offset to TSP packet within MPEG-2 TS transport
268 * stride, in bytes.
269 * bPacketLength - Length of TSP packet, in bytes (typically 188).
270 * bStrideLength - Length of MPEG-2 TS transport stride.
271 * guidStrideFormat - A Globally Unique Identifier indicating the
272 * format of the stride data (if any). Set to zeros
273 * if there is no Stride Data, or if the Stride
274 * Data is to be ignored by the application.
275 *
276 */
277struct tmComResTSFormatDescrHeader {
278 u8 len;
279 u8 type;
280 u8 subtype;
281 u8 bFormatIndex;
282 u8 bDataOffset;
283 u8 bPacketLength;
284 u8 bStrideLength;
285 u8 guidStrideFormat[16];
286} __attribute__((packed));
287
288/* Encoder related structures */
289
290/* A/V Mux Selector */
291struct tmComResSelDescrHeader {
292 u8 len;
293 u8 type;
294 u8 subtype;
295 u8 unitid;
296 u8 nrinpins;
297 u8 sourceid;
298} __attribute__((packed));
299
300/* A/V Audio processor definitions */
301struct tmComResProcDescrHeader {
302 u8 len;
303 u8 type;
304 u8 subtype;
305 u8 unitid;
306 u8 sourceid;
307 u16 wreserved;
308 u8 controlsize;
309} __attribute__((packed));
310
311/* Video bitrate control message */
312#define EU_VIDEO_BIT_RATE_MODE_CONSTANT (0)
313#define EU_VIDEO_BIT_RATE_MODE_VARIABLE_AVERAGE (1)
314#define EU_VIDEO_BIT_RATE_MODE_VARIABLE_PEAK (2)
315struct tmComResEncVideoBitRate {
316 u8 ucVideoBitRateMode;
317 u32 dwVideoBitRate;
318 u32 dwVideoBitRatePeak;
319} __attribute__((packed));
320
321/* Video Encoder Aspect Ratio message */
322struct tmComResEncVideoInputAspectRatio {
323 u8 width;
324 u8 height;
325} __attribute__((packed));
326
327/* Video Encoder GOP IBP message */
328/* 1. IPPPPPPPPPPPPPP */
329/* 2. IBPBPBPBPBPBPBP */
330/* 3. IBBPBBPBBPBBP */
331#define SAA7164_ENCODER_DEFAULT_GOP_DIST (1)
332#define SAA7164_ENCODER_DEFAULT_GOP_SIZE (15)
333struct tmComResEncVideoGopStructure {
334 u8 ucGOPSize; /* GOP Size 12, 15 */
335 u8 ucRefFrameDist; /* Reference Frame Distance */
336} __attribute__((packed));
337
338/* Encoder processor definition */
339struct tmComResEncoderDescrHeader {
340 u8 len;
341 u8 type;
342 u8 subtype;
343 u8 unitid;
344 u8 vsourceid;
345 u8 asourceid;
346 u8 iunit;
347 u32 dwmControlCap;
348 u32 dwmProfileCap;
349 u32 dwmVidFormatCap;
350 u8 bmVidBitrateCap;
351 u16 wmVidResolutionsCap;
352 u16 wmVidFrmRateCap;
353 u32 dwmAudFormatCap;
354 u8 bmAudBitrateCap;
355} __attribute__((packed));
356
357/* Audio processor definition */
358struct tmComResAFeatureDescrHeader {
359 u8 len;
360 u8 type;
361 u8 subtype;
362 u8 unitid;
363 u8 sourceid;
364 u8 controlsize;
365} __attribute__((packed));
366
367/* Audio control messages */
368struct tmComResAudioDefaults {
369 u8 ucDecoderLevel;
370 u8 ucDecoderFM_Level;
371 u8 ucMonoLevel;
372 u8 ucNICAM_Level;
373 u8 ucSAP_Level;
374 u8 ucADC_Level;
375} __attribute__((packed));
376
377/* Audio bitrate control message */
378struct tmComResEncAudioBitRate {
379 u8 ucAudioBitRateMode;
380 u32 dwAudioBitRate;
381 u32 dwAudioBitRatePeak;
382} __attribute__((packed));
383
384/* Tuner / AV Decoder messages */
385struct tmComResTunerStandard {
386 u8 std;
387 u32 country;
388} __attribute__((packed));
389
390struct tmComResTunerStandardAuto {
391 u8 mode;
392} __attribute__((packed));
393
394/* EEPROM definition for PS stream types */
395struct tmComResPSFormatDescrHeader {
396 u8 len;
397 u8 type;
398 u8 subtype;
399 u8 bFormatIndex;
400 u16 wPacketLength;
401 u16 wPackLength;
402 u8 bPackDataType;
403} __attribute__((packed));
404
405/* VBI control structure */
406struct tmComResVBIFormatDescrHeader {
407 u8 len;
408 u8 type;
409 u8 subtype; /* VS_FORMAT_VBI */
410 u8 bFormatIndex;
411 u32 VideoStandard; /* See KS_AnalogVideoStandard, NTSC = 1 */
412 u8 StartLine; /* NTSC Start = 10 */
413 u8 EndLine; /* NTSC = 21 */
414 u8 FieldRate; /* 60 for NTSC */
415 u8 bNumLines; /* Unused - scheduled for removal */
416} __attribute__((packed));
417
418struct tmComResProbeCommit {
419 u16 bmHint;
420 u8 bFormatIndex;
421 u8 bFrameIndex;
422} __attribute__((packed));
423
424struct tmComResDebugSetLevel {
425 u32 dwDebugLevel;
426} __attribute__((packed));
427
428struct tmComResDebugGetData {
429 u32 dwResult;
430 u8 ucDebugData[256];
431} __attribute__((packed));
432
433struct tmFwInfoStruct {
434 u32 status;
435 u32 mode;
436 u32 devicespec;
437 u32 deviceinst;
438 u32 CPULoad;
439 u32 RemainHeap;
440 u32 CPUClock;
441 u32 RAMSpeed;
442} __attribute__((packed));
diff --git a/drivers/media/pci/saa7164/saa7164-vbi.c b/drivers/media/pci/saa7164/saa7164-vbi.c
new file mode 100644
index 000000000000..d8e6c8f14079
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164-vbi.c
@@ -0,0 +1,1374 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "saa7164.h"
23
24static struct saa7164_tvnorm saa7164_tvnorms[] = {
25 {
26 .name = "NTSC-M",
27 .id = V4L2_STD_NTSC_M,
28 }, {
29 .name = "NTSC-JP",
30 .id = V4L2_STD_NTSC_M_JP,
31 }
32};
33
34static const u32 saa7164_v4l2_ctrls[] = {
35 0
36};
37
38/* Take the encoder configuration from the port struct and
39 * flush it to the hardware.
40 */
41static void saa7164_vbi_configure(struct saa7164_port *port)
42{
43 struct saa7164_dev *dev = port->dev;
44 dprintk(DBGLVL_VBI, "%s()\n", __func__);
45
46 port->vbi_params.width = port->width;
47 port->vbi_params.height = port->height;
48 port->vbi_params.is_50hz =
49 (port->encodernorm.id & V4L2_STD_625_50) != 0;
50
51 /* Set up the DIF (enable it) for analog mode by default */
52 saa7164_api_initialize_dif(port);
53
54 /* Configure the correct video standard */
55#if 0
56 saa7164_api_configure_dif(port, port->encodernorm.id);
57#endif
58
59#if 0
60 /* Ensure the audio decoder is correct configured */
61 saa7164_api_set_audio_std(port);
62#endif
63 dprintk(DBGLVL_VBI, "%s() ends\n", __func__);
64}
65
66static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
67{
68 struct list_head *c, *n, *p, *q, *l, *v;
69 struct saa7164_dev *dev = port->dev;
70 struct saa7164_buffer *buf;
71 struct saa7164_user_buffer *ubuf;
72
73 /* Remove any allocated buffers */
74 mutex_lock(&port->dmaqueue_lock);
75
76 dprintk(DBGLVL_VBI, "%s(port=%d) dmaqueue\n", __func__, port->nr);
77 list_for_each_safe(c, n, &port->dmaqueue.list) {
78 buf = list_entry(c, struct saa7164_buffer, list);
79 list_del(c);
80 saa7164_buffer_dealloc(buf);
81 }
82
83 dprintk(DBGLVL_VBI, "%s(port=%d) used\n", __func__, port->nr);
84 list_for_each_safe(p, q, &port->list_buf_used.list) {
85 ubuf = list_entry(p, struct saa7164_user_buffer, list);
86 list_del(p);
87 saa7164_buffer_dealloc_user(ubuf);
88 }
89
90 dprintk(DBGLVL_VBI, "%s(port=%d) free\n", __func__, port->nr);
91 list_for_each_safe(l, v, &port->list_buf_free.list) {
92 ubuf = list_entry(l, struct saa7164_user_buffer, list);
93 list_del(l);
94 saa7164_buffer_dealloc_user(ubuf);
95 }
96
97 mutex_unlock(&port->dmaqueue_lock);
98 dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
99
100 return 0;
101}
102
103/* Dynamic buffer switch at vbi start time */
104static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
105{
106 struct saa7164_dev *dev = port->dev;
107 struct saa7164_buffer *buf;
108 struct saa7164_user_buffer *ubuf;
109 struct tmHWStreamParameters *params = &port->hw_streamingparams;
110 int result = -ENODEV, i;
111 int len = 0;
112
113 dprintk(DBGLVL_VBI, "%s()\n", __func__);
114
115 /* TODO: NTSC SPECIFIC */
116 /* Init and establish defaults */
117 params->samplesperline = 1440;
118 params->numberoflines = 12;
119 params->numberoflines = 18;
120 params->pitch = 1600;
121 params->pitch = 1440;
122 params->numpagetables = 2 +
123 ((params->numberoflines * params->pitch) / PAGE_SIZE);
124 params->bitspersample = 8;
125 params->linethreshold = 0;
126 params->pagetablelistvirt = NULL;
127 params->pagetablelistphys = NULL;
128 params->numpagetableentries = port->hwcfg.buffercount;
129
130 /* Allocate the PCI resources, buffers (hard) */
131 for (i = 0; i < port->hwcfg.buffercount; i++) {
132 buf = saa7164_buffer_alloc(port,
133 params->numberoflines *
134 params->pitch);
135
136 if (!buf) {
137 printk(KERN_ERR "%s() failed "
138 "(errno = %d), unable to allocate buffer\n",
139 __func__, result);
140 result = -ENOMEM;
141 goto failed;
142 } else {
143
144 mutex_lock(&port->dmaqueue_lock);
145 list_add_tail(&buf->list, &port->dmaqueue.list);
146 mutex_unlock(&port->dmaqueue_lock);
147
148 }
149 }
150
151 /* Allocate some kernel buffers for copying
152 * to userpsace.
153 */
154 len = params->numberoflines * params->pitch;
155
156 if (vbi_buffers < 16)
157 vbi_buffers = 16;
158 if (vbi_buffers > 512)
159 vbi_buffers = 512;
160
161 for (i = 0; i < vbi_buffers; i++) {
162
163 ubuf = saa7164_buffer_alloc_user(dev, len);
164 if (ubuf) {
165 mutex_lock(&port->dmaqueue_lock);
166 list_add_tail(&ubuf->list, &port->list_buf_free.list);
167 mutex_unlock(&port->dmaqueue_lock);
168 }
169
170 }
171
172 result = 0;
173
174failed:
175 return result;
176}
177
178
179static int saa7164_vbi_initialize(struct saa7164_port *port)
180{
181 saa7164_vbi_configure(port);
182 return 0;
183}
184
185/* -- V4L2 --------------------------------------------------------- */
186static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
187{
188 struct saa7164_vbi_fh *fh = file->private_data;
189 struct saa7164_port *port = fh->port;
190 struct saa7164_dev *dev = port->dev;
191 unsigned int i;
192
193 dprintk(DBGLVL_VBI, "%s(id=0x%x)\n", __func__, (u32)*id);
194
195 for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
196 if (*id & saa7164_tvnorms[i].id)
197 break;
198 }
199 if (i == ARRAY_SIZE(saa7164_tvnorms))
200 return -EINVAL;
201
202 port->encodernorm = saa7164_tvnorms[i];
203
204 /* Update the audio decoder while is not running in
205 * auto detect mode.
206 */
207 saa7164_api_set_audio_std(port);
208
209 dprintk(DBGLVL_VBI, "%s(id=0x%x) OK\n", __func__, (u32)*id);
210
211 return 0;
212}
213
214static int vidioc_enum_input(struct file *file, void *priv,
215 struct v4l2_input *i)
216{
217 int n;
218
219 char *inputs[] = { "tuner", "composite", "svideo", "aux",
220 "composite 2", "svideo 2", "aux 2" };
221
222 if (i->index >= 7)
223 return -EINVAL;
224
225 strcpy(i->name, inputs[i->index]);
226
227 if (i->index == 0)
228 i->type = V4L2_INPUT_TYPE_TUNER;
229 else
230 i->type = V4L2_INPUT_TYPE_CAMERA;
231
232 for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
233 i->std |= saa7164_tvnorms[n].id;
234
235 return 0;
236}
237
238static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
239{
240 struct saa7164_vbi_fh *fh = file->private_data;
241 struct saa7164_port *port = fh->port;
242 struct saa7164_dev *dev = port->dev;
243
244 if (saa7164_api_get_videomux(port) != SAA_OK)
245 return -EIO;
246
247 *i = (port->mux_input - 1);
248
249 dprintk(DBGLVL_VBI, "%s() input=%d\n", __func__, *i);
250
251 return 0;
252}
253
254static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
255{
256 struct saa7164_vbi_fh *fh = file->private_data;
257 struct saa7164_port *port = fh->port;
258 struct saa7164_dev *dev = port->dev;
259
260 dprintk(DBGLVL_VBI, "%s() input=%d\n", __func__, i);
261
262 if (i >= 7)
263 return -EINVAL;
264
265 port->mux_input = i + 1;
266
267 if (saa7164_api_set_videomux(port) != SAA_OK)
268 return -EIO;
269
270 return 0;
271}
272
273static int vidioc_g_tuner(struct file *file, void *priv,
274 struct v4l2_tuner *t)
275{
276 struct saa7164_vbi_fh *fh = file->private_data;
277 struct saa7164_port *port = fh->port;
278 struct saa7164_dev *dev = port->dev;
279
280 if (0 != t->index)
281 return -EINVAL;
282
283 strcpy(t->name, "tuner");
284 t->type = V4L2_TUNER_ANALOG_TV;
285 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
286
287 dprintk(DBGLVL_VBI, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
288
289 return 0;
290}
291
292static int vidioc_s_tuner(struct file *file, void *priv,
293 struct v4l2_tuner *t)
294{
295 /* Update the A/V core */
296 return 0;
297}
298
299static int vidioc_g_frequency(struct file *file, void *priv,
300 struct v4l2_frequency *f)
301{
302 struct saa7164_vbi_fh *fh = file->private_data;
303 struct saa7164_port *port = fh->port;
304
305 f->type = V4L2_TUNER_ANALOG_TV;
306 f->frequency = port->freq;
307
308 return 0;
309}
310
311static int vidioc_s_frequency(struct file *file, void *priv,
312 struct v4l2_frequency *f)
313{
314 struct saa7164_vbi_fh *fh = file->private_data;
315 struct saa7164_port *port = fh->port;
316 struct saa7164_dev *dev = port->dev;
317 struct saa7164_port *tsport;
318 struct dvb_frontend *fe;
319
320 /* TODO: Pull this for the std */
321 struct analog_parameters params = {
322 .mode = V4L2_TUNER_ANALOG_TV,
323 .audmode = V4L2_TUNER_MODE_STEREO,
324 .std = port->encodernorm.id,
325 .frequency = f->frequency
326 };
327
328 /* Stop the encoder */
329 dprintk(DBGLVL_VBI, "%s() frequency=%d tuner=%d\n", __func__,
330 f->frequency, f->tuner);
331
332 if (f->tuner != 0)
333 return -EINVAL;
334
335 if (f->type != V4L2_TUNER_ANALOG_TV)
336 return -EINVAL;
337
338 port->freq = f->frequency;
339
340 /* Update the hardware */
341 if (port->nr == SAA7164_PORT_VBI1)
342 tsport = &dev->ports[SAA7164_PORT_TS1];
343 else
344 if (port->nr == SAA7164_PORT_VBI2)
345 tsport = &dev->ports[SAA7164_PORT_TS2];
346 else
347 BUG();
348
349 fe = tsport->dvb.frontend;
350
351 if (fe && fe->ops.tuner_ops.set_analog_params)
352 fe->ops.tuner_ops.set_analog_params(fe, &params);
353 else
354 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
355
356 saa7164_vbi_initialize(port);
357
358 return 0;
359}
360
361static int vidioc_g_ctrl(struct file *file, void *priv,
362 struct v4l2_control *ctl)
363{
364 struct saa7164_vbi_fh *fh = file->private_data;
365 struct saa7164_port *port = fh->port;
366 struct saa7164_dev *dev = port->dev;
367
368 dprintk(DBGLVL_VBI, "%s(id=%d, value=%d)\n", __func__,
369 ctl->id, ctl->value);
370
371 switch (ctl->id) {
372 case V4L2_CID_BRIGHTNESS:
373 ctl->value = port->ctl_brightness;
374 break;
375 case V4L2_CID_CONTRAST:
376 ctl->value = port->ctl_contrast;
377 break;
378 case V4L2_CID_SATURATION:
379 ctl->value = port->ctl_saturation;
380 break;
381 case V4L2_CID_HUE:
382 ctl->value = port->ctl_hue;
383 break;
384 case V4L2_CID_SHARPNESS:
385 ctl->value = port->ctl_sharpness;
386 break;
387 case V4L2_CID_AUDIO_VOLUME:
388 ctl->value = port->ctl_volume;
389 break;
390 default:
391 return -EINVAL;
392 }
393
394 return 0;
395}
396
397static int vidioc_s_ctrl(struct file *file, void *priv,
398 struct v4l2_control *ctl)
399{
400 struct saa7164_vbi_fh *fh = file->private_data;
401 struct saa7164_port *port = fh->port;
402 struct saa7164_dev *dev = port->dev;
403 int ret = 0;
404
405 dprintk(DBGLVL_VBI, "%s(id=%d, value=%d)\n", __func__,
406 ctl->id, ctl->value);
407
408 switch (ctl->id) {
409 case V4L2_CID_BRIGHTNESS:
410 if ((ctl->value >= 0) && (ctl->value <= 255)) {
411 port->ctl_brightness = ctl->value;
412 saa7164_api_set_usercontrol(port,
413 PU_BRIGHTNESS_CONTROL);
414 } else
415 ret = -EINVAL;
416 break;
417 case V4L2_CID_CONTRAST:
418 if ((ctl->value >= 0) && (ctl->value <= 255)) {
419 port->ctl_contrast = ctl->value;
420 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
421 } else
422 ret = -EINVAL;
423 break;
424 case V4L2_CID_SATURATION:
425 if ((ctl->value >= 0) && (ctl->value <= 255)) {
426 port->ctl_saturation = ctl->value;
427 saa7164_api_set_usercontrol(port,
428 PU_SATURATION_CONTROL);
429 } else
430 ret = -EINVAL;
431 break;
432 case V4L2_CID_HUE:
433 if ((ctl->value >= 0) && (ctl->value <= 255)) {
434 port->ctl_hue = ctl->value;
435 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
436 } else
437 ret = -EINVAL;
438 break;
439 case V4L2_CID_SHARPNESS:
440 if ((ctl->value >= 0) && (ctl->value <= 255)) {
441 port->ctl_sharpness = ctl->value;
442 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
443 } else
444 ret = -EINVAL;
445 break;
446 case V4L2_CID_AUDIO_VOLUME:
447 if ((ctl->value >= -83) && (ctl->value <= 24)) {
448 port->ctl_volume = ctl->value;
449 saa7164_api_set_audio_volume(port, port->ctl_volume);
450 } else
451 ret = -EINVAL;
452 break;
453 default:
454 ret = -EINVAL;
455 }
456
457 return ret;
458}
459
460static int saa7164_get_ctrl(struct saa7164_port *port,
461 struct v4l2_ext_control *ctrl)
462{
463 struct saa7164_vbi_params *params = &port->vbi_params;
464
465 switch (ctrl->id) {
466 case V4L2_CID_MPEG_STREAM_TYPE:
467 ctrl->value = params->stream_type;
468 break;
469 case V4L2_CID_MPEG_AUDIO_MUTE:
470 ctrl->value = params->ctl_mute;
471 break;
472 case V4L2_CID_MPEG_VIDEO_ASPECT:
473 ctrl->value = params->ctl_aspect;
474 break;
475 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
476 ctrl->value = params->refdist;
477 break;
478 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
479 ctrl->value = params->gop_size;
480 break;
481 default:
482 return -EINVAL;
483 }
484 return 0;
485}
486
487static int vidioc_g_ext_ctrls(struct file *file, void *priv,
488 struct v4l2_ext_controls *ctrls)
489{
490 struct saa7164_vbi_fh *fh = file->private_data;
491 struct saa7164_port *port = fh->port;
492 int i, err = 0;
493
494 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
495 for (i = 0; i < ctrls->count; i++) {
496 struct v4l2_ext_control *ctrl = ctrls->controls + i;
497
498 err = saa7164_get_ctrl(port, ctrl);
499 if (err) {
500 ctrls->error_idx = i;
501 break;
502 }
503 }
504 return err;
505
506 }
507
508 return -EINVAL;
509}
510
511static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
512{
513 int ret = -EINVAL;
514
515 switch (ctrl->id) {
516 case V4L2_CID_MPEG_STREAM_TYPE:
517 if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
518 (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
519 ret = 0;
520 break;
521 case V4L2_CID_MPEG_AUDIO_MUTE:
522 if ((ctrl->value >= 0) &&
523 (ctrl->value <= 1))
524 ret = 0;
525 break;
526 case V4L2_CID_MPEG_VIDEO_ASPECT:
527 if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
528 (ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
529 ret = 0;
530 break;
531 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
532 if ((ctrl->value >= 0) &&
533 (ctrl->value <= 255))
534 ret = 0;
535 break;
536 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
537 if ((ctrl->value >= 1) &&
538 (ctrl->value <= 3))
539 ret = 0;
540 break;
541 default:
542 ret = -EINVAL;
543 }
544
545 return ret;
546}
547
548static int vidioc_try_ext_ctrls(struct file *file, void *priv,
549 struct v4l2_ext_controls *ctrls)
550{
551 int i, err = 0;
552
553 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
554 for (i = 0; i < ctrls->count; i++) {
555 struct v4l2_ext_control *ctrl = ctrls->controls + i;
556
557 err = saa7164_try_ctrl(ctrl, 0);
558 if (err) {
559 ctrls->error_idx = i;
560 break;
561 }
562 }
563 return err;
564 }
565
566 return -EINVAL;
567}
568
569static int saa7164_set_ctrl(struct saa7164_port *port,
570 struct v4l2_ext_control *ctrl)
571{
572 struct saa7164_vbi_params *params = &port->vbi_params;
573 int ret = 0;
574
575 switch (ctrl->id) {
576 case V4L2_CID_MPEG_STREAM_TYPE:
577 params->stream_type = ctrl->value;
578 break;
579 case V4L2_CID_MPEG_AUDIO_MUTE:
580 params->ctl_mute = ctrl->value;
581 ret = saa7164_api_audio_mute(port, params->ctl_mute);
582 if (ret != SAA_OK) {
583 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
584 ret);
585 ret = -EIO;
586 }
587 break;
588 case V4L2_CID_MPEG_VIDEO_ASPECT:
589 params->ctl_aspect = ctrl->value;
590 ret = saa7164_api_set_aspect_ratio(port);
591 if (ret != SAA_OK) {
592 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
593 ret);
594 ret = -EIO;
595 }
596 break;
597 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
598 params->refdist = ctrl->value;
599 break;
600 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
601 params->gop_size = ctrl->value;
602 break;
603 default:
604 return -EINVAL;
605 }
606
607 /* TODO: Update the hardware */
608
609 return ret;
610}
611
612static int vidioc_s_ext_ctrls(struct file *file, void *priv,
613 struct v4l2_ext_controls *ctrls)
614{
615 struct saa7164_vbi_fh *fh = file->private_data;
616 struct saa7164_port *port = fh->port;
617 int i, err = 0;
618
619 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
620 for (i = 0; i < ctrls->count; i++) {
621 struct v4l2_ext_control *ctrl = ctrls->controls + i;
622
623 err = saa7164_try_ctrl(ctrl, 0);
624 if (err) {
625 ctrls->error_idx = i;
626 break;
627 }
628 err = saa7164_set_ctrl(port, ctrl);
629 if (err) {
630 ctrls->error_idx = i;
631 break;
632 }
633 }
634 return err;
635
636 }
637
638 return -EINVAL;
639}
640
641static int vidioc_querycap(struct file *file, void *priv,
642 struct v4l2_capability *cap)
643{
644 struct saa7164_vbi_fh *fh = file->private_data;
645 struct saa7164_port *port = fh->port;
646 struct saa7164_dev *dev = port->dev;
647
648 strcpy(cap->driver, dev->name);
649 strlcpy(cap->card, saa7164_boards[dev->board].name,
650 sizeof(cap->card));
651 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
652
653 cap->capabilities =
654 V4L2_CAP_VBI_CAPTURE |
655 V4L2_CAP_READWRITE |
656 0;
657
658 cap->capabilities |= V4L2_CAP_TUNER;
659 cap->version = 0;
660
661 return 0;
662}
663
664static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
665 struct v4l2_fmtdesc *f)
666{
667 if (f->index != 0)
668 return -EINVAL;
669
670 strlcpy(f->description, "VBI", sizeof(f->description));
671 f->pixelformat = V4L2_PIX_FMT_MPEG;
672
673 return 0;
674}
675
676static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
677 struct v4l2_format *f)
678{
679 struct saa7164_vbi_fh *fh = file->private_data;
680 struct saa7164_port *port = fh->port;
681 struct saa7164_dev *dev = port->dev;
682
683 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
684 f->fmt.pix.bytesperline = 0;
685 f->fmt.pix.sizeimage =
686 port->ts_packet_size * port->ts_packet_count;
687 f->fmt.pix.colorspace = 0;
688 f->fmt.pix.width = port->width;
689 f->fmt.pix.height = port->height;
690
691 dprintk(DBGLVL_VBI, "VIDIOC_G_FMT: w: %d, h: %d\n",
692 port->width, port->height);
693
694 return 0;
695}
696
697static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
698 struct v4l2_format *f)
699{
700 struct saa7164_vbi_fh *fh = file->private_data;
701 struct saa7164_port *port = fh->port;
702 struct saa7164_dev *dev = port->dev;
703
704 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
705 f->fmt.pix.bytesperline = 0;
706 f->fmt.pix.sizeimage =
707 port->ts_packet_size * port->ts_packet_count;
708 f->fmt.pix.colorspace = 0;
709 dprintk(DBGLVL_VBI, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
710 port->width, port->height);
711 return 0;
712}
713
714static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
715 struct v4l2_format *f)
716{
717 struct saa7164_vbi_fh *fh = file->private_data;
718 struct saa7164_port *port = fh->port;
719 struct saa7164_dev *dev = port->dev;
720
721 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
722 f->fmt.pix.bytesperline = 0;
723 f->fmt.pix.sizeimage =
724 port->ts_packet_size * port->ts_packet_count;
725 f->fmt.pix.colorspace = 0;
726
727 dprintk(DBGLVL_VBI, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
728 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
729
730 return 0;
731}
732
733static int fill_queryctrl(struct saa7164_vbi_params *params,
734 struct v4l2_queryctrl *c)
735{
736 switch (c->id) {
737 case V4L2_CID_BRIGHTNESS:
738 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
739 case V4L2_CID_CONTRAST:
740 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
741 case V4L2_CID_SATURATION:
742 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
743 case V4L2_CID_HUE:
744 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
745 case V4L2_CID_SHARPNESS:
746 return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
747 case V4L2_CID_MPEG_AUDIO_MUTE:
748 return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
749 case V4L2_CID_AUDIO_VOLUME:
750 return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
751 case V4L2_CID_MPEG_STREAM_TYPE:
752 return v4l2_ctrl_query_fill(c,
753 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
754 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
755 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
756 case V4L2_CID_MPEG_VIDEO_ASPECT:
757 return v4l2_ctrl_query_fill(c,
758 V4L2_MPEG_VIDEO_ASPECT_1x1,
759 V4L2_MPEG_VIDEO_ASPECT_221x100,
760 1, V4L2_MPEG_VIDEO_ASPECT_4x3);
761 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
762 return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
763 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
764 return v4l2_ctrl_query_fill(c,
765 1, 3, 1, 1);
766 default:
767 return -EINVAL;
768 }
769}
770
771static int vidioc_queryctrl(struct file *file, void *priv,
772 struct v4l2_queryctrl *c)
773{
774 struct saa7164_vbi_fh *fh = priv;
775 struct saa7164_port *port = fh->port;
776 int i, next;
777 u32 id = c->id;
778
779 memset(c, 0, sizeof(*c));
780
781 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
782 c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
783
784 for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
785 if (next) {
786 if (c->id < saa7164_v4l2_ctrls[i])
787 c->id = saa7164_v4l2_ctrls[i];
788 else
789 continue;
790 }
791
792 if (c->id == saa7164_v4l2_ctrls[i])
793 return fill_queryctrl(&port->vbi_params, c);
794
795 if (c->id < saa7164_v4l2_ctrls[i])
796 break;
797 }
798
799 return -EINVAL;
800}
801
802static int saa7164_vbi_stop_port(struct saa7164_port *port)
803{
804 struct saa7164_dev *dev = port->dev;
805 int ret;
806
807 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
808 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
809 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
810 __func__, ret);
811 ret = -EIO;
812 } else {
813 dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
814 ret = 0;
815 }
816
817 return ret;
818}
819
820static int saa7164_vbi_acquire_port(struct saa7164_port *port)
821{
822 struct saa7164_dev *dev = port->dev;
823 int ret;
824
825 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
826 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
827 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
828 __func__, ret);
829 ret = -EIO;
830 } else {
831 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
832 ret = 0;
833 }
834
835 return ret;
836}
837
838static int saa7164_vbi_pause_port(struct saa7164_port *port)
839{
840 struct saa7164_dev *dev = port->dev;
841 int ret;
842
843 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
844 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
845 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
846 __func__, ret);
847 ret = -EIO;
848 } else {
849 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
850 ret = 0;
851 }
852
853 return ret;
854}
855
856/* Firmware is very windows centric, meaning you have to transition
857 * the part through AVStream / KS Windows stages, forwards or backwards.
858 * States are: stopped, acquired (h/w), paused, started.
859 * We have to leave here will all of the soft buffers on the free list,
860 * else the cfg_post() func won't have soft buffers to correctly configure.
861 */
862static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
863{
864 struct saa7164_dev *dev = port->dev;
865 struct saa7164_buffer *buf;
866 struct saa7164_user_buffer *ubuf;
867 struct list_head *c, *n;
868 int ret;
869
870 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
871
872 ret = saa7164_vbi_pause_port(port);
873 ret = saa7164_vbi_acquire_port(port);
874 ret = saa7164_vbi_stop_port(port);
875
876 dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
877 port->nr);
878
879 /* Reset the state of any allocated buffer resources */
880 mutex_lock(&port->dmaqueue_lock);
881
882 /* Reset the hard and soft buffer state */
883 list_for_each_safe(c, n, &port->dmaqueue.list) {
884 buf = list_entry(c, struct saa7164_buffer, list);
885 buf->flags = SAA7164_BUFFER_FREE;
886 buf->pos = 0;
887 }
888
889 list_for_each_safe(c, n, &port->list_buf_used.list) {
890 ubuf = list_entry(c, struct saa7164_user_buffer, list);
891 ubuf->pos = 0;
892 list_move_tail(&ubuf->list, &port->list_buf_free.list);
893 }
894
895 mutex_unlock(&port->dmaqueue_lock);
896
897 /* Free any allocated resources */
898 saa7164_vbi_buffers_dealloc(port);
899
900 dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
901
902 return ret;
903}
904
905static int saa7164_vbi_start_streaming(struct saa7164_port *port)
906{
907 struct saa7164_dev *dev = port->dev;
908 int result, ret = 0;
909
910 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
911
912 port->done_first_interrupt = 0;
913
914 /* allocate all of the PCIe DMA buffer resources on the fly,
915 * allowing switching between TS and PS payloads without
916 * requiring a complete driver reload.
917 */
918 saa7164_vbi_buffers_alloc(port);
919
920 /* Configure the encoder with any cache values */
921#if 0
922 saa7164_api_set_encoder(port);
923 saa7164_api_get_encoder(port);
924#endif
925
926 /* Place the empty buffers on the hardware */
927 saa7164_buffer_cfg_port(port);
928
929 /* Negotiate format */
930 if (saa7164_api_set_vbi_format(port) != SAA_OK) {
931 printk(KERN_ERR "%s() No supported VBI format\n", __func__);
932 ret = -EIO;
933 goto out;
934 }
935
936 /* Acquire the hardware */
937 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
938 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
939 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
940 __func__, result);
941
942 ret = -EIO;
943 goto out;
944 } else
945 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
946
947 /* Pause the hardware */
948 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
949 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
950 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
951 __func__, result);
952
953 /* Stop the hardware, regardless */
954 result = saa7164_vbi_stop_port(port);
955 if (result != SAA_OK) {
956 printk(KERN_ERR "%s() pause/forced stop transition "
957 "failed, res = 0x%x\n", __func__, result);
958 }
959
960 ret = -EIO;
961 goto out;
962 } else
963 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
964
965 /* Start the hardware */
966 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
967 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
968 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
969 __func__, result);
970
971 /* Stop the hardware, regardless */
972 result = saa7164_vbi_acquire_port(port);
973 result = saa7164_vbi_stop_port(port);
974 if (result != SAA_OK) {
975 printk(KERN_ERR "%s() run/forced stop transition "
976 "failed, res = 0x%x\n", __func__, result);
977 }
978
979 ret = -EIO;
980 } else
981 dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
982
983out:
984 return ret;
985}
986
987int saa7164_vbi_fmt(struct file *file, void *priv, struct v4l2_format *f)
988{
989 /* ntsc */
990 f->fmt.vbi.samples_per_line = 1600;
991 f->fmt.vbi.samples_per_line = 1440;
992 f->fmt.vbi.sampling_rate = 27000000;
993 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
994 f->fmt.vbi.offset = 0;
995 f->fmt.vbi.flags = 0;
996 f->fmt.vbi.start[0] = 10;
997 f->fmt.vbi.count[0] = 18;
998 f->fmt.vbi.start[1] = 263 + 10 + 1;
999 f->fmt.vbi.count[1] = 18;
1000 return 0;
1001}
1002
1003static int fops_open(struct file *file)
1004{
1005 struct saa7164_dev *dev;
1006 struct saa7164_port *port;
1007 struct saa7164_vbi_fh *fh;
1008
1009 port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
1010 if (!port)
1011 return -ENODEV;
1012
1013 dev = port->dev;
1014
1015 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1016
1017 /* allocate + initialize per filehandle data */
1018 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1019 if (NULL == fh)
1020 return -ENOMEM;
1021
1022 file->private_data = fh;
1023 fh->port = port;
1024
1025 return 0;
1026}
1027
1028static int fops_release(struct file *file)
1029{
1030 struct saa7164_vbi_fh *fh = file->private_data;
1031 struct saa7164_port *port = fh->port;
1032 struct saa7164_dev *dev = port->dev;
1033
1034 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1035
1036 /* Shut device down on last close */
1037 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1038 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
1039 /* stop vbi capture then cancel buffers */
1040 saa7164_vbi_stop_streaming(port);
1041 }
1042 }
1043
1044 file->private_data = NULL;
1045 kfree(fh);
1046
1047 return 0;
1048}
1049
1050struct saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
1051{
1052 struct saa7164_user_buffer *ubuf = NULL;
1053 struct saa7164_dev *dev = port->dev;
1054 u32 crc;
1055
1056 mutex_lock(&port->dmaqueue_lock);
1057 if (!list_empty(&port->list_buf_used.list)) {
1058 ubuf = list_first_entry(&port->list_buf_used.list,
1059 struct saa7164_user_buffer, list);
1060
1061 if (crc_checking) {
1062 crc = crc32(0, ubuf->data, ubuf->actual_size);
1063 if (crc != ubuf->crc) {
1064 printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
1065 __func__,
1066 ubuf, ubuf->crc, crc);
1067 }
1068 }
1069
1070 }
1071 mutex_unlock(&port->dmaqueue_lock);
1072
1073 dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
1074
1075 return ubuf;
1076}
1077
1078static ssize_t fops_read(struct file *file, char __user *buffer,
1079 size_t count, loff_t *pos)
1080{
1081 struct saa7164_vbi_fh *fh = file->private_data;
1082 struct saa7164_port *port = fh->port;
1083 struct saa7164_user_buffer *ubuf = NULL;
1084 struct saa7164_dev *dev = port->dev;
1085 int ret = 0;
1086 int rem, cnt;
1087 u8 *p;
1088
1089 port->last_read_msecs_diff = port->last_read_msecs;
1090 port->last_read_msecs = jiffies_to_msecs(jiffies);
1091 port->last_read_msecs_diff = port->last_read_msecs -
1092 port->last_read_msecs_diff;
1093
1094 saa7164_histogram_update(&port->read_interval,
1095 port->last_read_msecs_diff);
1096
1097 if (*pos) {
1098 printk(KERN_ERR "%s() ESPIPE\n", __func__);
1099 return -ESPIPE;
1100 }
1101
1102 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1103 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1104
1105 if (saa7164_vbi_initialize(port) < 0) {
1106 printk(KERN_ERR "%s() EINVAL\n", __func__);
1107 return -EINVAL;
1108 }
1109
1110 saa7164_vbi_start_streaming(port);
1111 msleep(200);
1112 }
1113 }
1114
1115 /* blocking wait for buffer */
1116 if ((file->f_flags & O_NONBLOCK) == 0) {
1117 if (wait_event_interruptible(port->wait_read,
1118 saa7164_vbi_next_buf(port))) {
1119 printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
1120 return -ERESTARTSYS;
1121 }
1122 }
1123
1124 /* Pull the first buffer from the used list */
1125 ubuf = saa7164_vbi_next_buf(port);
1126
1127 while ((count > 0) && ubuf) {
1128
1129 /* set remaining bytes to copy */
1130 rem = ubuf->actual_size - ubuf->pos;
1131 cnt = rem > count ? count : rem;
1132
1133 p = ubuf->data + ubuf->pos;
1134
1135 dprintk(DBGLVL_VBI,
1136 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1137 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1138
1139 if (copy_to_user(buffer, p, cnt)) {
1140 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1141 if (!ret) {
1142 printk(KERN_ERR "%s() EFAULT\n", __func__);
1143 ret = -EFAULT;
1144 }
1145 goto err;
1146 }
1147
1148 ubuf->pos += cnt;
1149 count -= cnt;
1150 buffer += cnt;
1151 ret += cnt;
1152
1153 if (ubuf->pos > ubuf->actual_size)
1154 printk(KERN_ERR "read() pos > actual, huh?\n");
1155
1156 if (ubuf->pos == ubuf->actual_size) {
1157
1158 /* finished with current buffer, take next buffer */
1159
1160 /* Requeue the buffer on the free list */
1161 ubuf->pos = 0;
1162
1163 mutex_lock(&port->dmaqueue_lock);
1164 list_move_tail(&ubuf->list, &port->list_buf_free.list);
1165 mutex_unlock(&port->dmaqueue_lock);
1166
1167 /* Dequeue next */
1168 if ((file->f_flags & O_NONBLOCK) == 0) {
1169 if (wait_event_interruptible(port->wait_read,
1170 saa7164_vbi_next_buf(port))) {
1171 break;
1172 }
1173 }
1174 ubuf = saa7164_vbi_next_buf(port);
1175 }
1176 }
1177err:
1178 if (!ret && !ubuf) {
1179 printk(KERN_ERR "%s() EAGAIN\n", __func__);
1180 ret = -EAGAIN;
1181 }
1182
1183 return ret;
1184}
1185
1186static unsigned int fops_poll(struct file *file, poll_table *wait)
1187{
1188 struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
1189 struct saa7164_port *port = fh->port;
1190 unsigned int mask = 0;
1191
1192 port->last_poll_msecs_diff = port->last_poll_msecs;
1193 port->last_poll_msecs = jiffies_to_msecs(jiffies);
1194 port->last_poll_msecs_diff = port->last_poll_msecs -
1195 port->last_poll_msecs_diff;
1196
1197 saa7164_histogram_update(&port->poll_interval,
1198 port->last_poll_msecs_diff);
1199
1200 if (!video_is_registered(port->v4l_device))
1201 return -EIO;
1202
1203 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1204 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1205 if (saa7164_vbi_initialize(port) < 0)
1206 return -EINVAL;
1207 saa7164_vbi_start_streaming(port);
1208 msleep(200);
1209 }
1210 }
1211
1212 /* blocking wait for buffer */
1213 if ((file->f_flags & O_NONBLOCK) == 0) {
1214 if (wait_event_interruptible(port->wait_read,
1215 saa7164_vbi_next_buf(port))) {
1216 return -ERESTARTSYS;
1217 }
1218 }
1219
1220 /* Pull the first buffer from the used list */
1221 if (!list_empty(&port->list_buf_used.list))
1222 mask |= POLLIN | POLLRDNORM;
1223
1224 return mask;
1225}
1226static const struct v4l2_file_operations vbi_fops = {
1227 .owner = THIS_MODULE,
1228 .open = fops_open,
1229 .release = fops_release,
1230 .read = fops_read,
1231 .poll = fops_poll,
1232 .unlocked_ioctl = video_ioctl2,
1233};
1234
1235static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1236 .vidioc_s_std = vidioc_s_std,
1237 .vidioc_enum_input = vidioc_enum_input,
1238 .vidioc_g_input = vidioc_g_input,
1239 .vidioc_s_input = vidioc_s_input,
1240 .vidioc_g_tuner = vidioc_g_tuner,
1241 .vidioc_s_tuner = vidioc_s_tuner,
1242 .vidioc_g_frequency = vidioc_g_frequency,
1243 .vidioc_s_frequency = vidioc_s_frequency,
1244 .vidioc_s_ctrl = vidioc_s_ctrl,
1245 .vidioc_g_ctrl = vidioc_g_ctrl,
1246 .vidioc_querycap = vidioc_querycap,
1247 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1248 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1249 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1250 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1251 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1252 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1253 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1254 .vidioc_queryctrl = vidioc_queryctrl,
1255#if 0
1256 .vidioc_g_chip_ident = saa7164_g_chip_ident,
1257#endif
1258#ifdef CONFIG_VIDEO_ADV_DEBUG
1259#if 0
1260 .vidioc_g_register = saa7164_g_register,
1261 .vidioc_s_register = saa7164_s_register,
1262#endif
1263#endif
1264 .vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
1265 .vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
1266 .vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
1267};
1268
1269static struct video_device saa7164_vbi_template = {
1270 .name = "saa7164",
1271 .fops = &vbi_fops,
1272 .ioctl_ops = &vbi_ioctl_ops,
1273 .minor = -1,
1274 .tvnorms = SAA7164_NORMS,
1275 .current_norm = V4L2_STD_NTSC_M,
1276};
1277
1278static struct video_device *saa7164_vbi_alloc(
1279 struct saa7164_port *port,
1280 struct pci_dev *pci,
1281 struct video_device *template,
1282 char *type)
1283{
1284 struct video_device *vfd;
1285 struct saa7164_dev *dev = port->dev;
1286
1287 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1288
1289 vfd = video_device_alloc();
1290 if (NULL == vfd)
1291 return NULL;
1292
1293 *vfd = *template;
1294 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1295 type, saa7164_boards[dev->board].name);
1296
1297 vfd->parent = &pci->dev;
1298 vfd->release = video_device_release;
1299 return vfd;
1300}
1301
1302int saa7164_vbi_register(struct saa7164_port *port)
1303{
1304 struct saa7164_dev *dev = port->dev;
1305 int result = -ENODEV;
1306
1307 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1308
1309 if (port->type != SAA7164_MPEG_VBI)
1310 BUG();
1311
1312 /* Sanity check that the PCI configuration space is active */
1313 if (port->hwcfg.BARLocation == 0) {
1314 printk(KERN_ERR "%s() failed "
1315 "(errno = %d), NO PCI configuration\n",
1316 __func__, result);
1317 result = -ENOMEM;
1318 goto failed;
1319 }
1320
1321 /* Establish VBI defaults here */
1322
1323 /* Allocate and register the video device node */
1324 port->v4l_device = saa7164_vbi_alloc(port,
1325 dev->pci, &saa7164_vbi_template, "vbi");
1326
1327 if (!port->v4l_device) {
1328 printk(KERN_INFO "%s: can't allocate vbi device\n",
1329 dev->name);
1330 result = -ENOMEM;
1331 goto failed;
1332 }
1333
1334 video_set_drvdata(port->v4l_device, port);
1335 result = video_register_device(port->v4l_device,
1336 VFL_TYPE_VBI, -1);
1337 if (result < 0) {
1338 printk(KERN_INFO "%s: can't register vbi device\n",
1339 dev->name);
1340 /* TODO: We're going to leak here if we don't dealloc
1341 The buffers above. The unreg function can't deal wit it.
1342 */
1343 goto failed;
1344 }
1345
1346 printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
1347 dev->name, port->v4l_device->num);
1348
1349 /* Configure the hardware defaults */
1350
1351 result = 0;
1352failed:
1353 return result;
1354}
1355
1356void saa7164_vbi_unregister(struct saa7164_port *port)
1357{
1358 struct saa7164_dev *dev = port->dev;
1359
1360 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
1361
1362 if (port->type != SAA7164_MPEG_VBI)
1363 BUG();
1364
1365 if (port->v4l_device) {
1366 if (port->v4l_device->minor != -1)
1367 video_unregister_device(port->v4l_device);
1368 else
1369 video_device_release(port->v4l_device);
1370
1371 port->v4l_device = NULL;
1372 }
1373
1374}
diff --git a/drivers/media/pci/saa7164/saa7164.h b/drivers/media/pci/saa7164/saa7164.h
new file mode 100644
index 000000000000..437284e747c9
--- /dev/null
+++ b/drivers/media/pci/saa7164/saa7164.h
@@ -0,0 +1,616 @@
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/*
23 Driver architecture
24 *******************
25
26 saa7164_core.c/buffer.c/cards.c/i2c.c/dvb.c
27 | : Standard Linux driver framework for creating
28 | : exposing and managing interfaces to the rest
29 | : of the kernel or userland. Also uses _fw.c to load
30 | : firmware direct into the PCIe bus, bypassing layers.
31 V
32 saa7164_api..() : Translate kernel specific functions/features
33 | : into command buffers.
34 V
35 saa7164_cmd..() : Manages the flow of command packets on/off,
36 | : the bus. Deal with bus errors, timeouts etc.
37 V
38 saa7164_bus..() : Manage a read/write memory ring buffer in the
39 | : PCIe Address space.
40 |
41 | saa7164_fw...() : Load any frimware
42 | | : direct into the device
43 V V
44 <- ----------------- PCIe address space -------------------- ->
45*/
46
47#include <linux/pci.h>
48#include <linux/i2c.h>
49#include <linux/kdev_t.h>
50#include <linux/mutex.h>
51#include <linux/crc32.h>
52#include <linux/kthread.h>
53#include <linux/freezer.h>
54
55#include <media/tuner.h>
56#include <media/tveeprom.h>
57#include <media/videobuf-dma-sg.h>
58#include <media/videobuf-dvb.h>
59#include <dvb_demux.h>
60#include <dvb_frontend.h>
61#include <dvb_net.h>
62#include <dvbdev.h>
63#include <dmxdev.h>
64#include <media/v4l2-common.h>
65#include <media/v4l2-ioctl.h>
66#include <media/v4l2-chip-ident.h>
67
68#include "saa7164-reg.h"
69#include "saa7164-types.h"
70
71#define SAA7164_MAXBOARDS 8
72
73#define UNSET (-1U)
74#define SAA7164_BOARD_NOAUTO UNSET
75#define SAA7164_BOARD_UNKNOWN 0
76#define SAA7164_BOARD_UNKNOWN_REV2 1
77#define SAA7164_BOARD_UNKNOWN_REV3 2
78#define SAA7164_BOARD_HAUPPAUGE_HVR2250 3
79#define SAA7164_BOARD_HAUPPAUGE_HVR2200 4
80#define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5
81#define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6
82#define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7
83#define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
84#define SAA7164_BOARD_HAUPPAUGE_HVR2200_4 9
85#define SAA7164_BOARD_HAUPPAUGE_HVR2200_5 10
86
87#define SAA7164_MAX_UNITS 8
88#define SAA7164_TS_NUMBER_OF_LINES 312
89#define SAA7164_PS_NUMBER_OF_LINES 256
90#define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
91#define SAA7164_MAX_ENCODER_BUFFERS 64 /* max 5secs of latency at 6Mbps */
92#define SAA7164_MAX_VBI_BUFFERS 64
93
94/* Port related defines */
95#define SAA7164_PORT_TS1 (0)
96#define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
97#define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
98#define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
99#define SAA7164_PORT_VBI1 (SAA7164_PORT_ENC2 + 1)
100#define SAA7164_PORT_VBI2 (SAA7164_PORT_VBI1 + 1)
101#define SAA7164_MAX_PORTS (SAA7164_PORT_VBI2 + 1)
102
103#define DBGLVL_FW 4
104#define DBGLVL_DVB 8
105#define DBGLVL_I2C 16
106#define DBGLVL_API 32
107#define DBGLVL_CMD 64
108#define DBGLVL_BUS 128
109#define DBGLVL_IRQ 256
110#define DBGLVL_BUF 512
111#define DBGLVL_ENC 1024
112#define DBGLVL_VBI 2048
113#define DBGLVL_THR 4096
114#define DBGLVL_CPU 8192
115
116#define SAA7164_NORMS \
117 (V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_443)
118
119enum port_t {
120 SAA7164_MPEG_UNDEFINED = 0,
121 SAA7164_MPEG_DVB,
122 SAA7164_MPEG_ENCODER,
123 SAA7164_MPEG_VBI,
124};
125
126enum saa7164_i2c_bus_nr {
127 SAA7164_I2C_BUS_0 = 0,
128 SAA7164_I2C_BUS_1,
129 SAA7164_I2C_BUS_2,
130};
131
132enum saa7164_buffer_flags {
133 SAA7164_BUFFER_UNDEFINED = 0,
134 SAA7164_BUFFER_FREE,
135 SAA7164_BUFFER_BUSY,
136 SAA7164_BUFFER_FULL
137};
138
139enum saa7164_unit_type {
140 SAA7164_UNIT_UNDEFINED = 0,
141 SAA7164_UNIT_DIGITAL_DEMODULATOR,
142 SAA7164_UNIT_ANALOG_DEMODULATOR,
143 SAA7164_UNIT_TUNER,
144 SAA7164_UNIT_EEPROM,
145 SAA7164_UNIT_ZILOG_IRBLASTER,
146 SAA7164_UNIT_ENCODER,
147};
148
149/* The PCIe bridge doesn't grant direct access to i2c.
150 * Instead, you address i2c devices using a uniqely
151 * allocated 'unitid' value via a messaging API. This
152 * is a problem. The kernel and existing demod/tuner
153 * drivers expect to talk 'i2c', so we have to maintain
154 * a translation layer, and a series of functions to
155 * convert i2c bus + device address into a unit id.
156 */
157struct saa7164_unit {
158 enum saa7164_unit_type type;
159 u8 id;
160 char *name;
161 enum saa7164_i2c_bus_nr i2c_bus_nr;
162 u8 i2c_bus_addr;
163 u8 i2c_reg_len;
164};
165
166struct saa7164_board {
167 char *name;
168 enum port_t porta, portb, portc,
169 portd, porte, portf;
170 enum {
171 SAA7164_CHIP_UNDEFINED = 0,
172 SAA7164_CHIP_REV2,
173 SAA7164_CHIP_REV3,
174 } chiprev;
175 struct saa7164_unit unit[SAA7164_MAX_UNITS];
176};
177
178struct saa7164_subid {
179 u16 subvendor;
180 u16 subdevice;
181 u32 card;
182};
183
184struct saa7164_encoder_fh {
185 struct saa7164_port *port;
186 atomic_t v4l_reading;
187};
188
189struct saa7164_vbi_fh {
190 struct saa7164_port *port;
191 atomic_t v4l_reading;
192};
193
194struct saa7164_histogram_bucket {
195 u32 val;
196 u32 count;
197 u64 update_time;
198};
199
200struct saa7164_histogram {
201 char name[32];
202 struct saa7164_histogram_bucket counter1[64];
203};
204
205struct saa7164_user_buffer {
206 struct list_head list;
207
208 /* Attributes */
209 u8 *data;
210 u32 pos;
211 u32 actual_size;
212
213 u32 crc;
214};
215
216struct saa7164_fw_status {
217
218 /* RISC Core details */
219 u32 status;
220 u32 mode;
221 u32 spec;
222 u32 inst;
223 u32 cpuload;
224 u32 remainheap;
225
226 /* Firmware version */
227 u32 version;
228 u32 major;
229 u32 sub;
230 u32 rel;
231 u32 buildnr;
232};
233
234struct saa7164_dvb {
235 struct mutex lock;
236 struct dvb_adapter adapter;
237 struct dvb_frontend *frontend;
238 struct dvb_demux demux;
239 struct dmxdev dmxdev;
240 struct dmx_frontend fe_hw;
241 struct dmx_frontend fe_mem;
242 struct dvb_net net;
243 int feeding;
244};
245
246struct saa7164_i2c {
247 struct saa7164_dev *dev;
248
249 enum saa7164_i2c_bus_nr nr;
250
251 /* I2C I/O */
252 struct i2c_adapter i2c_adap;
253 struct i2c_client i2c_client;
254 u32 i2c_rc;
255};
256
257struct saa7164_ctrl {
258 struct v4l2_queryctrl v;
259};
260
261struct saa7164_tvnorm {
262 char *name;
263 v4l2_std_id id;
264};
265
266struct saa7164_encoder_params {
267 struct saa7164_tvnorm encodernorm;
268 u32 height;
269 u32 width;
270 u32 is_50hz;
271 u32 bitrate; /* bps */
272 u32 bitrate_peak; /* bps */
273 u32 bitrate_mode;
274 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
275
276 u32 audio_sampling_freq;
277 u32 ctl_mute;
278 u32 ctl_aspect;
279 u32 refdist;
280 u32 gop_size;
281};
282
283struct saa7164_vbi_params {
284 struct saa7164_tvnorm encodernorm;
285 u32 height;
286 u32 width;
287 u32 is_50hz;
288 u32 bitrate; /* bps */
289 u32 bitrate_peak; /* bps */
290 u32 bitrate_mode;
291 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
292
293 u32 audio_sampling_freq;
294 u32 ctl_mute;
295 u32 ctl_aspect;
296 u32 refdist;
297 u32 gop_size;
298};
299
300struct saa7164_port;
301
302struct saa7164_buffer {
303 struct list_head list;
304
305 /* Note of which h/w buffer list index position we occupy */
306 int idx;
307
308 struct saa7164_port *port;
309
310 /* Hardware Specific */
311 /* PCI Memory allocations */
312 enum saa7164_buffer_flags flags; /* Free, Busy, Full */
313
314 /* A block of page align PCI memory */
315 u32 pci_size; /* PCI allocation size in bytes */
316 u64 __iomem *cpu; /* Virtual address */
317 dma_addr_t dma; /* Physical address */
318 u32 crc; /* Checksum for the entire buffer data */
319
320 /* A page table that splits the block into a number of entries */
321 u32 pt_size; /* PCI allocation size in bytes */
322 u64 __iomem *pt_cpu; /* Virtual address */
323 dma_addr_t pt_dma; /* Physical address */
324
325 /* Encoder fops */
326 u32 pos;
327 u32 actual_size;
328};
329
330struct saa7164_port {
331
332 struct saa7164_dev *dev;
333 enum port_t type;
334 int nr;
335
336 /* --- Generic port attributes --- */
337
338 /* HW stream parameters */
339 struct tmHWStreamParameters hw_streamingparams;
340
341 /* DMA configuration values, is seeded during initialization */
342 struct tmComResDMATermDescrHeader hwcfg;
343
344 /* hardware specific registers */
345 u32 bufcounter;
346 u32 pitch;
347 u32 bufsize;
348 u32 bufoffset;
349 u32 bufptr32l;
350 u32 bufptr32h;
351 u64 bufptr64;
352
353 u32 numpte; /* Number of entries in array, only valid in head */
354
355 struct mutex dmaqueue_lock;
356 struct saa7164_buffer dmaqueue;
357
358 u64 last_irq_msecs, last_svc_msecs;
359 u64 last_irq_msecs_diff, last_svc_msecs_diff;
360 u32 last_svc_wp;
361 u32 last_svc_rp;
362 u64 last_irq_svc_msecs_diff;
363 u64 last_read_msecs, last_read_msecs_diff;
364 u64 last_poll_msecs, last_poll_msecs_diff;
365
366 struct saa7164_histogram irq_interval;
367 struct saa7164_histogram svc_interval;
368 struct saa7164_histogram irq_svc_interval;
369 struct saa7164_histogram read_interval;
370 struct saa7164_histogram poll_interval;
371
372 /* --- DVB Transport Specific --- */
373 struct saa7164_dvb dvb;
374
375 /* --- Encoder/V4L related attributes --- */
376 /* Encoder */
377 /* Defaults established in saa7164-encoder.c */
378 struct saa7164_tvnorm encodernorm;
379 u32 height;
380 u32 width;
381 u32 freq;
382 u32 ts_packet_size;
383 u32 ts_packet_count;
384 u8 mux_input;
385 u8 encoder_profile;
386 u8 video_format;
387 u8 audio_format;
388 u8 video_resolution;
389 u16 ctl_brightness;
390 u16 ctl_contrast;
391 u16 ctl_hue;
392 u16 ctl_saturation;
393 u16 ctl_sharpness;
394 s8 ctl_volume;
395
396 struct tmComResAFeatureDescrHeader audfeat;
397 struct tmComResEncoderDescrHeader encunit;
398 struct tmComResProcDescrHeader vidproc;
399 struct tmComResExtDevDescrHeader ifunit;
400 struct tmComResTunerDescrHeader tunerunit;
401
402 struct work_struct workenc;
403
404 /* V4L Encoder Video */
405 struct saa7164_encoder_params encoder_params;
406 struct video_device *v4l_device;
407 atomic_t v4l_reader_count;
408
409 struct saa7164_buffer list_buf_used;
410 struct saa7164_buffer list_buf_free;
411 wait_queue_head_t wait_read;
412
413 /* V4L VBI */
414 struct tmComResVBIFormatDescrHeader vbi_fmt_ntsc;
415 struct saa7164_vbi_params vbi_params;
416
417 /* Debug */
418 u32 sync_errors;
419 u32 v_cc_errors;
420 u32 a_cc_errors;
421 u8 last_v_cc;
422 u8 last_a_cc;
423 u32 done_first_interrupt;
424};
425
426struct saa7164_dev {
427 struct list_head devlist;
428 atomic_t refcount;
429
430 /* pci stuff */
431 struct pci_dev *pci;
432 unsigned char pci_rev, pci_lat;
433 int pci_bus, pci_slot;
434 u32 __iomem *lmmio;
435 u8 __iomem *bmmio;
436 u32 __iomem *lmmio2;
437 u8 __iomem *bmmio2;
438 int pci_irqmask;
439
440 /* board details */
441 int nr;
442 int hwrevision;
443 u32 board;
444 char name[16];
445
446 /* firmware status */
447 struct saa7164_fw_status fw_status;
448 u32 firmwareloaded;
449
450 struct tmComResHWDescr hwdesc;
451 struct tmComResInterfaceDescr intfdesc;
452 struct tmComResBusDescr busdesc;
453
454 struct tmComResBusInfo bus;
455
456 /* Interrupt status and ack registers */
457 u32 int_status;
458 u32 int_ack;
459
460 struct cmd cmds[SAA_CMD_MAX_MSG_UNITS];
461 struct mutex lock;
462
463 /* I2c related */
464 struct saa7164_i2c i2c_bus[3];
465
466 /* Transport related */
467 struct saa7164_port ports[SAA7164_MAX_PORTS];
468
469 /* Deferred command/api interrupts handling */
470 struct work_struct workcmd;
471
472 /* A kernel thread to monitor the firmware log, used
473 * only in debug mode.
474 */
475 struct task_struct *kthread;
476
477};
478
479extern struct list_head saa7164_devlist;
480extern unsigned int waitsecs;
481extern unsigned int encoder_buffers;
482extern unsigned int vbi_buffers;
483
484/* ----------------------------------------------------------- */
485/* saa7164-core.c */
486void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr);
487void saa7164_getfirmwarestatus(struct saa7164_dev *dev);
488u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev);
489void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val);
490
491/* ----------------------------------------------------------- */
492/* saa7164-fw.c */
493int saa7164_downloadfirmware(struct saa7164_dev *dev);
494
495/* ----------------------------------------------------------- */
496/* saa7164-i2c.c */
497extern int saa7164_i2c_register(struct saa7164_i2c *bus);
498extern int saa7164_i2c_unregister(struct saa7164_i2c *bus);
499extern void saa7164_call_i2c_clients(struct saa7164_i2c *bus,
500 unsigned int cmd, void *arg);
501
502/* ----------------------------------------------------------- */
503/* saa7164-bus.c */
504int saa7164_bus_setup(struct saa7164_dev *dev);
505void saa7164_bus_dump(struct saa7164_dev *dev);
506int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
507 void *buf);
508int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
509 void *buf, int peekonly);
510
511/* ----------------------------------------------------------- */
512/* saa7164-cmd.c */
513int saa7164_cmd_send(struct saa7164_dev *dev,
514 u8 id, enum tmComResCmd command, u16 controlselector,
515 u16 size, void *buf);
516void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno);
517int saa7164_irq_dequeue(struct saa7164_dev *dev);
518
519/* ----------------------------------------------------------- */
520/* saa7164-api.c */
521int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version);
522int saa7164_api_enum_subdevs(struct saa7164_dev *dev);
523int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
524 u32 datalen, u8 *data);
525int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr,
526 u32 datalen, u8 *data);
527int saa7164_api_dif_write(struct saa7164_i2c *bus, u8 addr,
528 u32 datalen, u8 *data);
529int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen);
530int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
531int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
532int saa7164_api_transition_port(struct saa7164_port *port, u8 mode);
533int saa7164_api_initialize_dif(struct saa7164_port *port);
534int saa7164_api_configure_dif(struct saa7164_port *port, u32 std);
535int saa7164_api_set_encoder(struct saa7164_port *port);
536int saa7164_api_get_encoder(struct saa7164_port *port);
537int saa7164_api_set_aspect_ratio(struct saa7164_port *port);
538int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl);
539int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl);
540int saa7164_api_set_videomux(struct saa7164_port *port);
541int saa7164_api_audio_mute(struct saa7164_port *port, int mute);
542int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level);
543int saa7164_api_set_audio_std(struct saa7164_port *port);
544int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect);
545int saa7164_api_get_videomux(struct saa7164_port *port);
546int saa7164_api_set_vbi_format(struct saa7164_port *port);
547int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level);
548int saa7164_api_collect_debug(struct saa7164_dev *dev);
549int saa7164_api_get_load_info(struct saa7164_dev *dev,
550 struct tmFwInfoStruct *i);
551
552/* ----------------------------------------------------------- */
553/* saa7164-cards.c */
554extern struct saa7164_board saa7164_boards[];
555extern const unsigned int saa7164_bcount;
556
557extern struct saa7164_subid saa7164_subids[];
558extern const unsigned int saa7164_idcount;
559
560extern void saa7164_card_list(struct saa7164_dev *dev);
561extern void saa7164_gpio_setup(struct saa7164_dev *dev);
562extern void saa7164_card_setup(struct saa7164_dev *dev);
563
564extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr);
565extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr);
566extern char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid);
567
568/* ----------------------------------------------------------- */
569/* saa7164-dvb.c */
570extern int saa7164_dvb_register(struct saa7164_port *port);
571extern int saa7164_dvb_unregister(struct saa7164_port *port);
572
573/* ----------------------------------------------------------- */
574/* saa7164-buffer.c */
575extern struct saa7164_buffer *saa7164_buffer_alloc(
576 struct saa7164_port *port, u32 len);
577extern int saa7164_buffer_dealloc(struct saa7164_buffer *buf);
578extern void saa7164_buffer_display(struct saa7164_buffer *buf);
579extern int saa7164_buffer_activate(struct saa7164_buffer *buf, int i);
580extern int saa7164_buffer_cfg_port(struct saa7164_port *port);
581extern struct saa7164_user_buffer *saa7164_buffer_alloc_user(
582 struct saa7164_dev *dev, u32 len);
583extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf);
584extern int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i);
585
586/* ----------------------------------------------------------- */
587/* saa7164-encoder.c */
588int saa7164_encoder_register(struct saa7164_port *port);
589void saa7164_encoder_unregister(struct saa7164_port *port);
590
591/* ----------------------------------------------------------- */
592/* saa7164-vbi.c */
593int saa7164_vbi_register(struct saa7164_port *port);
594void saa7164_vbi_unregister(struct saa7164_port *port);
595
596/* ----------------------------------------------------------- */
597
598extern unsigned int crc_checking;
599
600extern unsigned int saa_debug;
601#define dprintk(level, fmt, arg...)\
602 do { if (saa_debug & level)\
603 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
604 } while (0)
605
606#define log_warn(fmt, arg...)\
607 do { \
608 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
609 } while (0)
610
611#define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
612#define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
613
614#define saa7164_readb(reg) readl(dev->bmmio + (reg))
615#define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))
616
diff --git a/drivers/media/pci/zoran/Kconfig b/drivers/media/pci/zoran/Kconfig
new file mode 100644
index 000000000000..fd4120e4c104
--- /dev/null
+++ b/drivers/media/pci/zoran/Kconfig
@@ -0,0 +1,74 @@
1config VIDEO_ZORAN
2 tristate "Zoran ZR36057/36067 Video For Linux"
3 depends on PCI && I2C_ALGOBIT && VIDEO_V4L2 && VIRT_TO_BUS
4 help
5 Say Y for support for MJPEG capture cards based on the Zoran
6 36057/36067 PCI controller chipset. This includes the Iomega
7 Buz, Pinnacle DC10+ and the Linux Media Labs LML33. There is
8 a driver homepage at <http://mjpeg.sf.net/driver-zoran/>. For
9 more information, check <file:Documentation/video4linux/Zoran>.
10
11 To compile this driver as a module, choose M here: the
12 module will be called zr36067.
13
14config VIDEO_ZORAN_DC30
15 tristate "Pinnacle/Miro DC30(+) support"
16 depends on VIDEO_ZORAN
17 select VIDEO_ADV7175 if VIDEO_HELPER_CHIPS_AUTO
18 select VIDEO_VPX3220 if VIDEO_HELPER_CHIPS_AUTO
19 help
20 Support for the Pinnacle/Miro DC30(+) MJPEG capture/playback
21 card. This also supports really old DC10 cards based on the
22 zr36050 MJPEG codec and zr36016 VFE.
23
24config VIDEO_ZORAN_ZR36060
25 tristate "Zoran ZR36060"
26 depends on VIDEO_ZORAN
27 help
28 Say Y to support Zoran boards based on 36060 chips.
29 This includes Iomega Buz, Pinnacle DC10, Linux media Labs 33
30 and 33 R10 and AverMedia 6 boards.
31
32config VIDEO_ZORAN_BUZ
33 tristate "Iomega Buz support"
34 depends on VIDEO_ZORAN_ZR36060
35 select VIDEO_SAA711X if VIDEO_HELPER_CHIPS_AUTO
36 select VIDEO_SAA7185 if VIDEO_HELPER_CHIPS_AUTO
37 help
38 Support for the Iomega Buz MJPEG capture/playback card.
39
40config VIDEO_ZORAN_DC10
41 tristate "Pinnacle/Miro DC10(+) support"
42 depends on VIDEO_ZORAN_ZR36060
43 select VIDEO_SAA7110 if VIDEO_HELPER_CHIPS_AUTO
44 select VIDEO_ADV7175 if VIDEO_HELPER_CHIPS_AUTO
45 help
46 Support for the Pinnacle/Miro DC10(+) MJPEG capture/playback
47 card.
48
49config VIDEO_ZORAN_LML33
50 tristate "Linux Media Labs LML33 support"
51 depends on VIDEO_ZORAN_ZR36060
52 select VIDEO_BT819 if VIDEO_HELPER_CHIPS_AUTO
53 select VIDEO_BT856 if VIDEO_HELPER_CHIPS_AUTO
54 help
55 Support for the Linux Media Labs LML33 MJPEG capture/playback
56 card.
57
58config VIDEO_ZORAN_LML33R10
59 tristate "Linux Media Labs LML33R10 support"
60 depends on VIDEO_ZORAN_ZR36060
61 select VIDEO_SAA711X if VIDEO_HELPER_CHIPS_AUTO
62 select VIDEO_ADV7170 if VIDEO_HELPER_CHIPS_AUTO
63 help
64 support for the Linux Media Labs LML33R10 MJPEG capture/playback
65 card.
66
67config VIDEO_ZORAN_AVS6EYES
68 tristate "AverMedia 6 Eyes support (EXPERIMENTAL)"
69 depends on VIDEO_ZORAN_ZR36060 && EXPERIMENTAL
70 select VIDEO_BT856 if VIDEO_HELPER_CHIPS_AUTO
71 select VIDEO_BT866 if VIDEO_HELPER_CHIPS_AUTO
72 select VIDEO_KS0127 if VIDEO_HELPER_CHIPS_AUTO
73 help
74 Support for the AverMedia 6 Eyes video surveillance card.
diff --git a/drivers/media/pci/zoran/Makefile b/drivers/media/pci/zoran/Makefile
new file mode 100644
index 000000000000..44cc13352c88
--- /dev/null
+++ b/drivers/media/pci/zoran/Makefile
@@ -0,0 +1,6 @@
1zr36067-objs := zoran_procfs.o zoran_device.o \
2 zoran_driver.o zoran_card.o
3
4obj-$(CONFIG_VIDEO_ZORAN) += zr36067.o videocodec.o
5obj-$(CONFIG_VIDEO_ZORAN_DC30) += zr36050.o zr36016.o
6obj-$(CONFIG_VIDEO_ZORAN_ZR36060) += zr36060.o
diff --git a/drivers/media/pci/zoran/videocodec.c b/drivers/media/pci/zoran/videocodec.c
new file mode 100644
index 000000000000..c01071635290
--- /dev/null
+++ b/drivers/media/pci/zoran/videocodec.c
@@ -0,0 +1,407 @@
1/*
2 * VIDEO MOTION CODECs internal API for video devices
3 *
4 * Interface for MJPEG (and maybe later MPEG/WAVELETS) codec's
5 * bound to a master device.
6 *
7 * (c) 2002 Wolfgang Scherr <scherr@net4you.at>
8 *
9 * $Id: videocodec.c,v 1.1.2.8 2003/03/29 07:16:04 rbultje Exp $
10 *
11 * ------------------------------------------------------------------------
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 * ------------------------------------------------------------------------
28 */
29
30#define VIDEOCODEC_VERSION "v0.2"
31
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/slab.h>
37
38// kernel config is here (procfs flag)
39
40#ifdef CONFIG_PROC_FS
41#include <linux/proc_fs.h>
42#include <linux/seq_file.h>
43#include <asm/uaccess.h>
44#endif
45
46#include "videocodec.h"
47
48static int debug;
49module_param(debug, int, 0);
50MODULE_PARM_DESC(debug, "Debug level (0-4)");
51
52#define dprintk(num, format, args...) \
53 do { \
54 if (debug >= num) \
55 printk(format, ##args); \
56 } while (0)
57
58struct attached_list {
59 struct videocodec *codec;
60 struct attached_list *next;
61};
62
63struct codec_list {
64 const struct videocodec *codec;
65 int attached;
66 struct attached_list *list;
67 struct codec_list *next;
68};
69
70static struct codec_list *codeclist_top = NULL;
71
72/* ================================================= */
73/* function prototypes of the master/slave interface */
74/* ================================================= */
75
76struct videocodec *
77videocodec_attach (struct videocodec_master *master)
78{
79 struct codec_list *h = codeclist_top;
80 struct attached_list *a, *ptr;
81 struct videocodec *codec;
82 int res;
83
84 if (!master) {
85 dprintk(1, KERN_ERR "videocodec_attach: no data\n");
86 return NULL;
87 }
88
89 dprintk(2,
90 "videocodec_attach: '%s', flags %lx, magic %lx\n",
91 master->name, master->flags, master->magic);
92
93 if (!h) {
94 dprintk(1,
95 KERN_ERR
96 "videocodec_attach: no device available\n");
97 return NULL;
98 }
99
100 while (h) {
101 // attach only if the slave has at least the flags
102 // expected by the master
103 if ((master->flags & h->codec->flags) == master->flags) {
104 dprintk(4, "videocodec_attach: try '%s'\n",
105 h->codec->name);
106
107 if (!try_module_get(h->codec->owner))
108 return NULL;
109
110 codec = kmemdup(h->codec, sizeof(struct videocodec),
111 GFP_KERNEL);
112 if (!codec) {
113 dprintk(1,
114 KERN_ERR
115 "videocodec_attach: no mem\n");
116 goto out_module_put;
117 }
118
119 snprintf(codec->name, sizeof(codec->name),
120 "%s[%d]", codec->name, h->attached);
121 codec->master_data = master;
122 res = codec->setup(codec);
123 if (res == 0) {
124 dprintk(3, "videocodec_attach '%s'\n",
125 codec->name);
126 ptr = kzalloc(sizeof(struct attached_list), GFP_KERNEL);
127 if (!ptr) {
128 dprintk(1,
129 KERN_ERR
130 "videocodec_attach: no memory\n");
131 goto out_kfree;
132 }
133 ptr->codec = codec;
134
135 a = h->list;
136 if (!a) {
137 h->list = ptr;
138 dprintk(4,
139 "videocodec: first element\n");
140 } else {
141 while (a->next)
142 a = a->next; // find end
143 a->next = ptr;
144 dprintk(4,
145 "videocodec: in after '%s'\n",
146 h->codec->name);
147 }
148
149 h->attached += 1;
150 return codec;
151 } else {
152 kfree(codec);
153 }
154 }
155 h = h->next;
156 }
157
158 dprintk(1, KERN_ERR "videocodec_attach: no codec found!\n");
159 return NULL;
160
161 out_module_put:
162 module_put(h->codec->owner);
163 out_kfree:
164 kfree(codec);
165 return NULL;
166}
167
168int
169videocodec_detach (struct videocodec *codec)
170{
171 struct codec_list *h = codeclist_top;
172 struct attached_list *a, *prev;
173 int res;
174
175 if (!codec) {
176 dprintk(1, KERN_ERR "videocodec_detach: no data\n");
177 return -EINVAL;
178 }
179
180 dprintk(2,
181 "videocodec_detach: '%s', type: %x, flags %lx, magic %lx\n",
182 codec->name, codec->type, codec->flags, codec->magic);
183
184 if (!h) {
185 dprintk(1,
186 KERN_ERR "videocodec_detach: no device left...\n");
187 return -ENXIO;
188 }
189
190 while (h) {
191 a = h->list;
192 prev = NULL;
193 while (a) {
194 if (codec == a->codec) {
195 res = a->codec->unset(a->codec);
196 if (res >= 0) {
197 dprintk(3,
198 "videocodec_detach: '%s'\n",
199 a->codec->name);
200 a->codec->master_data = NULL;
201 } else {
202 dprintk(1,
203 KERN_ERR
204 "videocodec_detach: '%s'\n",
205 a->codec->name);
206 a->codec->master_data = NULL;
207 }
208 if (prev == NULL) {
209 h->list = a->next;
210 dprintk(4,
211 "videocodec: delete first\n");
212 } else {
213 prev->next = a->next;
214 dprintk(4,
215 "videocodec: delete middle\n");
216 }
217 module_put(a->codec->owner);
218 kfree(a->codec);
219 kfree(a);
220 h->attached -= 1;
221 return 0;
222 }
223 prev = a;
224 a = a->next;
225 }
226 h = h->next;
227 }
228
229 dprintk(1, KERN_ERR "videocodec_detach: given codec not found!\n");
230 return -EINVAL;
231}
232
233int
234videocodec_register (const struct videocodec *codec)
235{
236 struct codec_list *ptr, *h = codeclist_top;
237
238 if (!codec) {
239 dprintk(1, KERN_ERR "videocodec_register: no data!\n");
240 return -EINVAL;
241 }
242
243 dprintk(2,
244 "videocodec: register '%s', type: %x, flags %lx, magic %lx\n",
245 codec->name, codec->type, codec->flags, codec->magic);
246
247 ptr = kzalloc(sizeof(struct codec_list), GFP_KERNEL);
248 if (!ptr) {
249 dprintk(1, KERN_ERR "videocodec_register: no memory\n");
250 return -ENOMEM;
251 }
252 ptr->codec = codec;
253
254 if (!h) {
255 codeclist_top = ptr;
256 dprintk(4, "videocodec: hooked in as first element\n");
257 } else {
258 while (h->next)
259 h = h->next; // find the end
260 h->next = ptr;
261 dprintk(4, "videocodec: hooked in after '%s'\n",
262 h->codec->name);
263 }
264
265 return 0;
266}
267
268int
269videocodec_unregister (const struct videocodec *codec)
270{
271 struct codec_list *prev = NULL, *h = codeclist_top;
272
273 if (!codec) {
274 dprintk(1, KERN_ERR "videocodec_unregister: no data!\n");
275 return -EINVAL;
276 }
277
278 dprintk(2,
279 "videocodec: unregister '%s', type: %x, flags %lx, magic %lx\n",
280 codec->name, codec->type, codec->flags, codec->magic);
281
282 if (!h) {
283 dprintk(1,
284 KERN_ERR
285 "videocodec_unregister: no device left...\n");
286 return -ENXIO;
287 }
288
289 while (h) {
290 if (codec == h->codec) {
291 if (h->attached) {
292 dprintk(1,
293 KERN_ERR
294 "videocodec: '%s' is used\n",
295 h->codec->name);
296 return -EBUSY;
297 }
298 dprintk(3, "videocodec: unregister '%s' is ok.\n",
299 h->codec->name);
300 if (prev == NULL) {
301 codeclist_top = h->next;
302 dprintk(4,
303 "videocodec: delete first element\n");
304 } else {
305 prev->next = h->next;
306 dprintk(4,
307 "videocodec: delete middle element\n");
308 }
309 kfree(h);
310 return 0;
311 }
312 prev = h;
313 h = h->next;
314 }
315
316 dprintk(1,
317 KERN_ERR
318 "videocodec_unregister: given codec not found!\n");
319 return -EINVAL;
320}
321
322#ifdef CONFIG_PROC_FS
323static int proc_videocodecs_show(struct seq_file *m, void *v)
324{
325 struct codec_list *h = codeclist_top;
326 struct attached_list *a;
327
328 seq_printf(m, "<S>lave or attached <M>aster name type flags magic ");
329 seq_printf(m, "(connected as)\n");
330
331 h = codeclist_top;
332 while (h) {
333 seq_printf(m, "S %32s %04x %08lx %08lx (TEMPLATE)\n",
334 h->codec->name, h->codec->type,
335 h->codec->flags, h->codec->magic);
336 a = h->list;
337 while (a) {
338 seq_printf(m, "M %32s %04x %08lx %08lx (%s)\n",
339 a->codec->master_data->name,
340 a->codec->master_data->type,
341 a->codec->master_data->flags,
342 a->codec->master_data->magic,
343 a->codec->name);
344 a = a->next;
345 }
346 h = h->next;
347 }
348
349 return 0;
350}
351
352static int proc_videocodecs_open(struct inode *inode, struct file *file)
353{
354 return single_open(file, proc_videocodecs_show, NULL);
355}
356
357static const struct file_operations videocodecs_proc_fops = {
358 .owner = THIS_MODULE,
359 .open = proc_videocodecs_open,
360 .read = seq_read,
361 .llseek = seq_lseek,
362 .release = single_release,
363};
364#endif
365
366/* ===================== */
367/* hook in driver module */
368/* ===================== */
369static int __init
370videocodec_init (void)
371{
372#ifdef CONFIG_PROC_FS
373 static struct proc_dir_entry *videocodec_proc_entry;
374#endif
375
376 printk(KERN_INFO "Linux video codec intermediate layer: %s\n",
377 VIDEOCODEC_VERSION);
378
379#ifdef CONFIG_PROC_FS
380 videocodec_proc_entry = proc_create("videocodecs", 0, NULL, &videocodecs_proc_fops);
381 if (!videocodec_proc_entry) {
382 dprintk(1, KERN_ERR "videocodec: can't init procfs.\n");
383 }
384#endif
385 return 0;
386}
387
388static void __exit
389videocodec_exit (void)
390{
391#ifdef CONFIG_PROC_FS
392 remove_proc_entry("videocodecs", NULL);
393#endif
394}
395
396EXPORT_SYMBOL(videocodec_attach);
397EXPORT_SYMBOL(videocodec_detach);
398EXPORT_SYMBOL(videocodec_register);
399EXPORT_SYMBOL(videocodec_unregister);
400
401module_init(videocodec_init);
402module_exit(videocodec_exit);
403
404MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
405MODULE_DESCRIPTION("Intermediate API module for video codecs "
406 VIDEOCODEC_VERSION);
407MODULE_LICENSE("GPL");
diff --git a/drivers/media/pci/zoran/videocodec.h b/drivers/media/pci/zoran/videocodec.h
new file mode 100644
index 000000000000..def55585ad23
--- /dev/null
+++ b/drivers/media/pci/zoran/videocodec.h
@@ -0,0 +1,353 @@
1/*
2 * VIDEO MOTION CODECs internal API for video devices
3 *
4 * Interface for MJPEG (and maybe later MPEG/WAVELETS) codec's
5 * bound to a master device.
6 *
7 * (c) 2002 Wolfgang Scherr <scherr@net4you.at>
8 *
9 * $Id: videocodec.h,v 1.1.2.4 2003/01/14 21:15:03 rbultje Exp $
10 *
11 * ------------------------------------------------------------------------
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 * ------------------------------------------------------------------------
28 */
29
30/* =================== */
31/* general description */
32/* =================== */
33
34/* Should ease the (re-)usage of drivers supporting cards with (different)
35 video codecs. The codecs register to this module their functionality,
36 and the processors (masters) can attach to them if they fit.
37
38 The codecs are typically have a "strong" binding to their master - so I
39 don't think it makes sense to have a full blown interfacing as with e.g.
40 i2c. If you have an other opinion, let's discuss & implement it :-)))
41
42 Usage:
43
44 The slave has just to setup the videocodec structure and use two functions:
45 videocodec_register(codecdata);
46 videocodec_unregister(codecdata);
47 The best is just calling them at module (de-)initialisation.
48
49 The master sets up the structure videocodec_master and calls:
50 codecdata=videocodec_attach(master_codecdata);
51 videocodec_detach(codecdata);
52
53 The slave is called during attach/detach via functions setup previously
54 during register. At that time, the master_data pointer is set up
55 and the slave can access any io registers of the master device (in the case
56 the slave is bound to it). Otherwise it doesn't need this functions and
57 therfor they may not be initialized.
58
59 The other functions are just for convenience, as they are for sure used by
60 most/all of the codecs. The last ones may be omitted, too.
61
62 See the structure declaration below for more information and which data has
63 to be set up for the master and the slave.
64
65 ----------------------------------------------------------------------------
66 The master should have "knowledge" of the slave and vice versa. So the data
67 structures sent to/from slave via set_data/get_data set_image/get_image are
68 device dependent and vary between MJPEG/MPEG/WAVELET/... devices. (!!!!)
69 ----------------------------------------------------------------------------
70*/
71
72
73/* ========================================== */
74/* description of the videocodec_io structure */
75/* ========================================== */
76
77/*
78 ==== master setup ====
79 name -> name of the device structure for reference and debugging
80 master_data -> data ref. for the master (e.g. the zr36055,57,67)
81 readreg -> ref. to read-fn from register (setup by master, used by slave)
82 writereg -> ref. to write-fn to register (setup by master, used by slave)
83 this two functions do the lowlevel I/O job
84
85 ==== slave functionality setup ====
86 slave_data -> data ref. for the slave (e.g. the zr36050,60)
87 check -> fn-ref. checks availability of an device, returns -EIO on failure or
88 the type on success
89 this makes espcecially sense if a driver module supports more than
90 one codec which may be quite similar to access, nevertheless it
91 is good for a first functionality check
92
93 -- main functions you always need for compression/decompression --
94
95 set_mode -> this fn-ref. resets the entire codec, and sets up the mode
96 with the last defined norm/size (or device default if not
97 available) - it returns 0 if the mode is possible
98 set_size -> this fn-ref. sets the norm and image size for
99 compression/decompression (returns 0 on success)
100 the norm param is defined in videodev2.h (V4L2_STD_*)
101
102 additional setup may be available, too - but the codec should work with
103 some default values even without this
104
105 set_data -> sets device-specific data (tables, quality etc.)
106 get_data -> query device-specific data (tables, quality etc.)
107
108 if the device delivers interrupts, they may be setup/handled here
109 setup_interrupt -> codec irq setup (not needed for 36050/60)
110 handle_interrupt -> codec irq handling (not needed for 36050/60)
111
112 if the device delivers pictures, they may be handled here
113 put_image -> puts image data to the codec (not needed for 36050/60)
114 get_image -> gets image data from the codec (not needed for 36050/60)
115 the calls include frame numbers and flags (even/odd/...)
116 if needed and a flag which allows blocking until its ready
117*/
118
119/* ============== */
120/* user interface */
121/* ============== */
122
123/*
124 Currently there is only a information display planned, as the layer
125 is not visible for the user space at all.
126
127 Information is available via procfs. The current entry is "/proc/videocodecs"
128 but it makes sense to "hide" it in the /proc/video tree of v4l(2) --TODO--.
129
130A example for such an output is:
131
132<S>lave or attached <M>aster name type flags magic (connected as)
133S zr36050 0002 0000d001 00000000 (TEMPLATE)
134M zr36055[0] 0001 0000c001 00000000 (zr36050[0])
135M zr36055[1] 0001 0000c001 00000000 (zr36050[1])
136
137*/
138
139
140/* =============================================== */
141/* special defines for the videocodec_io structure */
142/* =============================================== */
143
144#ifndef __LINUX_VIDEOCODEC_H
145#define __LINUX_VIDEOCODEC_H
146
147#include <linux/videodev2.h>
148
149#define CODEC_DO_COMPRESSION 0
150#define CODEC_DO_EXPANSION 1
151
152/* this are the current codec flags I think they are needed */
153/* -> type value in structure */
154#define CODEC_FLAG_JPEG 0x00000001L // JPEG codec
155#define CODEC_FLAG_MPEG 0x00000002L // MPEG1/2/4 codec
156#define CODEC_FLAG_DIVX 0x00000004L // DIVX codec
157#define CODEC_FLAG_WAVELET 0x00000008L // WAVELET codec
158 // room for other types
159
160#define CODEC_FLAG_MAGIC 0x00000800L // magic key must match
161#define CODEC_FLAG_HARDWARE 0x00001000L // is a hardware codec
162#define CODEC_FLAG_VFE 0x00002000L // has direct video frontend
163#define CODEC_FLAG_ENCODER 0x00004000L // compression capability
164#define CODEC_FLAG_DECODER 0x00008000L // decompression capability
165#define CODEC_FLAG_NEEDIRQ 0x00010000L // needs irq handling
166#define CODEC_FLAG_RDWRPIC 0x00020000L // handles picture I/O
167
168/* a list of modes, some are just examples (is there any HW?) */
169#define CODEC_MODE_BJPG 0x0001 // Baseline JPEG
170#define CODEC_MODE_LJPG 0x0002 // Lossless JPEG
171#define CODEC_MODE_MPEG1 0x0003 // MPEG 1
172#define CODEC_MODE_MPEG2 0x0004 // MPEG 2
173#define CODEC_MODE_MPEG4 0x0005 // MPEG 4
174#define CODEC_MODE_MSDIVX 0x0006 // MS DivX
175#define CODEC_MODE_ODIVX 0x0007 // Open DivX
176#define CODEC_MODE_WAVELET 0x0008 // Wavelet
177
178/* this are the current codec types I want to implement */
179/* -> type value in structure */
180#define CODEC_TYPE_NONE 0
181#define CODEC_TYPE_L64702 1
182#define CODEC_TYPE_ZR36050 2
183#define CODEC_TYPE_ZR36016 3
184#define CODEC_TYPE_ZR36060 4
185
186/* the type of data may be enhanced by future implementations (data-fn.'s) */
187/* -> used in command */
188#define CODEC_G_STATUS 0x0000 /* codec status (query only) */
189#define CODEC_S_CODEC_MODE 0x0001 /* codec mode (baseline JPEG, MPEG1,... */
190#define CODEC_G_CODEC_MODE 0x8001
191#define CODEC_S_VFE 0x0002 /* additional video frontend setup */
192#define CODEC_G_VFE 0x8002
193#define CODEC_S_MMAP 0x0003 /* MMAP setup (if available) */
194
195#define CODEC_S_JPEG_TDS_BYTE 0x0010 /* target data size in bytes */
196#define CODEC_G_JPEG_TDS_BYTE 0x8010
197#define CODEC_S_JPEG_SCALE 0x0011 /* scaling factor for quant. tables */
198#define CODEC_G_JPEG_SCALE 0x8011
199#define CODEC_S_JPEG_HDT_DATA 0x0018 /* huffman-tables */
200#define CODEC_G_JPEG_HDT_DATA 0x8018
201#define CODEC_S_JPEG_QDT_DATA 0x0019 /* quantizing-tables */
202#define CODEC_G_JPEG_QDT_DATA 0x8019
203#define CODEC_S_JPEG_APP_DATA 0x001A /* APP marker */
204#define CODEC_G_JPEG_APP_DATA 0x801A
205#define CODEC_S_JPEG_COM_DATA 0x001B /* COM marker */
206#define CODEC_G_JPEG_COM_DATA 0x801B
207
208#define CODEC_S_PRIVATE 0x1000 /* "private" commands start here */
209#define CODEC_G_PRIVATE 0x9000
210
211#define CODEC_G_FLAG 0x8000 /* this is how 'get' is detected */
212
213/* types of transfer, directly user space or a kernel buffer (image-fn.'s) */
214/* -> used in get_image, put_image */
215#define CODEC_TRANSFER_KERNEL 0 /* use "memcopy" */
216#define CODEC_TRANSFER_USER 1 /* use "to/from_user" */
217
218
219/* ========================= */
220/* the structures itself ... */
221/* ========================= */
222
223struct vfe_polarity {
224 unsigned int vsync_pol:1;
225 unsigned int hsync_pol:1;
226 unsigned int field_pol:1;
227 unsigned int blank_pol:1;
228 unsigned int subimg_pol:1;
229 unsigned int poe_pol:1;
230 unsigned int pvalid_pol:1;
231 unsigned int vclk_pol:1;
232};
233
234struct vfe_settings {
235 __u32 x, y; /* Offsets into image */
236 __u32 width, height; /* Area to capture */
237 __u16 decimation; /* Decimation divider */
238 __u16 flags; /* Flags for capture */
239 __u16 quality; /* quality of the video */
240};
241
242struct tvnorm {
243 u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
244};
245
246struct jpeg_com_marker {
247 int len; /* number of usable bytes in data */
248 char data[60];
249};
250
251struct jpeg_app_marker {
252 int appn; /* number app segment */
253 int len; /* number of usable bytes in data */
254 char data[60];
255};
256
257struct videocodec {
258 struct module *owner;
259 /* -- filled in by slave device during register -- */
260 char name[32];
261 unsigned long magic; /* may be used for client<->master attaching */
262 unsigned long flags; /* functionality flags */
263 unsigned int type; /* codec type */
264
265 /* -- these is filled in later during master device attach -- */
266
267 struct videocodec_master *master_data;
268
269 /* -- these are filled in by the slave device during register -- */
270
271 void *data; /* private slave data */
272
273 /* attach/detach client functions (indirect call) */
274 int (*setup) (struct videocodec * codec);
275 int (*unset) (struct videocodec * codec);
276
277 /* main functions, every client needs them for sure! */
278 // set compression or decompression (or freeze, stop, standby, etc)
279 int (*set_mode) (struct videocodec * codec,
280 int mode);
281 // setup picture size and norm (for the codec's video frontend)
282 int (*set_video) (struct videocodec * codec,
283 struct tvnorm * norm,
284 struct vfe_settings * cap,
285 struct vfe_polarity * pol);
286 // other control commands, also mmap setup etc.
287 int (*control) (struct videocodec * codec,
288 int type,
289 int size,
290 void *data);
291
292 /* additional setup/query/processing (may be NULL pointer) */
293 // interrupt setup / handling (for irq's delivered by master)
294 int (*setup_interrupt) (struct videocodec * codec,
295 long mode);
296 int (*handle_interrupt) (struct videocodec * codec,
297 int source,
298 long flag);
299 // picture interface (if any)
300 long (*put_image) (struct videocodec * codec,
301 int tr_type,
302 int block,
303 long *fr_num,
304 long *flag,
305 long size,
306 void *buf);
307 long (*get_image) (struct videocodec * codec,
308 int tr_type,
309 int block,
310 long *fr_num,
311 long *flag,
312 long size,
313 void *buf);
314};
315
316struct videocodec_master {
317 /* -- filled in by master device for registration -- */
318 char name[32];
319 unsigned long magic; /* may be used for client<->master attaching */
320 unsigned long flags; /* functionality flags */
321 unsigned int type; /* master type */
322
323 void *data; /* private master data */
324
325 __u32(*readreg) (struct videocodec * codec,
326 __u16 reg);
327 void (*writereg) (struct videocodec * codec,
328 __u16 reg,
329 __u32 value);
330};
331
332
333/* ================================================= */
334/* function prototypes of the master/slave interface */
335/* ================================================= */
336
337/* attach and detach commands for the master */
338// * master structure needs to be kmalloc'ed before calling attach
339// and free'd after calling detach
340// * returns pointer on success, NULL on failure
341extern struct videocodec *videocodec_attach(struct videocodec_master *);
342// * 0 on success, <0 (errno) on failure
343extern int videocodec_detach(struct videocodec *);
344
345/* register and unregister commands for the slaves */
346// * 0 on success, <0 (errno) on failure
347extern int videocodec_register(const struct videocodec *);
348// * 0 on success, <0 (errno) on failure
349extern int videocodec_unregister(const struct videocodec *);
350
351/* the other calls are directly done via the videocodec structure! */
352
353#endif /*ifndef __LINUX_VIDEOCODEC_H */
diff --git a/drivers/media/pci/zoran/zoran.h b/drivers/media/pci/zoran/zoran.h
new file mode 100644
index 000000000000..ca2754a3cd63
--- /dev/null
+++ b/drivers/media/pci/zoran/zoran.h
@@ -0,0 +1,403 @@
1/*
2 * zoran - Iomega Buz driver
3 *
4 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
5 *
6 * based on
7 *
8 * zoran.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
9 *
10 * and
11 *
12 * bttv - Bt848 frame grabber driver
13 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
14 * & Marcus Metzler (mocm@thp.uni-koeln.de)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31#ifndef _BUZ_H_
32#define _BUZ_H_
33
34#include <media/v4l2-device.h>
35
36struct zoran_sync {
37 unsigned long frame; /* number of buffer that has been free'd */
38 unsigned long length; /* number of code bytes in buffer (capture only) */
39 unsigned long seq; /* frame sequence number */
40 struct timeval timestamp; /* timestamp */
41};
42
43
44#define ZORAN_NAME "ZORAN" /* name of the device */
45
46#define ZR_DEVNAME(zr) ((zr)->name)
47
48#define BUZ_MAX_WIDTH (zr->timing->Wa)
49#define BUZ_MAX_HEIGHT (zr->timing->Ha)
50#define BUZ_MIN_WIDTH 32 /* never display less than 32 pixels */
51#define BUZ_MIN_HEIGHT 24 /* never display less than 24 rows */
52
53#define BUZ_NUM_STAT_COM 4
54#define BUZ_MASK_STAT_COM 3
55
56#define BUZ_MAX_FRAME 256 /* Must be a power of 2 */
57#define BUZ_MASK_FRAME 255 /* Must be BUZ_MAX_FRAME-1 */
58
59#define BUZ_MAX_INPUT 16
60
61#if VIDEO_MAX_FRAME <= 32
62# define V4L_MAX_FRAME 32
63#elif VIDEO_MAX_FRAME <= 64
64# define V4L_MAX_FRAME 64
65#else
66# error "Too many video frame buffers to handle"
67#endif
68#define V4L_MASK_FRAME (V4L_MAX_FRAME - 1)
69
70#define MAX_FRAME (BUZ_MAX_FRAME > VIDEO_MAX_FRAME ? BUZ_MAX_FRAME : VIDEO_MAX_FRAME)
71
72#include "zr36057.h"
73
74enum card_type {
75 UNKNOWN = -1,
76
77 /* Pinnacle/Miro */
78 DC10_old, /* DC30 like */
79 DC10_new, /* DC10plus like */
80 DC10plus,
81 DC30,
82 DC30plus,
83
84 /* Linux Media Labs */
85 LML33,
86 LML33R10,
87
88 /* Iomega */
89 BUZ,
90
91 /* AverMedia */
92 AVS6EYES,
93
94 /* total number of cards */
95 NUM_CARDS
96};
97
98enum zoran_codec_mode {
99 BUZ_MODE_IDLE, /* nothing going on */
100 BUZ_MODE_MOTION_COMPRESS, /* grabbing frames */
101 BUZ_MODE_MOTION_DECOMPRESS, /* playing frames */
102 BUZ_MODE_STILL_COMPRESS, /* still frame conversion */
103 BUZ_MODE_STILL_DECOMPRESS /* still frame conversion */
104};
105
106enum zoran_buffer_state {
107 BUZ_STATE_USER, /* buffer is owned by application */
108 BUZ_STATE_PEND, /* buffer is queued in pend[] ready to feed to I/O */
109 BUZ_STATE_DMA, /* buffer is queued in dma[] for I/O */
110 BUZ_STATE_DONE /* buffer is ready to return to application */
111};
112
113enum zoran_map_mode {
114 ZORAN_MAP_MODE_RAW,
115 ZORAN_MAP_MODE_JPG_REC,
116#define ZORAN_MAP_MODE_JPG ZORAN_MAP_MODE_JPG_REC
117 ZORAN_MAP_MODE_JPG_PLAY,
118};
119
120enum gpio_type {
121 ZR_GPIO_JPEG_SLEEP = 0,
122 ZR_GPIO_JPEG_RESET,
123 ZR_GPIO_JPEG_FRAME,
124 ZR_GPIO_VID_DIR,
125 ZR_GPIO_VID_EN,
126 ZR_GPIO_VID_RESET,
127 ZR_GPIO_CLK_SEL1,
128 ZR_GPIO_CLK_SEL2,
129 ZR_GPIO_MAX,
130};
131
132enum gpcs_type {
133 GPCS_JPEG_RESET = 0,
134 GPCS_JPEG_START,
135 GPCS_MAX,
136};
137
138struct zoran_format {
139 char *name;
140 __u32 fourcc;
141 int colorspace;
142 int depth;
143 __u32 flags;
144 __u32 vfespfr;
145};
146/* flags */
147#define ZORAN_FORMAT_COMPRESSED 1<<0
148#define ZORAN_FORMAT_OVERLAY 1<<1
149#define ZORAN_FORMAT_CAPTURE 1<<2
150#define ZORAN_FORMAT_PLAYBACK 1<<3
151
152/* overlay-settings */
153struct zoran_overlay_settings {
154 int is_set;
155 int x, y, width, height; /* position */
156 int clipcount; /* position and number of clips */
157 const struct zoran_format *format; /* overlay format */
158};
159
160/* v4l-capture settings */
161struct zoran_v4l_settings {
162 int width, height, bytesperline; /* capture size */
163 const struct zoran_format *format; /* capture format */
164};
165
166/* jpg-capture/-playback settings */
167struct zoran_jpg_settings {
168 int decimation; /* this bit is used to set everything to default */
169 int HorDcm, VerDcm, TmpDcm; /* capture decimation settings (TmpDcm=1 means both fields) */
170 int field_per_buff, odd_even; /* field-settings (odd_even=1 (+TmpDcm=1) means top-field-first) */
171 int img_x, img_y, img_width, img_height; /* crop settings (subframe capture) */
172 struct v4l2_jpegcompression jpg_comp; /* JPEG-specific capture settings */
173};
174
175struct zoran_fh;
176
177struct zoran_mapping {
178 struct zoran_fh *fh;
179 int count;
180};
181
182struct zoran_buffer {
183 struct zoran_mapping *map;
184 enum zoran_buffer_state state; /* state: unused/pending/dma/done */
185 struct zoran_sync bs; /* DONE: info to return to application */
186 union {
187 struct {
188 __le32 *frag_tab; /* addresses of frag table */
189 u32 frag_tab_bus; /* same value cached to save time in ISR */
190 } jpg;
191 struct {
192 char *fbuffer; /* virtual address of frame buffer */
193 unsigned long fbuffer_phys;/* physical address of frame buffer */
194 unsigned long fbuffer_bus;/* bus address of frame buffer */
195 } v4l;
196 };
197};
198
199enum zoran_lock_activity {
200 ZORAN_FREE, /* free for use */
201 ZORAN_ACTIVE, /* active but unlocked */
202 ZORAN_LOCKED, /* locked */
203};
204
205/* buffer collections */
206struct zoran_buffer_col {
207 enum zoran_lock_activity active; /* feature currently in use? */
208 unsigned int num_buffers, buffer_size;
209 struct zoran_buffer buffer[MAX_FRAME]; /* buffers */
210 u8 allocated; /* Flag if buffers are allocated */
211 u8 need_contiguous; /* Flag if contiguous buffers are needed */
212 /* only applies to jpg buffers, raw buffers are always contiguous */
213};
214
215struct zoran;
216
217/* zoran_fh contains per-open() settings */
218struct zoran_fh {
219 struct zoran *zr;
220
221 enum zoran_map_mode map_mode; /* Flag which bufferset will map by next mmap() */
222
223 struct zoran_overlay_settings overlay_settings;
224 u32 *overlay_mask; /* overlay mask */
225 enum zoran_lock_activity overlay_active;/* feature currently in use? */
226
227 struct zoran_buffer_col buffers; /* buffers' info */
228
229 struct zoran_v4l_settings v4l_settings; /* structure with a lot of things to play with */
230 struct zoran_jpg_settings jpg_settings; /* structure with a lot of things to play with */
231};
232
233struct card_info {
234 enum card_type type;
235 char name[32];
236 const char *i2c_decoder; /* i2c decoder device */
237 const unsigned short *addrs_decoder;
238 const char *i2c_encoder; /* i2c encoder device */
239 const unsigned short *addrs_encoder;
240 u16 video_vfe, video_codec; /* videocodec types */
241 u16 audio_chip; /* audio type */
242
243 int inputs; /* number of video inputs */
244 struct input {
245 int muxsel;
246 char name[32];
247 } input[BUZ_MAX_INPUT];
248
249 v4l2_std_id norms;
250 struct tvnorm *tvn[3]; /* supported TV norms */
251
252 u32 jpeg_int; /* JPEG interrupt */
253 u32 vsync_int; /* VSYNC interrupt */
254 s8 gpio[ZR_GPIO_MAX];
255 u8 gpcs[GPCS_MAX];
256
257 struct vfe_polarity vfe_pol;
258 u8 gpio_pol[ZR_GPIO_MAX];
259
260 /* is the /GWS line connected? */
261 u8 gws_not_connected;
262
263 /* avs6eyes mux setting */
264 u8 input_mux;
265
266 void (*init) (struct zoran * zr);
267};
268
269struct zoran {
270 struct v4l2_device v4l2_dev;
271 struct video_device *video_dev;
272
273 struct i2c_adapter i2c_adapter; /* */
274 struct i2c_algo_bit_data i2c_algo; /* */
275 u32 i2cbr;
276
277 struct v4l2_subdev *decoder; /* video decoder sub-device */
278 struct v4l2_subdev *encoder; /* video encoder sub-device */
279
280 struct videocodec *codec; /* video codec */
281 struct videocodec *vfe; /* video front end */
282
283 struct mutex resource_lock; /* prevent evil stuff */
284 struct mutex other_lock; /* please merge with above */
285
286 u8 initialized; /* flag if zoran has been correctly initialized */
287 int user; /* number of current users */
288 struct card_info card;
289 struct tvnorm *timing;
290
291 unsigned short id; /* number of this device */
292 char name[32]; /* name of this device */
293 struct pci_dev *pci_dev; /* PCI device */
294 unsigned char revision; /* revision of zr36057 */
295 unsigned char __iomem *zr36057_mem;/* pointer to mapped IO memory */
296
297 spinlock_t spinlock; /* Spinlock */
298
299 /* Video for Linux parameters */
300 int input; /* card's norm and input */
301 v4l2_std_id norm;
302
303 /* Current buffer params */
304 void *vbuf_base;
305 int vbuf_height, vbuf_width;
306 int vbuf_depth;
307 int vbuf_bytesperline;
308
309 struct zoran_overlay_settings overlay_settings;
310 u32 *overlay_mask; /* overlay mask */
311 enum zoran_lock_activity overlay_active; /* feature currently in use? */
312
313 wait_queue_head_t v4l_capq;
314
315 int v4l_overlay_active; /* Overlay grab is activated */
316 int v4l_memgrab_active; /* Memory grab is activated */
317
318 int v4l_grab_frame; /* Frame number being currently grabbed */
319#define NO_GRAB_ACTIVE (-1)
320 unsigned long v4l_grab_seq; /* Number of frames grabbed */
321 struct zoran_v4l_settings v4l_settings; /* structure with a lot of things to play with */
322
323 /* V4L grab queue of frames pending */
324 unsigned long v4l_pend_head;
325 unsigned long v4l_pend_tail;
326 unsigned long v4l_sync_tail;
327 int v4l_pend[V4L_MAX_FRAME];
328 struct zoran_buffer_col v4l_buffers; /* V4L buffers' info */
329
330 /* Buz MJPEG parameters */
331 enum zoran_codec_mode codec_mode; /* status of codec */
332 struct zoran_jpg_settings jpg_settings; /* structure with a lot of things to play with */
333
334 wait_queue_head_t jpg_capq; /* wait here for grab to finish */
335
336 /* grab queue counts/indices, mask with BUZ_MASK_STAT_COM before using as index */
337 /* (dma_head - dma_tail) is number active in DMA, must be <= BUZ_NUM_STAT_COM */
338 /* (value & BUZ_MASK_STAT_COM) corresponds to index in stat_com table */
339 unsigned long jpg_que_head; /* Index where to put next buffer which is queued */
340 unsigned long jpg_dma_head; /* Index of next buffer which goes into stat_com */
341 unsigned long jpg_dma_tail; /* Index of last buffer in stat_com */
342 unsigned long jpg_que_tail; /* Index of last buffer in queue */
343 unsigned long jpg_seq_num; /* count of frames since grab/play started */
344 unsigned long jpg_err_seq; /* last seq_num before error */
345 unsigned long jpg_err_shift;
346 unsigned long jpg_queued_num; /* count of frames queued since grab/play started */
347
348 /* zr36057's code buffer table */
349 __le32 *stat_com; /* stat_com[i] is indexed by dma_head/tail & BUZ_MASK_STAT_COM */
350
351 /* (value & BUZ_MASK_FRAME) corresponds to index in pend[] queue */
352 int jpg_pend[BUZ_MAX_FRAME];
353
354 /* array indexed by frame number */
355 struct zoran_buffer_col jpg_buffers; /* MJPEG buffers' info */
356
357 /* Additional stuff for testing */
358#ifdef CONFIG_PROC_FS
359 struct proc_dir_entry *zoran_proc;
360#else
361 void *zoran_proc;
362#endif
363 int testing;
364 int jpeg_error;
365 int intr_counter_GIRQ1;
366 int intr_counter_GIRQ0;
367 int intr_counter_CodRepIRQ;
368 int intr_counter_JPEGRepIRQ;
369 int field_counter;
370 int IRQ1_in;
371 int IRQ1_out;
372 int JPEG_in;
373 int JPEG_out;
374 int JPEG_0;
375 int JPEG_1;
376 int END_event_missed;
377 int JPEG_missed;
378 int JPEG_error;
379 int num_errors;
380 int JPEG_max_missed;
381 int JPEG_min_missed;
382
383 u32 last_isr;
384 unsigned long frame_num;
385
386 wait_queue_head_t test_q;
387};
388
389static inline struct zoran *to_zoran(struct v4l2_device *v4l2_dev)
390{
391 return container_of(v4l2_dev, struct zoran, v4l2_dev);
392}
393
394/* There was something called _ALPHA_BUZ that used the PCI address instead of
395 * the kernel iomapped address for btread/btwrite. */
396#define btwrite(dat,adr) writel((dat), zr->zr36057_mem+(adr))
397#define btread(adr) readl(zr->zr36057_mem+(adr))
398
399#define btand(dat,adr) btwrite((dat) & btread(adr), adr)
400#define btor(dat,adr) btwrite((dat) | btread(adr), adr)
401#define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
402
403#endif
diff --git a/drivers/media/pci/zoran/zoran_card.c b/drivers/media/pci/zoran/zoran_card.c
new file mode 100644
index 000000000000..c3602d6cd48e
--- /dev/null
+++ b/drivers/media/pci/zoran/zoran_card.c
@@ -0,0 +1,1524 @@
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles card-specific data and detection
7 *
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 * Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Mailinglist <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/delay.h>
31
32#include <linux/types.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/vmalloc.h>
37#include <linux/slab.h>
38
39#include <linux/proc_fs.h>
40#include <linux/i2c.h>
41#include <linux/i2c-algo-bit.h>
42#include <linux/videodev2.h>
43#include <linux/spinlock.h>
44#include <linux/sem.h>
45#include <linux/kmod.h>
46#include <linux/wait.h>
47
48#include <linux/pci.h>
49#include <linux/interrupt.h>
50#include <linux/mutex.h>
51#include <linux/io.h>
52#include <media/v4l2-common.h>
53#include <media/bt819.h>
54
55#include "videocodec.h"
56#include "zoran.h"
57#include "zoran_card.h"
58#include "zoran_device.h"
59#include "zoran_procfs.h"
60
61extern const struct zoran_format zoran_formats[];
62
63static int card[BUZ_MAX] = { [0 ... (BUZ_MAX-1)] = -1 };
64module_param_array(card, int, NULL, 0444);
65MODULE_PARM_DESC(card, "Card type");
66
67/*
68 The video mem address of the video card.
69 The driver has a little database for some videocards
70 to determine it from there. If your video card is not in there
71 you have either to give it to the driver as a parameter
72 or set in in a VIDIOCSFBUF ioctl
73 */
74
75static unsigned long vidmem; /* default = 0 - Video memory base address */
76module_param(vidmem, ulong, 0444);
77MODULE_PARM_DESC(vidmem, "Default video memory base address");
78
79/*
80 Default input and video norm at startup of the driver.
81*/
82
83static unsigned int default_input; /* default 0 = Composite, 1 = S-Video */
84module_param(default_input, uint, 0444);
85MODULE_PARM_DESC(default_input,
86 "Default input (0=Composite, 1=S-Video, 2=Internal)");
87
88static int default_mux = 1; /* 6 Eyes input selection */
89module_param(default_mux, int, 0644);
90MODULE_PARM_DESC(default_mux,
91 "Default 6 Eyes mux setting (Input selection)");
92
93static int default_norm; /* default 0 = PAL, 1 = NTSC 2 = SECAM */
94module_param(default_norm, int, 0444);
95MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
96
97/* /dev/videoN, -1 for autodetect */
98static int video_nr[BUZ_MAX] = { [0 ... (BUZ_MAX-1)] = -1 };
99module_param_array(video_nr, int, NULL, 0444);
100MODULE_PARM_DESC(video_nr, "Video device number (-1=Auto)");
101
102int v4l_nbufs = 4;
103int v4l_bufsize = 864; /* Everybody should be able to work with this setting */
104module_param(v4l_nbufs, int, 0644);
105MODULE_PARM_DESC(v4l_nbufs, "Maximum number of V4L buffers to use");
106module_param(v4l_bufsize, int, 0644);
107MODULE_PARM_DESC(v4l_bufsize, "Maximum size per V4L buffer (in kB)");
108
109int jpg_nbufs = 32;
110int jpg_bufsize = 512; /* max size for 100% quality full-PAL frame */
111module_param(jpg_nbufs, int, 0644);
112MODULE_PARM_DESC(jpg_nbufs, "Maximum number of JPG buffers to use");
113module_param(jpg_bufsize, int, 0644);
114MODULE_PARM_DESC(jpg_bufsize, "Maximum size per JPG buffer (in kB)");
115
116int pass_through = 0; /* 1=Pass through TV signal when device is not used */
117 /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
118module_param(pass_through, int, 0644);
119MODULE_PARM_DESC(pass_through,
120 "Pass TV signal through to TV-out when idling");
121
122int zr36067_debug = 1;
123module_param_named(debug, zr36067_debug, int, 0644);
124MODULE_PARM_DESC(debug, "Debug level (0-5)");
125
126#define ZORAN_VERSION "0.10.1"
127
128MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
129MODULE_AUTHOR("Serguei Miridonov");
130MODULE_LICENSE("GPL");
131MODULE_VERSION(ZORAN_VERSION);
132
133#define ZR_DEVICE(subven, subdev, data) { \
134 .vendor = PCI_VENDOR_ID_ZORAN, .device = PCI_DEVICE_ID_ZORAN_36057, \
135 .subvendor = (subven), .subdevice = (subdev), .driver_data = (data) }
136
137static struct pci_device_id zr36067_pci_tbl[] = {
138 ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC10PLUS, DC10plus),
139 ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC30PLUS, DC30plus),
140 ZR_DEVICE(PCI_VENDOR_ID_ELECTRONICDESIGNGMBH, PCI_DEVICE_ID_LML_33R10, LML33R10),
141 ZR_DEVICE(PCI_VENDOR_ID_IOMEGA, PCI_DEVICE_ID_IOMEGA_BUZ, BUZ),
142 ZR_DEVICE(PCI_ANY_ID, PCI_ANY_ID, NUM_CARDS),
143 {0}
144};
145MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
146
147static unsigned int zoran_num; /* number of cards found */
148
149/* videocodec bus functions ZR36060 */
150static u32
151zr36060_read (struct videocodec *codec,
152 u16 reg)
153{
154 struct zoran *zr = (struct zoran *) codec->master_data->data;
155 __u32 data;
156
157 if (post_office_wait(zr)
158 || post_office_write(zr, 0, 1, reg >> 8)
159 || post_office_write(zr, 0, 2, reg & 0xff)) {
160 return -1;
161 }
162
163 data = post_office_read(zr, 0, 3) & 0xff;
164 return data;
165}
166
167static void
168zr36060_write (struct videocodec *codec,
169 u16 reg,
170 u32 val)
171{
172 struct zoran *zr = (struct zoran *) codec->master_data->data;
173
174 if (post_office_wait(zr)
175 || post_office_write(zr, 0, 1, reg >> 8)
176 || post_office_write(zr, 0, 2, reg & 0xff)) {
177 return;
178 }
179
180 post_office_write(zr, 0, 3, val & 0xff);
181}
182
183/* videocodec bus functions ZR36050 */
184static u32
185zr36050_read (struct videocodec *codec,
186 u16 reg)
187{
188 struct zoran *zr = (struct zoran *) codec->master_data->data;
189 __u32 data;
190
191 if (post_office_wait(zr)
192 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
193 return -1;
194 }
195
196 data = post_office_read(zr, 0, reg & 0x03) & 0xff; // reg. LOWBYTES + read
197 return data;
198}
199
200static void
201zr36050_write (struct videocodec *codec,
202 u16 reg,
203 u32 val)
204{
205 struct zoran *zr = (struct zoran *) codec->master_data->data;
206
207 if (post_office_wait(zr)
208 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
209 return;
210 }
211
212 post_office_write(zr, 0, reg & 0x03, val & 0xff); // reg. LOWBYTES + wr. data
213}
214
215/* videocodec bus functions ZR36016 */
216static u32
217zr36016_read (struct videocodec *codec,
218 u16 reg)
219{
220 struct zoran *zr = (struct zoran *) codec->master_data->data;
221 __u32 data;
222
223 if (post_office_wait(zr)) {
224 return -1;
225 }
226
227 data = post_office_read(zr, 2, reg & 0x03) & 0xff; // read
228 return data;
229}
230
231/* hack for in zoran_device.c */
232void
233zr36016_write (struct videocodec *codec,
234 u16 reg,
235 u32 val)
236{
237 struct zoran *zr = (struct zoran *) codec->master_data->data;
238
239 if (post_office_wait(zr)) {
240 return;
241 }
242
243 post_office_write(zr, 2, reg & 0x03, val & 0x0ff); // wr. data
244}
245
246/*
247 * Board specific information
248 */
249
250static void
251dc10_init (struct zoran *zr)
252{
253 dprintk(3, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
254
255 /* Pixel clock selection */
256 GPIO(zr, 4, 0);
257 GPIO(zr, 5, 1);
258 /* Enable the video bus sync signals */
259 GPIO(zr, 7, 0);
260}
261
262static void
263dc10plus_init (struct zoran *zr)
264{
265 dprintk(3, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
266}
267
268static void
269buz_init (struct zoran *zr)
270{
271 dprintk(3, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
272
273 /* some stuff from Iomega */
274 pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
275 pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
276 pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
277}
278
279static void
280lml33_init (struct zoran *zr)
281{
282 dprintk(3, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
283
284 GPIO(zr, 2, 1); // Set Composite input/output
285}
286
287static void
288avs6eyes_init (struct zoran *zr)
289{
290 // AverMedia 6-Eyes original driver by Christer Weinigel
291
292 // Lifted straight from Christer's old driver and
293 // modified slightly by Martin Samuelsson.
294
295 int mux = default_mux; /* 1 = BT866, 7 = VID1 */
296
297 GPIO(zr, 4, 1); /* Bt866 SLEEP on */
298 udelay(2);
299
300 GPIO(zr, 0, 1); /* ZR36060 /RESET on */
301 GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
302 GPIO(zr, 2, mux & 1); /* MUX S0 */
303 GPIO(zr, 3, 0); /* /FRAME on */
304 GPIO(zr, 4, 0); /* Bt866 SLEEP off */
305 GPIO(zr, 5, mux & 2); /* MUX S1 */
306 GPIO(zr, 6, 0); /* ? */
307 GPIO(zr, 7, mux & 4); /* MUX S2 */
308
309}
310
311static char *
312codecid_to_modulename (u16 codecid)
313{
314 char *name = NULL;
315
316 switch (codecid) {
317 case CODEC_TYPE_ZR36060:
318 name = "zr36060";
319 break;
320 case CODEC_TYPE_ZR36050:
321 name = "zr36050";
322 break;
323 case CODEC_TYPE_ZR36016:
324 name = "zr36016";
325 break;
326 }
327
328 return name;
329}
330
331// struct tvnorm {
332// u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
333// };
334
335static struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
336static struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
337static struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
338static struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
339
340static struct tvnorm f50ccir601_lml33 = { 864, 720, 75+34, 804, 625, 576, 18 };
341static struct tvnorm f60ccir601_lml33 = { 858, 720, 57+34, 788, 525, 480, 16 };
342
343/* The DC10 (57/16/50) uses VActive as HSync, so HStart must be 0 */
344static struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
345static struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
346
347/* FIXME: I cannot swap U and V in saa7114, so i do one
348 * pixel left shift in zoran (75 -> 74)
349 * (Maxim Yevtyushkin <max@linuxmedialabs.com>) */
350static struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74+54, 804, 625, 576, 18 };
351static struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56+54, 788, 525, 480, 16 };
352
353/* FIXME: The ks0127 seem incapable of swapping U and V, too, which is why I
354 * copy Maxim's left shift hack for the 6 Eyes.
355 *
356 * Christer's driver used the unshifted norms, though...
357 * /Sam */
358static struct tvnorm f50ccir601_avs6eyes = { 864, 720, 74, 804, 625, 576, 18 };
359static struct tvnorm f60ccir601_avs6eyes = { 858, 720, 56, 788, 525, 480, 16 };
360
361static const unsigned short vpx3220_addrs[] = { 0x43, 0x47, I2C_CLIENT_END };
362static const unsigned short saa7110_addrs[] = { 0x4e, 0x4f, I2C_CLIENT_END };
363static const unsigned short saa7111_addrs[] = { 0x25, 0x24, I2C_CLIENT_END };
364static const unsigned short saa7114_addrs[] = { 0x21, 0x20, I2C_CLIENT_END };
365static const unsigned short adv717x_addrs[] = { 0x6a, 0x6b, 0x2a, 0x2b, I2C_CLIENT_END };
366static const unsigned short ks0127_addrs[] = { 0x6c, 0x6d, I2C_CLIENT_END };
367static const unsigned short saa7185_addrs[] = { 0x44, I2C_CLIENT_END };
368static const unsigned short bt819_addrs[] = { 0x45, I2C_CLIENT_END };
369static const unsigned short bt856_addrs[] = { 0x44, I2C_CLIENT_END };
370static const unsigned short bt866_addrs[] = { 0x44, I2C_CLIENT_END };
371
372static struct card_info zoran_cards[NUM_CARDS] __devinitdata = {
373 {
374 .type = DC10_old,
375 .name = "DC10(old)",
376 .i2c_decoder = "vpx3220a",
377 .addrs_decoder = vpx3220_addrs,
378 .video_codec = CODEC_TYPE_ZR36050,
379 .video_vfe = CODEC_TYPE_ZR36016,
380
381 .inputs = 3,
382 .input = {
383 { 1, "Composite" },
384 { 2, "S-Video" },
385 { 0, "Internal/comp" }
386 },
387 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
388 .tvn = {
389 &f50sqpixel_dc10,
390 &f60sqpixel_dc10,
391 &f50sqpixel_dc10
392 },
393 .jpeg_int = 0,
394 .vsync_int = ZR36057_ISR_GIRQ1,
395 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
396 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
397 .gpcs = { -1, 0 },
398 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
399 .gws_not_connected = 0,
400 .input_mux = 0,
401 .init = &dc10_init,
402 }, {
403 .type = DC10_new,
404 .name = "DC10(new)",
405 .i2c_decoder = "saa7110",
406 .addrs_decoder = saa7110_addrs,
407 .i2c_encoder = "adv7175",
408 .addrs_encoder = adv717x_addrs,
409 .video_codec = CODEC_TYPE_ZR36060,
410
411 .inputs = 3,
412 .input = {
413 { 0, "Composite" },
414 { 7, "S-Video" },
415 { 5, "Internal/comp" }
416 },
417 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
418 .tvn = {
419 &f50sqpixel,
420 &f60sqpixel,
421 &f50sqpixel},
422 .jpeg_int = ZR36057_ISR_GIRQ0,
423 .vsync_int = ZR36057_ISR_GIRQ1,
424 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
425 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
426 .gpcs = { -1, 1},
427 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
428 .gws_not_connected = 0,
429 .input_mux = 0,
430 .init = &dc10plus_init,
431 }, {
432 .type = DC10plus,
433 .name = "DC10plus",
434 .i2c_decoder = "saa7110",
435 .addrs_decoder = saa7110_addrs,
436 .i2c_encoder = "adv7175",
437 .addrs_encoder = adv717x_addrs,
438 .video_codec = CODEC_TYPE_ZR36060,
439
440 .inputs = 3,
441 .input = {
442 { 0, "Composite" },
443 { 7, "S-Video" },
444 { 5, "Internal/comp" }
445 },
446 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
447 .tvn = {
448 &f50sqpixel,
449 &f60sqpixel,
450 &f50sqpixel
451 },
452 .jpeg_int = ZR36057_ISR_GIRQ0,
453 .vsync_int = ZR36057_ISR_GIRQ1,
454 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
455 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
456 .gpcs = { -1, 1 },
457 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
458 .gws_not_connected = 0,
459 .input_mux = 0,
460 .init = &dc10plus_init,
461 }, {
462 .type = DC30,
463 .name = "DC30",
464 .i2c_decoder = "vpx3220a",
465 .addrs_decoder = vpx3220_addrs,
466 .i2c_encoder = "adv7175",
467 .addrs_encoder = adv717x_addrs,
468 .video_codec = CODEC_TYPE_ZR36050,
469 .video_vfe = CODEC_TYPE_ZR36016,
470
471 .inputs = 3,
472 .input = {
473 { 1, "Composite" },
474 { 2, "S-Video" },
475 { 0, "Internal/comp" }
476 },
477 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
478 .tvn = {
479 &f50sqpixel_dc10,
480 &f60sqpixel_dc10,
481 &f50sqpixel_dc10
482 },
483 .jpeg_int = 0,
484 .vsync_int = ZR36057_ISR_GIRQ1,
485 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
486 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
487 .gpcs = { -1, 0 },
488 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
489 .gws_not_connected = 0,
490 .input_mux = 0,
491 .init = &dc10_init,
492 }, {
493 .type = DC30plus,
494 .name = "DC30plus",
495 .i2c_decoder = "vpx3220a",
496 .addrs_decoder = vpx3220_addrs,
497 .i2c_encoder = "adv7175",
498 .addrs_encoder = adv717x_addrs,
499 .video_codec = CODEC_TYPE_ZR36050,
500 .video_vfe = CODEC_TYPE_ZR36016,
501
502 .inputs = 3,
503 .input = {
504 { 1, "Composite" },
505 { 2, "S-Video" },
506 { 0, "Internal/comp" }
507 },
508 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
509 .tvn = {
510 &f50sqpixel_dc10,
511 &f60sqpixel_dc10,
512 &f50sqpixel_dc10
513 },
514 .jpeg_int = 0,
515 .vsync_int = ZR36057_ISR_GIRQ1,
516 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
517 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
518 .gpcs = { -1, 0 },
519 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
520 .gws_not_connected = 0,
521 .input_mux = 0,
522 .init = &dc10_init,
523 }, {
524 .type = LML33,
525 .name = "LML33",
526 .i2c_decoder = "bt819a",
527 .addrs_decoder = bt819_addrs,
528 .i2c_encoder = "bt856",
529 .addrs_encoder = bt856_addrs,
530 .video_codec = CODEC_TYPE_ZR36060,
531
532 .inputs = 2,
533 .input = {
534 { 0, "Composite" },
535 { 7, "S-Video" }
536 },
537 .norms = V4L2_STD_NTSC|V4L2_STD_PAL,
538 .tvn = {
539 &f50ccir601_lml33,
540 &f60ccir601_lml33,
541 NULL
542 },
543 .jpeg_int = ZR36057_ISR_GIRQ1,
544 .vsync_int = ZR36057_ISR_GIRQ0,
545 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
546 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
547 .gpcs = { 3, 1 },
548 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
549 .gws_not_connected = 1,
550 .input_mux = 0,
551 .init = &lml33_init,
552 }, {
553 .type = LML33R10,
554 .name = "LML33R10",
555 .i2c_decoder = "saa7114",
556 .addrs_decoder = saa7114_addrs,
557 .i2c_encoder = "adv7170",
558 .addrs_encoder = adv717x_addrs,
559 .video_codec = CODEC_TYPE_ZR36060,
560
561 .inputs = 2,
562 .input = {
563 { 0, "Composite" },
564 { 7, "S-Video" }
565 },
566 .norms = V4L2_STD_NTSC|V4L2_STD_PAL,
567 .tvn = {
568 &f50ccir601_lm33r10,
569 &f60ccir601_lm33r10,
570 NULL
571 },
572 .jpeg_int = ZR36057_ISR_GIRQ1,
573 .vsync_int = ZR36057_ISR_GIRQ0,
574 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
575 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
576 .gpcs = { 3, 1 },
577 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
578 .gws_not_connected = 1,
579 .input_mux = 0,
580 .init = &lml33_init,
581 }, {
582 .type = BUZ,
583 .name = "Buz",
584 .i2c_decoder = "saa7111",
585 .addrs_decoder = saa7111_addrs,
586 .i2c_encoder = "saa7185",
587 .addrs_encoder = saa7185_addrs,
588 .video_codec = CODEC_TYPE_ZR36060,
589
590 .inputs = 2,
591 .input = {
592 { 3, "Composite" },
593 { 7, "S-Video" }
594 },
595 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
596 .tvn = {
597 &f50ccir601,
598 &f60ccir601,
599 &f50ccir601
600 },
601 .jpeg_int = ZR36057_ISR_GIRQ1,
602 .vsync_int = ZR36057_ISR_GIRQ0,
603 .gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
604 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
605 .gpcs = { 3, 1 },
606 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
607 .gws_not_connected = 1,
608 .input_mux = 0,
609 .init = &buz_init,
610 }, {
611 .type = AVS6EYES,
612 .name = "6-Eyes",
613 /* AverMedia chose not to brand the 6-Eyes. Thus it
614 can't be autodetected, and requires card=x. */
615 .i2c_decoder = "ks0127",
616 .addrs_decoder = ks0127_addrs,
617 .i2c_encoder = "bt866",
618 .addrs_encoder = bt866_addrs,
619 .video_codec = CODEC_TYPE_ZR36060,
620
621 .inputs = 10,
622 .input = {
623 { 0, "Composite 1" },
624 { 1, "Composite 2" },
625 { 2, "Composite 3" },
626 { 4, "Composite 4" },
627 { 5, "Composite 5" },
628 { 6, "Composite 6" },
629 { 8, "S-Video 1" },
630 { 9, "S-Video 2" },
631 {10, "S-Video 3" },
632 {15, "YCbCr" }
633 },
634 .norms = V4L2_STD_NTSC|V4L2_STD_PAL,
635 .tvn = {
636 &f50ccir601_avs6eyes,
637 &f60ccir601_avs6eyes,
638 NULL
639 },
640 .jpeg_int = ZR36057_ISR_GIRQ1,
641 .vsync_int = ZR36057_ISR_GIRQ0,
642 .gpio = { 1, 0, 3, -1, -1, -1, -1, -1 },// Validity unknown /Sam
643 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 }, // Validity unknown /Sam
644 .gpcs = { 3, 1 }, // Validity unknown /Sam
645 .vfe_pol = { 1, 0, 0, 0, 0, 1, 0, 0 }, // Validity unknown /Sam
646 .gws_not_connected = 1,
647 .input_mux = 1,
648 .init = &avs6eyes_init,
649 }
650
651};
652
653/*
654 * I2C functions
655 */
656/* software I2C functions */
657static int
658zoran_i2c_getsda (void *data)
659{
660 struct zoran *zr = (struct zoran *) data;
661
662 return (btread(ZR36057_I2CBR) >> 1) & 1;
663}
664
665static int
666zoran_i2c_getscl (void *data)
667{
668 struct zoran *zr = (struct zoran *) data;
669
670 return btread(ZR36057_I2CBR) & 1;
671}
672
673static void
674zoran_i2c_setsda (void *data,
675 int state)
676{
677 struct zoran *zr = (struct zoran *) data;
678
679 if (state)
680 zr->i2cbr |= 2;
681 else
682 zr->i2cbr &= ~2;
683 btwrite(zr->i2cbr, ZR36057_I2CBR);
684}
685
686static void
687zoran_i2c_setscl (void *data,
688 int state)
689{
690 struct zoran *zr = (struct zoran *) data;
691
692 if (state)
693 zr->i2cbr |= 1;
694 else
695 zr->i2cbr &= ~1;
696 btwrite(zr->i2cbr, ZR36057_I2CBR);
697}
698
699static const struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
700 .setsda = zoran_i2c_setsda,
701 .setscl = zoran_i2c_setscl,
702 .getsda = zoran_i2c_getsda,
703 .getscl = zoran_i2c_getscl,
704 .udelay = 10,
705 .timeout = 100,
706};
707
708static int
709zoran_register_i2c (struct zoran *zr)
710{
711 memcpy(&zr->i2c_algo, &zoran_i2c_bit_data_template,
712 sizeof(struct i2c_algo_bit_data));
713 zr->i2c_algo.data = zr;
714 strlcpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
715 sizeof(zr->i2c_adapter.name));
716 i2c_set_adapdata(&zr->i2c_adapter, &zr->v4l2_dev);
717 zr->i2c_adapter.algo_data = &zr->i2c_algo;
718 zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
719 return i2c_bit_add_bus(&zr->i2c_adapter);
720}
721
722static void
723zoran_unregister_i2c (struct zoran *zr)
724{
725 i2c_del_adapter(&zr->i2c_adapter);
726}
727
728/* Check a zoran_params struct for correctness, insert default params */
729
730int
731zoran_check_jpg_settings (struct zoran *zr,
732 struct zoran_jpg_settings *settings,
733 int try)
734{
735 int err = 0, err0 = 0;
736
737 dprintk(4,
738 KERN_DEBUG
739 "%s: %s - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
740 ZR_DEVNAME(zr), __func__, settings->decimation, settings->HorDcm,
741 settings->VerDcm, settings->TmpDcm);
742 dprintk(4,
743 KERN_DEBUG
744 "%s: %s - x: %d, y: %d, w: %d, y: %d\n",
745 ZR_DEVNAME(zr), __func__, settings->img_x, settings->img_y,
746 settings->img_width, settings->img_height);
747 /* Check decimation, set default values for decimation = 1, 2, 4 */
748 switch (settings->decimation) {
749 case 1:
750
751 settings->HorDcm = 1;
752 settings->VerDcm = 1;
753 settings->TmpDcm = 1;
754 settings->field_per_buff = 2;
755 settings->img_x = 0;
756 settings->img_y = 0;
757 settings->img_width = BUZ_MAX_WIDTH;
758 settings->img_height = BUZ_MAX_HEIGHT / 2;
759 break;
760 case 2:
761
762 settings->HorDcm = 2;
763 settings->VerDcm = 1;
764 settings->TmpDcm = 2;
765 settings->field_per_buff = 1;
766 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
767 settings->img_y = 0;
768 settings->img_width =
769 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
770 settings->img_height = BUZ_MAX_HEIGHT / 2;
771 break;
772 case 4:
773
774 if (zr->card.type == DC10_new) {
775 dprintk(1,
776 KERN_DEBUG
777 "%s: %s - HDec by 4 is not supported on the DC10\n",
778 ZR_DEVNAME(zr), __func__);
779 err0++;
780 break;
781 }
782
783 settings->HorDcm = 4;
784 settings->VerDcm = 2;
785 settings->TmpDcm = 2;
786 settings->field_per_buff = 1;
787 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
788 settings->img_y = 0;
789 settings->img_width =
790 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
791 settings->img_height = BUZ_MAX_HEIGHT / 2;
792 break;
793 case 0:
794
795 /* We have to check the data the user has set */
796
797 if (settings->HorDcm != 1 && settings->HorDcm != 2 &&
798 (zr->card.type == DC10_new || settings->HorDcm != 4)) {
799 settings->HorDcm = clamp(settings->HorDcm, 1, 2);
800 err0++;
801 }
802 if (settings->VerDcm != 1 && settings->VerDcm != 2) {
803 settings->VerDcm = clamp(settings->VerDcm, 1, 2);
804 err0++;
805 }
806 if (settings->TmpDcm != 1 && settings->TmpDcm != 2) {
807 settings->TmpDcm = clamp(settings->TmpDcm, 1, 2);
808 err0++;
809 }
810 if (settings->field_per_buff != 1 &&
811 settings->field_per_buff != 2) {
812 settings->field_per_buff = clamp(settings->field_per_buff, 1, 2);
813 err0++;
814 }
815 if (settings->img_x < 0) {
816 settings->img_x = 0;
817 err0++;
818 }
819 if (settings->img_y < 0) {
820 settings->img_y = 0;
821 err0++;
822 }
823 if (settings->img_width < 0 || settings->img_width > BUZ_MAX_WIDTH) {
824 settings->img_width = clamp(settings->img_width, 0, (int)BUZ_MAX_WIDTH);
825 err0++;
826 }
827 if (settings->img_height < 0 || settings->img_height > BUZ_MAX_HEIGHT / 2) {
828 settings->img_height = clamp(settings->img_height, 0, BUZ_MAX_HEIGHT / 2);
829 err0++;
830 }
831 if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH) {
832 settings->img_x = BUZ_MAX_WIDTH - settings->img_width;
833 err0++;
834 }
835 if (settings->img_y + settings->img_height > BUZ_MAX_HEIGHT / 2) {
836 settings->img_y = BUZ_MAX_HEIGHT / 2 - settings->img_height;
837 err0++;
838 }
839 if (settings->img_width % (16 * settings->HorDcm) != 0) {
840 settings->img_width -= settings->img_width % (16 * settings->HorDcm);
841 if (settings->img_width == 0)
842 settings->img_width = 16 * settings->HorDcm;
843 err0++;
844 }
845 if (settings->img_height % (8 * settings->VerDcm) != 0) {
846 settings->img_height -= settings->img_height % (8 * settings->VerDcm);
847 if (settings->img_height == 0)
848 settings->img_height = 8 * settings->VerDcm;
849 err0++;
850 }
851
852 if (!try && err0) {
853 dprintk(1,
854 KERN_ERR
855 "%s: %s - error in params for decimation = 0\n",
856 ZR_DEVNAME(zr), __func__);
857 err++;
858 }
859 break;
860 default:
861 dprintk(1,
862 KERN_ERR
863 "%s: %s - decimation = %d, must be 0, 1, 2 or 4\n",
864 ZR_DEVNAME(zr), __func__, settings->decimation);
865 err++;
866 break;
867 }
868
869 if (settings->jpg_comp.quality > 100)
870 settings->jpg_comp.quality = 100;
871 if (settings->jpg_comp.quality < 5)
872 settings->jpg_comp.quality = 5;
873 if (settings->jpg_comp.APPn < 0)
874 settings->jpg_comp.APPn = 0;
875 if (settings->jpg_comp.APPn > 15)
876 settings->jpg_comp.APPn = 15;
877 if (settings->jpg_comp.APP_len < 0)
878 settings->jpg_comp.APP_len = 0;
879 if (settings->jpg_comp.APP_len > 60)
880 settings->jpg_comp.APP_len = 60;
881 if (settings->jpg_comp.COM_len < 0)
882 settings->jpg_comp.COM_len = 0;
883 if (settings->jpg_comp.COM_len > 60)
884 settings->jpg_comp.COM_len = 60;
885 if (err)
886 return -EINVAL;
887 return 0;
888}
889
890void
891zoran_open_init_params (struct zoran *zr)
892{
893 int i;
894
895 /* User must explicitly set a window */
896 zr->overlay_settings.is_set = 0;
897 zr->overlay_mask = NULL;
898 zr->overlay_active = ZORAN_FREE;
899
900 zr->v4l_memgrab_active = 0;
901 zr->v4l_overlay_active = 0;
902 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
903 zr->v4l_grab_seq = 0;
904 zr->v4l_settings.width = 192;
905 zr->v4l_settings.height = 144;
906 zr->v4l_settings.format = &zoran_formats[7]; /* YUY2 - YUV-4:2:2 packed */
907 zr->v4l_settings.bytesperline =
908 zr->v4l_settings.width *
909 ((zr->v4l_settings.format->depth + 7) / 8);
910
911 /* DMA ring stuff for V4L */
912 zr->v4l_pend_tail = 0;
913 zr->v4l_pend_head = 0;
914 zr->v4l_sync_tail = 0;
915 zr->v4l_buffers.active = ZORAN_FREE;
916 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
917 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
918 }
919 zr->v4l_buffers.allocated = 0;
920
921 for (i = 0; i < BUZ_MAX_FRAME; i++) {
922 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
923 }
924 zr->jpg_buffers.active = ZORAN_FREE;
925 zr->jpg_buffers.allocated = 0;
926 /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
927 zr->jpg_settings.decimation = 1;
928 zr->jpg_settings.jpg_comp.quality = 50; /* default compression factor 8 */
929 if (zr->card.type != BUZ)
930 zr->jpg_settings.odd_even = 1;
931 else
932 zr->jpg_settings.odd_even = 0;
933 zr->jpg_settings.jpg_comp.APPn = 0;
934 zr->jpg_settings.jpg_comp.APP_len = 0; /* No APPn marker */
935 memset(zr->jpg_settings.jpg_comp.APP_data, 0,
936 sizeof(zr->jpg_settings.jpg_comp.APP_data));
937 zr->jpg_settings.jpg_comp.COM_len = 0; /* No COM marker */
938 memset(zr->jpg_settings.jpg_comp.COM_data, 0,
939 sizeof(zr->jpg_settings.jpg_comp.COM_data));
940 zr->jpg_settings.jpg_comp.jpeg_markers =
941 V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT;
942 i = zoran_check_jpg_settings(zr, &zr->jpg_settings, 0);
943 if (i)
944 dprintk(1, KERN_ERR "%s: %s internal error\n",
945 ZR_DEVNAME(zr), __func__);
946
947 clear_interrupt_counters(zr);
948 zr->testing = 0;
949}
950
951static void __devinit
952test_interrupts (struct zoran *zr)
953{
954 DEFINE_WAIT(wait);
955 int timeout, icr;
956
957 clear_interrupt_counters(zr);
958
959 zr->testing = 1;
960 icr = btread(ZR36057_ICR);
961 btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
962 prepare_to_wait(&zr->test_q, &wait, TASK_INTERRUPTIBLE);
963 timeout = schedule_timeout(HZ);
964 finish_wait(&zr->test_q, &wait);
965 btwrite(0, ZR36057_ICR);
966 btwrite(0x78000000, ZR36057_ISR);
967 zr->testing = 0;
968 dprintk(5, KERN_INFO "%s: Testing interrupts...\n", ZR_DEVNAME(zr));
969 if (timeout) {
970 dprintk(1, ": time spent: %d\n", 1 * HZ - timeout);
971 }
972 if (zr36067_debug > 1)
973 print_interrupts(zr);
974 btwrite(icr, ZR36057_ICR);
975}
976
977static int __devinit
978zr36057_init (struct zoran *zr)
979{
980 int j, err;
981
982 dprintk(1,
983 KERN_INFO
984 "%s: %s - initializing card[%d], zr=%p\n",
985 ZR_DEVNAME(zr), __func__, zr->id, zr);
986
987 /* default setup of all parameters which will persist between opens */
988 zr->user = 0;
989
990 init_waitqueue_head(&zr->v4l_capq);
991 init_waitqueue_head(&zr->jpg_capq);
992 init_waitqueue_head(&zr->test_q);
993 zr->jpg_buffers.allocated = 0;
994 zr->v4l_buffers.allocated = 0;
995
996 zr->vbuf_base = (void *) vidmem;
997 zr->vbuf_width = 0;
998 zr->vbuf_height = 0;
999 zr->vbuf_depth = 0;
1000 zr->vbuf_bytesperline = 0;
1001
1002 /* Avoid nonsense settings from user for default input/norm */
1003 if (default_norm < 0 || default_norm > 2)
1004 default_norm = 0;
1005 if (default_norm == 0) {
1006 zr->norm = V4L2_STD_PAL;
1007 zr->timing = zr->card.tvn[0];
1008 } else if (default_norm == 1) {
1009 zr->norm = V4L2_STD_NTSC;
1010 zr->timing = zr->card.tvn[1];
1011 } else {
1012 zr->norm = V4L2_STD_SECAM;
1013 zr->timing = zr->card.tvn[2];
1014 }
1015 if (zr->timing == NULL) {
1016 dprintk(1,
1017 KERN_WARNING
1018 "%s: %s - default TV standard not supported by hardware. PAL will be used.\n",
1019 ZR_DEVNAME(zr), __func__);
1020 zr->norm = V4L2_STD_PAL;
1021 zr->timing = zr->card.tvn[0];
1022 }
1023
1024 if (default_input > zr->card.inputs-1) {
1025 dprintk(1,
1026 KERN_WARNING
1027 "%s: default_input value %d out of range (0-%d)\n",
1028 ZR_DEVNAME(zr), default_input, zr->card.inputs-1);
1029 default_input = 0;
1030 }
1031 zr->input = default_input;
1032
1033 /* default setup (will be repeated at every open) */
1034 zoran_open_init_params(zr);
1035
1036 /* allocate memory *before* doing anything to the hardware
1037 * in case allocation fails */
1038 zr->stat_com = kzalloc(BUZ_NUM_STAT_COM * 4, GFP_KERNEL);
1039 zr->video_dev = video_device_alloc();
1040 if (!zr->stat_com || !zr->video_dev) {
1041 dprintk(1,
1042 KERN_ERR
1043 "%s: %s - kmalloc (STAT_COM) failed\n",
1044 ZR_DEVNAME(zr), __func__);
1045 err = -ENOMEM;
1046 goto exit_free;
1047 }
1048 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1049 zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
1050 }
1051
1052 /*
1053 * Now add the template and register the device unit.
1054 */
1055 memcpy(zr->video_dev, &zoran_template, sizeof(zoran_template));
1056 zr->video_dev->parent = &zr->pci_dev->dev;
1057 strcpy(zr->video_dev->name, ZR_DEVNAME(zr));
1058 err = video_register_device(zr->video_dev, VFL_TYPE_GRABBER, video_nr[zr->id]);
1059 if (err < 0)
1060 goto exit_free;
1061 video_set_drvdata(zr->video_dev, zr);
1062
1063 zoran_init_hardware(zr);
1064 if (zr36067_debug > 2)
1065 detect_guest_activity(zr);
1066 test_interrupts(zr);
1067 if (!pass_through) {
1068 decoder_call(zr, video, s_stream, 0);
1069 encoder_call(zr, video, s_routing, 2, 0, 0);
1070 }
1071
1072 zr->zoran_proc = NULL;
1073 zr->initialized = 1;
1074 return 0;
1075
1076exit_free:
1077 kfree(zr->stat_com);
1078 kfree(zr->video_dev);
1079 return err;
1080}
1081
1082static void __devexit zoran_remove(struct pci_dev *pdev)
1083{
1084 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
1085 struct zoran *zr = to_zoran(v4l2_dev);
1086
1087 if (!zr->initialized)
1088 goto exit_free;
1089
1090 /* unregister videocodec bus */
1091 if (zr->codec) {
1092 struct videocodec_master *master = zr->codec->master_data;
1093
1094 videocodec_detach(zr->codec);
1095 kfree(master);
1096 }
1097 if (zr->vfe) {
1098 struct videocodec_master *master = zr->vfe->master_data;
1099
1100 videocodec_detach(zr->vfe);
1101 kfree(master);
1102 }
1103
1104 /* unregister i2c bus */
1105 zoran_unregister_i2c(zr);
1106 /* disable PCI bus-mastering */
1107 zoran_set_pci_master(zr, 0);
1108 /* put chip into reset */
1109 btwrite(0, ZR36057_SPGPPCR);
1110 free_irq(zr->pci_dev->irq, zr);
1111 /* unmap and free memory */
1112 kfree(zr->stat_com);
1113 zoran_proc_cleanup(zr);
1114 iounmap(zr->zr36057_mem);
1115 pci_disable_device(zr->pci_dev);
1116 video_unregister_device(zr->video_dev);
1117exit_free:
1118 v4l2_device_unregister(&zr->v4l2_dev);
1119 kfree(zr);
1120}
1121
1122void
1123zoran_vdev_release (struct video_device *vdev)
1124{
1125 kfree(vdev);
1126}
1127
1128static struct videocodec_master * __devinit
1129zoran_setup_videocodec (struct zoran *zr,
1130 int type)
1131{
1132 struct videocodec_master *m = NULL;
1133
1134 m = kmalloc(sizeof(struct videocodec_master), GFP_KERNEL);
1135 if (!m) {
1136 dprintk(1, KERN_ERR "%s: %s - no memory\n",
1137 ZR_DEVNAME(zr), __func__);
1138 return m;
1139 }
1140
1141 /* magic and type are unused for master struct. Makes sense only at
1142 codec structs.
1143 In the past, .type were initialized to the old V4L1 .hardware
1144 value, as VID_HARDWARE_ZR36067
1145 */
1146 m->magic = 0L;
1147 m->type = 0;
1148
1149 m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1150 strlcpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1151 m->data = zr;
1152
1153 switch (type)
1154 {
1155 case CODEC_TYPE_ZR36060:
1156 m->readreg = zr36060_read;
1157 m->writereg = zr36060_write;
1158 m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1159 break;
1160 case CODEC_TYPE_ZR36050:
1161 m->readreg = zr36050_read;
1162 m->writereg = zr36050_write;
1163 m->flags |= CODEC_FLAG_JPEG;
1164 break;
1165 case CODEC_TYPE_ZR36016:
1166 m->readreg = zr36016_read;
1167 m->writereg = zr36016_write;
1168 m->flags |= CODEC_FLAG_VFE;
1169 break;
1170 }
1171
1172 return m;
1173}
1174
1175static void zoran_subdev_notify(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1176{
1177 struct zoran *zr = to_zoran(sd->v4l2_dev);
1178
1179 /* Bt819 needs to reset its FIFO buffer using #FRST pin and
1180 LML33 card uses GPIO(7) for that. */
1181 if (cmd == BT819_FIFO_RESET_LOW)
1182 GPIO(zr, 7, 0);
1183 else if (cmd == BT819_FIFO_RESET_HIGH)
1184 GPIO(zr, 7, 1);
1185}
1186
1187/*
1188 * Scan for a Buz card (actually for the PCI controller ZR36057),
1189 * request the irq and map the io memory
1190 */
1191static int __devinit zoran_probe(struct pci_dev *pdev,
1192 const struct pci_device_id *ent)
1193{
1194 unsigned char latency, need_latency;
1195 struct zoran *zr;
1196 int result;
1197 struct videocodec_master *master_vfe = NULL;
1198 struct videocodec_master *master_codec = NULL;
1199 int card_num;
1200 char *codec_name, *vfe_name;
1201 unsigned int nr;
1202
1203
1204 nr = zoran_num++;
1205 if (nr >= BUZ_MAX) {
1206 dprintk(1, KERN_ERR "%s: driver limited to %d card(s) maximum\n",
1207 ZORAN_NAME, BUZ_MAX);
1208 return -ENOENT;
1209 }
1210
1211 zr = kzalloc(sizeof(struct zoran), GFP_KERNEL);
1212 if (!zr) {
1213 dprintk(1, KERN_ERR "%s: %s - kzalloc failed\n",
1214 ZORAN_NAME, __func__);
1215 return -ENOMEM;
1216 }
1217 zr->v4l2_dev.notify = zoran_subdev_notify;
1218 if (v4l2_device_register(&pdev->dev, &zr->v4l2_dev))
1219 goto zr_free_mem;
1220 zr->pci_dev = pdev;
1221 zr->id = nr;
1222 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1223 spin_lock_init(&zr->spinlock);
1224 mutex_init(&zr->resource_lock);
1225 mutex_init(&zr->other_lock);
1226 if (pci_enable_device(pdev))
1227 goto zr_unreg;
1228 zr->revision = zr->pci_dev->revision;
1229
1230 dprintk(1,
1231 KERN_INFO
1232 "%s: Zoran ZR360%c7 (rev %d), irq: %d, memory: 0x%08llx\n",
1233 ZR_DEVNAME(zr), zr->revision < 2 ? '5' : '6', zr->revision,
1234 zr->pci_dev->irq, (uint64_t)pci_resource_start(zr->pci_dev, 0));
1235 if (zr->revision >= 2) {
1236 dprintk(1,
1237 KERN_INFO
1238 "%s: Subsystem vendor=0x%04x id=0x%04x\n",
1239 ZR_DEVNAME(zr), zr->pci_dev->subsystem_vendor,
1240 zr->pci_dev->subsystem_device);
1241 }
1242
1243 /* Use auto-detected card type? */
1244 if (card[nr] == -1) {
1245 if (zr->revision < 2) {
1246 dprintk(1,
1247 KERN_ERR
1248 "%s: No card type specified, please use the card=X module parameter\n",
1249 ZR_DEVNAME(zr));
1250 dprintk(1,
1251 KERN_ERR
1252 "%s: It is not possible to auto-detect ZR36057 based cards\n",
1253 ZR_DEVNAME(zr));
1254 goto zr_unreg;
1255 }
1256
1257 card_num = ent->driver_data;
1258 if (card_num >= NUM_CARDS) {
1259 dprintk(1,
1260 KERN_ERR
1261 "%s: Unknown card, try specifying card=X module parameter\n",
1262 ZR_DEVNAME(zr));
1263 goto zr_unreg;
1264 }
1265 dprintk(3,
1266 KERN_DEBUG
1267 "%s: %s() - card %s detected\n",
1268 ZR_DEVNAME(zr), __func__, zoran_cards[card_num].name);
1269 } else {
1270 card_num = card[nr];
1271 if (card_num >= NUM_CARDS || card_num < 0) {
1272 dprintk(1,
1273 KERN_ERR
1274 "%s: User specified card type %d out of range (0 .. %d)\n",
1275 ZR_DEVNAME(zr), card_num, NUM_CARDS - 1);
1276 goto zr_unreg;
1277 }
1278 }
1279
1280 /* even though we make this a non pointer and thus
1281 * theoretically allow for making changes to this struct
1282 * on a per-individual card basis at runtime, this is
1283 * strongly discouraged. This structure is intended to
1284 * keep general card information, no settings or anything */
1285 zr->card = zoran_cards[card_num];
1286 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)),
1287 "%s[%u]", zr->card.name, zr->id);
1288
1289 zr->zr36057_mem = pci_ioremap_bar(zr->pci_dev, 0);
1290 if (!zr->zr36057_mem) {
1291 dprintk(1, KERN_ERR "%s: %s() - ioremap failed\n",
1292 ZR_DEVNAME(zr), __func__);
1293 goto zr_unreg;
1294 }
1295
1296 result = request_irq(zr->pci_dev->irq, zoran_irq,
1297 IRQF_SHARED | IRQF_DISABLED, ZR_DEVNAME(zr), zr);
1298 if (result < 0) {
1299 if (result == -EINVAL) {
1300 dprintk(1,
1301 KERN_ERR
1302 "%s: %s - bad irq number or handler\n",
1303 ZR_DEVNAME(zr), __func__);
1304 } else if (result == -EBUSY) {
1305 dprintk(1,
1306 KERN_ERR
1307 "%s: %s - IRQ %d busy, change your PnP config in BIOS\n",
1308 ZR_DEVNAME(zr), __func__, zr->pci_dev->irq);
1309 } else {
1310 dprintk(1,
1311 KERN_ERR
1312 "%s: %s - can't assign irq, error code %d\n",
1313 ZR_DEVNAME(zr), __func__, result);
1314 }
1315 goto zr_unmap;
1316 }
1317
1318 /* set PCI latency timer */
1319 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1320 &latency);
1321 need_latency = zr->revision > 1 ? 32 : 48;
1322 if (latency != need_latency) {
1323 dprintk(2, KERN_INFO "%s: Changing PCI latency from %d to %d\n",
1324 ZR_DEVNAME(zr), latency, need_latency);
1325 pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1326 need_latency);
1327 }
1328
1329 zr36057_restart(zr);
1330 /* i2c */
1331 dprintk(2, KERN_INFO "%s: Initializing i2c bus...\n",
1332 ZR_DEVNAME(zr));
1333
1334 if (zoran_register_i2c(zr) < 0) {
1335 dprintk(1, KERN_ERR "%s: %s - can't initialize i2c bus\n",
1336 ZR_DEVNAME(zr), __func__);
1337 goto zr_free_irq;
1338 }
1339
1340 zr->decoder = v4l2_i2c_new_subdev(&zr->v4l2_dev,
1341 &zr->i2c_adapter, zr->card.i2c_decoder,
1342 0, zr->card.addrs_decoder);
1343
1344 if (zr->card.i2c_encoder)
1345 zr->encoder = v4l2_i2c_new_subdev(&zr->v4l2_dev,
1346 &zr->i2c_adapter, zr->card.i2c_encoder,
1347 0, zr->card.addrs_encoder);
1348
1349 dprintk(2,
1350 KERN_INFO "%s: Initializing videocodec bus...\n",
1351 ZR_DEVNAME(zr));
1352
1353 if (zr->card.video_codec) {
1354 codec_name = codecid_to_modulename(zr->card.video_codec);
1355 if (codec_name) {
1356 result = request_module(codec_name);
1357 if (result) {
1358 dprintk(1,
1359 KERN_ERR
1360 "%s: failed to load modules %s: %d\n",
1361 ZR_DEVNAME(zr), codec_name, result);
1362 }
1363 }
1364 }
1365 if (zr->card.video_vfe) {
1366 vfe_name = codecid_to_modulename(zr->card.video_vfe);
1367 if (vfe_name) {
1368 result = request_module(vfe_name);
1369 if (result < 0) {
1370 dprintk(1,
1371 KERN_ERR
1372 "%s: failed to load modules %s: %d\n",
1373 ZR_DEVNAME(zr), vfe_name, result);
1374 }
1375 }
1376 }
1377
1378 /* reset JPEG codec */
1379 jpeg_codec_sleep(zr, 1);
1380 jpeg_codec_reset(zr);
1381 /* video bus enabled */
1382 /* display codec revision */
1383 if (zr->card.video_codec != 0) {
1384 master_codec = zoran_setup_videocodec(zr, zr->card.video_codec);
1385 if (!master_codec)
1386 goto zr_unreg_i2c;
1387 zr->codec = videocodec_attach(master_codec);
1388 if (!zr->codec) {
1389 dprintk(1, KERN_ERR "%s: %s - no codec found\n",
1390 ZR_DEVNAME(zr), __func__);
1391 goto zr_free_codec;
1392 }
1393 if (zr->codec->type != zr->card.video_codec) {
1394 dprintk(1, KERN_ERR "%s: %s - wrong codec\n",
1395 ZR_DEVNAME(zr), __func__);
1396 goto zr_detach_codec;
1397 }
1398 }
1399 if (zr->card.video_vfe != 0) {
1400 master_vfe = zoran_setup_videocodec(zr, zr->card.video_vfe);
1401 if (!master_vfe)
1402 goto zr_detach_codec;
1403 zr->vfe = videocodec_attach(master_vfe);
1404 if (!zr->vfe) {
1405 dprintk(1, KERN_ERR "%s: %s - no VFE found\n",
1406 ZR_DEVNAME(zr), __func__);
1407 goto zr_free_vfe;
1408 }
1409 if (zr->vfe->type != zr->card.video_vfe) {
1410 dprintk(1, KERN_ERR "%s: %s = wrong VFE\n",
1411 ZR_DEVNAME(zr), __func__);
1412 goto zr_detach_vfe;
1413 }
1414 }
1415
1416 /* take care of Natoma chipset and a revision 1 zr36057 */
1417 if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1) {
1418 zr->jpg_buffers.need_contiguous = 1;
1419 dprintk(1, KERN_INFO
1420 "%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
1421 ZR_DEVNAME(zr));
1422 }
1423
1424 if (zr36057_init(zr) < 0)
1425 goto zr_detach_vfe;
1426
1427 zoran_proc_init(zr);
1428
1429 return 0;
1430
1431zr_detach_vfe:
1432 videocodec_detach(zr->vfe);
1433zr_free_vfe:
1434 kfree(master_vfe);
1435zr_detach_codec:
1436 videocodec_detach(zr->codec);
1437zr_free_codec:
1438 kfree(master_codec);
1439zr_unreg_i2c:
1440 zoran_unregister_i2c(zr);
1441zr_free_irq:
1442 btwrite(0, ZR36057_SPGPPCR);
1443 free_irq(zr->pci_dev->irq, zr);
1444zr_unmap:
1445 iounmap(zr->zr36057_mem);
1446zr_unreg:
1447 v4l2_device_unregister(&zr->v4l2_dev);
1448zr_free_mem:
1449 kfree(zr);
1450
1451 return -ENODEV;
1452}
1453
1454static struct pci_driver zoran_driver = {
1455 .name = "zr36067",
1456 .id_table = zr36067_pci_tbl,
1457 .probe = zoran_probe,
1458 .remove = __devexit_p(zoran_remove),
1459};
1460
1461static int __init zoran_init(void)
1462{
1463 int res;
1464
1465 printk(KERN_INFO "Zoran MJPEG board driver version %s\n",
1466 ZORAN_VERSION);
1467
1468 /* check the parameters we have been given, adjust if necessary */
1469 if (v4l_nbufs < 2)
1470 v4l_nbufs = 2;
1471 if (v4l_nbufs > VIDEO_MAX_FRAME)
1472 v4l_nbufs = VIDEO_MAX_FRAME;
1473 /* The user specfies the in KB, we want them in byte
1474 * (and page aligned) */
1475 v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
1476 if (v4l_bufsize < 32768)
1477 v4l_bufsize = 32768;
1478 /* 2 MB is arbitrary but sufficient for the maximum possible images */
1479 if (v4l_bufsize > 2048 * 1024)
1480 v4l_bufsize = 2048 * 1024;
1481 if (jpg_nbufs < 4)
1482 jpg_nbufs = 4;
1483 if (jpg_nbufs > BUZ_MAX_FRAME)
1484 jpg_nbufs = BUZ_MAX_FRAME;
1485 jpg_bufsize = PAGE_ALIGN(jpg_bufsize * 1024);
1486 if (jpg_bufsize < 8192)
1487 jpg_bufsize = 8192;
1488 if (jpg_bufsize > (512 * 1024))
1489 jpg_bufsize = 512 * 1024;
1490 /* Use parameter for vidmem or try to find a video card */
1491 if (vidmem) {
1492 dprintk(1,
1493 KERN_INFO
1494 "%s: Using supplied video memory base address @ 0x%lx\n",
1495 ZORAN_NAME, vidmem);
1496 }
1497
1498 /* some mainboards might not do PCI-PCI data transfer well */
1499 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL|PCIPCI_ALIMAGIK)) {
1500 dprintk(1,
1501 KERN_WARNING
1502 "%s: chipset does not support reliable PCI-PCI DMA\n",
1503 ZORAN_NAME);
1504 }
1505
1506 res = pci_register_driver(&zoran_driver);
1507 if (res) {
1508 dprintk(1,
1509 KERN_ERR
1510 "%s: Unable to register ZR36057 driver\n",
1511 ZORAN_NAME);
1512 return res;
1513 }
1514
1515 return 0;
1516}
1517
1518static void __exit zoran_exit(void)
1519{
1520 pci_unregister_driver(&zoran_driver);
1521}
1522
1523module_init(zoran_init);
1524module_exit(zoran_exit);
diff --git a/drivers/media/pci/zoran/zoran_card.h b/drivers/media/pci/zoran/zoran_card.h
new file mode 100644
index 000000000000..4936fead73e8
--- /dev/null
+++ b/drivers/media/pci/zoran/zoran_card.h
@@ -0,0 +1,54 @@
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles card-specific data and detection
7 *
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 * Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Mailinglist <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#ifndef __ZORAN_CARD_H__
31#define __ZORAN_CARD_H__
32
33extern int zr36067_debug;
34
35#define dprintk(num, format, args...) \
36 do { \
37 if (zr36067_debug >= num) \
38 printk(format, ##args); \
39 } while (0)
40
41/* Anybody who uses more than four? */
42#define BUZ_MAX 4
43
44extern struct video_device zoran_template;
45
46extern int zoran_check_jpg_settings(struct zoran *zr,
47 struct zoran_jpg_settings *settings,
48 int try);
49extern void zoran_open_init_params(struct zoran *zr);
50extern void zoran_vdev_release(struct video_device *vdev);
51
52void zr36016_write(struct videocodec *codec, u16 reg, u32 val);
53
54#endif /* __ZORAN_CARD_H__ */
diff --git a/drivers/media/pci/zoran/zoran_device.c b/drivers/media/pci/zoran/zoran_device.c
new file mode 100644
index 000000000000..a4cd504b8eee
--- /dev/null
+++ b/drivers/media/pci/zoran/zoran_device.c
@@ -0,0 +1,1640 @@
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles device access (PCI/I2C/codec/...)
7 *
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 * Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Mailinglist <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/types.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/vmalloc.h>
34
35#include <linux/interrupt.h>
36#include <linux/proc_fs.h>
37#include <linux/i2c.h>
38#include <linux/i2c-algo-bit.h>
39#include <linux/videodev2.h>
40#include <media/v4l2-common.h>
41#include <linux/spinlock.h>
42#include <linux/sem.h>
43
44#include <linux/pci.h>
45#include <linux/delay.h>
46#include <linux/wait.h>
47
48#include <asm/byteorder.h>
49#include <asm/io.h>
50
51#include "videocodec.h"
52#include "zoran.h"
53#include "zoran_device.h"
54#include "zoran_card.h"
55
56#define IRQ_MASK ( ZR36057_ISR_GIRQ0 | \
57 ZR36057_ISR_GIRQ1 | \
58 ZR36057_ISR_JPEGRepIRQ )
59
60static bool lml33dpath; /* default = 0
61 * 1 will use digital path in capture
62 * mode instead of analog. It can be
63 * used for picture adjustments using
64 * tool like xawtv while watching image
65 * on TV monitor connected to the output.
66 * However, due to absence of 75 Ohm
67 * load on Bt819 input, there will be
68 * some image imperfections */
69
70module_param(lml33dpath, bool, 0644);
71MODULE_PARM_DESC(lml33dpath,
72 "Use digital path capture mode (on LML33 cards)");
73
74static void
75zr36057_init_vfe (struct zoran *zr);
76
77/*
78 * General Purpose I/O and Guest bus access
79 */
80
81/*
82 * This is a bit tricky. When a board lacks a GPIO function, the corresponding
83 * GPIO bit number in the card_info structure is set to 0.
84 */
85
86void
87GPIO (struct zoran *zr,
88 int bit,
89 unsigned int value)
90{
91 u32 reg;
92 u32 mask;
93
94 /* Make sure the bit number is legal
95 * A bit number of -1 (lacking) gives a mask of 0,
96 * making it harmless */
97 mask = (1 << (24 + bit)) & 0xff000000;
98 reg = btread(ZR36057_GPPGCR1) & ~mask;
99 if (value) {
100 reg |= mask;
101 }
102 btwrite(reg, ZR36057_GPPGCR1);
103 udelay(1);
104}
105
106/*
107 * Wait til post office is no longer busy
108 */
109
110int
111post_office_wait (struct zoran *zr)
112{
113 u32 por;
114
115// while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
116 while ((por = btread(ZR36057_POR)) & ZR36057_POR_POPen) {
117 /* wait for something to happen */
118 }
119 if ((por & ZR36057_POR_POTime) && !zr->card.gws_not_connected) {
120 /* In LML33/BUZ \GWS line is not connected, so it has always timeout set */
121 dprintk(1, KERN_INFO "%s: pop timeout %08x\n", ZR_DEVNAME(zr),
122 por);
123 return -1;
124 }
125
126 return 0;
127}
128
129int
130post_office_write (struct zoran *zr,
131 unsigned int guest,
132 unsigned int reg,
133 unsigned int value)
134{
135 u32 por;
136
137 por =
138 ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) |
139 ((reg & 7) << 16) | (value & 0xFF);
140 btwrite(por, ZR36057_POR);
141
142 return post_office_wait(zr);
143}
144
145int
146post_office_read (struct zoran *zr,
147 unsigned int guest,
148 unsigned int reg)
149{
150 u32 por;
151
152 por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
153 btwrite(por, ZR36057_POR);
154 if (post_office_wait(zr) < 0) {
155 return -1;
156 }
157
158 return btread(ZR36057_POR) & 0xFF;
159}
160
161/*
162 * detect guests
163 */
164
165static void
166dump_guests (struct zoran *zr)
167{
168 if (zr36067_debug > 2) {
169 int i, guest[8];
170
171 for (i = 1; i < 8; i++) { // Don't read jpeg codec here
172 guest[i] = post_office_read(zr, i, 0);
173 }
174
175 printk(KERN_INFO "%s: Guests:", ZR_DEVNAME(zr));
176
177 for (i = 1; i < 8; i++) {
178 printk(" 0x%02x", guest[i]);
179 }
180 printk("\n");
181 }
182}
183
184static inline unsigned long
185get_time (void)
186{
187 struct timeval tv;
188
189 do_gettimeofday(&tv);
190 return (1000000 * tv.tv_sec + tv.tv_usec);
191}
192
193void
194detect_guest_activity (struct zoran *zr)
195{
196 int timeout, i, j, res, guest[8], guest0[8], change[8][3];
197 unsigned long t0, t1;
198
199 dump_guests(zr);
200 printk(KERN_INFO "%s: Detecting guests activity, please wait...\n",
201 ZR_DEVNAME(zr));
202 for (i = 1; i < 8; i++) { // Don't read jpeg codec here
203 guest0[i] = guest[i] = post_office_read(zr, i, 0);
204 }
205
206 timeout = 0;
207 j = 0;
208 t0 = get_time();
209 while (timeout < 10000) {
210 udelay(10);
211 timeout++;
212 for (i = 1; (i < 8) && (j < 8); i++) {
213 res = post_office_read(zr, i, 0);
214 if (res != guest[i]) {
215 t1 = get_time();
216 change[j][0] = (t1 - t0);
217 t0 = t1;
218 change[j][1] = i;
219 change[j][2] = res;
220 j++;
221 guest[i] = res;
222 }
223 }
224 if (j >= 8)
225 break;
226 }
227 printk(KERN_INFO "%s: Guests:", ZR_DEVNAME(zr));
228
229 for (i = 1; i < 8; i++) {
230 printk(" 0x%02x", guest0[i]);
231 }
232 printk("\n");
233 if (j == 0) {
234 printk(KERN_INFO "%s: No activity detected.\n", ZR_DEVNAME(zr));
235 return;
236 }
237 for (i = 0; i < j; i++) {
238 printk(KERN_INFO "%s: %6d: %d => 0x%02x\n", ZR_DEVNAME(zr),
239 change[i][0], change[i][1], change[i][2]);
240 }
241}
242
243/*
244 * JPEG Codec access
245 */
246
247void
248jpeg_codec_sleep (struct zoran *zr,
249 int sleep)
250{
251 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_SLEEP], !sleep);
252 if (!sleep) {
253 dprintk(3,
254 KERN_DEBUG
255 "%s: jpeg_codec_sleep() - wake GPIO=0x%08x\n",
256 ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
257 udelay(500);
258 } else {
259 dprintk(3,
260 KERN_DEBUG
261 "%s: jpeg_codec_sleep() - sleep GPIO=0x%08x\n",
262 ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
263 udelay(2);
264 }
265}
266
267int
268jpeg_codec_reset (struct zoran *zr)
269{
270 /* Take the codec out of sleep */
271 jpeg_codec_sleep(zr, 0);
272
273 if (zr->card.gpcs[GPCS_JPEG_RESET] != 0xff) {
274 post_office_write(zr, zr->card.gpcs[GPCS_JPEG_RESET], 0,
275 0);
276 udelay(2);
277 } else {
278 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_RESET], 0);
279 udelay(2);
280 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_RESET], 1);
281 udelay(2);
282 }
283
284 return 0;
285}
286
287/*
288 * Set the registers for the size we have specified. Don't bother
289 * trying to understand this without the ZR36057 manual in front of
290 * you [AC].
291 *
292 * PS: The manual is free for download in .pdf format from
293 * www.zoran.com - nicely done those folks.
294 */
295
296static void
297zr36057_adjust_vfe (struct zoran *zr,
298 enum zoran_codec_mode mode)
299{
300 u32 reg;
301
302 switch (mode) {
303 case BUZ_MODE_MOTION_DECOMPRESS:
304 btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
305 reg = btread(ZR36057_VFEHCR);
306 if ((reg & (1 << 10)) && zr->card.type != LML33R10) {
307 reg += ((1 << 10) | 1);
308 }
309 btwrite(reg, ZR36057_VFEHCR);
310 break;
311 case BUZ_MODE_MOTION_COMPRESS:
312 case BUZ_MODE_IDLE:
313 default:
314 if ((zr->norm & V4L2_STD_NTSC) ||
315 (zr->card.type == LML33R10 &&
316 (zr->norm & V4L2_STD_PAL)))
317 btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
318 else
319 btor(ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
320 reg = btread(ZR36057_VFEHCR);
321 if (!(reg & (1 << 10)) && zr->card.type != LML33R10) {
322 reg -= ((1 << 10) | 1);
323 }
324 btwrite(reg, ZR36057_VFEHCR);
325 break;
326 }
327}
328
329/*
330 * set geometry
331 */
332
333static void
334zr36057_set_vfe (struct zoran *zr,
335 int video_width,
336 int video_height,
337 const struct zoran_format *format)
338{
339 struct tvnorm *tvn;
340 unsigned HStart, HEnd, VStart, VEnd;
341 unsigned DispMode;
342 unsigned VidWinWid, VidWinHt;
343 unsigned hcrop1, hcrop2, vcrop1, vcrop2;
344 unsigned Wa, We, Ha, He;
345 unsigned X, Y, HorDcm, VerDcm;
346 u32 reg;
347 unsigned mask_line_size;
348
349 tvn = zr->timing;
350
351 Wa = tvn->Wa;
352 Ha = tvn->Ha;
353
354 dprintk(2, KERN_INFO "%s: set_vfe() - width = %d, height = %d\n",
355 ZR_DEVNAME(zr), video_width, video_height);
356
357 if (video_width < BUZ_MIN_WIDTH ||
358 video_height < BUZ_MIN_HEIGHT ||
359 video_width > Wa || video_height > Ha) {
360 dprintk(1, KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n",
361 ZR_DEVNAME(zr), video_width, video_height);
362 return;
363 }
364
365 /**** zr36057 ****/
366
367 /* horizontal */
368 VidWinWid = video_width;
369 X = DIV_ROUND_UP(VidWinWid * 64, tvn->Wa);
370 We = (VidWinWid * 64) / X;
371 HorDcm = 64 - X;
372 hcrop1 = 2 * ((tvn->Wa - We) / 4);
373 hcrop2 = tvn->Wa - We - hcrop1;
374 HStart = tvn->HStart ? tvn->HStart : 1;
375 /* (Ronald) Original comment:
376 * "| 1 Doesn't have any effect, tested on both a DC10 and a DC10+"
377 * this is false. It inverses chroma values on the LML33R10 (so Cr
378 * suddenly is shown as Cb and reverse, really cool effect if you
379 * want to see blue faces, not useful otherwise). So don't use |1.
380 * However, the DC10 has '0' as HStart, but does need |1, so we
381 * use a dirty check...
382 */
383 HEnd = HStart + tvn->Wa - 1;
384 HStart += hcrop1;
385 HEnd -= hcrop2;
386 reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
387 | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
388 if (zr->card.vfe_pol.hsync_pol)
389 reg |= ZR36057_VFEHCR_HSPol;
390 btwrite(reg, ZR36057_VFEHCR);
391
392 /* Vertical */
393 DispMode = !(video_height > BUZ_MAX_HEIGHT / 2);
394 VidWinHt = DispMode ? video_height : video_height / 2;
395 Y = DIV_ROUND_UP(VidWinHt * 64 * 2, tvn->Ha);
396 He = (VidWinHt * 64) / Y;
397 VerDcm = 64 - Y;
398 vcrop1 = (tvn->Ha / 2 - He) / 2;
399 vcrop2 = tvn->Ha / 2 - He - vcrop1;
400 VStart = tvn->VStart;
401 VEnd = VStart + tvn->Ha / 2; // - 1; FIXME SnapShot times out with -1 in 768*576 on the DC10 - LP
402 VStart += vcrop1;
403 VEnd -= vcrop2;
404 reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
405 | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
406 if (zr->card.vfe_pol.vsync_pol)
407 reg |= ZR36057_VFEVCR_VSPol;
408 btwrite(reg, ZR36057_VFEVCR);
409
410 /* scaler and pixel format */
411 reg = 0;
412 reg |= (HorDcm << ZR36057_VFESPFR_HorDcm);
413 reg |= (VerDcm << ZR36057_VFESPFR_VerDcm);
414 reg |= (DispMode << ZR36057_VFESPFR_DispMode);
415 /* RJ: I don't know, why the following has to be the opposite
416 * of the corresponding ZR36060 setting, but only this way
417 * we get the correct colors when uncompressing to the screen */
418 //reg |= ZR36057_VFESPFR_VCLKPol; /**/
419 /* RJ: Don't know if that is needed for NTSC also */
420 if (!(zr->norm & V4L2_STD_NTSC))
421 reg |= ZR36057_VFESPFR_ExtFl; // NEEDED!!!!!!! Wolfgang
422 reg |= ZR36057_VFESPFR_TopField;
423 if (HorDcm >= 48) {
424 reg |= 3 << ZR36057_VFESPFR_HFilter; /* 5 tap filter */
425 } else if (HorDcm >= 32) {
426 reg |= 2 << ZR36057_VFESPFR_HFilter; /* 4 tap filter */
427 } else if (HorDcm >= 16) {
428 reg |= 1 << ZR36057_VFESPFR_HFilter; /* 3 tap filter */
429 }
430 reg |= format->vfespfr;
431 btwrite(reg, ZR36057_VFESPFR);
432
433 /* display configuration */
434 reg = (16 << ZR36057_VDCR_MinPix)
435 | (VidWinHt << ZR36057_VDCR_VidWinHt)
436 | (VidWinWid << ZR36057_VDCR_VidWinWid);
437 if (pci_pci_problems & PCIPCI_TRITON)
438 // || zr->revision < 1) // Revision 1 has also Triton support
439 reg &= ~ZR36057_VDCR_Triton;
440 else
441 reg |= ZR36057_VDCR_Triton;
442 btwrite(reg, ZR36057_VDCR);
443
444 /* (Ronald) don't write this if overlay_mask = NULL */
445 if (zr->overlay_mask) {
446 /* Write overlay clipping mask data, but don't enable overlay clipping */
447 /* RJ: since this makes only sense on the screen, we use
448 * zr->overlay_settings.width instead of video_width */
449
450 mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
451 reg = virt_to_bus(zr->overlay_mask);
452 btwrite(reg, ZR36057_MMTR);
453 reg = virt_to_bus(zr->overlay_mask + mask_line_size);
454 btwrite(reg, ZR36057_MMBR);
455 reg =
456 mask_line_size - (zr->overlay_settings.width +
457 31) / 32;
458 if (DispMode == 0)
459 reg += mask_line_size;
460 reg <<= ZR36057_OCR_MaskStride;
461 btwrite(reg, ZR36057_OCR);
462 }
463
464 zr36057_adjust_vfe(zr, zr->codec_mode);
465}
466
467/*
468 * Switch overlay on or off
469 */
470
471void
472zr36057_overlay (struct zoran *zr,
473 int on)
474{
475 u32 reg;
476
477 if (on) {
478 /* do the necessary settings ... */
479 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR); /* switch it off first */
480
481 zr36057_set_vfe(zr,
482 zr->overlay_settings.width,
483 zr->overlay_settings.height,
484 zr->overlay_settings.format);
485
486 /* Start and length of each line MUST be 4-byte aligned.
487 * This should be already checked before the call to this routine.
488 * All error messages are internal driver checking only! */
489
490 /* video display top and bottom registers */
491 reg = (long) zr->vbuf_base +
492 zr->overlay_settings.x *
493 ((zr->overlay_settings.format->depth + 7) / 8) +
494 zr->overlay_settings.y *
495 zr->vbuf_bytesperline;
496 btwrite(reg, ZR36057_VDTR);
497 if (reg & 3)
498 dprintk(1,
499 KERN_ERR
500 "%s: zr36057_overlay() - video_address not aligned\n",
501 ZR_DEVNAME(zr));
502 if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
503 reg += zr->vbuf_bytesperline;
504 btwrite(reg, ZR36057_VDBR);
505
506 /* video stride, status, and frame grab register */
507 reg = zr->vbuf_bytesperline -
508 zr->overlay_settings.width *
509 ((zr->overlay_settings.format->depth + 7) / 8);
510 if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
511 reg += zr->vbuf_bytesperline;
512 if (reg & 3)
513 dprintk(1,
514 KERN_ERR
515 "%s: zr36057_overlay() - video_stride not aligned\n",
516 ZR_DEVNAME(zr));
517 reg = (reg << ZR36057_VSSFGR_DispStride);
518 reg |= ZR36057_VSSFGR_VidOvf; /* clear overflow status */
519 btwrite(reg, ZR36057_VSSFGR);
520
521 /* Set overlay clipping */
522 if (zr->overlay_settings.clipcount > 0)
523 btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
524
525 /* ... and switch it on */
526 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
527 } else {
528 /* Switch it off */
529 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
530 }
531}
532
533/*
534 * The overlay mask has one bit for each pixel on a scan line,
535 * and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
536 */
537
538void write_overlay_mask(struct zoran_fh *fh, struct v4l2_clip *vp, int count)
539{
540 struct zoran *zr = fh->zr;
541 unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
542 u32 *mask;
543 int x, y, width, height;
544 unsigned i, j, k;
545
546 /* fill mask with one bits */
547 memset(fh->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
548
549 for (i = 0; i < count; ++i) {
550 /* pick up local copy of clip */
551 x = vp[i].c.left;
552 y = vp[i].c.top;
553 width = vp[i].c.width;
554 height = vp[i].c.height;
555
556 /* trim clips that extend beyond the window */
557 if (x < 0) {
558 width += x;
559 x = 0;
560 }
561 if (y < 0) {
562 height += y;
563 y = 0;
564 }
565 if (x + width > fh->overlay_settings.width) {
566 width = fh->overlay_settings.width - x;
567 }
568 if (y + height > fh->overlay_settings.height) {
569 height = fh->overlay_settings.height - y;
570 }
571
572 /* ignore degenerate clips */
573 if (height <= 0) {
574 continue;
575 }
576 if (width <= 0) {
577 continue;
578 }
579
580 /* apply clip for each scan line */
581 for (j = 0; j < height; ++j) {
582 /* reset bit for each pixel */
583 /* this can be optimized later if need be */
584 mask = fh->overlay_mask + (y + j) * mask_line_size;
585 for (k = 0; k < width; ++k) {
586 mask[(x + k) / 32] &=
587 ~((u32) 1 << (x + k) % 32);
588 }
589 }
590 }
591}
592
593/* Enable/Disable uncompressed memory grabbing of the 36057 */
594
595void
596zr36057_set_memgrab (struct zoran *zr,
597 int mode)
598{
599 if (mode) {
600 /* We only check SnapShot and not FrameGrab here. SnapShot==1
601 * means a capture is already in progress, but FrameGrab==1
602 * doesn't necessary mean that. It's more correct to say a 1
603 * to 0 transition indicates a capture completed. If a
604 * capture is pending when capturing is tuned off, FrameGrab
605 * will be stuck at 1 until capturing is turned back on.
606 */
607 if (btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot)
608 dprintk(1,
609 KERN_WARNING
610 "%s: zr36057_set_memgrab(1) with SnapShot on!?\n",
611 ZR_DEVNAME(zr));
612
613 /* switch on VSync interrupts */
614 btwrite(IRQ_MASK, ZR36057_ISR); // Clear Interrupts
615 btor(zr->card.vsync_int, ZR36057_ICR); // SW
616
617 /* enable SnapShot */
618 btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
619
620 /* Set zr36057 video front end and enable video */
621 zr36057_set_vfe(zr, zr->v4l_settings.width,
622 zr->v4l_settings.height,
623 zr->v4l_settings.format);
624
625 zr->v4l_memgrab_active = 1;
626 } else {
627 /* switch off VSync interrupts */
628 btand(~zr->card.vsync_int, ZR36057_ICR); // SW
629
630 zr->v4l_memgrab_active = 0;
631 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
632
633 /* reenable grabbing to screen if it was running */
634 if (zr->v4l_overlay_active) {
635 zr36057_overlay(zr, 1);
636 } else {
637 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
638 btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
639 }
640 }
641}
642
643int
644wait_grab_pending (struct zoran *zr)
645{
646 unsigned long flags;
647
648 /* wait until all pending grabs are finished */
649
650 if (!zr->v4l_memgrab_active)
651 return 0;
652
653 wait_event_interruptible(zr->v4l_capq,
654 (zr->v4l_pend_tail == zr->v4l_pend_head));
655 if (signal_pending(current))
656 return -ERESTARTSYS;
657
658 spin_lock_irqsave(&zr->spinlock, flags);
659 zr36057_set_memgrab(zr, 0);
660 spin_unlock_irqrestore(&zr->spinlock, flags);
661
662 return 0;
663}
664
665/*****************************************************************************
666 * *
667 * Set up the Buz-specific MJPEG part *
668 * *
669 *****************************************************************************/
670
671static inline void
672set_frame (struct zoran *zr,
673 int val)
674{
675 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_FRAME], val);
676}
677
678static void
679set_videobus_dir (struct zoran *zr,
680 int val)
681{
682 switch (zr->card.type) {
683 case LML33:
684 case LML33R10:
685 if (lml33dpath == 0)
686 GPIO(zr, 5, val);
687 else
688 GPIO(zr, 5, 1);
689 break;
690 default:
691 GPIO(zr, zr->card.gpio[ZR_GPIO_VID_DIR],
692 zr->card.gpio_pol[ZR_GPIO_VID_DIR] ? !val : val);
693 break;
694 }
695}
696
697static void
698init_jpeg_queue (struct zoran *zr)
699{
700 int i;
701
702 /* re-initialize DMA ring stuff */
703 zr->jpg_que_head = 0;
704 zr->jpg_dma_head = 0;
705 zr->jpg_dma_tail = 0;
706 zr->jpg_que_tail = 0;
707 zr->jpg_seq_num = 0;
708 zr->JPEG_error = 0;
709 zr->num_errors = 0;
710 zr->jpg_err_seq = 0;
711 zr->jpg_err_shift = 0;
712 zr->jpg_queued_num = 0;
713 for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
714 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
715 }
716 for (i = 0; i < BUZ_NUM_STAT_COM; i++) {
717 zr->stat_com[i] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
718 }
719}
720
721static void
722zr36057_set_jpg (struct zoran *zr,
723 enum zoran_codec_mode mode)
724{
725 struct tvnorm *tvn;
726 u32 reg;
727
728 tvn = zr->timing;
729
730 /* assert P_Reset, disable code transfer, deassert Active */
731 btwrite(0, ZR36057_JPC);
732
733 /* MJPEG compression mode */
734 switch (mode) {
735
736 case BUZ_MODE_MOTION_COMPRESS:
737 default:
738 reg = ZR36057_JMC_MJPGCmpMode;
739 break;
740
741 case BUZ_MODE_MOTION_DECOMPRESS:
742 reg = ZR36057_JMC_MJPGExpMode;
743 reg |= ZR36057_JMC_SyncMstr;
744 /* RJ: The following is experimental - improves the output to screen */
745 //if(zr->jpg_settings.VFIFO_FB) reg |= ZR36057_JMC_VFIFO_FB; // No, it doesn't. SM
746 break;
747
748 case BUZ_MODE_STILL_COMPRESS:
749 reg = ZR36057_JMC_JPGCmpMode;
750 break;
751
752 case BUZ_MODE_STILL_DECOMPRESS:
753 reg = ZR36057_JMC_JPGExpMode;
754 break;
755
756 }
757 reg |= ZR36057_JMC_JPG;
758 if (zr->jpg_settings.field_per_buff == 1)
759 reg |= ZR36057_JMC_Fld_per_buff;
760 btwrite(reg, ZR36057_JMC);
761
762 /* vertical */
763 btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
764 reg = (6 << ZR36057_VSP_VsyncSize) |
765 (tvn->Ht << ZR36057_VSP_FrmTot);
766 btwrite(reg, ZR36057_VSP);
767 reg = ((zr->jpg_settings.img_y + tvn->VStart) << ZR36057_FVAP_NAY) |
768 (zr->jpg_settings.img_height << ZR36057_FVAP_PAY);
769 btwrite(reg, ZR36057_FVAP);
770
771 /* horizontal */
772 if (zr->card.vfe_pol.hsync_pol)
773 btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
774 else
775 btand(~ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
776 reg = ((tvn->HSyncStart) << ZR36057_HSP_HsyncStart) |
777 (tvn->Wt << ZR36057_HSP_LineTot);
778 btwrite(reg, ZR36057_HSP);
779 reg = ((zr->jpg_settings.img_x +
780 tvn->HStart + 4) << ZR36057_FHAP_NAX) |
781 (zr->jpg_settings.img_width << ZR36057_FHAP_PAX);
782 btwrite(reg, ZR36057_FHAP);
783
784 /* field process parameters */
785 if (zr->jpg_settings.odd_even)
786 reg = ZR36057_FPP_Odd_Even;
787 else
788 reg = 0;
789
790 btwrite(reg, ZR36057_FPP);
791
792 /* Set proper VCLK Polarity, else colors will be wrong during playback */
793 //btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
794
795 /* code base address */
796 reg = virt_to_bus(zr->stat_com);
797 btwrite(reg, ZR36057_JCBA);
798
799 /* FIFO threshold (FIFO is 160. double words) */
800 /* NOTE: decimal values here */
801 switch (mode) {
802
803 case BUZ_MODE_STILL_COMPRESS:
804 case BUZ_MODE_MOTION_COMPRESS:
805 if (zr->card.type != BUZ)
806 reg = 140;
807 else
808 reg = 60;
809 break;
810
811 case BUZ_MODE_STILL_DECOMPRESS:
812 case BUZ_MODE_MOTION_DECOMPRESS:
813 reg = 20;
814 break;
815
816 default:
817 reg = 80;
818 break;
819
820 }
821 btwrite(reg, ZR36057_JCFT);
822 zr36057_adjust_vfe(zr, mode);
823
824}
825
826void
827print_interrupts (struct zoran *zr)
828{
829 int res, noerr = 0;
830
831 printk(KERN_INFO "%s: interrupts received:", ZR_DEVNAME(zr));
832 if ((res = zr->field_counter) < -1 || res > 1) {
833 printk(" FD:%d", res);
834 }
835 if ((res = zr->intr_counter_GIRQ1) != 0) {
836 printk(" GIRQ1:%d", res);
837 noerr++;
838 }
839 if ((res = zr->intr_counter_GIRQ0) != 0) {
840 printk(" GIRQ0:%d", res);
841 noerr++;
842 }
843 if ((res = zr->intr_counter_CodRepIRQ) != 0) {
844 printk(" CodRepIRQ:%d", res);
845 noerr++;
846 }
847 if ((res = zr->intr_counter_JPEGRepIRQ) != 0) {
848 printk(" JPEGRepIRQ:%d", res);
849 noerr++;
850 }
851 if (zr->JPEG_max_missed) {
852 printk(" JPEG delays: max=%d min=%d", zr->JPEG_max_missed,
853 zr->JPEG_min_missed);
854 }
855 if (zr->END_event_missed) {
856 printk(" ENDs missed: %d", zr->END_event_missed);
857 }
858 //if (zr->jpg_queued_num) {
859 printk(" queue_state=%ld/%ld/%ld/%ld", zr->jpg_que_tail,
860 zr->jpg_dma_tail, zr->jpg_dma_head, zr->jpg_que_head);
861 //}
862 if (!noerr) {
863 printk(": no interrupts detected.");
864 }
865 printk("\n");
866}
867
868void
869clear_interrupt_counters (struct zoran *zr)
870{
871 zr->intr_counter_GIRQ1 = 0;
872 zr->intr_counter_GIRQ0 = 0;
873 zr->intr_counter_CodRepIRQ = 0;
874 zr->intr_counter_JPEGRepIRQ = 0;
875 zr->field_counter = 0;
876 zr->IRQ1_in = 0;
877 zr->IRQ1_out = 0;
878 zr->JPEG_in = 0;
879 zr->JPEG_out = 0;
880 zr->JPEG_0 = 0;
881 zr->JPEG_1 = 0;
882 zr->END_event_missed = 0;
883 zr->JPEG_missed = 0;
884 zr->JPEG_max_missed = 0;
885 zr->JPEG_min_missed = 0x7fffffff;
886}
887
888static u32
889count_reset_interrupt (struct zoran *zr)
890{
891 u32 isr;
892
893 if ((isr = btread(ZR36057_ISR) & 0x78000000)) {
894 if (isr & ZR36057_ISR_GIRQ1) {
895 btwrite(ZR36057_ISR_GIRQ1, ZR36057_ISR);
896 zr->intr_counter_GIRQ1++;
897 }
898 if (isr & ZR36057_ISR_GIRQ0) {
899 btwrite(ZR36057_ISR_GIRQ0, ZR36057_ISR);
900 zr->intr_counter_GIRQ0++;
901 }
902 if (isr & ZR36057_ISR_CodRepIRQ) {
903 btwrite(ZR36057_ISR_CodRepIRQ, ZR36057_ISR);
904 zr->intr_counter_CodRepIRQ++;
905 }
906 if (isr & ZR36057_ISR_JPEGRepIRQ) {
907 btwrite(ZR36057_ISR_JPEGRepIRQ, ZR36057_ISR);
908 zr->intr_counter_JPEGRepIRQ++;
909 }
910 }
911 return isr;
912}
913
914void
915jpeg_start (struct zoran *zr)
916{
917 int reg;
918
919 zr->frame_num = 0;
920
921 /* deassert P_reset, disable code transfer, deassert Active */
922 btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
923 /* stop flushing the internal code buffer */
924 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
925 /* enable code transfer */
926 btor(ZR36057_JPC_CodTrnsEn, ZR36057_JPC);
927
928 /* clear IRQs */
929 btwrite(IRQ_MASK, ZR36057_ISR);
930 /* enable the JPEG IRQs */
931 btwrite(zr->card.jpeg_int |
932 ZR36057_ICR_JPEGRepIRQ |
933 ZR36057_ICR_IntPinEn,
934 ZR36057_ICR);
935
936 set_frame(zr, 0); // \FRAME
937
938 /* set the JPEG codec guest ID */
939 reg = (zr->card.gpcs[1] << ZR36057_JCGI_JPEGuestID) |
940 (0 << ZR36057_JCGI_JPEGuestReg);
941 btwrite(reg, ZR36057_JCGI);
942
943 if (zr->card.video_vfe == CODEC_TYPE_ZR36016 &&
944 zr->card.video_codec == CODEC_TYPE_ZR36050) {
945 /* Enable processing on the ZR36016 */
946 if (zr->vfe)
947 zr36016_write(zr->vfe, 0, 1);
948
949 /* load the address of the GO register in the ZR36050 latch */
950 post_office_write(zr, 0, 0, 0);
951 }
952
953 /* assert Active */
954 btor(ZR36057_JPC_Active, ZR36057_JPC);
955
956 /* enable the Go generation */
957 btor(ZR36057_JMC_Go_en, ZR36057_JMC);
958 udelay(30);
959
960 set_frame(zr, 1); // /FRAME
961
962 dprintk(3, KERN_DEBUG "%s: jpeg_start\n", ZR_DEVNAME(zr));
963}
964
965void
966zr36057_enable_jpg (struct zoran *zr,
967 enum zoran_codec_mode mode)
968{
969 struct vfe_settings cap;
970 int field_size =
971 zr->jpg_buffers.buffer_size / zr->jpg_settings.field_per_buff;
972
973 zr->codec_mode = mode;
974
975 cap.x = zr->jpg_settings.img_x;
976 cap.y = zr->jpg_settings.img_y;
977 cap.width = zr->jpg_settings.img_width;
978 cap.height = zr->jpg_settings.img_height;
979 cap.decimation =
980 zr->jpg_settings.HorDcm | (zr->jpg_settings.VerDcm << 8);
981 cap.quality = zr->jpg_settings.jpg_comp.quality;
982
983 switch (mode) {
984
985 case BUZ_MODE_MOTION_COMPRESS: {
986 struct jpeg_app_marker app;
987 struct jpeg_com_marker com;
988
989 /* In motion compress mode, the decoder output must be enabled, and
990 * the video bus direction set to input.
991 */
992 set_videobus_dir(zr, 0);
993 decoder_call(zr, video, s_stream, 1);
994 encoder_call(zr, video, s_routing, 0, 0, 0);
995
996 /* Take the JPEG codec and the VFE out of sleep */
997 jpeg_codec_sleep(zr, 0);
998
999 /* set JPEG app/com marker */
1000 app.appn = zr->jpg_settings.jpg_comp.APPn;
1001 app.len = zr->jpg_settings.jpg_comp.APP_len;
1002 memcpy(app.data, zr->jpg_settings.jpg_comp.APP_data, 60);
1003 zr->codec->control(zr->codec, CODEC_S_JPEG_APP_DATA,
1004 sizeof(struct jpeg_app_marker), &app);
1005
1006 com.len = zr->jpg_settings.jpg_comp.COM_len;
1007 memcpy(com.data, zr->jpg_settings.jpg_comp.COM_data, 60);
1008 zr->codec->control(zr->codec, CODEC_S_JPEG_COM_DATA,
1009 sizeof(struct jpeg_com_marker), &com);
1010
1011 /* Setup the JPEG codec */
1012 zr->codec->control(zr->codec, CODEC_S_JPEG_TDS_BYTE,
1013 sizeof(int), &field_size);
1014 zr->codec->set_video(zr->codec, zr->timing, &cap,
1015 &zr->card.vfe_pol);
1016 zr->codec->set_mode(zr->codec, CODEC_DO_COMPRESSION);
1017
1018 /* Setup the VFE */
1019 if (zr->vfe) {
1020 zr->vfe->control(zr->vfe, CODEC_S_JPEG_TDS_BYTE,
1021 sizeof(int), &field_size);
1022 zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1023 &zr->card.vfe_pol);
1024 zr->vfe->set_mode(zr->vfe, CODEC_DO_COMPRESSION);
1025 }
1026
1027 init_jpeg_queue(zr);
1028 zr36057_set_jpg(zr, mode); // \P_Reset, ... Video param, FIFO
1029
1030 clear_interrupt_counters(zr);
1031 dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_COMPRESS)\n",
1032 ZR_DEVNAME(zr));
1033 break;
1034 }
1035
1036 case BUZ_MODE_MOTION_DECOMPRESS:
1037 /* In motion decompression mode, the decoder output must be disabled, and
1038 * the video bus direction set to output.
1039 */
1040 decoder_call(zr, video, s_stream, 0);
1041 set_videobus_dir(zr, 1);
1042 encoder_call(zr, video, s_routing, 1, 0, 0);
1043
1044 /* Take the JPEG codec and the VFE out of sleep */
1045 jpeg_codec_sleep(zr, 0);
1046 /* Setup the VFE */
1047 if (zr->vfe) {
1048 zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1049 &zr->card.vfe_pol);
1050 zr->vfe->set_mode(zr->vfe, CODEC_DO_EXPANSION);
1051 }
1052 /* Setup the JPEG codec */
1053 zr->codec->set_video(zr->codec, zr->timing, &cap,
1054 &zr->card.vfe_pol);
1055 zr->codec->set_mode(zr->codec, CODEC_DO_EXPANSION);
1056
1057 init_jpeg_queue(zr);
1058 zr36057_set_jpg(zr, mode); // \P_Reset, ... Video param, FIFO
1059
1060 clear_interrupt_counters(zr);
1061 dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_DECOMPRESS)\n",
1062 ZR_DEVNAME(zr));
1063 break;
1064
1065 case BUZ_MODE_IDLE:
1066 default:
1067 /* shut down processing */
1068 btand(~(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ),
1069 ZR36057_ICR);
1070 btwrite(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ,
1071 ZR36057_ISR);
1072 btand(~ZR36057_JMC_Go_en, ZR36057_JMC); // \Go_en
1073
1074 msleep(50);
1075
1076 set_videobus_dir(zr, 0);
1077 set_frame(zr, 1); // /FRAME
1078 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR); // /CFlush
1079 btwrite(0, ZR36057_JPC); // \P_Reset,\CodTrnsEn,\Active
1080 btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
1081 btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
1082 jpeg_codec_reset(zr);
1083 jpeg_codec_sleep(zr, 1);
1084 zr36057_adjust_vfe(zr, mode);
1085
1086 decoder_call(zr, video, s_stream, 1);
1087 encoder_call(zr, video, s_routing, 0, 0, 0);
1088
1089 dprintk(2, KERN_INFO "%s: enable_jpg(IDLE)\n", ZR_DEVNAME(zr));
1090 break;
1091
1092 }
1093}
1094
1095/* when this is called the spinlock must be held */
1096void
1097zoran_feed_stat_com (struct zoran *zr)
1098{
1099 /* move frames from pending queue to DMA */
1100
1101 int frame, i, max_stat_com;
1102
1103 max_stat_com =
1104 (zr->jpg_settings.TmpDcm ==
1105 1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
1106
1107 while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com &&
1108 zr->jpg_dma_head < zr->jpg_que_head) {
1109
1110 frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
1111 if (zr->jpg_settings.TmpDcm == 1) {
1112 /* fill 1 stat_com entry */
1113 i = (zr->jpg_dma_head -
1114 zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1115 if (!(zr->stat_com[i] & cpu_to_le32(1)))
1116 break;
1117 zr->stat_com[i] =
1118 cpu_to_le32(zr->jpg_buffers.buffer[frame].jpg.frag_tab_bus);
1119 } else {
1120 /* fill 2 stat_com entries */
1121 i = ((zr->jpg_dma_head -
1122 zr->jpg_err_shift) & 1) * 2;
1123 if (!(zr->stat_com[i] & cpu_to_le32(1)))
1124 break;
1125 zr->stat_com[i] =
1126 cpu_to_le32(zr->jpg_buffers.buffer[frame].jpg.frag_tab_bus);
1127 zr->stat_com[i + 1] =
1128 cpu_to_le32(zr->jpg_buffers.buffer[frame].jpg.frag_tab_bus);
1129 }
1130 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_DMA;
1131 zr->jpg_dma_head++;
1132
1133 }
1134 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
1135 zr->jpg_queued_num++;
1136}
1137
1138/* when this is called the spinlock must be held */
1139static void
1140zoran_reap_stat_com (struct zoran *zr)
1141{
1142 /* move frames from DMA queue to done queue */
1143
1144 int i;
1145 u32 stat_com;
1146 unsigned int seq;
1147 unsigned int dif;
1148 struct zoran_buffer *buffer;
1149 int frame;
1150
1151 /* In motion decompress we don't have a hardware frame counter,
1152 * we just count the interrupts here */
1153
1154 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1155 zr->jpg_seq_num++;
1156 }
1157 while (zr->jpg_dma_tail < zr->jpg_dma_head) {
1158 if (zr->jpg_settings.TmpDcm == 1)
1159 i = (zr->jpg_dma_tail -
1160 zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1161 else
1162 i = ((zr->jpg_dma_tail -
1163 zr->jpg_err_shift) & 1) * 2 + 1;
1164
1165 stat_com = le32_to_cpu(zr->stat_com[i]);
1166
1167 if ((stat_com & 1) == 0) {
1168 return;
1169 }
1170 frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1171 buffer = &zr->jpg_buffers.buffer[frame];
1172 do_gettimeofday(&buffer->bs.timestamp);
1173
1174 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1175 buffer->bs.length = (stat_com & 0x7fffff) >> 1;
1176
1177 /* update sequence number with the help of the counter in stat_com */
1178
1179 seq = ((stat_com >> 24) + zr->jpg_err_seq) & 0xff;
1180 dif = (seq - zr->jpg_seq_num) & 0xff;
1181 zr->jpg_seq_num += dif;
1182 } else {
1183 buffer->bs.length = 0;
1184 }
1185 buffer->bs.seq =
1186 zr->jpg_settings.TmpDcm ==
1187 2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
1188 buffer->state = BUZ_STATE_DONE;
1189
1190 zr->jpg_dma_tail++;
1191 }
1192}
1193
1194static void zoran_restart(struct zoran *zr)
1195{
1196 /* Now the stat_comm buffer is ready for restart */
1197 unsigned int status = 0;
1198 int mode;
1199
1200 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1201 decoder_call(zr, video, g_input_status, &status);
1202 mode = CODEC_DO_COMPRESSION;
1203 } else {
1204 status = V4L2_IN_ST_NO_SIGNAL;
1205 mode = CODEC_DO_EXPANSION;
1206 }
1207 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1208 !(status & V4L2_IN_ST_NO_SIGNAL)) {
1209 /********** RESTART code *************/
1210 jpeg_codec_reset(zr);
1211 zr->codec->set_mode(zr->codec, mode);
1212 zr36057_set_jpg(zr, zr->codec_mode);
1213 jpeg_start(zr);
1214
1215 if (zr->num_errors <= 8)
1216 dprintk(2, KERN_INFO "%s: Restart\n",
1217 ZR_DEVNAME(zr));
1218
1219 zr->JPEG_missed = 0;
1220 zr->JPEG_error = 2;
1221 /********** End RESTART code ***********/
1222 }
1223}
1224
1225static void
1226error_handler (struct zoran *zr,
1227 u32 astat,
1228 u32 stat)
1229{
1230 int i;
1231
1232 /* This is JPEG error handling part */
1233 if (zr->codec_mode != BUZ_MODE_MOTION_COMPRESS &&
1234 zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS) {
1235 return;
1236 }
1237
1238 if ((stat & 1) == 0 &&
1239 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS &&
1240 zr->jpg_dma_tail - zr->jpg_que_tail >= zr->jpg_buffers.num_buffers) {
1241 /* No free buffers... */
1242 zoran_reap_stat_com(zr);
1243 zoran_feed_stat_com(zr);
1244 wake_up_interruptible(&zr->jpg_capq);
1245 zr->JPEG_missed = 0;
1246 return;
1247 }
1248
1249 if (zr->JPEG_error == 1) {
1250 zoran_restart(zr);
1251 return;
1252 }
1253
1254 /*
1255 * First entry: error just happened during normal operation
1256 *
1257 * In BUZ_MODE_MOTION_COMPRESS:
1258 *
1259 * Possible glitch in TV signal. In this case we should
1260 * stop the codec and wait for good quality signal before
1261 * restarting it to avoid further problems
1262 *
1263 * In BUZ_MODE_MOTION_DECOMPRESS:
1264 *
1265 * Bad JPEG frame: we have to mark it as processed (codec crashed
1266 * and was not able to do it itself), and to remove it from queue.
1267 */
1268 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1269 udelay(1);
1270 stat = stat | (post_office_read(zr, 7, 0) & 3) << 8;
1271 btwrite(0, ZR36057_JPC);
1272 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1273 jpeg_codec_reset(zr);
1274 jpeg_codec_sleep(zr, 1);
1275 zr->JPEG_error = 1;
1276 zr->num_errors++;
1277
1278 /* Report error */
1279 if (zr36067_debug > 1 && zr->num_errors <= 8) {
1280 long frame;
1281 int j;
1282
1283 frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1284 printk(KERN_ERR
1285 "%s: JPEG error stat=0x%08x(0x%08x) queue_state=%ld/%ld/%ld/%ld seq=%ld frame=%ld. Codec stopped. ",
1286 ZR_DEVNAME(zr), stat, zr->last_isr,
1287 zr->jpg_que_tail, zr->jpg_dma_tail,
1288 zr->jpg_dma_head, zr->jpg_que_head,
1289 zr->jpg_seq_num, frame);
1290 printk(KERN_INFO "stat_com frames:");
1291 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1292 for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
1293 if (le32_to_cpu(zr->stat_com[j]) == zr->jpg_buffers.buffer[i].jpg.frag_tab_bus)
1294 printk(KERN_CONT "% d->%d", j, i);
1295 }
1296 }
1297 printk(KERN_CONT "\n");
1298 }
1299 /* Find an entry in stat_com and rotate contents */
1300 if (zr->jpg_settings.TmpDcm == 1)
1301 i = (zr->jpg_dma_tail - zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1302 else
1303 i = ((zr->jpg_dma_tail - zr->jpg_err_shift) & 1) * 2;
1304 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1305 /* Mimic zr36067 operation */
1306 zr->stat_com[i] |= cpu_to_le32(1);
1307 if (zr->jpg_settings.TmpDcm != 1)
1308 zr->stat_com[i + 1] |= cpu_to_le32(1);
1309 /* Refill */
1310 zoran_reap_stat_com(zr);
1311 zoran_feed_stat_com(zr);
1312 wake_up_interruptible(&zr->jpg_capq);
1313 /* Find an entry in stat_com again after refill */
1314 if (zr->jpg_settings.TmpDcm == 1)
1315 i = (zr->jpg_dma_tail - zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1316 else
1317 i = ((zr->jpg_dma_tail - zr->jpg_err_shift) & 1) * 2;
1318 }
1319 if (i) {
1320 /* Rotate stat_comm entries to make current entry first */
1321 int j;
1322 __le32 bus_addr[BUZ_NUM_STAT_COM];
1323
1324 /* Here we are copying the stat_com array, which
1325 * is already in little endian format, so
1326 * no endian conversions here
1327 */
1328 memcpy(bus_addr, zr->stat_com, sizeof(bus_addr));
1329
1330 for (j = 0; j < BUZ_NUM_STAT_COM; j++)
1331 zr->stat_com[j] = bus_addr[(i + j) & BUZ_MASK_STAT_COM];
1332
1333 zr->jpg_err_shift += i;
1334 zr->jpg_err_shift &= BUZ_MASK_STAT_COM;
1335 }
1336 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
1337 zr->jpg_err_seq = zr->jpg_seq_num; /* + 1; */
1338 zoran_restart(zr);
1339}
1340
1341irqreturn_t
1342zoran_irq (int irq,
1343 void *dev_id)
1344{
1345 u32 stat, astat;
1346 int count;
1347 struct zoran *zr;
1348 unsigned long flags;
1349
1350 zr = dev_id;
1351 count = 0;
1352
1353 if (zr->testing) {
1354 /* Testing interrupts */
1355 spin_lock_irqsave(&zr->spinlock, flags);
1356 while ((stat = count_reset_interrupt(zr))) {
1357 if (count++ > 100) {
1358 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1359 dprintk(1,
1360 KERN_ERR
1361 "%s: IRQ lockup while testing, isr=0x%08x, cleared int mask\n",
1362 ZR_DEVNAME(zr), stat);
1363 wake_up_interruptible(&zr->test_q);
1364 }
1365 }
1366 zr->last_isr = stat;
1367 spin_unlock_irqrestore(&zr->spinlock, flags);
1368 return IRQ_HANDLED;
1369 }
1370
1371 spin_lock_irqsave(&zr->spinlock, flags);
1372 while (1) {
1373 /* get/clear interrupt status bits */
1374 stat = count_reset_interrupt(zr);
1375 astat = stat & IRQ_MASK;
1376 if (!astat) {
1377 break;
1378 }
1379 dprintk(4,
1380 KERN_DEBUG
1381 "zoran_irq: astat: 0x%08x, mask: 0x%08x\n",
1382 astat, btread(ZR36057_ICR));
1383 if (astat & zr->card.vsync_int) { // SW
1384
1385 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1386 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1387 /* count missed interrupts */
1388 zr->JPEG_missed++;
1389 }
1390 //post_office_read(zr,1,0);
1391 /* Interrupts may still happen when
1392 * zr->v4l_memgrab_active is switched off.
1393 * We simply ignore them */
1394
1395 if (zr->v4l_memgrab_active) {
1396 /* A lot more checks should be here ... */
1397 if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot) == 0)
1398 dprintk(1,
1399 KERN_WARNING
1400 "%s: BuzIRQ with SnapShot off ???\n",
1401 ZR_DEVNAME(zr));
1402
1403 if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
1404 /* There is a grab on a frame going on, check if it has finished */
1405 if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_FrameGrab) == 0) {
1406 /* it is finished, notify the user */
1407
1408 zr->v4l_buffers.buffer[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
1409 zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.seq = zr->v4l_grab_seq;
1410 do_gettimeofday(&zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.timestamp);
1411 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
1412 zr->v4l_pend_tail++;
1413 }
1414 }
1415
1416 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
1417 wake_up_interruptible(&zr->v4l_capq);
1418
1419 /* Check if there is another grab queued */
1420
1421 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
1422 zr->v4l_pend_tail != zr->v4l_pend_head) {
1423 int frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
1424 u32 reg;
1425
1426 zr->v4l_grab_frame = frame;
1427
1428 /* Set zr36057 video front end and enable video */
1429
1430 /* Buffer address */
1431
1432 reg = zr->v4l_buffers.buffer[frame].v4l.fbuffer_bus;
1433 btwrite(reg, ZR36057_VDTR);
1434 if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
1435 reg += zr->v4l_settings.bytesperline;
1436 btwrite(reg, ZR36057_VDBR);
1437
1438 /* video stride, status, and frame grab register */
1439 reg = 0;
1440 if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
1441 reg += zr->v4l_settings.bytesperline;
1442 reg = (reg << ZR36057_VSSFGR_DispStride);
1443 reg |= ZR36057_VSSFGR_VidOvf;
1444 reg |= ZR36057_VSSFGR_SnapShot;
1445 reg |= ZR36057_VSSFGR_FrameGrab;
1446 btwrite(reg, ZR36057_VSSFGR);
1447
1448 btor(ZR36057_VDCR_VidEn,
1449 ZR36057_VDCR);
1450 }
1451 }
1452
1453 /* even if we don't grab, we do want to increment
1454 * the sequence counter to see lost frames */
1455 zr->v4l_grab_seq++;
1456 }
1457#if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
1458 if (astat & ZR36057_ISR_CodRepIRQ) {
1459 zr->intr_counter_CodRepIRQ++;
1460 IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n",
1461 ZR_DEVNAME(zr)));
1462 btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
1463 }
1464#endif /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
1465
1466#if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
1467 if ((astat & ZR36057_ISR_JPEGRepIRQ) &&
1468 (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1469 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)) {
1470 if (zr36067_debug > 1 && (!zr->frame_num || zr->JPEG_error)) {
1471 char sv[BUZ_NUM_STAT_COM + 1];
1472 int i;
1473
1474 printk(KERN_INFO
1475 "%s: first frame ready: state=0x%08x odd_even=%d field_per_buff=%d delay=%d\n",
1476 ZR_DEVNAME(zr), stat,
1477 zr->jpg_settings.odd_even,
1478 zr->jpg_settings.field_per_buff,
1479 zr->JPEG_missed);
1480
1481 for (i = 0; i < BUZ_NUM_STAT_COM; i++)
1482 sv[i] = le32_to_cpu(zr->stat_com[i]) & 1 ? '1' : '0';
1483 sv[BUZ_NUM_STAT_COM] = 0;
1484 printk(KERN_INFO
1485 "%s: stat_com=%s queue_state=%ld/%ld/%ld/%ld\n",
1486 ZR_DEVNAME(zr), sv,
1487 zr->jpg_que_tail,
1488 zr->jpg_dma_tail,
1489 zr->jpg_dma_head,
1490 zr->jpg_que_head);
1491 } else {
1492 /* Get statistics */
1493 if (zr->JPEG_missed > zr->JPEG_max_missed)
1494 zr->JPEG_max_missed = zr->JPEG_missed;
1495 if (zr->JPEG_missed < zr->JPEG_min_missed)
1496 zr->JPEG_min_missed = zr->JPEG_missed;
1497 }
1498
1499 if (zr36067_debug > 2 && zr->frame_num < 6) {
1500 int i;
1501
1502 printk(KERN_INFO "%s: seq=%ld stat_com:",
1503 ZR_DEVNAME(zr), zr->jpg_seq_num);
1504 for (i = 0; i < 4; i++) {
1505 printk(KERN_CONT " %08x",
1506 le32_to_cpu(zr->stat_com[i]));
1507 }
1508 printk(KERN_CONT "\n");
1509 }
1510 zr->frame_num++;
1511 zr->JPEG_missed = 0;
1512 zr->JPEG_error = 0;
1513 zoran_reap_stat_com(zr);
1514 zoran_feed_stat_com(zr);
1515 wake_up_interruptible(&zr->jpg_capq);
1516 }
1517#endif /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
1518
1519 /* DATERR, too many fields missed, error processing */
1520 if ((astat & zr->card.jpeg_int) ||
1521 zr->JPEG_missed > 25 ||
1522 zr->JPEG_error == 1 ||
1523 ((zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) &&
1524 (zr->frame_num && (zr->JPEG_missed > zr->jpg_settings.field_per_buff)))) {
1525 error_handler(zr, astat, stat);
1526 }
1527
1528 count++;
1529 if (count > 10) {
1530 dprintk(2, KERN_WARNING "%s: irq loop %d\n",
1531 ZR_DEVNAME(zr), count);
1532 if (count > 20) {
1533 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1534 dprintk(2,
1535 KERN_ERR
1536 "%s: IRQ lockup, cleared int mask\n",
1537 ZR_DEVNAME(zr));
1538 break;
1539 }
1540 }
1541 zr->last_isr = stat;
1542 }
1543 spin_unlock_irqrestore(&zr->spinlock, flags);
1544
1545 return IRQ_HANDLED;
1546}
1547
1548void
1549zoran_set_pci_master (struct zoran *zr,
1550 int set_master)
1551{
1552 if (set_master) {
1553 pci_set_master(zr->pci_dev);
1554 } else {
1555 u16 command;
1556
1557 pci_read_config_word(zr->pci_dev, PCI_COMMAND, &command);
1558 command &= ~PCI_COMMAND_MASTER;
1559 pci_write_config_word(zr->pci_dev, PCI_COMMAND, command);
1560 }
1561}
1562
1563void
1564zoran_init_hardware (struct zoran *zr)
1565{
1566 /* Enable bus-mastering */
1567 zoran_set_pci_master(zr, 1);
1568
1569 /* Initialize the board */
1570 if (zr->card.init) {
1571 zr->card.init(zr);
1572 }
1573
1574 decoder_call(zr, core, init, 0);
1575 decoder_call(zr, core, s_std, zr->norm);
1576 decoder_call(zr, video, s_routing,
1577 zr->card.input[zr->input].muxsel, 0, 0);
1578
1579 encoder_call(zr, core, init, 0);
1580 encoder_call(zr, video, s_std_output, zr->norm);
1581 encoder_call(zr, video, s_routing, 0, 0, 0);
1582
1583 /* toggle JPEG codec sleep to sync PLL */
1584 jpeg_codec_sleep(zr, 1);
1585 jpeg_codec_sleep(zr, 0);
1586
1587 /* set individual interrupt enables (without GIRQ1)
1588 * but don't global enable until zoran_open() */
1589
1590 //btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ1, ZR36057_ICR); // SW
1591 // It looks like using only JPEGRepIRQEn is not always reliable,
1592 // may be when JPEG codec crashes it won't generate IRQ? So,
1593 /*CP*/ // btwrite(IRQ_MASK, ZR36057_ICR); // Enable Vsync interrupts too. SM WHY ? LP
1594 zr36057_init_vfe(zr);
1595
1596 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1597
1598 btwrite(IRQ_MASK, ZR36057_ISR); // Clears interrupts
1599}
1600
1601void
1602zr36057_restart (struct zoran *zr)
1603{
1604 btwrite(0, ZR36057_SPGPPCR);
1605 mdelay(1);
1606 btor(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
1607 mdelay(1);
1608
1609 /* assert P_Reset */
1610 btwrite(0, ZR36057_JPC);
1611 /* set up GPIO direction - all output */
1612 btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
1613
1614 /* set up GPIO pins and guest bus timing */
1615 btwrite((0x81 << 24) | 0x8888, ZR36057_GPPGCR1);
1616}
1617
1618/*
1619 * initialize video front end
1620 */
1621
1622static void
1623zr36057_init_vfe (struct zoran *zr)
1624{
1625 u32 reg;
1626
1627 reg = btread(ZR36057_VFESPFR);
1628 reg |= ZR36057_VFESPFR_LittleEndian;
1629 reg &= ~ZR36057_VFESPFR_VCLKPol;
1630 reg |= ZR36057_VFESPFR_ExtFl;
1631 reg |= ZR36057_VFESPFR_TopField;
1632 btwrite(reg, ZR36057_VFESPFR);
1633 reg = btread(ZR36057_VDCR);
1634 if (pci_pci_problems & PCIPCI_TRITON)
1635 // || zr->revision < 1) // Revision 1 has also Triton support
1636 reg &= ~ZR36057_VDCR_Triton;
1637 else
1638 reg |= ZR36057_VDCR_Triton;
1639 btwrite(reg, ZR36057_VDCR);
1640}
diff --git a/drivers/media/pci/zoran/zoran_device.h b/drivers/media/pci/zoran/zoran_device.h
new file mode 100644
index 000000000000..07f2c23ff740
--- /dev/null
+++ b/drivers/media/pci/zoran/zoran_device.h
@@ -0,0 +1,95 @@
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles card-specific data and detection
7 *
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 * Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Mailinglist <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#ifndef __ZORAN_DEVICE_H__
31#define __ZORAN_DEVICE_H__
32
33/* general purpose I/O */
34extern void GPIO(struct zoran *zr,
35 int bit,
36 unsigned int value);
37
38/* codec (or actually: guest bus) access */
39extern int post_office_wait(struct zoran *zr);
40extern int post_office_write(struct zoran *zr,
41 unsigned guest,
42 unsigned reg,
43 unsigned value);
44extern int post_office_read(struct zoran *zr,
45 unsigned guest,
46 unsigned reg);
47
48extern void detect_guest_activity(struct zoran *zr);
49
50extern void jpeg_codec_sleep(struct zoran *zr,
51 int sleep);
52extern int jpeg_codec_reset(struct zoran *zr);
53
54/* zr360x7 access to raw capture */
55extern void zr36057_overlay(struct zoran *zr,
56 int on);
57extern void write_overlay_mask(struct zoran_fh *fh,
58 struct v4l2_clip *vp,
59 int count);
60extern void zr36057_set_memgrab(struct zoran *zr,
61 int mode);
62extern int wait_grab_pending(struct zoran *zr);
63
64/* interrupts */
65extern void print_interrupts(struct zoran *zr);
66extern void clear_interrupt_counters(struct zoran *zr);
67extern irqreturn_t zoran_irq(int irq, void *dev_id);
68
69/* JPEG codec access */
70extern void jpeg_start(struct zoran *zr);
71extern void zr36057_enable_jpg(struct zoran *zr,
72 enum zoran_codec_mode mode);
73extern void zoran_feed_stat_com(struct zoran *zr);
74
75/* general */
76extern void zoran_set_pci_master(struct zoran *zr,
77 int set_master);
78extern void zoran_init_hardware(struct zoran *zr);
79extern void zr36057_restart(struct zoran *zr);
80
81extern const struct zoran_format zoran_formats[];
82
83extern int v4l_nbufs;
84extern int v4l_bufsize;
85extern int jpg_nbufs;
86extern int jpg_bufsize;
87extern int pass_through;
88
89/* i2c */
90#define decoder_call(zr, o, f, args...) \
91 v4l2_subdev_call(zr->decoder, o, f, ##args)
92#define encoder_call(zr, o, f, args...) \
93 v4l2_subdev_call(zr->encoder, o, f, ##args)
94
95#endif /* __ZORAN_DEVICE_H__ */
diff --git a/drivers/media/pci/zoran/zoran_driver.c b/drivers/media/pci/zoran/zoran_driver.c
new file mode 100644
index 000000000000..c6ccdeb6d8d6
--- /dev/null
+++ b/drivers/media/pci/zoran/zoran_driver.c
@@ -0,0 +1,3090 @@
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7 *
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
9 *
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
11 *
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
13 *
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
15 *
16 * Based on
17 *
18 * Miro DC10 driver
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
20 *
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
23 *
24 * buz.0.0.3
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
26 *
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
30 *
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 */
46
47#include <linux/init.h>
48#include <linux/module.h>
49#include <linux/delay.h>
50#include <linux/slab.h>
51#include <linux/pci.h>
52#include <linux/vmalloc.h>
53#include <linux/wait.h>
54
55#include <linux/interrupt.h>
56#include <linux/i2c.h>
57#include <linux/i2c-algo-bit.h>
58
59#include <linux/spinlock.h>
60
61#include <linux/videodev2.h>
62#include <media/v4l2-common.h>
63#include <media/v4l2-ioctl.h>
64#include "videocodec.h"
65
66#include <asm/byteorder.h>
67#include <asm/io.h>
68#include <asm/uaccess.h>
69#include <linux/proc_fs.h>
70
71#include <linux/mutex.h>
72#include "zoran.h"
73#include "zoran_device.h"
74#include "zoran_card.h"
75
76
77const struct zoran_format zoran_formats[] = {
78 {
79 .name = "15-bit RGB LE",
80 .fourcc = V4L2_PIX_FMT_RGB555,
81 .colorspace = V4L2_COLORSPACE_SRGB,
82 .depth = 15,
83 .flags = ZORAN_FORMAT_CAPTURE |
84 ZORAN_FORMAT_OVERLAY,
85 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
86 ZR36057_VFESPFR_LittleEndian,
87 }, {
88 .name = "15-bit RGB BE",
89 .fourcc = V4L2_PIX_FMT_RGB555X,
90 .colorspace = V4L2_COLORSPACE_SRGB,
91 .depth = 15,
92 .flags = ZORAN_FORMAT_CAPTURE |
93 ZORAN_FORMAT_OVERLAY,
94 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
95 }, {
96 .name = "16-bit RGB LE",
97 .fourcc = V4L2_PIX_FMT_RGB565,
98 .colorspace = V4L2_COLORSPACE_SRGB,
99 .depth = 16,
100 .flags = ZORAN_FORMAT_CAPTURE |
101 ZORAN_FORMAT_OVERLAY,
102 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
103 ZR36057_VFESPFR_LittleEndian,
104 }, {
105 .name = "16-bit RGB BE",
106 .fourcc = V4L2_PIX_FMT_RGB565X,
107 .colorspace = V4L2_COLORSPACE_SRGB,
108 .depth = 16,
109 .flags = ZORAN_FORMAT_CAPTURE |
110 ZORAN_FORMAT_OVERLAY,
111 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
112 }, {
113 .name = "24-bit RGB",
114 .fourcc = V4L2_PIX_FMT_BGR24,
115 .colorspace = V4L2_COLORSPACE_SRGB,
116 .depth = 24,
117 .flags = ZORAN_FORMAT_CAPTURE |
118 ZORAN_FORMAT_OVERLAY,
119 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
120 }, {
121 .name = "32-bit RGB LE",
122 .fourcc = V4L2_PIX_FMT_BGR32,
123 .colorspace = V4L2_COLORSPACE_SRGB,
124 .depth = 32,
125 .flags = ZORAN_FORMAT_CAPTURE |
126 ZORAN_FORMAT_OVERLAY,
127 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
128 }, {
129 .name = "32-bit RGB BE",
130 .fourcc = V4L2_PIX_FMT_RGB32,
131 .colorspace = V4L2_COLORSPACE_SRGB,
132 .depth = 32,
133 .flags = ZORAN_FORMAT_CAPTURE |
134 ZORAN_FORMAT_OVERLAY,
135 .vfespfr = ZR36057_VFESPFR_RGB888,
136 }, {
137 .name = "4:2:2, packed, YUYV",
138 .fourcc = V4L2_PIX_FMT_YUYV,
139 .colorspace = V4L2_COLORSPACE_SMPTE170M,
140 .depth = 16,
141 .flags = ZORAN_FORMAT_CAPTURE |
142 ZORAN_FORMAT_OVERLAY,
143 .vfespfr = ZR36057_VFESPFR_YUV422,
144 }, {
145 .name = "4:2:2, packed, UYVY",
146 .fourcc = V4L2_PIX_FMT_UYVY,
147 .colorspace = V4L2_COLORSPACE_SMPTE170M,
148 .depth = 16,
149 .flags = ZORAN_FORMAT_CAPTURE |
150 ZORAN_FORMAT_OVERLAY,
151 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
152 }, {
153 .name = "Hardware-encoded Motion-JPEG",
154 .fourcc = V4L2_PIX_FMT_MJPEG,
155 .colorspace = V4L2_COLORSPACE_SMPTE170M,
156 .depth = 0,
157 .flags = ZORAN_FORMAT_CAPTURE |
158 ZORAN_FORMAT_PLAYBACK |
159 ZORAN_FORMAT_COMPRESSED,
160 }
161};
162#define NUM_FORMATS ARRAY_SIZE(zoran_formats)
163
164 /* small helper function for calculating buffersizes for v4l2
165 * we calculate the nearest higher power-of-two, which
166 * will be the recommended buffersize */
167static __u32
168zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
169{
170 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
171 __u32 num = (1024 * 512) / (div);
172 __u32 result = 2;
173
174 num--;
175 while (num) {
176 num >>= 1;
177 result <<= 1;
178 }
179
180 if (result > jpg_bufsize)
181 return jpg_bufsize;
182 if (result < 8192)
183 return 8192;
184 return result;
185}
186
187/* forward references */
188static void v4l_fbuffer_free(struct zoran_fh *fh);
189static void jpg_fbuffer_free(struct zoran_fh *fh);
190
191/* Set mapping mode */
192static void map_mode_raw(struct zoran_fh *fh)
193{
194 fh->map_mode = ZORAN_MAP_MODE_RAW;
195 fh->buffers.buffer_size = v4l_bufsize;
196 fh->buffers.num_buffers = v4l_nbufs;
197}
198static void map_mode_jpg(struct zoran_fh *fh, int play)
199{
200 fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
201 fh->buffers.buffer_size = jpg_bufsize;
202 fh->buffers.num_buffers = jpg_nbufs;
203}
204static inline const char *mode_name(enum zoran_map_mode mode)
205{
206 return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
207}
208
209/*
210 * Allocate the V4L grab buffers
211 *
212 * These have to be pysically contiguous.
213 */
214
215static int v4l_fbuffer_alloc(struct zoran_fh *fh)
216{
217 struct zoran *zr = fh->zr;
218 int i, off;
219 unsigned char *mem;
220
221 for (i = 0; i < fh->buffers.num_buffers; i++) {
222 if (fh->buffers.buffer[i].v4l.fbuffer)
223 dprintk(2,
224 KERN_WARNING
225 "%s: %s - buffer %d already allocated!?\n",
226 ZR_DEVNAME(zr), __func__, i);
227
228 //udelay(20);
229 mem = kmalloc(fh->buffers.buffer_size,
230 GFP_KERNEL | __GFP_NOWARN);
231 if (!mem) {
232 dprintk(1,
233 KERN_ERR
234 "%s: %s - kmalloc for V4L buf %d failed\n",
235 ZR_DEVNAME(zr), __func__, i);
236 v4l_fbuffer_free(fh);
237 return -ENOBUFS;
238 }
239 fh->buffers.buffer[i].v4l.fbuffer = mem;
240 fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
241 fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
242 for (off = 0; off < fh->buffers.buffer_size;
243 off += PAGE_SIZE)
244 SetPageReserved(virt_to_page(mem + off));
245 dprintk(4,
246 KERN_INFO
247 "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
248 ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
249 (unsigned long long)virt_to_bus(mem));
250 }
251
252 fh->buffers.allocated = 1;
253
254 return 0;
255}
256
257/* free the V4L grab buffers */
258static void v4l_fbuffer_free(struct zoran_fh *fh)
259{
260 struct zoran *zr = fh->zr;
261 int i, off;
262 unsigned char *mem;
263
264 dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
265
266 for (i = 0; i < fh->buffers.num_buffers; i++) {
267 if (!fh->buffers.buffer[i].v4l.fbuffer)
268 continue;
269
270 mem = fh->buffers.buffer[i].v4l.fbuffer;
271 for (off = 0; off < fh->buffers.buffer_size;
272 off += PAGE_SIZE)
273 ClearPageReserved(virt_to_page(mem + off));
274 kfree(fh->buffers.buffer[i].v4l.fbuffer);
275 fh->buffers.buffer[i].v4l.fbuffer = NULL;
276 }
277
278 fh->buffers.allocated = 0;
279}
280
281/*
282 * Allocate the MJPEG grab buffers.
283 *
284 * If a Natoma chipset is present and this is a revision 1 zr36057,
285 * each MJPEG buffer needs to be physically contiguous.
286 * (RJ: This statement is from Dave Perks' original driver,
287 * I could never check it because I have a zr36067)
288 *
289 * RJ: The contents grab buffers needs never be accessed in the driver.
290 * Therefore there is no need to allocate them with vmalloc in order
291 * to get a contiguous virtual memory space.
292 * I don't understand why many other drivers first allocate them with
293 * vmalloc (which uses internally also get_zeroed_page, but delivers you
294 * virtual addresses) and then again have to make a lot of efforts
295 * to get the physical address.
296 *
297 * Ben Capper:
298 * On big-endian architectures (such as ppc) some extra steps
299 * are needed. When reading and writing to the stat_com array
300 * and fragment buffers, the device expects to see little-
301 * endian values. The use of cpu_to_le32() and le32_to_cpu()
302 * in this function (and one or two others in zoran_device.c)
303 * ensure that these values are always stored in little-endian
304 * form, regardless of architecture. The zr36057 does Very Bad
305 * Things on big endian architectures if the stat_com array
306 * and fragment buffers are not little-endian.
307 */
308
309static int jpg_fbuffer_alloc(struct zoran_fh *fh)
310{
311 struct zoran *zr = fh->zr;
312 int i, j, off;
313 u8 *mem;
314
315 for (i = 0; i < fh->buffers.num_buffers; i++) {
316 if (fh->buffers.buffer[i].jpg.frag_tab)
317 dprintk(2,
318 KERN_WARNING
319 "%s: %s - buffer %d already allocated!?\n",
320 ZR_DEVNAME(zr), __func__, i);
321
322 /* Allocate fragment table for this buffer */
323
324 mem = (void *)get_zeroed_page(GFP_KERNEL);
325 if (!mem) {
326 dprintk(1,
327 KERN_ERR
328 "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
329 ZR_DEVNAME(zr), __func__, i);
330 jpg_fbuffer_free(fh);
331 return -ENOBUFS;
332 }
333 fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
334 fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
335
336 if (fh->buffers.need_contiguous) {
337 mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
338 if (mem == NULL) {
339 dprintk(1,
340 KERN_ERR
341 "%s: %s - kmalloc failed for buffer %d\n",
342 ZR_DEVNAME(zr), __func__, i);
343 jpg_fbuffer_free(fh);
344 return -ENOBUFS;
345 }
346 fh->buffers.buffer[i].jpg.frag_tab[0] =
347 cpu_to_le32(virt_to_bus(mem));
348 fh->buffers.buffer[i].jpg.frag_tab[1] =
349 cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
350 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
351 SetPageReserved(virt_to_page(mem + off));
352 } else {
353 /* jpg_bufsize is already page aligned */
354 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
355 mem = (void *)get_zeroed_page(GFP_KERNEL);
356 if (mem == NULL) {
357 dprintk(1,
358 KERN_ERR
359 "%s: %s - get_zeroed_page failed for buffer %d\n",
360 ZR_DEVNAME(zr), __func__, i);
361 jpg_fbuffer_free(fh);
362 return -ENOBUFS;
363 }
364
365 fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
366 cpu_to_le32(virt_to_bus(mem));
367 fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
368 cpu_to_le32((PAGE_SIZE >> 2) << 1);
369 SetPageReserved(virt_to_page(mem));
370 }
371
372 fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
373 }
374 }
375
376 dprintk(4,
377 KERN_DEBUG "%s: %s - %d KB allocated\n",
378 ZR_DEVNAME(zr), __func__,
379 (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
380
381 fh->buffers.allocated = 1;
382
383 return 0;
384}
385
386/* free the MJPEG grab buffers */
387static void jpg_fbuffer_free(struct zoran_fh *fh)
388{
389 struct zoran *zr = fh->zr;
390 int i, j, off;
391 unsigned char *mem;
392 __le32 frag_tab;
393 struct zoran_buffer *buffer;
394
395 dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
396
397 for (i = 0, buffer = &fh->buffers.buffer[0];
398 i < fh->buffers.num_buffers; i++, buffer++) {
399 if (!buffer->jpg.frag_tab)
400 continue;
401
402 if (fh->buffers.need_contiguous) {
403 frag_tab = buffer->jpg.frag_tab[0];
404
405 if (frag_tab) {
406 mem = bus_to_virt(le32_to_cpu(frag_tab));
407 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
408 ClearPageReserved(virt_to_page(mem + off));
409 kfree(mem);
410 buffer->jpg.frag_tab[0] = 0;
411 buffer->jpg.frag_tab[1] = 0;
412 }
413 } else {
414 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
415 frag_tab = buffer->jpg.frag_tab[2 * j];
416
417 if (!frag_tab)
418 break;
419 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
420 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
421 buffer->jpg.frag_tab[2 * j] = 0;
422 buffer->jpg.frag_tab[2 * j + 1] = 0;
423 }
424 }
425
426 free_page((unsigned long)buffer->jpg.frag_tab);
427 buffer->jpg.frag_tab = NULL;
428 }
429
430 fh->buffers.allocated = 0;
431}
432
433/*
434 * V4L Buffer grabbing
435 */
436
437static int
438zoran_v4l_set_format (struct zoran_fh *fh,
439 int width,
440 int height,
441 const struct zoran_format *format)
442{
443 struct zoran *zr = fh->zr;
444 int bpp;
445
446 /* Check size and format of the grab wanted */
447
448 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
449 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
450 dprintk(1,
451 KERN_ERR
452 "%s: %s - wrong frame size (%dx%d)\n",
453 ZR_DEVNAME(zr), __func__, width, height);
454 return -EINVAL;
455 }
456
457 bpp = (format->depth + 7) / 8;
458
459 /* Check against available buffer size */
460 if (height * width * bpp > fh->buffers.buffer_size) {
461 dprintk(1,
462 KERN_ERR
463 "%s: %s - video buffer size (%d kB) is too small\n",
464 ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
465 return -EINVAL;
466 }
467
468 /* The video front end needs 4-byte alinged line sizes */
469
470 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
471 dprintk(1,
472 KERN_ERR
473 "%s: %s - wrong frame alignment\n",
474 ZR_DEVNAME(zr), __func__);
475 return -EINVAL;
476 }
477
478 fh->v4l_settings.width = width;
479 fh->v4l_settings.height = height;
480 fh->v4l_settings.format = format;
481 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
482
483 return 0;
484}
485
486static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
487{
488 struct zoran *zr = fh->zr;
489 unsigned long flags;
490 int res = 0;
491
492 if (!fh->buffers.allocated) {
493 dprintk(1,
494 KERN_ERR
495 "%s: %s - buffers not yet allocated\n",
496 ZR_DEVNAME(zr), __func__);
497 res = -ENOMEM;
498 }
499
500 /* No grabbing outside the buffer range! */
501 if (num >= fh->buffers.num_buffers || num < 0) {
502 dprintk(1,
503 KERN_ERR
504 "%s: %s - buffer %d is out of range\n",
505 ZR_DEVNAME(zr), __func__, num);
506 res = -EINVAL;
507 }
508
509 spin_lock_irqsave(&zr->spinlock, flags);
510
511 if (fh->buffers.active == ZORAN_FREE) {
512 if (zr->v4l_buffers.active == ZORAN_FREE) {
513 zr->v4l_buffers = fh->buffers;
514 fh->buffers.active = ZORAN_ACTIVE;
515 } else {
516 dprintk(1,
517 KERN_ERR
518 "%s: %s - another session is already capturing\n",
519 ZR_DEVNAME(zr), __func__);
520 res = -EBUSY;
521 }
522 }
523
524 /* make sure a grab isn't going on currently with this buffer */
525 if (!res) {
526 switch (zr->v4l_buffers.buffer[num].state) {
527 default:
528 case BUZ_STATE_PEND:
529 if (zr->v4l_buffers.active == ZORAN_FREE) {
530 fh->buffers.active = ZORAN_FREE;
531 zr->v4l_buffers.allocated = 0;
532 }
533 res = -EBUSY; /* what are you doing? */
534 break;
535 case BUZ_STATE_DONE:
536 dprintk(2,
537 KERN_WARNING
538 "%s: %s - queueing buffer %d in state DONE!?\n",
539 ZR_DEVNAME(zr), __func__, num);
540 case BUZ_STATE_USER:
541 /* since there is at least one unused buffer there's room for at least
542 * one more pend[] entry */
543 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
544 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
545 zr->v4l_buffers.buffer[num].bs.length =
546 fh->v4l_settings.bytesperline *
547 zr->v4l_settings.height;
548 fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
549 break;
550 }
551 }
552
553 spin_unlock_irqrestore(&zr->spinlock, flags);
554
555 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
556 zr->v4l_buffers.active = fh->buffers.active;
557
558 return res;
559}
560
561/*
562 * Sync on a V4L buffer
563 */
564
565static int v4l_sync(struct zoran_fh *fh, int frame)
566{
567 struct zoran *zr = fh->zr;
568 unsigned long flags;
569
570 if (fh->buffers.active == ZORAN_FREE) {
571 dprintk(1,
572 KERN_ERR
573 "%s: %s - no grab active for this session\n",
574 ZR_DEVNAME(zr), __func__);
575 return -EINVAL;
576 }
577
578 /* check passed-in frame number */
579 if (frame >= fh->buffers.num_buffers || frame < 0) {
580 dprintk(1,
581 KERN_ERR "%s: %s - frame %d is invalid\n",
582 ZR_DEVNAME(zr), __func__, frame);
583 return -EINVAL;
584 }
585
586 /* Check if is buffer was queued at all */
587 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
588 dprintk(1,
589 KERN_ERR
590 "%s: %s - attempt to sync on a buffer which was not queued?\n",
591 ZR_DEVNAME(zr), __func__);
592 return -EPROTO;
593 }
594
595 /* wait on this buffer to get ready */
596 if (!wait_event_interruptible_timeout(zr->v4l_capq,
597 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
598 return -ETIME;
599 if (signal_pending(current))
600 return -ERESTARTSYS;
601
602 /* buffer should now be in BUZ_STATE_DONE */
603 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
604 dprintk(2,
605 KERN_ERR "%s: %s - internal state error\n",
606 ZR_DEVNAME(zr), __func__);
607
608 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
609 fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
610
611 spin_lock_irqsave(&zr->spinlock, flags);
612
613 /* Check if streaming capture has finished */
614 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
615 zr36057_set_memgrab(zr, 0);
616 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
617 fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
618 zr->v4l_buffers.allocated = 0;
619 }
620 }
621
622 spin_unlock_irqrestore(&zr->spinlock, flags);
623
624 return 0;
625}
626
627/*
628 * Queue a MJPEG buffer for capture/playback
629 */
630
631static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
632 enum zoran_codec_mode mode)
633{
634 struct zoran *zr = fh->zr;
635 unsigned long flags;
636 int res = 0;
637
638 /* Check if buffers are allocated */
639 if (!fh->buffers.allocated) {
640 dprintk(1,
641 KERN_ERR
642 "%s: %s - buffers not yet allocated\n",
643 ZR_DEVNAME(zr), __func__);
644 return -ENOMEM;
645 }
646
647 /* No grabbing outside the buffer range! */
648 if (num >= fh->buffers.num_buffers || num < 0) {
649 dprintk(1,
650 KERN_ERR
651 "%s: %s - buffer %d out of range\n",
652 ZR_DEVNAME(zr), __func__, num);
653 return -EINVAL;
654 }
655
656 /* what is the codec mode right now? */
657 if (zr->codec_mode == BUZ_MODE_IDLE) {
658 zr->jpg_settings = fh->jpg_settings;
659 } else if (zr->codec_mode != mode) {
660 /* wrong codec mode active - invalid */
661 dprintk(1,
662 KERN_ERR
663 "%s: %s - codec in wrong mode\n",
664 ZR_DEVNAME(zr), __func__);
665 return -EINVAL;
666 }
667
668 if (fh->buffers.active == ZORAN_FREE) {
669 if (zr->jpg_buffers.active == ZORAN_FREE) {
670 zr->jpg_buffers = fh->buffers;
671 fh->buffers.active = ZORAN_ACTIVE;
672 } else {
673 dprintk(1,
674 KERN_ERR
675 "%s: %s - another session is already capturing\n",
676 ZR_DEVNAME(zr), __func__);
677 res = -EBUSY;
678 }
679 }
680
681 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
682 /* Ok load up the jpeg codec */
683 zr36057_enable_jpg(zr, mode);
684 }
685
686 spin_lock_irqsave(&zr->spinlock, flags);
687
688 if (!res) {
689 switch (zr->jpg_buffers.buffer[num].state) {
690 case BUZ_STATE_DONE:
691 dprintk(2,
692 KERN_WARNING
693 "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
694 ZR_DEVNAME(zr), __func__);
695 case BUZ_STATE_USER:
696 /* since there is at least one unused buffer there's room for at
697 *least one more pend[] entry */
698 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
699 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
700 fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
701 zoran_feed_stat_com(zr);
702 break;
703 default:
704 case BUZ_STATE_DMA:
705 case BUZ_STATE_PEND:
706 if (zr->jpg_buffers.active == ZORAN_FREE) {
707 fh->buffers.active = ZORAN_FREE;
708 zr->jpg_buffers.allocated = 0;
709 }
710 res = -EBUSY; /* what are you doing? */
711 break;
712 }
713 }
714
715 spin_unlock_irqrestore(&zr->spinlock, flags);
716
717 if (!res && zr->jpg_buffers.active == ZORAN_FREE)
718 zr->jpg_buffers.active = fh->buffers.active;
719
720 return res;
721}
722
723static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
724{
725 struct zoran *zr = fh->zr;
726 int res = 0;
727
728 /* Does the user want to stop streaming? */
729 if (frame < 0) {
730 if (zr->codec_mode == mode) {
731 if (fh->buffers.active == ZORAN_FREE) {
732 dprintk(1,
733 KERN_ERR
734 "%s: %s(-1) - session not active\n",
735 ZR_DEVNAME(zr), __func__);
736 return -EINVAL;
737 }
738 fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
739 zr->jpg_buffers.allocated = 0;
740 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
741 return 0;
742 } else {
743 dprintk(1,
744 KERN_ERR
745 "%s: %s - stop streaming but not in streaming mode\n",
746 ZR_DEVNAME(zr), __func__);
747 return -EINVAL;
748 }
749 }
750
751 if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
752 return res;
753
754 /* Start the jpeg codec when the first frame is queued */
755 if (!res && zr->jpg_que_head == 1)
756 jpeg_start(zr);
757
758 return res;
759}
760
761/*
762 * Sync on a MJPEG buffer
763 */
764
765static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
766{
767 struct zoran *zr = fh->zr;
768 unsigned long flags;
769 int frame;
770
771 if (fh->buffers.active == ZORAN_FREE) {
772 dprintk(1,
773 KERN_ERR
774 "%s: %s - capture is not currently active\n",
775 ZR_DEVNAME(zr), __func__);
776 return -EINVAL;
777 }
778 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
779 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
780 dprintk(1,
781 KERN_ERR
782 "%s: %s - codec not in streaming mode\n",
783 ZR_DEVNAME(zr), __func__);
784 return -EINVAL;
785 }
786 if (!wait_event_interruptible_timeout(zr->jpg_capq,
787 (zr->jpg_que_tail != zr->jpg_dma_tail ||
788 zr->jpg_dma_tail == zr->jpg_dma_head),
789 10*HZ)) {
790 int isr;
791
792 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
793 udelay(1);
794 zr->codec->control(zr->codec, CODEC_G_STATUS,
795 sizeof(isr), &isr);
796 dprintk(1,
797 KERN_ERR
798 "%s: %s - timeout: codec isr=0x%02x\n",
799 ZR_DEVNAME(zr), __func__, isr);
800
801 return -ETIME;
802
803 }
804 if (signal_pending(current))
805 return -ERESTARTSYS;
806
807 spin_lock_irqsave(&zr->spinlock, flags);
808
809 if (zr->jpg_dma_tail != zr->jpg_dma_head)
810 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
811 else
812 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
813
814 /* buffer should now be in BUZ_STATE_DONE */
815 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
816 dprintk(2,
817 KERN_ERR "%s: %s - internal state error\n",
818 ZR_DEVNAME(zr), __func__);
819
820 *bs = zr->jpg_buffers.buffer[frame].bs;
821 bs->frame = frame;
822 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
823 fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
824
825 spin_unlock_irqrestore(&zr->spinlock, flags);
826
827 return 0;
828}
829
830static void zoran_open_init_session(struct zoran_fh *fh)
831{
832 int i;
833 struct zoran *zr = fh->zr;
834
835 /* Per default, map the V4L Buffers */
836 map_mode_raw(fh);
837
838 /* take over the card's current settings */
839 fh->overlay_settings = zr->overlay_settings;
840 fh->overlay_settings.is_set = 0;
841 fh->overlay_settings.format = zr->overlay_settings.format;
842 fh->overlay_active = ZORAN_FREE;
843
844 /* v4l settings */
845 fh->v4l_settings = zr->v4l_settings;
846 /* jpg settings */
847 fh->jpg_settings = zr->jpg_settings;
848
849 /* buffers */
850 memset(&fh->buffers, 0, sizeof(fh->buffers));
851 for (i = 0; i < MAX_FRAME; i++) {
852 fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
853 fh->buffers.buffer[i].bs.frame = i;
854 }
855 fh->buffers.allocated = 0;
856 fh->buffers.active = ZORAN_FREE;
857}
858
859static void zoran_close_end_session(struct zoran_fh *fh)
860{
861 struct zoran *zr = fh->zr;
862
863 /* overlay */
864 if (fh->overlay_active != ZORAN_FREE) {
865 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
866 zr->v4l_overlay_active = 0;
867 if (!zr->v4l_memgrab_active)
868 zr36057_overlay(zr, 0);
869 zr->overlay_mask = NULL;
870 }
871
872 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
873 /* v4l capture */
874 if (fh->buffers.active != ZORAN_FREE) {
875 unsigned long flags;
876
877 spin_lock_irqsave(&zr->spinlock, flags);
878 zr36057_set_memgrab(zr, 0);
879 zr->v4l_buffers.allocated = 0;
880 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
881 spin_unlock_irqrestore(&zr->spinlock, flags);
882 }
883
884 /* v4l buffers */
885 if (fh->buffers.allocated)
886 v4l_fbuffer_free(fh);
887 } else {
888 /* jpg capture */
889 if (fh->buffers.active != ZORAN_FREE) {
890 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
891 zr->jpg_buffers.allocated = 0;
892 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
893 }
894
895 /* jpg buffers */
896 if (fh->buffers.allocated)
897 jpg_fbuffer_free(fh);
898 }
899}
900
901/*
902 * Open a zoran card. Right now the flags stuff is just playing
903 */
904
905static int zoran_open(struct file *file)
906{
907 struct zoran *zr = video_drvdata(file);
908 struct zoran_fh *fh;
909 int res, first_open = 0;
910
911 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
912 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
913
914 mutex_lock(&zr->other_lock);
915
916 if (zr->user >= 2048) {
917 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
918 ZR_DEVNAME(zr), zr->user);
919 res = -EBUSY;
920 goto fail_unlock;
921 }
922
923 /* now, create the open()-specific file_ops struct */
924 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
925 if (!fh) {
926 dprintk(1,
927 KERN_ERR
928 "%s: %s - allocation of zoran_fh failed\n",
929 ZR_DEVNAME(zr), __func__);
930 res = -ENOMEM;
931 goto fail_unlock;
932 }
933 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
934 * on norm-change! */
935 fh->overlay_mask =
936 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
937 if (!fh->overlay_mask) {
938 dprintk(1,
939 KERN_ERR
940 "%s: %s - allocation of overlay_mask failed\n",
941 ZR_DEVNAME(zr), __func__);
942 res = -ENOMEM;
943 goto fail_fh;
944 }
945
946 if (zr->user++ == 0)
947 first_open = 1;
948
949 /*mutex_unlock(&zr->resource_lock);*/
950
951 /* default setup - TODO: look at flags */
952 if (first_open) { /* First device open */
953 zr36057_restart(zr);
954 zoran_open_init_params(zr);
955 zoran_init_hardware(zr);
956
957 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
958 }
959
960 /* set file_ops stuff */
961 file->private_data = fh;
962 fh->zr = zr;
963 zoran_open_init_session(fh);
964 mutex_unlock(&zr->other_lock);
965
966 return 0;
967
968fail_fh:
969 kfree(fh);
970fail_unlock:
971 mutex_unlock(&zr->other_lock);
972
973 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
974 ZR_DEVNAME(zr), res, zr->user);
975
976 return res;
977}
978
979static int
980zoran_close(struct file *file)
981{
982 struct zoran_fh *fh = file->private_data;
983 struct zoran *zr = fh->zr;
984
985 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
986 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
987
988 /* kernel locks (fs/device.c), so don't do that ourselves
989 * (prevents deadlocks) */
990 mutex_lock(&zr->other_lock);
991
992 zoran_close_end_session(fh);
993
994 if (zr->user-- == 1) { /* Last process */
995 /* Clean up JPEG process */
996 wake_up_interruptible(&zr->jpg_capq);
997 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
998 zr->jpg_buffers.allocated = 0;
999 zr->jpg_buffers.active = ZORAN_FREE;
1000
1001 /* disable interrupts */
1002 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1003
1004 if (zr36067_debug > 1)
1005 print_interrupts(zr);
1006
1007 /* Overlay off */
1008 zr->v4l_overlay_active = 0;
1009 zr36057_overlay(zr, 0);
1010 zr->overlay_mask = NULL;
1011
1012 /* capture off */
1013 wake_up_interruptible(&zr->v4l_capq);
1014 zr36057_set_memgrab(zr, 0);
1015 zr->v4l_buffers.allocated = 0;
1016 zr->v4l_buffers.active = ZORAN_FREE;
1017 zoran_set_pci_master(zr, 0);
1018
1019 if (!pass_through) { /* Switch to color bar */
1020 decoder_call(zr, video, s_stream, 0);
1021 encoder_call(zr, video, s_routing, 2, 0, 0);
1022 }
1023 }
1024 mutex_unlock(&zr->other_lock);
1025
1026 file->private_data = NULL;
1027 kfree(fh->overlay_mask);
1028 kfree(fh);
1029
1030 dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
1031
1032 return 0;
1033}
1034
1035
1036static ssize_t
1037zoran_read (struct file *file,
1038 char __user *data,
1039 size_t count,
1040 loff_t *ppos)
1041{
1042 /* we simply don't support read() (yet)... */
1043
1044 return -EINVAL;
1045}
1046
1047static ssize_t
1048zoran_write (struct file *file,
1049 const char __user *data,
1050 size_t count,
1051 loff_t *ppos)
1052{
1053 /* ...and the same goes for write() */
1054
1055 return -EINVAL;
1056}
1057
1058static int setup_fbuffer(struct zoran_fh *fh,
1059 void *base,
1060 const struct zoran_format *fmt,
1061 int width,
1062 int height,
1063 int bytesperline)
1064{
1065 struct zoran *zr = fh->zr;
1066
1067 /* (Ronald) v4l/v4l2 guidelines */
1068 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1069 return -EPERM;
1070
1071 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1072 ALi Magik (that needs very low latency while the card needs a
1073 higher value always) */
1074
1075 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1076 return -ENXIO;
1077
1078 /* we need a bytesperline value, even if not given */
1079 if (!bytesperline)
1080 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1081
1082#if 0
1083 if (zr->overlay_active) {
1084 /* dzjee... stupid users... don't even bother to turn off
1085 * overlay before changing the memory location...
1086 * normally, we would return errors here. However, one of
1087 * the tools that does this is... xawtv! and since xawtv
1088 * is used by +/- 99% of the users, we'd rather be user-
1089 * friendly and silently do as if nothing went wrong */
1090 dprintk(3,
1091 KERN_ERR
1092 "%s: %s - forced overlay turnoff because framebuffer changed\n",
1093 ZR_DEVNAME(zr), __func__);
1094 zr36057_overlay(zr, 0);
1095 }
1096#endif
1097
1098 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1099 dprintk(1,
1100 KERN_ERR
1101 "%s: %s - no valid overlay format given\n",
1102 ZR_DEVNAME(zr), __func__);
1103 return -EINVAL;
1104 }
1105 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1106 dprintk(1,
1107 KERN_ERR
1108 "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1109 ZR_DEVNAME(zr), __func__, width, height, bytesperline);
1110 return -EINVAL;
1111 }
1112 if (bytesperline & 3) {
1113 dprintk(1,
1114 KERN_ERR
1115 "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1116 ZR_DEVNAME(zr), __func__, bytesperline);
1117 return -EINVAL;
1118 }
1119
1120 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1121 zr->vbuf_height = height;
1122 zr->vbuf_width = width;
1123 zr->vbuf_depth = fmt->depth;
1124 zr->overlay_settings.format = fmt;
1125 zr->vbuf_bytesperline = bytesperline;
1126
1127 /* The user should set new window parameters */
1128 zr->overlay_settings.is_set = 0;
1129
1130 return 0;
1131}
1132
1133
1134static int setup_window(struct zoran_fh *fh,
1135 int x,
1136 int y,
1137 int width,
1138 int height,
1139 struct v4l2_clip __user *clips,
1140 unsigned int clipcount,
1141 void __user *bitmap)
1142{
1143 struct zoran *zr = fh->zr;
1144 struct v4l2_clip *vcp = NULL;
1145 int on, end;
1146
1147
1148 if (!zr->vbuf_base) {
1149 dprintk(1,
1150 KERN_ERR
1151 "%s: %s - frame buffer has to be set first\n",
1152 ZR_DEVNAME(zr), __func__);
1153 return -EINVAL;
1154 }
1155
1156 if (!fh->overlay_settings.format) {
1157 dprintk(1,
1158 KERN_ERR
1159 "%s: %s - no overlay format set\n",
1160 ZR_DEVNAME(zr), __func__);
1161 return -EINVAL;
1162 }
1163
1164 if (clipcount > 2048) {
1165 dprintk(1,
1166 KERN_ERR
1167 "%s: %s - invalid clipcount\n",
1168 ZR_DEVNAME(zr), __func__);
1169 return -EINVAL;
1170 }
1171
1172 /*
1173 * The video front end needs 4-byte alinged line sizes, we correct that
1174 * silently here if necessary
1175 */
1176 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1177 end = (x + width) & ~1; /* round down */
1178 x = (x + 1) & ~1; /* round up */
1179 width = end - x;
1180 }
1181
1182 if (zr->vbuf_depth == 24) {
1183 end = (x + width) & ~3; /* round down */
1184 x = (x + 3) & ~3; /* round up */
1185 width = end - x;
1186 }
1187
1188 if (width > BUZ_MAX_WIDTH)
1189 width = BUZ_MAX_WIDTH;
1190 if (height > BUZ_MAX_HEIGHT)
1191 height = BUZ_MAX_HEIGHT;
1192
1193 /* Check for invalid parameters */
1194 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1195 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1196 dprintk(1,
1197 KERN_ERR
1198 "%s: %s - width = %d or height = %d invalid\n",
1199 ZR_DEVNAME(zr), __func__, width, height);
1200 return -EINVAL;
1201 }
1202
1203 fh->overlay_settings.x = x;
1204 fh->overlay_settings.y = y;
1205 fh->overlay_settings.width = width;
1206 fh->overlay_settings.height = height;
1207 fh->overlay_settings.clipcount = clipcount;
1208
1209 /*
1210 * If an overlay is running, we have to switch it off
1211 * and switch it on again in order to get the new settings in effect.
1212 *
1213 * We also want to avoid that the overlay mask is written
1214 * when an overlay is running.
1215 */
1216
1217 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1218 zr->overlay_active != ZORAN_FREE &&
1219 fh->overlay_active != ZORAN_FREE;
1220 if (on)
1221 zr36057_overlay(zr, 0);
1222
1223 /*
1224 * Write the overlay mask if clips are wanted.
1225 * We prefer a bitmap.
1226 */
1227 if (bitmap) {
1228 /* fake value - it just means we want clips */
1229 fh->overlay_settings.clipcount = 1;
1230
1231 if (copy_from_user(fh->overlay_mask, bitmap,
1232 (width * height + 7) / 8)) {
1233 return -EFAULT;
1234 }
1235 } else if (clipcount) {
1236 /* write our own bitmap from the clips */
1237 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
1238 if (vcp == NULL) {
1239 dprintk(1,
1240 KERN_ERR
1241 "%s: %s - Alloc of clip mask failed\n",
1242 ZR_DEVNAME(zr), __func__);
1243 return -ENOMEM;
1244 }
1245 if (copy_from_user
1246 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1247 vfree(vcp);
1248 return -EFAULT;
1249 }
1250 write_overlay_mask(fh, vcp, clipcount);
1251 vfree(vcp);
1252 }
1253
1254 fh->overlay_settings.is_set = 1;
1255 if (fh->overlay_active != ZORAN_FREE &&
1256 zr->overlay_active != ZORAN_FREE)
1257 zr->overlay_settings = fh->overlay_settings;
1258
1259 if (on)
1260 zr36057_overlay(zr, 1);
1261
1262 /* Make sure the changes come into effect */
1263 return wait_grab_pending(zr);
1264}
1265
1266static int setup_overlay(struct zoran_fh *fh, int on)
1267{
1268 struct zoran *zr = fh->zr;
1269
1270 /* If there is nothing to do, return immediately */
1271 if ((on && fh->overlay_active != ZORAN_FREE) ||
1272 (!on && fh->overlay_active == ZORAN_FREE))
1273 return 0;
1274
1275 /* check whether we're touching someone else's overlay */
1276 if (on && zr->overlay_active != ZORAN_FREE &&
1277 fh->overlay_active == ZORAN_FREE) {
1278 dprintk(1,
1279 KERN_ERR
1280 "%s: %s - overlay is already active for another session\n",
1281 ZR_DEVNAME(zr), __func__);
1282 return -EBUSY;
1283 }
1284 if (!on && zr->overlay_active != ZORAN_FREE &&
1285 fh->overlay_active == ZORAN_FREE) {
1286 dprintk(1,
1287 KERN_ERR
1288 "%s: %s - you cannot cancel someone else's session\n",
1289 ZR_DEVNAME(zr), __func__);
1290 return -EPERM;
1291 }
1292
1293 if (on == 0) {
1294 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1295 zr->v4l_overlay_active = 0;
1296 /* When a grab is running, the video simply
1297 * won't be switched on any more */
1298 if (!zr->v4l_memgrab_active)
1299 zr36057_overlay(zr, 0);
1300 zr->overlay_mask = NULL;
1301 } else {
1302 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1303 dprintk(1,
1304 KERN_ERR
1305 "%s: %s - buffer or window not set\n",
1306 ZR_DEVNAME(zr), __func__);
1307 return -EINVAL;
1308 }
1309 if (!fh->overlay_settings.format) {
1310 dprintk(1,
1311 KERN_ERR
1312 "%s: %s - no overlay format set\n",
1313 ZR_DEVNAME(zr), __func__);
1314 return -EINVAL;
1315 }
1316 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1317 zr->v4l_overlay_active = 1;
1318 zr->overlay_mask = fh->overlay_mask;
1319 zr->overlay_settings = fh->overlay_settings;
1320 if (!zr->v4l_memgrab_active)
1321 zr36057_overlay(zr, 1);
1322 /* When a grab is running, the video will be
1323 * switched on when grab is finished */
1324 }
1325
1326 /* Make sure the changes come into effect */
1327 return wait_grab_pending(zr);
1328}
1329
1330/* get the status of a buffer in the clients buffer queue */
1331static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
1332 struct v4l2_buffer *buf, int num)
1333{
1334 struct zoran *zr = fh->zr;
1335 unsigned long flags;
1336
1337 buf->flags = V4L2_BUF_FLAG_MAPPED;
1338
1339 switch (fh->map_mode) {
1340 case ZORAN_MAP_MODE_RAW:
1341 /* check range */
1342 if (num < 0 || num >= fh->buffers.num_buffers ||
1343 !fh->buffers.allocated) {
1344 dprintk(1,
1345 KERN_ERR
1346 "%s: %s - wrong number or buffers not allocated\n",
1347 ZR_DEVNAME(zr), __func__);
1348 return -EINVAL;
1349 }
1350
1351 spin_lock_irqsave(&zr->spinlock, flags);
1352 dprintk(3,
1353 KERN_DEBUG
1354 "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1355 ZR_DEVNAME(zr), __func__,
1356 "FAL"[fh->buffers.active], num,
1357 "UPMD"[zr->v4l_buffers.buffer[num].state],
1358 fh->buffers.buffer[num].map ? 'Y' : 'N');
1359 spin_unlock_irqrestore(&zr->spinlock, flags);
1360
1361 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1362 buf->length = fh->buffers.buffer_size;
1363
1364 /* get buffer */
1365 buf->bytesused = fh->buffers.buffer[num].bs.length;
1366 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1367 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1368 buf->sequence = fh->buffers.buffer[num].bs.seq;
1369 buf->flags |= V4L2_BUF_FLAG_DONE;
1370 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1371 } else {
1372 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1373 }
1374
1375 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1376 buf->field = V4L2_FIELD_TOP;
1377 else
1378 buf->field = V4L2_FIELD_INTERLACED;
1379
1380 break;
1381
1382 case ZORAN_MAP_MODE_JPG_REC:
1383 case ZORAN_MAP_MODE_JPG_PLAY:
1384
1385 /* check range */
1386 if (num < 0 || num >= fh->buffers.num_buffers ||
1387 !fh->buffers.allocated) {
1388 dprintk(1,
1389 KERN_ERR
1390 "%s: %s - wrong number or buffers not allocated\n",
1391 ZR_DEVNAME(zr), __func__);
1392 return -EINVAL;
1393 }
1394
1395 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1396 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1397 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1398 buf->length = fh->buffers.buffer_size;
1399
1400 /* these variables are only written after frame has been captured */
1401 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1402 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1403 buf->sequence = fh->buffers.buffer[num].bs.seq;
1404 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1405 buf->bytesused = fh->buffers.buffer[num].bs.length;
1406 buf->flags |= V4L2_BUF_FLAG_DONE;
1407 } else {
1408 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1409 }
1410
1411 /* which fields are these? */
1412 if (fh->jpg_settings.TmpDcm != 1)
1413 buf->field = fh->jpg_settings.odd_even ?
1414 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1415 else
1416 buf->field = fh->jpg_settings.odd_even ?
1417 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1418
1419 break;
1420
1421 default:
1422
1423 dprintk(5,
1424 KERN_ERR
1425 "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1426 ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
1427 return -EINVAL;
1428 }
1429
1430 buf->memory = V4L2_MEMORY_MMAP;
1431 buf->index = num;
1432 buf->m.offset = buf->length * num;
1433
1434 return 0;
1435}
1436
1437static int
1438zoran_set_norm (struct zoran *zr,
1439 v4l2_std_id norm)
1440{
1441 int on;
1442
1443 if (zr->v4l_buffers.active != ZORAN_FREE ||
1444 zr->jpg_buffers.active != ZORAN_FREE) {
1445 dprintk(1,
1446 KERN_WARNING
1447 "%s: %s called while in playback/capture mode\n",
1448 ZR_DEVNAME(zr), __func__);
1449 return -EBUSY;
1450 }
1451
1452 if (!(norm & zr->card.norms)) {
1453 dprintk(1,
1454 KERN_ERR "%s: %s - unsupported norm %llx\n",
1455 ZR_DEVNAME(zr), __func__, norm);
1456 return -EINVAL;
1457 }
1458
1459 if (norm == V4L2_STD_ALL) {
1460 unsigned int status = 0;
1461 v4l2_std_id std = 0;
1462
1463 decoder_call(zr, video, querystd, &std);
1464 decoder_call(zr, core, s_std, std);
1465
1466 /* let changes come into effect */
1467 ssleep(2);
1468
1469 decoder_call(zr, video, g_input_status, &status);
1470 if (status & V4L2_IN_ST_NO_SIGNAL) {
1471 dprintk(1,
1472 KERN_ERR
1473 "%s: %s - no norm detected\n",
1474 ZR_DEVNAME(zr), __func__);
1475 /* reset norm */
1476 decoder_call(zr, core, s_std, zr->norm);
1477 return -EIO;
1478 }
1479
1480 norm = std;
1481 }
1482 if (norm & V4L2_STD_SECAM)
1483 zr->timing = zr->card.tvn[2];
1484 else if (norm & V4L2_STD_NTSC)
1485 zr->timing = zr->card.tvn[1];
1486 else
1487 zr->timing = zr->card.tvn[0];
1488
1489 /* We switch overlay off and on since a change in the
1490 * norm needs different VFE settings */
1491 on = zr->overlay_active && !zr->v4l_memgrab_active;
1492 if (on)
1493 zr36057_overlay(zr, 0);
1494
1495 decoder_call(zr, core, s_std, norm);
1496 encoder_call(zr, video, s_std_output, norm);
1497
1498 if (on)
1499 zr36057_overlay(zr, 1);
1500
1501 /* Make sure the changes come into effect */
1502 zr->norm = norm;
1503
1504 return 0;
1505}
1506
1507static int
1508zoran_set_input (struct zoran *zr,
1509 int input)
1510{
1511 if (input == zr->input) {
1512 return 0;
1513 }
1514
1515 if (zr->v4l_buffers.active != ZORAN_FREE ||
1516 zr->jpg_buffers.active != ZORAN_FREE) {
1517 dprintk(1,
1518 KERN_WARNING
1519 "%s: %s called while in playback/capture mode\n",
1520 ZR_DEVNAME(zr), __func__);
1521 return -EBUSY;
1522 }
1523
1524 if (input < 0 || input >= zr->card.inputs) {
1525 dprintk(1,
1526 KERN_ERR
1527 "%s: %s - unnsupported input %d\n",
1528 ZR_DEVNAME(zr), __func__, input);
1529 return -EINVAL;
1530 }
1531
1532 zr->input = input;
1533
1534 decoder_call(zr, video, s_routing,
1535 zr->card.input[input].muxsel, 0, 0);
1536
1537 return 0;
1538}
1539
1540/*
1541 * ioctl routine
1542 */
1543
1544static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1545{
1546 struct zoran_fh *fh = __fh;
1547 struct zoran *zr = fh->zr;
1548
1549 memset(cap, 0, sizeof(*cap));
1550 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1551 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1552 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1553 pci_name(zr->pci_dev));
1554 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1555 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1556 return 0;
1557}
1558
1559static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1560{
1561 unsigned int num, i;
1562
1563 for (num = i = 0; i < NUM_FORMATS; i++) {
1564 if (zoran_formats[i].flags & flag && num++ == fmt->index) {
1565 strncpy(fmt->description, zoran_formats[i].name,
1566 sizeof(fmt->description) - 1);
1567 /* fmt struct pre-zeroed, so adding '\0' not needed */
1568 fmt->pixelformat = zoran_formats[i].fourcc;
1569 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1570 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1571 return 0;
1572 }
1573 }
1574 return -EINVAL;
1575}
1576
1577static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1578 struct v4l2_fmtdesc *f)
1579{
1580 struct zoran_fh *fh = __fh;
1581 struct zoran *zr = fh->zr;
1582
1583 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1584}
1585
1586static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1587 struct v4l2_fmtdesc *f)
1588{
1589 struct zoran_fh *fh = __fh;
1590 struct zoran *zr = fh->zr;
1591
1592 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1593}
1594
1595static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1596 struct v4l2_fmtdesc *f)
1597{
1598 struct zoran_fh *fh = __fh;
1599 struct zoran *zr = fh->zr;
1600
1601 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1602}
1603
1604static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1605 struct v4l2_format *fmt)
1606{
1607 struct zoran_fh *fh = __fh;
1608 struct zoran *zr = fh->zr;
1609
1610 mutex_lock(&zr->resource_lock);
1611
1612 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1613 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1614 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1615 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1616 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1617 if (fh->jpg_settings.TmpDcm == 1)
1618 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1619 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1620 else
1621 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1622 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1623 fmt->fmt.pix.bytesperline = 0;
1624 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1625
1626 mutex_unlock(&zr->resource_lock);
1627 return 0;
1628}
1629
1630static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1631 struct v4l2_format *fmt)
1632{
1633 struct zoran_fh *fh = __fh;
1634 struct zoran *zr = fh->zr;
1635
1636 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1637 return zoran_g_fmt_vid_out(file, fh, fmt);
1638
1639 mutex_lock(&zr->resource_lock);
1640 fmt->fmt.pix.width = fh->v4l_settings.width;
1641 fmt->fmt.pix.height = fh->v4l_settings.height;
1642 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1643 fh->v4l_settings.height;
1644 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
1645 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1646 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1647 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1648 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1649 else
1650 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1651 mutex_unlock(&zr->resource_lock);
1652 return 0;
1653}
1654
1655static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
1656 struct v4l2_format *fmt)
1657{
1658 struct zoran_fh *fh = __fh;
1659 struct zoran *zr = fh->zr;
1660
1661 mutex_lock(&zr->resource_lock);
1662
1663 fmt->fmt.win.w.left = fh->overlay_settings.x;
1664 fmt->fmt.win.w.top = fh->overlay_settings.y;
1665 fmt->fmt.win.w.width = fh->overlay_settings.width;
1666 fmt->fmt.win.w.height = fh->overlay_settings.height;
1667 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
1668 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
1669 else
1670 fmt->fmt.win.field = V4L2_FIELD_TOP;
1671
1672 mutex_unlock(&zr->resource_lock);
1673 return 0;
1674}
1675
1676static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
1677 struct v4l2_format *fmt)
1678{
1679 struct zoran_fh *fh = __fh;
1680 struct zoran *zr = fh->zr;
1681
1682 mutex_lock(&zr->resource_lock);
1683
1684 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
1685 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
1686 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
1687 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
1688 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
1689 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
1690 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
1691 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
1692
1693 mutex_unlock(&zr->resource_lock);
1694 return 0;
1695}
1696
1697static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
1698 struct v4l2_format *fmt)
1699{
1700 struct zoran_fh *fh = __fh;
1701 struct zoran *zr = fh->zr;
1702 struct zoran_jpg_settings settings;
1703 int res = 0;
1704
1705 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
1706 return -EINVAL;
1707
1708 mutex_lock(&zr->resource_lock);
1709 settings = fh->jpg_settings;
1710
1711 /* we actually need to set 'real' parameters now */
1712 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
1713 settings.TmpDcm = 1;
1714 else
1715 settings.TmpDcm = 2;
1716 settings.decimation = 0;
1717 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
1718 settings.VerDcm = 2;
1719 else
1720 settings.VerDcm = 1;
1721 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
1722 settings.HorDcm = 4;
1723 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
1724 settings.HorDcm = 2;
1725 else
1726 settings.HorDcm = 1;
1727 if (settings.TmpDcm == 1)
1728 settings.field_per_buff = 2;
1729 else
1730 settings.field_per_buff = 1;
1731
1732 if (settings.HorDcm > 1) {
1733 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
1734 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
1735 } else {
1736 settings.img_x = 0;
1737 settings.img_width = BUZ_MAX_WIDTH;
1738 }
1739
1740 /* check */
1741 res = zoran_check_jpg_settings(zr, &settings, 1);
1742 if (res)
1743 goto tryfmt_unlock_and_return;
1744
1745 /* tell the user what we actually did */
1746 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
1747 fmt->fmt.pix.height = settings.img_height * 2 /
1748 (settings.TmpDcm * settings.VerDcm);
1749 if (settings.TmpDcm == 1)
1750 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1751 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1752 else
1753 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1754 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1755
1756 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
1757 fmt->fmt.pix.bytesperline = 0;
1758 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1759tryfmt_unlock_and_return:
1760 mutex_unlock(&zr->resource_lock);
1761 return res;
1762}
1763
1764static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
1765 struct v4l2_format *fmt)
1766{
1767 struct zoran_fh *fh = __fh;
1768 struct zoran *zr = fh->zr;
1769 int bpp;
1770 int i;
1771
1772 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
1773 return zoran_try_fmt_vid_out(file, fh, fmt);
1774
1775 mutex_lock(&zr->resource_lock);
1776
1777 for (i = 0; i < NUM_FORMATS; i++)
1778 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
1779 break;
1780
1781 if (i == NUM_FORMATS) {
1782 mutex_unlock(&zr->resource_lock);
1783 return -EINVAL;
1784 }
1785
1786 bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
1787 v4l_bound_align_image(
1788 &fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
1789 &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
1790 mutex_unlock(&zr->resource_lock);
1791
1792 return 0;
1793}
1794
1795static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
1796 struct v4l2_format *fmt)
1797{
1798 struct zoran_fh *fh = __fh;
1799 struct zoran *zr = fh->zr;
1800 int res;
1801
1802 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
1803 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
1804 fmt->fmt.win.w.width,
1805 fmt->fmt.win.w.height,
1806 fmt->fmt.win.clipcount,
1807 fmt->fmt.win.bitmap);
1808 mutex_lock(&zr->resource_lock);
1809 res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
1810 fmt->fmt.win.w.width, fmt->fmt.win.w.height,
1811 (struct v4l2_clip __user *)fmt->fmt.win.clips,
1812 fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
1813 mutex_unlock(&zr->resource_lock);
1814 return res;
1815}
1816
1817static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
1818 struct v4l2_format *fmt)
1819{
1820 struct zoran_fh *fh = __fh;
1821 struct zoran *zr = fh->zr;
1822 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
1823 struct zoran_jpg_settings settings;
1824 int res = 0;
1825
1826 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
1827 fmt->fmt.pix.width, fmt->fmt.pix.height,
1828 fmt->fmt.pix.pixelformat,
1829 (char *) &printformat);
1830 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
1831 return -EINVAL;
1832
1833 mutex_lock(&zr->resource_lock);
1834
1835 if (fh->buffers.allocated) {
1836 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
1837 ZR_DEVNAME(zr));
1838 res = -EBUSY;
1839 goto sfmtjpg_unlock_and_return;
1840 }
1841
1842 settings = fh->jpg_settings;
1843
1844 /* we actually need to set 'real' parameters now */
1845 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
1846 settings.TmpDcm = 1;
1847 else
1848 settings.TmpDcm = 2;
1849 settings.decimation = 0;
1850 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
1851 settings.VerDcm = 2;
1852 else
1853 settings.VerDcm = 1;
1854 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
1855 settings.HorDcm = 4;
1856 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
1857 settings.HorDcm = 2;
1858 else
1859 settings.HorDcm = 1;
1860 if (settings.TmpDcm == 1)
1861 settings.field_per_buff = 2;
1862 else
1863 settings.field_per_buff = 1;
1864
1865 if (settings.HorDcm > 1) {
1866 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
1867 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
1868 } else {
1869 settings.img_x = 0;
1870 settings.img_width = BUZ_MAX_WIDTH;
1871 }
1872
1873 /* check */
1874 res = zoran_check_jpg_settings(zr, &settings, 0);
1875 if (res)
1876 goto sfmtjpg_unlock_and_return;
1877
1878 /* it's ok, so set them */
1879 fh->jpg_settings = settings;
1880
1881 map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
1882 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1883
1884 /* tell the user what we actually did */
1885 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
1886 fmt->fmt.pix.height = settings.img_height * 2 /
1887 (settings.TmpDcm * settings.VerDcm);
1888 if (settings.TmpDcm == 1)
1889 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1890 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1891 else
1892 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1893 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1894 fmt->fmt.pix.bytesperline = 0;
1895 fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
1896 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1897
1898sfmtjpg_unlock_and_return:
1899 mutex_unlock(&zr->resource_lock);
1900 return res;
1901}
1902
1903static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
1904 struct v4l2_format *fmt)
1905{
1906 struct zoran_fh *fh = __fh;
1907 struct zoran *zr = fh->zr;
1908 int i;
1909 int res = 0;
1910
1911 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
1912 return zoran_s_fmt_vid_out(file, fh, fmt);
1913
1914 for (i = 0; i < NUM_FORMATS; i++)
1915 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
1916 break;
1917 if (i == NUM_FORMATS) {
1918 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
1919 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
1920 return -EINVAL;
1921 }
1922
1923 mutex_lock(&zr->resource_lock);
1924
1925 if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
1926 fh->buffers.active != ZORAN_FREE) {
1927 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
1928 ZR_DEVNAME(zr));
1929 res = -EBUSY;
1930 goto sfmtv4l_unlock_and_return;
1931 }
1932 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
1933 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
1934 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
1935 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
1936
1937 map_mode_raw(fh);
1938
1939 res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
1940 &zoran_formats[i]);
1941 if (res)
1942 goto sfmtv4l_unlock_and_return;
1943
1944 /* tell the user the results/missing stuff */
1945 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1946 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
1947 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1948 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1949 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1950 else
1951 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1952
1953sfmtv4l_unlock_and_return:
1954 mutex_unlock(&zr->resource_lock);
1955 return res;
1956}
1957
1958static int zoran_g_fbuf(struct file *file, void *__fh,
1959 struct v4l2_framebuffer *fb)
1960{
1961 struct zoran_fh *fh = __fh;
1962 struct zoran *zr = fh->zr;
1963
1964 memset(fb, 0, sizeof(*fb));
1965 mutex_lock(&zr->resource_lock);
1966 fb->base = zr->vbuf_base;
1967 fb->fmt.width = zr->vbuf_width;
1968 fb->fmt.height = zr->vbuf_height;
1969 if (zr->overlay_settings.format)
1970 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
1971 fb->fmt.bytesperline = zr->vbuf_bytesperline;
1972 mutex_unlock(&zr->resource_lock);
1973 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1974 fb->fmt.field = V4L2_FIELD_INTERLACED;
1975 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1976
1977 return 0;
1978}
1979
1980static int zoran_s_fbuf(struct file *file, void *__fh,
1981 struct v4l2_framebuffer *fb)
1982{
1983 struct zoran_fh *fh = __fh;
1984 struct zoran *zr = fh->zr;
1985 int i, res = 0;
1986 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
1987
1988 for (i = 0; i < NUM_FORMATS; i++)
1989 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
1990 break;
1991 if (i == NUM_FORMATS) {
1992 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
1993 ZR_DEVNAME(zr), fb->fmt.pixelformat,
1994 (char *)&printformat);
1995 return -EINVAL;
1996 }
1997
1998 mutex_lock(&zr->resource_lock);
1999 res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
2000 fb->fmt.height, fb->fmt.bytesperline);
2001 mutex_unlock(&zr->resource_lock);
2002
2003 return res;
2004}
2005
2006static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2007{
2008 struct zoran_fh *fh = __fh;
2009 struct zoran *zr = fh->zr;
2010 int res;
2011
2012 mutex_lock(&zr->resource_lock);
2013 res = setup_overlay(fh, on);
2014 mutex_unlock(&zr->resource_lock);
2015
2016 return res;
2017}
2018
2019static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2020
2021static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2022{
2023 struct zoran_fh *fh = __fh;
2024 struct zoran *zr = fh->zr;
2025 int res = 0;
2026
2027 if (req->memory != V4L2_MEMORY_MMAP) {
2028 dprintk(2,
2029 KERN_ERR
2030 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2031 ZR_DEVNAME(zr), req->memory);
2032 return -EINVAL;
2033 }
2034
2035 if (req->count == 0)
2036 return zoran_streamoff(file, fh, req->type);
2037
2038 mutex_lock(&zr->resource_lock);
2039 if (fh->buffers.allocated) {
2040 dprintk(2,
2041 KERN_ERR
2042 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2043 ZR_DEVNAME(zr));
2044 res = -EBUSY;
2045 goto v4l2reqbuf_unlock_and_return;
2046 }
2047
2048 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
2049 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2050 /* control user input */
2051 if (req->count < 2)
2052 req->count = 2;
2053 if (req->count > v4l_nbufs)
2054 req->count = v4l_nbufs;
2055
2056 /* The next mmap will map the V4L buffers */
2057 map_mode_raw(fh);
2058 fh->buffers.num_buffers = req->count;
2059
2060 if (v4l_fbuffer_alloc(fh)) {
2061 res = -ENOMEM;
2062 goto v4l2reqbuf_unlock_and_return;
2063 }
2064 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
2065 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2066 /* we need to calculate size ourselves now */
2067 if (req->count < 4)
2068 req->count = 4;
2069 if (req->count > jpg_nbufs)
2070 req->count = jpg_nbufs;
2071
2072 /* The next mmap will map the MJPEG buffers */
2073 map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2074 fh->buffers.num_buffers = req->count;
2075 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2076
2077 if (jpg_fbuffer_alloc(fh)) {
2078 res = -ENOMEM;
2079 goto v4l2reqbuf_unlock_and_return;
2080 }
2081 } else {
2082 dprintk(1,
2083 KERN_ERR
2084 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2085 ZR_DEVNAME(zr), req->type);
2086 res = -EINVAL;
2087 goto v4l2reqbuf_unlock_and_return;
2088 }
2089v4l2reqbuf_unlock_and_return:
2090 mutex_unlock(&zr->resource_lock);
2091
2092 return res;
2093}
2094
2095static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2096{
2097 struct zoran_fh *fh = __fh;
2098 struct zoran *zr = fh->zr;
2099 int res;
2100
2101 mutex_lock(&zr->resource_lock);
2102 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2103 mutex_unlock(&zr->resource_lock);
2104
2105 return res;
2106}
2107
2108static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2109{
2110 struct zoran_fh *fh = __fh;
2111 struct zoran *zr = fh->zr;
2112 int res = 0, codec_mode, buf_type;
2113
2114 mutex_lock(&zr->resource_lock);
2115
2116 switch (fh->map_mode) {
2117 case ZORAN_MAP_MODE_RAW:
2118 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2119 dprintk(1, KERN_ERR
2120 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2121 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2122 res = -EINVAL;
2123 goto qbuf_unlock_and_return;
2124 }
2125
2126 res = zoran_v4l_queue_frame(fh, buf->index);
2127 if (res)
2128 goto qbuf_unlock_and_return;
2129 if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
2130 zr36057_set_memgrab(zr, 1);
2131 break;
2132
2133 case ZORAN_MAP_MODE_JPG_REC:
2134 case ZORAN_MAP_MODE_JPG_PLAY:
2135 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2136 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2137 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2138 } else {
2139 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2140 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2141 }
2142
2143 if (buf->type != buf_type) {
2144 dprintk(1, KERN_ERR
2145 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2146 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2147 res = -EINVAL;
2148 goto qbuf_unlock_and_return;
2149 }
2150
2151 res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
2152 if (res != 0)
2153 goto qbuf_unlock_and_return;
2154 if (zr->codec_mode == BUZ_MODE_IDLE &&
2155 fh->buffers.active == ZORAN_LOCKED)
2156 zr36057_enable_jpg(zr, codec_mode);
2157
2158 break;
2159
2160 default:
2161 dprintk(1, KERN_ERR
2162 "%s: VIDIOC_QBUF - unsupported type %d\n",
2163 ZR_DEVNAME(zr), buf->type);
2164 res = -EINVAL;
2165 break;
2166 }
2167qbuf_unlock_and_return:
2168 mutex_unlock(&zr->resource_lock);
2169
2170 return res;
2171}
2172
2173static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2174{
2175 struct zoran_fh *fh = __fh;
2176 struct zoran *zr = fh->zr;
2177 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2178
2179 mutex_lock(&zr->resource_lock);
2180
2181 switch (fh->map_mode) {
2182 case ZORAN_MAP_MODE_RAW:
2183 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2184 dprintk(1, KERN_ERR
2185 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2186 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2187 res = -EINVAL;
2188 goto dqbuf_unlock_and_return;
2189 }
2190
2191 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2192 if (file->f_flags & O_NONBLOCK &&
2193 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2194 res = -EAGAIN;
2195 goto dqbuf_unlock_and_return;
2196 }
2197 res = v4l_sync(fh, num);
2198 if (res)
2199 goto dqbuf_unlock_and_return;
2200 zr->v4l_sync_tail++;
2201 res = zoran_v4l2_buffer_status(fh, buf, num);
2202 break;
2203
2204 case ZORAN_MAP_MODE_JPG_REC:
2205 case ZORAN_MAP_MODE_JPG_PLAY:
2206 {
2207 struct zoran_sync bs;
2208
2209 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2210 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2211 else
2212 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2213
2214 if (buf->type != buf_type) {
2215 dprintk(1, KERN_ERR
2216 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2217 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2218 res = -EINVAL;
2219 goto dqbuf_unlock_and_return;
2220 }
2221
2222 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2223
2224 if (file->f_flags & O_NONBLOCK &&
2225 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2226 res = -EAGAIN;
2227 goto dqbuf_unlock_and_return;
2228 }
2229 bs.frame = 0; /* suppress compiler warning */
2230 res = jpg_sync(fh, &bs);
2231 if (res)
2232 goto dqbuf_unlock_and_return;
2233 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2234 break;
2235 }
2236
2237 default:
2238 dprintk(1, KERN_ERR
2239 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2240 ZR_DEVNAME(zr), buf->type);
2241 res = -EINVAL;
2242 break;
2243 }
2244dqbuf_unlock_and_return:
2245 mutex_unlock(&zr->resource_lock);
2246
2247 return res;
2248}
2249
2250static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2251{
2252 struct zoran_fh *fh = __fh;
2253 struct zoran *zr = fh->zr;
2254 int res = 0;
2255
2256 mutex_lock(&zr->resource_lock);
2257
2258 switch (fh->map_mode) {
2259 case ZORAN_MAP_MODE_RAW: /* raw capture */
2260 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2261 fh->buffers.active != ZORAN_ACTIVE) {
2262 res = -EBUSY;
2263 goto strmon_unlock_and_return;
2264 }
2265
2266 zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2267 zr->v4l_settings = fh->v4l_settings;
2268
2269 zr->v4l_sync_tail = zr->v4l_pend_tail;
2270 if (!zr->v4l_memgrab_active &&
2271 zr->v4l_pend_head != zr->v4l_pend_tail) {
2272 zr36057_set_memgrab(zr, 1);
2273 }
2274 break;
2275
2276 case ZORAN_MAP_MODE_JPG_REC:
2277 case ZORAN_MAP_MODE_JPG_PLAY:
2278 /* what is the codec mode right now? */
2279 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2280 fh->buffers.active != ZORAN_ACTIVE) {
2281 res = -EBUSY;
2282 goto strmon_unlock_and_return;
2283 }
2284
2285 zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2286
2287 if (zr->jpg_que_head != zr->jpg_que_tail) {
2288 /* Start the jpeg codec when the first frame is queued */
2289 jpeg_start(zr);
2290 }
2291 break;
2292
2293 default:
2294 dprintk(1,
2295 KERN_ERR
2296 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2297 ZR_DEVNAME(zr), fh->map_mode);
2298 res = -EINVAL;
2299 break;
2300 }
2301strmon_unlock_and_return:
2302 mutex_unlock(&zr->resource_lock);
2303
2304 return res;
2305}
2306
2307static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2308{
2309 struct zoran_fh *fh = __fh;
2310 struct zoran *zr = fh->zr;
2311 int i, res = 0;
2312 unsigned long flags;
2313
2314 mutex_lock(&zr->resource_lock);
2315
2316 switch (fh->map_mode) {
2317 case ZORAN_MAP_MODE_RAW: /* raw capture */
2318 if (fh->buffers.active == ZORAN_FREE &&
2319 zr->v4l_buffers.active != ZORAN_FREE) {
2320 res = -EPERM; /* stay off other's settings! */
2321 goto strmoff_unlock_and_return;
2322 }
2323 if (zr->v4l_buffers.active == ZORAN_FREE)
2324 goto strmoff_unlock_and_return;
2325
2326 spin_lock_irqsave(&zr->spinlock, flags);
2327 /* unload capture */
2328 if (zr->v4l_memgrab_active) {
2329
2330 zr36057_set_memgrab(zr, 0);
2331 }
2332
2333 for (i = 0; i < fh->buffers.num_buffers; i++)
2334 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2335 fh->buffers = zr->v4l_buffers;
2336
2337 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
2338
2339 zr->v4l_grab_seq = 0;
2340 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2341 zr->v4l_sync_tail = 0;
2342
2343 spin_unlock_irqrestore(&zr->spinlock, flags);
2344
2345 break;
2346
2347 case ZORAN_MAP_MODE_JPG_REC:
2348 case ZORAN_MAP_MODE_JPG_PLAY:
2349 if (fh->buffers.active == ZORAN_FREE &&
2350 zr->jpg_buffers.active != ZORAN_FREE) {
2351 res = -EPERM; /* stay off other's settings! */
2352 goto strmoff_unlock_and_return;
2353 }
2354 if (zr->jpg_buffers.active == ZORAN_FREE)
2355 goto strmoff_unlock_and_return;
2356
2357 res = jpg_qbuf(fh, -1,
2358 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2359 BUZ_MODE_MOTION_COMPRESS :
2360 BUZ_MODE_MOTION_DECOMPRESS);
2361 if (res)
2362 goto strmoff_unlock_and_return;
2363 break;
2364 default:
2365 dprintk(1, KERN_ERR
2366 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2367 ZR_DEVNAME(zr), fh->map_mode);
2368 res = -EINVAL;
2369 break;
2370 }
2371strmoff_unlock_and_return:
2372 mutex_unlock(&zr->resource_lock);
2373
2374 return res;
2375}
2376
2377static int zoran_queryctrl(struct file *file, void *__fh,
2378 struct v4l2_queryctrl *ctrl)
2379{
2380 struct zoran_fh *fh = __fh;
2381 struct zoran *zr = fh->zr;
2382
2383 /* we only support hue/saturation/contrast/brightness */
2384 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2385 ctrl->id > V4L2_CID_HUE)
2386 return -EINVAL;
2387
2388 decoder_call(zr, core, queryctrl, ctrl);
2389
2390 return 0;
2391}
2392
2393static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2394{
2395 struct zoran_fh *fh = __fh;
2396 struct zoran *zr = fh->zr;
2397
2398 /* we only support hue/saturation/contrast/brightness */
2399 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2400 ctrl->id > V4L2_CID_HUE)
2401 return -EINVAL;
2402
2403 mutex_lock(&zr->resource_lock);
2404 decoder_call(zr, core, g_ctrl, ctrl);
2405 mutex_unlock(&zr->resource_lock);
2406
2407 return 0;
2408}
2409
2410static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2411{
2412 struct zoran_fh *fh = __fh;
2413 struct zoran *zr = fh->zr;
2414
2415 /* we only support hue/saturation/contrast/brightness */
2416 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2417 ctrl->id > V4L2_CID_HUE)
2418 return -EINVAL;
2419
2420 mutex_lock(&zr->resource_lock);
2421 decoder_call(zr, core, s_ctrl, ctrl);
2422 mutex_unlock(&zr->resource_lock);
2423
2424 return 0;
2425}
2426
2427static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2428{
2429 struct zoran_fh *fh = __fh;
2430 struct zoran *zr = fh->zr;
2431
2432 mutex_lock(&zr->resource_lock);
2433 *std = zr->norm;
2434 mutex_unlock(&zr->resource_lock);
2435 return 0;
2436}
2437
2438static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2439{
2440 struct zoran_fh *fh = __fh;
2441 struct zoran *zr = fh->zr;
2442 int res = 0;
2443
2444 mutex_lock(&zr->resource_lock);
2445 res = zoran_set_norm(zr, *std);
2446 if (res)
2447 goto sstd_unlock_and_return;
2448
2449 res = wait_grab_pending(zr);
2450sstd_unlock_and_return:
2451 mutex_unlock(&zr->resource_lock);
2452 return res;
2453}
2454
2455static int zoran_enum_input(struct file *file, void *__fh,
2456 struct v4l2_input *inp)
2457{
2458 struct zoran_fh *fh = __fh;
2459 struct zoran *zr = fh->zr;
2460
2461 if (inp->index >= zr->card.inputs)
2462 return -EINVAL;
2463
2464 strncpy(inp->name, zr->card.input[inp->index].name,
2465 sizeof(inp->name) - 1);
2466 inp->type = V4L2_INPUT_TYPE_CAMERA;
2467 inp->std = V4L2_STD_ALL;
2468
2469 /* Get status of video decoder */
2470 mutex_lock(&zr->resource_lock);
2471 decoder_call(zr, video, g_input_status, &inp->status);
2472 mutex_unlock(&zr->resource_lock);
2473 return 0;
2474}
2475
2476static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2477{
2478 struct zoran_fh *fh = __fh;
2479 struct zoran *zr = fh->zr;
2480
2481 mutex_lock(&zr->resource_lock);
2482 *input = zr->input;
2483 mutex_unlock(&zr->resource_lock);
2484
2485 return 0;
2486}
2487
2488static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2489{
2490 struct zoran_fh *fh = __fh;
2491 struct zoran *zr = fh->zr;
2492 int res;
2493
2494 mutex_lock(&zr->resource_lock);
2495 res = zoran_set_input(zr, input);
2496 if (res)
2497 goto sinput_unlock_and_return;
2498
2499 /* Make sure the changes come into effect */
2500 res = wait_grab_pending(zr);
2501sinput_unlock_and_return:
2502 mutex_unlock(&zr->resource_lock);
2503 return res;
2504}
2505
2506static int zoran_enum_output(struct file *file, void *__fh,
2507 struct v4l2_output *outp)
2508{
2509 if (outp->index != 0)
2510 return -EINVAL;
2511
2512 outp->index = 0;
2513 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2514 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2515
2516 return 0;
2517}
2518
2519static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2520{
2521 *output = 0;
2522
2523 return 0;
2524}
2525
2526static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2527{
2528 if (output != 0)
2529 return -EINVAL;
2530
2531 return 0;
2532}
2533
2534/* cropping (sub-frame capture) */
2535static int zoran_cropcap(struct file *file, void *__fh,
2536 struct v4l2_cropcap *cropcap)
2537{
2538 struct zoran_fh *fh = __fh;
2539 struct zoran *zr = fh->zr;
2540 int type = cropcap->type, res = 0;
2541
2542 memset(cropcap, 0, sizeof(*cropcap));
2543 cropcap->type = type;
2544
2545 mutex_lock(&zr->resource_lock);
2546
2547 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2548 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2549 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2550 dprintk(1, KERN_ERR
2551 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2552 ZR_DEVNAME(zr));
2553 res = -EINVAL;
2554 goto cropcap_unlock_and_return;
2555 }
2556
2557 cropcap->bounds.top = cropcap->bounds.left = 0;
2558 cropcap->bounds.width = BUZ_MAX_WIDTH;
2559 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2560 cropcap->defrect.top = cropcap->defrect.left = 0;
2561 cropcap->defrect.width = BUZ_MIN_WIDTH;
2562 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2563cropcap_unlock_and_return:
2564 mutex_unlock(&zr->resource_lock);
2565 return res;
2566}
2567
2568static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2569{
2570 struct zoran_fh *fh = __fh;
2571 struct zoran *zr = fh->zr;
2572 int type = crop->type, res = 0;
2573
2574 memset(crop, 0, sizeof(*crop));
2575 crop->type = type;
2576
2577 mutex_lock(&zr->resource_lock);
2578
2579 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2580 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2581 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2582 dprintk(1,
2583 KERN_ERR
2584 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2585 ZR_DEVNAME(zr));
2586 res = -EINVAL;
2587 goto gcrop_unlock_and_return;
2588 }
2589
2590 crop->c.top = fh->jpg_settings.img_y;
2591 crop->c.left = fh->jpg_settings.img_x;
2592 crop->c.width = fh->jpg_settings.img_width;
2593 crop->c.height = fh->jpg_settings.img_height;
2594
2595gcrop_unlock_and_return:
2596 mutex_unlock(&zr->resource_lock);
2597
2598 return res;
2599}
2600
2601static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2602{
2603 struct zoran_fh *fh = __fh;
2604 struct zoran *zr = fh->zr;
2605 int res = 0;
2606 struct zoran_jpg_settings settings;
2607
2608 settings = fh->jpg_settings;
2609
2610 mutex_lock(&zr->resource_lock);
2611
2612 if (fh->buffers.allocated) {
2613 dprintk(1, KERN_ERR
2614 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
2615 ZR_DEVNAME(zr));
2616 res = -EBUSY;
2617 goto scrop_unlock_and_return;
2618 }
2619
2620 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2621 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2622 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2623 dprintk(1, KERN_ERR
2624 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2625 ZR_DEVNAME(zr));
2626 res = -EINVAL;
2627 goto scrop_unlock_and_return;
2628 }
2629
2630 /* move into a form that we understand */
2631 settings.img_x = crop->c.left;
2632 settings.img_y = crop->c.top;
2633 settings.img_width = crop->c.width;
2634 settings.img_height = crop->c.height;
2635
2636 /* check validity */
2637 res = zoran_check_jpg_settings(zr, &settings, 0);
2638 if (res)
2639 goto scrop_unlock_and_return;
2640
2641 /* accept */
2642 fh->jpg_settings = settings;
2643
2644scrop_unlock_and_return:
2645 mutex_unlock(&zr->resource_lock);
2646 return res;
2647}
2648
2649static int zoran_g_jpegcomp(struct file *file, void *__fh,
2650 struct v4l2_jpegcompression *params)
2651{
2652 struct zoran_fh *fh = __fh;
2653 struct zoran *zr = fh->zr;
2654 memset(params, 0, sizeof(*params));
2655
2656 mutex_lock(&zr->resource_lock);
2657
2658 params->quality = fh->jpg_settings.jpg_comp.quality;
2659 params->APPn = fh->jpg_settings.jpg_comp.APPn;
2660 memcpy(params->APP_data,
2661 fh->jpg_settings.jpg_comp.APP_data,
2662 fh->jpg_settings.jpg_comp.APP_len);
2663 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2664 memcpy(params->COM_data,
2665 fh->jpg_settings.jpg_comp.COM_data,
2666 fh->jpg_settings.jpg_comp.COM_len);
2667 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
2668 params->jpeg_markers =
2669 fh->jpg_settings.jpg_comp.jpeg_markers;
2670
2671 mutex_unlock(&zr->resource_lock);
2672
2673 return 0;
2674}
2675
2676static int zoran_s_jpegcomp(struct file *file, void *__fh,
2677 struct v4l2_jpegcompression *params)
2678{
2679 struct zoran_fh *fh = __fh;
2680 struct zoran *zr = fh->zr;
2681 int res = 0;
2682 struct zoran_jpg_settings settings;
2683
2684 settings = fh->jpg_settings;
2685
2686 settings.jpg_comp = *params;
2687
2688 mutex_lock(&zr->resource_lock);
2689
2690 if (fh->buffers.active != ZORAN_FREE) {
2691 dprintk(1, KERN_WARNING
2692 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
2693 ZR_DEVNAME(zr));
2694 res = -EBUSY;
2695 goto sjpegc_unlock_and_return;
2696 }
2697
2698 res = zoran_check_jpg_settings(zr, &settings, 0);
2699 if (res)
2700 goto sjpegc_unlock_and_return;
2701 if (!fh->buffers.allocated)
2702 fh->buffers.buffer_size =
2703 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2704 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
2705sjpegc_unlock_and_return:
2706 mutex_unlock(&zr->resource_lock);
2707
2708 return res;
2709}
2710
2711static unsigned int
2712zoran_poll (struct file *file,
2713 poll_table *wait)
2714{
2715 struct zoran_fh *fh = file->private_data;
2716 struct zoran *zr = fh->zr;
2717 int res = 0, frame;
2718 unsigned long flags;
2719
2720 /* we should check whether buffers are ready to be synced on
2721 * (w/o waits - O_NONBLOCK) here
2722 * if ready for read (sync), return POLLIN|POLLRDNORM,
2723 * if ready for write (sync), return POLLOUT|POLLWRNORM,
2724 * if error, return POLLERR,
2725 * if no buffers queued or so, return POLLNVAL
2726 */
2727
2728 mutex_lock(&zr->resource_lock);
2729
2730 switch (fh->map_mode) {
2731 case ZORAN_MAP_MODE_RAW:
2732 poll_wait(file, &zr->v4l_capq, wait);
2733 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2734
2735 spin_lock_irqsave(&zr->spinlock, flags);
2736 dprintk(3,
2737 KERN_DEBUG
2738 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
2739 ZR_DEVNAME(zr), __func__,
2740 "FAL"[fh->buffers.active], zr->v4l_sync_tail,
2741 "UPMD"[zr->v4l_buffers.buffer[frame].state],
2742 zr->v4l_pend_tail, zr->v4l_pend_head);
2743 /* Process is the one capturing? */
2744 if (fh->buffers.active != ZORAN_FREE &&
2745 /* Buffer ready to DQBUF? */
2746 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
2747 res = POLLIN | POLLRDNORM;
2748 spin_unlock_irqrestore(&zr->spinlock, flags);
2749
2750 break;
2751
2752 case ZORAN_MAP_MODE_JPG_REC:
2753 case ZORAN_MAP_MODE_JPG_PLAY:
2754 poll_wait(file, &zr->jpg_capq, wait);
2755 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2756
2757 spin_lock_irqsave(&zr->spinlock, flags);
2758 dprintk(3,
2759 KERN_DEBUG
2760 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
2761 ZR_DEVNAME(zr), __func__,
2762 "FAL"[fh->buffers.active], zr->jpg_que_tail,
2763 "UPMD"[zr->jpg_buffers.buffer[frame].state],
2764 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
2765 if (fh->buffers.active != ZORAN_FREE &&
2766 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
2767 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
2768 res = POLLIN | POLLRDNORM;
2769 else
2770 res = POLLOUT | POLLWRNORM;
2771 }
2772 spin_unlock_irqrestore(&zr->spinlock, flags);
2773
2774 break;
2775
2776 default:
2777 dprintk(1,
2778 KERN_ERR
2779 "%s: %s - internal error, unknown map_mode=%d\n",
2780 ZR_DEVNAME(zr), __func__, fh->map_mode);
2781 res = POLLNVAL;
2782 }
2783
2784 mutex_unlock(&zr->resource_lock);
2785
2786 return res;
2787}
2788
2789
2790/*
2791 * This maps the buffers to user space.
2792 *
2793 * Depending on the state of fh->map_mode
2794 * the V4L or the MJPEG buffers are mapped
2795 * per buffer or all together
2796 *
2797 * Note that we need to connect to some
2798 * unmap signal event to unmap the de-allocate
2799 * the buffer accordingly (zoran_vm_close())
2800 */
2801
2802static void
2803zoran_vm_open (struct vm_area_struct *vma)
2804{
2805 struct zoran_mapping *map = vma->vm_private_data;
2806
2807 map->count++;
2808}
2809
2810static void
2811zoran_vm_close (struct vm_area_struct *vma)
2812{
2813 struct zoran_mapping *map = vma->vm_private_data;
2814 struct zoran_fh *fh = map->fh;
2815 struct zoran *zr = fh->zr;
2816 int i;
2817
2818 if (--map->count > 0)
2819 return;
2820
2821 dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
2822 __func__, mode_name(fh->map_mode));
2823
2824 for (i = 0; i < fh->buffers.num_buffers; i++) {
2825 if (fh->buffers.buffer[i].map == map)
2826 fh->buffers.buffer[i].map = NULL;
2827 }
2828 kfree(map);
2829
2830 /* Any buffers still mapped? */
2831 for (i = 0; i < fh->buffers.num_buffers; i++)
2832 if (fh->buffers.buffer[i].map)
2833 return;
2834
2835 dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
2836 __func__, mode_name(fh->map_mode));
2837
2838 mutex_lock(&zr->resource_lock);
2839
2840 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
2841 if (fh->buffers.active != ZORAN_FREE) {
2842 unsigned long flags;
2843
2844 spin_lock_irqsave(&zr->spinlock, flags);
2845 zr36057_set_memgrab(zr, 0);
2846 zr->v4l_buffers.allocated = 0;
2847 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
2848 spin_unlock_irqrestore(&zr->spinlock, flags);
2849 }
2850 v4l_fbuffer_free(fh);
2851 } else {
2852 if (fh->buffers.active != ZORAN_FREE) {
2853 jpg_qbuf(fh, -1, zr->codec_mode);
2854 zr->jpg_buffers.allocated = 0;
2855 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
2856 }
2857 jpg_fbuffer_free(fh);
2858 }
2859
2860 mutex_unlock(&zr->resource_lock);
2861}
2862
2863static const struct vm_operations_struct zoran_vm_ops = {
2864 .open = zoran_vm_open,
2865 .close = zoran_vm_close,
2866};
2867
2868static int
2869zoran_mmap (struct file *file,
2870 struct vm_area_struct *vma)
2871{
2872 struct zoran_fh *fh = file->private_data;
2873 struct zoran *zr = fh->zr;
2874 unsigned long size = (vma->vm_end - vma->vm_start);
2875 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
2876 int i, j;
2877 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
2878 int first, last;
2879 struct zoran_mapping *map;
2880 int res = 0;
2881
2882 dprintk(3,
2883 KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
2884 ZR_DEVNAME(zr), __func__,
2885 mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
2886
2887 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
2888 !(vma->vm_flags & VM_WRITE)) {
2889 dprintk(1,
2890 KERN_ERR
2891 "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
2892 ZR_DEVNAME(zr), __func__);
2893 return -EINVAL;
2894 }
2895
2896 mutex_lock(&zr->resource_lock);
2897
2898 if (!fh->buffers.allocated) {
2899 dprintk(1,
2900 KERN_ERR
2901 "%s: %s(%s) - buffers not yet allocated\n",
2902 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
2903 res = -ENOMEM;
2904 goto mmap_unlock_and_return;
2905 }
2906
2907 first = offset / fh->buffers.buffer_size;
2908 last = first - 1 + size / fh->buffers.buffer_size;
2909 if (offset % fh->buffers.buffer_size != 0 ||
2910 size % fh->buffers.buffer_size != 0 || first < 0 ||
2911 last < 0 || first >= fh->buffers.num_buffers ||
2912 last >= fh->buffers.buffer_size) {
2913 dprintk(1,
2914 KERN_ERR
2915 "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
2916 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
2917 fh->buffers.buffer_size,
2918 fh->buffers.num_buffers);
2919 res = -EINVAL;
2920 goto mmap_unlock_and_return;
2921 }
2922
2923 /* Check if any buffers are already mapped */
2924 for (i = first; i <= last; i++) {
2925 if (fh->buffers.buffer[i].map) {
2926 dprintk(1,
2927 KERN_ERR
2928 "%s: %s(%s) - buffer %d already mapped\n",
2929 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
2930 res = -EBUSY;
2931 goto mmap_unlock_and_return;
2932 }
2933 }
2934
2935 /* map these buffers */
2936 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
2937 if (!map) {
2938 res = -ENOMEM;
2939 goto mmap_unlock_and_return;
2940 }
2941 map->fh = fh;
2942 map->count = 1;
2943
2944 vma->vm_ops = &zoran_vm_ops;
2945 vma->vm_flags |= VM_DONTEXPAND;
2946 vma->vm_private_data = map;
2947
2948 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
2949 for (i = first; i <= last; i++) {
2950 todo = size;
2951 if (todo > fh->buffers.buffer_size)
2952 todo = fh->buffers.buffer_size;
2953 page = fh->buffers.buffer[i].v4l.fbuffer_phys;
2954 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
2955 todo, PAGE_SHARED)) {
2956 dprintk(1,
2957 KERN_ERR
2958 "%s: %s(V4L) - remap_pfn_range failed\n",
2959 ZR_DEVNAME(zr), __func__);
2960 res = -EAGAIN;
2961 goto mmap_unlock_and_return;
2962 }
2963 size -= todo;
2964 start += todo;
2965 fh->buffers.buffer[i].map = map;
2966 if (size == 0)
2967 break;
2968 }
2969 } else {
2970 for (i = first; i <= last; i++) {
2971 for (j = 0;
2972 j < fh->buffers.buffer_size / PAGE_SIZE;
2973 j++) {
2974 fraglen =
2975 (le32_to_cpu(fh->buffers.buffer[i].jpg.
2976 frag_tab[2 * j + 1]) & ~1) << 1;
2977 todo = size;
2978 if (todo > fraglen)
2979 todo = fraglen;
2980 pos =
2981 le32_to_cpu(fh->buffers.
2982 buffer[i].jpg.frag_tab[2 * j]);
2983 /* should just be pos on i386 */
2984 page = virt_to_phys(bus_to_virt(pos))
2985 >> PAGE_SHIFT;
2986 if (remap_pfn_range(vma, start, page,
2987 todo, PAGE_SHARED)) {
2988 dprintk(1,
2989 KERN_ERR
2990 "%s: %s(V4L) - remap_pfn_range failed\n",
2991 ZR_DEVNAME(zr), __func__);
2992 res = -EAGAIN;
2993 goto mmap_unlock_and_return;
2994 }
2995 size -= todo;
2996 start += todo;
2997 if (size == 0)
2998 break;
2999 if (le32_to_cpu(fh->buffers.buffer[i].jpg.
3000 frag_tab[2 * j + 1]) & 1)
3001 break; /* was last fragment */
3002 }
3003 fh->buffers.buffer[i].map = map;
3004 if (size == 0)
3005 break;
3006
3007 }
3008 }
3009
3010mmap_unlock_and_return:
3011 mutex_unlock(&zr->resource_lock);
3012
3013 return res;
3014}
3015
3016static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3017 .vidioc_querycap = zoran_querycap,
3018 .vidioc_cropcap = zoran_cropcap,
3019 .vidioc_s_crop = zoran_s_crop,
3020 .vidioc_g_crop = zoran_g_crop,
3021 .vidioc_enum_input = zoran_enum_input,
3022 .vidioc_g_input = zoran_g_input,
3023 .vidioc_s_input = zoran_s_input,
3024 .vidioc_enum_output = zoran_enum_output,
3025 .vidioc_g_output = zoran_g_output,
3026 .vidioc_s_output = zoran_s_output,
3027 .vidioc_g_fbuf = zoran_g_fbuf,
3028 .vidioc_s_fbuf = zoran_s_fbuf,
3029 .vidioc_g_std = zoran_g_std,
3030 .vidioc_s_std = zoran_s_std,
3031 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3032 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3033 .vidioc_overlay = zoran_overlay,
3034 .vidioc_reqbufs = zoran_reqbufs,
3035 .vidioc_querybuf = zoran_querybuf,
3036 .vidioc_qbuf = zoran_qbuf,
3037 .vidioc_dqbuf = zoran_dqbuf,
3038 .vidioc_streamon = zoran_streamon,
3039 .vidioc_streamoff = zoran_streamoff,
3040 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3041 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3042 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3043 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3044 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3045 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3046 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3047 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3048 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3049 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3050 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3051 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3052 .vidioc_queryctrl = zoran_queryctrl,
3053 .vidioc_s_ctrl = zoran_s_ctrl,
3054 .vidioc_g_ctrl = zoran_g_ctrl,
3055};
3056
3057/* please use zr->resource_lock consistently and kill this wrapper */
3058static long zoran_ioctl(struct file *file, unsigned int cmd,
3059 unsigned long arg)
3060{
3061 struct zoran_fh *fh = file->private_data;
3062 struct zoran *zr = fh->zr;
3063 int ret;
3064
3065 mutex_lock(&zr->other_lock);
3066 ret = video_ioctl2(file, cmd, arg);
3067 mutex_unlock(&zr->other_lock);
3068
3069 return ret;
3070}
3071
3072static const struct v4l2_file_operations zoran_fops = {
3073 .owner = THIS_MODULE,
3074 .open = zoran_open,
3075 .release = zoran_close,
3076 .unlocked_ioctl = zoran_ioctl,
3077 .read = zoran_read,
3078 .write = zoran_write,
3079 .mmap = zoran_mmap,
3080 .poll = zoran_poll,
3081};
3082
3083struct video_device zoran_template __devinitdata = {
3084 .name = ZORAN_NAME,
3085 .fops = &zoran_fops,
3086 .ioctl_ops = &zoran_ioctl_ops,
3087 .release = &zoran_vdev_release,
3088 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
3089};
3090
diff --git a/drivers/media/pci/zoran/zoran_procfs.c b/drivers/media/pci/zoran/zoran_procfs.c
new file mode 100644
index 000000000000..f1423b777db1
--- /dev/null
+++ b/drivers/media/pci/zoran/zoran_procfs.c
@@ -0,0 +1,225 @@
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles the procFS entries (/proc/ZORAN[%d])
7 *
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 * Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Mailinglist <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/types.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/vmalloc.h>
34
35#include <linux/proc_fs.h>
36#include <linux/pci.h>
37#include <linux/i2c.h>
38#include <linux/i2c-algo-bit.h>
39#include <linux/videodev2.h>
40#include <linux/spinlock.h>
41#include <linux/sem.h>
42#include <linux/seq_file.h>
43
44#include <linux/ctype.h>
45#include <linux/poll.h>
46#include <asm/io.h>
47
48#include "videocodec.h"
49#include "zoran.h"
50#include "zoran_procfs.h"
51#include "zoran_card.h"
52
53#ifdef CONFIG_PROC_FS
54struct procfs_params_zr36067 {
55 char *name;
56 short reg;
57 u32 mask;
58 short bit;
59};
60
61static const struct procfs_params_zr36067 zr67[] = {
62 {"HSPol", 0x000, 1, 30},
63 {"HStart", 0x000, 0x3ff, 10},
64 {"HEnd", 0x000, 0x3ff, 0},
65
66 {"VSPol", 0x004, 1, 30},
67 {"VStart", 0x004, 0x3ff, 10},
68 {"VEnd", 0x004, 0x3ff, 0},
69
70 {"ExtFl", 0x008, 1, 26},
71 {"TopField", 0x008, 1, 25},
72 {"VCLKPol", 0x008, 1, 24},
73 {"DupFld", 0x008, 1, 20},
74 {"LittleEndian", 0x008, 1, 0},
75
76 {"HsyncStart", 0x10c, 0xffff, 16},
77 {"LineTot", 0x10c, 0xffff, 0},
78
79 {"NAX", 0x110, 0xffff, 16},
80 {"PAX", 0x110, 0xffff, 0},
81
82 {"NAY", 0x114, 0xffff, 16},
83 {"PAY", 0x114, 0xffff, 0},
84
85 /* {"",,,}, */
86
87 {NULL, 0, 0, 0},
88};
89
90static void
91setparam (struct zoran *zr,
92 char *name,
93 char *sval)
94{
95 int i = 0, reg0, reg, val;
96
97 while (zr67[i].name != NULL) {
98 if (!strncmp(name, zr67[i].name, strlen(zr67[i].name))) {
99 reg = reg0 = btread(zr67[i].reg);
100 reg &= ~(zr67[i].mask << zr67[i].bit);
101 if (!isdigit(sval[0]))
102 break;
103 val = simple_strtoul(sval, NULL, 0);
104 if ((val & ~zr67[i].mask))
105 break;
106 reg |= (val & zr67[i].mask) << zr67[i].bit;
107 dprintk(4,
108 KERN_INFO
109 "%s: setparam: setting ZR36067 register 0x%03x: 0x%08x=>0x%08x %s=%d\n",
110 ZR_DEVNAME(zr), zr67[i].reg, reg0, reg,
111 zr67[i].name, val);
112 btwrite(reg, zr67[i].reg);
113 break;
114 }
115 i++;
116 }
117}
118
119static int zoran_show(struct seq_file *p, void *v)
120{
121 struct zoran *zr = p->private;
122 int i;
123
124 seq_printf(p, "ZR36067 registers:\n");
125 for (i = 0; i < 0x130; i += 16)
126 seq_printf(p, "%03X %08X %08X %08X %08X \n", i,
127 btread(i), btread(i+4), btread(i+8), btread(i+12));
128 return 0;
129}
130
131static int zoran_open(struct inode *inode, struct file *file)
132{
133 struct zoran *data = PDE(inode)->data;
134 return single_open(file, zoran_show, data);
135}
136
137static ssize_t zoran_write(struct file *file, const char __user *buffer,
138 size_t count, loff_t *ppos)
139{
140 struct zoran *zr = PDE(file->f_path.dentry->d_inode)->data;
141 char *string, *sp;
142 char *line, *ldelim, *varname, *svar, *tdelim;
143
144 if (count > 32768) /* Stupidity filter */
145 return -EINVAL;
146
147 string = sp = vmalloc(count + 1);
148 if (!string) {
149 dprintk(1,
150 KERN_ERR
151 "%s: write_proc: can not allocate memory\n",
152 ZR_DEVNAME(zr));
153 return -ENOMEM;
154 }
155 if (copy_from_user(string, buffer, count)) {
156 vfree (string);
157 return -EFAULT;
158 }
159 string[count] = 0;
160 dprintk(4, KERN_INFO "%s: write_proc: name=%s count=%zu zr=%p\n",
161 ZR_DEVNAME(zr), file->f_path.dentry->d_name.name, count, zr);
162 ldelim = " \t\n";
163 tdelim = "=";
164 line = strpbrk(sp, ldelim);
165 while (line) {
166 *line = 0;
167 svar = strpbrk(sp, tdelim);
168 if (svar) {
169 *svar = 0;
170 varname = sp;
171 svar++;
172 setparam(zr, varname, svar);
173 }
174 sp = line + 1;
175 line = strpbrk(sp, ldelim);
176 }
177 vfree(string);
178
179 return count;
180}
181
182static const struct file_operations zoran_operations = {
183 .owner = THIS_MODULE,
184 .open = zoran_open,
185 .read = seq_read,
186 .write = zoran_write,
187 .llseek = seq_lseek,
188 .release = single_release,
189};
190#endif
191
192int
193zoran_proc_init (struct zoran *zr)
194{
195#ifdef CONFIG_PROC_FS
196 char name[8];
197
198 snprintf(name, 7, "zoran%d", zr->id);
199 zr->zoran_proc = proc_create_data(name, 0, NULL, &zoran_operations, zr);
200 if (zr->zoran_proc != NULL) {
201 dprintk(2,
202 KERN_INFO
203 "%s: procfs entry /proc/%s allocated. data=%p\n",
204 ZR_DEVNAME(zr), name, zr->zoran_proc->data);
205 } else {
206 dprintk(1, KERN_ERR "%s: Unable to initialise /proc/%s\n",
207 ZR_DEVNAME(zr), name);
208 return 1;
209 }
210#endif
211 return 0;
212}
213
214void
215zoran_proc_cleanup (struct zoran *zr)
216{
217#ifdef CONFIG_PROC_FS
218 char name[8];
219
220 snprintf(name, 7, "zoran%d", zr->id);
221 if (zr->zoran_proc)
222 remove_proc_entry(name, NULL);
223 zr->zoran_proc = NULL;
224#endif
225}
diff --git a/drivers/media/pci/zoran/zoran_procfs.h b/drivers/media/pci/zoran/zoran_procfs.h
new file mode 100644
index 000000000000..f2d5b1ba448f
--- /dev/null
+++ b/drivers/media/pci/zoran/zoran_procfs.h
@@ -0,0 +1,36 @@
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles card-specific data and detection
7 *
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 * Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Mailinglist <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#ifndef __ZORAN_PROCFS_H__
31#define __ZORAN_PROCFS_H__
32
33extern int zoran_proc_init(struct zoran *zr);
34extern void zoran_proc_cleanup(struct zoran *zr);
35
36#endif /* __ZORAN_PROCFS_H__ */
diff --git a/drivers/media/pci/zoran/zr36016.c b/drivers/media/pci/zoran/zr36016.c
new file mode 100644
index 000000000000..b87ddba8608f
--- /dev/null
+++ b/drivers/media/pci/zoran/zr36016.c
@@ -0,0 +1,524 @@
1/*
2 * Zoran ZR36016 basic configuration functions
3 *
4 * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
5 *
6 * $Id: zr36016.c,v 1.1.2.14 2003/08/20 19:46:55 rbultje Exp $
7 *
8 * ------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * ------------------------------------------------------------------------
25 */
26
27#define ZR016_VERSION "v0.7"
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/delay.h>
33
34#include <linux/types.h>
35#include <linux/wait.h>
36
37/* I/O commands, error codes */
38#include <asm/io.h>
39
40/* v4l API */
41
42/* headerfile of this module */
43#include "zr36016.h"
44
45/* codec io API */
46#include "videocodec.h"
47
48/* it doesn't make sense to have more than 20 or so,
49 just to prevent some unwanted loops */
50#define MAX_CODECS 20
51
52/* amount of chips attached via this driver */
53static int zr36016_codecs;
54
55/* debugging is available via module parameter */
56static int debug;
57module_param(debug, int, 0);
58MODULE_PARM_DESC(debug, "Debug level (0-4)");
59
60#define dprintk(num, format, args...) \
61 do { \
62 if (debug >= num) \
63 printk(format, ##args); \
64 } while (0)
65
66/* =========================================================================
67 Local hardware I/O functions:
68
69 read/write via codec layer (registers are located in the master device)
70 ========================================================================= */
71
72/* read and write functions */
73static u8
74zr36016_read (struct zr36016 *ptr,
75 u16 reg)
76{
77 u8 value = 0;
78
79 // just in case something is wrong...
80 if (ptr->codec->master_data->readreg)
81 value =
82 (ptr->codec->master_data->
83 readreg(ptr->codec, reg)) & 0xFF;
84 else
85 dprintk(1,
86 KERN_ERR "%s: invalid I/O setup, nothing read!\n",
87 ptr->name);
88
89 dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr->name, reg,
90 value);
91
92 return value;
93}
94
95static void
96zr36016_write (struct zr36016 *ptr,
97 u16 reg,
98 u8 value)
99{
100 dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr->name, value,
101 reg);
102
103 // just in case something is wrong...
104 if (ptr->codec->master_data->writereg) {
105 ptr->codec->master_data->writereg(ptr->codec, reg, value);
106 } else
107 dprintk(1,
108 KERN_ERR
109 "%s: invalid I/O setup, nothing written!\n",
110 ptr->name);
111}
112
113/* indirect read and write functions */
114/* the 016 supports auto-addr-increment, but
115 * writing it all time cost not much and is safer... */
116static u8
117zr36016_readi (struct zr36016 *ptr,
118 u16 reg)
119{
120 u8 value = 0;
121
122 // just in case something is wrong...
123 if ((ptr->codec->master_data->writereg) &&
124 (ptr->codec->master_data->readreg)) {
125 ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
126 value = (ptr->codec->master_data->readreg(ptr->codec, ZR016_IDATA)) & 0xFF; // DATA
127 } else
128 dprintk(1,
129 KERN_ERR
130 "%s: invalid I/O setup, nothing read (i)!\n",
131 ptr->name);
132
133 dprintk(4, "%s: reading indirect from 0x%04x: %02x\n", ptr->name,
134 reg, value);
135 return value;
136}
137
138static void
139zr36016_writei (struct zr36016 *ptr,
140 u16 reg,
141 u8 value)
142{
143 dprintk(4, "%s: writing indirect 0x%02x to 0x%04x\n", ptr->name,
144 value, reg);
145
146 // just in case something is wrong...
147 if (ptr->codec->master_data->writereg) {
148 ptr->codec->master_data->writereg(ptr->codec, ZR016_IADDR, reg & 0x0F); // ADDR
149 ptr->codec->master_data->writereg(ptr->codec, ZR016_IDATA, value & 0x0FF); // DATA
150 } else
151 dprintk(1,
152 KERN_ERR
153 "%s: invalid I/O setup, nothing written (i)!\n",
154 ptr->name);
155}
156
157/* =========================================================================
158 Local helper function:
159
160 version read
161 ========================================================================= */
162
163/* version kept in datastructure */
164static u8
165zr36016_read_version (struct zr36016 *ptr)
166{
167 ptr->version = zr36016_read(ptr, 0) >> 4;
168 return ptr->version;
169}
170
171/* =========================================================================
172 Local helper function:
173
174 basic test of "connectivity", writes/reads to/from PAX-Lo register
175 ========================================================================= */
176
177static int
178zr36016_basic_test (struct zr36016 *ptr)
179{
180 if (debug) {
181 int i;
182 zr36016_writei(ptr, ZR016I_PAX_LO, 0x55);
183 dprintk(1, KERN_INFO "%s: registers: ", ptr->name);
184 for (i = 0; i <= 0x0b; i++)
185 dprintk(1, "%02x ", zr36016_readi(ptr, i));
186 dprintk(1, "\n");
187 }
188 // for testing just write 0, then the default value to a register and read
189 // it back in both cases
190 zr36016_writei(ptr, ZR016I_PAX_LO, 0x00);
191 if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0) {
192 dprintk(1,
193 KERN_ERR
194 "%s: attach failed, can't connect to vfe processor!\n",
195 ptr->name);
196 return -ENXIO;
197 }
198 zr36016_writei(ptr, ZR016I_PAX_LO, 0x0d0);
199 if (zr36016_readi(ptr, ZR016I_PAX_LO) != 0x0d0) {
200 dprintk(1,
201 KERN_ERR
202 "%s: attach failed, can't connect to vfe processor!\n",
203 ptr->name);
204 return -ENXIO;
205 }
206 // we allow version numbers from 0-3, should be enough, though
207 zr36016_read_version(ptr);
208 if (ptr->version & 0x0c) {
209 dprintk(1,
210 KERN_ERR
211 "%s: attach failed, suspicious version %d found...\n",
212 ptr->name, ptr->version);
213 return -ENXIO;
214 }
215
216 return 0; /* looks good! */
217}
218
219/* =========================================================================
220 Local helper function:
221
222 simple loop for pushing the init datasets - NO USE --
223 ========================================================================= */
224
225#if 0
226static int zr36016_pushit (struct zr36016 *ptr,
227 u16 startreg,
228 u16 len,
229 const char *data)
230{
231 int i=0;
232
233 dprintk(4, "%s: write data block to 0x%04x (len=%d)\n",
234 ptr->name, startreg,len);
235 while (i<len) {
236 zr36016_writei(ptr, startreg++, data[i++]);
237 }
238
239 return i;
240}
241#endif
242
243/* =========================================================================
244 Basic datasets & init:
245
246 //TODO//
247 ========================================================================= */
248
249// needed offset values PAL NTSC SECAM
250static const int zr016_xoff[] = { 20, 20, 20 };
251static const int zr016_yoff[] = { 8, 9, 7 };
252
253static void
254zr36016_init (struct zr36016 *ptr)
255{
256 // stop any processing
257 zr36016_write(ptr, ZR016_GOSTOP, 0);
258
259 // mode setup (yuv422 in and out, compression/expansuon due to mode)
260 zr36016_write(ptr, ZR016_MODE,
261 ZR016_YUV422 | ZR016_YUV422_YUV422 |
262 (ptr->mode == CODEC_DO_COMPRESSION ?
263 ZR016_COMPRESSION : ZR016_EXPANSION));
264
265 // misc setup
266 zr36016_writei(ptr, ZR016I_SETUP1,
267 (ptr->xdec ? (ZR016_HRFL | ZR016_HORZ) : 0) |
268 (ptr->ydec ? ZR016_VERT : 0) | ZR016_CNTI);
269 zr36016_writei(ptr, ZR016I_SETUP2, ZR016_CCIR);
270
271 // Window setup
272 // (no extra offset for now, norm defines offset, default width height)
273 zr36016_writei(ptr, ZR016I_PAX_HI, ptr->width >> 8);
274 zr36016_writei(ptr, ZR016I_PAX_LO, ptr->width & 0xFF);
275 zr36016_writei(ptr, ZR016I_PAY_HI, ptr->height >> 8);
276 zr36016_writei(ptr, ZR016I_PAY_LO, ptr->height & 0xFF);
277 zr36016_writei(ptr, ZR016I_NAX_HI, ptr->xoff >> 8);
278 zr36016_writei(ptr, ZR016I_NAX_LO, ptr->xoff & 0xFF);
279 zr36016_writei(ptr, ZR016I_NAY_HI, ptr->yoff >> 8);
280 zr36016_writei(ptr, ZR016I_NAY_LO, ptr->yoff & 0xFF);
281
282 /* shall we continue now, please? */
283 zr36016_write(ptr, ZR016_GOSTOP, 1);
284}
285
286/* =========================================================================
287 CODEC API FUNCTIONS
288
289 this functions are accessed by the master via the API structure
290 ========================================================================= */
291
292/* set compression/expansion mode and launches codec -
293 this should be the last call from the master before starting processing */
294static int
295zr36016_set_mode (struct videocodec *codec,
296 int mode)
297{
298 struct zr36016 *ptr = (struct zr36016 *) codec->data;
299
300 dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
301
302 if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
303 return -EINVAL;
304
305 ptr->mode = mode;
306 zr36016_init(ptr);
307
308 return 0;
309}
310
311/* set picture size */
312static int
313zr36016_set_video (struct videocodec *codec,
314 struct tvnorm *norm,
315 struct vfe_settings *cap,
316 struct vfe_polarity *pol)
317{
318 struct zr36016 *ptr = (struct zr36016 *) codec->data;
319
320 dprintk(2, "%s: set_video %d.%d, %d/%d-%dx%d (0x%x) call\n",
321 ptr->name, norm->HStart, norm->VStart,
322 cap->x, cap->y, cap->width, cap->height,
323 cap->decimation);
324
325 /* if () return -EINVAL;
326 * trust the master driver that it knows what it does - so
327 * we allow invalid startx/y for now ... */
328 ptr->width = cap->width;
329 ptr->height = cap->height;
330 /* (Ronald) This is ugly. zoran_device.c, line 387
331 * already mentions what happens if HStart is even
332 * (blue faces, etc., cr/cb inversed). There's probably
333 * some good reason why HStart is 0 instead of 1, so I'm
334 * leaving it to this for now, but really... This can be
335 * done a lot simpler */
336 ptr->xoff = (norm->HStart ? norm->HStart : 1) + cap->x;
337 /* Something to note here (I don't understand it), setting
338 * VStart too high will cause the codec to 'not work'. I
339 * really don't get it. values of 16 (VStart) already break
340 * it here. Just '0' seems to work. More testing needed! */
341 ptr->yoff = norm->VStart + cap->y;
342 /* (Ronald) dzjeeh, can't this thing do hor_decimation = 4? */
343 ptr->xdec = ((cap->decimation & 0xff) == 1) ? 0 : 1;
344 ptr->ydec = (((cap->decimation >> 8) & 0xff) == 1) ? 0 : 1;
345
346 return 0;
347}
348
349/* additional control functions */
350static int
351zr36016_control (struct videocodec *codec,
352 int type,
353 int size,
354 void *data)
355{
356 struct zr36016 *ptr = (struct zr36016 *) codec->data;
357 int *ival = (int *) data;
358
359 dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type,
360 size);
361
362 switch (type) {
363 case CODEC_G_STATUS: /* get last status - we don't know it ... */
364 if (size != sizeof(int))
365 return -EFAULT;
366 *ival = 0;
367 break;
368
369 case CODEC_G_CODEC_MODE:
370 if (size != sizeof(int))
371 return -EFAULT;
372 *ival = 0;
373 break;
374
375 case CODEC_S_CODEC_MODE:
376 if (size != sizeof(int))
377 return -EFAULT;
378 if (*ival != 0)
379 return -EINVAL;
380 /* not needed, do nothing */
381 return 0;
382
383 case CODEC_G_VFE:
384 case CODEC_S_VFE:
385 return 0;
386
387 case CODEC_S_MMAP:
388 /* not available, give an error */
389 return -ENXIO;
390
391 default:
392 return -EINVAL;
393 }
394
395 return size;
396}
397
398/* =========================================================================
399 Exit and unregister function:
400
401 Deinitializes Zoran's JPEG processor
402 ========================================================================= */
403
404static int
405zr36016_unset (struct videocodec *codec)
406{
407 struct zr36016 *ptr = codec->data;
408
409 if (ptr) {
410 /* do wee need some codec deinit here, too ???? */
411
412 dprintk(1, "%s: finished codec #%d\n", ptr->name,
413 ptr->num);
414 kfree(ptr);
415 codec->data = NULL;
416
417 zr36016_codecs--;
418 return 0;
419 }
420
421 return -EFAULT;
422}
423
424/* =========================================================================
425 Setup and registry function:
426
427 Initializes Zoran's JPEG processor
428
429 Also sets pixel size, average code size, mode (compr./decompr.)
430 (the given size is determined by the processor with the video interface)
431 ========================================================================= */
432
433static int
434zr36016_setup (struct videocodec *codec)
435{
436 struct zr36016 *ptr;
437 int res;
438
439 dprintk(2, "zr36016: initializing VFE subsystem #%d.\n",
440 zr36016_codecs);
441
442 if (zr36016_codecs == MAX_CODECS) {
443 dprintk(1,
444 KERN_ERR "zr36016: Can't attach more codecs!\n");
445 return -ENOSPC;
446 }
447 //mem structure init
448 codec->data = ptr = kzalloc(sizeof(struct zr36016), GFP_KERNEL);
449 if (NULL == ptr) {
450 dprintk(1, KERN_ERR "zr36016: Can't get enough memory!\n");
451 return -ENOMEM;
452 }
453
454 snprintf(ptr->name, sizeof(ptr->name), "zr36016[%d]",
455 zr36016_codecs);
456 ptr->num = zr36016_codecs++;
457 ptr->codec = codec;
458
459 //testing
460 res = zr36016_basic_test(ptr);
461 if (res < 0) {
462 zr36016_unset(codec);
463 return res;
464 }
465 //final setup
466 ptr->mode = CODEC_DO_COMPRESSION;
467 ptr->width = 768;
468 ptr->height = 288;
469 ptr->xdec = 1;
470 ptr->ydec = 0;
471 zr36016_init(ptr);
472
473 dprintk(1, KERN_INFO "%s: codec v%d attached and running\n",
474 ptr->name, ptr->version);
475
476 return 0;
477}
478
479static const struct videocodec zr36016_codec = {
480 .owner = THIS_MODULE,
481 .name = "zr36016",
482 .magic = 0L, // magic not used
483 .flags =
484 CODEC_FLAG_HARDWARE | CODEC_FLAG_VFE | CODEC_FLAG_ENCODER |
485 CODEC_FLAG_DECODER,
486 .type = CODEC_TYPE_ZR36016,
487 .setup = zr36016_setup, // functionality
488 .unset = zr36016_unset,
489 .set_mode = zr36016_set_mode,
490 .set_video = zr36016_set_video,
491 .control = zr36016_control,
492 // others are not used
493};
494
495/* =========================================================================
496 HOOK IN DRIVER AS KERNEL MODULE
497 ========================================================================= */
498
499static int __init
500zr36016_init_module (void)
501{
502 //dprintk(1, "ZR36016 driver %s\n",ZR016_VERSION);
503 zr36016_codecs = 0;
504 return videocodec_register(&zr36016_codec);
505}
506
507static void __exit
508zr36016_cleanup_module (void)
509{
510 if (zr36016_codecs) {
511 dprintk(1,
512 "zr36016: something's wrong - %d codecs left somehow.\n",
513 zr36016_codecs);
514 }
515 videocodec_unregister(&zr36016_codec);
516}
517
518module_init(zr36016_init_module);
519module_exit(zr36016_cleanup_module);
520
521MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
522MODULE_DESCRIPTION("Driver module for ZR36016 video frontends "
523 ZR016_VERSION);
524MODULE_LICENSE("GPL");
diff --git a/drivers/media/pci/zoran/zr36016.h b/drivers/media/pci/zoran/zr36016.h
new file mode 100644
index 000000000000..8c79229f69d1
--- /dev/null
+++ b/drivers/media/pci/zoran/zr36016.h
@@ -0,0 +1,111 @@
1/*
2 * Zoran ZR36016 basic configuration functions - header file
3 *
4 * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
5 *
6 * $Id: zr36016.h,v 1.1.2.3 2003/01/14 21:18:07 rbultje Exp $
7 *
8 * ------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * ------------------------------------------------------------------------
25 */
26
27#ifndef ZR36016_H
28#define ZR36016_H
29
30/* data stored for each zoran jpeg codec chip */
31struct zr36016 {
32 char name[32];
33 int num;
34 /* io datastructure */
35 struct videocodec *codec;
36 // coder status
37 __u8 version;
38 // actual coder setup
39 int mode;
40
41 __u16 xoff;
42 __u16 yoff;
43 __u16 width;
44 __u16 height;
45 __u16 xdec;
46 __u16 ydec;
47};
48
49/* direct register addresses */
50#define ZR016_GOSTOP 0x00
51#define ZR016_MODE 0x01
52#define ZR016_IADDR 0x02
53#define ZR016_IDATA 0x03
54
55/* indirect register addresses */
56#define ZR016I_SETUP1 0x00
57#define ZR016I_SETUP2 0x01
58#define ZR016I_NAX_LO 0x02
59#define ZR016I_NAX_HI 0x03
60#define ZR016I_PAX_LO 0x04
61#define ZR016I_PAX_HI 0x05
62#define ZR016I_NAY_LO 0x06
63#define ZR016I_NAY_HI 0x07
64#define ZR016I_PAY_LO 0x08
65#define ZR016I_PAY_HI 0x09
66#define ZR016I_NOL_LO 0x0a
67#define ZR016I_NOL_HI 0x0b
68
69/* possible values for mode register */
70#define ZR016_RGB444_YUV444 0x00
71#define ZR016_RGB444_YUV422 0x01
72#define ZR016_RGB444_YUV411 0x02
73#define ZR016_RGB444_Y400 0x03
74#define ZR016_RGB444_RGB444 0x04
75#define ZR016_YUV444_YUV444 0x08
76#define ZR016_YUV444_YUV422 0x09
77#define ZR016_YUV444_YUV411 0x0a
78#define ZR016_YUV444_Y400 0x0b
79#define ZR016_YUV444_RGB444 0x0c
80#define ZR016_YUV422_YUV422 0x11
81#define ZR016_YUV422_YUV411 0x12
82#define ZR016_YUV422_Y400 0x13
83#define ZR016_YUV411_YUV411 0x16
84#define ZR016_YUV411_Y400 0x17
85#define ZR016_4444_4444 0x19
86#define ZR016_100_100 0x1b
87
88#define ZR016_RGB444 0x00
89#define ZR016_YUV444 0x20
90#define ZR016_YUV422 0x40
91
92#define ZR016_COMPRESSION 0x80
93#define ZR016_EXPANSION 0x80
94
95/* possible values for setup 1 register */
96#define ZR016_CKRT 0x80
97#define ZR016_VERT 0x40
98#define ZR016_HORZ 0x20
99#define ZR016_HRFL 0x10
100#define ZR016_DSFL 0x08
101#define ZR016_SBFL 0x04
102#define ZR016_RSTR 0x02
103#define ZR016_CNTI 0x01
104
105/* possible values for setup 2 register */
106#define ZR016_SYEN 0x40
107#define ZR016_CCIR 0x04
108#define ZR016_SIGN 0x02
109#define ZR016_YMCS 0x01
110
111#endif /*fndef ZR36016_H */
diff --git a/drivers/media/pci/zoran/zr36050.c b/drivers/media/pci/zoran/zr36050.c
new file mode 100644
index 000000000000..e1985609af4b
--- /dev/null
+++ b/drivers/media/pci/zoran/zr36050.c
@@ -0,0 +1,900 @@
1/*
2 * Zoran ZR36050 basic configuration functions
3 *
4 * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
5 *
6 * $Id: zr36050.c,v 1.1.2.11 2003/08/03 14:54:53 rbultje Exp $
7 *
8 * ------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * ------------------------------------------------------------------------
25 */
26
27#define ZR050_VERSION "v0.7.1"
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/delay.h>
33
34#include <linux/types.h>
35#include <linux/wait.h>
36
37/* I/O commands, error codes */
38#include <asm/io.h>
39
40/* headerfile of this module */
41#include "zr36050.h"
42
43/* codec io API */
44#include "videocodec.h"
45
46/* it doesn't make sense to have more than 20 or so,
47 just to prevent some unwanted loops */
48#define MAX_CODECS 20
49
50/* amount of chips attached via this driver */
51static int zr36050_codecs;
52
53/* debugging is available via module parameter */
54static int debug;
55module_param(debug, int, 0);
56MODULE_PARM_DESC(debug, "Debug level (0-4)");
57
58#define dprintk(num, format, args...) \
59 do { \
60 if (debug >= num) \
61 printk(format, ##args); \
62 } while (0)
63
64/* =========================================================================
65 Local hardware I/O functions:
66
67 read/write via codec layer (registers are located in the master device)
68 ========================================================================= */
69
70/* read and write functions */
71static u8
72zr36050_read (struct zr36050 *ptr,
73 u16 reg)
74{
75 u8 value = 0;
76
77 // just in case something is wrong...
78 if (ptr->codec->master_data->readreg)
79 value = (ptr->codec->master_data->readreg(ptr->codec,
80 reg)) & 0xFF;
81 else
82 dprintk(1,
83 KERN_ERR "%s: invalid I/O setup, nothing read!\n",
84 ptr->name);
85
86 dprintk(4, "%s: reading from 0x%04x: %02x\n", ptr->name, reg,
87 value);
88
89 return value;
90}
91
92static void
93zr36050_write (struct zr36050 *ptr,
94 u16 reg,
95 u8 value)
96{
97 dprintk(4, "%s: writing 0x%02x to 0x%04x\n", ptr->name, value,
98 reg);
99
100 // just in case something is wrong...
101 if (ptr->codec->master_data->writereg)
102 ptr->codec->master_data->writereg(ptr->codec, reg, value);
103 else
104 dprintk(1,
105 KERN_ERR
106 "%s: invalid I/O setup, nothing written!\n",
107 ptr->name);
108}
109
110/* =========================================================================
111 Local helper function:
112
113 status read
114 ========================================================================= */
115
116/* status is kept in datastructure */
117static u8
118zr36050_read_status1 (struct zr36050 *ptr)
119{
120 ptr->status1 = zr36050_read(ptr, ZR050_STATUS_1);
121
122 zr36050_read(ptr, 0);
123 return ptr->status1;
124}
125
126/* =========================================================================
127 Local helper function:
128
129 scale factor read
130 ========================================================================= */
131
132/* scale factor is kept in datastructure */
133static u16
134zr36050_read_scalefactor (struct zr36050 *ptr)
135{
136 ptr->scalefact = (zr36050_read(ptr, ZR050_SF_HI) << 8) |
137 (zr36050_read(ptr, ZR050_SF_LO) & 0xFF);
138
139 /* leave 0 selected for an eventually GO from master */
140 zr36050_read(ptr, 0);
141 return ptr->scalefact;
142}
143
144/* =========================================================================
145 Local helper function:
146
147 wait if codec is ready to proceed (end of processing) or time is over
148 ========================================================================= */
149
150static void
151zr36050_wait_end (struct zr36050 *ptr)
152{
153 int i = 0;
154
155 while (!(zr36050_read_status1(ptr) & 0x4)) {
156 udelay(1);
157 if (i++ > 200000) { // 200ms, there is for sure something wrong!!!
158 dprintk(1,
159 "%s: timeout at wait_end (last status: 0x%02x)\n",
160 ptr->name, ptr->status1);
161 break;
162 }
163 }
164}
165
166/* =========================================================================
167 Local helper function:
168
169 basic test of "connectivity", writes/reads to/from memory the SOF marker
170 ========================================================================= */
171
172static int
173zr36050_basic_test (struct zr36050 *ptr)
174{
175 zr36050_write(ptr, ZR050_SOF_IDX, 0x00);
176 zr36050_write(ptr, ZR050_SOF_IDX + 1, 0x00);
177 if ((zr36050_read(ptr, ZR050_SOF_IDX) |
178 zr36050_read(ptr, ZR050_SOF_IDX + 1)) != 0x0000) {
179 dprintk(1,
180 KERN_ERR
181 "%s: attach failed, can't connect to jpeg processor!\n",
182 ptr->name);
183 return -ENXIO;
184 }
185 zr36050_write(ptr, ZR050_SOF_IDX, 0xff);
186 zr36050_write(ptr, ZR050_SOF_IDX + 1, 0xc0);
187 if (((zr36050_read(ptr, ZR050_SOF_IDX) << 8) |
188 zr36050_read(ptr, ZR050_SOF_IDX + 1)) != 0xffc0) {
189 dprintk(1,
190 KERN_ERR
191 "%s: attach failed, can't connect to jpeg processor!\n",
192 ptr->name);
193 return -ENXIO;
194 }
195
196 zr36050_wait_end(ptr);
197 if ((ptr->status1 & 0x4) == 0) {
198 dprintk(1,
199 KERN_ERR
200 "%s: attach failed, jpeg processor failed (end flag)!\n",
201 ptr->name);
202 return -EBUSY;
203 }
204
205 return 0; /* looks good! */
206}
207
208/* =========================================================================
209 Local helper function:
210
211 simple loop for pushing the init datasets
212 ========================================================================= */
213
214static int
215zr36050_pushit (struct zr36050 *ptr,
216 u16 startreg,
217 u16 len,
218 const char *data)
219{
220 int i = 0;
221
222 dprintk(4, "%s: write data block to 0x%04x (len=%d)\n", ptr->name,
223 startreg, len);
224 while (i < len) {
225 zr36050_write(ptr, startreg++, data[i++]);
226 }
227
228 return i;
229}
230
231/* =========================================================================
232 Basic datasets:
233
234 jpeg baseline setup data (you find it on lots places in internet, or just
235 extract it from any regular .jpg image...)
236
237 Could be variable, but until it's not needed it they are just fixed to save
238 memory. Otherwise expand zr36050 structure with arrays, push the values to
239 it and initialize from there, as e.g. the linux zr36057/60 driver does it.
240 ========================================================================= */
241
242static const char zr36050_dqt[0x86] = {
243 0xff, 0xdb, //Marker: DQT
244 0x00, 0x84, //Length: 2*65+2
245 0x00, //Pq,Tq first table
246 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
247 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
248 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
249 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
250 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
251 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
252 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
253 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
254 0x01, //Pq,Tq second table
255 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
256 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
257 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
258 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
259 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
260 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
261 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
262 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
263};
264
265static const char zr36050_dht[0x1a4] = {
266 0xff, 0xc4, //Marker: DHT
267 0x01, 0xa2, //Length: 2*AC, 2*DC
268 0x00, //DC first table
269 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
270 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
271 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
272 0x01, //DC second table
273 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
274 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
275 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
276 0x10, //AC first table
277 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
278 0x05, 0x05, 0x04, 0x04, 0x00, 0x00,
279 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11,
280 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
281 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
282 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24,
283 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17,
284 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34,
285 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
286 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
287 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66,
288 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
289 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
290 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
291 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
292 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9,
293 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
294 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
295 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
296 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
297 0xF8, 0xF9, 0xFA,
298 0x11, //AC second table
299 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
300 0x07, 0x05, 0x04, 0x04, 0x00, 0x01,
301 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
302 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
303 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
304 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62,
305 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25,
306 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A,
307 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
308 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
309 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66,
310 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
311 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
312 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
313 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
314 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
315 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
316 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8,
317 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
318 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
319 0xF9, 0xFA
320};
321
322/* jpeg baseline setup, this is just fixed in this driver (YUV pictures) */
323#define NO_OF_COMPONENTS 0x3 //Y,U,V
324#define BASELINE_PRECISION 0x8 //MCU size (?)
325static const char zr36050_tq[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's QT
326static const char zr36050_td[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's DC
327static const char zr36050_ta[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's AC
328
329/* horizontal 422 decimation setup (maybe we support 411 or so later, too) */
330static const char zr36050_decimation_h[8] = { 2, 1, 1, 0, 0, 0, 0, 0 };
331static const char zr36050_decimation_v[8] = { 1, 1, 1, 0, 0, 0, 0, 0 };
332
333/* =========================================================================
334 Local helper functions:
335
336 calculation and setup of parameter-dependent JPEG baseline segments
337 (needed for compression only)
338 ========================================================================= */
339
340/* ------------------------------------------------------------------------- */
341
342/* SOF (start of frame) segment depends on width, height and sampling ratio
343 of each color component */
344
345static int
346zr36050_set_sof (struct zr36050 *ptr)
347{
348 char sof_data[34]; // max. size of register set
349 int i;
350
351 dprintk(3, "%s: write SOF (%dx%d, %d components)\n", ptr->name,
352 ptr->width, ptr->height, NO_OF_COMPONENTS);
353 sof_data[0] = 0xff;
354 sof_data[1] = 0xc0;
355 sof_data[2] = 0x00;
356 sof_data[3] = (3 * NO_OF_COMPONENTS) + 8;
357 sof_data[4] = BASELINE_PRECISION; // only '8' possible with zr36050
358 sof_data[5] = (ptr->height) >> 8;
359 sof_data[6] = (ptr->height) & 0xff;
360 sof_data[7] = (ptr->width) >> 8;
361 sof_data[8] = (ptr->width) & 0xff;
362 sof_data[9] = NO_OF_COMPONENTS;
363 for (i = 0; i < NO_OF_COMPONENTS; i++) {
364 sof_data[10 + (i * 3)] = i; // index identifier
365 sof_data[11 + (i * 3)] = (ptr->h_samp_ratio[i] << 4) | (ptr->v_samp_ratio[i]); // sampling ratios
366 sof_data[12 + (i * 3)] = zr36050_tq[i]; // Q table selection
367 }
368 return zr36050_pushit(ptr, ZR050_SOF_IDX,
369 (3 * NO_OF_COMPONENTS) + 10, sof_data);
370}
371
372/* ------------------------------------------------------------------------- */
373
374/* SOS (start of scan) segment depends on the used scan components
375 of each color component */
376
377static int
378zr36050_set_sos (struct zr36050 *ptr)
379{
380 char sos_data[16]; // max. size of register set
381 int i;
382
383 dprintk(3, "%s: write SOS\n", ptr->name);
384 sos_data[0] = 0xff;
385 sos_data[1] = 0xda;
386 sos_data[2] = 0x00;
387 sos_data[3] = 2 + 1 + (2 * NO_OF_COMPONENTS) + 3;
388 sos_data[4] = NO_OF_COMPONENTS;
389 for (i = 0; i < NO_OF_COMPONENTS; i++) {
390 sos_data[5 + (i * 2)] = i; // index
391 sos_data[6 + (i * 2)] = (zr36050_td[i] << 4) | zr36050_ta[i]; // AC/DC tbl.sel.
392 }
393 sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 2] = 00; // scan start
394 sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 3] = 0x3F;
395 sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 4] = 00;
396 return zr36050_pushit(ptr, ZR050_SOS1_IDX,
397 4 + 1 + (2 * NO_OF_COMPONENTS) + 3,
398 sos_data);
399}
400
401/* ------------------------------------------------------------------------- */
402
403/* DRI (define restart interval) */
404
405static int
406zr36050_set_dri (struct zr36050 *ptr)
407{
408 char dri_data[6]; // max. size of register set
409
410 dprintk(3, "%s: write DRI\n", ptr->name);
411 dri_data[0] = 0xff;
412 dri_data[1] = 0xdd;
413 dri_data[2] = 0x00;
414 dri_data[3] = 0x04;
415 dri_data[4] = ptr->dri >> 8;
416 dri_data[5] = ptr->dri & 0xff;
417 return zr36050_pushit(ptr, ZR050_DRI_IDX, 6, dri_data);
418}
419
420/* =========================================================================
421 Setup function:
422
423 Setup compression/decompression of Zoran's JPEG processor
424 ( see also zoran 36050 manual )
425
426 ... sorry for the spaghetti code ...
427 ========================================================================= */
428static void
429zr36050_init (struct zr36050 *ptr)
430{
431 int sum = 0;
432 long bitcnt, tmp;
433
434 if (ptr->mode == CODEC_DO_COMPRESSION) {
435 dprintk(2, "%s: COMPRESSION SETUP\n", ptr->name);
436
437 /* 050 communicates with 057 in master mode */
438 zr36050_write(ptr, ZR050_HARDWARE, ZR050_HW_MSTR);
439
440 /* encoding table preload for compression */
441 zr36050_write(ptr, ZR050_MODE,
442 ZR050_MO_COMP | ZR050_MO_TLM);
443 zr36050_write(ptr, ZR050_OPTIONS, 0);
444
445 /* disable all IRQs */
446 zr36050_write(ptr, ZR050_INT_REQ_0, 0);
447 zr36050_write(ptr, ZR050_INT_REQ_1, 3); // low 2 bits always 1
448
449 /* volume control settings */
450 /*zr36050_write(ptr, ZR050_MBCV, ptr->max_block_vol);*/
451 zr36050_write(ptr, ZR050_SF_HI, ptr->scalefact >> 8);
452 zr36050_write(ptr, ZR050_SF_LO, ptr->scalefact & 0xff);
453
454 zr36050_write(ptr, ZR050_AF_HI, 0xff);
455 zr36050_write(ptr, ZR050_AF_M, 0xff);
456 zr36050_write(ptr, ZR050_AF_LO, 0xff);
457
458 /* setup the variable jpeg tables */
459 sum += zr36050_set_sof(ptr);
460 sum += zr36050_set_sos(ptr);
461 sum += zr36050_set_dri(ptr);
462
463 /* setup the fixed jpeg tables - maybe variable, though -
464 * (see table init section above) */
465 dprintk(3, "%s: write DQT, DHT, APP\n", ptr->name);
466 sum += zr36050_pushit(ptr, ZR050_DQT_IDX,
467 sizeof(zr36050_dqt), zr36050_dqt);
468 sum += zr36050_pushit(ptr, ZR050_DHT_IDX,
469 sizeof(zr36050_dht), zr36050_dht);
470 zr36050_write(ptr, ZR050_APP_IDX, 0xff);
471 zr36050_write(ptr, ZR050_APP_IDX + 1, 0xe0 + ptr->app.appn);
472 zr36050_write(ptr, ZR050_APP_IDX + 2, 0x00);
473 zr36050_write(ptr, ZR050_APP_IDX + 3, ptr->app.len + 2);
474 sum += zr36050_pushit(ptr, ZR050_APP_IDX + 4, 60,
475 ptr->app.data) + 4;
476 zr36050_write(ptr, ZR050_COM_IDX, 0xff);
477 zr36050_write(ptr, ZR050_COM_IDX + 1, 0xfe);
478 zr36050_write(ptr, ZR050_COM_IDX + 2, 0x00);
479 zr36050_write(ptr, ZR050_COM_IDX + 3, ptr->com.len + 2);
480 sum += zr36050_pushit(ptr, ZR050_COM_IDX + 4, 60,
481 ptr->com.data) + 4;
482
483 /* do the internal huffman table preload */
484 zr36050_write(ptr, ZR050_MARKERS_EN, ZR050_ME_DHTI);
485
486 zr36050_write(ptr, ZR050_GO, 1); // launch codec
487 zr36050_wait_end(ptr);
488 dprintk(2, "%s: Status after table preload: 0x%02x\n",
489 ptr->name, ptr->status1);
490
491 if ((ptr->status1 & 0x4) == 0) {
492 dprintk(1, KERN_ERR "%s: init aborted!\n",
493 ptr->name);
494 return; // something is wrong, its timed out!!!!
495 }
496
497 /* setup misc. data for compression (target code sizes) */
498
499 /* size of compressed code to reach without header data */
500 sum = ptr->real_code_vol - sum;
501 bitcnt = sum << 3; /* need the size in bits */
502
503 tmp = bitcnt >> 16;
504 dprintk(3,
505 "%s: code: csize=%d, tot=%d, bit=%ld, highbits=%ld\n",
506 ptr->name, sum, ptr->real_code_vol, bitcnt, tmp);
507 zr36050_write(ptr, ZR050_TCV_NET_HI, tmp >> 8);
508 zr36050_write(ptr, ZR050_TCV_NET_MH, tmp & 0xff);
509 tmp = bitcnt & 0xffff;
510 zr36050_write(ptr, ZR050_TCV_NET_ML, tmp >> 8);
511 zr36050_write(ptr, ZR050_TCV_NET_LO, tmp & 0xff);
512
513 bitcnt -= bitcnt >> 7; // bits without stuffing
514 bitcnt -= ((bitcnt * 5) >> 6); // bits without eob
515
516 tmp = bitcnt >> 16;
517 dprintk(3, "%s: code: nettobit=%ld, highnettobits=%ld\n",
518 ptr->name, bitcnt, tmp);
519 zr36050_write(ptr, ZR050_TCV_DATA_HI, tmp >> 8);
520 zr36050_write(ptr, ZR050_TCV_DATA_MH, tmp & 0xff);
521 tmp = bitcnt & 0xffff;
522 zr36050_write(ptr, ZR050_TCV_DATA_ML, tmp >> 8);
523 zr36050_write(ptr, ZR050_TCV_DATA_LO, tmp & 0xff);
524
525 /* compression setup with or without bitrate control */
526 zr36050_write(ptr, ZR050_MODE,
527 ZR050_MO_COMP | ZR050_MO_PASS2 |
528 (ptr->bitrate_ctrl ? ZR050_MO_BRC : 0));
529
530 /* this headers seem to deliver "valid AVI" jpeg frames */
531 zr36050_write(ptr, ZR050_MARKERS_EN,
532 ZR050_ME_DQT | ZR050_ME_DHT |
533 ((ptr->app.len > 0) ? ZR050_ME_APP : 0) |
534 ((ptr->com.len > 0) ? ZR050_ME_COM : 0));
535 } else {
536 dprintk(2, "%s: EXPANSION SETUP\n", ptr->name);
537
538 /* 050 communicates with 055 in master mode */
539 zr36050_write(ptr, ZR050_HARDWARE,
540 ZR050_HW_MSTR | ZR050_HW_CFIS_2_CLK);
541
542 /* encoding table preload */
543 zr36050_write(ptr, ZR050_MODE, ZR050_MO_TLM);
544
545 /* disable all IRQs */
546 zr36050_write(ptr, ZR050_INT_REQ_0, 0);
547 zr36050_write(ptr, ZR050_INT_REQ_1, 3); // low 2 bits always 1
548
549 dprintk(3, "%s: write DHT\n", ptr->name);
550 zr36050_pushit(ptr, ZR050_DHT_IDX, sizeof(zr36050_dht),
551 zr36050_dht);
552
553 /* do the internal huffman table preload */
554 zr36050_write(ptr, ZR050_MARKERS_EN, ZR050_ME_DHTI);
555
556 zr36050_write(ptr, ZR050_GO, 1); // launch codec
557 zr36050_wait_end(ptr);
558 dprintk(2, "%s: Status after table preload: 0x%02x\n",
559 ptr->name, ptr->status1);
560
561 if ((ptr->status1 & 0x4) == 0) {
562 dprintk(1, KERN_ERR "%s: init aborted!\n",
563 ptr->name);
564 return; // something is wrong, its timed out!!!!
565 }
566
567 /* setup misc. data for expansion */
568 zr36050_write(ptr, ZR050_MODE, 0);
569 zr36050_write(ptr, ZR050_MARKERS_EN, 0);
570 }
571
572 /* adr on selected, to allow GO from master */
573 zr36050_read(ptr, 0);
574}
575
576/* =========================================================================
577 CODEC API FUNCTIONS
578
579 this functions are accessed by the master via the API structure
580 ========================================================================= */
581
582/* set compression/expansion mode and launches codec -
583 this should be the last call from the master before starting processing */
584static int
585zr36050_set_mode (struct videocodec *codec,
586 int mode)
587{
588 struct zr36050 *ptr = (struct zr36050 *) codec->data;
589
590 dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
591
592 if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
593 return -EINVAL;
594
595 ptr->mode = mode;
596 zr36050_init(ptr);
597
598 return 0;
599}
600
601/* set picture size (norm is ignored as the codec doesn't know about it) */
602static int
603zr36050_set_video (struct videocodec *codec,
604 struct tvnorm *norm,
605 struct vfe_settings *cap,
606 struct vfe_polarity *pol)
607{
608 struct zr36050 *ptr = (struct zr36050 *) codec->data;
609 int size;
610
611 dprintk(2, "%s: set_video %d.%d, %d/%d-%dx%d (0x%x) q%d call\n",
612 ptr->name, norm->HStart, norm->VStart,
613 cap->x, cap->y, cap->width, cap->height,
614 cap->decimation, cap->quality);
615 /* if () return -EINVAL;
616 * trust the master driver that it knows what it does - so
617 * we allow invalid startx/y and norm for now ... */
618 ptr->width = cap->width / (cap->decimation & 0xff);
619 ptr->height = cap->height / ((cap->decimation >> 8) & 0xff);
620
621 /* (KM) JPEG quality */
622 size = ptr->width * ptr->height;
623 size *= 16; /* size in bits */
624 /* apply quality setting */
625 size = size * cap->quality / 200;
626
627 /* Minimum: 1kb */
628 if (size < 8192)
629 size = 8192;
630 /* Maximum: 7/8 of code buffer */
631 if (size > ptr->total_code_vol * 7)
632 size = ptr->total_code_vol * 7;
633
634 ptr->real_code_vol = size >> 3; /* in bytes */
635
636 /* Set max_block_vol here (previously in zr36050_init, moved
637 * here for consistency with zr36060 code */
638 zr36050_write(ptr, ZR050_MBCV, ptr->max_block_vol);
639
640 return 0;
641}
642
643/* additional control functions */
644static int
645zr36050_control (struct videocodec *codec,
646 int type,
647 int size,
648 void *data)
649{
650 struct zr36050 *ptr = (struct zr36050 *) codec->data;
651 int *ival = (int *) data;
652
653 dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type,
654 size);
655
656 switch (type) {
657 case CODEC_G_STATUS: /* get last status */
658 if (size != sizeof(int))
659 return -EFAULT;
660 zr36050_read_status1(ptr);
661 *ival = ptr->status1;
662 break;
663
664 case CODEC_G_CODEC_MODE:
665 if (size != sizeof(int))
666 return -EFAULT;
667 *ival = CODEC_MODE_BJPG;
668 break;
669
670 case CODEC_S_CODEC_MODE:
671 if (size != sizeof(int))
672 return -EFAULT;
673 if (*ival != CODEC_MODE_BJPG)
674 return -EINVAL;
675 /* not needed, do nothing */
676 return 0;
677
678 case CODEC_G_VFE:
679 case CODEC_S_VFE:
680 /* not needed, do nothing */
681 return 0;
682
683 case CODEC_S_MMAP:
684 /* not available, give an error */
685 return -ENXIO;
686
687 case CODEC_G_JPEG_TDS_BYTE: /* get target volume in byte */
688 if (size != sizeof(int))
689 return -EFAULT;
690 *ival = ptr->total_code_vol;
691 break;
692
693 case CODEC_S_JPEG_TDS_BYTE: /* get target volume in byte */
694 if (size != sizeof(int))
695 return -EFAULT;
696 ptr->total_code_vol = *ival;
697 /* (Kieran Morrissey)
698 * code copied from zr36060.c to ensure proper bitrate */
699 ptr->real_code_vol = (ptr->total_code_vol * 6) >> 3;
700 break;
701
702 case CODEC_G_JPEG_SCALE: /* get scaling factor */
703 if (size != sizeof(int))
704 return -EFAULT;
705 *ival = zr36050_read_scalefactor(ptr);
706 break;
707
708 case CODEC_S_JPEG_SCALE: /* set scaling factor */
709 if (size != sizeof(int))
710 return -EFAULT;
711 ptr->scalefact = *ival;
712 break;
713
714 case CODEC_G_JPEG_APP_DATA: { /* get appn marker data */
715 struct jpeg_app_marker *app = data;
716
717 if (size != sizeof(struct jpeg_app_marker))
718 return -EFAULT;
719
720 *app = ptr->app;
721 break;
722 }
723
724 case CODEC_S_JPEG_APP_DATA: { /* set appn marker data */
725 struct jpeg_app_marker *app = data;
726
727 if (size != sizeof(struct jpeg_app_marker))
728 return -EFAULT;
729
730 ptr->app = *app;
731 break;
732 }
733
734 case CODEC_G_JPEG_COM_DATA: { /* get comment marker data */
735 struct jpeg_com_marker *com = data;
736
737 if (size != sizeof(struct jpeg_com_marker))
738 return -EFAULT;
739
740 *com = ptr->com;
741 break;
742 }
743
744 case CODEC_S_JPEG_COM_DATA: { /* set comment marker data */
745 struct jpeg_com_marker *com = data;
746
747 if (size != sizeof(struct jpeg_com_marker))
748 return -EFAULT;
749
750 ptr->com = *com;
751 break;
752 }
753
754 default:
755 return -EINVAL;
756 }
757
758 return size;
759}
760
761/* =========================================================================
762 Exit and unregister function:
763
764 Deinitializes Zoran's JPEG processor
765 ========================================================================= */
766
767static int
768zr36050_unset (struct videocodec *codec)
769{
770 struct zr36050 *ptr = codec->data;
771
772 if (ptr) {
773 /* do wee need some codec deinit here, too ???? */
774
775 dprintk(1, "%s: finished codec #%d\n", ptr->name,
776 ptr->num);
777 kfree(ptr);
778 codec->data = NULL;
779
780 zr36050_codecs--;
781 return 0;
782 }
783
784 return -EFAULT;
785}
786
787/* =========================================================================
788 Setup and registry function:
789
790 Initializes Zoran's JPEG processor
791
792 Also sets pixel size, average code size, mode (compr./decompr.)
793 (the given size is determined by the processor with the video interface)
794 ========================================================================= */
795
796static int
797zr36050_setup (struct videocodec *codec)
798{
799 struct zr36050 *ptr;
800 int res;
801
802 dprintk(2, "zr36050: initializing MJPEG subsystem #%d.\n",
803 zr36050_codecs);
804
805 if (zr36050_codecs == MAX_CODECS) {
806 dprintk(1,
807 KERN_ERR "zr36050: Can't attach more codecs!\n");
808 return -ENOSPC;
809 }
810 //mem structure init
811 codec->data = ptr = kzalloc(sizeof(struct zr36050), GFP_KERNEL);
812 if (NULL == ptr) {
813 dprintk(1, KERN_ERR "zr36050: Can't get enough memory!\n");
814 return -ENOMEM;
815 }
816
817 snprintf(ptr->name, sizeof(ptr->name), "zr36050[%d]",
818 zr36050_codecs);
819 ptr->num = zr36050_codecs++;
820 ptr->codec = codec;
821
822 //testing
823 res = zr36050_basic_test(ptr);
824 if (res < 0) {
825 zr36050_unset(codec);
826 return res;
827 }
828 //final setup
829 memcpy(ptr->h_samp_ratio, zr36050_decimation_h, 8);
830 memcpy(ptr->v_samp_ratio, zr36050_decimation_v, 8);
831
832 ptr->bitrate_ctrl = 0; /* 0 or 1 - fixed file size flag
833 * (what is the difference?) */
834 ptr->mode = CODEC_DO_COMPRESSION;
835 ptr->width = 384;
836 ptr->height = 288;
837 ptr->total_code_vol = 16000;
838 ptr->max_block_vol = 240;
839 ptr->scalefact = 0x100;
840 ptr->dri = 1;
841
842 /* no app/com marker by default */
843 ptr->app.appn = 0;
844 ptr->app.len = 0;
845 ptr->com.len = 0;
846
847 zr36050_init(ptr);
848
849 dprintk(1, KERN_INFO "%s: codec attached and running\n",
850 ptr->name);
851
852 return 0;
853}
854
855static const struct videocodec zr36050_codec = {
856 .owner = THIS_MODULE,
857 .name = "zr36050",
858 .magic = 0L, // magic not used
859 .flags =
860 CODEC_FLAG_JPEG | CODEC_FLAG_HARDWARE | CODEC_FLAG_ENCODER |
861 CODEC_FLAG_DECODER,
862 .type = CODEC_TYPE_ZR36050,
863 .setup = zr36050_setup, // functionality
864 .unset = zr36050_unset,
865 .set_mode = zr36050_set_mode,
866 .set_video = zr36050_set_video,
867 .control = zr36050_control,
868 // others are not used
869};
870
871/* =========================================================================
872 HOOK IN DRIVER AS KERNEL MODULE
873 ========================================================================= */
874
875static int __init
876zr36050_init_module (void)
877{
878 //dprintk(1, "ZR36050 driver %s\n",ZR050_VERSION);
879 zr36050_codecs = 0;
880 return videocodec_register(&zr36050_codec);
881}
882
883static void __exit
884zr36050_cleanup_module (void)
885{
886 if (zr36050_codecs) {
887 dprintk(1,
888 "zr36050: something's wrong - %d codecs left somehow.\n",
889 zr36050_codecs);
890 }
891 videocodec_unregister(&zr36050_codec);
892}
893
894module_init(zr36050_init_module);
895module_exit(zr36050_cleanup_module);
896
897MODULE_AUTHOR("Wolfgang Scherr <scherr@net4you.at>");
898MODULE_DESCRIPTION("Driver module for ZR36050 jpeg processors "
899 ZR050_VERSION);
900MODULE_LICENSE("GPL");
diff --git a/drivers/media/pci/zoran/zr36050.h b/drivers/media/pci/zoran/zr36050.h
new file mode 100644
index 000000000000..9f52f0cdde50
--- /dev/null
+++ b/drivers/media/pci/zoran/zr36050.h
@@ -0,0 +1,184 @@
1/*
2 * Zoran ZR36050 basic configuration functions - header file
3 *
4 * Copyright (C) 2001 Wolfgang Scherr <scherr@net4you.at>
5 *
6 * $Id: zr36050.h,v 1.1.2.2 2003/01/14 21:18:22 rbultje Exp $
7 *
8 * ------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * ------------------------------------------------------------------------
25 */
26
27#ifndef ZR36050_H
28#define ZR36050_H
29
30#include "videocodec.h"
31
32/* data stored for each zoran jpeg codec chip */
33struct zr36050 {
34 char name[32];
35 int num;
36 /* io datastructure */
37 struct videocodec *codec;
38 // last coder status
39 __u8 status1;
40 // actual coder setup
41 int mode;
42
43 __u16 width;
44 __u16 height;
45
46 __u16 bitrate_ctrl;
47
48 __u32 total_code_vol;
49 __u32 real_code_vol;
50 __u16 max_block_vol;
51
52 __u8 h_samp_ratio[8];
53 __u8 v_samp_ratio[8];
54 __u16 scalefact;
55 __u16 dri;
56
57 /* com/app marker */
58 struct jpeg_com_marker com;
59 struct jpeg_app_marker app;
60};
61
62/* zr36050 register addresses */
63#define ZR050_GO 0x000
64#define ZR050_HARDWARE 0x002
65#define ZR050_MODE 0x003
66#define ZR050_OPTIONS 0x004
67#define ZR050_MBCV 0x005
68#define ZR050_MARKERS_EN 0x006
69#define ZR050_INT_REQ_0 0x007
70#define ZR050_INT_REQ_1 0x008
71#define ZR050_TCV_NET_HI 0x009
72#define ZR050_TCV_NET_MH 0x00a
73#define ZR050_TCV_NET_ML 0x00b
74#define ZR050_TCV_NET_LO 0x00c
75#define ZR050_TCV_DATA_HI 0x00d
76#define ZR050_TCV_DATA_MH 0x00e
77#define ZR050_TCV_DATA_ML 0x00f
78#define ZR050_TCV_DATA_LO 0x010
79#define ZR050_SF_HI 0x011
80#define ZR050_SF_LO 0x012
81#define ZR050_AF_HI 0x013
82#define ZR050_AF_M 0x014
83#define ZR050_AF_LO 0x015
84#define ZR050_ACV_HI 0x016
85#define ZR050_ACV_MH 0x017
86#define ZR050_ACV_ML 0x018
87#define ZR050_ACV_LO 0x019
88#define ZR050_ACT_HI 0x01a
89#define ZR050_ACT_MH 0x01b
90#define ZR050_ACT_ML 0x01c
91#define ZR050_ACT_LO 0x01d
92#define ZR050_ACV_TRUN_HI 0x01e
93#define ZR050_ACV_TRUN_MH 0x01f
94#define ZR050_ACV_TRUN_ML 0x020
95#define ZR050_ACV_TRUN_LO 0x021
96#define ZR050_STATUS_0 0x02e
97#define ZR050_STATUS_1 0x02f
98
99#define ZR050_SOF_IDX 0x040
100#define ZR050_SOS1_IDX 0x07a
101#define ZR050_SOS2_IDX 0x08a
102#define ZR050_SOS3_IDX 0x09a
103#define ZR050_SOS4_IDX 0x0aa
104#define ZR050_DRI_IDX 0x0c0
105#define ZR050_DNL_IDX 0x0c6
106#define ZR050_DQT_IDX 0x0cc
107#define ZR050_DHT_IDX 0x1d4
108#define ZR050_APP_IDX 0x380
109#define ZR050_COM_IDX 0x3c0
110
111/* zr36050 hardware register bits */
112
113#define ZR050_HW_BSWD 0x80
114#define ZR050_HW_MSTR 0x40
115#define ZR050_HW_DMA 0x20
116#define ZR050_HW_CFIS_1_CLK 0x00
117#define ZR050_HW_CFIS_2_CLK 0x04
118#define ZR050_HW_CFIS_3_CLK 0x08
119#define ZR050_HW_CFIS_4_CLK 0x0C
120#define ZR050_HW_CFIS_5_CLK 0x10
121#define ZR050_HW_CFIS_6_CLK 0x14
122#define ZR050_HW_CFIS_7_CLK 0x18
123#define ZR050_HW_CFIS_8_CLK 0x1C
124#define ZR050_HW_BELE 0x01
125
126/* zr36050 mode register bits */
127
128#define ZR050_MO_COMP 0x80
129#define ZR050_MO_COMP 0x80
130#define ZR050_MO_ATP 0x40
131#define ZR050_MO_PASS2 0x20
132#define ZR050_MO_TLM 0x10
133#define ZR050_MO_DCONLY 0x08
134#define ZR050_MO_BRC 0x04
135
136#define ZR050_MO_ATP 0x40
137#define ZR050_MO_PASS2 0x20
138#define ZR050_MO_TLM 0x10
139#define ZR050_MO_DCONLY 0x08
140
141/* zr36050 option register bits */
142
143#define ZR050_OP_NSCN_1 0x00
144#define ZR050_OP_NSCN_2 0x20
145#define ZR050_OP_NSCN_3 0x40
146#define ZR050_OP_NSCN_4 0x60
147#define ZR050_OP_NSCN_5 0x80
148#define ZR050_OP_NSCN_6 0xA0
149#define ZR050_OP_NSCN_7 0xC0
150#define ZR050_OP_NSCN_8 0xE0
151#define ZR050_OP_OVF 0x10
152
153
154/* zr36050 markers-enable register bits */
155
156#define ZR050_ME_APP 0x80
157#define ZR050_ME_COM 0x40
158#define ZR050_ME_DRI 0x20
159#define ZR050_ME_DQT 0x10
160#define ZR050_ME_DHT 0x08
161#define ZR050_ME_DNL 0x04
162#define ZR050_ME_DQTI 0x02
163#define ZR050_ME_DHTI 0x01
164
165/* zr36050 status0/1 register bit masks */
166
167#define ZR050_ST_RST_MASK 0x20
168#define ZR050_ST_SOF_MASK 0x02
169#define ZR050_ST_SOS_MASK 0x02
170#define ZR050_ST_DATRDY_MASK 0x80
171#define ZR050_ST_MRKDET_MASK 0x40
172#define ZR050_ST_RFM_MASK 0x10
173#define ZR050_ST_RFD_MASK 0x08
174#define ZR050_ST_END_MASK 0x04
175#define ZR050_ST_TCVOVF_MASK 0x02
176#define ZR050_ST_DATOVF_MASK 0x01
177
178/* pixel component idx */
179
180#define ZR050_Y_COMPONENT 0
181#define ZR050_U_COMPONENT 1
182#define ZR050_V_COMPONENT 2
183
184#endif /*fndef ZR36050_H */
diff --git a/drivers/media/pci/zoran/zr36057.h b/drivers/media/pci/zoran/zr36057.h
new file mode 100644
index 000000000000..54c9362aa980
--- /dev/null
+++ b/drivers/media/pci/zoran/zr36057.h
@@ -0,0 +1,168 @@
1/*
2 * zr36057.h - zr36057 register offsets
3 *
4 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#ifndef _ZR36057_H_
22#define _ZR36057_H_
23
24
25/* Zoran ZR36057 registers */
26
27#define ZR36057_VFEHCR 0x000 /* Video Front End, Horizontal Configuration Register */
28#define ZR36057_VFEHCR_HSPol (1<<30)
29#define ZR36057_VFEHCR_HStart 10
30#define ZR36057_VFEHCR_HEnd 0
31#define ZR36057_VFEHCR_Hmask 0x3ff
32
33#define ZR36057_VFEVCR 0x004 /* Video Front End, Vertical Configuration Register */
34#define ZR36057_VFEVCR_VSPol (1<<30)
35#define ZR36057_VFEVCR_VStart 10
36#define ZR36057_VFEVCR_VEnd 0
37#define ZR36057_VFEVCR_Vmask 0x3ff
38
39#define ZR36057_VFESPFR 0x008 /* Video Front End, Scaler and Pixel Format Register */
40#define ZR36057_VFESPFR_ExtFl (1<<26)
41#define ZR36057_VFESPFR_TopField (1<<25)
42#define ZR36057_VFESPFR_VCLKPol (1<<24)
43#define ZR36057_VFESPFR_HFilter 21
44#define ZR36057_VFESPFR_HorDcm 14
45#define ZR36057_VFESPFR_VerDcm 8
46#define ZR36057_VFESPFR_DispMode 6
47#define ZR36057_VFESPFR_YUV422 (0<<3)
48#define ZR36057_VFESPFR_RGB888 (1<<3)
49#define ZR36057_VFESPFR_RGB565 (2<<3)
50#define ZR36057_VFESPFR_RGB555 (3<<3)
51#define ZR36057_VFESPFR_ErrDif (1<<2)
52#define ZR36057_VFESPFR_Pack24 (1<<1)
53#define ZR36057_VFESPFR_LittleEndian (1<<0)
54
55#define ZR36057_VDTR 0x00c /* Video Display "Top" Register */
56
57#define ZR36057_VDBR 0x010 /* Video Display "Bottom" Register */
58
59#define ZR36057_VSSFGR 0x014 /* Video Stride, Status, and Frame Grab Register */
60#define ZR36057_VSSFGR_DispStride 16
61#define ZR36057_VSSFGR_VidOvf (1<<8)
62#define ZR36057_VSSFGR_SnapShot (1<<1)
63#define ZR36057_VSSFGR_FrameGrab (1<<0)
64
65#define ZR36057_VDCR 0x018 /* Video Display Configuration Register */
66#define ZR36057_VDCR_VidEn (1<<31)
67#define ZR36057_VDCR_MinPix 24
68#define ZR36057_VDCR_Triton (1<<24)
69#define ZR36057_VDCR_VidWinHt 12
70#define ZR36057_VDCR_VidWinWid 0
71
72#define ZR36057_MMTR 0x01c /* Masking Map "Top" Register */
73
74#define ZR36057_MMBR 0x020 /* Masking Map "Bottom" Register */
75
76#define ZR36057_OCR 0x024 /* Overlay Control Register */
77#define ZR36057_OCR_OvlEnable (1 << 15)
78#define ZR36057_OCR_MaskStride 0
79
80#define ZR36057_SPGPPCR 0x028 /* System, PCI, and General Purpose Pins Control Register */
81#define ZR36057_SPGPPCR_SoftReset (1<<24)
82
83#define ZR36057_GPPGCR1 0x02c /* General Purpose Pins and GuestBus Control Register (1) */
84
85#define ZR36057_MCSAR 0x030 /* MPEG Code Source Address Register */
86
87#define ZR36057_MCTCR 0x034 /* MPEG Code Transfer Control Register */
88#define ZR36057_MCTCR_CodTime (1 << 30)
89#define ZR36057_MCTCR_CEmpty (1 << 29)
90#define ZR36057_MCTCR_CFlush (1 << 28)
91#define ZR36057_MCTCR_CodGuestID 20
92#define ZR36057_MCTCR_CodGuestReg 16
93
94#define ZR36057_MCMPR 0x038 /* MPEG Code Memory Pointer Register */
95
96#define ZR36057_ISR 0x03c /* Interrupt Status Register */
97#define ZR36057_ISR_GIRQ1 (1<<30)
98#define ZR36057_ISR_GIRQ0 (1<<29)
99#define ZR36057_ISR_CodRepIRQ (1<<28)
100#define ZR36057_ISR_JPEGRepIRQ (1<<27)
101
102#define ZR36057_ICR 0x040 /* Interrupt Control Register */
103#define ZR36057_ICR_GIRQ1 (1<<30)
104#define ZR36057_ICR_GIRQ0 (1<<29)
105#define ZR36057_ICR_CodRepIRQ (1<<28)
106#define ZR36057_ICR_JPEGRepIRQ (1<<27)
107#define ZR36057_ICR_IntPinEn (1<<24)
108
109#define ZR36057_I2CBR 0x044 /* I2C Bus Register */
110#define ZR36057_I2CBR_SDA (1<<1)
111#define ZR36057_I2CBR_SCL (1<<0)
112
113#define ZR36057_JMC 0x100 /* JPEG Mode and Control */
114#define ZR36057_JMC_JPG (1 << 31)
115#define ZR36057_JMC_JPGExpMode (0 << 29)
116#define ZR36057_JMC_JPGCmpMode (1 << 29)
117#define ZR36057_JMC_MJPGExpMode (2 << 29)
118#define ZR36057_JMC_MJPGCmpMode (3 << 29)
119#define ZR36057_JMC_RTBUSY_FB (1 << 6)
120#define ZR36057_JMC_Go_en (1 << 5)
121#define ZR36057_JMC_SyncMstr (1 << 4)
122#define ZR36057_JMC_Fld_per_buff (1 << 3)
123#define ZR36057_JMC_VFIFO_FB (1 << 2)
124#define ZR36057_JMC_CFIFO_FB (1 << 1)
125#define ZR36057_JMC_Stll_LitEndian (1 << 0)
126
127#define ZR36057_JPC 0x104 /* JPEG Process Control */
128#define ZR36057_JPC_P_Reset (1 << 7)
129#define ZR36057_JPC_CodTrnsEn (1 << 5)
130#define ZR36057_JPC_Active (1 << 0)
131
132#define ZR36057_VSP 0x108 /* Vertical Sync Parameters */
133#define ZR36057_VSP_VsyncSize 16
134#define ZR36057_VSP_FrmTot 0
135
136#define ZR36057_HSP 0x10c /* Horizontal Sync Parameters */
137#define ZR36057_HSP_HsyncStart 16
138#define ZR36057_HSP_LineTot 0
139
140#define ZR36057_FHAP 0x110 /* Field Horizontal Active Portion */
141#define ZR36057_FHAP_NAX 16
142#define ZR36057_FHAP_PAX 0
143
144#define ZR36057_FVAP 0x114 /* Field Vertical Active Portion */
145#define ZR36057_FVAP_NAY 16
146#define ZR36057_FVAP_PAY 0
147
148#define ZR36057_FPP 0x118 /* Field Process Parameters */
149#define ZR36057_FPP_Odd_Even (1 << 0)
150
151#define ZR36057_JCBA 0x11c /* JPEG Code Base Address */
152
153#define ZR36057_JCFT 0x120 /* JPEG Code FIFO Threshold */
154
155#define ZR36057_JCGI 0x124 /* JPEG Codec Guest ID */
156#define ZR36057_JCGI_JPEGuestID 4
157#define ZR36057_JCGI_JPEGuestReg 0
158
159#define ZR36057_GCR2 0x12c /* GuestBus Control Register (2) */
160
161#define ZR36057_POR 0x200 /* Post Office Register */
162#define ZR36057_POR_POPen (1<<25)
163#define ZR36057_POR_POTime (1<<24)
164#define ZR36057_POR_PODir (1<<23)
165
166#define ZR36057_STR 0x300 /* "Still" Transfer Register */
167
168#endif
diff --git a/drivers/media/pci/zoran/zr36060.c b/drivers/media/pci/zoran/zr36060.c
new file mode 100644
index 000000000000..f08546fe2234
--- /dev/null
+++ b/drivers/media/pci/zoran/zr36060.c
@@ -0,0 +1,1010 @@
1/*
2 * Zoran ZR36060 basic configuration functions
3 *
4 * Copyright (C) 2002 Laurent Pinchart <laurent.pinchart@skynet.be>
5 *
6 * $Id: zr36060.c,v 1.1.2.22 2003/05/06 09:35:36 rbultje Exp $
7 *
8 * ------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * ------------------------------------------------------------------------
25 */
26
27#define ZR060_VERSION "v0.7"
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/delay.h>
33
34#include <linux/types.h>
35#include <linux/wait.h>
36
37/* I/O commands, error codes */
38#include <asm/io.h>
39
40/* headerfile of this module */
41#include "zr36060.h"
42
43/* codec io API */
44#include "videocodec.h"
45
46/* it doesn't make sense to have more than 20 or so,
47 just to prevent some unwanted loops */
48#define MAX_CODECS 20
49
50/* amount of chips attached via this driver */
51static int zr36060_codecs;
52
53static bool low_bitrate;
54module_param(low_bitrate, bool, 0);
55MODULE_PARM_DESC(low_bitrate, "Buz compatibility option, halves bitrate");
56
57/* debugging is available via module parameter */
58static int debug;
59module_param(debug, int, 0);
60MODULE_PARM_DESC(debug, "Debug level (0-4)");
61
62#define dprintk(num, format, args...) \
63 do { \
64 if (debug >= num) \
65 printk(format, ##args); \
66 } while (0)
67
68/* =========================================================================
69 Local hardware I/O functions:
70
71 read/write via codec layer (registers are located in the master device)
72 ========================================================================= */
73
74/* read and write functions */
75static u8
76zr36060_read (struct zr36060 *ptr,
77 u16 reg)
78{
79 u8 value = 0;
80
81 // just in case something is wrong...
82 if (ptr->codec->master_data->readreg)
83 value = (ptr->codec->master_data->readreg(ptr->codec,
84 reg)) & 0xff;
85 else
86 dprintk(1,
87 KERN_ERR "%s: invalid I/O setup, nothing read!\n",
88 ptr->name);
89
90 //dprintk(4, "%s: reading from 0x%04x: %02x\n",ptr->name,reg,value);
91
92 return value;
93}
94
95static void
96zr36060_write(struct zr36060 *ptr,
97 u16 reg,
98 u8 value)
99{
100 //dprintk(4, "%s: writing 0x%02x to 0x%04x\n",ptr->name,value,reg);
101 dprintk(4, "0x%02x @0x%04x\n", value, reg);
102
103 // just in case something is wrong...
104 if (ptr->codec->master_data->writereg)
105 ptr->codec->master_data->writereg(ptr->codec, reg, value);
106 else
107 dprintk(1,
108 KERN_ERR
109 "%s: invalid I/O setup, nothing written!\n",
110 ptr->name);
111}
112
113/* =========================================================================
114 Local helper function:
115
116 status read
117 ========================================================================= */
118
119/* status is kept in datastructure */
120static u8
121zr36060_read_status (struct zr36060 *ptr)
122{
123 ptr->status = zr36060_read(ptr, ZR060_CFSR);
124
125 zr36060_read(ptr, 0);
126 return ptr->status;
127}
128
129/* =========================================================================
130 Local helper function:
131
132 scale factor read
133 ========================================================================= */
134
135/* scale factor is kept in datastructure */
136static u16
137zr36060_read_scalefactor (struct zr36060 *ptr)
138{
139 ptr->scalefact = (zr36060_read(ptr, ZR060_SF_HI) << 8) |
140 (zr36060_read(ptr, ZR060_SF_LO) & 0xFF);
141
142 /* leave 0 selected for an eventually GO from master */
143 zr36060_read(ptr, 0);
144 return ptr->scalefact;
145}
146
147/* =========================================================================
148 Local helper function:
149
150 wait if codec is ready to proceed (end of processing) or time is over
151 ========================================================================= */
152
153static void
154zr36060_wait_end (struct zr36060 *ptr)
155{
156 int i = 0;
157
158 while (zr36060_read_status(ptr) & ZR060_CFSR_Busy) {
159 udelay(1);
160 if (i++ > 200000) { // 200ms, there is for sure something wrong!!!
161 dprintk(1,
162 "%s: timeout at wait_end (last status: 0x%02x)\n",
163 ptr->name, ptr->status);
164 break;
165 }
166 }
167}
168
169/* =========================================================================
170 Local helper function:
171
172 basic test of "connectivity", writes/reads to/from memory the SOF marker
173 ========================================================================= */
174
175static int
176zr36060_basic_test (struct zr36060 *ptr)
177{
178 if ((zr36060_read(ptr, ZR060_IDR_DEV) != 0x33) &&
179 (zr36060_read(ptr, ZR060_IDR_REV) != 0x01)) {
180 dprintk(1,
181 KERN_ERR
182 "%s: attach failed, can't connect to jpeg processor!\n",
183 ptr->name);
184 return -ENXIO;
185 }
186
187 zr36060_wait_end(ptr);
188 if (ptr->status & ZR060_CFSR_Busy) {
189 dprintk(1,
190 KERN_ERR
191 "%s: attach failed, jpeg processor failed (end flag)!\n",
192 ptr->name);
193 return -EBUSY;
194 }
195
196 return 0; /* looks good! */
197}
198
199/* =========================================================================
200 Local helper function:
201
202 simple loop for pushing the init datasets
203 ========================================================================= */
204
205static int
206zr36060_pushit (struct zr36060 *ptr,
207 u16 startreg,
208 u16 len,
209 const char *data)
210{
211 int i = 0;
212
213 dprintk(4, "%s: write data block to 0x%04x (len=%d)\n", ptr->name,
214 startreg, len);
215 while (i < len) {
216 zr36060_write(ptr, startreg++, data[i++]);
217 }
218
219 return i;
220}
221
222/* =========================================================================
223 Basic datasets:
224
225 jpeg baseline setup data (you find it on lots places in internet, or just
226 extract it from any regular .jpg image...)
227
228 Could be variable, but until it's not needed it they are just fixed to save
229 memory. Otherwise expand zr36060 structure with arrays, push the values to
230 it and initialize from there, as e.g. the linux zr36057/60 driver does it.
231 ========================================================================= */
232
233static const char zr36060_dqt[0x86] = {
234 0xff, 0xdb, //Marker: DQT
235 0x00, 0x84, //Length: 2*65+2
236 0x00, //Pq,Tq first table
237 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
238 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
239 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
240 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
241 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
242 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
243 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
244 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
245 0x01, //Pq,Tq second table
246 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
247 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
248 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
249 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
250 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
251 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
252 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
253 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
254};
255
256static const char zr36060_dht[0x1a4] = {
257 0xff, 0xc4, //Marker: DHT
258 0x01, 0xa2, //Length: 2*AC, 2*DC
259 0x00, //DC first table
260 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
261 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
262 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
263 0x01, //DC second table
264 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
265 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
266 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
267 0x10, //AC first table
268 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
269 0x05, 0x05, 0x04, 0x04, 0x00, 0x00,
270 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11,
271 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
272 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
273 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24,
274 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17,
275 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34,
276 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
277 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
278 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66,
279 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
280 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
281 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
282 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
283 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9,
284 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
285 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
286 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
287 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
288 0xF8, 0xF9, 0xFA,
289 0x11, //AC second table
290 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
291 0x07, 0x05, 0x04, 0x04, 0x00, 0x01,
292 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
293 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
294 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
295 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62,
296 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25,
297 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A,
298 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
299 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
300 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66,
301 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
302 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
303 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
304 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
305 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
306 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
307 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8,
308 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
309 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
310 0xF9, 0xFA
311};
312
313/* jpeg baseline setup, this is just fixed in this driver (YUV pictures) */
314#define NO_OF_COMPONENTS 0x3 //Y,U,V
315#define BASELINE_PRECISION 0x8 //MCU size (?)
316static const char zr36060_tq[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's QT
317static const char zr36060_td[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's DC
318static const char zr36060_ta[8] = { 0, 1, 1, 0, 0, 0, 0, 0 }; //table idx's AC
319
320/* horizontal 422 decimation setup (maybe we support 411 or so later, too) */
321static const char zr36060_decimation_h[8] = { 2, 1, 1, 0, 0, 0, 0, 0 };
322static const char zr36060_decimation_v[8] = { 1, 1, 1, 0, 0, 0, 0, 0 };
323
324/* =========================================================================
325 Local helper functions:
326
327 calculation and setup of parameter-dependent JPEG baseline segments
328 (needed for compression only)
329 ========================================================================= */
330
331/* ------------------------------------------------------------------------- */
332
333/* SOF (start of frame) segment depends on width, height and sampling ratio
334 of each color component */
335
336static int
337zr36060_set_sof (struct zr36060 *ptr)
338{
339 char sof_data[34]; // max. size of register set
340 int i;
341
342 dprintk(3, "%s: write SOF (%dx%d, %d components)\n", ptr->name,
343 ptr->width, ptr->height, NO_OF_COMPONENTS);
344 sof_data[0] = 0xff;
345 sof_data[1] = 0xc0;
346 sof_data[2] = 0x00;
347 sof_data[3] = (3 * NO_OF_COMPONENTS) + 8;
348 sof_data[4] = BASELINE_PRECISION; // only '8' possible with zr36060
349 sof_data[5] = (ptr->height) >> 8;
350 sof_data[6] = (ptr->height) & 0xff;
351 sof_data[7] = (ptr->width) >> 8;
352 sof_data[8] = (ptr->width) & 0xff;
353 sof_data[9] = NO_OF_COMPONENTS;
354 for (i = 0; i < NO_OF_COMPONENTS; i++) {
355 sof_data[10 + (i * 3)] = i; // index identifier
356 sof_data[11 + (i * 3)] = (ptr->h_samp_ratio[i] << 4) |
357 (ptr->v_samp_ratio[i]); // sampling ratios
358 sof_data[12 + (i * 3)] = zr36060_tq[i]; // Q table selection
359 }
360 return zr36060_pushit(ptr, ZR060_SOF_IDX,
361 (3 * NO_OF_COMPONENTS) + 10, sof_data);
362}
363
364/* ------------------------------------------------------------------------- */
365
366/* SOS (start of scan) segment depends on the used scan components
367 of each color component */
368
369static int
370zr36060_set_sos (struct zr36060 *ptr)
371{
372 char sos_data[16]; // max. size of register set
373 int i;
374
375 dprintk(3, "%s: write SOS\n", ptr->name);
376 sos_data[0] = 0xff;
377 sos_data[1] = 0xda;
378 sos_data[2] = 0x00;
379 sos_data[3] = 2 + 1 + (2 * NO_OF_COMPONENTS) + 3;
380 sos_data[4] = NO_OF_COMPONENTS;
381 for (i = 0; i < NO_OF_COMPONENTS; i++) {
382 sos_data[5 + (i * 2)] = i; // index
383 sos_data[6 + (i * 2)] = (zr36060_td[i] << 4) |
384 zr36060_ta[i]; // AC/DC tbl.sel.
385 }
386 sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 2] = 00; // scan start
387 sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 3] = 0x3f;
388 sos_data[2 + 1 + (2 * NO_OF_COMPONENTS) + 4] = 00;
389 return zr36060_pushit(ptr, ZR060_SOS_IDX,
390 4 + 1 + (2 * NO_OF_COMPONENTS) + 3,
391 sos_data);
392}
393
394/* ------------------------------------------------------------------------- */
395
396/* DRI (define restart interval) */
397
398static int
399zr36060_set_dri (struct zr36060 *ptr)
400{
401 char dri_data[6]; // max. size of register set
402
403 dprintk(3, "%s: write DRI\n", ptr->name);
404 dri_data[0] = 0xff;
405 dri_data[1] = 0xdd;
406 dri_data[2] = 0x00;
407 dri_data[3] = 0x04;
408 dri_data[4] = (ptr->dri) >> 8;
409 dri_data[5] = (ptr->dri) & 0xff;
410 return zr36060_pushit(ptr, ZR060_DRI_IDX, 6, dri_data);
411}
412
413/* =========================================================================
414 Setup function:
415
416 Setup compression/decompression of Zoran's JPEG processor
417 ( see also zoran 36060 manual )
418
419 ... sorry for the spaghetti code ...
420 ========================================================================= */
421static void
422zr36060_init (struct zr36060 *ptr)
423{
424 int sum = 0;
425 long bitcnt, tmp;
426
427 if (ptr->mode == CODEC_DO_COMPRESSION) {
428 dprintk(2, "%s: COMPRESSION SETUP\n", ptr->name);
429
430 zr36060_write(ptr, ZR060_LOAD, ZR060_LOAD_SyncRst);
431
432 /* 060 communicates with 067 in master mode */
433 zr36060_write(ptr, ZR060_CIR, ZR060_CIR_CodeMstr);
434
435 /* Compression with or without variable scale factor */
436 /*FIXME: What about ptr->bitrate_ctrl? */
437 zr36060_write(ptr, ZR060_CMR,
438 ZR060_CMR_Comp | ZR060_CMR_Pass2 |
439 ZR060_CMR_BRB);
440
441 /* Must be zero */
442 zr36060_write(ptr, ZR060_MBZ, 0x00);
443 zr36060_write(ptr, ZR060_TCR_HI, 0x00);
444 zr36060_write(ptr, ZR060_TCR_LO, 0x00);
445
446 /* Disable all IRQs - no DataErr means autoreset */
447 zr36060_write(ptr, ZR060_IMR, 0);
448
449 /* volume control settings */
450 zr36060_write(ptr, ZR060_SF_HI, ptr->scalefact >> 8);
451 zr36060_write(ptr, ZR060_SF_LO, ptr->scalefact & 0xff);
452
453 zr36060_write(ptr, ZR060_AF_HI, 0xff);
454 zr36060_write(ptr, ZR060_AF_M, 0xff);
455 zr36060_write(ptr, ZR060_AF_LO, 0xff);
456
457 /* setup the variable jpeg tables */
458 sum += zr36060_set_sof(ptr);
459 sum += zr36060_set_sos(ptr);
460 sum += zr36060_set_dri(ptr);
461
462 /* setup the fixed jpeg tables - maybe variable, though -
463 * (see table init section above) */
464 sum +=
465 zr36060_pushit(ptr, ZR060_DQT_IDX, sizeof(zr36060_dqt),
466 zr36060_dqt);
467 sum +=
468 zr36060_pushit(ptr, ZR060_DHT_IDX, sizeof(zr36060_dht),
469 zr36060_dht);
470 zr36060_write(ptr, ZR060_APP_IDX, 0xff);
471 zr36060_write(ptr, ZR060_APP_IDX + 1, 0xe0 + ptr->app.appn);
472 zr36060_write(ptr, ZR060_APP_IDX + 2, 0x00);
473 zr36060_write(ptr, ZR060_APP_IDX + 3, ptr->app.len + 2);
474 sum += zr36060_pushit(ptr, ZR060_APP_IDX + 4, 60,
475 ptr->app.data) + 4;
476 zr36060_write(ptr, ZR060_COM_IDX, 0xff);
477 zr36060_write(ptr, ZR060_COM_IDX + 1, 0xfe);
478 zr36060_write(ptr, ZR060_COM_IDX + 2, 0x00);
479 zr36060_write(ptr, ZR060_COM_IDX + 3, ptr->com.len + 2);
480 sum += zr36060_pushit(ptr, ZR060_COM_IDX + 4, 60,
481 ptr->com.data) + 4;
482
483 /* setup misc. data for compression (target code sizes) */
484
485 /* size of compressed code to reach without header data */
486 sum = ptr->real_code_vol - sum;
487 bitcnt = sum << 3; /* need the size in bits */
488
489 tmp = bitcnt >> 16;
490 dprintk(3,
491 "%s: code: csize=%d, tot=%d, bit=%ld, highbits=%ld\n",
492 ptr->name, sum, ptr->real_code_vol, bitcnt, tmp);
493 zr36060_write(ptr, ZR060_TCV_NET_HI, tmp >> 8);
494 zr36060_write(ptr, ZR060_TCV_NET_MH, tmp & 0xff);
495 tmp = bitcnt & 0xffff;
496 zr36060_write(ptr, ZR060_TCV_NET_ML, tmp >> 8);
497 zr36060_write(ptr, ZR060_TCV_NET_LO, tmp & 0xff);
498
499 bitcnt -= bitcnt >> 7; // bits without stuffing
500 bitcnt -= ((bitcnt * 5) >> 6); // bits without eob
501
502 tmp = bitcnt >> 16;
503 dprintk(3, "%s: code: nettobit=%ld, highnettobits=%ld\n",
504 ptr->name, bitcnt, tmp);
505 zr36060_write(ptr, ZR060_TCV_DATA_HI, tmp >> 8);
506 zr36060_write(ptr, ZR060_TCV_DATA_MH, tmp & 0xff);
507 tmp = bitcnt & 0xffff;
508 zr36060_write(ptr, ZR060_TCV_DATA_ML, tmp >> 8);
509 zr36060_write(ptr, ZR060_TCV_DATA_LO, tmp & 0xff);
510
511 /* JPEG markers to be included in the compressed stream */
512 zr36060_write(ptr, ZR060_MER,
513 ZR060_MER_DQT | ZR060_MER_DHT |
514 ((ptr->com.len > 0) ? ZR060_MER_Com : 0) |
515 ((ptr->app.len > 0) ? ZR060_MER_App : 0));
516
517 /* Setup the Video Frontend */
518 /* Limit pixel range to 16..235 as per CCIR-601 */
519 zr36060_write(ptr, ZR060_VCR, ZR060_VCR_Range);
520
521 } else {
522 dprintk(2, "%s: EXPANSION SETUP\n", ptr->name);
523
524 zr36060_write(ptr, ZR060_LOAD, ZR060_LOAD_SyncRst);
525
526 /* 060 communicates with 067 in master mode */
527 zr36060_write(ptr, ZR060_CIR, ZR060_CIR_CodeMstr);
528
529 /* Decompression */
530 zr36060_write(ptr, ZR060_CMR, 0);
531
532 /* Must be zero */
533 zr36060_write(ptr, ZR060_MBZ, 0x00);
534 zr36060_write(ptr, ZR060_TCR_HI, 0x00);
535 zr36060_write(ptr, ZR060_TCR_LO, 0x00);
536
537 /* Disable all IRQs - no DataErr means autoreset */
538 zr36060_write(ptr, ZR060_IMR, 0);
539
540 /* setup misc. data for expansion */
541 zr36060_write(ptr, ZR060_MER, 0);
542
543 /* setup the fixed jpeg tables - maybe variable, though -
544 * (see table init section above) */
545 zr36060_pushit(ptr, ZR060_DHT_IDX, sizeof(zr36060_dht),
546 zr36060_dht);
547
548 /* Setup the Video Frontend */
549 //zr36060_write(ptr, ZR060_VCR, ZR060_VCR_FIExt);
550 //this doesn't seem right and doesn't work...
551 zr36060_write(ptr, ZR060_VCR, ZR060_VCR_Range);
552 }
553
554 /* Load the tables */
555 zr36060_write(ptr, ZR060_LOAD,
556 ZR060_LOAD_SyncRst | ZR060_LOAD_Load);
557 zr36060_wait_end(ptr);
558 dprintk(2, "%s: Status after table preload: 0x%02x\n", ptr->name,
559 ptr->status);
560
561 if (ptr->status & ZR060_CFSR_Busy) {
562 dprintk(1, KERN_ERR "%s: init aborted!\n", ptr->name);
563 return; // something is wrong, its timed out!!!!
564 }
565}
566
567/* =========================================================================
568 CODEC API FUNCTIONS
569
570 this functions are accessed by the master via the API structure
571 ========================================================================= */
572
573/* set compression/expansion mode and launches codec -
574 this should be the last call from the master before starting processing */
575static int
576zr36060_set_mode (struct videocodec *codec,
577 int mode)
578{
579 struct zr36060 *ptr = (struct zr36060 *) codec->data;
580
581 dprintk(2, "%s: set_mode %d call\n", ptr->name, mode);
582
583 if ((mode != CODEC_DO_EXPANSION) && (mode != CODEC_DO_COMPRESSION))
584 return -EINVAL;
585
586 ptr->mode = mode;
587 zr36060_init(ptr);
588
589 return 0;
590}
591
592/* set picture size (norm is ignored as the codec doesn't know about it) */
593static int
594zr36060_set_video (struct videocodec *codec,
595 struct tvnorm *norm,
596 struct vfe_settings *cap,
597 struct vfe_polarity *pol)
598{
599 struct zr36060 *ptr = (struct zr36060 *) codec->data;
600 u32 reg;
601 int size;
602
603 dprintk(2, "%s: set_video %d/%d-%dx%d (%%%d) call\n", ptr->name,
604 cap->x, cap->y, cap->width, cap->height, cap->decimation);
605
606 /* if () return -EINVAL;
607 * trust the master driver that it knows what it does - so
608 * we allow invalid startx/y and norm for now ... */
609 ptr->width = cap->width / (cap->decimation & 0xff);
610 ptr->height = cap->height / (cap->decimation >> 8);
611
612 zr36060_write(ptr, ZR060_LOAD, ZR060_LOAD_SyncRst);
613
614 /* Note that VSPol/HSPol bits in zr36060 have the opposite
615 * meaning of their zr360x7 counterparts with the same names
616 * N.b. for VSPol this is only true if FIVEdge = 0 (default,
617 * left unchanged here - in accordance with datasheet).
618 */
619 reg = (!pol->vsync_pol ? ZR060_VPR_VSPol : 0)
620 | (!pol->hsync_pol ? ZR060_VPR_HSPol : 0)
621 | (pol->field_pol ? ZR060_VPR_FIPol : 0)
622 | (pol->blank_pol ? ZR060_VPR_BLPol : 0)
623 | (pol->subimg_pol ? ZR060_VPR_SImgPol : 0)
624 | (pol->poe_pol ? ZR060_VPR_PoePol : 0)
625 | (pol->pvalid_pol ? ZR060_VPR_PValPol : 0)
626 | (pol->vclk_pol ? ZR060_VPR_VCLKPol : 0);
627 zr36060_write(ptr, ZR060_VPR, reg);
628
629 reg = 0;
630 switch (cap->decimation & 0xff) {
631 default:
632 case 1:
633 break;
634
635 case 2:
636 reg |= ZR060_SR_HScale2;
637 break;
638
639 case 4:
640 reg |= ZR060_SR_HScale4;
641 break;
642 }
643
644 switch (cap->decimation >> 8) {
645 default:
646 case 1:
647 break;
648
649 case 2:
650 reg |= ZR060_SR_VScale;
651 break;
652 }
653 zr36060_write(ptr, ZR060_SR, reg);
654
655 zr36060_write(ptr, ZR060_BCR_Y, 0x00);
656 zr36060_write(ptr, ZR060_BCR_U, 0x80);
657 zr36060_write(ptr, ZR060_BCR_V, 0x80);
658
659 /* sync generator */
660
661 reg = norm->Ht - 1; /* Vtotal */
662 zr36060_write(ptr, ZR060_SGR_VTOTAL_HI, (reg >> 8) & 0xff);
663 zr36060_write(ptr, ZR060_SGR_VTOTAL_LO, (reg >> 0) & 0xff);
664
665 reg = norm->Wt - 1; /* Htotal */
666 zr36060_write(ptr, ZR060_SGR_HTOTAL_HI, (reg >> 8) & 0xff);
667 zr36060_write(ptr, ZR060_SGR_HTOTAL_LO, (reg >> 0) & 0xff);
668
669 reg = 6 - 1; /* VsyncSize */
670 zr36060_write(ptr, ZR060_SGR_VSYNC, reg);
671
672 //reg = 30 - 1; /* HsyncSize */
673///*CP*/ reg = (zr->params.norm == 1 ? 57 : 68);
674 reg = 68;
675 zr36060_write(ptr, ZR060_SGR_HSYNC, reg);
676
677 reg = norm->VStart - 1; /* BVstart */
678 zr36060_write(ptr, ZR060_SGR_BVSTART, reg);
679
680 reg += norm->Ha / 2; /* BVend */
681 zr36060_write(ptr, ZR060_SGR_BVEND_HI, (reg >> 8) & 0xff);
682 zr36060_write(ptr, ZR060_SGR_BVEND_LO, (reg >> 0) & 0xff);
683
684 reg = norm->HStart - 1; /* BHstart */
685 zr36060_write(ptr, ZR060_SGR_BHSTART, reg);
686
687 reg += norm->Wa; /* BHend */
688 zr36060_write(ptr, ZR060_SGR_BHEND_HI, (reg >> 8) & 0xff);
689 zr36060_write(ptr, ZR060_SGR_BHEND_LO, (reg >> 0) & 0xff);
690
691 /* active area */
692 reg = cap->y + norm->VStart; /* Vstart */
693 zr36060_write(ptr, ZR060_AAR_VSTART_HI, (reg >> 8) & 0xff);
694 zr36060_write(ptr, ZR060_AAR_VSTART_LO, (reg >> 0) & 0xff);
695
696 reg += cap->height; /* Vend */
697 zr36060_write(ptr, ZR060_AAR_VEND_HI, (reg >> 8) & 0xff);
698 zr36060_write(ptr, ZR060_AAR_VEND_LO, (reg >> 0) & 0xff);
699
700 reg = cap->x + norm->HStart; /* Hstart */
701 zr36060_write(ptr, ZR060_AAR_HSTART_HI, (reg >> 8) & 0xff);
702 zr36060_write(ptr, ZR060_AAR_HSTART_LO, (reg >> 0) & 0xff);
703
704 reg += cap->width; /* Hend */
705 zr36060_write(ptr, ZR060_AAR_HEND_HI, (reg >> 8) & 0xff);
706 zr36060_write(ptr, ZR060_AAR_HEND_LO, (reg >> 0) & 0xff);
707
708 /* subimage area */
709 reg = norm->VStart - 4; /* SVstart */
710 zr36060_write(ptr, ZR060_SWR_VSTART_HI, (reg >> 8) & 0xff);
711 zr36060_write(ptr, ZR060_SWR_VSTART_LO, (reg >> 0) & 0xff);
712
713 reg += norm->Ha / 2 + 8; /* SVend */
714 zr36060_write(ptr, ZR060_SWR_VEND_HI, (reg >> 8) & 0xff);
715 zr36060_write(ptr, ZR060_SWR_VEND_LO, (reg >> 0) & 0xff);
716
717 reg = norm->HStart /*+ 64 */ - 4; /* SHstart */
718 zr36060_write(ptr, ZR060_SWR_HSTART_HI, (reg >> 8) & 0xff);
719 zr36060_write(ptr, ZR060_SWR_HSTART_LO, (reg >> 0) & 0xff);
720
721 reg += norm->Wa + 8; /* SHend */
722 zr36060_write(ptr, ZR060_SWR_HEND_HI, (reg >> 8) & 0xff);
723 zr36060_write(ptr, ZR060_SWR_HEND_LO, (reg >> 0) & 0xff);
724
725 size = ptr->width * ptr->height;
726 /* Target compressed field size in bits: */
727 size = size * 16; /* uncompressed size in bits */
728 /* (Ronald) by default, quality = 100 is a compression
729 * ratio 1:2. Setting low_bitrate (insmod option) sets
730 * it to 1:4 (instead of 1:2, zr36060 max) as limit because the
731 * buz can't handle more at decimation=1... Use low_bitrate if
732 * you have a Buz, unless you know what you're doing */
733 size = size * cap->quality / (low_bitrate ? 400 : 200);
734 /* Lower limit (arbitrary, 1 KB) */
735 if (size < 8192)
736 size = 8192;
737 /* Upper limit: 7/8 of the code buffers */
738 if (size > ptr->total_code_vol * 7)
739 size = ptr->total_code_vol * 7;
740
741 ptr->real_code_vol = size >> 3; /* in bytes */
742
743 /* the MBCVR is the *maximum* block volume, according to the
744 * JPEG ISO specs, this shouldn't be used, since that allows
745 * for the best encoding quality. So set it to it's max value */
746 reg = ptr->max_block_vol;
747 zr36060_write(ptr, ZR060_MBCVR, reg);
748
749 return 0;
750}
751
752/* additional control functions */
753static int
754zr36060_control (struct videocodec *codec,
755 int type,
756 int size,
757 void *data)
758{
759 struct zr36060 *ptr = (struct zr36060 *) codec->data;
760 int *ival = (int *) data;
761
762 dprintk(2, "%s: control %d call with %d byte\n", ptr->name, type,
763 size);
764
765 switch (type) {
766 case CODEC_G_STATUS: /* get last status */
767 if (size != sizeof(int))
768 return -EFAULT;
769 zr36060_read_status(ptr);
770 *ival = ptr->status;
771 break;
772
773 case CODEC_G_CODEC_MODE:
774 if (size != sizeof(int))
775 return -EFAULT;
776 *ival = CODEC_MODE_BJPG;
777 break;
778
779 case CODEC_S_CODEC_MODE:
780 if (size != sizeof(int))
781 return -EFAULT;
782 if (*ival != CODEC_MODE_BJPG)
783 return -EINVAL;
784 /* not needed, do nothing */
785 return 0;
786
787 case CODEC_G_VFE:
788 case CODEC_S_VFE:
789 /* not needed, do nothing */
790 return 0;
791
792 case CODEC_S_MMAP:
793 /* not available, give an error */
794 return -ENXIO;
795
796 case CODEC_G_JPEG_TDS_BYTE: /* get target volume in byte */
797 if (size != sizeof(int))
798 return -EFAULT;
799 *ival = ptr->total_code_vol;
800 break;
801
802 case CODEC_S_JPEG_TDS_BYTE: /* get target volume in byte */
803 if (size != sizeof(int))
804 return -EFAULT;
805 ptr->total_code_vol = *ival;
806 ptr->real_code_vol = (ptr->total_code_vol * 6) >> 3;
807 break;
808
809 case CODEC_G_JPEG_SCALE: /* get scaling factor */
810 if (size != sizeof(int))
811 return -EFAULT;
812 *ival = zr36060_read_scalefactor(ptr);
813 break;
814
815 case CODEC_S_JPEG_SCALE: /* set scaling factor */
816 if (size != sizeof(int))
817 return -EFAULT;
818 ptr->scalefact = *ival;
819 break;
820
821 case CODEC_G_JPEG_APP_DATA: { /* get appn marker data */
822 struct jpeg_app_marker *app = data;
823
824 if (size != sizeof(struct jpeg_app_marker))
825 return -EFAULT;
826
827 *app = ptr->app;
828 break;
829 }
830
831 case CODEC_S_JPEG_APP_DATA: { /* set appn marker data */
832 struct jpeg_app_marker *app = data;
833
834 if (size != sizeof(struct jpeg_app_marker))
835 return -EFAULT;
836
837 ptr->app = *app;
838 break;
839 }
840
841 case CODEC_G_JPEG_COM_DATA: { /* get comment marker data */
842 struct jpeg_com_marker *com = data;
843
844 if (size != sizeof(struct jpeg_com_marker))
845 return -EFAULT;
846
847 *com = ptr->com;
848 break;
849 }
850
851 case CODEC_S_JPEG_COM_DATA: { /* set comment marker data */
852 struct jpeg_com_marker *com = data;
853
854 if (size != sizeof(struct jpeg_com_marker))
855 return -EFAULT;
856
857 ptr->com = *com;
858 break;
859 }
860
861 default:
862 return -EINVAL;
863 }
864
865 return size;
866}
867
868/* =========================================================================
869 Exit and unregister function:
870
871 Deinitializes Zoran's JPEG processor
872 ========================================================================= */
873
874static int
875zr36060_unset (struct videocodec *codec)
876{
877 struct zr36060 *ptr = codec->data;
878
879 if (ptr) {
880 /* do wee need some codec deinit here, too ???? */
881
882 dprintk(1, "%s: finished codec #%d\n", ptr->name,
883 ptr->num);
884 kfree(ptr);
885 codec->data = NULL;
886
887 zr36060_codecs--;
888 return 0;
889 }
890
891 return -EFAULT;
892}
893
894/* =========================================================================
895 Setup and registry function:
896
897 Initializes Zoran's JPEG processor
898
899 Also sets pixel size, average code size, mode (compr./decompr.)
900 (the given size is determined by the processor with the video interface)
901 ========================================================================= */
902
903static int
904zr36060_setup (struct videocodec *codec)
905{
906 struct zr36060 *ptr;
907 int res;
908
909 dprintk(2, "zr36060: initializing MJPEG subsystem #%d.\n",
910 zr36060_codecs);
911
912 if (zr36060_codecs == MAX_CODECS) {
913 dprintk(1,
914 KERN_ERR "zr36060: Can't attach more codecs!\n");
915 return -ENOSPC;
916 }
917 //mem structure init
918 codec->data = ptr = kzalloc(sizeof(struct zr36060), GFP_KERNEL);
919 if (NULL == ptr) {
920 dprintk(1, KERN_ERR "zr36060: Can't get enough memory!\n");
921 return -ENOMEM;
922 }
923
924 snprintf(ptr->name, sizeof(ptr->name), "zr36060[%d]",
925 zr36060_codecs);
926 ptr->num = zr36060_codecs++;
927 ptr->codec = codec;
928
929 //testing
930 res = zr36060_basic_test(ptr);
931 if (res < 0) {
932 zr36060_unset(codec);
933 return res;
934 }
935 //final setup
936 memcpy(ptr->h_samp_ratio, zr36060_decimation_h, 8);
937 memcpy(ptr->v_samp_ratio, zr36060_decimation_v, 8);
938
939 ptr->bitrate_ctrl = 0; /* 0 or 1 - fixed file size flag
940 * (what is the difference?) */
941 ptr->mode = CODEC_DO_COMPRESSION;
942 ptr->width = 384;
943 ptr->height = 288;
944 ptr->total_code_vol = 16000; /* CHECKME */
945 ptr->real_code_vol = (ptr->total_code_vol * 6) >> 3;
946 ptr->max_block_vol = 240; /* CHECKME, was 120 is 240 */
947 ptr->scalefact = 0x100;
948 ptr->dri = 1; /* CHECKME, was 8 is 1 */
949
950 /* by default, no COM or APP markers - app should set those */
951 ptr->com.len = 0;
952 ptr->app.appn = 0;
953 ptr->app.len = 0;
954
955 zr36060_init(ptr);
956
957 dprintk(1, KERN_INFO "%s: codec attached and running\n",
958 ptr->name);
959
960 return 0;
961}
962
963static const struct videocodec zr36060_codec = {
964 .owner = THIS_MODULE,
965 .name = "zr36060",
966 .magic = 0L, // magic not used
967 .flags =
968 CODEC_FLAG_JPEG | CODEC_FLAG_HARDWARE | CODEC_FLAG_ENCODER |
969 CODEC_FLAG_DECODER | CODEC_FLAG_VFE,
970 .type = CODEC_TYPE_ZR36060,
971 .setup = zr36060_setup, // functionality
972 .unset = zr36060_unset,
973 .set_mode = zr36060_set_mode,
974 .set_video = zr36060_set_video,
975 .control = zr36060_control,
976 // others are not used
977};
978
979/* =========================================================================
980 HOOK IN DRIVER AS KERNEL MODULE
981 ========================================================================= */
982
983static int __init
984zr36060_init_module (void)
985{
986 //dprintk(1, "zr36060 driver %s\n",ZR060_VERSION);
987 zr36060_codecs = 0;
988 return videocodec_register(&zr36060_codec);
989}
990
991static void __exit
992zr36060_cleanup_module (void)
993{
994 if (zr36060_codecs) {
995 dprintk(1,
996 "zr36060: something's wrong - %d codecs left somehow.\n",
997 zr36060_codecs);
998 }
999
1000 /* however, we can't just stay alive */
1001 videocodec_unregister(&zr36060_codec);
1002}
1003
1004module_init(zr36060_init_module);
1005module_exit(zr36060_cleanup_module);
1006
1007MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@skynet.be>");
1008MODULE_DESCRIPTION("Driver module for ZR36060 jpeg processors "
1009 ZR060_VERSION);
1010MODULE_LICENSE("GPL");
diff --git a/drivers/media/pci/zoran/zr36060.h b/drivers/media/pci/zoran/zr36060.h
new file mode 100644
index 000000000000..914ffa4ad8d3
--- /dev/null
+++ b/drivers/media/pci/zoran/zr36060.h
@@ -0,0 +1,220 @@
1/*
2 * Zoran ZR36060 basic configuration functions - header file
3 *
4 * Copyright (C) 2002 Laurent Pinchart <laurent.pinchart@skynet.be>
5 *
6 * $Id: zr36060.h,v 1.1.1.1.2.3 2003/01/14 21:18:47 rbultje Exp $
7 *
8 * ------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * ------------------------------------------------------------------------
25 */
26
27#ifndef ZR36060_H
28#define ZR36060_H
29
30#include "videocodec.h"
31
32/* data stored for each zoran jpeg codec chip */
33struct zr36060 {
34 char name[32];
35 int num;
36 /* io datastructure */
37 struct videocodec *codec;
38 // last coder status
39 __u8 status;
40 // actual coder setup
41 int mode;
42
43 __u16 width;
44 __u16 height;
45
46 __u16 bitrate_ctrl;
47
48 __u32 total_code_vol;
49 __u32 real_code_vol;
50 __u16 max_block_vol;
51
52 __u8 h_samp_ratio[8];
53 __u8 v_samp_ratio[8];
54 __u16 scalefact;
55 __u16 dri;
56
57 /* app/com marker data */
58 struct jpeg_app_marker app;
59 struct jpeg_com_marker com;
60};
61
62/* ZR36060 register addresses */
63#define ZR060_LOAD 0x000
64#define ZR060_CFSR 0x001
65#define ZR060_CIR 0x002
66#define ZR060_CMR 0x003
67#define ZR060_MBZ 0x004
68#define ZR060_MBCVR 0x005
69#define ZR060_MER 0x006
70#define ZR060_IMR 0x007
71#define ZR060_ISR 0x008
72#define ZR060_TCV_NET_HI 0x009
73#define ZR060_TCV_NET_MH 0x00a
74#define ZR060_TCV_NET_ML 0x00b
75#define ZR060_TCV_NET_LO 0x00c
76#define ZR060_TCV_DATA_HI 0x00d
77#define ZR060_TCV_DATA_MH 0x00e
78#define ZR060_TCV_DATA_ML 0x00f
79#define ZR060_TCV_DATA_LO 0x010
80#define ZR060_SF_HI 0x011
81#define ZR060_SF_LO 0x012
82#define ZR060_AF_HI 0x013
83#define ZR060_AF_M 0x014
84#define ZR060_AF_LO 0x015
85#define ZR060_ACV_HI 0x016
86#define ZR060_ACV_MH 0x017
87#define ZR060_ACV_ML 0x018
88#define ZR060_ACV_LO 0x019
89#define ZR060_ACT_HI 0x01a
90#define ZR060_ACT_MH 0x01b
91#define ZR060_ACT_ML 0x01c
92#define ZR060_ACT_LO 0x01d
93#define ZR060_ACV_TRUN_HI 0x01e
94#define ZR060_ACV_TRUN_MH 0x01f
95#define ZR060_ACV_TRUN_ML 0x020
96#define ZR060_ACV_TRUN_LO 0x021
97#define ZR060_IDR_DEV 0x022
98#define ZR060_IDR_REV 0x023
99#define ZR060_TCR_HI 0x024
100#define ZR060_TCR_LO 0x025
101#define ZR060_VCR 0x030
102#define ZR060_VPR 0x031
103#define ZR060_SR 0x032
104#define ZR060_BCR_Y 0x033
105#define ZR060_BCR_U 0x034
106#define ZR060_BCR_V 0x035
107#define ZR060_SGR_VTOTAL_HI 0x036
108#define ZR060_SGR_VTOTAL_LO 0x037
109#define ZR060_SGR_HTOTAL_HI 0x038
110#define ZR060_SGR_HTOTAL_LO 0x039
111#define ZR060_SGR_VSYNC 0x03a
112#define ZR060_SGR_HSYNC 0x03b
113#define ZR060_SGR_BVSTART 0x03c
114#define ZR060_SGR_BHSTART 0x03d
115#define ZR060_SGR_BVEND_HI 0x03e
116#define ZR060_SGR_BVEND_LO 0x03f
117#define ZR060_SGR_BHEND_HI 0x040
118#define ZR060_SGR_BHEND_LO 0x041
119#define ZR060_AAR_VSTART_HI 0x042
120#define ZR060_AAR_VSTART_LO 0x043
121#define ZR060_AAR_VEND_HI 0x044
122#define ZR060_AAR_VEND_LO 0x045
123#define ZR060_AAR_HSTART_HI 0x046
124#define ZR060_AAR_HSTART_LO 0x047
125#define ZR060_AAR_HEND_HI 0x048
126#define ZR060_AAR_HEND_LO 0x049
127#define ZR060_SWR_VSTART_HI 0x04a
128#define ZR060_SWR_VSTART_LO 0x04b
129#define ZR060_SWR_VEND_HI 0x04c
130#define ZR060_SWR_VEND_LO 0x04d
131#define ZR060_SWR_HSTART_HI 0x04e
132#define ZR060_SWR_HSTART_LO 0x04f
133#define ZR060_SWR_HEND_HI 0x050
134#define ZR060_SWR_HEND_LO 0x051
135
136#define ZR060_SOF_IDX 0x060
137#define ZR060_SOS_IDX 0x07a
138#define ZR060_DRI_IDX 0x0c0
139#define ZR060_DQT_IDX 0x0cc
140#define ZR060_DHT_IDX 0x1d4
141#define ZR060_APP_IDX 0x380
142#define ZR060_COM_IDX 0x3c0
143
144/* ZR36060 LOAD register bits */
145
146#define ZR060_LOAD_Load (1 << 7)
147#define ZR060_LOAD_SyncRst (1 << 0)
148
149/* ZR36060 Code FIFO Status register bits */
150
151#define ZR060_CFSR_Busy (1 << 7)
152#define ZR060_CFSR_CBusy (1 << 2)
153#define ZR060_CFSR_CFIFO (3 << 0)
154
155/* ZR36060 Code Interface register */
156
157#define ZR060_CIR_Code16 (1 << 7)
158#define ZR060_CIR_Endian (1 << 6)
159#define ZR060_CIR_CFIS (1 << 2)
160#define ZR060_CIR_CodeMstr (1 << 0)
161
162/* ZR36060 Codec Mode register */
163
164#define ZR060_CMR_Comp (1 << 7)
165#define ZR060_CMR_ATP (1 << 6)
166#define ZR060_CMR_Pass2 (1 << 5)
167#define ZR060_CMR_TLM (1 << 4)
168#define ZR060_CMR_BRB (1 << 2)
169#define ZR060_CMR_FSF (1 << 1)
170
171/* ZR36060 Markers Enable register */
172
173#define ZR060_MER_App (1 << 7)
174#define ZR060_MER_Com (1 << 6)
175#define ZR060_MER_DRI (1 << 5)
176#define ZR060_MER_DQT (1 << 4)
177#define ZR060_MER_DHT (1 << 3)
178
179/* ZR36060 Interrupt Mask register */
180
181#define ZR060_IMR_EOAV (1 << 3)
182#define ZR060_IMR_EOI (1 << 2)
183#define ZR060_IMR_End (1 << 1)
184#define ZR060_IMR_DataErr (1 << 0)
185
186/* ZR36060 Interrupt Status register */
187
188#define ZR060_ISR_ProCnt (3 << 6)
189#define ZR060_ISR_EOAV (1 << 3)
190#define ZR060_ISR_EOI (1 << 2)
191#define ZR060_ISR_End (1 << 1)
192#define ZR060_ISR_DataErr (1 << 0)
193
194/* ZR36060 Video Control register */
195
196#define ZR060_VCR_Video8 (1 << 7)
197#define ZR060_VCR_Range (1 << 6)
198#define ZR060_VCR_FIDet (1 << 3)
199#define ZR060_VCR_FIVedge (1 << 2)
200#define ZR060_VCR_FIExt (1 << 1)
201#define ZR060_VCR_SyncMstr (1 << 0)
202
203/* ZR36060 Video Polarity register */
204
205#define ZR060_VPR_VCLKPol (1 << 7)
206#define ZR060_VPR_PValPol (1 << 6)
207#define ZR060_VPR_PoePol (1 << 5)
208#define ZR060_VPR_SImgPol (1 << 4)
209#define ZR060_VPR_BLPol (1 << 3)
210#define ZR060_VPR_FIPol (1 << 2)
211#define ZR060_VPR_HSPol (1 << 1)
212#define ZR060_VPR_VSPol (1 << 0)
213
214/* ZR36060 Scaling register */
215
216#define ZR060_SR_VScale (1 << 2)
217#define ZR060_SR_HScale2 (1 << 0)
218#define ZR060_SR_HScale4 (2 << 0)
219
220#endif /*fndef ZR36060_H */